aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/char
diff options
context:
space:
mode:
authorGlenn Elliott <gelliott@cs.unc.edu>2012-03-04 19:47:13 -0500
committerGlenn Elliott <gelliott@cs.unc.edu>2012-03-04 19:47:13 -0500
commitc71c03bda1e86c9d5198c5d83f712e695c4f2a1e (patch)
treeecb166cb3e2b7e2adb3b5e292245fefd23381ac8 /drivers/char
parentea53c912f8a86a8567697115b6a0d8152beee5c8 (diff)
parent6a00f206debf8a5c8899055726ad127dbeeed098 (diff)
Merge branch 'mpi-master' into wip-k-fmlpwip-k-fmlp
Conflicts: litmus/sched_cedf.c
Diffstat (limited to 'drivers/char')
-rw-r--r--drivers/char/.gitignore2
-rw-r--r--drivers/char/Kconfig559
-rw-r--r--drivers/char/Makefile82
-rw-r--r--drivers/char/agp/Kconfig6
-rw-r--r--drivers/char/agp/Makefile1
-rw-r--r--drivers/char/agp/agp.h8
-rw-r--r--drivers/char/agp/amd-k7-agp.c27
-rw-r--r--drivers/char/agp/amd64-agp.c53
-rw-r--r--drivers/char/agp/backend.c22
-rw-r--r--drivers/char/agp/compat_ioctl.c1
-rw-r--r--drivers/char/agp/compat_ioctl.h1
-rw-r--r--drivers/char/agp/frontend.c9
-rw-r--r--drivers/char/agp/generic.c58
-rw-r--r--drivers/char/agp/i460-agp.c2
-rw-r--r--drivers/char/agp/intel-agp.c236
-rw-r--r--drivers/char/agp/intel-agp.h71
-rw-r--r--drivers/char/agp/intel-gtt.c2085
-rw-r--r--drivers/char/agp/parisc-agp.c5
-rw-r--r--drivers/char/agp/sworks-agp.c2
-rw-r--r--drivers/char/agp/uninorth-agp.c2
-rw-r--r--drivers/char/agp/via-agp.c2
-rw-r--r--drivers/char/amiserial.c2174
-rw-r--r--drivers/char/apm-emulation.c11
-rw-r--r--drivers/char/applicom.c10
-rw-r--r--drivers/char/bfin-otp.c1
-rw-r--r--drivers/char/bfin_jtag_comm.c366
-rw-r--r--drivers/char/briq_panel.c2
-rw-r--r--drivers/char/bsr.c3
-rw-r--r--drivers/char/cd1865.h263
-rw-r--r--drivers/char/consolemap.c745
-rw-r--r--drivers/char/cp437.uni291
-rw-r--r--drivers/char/cs5535_gpio.c258
-rw-r--r--drivers/char/cyclades.c4197
-rw-r--r--drivers/char/defkeymap.c_shipped262
-rw-r--r--drivers/char/defkeymap.map357
-rw-r--r--drivers/char/digi1.h100
-rw-r--r--drivers/char/digiFep1.h136
-rw-r--r--drivers/char/digiPCI.h42
-rw-r--r--drivers/char/ds1302.c16
-rw-r--r--drivers/char/ds1620.c9
-rw-r--r--drivers/char/dsp56k.c28
-rw-r--r--drivers/char/dtlk.c9
-rw-r--r--drivers/char/epca.c2784
-rw-r--r--drivers/char/epca.h158
-rw-r--r--drivers/char/epcaconfig.h7
-rw-r--r--drivers/char/generic_nvram.c7
-rw-r--r--drivers/char/generic_serial.c844
-rw-r--r--drivers/char/genrtc.c14
-rw-r--r--drivers/char/hpet.c164
-rw-r--r--drivers/char/hvc_beat.c134
-rw-r--r--drivers/char/hvc_console.c915
-rw-r--r--drivers/char/hvc_console.h119
-rw-r--r--drivers/char/hvc_irq.c49
-rw-r--r--drivers/char/hvc_iseries.c598
-rw-r--r--drivers/char/hvc_iucv.c1339
-rw-r--r--drivers/char/hvc_rtas.c133
-rw-r--r--drivers/char/hvc_tile.c67
-rw-r--r--drivers/char/hvc_udbg.c96
-rw-r--r--drivers/char/hvc_vio.c173
-rw-r--r--drivers/char/hvc_xen.c238
-rw-r--r--drivers/char/hvcs.c1604
-rw-r--r--drivers/char/hvsi.c1314
-rw-r--r--drivers/char/hw_random/Kconfig14
-rw-r--r--drivers/char/hw_random/Makefile1
-rw-r--r--drivers/char/hw_random/amd-rng.c9
-rw-r--r--drivers/char/hw_random/core.c2
-rw-r--r--drivers/char/hw_random/n2-drv.c19
-rw-r--r--drivers/char/hw_random/nomadik-rng.c2
-rw-r--r--drivers/char/hw_random/omap-rng.c14
-rw-r--r--drivers/char/hw_random/pasemi-rng.c9
-rw-r--r--drivers/char/hw_random/picoxcell-rng.c208
-rw-r--r--drivers/char/hw_random/via-rng.c10
-rw-r--r--drivers/char/i8k.c180
-rw-r--r--drivers/char/ip2/Makefile8
-rw-r--r--drivers/char/ip2/i2cmd.c210
-rw-r--r--drivers/char/ip2/i2cmd.h630
-rw-r--r--drivers/char/ip2/i2ellis.c1403
-rw-r--r--drivers/char/ip2/i2ellis.h566
-rw-r--r--drivers/char/ip2/i2hw.h652
-rw-r--r--drivers/char/ip2/i2lib.c2214
-rw-r--r--drivers/char/ip2/i2lib.h351
-rw-r--r--drivers/char/ip2/i2pack.h364
-rw-r--r--drivers/char/ip2/ip2.h107
-rw-r--r--drivers/char/ip2/ip2ioctl.h35
-rw-r--r--drivers/char/ip2/ip2main.c3219
-rw-r--r--drivers/char/ip2/ip2trace.h42
-rw-r--r--drivers/char/ip2/ip2types.h57
-rw-r--r--drivers/char/ipmi/Makefile2
-rw-r--r--drivers/char/ipmi/ipmi_bt_sm.c2
-rw-r--r--drivers/char/ipmi/ipmi_devintf.c19
-rw-r--r--drivers/char/ipmi/ipmi_msghandler.c169
-rw-r--r--drivers/char/ipmi/ipmi_poweroff.c2
-rw-r--r--drivers/char/ipmi/ipmi_si_intf.c261
-rw-r--r--drivers/char/ipmi/ipmi_watchdog.c11
-rw-r--r--drivers/char/isicom.c1736
-rw-r--r--drivers/char/istallion.c4507
-rw-r--r--drivers/char/keyboard.c1437
-rw-r--r--drivers/char/lp.c16
-rw-r--r--drivers/char/mbcs.c8
-rw-r--r--drivers/char/mbcs.h4
-rw-r--r--drivers/char/mem.c54
-rw-r--r--drivers/char/misc.c1
-rw-r--r--drivers/char/mmtimer.c98
-rw-r--r--drivers/char/moxa.c2092
-rw-r--r--drivers/char/moxa.h304
-rw-r--r--drivers/char/msm_smd_pkt.c466
-rw-r--r--drivers/char/mspec.c14
-rw-r--r--drivers/char/mwave/3780i.h2
-rw-r--r--drivers/char/mwave/Makefile6
-rw-r--r--drivers/char/mwave/README2
-rw-r--r--drivers/char/mwave/mwavedd.c47
-rw-r--r--drivers/char/mxser.c2748
-rw-r--r--drivers/char/mxser.h150
-rw-r--r--drivers/char/n_gsm.c2763
-rw-r--r--drivers/char/n_hdlc.c1007
-rw-r--r--drivers/char/n_r3964.c1265
-rw-r--r--drivers/char/n_tty.c2121
-rw-r--r--drivers/char/nozomi.c1995
-rw-r--r--drivers/char/nvram.c11
-rw-r--r--drivers/char/nwbutton.c3
-rw-r--r--drivers/char/nwflash.c12
-rw-r--r--drivers/char/pc8736x_gpio.c1
-rw-r--r--drivers/char/pcmcia/Kconfig4
-rw-r--r--drivers/char/pcmcia/Makefile2
-rw-r--r--drivers/char/pcmcia/cm4000_cs.c54
-rw-r--r--drivers/char/pcmcia/cm4040_cs.c49
-rw-r--r--drivers/char/pcmcia/ipwireless/Makefile10
-rw-r--r--drivers/char/pcmcia/ipwireless/hardware.c1764
-rw-r--r--drivers/char/pcmcia/ipwireless/hardware.h62
-rw-r--r--drivers/char/pcmcia/ipwireless/main.c370
-rw-r--r--drivers/char/pcmcia/ipwireless/main.h73
-rw-r--r--drivers/char/pcmcia/ipwireless/network.c507
-rw-r--r--drivers/char/pcmcia/ipwireless/network.h53
-rw-r--r--drivers/char/pcmcia/ipwireless/setup_protocol.h108
-rw-r--r--drivers/char/pcmcia/ipwireless/tty.c679
-rw-r--r--drivers/char/pcmcia/ipwireless/tty.h46
-rw-r--r--drivers/char/pcmcia/synclink_cs.c126
-rw-r--r--drivers/char/ppdev.c10
-rw-r--r--drivers/char/pty.c775
-rw-r--r--drivers/char/ramoops.c51
-rw-r--r--drivers/char/random.c19
-rw-r--r--drivers/char/raw.c291
-rw-r--r--drivers/char/rio/Makefile12
-rw-r--r--drivers/char/rio/board.h132
-rw-r--r--drivers/char/rio/cirrus.h210
-rw-r--r--drivers/char/rio/cmdblk.h53
-rw-r--r--drivers/char/rio/cmdpkt.h177
-rw-r--r--drivers/char/rio/daemon.h307
-rw-r--r--drivers/char/rio/errors.h98
-rw-r--r--drivers/char/rio/func.h143
-rw-r--r--drivers/char/rio/host.h123
-rw-r--r--drivers/char/rio/link.h96
-rw-r--r--drivers/char/rio/linux_compat.h77
-rw-r--r--drivers/char/rio/map.h98
-rw-r--r--drivers/char/rio/param.h55
-rw-r--r--drivers/char/rio/parmmap.h81
-rw-r--r--drivers/char/rio/pci.h72
-rw-r--r--drivers/char/rio/phb.h142
-rw-r--r--drivers/char/rio/pkt.h77
-rw-r--r--drivers/char/rio/port.h179
-rw-r--r--drivers/char/rio/protsts.h110
-rw-r--r--drivers/char/rio/rio.h208
-rw-r--r--drivers/char/rio/rio_linux.c1202
-rw-r--r--drivers/char/rio/rio_linux.h197
-rw-r--r--drivers/char/rio/rioboard.h275
-rw-r--r--drivers/char/rio/rioboot.c1113
-rw-r--r--drivers/char/rio/riocmd.c939
-rw-r--r--drivers/char/rio/rioctrl.c1504
-rw-r--r--drivers/char/rio/riodrvr.h138
-rw-r--r--drivers/char/rio/rioinfo.h92
-rw-r--r--drivers/char/rio/rioinit.c421
-rw-r--r--drivers/char/rio/riointr.c645
-rw-r--r--drivers/char/rio/rioioctl.h57
-rw-r--r--drivers/char/rio/rioparam.c663
-rw-r--r--drivers/char/rio/rioroute.c1039
-rw-r--r--drivers/char/rio/riospace.h154
-rw-r--r--drivers/char/rio/riotable.c941
-rw-r--r--drivers/char/rio/riotty.c654
-rw-r--r--drivers/char/rio/route.h101
-rw-r--r--drivers/char/rio/rup.h69
-rw-r--r--drivers/char/rio/unixrup.h51
-rw-r--r--drivers/char/riscom8.c1560
-rw-r--r--drivers/char/riscom8.h91
-rw-r--r--drivers/char/riscom8_reg.h254
-rw-r--r--drivers/char/rocket.c3196
-rw-r--r--drivers/char/rocket.h111
-rw-r--r--drivers/char/rocket_int.h1214
-rw-r--r--drivers/char/scx200_gpio.c1
-rw-r--r--drivers/char/selection.c348
-rw-r--r--drivers/char/ser_a2232.c831
-rw-r--r--drivers/char/ser_a2232.h202
-rw-r--r--drivers/char/ser_a2232fw.ax529
-rw-r--r--drivers/char/ser_a2232fw.h306
-rw-r--r--drivers/char/serial167.c2491
-rw-r--r--drivers/char/snsc.c10
-rw-r--r--drivers/char/snsc.h1
-rw-r--r--drivers/char/sonypi.c6
-rw-r--r--drivers/char/specialix.c2369
-rw-r--r--drivers/char/specialix_io8.h140
-rw-r--r--drivers/char/stallion.c4652
-rw-r--r--drivers/char/sx.c2894
-rw-r--r--drivers/char/sx.h201
-rw-r--r--drivers/char/sxboards.h206
-rw-r--r--drivers/char/sxwindow.h393
-rw-r--r--drivers/char/synclink.c8125
-rw-r--r--drivers/char/synclink_gt.c5049
-rw-r--r--drivers/char/synclinkmp.c5606
-rw-r--r--drivers/char/sysrq.c801
-rw-r--r--drivers/char/tb0219.c1
-rw-r--r--drivers/char/tlclk.c7
-rw-r--r--drivers/char/toshiba.c10
-rw-r--r--drivers/char/tpm/Kconfig2
-rw-r--r--drivers/char/tpm/tpm.c48
-rw-r--r--drivers/char/tpm/tpm.h5
-rw-r--r--drivers/char/tpm/tpm_infineon.c2
-rw-r--r--drivers/char/tpm/tpm_tis.c24
-rw-r--r--drivers/char/tty_audit.c340
-rw-r--r--drivers/char/tty_buffer.c524
-rw-r--r--drivers/char/tty_io.c3198
-rw-r--r--drivers/char/tty_ioctl.c1179
-rw-r--r--drivers/char/tty_ldisc.c915
-rw-r--r--drivers/char/tty_mutex.c47
-rw-r--r--drivers/char/tty_port.c446
-rw-r--r--drivers/char/ttyprintk.c225
-rw-r--r--drivers/char/uv_mmtimer.c2
-rw-r--r--drivers/char/vc_screen.c509
-rw-r--r--drivers/char/viotape.c12
-rw-r--r--drivers/char/virtio_console.c321
-rw-r--r--drivers/char/vme_scc.c1145
-rw-r--r--drivers/char/vt.c4204
-rw-r--r--drivers/char/vt_ioctl.c1787
-rw-r--r--drivers/char/xilinx_hwicap/xilinx_hwicap.c142
232 files changed, 3578 insertions, 127795 deletions
diff --git a/drivers/char/.gitignore b/drivers/char/.gitignore
deleted file mode 100644
index 83683a2d8e6a..000000000000
--- a/drivers/char/.gitignore
+++ /dev/null
@@ -1,2 +0,0 @@
1consolemap_deftbl.c
2defkeymap.c
diff --git a/drivers/char/Kconfig b/drivers/char/Kconfig
index 3d44ec724c17..49502bc5360a 100644
--- a/drivers/char/Kconfig
+++ b/drivers/char/Kconfig
@@ -4,89 +4,7 @@
4 4
5menu "Character devices" 5menu "Character devices"
6 6
7config VT 7source "drivers/tty/Kconfig"
8 bool "Virtual terminal" if EMBEDDED
9 depends on !S390
10 select INPUT
11 default y
12 ---help---
13 If you say Y here, you will get support for terminal devices with
14 display and keyboard devices. These are called "virtual" because you
15 can run several virtual terminals (also called virtual consoles) on
16 one physical terminal. This is rather useful, for example one
17 virtual terminal can collect system messages and warnings, another
18 one can be used for a text-mode user session, and a third could run
19 an X session, all in parallel. Switching between virtual terminals
20 is done with certain key combinations, usually Alt-<function key>.
21
22 The setterm command ("man setterm") can be used to change the
23 properties (such as colors or beeping) of a virtual terminal. The
24 man page console_codes(4) ("man console_codes") contains the special
25 character sequences that can be used to change those properties
26 directly. The fonts used on virtual terminals can be changed with
27 the setfont ("man setfont") command and the key bindings are defined
28 with the loadkeys ("man loadkeys") command.
29
30 You need at least one virtual terminal device in order to make use
31 of your keyboard and monitor. Therefore, only people configuring an
32 embedded system would want to say N here in order to save some
33 memory; the only way to log into such a system is then via a serial
34 or network connection.
35
36 If unsure, say Y, or else you won't be able to do much with your new
37 shiny Linux system :-)
38
39config CONSOLE_TRANSLATIONS
40 depends on VT
41 default y
42 bool "Enable character translations in console" if EMBEDDED
43 ---help---
44 This enables support for font mapping and Unicode translation
45 on virtual consoles.
46
47config VT_CONSOLE
48 bool "Support for console on virtual terminal" if EMBEDDED
49 depends on VT
50 default y
51 ---help---
52 The system console is the device which receives all kernel messages
53 and warnings and which allows logins in single user mode. If you
54 answer Y here, a virtual terminal (the device used to interact with
55 a physical terminal) can be used as system console. This is the most
56 common mode of operations, so you should say Y here unless you want
57 the kernel messages be output only to a serial port (in which case
58 you should say Y to "Console on serial port", below).
59
60 If you do say Y here, by default the currently visible virtual
61 terminal (/dev/tty0) will be used as system console. You can change
62 that with a kernel command line option such as "console=tty3" which
63 would use the third virtual terminal as system console. (Try "man
64 bootparam" or see the documentation of your boot loader (lilo or
65 loadlin) about how to pass options to the kernel at boot time.)
66
67 If unsure, say Y.
68
69config HW_CONSOLE
70 bool
71 depends on VT && !S390 && !UML
72 default y
73
74config VT_HW_CONSOLE_BINDING
75 bool "Support for binding and unbinding console drivers"
76 depends on HW_CONSOLE
77 default n
78 ---help---
79 The virtual terminal is the device that interacts with the physical
80 terminal through console drivers. On these systems, at least one
81 console driver is loaded. In other configurations, additional console
82 drivers may be enabled, such as the framebuffer console. If more than
83 1 console driver is enabled, setting this to 'y' will allow you to
84 select the console driver that will serve as the backend for the
85 virtual terminals.
86
87 See <file:Documentation/console/console.txt> for more
88 information. For framebuffer console users, please refer to
89 <file:Documentation/fb/fbcon.txt>.
90 8
91config DEVKMEM 9config DEVKMEM
92 bool "/dev/kmem virtual device support" 10 bool "/dev/kmem virtual device support"
@@ -97,253 +15,6 @@ config DEVKMEM
97 kind of kernel debugging operations. 15 kind of kernel debugging operations.
98 When in doubt, say "N". 16 When in doubt, say "N".
99 17
100config BFIN_JTAG_COMM
101 tristate "Blackfin JTAG Communication"
102 depends on BLACKFIN
103 help
104 Add support for emulating a TTY device over the Blackfin JTAG.
105
106 To compile this driver as a module, choose M here: the
107 module will be called bfin_jtag_comm.
108
109config BFIN_JTAG_COMM_CONSOLE
110 bool "Console on Blackfin JTAG"
111 depends on BFIN_JTAG_COMM=y
112
113config SERIAL_NONSTANDARD
114 bool "Non-standard serial port support"
115 depends on HAS_IOMEM
116 ---help---
117 Say Y here if you have any non-standard serial boards -- boards
118 which aren't supported using the standard "dumb" serial driver.
119 This includes intelligent serial boards such as Cyclades,
120 Digiboards, etc. These are usually used for systems that need many
121 serial ports because they serve many terminals or dial-in
122 connections.
123
124 Note that the answer to this question won't directly affect the
125 kernel: saying N will just cause the configurator to skip all
126 the questions about non-standard serial boards.
127
128 Most people can say N here.
129
130config COMPUTONE
131 tristate "Computone IntelliPort Plus serial support"
132 depends on SERIAL_NONSTANDARD && (ISA || EISA || PCI)
133 ---help---
134 This driver supports the entire family of Intelliport II/Plus
135 controllers with the exception of the MicroChannel controllers and
136 products previous to the Intelliport II. These are multiport cards,
137 which give you many serial ports. You would need something like this
138 to connect more than two modems to your Linux box, for instance in
139 order to become a dial-in server. If you have a card like that, say
140 Y here and read <file:Documentation/serial/computone.txt>.
141
142 To compile this driver as module, choose M here: the
143 module will be called ip2.
144
145config ROCKETPORT
146 tristate "Comtrol RocketPort support"
147 depends on SERIAL_NONSTANDARD && (ISA || EISA || PCI)
148 help
149 This driver supports Comtrol RocketPort and RocketModem PCI boards.
150 These boards provide 2, 4, 8, 16, or 32 high-speed serial ports or
151 modems. For information about the RocketPort/RocketModem boards
152 and this driver read <file:Documentation/serial/rocket.txt>.
153
154 To compile this driver as a module, choose M here: the
155 module will be called rocket.
156
157 If you want to compile this driver into the kernel, say Y here. If
158 you don't have a Comtrol RocketPort/RocketModem card installed, say N.
159
160config CYCLADES
161 tristate "Cyclades async mux support"
162 depends on SERIAL_NONSTANDARD && (PCI || ISA)
163 select FW_LOADER
164 ---help---
165 This driver supports Cyclades Z and Y multiserial boards.
166 You would need something like this to connect more than two modems to
167 your Linux box, for instance in order to become a dial-in server.
168
169 For information about the Cyclades-Z card, read
170 <file:Documentation/serial/README.cycladesZ>.
171
172 To compile this driver as a module, choose M here: the
173 module will be called cyclades.
174
175 If you haven't heard about it, it's safe to say N.
176
177config CYZ_INTR
178 bool "Cyclades-Z interrupt mode operation (EXPERIMENTAL)"
179 depends on EXPERIMENTAL && CYCLADES
180 help
181 The Cyclades-Z family of multiport cards allows 2 (two) driver op
182 modes: polling and interrupt. In polling mode, the driver will check
183 the status of the Cyclades-Z ports every certain amount of time
184 (which is called polling cycle and is configurable). In interrupt
185 mode, it will use an interrupt line (IRQ) in order to check the
186 status of the Cyclades-Z ports. The default op mode is polling. If
187 unsure, say N.
188
189config DIGIEPCA
190 tristate "Digiboard Intelligent Async Support"
191 depends on SERIAL_NONSTANDARD && (ISA || EISA || PCI)
192 ---help---
193 This is a driver for Digi International's Xx, Xeve, and Xem series
194 of cards which provide multiple serial ports. You would need
195 something like this to connect more than two modems to your Linux
196 box, for instance in order to become a dial-in server. This driver
197 supports the original PC (ISA) boards as well as PCI, and EISA. If
198 you have a card like this, say Y here and read the file
199 <file:Documentation/serial/digiepca.txt>.
200
201 To compile this driver as a module, choose M here: the
202 module will be called epca.
203
204config MOXA_INTELLIO
205 tristate "Moxa Intellio support"
206 depends on SERIAL_NONSTANDARD && (ISA || EISA || PCI)
207 select FW_LOADER
208 help
209 Say Y here if you have a Moxa Intellio multiport serial card.
210
211 To compile this driver as a module, choose M here: the
212 module will be called moxa.
213
214config MOXA_SMARTIO
215 tristate "Moxa SmartIO support v. 2.0"
216 depends on SERIAL_NONSTANDARD && (PCI || EISA || ISA)
217 help
218 Say Y here if you have a Moxa SmartIO multiport serial card and/or
219 want to help develop a new version of this driver.
220
221 This is upgraded (1.9.1) driver from original Moxa drivers with
222 changes finally resulting in PCI probing.
223
224 This driver can also be built as a module. The module will be called
225 mxser. If you want to do that, say M here.
226
227config ISI
228 tristate "Multi-Tech multiport card support (EXPERIMENTAL)"
229 depends on SERIAL_NONSTANDARD && PCI
230 select FW_LOADER
231 help
232 This is a driver for the Multi-Tech cards which provide several
233 serial ports. The driver is experimental and can currently only be
234 built as a module. The module will be called isicom.
235 If you want to do that, choose M here.
236
237config SYNCLINK
238 tristate "Microgate SyncLink card support"
239 depends on SERIAL_NONSTANDARD && PCI && ISA_DMA_API
240 help
241 Provides support for the SyncLink ISA and PCI multiprotocol serial
242 adapters. These adapters support asynchronous and HDLC bit
243 synchronous communication up to 10Mbps (PCI adapter).
244
245 This driver can only be built as a module ( = code which can be
246 inserted in and removed from the running kernel whenever you want).
247 The module will be called synclink. If you want to do that, say M
248 here.
249
250config SYNCLINKMP
251 tristate "SyncLink Multiport support"
252 depends on SERIAL_NONSTANDARD && PCI
253 help
254 Enable support for the SyncLink Multiport (2 or 4 ports)
255 serial adapter, running asynchronous and HDLC communications up
256 to 2.048Mbps. Each ports is independently selectable for
257 RS-232, V.35, RS-449, RS-530, and X.21
258
259 This driver may be built as a module ( = code which can be
260 inserted in and removed from the running kernel whenever you want).
261 The module will be called synclinkmp. If you want to do that, say M
262 here.
263
264config SYNCLINK_GT
265 tristate "SyncLink GT/AC support"
266 depends on SERIAL_NONSTANDARD && PCI
267 help
268 Support for SyncLink GT and SyncLink AC families of
269 synchronous and asynchronous serial adapters
270 manufactured by Microgate Systems, Ltd. (www.microgate.com)
271
272config N_HDLC
273 tristate "HDLC line discipline support"
274 depends on SERIAL_NONSTANDARD
275 help
276 Allows synchronous HDLC communications with tty device drivers that
277 support synchronous HDLC such as the Microgate SyncLink adapter.
278
279 This driver can be built as a module ( = code which can be
280 inserted in and removed from the running kernel whenever you want).
281 The module will be called n_hdlc. If you want to do that, say M
282 here.
283
284config N_GSM
285 tristate "GSM MUX line discipline support (EXPERIMENTAL)"
286 depends on EXPERIMENTAL
287 depends on NET
288 help
289 This line discipline provides support for the GSM MUX protocol and
290 presents the mux as a set of 61 individual tty devices.
291
292config RISCOM8
293 tristate "SDL RISCom/8 card support"
294 depends on SERIAL_NONSTANDARD
295 help
296 This is a driver for the SDL Communications RISCom/8 multiport card,
297 which gives you many serial ports. You would need something like
298 this to connect more than two modems to your Linux box, for instance
299 in order to become a dial-in server. If you have a card like that,
300 say Y here and read the file <file:Documentation/serial/riscom8.txt>.
301
302 Also it's possible to say M here and compile this driver as kernel
303 loadable module; the module will be called riscom8.
304
305config SPECIALIX
306 tristate "Specialix IO8+ card support"
307 depends on SERIAL_NONSTANDARD
308 help
309 This is a driver for the Specialix IO8+ multiport card (both the
310 ISA and the PCI version) which gives you many serial ports. You
311 would need something like this to connect more than two modems to
312 your Linux box, for instance in order to become a dial-in server.
313
314 If you have a card like that, say Y here and read the file
315 <file:Documentation/serial/specialix.txt>. Also it's possible to say
316 M here and compile this driver as kernel loadable module which will be
317 called specialix.
318
319config SX
320 tristate "Specialix SX (and SI) card support"
321 depends on SERIAL_NONSTANDARD && (PCI || EISA || ISA) && BROKEN
322 help
323 This is a driver for the SX and SI multiport serial cards.
324 Please read the file <file:Documentation/serial/sx.txt> for details.
325
326 This driver can only be built as a module ( = code which can be
327 inserted in and removed from the running kernel whenever you want).
328 The module will be called sx. If you want to do that, say M here.
329
330config RIO
331 tristate "Specialix RIO system support"
332 depends on SERIAL_NONSTANDARD && BROKEN
333 help
334 This is a driver for the Specialix RIO, a smart serial card which
335 drives an outboard box that can support up to 128 ports. Product
336 information is at <http://www.perle.com/support/documentation.html#multiport>.
337 There are both ISA and PCI versions.
338
339config RIO_OLDPCI
340 bool "Support really old RIO/PCI cards"
341 depends on RIO
342 help
343 Older RIO PCI cards need some initialization-time configuration to
344 determine the IRQ and some control addresses. If you have a RIO and
345 this doesn't seem to work, try setting this to Y.
346
347config STALDRV 18config STALDRV
348 bool "Stallion multiport serial support" 19 bool "Stallion multiport serial support"
349 depends on SERIAL_NONSTANDARD 20 depends on SERIAL_NONSTANDARD
@@ -356,54 +27,6 @@ config STALDRV
356 in this case. If you have never heard about all this, it's safe to 27 in this case. If you have never heard about all this, it's safe to
357 say N. 28 say N.
358 29
359config STALLION
360 tristate "Stallion EasyIO or EC8/32 support"
361 depends on STALDRV && (ISA || EISA || PCI)
362 help
363 If you have an EasyIO or EasyConnection 8/32 multiport Stallion
364 card, then this is for you; say Y. Make sure to read
365 <file:Documentation/serial/stallion.txt>.
366
367 To compile this driver as a module, choose M here: the
368 module will be called stallion.
369
370config ISTALLION
371 tristate "Stallion EC8/64, ONboard, Brumby support"
372 depends on STALDRV && (ISA || EISA || PCI)
373 help
374 If you have an EasyConnection 8/64, ONboard, Brumby or Stallion
375 serial multiport card, say Y here. Make sure to read
376 <file:Documentation/serial/stallion.txt>.
377
378 To compile this driver as a module, choose M here: the
379 module will be called istallion.
380
381config NOZOMI
382 tristate "HSDPA Broadband Wireless Data Card - Globe Trotter"
383 depends on PCI && EXPERIMENTAL
384 help
385 If you have a HSDPA driver Broadband Wireless Data Card -
386 Globe Trotter PCMCIA card, say Y here.
387
388 To compile this driver as a module, choose M here, the module
389 will be called nozomi.
390
391config A2232
392 tristate "Commodore A2232 serial support (EXPERIMENTAL)"
393 depends on EXPERIMENTAL && ZORRO && BROKEN
394 ---help---
395 This option supports the 2232 7-port serial card shipped with the
396 Amiga 2000 and other Zorro-bus machines, dating from 1989. At
397 a max of 19,200 bps, the ports are served by a 6551 ACIA UART chip
398 each, plus a 8520 CIA, and a master 6502 CPU and buffer as well. The
399 ports were connected with 8 pin DIN connectors on the card bracket,
400 for which 8 pin to DB25 adapters were supplied. The card also had
401 jumpers internally to toggle various pinning configurations.
402
403 This driver can be built as a module; but then "generic_serial"
404 will also be built as a module. This has to be loaded before
405 "ser_a2232". If you want to do this, answer M here.
406
407config SGI_SNSC 30config SGI_SNSC
408 bool "SGI Altix system controller communication support" 31 bool "SGI Altix system controller communication support"
409 depends on (IA64_SGI_SN2 || IA64_GENERIC) 32 depends on (IA64_SGI_SN2 || IA64_GENERIC)
@@ -426,72 +49,22 @@ config SGI_MBCS
426 If you have an SGI Altix with an attached SABrick 49 If you have an SGI Altix with an attached SABrick
427 say Y or M here, otherwise say N. 50 say Y or M here, otherwise say N.
428 51
429source "drivers/serial/Kconfig" 52source "drivers/tty/serial/Kconfig"
430 53
431config UNIX98_PTYS 54config TTY_PRINTK
432 bool "Unix98 PTY support" if EMBEDDED 55 bool "TTY driver to output user messages via printk"
433 default y 56 depends on EXPERT
434 ---help---
435 A pseudo terminal (PTY) is a software device consisting of two
436 halves: a master and a slave. The slave device behaves identical to
437 a physical terminal; the master device is used by a process to
438 read data from and write data to the slave, thereby emulating a
439 terminal. Typical programs for the master side are telnet servers
440 and xterms.
441
442 Linux has traditionally used the BSD-like names /dev/ptyxx for
443 masters and /dev/ttyxx for slaves of pseudo terminals. This scheme
444 has a number of problems. The GNU C library glibc 2.1 and later,
445 however, supports the Unix98 naming standard: in order to acquire a
446 pseudo terminal, a process opens /dev/ptmx; the number of the pseudo
447 terminal is then made available to the process and the pseudo
448 terminal slave can be accessed as /dev/pts/<number>. What was
449 traditionally /dev/ttyp2 will then be /dev/pts/2, for example.
450
451 All modern Linux systems use the Unix98 ptys. Say Y unless
452 you're on an embedded system and want to conserve memory.
453
454config DEVPTS_MULTIPLE_INSTANCES
455 bool "Support multiple instances of devpts"
456 depends on UNIX98_PTYS
457 default n 57 default n
458 ---help--- 58 ---help---
459 Enable support for multiple instances of devpts filesystem. 59 If you say Y here, the support for writing user messages (i.e.
460 If you want to have isolated PTY namespaces (eg: in containers), 60 console messages) via printk is available.
461 say Y here. Otherwise, say N. If enabled, each mount of devpts 61
462 filesystem with the '-o newinstance' option will create an 62 The feature is useful to inline user messages with kernel
463 independent PTY namespace. 63 messages.
464 64 In order to use this feature, you should output user messages
465config LEGACY_PTYS 65 to /dev/ttyprintk or redirect console to this TTY.
466 bool "Legacy (BSD) PTY support"
467 default y
468 ---help---
469 A pseudo terminal (PTY) is a software device consisting of two
470 halves: a master and a slave. The slave device behaves identical to
471 a physical terminal; the master device is used by a process to
472 read data from and write data to the slave, thereby emulating a
473 terminal. Typical programs for the master side are telnet servers
474 and xterms.
475
476 Linux has traditionally used the BSD-like names /dev/ptyxx
477 for masters and /dev/ttyxx for slaves of pseudo
478 terminals. This scheme has a number of problems, including
479 security. This option enables these legacy devices; on most
480 systems, it is safe to say N.
481
482
483config LEGACY_PTY_COUNT
484 int "Maximum number of legacy PTY in use"
485 depends on LEGACY_PTYS
486 range 0 256
487 default "256"
488 ---help---
489 The maximum number of legacy PTYs that can be used at any one time.
490 The default is 256, and should be more than enough. Embedded
491 systems may want to reduce this to save memory.
492 66
493 When not in use, each legacy PTY occupies 12 bytes on 32-bit 67 If unsure, say N.
494 architectures and 24 bytes on 64-bit architectures.
495 68
496config BRIQ_PANEL 69config BRIQ_PANEL
497 tristate 'Total Impact briQ front panel driver' 70 tristate 'Total Impact briQ front panel driver'
@@ -597,75 +170,7 @@ config PPDEV
597 170
598 If unsure, say N. 171 If unsure, say N.
599 172
600config HVC_DRIVER 173source "drivers/tty/hvc/Kconfig"
601 bool
602 help
603 Generic "hypervisor virtual console" infrastructure for various
604 hypervisors (pSeries, iSeries, Xen, lguest).
605 It will automatically be selected if one of the back-end console drivers
606 is selected.
607
608config HVC_IRQ
609 bool
610
611config HVC_CONSOLE
612 bool "pSeries Hypervisor Virtual Console support"
613 depends on PPC_PSERIES
614 select HVC_DRIVER
615 select HVC_IRQ
616 help
617 pSeries machines when partitioned support a hypervisor virtual
618 console. This driver allows each pSeries partition to have a console
619 which is accessed via the HMC.
620
621config HVC_ISERIES
622 bool "iSeries Hypervisor Virtual Console support"
623 depends on PPC_ISERIES
624 default y
625 select HVC_DRIVER
626 select HVC_IRQ
627 select VIOPATH
628 help
629 iSeries machines support a hypervisor virtual console.
630
631config HVC_RTAS
632 bool "IBM RTAS Console support"
633 depends on PPC_RTAS
634 select HVC_DRIVER
635 help
636 IBM Console device driver which makes use of RTAS
637
638config HVC_BEAT
639 bool "Toshiba's Beat Hypervisor Console support"
640 depends on PPC_CELLEB
641 select HVC_DRIVER
642 help
643 Toshiba's Cell Reference Set Beat Console device driver
644
645config HVC_IUCV
646 bool "z/VM IUCV Hypervisor console support (VM only)"
647 depends on S390
648 select HVC_DRIVER
649 select IUCV
650 default y
651 help
652 This driver provides a Hypervisor console (HVC) back-end to access
653 a Linux (console) terminal via a z/VM IUCV communication path.
654
655config HVC_XEN
656 bool "Xen Hypervisor Console support"
657 depends on XEN
658 select HVC_DRIVER
659 select HVC_IRQ
660 default y
661 help
662 Xen virtual console device driver
663
664config HVC_UDBG
665 bool "udbg based fake hypervisor console"
666 depends on PPC && EXPERIMENTAL
667 select HVC_DRIVER
668 default n
669 174
670config VIRTIO_CONSOLE 175config VIRTIO_CONSOLE
671 tristate "Virtio console" 176 tristate "Virtio console"
@@ -683,23 +188,6 @@ config VIRTIO_CONSOLE
683 the port which can be used by udev scripts to create a 188 the port which can be used by udev scripts to create a
684 symlink to the device. 189 symlink to the device.
685 190
686config HVCS
687 tristate "IBM Hypervisor Virtual Console Server support"
688 depends on PPC_PSERIES && HVC_CONSOLE
689 help
690 Partitionable IBM Power5 ppc64 machines allow hosting of
691 firmware virtual consoles from one Linux partition by
692 another Linux partition. This driver allows console data
693 from Linux partitions to be accessed through TTY device
694 interfaces in the device tree of a Linux partition running
695 this driver.
696
697 To compile this driver as a module, choose M here: the
698 module will be called hvcs. Additionally, this module
699 will depend on arch specific APIs exported from hvcserver.ko
700 which will also be compiled when this driver is built as a
701 module.
702
703config IBM_BSR 191config IBM_BSR
704 tristate "IBM POWER Barrier Synchronization Register support" 192 tristate "IBM POWER Barrier Synchronization Register support"
705 depends on PPC_PSERIES 193 depends on PPC_PSERIES
@@ -1023,15 +511,6 @@ config NSC_GPIO
1023 pc8736x_gpio drivers. If those drivers are built as 511 pc8736x_gpio drivers. If those drivers are built as
1024 modules, this one will be too, named nsc_gpio 512 modules, this one will be too, named nsc_gpio
1025 513
1026config CS5535_GPIO
1027 tristate "AMD CS5535/CS5536 GPIO (Geode Companion Device)"
1028 depends on X86_32
1029 help
1030 Give userspace access to the GPIO pins on the AMD CS5535 and
1031 CS5536 Geode companion devices.
1032
1033 If compiled as a module, it will be called cs5535_gpio.
1034
1035config RAW_DRIVER 514config RAW_DRIVER
1036 tristate "RAW driver (/dev/raw/rawN)" 515 tristate "RAW driver (/dev/raw/rawN)"
1037 depends on BLOCK 516 depends on BLOCK
@@ -1044,7 +523,7 @@ config RAW_DRIVER
1044 with the O_DIRECT flag. 523 with the O_DIRECT flag.
1045 524
1046config MAX_RAW_DEVS 525config MAX_RAW_DEVS
1047 int "Maximum number of RAW devices to support (1-8192)" 526 int "Maximum number of RAW devices to support (1-65536)"
1048 depends on RAW_DRIVER 527 depends on RAW_DRIVER
1049 default "256" 528 default "256"
1050 help 529 help
@@ -1129,5 +608,13 @@ config RAMOOPS
1129 This enables panic and oops messages to be logged to a circular 608 This enables panic and oops messages to be logged to a circular
1130 buffer in RAM where it can be read back at some later point. 609 buffer in RAM where it can be read back at some later point.
1131 610
611config MSM_SMD_PKT
612 bool "Enable device interface for some SMD packet ports"
613 default n
614 depends on MSM_SMD
615 help
616 Enables userspace clients to read and write to some packet SMD
617 ports via device interface for MSM chipset.
618
1132endmenu 619endmenu
1133 620
diff --git a/drivers/char/Makefile b/drivers/char/Makefile
index dc9641660605..7a00672bd85d 100644
--- a/drivers/char/Makefile
+++ b/drivers/char/Makefile
@@ -2,67 +2,18 @@
2# Makefile for the kernel character device drivers. 2# Makefile for the kernel character device drivers.
3# 3#
4 4
5# 5obj-y += mem.o random.o
6# This file contains the font map for the default (hardware) font 6obj-$(CONFIG_TTY_PRINTK) += ttyprintk.o
7#
8FONTMAPFILE = cp437.uni
9
10obj-y += mem.o random.o tty_io.o n_tty.o tty_ioctl.o tty_ldisc.o tty_buffer.o tty_port.o
11
12obj-y += tty_mutex.o
13obj-$(CONFIG_LEGACY_PTYS) += pty.o
14obj-$(CONFIG_UNIX98_PTYS) += pty.o
15obj-y += misc.o 7obj-y += misc.o
16obj-$(CONFIG_VT) += vt_ioctl.o vc_screen.o selection.o keyboard.o
17obj-$(CONFIG_BFIN_JTAG_COMM) += bfin_jtag_comm.o
18obj-$(CONFIG_CONSOLE_TRANSLATIONS) += consolemap.o consolemap_deftbl.o
19obj-$(CONFIG_HW_CONSOLE) += vt.o defkeymap.o
20obj-$(CONFIG_AUDIT) += tty_audit.o
21obj-$(CONFIG_MAGIC_SYSRQ) += sysrq.o
22obj-$(CONFIG_MVME147_SCC) += generic_serial.o vme_scc.o
23obj-$(CONFIG_MVME162_SCC) += generic_serial.o vme_scc.o
24obj-$(CONFIG_BVME6000_SCC) += generic_serial.o vme_scc.o
25obj-$(CONFIG_ROCKETPORT) += rocket.o
26obj-$(CONFIG_SERIAL167) += serial167.o
27obj-$(CONFIG_CYCLADES) += cyclades.o
28obj-$(CONFIG_STALLION) += stallion.o
29obj-$(CONFIG_ISTALLION) += istallion.o
30obj-$(CONFIG_NOZOMI) += nozomi.o
31obj-$(CONFIG_DIGIEPCA) += epca.o
32obj-$(CONFIG_SPECIALIX) += specialix.o
33obj-$(CONFIG_MOXA_INTELLIO) += moxa.o
34obj-$(CONFIG_A2232) += ser_a2232.o generic_serial.o
35obj-$(CONFIG_ATARI_DSP56K) += dsp56k.o 8obj-$(CONFIG_ATARI_DSP56K) += dsp56k.o
36obj-$(CONFIG_MOXA_SMARTIO) += mxser.o
37obj-$(CONFIG_COMPUTONE) += ip2/
38obj-$(CONFIG_RISCOM8) += riscom8.o
39obj-$(CONFIG_ISI) += isicom.o
40obj-$(CONFIG_SYNCLINK) += synclink.o
41obj-$(CONFIG_SYNCLINKMP) += synclinkmp.o
42obj-$(CONFIG_SYNCLINK_GT) += synclink_gt.o
43obj-$(CONFIG_N_HDLC) += n_hdlc.o
44obj-$(CONFIG_N_GSM) += n_gsm.o
45obj-$(CONFIG_AMIGA_BUILTIN_SERIAL) += amiserial.o
46obj-$(CONFIG_SX) += sx.o generic_serial.o
47obj-$(CONFIG_RIO) += rio/ generic_serial.o
48obj-$(CONFIG_HVC_CONSOLE) += hvc_vio.o hvsi.o
49obj-$(CONFIG_HVC_ISERIES) += hvc_iseries.o
50obj-$(CONFIG_HVC_RTAS) += hvc_rtas.o
51obj-$(CONFIG_HVC_TILE) += hvc_tile.o
52obj-$(CONFIG_HVC_BEAT) += hvc_beat.o
53obj-$(CONFIG_HVC_DRIVER) += hvc_console.o
54obj-$(CONFIG_HVC_IRQ) += hvc_irq.o
55obj-$(CONFIG_HVC_XEN) += hvc_xen.o
56obj-$(CONFIG_HVC_IUCV) += hvc_iucv.o
57obj-$(CONFIG_HVC_UDBG) += hvc_udbg.o
58obj-$(CONFIG_VIRTIO_CONSOLE) += virtio_console.o 9obj-$(CONFIG_VIRTIO_CONSOLE) += virtio_console.o
59obj-$(CONFIG_RAW_DRIVER) += raw.o 10obj-$(CONFIG_RAW_DRIVER) += raw.o
60obj-$(CONFIG_SGI_SNSC) += snsc.o snsc_event.o 11obj-$(CONFIG_SGI_SNSC) += snsc.o snsc_event.o
12obj-$(CONFIG_MSM_SMD_PKT) += msm_smd_pkt.o
61obj-$(CONFIG_MSPEC) += mspec.o 13obj-$(CONFIG_MSPEC) += mspec.o
62obj-$(CONFIG_MMTIMER) += mmtimer.o 14obj-$(CONFIG_MMTIMER) += mmtimer.o
63obj-$(CONFIG_UV_MMTIMER) += uv_mmtimer.o 15obj-$(CONFIG_UV_MMTIMER) += uv_mmtimer.o
64obj-$(CONFIG_VIOTAPE) += viotape.o 16obj-$(CONFIG_VIOTAPE) += viotape.o
65obj-$(CONFIG_HVCS) += hvcs.o
66obj-$(CONFIG_IBM_BSR) += bsr.o 17obj-$(CONFIG_IBM_BSR) += bsr.o
67obj-$(CONFIG_SGI_MBCS) += mbcs.o 18obj-$(CONFIG_SGI_MBCS) += mbcs.o
68obj-$(CONFIG_BRIQ_PANEL) += briq_panel.o 19obj-$(CONFIG_BRIQ_PANEL) += briq_panel.o
@@ -73,7 +24,6 @@ obj-$(CONFIG_PRINTER) += lp.o
73obj-$(CONFIG_APM_EMULATION) += apm-emulation.o 24obj-$(CONFIG_APM_EMULATION) += apm-emulation.o
74 25
75obj-$(CONFIG_DTLK) += dtlk.o 26obj-$(CONFIG_DTLK) += dtlk.o
76obj-$(CONFIG_R3964) += n_r3964.o
77obj-$(CONFIG_APPLICOM) += applicom.o 27obj-$(CONFIG_APPLICOM) += applicom.o
78obj-$(CONFIG_SONYPI) += sonypi.o 28obj-$(CONFIG_SONYPI) += sonypi.o
79obj-$(CONFIG_RTC) += rtc.o 29obj-$(CONFIG_RTC) += rtc.o
@@ -97,7 +47,6 @@ obj-$(CONFIG_NWFLASH) += nwflash.o
97obj-$(CONFIG_SCx200_GPIO) += scx200_gpio.o 47obj-$(CONFIG_SCx200_GPIO) += scx200_gpio.o
98obj-$(CONFIG_PC8736x_GPIO) += pc8736x_gpio.o 48obj-$(CONFIG_PC8736x_GPIO) += pc8736x_gpio.o
99obj-$(CONFIG_NSC_GPIO) += nsc_gpio.o 49obj-$(CONFIG_NSC_GPIO) += nsc_gpio.o
100obj-$(CONFIG_CS5535_GPIO) += cs5535_gpio.o
101obj-$(CONFIG_GPIO_TB0219) += tb0219.o 50obj-$(CONFIG_GPIO_TB0219) += tb0219.o
102obj-$(CONFIG_TELCLOCK) += tlclk.o 51obj-$(CONFIG_TELCLOCK) += tlclk.o
103 52
@@ -114,28 +63,3 @@ obj-$(CONFIG_RAMOOPS) += ramoops.o
114 63
115obj-$(CONFIG_JS_RTC) += js-rtc.o 64obj-$(CONFIG_JS_RTC) += js-rtc.o
116js-rtc-y = rtc.o 65js-rtc-y = rtc.o
117
118# Files generated that shall be removed upon make clean
119clean-files := consolemap_deftbl.c defkeymap.c
120
121quiet_cmd_conmk = CONMK $@
122 cmd_conmk = scripts/conmakehash $< > $@
123
124$(obj)/consolemap_deftbl.c: $(src)/$(FONTMAPFILE)
125 $(call cmd,conmk)
126
127$(obj)/defkeymap.o: $(obj)/defkeymap.c
128
129# Uncomment if you're changing the keymap and have an appropriate
130# loadkeys version for the map. By default, we'll use the shipped
131# versions.
132# GENERATE_KEYMAP := 1
133
134ifdef GENERATE_KEYMAP
135
136$(obj)/defkeymap.c: $(obj)/%.c: $(src)/%.map
137 loadkeys --mktable $< > $@.tmp
138 sed -e 's/^static *//' $@.tmp > $@
139 rm $@.tmp
140
141endif
diff --git a/drivers/char/agp/Kconfig b/drivers/char/agp/Kconfig
index 4b66c69eaf57..d8b1b576556c 100644
--- a/drivers/char/agp/Kconfig
+++ b/drivers/char/agp/Kconfig
@@ -34,7 +34,7 @@ config AGP_ALI
34 X on the following ALi chipsets. The supported chipsets 34 X on the following ALi chipsets. The supported chipsets
35 include M1541, M1621, M1631, M1632, M1641,M1647,and M1651. 35 include M1541, M1621, M1631, M1632, M1641,M1647,and M1651.
36 For the ALi-chipset question, ALi suggests you refer to 36 For the ALi-chipset question, ALi suggests you refer to
37 <http://www.ali.com.tw/eng/support/index.shtml>. 37 <http://www.ali.com.tw/>.
38 38
39 The M1541 chipset can do AGP 1x and 2x, but note that there is an 39 The M1541 chipset can do AGP 1x and 2x, but note that there is an
40 acknowledged incompatibility with Matrox G200 cards. Due to 40 acknowledged incompatibility with Matrox G200 cards. Due to
@@ -50,14 +50,14 @@ config AGP_ATI
50 50
51config AGP_AMD 51config AGP_AMD
52 tristate "AMD Irongate, 761, and 762 chipset support" 52 tristate "AMD Irongate, 761, and 762 chipset support"
53 depends on AGP && (X86_32 || ALPHA) 53 depends on AGP && X86_32
54 help 54 help
55 This option gives you AGP support for the GLX component of 55 This option gives you AGP support for the GLX component of
56 X on AMD Irongate, 761, and 762 chipsets. 56 X on AMD Irongate, 761, and 762 chipsets.
57 57
58config AGP_AMD64 58config AGP_AMD64
59 tristate "AMD Opteron/Athlon64 on-CPU GART support" 59 tristate "AMD Opteron/Athlon64 on-CPU GART support"
60 depends on AGP && X86 && K8_NB 60 depends on AGP && X86 && AMD_NB
61 help 61 help
62 This option gives you AGP support for the GLX component of 62 This option gives you AGP support for the GLX component of
63 X using the on-CPU northbridge of the AMD Athlon64/Opteron CPUs. 63 X using the on-CPU northbridge of the AMD Athlon64/Opteron CPUs.
diff --git a/drivers/char/agp/Makefile b/drivers/char/agp/Makefile
index 627f542827c7..8eb56e273e75 100644
--- a/drivers/char/agp/Makefile
+++ b/drivers/char/agp/Makefile
@@ -13,6 +13,7 @@ obj-$(CONFIG_AGP_HP_ZX1) += hp-agp.o
13obj-$(CONFIG_AGP_PARISC) += parisc-agp.o 13obj-$(CONFIG_AGP_PARISC) += parisc-agp.o
14obj-$(CONFIG_AGP_I460) += i460-agp.o 14obj-$(CONFIG_AGP_I460) += i460-agp.o
15obj-$(CONFIG_AGP_INTEL) += intel-agp.o 15obj-$(CONFIG_AGP_INTEL) += intel-agp.o
16obj-$(CONFIG_AGP_INTEL) += intel-gtt.o
16obj-$(CONFIG_AGP_NVIDIA) += nvidia-agp.o 17obj-$(CONFIG_AGP_NVIDIA) += nvidia-agp.o
17obj-$(CONFIG_AGP_SGI_TIOCA) += sgi-agp.o 18obj-$(CONFIG_AGP_SGI_TIOCA) += sgi-agp.o
18obj-$(CONFIG_AGP_SIS) += sis-agp.o 19obj-$(CONFIG_AGP_SIS) += sis-agp.o
diff --git a/drivers/char/agp/agp.h b/drivers/char/agp/agp.h
index 120490949997..923f99df4f1c 100644
--- a/drivers/char/agp/agp.h
+++ b/drivers/char/agp/agp.h
@@ -120,12 +120,6 @@ struct agp_bridge_driver {
120 void (*agp_destroy_page)(struct page *, int flags); 120 void (*agp_destroy_page)(struct page *, int flags);
121 void (*agp_destroy_pages)(struct agp_memory *); 121 void (*agp_destroy_pages)(struct agp_memory *);
122 int (*agp_type_to_mask_type) (struct agp_bridge_data *, int); 122 int (*agp_type_to_mask_type) (struct agp_bridge_data *, int);
123 void (*chipset_flush)(struct agp_bridge_data *);
124
125 int (*agp_map_page)(struct page *page, dma_addr_t *ret);
126 void (*agp_unmap_page)(struct page *page, dma_addr_t dma);
127 int (*agp_map_memory)(struct agp_memory *mem);
128 void (*agp_unmap_memory)(struct agp_memory *mem);
129}; 123};
130 124
131struct agp_bridge_data { 125struct agp_bridge_data {
@@ -243,7 +237,7 @@ extern int agp_try_unsupported_boot;
243 237
244long compat_agp_ioctl(struct file *file, unsigned int cmd, unsigned long arg); 238long compat_agp_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
245 239
246/* Chipset independant registers (from AGP Spec) */ 240/* Chipset independent registers (from AGP Spec) */
247#define AGP_APBASE 0x10 241#define AGP_APBASE 0x10
248 242
249#define AGPSTAT 0x4 243#define AGPSTAT 0x4
diff --git a/drivers/char/agp/amd-k7-agp.c b/drivers/char/agp/amd-k7-agp.c
index b6b1568314c8..f7e88787af97 100644
--- a/drivers/char/agp/amd-k7-agp.c
+++ b/drivers/char/agp/amd-k7-agp.c
@@ -41,22 +41,8 @@ static int amd_create_page_map(struct amd_page_map *page_map)
41 if (page_map->real == NULL) 41 if (page_map->real == NULL)
42 return -ENOMEM; 42 return -ENOMEM;
43 43
44#ifndef CONFIG_X86
45 SetPageReserved(virt_to_page(page_map->real));
46 global_cache_flush();
47 page_map->remapped = ioremap_nocache(virt_to_phys(page_map->real),
48 PAGE_SIZE);
49 if (page_map->remapped == NULL) {
50 ClearPageReserved(virt_to_page(page_map->real));
51 free_page((unsigned long) page_map->real);
52 page_map->real = NULL;
53 return -ENOMEM;
54 }
55 global_cache_flush();
56#else
57 set_memory_uc((unsigned long)page_map->real, 1); 44 set_memory_uc((unsigned long)page_map->real, 1);
58 page_map->remapped = page_map->real; 45 page_map->remapped = page_map->real;
59#endif
60 46
61 for (i = 0; i < PAGE_SIZE / sizeof(unsigned long); i++) { 47 for (i = 0; i < PAGE_SIZE / sizeof(unsigned long); i++) {
62 writel(agp_bridge->scratch_page, page_map->remapped+i); 48 writel(agp_bridge->scratch_page, page_map->remapped+i);
@@ -68,12 +54,7 @@ static int amd_create_page_map(struct amd_page_map *page_map)
68 54
69static void amd_free_page_map(struct amd_page_map *page_map) 55static void amd_free_page_map(struct amd_page_map *page_map)
70{ 56{
71#ifndef CONFIG_X86
72 iounmap(page_map->remapped);
73 ClearPageReserved(virt_to_page(page_map->real));
74#else
75 set_memory_wb((unsigned long)page_map->real, 1); 57 set_memory_wb((unsigned long)page_map->real, 1);
76#endif
77 free_page((unsigned long) page_map->real); 58 free_page((unsigned long) page_map->real);
78} 59}
79 60
@@ -291,7 +272,7 @@ static void amd_irongate_cleanup(void)
291 * This routine could be implemented by taking the addresses 272 * This routine could be implemented by taking the addresses
292 * written to the GATT, and flushing them individually. However 273 * written to the GATT, and flushing them individually. However
293 * currently it just flushes the whole table. Which is probably 274 * currently it just flushes the whole table. Which is probably
294 * more efficent, since agp_memory blocks can be a large number of 275 * more efficient, since agp_memory blocks can be a large number of
295 * entries. 276 * entries.
296 */ 277 */
297 278
@@ -309,7 +290,8 @@ static int amd_insert_memory(struct agp_memory *mem, off_t pg_start, int type)
309 290
310 num_entries = A_SIZE_LVL2(agp_bridge->current_size)->num_entries; 291 num_entries = A_SIZE_LVL2(agp_bridge->current_size)->num_entries;
311 292
312 if (type != 0 || mem->type != 0) 293 if (type != mem->type ||
294 agp_bridge->driver->agp_type_to_mask_type(agp_bridge, type))
313 return -EINVAL; 295 return -EINVAL;
314 296
315 if ((pg_start + mem->page_count) > num_entries) 297 if ((pg_start + mem->page_count) > num_entries)
@@ -348,7 +330,8 @@ static int amd_remove_memory(struct agp_memory *mem, off_t pg_start, int type)
348 unsigned long __iomem *cur_gatt; 330 unsigned long __iomem *cur_gatt;
349 unsigned long addr; 331 unsigned long addr;
350 332
351 if (type != 0 || mem->type != 0) 333 if (type != mem->type ||
334 agp_bridge->driver->agp_type_to_mask_type(agp_bridge, type))
352 return -EINVAL; 335 return -EINVAL;
353 336
354 for (i = pg_start; i < (mem->page_count + pg_start); i++) { 337 for (i = pg_start; i < (mem->page_count + pg_start); i++) {
diff --git a/drivers/char/agp/amd64-agp.c b/drivers/char/agp/amd64-agp.c
index 70312da4c968..780498d76581 100644
--- a/drivers/char/agp/amd64-agp.c
+++ b/drivers/char/agp/amd64-agp.c
@@ -15,7 +15,7 @@
15#include <linux/mmzone.h> 15#include <linux/mmzone.h>
16#include <asm/page.h> /* PAGE_SIZE */ 16#include <asm/page.h> /* PAGE_SIZE */
17#include <asm/e820.h> 17#include <asm/e820.h>
18#include <asm/k8.h> 18#include <asm/amd_nb.h>
19#include <asm/gart.h> 19#include <asm/gart.h>
20#include "agp.h" 20#include "agp.h"
21 21
@@ -38,7 +38,7 @@ static int agp_bridges_found;
38 38
39static void amd64_tlbflush(struct agp_memory *temp) 39static void amd64_tlbflush(struct agp_memory *temp)
40{ 40{
41 k8_flush_garts(); 41 amd_flush_garts();
42} 42}
43 43
44static int amd64_insert_memory(struct agp_memory *mem, off_t pg_start, int type) 44static int amd64_insert_memory(struct agp_memory *mem, off_t pg_start, int type)
@@ -124,7 +124,7 @@ static int amd64_fetch_size(void)
124 u32 temp; 124 u32 temp;
125 struct aper_size_info_32 *values; 125 struct aper_size_info_32 *values;
126 126
127 dev = k8_northbridges[0]; 127 dev = node_to_amd_nb(0)->misc;
128 if (dev==NULL) 128 if (dev==NULL)
129 return 0; 129 return 0;
130 130
@@ -181,12 +181,15 @@ static int amd_8151_configure(void)
181 unsigned long gatt_bus = virt_to_phys(agp_bridge->gatt_table_real); 181 unsigned long gatt_bus = virt_to_phys(agp_bridge->gatt_table_real);
182 int i; 182 int i;
183 183
184 if (!amd_nb_has_feature(AMD_NB_GART))
185 return 0;
186
184 /* Configure AGP regs in each x86-64 host bridge. */ 187 /* Configure AGP regs in each x86-64 host bridge. */
185 for (i = 0; i < num_k8_northbridges; i++) { 188 for (i = 0; i < amd_nb_num(); i++) {
186 agp_bridge->gart_bus_addr = 189 agp_bridge->gart_bus_addr =
187 amd64_configure(k8_northbridges[i], gatt_bus); 190 amd64_configure(node_to_amd_nb(i)->misc, gatt_bus);
188 } 191 }
189 k8_flush_garts(); 192 amd_flush_garts();
190 return 0; 193 return 0;
191} 194}
192 195
@@ -195,11 +198,15 @@ static void amd64_cleanup(void)
195{ 198{
196 u32 tmp; 199 u32 tmp;
197 int i; 200 int i;
198 for (i = 0; i < num_k8_northbridges; i++) { 201
199 struct pci_dev *dev = k8_northbridges[i]; 202 if (!amd_nb_has_feature(AMD_NB_GART))
203 return;
204
205 for (i = 0; i < amd_nb_num(); i++) {
206 struct pci_dev *dev = node_to_amd_nb(i)->misc;
200 /* disable gart translation */ 207 /* disable gart translation */
201 pci_read_config_dword(dev, AMD64_GARTAPERTURECTL, &tmp); 208 pci_read_config_dword(dev, AMD64_GARTAPERTURECTL, &tmp);
202 tmp &= ~AMD64_GARTEN; 209 tmp &= ~GARTEN;
203 pci_write_config_dword(dev, AMD64_GARTAPERTURECTL, tmp); 210 pci_write_config_dword(dev, AMD64_GARTAPERTURECTL, tmp);
204 } 211 }
205} 212}
@@ -313,22 +320,25 @@ static __devinit int fix_northbridge(struct pci_dev *nb, struct pci_dev *agp,
313 if (order < 0 || !agp_aperture_valid(aper, (32*1024*1024)<<order)) 320 if (order < 0 || !agp_aperture_valid(aper, (32*1024*1024)<<order))
314 return -1; 321 return -1;
315 322
316 pci_write_config_dword(nb, AMD64_GARTAPERTURECTL, order << 1); 323 gart_set_size_and_enable(nb, order);
317 pci_write_config_dword(nb, AMD64_GARTAPERTUREBASE, aper >> 25); 324 pci_write_config_dword(nb, AMD64_GARTAPERTUREBASE, aper >> 25);
318 325
319 return 0; 326 return 0;
320} 327}
321 328
322static __devinit int cache_nbs (struct pci_dev *pdev, u32 cap_ptr) 329static __devinit int cache_nbs(struct pci_dev *pdev, u32 cap_ptr)
323{ 330{
324 int i; 331 int i;
325 332
326 if (cache_k8_northbridges() < 0) 333 if (amd_cache_northbridges() < 0)
334 return -ENODEV;
335
336 if (!amd_nb_has_feature(AMD_NB_GART))
327 return -ENODEV; 337 return -ENODEV;
328 338
329 i = 0; 339 i = 0;
330 for (i = 0; i < num_k8_northbridges; i++) { 340 for (i = 0; i < amd_nb_num(); i++) {
331 struct pci_dev *dev = k8_northbridges[i]; 341 struct pci_dev *dev = node_to_amd_nb(i)->misc;
332 if (fix_northbridge(dev, pdev, cap_ptr) < 0) { 342 if (fix_northbridge(dev, pdev, cap_ptr) < 0) {
333 dev_err(&dev->dev, "no usable aperture found\n"); 343 dev_err(&dev->dev, "no usable aperture found\n");
334#ifdef __x86_64__ 344#ifdef __x86_64__
@@ -405,7 +415,8 @@ static int __devinit uli_agp_init(struct pci_dev *pdev)
405 } 415 }
406 416
407 /* shadow x86-64 registers into ULi registers */ 417 /* shadow x86-64 registers into ULi registers */
408 pci_read_config_dword (k8_northbridges[0], AMD64_GARTAPERTUREBASE, &httfea); 418 pci_read_config_dword (node_to_amd_nb(0)->misc, AMD64_GARTAPERTUREBASE,
419 &httfea);
409 420
410 /* if x86-64 aperture base is beyond 4G, exit here */ 421 /* if x86-64 aperture base is beyond 4G, exit here */
411 if ((httfea & 0x7fff) >> (32 - 25)) { 422 if ((httfea & 0x7fff) >> (32 - 25)) {
@@ -472,7 +483,8 @@ static int nforce3_agp_init(struct pci_dev *pdev)
472 pci_write_config_dword(dev1, NVIDIA_X86_64_1_APSIZE, tmp); 483 pci_write_config_dword(dev1, NVIDIA_X86_64_1_APSIZE, tmp);
473 484
474 /* shadow x86-64 registers into NVIDIA registers */ 485 /* shadow x86-64 registers into NVIDIA registers */
475 pci_read_config_dword (k8_northbridges[0], AMD64_GARTAPERTUREBASE, &apbase); 486 pci_read_config_dword (node_to_amd_nb(0)->misc, AMD64_GARTAPERTUREBASE,
487 &apbase);
476 488
477 /* if x86-64 aperture base is beyond 4G, exit here */ 489 /* if x86-64 aperture base is beyond 4G, exit here */
478 if ( (apbase & 0x7fff) >> (32 - 25) ) { 490 if ( (apbase & 0x7fff) >> (32 - 25) ) {
@@ -761,18 +773,23 @@ int __init agp_amd64_init(void)
761#else 773#else
762 printk(KERN_INFO PFX "You can boot with agp=try_unsupported\n"); 774 printk(KERN_INFO PFX "You can boot with agp=try_unsupported\n");
763#endif 775#endif
776 pci_unregister_driver(&agp_amd64_pci_driver);
764 return -ENODEV; 777 return -ENODEV;
765 } 778 }
766 779
767 /* First check that we have at least one AMD64 NB */ 780 /* First check that we have at least one AMD64 NB */
768 if (!pci_dev_present(k8_nb_ids)) 781 if (!pci_dev_present(amd_nb_misc_ids)) {
782 pci_unregister_driver(&agp_amd64_pci_driver);
769 return -ENODEV; 783 return -ENODEV;
784 }
770 785
771 /* Look for any AGP bridge */ 786 /* Look for any AGP bridge */
772 agp_amd64_pci_driver.id_table = agp_amd64_pci_promisc_table; 787 agp_amd64_pci_driver.id_table = agp_amd64_pci_promisc_table;
773 err = driver_attach(&agp_amd64_pci_driver.driver); 788 err = driver_attach(&agp_amd64_pci_driver.driver);
774 if (err == 0 && agp_bridges_found == 0) 789 if (err == 0 && agp_bridges_found == 0) {
790 pci_unregister_driver(&agp_amd64_pci_driver);
775 err = -ENODEV; 791 err = -ENODEV;
792 }
776 } 793 }
777 return err; 794 return err;
778} 795}
diff --git a/drivers/char/agp/backend.c b/drivers/char/agp/backend.c
index ee4f855611b6..f27d0d0816d3 100644
--- a/drivers/char/agp/backend.c
+++ b/drivers/char/agp/backend.c
@@ -151,17 +151,7 @@ static int agp_backend_initialize(struct agp_bridge_data *bridge)
151 } 151 }
152 152
153 bridge->scratch_page_page = page; 153 bridge->scratch_page_page = page;
154 if (bridge->driver->agp_map_page) { 154 bridge->scratch_page_dma = page_to_phys(page);
155 if (bridge->driver->agp_map_page(page,
156 &bridge->scratch_page_dma)) {
157 dev_err(&bridge->dev->dev,
158 "unable to dma-map scratch page\n");
159 rc = -ENOMEM;
160 goto err_out_nounmap;
161 }
162 } else {
163 bridge->scratch_page_dma = page_to_phys(page);
164 }
165 155
166 bridge->scratch_page = bridge->driver->mask_memory(bridge, 156 bridge->scratch_page = bridge->driver->mask_memory(bridge,
167 bridge->scratch_page_dma, 0); 157 bridge->scratch_page_dma, 0);
@@ -204,12 +194,6 @@ static int agp_backend_initialize(struct agp_bridge_data *bridge)
204 return 0; 194 return 0;
205 195
206err_out: 196err_out:
207 if (bridge->driver->needs_scratch_page &&
208 bridge->driver->agp_unmap_page) {
209 bridge->driver->agp_unmap_page(bridge->scratch_page_page,
210 bridge->scratch_page_dma);
211 }
212err_out_nounmap:
213 if (bridge->driver->needs_scratch_page) { 197 if (bridge->driver->needs_scratch_page) {
214 void *va = page_address(bridge->scratch_page_page); 198 void *va = page_address(bridge->scratch_page_page);
215 199
@@ -240,10 +224,6 @@ static void agp_backend_cleanup(struct agp_bridge_data *bridge)
240 bridge->driver->needs_scratch_page) { 224 bridge->driver->needs_scratch_page) {
241 void *va = page_address(bridge->scratch_page_page); 225 void *va = page_address(bridge->scratch_page_page);
242 226
243 if (bridge->driver->agp_unmap_page)
244 bridge->driver->agp_unmap_page(bridge->scratch_page_page,
245 bridge->scratch_page_dma);
246
247 bridge->driver->agp_destroy_page(va, AGP_PAGE_DESTROY_UNMAP); 227 bridge->driver->agp_destroy_page(va, AGP_PAGE_DESTROY_UNMAP);
248 bridge->driver->agp_destroy_page(va, AGP_PAGE_DESTROY_FREE); 228 bridge->driver->agp_destroy_page(va, AGP_PAGE_DESTROY_FREE);
249 } 229 }
diff --git a/drivers/char/agp/compat_ioctl.c b/drivers/char/agp/compat_ioctl.c
index 9d2c97a69cdd..a48e05b31593 100644
--- a/drivers/char/agp/compat_ioctl.c
+++ b/drivers/char/agp/compat_ioctl.c
@@ -276,7 +276,6 @@ long compat_agp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
276 break; 276 break;
277 277
278 case AGPIOC_CHIPSET_FLUSH32: 278 case AGPIOC_CHIPSET_FLUSH32:
279 ret_val = agpioc_chipset_flush_wrap(curr_priv);
280 break; 279 break;
281 } 280 }
282 281
diff --git a/drivers/char/agp/compat_ioctl.h b/drivers/char/agp/compat_ioctl.h
index 0c9678ac0371..f30e0fd97963 100644
--- a/drivers/char/agp/compat_ioctl.h
+++ b/drivers/char/agp/compat_ioctl.h
@@ -102,6 +102,5 @@ void agp_free_memory_wrap(struct agp_memory *memory);
102struct agp_memory *agp_allocate_memory_wrap(size_t pg_count, u32 type); 102struct agp_memory *agp_allocate_memory_wrap(size_t pg_count, u32 type);
103struct agp_memory *agp_find_mem_by_key(int key); 103struct agp_memory *agp_find_mem_by_key(int key);
104struct agp_client *agp_find_client_by_pid(pid_t id); 104struct agp_client *agp_find_client_by_pid(pid_t id);
105int agpioc_chipset_flush_wrap(struct agp_file_private *priv);
106 105
107#endif /* _AGP_COMPAT_H */ 106#endif /* _AGP_COMPAT_H */
diff --git a/drivers/char/agp/frontend.c b/drivers/char/agp/frontend.c
index 43412c03969e..2e044338753c 100644
--- a/drivers/char/agp/frontend.c
+++ b/drivers/char/agp/frontend.c
@@ -39,7 +39,6 @@
39#include <linux/mm.h> 39#include <linux/mm.h>
40#include <linux/fs.h> 40#include <linux/fs.h>
41#include <linux/sched.h> 41#include <linux/sched.h>
42#include <linux/smp_lock.h>
43#include <asm/uaccess.h> 42#include <asm/uaccess.h>
44#include <asm/pgtable.h> 43#include <asm/pgtable.h>
45#include "agp.h" 44#include "agp.h"
@@ -958,13 +957,6 @@ static int agpioc_unbind_wrap(struct agp_file_private *priv, void __user *arg)
958 return agp_unbind_memory(memory); 957 return agp_unbind_memory(memory);
959} 958}
960 959
961int agpioc_chipset_flush_wrap(struct agp_file_private *priv)
962{
963 DBG("");
964 agp_flush_chipset(agp_bridge);
965 return 0;
966}
967
968static long agp_ioctl(struct file *file, 960static long agp_ioctl(struct file *file,
969 unsigned int cmd, unsigned long arg) 961 unsigned int cmd, unsigned long arg)
970{ 962{
@@ -1040,7 +1032,6 @@ static long agp_ioctl(struct file *file,
1040 break; 1032 break;
1041 1033
1042 case AGPIOC_CHIPSET_FLUSH: 1034 case AGPIOC_CHIPSET_FLUSH:
1043 ret_val = agpioc_chipset_flush_wrap(curr_priv);
1044 break; 1035 break;
1045 } 1036 }
1046 1037
diff --git a/drivers/char/agp/generic.c b/drivers/char/agp/generic.c
index d2abf5143983..b072648dc3f6 100644
--- a/drivers/char/agp/generic.c
+++ b/drivers/char/agp/generic.c
@@ -81,13 +81,6 @@ static int agp_get_key(void)
81 return -1; 81 return -1;
82} 82}
83 83
84void agp_flush_chipset(struct agp_bridge_data *bridge)
85{
86 if (bridge->driver->chipset_flush)
87 bridge->driver->chipset_flush(bridge);
88}
89EXPORT_SYMBOL(agp_flush_chipset);
90
91/* 84/*
92 * Use kmalloc if possible for the page list. Otherwise fall back to 85 * Use kmalloc if possible for the page list. Otherwise fall back to
93 * vmalloc. This speeds things up and also saves memory for small AGP 86 * vmalloc. This speeds things up and also saves memory for small AGP
@@ -122,6 +115,9 @@ static struct agp_memory *agp_create_user_memory(unsigned long num_agp_pages)
122 struct agp_memory *new; 115 struct agp_memory *new;
123 unsigned long alloc_size = num_agp_pages*sizeof(struct page *); 116 unsigned long alloc_size = num_agp_pages*sizeof(struct page *);
124 117
118 if (INT_MAX/sizeof(struct page *) < num_agp_pages)
119 return NULL;
120
125 new = kzalloc(sizeof(struct agp_memory), GFP_KERNEL); 121 new = kzalloc(sizeof(struct agp_memory), GFP_KERNEL);
126 if (new == NULL) 122 if (new == NULL)
127 return NULL; 123 return NULL;
@@ -241,11 +237,14 @@ struct agp_memory *agp_allocate_memory(struct agp_bridge_data *bridge,
241 int scratch_pages; 237 int scratch_pages;
242 struct agp_memory *new; 238 struct agp_memory *new;
243 size_t i; 239 size_t i;
240 int cur_memory;
244 241
245 if (!bridge) 242 if (!bridge)
246 return NULL; 243 return NULL;
247 244
248 if ((atomic_read(&bridge->current_memory_agp) + page_count) > bridge->max_memory_agp) 245 cur_memory = atomic_read(&bridge->current_memory_agp);
246 if ((cur_memory + page_count > bridge->max_memory_agp) ||
247 (cur_memory + page_count < page_count))
249 return NULL; 248 return NULL;
250 249
251 if (type >= AGP_USER_TYPES) { 250 if (type >= AGP_USER_TYPES) {
@@ -437,11 +436,6 @@ int agp_bind_memory(struct agp_memory *curr, off_t pg_start)
437 curr->is_flushed = true; 436 curr->is_flushed = true;
438 } 437 }
439 438
440 if (curr->bridge->driver->agp_map_memory) {
441 ret_val = curr->bridge->driver->agp_map_memory(curr);
442 if (ret_val)
443 return ret_val;
444 }
445 ret_val = curr->bridge->driver->insert_memory(curr, pg_start, curr->type); 439 ret_val = curr->bridge->driver->insert_memory(curr, pg_start, curr->type);
446 440
447 if (ret_val != 0) 441 if (ret_val != 0)
@@ -483,9 +477,6 @@ int agp_unbind_memory(struct agp_memory *curr)
483 if (ret_val != 0) 477 if (ret_val != 0)
484 return ret_val; 478 return ret_val;
485 479
486 if (curr->bridge->driver->agp_unmap_memory)
487 curr->bridge->driver->agp_unmap_memory(curr);
488
489 curr->is_bound = false; 480 curr->is_bound = false;
490 curr->pg_start = 0; 481 curr->pg_start = 0;
491 spin_lock(&curr->bridge->mapped_lock); 482 spin_lock(&curr->bridge->mapped_lock);
@@ -495,26 +486,6 @@ int agp_unbind_memory(struct agp_memory *curr)
495} 486}
496EXPORT_SYMBOL(agp_unbind_memory); 487EXPORT_SYMBOL(agp_unbind_memory);
497 488
498/**
499 * agp_rebind_emmory - Rewrite the entire GATT, useful on resume
500 */
501int agp_rebind_memory(void)
502{
503 struct agp_memory *curr;
504 int ret_val = 0;
505
506 spin_lock(&agp_bridge->mapped_lock);
507 list_for_each_entry(curr, &agp_bridge->mapped_list, mapped_list) {
508 ret_val = curr->bridge->driver->insert_memory(curr,
509 curr->pg_start,
510 curr->type);
511 if (ret_val != 0)
512 break;
513 }
514 spin_unlock(&agp_bridge->mapped_lock);
515 return ret_val;
516}
517EXPORT_SYMBOL(agp_rebind_memory);
518 489
519/* End - Routines for handling swapping of agp_memory into the GATT */ 490/* End - Routines for handling swapping of agp_memory into the GATT */
520 491
@@ -984,7 +955,9 @@ int agp_generic_create_gatt_table(struct agp_bridge_data *bridge)
984 955
985 bridge->driver->cache_flush(); 956 bridge->driver->cache_flush();
986#ifdef CONFIG_X86 957#ifdef CONFIG_X86
987 set_memory_uc((unsigned long)table, 1 << page_order); 958 if (set_memory_uc((unsigned long)table, 1 << page_order))
959 printk(KERN_WARNING "Could not set GATT table memory to UC!");
960
988 bridge->gatt_table = (void *)table; 961 bridge->gatt_table = (void *)table;
989#else 962#else
990 bridge->gatt_table = ioremap_nocache(virt_to_phys(table), 963 bridge->gatt_table = ioremap_nocache(virt_to_phys(table),
@@ -1122,8 +1095,8 @@ int agp_generic_insert_memory(struct agp_memory * mem, off_t pg_start, int type)
1122 return -EINVAL; 1095 return -EINVAL;
1123 } 1096 }
1124 1097
1125 /* AK: could wrap */ 1098 if (((pg_start + mem->page_count) > num_entries) ||
1126 if ((pg_start + mem->page_count) > num_entries) 1099 ((pg_start + mem->page_count) < pg_start))
1127 return -EINVAL; 1100 return -EINVAL;
1128 1101
1129 j = pg_start; 1102 j = pg_start;
@@ -1157,7 +1130,7 @@ int agp_generic_remove_memory(struct agp_memory *mem, off_t pg_start, int type)
1157{ 1130{
1158 size_t i; 1131 size_t i;
1159 struct agp_bridge_data *bridge; 1132 struct agp_bridge_data *bridge;
1160 int mask_type; 1133 int mask_type, num_entries;
1161 1134
1162 bridge = mem->bridge; 1135 bridge = mem->bridge;
1163 if (!bridge) 1136 if (!bridge)
@@ -1169,6 +1142,11 @@ int agp_generic_remove_memory(struct agp_memory *mem, off_t pg_start, int type)
1169 if (type != mem->type) 1142 if (type != mem->type)
1170 return -EINVAL; 1143 return -EINVAL;
1171 1144
1145 num_entries = agp_num_entries();
1146 if (((pg_start + mem->page_count) > num_entries) ||
1147 ((pg_start + mem->page_count) < pg_start))
1148 return -EINVAL;
1149
1172 mask_type = bridge->driver->agp_type_to_mask_type(bridge, type); 1150 mask_type = bridge->driver->agp_type_to_mask_type(bridge, type);
1173 if (mask_type != 0) { 1151 if (mask_type != 0) {
1174 /* The generic routines know nothing of memory types */ 1152 /* The generic routines know nothing of memory types */
diff --git a/drivers/char/agp/i460-agp.c b/drivers/char/agp/i460-agp.c
index e763d3312ce7..75b763cb3ea1 100644
--- a/drivers/char/agp/i460-agp.c
+++ b/drivers/char/agp/i460-agp.c
@@ -1,7 +1,7 @@
1/* 1/*
2 * For documentation on the i460 AGP interface, see Chapter 7 (AGP Subsystem) of 2 * For documentation on the i460 AGP interface, see Chapter 7 (AGP Subsystem) of
3 * the "Intel 460GTX Chipset Software Developer's Manual": 3 * the "Intel 460GTX Chipset Software Developer's Manual":
4 * http://developer.intel.com/design/itanium/downloads/24870401s.htm 4 * http://www.intel.com/design/archives/itanium/downloads/248704.htm
5 */ 5 */
6/* 6/*
7 * 460GX support by Chris Ahna <christopher.j.ahna@intel.com> 7 * 460GX support by Chris Ahna <christopher.j.ahna@intel.com>
diff --git a/drivers/char/agp/intel-agp.c b/drivers/char/agp/intel-agp.c
index cd18493c9527..b427711be4be 100644
--- a/drivers/char/agp/intel-agp.c
+++ b/drivers/char/agp/intel-agp.c
@@ -12,9 +12,6 @@
12#include <asm/smp.h> 12#include <asm/smp.h>
13#include "agp.h" 13#include "agp.h"
14#include "intel-agp.h" 14#include "intel-agp.h"
15#include <linux/intel-gtt.h>
16
17#include "intel-gtt.c"
18 15
19int intel_agp_enabled; 16int intel_agp_enabled;
20EXPORT_SYMBOL(intel_agp_enabled); 17EXPORT_SYMBOL(intel_agp_enabled);
@@ -703,179 +700,37 @@ static const struct agp_bridge_driver intel_7505_driver = {
703 .agp_type_to_mask_type = agp_generic_type_to_mask_type, 700 .agp_type_to_mask_type = agp_generic_type_to_mask_type,
704}; 701};
705 702
706static int find_gmch(u16 device)
707{
708 struct pci_dev *gmch_device;
709
710 gmch_device = pci_get_device(PCI_VENDOR_ID_INTEL, device, NULL);
711 if (gmch_device && PCI_FUNC(gmch_device->devfn) != 0) {
712 gmch_device = pci_get_device(PCI_VENDOR_ID_INTEL,
713 device, gmch_device);
714 }
715
716 if (!gmch_device)
717 return 0;
718
719 intel_private.pcidev = gmch_device;
720 return 1;
721}
722
723/* Table to describe Intel GMCH and AGP/PCIE GART drivers. At least one of 703/* Table to describe Intel GMCH and AGP/PCIE GART drivers. At least one of
724 * driver and gmch_driver must be non-null, and find_gmch will determine 704 * driver and gmch_driver must be non-null, and find_gmch will determine
725 * which one should be used if a gmch_chip_id is present. 705 * which one should be used if a gmch_chip_id is present.
726 */ 706 */
727static const struct intel_driver_description { 707static const struct intel_agp_driver_description {
728 unsigned int chip_id; 708 unsigned int chip_id;
729 unsigned int gmch_chip_id;
730 char *name; 709 char *name;
731 const struct agp_bridge_driver *driver; 710 const struct agp_bridge_driver *driver;
732 const struct agp_bridge_driver *gmch_driver;
733} intel_agp_chipsets[] = { 711} intel_agp_chipsets[] = {
734 { PCI_DEVICE_ID_INTEL_82443LX_0, 0, "440LX", &intel_generic_driver, NULL }, 712 { PCI_DEVICE_ID_INTEL_82443LX_0, "440LX", &intel_generic_driver },
735 { PCI_DEVICE_ID_INTEL_82443BX_0, 0, "440BX", &intel_generic_driver, NULL }, 713 { PCI_DEVICE_ID_INTEL_82443BX_0, "440BX", &intel_generic_driver },
736 { PCI_DEVICE_ID_INTEL_82443GX_0, 0, "440GX", &intel_generic_driver, NULL }, 714 { PCI_DEVICE_ID_INTEL_82443GX_0, "440GX", &intel_generic_driver },
737 { PCI_DEVICE_ID_INTEL_82810_MC1, PCI_DEVICE_ID_INTEL_82810_IG1, "i810", 715 { PCI_DEVICE_ID_INTEL_82815_MC, "i815", &intel_815_driver },
738 NULL, &intel_810_driver }, 716 { PCI_DEVICE_ID_INTEL_82820_HB, "i820", &intel_820_driver },
739 { PCI_DEVICE_ID_INTEL_82810_MC3, PCI_DEVICE_ID_INTEL_82810_IG3, "i810", 717 { PCI_DEVICE_ID_INTEL_82820_UP_HB, "i820", &intel_820_driver },
740 NULL, &intel_810_driver }, 718 { PCI_DEVICE_ID_INTEL_82830_HB, "830M", &intel_830mp_driver },
741 { PCI_DEVICE_ID_INTEL_82810E_MC, PCI_DEVICE_ID_INTEL_82810E_IG, "i810", 719 { PCI_DEVICE_ID_INTEL_82840_HB, "i840", &intel_840_driver },
742 NULL, &intel_810_driver }, 720 { PCI_DEVICE_ID_INTEL_82845_HB, "i845", &intel_845_driver },
743 { PCI_DEVICE_ID_INTEL_82815_MC, PCI_DEVICE_ID_INTEL_82815_CGC, "i815", 721 { PCI_DEVICE_ID_INTEL_82845G_HB, "845G", &intel_845_driver },
744 &intel_815_driver, &intel_810_driver }, 722 { PCI_DEVICE_ID_INTEL_82850_HB, "i850", &intel_850_driver },
745 { PCI_DEVICE_ID_INTEL_82820_HB, 0, "i820", &intel_820_driver, NULL }, 723 { PCI_DEVICE_ID_INTEL_82854_HB, "854", &intel_845_driver },
746 { PCI_DEVICE_ID_INTEL_82820_UP_HB, 0, "i820", &intel_820_driver, NULL }, 724 { PCI_DEVICE_ID_INTEL_82855PM_HB, "855PM", &intel_845_driver },
747 { PCI_DEVICE_ID_INTEL_82830_HB, PCI_DEVICE_ID_INTEL_82830_CGC, "830M", 725 { PCI_DEVICE_ID_INTEL_82855GM_HB, "855GM", &intel_845_driver },
748 &intel_830mp_driver, &intel_830_driver }, 726 { PCI_DEVICE_ID_INTEL_82860_HB, "i860", &intel_860_driver },
749 { PCI_DEVICE_ID_INTEL_82840_HB, 0, "i840", &intel_840_driver, NULL }, 727 { PCI_DEVICE_ID_INTEL_82865_HB, "865", &intel_845_driver },
750 { PCI_DEVICE_ID_INTEL_82845_HB, 0, "845G", &intel_845_driver, NULL }, 728 { PCI_DEVICE_ID_INTEL_82875_HB, "i875", &intel_845_driver },
751 { PCI_DEVICE_ID_INTEL_82845G_HB, PCI_DEVICE_ID_INTEL_82845G_IG, "830M", 729 { PCI_DEVICE_ID_INTEL_7505_0, "E7505", &intel_7505_driver },
752 &intel_845_driver, &intel_830_driver }, 730 { PCI_DEVICE_ID_INTEL_7205_0, "E7205", &intel_7505_driver },
753 { PCI_DEVICE_ID_INTEL_82850_HB, 0, "i850", &intel_850_driver, NULL }, 731 { 0, NULL, NULL }
754 { PCI_DEVICE_ID_INTEL_82854_HB, PCI_DEVICE_ID_INTEL_82854_IG, "854",
755 &intel_845_driver, &intel_830_driver },
756 { PCI_DEVICE_ID_INTEL_82855PM_HB, 0, "855PM", &intel_845_driver, NULL },
757 { PCI_DEVICE_ID_INTEL_82855GM_HB, PCI_DEVICE_ID_INTEL_82855GM_IG, "855GM",
758 &intel_845_driver, &intel_830_driver },
759 { PCI_DEVICE_ID_INTEL_82860_HB, 0, "i860", &intel_860_driver, NULL },
760 { PCI_DEVICE_ID_INTEL_82865_HB, PCI_DEVICE_ID_INTEL_82865_IG, "865",
761 &intel_845_driver, &intel_830_driver },
762 { PCI_DEVICE_ID_INTEL_82875_HB, 0, "i875", &intel_845_driver, NULL },
763 { PCI_DEVICE_ID_INTEL_E7221_HB, PCI_DEVICE_ID_INTEL_E7221_IG, "E7221 (i915)",
764 NULL, &intel_915_driver },
765 { PCI_DEVICE_ID_INTEL_82915G_HB, PCI_DEVICE_ID_INTEL_82915G_IG, "915G",
766 NULL, &intel_915_driver },
767 { PCI_DEVICE_ID_INTEL_82915GM_HB, PCI_DEVICE_ID_INTEL_82915GM_IG, "915GM",
768 NULL, &intel_915_driver },
769 { PCI_DEVICE_ID_INTEL_82945G_HB, PCI_DEVICE_ID_INTEL_82945G_IG, "945G",
770 NULL, &intel_915_driver },
771 { PCI_DEVICE_ID_INTEL_82945GM_HB, PCI_DEVICE_ID_INTEL_82945GM_IG, "945GM",
772 NULL, &intel_915_driver },
773 { PCI_DEVICE_ID_INTEL_82945GME_HB, PCI_DEVICE_ID_INTEL_82945GME_IG, "945GME",
774 NULL, &intel_915_driver },
775 { PCI_DEVICE_ID_INTEL_82946GZ_HB, PCI_DEVICE_ID_INTEL_82946GZ_IG, "946GZ",
776 NULL, &intel_i965_driver },
777 { PCI_DEVICE_ID_INTEL_82G35_HB, PCI_DEVICE_ID_INTEL_82G35_IG, "G35",
778 NULL, &intel_i965_driver },
779 { PCI_DEVICE_ID_INTEL_82965Q_HB, PCI_DEVICE_ID_INTEL_82965Q_IG, "965Q",
780 NULL, &intel_i965_driver },
781 { PCI_DEVICE_ID_INTEL_82965G_HB, PCI_DEVICE_ID_INTEL_82965G_IG, "965G",
782 NULL, &intel_i965_driver },
783 { PCI_DEVICE_ID_INTEL_82965GM_HB, PCI_DEVICE_ID_INTEL_82965GM_IG, "965GM",
784 NULL, &intel_i965_driver },
785 { PCI_DEVICE_ID_INTEL_82965GME_HB, PCI_DEVICE_ID_INTEL_82965GME_IG, "965GME/GLE",
786 NULL, &intel_i965_driver },
787 { PCI_DEVICE_ID_INTEL_7505_0, 0, "E7505", &intel_7505_driver, NULL },
788 { PCI_DEVICE_ID_INTEL_7205_0, 0, "E7205", &intel_7505_driver, NULL },
789 { PCI_DEVICE_ID_INTEL_G33_HB, PCI_DEVICE_ID_INTEL_G33_IG, "G33",
790 NULL, &intel_g33_driver },
791 { PCI_DEVICE_ID_INTEL_Q35_HB, PCI_DEVICE_ID_INTEL_Q35_IG, "Q35",
792 NULL, &intel_g33_driver },
793 { PCI_DEVICE_ID_INTEL_Q33_HB, PCI_DEVICE_ID_INTEL_Q33_IG, "Q33",
794 NULL, &intel_g33_driver },
795 { PCI_DEVICE_ID_INTEL_PINEVIEW_M_HB, PCI_DEVICE_ID_INTEL_PINEVIEW_M_IG, "GMA3150",
796 NULL, &intel_g33_driver },
797 { PCI_DEVICE_ID_INTEL_PINEVIEW_HB, PCI_DEVICE_ID_INTEL_PINEVIEW_IG, "GMA3150",
798 NULL, &intel_g33_driver },
799 { PCI_DEVICE_ID_INTEL_GM45_HB, PCI_DEVICE_ID_INTEL_GM45_IG,
800 "GM45", NULL, &intel_i965_driver },
801 { PCI_DEVICE_ID_INTEL_EAGLELAKE_HB, PCI_DEVICE_ID_INTEL_EAGLELAKE_IG,
802 "Eaglelake", NULL, &intel_i965_driver },
803 { PCI_DEVICE_ID_INTEL_Q45_HB, PCI_DEVICE_ID_INTEL_Q45_IG,
804 "Q45/Q43", NULL, &intel_i965_driver },
805 { PCI_DEVICE_ID_INTEL_G45_HB, PCI_DEVICE_ID_INTEL_G45_IG,
806 "G45/G43", NULL, &intel_i965_driver },
807 { PCI_DEVICE_ID_INTEL_B43_HB, PCI_DEVICE_ID_INTEL_B43_IG,
808 "B43", NULL, &intel_i965_driver },
809 { PCI_DEVICE_ID_INTEL_B43_1_HB, PCI_DEVICE_ID_INTEL_B43_1_IG,
810 "B43", NULL, &intel_i965_driver },
811 { PCI_DEVICE_ID_INTEL_G41_HB, PCI_DEVICE_ID_INTEL_G41_IG,
812 "G41", NULL, &intel_i965_driver },
813 { PCI_DEVICE_ID_INTEL_IRONLAKE_D_HB, PCI_DEVICE_ID_INTEL_IRONLAKE_D_IG,
814 "HD Graphics", NULL, &intel_i965_driver },
815 { PCI_DEVICE_ID_INTEL_IRONLAKE_M_HB, PCI_DEVICE_ID_INTEL_IRONLAKE_M_IG,
816 "HD Graphics", NULL, &intel_i965_driver },
817 { PCI_DEVICE_ID_INTEL_IRONLAKE_MA_HB, PCI_DEVICE_ID_INTEL_IRONLAKE_M_IG,
818 "HD Graphics", NULL, &intel_i965_driver },
819 { PCI_DEVICE_ID_INTEL_IRONLAKE_MC2_HB, PCI_DEVICE_ID_INTEL_IRONLAKE_M_IG,
820 "HD Graphics", NULL, &intel_i965_driver },
821 { PCI_DEVICE_ID_INTEL_SANDYBRIDGE_HB, PCI_DEVICE_ID_INTEL_SANDYBRIDGE_GT1_IG,
822 "Sandybridge", NULL, &intel_gen6_driver },
823 { PCI_DEVICE_ID_INTEL_SANDYBRIDGE_HB, PCI_DEVICE_ID_INTEL_SANDYBRIDGE_GT2_IG,
824 "Sandybridge", NULL, &intel_gen6_driver },
825 { PCI_DEVICE_ID_INTEL_SANDYBRIDGE_HB, PCI_DEVICE_ID_INTEL_SANDYBRIDGE_GT2_PLUS_IG,
826 "Sandybridge", NULL, &intel_gen6_driver },
827 { PCI_DEVICE_ID_INTEL_SANDYBRIDGE_M_HB, PCI_DEVICE_ID_INTEL_SANDYBRIDGE_M_GT1_IG,
828 "Sandybridge", NULL, &intel_gen6_driver },
829 { PCI_DEVICE_ID_INTEL_SANDYBRIDGE_M_HB, PCI_DEVICE_ID_INTEL_SANDYBRIDGE_M_GT2_IG,
830 "Sandybridge", NULL, &intel_gen6_driver },
831 { PCI_DEVICE_ID_INTEL_SANDYBRIDGE_M_HB, PCI_DEVICE_ID_INTEL_SANDYBRIDGE_M_GT2_PLUS_IG,
832 "Sandybridge", NULL, &intel_gen6_driver },
833 { PCI_DEVICE_ID_INTEL_SANDYBRIDGE_S_HB, PCI_DEVICE_ID_INTEL_SANDYBRIDGE_S_IG,
834 "Sandybridge", NULL, &intel_gen6_driver },
835 { 0, 0, NULL, NULL, NULL }
836}; 732};
837 733
838static int __devinit intel_gmch_probe(struct pci_dev *pdev,
839 struct agp_bridge_data *bridge)
840{
841 int i, mask;
842
843 bridge->driver = NULL;
844
845 for (i = 0; intel_agp_chipsets[i].name != NULL; i++) {
846 if ((intel_agp_chipsets[i].gmch_chip_id != 0) &&
847 find_gmch(intel_agp_chipsets[i].gmch_chip_id)) {
848 bridge->driver =
849 intel_agp_chipsets[i].gmch_driver;
850 break;
851 }
852 }
853
854 if (!bridge->driver)
855 return 0;
856
857 bridge->dev_private_data = &intel_private;
858 bridge->dev = pdev;
859
860 dev_info(&pdev->dev, "Intel %s Chipset\n", intel_agp_chipsets[i].name);
861
862 if (bridge->driver->mask_memory == intel_gen6_mask_memory)
863 mask = 40;
864 else if (bridge->driver->mask_memory == intel_i965_mask_memory)
865 mask = 36;
866 else
867 mask = 32;
868
869 if (pci_set_dma_mask(intel_private.pcidev, DMA_BIT_MASK(mask)))
870 dev_err(&intel_private.pcidev->dev,
871 "set gfx device dma mask %d-bit failed!\n", mask);
872 else
873 pci_set_consistent_dma_mask(intel_private.pcidev,
874 DMA_BIT_MASK(mask));
875
876 return 1;
877}
878
879static int __devinit agp_intel_probe(struct pci_dev *pdev, 734static int __devinit agp_intel_probe(struct pci_dev *pdev,
880 const struct pci_device_id *ent) 735 const struct pci_device_id *ent)
881{ 736{
@@ -905,7 +760,7 @@ static int __devinit agp_intel_probe(struct pci_dev *pdev,
905 } 760 }
906 } 761 }
907 762
908 if (intel_agp_chipsets[i].name == NULL) { 763 if (!bridge->driver) {
909 if (cap_ptr) 764 if (cap_ptr)
910 dev_warn(&pdev->dev, "unsupported Intel chipset [%04x/%04x]\n", 765 dev_warn(&pdev->dev, "unsupported Intel chipset [%04x/%04x]\n",
911 pdev->vendor, pdev->device); 766 pdev->vendor, pdev->device);
@@ -913,34 +768,20 @@ static int __devinit agp_intel_probe(struct pci_dev *pdev,
913 return -ENODEV; 768 return -ENODEV;
914 } 769 }
915 770
916 if (!bridge->driver) {
917 if (cap_ptr)
918 dev_warn(&pdev->dev, "can't find bridge device (chip_id: %04x)\n",
919 intel_agp_chipsets[i].gmch_chip_id);
920 agp_put_bridge(bridge);
921 return -ENODEV;
922 }
923
924 bridge->dev = pdev; 771 bridge->dev = pdev;
925 bridge->dev_private_data = NULL; 772 bridge->dev_private_data = NULL;
926 773
927 dev_info(&pdev->dev, "Intel %s Chipset\n", intel_agp_chipsets[i].name); 774 dev_info(&pdev->dev, "Intel %s Chipset\n", intel_agp_chipsets[i].name);
928 775
929 /* 776 /*
930 * If the device has not been properly setup, the following will catch
931 * the problem and should stop the system from crashing.
932 * 20030610 - hamish@zot.org
933 */
934 if (pci_enable_device(pdev)) {
935 dev_err(&pdev->dev, "can't enable PCI device\n");
936 agp_put_bridge(bridge);
937 return -ENODEV;
938 }
939
940 /*
941 * The following fixes the case where the BIOS has "forgotten" to 777 * The following fixes the case where the BIOS has "forgotten" to
942 * provide an address range for the GART. 778 * provide an address range for the GART.
943 * 20030610 - hamish@zot.org 779 * 20030610 - hamish@zot.org
780 * This happens before pci_enable_device() intentionally;
781 * calling pci_enable_device() before assigning the resource
782 * will result in the GART being disabled on machines with such
783 * BIOSs (the GART ends up with a BAR starting at 0, which
784 * conflicts a lot of other devices).
944 */ 785 */
945 r = &pdev->resource[0]; 786 r = &pdev->resource[0];
946 if (!r->start && r->end) { 787 if (!r->start && r->end) {
@@ -951,6 +792,17 @@ static int __devinit agp_intel_probe(struct pci_dev *pdev,
951 } 792 }
952 } 793 }
953 794
795 /*
796 * If the device has not been properly setup, the following will catch
797 * the problem and should stop the system from crashing.
798 * 20030610 - hamish@zot.org
799 */
800 if (pci_enable_device(pdev)) {
801 dev_err(&pdev->dev, "can't enable PCI device\n");
802 agp_put_bridge(bridge);
803 return -ENODEV;
804 }
805
954 /* Fill in the mode register */ 806 /* Fill in the mode register */
955 if (cap_ptr) { 807 if (cap_ptr) {
956 pci_read_config_dword(pdev, 808 pci_read_config_dword(pdev,
@@ -972,8 +824,7 @@ static void __devexit agp_intel_remove(struct pci_dev *pdev)
972 824
973 agp_remove_bridge(bridge); 825 agp_remove_bridge(bridge);
974 826
975 if (intel_private.pcidev) 827 intel_gmch_remove(pdev);
976 pci_dev_put(intel_private.pcidev);
977 828
978 agp_put_bridge(bridge); 829 agp_put_bridge(bridge);
979} 830}
@@ -982,14 +833,9 @@ static void __devexit agp_intel_remove(struct pci_dev *pdev)
982static int agp_intel_resume(struct pci_dev *pdev) 833static int agp_intel_resume(struct pci_dev *pdev)
983{ 834{
984 struct agp_bridge_data *bridge = pci_get_drvdata(pdev); 835 struct agp_bridge_data *bridge = pci_get_drvdata(pdev);
985 int ret_val;
986 836
987 bridge->driver->configure(); 837 bridge->driver->configure();
988 838
989 ret_val = agp_rebind_memory();
990 if (ret_val != 0)
991 return ret_val;
992
993 return 0; 839 return 0;
994} 840}
995#endif 841#endif
@@ -1049,6 +895,7 @@ static struct pci_device_id agp_intel_pci_table[] = {
1049 ID(PCI_DEVICE_ID_INTEL_G45_HB), 895 ID(PCI_DEVICE_ID_INTEL_G45_HB),
1050 ID(PCI_DEVICE_ID_INTEL_G41_HB), 896 ID(PCI_DEVICE_ID_INTEL_G41_HB),
1051 ID(PCI_DEVICE_ID_INTEL_B43_HB), 897 ID(PCI_DEVICE_ID_INTEL_B43_HB),
898 ID(PCI_DEVICE_ID_INTEL_B43_1_HB),
1052 ID(PCI_DEVICE_ID_INTEL_IRONLAKE_D_HB), 899 ID(PCI_DEVICE_ID_INTEL_IRONLAKE_D_HB),
1053 ID(PCI_DEVICE_ID_INTEL_IRONLAKE_M_HB), 900 ID(PCI_DEVICE_ID_INTEL_IRONLAKE_M_HB),
1054 ID(PCI_DEVICE_ID_INTEL_IRONLAKE_MA_HB), 901 ID(PCI_DEVICE_ID_INTEL_IRONLAKE_MA_HB),
@@ -1056,6 +903,9 @@ static struct pci_device_id agp_intel_pci_table[] = {
1056 ID(PCI_DEVICE_ID_INTEL_SANDYBRIDGE_HB), 903 ID(PCI_DEVICE_ID_INTEL_SANDYBRIDGE_HB),
1057 ID(PCI_DEVICE_ID_INTEL_SANDYBRIDGE_M_HB), 904 ID(PCI_DEVICE_ID_INTEL_SANDYBRIDGE_M_HB),
1058 ID(PCI_DEVICE_ID_INTEL_SANDYBRIDGE_S_HB), 905 ID(PCI_DEVICE_ID_INTEL_SANDYBRIDGE_S_HB),
906 ID(PCI_DEVICE_ID_INTEL_IVYBRIDGE_HB),
907 ID(PCI_DEVICE_ID_INTEL_IVYBRIDGE_M_HB),
908 ID(PCI_DEVICE_ID_INTEL_IVYBRIDGE_S_HB),
1059 { } 909 { }
1060}; 910};
1061 911
diff --git a/drivers/char/agp/intel-agp.h b/drivers/char/agp/intel-agp.h
index d09b1ab7e8ab..5da67f165afa 100644
--- a/drivers/char/agp/intel-agp.h
+++ b/drivers/char/agp/intel-agp.h
@@ -75,6 +75,8 @@
75#define I810_GMS_DISABLE 0x00000000 75#define I810_GMS_DISABLE 0x00000000
76#define I810_PGETBL_CTL 0x2020 76#define I810_PGETBL_CTL 0x2020
77#define I810_PGETBL_ENABLED 0x00000001 77#define I810_PGETBL_ENABLED 0x00000001
78/* Note: PGETBL_CTL2 has a different offset on G33. */
79#define I965_PGETBL_CTL2 0x20c4
78#define I965_PGETBL_SIZE_MASK 0x0000000e 80#define I965_PGETBL_SIZE_MASK 0x0000000e
79#define I965_PGETBL_SIZE_512KB (0 << 1) 81#define I965_PGETBL_SIZE_512KB (0 << 1)
80#define I965_PGETBL_SIZE_256KB (1 << 1) 82#define I965_PGETBL_SIZE_256KB (1 << 1)
@@ -82,9 +84,18 @@
82#define I965_PGETBL_SIZE_1MB (3 << 1) 84#define I965_PGETBL_SIZE_1MB (3 << 1)
83#define I965_PGETBL_SIZE_2MB (4 << 1) 85#define I965_PGETBL_SIZE_2MB (4 << 1)
84#define I965_PGETBL_SIZE_1_5MB (5 << 1) 86#define I965_PGETBL_SIZE_1_5MB (5 << 1)
85#define G33_PGETBL_SIZE_MASK (3 << 8) 87#define G33_GMCH_SIZE_MASK (3 << 8)
86#define G33_PGETBL_SIZE_1M (1 << 8) 88#define G33_GMCH_SIZE_1M (1 << 8)
87#define G33_PGETBL_SIZE_2M (2 << 8) 89#define G33_GMCH_SIZE_2M (2 << 8)
90#define G4x_GMCH_SIZE_MASK (0xf << 8)
91#define G4x_GMCH_SIZE_1M (0x1 << 8)
92#define G4x_GMCH_SIZE_2M (0x3 << 8)
93#define G4x_GMCH_SIZE_VT_EN (0x8 << 8)
94#define G4x_GMCH_SIZE_VT_1M (G4x_GMCH_SIZE_1M | G4x_GMCH_SIZE_VT_EN)
95#define G4x_GMCH_SIZE_VT_1_5M ((0x2 << 8) | G4x_GMCH_SIZE_VT_EN)
96#define G4x_GMCH_SIZE_VT_2M (G4x_GMCH_SIZE_2M | G4x_GMCH_SIZE_VT_EN)
97
98#define GFX_FLSH_CNTL 0x2170 /* 915+ */
88 99
89#define I810_DRAM_CTL 0x3000 100#define I810_DRAM_CTL 0x3000
90#define I810_DRAM_ROW_0 0x00000001 101#define I810_DRAM_ROW_0 0x00000001
@@ -120,6 +131,7 @@
120#define INTEL_GMCH_GMS_STOLEN_352M (0xd << 4) 131#define INTEL_GMCH_GMS_STOLEN_352M (0xd << 4)
121 132
122#define I915_IFPADDR 0x60 133#define I915_IFPADDR 0x60
134#define I830_HIC 0x70
123 135
124/* Intel 965G registers */ 136/* Intel 965G registers */
125#define I965_MSAC 0x62 137#define I965_MSAC 0x62
@@ -214,45 +226,16 @@
214#define PCI_DEVICE_ID_INTEL_SANDYBRIDGE_M_GT2_PLUS_IG 0x0126 226#define PCI_DEVICE_ID_INTEL_SANDYBRIDGE_M_GT2_PLUS_IG 0x0126
215#define PCI_DEVICE_ID_INTEL_SANDYBRIDGE_S_HB 0x0108 /* Server */ 227#define PCI_DEVICE_ID_INTEL_SANDYBRIDGE_S_HB 0x0108 /* Server */
216#define PCI_DEVICE_ID_INTEL_SANDYBRIDGE_S_IG 0x010A 228#define PCI_DEVICE_ID_INTEL_SANDYBRIDGE_S_IG 0x010A
217 229#define PCI_DEVICE_ID_INTEL_IVYBRIDGE_HB 0x0150 /* Desktop */
218/* cover 915 and 945 variants */ 230#define PCI_DEVICE_ID_INTEL_IVYBRIDGE_GT1_IG 0x0152
219#define IS_I915 (agp_bridge->dev->device == PCI_DEVICE_ID_INTEL_E7221_HB || \ 231#define PCI_DEVICE_ID_INTEL_IVYBRIDGE_GT2_IG 0x0162
220 agp_bridge->dev->device == PCI_DEVICE_ID_INTEL_82915G_HB || \ 232#define PCI_DEVICE_ID_INTEL_IVYBRIDGE_M_HB 0x0154 /* Mobile */
221 agp_bridge->dev->device == PCI_DEVICE_ID_INTEL_82915GM_HB || \ 233#define PCI_DEVICE_ID_INTEL_IVYBRIDGE_M_GT1_IG 0x0156
222 agp_bridge->dev->device == PCI_DEVICE_ID_INTEL_82945G_HB || \ 234#define PCI_DEVICE_ID_INTEL_IVYBRIDGE_M_GT2_IG 0x0166
223 agp_bridge->dev->device == PCI_DEVICE_ID_INTEL_82945GM_HB || \ 235#define PCI_DEVICE_ID_INTEL_IVYBRIDGE_S_HB 0x0158 /* Server */
224 agp_bridge->dev->device == PCI_DEVICE_ID_INTEL_82945GME_HB) 236#define PCI_DEVICE_ID_INTEL_IVYBRIDGE_S_GT1_IG 0x015A
225 237
226#define IS_I965 (agp_bridge->dev->device == PCI_DEVICE_ID_INTEL_82946GZ_HB || \ 238int intel_gmch_probe(struct pci_dev *pdev,
227 agp_bridge->dev->device == PCI_DEVICE_ID_INTEL_82G35_HB || \ 239 struct agp_bridge_data *bridge);
228 agp_bridge->dev->device == PCI_DEVICE_ID_INTEL_82965Q_HB || \ 240void intel_gmch_remove(struct pci_dev *pdev);
229 agp_bridge->dev->device == PCI_DEVICE_ID_INTEL_82965G_HB || \
230 agp_bridge->dev->device == PCI_DEVICE_ID_INTEL_82965GM_HB || \
231 agp_bridge->dev->device == PCI_DEVICE_ID_INTEL_82965GME_HB)
232
233#define IS_G33 (agp_bridge->dev->device == PCI_DEVICE_ID_INTEL_G33_HB || \
234 agp_bridge->dev->device == PCI_DEVICE_ID_INTEL_Q35_HB || \
235 agp_bridge->dev->device == PCI_DEVICE_ID_INTEL_Q33_HB || \
236 agp_bridge->dev->device == PCI_DEVICE_ID_INTEL_PINEVIEW_M_HB || \
237 agp_bridge->dev->device == PCI_DEVICE_ID_INTEL_PINEVIEW_HB)
238
239#define IS_PINEVIEW (agp_bridge->dev->device == PCI_DEVICE_ID_INTEL_PINEVIEW_M_HB || \
240 agp_bridge->dev->device == PCI_DEVICE_ID_INTEL_PINEVIEW_HB)
241
242#define IS_SNB (agp_bridge->dev->device == PCI_DEVICE_ID_INTEL_SANDYBRIDGE_HB || \
243 agp_bridge->dev->device == PCI_DEVICE_ID_INTEL_SANDYBRIDGE_M_HB || \
244 agp_bridge->dev->device == PCI_DEVICE_ID_INTEL_SANDYBRIDGE_S_HB)
245
246#define IS_G4X (agp_bridge->dev->device == PCI_DEVICE_ID_INTEL_EAGLELAKE_HB || \
247 agp_bridge->dev->device == PCI_DEVICE_ID_INTEL_Q45_HB || \
248 agp_bridge->dev->device == PCI_DEVICE_ID_INTEL_G45_HB || \
249 agp_bridge->dev->device == PCI_DEVICE_ID_INTEL_GM45_HB || \
250 agp_bridge->dev->device == PCI_DEVICE_ID_INTEL_G41_HB || \
251 agp_bridge->dev->device == PCI_DEVICE_ID_INTEL_B43_HB || \
252 agp_bridge->dev->device == PCI_DEVICE_ID_INTEL_IRONLAKE_D_HB || \
253 agp_bridge->dev->device == PCI_DEVICE_ID_INTEL_IRONLAKE_M_HB || \
254 agp_bridge->dev->device == PCI_DEVICE_ID_INTEL_IRONLAKE_MA_HB || \
255 agp_bridge->dev->device == PCI_DEVICE_ID_INTEL_IRONLAKE_MC2_HB || \
256 IS_SNB)
257
258#endif 241#endif
diff --git a/drivers/char/agp/intel-gtt.c b/drivers/char/agp/intel-gtt.c
index 75e0a3497888..85151019dde1 100644
--- a/drivers/char/agp/intel-gtt.c
+++ b/drivers/char/agp/intel-gtt.c
@@ -15,6 +15,18 @@
15 * /fairy-tale-mode off 15 * /fairy-tale-mode off
16 */ 16 */
17 17
18#include <linux/module.h>
19#include <linux/pci.h>
20#include <linux/init.h>
21#include <linux/kernel.h>
22#include <linux/pagemap.h>
23#include <linux/agp_backend.h>
24#include <linux/delay.h>
25#include <asm/smp.h>
26#include "agp.h"
27#include "intel-agp.h"
28#include <drm/intel-gtt.h>
29
18/* 30/*
19 * If we have Intel graphics, we're not going to have anything other than 31 * If we have Intel graphics, we're not going to have anything other than
20 * an Intel IOMMU. So make the correct use of the PCI DMA API contingent 32 * an Intel IOMMU. So make the correct use of the PCI DMA API contingent
@@ -23,121 +35,79 @@
23 */ 35 */
24#ifdef CONFIG_DMAR 36#ifdef CONFIG_DMAR
25#define USE_PCI_DMA_API 1 37#define USE_PCI_DMA_API 1
38#else
39#define USE_PCI_DMA_API 0
26#endif 40#endif
27 41
28/* Max amount of stolen space, anything above will be returned to Linux */ 42struct intel_gtt_driver {
29int intel_max_stolen = 32 * 1024 * 1024; 43 unsigned int gen : 8;
30EXPORT_SYMBOL(intel_max_stolen); 44 unsigned int is_g33 : 1;
31 45 unsigned int is_pineview : 1;
32static const struct aper_size_info_fixed intel_i810_sizes[] = 46 unsigned int is_ironlake : 1;
33{ 47 unsigned int has_pgtbl_enable : 1;
34 {64, 16384, 4}, 48 unsigned int dma_mask_size : 8;
35 /* The 32M mode still requires a 64k gatt */ 49 /* Chipset specific GTT setup */
36 {32, 8192, 4} 50 int (*setup)(void);
37}; 51 /* This should undo anything done in ->setup() save the unmapping
38 52 * of the mmio register file, that's done in the generic code. */
39#define AGP_DCACHE_MEMORY 1 53 void (*cleanup)(void);
40#define AGP_PHYS_MEMORY 2 54 void (*write_entry)(dma_addr_t addr, unsigned int entry, unsigned int flags);
41#define INTEL_AGP_CACHED_MEMORY 3 55 /* Flags is a more or less chipset specific opaque value.
42 56 * For chipsets that need to support old ums (non-gem) code, this
43static struct gatt_mask intel_i810_masks[] = 57 * needs to be identical to the various supported agp memory types! */
44{ 58 bool (*check_flags)(unsigned int flags);
45 {.mask = I810_PTE_VALID, .type = 0}, 59 void (*chipset_flush)(void);
46 {.mask = (I810_PTE_VALID | I810_PTE_LOCAL), .type = AGP_DCACHE_MEMORY},
47 {.mask = I810_PTE_VALID, .type = 0},
48 {.mask = I810_PTE_VALID | I830_PTE_SYSTEM_CACHED,
49 .type = INTEL_AGP_CACHED_MEMORY}
50};
51
52#define INTEL_AGP_UNCACHED_MEMORY 0
53#define INTEL_AGP_CACHED_MEMORY_LLC 1
54#define INTEL_AGP_CACHED_MEMORY_LLC_GFDT 2
55#define INTEL_AGP_CACHED_MEMORY_LLC_MLC 3
56#define INTEL_AGP_CACHED_MEMORY_LLC_MLC_GFDT 4
57
58static struct gatt_mask intel_gen6_masks[] =
59{
60 {.mask = I810_PTE_VALID | GEN6_PTE_UNCACHED,
61 .type = INTEL_AGP_UNCACHED_MEMORY },
62 {.mask = I810_PTE_VALID | GEN6_PTE_LLC,
63 .type = INTEL_AGP_CACHED_MEMORY_LLC },
64 {.mask = I810_PTE_VALID | GEN6_PTE_LLC | GEN6_PTE_GFDT,
65 .type = INTEL_AGP_CACHED_MEMORY_LLC_GFDT },
66 {.mask = I810_PTE_VALID | GEN6_PTE_LLC_MLC,
67 .type = INTEL_AGP_CACHED_MEMORY_LLC_MLC },
68 {.mask = I810_PTE_VALID | GEN6_PTE_LLC_MLC | GEN6_PTE_GFDT,
69 .type = INTEL_AGP_CACHED_MEMORY_LLC_MLC_GFDT },
70}; 60};
71 61
72static struct _intel_private { 62static struct _intel_private {
63 struct intel_gtt base;
64 const struct intel_gtt_driver *driver;
73 struct pci_dev *pcidev; /* device one */ 65 struct pci_dev *pcidev; /* device one */
66 struct pci_dev *bridge_dev;
74 u8 __iomem *registers; 67 u8 __iomem *registers;
68 phys_addr_t gtt_bus_addr;
69 phys_addr_t gma_bus_addr;
70 u32 PGETBL_save;
75 u32 __iomem *gtt; /* I915G */ 71 u32 __iomem *gtt; /* I915G */
72 bool clear_fake_agp; /* on first access via agp, fill with scratch */
76 int num_dcache_entries; 73 int num_dcache_entries;
77 /* gtt_entries is the number of gtt entries that are already mapped 74 void __iomem *i9xx_flush_page;
78 * to stolen memory. Stolen memory is larger than the memory mapped 75 char *i81x_gtt_table;
79 * through gtt_entries, as it includes some reserved space for the BIOS
80 * popup and for the GTT.
81 */
82 int gtt_entries; /* i830+ */
83 int gtt_total_size;
84 union {
85 void __iomem *i9xx_flush_page;
86 void *i8xx_flush_page;
87 };
88 struct page *i8xx_page;
89 struct resource ifp_resource; 76 struct resource ifp_resource;
90 int resource_valid; 77 int resource_valid;
78 struct page *scratch_page;
79 dma_addr_t scratch_page_dma;
91} intel_private; 80} intel_private;
92 81
93#ifdef USE_PCI_DMA_API 82#define INTEL_GTT_GEN intel_private.driver->gen
94static int intel_agp_map_page(struct page *page, dma_addr_t *ret) 83#define IS_G33 intel_private.driver->is_g33
95{ 84#define IS_PINEVIEW intel_private.driver->is_pineview
96 *ret = pci_map_page(intel_private.pcidev, page, 0, 85#define IS_IRONLAKE intel_private.driver->is_ironlake
97 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL); 86#define HAS_PGTBL_EN intel_private.driver->has_pgtbl_enable
98 if (pci_dma_mapping_error(intel_private.pcidev, *ret))
99 return -EINVAL;
100 return 0;
101}
102 87
103static void intel_agp_unmap_page(struct page *page, dma_addr_t dma) 88int intel_gtt_map_memory(struct page **pages, unsigned int num_entries,
104{ 89 struct scatterlist **sg_list, int *num_sg)
105 pci_unmap_page(intel_private.pcidev, dma,
106 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
107}
108
109static void intel_agp_free_sglist(struct agp_memory *mem)
110{
111 struct sg_table st;
112
113 st.sgl = mem->sg_list;
114 st.orig_nents = st.nents = mem->page_count;
115
116 sg_free_table(&st);
117
118 mem->sg_list = NULL;
119 mem->num_sg = 0;
120}
121
122static int intel_agp_map_memory(struct agp_memory *mem)
123{ 90{
124 struct sg_table st; 91 struct sg_table st;
125 struct scatterlist *sg; 92 struct scatterlist *sg;
126 int i; 93 int i;
127 94
128 DBG("try mapping %lu pages\n", (unsigned long)mem->page_count); 95 if (*sg_list)
96 return 0; /* already mapped (for e.g. resume */
97
98 DBG("try mapping %lu pages\n", (unsigned long)num_entries);
129 99
130 if (sg_alloc_table(&st, mem->page_count, GFP_KERNEL)) 100 if (sg_alloc_table(&st, num_entries, GFP_KERNEL))
131 goto err; 101 goto err;
132 102
133 mem->sg_list = sg = st.sgl; 103 *sg_list = sg = st.sgl;
134 104
135 for (i = 0 ; i < mem->page_count; i++, sg = sg_next(sg)) 105 for (i = 0 ; i < num_entries; i++, sg = sg_next(sg))
136 sg_set_page(sg, mem->pages[i], PAGE_SIZE, 0); 106 sg_set_page(sg, pages[i], PAGE_SIZE, 0);
137 107
138 mem->num_sg = pci_map_sg(intel_private.pcidev, mem->sg_list, 108 *num_sg = pci_map_sg(intel_private.pcidev, *sg_list,
139 mem->page_count, PCI_DMA_BIDIRECTIONAL); 109 num_entries, PCI_DMA_BIDIRECTIONAL);
140 if (unlikely(!mem->num_sg)) 110 if (unlikely(!*num_sg))
141 goto err; 111 goto err;
142 112
143 return 0; 113 return 0;
@@ -146,145 +116,24 @@ err:
146 sg_free_table(&st); 116 sg_free_table(&st);
147 return -ENOMEM; 117 return -ENOMEM;
148} 118}
119EXPORT_SYMBOL(intel_gtt_map_memory);
149 120
150static void intel_agp_unmap_memory(struct agp_memory *mem) 121void intel_gtt_unmap_memory(struct scatterlist *sg_list, int num_sg)
151{ 122{
123 struct sg_table st;
152 DBG("try unmapping %lu pages\n", (unsigned long)mem->page_count); 124 DBG("try unmapping %lu pages\n", (unsigned long)mem->page_count);
153 125
154 pci_unmap_sg(intel_private.pcidev, mem->sg_list, 126 pci_unmap_sg(intel_private.pcidev, sg_list,
155 mem->page_count, PCI_DMA_BIDIRECTIONAL); 127 num_sg, PCI_DMA_BIDIRECTIONAL);
156 intel_agp_free_sglist(mem);
157}
158
159static void intel_agp_insert_sg_entries(struct agp_memory *mem,
160 off_t pg_start, int mask_type)
161{
162 struct scatterlist *sg;
163 int i, j;
164
165 j = pg_start;
166
167 WARN_ON(!mem->num_sg);
168
169 if (mem->num_sg == mem->page_count) {
170 for_each_sg(mem->sg_list, sg, mem->page_count, i) {
171 writel(agp_bridge->driver->mask_memory(agp_bridge,
172 sg_dma_address(sg), mask_type),
173 intel_private.gtt+j);
174 j++;
175 }
176 } else {
177 /* sg may merge pages, but we have to separate
178 * per-page addr for GTT */
179 unsigned int len, m;
180
181 for_each_sg(mem->sg_list, sg, mem->num_sg, i) {
182 len = sg_dma_len(sg) / PAGE_SIZE;
183 for (m = 0; m < len; m++) {
184 writel(agp_bridge->driver->mask_memory(agp_bridge,
185 sg_dma_address(sg) + m * PAGE_SIZE,
186 mask_type),
187 intel_private.gtt+j);
188 j++;
189 }
190 }
191 }
192 readl(intel_private.gtt+j-1);
193}
194
195#else
196
197static void intel_agp_insert_sg_entries(struct agp_memory *mem,
198 off_t pg_start, int mask_type)
199{
200 int i, j;
201
202 for (i = 0, j = pg_start; i < mem->page_count; i++, j++) {
203 writel(agp_bridge->driver->mask_memory(agp_bridge,
204 page_to_phys(mem->pages[i]), mask_type),
205 intel_private.gtt+j);
206 }
207
208 readl(intel_private.gtt+j-1);
209}
210
211#endif
212
213static int intel_i810_fetch_size(void)
214{
215 u32 smram_miscc;
216 struct aper_size_info_fixed *values;
217 128
218 pci_read_config_dword(agp_bridge->dev, I810_SMRAM_MISCC, &smram_miscc); 129 st.sgl = sg_list;
219 values = A_SIZE_FIX(agp_bridge->driver->aperture_sizes); 130 st.orig_nents = st.nents = num_sg;
220
221 if ((smram_miscc & I810_GMS) == I810_GMS_DISABLE) {
222 dev_warn(&agp_bridge->dev->dev, "i810 is disabled\n");
223 return 0;
224 }
225 if ((smram_miscc & I810_GFX_MEM_WIN_SIZE) == I810_GFX_MEM_WIN_32M) {
226 agp_bridge->current_size = (void *) (values + 1);
227 agp_bridge->aperture_size_idx = 1;
228 return values[1].size;
229 } else {
230 agp_bridge->current_size = (void *) (values);
231 agp_bridge->aperture_size_idx = 0;
232 return values[0].size;
233 }
234
235 return 0;
236}
237
238static int intel_i810_configure(void)
239{
240 struct aper_size_info_fixed *current_size;
241 u32 temp;
242 int i;
243
244 current_size = A_SIZE_FIX(agp_bridge->current_size);
245
246 if (!intel_private.registers) {
247 pci_read_config_dword(intel_private.pcidev, I810_MMADDR, &temp);
248 temp &= 0xfff80000;
249
250 intel_private.registers = ioremap(temp, 128 * 4096);
251 if (!intel_private.registers) {
252 dev_err(&intel_private.pcidev->dev,
253 "can't remap memory\n");
254 return -ENOMEM;
255 }
256 }
257
258 if ((readl(intel_private.registers+I810_DRAM_CTL)
259 & I810_DRAM_ROW_0) == I810_DRAM_ROW_0_SDRAM) {
260 /* This will need to be dynamically assigned */
261 dev_info(&intel_private.pcidev->dev,
262 "detected 4MB dedicated video ram\n");
263 intel_private.num_dcache_entries = 1024;
264 }
265 pci_read_config_dword(intel_private.pcidev, I810_GMADDR, &temp);
266 agp_bridge->gart_bus_addr = (temp & PCI_BASE_ADDRESS_MEM_MASK);
267 writel(agp_bridge->gatt_bus_addr | I810_PGETBL_ENABLED, intel_private.registers+I810_PGETBL_CTL);
268 readl(intel_private.registers+I810_PGETBL_CTL); /* PCI Posting. */
269
270 if (agp_bridge->driver->needs_scratch_page) {
271 for (i = 0; i < current_size->num_entries; i++) {
272 writel(agp_bridge->scratch_page, intel_private.registers+I810_PTE_BASE+(i*4));
273 }
274 readl(intel_private.registers+I810_PTE_BASE+((i-1)*4)); /* PCI posting. */
275 }
276 global_cache_flush();
277 return 0;
278}
279 131
280static void intel_i810_cleanup(void) 132 sg_free_table(&st);
281{
282 writel(0, intel_private.registers+I810_PGETBL_CTL);
283 readl(intel_private.registers); /* PCI Posting. */
284 iounmap(intel_private.registers);
285} 133}
134EXPORT_SYMBOL(intel_gtt_unmap_memory);
286 135
287static void intel_i810_agp_enable(struct agp_bridge_data *bridge, u32 mode) 136static void intel_fake_agp_enable(struct agp_bridge_data *bridge, u32 mode)
288{ 137{
289 return; 138 return;
290} 139}
@@ -319,108 +168,64 @@ static void i8xx_destroy_pages(struct page *page)
319 atomic_dec(&agp_bridge->current_memory_agp); 168 atomic_dec(&agp_bridge->current_memory_agp);
320} 169}
321 170
322static int intel_i830_type_to_mask_type(struct agp_bridge_data *bridge, 171#define I810_GTT_ORDER 4
323 int type) 172static int i810_setup(void)
324{
325 if (type < AGP_USER_TYPES)
326 return type;
327 else if (type == AGP_USER_CACHED_MEMORY)
328 return INTEL_AGP_CACHED_MEMORY;
329 else
330 return 0;
331}
332
333static int intel_gen6_type_to_mask_type(struct agp_bridge_data *bridge,
334 int type)
335{ 173{
336 unsigned int type_mask = type & ~AGP_USER_CACHED_MEMORY_GFDT; 174 u32 reg_addr;
337 unsigned int gfdt = type & AGP_USER_CACHED_MEMORY_GFDT; 175 char *gtt_table;
338
339 if (type_mask == AGP_USER_UNCACHED_MEMORY)
340 return INTEL_AGP_UNCACHED_MEMORY;
341 else if (type_mask == AGP_USER_CACHED_MEMORY_LLC_MLC)
342 return gfdt ? INTEL_AGP_CACHED_MEMORY_LLC_MLC_GFDT :
343 INTEL_AGP_CACHED_MEMORY_LLC_MLC;
344 else /* set 'normal'/'cached' to LLC by default */
345 return gfdt ? INTEL_AGP_CACHED_MEMORY_LLC_GFDT :
346 INTEL_AGP_CACHED_MEMORY_LLC;
347}
348 176
177 /* i81x does not preallocate the gtt. It's always 64kb in size. */
178 gtt_table = alloc_gatt_pages(I810_GTT_ORDER);
179 if (gtt_table == NULL)
180 return -ENOMEM;
181 intel_private.i81x_gtt_table = gtt_table;
349 182
350static int intel_i810_insert_entries(struct agp_memory *mem, off_t pg_start, 183 pci_read_config_dword(intel_private.pcidev, I810_MMADDR, &reg_addr);
351 int type) 184 reg_addr &= 0xfff80000;
352{
353 int i, j, num_entries;
354 void *temp;
355 int ret = -EINVAL;
356 int mask_type;
357
358 if (mem->page_count == 0)
359 goto out;
360 185
361 temp = agp_bridge->current_size; 186 intel_private.registers = ioremap(reg_addr, KB(64));
362 num_entries = A_SIZE_FIX(temp)->num_entries; 187 if (!intel_private.registers)
188 return -ENOMEM;
363 189
364 if ((pg_start + mem->page_count) > num_entries) 190 writel(virt_to_phys(gtt_table) | I810_PGETBL_ENABLED,
365 goto out_err; 191 intel_private.registers+I810_PGETBL_CTL);
366 192
193 intel_private.gtt_bus_addr = reg_addr + I810_PTE_BASE;
367 194
368 for (j = pg_start; j < (pg_start + mem->page_count); j++) { 195 if ((readl(intel_private.registers+I810_DRAM_CTL)
369 if (!PGE_EMPTY(agp_bridge, readl(agp_bridge->gatt_table+j))) { 196 & I810_DRAM_ROW_0) == I810_DRAM_ROW_0_SDRAM) {
370 ret = -EBUSY; 197 dev_info(&intel_private.pcidev->dev,
371 goto out_err; 198 "detected 4MB dedicated video ram\n");
372 } 199 intel_private.num_dcache_entries = 1024;
373 } 200 }
374 201
375 if (type != mem->type) 202 return 0;
376 goto out_err; 203}
377
378 mask_type = agp_bridge->driver->agp_type_to_mask_type(agp_bridge, type);
379
380 switch (mask_type) {
381 case AGP_DCACHE_MEMORY:
382 if (!mem->is_flushed)
383 global_cache_flush();
384 for (i = pg_start; i < (pg_start + mem->page_count); i++) {
385 writel((i*4096)|I810_PTE_LOCAL|I810_PTE_VALID,
386 intel_private.registers+I810_PTE_BASE+(i*4));
387 }
388 readl(intel_private.registers+I810_PTE_BASE+((i-1)*4));
389 break;
390 case AGP_PHYS_MEMORY:
391 case AGP_NORMAL_MEMORY:
392 if (!mem->is_flushed)
393 global_cache_flush();
394 for (i = 0, j = pg_start; i < mem->page_count; i++, j++) {
395 writel(agp_bridge->driver->mask_memory(agp_bridge,
396 page_to_phys(mem->pages[i]), mask_type),
397 intel_private.registers+I810_PTE_BASE+(j*4));
398 }
399 readl(intel_private.registers+I810_PTE_BASE+((j-1)*4));
400 break;
401 default:
402 goto out_err;
403 }
404 204
405out: 205static void i810_cleanup(void)
406 ret = 0; 206{
407out_err: 207 writel(0, intel_private.registers+I810_PGETBL_CTL);
408 mem->is_flushed = true; 208 free_gatt_pages(intel_private.i81x_gtt_table, I810_GTT_ORDER);
409 return ret;
410} 209}
411 210
412static int intel_i810_remove_entries(struct agp_memory *mem, off_t pg_start, 211static int i810_insert_dcache_entries(struct agp_memory *mem, off_t pg_start,
413 int type) 212 int type)
414{ 213{
415 int i; 214 int i;
416 215
417 if (mem->page_count == 0) 216 if ((pg_start + mem->page_count)
418 return 0; 217 > intel_private.num_dcache_entries)
218 return -EINVAL;
219
220 if (!mem->is_flushed)
221 global_cache_flush();
419 222
420 for (i = pg_start; i < (mem->page_count + pg_start); i++) { 223 for (i = pg_start; i < (pg_start + mem->page_count); i++) {
421 writel(agp_bridge->scratch_page, intel_private.registers+I810_PTE_BASE+(i*4)); 224 dma_addr_t addr = i << PAGE_SHIFT;
225 intel_private.driver->write_entry(addr,
226 i, type);
422 } 227 }
423 readl(intel_private.registers+I810_PTE_BASE+((i-1)*4)); 228 readl(intel_private.gtt+i-1);
424 229
425 return 0; 230 return 0;
426} 231}
@@ -467,29 +272,6 @@ static struct agp_memory *alloc_agpphysmem_i8xx(size_t pg_count, int type)
467 return new; 272 return new;
468} 273}
469 274
470static struct agp_memory *intel_i810_alloc_by_type(size_t pg_count, int type)
471{
472 struct agp_memory *new;
473
474 if (type == AGP_DCACHE_MEMORY) {
475 if (pg_count != intel_private.num_dcache_entries)
476 return NULL;
477
478 new = agp_create_memory(1);
479 if (new == NULL)
480 return NULL;
481
482 new->type = AGP_DCACHE_MEMORY;
483 new->page_count = pg_count;
484 new->num_scratch_pages = 0;
485 agp_free_page_array(new);
486 return new;
487 }
488 if (type == AGP_PHYS_MEMORY)
489 return alloc_agpphysmem_i8xx(pg_count, type);
490 return NULL;
491}
492
493static void intel_i810_free_by_type(struct agp_memory *curr) 275static void intel_i810_free_by_type(struct agp_memory *curr)
494{ 276{
495 agp_free_key(curr->key); 277 agp_free_key(curr->key);
@@ -507,118 +289,94 @@ static void intel_i810_free_by_type(struct agp_memory *curr)
507 kfree(curr); 289 kfree(curr);
508} 290}
509 291
510static unsigned long intel_i810_mask_memory(struct agp_bridge_data *bridge, 292static int intel_gtt_setup_scratch_page(void)
511 dma_addr_t addr, int type)
512{ 293{
513 /* Type checking must be done elsewhere */ 294 struct page *page;
514 return addr | bridge->driver->masks[type].mask; 295 dma_addr_t dma_addr;
296
297 page = alloc_page(GFP_KERNEL | GFP_DMA32 | __GFP_ZERO);
298 if (page == NULL)
299 return -ENOMEM;
300 get_page(page);
301 set_pages_uc(page, 1);
302
303 if (intel_private.base.needs_dmar) {
304 dma_addr = pci_map_page(intel_private.pcidev, page, 0,
305 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
306 if (pci_dma_mapping_error(intel_private.pcidev, dma_addr))
307 return -EINVAL;
308
309 intel_private.scratch_page_dma = dma_addr;
310 } else
311 intel_private.scratch_page_dma = page_to_phys(page);
312
313 intel_private.scratch_page = page;
314
315 return 0;
515} 316}
516 317
517static struct aper_size_info_fixed intel_i830_sizes[] = 318static void i810_write_entry(dma_addr_t addr, unsigned int entry,
319 unsigned int flags)
518{ 320{
321 u32 pte_flags = I810_PTE_VALID;
322
323 switch (flags) {
324 case AGP_DCACHE_MEMORY:
325 pte_flags |= I810_PTE_LOCAL;
326 break;
327 case AGP_USER_CACHED_MEMORY:
328 pte_flags |= I830_PTE_SYSTEM_CACHED;
329 break;
330 }
331
332 writel(addr | pte_flags, intel_private.gtt + entry);
333}
334
335static const struct aper_size_info_fixed intel_fake_agp_sizes[] = {
336 {32, 8192, 3},
337 {64, 16384, 4},
519 {128, 32768, 5}, 338 {128, 32768, 5},
520 /* The 64M mode still requires a 128k gatt */
521 {64, 16384, 5},
522 {256, 65536, 6}, 339 {256, 65536, 6},
523 {512, 131072, 7}, 340 {512, 131072, 7},
524}; 341};
525 342
526static void intel_i830_init_gtt_entries(void) 343static unsigned int intel_gtt_stolen_size(void)
527{ 344{
528 u16 gmch_ctrl; 345 u16 gmch_ctrl;
529 int gtt_entries = 0;
530 u8 rdct; 346 u8 rdct;
531 int local = 0; 347 int local = 0;
532 static const int ddt[4] = { 0, 16, 32, 64 }; 348 static const int ddt[4] = { 0, 16, 32, 64 };
533 int size; /* reserved space (in kb) at the top of stolen memory */ 349 unsigned int stolen_size = 0;
534 350
535 pci_read_config_word(agp_bridge->dev, I830_GMCH_CTRL, &gmch_ctrl); 351 if (INTEL_GTT_GEN == 1)
352 return 0; /* no stolen mem on i81x */
536 353
537 if (IS_I965) { 354 pci_read_config_word(intel_private.bridge_dev,
538 u32 pgetbl_ctl; 355 I830_GMCH_CTRL, &gmch_ctrl);
539 pgetbl_ctl = readl(intel_private.registers+I810_PGETBL_CTL);
540
541 /* The 965 has a field telling us the size of the GTT,
542 * which may be larger than what is necessary to map the
543 * aperture.
544 */
545 switch (pgetbl_ctl & I965_PGETBL_SIZE_MASK) {
546 case I965_PGETBL_SIZE_128KB:
547 size = 128;
548 break;
549 case I965_PGETBL_SIZE_256KB:
550 size = 256;
551 break;
552 case I965_PGETBL_SIZE_512KB:
553 size = 512;
554 break;
555 case I965_PGETBL_SIZE_1MB:
556 size = 1024;
557 break;
558 case I965_PGETBL_SIZE_2MB:
559 size = 2048;
560 break;
561 case I965_PGETBL_SIZE_1_5MB:
562 size = 1024 + 512;
563 break;
564 default:
565 dev_info(&intel_private.pcidev->dev,
566 "unknown page table size, assuming 512KB\n");
567 size = 512;
568 }
569 size += 4; /* add in BIOS popup space */
570 } else if (IS_G33 && !IS_PINEVIEW) {
571 /* G33's GTT size defined in gmch_ctrl */
572 switch (gmch_ctrl & G33_PGETBL_SIZE_MASK) {
573 case G33_PGETBL_SIZE_1M:
574 size = 1024;
575 break;
576 case G33_PGETBL_SIZE_2M:
577 size = 2048;
578 break;
579 default:
580 dev_info(&agp_bridge->dev->dev,
581 "unknown page table size 0x%x, assuming 512KB\n",
582 (gmch_ctrl & G33_PGETBL_SIZE_MASK));
583 size = 512;
584 }
585 size += 4;
586 } else if (IS_G4X || IS_PINEVIEW) {
587 /* On 4 series hardware, GTT stolen is separate from graphics
588 * stolen, ignore it in stolen gtt entries counting. However,
589 * 4KB of the stolen memory doesn't get mapped to the GTT.
590 */
591 size = 4;
592 } else {
593 /* On previous hardware, the GTT size was just what was
594 * required to map the aperture.
595 */
596 size = agp_bridge->driver->fetch_size() + 4;
597 }
598 356
599 if (agp_bridge->dev->device == PCI_DEVICE_ID_INTEL_82830_HB || 357 if (intel_private.bridge_dev->device == PCI_DEVICE_ID_INTEL_82830_HB ||
600 agp_bridge->dev->device == PCI_DEVICE_ID_INTEL_82845G_HB) { 358 intel_private.bridge_dev->device == PCI_DEVICE_ID_INTEL_82845G_HB) {
601 switch (gmch_ctrl & I830_GMCH_GMS_MASK) { 359 switch (gmch_ctrl & I830_GMCH_GMS_MASK) {
602 case I830_GMCH_GMS_STOLEN_512: 360 case I830_GMCH_GMS_STOLEN_512:
603 gtt_entries = KB(512) - KB(size); 361 stolen_size = KB(512);
604 break; 362 break;
605 case I830_GMCH_GMS_STOLEN_1024: 363 case I830_GMCH_GMS_STOLEN_1024:
606 gtt_entries = MB(1) - KB(size); 364 stolen_size = MB(1);
607 break; 365 break;
608 case I830_GMCH_GMS_STOLEN_8192: 366 case I830_GMCH_GMS_STOLEN_8192:
609 gtt_entries = MB(8) - KB(size); 367 stolen_size = MB(8);
610 break; 368 break;
611 case I830_GMCH_GMS_LOCAL: 369 case I830_GMCH_GMS_LOCAL:
612 rdct = readb(intel_private.registers+I830_RDRAM_CHANNEL_TYPE); 370 rdct = readb(intel_private.registers+I830_RDRAM_CHANNEL_TYPE);
613 gtt_entries = (I830_RDRAM_ND(rdct) + 1) * 371 stolen_size = (I830_RDRAM_ND(rdct) + 1) *
614 MB(ddt[I830_RDRAM_DDT(rdct)]); 372 MB(ddt[I830_RDRAM_DDT(rdct)]);
615 local = 1; 373 local = 1;
616 break; 374 break;
617 default: 375 default:
618 gtt_entries = 0; 376 stolen_size = 0;
619 break; 377 break;
620 } 378 }
621 } else if (IS_SNB) { 379 } else if (INTEL_GTT_GEN == 6) {
622 /* 380 /*
623 * SandyBridge has new memory control reg at 0x50.w 381 * SandyBridge has new memory control reg at 0x50.w
624 */ 382 */
@@ -626,167 +384,341 @@ static void intel_i830_init_gtt_entries(void)
626 pci_read_config_word(intel_private.pcidev, SNB_GMCH_CTRL, &snb_gmch_ctl); 384 pci_read_config_word(intel_private.pcidev, SNB_GMCH_CTRL, &snb_gmch_ctl);
627 switch (snb_gmch_ctl & SNB_GMCH_GMS_STOLEN_MASK) { 385 switch (snb_gmch_ctl & SNB_GMCH_GMS_STOLEN_MASK) {
628 case SNB_GMCH_GMS_STOLEN_32M: 386 case SNB_GMCH_GMS_STOLEN_32M:
629 gtt_entries = MB(32) - KB(size); 387 stolen_size = MB(32);
630 break; 388 break;
631 case SNB_GMCH_GMS_STOLEN_64M: 389 case SNB_GMCH_GMS_STOLEN_64M:
632 gtt_entries = MB(64) - KB(size); 390 stolen_size = MB(64);
633 break; 391 break;
634 case SNB_GMCH_GMS_STOLEN_96M: 392 case SNB_GMCH_GMS_STOLEN_96M:
635 gtt_entries = MB(96) - KB(size); 393 stolen_size = MB(96);
636 break; 394 break;
637 case SNB_GMCH_GMS_STOLEN_128M: 395 case SNB_GMCH_GMS_STOLEN_128M:
638 gtt_entries = MB(128) - KB(size); 396 stolen_size = MB(128);
639 break; 397 break;
640 case SNB_GMCH_GMS_STOLEN_160M: 398 case SNB_GMCH_GMS_STOLEN_160M:
641 gtt_entries = MB(160) - KB(size); 399 stolen_size = MB(160);
642 break; 400 break;
643 case SNB_GMCH_GMS_STOLEN_192M: 401 case SNB_GMCH_GMS_STOLEN_192M:
644 gtt_entries = MB(192) - KB(size); 402 stolen_size = MB(192);
645 break; 403 break;
646 case SNB_GMCH_GMS_STOLEN_224M: 404 case SNB_GMCH_GMS_STOLEN_224M:
647 gtt_entries = MB(224) - KB(size); 405 stolen_size = MB(224);
648 break; 406 break;
649 case SNB_GMCH_GMS_STOLEN_256M: 407 case SNB_GMCH_GMS_STOLEN_256M:
650 gtt_entries = MB(256) - KB(size); 408 stolen_size = MB(256);
651 break; 409 break;
652 case SNB_GMCH_GMS_STOLEN_288M: 410 case SNB_GMCH_GMS_STOLEN_288M:
653 gtt_entries = MB(288) - KB(size); 411 stolen_size = MB(288);
654 break; 412 break;
655 case SNB_GMCH_GMS_STOLEN_320M: 413 case SNB_GMCH_GMS_STOLEN_320M:
656 gtt_entries = MB(320) - KB(size); 414 stolen_size = MB(320);
657 break; 415 break;
658 case SNB_GMCH_GMS_STOLEN_352M: 416 case SNB_GMCH_GMS_STOLEN_352M:
659 gtt_entries = MB(352) - KB(size); 417 stolen_size = MB(352);
660 break; 418 break;
661 case SNB_GMCH_GMS_STOLEN_384M: 419 case SNB_GMCH_GMS_STOLEN_384M:
662 gtt_entries = MB(384) - KB(size); 420 stolen_size = MB(384);
663 break; 421 break;
664 case SNB_GMCH_GMS_STOLEN_416M: 422 case SNB_GMCH_GMS_STOLEN_416M:
665 gtt_entries = MB(416) - KB(size); 423 stolen_size = MB(416);
666 break; 424 break;
667 case SNB_GMCH_GMS_STOLEN_448M: 425 case SNB_GMCH_GMS_STOLEN_448M:
668 gtt_entries = MB(448) - KB(size); 426 stolen_size = MB(448);
669 break; 427 break;
670 case SNB_GMCH_GMS_STOLEN_480M: 428 case SNB_GMCH_GMS_STOLEN_480M:
671 gtt_entries = MB(480) - KB(size); 429 stolen_size = MB(480);
672 break; 430 break;
673 case SNB_GMCH_GMS_STOLEN_512M: 431 case SNB_GMCH_GMS_STOLEN_512M:
674 gtt_entries = MB(512) - KB(size); 432 stolen_size = MB(512);
675 break; 433 break;
676 } 434 }
677 } else { 435 } else {
678 switch (gmch_ctrl & I855_GMCH_GMS_MASK) { 436 switch (gmch_ctrl & I855_GMCH_GMS_MASK) {
679 case I855_GMCH_GMS_STOLEN_1M: 437 case I855_GMCH_GMS_STOLEN_1M:
680 gtt_entries = MB(1) - KB(size); 438 stolen_size = MB(1);
681 break; 439 break;
682 case I855_GMCH_GMS_STOLEN_4M: 440 case I855_GMCH_GMS_STOLEN_4M:
683 gtt_entries = MB(4) - KB(size); 441 stolen_size = MB(4);
684 break; 442 break;
685 case I855_GMCH_GMS_STOLEN_8M: 443 case I855_GMCH_GMS_STOLEN_8M:
686 gtt_entries = MB(8) - KB(size); 444 stolen_size = MB(8);
687 break; 445 break;
688 case I855_GMCH_GMS_STOLEN_16M: 446 case I855_GMCH_GMS_STOLEN_16M:
689 gtt_entries = MB(16) - KB(size); 447 stolen_size = MB(16);
690 break; 448 break;
691 case I855_GMCH_GMS_STOLEN_32M: 449 case I855_GMCH_GMS_STOLEN_32M:
692 gtt_entries = MB(32) - KB(size); 450 stolen_size = MB(32);
693 break; 451 break;
694 case I915_GMCH_GMS_STOLEN_48M: 452 case I915_GMCH_GMS_STOLEN_48M:
695 /* Check it's really I915G */ 453 stolen_size = MB(48);
696 if (IS_I915 || IS_I965 || IS_G33 || IS_G4X)
697 gtt_entries = MB(48) - KB(size);
698 else
699 gtt_entries = 0;
700 break; 454 break;
701 case I915_GMCH_GMS_STOLEN_64M: 455 case I915_GMCH_GMS_STOLEN_64M:
702 /* Check it's really I915G */ 456 stolen_size = MB(64);
703 if (IS_I915 || IS_I965 || IS_G33 || IS_G4X)
704 gtt_entries = MB(64) - KB(size);
705 else
706 gtt_entries = 0;
707 break; 457 break;
708 case G33_GMCH_GMS_STOLEN_128M: 458 case G33_GMCH_GMS_STOLEN_128M:
709 if (IS_G33 || IS_I965 || IS_G4X) 459 stolen_size = MB(128);
710 gtt_entries = MB(128) - KB(size);
711 else
712 gtt_entries = 0;
713 break; 460 break;
714 case G33_GMCH_GMS_STOLEN_256M: 461 case G33_GMCH_GMS_STOLEN_256M:
715 if (IS_G33 || IS_I965 || IS_G4X) 462 stolen_size = MB(256);
716 gtt_entries = MB(256) - KB(size);
717 else
718 gtt_entries = 0;
719 break; 463 break;
720 case INTEL_GMCH_GMS_STOLEN_96M: 464 case INTEL_GMCH_GMS_STOLEN_96M:
721 if (IS_I965 || IS_G4X) 465 stolen_size = MB(96);
722 gtt_entries = MB(96) - KB(size);
723 else
724 gtt_entries = 0;
725 break; 466 break;
726 case INTEL_GMCH_GMS_STOLEN_160M: 467 case INTEL_GMCH_GMS_STOLEN_160M:
727 if (IS_I965 || IS_G4X) 468 stolen_size = MB(160);
728 gtt_entries = MB(160) - KB(size);
729 else
730 gtt_entries = 0;
731 break; 469 break;
732 case INTEL_GMCH_GMS_STOLEN_224M: 470 case INTEL_GMCH_GMS_STOLEN_224M:
733 if (IS_I965 || IS_G4X) 471 stolen_size = MB(224);
734 gtt_entries = MB(224) - KB(size);
735 else
736 gtt_entries = 0;
737 break; 472 break;
738 case INTEL_GMCH_GMS_STOLEN_352M: 473 case INTEL_GMCH_GMS_STOLEN_352M:
739 if (IS_I965 || IS_G4X) 474 stolen_size = MB(352);
740 gtt_entries = MB(352) - KB(size);
741 else
742 gtt_entries = 0;
743 break; 475 break;
744 default: 476 default:
745 gtt_entries = 0; 477 stolen_size = 0;
746 break; 478 break;
747 } 479 }
748 } 480 }
749 if (!local && gtt_entries > intel_max_stolen) { 481
750 dev_info(&agp_bridge->dev->dev, 482 if (stolen_size > 0) {
751 "detected %dK stolen memory, trimming to %dK\n", 483 dev_info(&intel_private.bridge_dev->dev, "detected %dK %s memory\n",
752 gtt_entries / KB(1), intel_max_stolen / KB(1)); 484 stolen_size / KB(1), local ? "local" : "stolen");
753 gtt_entries = intel_max_stolen / KB(4);
754 } else if (gtt_entries > 0) {
755 dev_info(&agp_bridge->dev->dev, "detected %dK %s memory\n",
756 gtt_entries / KB(1), local ? "local" : "stolen");
757 gtt_entries /= KB(4);
758 } else { 485 } else {
759 dev_info(&agp_bridge->dev->dev, 486 dev_info(&intel_private.bridge_dev->dev,
760 "no pre-allocated video memory detected\n"); 487 "no pre-allocated video memory detected\n");
761 gtt_entries = 0; 488 stolen_size = 0;
762 } 489 }
763 490
764 intel_private.gtt_entries = gtt_entries; 491 return stolen_size;
492}
493
494static void i965_adjust_pgetbl_size(unsigned int size_flag)
495{
496 u32 pgetbl_ctl, pgetbl_ctl2;
497
498 /* ensure that ppgtt is disabled */
499 pgetbl_ctl2 = readl(intel_private.registers+I965_PGETBL_CTL2);
500 pgetbl_ctl2 &= ~I810_PGETBL_ENABLED;
501 writel(pgetbl_ctl2, intel_private.registers+I965_PGETBL_CTL2);
502
503 /* write the new ggtt size */
504 pgetbl_ctl = readl(intel_private.registers+I810_PGETBL_CTL);
505 pgetbl_ctl &= ~I965_PGETBL_SIZE_MASK;
506 pgetbl_ctl |= size_flag;
507 writel(pgetbl_ctl, intel_private.registers+I810_PGETBL_CTL);
765} 508}
766 509
767static void intel_i830_fini_flush(void) 510static unsigned int i965_gtt_total_entries(void)
768{ 511{
769 kunmap(intel_private.i8xx_page); 512 int size;
770 intel_private.i8xx_flush_page = NULL; 513 u32 pgetbl_ctl;
771 unmap_page_from_agp(intel_private.i8xx_page); 514 u16 gmch_ctl;
515
516 pci_read_config_word(intel_private.bridge_dev,
517 I830_GMCH_CTRL, &gmch_ctl);
772 518
773 __free_page(intel_private.i8xx_page); 519 if (INTEL_GTT_GEN == 5) {
774 intel_private.i8xx_page = NULL; 520 switch (gmch_ctl & G4x_GMCH_SIZE_MASK) {
521 case G4x_GMCH_SIZE_1M:
522 case G4x_GMCH_SIZE_VT_1M:
523 i965_adjust_pgetbl_size(I965_PGETBL_SIZE_1MB);
524 break;
525 case G4x_GMCH_SIZE_VT_1_5M:
526 i965_adjust_pgetbl_size(I965_PGETBL_SIZE_1_5MB);
527 break;
528 case G4x_GMCH_SIZE_2M:
529 case G4x_GMCH_SIZE_VT_2M:
530 i965_adjust_pgetbl_size(I965_PGETBL_SIZE_2MB);
531 break;
532 }
533 }
534
535 pgetbl_ctl = readl(intel_private.registers+I810_PGETBL_CTL);
536
537 switch (pgetbl_ctl & I965_PGETBL_SIZE_MASK) {
538 case I965_PGETBL_SIZE_128KB:
539 size = KB(128);
540 break;
541 case I965_PGETBL_SIZE_256KB:
542 size = KB(256);
543 break;
544 case I965_PGETBL_SIZE_512KB:
545 size = KB(512);
546 break;
547 /* GTT pagetable sizes bigger than 512KB are not possible on G33! */
548 case I965_PGETBL_SIZE_1MB:
549 size = KB(1024);
550 break;
551 case I965_PGETBL_SIZE_2MB:
552 size = KB(2048);
553 break;
554 case I965_PGETBL_SIZE_1_5MB:
555 size = KB(1024 + 512);
556 break;
557 default:
558 dev_info(&intel_private.pcidev->dev,
559 "unknown page table size, assuming 512KB\n");
560 size = KB(512);
561 }
562
563 return size/4;
775} 564}
776 565
777static void intel_i830_setup_flush(void) 566static unsigned int intel_gtt_total_entries(void)
778{ 567{
779 /* return if we've already set the flush mechanism up */ 568 int size;
780 if (intel_private.i8xx_page)
781 return;
782 569
783 intel_private.i8xx_page = alloc_page(GFP_KERNEL | __GFP_ZERO | GFP_DMA32); 570 if (IS_G33 || INTEL_GTT_GEN == 4 || INTEL_GTT_GEN == 5)
784 if (!intel_private.i8xx_page) 571 return i965_gtt_total_entries();
785 return; 572 else if (INTEL_GTT_GEN == 6) {
573 u16 snb_gmch_ctl;
574
575 pci_read_config_word(intel_private.pcidev, SNB_GMCH_CTRL, &snb_gmch_ctl);
576 switch (snb_gmch_ctl & SNB_GTT_SIZE_MASK) {
577 default:
578 case SNB_GTT_SIZE_0M:
579 printk(KERN_ERR "Bad GTT size mask: 0x%04x.\n", snb_gmch_ctl);
580 size = MB(0);
581 break;
582 case SNB_GTT_SIZE_1M:
583 size = MB(1);
584 break;
585 case SNB_GTT_SIZE_2M:
586 size = MB(2);
587 break;
588 }
589 return size/4;
590 } else {
591 /* On previous hardware, the GTT size was just what was
592 * required to map the aperture.
593 */
594 return intel_private.base.gtt_mappable_entries;
595 }
596}
597
598static unsigned int intel_gtt_mappable_entries(void)
599{
600 unsigned int aperture_size;
601
602 if (INTEL_GTT_GEN == 1) {
603 u32 smram_miscc;
604
605 pci_read_config_dword(intel_private.bridge_dev,
606 I810_SMRAM_MISCC, &smram_miscc);
607
608 if ((smram_miscc & I810_GFX_MEM_WIN_SIZE)
609 == I810_GFX_MEM_WIN_32M)
610 aperture_size = MB(32);
611 else
612 aperture_size = MB(64);
613 } else if (INTEL_GTT_GEN == 2) {
614 u16 gmch_ctrl;
615
616 pci_read_config_word(intel_private.bridge_dev,
617 I830_GMCH_CTRL, &gmch_ctrl);
618
619 if ((gmch_ctrl & I830_GMCH_MEM_MASK) == I830_GMCH_MEM_64M)
620 aperture_size = MB(64);
621 else
622 aperture_size = MB(128);
623 } else {
624 /* 9xx supports large sizes, just look at the length */
625 aperture_size = pci_resource_len(intel_private.pcidev, 2);
626 }
627
628 return aperture_size >> PAGE_SHIFT;
629}
630
631static void intel_gtt_teardown_scratch_page(void)
632{
633 set_pages_wb(intel_private.scratch_page, 1);
634 pci_unmap_page(intel_private.pcidev, intel_private.scratch_page_dma,
635 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
636 put_page(intel_private.scratch_page);
637 __free_page(intel_private.scratch_page);
638}
639
640static void intel_gtt_cleanup(void)
641{
642 intel_private.driver->cleanup();
643
644 iounmap(intel_private.gtt);
645 iounmap(intel_private.registers);
646
647 intel_gtt_teardown_scratch_page();
648}
649
650static int intel_gtt_init(void)
651{
652 u32 gtt_map_size;
653 int ret;
654
655 ret = intel_private.driver->setup();
656 if (ret != 0)
657 return ret;
658
659 intel_private.base.gtt_mappable_entries = intel_gtt_mappable_entries();
660 intel_private.base.gtt_total_entries = intel_gtt_total_entries();
661
662 /* save the PGETBL reg for resume */
663 intel_private.PGETBL_save =
664 readl(intel_private.registers+I810_PGETBL_CTL)
665 & ~I810_PGETBL_ENABLED;
666 /* we only ever restore the register when enabling the PGTBL... */
667 if (HAS_PGTBL_EN)
668 intel_private.PGETBL_save |= I810_PGETBL_ENABLED;
669
670 dev_info(&intel_private.bridge_dev->dev,
671 "detected gtt size: %dK total, %dK mappable\n",
672 intel_private.base.gtt_total_entries * 4,
673 intel_private.base.gtt_mappable_entries * 4);
674
675 gtt_map_size = intel_private.base.gtt_total_entries * 4;
676
677 intel_private.gtt = ioremap(intel_private.gtt_bus_addr,
678 gtt_map_size);
679 if (!intel_private.gtt) {
680 intel_private.driver->cleanup();
681 iounmap(intel_private.registers);
682 return -ENOMEM;
683 }
684
685 global_cache_flush(); /* FIXME: ? */
786 686
787 intel_private.i8xx_flush_page = kmap(intel_private.i8xx_page); 687 intel_private.base.stolen_size = intel_gtt_stolen_size();
788 if (!intel_private.i8xx_flush_page) 688
789 intel_i830_fini_flush(); 689 intel_private.base.needs_dmar = USE_PCI_DMA_API && INTEL_GTT_GEN > 2;
690
691 ret = intel_gtt_setup_scratch_page();
692 if (ret != 0) {
693 intel_gtt_cleanup();
694 return ret;
695 }
696
697 return 0;
698}
699
700static int intel_fake_agp_fetch_size(void)
701{
702 int num_sizes = ARRAY_SIZE(intel_fake_agp_sizes);
703 unsigned int aper_size;
704 int i;
705
706 aper_size = (intel_private.base.gtt_mappable_entries << PAGE_SHIFT)
707 / MB(1);
708
709 for (i = 0; i < num_sizes; i++) {
710 if (aper_size == intel_fake_agp_sizes[i].size) {
711 agp_bridge->current_size =
712 (void *) (intel_fake_agp_sizes + i);
713 return aper_size;
714 }
715 }
716
717 return 0;
718}
719
720static void i830_cleanup(void)
721{
790} 722}
791 723
792/* The chipset_flush interface needs to get data that has already been 724/* The chipset_flush interface needs to get data that has already been
@@ -799,181 +731,234 @@ static void intel_i830_setup_flush(void)
799 * that buffer out, we just fill 1KB and clflush it out, on the assumption 731 * that buffer out, we just fill 1KB and clflush it out, on the assumption
800 * that it'll push whatever was in there out. It appears to work. 732 * that it'll push whatever was in there out. It appears to work.
801 */ 733 */
802static void intel_i830_chipset_flush(struct agp_bridge_data *bridge) 734static void i830_chipset_flush(void)
803{ 735{
804 unsigned int *pg = intel_private.i8xx_flush_page; 736 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
805 737
806 memset(pg, 0, 1024); 738 /* Forcibly evict everything from the CPU write buffers.
739 * clflush appears to be insufficient.
740 */
741 wbinvd_on_all_cpus();
807 742
808 if (cpu_has_clflush) 743 /* Now we've only seen documents for this magic bit on 855GM,
809 clflush_cache_range(pg, 1024); 744 * we hope it exists for the other gen2 chipsets...
810 else if (wbinvd_on_all_cpus() != 0) 745 *
811 printk(KERN_ERR "Timed out waiting for cache flush.\n"); 746 * Also works as advertised on my 845G.
747 */
748 writel(readl(intel_private.registers+I830_HIC) | (1<<31),
749 intel_private.registers+I830_HIC);
750
751 while (readl(intel_private.registers+I830_HIC) & (1<<31)) {
752 if (time_after(jiffies, timeout))
753 break;
754
755 udelay(50);
756 }
812} 757}
813 758
814/* The intel i830 automatically initializes the agp aperture during POST. 759static void i830_write_entry(dma_addr_t addr, unsigned int entry,
815 * Use the memory already set aside for in the GTT. 760 unsigned int flags)
816 */
817static int intel_i830_create_gatt_table(struct agp_bridge_data *bridge)
818{ 761{
819 int page_order; 762 u32 pte_flags = I810_PTE_VALID;
820 struct aper_size_info_fixed *size;
821 int num_entries;
822 u32 temp;
823 763
824 size = agp_bridge->current_size; 764 if (flags == AGP_USER_CACHED_MEMORY)
825 page_order = size->page_order; 765 pte_flags |= I830_PTE_SYSTEM_CACHED;
826 num_entries = size->num_entries;
827 agp_bridge->gatt_table_real = NULL;
828 766
829 pci_read_config_dword(intel_private.pcidev, I810_MMADDR, &temp); 767 writel(addr | pte_flags, intel_private.gtt + entry);
830 temp &= 0xfff80000; 768}
831 769
832 intel_private.registers = ioremap(temp, 128 * 4096); 770static bool intel_enable_gtt(void)
833 if (!intel_private.registers) 771{
834 return -ENOMEM; 772 u32 gma_addr;
773 u8 __iomem *reg;
835 774
836 temp = readl(intel_private.registers+I810_PGETBL_CTL) & 0xfffff000; 775 if (INTEL_GTT_GEN <= 2)
837 global_cache_flush(); /* FIXME: ?? */ 776 pci_read_config_dword(intel_private.pcidev, I810_GMADDR,
777 &gma_addr);
778 else
779 pci_read_config_dword(intel_private.pcidev, I915_GMADDR,
780 &gma_addr);
838 781
839 /* we have to call this as early as possible after the MMIO base address is known */ 782 intel_private.gma_bus_addr = (gma_addr & PCI_BASE_ADDRESS_MEM_MASK);
840 intel_i830_init_gtt_entries(); 783
841 if (intel_private.gtt_entries == 0) { 784 if (INTEL_GTT_GEN >= 6)
842 iounmap(intel_private.registers); 785 return true;
843 return -ENOMEM; 786
787 if (INTEL_GTT_GEN == 2) {
788 u16 gmch_ctrl;
789
790 pci_read_config_word(intel_private.bridge_dev,
791 I830_GMCH_CTRL, &gmch_ctrl);
792 gmch_ctrl |= I830_GMCH_ENABLED;
793 pci_write_config_word(intel_private.bridge_dev,
794 I830_GMCH_CTRL, gmch_ctrl);
795
796 pci_read_config_word(intel_private.bridge_dev,
797 I830_GMCH_CTRL, &gmch_ctrl);
798 if ((gmch_ctrl & I830_GMCH_ENABLED) == 0) {
799 dev_err(&intel_private.pcidev->dev,
800 "failed to enable the GTT: GMCH_CTRL=%x\n",
801 gmch_ctrl);
802 return false;
803 }
844 } 804 }
845 805
846 agp_bridge->gatt_table = NULL; 806 /* On the resume path we may be adjusting the PGTBL value, so
807 * be paranoid and flush all chipset write buffers...
808 */
809 if (INTEL_GTT_GEN >= 3)
810 writel(0, intel_private.registers+GFX_FLSH_CNTL);
847 811
848 agp_bridge->gatt_bus_addr = temp; 812 reg = intel_private.registers+I810_PGETBL_CTL;
813 writel(intel_private.PGETBL_save, reg);
814 if (HAS_PGTBL_EN && (readl(reg) & I810_PGETBL_ENABLED) == 0) {
815 dev_err(&intel_private.pcidev->dev,
816 "failed to enable the GTT: PGETBL=%x [expected %x]\n",
817 readl(reg), intel_private.PGETBL_save);
818 return false;
819 }
849 820
850 return 0; 821 if (INTEL_GTT_GEN >= 3)
822 writel(0, intel_private.registers+GFX_FLSH_CNTL);
823
824 return true;
851} 825}
852 826
853/* Return the gatt table to a sane state. Use the top of stolen 827static int i830_setup(void)
854 * memory for the GTT.
855 */
856static int intel_i830_free_gatt_table(struct agp_bridge_data *bridge)
857{ 828{
829 u32 reg_addr;
830
831 pci_read_config_dword(intel_private.pcidev, I810_MMADDR, &reg_addr);
832 reg_addr &= 0xfff80000;
833
834 intel_private.registers = ioremap(reg_addr, KB(64));
835 if (!intel_private.registers)
836 return -ENOMEM;
837
838 intel_private.gtt_bus_addr = reg_addr + I810_PTE_BASE;
839
858 return 0; 840 return 0;
859} 841}
860 842
861static int intel_i830_fetch_size(void) 843static int intel_fake_agp_create_gatt_table(struct agp_bridge_data *bridge)
862{ 844{
863 u16 gmch_ctrl; 845 agp_bridge->gatt_table_real = NULL;
864 struct aper_size_info_fixed *values; 846 agp_bridge->gatt_table = NULL;
847 agp_bridge->gatt_bus_addr = 0;
865 848
866 values = A_SIZE_FIX(agp_bridge->driver->aperture_sizes); 849 return 0;
850}
867 851
868 if (agp_bridge->dev->device != PCI_DEVICE_ID_INTEL_82830_HB && 852static int intel_fake_agp_free_gatt_table(struct agp_bridge_data *bridge)
869 agp_bridge->dev->device != PCI_DEVICE_ID_INTEL_82845G_HB) { 853{
870 /* 855GM/852GM/865G has 128MB aperture size */ 854 return 0;
871 agp_bridge->current_size = (void *) values; 855}
872 agp_bridge->aperture_size_idx = 0;
873 return values[0].size;
874 }
875 856
876 pci_read_config_word(agp_bridge->dev, I830_GMCH_CTRL, &gmch_ctrl); 857static int intel_fake_agp_configure(void)
858{
859 if (!intel_enable_gtt())
860 return -EIO;
877 861
878 if ((gmch_ctrl & I830_GMCH_MEM_MASK) == I830_GMCH_MEM_128M) { 862 intel_private.clear_fake_agp = true;
879 agp_bridge->current_size = (void *) values; 863 agp_bridge->gart_bus_addr = intel_private.gma_bus_addr;
880 agp_bridge->aperture_size_idx = 0;
881 return values[0].size;
882 } else {
883 agp_bridge->current_size = (void *) (values + 1);
884 agp_bridge->aperture_size_idx = 1;
885 return values[1].size;
886 }
887 864
888 return 0; 865 return 0;
889} 866}
890 867
891static int intel_i830_configure(void) 868static bool i830_check_flags(unsigned int flags)
892{ 869{
893 struct aper_size_info_fixed *current_size; 870 switch (flags) {
894 u32 temp; 871 case 0:
895 u16 gmch_ctrl; 872 case AGP_PHYS_MEMORY:
896 int i; 873 case AGP_USER_CACHED_MEMORY:
897 874 case AGP_USER_MEMORY:
898 current_size = A_SIZE_FIX(agp_bridge->current_size); 875 return true;
876 }
899 877
900 pci_read_config_dword(intel_private.pcidev, I810_GMADDR, &temp); 878 return false;
901 agp_bridge->gart_bus_addr = (temp & PCI_BASE_ADDRESS_MEM_MASK); 879}
902 880
903 pci_read_config_word(agp_bridge->dev, I830_GMCH_CTRL, &gmch_ctrl); 881void intel_gtt_insert_sg_entries(struct scatterlist *sg_list,
904 gmch_ctrl |= I830_GMCH_ENABLED; 882 unsigned int sg_len,
905 pci_write_config_word(agp_bridge->dev, I830_GMCH_CTRL, gmch_ctrl); 883 unsigned int pg_start,
884 unsigned int flags)
885{
886 struct scatterlist *sg;
887 unsigned int len, m;
888 int i, j;
906 889
907 writel(agp_bridge->gatt_bus_addr|I810_PGETBL_ENABLED, intel_private.registers+I810_PGETBL_CTL); 890 j = pg_start;
908 readl(intel_private.registers+I810_PGETBL_CTL); /* PCI Posting. */
909 891
910 if (agp_bridge->driver->needs_scratch_page) { 892 /* sg may merge pages, but we have to separate
911 for (i = intel_private.gtt_entries; i < current_size->num_entries; i++) { 893 * per-page addr for GTT */
912 writel(agp_bridge->scratch_page, intel_private.registers+I810_PTE_BASE+(i*4)); 894 for_each_sg(sg_list, sg, sg_len, i) {
895 len = sg_dma_len(sg) >> PAGE_SHIFT;
896 for (m = 0; m < len; m++) {
897 dma_addr_t addr = sg_dma_address(sg) + (m << PAGE_SHIFT);
898 intel_private.driver->write_entry(addr,
899 j, flags);
900 j++;
913 } 901 }
914 readl(intel_private.registers+I810_PTE_BASE+((i-1)*4)); /* PCI Posting. */
915 } 902 }
916 903 readl(intel_private.gtt+j-1);
917 global_cache_flush();
918
919 intel_i830_setup_flush();
920 return 0;
921} 904}
905EXPORT_SYMBOL(intel_gtt_insert_sg_entries);
922 906
923static void intel_i830_cleanup(void) 907void intel_gtt_insert_pages(unsigned int first_entry, unsigned int num_entries,
908 struct page **pages, unsigned int flags)
924{ 909{
925 iounmap(intel_private.registers); 910 int i, j;
911
912 for (i = 0, j = first_entry; i < num_entries; i++, j++) {
913 dma_addr_t addr = page_to_phys(pages[i]);
914 intel_private.driver->write_entry(addr,
915 j, flags);
916 }
917 readl(intel_private.gtt+j-1);
926} 918}
919EXPORT_SYMBOL(intel_gtt_insert_pages);
927 920
928static int intel_i830_insert_entries(struct agp_memory *mem, off_t pg_start, 921static int intel_fake_agp_insert_entries(struct agp_memory *mem,
929 int type) 922 off_t pg_start, int type)
930{ 923{
931 int i, j, num_entries;
932 void *temp;
933 int ret = -EINVAL; 924 int ret = -EINVAL;
934 int mask_type;
935 925
936 if (mem->page_count == 0) 926 if (intel_private.clear_fake_agp) {
937 goto out; 927 int start = intel_private.base.stolen_size / PAGE_SIZE;
938 928 int end = intel_private.base.gtt_mappable_entries;
939 temp = agp_bridge->current_size; 929 intel_gtt_clear_range(start, end - start);
940 num_entries = A_SIZE_FIX(temp)->num_entries; 930 intel_private.clear_fake_agp = false;
931 }
941 932
942 if (pg_start < intel_private.gtt_entries) { 933 if (INTEL_GTT_GEN == 1 && type == AGP_DCACHE_MEMORY)
943 dev_printk(KERN_DEBUG, &intel_private.pcidev->dev, 934 return i810_insert_dcache_entries(mem, pg_start, type);
944 "pg_start == 0x%.8lx, intel_private.gtt_entries == 0x%.8x\n",
945 pg_start, intel_private.gtt_entries);
946 935
947 dev_info(&intel_private.pcidev->dev, 936 if (mem->page_count == 0)
948 "trying to insert into local/stolen memory\n"); 937 goto out;
949 goto out_err;
950 }
951 938
952 if ((pg_start + mem->page_count) > num_entries) 939 if (pg_start + mem->page_count > intel_private.base.gtt_total_entries)
953 goto out_err; 940 goto out_err;
954 941
955 /* The i830 can't check the GTT for entries since its read only,
956 * depend on the caller to make the correct offset decisions.
957 */
958
959 if (type != mem->type) 942 if (type != mem->type)
960 goto out_err; 943 goto out_err;
961 944
962 mask_type = agp_bridge->driver->agp_type_to_mask_type(agp_bridge, type); 945 if (!intel_private.driver->check_flags(type))
963
964 if (mask_type != 0 && mask_type != AGP_PHYS_MEMORY &&
965 mask_type != INTEL_AGP_CACHED_MEMORY)
966 goto out_err; 946 goto out_err;
967 947
968 if (!mem->is_flushed) 948 if (!mem->is_flushed)
969 global_cache_flush(); 949 global_cache_flush();
970 950
971 for (i = 0, j = pg_start; i < mem->page_count; i++, j++) { 951 if (intel_private.base.needs_dmar) {
972 writel(agp_bridge->driver->mask_memory(agp_bridge, 952 ret = intel_gtt_map_memory(mem->pages, mem->page_count,
973 page_to_phys(mem->pages[i]), mask_type), 953 &mem->sg_list, &mem->num_sg);
974 intel_private.registers+I810_PTE_BASE+(j*4)); 954 if (ret != 0)
975 } 955 return ret;
976 readl(intel_private.registers+I810_PTE_BASE+((j-1)*4)); 956
957 intel_gtt_insert_sg_entries(mem->sg_list, mem->num_sg,
958 pg_start, type);
959 } else
960 intel_gtt_insert_pages(pg_start, mem->page_count, mem->pages,
961 type);
977 962
978out: 963out:
979 ret = 0; 964 ret = 0;
@@ -982,30 +967,54 @@ out_err:
982 return ret; 967 return ret;
983} 968}
984 969
985static int intel_i830_remove_entries(struct agp_memory *mem, off_t pg_start, 970void intel_gtt_clear_range(unsigned int first_entry, unsigned int num_entries)
986 int type)
987{ 971{
988 int i; 972 unsigned int i;
973
974 for (i = first_entry; i < (first_entry + num_entries); i++) {
975 intel_private.driver->write_entry(intel_private.scratch_page_dma,
976 i, 0);
977 }
978 readl(intel_private.gtt+i-1);
979}
980EXPORT_SYMBOL(intel_gtt_clear_range);
989 981
982static int intel_fake_agp_remove_entries(struct agp_memory *mem,
983 off_t pg_start, int type)
984{
990 if (mem->page_count == 0) 985 if (mem->page_count == 0)
991 return 0; 986 return 0;
992 987
993 if (pg_start < intel_private.gtt_entries) { 988 intel_gtt_clear_range(pg_start, mem->page_count);
994 dev_info(&intel_private.pcidev->dev,
995 "trying to disable local/stolen memory\n");
996 return -EINVAL;
997 }
998 989
999 for (i = pg_start; i < (mem->page_count + pg_start); i++) { 990 if (intel_private.base.needs_dmar) {
1000 writel(agp_bridge->scratch_page, intel_private.registers+I810_PTE_BASE+(i*4)); 991 intel_gtt_unmap_memory(mem->sg_list, mem->num_sg);
992 mem->sg_list = NULL;
993 mem->num_sg = 0;
1001 } 994 }
1002 readl(intel_private.registers+I810_PTE_BASE+((i-1)*4));
1003 995
1004 return 0; 996 return 0;
1005} 997}
1006 998
1007static struct agp_memory *intel_i830_alloc_by_type(size_t pg_count, int type) 999static struct agp_memory *intel_fake_agp_alloc_by_type(size_t pg_count,
1000 int type)
1008{ 1001{
1002 struct agp_memory *new;
1003
1004 if (type == AGP_DCACHE_MEMORY && INTEL_GTT_GEN == 1) {
1005 if (pg_count != intel_private.num_dcache_entries)
1006 return NULL;
1007
1008 new = agp_create_memory(1);
1009 if (new == NULL)
1010 return NULL;
1011
1012 new->type = AGP_DCACHE_MEMORY;
1013 new->page_count = pg_count;
1014 new->num_scratch_pages = 0;
1015 agp_free_page_array(new);
1016 return new;
1017 }
1009 if (type == AGP_PHYS_MEMORY) 1018 if (type == AGP_PHYS_MEMORY)
1010 return alloc_agpphysmem_i8xx(pg_count, type); 1019 return alloc_agpphysmem_i8xx(pg_count, type);
1011 /* always return NULL for other allocation types for now */ 1020 /* always return NULL for other allocation types for now */
@@ -1015,9 +1024,9 @@ static struct agp_memory *intel_i830_alloc_by_type(size_t pg_count, int type)
1015static int intel_alloc_chipset_flush_resource(void) 1024static int intel_alloc_chipset_flush_resource(void)
1016{ 1025{
1017 int ret; 1026 int ret;
1018 ret = pci_bus_alloc_resource(agp_bridge->dev->bus, &intel_private.ifp_resource, PAGE_SIZE, 1027 ret = pci_bus_alloc_resource(intel_private.bridge_dev->bus, &intel_private.ifp_resource, PAGE_SIZE,
1019 PAGE_SIZE, PCIBIOS_MIN_MEM, 0, 1028 PAGE_SIZE, PCIBIOS_MIN_MEM, 0,
1020 pcibios_align_resource, agp_bridge->dev); 1029 pcibios_align_resource, intel_private.bridge_dev);
1021 1030
1022 return ret; 1031 return ret;
1023} 1032}
@@ -1027,11 +1036,11 @@ static void intel_i915_setup_chipset_flush(void)
1027 int ret; 1036 int ret;
1028 u32 temp; 1037 u32 temp;
1029 1038
1030 pci_read_config_dword(agp_bridge->dev, I915_IFPADDR, &temp); 1039 pci_read_config_dword(intel_private.bridge_dev, I915_IFPADDR, &temp);
1031 if (!(temp & 0x1)) { 1040 if (!(temp & 0x1)) {
1032 intel_alloc_chipset_flush_resource(); 1041 intel_alloc_chipset_flush_resource();
1033 intel_private.resource_valid = 1; 1042 intel_private.resource_valid = 1;
1034 pci_write_config_dword(agp_bridge->dev, I915_IFPADDR, (intel_private.ifp_resource.start & 0xffffffff) | 0x1); 1043 pci_write_config_dword(intel_private.bridge_dev, I915_IFPADDR, (intel_private.ifp_resource.start & 0xffffffff) | 0x1);
1035 } else { 1044 } else {
1036 temp &= ~1; 1045 temp &= ~1;
1037 1046
@@ -1050,17 +1059,17 @@ static void intel_i965_g33_setup_chipset_flush(void)
1050 u32 temp_hi, temp_lo; 1059 u32 temp_hi, temp_lo;
1051 int ret; 1060 int ret;
1052 1061
1053 pci_read_config_dword(agp_bridge->dev, I965_IFPADDR + 4, &temp_hi); 1062 pci_read_config_dword(intel_private.bridge_dev, I965_IFPADDR + 4, &temp_hi);
1054 pci_read_config_dword(agp_bridge->dev, I965_IFPADDR, &temp_lo); 1063 pci_read_config_dword(intel_private.bridge_dev, I965_IFPADDR, &temp_lo);
1055 1064
1056 if (!(temp_lo & 0x1)) { 1065 if (!(temp_lo & 0x1)) {
1057 1066
1058 intel_alloc_chipset_flush_resource(); 1067 intel_alloc_chipset_flush_resource();
1059 1068
1060 intel_private.resource_valid = 1; 1069 intel_private.resource_valid = 1;
1061 pci_write_config_dword(agp_bridge->dev, I965_IFPADDR + 4, 1070 pci_write_config_dword(intel_private.bridge_dev, I965_IFPADDR + 4,
1062 upper_32_bits(intel_private.ifp_resource.start)); 1071 upper_32_bits(intel_private.ifp_resource.start));
1063 pci_write_config_dword(agp_bridge->dev, I965_IFPADDR, (intel_private.ifp_resource.start & 0xffffffff) | 0x1); 1072 pci_write_config_dword(intel_private.bridge_dev, I965_IFPADDR, (intel_private.ifp_resource.start & 0xffffffff) | 0x1);
1064 } else { 1073 } else {
1065 u64 l64; 1074 u64 l64;
1066 1075
@@ -1083,7 +1092,7 @@ static void intel_i9xx_setup_flush(void)
1083 if (intel_private.ifp_resource.start) 1092 if (intel_private.ifp_resource.start)
1084 return; 1093 return;
1085 1094
1086 if (IS_SNB) 1095 if (INTEL_GTT_GEN == 6)
1087 return; 1096 return;
1088 1097
1089 /* setup a resource for this object */ 1098 /* setup a resource for this object */
@@ -1091,7 +1100,7 @@ static void intel_i9xx_setup_flush(void)
1091 intel_private.ifp_resource.flags = IORESOURCE_MEM; 1100 intel_private.ifp_resource.flags = IORESOURCE_MEM;
1092 1101
1093 /* Setup chipset flush for 915 */ 1102 /* Setup chipset flush for 915 */
1094 if (IS_I965 || IS_G33 || IS_G4X) { 1103 if (IS_G33 || INTEL_GTT_GEN >= 4) {
1095 intel_i965_g33_setup_chipset_flush(); 1104 intel_i965_g33_setup_chipset_flush();
1096 } else { 1105 } else {
1097 intel_i915_setup_chipset_flush(); 1106 intel_i915_setup_chipset_flush();
@@ -1104,41 +1113,7 @@ static void intel_i9xx_setup_flush(void)
1104 "can't ioremap flush page - no chipset flushing\n"); 1113 "can't ioremap flush page - no chipset flushing\n");
1105} 1114}
1106 1115
1107static int intel_i9xx_configure(void) 1116static void i9xx_cleanup(void)
1108{
1109 struct aper_size_info_fixed *current_size;
1110 u32 temp;
1111 u16 gmch_ctrl;
1112 int i;
1113
1114 current_size = A_SIZE_FIX(agp_bridge->current_size);
1115
1116 pci_read_config_dword(intel_private.pcidev, I915_GMADDR, &temp);
1117
1118 agp_bridge->gart_bus_addr = (temp & PCI_BASE_ADDRESS_MEM_MASK);
1119
1120 pci_read_config_word(agp_bridge->dev, I830_GMCH_CTRL, &gmch_ctrl);
1121 gmch_ctrl |= I830_GMCH_ENABLED;
1122 pci_write_config_word(agp_bridge->dev, I830_GMCH_CTRL, gmch_ctrl);
1123
1124 writel(agp_bridge->gatt_bus_addr|I810_PGETBL_ENABLED, intel_private.registers+I810_PGETBL_CTL);
1125 readl(intel_private.registers+I810_PGETBL_CTL); /* PCI Posting. */
1126
1127 if (agp_bridge->driver->needs_scratch_page) {
1128 for (i = intel_private.gtt_entries; i < intel_private.gtt_total_size; i++) {
1129 writel(agp_bridge->scratch_page, intel_private.gtt+i);
1130 }
1131 readl(intel_private.gtt+i-1); /* PCI Posting. */
1132 }
1133
1134 global_cache_flush();
1135
1136 intel_i9xx_setup_flush();
1137
1138 return 0;
1139}
1140
1141static void intel_i915_cleanup(void)
1142{ 1117{
1143 if (intel_private.i9xx_flush_page) 1118 if (intel_private.i9xx_flush_page)
1144 iounmap(intel_private.i9xx_flush_page); 1119 iounmap(intel_private.i9xx_flush_page);
@@ -1146,505 +1121,399 @@ static void intel_i915_cleanup(void)
1146 release_resource(&intel_private.ifp_resource); 1121 release_resource(&intel_private.ifp_resource);
1147 intel_private.ifp_resource.start = 0; 1122 intel_private.ifp_resource.start = 0;
1148 intel_private.resource_valid = 0; 1123 intel_private.resource_valid = 0;
1149 iounmap(intel_private.gtt);
1150 iounmap(intel_private.registers);
1151} 1124}
1152 1125
1153static void intel_i915_chipset_flush(struct agp_bridge_data *bridge) 1126static void i9xx_chipset_flush(void)
1154{ 1127{
1155 if (intel_private.i9xx_flush_page) 1128 if (intel_private.i9xx_flush_page)
1156 writel(1, intel_private.i9xx_flush_page); 1129 writel(1, intel_private.i9xx_flush_page);
1157} 1130}
1158 1131
1159static int intel_i915_insert_entries(struct agp_memory *mem, off_t pg_start, 1132static void i965_write_entry(dma_addr_t addr,
1160 int type) 1133 unsigned int entry,
1134 unsigned int flags)
1161{ 1135{
1162 int num_entries; 1136 u32 pte_flags;
1163 void *temp;
1164 int ret = -EINVAL;
1165 int mask_type;
1166
1167 if (mem->page_count == 0)
1168 goto out;
1169
1170 temp = agp_bridge->current_size;
1171 num_entries = A_SIZE_FIX(temp)->num_entries;
1172
1173 if (pg_start < intel_private.gtt_entries) {
1174 dev_printk(KERN_DEBUG, &intel_private.pcidev->dev,
1175 "pg_start == 0x%.8lx, intel_private.gtt_entries == 0x%.8x\n",
1176 pg_start, intel_private.gtt_entries);
1177
1178 dev_info(&intel_private.pcidev->dev,
1179 "trying to insert into local/stolen memory\n");
1180 goto out_err;
1181 }
1182
1183 if ((pg_start + mem->page_count) > num_entries)
1184 goto out_err;
1185
1186 /* The i915 can't check the GTT for entries since it's read only;
1187 * depend on the caller to make the correct offset decisions.
1188 */
1189
1190 if (type != mem->type)
1191 goto out_err;
1192
1193 mask_type = agp_bridge->driver->agp_type_to_mask_type(agp_bridge, type);
1194
1195 if (!IS_SNB && mask_type != 0 && mask_type != AGP_PHYS_MEMORY &&
1196 mask_type != INTEL_AGP_CACHED_MEMORY)
1197 goto out_err;
1198 1137
1199 if (!mem->is_flushed) 1138 pte_flags = I810_PTE_VALID;
1200 global_cache_flush(); 1139 if (flags == AGP_USER_CACHED_MEMORY)
1140 pte_flags |= I830_PTE_SYSTEM_CACHED;
1201 1141
1202 intel_agp_insert_sg_entries(mem, pg_start, mask_type); 1142 /* Shift high bits down */
1203 1143 addr |= (addr >> 28) & 0xf0;
1204 out: 1144 writel(addr | pte_flags, intel_private.gtt + entry);
1205 ret = 0;
1206 out_err:
1207 mem->is_flushed = true;
1208 return ret;
1209} 1145}
1210 1146
1211static int intel_i915_remove_entries(struct agp_memory *mem, off_t pg_start, 1147static bool gen6_check_flags(unsigned int flags)
1212 int type)
1213{ 1148{
1214 int i; 1149 return true;
1215
1216 if (mem->page_count == 0)
1217 return 0;
1218
1219 if (pg_start < intel_private.gtt_entries) {
1220 dev_info(&intel_private.pcidev->dev,
1221 "trying to disable local/stolen memory\n");
1222 return -EINVAL;
1223 }
1224
1225 for (i = pg_start; i < (mem->page_count + pg_start); i++)
1226 writel(agp_bridge->scratch_page, intel_private.gtt+i);
1227
1228 readl(intel_private.gtt+i-1);
1229
1230 return 0;
1231} 1150}
1232 1151
1233/* Return the aperture size by just checking the resource length. The effect 1152static void gen6_write_entry(dma_addr_t addr, unsigned int entry,
1234 * described in the spec of the MSAC registers is just changing of the 1153 unsigned int flags)
1235 * resource size.
1236 */
1237static int intel_i9xx_fetch_size(void)
1238{ 1154{
1239 int num_sizes = ARRAY_SIZE(intel_i830_sizes); 1155 unsigned int type_mask = flags & ~AGP_USER_CACHED_MEMORY_GFDT;
1240 int aper_size; /* size in megabytes */ 1156 unsigned int gfdt = flags & AGP_USER_CACHED_MEMORY_GFDT;
1241 int i; 1157 u32 pte_flags;
1242 1158
1243 aper_size = pci_resource_len(intel_private.pcidev, 2) / MB(1); 1159 if (type_mask == AGP_USER_MEMORY)
1244 1160 pte_flags = GEN6_PTE_UNCACHED | I810_PTE_VALID;
1245 for (i = 0; i < num_sizes; i++) { 1161 else if (type_mask == AGP_USER_CACHED_MEMORY_LLC_MLC) {
1246 if (aper_size == intel_i830_sizes[i].size) { 1162 pte_flags = GEN6_PTE_LLC_MLC | I810_PTE_VALID;
1247 agp_bridge->current_size = intel_i830_sizes + i; 1163 if (gfdt)
1248 return aper_size; 1164 pte_flags |= GEN6_PTE_GFDT;
1249 } 1165 } else { /* set 'normal'/'cached' to LLC by default */
1166 pte_flags = GEN6_PTE_LLC | I810_PTE_VALID;
1167 if (gfdt)
1168 pte_flags |= GEN6_PTE_GFDT;
1250 } 1169 }
1251 1170
1252 return 0; 1171 /* gen6 has bit11-4 for physical addr bit39-32 */
1172 addr |= (addr >> 28) & 0xff0;
1173 writel(addr | pte_flags, intel_private.gtt + entry);
1253} 1174}
1254 1175
1255static int intel_i915_get_gtt_size(void) 1176static void gen6_cleanup(void)
1256{ 1177{
1257 int size;
1258
1259 if (IS_G33) {
1260 u16 gmch_ctrl;
1261
1262 /* G33's GTT size defined in gmch_ctrl */
1263 pci_read_config_word(agp_bridge->dev, I830_GMCH_CTRL, &gmch_ctrl);
1264 switch (gmch_ctrl & I830_GMCH_GMS_MASK) {
1265 case I830_GMCH_GMS_STOLEN_512:
1266 size = 512;
1267 break;
1268 case I830_GMCH_GMS_STOLEN_1024:
1269 size = 1024;
1270 break;
1271 case I830_GMCH_GMS_STOLEN_8192:
1272 size = 8*1024;
1273 break;
1274 default:
1275 dev_info(&agp_bridge->dev->dev,
1276 "unknown page table size 0x%x, assuming 512KB\n",
1277 (gmch_ctrl & I830_GMCH_GMS_MASK));
1278 size = 512;
1279 }
1280 } else {
1281 /* On previous hardware, the GTT size was just what was
1282 * required to map the aperture.
1283 */
1284 size = agp_bridge->driver->fetch_size();
1285 }
1286
1287 return KB(size);
1288} 1178}
1289 1179
1290/* The intel i915 automatically initializes the agp aperture during POST. 1180static int i9xx_setup(void)
1291 * Use the memory already set aside for in the GTT.
1292 */
1293static int intel_i915_create_gatt_table(struct agp_bridge_data *bridge)
1294{ 1181{
1295 int page_order; 1182 u32 reg_addr;
1296 struct aper_size_info_fixed *size;
1297 int num_entries;
1298 u32 temp, temp2;
1299 int gtt_map_size;
1300
1301 size = agp_bridge->current_size;
1302 page_order = size->page_order;
1303 num_entries = size->num_entries;
1304 agp_bridge->gatt_table_real = NULL;
1305 1183
1306 pci_read_config_dword(intel_private.pcidev, I915_MMADDR, &temp); 1184 pci_read_config_dword(intel_private.pcidev, I915_MMADDR, &reg_addr);
1307 pci_read_config_dword(intel_private.pcidev, I915_PTEADDR, &temp2);
1308 1185
1309 gtt_map_size = intel_i915_get_gtt_size(); 1186 reg_addr &= 0xfff80000;
1310 1187
1311 intel_private.gtt = ioremap(temp2, gtt_map_size); 1188 intel_private.registers = ioremap(reg_addr, 128 * 4096);
1312 if (!intel_private.gtt) 1189 if (!intel_private.registers)
1313 return -ENOMEM; 1190 return -ENOMEM;
1314 1191
1315 intel_private.gtt_total_size = gtt_map_size / 4; 1192 if (INTEL_GTT_GEN == 3) {
1193 u32 gtt_addr;
1316 1194
1317 temp &= 0xfff80000; 1195 pci_read_config_dword(intel_private.pcidev,
1318 1196 I915_PTEADDR, &gtt_addr);
1319 intel_private.registers = ioremap(temp, 128 * 4096); 1197 intel_private.gtt_bus_addr = gtt_addr;
1320 if (!intel_private.registers) { 1198 } else {
1321 iounmap(intel_private.gtt); 1199 u32 gtt_offset;
1322 return -ENOMEM;
1323 }
1324
1325 temp = readl(intel_private.registers+I810_PGETBL_CTL) & 0xfffff000;
1326 global_cache_flush(); /* FIXME: ? */
1327 1200
1328 /* we have to call this as early as possible after the MMIO base address is known */ 1201 switch (INTEL_GTT_GEN) {
1329 intel_i830_init_gtt_entries(); 1202 case 5:
1330 if (intel_private.gtt_entries == 0) { 1203 case 6:
1331 iounmap(intel_private.gtt); 1204 gtt_offset = MB(2);
1332 iounmap(intel_private.registers); 1205 break;
1333 return -ENOMEM; 1206 case 4:
1207 default:
1208 gtt_offset = KB(512);
1209 break;
1210 }
1211 intel_private.gtt_bus_addr = reg_addr + gtt_offset;
1334 } 1212 }
1335 1213
1336 agp_bridge->gatt_table = NULL; 1214 intel_i9xx_setup_flush();
1337
1338 agp_bridge->gatt_bus_addr = temp;
1339 1215
1340 return 0; 1216 return 0;
1341} 1217}
1342 1218
1343/* 1219static const struct agp_bridge_driver intel_fake_agp_driver = {
1344 * The i965 supports 36-bit physical addresses, but to keep 1220 .owner = THIS_MODULE,
1345 * the format of the GTT the same, the bits that don't fit 1221 .size_type = FIXED_APER_SIZE,
1346 * in a 32-bit word are shifted down to bits 4..7. 1222 .aperture_sizes = intel_fake_agp_sizes,
1347 * 1223 .num_aperture_sizes = ARRAY_SIZE(intel_fake_agp_sizes),
1348 * Gcc is smart enough to notice that "(addr >> 28) & 0xf0" 1224 .configure = intel_fake_agp_configure,
1349 * is always zero on 32-bit architectures, so no need to make 1225 .fetch_size = intel_fake_agp_fetch_size,
1350 * this conditional. 1226 .cleanup = intel_gtt_cleanup,
1227 .agp_enable = intel_fake_agp_enable,
1228 .cache_flush = global_cache_flush,
1229 .create_gatt_table = intel_fake_agp_create_gatt_table,
1230 .free_gatt_table = intel_fake_agp_free_gatt_table,
1231 .insert_memory = intel_fake_agp_insert_entries,
1232 .remove_memory = intel_fake_agp_remove_entries,
1233 .alloc_by_type = intel_fake_agp_alloc_by_type,
1234 .free_by_type = intel_i810_free_by_type,
1235 .agp_alloc_page = agp_generic_alloc_page,
1236 .agp_alloc_pages = agp_generic_alloc_pages,
1237 .agp_destroy_page = agp_generic_destroy_page,
1238 .agp_destroy_pages = agp_generic_destroy_pages,
1239};
1240
1241static const struct intel_gtt_driver i81x_gtt_driver = {
1242 .gen = 1,
1243 .has_pgtbl_enable = 1,
1244 .dma_mask_size = 32,
1245 .setup = i810_setup,
1246 .cleanup = i810_cleanup,
1247 .check_flags = i830_check_flags,
1248 .write_entry = i810_write_entry,
1249};
1250static const struct intel_gtt_driver i8xx_gtt_driver = {
1251 .gen = 2,
1252 .has_pgtbl_enable = 1,
1253 .setup = i830_setup,
1254 .cleanup = i830_cleanup,
1255 .write_entry = i830_write_entry,
1256 .dma_mask_size = 32,
1257 .check_flags = i830_check_flags,
1258 .chipset_flush = i830_chipset_flush,
1259};
1260static const struct intel_gtt_driver i915_gtt_driver = {
1261 .gen = 3,
1262 .has_pgtbl_enable = 1,
1263 .setup = i9xx_setup,
1264 .cleanup = i9xx_cleanup,
1265 /* i945 is the last gpu to need phys mem (for overlay and cursors). */
1266 .write_entry = i830_write_entry,
1267 .dma_mask_size = 32,
1268 .check_flags = i830_check_flags,
1269 .chipset_flush = i9xx_chipset_flush,
1270};
1271static const struct intel_gtt_driver g33_gtt_driver = {
1272 .gen = 3,
1273 .is_g33 = 1,
1274 .setup = i9xx_setup,
1275 .cleanup = i9xx_cleanup,
1276 .write_entry = i965_write_entry,
1277 .dma_mask_size = 36,
1278 .check_flags = i830_check_flags,
1279 .chipset_flush = i9xx_chipset_flush,
1280};
1281static const struct intel_gtt_driver pineview_gtt_driver = {
1282 .gen = 3,
1283 .is_pineview = 1, .is_g33 = 1,
1284 .setup = i9xx_setup,
1285 .cleanup = i9xx_cleanup,
1286 .write_entry = i965_write_entry,
1287 .dma_mask_size = 36,
1288 .check_flags = i830_check_flags,
1289 .chipset_flush = i9xx_chipset_flush,
1290};
1291static const struct intel_gtt_driver i965_gtt_driver = {
1292 .gen = 4,
1293 .has_pgtbl_enable = 1,
1294 .setup = i9xx_setup,
1295 .cleanup = i9xx_cleanup,
1296 .write_entry = i965_write_entry,
1297 .dma_mask_size = 36,
1298 .check_flags = i830_check_flags,
1299 .chipset_flush = i9xx_chipset_flush,
1300};
1301static const struct intel_gtt_driver g4x_gtt_driver = {
1302 .gen = 5,
1303 .setup = i9xx_setup,
1304 .cleanup = i9xx_cleanup,
1305 .write_entry = i965_write_entry,
1306 .dma_mask_size = 36,
1307 .check_flags = i830_check_flags,
1308 .chipset_flush = i9xx_chipset_flush,
1309};
1310static const struct intel_gtt_driver ironlake_gtt_driver = {
1311 .gen = 5,
1312 .is_ironlake = 1,
1313 .setup = i9xx_setup,
1314 .cleanup = i9xx_cleanup,
1315 .write_entry = i965_write_entry,
1316 .dma_mask_size = 36,
1317 .check_flags = i830_check_flags,
1318 .chipset_flush = i9xx_chipset_flush,
1319};
1320static const struct intel_gtt_driver sandybridge_gtt_driver = {
1321 .gen = 6,
1322 .setup = i9xx_setup,
1323 .cleanup = gen6_cleanup,
1324 .write_entry = gen6_write_entry,
1325 .dma_mask_size = 40,
1326 .check_flags = gen6_check_flags,
1327 .chipset_flush = i9xx_chipset_flush,
1328};
1329
1330/* Table to describe Intel GMCH and AGP/PCIE GART drivers. At least one of
1331 * driver and gmch_driver must be non-null, and find_gmch will determine
1332 * which one should be used if a gmch_chip_id is present.
1351 */ 1333 */
1352static unsigned long intel_i965_mask_memory(struct agp_bridge_data *bridge, 1334static const struct intel_gtt_driver_description {
1353 dma_addr_t addr, int type) 1335 unsigned int gmch_chip_id;
1336 char *name;
1337 const struct intel_gtt_driver *gtt_driver;
1338} intel_gtt_chipsets[] = {
1339 { PCI_DEVICE_ID_INTEL_82810_IG1, "i810",
1340 &i81x_gtt_driver},
1341 { PCI_DEVICE_ID_INTEL_82810_IG3, "i810",
1342 &i81x_gtt_driver},
1343 { PCI_DEVICE_ID_INTEL_82810E_IG, "i810",
1344 &i81x_gtt_driver},
1345 { PCI_DEVICE_ID_INTEL_82815_CGC, "i815",
1346 &i81x_gtt_driver},
1347 { PCI_DEVICE_ID_INTEL_82830_CGC, "830M",
1348 &i8xx_gtt_driver},
1349 { PCI_DEVICE_ID_INTEL_82845G_IG, "845G",
1350 &i8xx_gtt_driver},
1351 { PCI_DEVICE_ID_INTEL_82854_IG, "854",
1352 &i8xx_gtt_driver},
1353 { PCI_DEVICE_ID_INTEL_82855GM_IG, "855GM",
1354 &i8xx_gtt_driver},
1355 { PCI_DEVICE_ID_INTEL_82865_IG, "865",
1356 &i8xx_gtt_driver},
1357 { PCI_DEVICE_ID_INTEL_E7221_IG, "E7221 (i915)",
1358 &i915_gtt_driver },
1359 { PCI_DEVICE_ID_INTEL_82915G_IG, "915G",
1360 &i915_gtt_driver },
1361 { PCI_DEVICE_ID_INTEL_82915GM_IG, "915GM",
1362 &i915_gtt_driver },
1363 { PCI_DEVICE_ID_INTEL_82945G_IG, "945G",
1364 &i915_gtt_driver },
1365 { PCI_DEVICE_ID_INTEL_82945GM_IG, "945GM",
1366 &i915_gtt_driver },
1367 { PCI_DEVICE_ID_INTEL_82945GME_IG, "945GME",
1368 &i915_gtt_driver },
1369 { PCI_DEVICE_ID_INTEL_82946GZ_IG, "946GZ",
1370 &i965_gtt_driver },
1371 { PCI_DEVICE_ID_INTEL_82G35_IG, "G35",
1372 &i965_gtt_driver },
1373 { PCI_DEVICE_ID_INTEL_82965Q_IG, "965Q",
1374 &i965_gtt_driver },
1375 { PCI_DEVICE_ID_INTEL_82965G_IG, "965G",
1376 &i965_gtt_driver },
1377 { PCI_DEVICE_ID_INTEL_82965GM_IG, "965GM",
1378 &i965_gtt_driver },
1379 { PCI_DEVICE_ID_INTEL_82965GME_IG, "965GME/GLE",
1380 &i965_gtt_driver },
1381 { PCI_DEVICE_ID_INTEL_G33_IG, "G33",
1382 &g33_gtt_driver },
1383 { PCI_DEVICE_ID_INTEL_Q35_IG, "Q35",
1384 &g33_gtt_driver },
1385 { PCI_DEVICE_ID_INTEL_Q33_IG, "Q33",
1386 &g33_gtt_driver },
1387 { PCI_DEVICE_ID_INTEL_PINEVIEW_M_IG, "GMA3150",
1388 &pineview_gtt_driver },
1389 { PCI_DEVICE_ID_INTEL_PINEVIEW_IG, "GMA3150",
1390 &pineview_gtt_driver },
1391 { PCI_DEVICE_ID_INTEL_GM45_IG, "GM45",
1392 &g4x_gtt_driver },
1393 { PCI_DEVICE_ID_INTEL_EAGLELAKE_IG, "Eaglelake",
1394 &g4x_gtt_driver },
1395 { PCI_DEVICE_ID_INTEL_Q45_IG, "Q45/Q43",
1396 &g4x_gtt_driver },
1397 { PCI_DEVICE_ID_INTEL_G45_IG, "G45/G43",
1398 &g4x_gtt_driver },
1399 { PCI_DEVICE_ID_INTEL_B43_IG, "B43",
1400 &g4x_gtt_driver },
1401 { PCI_DEVICE_ID_INTEL_B43_1_IG, "B43",
1402 &g4x_gtt_driver },
1403 { PCI_DEVICE_ID_INTEL_G41_IG, "G41",
1404 &g4x_gtt_driver },
1405 { PCI_DEVICE_ID_INTEL_IRONLAKE_D_IG,
1406 "HD Graphics", &ironlake_gtt_driver },
1407 { PCI_DEVICE_ID_INTEL_IRONLAKE_M_IG,
1408 "HD Graphics", &ironlake_gtt_driver },
1409 { PCI_DEVICE_ID_INTEL_SANDYBRIDGE_GT1_IG,
1410 "Sandybridge", &sandybridge_gtt_driver },
1411 { PCI_DEVICE_ID_INTEL_SANDYBRIDGE_GT2_IG,
1412 "Sandybridge", &sandybridge_gtt_driver },
1413 { PCI_DEVICE_ID_INTEL_SANDYBRIDGE_GT2_PLUS_IG,
1414 "Sandybridge", &sandybridge_gtt_driver },
1415 { PCI_DEVICE_ID_INTEL_SANDYBRIDGE_M_GT1_IG,
1416 "Sandybridge", &sandybridge_gtt_driver },
1417 { PCI_DEVICE_ID_INTEL_SANDYBRIDGE_M_GT2_IG,
1418 "Sandybridge", &sandybridge_gtt_driver },
1419 { PCI_DEVICE_ID_INTEL_SANDYBRIDGE_M_GT2_PLUS_IG,
1420 "Sandybridge", &sandybridge_gtt_driver },
1421 { PCI_DEVICE_ID_INTEL_SANDYBRIDGE_S_IG,
1422 "Sandybridge", &sandybridge_gtt_driver },
1423 { PCI_DEVICE_ID_INTEL_IVYBRIDGE_GT1_IG,
1424 "Ivybridge", &sandybridge_gtt_driver },
1425 { PCI_DEVICE_ID_INTEL_IVYBRIDGE_GT2_IG,
1426 "Ivybridge", &sandybridge_gtt_driver },
1427 { PCI_DEVICE_ID_INTEL_IVYBRIDGE_M_GT1_IG,
1428 "Ivybridge", &sandybridge_gtt_driver },
1429 { PCI_DEVICE_ID_INTEL_IVYBRIDGE_M_GT2_IG,
1430 "Ivybridge", &sandybridge_gtt_driver },
1431 { PCI_DEVICE_ID_INTEL_IVYBRIDGE_S_GT1_IG,
1432 "Ivybridge", &sandybridge_gtt_driver },
1433 { 0, NULL, NULL }
1434};
1435
1436static int find_gmch(u16 device)
1354{ 1437{
1355 /* Shift high bits down */ 1438 struct pci_dev *gmch_device;
1356 addr |= (addr >> 28) & 0xf0;
1357 1439
1358 /* Type checking must be done elsewhere */ 1440 gmch_device = pci_get_device(PCI_VENDOR_ID_INTEL, device, NULL);
1359 return addr | bridge->driver->masks[type].mask; 1441 if (gmch_device && PCI_FUNC(gmch_device->devfn) != 0) {
1360} 1442 gmch_device = pci_get_device(PCI_VENDOR_ID_INTEL,
1443 device, gmch_device);
1444 }
1361 1445
1362static unsigned long intel_gen6_mask_memory(struct agp_bridge_data *bridge, 1446 if (!gmch_device)
1363 dma_addr_t addr, int type) 1447 return 0;
1364{
1365 /* gen6 has bit11-4 for physical addr bit39-32 */
1366 addr |= (addr >> 28) & 0xff0;
1367 1448
1368 /* Type checking must be done elsewhere */ 1449 intel_private.pcidev = gmch_device;
1369 return addr | bridge->driver->masks[type].mask; 1450 return 1;
1370} 1451}
1371 1452
1372static void intel_i965_get_gtt_range(int *gtt_offset, int *gtt_size) 1453int intel_gmch_probe(struct pci_dev *pdev,
1454 struct agp_bridge_data *bridge)
1373{ 1455{
1374 u16 snb_gmch_ctl; 1456 int i, mask;
1375 1457 intel_private.driver = NULL;
1376 switch (agp_bridge->dev->device) {
1377 case PCI_DEVICE_ID_INTEL_GM45_HB:
1378 case PCI_DEVICE_ID_INTEL_EAGLELAKE_HB:
1379 case PCI_DEVICE_ID_INTEL_Q45_HB:
1380 case PCI_DEVICE_ID_INTEL_G45_HB:
1381 case PCI_DEVICE_ID_INTEL_G41_HB:
1382 case PCI_DEVICE_ID_INTEL_B43_HB:
1383 case PCI_DEVICE_ID_INTEL_IRONLAKE_D_HB:
1384 case PCI_DEVICE_ID_INTEL_IRONLAKE_M_HB:
1385 case PCI_DEVICE_ID_INTEL_IRONLAKE_MA_HB:
1386 case PCI_DEVICE_ID_INTEL_IRONLAKE_MC2_HB:
1387 *gtt_offset = *gtt_size = MB(2);
1388 break;
1389 case PCI_DEVICE_ID_INTEL_SANDYBRIDGE_HB:
1390 case PCI_DEVICE_ID_INTEL_SANDYBRIDGE_M_HB:
1391 case PCI_DEVICE_ID_INTEL_SANDYBRIDGE_S_HB:
1392 *gtt_offset = MB(2);
1393 1458
1394 pci_read_config_word(intel_private.pcidev, SNB_GMCH_CTRL, &snb_gmch_ctl); 1459 for (i = 0; intel_gtt_chipsets[i].name != NULL; i++) {
1395 switch (snb_gmch_ctl & SNB_GTT_SIZE_MASK) { 1460 if (find_gmch(intel_gtt_chipsets[i].gmch_chip_id)) {
1396 default: 1461 intel_private.driver =
1397 case SNB_GTT_SIZE_0M: 1462 intel_gtt_chipsets[i].gtt_driver;
1398 printk(KERN_ERR "Bad GTT size mask: 0x%04x.\n", snb_gmch_ctl);
1399 *gtt_size = MB(0);
1400 break;
1401 case SNB_GTT_SIZE_1M:
1402 *gtt_size = MB(1);
1403 break;
1404 case SNB_GTT_SIZE_2M:
1405 *gtt_size = MB(2);
1406 break; 1463 break;
1407 } 1464 }
1408 break;
1409 default:
1410 *gtt_offset = *gtt_size = KB(512);
1411 } 1465 }
1412}
1413
1414/* The intel i965 automatically initializes the agp aperture during POST.
1415 * Use the memory already set aside for in the GTT.
1416 */
1417static int intel_i965_create_gatt_table(struct agp_bridge_data *bridge)
1418{
1419 int page_order;
1420 struct aper_size_info_fixed *size;
1421 int num_entries;
1422 u32 temp;
1423 int gtt_offset, gtt_size;
1424
1425 size = agp_bridge->current_size;
1426 page_order = size->page_order;
1427 num_entries = size->num_entries;
1428 agp_bridge->gatt_table_real = NULL;
1429
1430 pci_read_config_dword(intel_private.pcidev, I915_MMADDR, &temp);
1431 1466
1432 temp &= 0xfff00000; 1467 if (!intel_private.driver)
1433 1468 return 0;
1434 intel_i965_get_gtt_range(&gtt_offset, &gtt_size);
1435
1436 intel_private.gtt = ioremap((temp + gtt_offset) , gtt_size);
1437 1469
1438 if (!intel_private.gtt) 1470 bridge->driver = &intel_fake_agp_driver;
1439 return -ENOMEM; 1471 bridge->dev_private_data = &intel_private;
1472 bridge->dev = pdev;
1440 1473
1441 intel_private.gtt_total_size = gtt_size / 4; 1474 intel_private.bridge_dev = pci_dev_get(pdev);
1442 1475
1443 intel_private.registers = ioremap(temp, 128 * 4096); 1476 dev_info(&pdev->dev, "Intel %s Chipset\n", intel_gtt_chipsets[i].name);
1444 if (!intel_private.registers) {
1445 iounmap(intel_private.gtt);
1446 return -ENOMEM;
1447 }
1448 1477
1449 temp = readl(intel_private.registers+I810_PGETBL_CTL) & 0xfffff000; 1478 mask = intel_private.driver->dma_mask_size;
1450 global_cache_flush(); /* FIXME: ? */ 1479 if (pci_set_dma_mask(intel_private.pcidev, DMA_BIT_MASK(mask)))
1451 1480 dev_err(&intel_private.pcidev->dev,
1452 /* we have to call this as early as possible after the MMIO base address is known */ 1481 "set gfx device dma mask %d-bit failed!\n", mask);
1453 intel_i830_init_gtt_entries(); 1482 else
1454 if (intel_private.gtt_entries == 0) { 1483 pci_set_consistent_dma_mask(intel_private.pcidev,
1455 iounmap(intel_private.gtt); 1484 DMA_BIT_MASK(mask));
1456 iounmap(intel_private.registers);
1457 return -ENOMEM;
1458 }
1459 1485
1460 agp_bridge->gatt_table = NULL; 1486 /*if (bridge->driver == &intel_810_driver)
1487 return 1;*/
1461 1488
1462 agp_bridge->gatt_bus_addr = temp; 1489 if (intel_gtt_init() != 0)
1490 return 0;
1463 1491
1464 return 0; 1492 return 1;
1465} 1493}
1494EXPORT_SYMBOL(intel_gmch_probe);
1466 1495
1467static const struct agp_bridge_driver intel_810_driver = { 1496const struct intel_gtt *intel_gtt_get(void)
1468 .owner = THIS_MODULE, 1497{
1469 .aperture_sizes = intel_i810_sizes, 1498 return &intel_private.base;
1470 .size_type = FIXED_APER_SIZE, 1499}
1471 .num_aperture_sizes = 2, 1500EXPORT_SYMBOL(intel_gtt_get);
1472 .needs_scratch_page = true,
1473 .configure = intel_i810_configure,
1474 .fetch_size = intel_i810_fetch_size,
1475 .cleanup = intel_i810_cleanup,
1476 .mask_memory = intel_i810_mask_memory,
1477 .masks = intel_i810_masks,
1478 .agp_enable = intel_i810_agp_enable,
1479 .cache_flush = global_cache_flush,
1480 .create_gatt_table = agp_generic_create_gatt_table,
1481 .free_gatt_table = agp_generic_free_gatt_table,
1482 .insert_memory = intel_i810_insert_entries,
1483 .remove_memory = intel_i810_remove_entries,
1484 .alloc_by_type = intel_i810_alloc_by_type,
1485 .free_by_type = intel_i810_free_by_type,
1486 .agp_alloc_page = agp_generic_alloc_page,
1487 .agp_alloc_pages = agp_generic_alloc_pages,
1488 .agp_destroy_page = agp_generic_destroy_page,
1489 .agp_destroy_pages = agp_generic_destroy_pages,
1490 .agp_type_to_mask_type = agp_generic_type_to_mask_type,
1491};
1492
1493static const struct agp_bridge_driver intel_830_driver = {
1494 .owner = THIS_MODULE,
1495 .aperture_sizes = intel_i830_sizes,
1496 .size_type = FIXED_APER_SIZE,
1497 .num_aperture_sizes = 4,
1498 .needs_scratch_page = true,
1499 .configure = intel_i830_configure,
1500 .fetch_size = intel_i830_fetch_size,
1501 .cleanup = intel_i830_cleanup,
1502 .mask_memory = intel_i810_mask_memory,
1503 .masks = intel_i810_masks,
1504 .agp_enable = intel_i810_agp_enable,
1505 .cache_flush = global_cache_flush,
1506 .create_gatt_table = intel_i830_create_gatt_table,
1507 .free_gatt_table = intel_i830_free_gatt_table,
1508 .insert_memory = intel_i830_insert_entries,
1509 .remove_memory = intel_i830_remove_entries,
1510 .alloc_by_type = intel_i830_alloc_by_type,
1511 .free_by_type = intel_i810_free_by_type,
1512 .agp_alloc_page = agp_generic_alloc_page,
1513 .agp_alloc_pages = agp_generic_alloc_pages,
1514 .agp_destroy_page = agp_generic_destroy_page,
1515 .agp_destroy_pages = agp_generic_destroy_pages,
1516 .agp_type_to_mask_type = intel_i830_type_to_mask_type,
1517 .chipset_flush = intel_i830_chipset_flush,
1518};
1519
1520static const struct agp_bridge_driver intel_915_driver = {
1521 .owner = THIS_MODULE,
1522 .aperture_sizes = intel_i830_sizes,
1523 .size_type = FIXED_APER_SIZE,
1524 .num_aperture_sizes = 4,
1525 .needs_scratch_page = true,
1526 .configure = intel_i9xx_configure,
1527 .fetch_size = intel_i9xx_fetch_size,
1528 .cleanup = intel_i915_cleanup,
1529 .mask_memory = intel_i810_mask_memory,
1530 .masks = intel_i810_masks,
1531 .agp_enable = intel_i810_agp_enable,
1532 .cache_flush = global_cache_flush,
1533 .create_gatt_table = intel_i915_create_gatt_table,
1534 .free_gatt_table = intel_i830_free_gatt_table,
1535 .insert_memory = intel_i915_insert_entries,
1536 .remove_memory = intel_i915_remove_entries,
1537 .alloc_by_type = intel_i830_alloc_by_type,
1538 .free_by_type = intel_i810_free_by_type,
1539 .agp_alloc_page = agp_generic_alloc_page,
1540 .agp_alloc_pages = agp_generic_alloc_pages,
1541 .agp_destroy_page = agp_generic_destroy_page,
1542 .agp_destroy_pages = agp_generic_destroy_pages,
1543 .agp_type_to_mask_type = intel_i830_type_to_mask_type,
1544 .chipset_flush = intel_i915_chipset_flush,
1545#ifdef USE_PCI_DMA_API
1546 .agp_map_page = intel_agp_map_page,
1547 .agp_unmap_page = intel_agp_unmap_page,
1548 .agp_map_memory = intel_agp_map_memory,
1549 .agp_unmap_memory = intel_agp_unmap_memory,
1550#endif
1551};
1552 1501
1553static const struct agp_bridge_driver intel_i965_driver = { 1502void intel_gtt_chipset_flush(void)
1554 .owner = THIS_MODULE, 1503{
1555 .aperture_sizes = intel_i830_sizes, 1504 if (intel_private.driver->chipset_flush)
1556 .size_type = FIXED_APER_SIZE, 1505 intel_private.driver->chipset_flush();
1557 .num_aperture_sizes = 4, 1506}
1558 .needs_scratch_page = true, 1507EXPORT_SYMBOL(intel_gtt_chipset_flush);
1559 .configure = intel_i9xx_configure,
1560 .fetch_size = intel_i9xx_fetch_size,
1561 .cleanup = intel_i915_cleanup,
1562 .mask_memory = intel_i965_mask_memory,
1563 .masks = intel_i810_masks,
1564 .agp_enable = intel_i810_agp_enable,
1565 .cache_flush = global_cache_flush,
1566 .create_gatt_table = intel_i965_create_gatt_table,
1567 .free_gatt_table = intel_i830_free_gatt_table,
1568 .insert_memory = intel_i915_insert_entries,
1569 .remove_memory = intel_i915_remove_entries,
1570 .alloc_by_type = intel_i830_alloc_by_type,
1571 .free_by_type = intel_i810_free_by_type,
1572 .agp_alloc_page = agp_generic_alloc_page,
1573 .agp_alloc_pages = agp_generic_alloc_pages,
1574 .agp_destroy_page = agp_generic_destroy_page,
1575 .agp_destroy_pages = agp_generic_destroy_pages,
1576 .agp_type_to_mask_type = intel_i830_type_to_mask_type,
1577 .chipset_flush = intel_i915_chipset_flush,
1578#ifdef USE_PCI_DMA_API
1579 .agp_map_page = intel_agp_map_page,
1580 .agp_unmap_page = intel_agp_unmap_page,
1581 .agp_map_memory = intel_agp_map_memory,
1582 .agp_unmap_memory = intel_agp_unmap_memory,
1583#endif
1584};
1585 1508
1586static const struct agp_bridge_driver intel_gen6_driver = { 1509void intel_gmch_remove(struct pci_dev *pdev)
1587 .owner = THIS_MODULE, 1510{
1588 .aperture_sizes = intel_i830_sizes, 1511 if (intel_private.pcidev)
1589 .size_type = FIXED_APER_SIZE, 1512 pci_dev_put(intel_private.pcidev);
1590 .num_aperture_sizes = 4, 1513 if (intel_private.bridge_dev)
1591 .needs_scratch_page = true, 1514 pci_dev_put(intel_private.bridge_dev);
1592 .configure = intel_i9xx_configure, 1515}
1593 .fetch_size = intel_i9xx_fetch_size, 1516EXPORT_SYMBOL(intel_gmch_remove);
1594 .cleanup = intel_i915_cleanup,
1595 .mask_memory = intel_gen6_mask_memory,
1596 .masks = intel_gen6_masks,
1597 .agp_enable = intel_i810_agp_enable,
1598 .cache_flush = global_cache_flush,
1599 .create_gatt_table = intel_i965_create_gatt_table,
1600 .free_gatt_table = intel_i830_free_gatt_table,
1601 .insert_memory = intel_i915_insert_entries,
1602 .remove_memory = intel_i915_remove_entries,
1603 .alloc_by_type = intel_i830_alloc_by_type,
1604 .free_by_type = intel_i810_free_by_type,
1605 .agp_alloc_page = agp_generic_alloc_page,
1606 .agp_alloc_pages = agp_generic_alloc_pages,
1607 .agp_destroy_page = agp_generic_destroy_page,
1608 .agp_destroy_pages = agp_generic_destroy_pages,
1609 .agp_type_to_mask_type = intel_gen6_type_to_mask_type,
1610 .chipset_flush = intel_i915_chipset_flush,
1611#ifdef USE_PCI_DMA_API
1612 .agp_map_page = intel_agp_map_page,
1613 .agp_unmap_page = intel_agp_unmap_page,
1614 .agp_map_memory = intel_agp_map_memory,
1615 .agp_unmap_memory = intel_agp_unmap_memory,
1616#endif
1617};
1618 1517
1619static const struct agp_bridge_driver intel_g33_driver = { 1518MODULE_AUTHOR("Dave Jones <davej@redhat.com>");
1620 .owner = THIS_MODULE, 1519MODULE_LICENSE("GPL and additional rights");
1621 .aperture_sizes = intel_i830_sizes,
1622 .size_type = FIXED_APER_SIZE,
1623 .num_aperture_sizes = 4,
1624 .needs_scratch_page = true,
1625 .configure = intel_i9xx_configure,
1626 .fetch_size = intel_i9xx_fetch_size,
1627 .cleanup = intel_i915_cleanup,
1628 .mask_memory = intel_i965_mask_memory,
1629 .masks = intel_i810_masks,
1630 .agp_enable = intel_i810_agp_enable,
1631 .cache_flush = global_cache_flush,
1632 .create_gatt_table = intel_i915_create_gatt_table,
1633 .free_gatt_table = intel_i830_free_gatt_table,
1634 .insert_memory = intel_i915_insert_entries,
1635 .remove_memory = intel_i915_remove_entries,
1636 .alloc_by_type = intel_i830_alloc_by_type,
1637 .free_by_type = intel_i810_free_by_type,
1638 .agp_alloc_page = agp_generic_alloc_page,
1639 .agp_alloc_pages = agp_generic_alloc_pages,
1640 .agp_destroy_page = agp_generic_destroy_page,
1641 .agp_destroy_pages = agp_generic_destroy_pages,
1642 .agp_type_to_mask_type = intel_i830_type_to_mask_type,
1643 .chipset_flush = intel_i915_chipset_flush,
1644#ifdef USE_PCI_DMA_API
1645 .agp_map_page = intel_agp_map_page,
1646 .agp_unmap_page = intel_agp_unmap_page,
1647 .agp_map_memory = intel_agp_map_memory,
1648 .agp_unmap_memory = intel_agp_unmap_memory,
1649#endif
1650};
diff --git a/drivers/char/agp/parisc-agp.c b/drivers/char/agp/parisc-agp.c
index 1c129211302d..94821ab01c6d 100644
--- a/drivers/char/agp/parisc-agp.c
+++ b/drivers/char/agp/parisc-agp.c
@@ -19,6 +19,7 @@
19#include <linux/klist.h> 19#include <linux/klist.h>
20#include <linux/agp_backend.h> 20#include <linux/agp_backend.h>
21#include <linux/log2.h> 21#include <linux/log2.h>
22#include <linux/slab.h>
22 23
23#include <asm/parisc-device.h> 24#include <asm/parisc-device.h>
24#include <asm/ropes.h> 25#include <asm/ropes.h>
@@ -358,8 +359,12 @@ parisc_agp_setup(void __iomem *ioc_hpa, void __iomem *lba_hpa)
358 bridge->dev = fake_bridge_dev; 359 bridge->dev = fake_bridge_dev;
359 360
360 error = agp_add_bridge(bridge); 361 error = agp_add_bridge(bridge);
362 if (error)
363 goto fail;
364 return 0;
361 365
362fail: 366fail:
367 kfree(fake_bridge_dev);
363 return error; 368 return error;
364} 369}
365 370
diff --git a/drivers/char/agp/sworks-agp.c b/drivers/char/agp/sworks-agp.c
index 13acaaf64edb..f02f9b07fd4c 100644
--- a/drivers/char/agp/sworks-agp.c
+++ b/drivers/char/agp/sworks-agp.c
@@ -229,7 +229,7 @@ static int serverworks_fetch_size(void)
229 * This routine could be implemented by taking the addresses 229 * This routine could be implemented by taking the addresses
230 * written to the GATT, and flushing them individually. However 230 * written to the GATT, and flushing them individually. However
231 * currently it just flushes the whole table. Which is probably 231 * currently it just flushes the whole table. Which is probably
232 * more efficent, since agp_memory blocks can be a large number of 232 * more efficient, since agp_memory blocks can be a large number of
233 * entries. 233 * entries.
234 */ 234 */
235static void serverworks_tlbflush(struct agp_memory *temp) 235static void serverworks_tlbflush(struct agp_memory *temp)
diff --git a/drivers/char/agp/uninorth-agp.c b/drivers/char/agp/uninorth-agp.c
index f845a8f718b3..a32c492baf5c 100644
--- a/drivers/char/agp/uninorth-agp.c
+++ b/drivers/char/agp/uninorth-agp.c
@@ -80,7 +80,7 @@ static void uninorth_tlbflush(struct agp_memory *mem)
80 ctrl | UNI_N_CFG_GART_INVAL); 80 ctrl | UNI_N_CFG_GART_INVAL);
81 pci_write_config_dword(agp_bridge->dev, UNI_N_CFG_GART_CTRL, ctrl); 81 pci_write_config_dword(agp_bridge->dev, UNI_N_CFG_GART_CTRL, ctrl);
82 82
83 if (uninorth_rev <= 0x30) { 83 if (!mem && uninorth_rev <= 0x30) {
84 pci_write_config_dword(agp_bridge->dev, UNI_N_CFG_GART_CTRL, 84 pci_write_config_dword(agp_bridge->dev, UNI_N_CFG_GART_CTRL,
85 ctrl | UNI_N_CFG_GART_2xRESET); 85 ctrl | UNI_N_CFG_GART_2xRESET);
86 pci_write_config_dword(agp_bridge->dev, UNI_N_CFG_GART_CTRL, 86 pci_write_config_dword(agp_bridge->dev, UNI_N_CFG_GART_CTRL,
diff --git a/drivers/char/agp/via-agp.c b/drivers/char/agp/via-agp.c
index df67e80019d2..8bc384937401 100644
--- a/drivers/char/agp/via-agp.c
+++ b/drivers/char/agp/via-agp.c
@@ -400,7 +400,7 @@ static struct agp_device_ids via_agp_device_ids[] __devinitdata =
400 * the traditional AGP which resides only in chipset. AGP is used 400 * the traditional AGP which resides only in chipset. AGP is used
401 * by 3D driver which wasn't available for the VT3336 and VT3364 401 * by 3D driver which wasn't available for the VT3336 and VT3364
402 * generation until now. Unfortunately, by testing, VT3364 works 402 * generation until now. Unfortunately, by testing, VT3364 works
403 * but VT3336 doesn't. - explaination from via, just leave this as 403 * but VT3336 doesn't. - explanation from via, just leave this as
404 * as a placeholder to avoid future patches adding it back in. 404 * as a placeholder to avoid future patches adding it back in.
405 */ 405 */
406#if 0 406#if 0
diff --git a/drivers/char/amiserial.c b/drivers/char/amiserial.c
deleted file mode 100644
index a11c8c9ca3d4..000000000000
--- a/drivers/char/amiserial.c
+++ /dev/null
@@ -1,2174 +0,0 @@
1/*
2 * linux/drivers/char/amiserial.c
3 *
4 * Serial driver for the amiga builtin port.
5 *
6 * This code was created by taking serial.c version 4.30 from kernel
7 * release 2.3.22, replacing all hardware related stuff with the
8 * corresponding amiga hardware actions, and removing all irrelevant
9 * code. As a consequence, it uses many of the constants and names
10 * associated with the registers and bits of 16550 compatible UARTS -
11 * but only to keep track of status, etc in the state variables. It
12 * was done this was to make it easier to keep the code in line with
13 * (non hardware specific) changes to serial.c.
14 *
15 * The port is registered with the tty driver as minor device 64, and
16 * therefore other ports should should only use 65 upwards.
17 *
18 * Richard Lucock 28/12/99
19 *
20 * Copyright (C) 1991, 1992 Linus Torvalds
21 * Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997,
22 * 1998, 1999 Theodore Ts'o
23 *
24 */
25
26/*
27 * Serial driver configuration section. Here are the various options:
28 *
29 * SERIAL_PARANOIA_CHECK
30 * Check the magic number for the async_structure where
31 * ever possible.
32 */
33
34#include <linux/delay.h>
35
36#undef SERIAL_PARANOIA_CHECK
37#define SERIAL_DO_RESTART
38
39/* Set of debugging defines */
40
41#undef SERIAL_DEBUG_INTR
42#undef SERIAL_DEBUG_OPEN
43#undef SERIAL_DEBUG_FLOW
44#undef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
45
46/* Sanity checks */
47
48#if defined(MODULE) && defined(SERIAL_DEBUG_MCOUNT)
49#define DBG_CNT(s) printk("(%s): [%x] refc=%d, serc=%d, ttyc=%d -> %s\n", \
50 tty->name, (info->flags), serial_driver->refcount,info->count,tty->count,s)
51#else
52#define DBG_CNT(s)
53#endif
54
55/*
56 * End of serial driver configuration section.
57 */
58
59#include <linux/module.h>
60
61#include <linux/types.h>
62#include <linux/serial.h>
63#include <linux/serialP.h>
64#include <linux/serial_reg.h>
65static char *serial_version = "4.30";
66
67#include <linux/errno.h>
68#include <linux/signal.h>
69#include <linux/sched.h>
70#include <linux/kernel.h>
71#include <linux/timer.h>
72#include <linux/interrupt.h>
73#include <linux/tty.h>
74#include <linux/tty_flip.h>
75#include <linux/console.h>
76#include <linux/major.h>
77#include <linux/string.h>
78#include <linux/fcntl.h>
79#include <linux/ptrace.h>
80#include <linux/ioport.h>
81#include <linux/mm.h>
82#include <linux/seq_file.h>
83#include <linux/slab.h>
84#include <linux/smp_lock.h>
85#include <linux/init.h>
86#include <linux/bitops.h>
87#include <linux/platform_device.h>
88
89#include <asm/setup.h>
90
91#include <asm/system.h>
92
93#include <asm/irq.h>
94
95#include <asm/amigahw.h>
96#include <asm/amigaints.h>
97
98#define custom amiga_custom
99static char *serial_name = "Amiga-builtin serial driver";
100
101static struct tty_driver *serial_driver;
102
103/* number of characters left in xmit buffer before we ask for more */
104#define WAKEUP_CHARS 256
105
106static struct async_struct *IRQ_ports;
107
108static unsigned char current_ctl_bits;
109
110static void change_speed(struct async_struct *info, struct ktermios *old);
111static void rs_wait_until_sent(struct tty_struct *tty, int timeout);
112
113
114static struct serial_state rs_table[1];
115
116#define NR_PORTS ARRAY_SIZE(rs_table)
117
118#include <asm/uaccess.h>
119
120#define serial_isroot() (capable(CAP_SYS_ADMIN))
121
122
123static inline int serial_paranoia_check(struct async_struct *info,
124 char *name, const char *routine)
125{
126#ifdef SERIAL_PARANOIA_CHECK
127 static const char *badmagic =
128 "Warning: bad magic number for serial struct (%s) in %s\n";
129 static const char *badinfo =
130 "Warning: null async_struct for (%s) in %s\n";
131
132 if (!info) {
133 printk(badinfo, name, routine);
134 return 1;
135 }
136 if (info->magic != SERIAL_MAGIC) {
137 printk(badmagic, name, routine);
138 return 1;
139 }
140#endif
141 return 0;
142}
143
144/* some serial hardware definitions */
145#define SDR_OVRUN (1<<15)
146#define SDR_RBF (1<<14)
147#define SDR_TBE (1<<13)
148#define SDR_TSRE (1<<12)
149
150#define SERPER_PARENB (1<<15)
151
152#define AC_SETCLR (1<<15)
153#define AC_UARTBRK (1<<11)
154
155#define SER_DTR (1<<7)
156#define SER_RTS (1<<6)
157#define SER_DCD (1<<5)
158#define SER_CTS (1<<4)
159#define SER_DSR (1<<3)
160
161static __inline__ void rtsdtr_ctrl(int bits)
162{
163 ciab.pra = ((bits & (SER_RTS | SER_DTR)) ^ (SER_RTS | SER_DTR)) | (ciab.pra & ~(SER_RTS | SER_DTR));
164}
165
166/*
167 * ------------------------------------------------------------
168 * rs_stop() and rs_start()
169 *
170 * This routines are called before setting or resetting tty->stopped.
171 * They enable or disable transmitter interrupts, as necessary.
172 * ------------------------------------------------------------
173 */
174static void rs_stop(struct tty_struct *tty)
175{
176 struct async_struct *info = tty->driver_data;
177 unsigned long flags;
178
179 if (serial_paranoia_check(info, tty->name, "rs_stop"))
180 return;
181
182 local_irq_save(flags);
183 if (info->IER & UART_IER_THRI) {
184 info->IER &= ~UART_IER_THRI;
185 /* disable Tx interrupt and remove any pending interrupts */
186 custom.intena = IF_TBE;
187 mb();
188 custom.intreq = IF_TBE;
189 mb();
190 }
191 local_irq_restore(flags);
192}
193
194static void rs_start(struct tty_struct *tty)
195{
196 struct async_struct *info = tty->driver_data;
197 unsigned long flags;
198
199 if (serial_paranoia_check(info, tty->name, "rs_start"))
200 return;
201
202 local_irq_save(flags);
203 if (info->xmit.head != info->xmit.tail
204 && info->xmit.buf
205 && !(info->IER & UART_IER_THRI)) {
206 info->IER |= UART_IER_THRI;
207 custom.intena = IF_SETCLR | IF_TBE;
208 mb();
209 /* set a pending Tx Interrupt, transmitter should restart now */
210 custom.intreq = IF_SETCLR | IF_TBE;
211 mb();
212 }
213 local_irq_restore(flags);
214}
215
216/*
217 * ----------------------------------------------------------------------
218 *
219 * Here starts the interrupt handling routines. All of the following
220 * subroutines are declared as inline and are folded into
221 * rs_interrupt(). They were separated out for readability's sake.
222 *
223 * Note: rs_interrupt() is a "fast" interrupt, which means that it
224 * runs with interrupts turned off. People who may want to modify
225 * rs_interrupt() should try to keep the interrupt handler as fast as
226 * possible. After you are done making modifications, it is not a bad
227 * idea to do:
228 *
229 * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
230 *
231 * and look at the resulting assemble code in serial.s.
232 *
233 * - Ted Ts'o (tytso@mit.edu), 7-Mar-93
234 * -----------------------------------------------------------------------
235 */
236
237/*
238 * This routine is used by the interrupt handler to schedule
239 * processing in the software interrupt portion of the driver.
240 */
241static void rs_sched_event(struct async_struct *info,
242 int event)
243{
244 info->event |= 1 << event;
245 tasklet_schedule(&info->tlet);
246}
247
248static void receive_chars(struct async_struct *info)
249{
250 int status;
251 int serdatr;
252 struct tty_struct *tty = info->tty;
253 unsigned char ch, flag;
254 struct async_icount *icount;
255 int oe = 0;
256
257 icount = &info->state->icount;
258
259 status = UART_LSR_DR; /* We obviously have a character! */
260 serdatr = custom.serdatr;
261 mb();
262 custom.intreq = IF_RBF;
263 mb();
264
265 if((serdatr & 0x1ff) == 0)
266 status |= UART_LSR_BI;
267 if(serdatr & SDR_OVRUN)
268 status |= UART_LSR_OE;
269
270 ch = serdatr & 0xff;
271 icount->rx++;
272
273#ifdef SERIAL_DEBUG_INTR
274 printk("DR%02x:%02x...", ch, status);
275#endif
276 flag = TTY_NORMAL;
277
278 /*
279 * We don't handle parity or frame errors - but I have left
280 * the code in, since I'm not sure that the errors can't be
281 * detected.
282 */
283
284 if (status & (UART_LSR_BI | UART_LSR_PE |
285 UART_LSR_FE | UART_LSR_OE)) {
286 /*
287 * For statistics only
288 */
289 if (status & UART_LSR_BI) {
290 status &= ~(UART_LSR_FE | UART_LSR_PE);
291 icount->brk++;
292 } else if (status & UART_LSR_PE)
293 icount->parity++;
294 else if (status & UART_LSR_FE)
295 icount->frame++;
296 if (status & UART_LSR_OE)
297 icount->overrun++;
298
299 /*
300 * Now check to see if character should be
301 * ignored, and mask off conditions which
302 * should be ignored.
303 */
304 if (status & info->ignore_status_mask)
305 goto out;
306
307 status &= info->read_status_mask;
308
309 if (status & (UART_LSR_BI)) {
310#ifdef SERIAL_DEBUG_INTR
311 printk("handling break....");
312#endif
313 flag = TTY_BREAK;
314 if (info->flags & ASYNC_SAK)
315 do_SAK(tty);
316 } else if (status & UART_LSR_PE)
317 flag = TTY_PARITY;
318 else if (status & UART_LSR_FE)
319 flag = TTY_FRAME;
320 if (status & UART_LSR_OE) {
321 /*
322 * Overrun is special, since it's
323 * reported immediately, and doesn't
324 * affect the current character
325 */
326 oe = 1;
327 }
328 }
329 tty_insert_flip_char(tty, ch, flag);
330 if (oe == 1)
331 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
332 tty_flip_buffer_push(tty);
333out:
334 return;
335}
336
337static void transmit_chars(struct async_struct *info)
338{
339 custom.intreq = IF_TBE;
340 mb();
341 if (info->x_char) {
342 custom.serdat = info->x_char | 0x100;
343 mb();
344 info->state->icount.tx++;
345 info->x_char = 0;
346 return;
347 }
348 if (info->xmit.head == info->xmit.tail
349 || info->tty->stopped
350 || info->tty->hw_stopped) {
351 info->IER &= ~UART_IER_THRI;
352 custom.intena = IF_TBE;
353 mb();
354 return;
355 }
356
357 custom.serdat = info->xmit.buf[info->xmit.tail++] | 0x100;
358 mb();
359 info->xmit.tail = info->xmit.tail & (SERIAL_XMIT_SIZE-1);
360 info->state->icount.tx++;
361
362 if (CIRC_CNT(info->xmit.head,
363 info->xmit.tail,
364 SERIAL_XMIT_SIZE) < WAKEUP_CHARS)
365 rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
366
367#ifdef SERIAL_DEBUG_INTR
368 printk("THRE...");
369#endif
370 if (info->xmit.head == info->xmit.tail) {
371 custom.intena = IF_TBE;
372 mb();
373 info->IER &= ~UART_IER_THRI;
374 }
375}
376
377static void check_modem_status(struct async_struct *info)
378{
379 unsigned char status = ciab.pra & (SER_DCD | SER_CTS | SER_DSR);
380 unsigned char dstatus;
381 struct async_icount *icount;
382
383 /* Determine bits that have changed */
384 dstatus = status ^ current_ctl_bits;
385 current_ctl_bits = status;
386
387 if (dstatus) {
388 icount = &info->state->icount;
389 /* update input line counters */
390 if (dstatus & SER_DSR)
391 icount->dsr++;
392 if (dstatus & SER_DCD) {
393 icount->dcd++;
394#ifdef CONFIG_HARD_PPS
395 if ((info->flags & ASYNC_HARDPPS_CD) &&
396 !(status & SER_DCD))
397 hardpps();
398#endif
399 }
400 if (dstatus & SER_CTS)
401 icount->cts++;
402 wake_up_interruptible(&info->delta_msr_wait);
403 }
404
405 if ((info->flags & ASYNC_CHECK_CD) && (dstatus & SER_DCD)) {
406#if (defined(SERIAL_DEBUG_OPEN) || defined(SERIAL_DEBUG_INTR))
407 printk("ttyS%d CD now %s...", info->line,
408 (!(status & SER_DCD)) ? "on" : "off");
409#endif
410 if (!(status & SER_DCD))
411 wake_up_interruptible(&info->open_wait);
412 else {
413#ifdef SERIAL_DEBUG_OPEN
414 printk("doing serial hangup...");
415#endif
416 if (info->tty)
417 tty_hangup(info->tty);
418 }
419 }
420 if (info->flags & ASYNC_CTS_FLOW) {
421 if (info->tty->hw_stopped) {
422 if (!(status & SER_CTS)) {
423#if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
424 printk("CTS tx start...");
425#endif
426 info->tty->hw_stopped = 0;
427 info->IER |= UART_IER_THRI;
428 custom.intena = IF_SETCLR | IF_TBE;
429 mb();
430 /* set a pending Tx Interrupt, transmitter should restart now */
431 custom.intreq = IF_SETCLR | IF_TBE;
432 mb();
433 rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
434 return;
435 }
436 } else {
437 if ((status & SER_CTS)) {
438#if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
439 printk("CTS tx stop...");
440#endif
441 info->tty->hw_stopped = 1;
442 info->IER &= ~UART_IER_THRI;
443 /* disable Tx interrupt and remove any pending interrupts */
444 custom.intena = IF_TBE;
445 mb();
446 custom.intreq = IF_TBE;
447 mb();
448 }
449 }
450 }
451}
452
453static irqreturn_t ser_vbl_int( int irq, void *data)
454{
455 /* vbl is just a periodic interrupt we tie into to update modem status */
456 struct async_struct * info = IRQ_ports;
457 /*
458 * TBD - is it better to unregister from this interrupt or to
459 * ignore it if MSI is clear ?
460 */
461 if(info->IER & UART_IER_MSI)
462 check_modem_status(info);
463 return IRQ_HANDLED;
464}
465
466static irqreturn_t ser_rx_int(int irq, void *dev_id)
467{
468 struct async_struct * info;
469
470#ifdef SERIAL_DEBUG_INTR
471 printk("ser_rx_int...");
472#endif
473
474 info = IRQ_ports;
475 if (!info || !info->tty)
476 return IRQ_NONE;
477
478 receive_chars(info);
479 info->last_active = jiffies;
480#ifdef SERIAL_DEBUG_INTR
481 printk("end.\n");
482#endif
483 return IRQ_HANDLED;
484}
485
486static irqreturn_t ser_tx_int(int irq, void *dev_id)
487{
488 struct async_struct * info;
489
490 if (custom.serdatr & SDR_TBE) {
491#ifdef SERIAL_DEBUG_INTR
492 printk("ser_tx_int...");
493#endif
494
495 info = IRQ_ports;
496 if (!info || !info->tty)
497 return IRQ_NONE;
498
499 transmit_chars(info);
500 info->last_active = jiffies;
501#ifdef SERIAL_DEBUG_INTR
502 printk("end.\n");
503#endif
504 }
505 return IRQ_HANDLED;
506}
507
508/*
509 * -------------------------------------------------------------------
510 * Here ends the serial interrupt routines.
511 * -------------------------------------------------------------------
512 */
513
514/*
515 * This routine is used to handle the "bottom half" processing for the
516 * serial driver, known also the "software interrupt" processing.
517 * This processing is done at the kernel interrupt level, after the
518 * rs_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON. This
519 * is where time-consuming activities which can not be done in the
520 * interrupt driver proper are done; the interrupt driver schedules
521 * them using rs_sched_event(), and they get done here.
522 */
523
524static void do_softint(unsigned long private_)
525{
526 struct async_struct *info = (struct async_struct *) private_;
527 struct tty_struct *tty;
528
529 tty = info->tty;
530 if (!tty)
531 return;
532
533 if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event))
534 tty_wakeup(tty);
535}
536
537/*
538 * ---------------------------------------------------------------
539 * Low level utility subroutines for the serial driver: routines to
540 * figure out the appropriate timeout for an interrupt chain, routines
541 * to initialize and startup a serial port, and routines to shutdown a
542 * serial port. Useful stuff like that.
543 * ---------------------------------------------------------------
544 */
545
546static int startup(struct async_struct * info)
547{
548 unsigned long flags;
549 int retval=0;
550 unsigned long page;
551
552 page = get_zeroed_page(GFP_KERNEL);
553 if (!page)
554 return -ENOMEM;
555
556 local_irq_save(flags);
557
558 if (info->flags & ASYNC_INITIALIZED) {
559 free_page(page);
560 goto errout;
561 }
562
563 if (info->xmit.buf)
564 free_page(page);
565 else
566 info->xmit.buf = (unsigned char *) page;
567
568#ifdef SERIAL_DEBUG_OPEN
569 printk("starting up ttys%d ...", info->line);
570#endif
571
572 /* Clear anything in the input buffer */
573
574 custom.intreq = IF_RBF;
575 mb();
576
577 retval = request_irq(IRQ_AMIGA_VERTB, ser_vbl_int, 0, "serial status", info);
578 if (retval) {
579 if (serial_isroot()) {
580 if (info->tty)
581 set_bit(TTY_IO_ERROR,
582 &info->tty->flags);
583 retval = 0;
584 }
585 goto errout;
586 }
587
588 /* enable both Rx and Tx interrupts */
589 custom.intena = IF_SETCLR | IF_RBF | IF_TBE;
590 mb();
591 info->IER = UART_IER_MSI;
592
593 /* remember current state of the DCD and CTS bits */
594 current_ctl_bits = ciab.pra & (SER_DCD | SER_CTS | SER_DSR);
595
596 IRQ_ports = info;
597
598 info->MCR = 0;
599 if (info->tty->termios->c_cflag & CBAUD)
600 info->MCR = SER_DTR | SER_RTS;
601 rtsdtr_ctrl(info->MCR);
602
603 if (info->tty)
604 clear_bit(TTY_IO_ERROR, &info->tty->flags);
605 info->xmit.head = info->xmit.tail = 0;
606
607 /*
608 * Set up the tty->alt_speed kludge
609 */
610 if (info->tty) {
611 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
612 info->tty->alt_speed = 57600;
613 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
614 info->tty->alt_speed = 115200;
615 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
616 info->tty->alt_speed = 230400;
617 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
618 info->tty->alt_speed = 460800;
619 }
620
621 /*
622 * and set the speed of the serial port
623 */
624 change_speed(info, NULL);
625
626 info->flags |= ASYNC_INITIALIZED;
627 local_irq_restore(flags);
628 return 0;
629
630errout:
631 local_irq_restore(flags);
632 return retval;
633}
634
635/*
636 * This routine will shutdown a serial port; interrupts are disabled, and
637 * DTR is dropped if the hangup on close termio flag is on.
638 */
639static void shutdown(struct async_struct * info)
640{
641 unsigned long flags;
642 struct serial_state *state;
643
644 if (!(info->flags & ASYNC_INITIALIZED))
645 return;
646
647 state = info->state;
648
649#ifdef SERIAL_DEBUG_OPEN
650 printk("Shutting down serial port %d ....\n", info->line);
651#endif
652
653 local_irq_save(flags); /* Disable interrupts */
654
655 /*
656 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
657 * here so the queue might never be waken up
658 */
659 wake_up_interruptible(&info->delta_msr_wait);
660
661 IRQ_ports = NULL;
662
663 /*
664 * Free the IRQ, if necessary
665 */
666 free_irq(IRQ_AMIGA_VERTB, info);
667
668 if (info->xmit.buf) {
669 free_page((unsigned long) info->xmit.buf);
670 info->xmit.buf = NULL;
671 }
672
673 info->IER = 0;
674 custom.intena = IF_RBF | IF_TBE;
675 mb();
676
677 /* disable break condition */
678 custom.adkcon = AC_UARTBRK;
679 mb();
680
681 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
682 info->MCR &= ~(SER_DTR|SER_RTS);
683 rtsdtr_ctrl(info->MCR);
684
685 if (info->tty)
686 set_bit(TTY_IO_ERROR, &info->tty->flags);
687
688 info->flags &= ~ASYNC_INITIALIZED;
689 local_irq_restore(flags);
690}
691
692
693/*
694 * This routine is called to set the UART divisor registers to match
695 * the specified baud rate for a serial port.
696 */
697static void change_speed(struct async_struct *info,
698 struct ktermios *old_termios)
699{
700 int quot = 0, baud_base, baud;
701 unsigned cflag, cval = 0;
702 int bits;
703 unsigned long flags;
704
705 if (!info->tty || !info->tty->termios)
706 return;
707 cflag = info->tty->termios->c_cflag;
708
709 /* Byte size is always 8 bits plus parity bit if requested */
710
711 cval = 3; bits = 10;
712 if (cflag & CSTOPB) {
713 cval |= 0x04;
714 bits++;
715 }
716 if (cflag & PARENB) {
717 cval |= UART_LCR_PARITY;
718 bits++;
719 }
720 if (!(cflag & PARODD))
721 cval |= UART_LCR_EPAR;
722#ifdef CMSPAR
723 if (cflag & CMSPAR)
724 cval |= UART_LCR_SPAR;
725#endif
726
727 /* Determine divisor based on baud rate */
728 baud = tty_get_baud_rate(info->tty);
729 if (!baud)
730 baud = 9600; /* B0 transition handled in rs_set_termios */
731 baud_base = info->state->baud_base;
732 if (baud == 38400 &&
733 ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST))
734 quot = info->state->custom_divisor;
735 else {
736 if (baud == 134)
737 /* Special case since 134 is really 134.5 */
738 quot = (2*baud_base / 269);
739 else if (baud)
740 quot = baud_base / baud;
741 }
742 /* If the quotient is zero refuse the change */
743 if (!quot && old_termios) {
744 /* FIXME: Will need updating for new tty in the end */
745 info->tty->termios->c_cflag &= ~CBAUD;
746 info->tty->termios->c_cflag |= (old_termios->c_cflag & CBAUD);
747 baud = tty_get_baud_rate(info->tty);
748 if (!baud)
749 baud = 9600;
750 if (baud == 38400 &&
751 ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST))
752 quot = info->state->custom_divisor;
753 else {
754 if (baud == 134)
755 /* Special case since 134 is really 134.5 */
756 quot = (2*baud_base / 269);
757 else if (baud)
758 quot = baud_base / baud;
759 }
760 }
761 /* As a last resort, if the quotient is zero, default to 9600 bps */
762 if (!quot)
763 quot = baud_base / 9600;
764 info->quot = quot;
765 info->timeout = ((info->xmit_fifo_size*HZ*bits*quot) / baud_base);
766 info->timeout += HZ/50; /* Add .02 seconds of slop */
767
768 /* CTS flow control flag and modem status interrupts */
769 info->IER &= ~UART_IER_MSI;
770 if (info->flags & ASYNC_HARDPPS_CD)
771 info->IER |= UART_IER_MSI;
772 if (cflag & CRTSCTS) {
773 info->flags |= ASYNC_CTS_FLOW;
774 info->IER |= UART_IER_MSI;
775 } else
776 info->flags &= ~ASYNC_CTS_FLOW;
777 if (cflag & CLOCAL)
778 info->flags &= ~ASYNC_CHECK_CD;
779 else {
780 info->flags |= ASYNC_CHECK_CD;
781 info->IER |= UART_IER_MSI;
782 }
783 /* TBD:
784 * Does clearing IER_MSI imply that we should disable the VBL interrupt ?
785 */
786
787 /*
788 * Set up parity check flag
789 */
790
791 info->read_status_mask = UART_LSR_OE | UART_LSR_DR;
792 if (I_INPCK(info->tty))
793 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
794 if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
795 info->read_status_mask |= UART_LSR_BI;
796
797 /*
798 * Characters to ignore
799 */
800 info->ignore_status_mask = 0;
801 if (I_IGNPAR(info->tty))
802 info->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
803 if (I_IGNBRK(info->tty)) {
804 info->ignore_status_mask |= UART_LSR_BI;
805 /*
806 * If we're ignore parity and break indicators, ignore
807 * overruns too. (For real raw support).
808 */
809 if (I_IGNPAR(info->tty))
810 info->ignore_status_mask |= UART_LSR_OE;
811 }
812 /*
813 * !!! ignore all characters if CREAD is not set
814 */
815 if ((cflag & CREAD) == 0)
816 info->ignore_status_mask |= UART_LSR_DR;
817 local_irq_save(flags);
818
819 {
820 short serper;
821
822 /* Set up the baud rate */
823 serper = quot - 1;
824
825 /* Enable or disable parity bit */
826
827 if(cval & UART_LCR_PARITY)
828 serper |= (SERPER_PARENB);
829
830 custom.serper = serper;
831 mb();
832 }
833
834 info->LCR = cval; /* Save LCR */
835 local_irq_restore(flags);
836}
837
838static int rs_put_char(struct tty_struct *tty, unsigned char ch)
839{
840 struct async_struct *info;
841 unsigned long flags;
842
843 info = tty->driver_data;
844
845 if (serial_paranoia_check(info, tty->name, "rs_put_char"))
846 return 0;
847
848 if (!info->xmit.buf)
849 return 0;
850
851 local_irq_save(flags);
852 if (CIRC_SPACE(info->xmit.head,
853 info->xmit.tail,
854 SERIAL_XMIT_SIZE) == 0) {
855 local_irq_restore(flags);
856 return 0;
857 }
858
859 info->xmit.buf[info->xmit.head++] = ch;
860 info->xmit.head &= SERIAL_XMIT_SIZE-1;
861 local_irq_restore(flags);
862 return 1;
863}
864
865static void rs_flush_chars(struct tty_struct *tty)
866{
867 struct async_struct *info = tty->driver_data;
868 unsigned long flags;
869
870 if (serial_paranoia_check(info, tty->name, "rs_flush_chars"))
871 return;
872
873 if (info->xmit.head == info->xmit.tail
874 || tty->stopped
875 || tty->hw_stopped
876 || !info->xmit.buf)
877 return;
878
879 local_irq_save(flags);
880 info->IER |= UART_IER_THRI;
881 custom.intena = IF_SETCLR | IF_TBE;
882 mb();
883 /* set a pending Tx Interrupt, transmitter should restart now */
884 custom.intreq = IF_SETCLR | IF_TBE;
885 mb();
886 local_irq_restore(flags);
887}
888
889static int rs_write(struct tty_struct * tty, const unsigned char *buf, int count)
890{
891 int c, ret = 0;
892 struct async_struct *info;
893 unsigned long flags;
894
895 info = tty->driver_data;
896
897 if (serial_paranoia_check(info, tty->name, "rs_write"))
898 return 0;
899
900 if (!info->xmit.buf)
901 return 0;
902
903 local_irq_save(flags);
904 while (1) {
905 c = CIRC_SPACE_TO_END(info->xmit.head,
906 info->xmit.tail,
907 SERIAL_XMIT_SIZE);
908 if (count < c)
909 c = count;
910 if (c <= 0) {
911 break;
912 }
913 memcpy(info->xmit.buf + info->xmit.head, buf, c);
914 info->xmit.head = ((info->xmit.head + c) &
915 (SERIAL_XMIT_SIZE-1));
916 buf += c;
917 count -= c;
918 ret += c;
919 }
920 local_irq_restore(flags);
921
922 if (info->xmit.head != info->xmit.tail
923 && !tty->stopped
924 && !tty->hw_stopped
925 && !(info->IER & UART_IER_THRI)) {
926 info->IER |= UART_IER_THRI;
927 local_irq_disable();
928 custom.intena = IF_SETCLR | IF_TBE;
929 mb();
930 /* set a pending Tx Interrupt, transmitter should restart now */
931 custom.intreq = IF_SETCLR | IF_TBE;
932 mb();
933 local_irq_restore(flags);
934 }
935 return ret;
936}
937
938static int rs_write_room(struct tty_struct *tty)
939{
940 struct async_struct *info = tty->driver_data;
941
942 if (serial_paranoia_check(info, tty->name, "rs_write_room"))
943 return 0;
944 return CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
945}
946
947static int rs_chars_in_buffer(struct tty_struct *tty)
948{
949 struct async_struct *info = tty->driver_data;
950
951 if (serial_paranoia_check(info, tty->name, "rs_chars_in_buffer"))
952 return 0;
953 return CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
954}
955
956static void rs_flush_buffer(struct tty_struct *tty)
957{
958 struct async_struct *info = tty->driver_data;
959 unsigned long flags;
960
961 if (serial_paranoia_check(info, tty->name, "rs_flush_buffer"))
962 return;
963 local_irq_save(flags);
964 info->xmit.head = info->xmit.tail = 0;
965 local_irq_restore(flags);
966 tty_wakeup(tty);
967}
968
969/*
970 * This function is used to send a high-priority XON/XOFF character to
971 * the device
972 */
973static void rs_send_xchar(struct tty_struct *tty, char ch)
974{
975 struct async_struct *info = tty->driver_data;
976 unsigned long flags;
977
978 if (serial_paranoia_check(info, tty->name, "rs_send_char"))
979 return;
980
981 info->x_char = ch;
982 if (ch) {
983 /* Make sure transmit interrupts are on */
984
985 /* Check this ! */
986 local_irq_save(flags);
987 if(!(custom.intenar & IF_TBE)) {
988 custom.intena = IF_SETCLR | IF_TBE;
989 mb();
990 /* set a pending Tx Interrupt, transmitter should restart now */
991 custom.intreq = IF_SETCLR | IF_TBE;
992 mb();
993 }
994 local_irq_restore(flags);
995
996 info->IER |= UART_IER_THRI;
997 }
998}
999
1000/*
1001 * ------------------------------------------------------------
1002 * rs_throttle()
1003 *
1004 * This routine is called by the upper-layer tty layer to signal that
1005 * incoming characters should be throttled.
1006 * ------------------------------------------------------------
1007 */
1008static void rs_throttle(struct tty_struct * tty)
1009{
1010 struct async_struct *info = tty->driver_data;
1011 unsigned long flags;
1012#ifdef SERIAL_DEBUG_THROTTLE
1013 char buf[64];
1014
1015 printk("throttle %s: %d....\n", tty_name(tty, buf),
1016 tty->ldisc.chars_in_buffer(tty));
1017#endif
1018
1019 if (serial_paranoia_check(info, tty->name, "rs_throttle"))
1020 return;
1021
1022 if (I_IXOFF(tty))
1023 rs_send_xchar(tty, STOP_CHAR(tty));
1024
1025 if (tty->termios->c_cflag & CRTSCTS)
1026 info->MCR &= ~SER_RTS;
1027
1028 local_irq_save(flags);
1029 rtsdtr_ctrl(info->MCR);
1030 local_irq_restore(flags);
1031}
1032
1033static void rs_unthrottle(struct tty_struct * tty)
1034{
1035 struct async_struct *info = tty->driver_data;
1036 unsigned long flags;
1037#ifdef SERIAL_DEBUG_THROTTLE
1038 char buf[64];
1039
1040 printk("unthrottle %s: %d....\n", tty_name(tty, buf),
1041 tty->ldisc.chars_in_buffer(tty));
1042#endif
1043
1044 if (serial_paranoia_check(info, tty->name, "rs_unthrottle"))
1045 return;
1046
1047 if (I_IXOFF(tty)) {
1048 if (info->x_char)
1049 info->x_char = 0;
1050 else
1051 rs_send_xchar(tty, START_CHAR(tty));
1052 }
1053 if (tty->termios->c_cflag & CRTSCTS)
1054 info->MCR |= SER_RTS;
1055 local_irq_save(flags);
1056 rtsdtr_ctrl(info->MCR);
1057 local_irq_restore(flags);
1058}
1059
1060/*
1061 * ------------------------------------------------------------
1062 * rs_ioctl() and friends
1063 * ------------------------------------------------------------
1064 */
1065
1066static int get_serial_info(struct async_struct * info,
1067 struct serial_struct __user * retinfo)
1068{
1069 struct serial_struct tmp;
1070 struct serial_state *state = info->state;
1071
1072 if (!retinfo)
1073 return -EFAULT;
1074 memset(&tmp, 0, sizeof(tmp));
1075 tty_lock();
1076 tmp.type = state->type;
1077 tmp.line = state->line;
1078 tmp.port = state->port;
1079 tmp.irq = state->irq;
1080 tmp.flags = state->flags;
1081 tmp.xmit_fifo_size = state->xmit_fifo_size;
1082 tmp.baud_base = state->baud_base;
1083 tmp.close_delay = state->close_delay;
1084 tmp.closing_wait = state->closing_wait;
1085 tmp.custom_divisor = state->custom_divisor;
1086 tty_unlock();
1087 if (copy_to_user(retinfo,&tmp,sizeof(*retinfo)))
1088 return -EFAULT;
1089 return 0;
1090}
1091
1092static int set_serial_info(struct async_struct * info,
1093 struct serial_struct __user * new_info)
1094{
1095 struct serial_struct new_serial;
1096 struct serial_state old_state, *state;
1097 unsigned int change_irq,change_port;
1098 int retval = 0;
1099
1100 if (copy_from_user(&new_serial,new_info,sizeof(new_serial)))
1101 return -EFAULT;
1102
1103 tty_lock();
1104 state = info->state;
1105 old_state = *state;
1106
1107 change_irq = new_serial.irq != state->irq;
1108 change_port = (new_serial.port != state->port);
1109 if(change_irq || change_port || (new_serial.xmit_fifo_size != state->xmit_fifo_size)) {
1110 tty_unlock();
1111 return -EINVAL;
1112 }
1113
1114 if (!serial_isroot()) {
1115 if ((new_serial.baud_base != state->baud_base) ||
1116 (new_serial.close_delay != state->close_delay) ||
1117 (new_serial.xmit_fifo_size != state->xmit_fifo_size) ||
1118 ((new_serial.flags & ~ASYNC_USR_MASK) !=
1119 (state->flags & ~ASYNC_USR_MASK)))
1120 return -EPERM;
1121 state->flags = ((state->flags & ~ASYNC_USR_MASK) |
1122 (new_serial.flags & ASYNC_USR_MASK));
1123 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
1124 (new_serial.flags & ASYNC_USR_MASK));
1125 state->custom_divisor = new_serial.custom_divisor;
1126 goto check_and_exit;
1127 }
1128
1129 if (new_serial.baud_base < 9600) {
1130 tty_unlock();
1131 return -EINVAL;
1132 }
1133
1134 /*
1135 * OK, past this point, all the error checking has been done.
1136 * At this point, we start making changes.....
1137 */
1138
1139 state->baud_base = new_serial.baud_base;
1140 state->flags = ((state->flags & ~ASYNC_FLAGS) |
1141 (new_serial.flags & ASYNC_FLAGS));
1142 info->flags = ((state->flags & ~ASYNC_INTERNAL_FLAGS) |
1143 (info->flags & ASYNC_INTERNAL_FLAGS));
1144 state->custom_divisor = new_serial.custom_divisor;
1145 state->close_delay = new_serial.close_delay * HZ/100;
1146 state->closing_wait = new_serial.closing_wait * HZ/100;
1147 info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
1148
1149check_and_exit:
1150 if (info->flags & ASYNC_INITIALIZED) {
1151 if (((old_state.flags & ASYNC_SPD_MASK) !=
1152 (state->flags & ASYNC_SPD_MASK)) ||
1153 (old_state.custom_divisor != state->custom_divisor)) {
1154 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
1155 info->tty->alt_speed = 57600;
1156 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
1157 info->tty->alt_speed = 115200;
1158 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
1159 info->tty->alt_speed = 230400;
1160 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
1161 info->tty->alt_speed = 460800;
1162 change_speed(info, NULL);
1163 }
1164 } else
1165 retval = startup(info);
1166 tty_unlock();
1167 return retval;
1168}
1169
1170
1171/*
1172 * get_lsr_info - get line status register info
1173 *
1174 * Purpose: Let user call ioctl() to get info when the UART physically
1175 * is emptied. On bus types like RS485, the transmitter must
1176 * release the bus after transmitting. This must be done when
1177 * the transmit shift register is empty, not be done when the
1178 * transmit holding register is empty. This functionality
1179 * allows an RS485 driver to be written in user space.
1180 */
1181static int get_lsr_info(struct async_struct * info, unsigned int __user *value)
1182{
1183 unsigned char status;
1184 unsigned int result;
1185 unsigned long flags;
1186
1187 local_irq_save(flags);
1188 status = custom.serdatr;
1189 mb();
1190 local_irq_restore(flags);
1191 result = ((status & SDR_TSRE) ? TIOCSER_TEMT : 0);
1192 if (copy_to_user(value, &result, sizeof(int)))
1193 return -EFAULT;
1194 return 0;
1195}
1196
1197
1198static int rs_tiocmget(struct tty_struct *tty, struct file *file)
1199{
1200 struct async_struct * info = tty->driver_data;
1201 unsigned char control, status;
1202 unsigned long flags;
1203
1204 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
1205 return -ENODEV;
1206 if (tty->flags & (1 << TTY_IO_ERROR))
1207 return -EIO;
1208
1209 control = info->MCR;
1210 local_irq_save(flags);
1211 status = ciab.pra;
1212 local_irq_restore(flags);
1213 return ((control & SER_RTS) ? TIOCM_RTS : 0)
1214 | ((control & SER_DTR) ? TIOCM_DTR : 0)
1215 | (!(status & SER_DCD) ? TIOCM_CAR : 0)
1216 | (!(status & SER_DSR) ? TIOCM_DSR : 0)
1217 | (!(status & SER_CTS) ? TIOCM_CTS : 0);
1218}
1219
1220static int rs_tiocmset(struct tty_struct *tty, struct file *file,
1221 unsigned int set, unsigned int clear)
1222{
1223 struct async_struct * info = tty->driver_data;
1224 unsigned long flags;
1225
1226 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
1227 return -ENODEV;
1228 if (tty->flags & (1 << TTY_IO_ERROR))
1229 return -EIO;
1230
1231 local_irq_save(flags);
1232 if (set & TIOCM_RTS)
1233 info->MCR |= SER_RTS;
1234 if (set & TIOCM_DTR)
1235 info->MCR |= SER_DTR;
1236 if (clear & TIOCM_RTS)
1237 info->MCR &= ~SER_RTS;
1238 if (clear & TIOCM_DTR)
1239 info->MCR &= ~SER_DTR;
1240 rtsdtr_ctrl(info->MCR);
1241 local_irq_restore(flags);
1242 return 0;
1243}
1244
1245/*
1246 * rs_break() --- routine which turns the break handling on or off
1247 */
1248static int rs_break(struct tty_struct *tty, int break_state)
1249{
1250 struct async_struct * info = tty->driver_data;
1251 unsigned long flags;
1252
1253 if (serial_paranoia_check(info, tty->name, "rs_break"))
1254 return -EINVAL;
1255
1256 local_irq_save(flags);
1257 if (break_state == -1)
1258 custom.adkcon = AC_SETCLR | AC_UARTBRK;
1259 else
1260 custom.adkcon = AC_UARTBRK;
1261 mb();
1262 local_irq_restore(flags);
1263 return 0;
1264}
1265
1266
1267static int rs_ioctl(struct tty_struct *tty, struct file * file,
1268 unsigned int cmd, unsigned long arg)
1269{
1270 struct async_struct * info = tty->driver_data;
1271 struct async_icount cprev, cnow; /* kernel counter temps */
1272 struct serial_icounter_struct icount;
1273 void __user *argp = (void __user *)arg;
1274 unsigned long flags;
1275
1276 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
1277 return -ENODEV;
1278
1279 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1280 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) &&
1281 (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
1282 if (tty->flags & (1 << TTY_IO_ERROR))
1283 return -EIO;
1284 }
1285
1286 switch (cmd) {
1287 case TIOCGSERIAL:
1288 return get_serial_info(info, argp);
1289 case TIOCSSERIAL:
1290 return set_serial_info(info, argp);
1291 case TIOCSERCONFIG:
1292 return 0;
1293
1294 case TIOCSERGETLSR: /* Get line status register */
1295 return get_lsr_info(info, argp);
1296
1297 case TIOCSERGSTRUCT:
1298 if (copy_to_user(argp,
1299 info, sizeof(struct async_struct)))
1300 return -EFAULT;
1301 return 0;
1302
1303 /*
1304 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1305 * - mask passed in arg for lines of interest
1306 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1307 * Caller should use TIOCGICOUNT to see which one it was
1308 */
1309 case TIOCMIWAIT:
1310 local_irq_save(flags);
1311 /* note the counters on entry */
1312 cprev = info->state->icount;
1313 local_irq_restore(flags);
1314 while (1) {
1315 interruptible_sleep_on(&info->delta_msr_wait);
1316 /* see if a signal did it */
1317 if (signal_pending(current))
1318 return -ERESTARTSYS;
1319 local_irq_save(flags);
1320 cnow = info->state->icount; /* atomic copy */
1321 local_irq_restore(flags);
1322 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1323 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1324 return -EIO; /* no change => error */
1325 if ( ((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1326 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1327 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1328 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
1329 return 0;
1330 }
1331 cprev = cnow;
1332 }
1333 /* NOTREACHED */
1334
1335 /*
1336 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1337 * Return: write counters to the user passed counter struct
1338 * NB: both 1->0 and 0->1 transitions are counted except for
1339 * RI where only 0->1 is counted.
1340 */
1341 case TIOCGICOUNT:
1342 local_irq_save(flags);
1343 cnow = info->state->icount;
1344 local_irq_restore(flags);
1345 icount.cts = cnow.cts;
1346 icount.dsr = cnow.dsr;
1347 icount.rng = cnow.rng;
1348 icount.dcd = cnow.dcd;
1349 icount.rx = cnow.rx;
1350 icount.tx = cnow.tx;
1351 icount.frame = cnow.frame;
1352 icount.overrun = cnow.overrun;
1353 icount.parity = cnow.parity;
1354 icount.brk = cnow.brk;
1355 icount.buf_overrun = cnow.buf_overrun;
1356
1357 if (copy_to_user(argp, &icount, sizeof(icount)))
1358 return -EFAULT;
1359 return 0;
1360 case TIOCSERGWILD:
1361 case TIOCSERSWILD:
1362 /* "setserial -W" is called in Debian boot */
1363 printk ("TIOCSER?WILD ioctl obsolete, ignored.\n");
1364 return 0;
1365
1366 default:
1367 return -ENOIOCTLCMD;
1368 }
1369 return 0;
1370}
1371
1372static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
1373{
1374 struct async_struct *info = tty->driver_data;
1375 unsigned long flags;
1376 unsigned int cflag = tty->termios->c_cflag;
1377
1378 change_speed(info, old_termios);
1379
1380 /* Handle transition to B0 status */
1381 if ((old_termios->c_cflag & CBAUD) &&
1382 !(cflag & CBAUD)) {
1383 info->MCR &= ~(SER_DTR|SER_RTS);
1384 local_irq_save(flags);
1385 rtsdtr_ctrl(info->MCR);
1386 local_irq_restore(flags);
1387 }
1388
1389 /* Handle transition away from B0 status */
1390 if (!(old_termios->c_cflag & CBAUD) &&
1391 (cflag & CBAUD)) {
1392 info->MCR |= SER_DTR;
1393 if (!(tty->termios->c_cflag & CRTSCTS) ||
1394 !test_bit(TTY_THROTTLED, &tty->flags)) {
1395 info->MCR |= SER_RTS;
1396 }
1397 local_irq_save(flags);
1398 rtsdtr_ctrl(info->MCR);
1399 local_irq_restore(flags);
1400 }
1401
1402 /* Handle turning off CRTSCTS */
1403 if ((old_termios->c_cflag & CRTSCTS) &&
1404 !(tty->termios->c_cflag & CRTSCTS)) {
1405 tty->hw_stopped = 0;
1406 rs_start(tty);
1407 }
1408
1409#if 0
1410 /*
1411 * No need to wake up processes in open wait, since they
1412 * sample the CLOCAL flag once, and don't recheck it.
1413 * XXX It's not clear whether the current behavior is correct
1414 * or not. Hence, this may change.....
1415 */
1416 if (!(old_termios->c_cflag & CLOCAL) &&
1417 (tty->termios->c_cflag & CLOCAL))
1418 wake_up_interruptible(&info->open_wait);
1419#endif
1420}
1421
1422/*
1423 * ------------------------------------------------------------
1424 * rs_close()
1425 *
1426 * This routine is called when the serial port gets closed. First, we
1427 * wait for the last remaining data to be sent. Then, we unlink its
1428 * async structure from the interrupt chain if necessary, and we free
1429 * that IRQ if nothing is left in the chain.
1430 * ------------------------------------------------------------
1431 */
1432static void rs_close(struct tty_struct *tty, struct file * filp)
1433{
1434 struct async_struct * info = tty->driver_data;
1435 struct serial_state *state;
1436 unsigned long flags;
1437
1438 if (!info || serial_paranoia_check(info, tty->name, "rs_close"))
1439 return;
1440
1441 state = info->state;
1442
1443 local_irq_save(flags);
1444
1445 if (tty_hung_up_p(filp)) {
1446 DBG_CNT("before DEC-hung");
1447 local_irq_restore(flags);
1448 return;
1449 }
1450
1451#ifdef SERIAL_DEBUG_OPEN
1452 printk("rs_close ttys%d, count = %d\n", info->line, state->count);
1453#endif
1454 if ((tty->count == 1) && (state->count != 1)) {
1455 /*
1456 * Uh, oh. tty->count is 1, which means that the tty
1457 * structure will be freed. state->count should always
1458 * be one in these conditions. If it's greater than
1459 * one, we've got real problems, since it means the
1460 * serial port won't be shutdown.
1461 */
1462 printk("rs_close: bad serial port count; tty->count is 1, "
1463 "state->count is %d\n", state->count);
1464 state->count = 1;
1465 }
1466 if (--state->count < 0) {
1467 printk("rs_close: bad serial port count for ttys%d: %d\n",
1468 info->line, state->count);
1469 state->count = 0;
1470 }
1471 if (state->count) {
1472 DBG_CNT("before DEC-2");
1473 local_irq_restore(flags);
1474 return;
1475 }
1476 info->flags |= ASYNC_CLOSING;
1477 /*
1478 * Now we wait for the transmit buffer to clear; and we notify
1479 * the line discipline to only process XON/XOFF characters.
1480 */
1481 tty->closing = 1;
1482 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1483 tty_wait_until_sent(tty, info->closing_wait);
1484 /*
1485 * At this point we stop accepting input. To do this, we
1486 * disable the receive line status interrupts, and tell the
1487 * interrupt driver to stop checking the data ready bit in the
1488 * line status register.
1489 */
1490 info->read_status_mask &= ~UART_LSR_DR;
1491 if (info->flags & ASYNC_INITIALIZED) {
1492 /* disable receive interrupts */
1493 custom.intena = IF_RBF;
1494 mb();
1495 /* clear any pending receive interrupt */
1496 custom.intreq = IF_RBF;
1497 mb();
1498
1499 /*
1500 * Before we drop DTR, make sure the UART transmitter
1501 * has completely drained; this is especially
1502 * important if there is a transmit FIFO!
1503 */
1504 rs_wait_until_sent(tty, info->timeout);
1505 }
1506 shutdown(info);
1507 rs_flush_buffer(tty);
1508
1509 tty_ldisc_flush(tty);
1510 tty->closing = 0;
1511 info->event = 0;
1512 info->tty = NULL;
1513 if (info->blocked_open) {
1514 if (info->close_delay) {
1515 msleep_interruptible(jiffies_to_msecs(info->close_delay));
1516 }
1517 wake_up_interruptible(&info->open_wait);
1518 }
1519 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
1520 wake_up_interruptible(&info->close_wait);
1521 local_irq_restore(flags);
1522}
1523
1524/*
1525 * rs_wait_until_sent() --- wait until the transmitter is empty
1526 */
1527static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
1528{
1529 struct async_struct * info = tty->driver_data;
1530 unsigned long orig_jiffies, char_time;
1531 int tty_was_locked = tty_locked();
1532 int lsr;
1533
1534 if (serial_paranoia_check(info, tty->name, "rs_wait_until_sent"))
1535 return;
1536
1537 if (info->xmit_fifo_size == 0)
1538 return; /* Just in case.... */
1539
1540 orig_jiffies = jiffies;
1541
1542 /*
1543 * tty_wait_until_sent is called from lots of places,
1544 * with or without the BTM.
1545 */
1546 if (!tty_was_locked)
1547 tty_lock();
1548 /*
1549 * Set the check interval to be 1/5 of the estimated time to
1550 * send a single character, and make it at least 1. The check
1551 * interval should also be less than the timeout.
1552 *
1553 * Note: we have to use pretty tight timings here to satisfy
1554 * the NIST-PCTS.
1555 */
1556 char_time = (info->timeout - HZ/50) / info->xmit_fifo_size;
1557 char_time = char_time / 5;
1558 if (char_time == 0)
1559 char_time = 1;
1560 if (timeout)
1561 char_time = min_t(unsigned long, char_time, timeout);
1562 /*
1563 * If the transmitter hasn't cleared in twice the approximate
1564 * amount of time to send the entire FIFO, it probably won't
1565 * ever clear. This assumes the UART isn't doing flow
1566 * control, which is currently the case. Hence, if it ever
1567 * takes longer than info->timeout, this is probably due to a
1568 * UART bug of some kind. So, we clamp the timeout parameter at
1569 * 2*info->timeout.
1570 */
1571 if (!timeout || timeout > 2*info->timeout)
1572 timeout = 2*info->timeout;
1573#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1574 printk("In rs_wait_until_sent(%d) check=%lu...", timeout, char_time);
1575 printk("jiff=%lu...", jiffies);
1576#endif
1577 while(!((lsr = custom.serdatr) & SDR_TSRE)) {
1578#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1579 printk("serdatr = %d (jiff=%lu)...", lsr, jiffies);
1580#endif
1581 msleep_interruptible(jiffies_to_msecs(char_time));
1582 if (signal_pending(current))
1583 break;
1584 if (timeout && time_after(jiffies, orig_jiffies + timeout))
1585 break;
1586 }
1587 __set_current_state(TASK_RUNNING);
1588 if (!tty_was_locked)
1589 tty_unlock();
1590#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1591 printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
1592#endif
1593}
1594
1595/*
1596 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1597 */
1598static void rs_hangup(struct tty_struct *tty)
1599{
1600 struct async_struct * info = tty->driver_data;
1601 struct serial_state *state = info->state;
1602
1603 if (serial_paranoia_check(info, tty->name, "rs_hangup"))
1604 return;
1605
1606 state = info->state;
1607
1608 rs_flush_buffer(tty);
1609 shutdown(info);
1610 info->event = 0;
1611 state->count = 0;
1612 info->flags &= ~ASYNC_NORMAL_ACTIVE;
1613 info->tty = NULL;
1614 wake_up_interruptible(&info->open_wait);
1615}
1616
1617/*
1618 * ------------------------------------------------------------
1619 * rs_open() and friends
1620 * ------------------------------------------------------------
1621 */
1622static int block_til_ready(struct tty_struct *tty, struct file * filp,
1623 struct async_struct *info)
1624{
1625#ifdef DECLARE_WAITQUEUE
1626 DECLARE_WAITQUEUE(wait, current);
1627#else
1628 struct wait_queue wait = { current, NULL };
1629#endif
1630 struct serial_state *state = info->state;
1631 int retval;
1632 int do_clocal = 0, extra_count = 0;
1633 unsigned long flags;
1634
1635 /*
1636 * If the device is in the middle of being closed, then block
1637 * until it's done, and then try again.
1638 */
1639 if (tty_hung_up_p(filp) ||
1640 (info->flags & ASYNC_CLOSING)) {
1641 if (info->flags & ASYNC_CLOSING)
1642 interruptible_sleep_on(&info->close_wait);
1643#ifdef SERIAL_DO_RESTART
1644 return ((info->flags & ASYNC_HUP_NOTIFY) ?
1645 -EAGAIN : -ERESTARTSYS);
1646#else
1647 return -EAGAIN;
1648#endif
1649 }
1650
1651 /*
1652 * If non-blocking mode is set, or the port is not enabled,
1653 * then make the check up front and then exit.
1654 */
1655 if ((filp->f_flags & O_NONBLOCK) ||
1656 (tty->flags & (1 << TTY_IO_ERROR))) {
1657 info->flags |= ASYNC_NORMAL_ACTIVE;
1658 return 0;
1659 }
1660
1661 if (tty->termios->c_cflag & CLOCAL)
1662 do_clocal = 1;
1663
1664 /*
1665 * Block waiting for the carrier detect and the line to become
1666 * free (i.e., not in use by the callout). While we are in
1667 * this loop, state->count is dropped by one, so that
1668 * rs_close() knows when to free things. We restore it upon
1669 * exit, either normal or abnormal.
1670 */
1671 retval = 0;
1672 add_wait_queue(&info->open_wait, &wait);
1673#ifdef SERIAL_DEBUG_OPEN
1674 printk("block_til_ready before block: ttys%d, count = %d\n",
1675 state->line, state->count);
1676#endif
1677 local_irq_save(flags);
1678 if (!tty_hung_up_p(filp)) {
1679 extra_count = 1;
1680 state->count--;
1681 }
1682 local_irq_restore(flags);
1683 info->blocked_open++;
1684 while (1) {
1685 local_irq_save(flags);
1686 if (tty->termios->c_cflag & CBAUD)
1687 rtsdtr_ctrl(SER_DTR|SER_RTS);
1688 local_irq_restore(flags);
1689 set_current_state(TASK_INTERRUPTIBLE);
1690 if (tty_hung_up_p(filp) ||
1691 !(info->flags & ASYNC_INITIALIZED)) {
1692#ifdef SERIAL_DO_RESTART
1693 if (info->flags & ASYNC_HUP_NOTIFY)
1694 retval = -EAGAIN;
1695 else
1696 retval = -ERESTARTSYS;
1697#else
1698 retval = -EAGAIN;
1699#endif
1700 break;
1701 }
1702 if (!(info->flags & ASYNC_CLOSING) &&
1703 (do_clocal || (!(ciab.pra & SER_DCD)) ))
1704 break;
1705 if (signal_pending(current)) {
1706 retval = -ERESTARTSYS;
1707 break;
1708 }
1709#ifdef SERIAL_DEBUG_OPEN
1710 printk("block_til_ready blocking: ttys%d, count = %d\n",
1711 info->line, state->count);
1712#endif
1713 tty_unlock();
1714 schedule();
1715 tty_lock();
1716 }
1717 __set_current_state(TASK_RUNNING);
1718 remove_wait_queue(&info->open_wait, &wait);
1719 if (extra_count)
1720 state->count++;
1721 info->blocked_open--;
1722#ifdef SERIAL_DEBUG_OPEN
1723 printk("block_til_ready after blocking: ttys%d, count = %d\n",
1724 info->line, state->count);
1725#endif
1726 if (retval)
1727 return retval;
1728 info->flags |= ASYNC_NORMAL_ACTIVE;
1729 return 0;
1730}
1731
1732static int get_async_struct(int line, struct async_struct **ret_info)
1733{
1734 struct async_struct *info;
1735 struct serial_state *sstate;
1736
1737 sstate = rs_table + line;
1738 sstate->count++;
1739 if (sstate->info) {
1740 *ret_info = sstate->info;
1741 return 0;
1742 }
1743 info = kzalloc(sizeof(struct async_struct), GFP_KERNEL);
1744 if (!info) {
1745 sstate->count--;
1746 return -ENOMEM;
1747 }
1748#ifdef DECLARE_WAITQUEUE
1749 init_waitqueue_head(&info->open_wait);
1750 init_waitqueue_head(&info->close_wait);
1751 init_waitqueue_head(&info->delta_msr_wait);
1752#endif
1753 info->magic = SERIAL_MAGIC;
1754 info->port = sstate->port;
1755 info->flags = sstate->flags;
1756 info->xmit_fifo_size = sstate->xmit_fifo_size;
1757 info->line = line;
1758 tasklet_init(&info->tlet, do_softint, (unsigned long)info);
1759 info->state = sstate;
1760 if (sstate->info) {
1761 kfree(info);
1762 *ret_info = sstate->info;
1763 return 0;
1764 }
1765 *ret_info = sstate->info = info;
1766 return 0;
1767}
1768
1769/*
1770 * This routine is called whenever a serial port is opened. It
1771 * enables interrupts for a serial port, linking in its async structure into
1772 * the IRQ chain. It also performs the serial-specific
1773 * initialization for the tty structure.
1774 */
1775static int rs_open(struct tty_struct *tty, struct file * filp)
1776{
1777 struct async_struct *info;
1778 int retval, line;
1779
1780 line = tty->index;
1781 if ((line < 0) || (line >= NR_PORTS)) {
1782 return -ENODEV;
1783 }
1784 retval = get_async_struct(line, &info);
1785 if (retval) {
1786 return retval;
1787 }
1788 tty->driver_data = info;
1789 info->tty = tty;
1790 if (serial_paranoia_check(info, tty->name, "rs_open"))
1791 return -ENODEV;
1792
1793#ifdef SERIAL_DEBUG_OPEN
1794 printk("rs_open %s, count = %d\n", tty->name, info->state->count);
1795#endif
1796 info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
1797
1798 /*
1799 * If the port is the middle of closing, bail out now
1800 */
1801 if (tty_hung_up_p(filp) ||
1802 (info->flags & ASYNC_CLOSING)) {
1803 if (info->flags & ASYNC_CLOSING)
1804 interruptible_sleep_on(&info->close_wait);
1805#ifdef SERIAL_DO_RESTART
1806 return ((info->flags & ASYNC_HUP_NOTIFY) ?
1807 -EAGAIN : -ERESTARTSYS);
1808#else
1809 return -EAGAIN;
1810#endif
1811 }
1812
1813 /*
1814 * Start up serial port
1815 */
1816 retval = startup(info);
1817 if (retval) {
1818 return retval;
1819 }
1820
1821 retval = block_til_ready(tty, filp, info);
1822 if (retval) {
1823#ifdef SERIAL_DEBUG_OPEN
1824 printk("rs_open returning after block_til_ready with %d\n",
1825 retval);
1826#endif
1827 return retval;
1828 }
1829
1830#ifdef SERIAL_DEBUG_OPEN
1831 printk("rs_open %s successful...", tty->name);
1832#endif
1833 return 0;
1834}
1835
1836/*
1837 * /proc fs routines....
1838 */
1839
1840static inline void line_info(struct seq_file *m, struct serial_state *state)
1841{
1842 struct async_struct *info = state->info, scr_info;
1843 char stat_buf[30], control, status;
1844 unsigned long flags;
1845
1846 seq_printf(m, "%d: uart:amiga_builtin",state->line);
1847
1848 /*
1849 * Figure out the current RS-232 lines
1850 */
1851 if (!info) {
1852 info = &scr_info; /* This is just for serial_{in,out} */
1853
1854 info->magic = SERIAL_MAGIC;
1855 info->flags = state->flags;
1856 info->quot = 0;
1857 info->tty = NULL;
1858 }
1859 local_irq_save(flags);
1860 status = ciab.pra;
1861 control = info ? info->MCR : status;
1862 local_irq_restore(flags);
1863
1864 stat_buf[0] = 0;
1865 stat_buf[1] = 0;
1866 if(!(control & SER_RTS))
1867 strcat(stat_buf, "|RTS");
1868 if(!(status & SER_CTS))
1869 strcat(stat_buf, "|CTS");
1870 if(!(control & SER_DTR))
1871 strcat(stat_buf, "|DTR");
1872 if(!(status & SER_DSR))
1873 strcat(stat_buf, "|DSR");
1874 if(!(status & SER_DCD))
1875 strcat(stat_buf, "|CD");
1876
1877 if (info->quot) {
1878 seq_printf(m, " baud:%d", state->baud_base / info->quot);
1879 }
1880
1881 seq_printf(m, " tx:%d rx:%d", state->icount.tx, state->icount.rx);
1882
1883 if (state->icount.frame)
1884 seq_printf(m, " fe:%d", state->icount.frame);
1885
1886 if (state->icount.parity)
1887 seq_printf(m, " pe:%d", state->icount.parity);
1888
1889 if (state->icount.brk)
1890 seq_printf(m, " brk:%d", state->icount.brk);
1891
1892 if (state->icount.overrun)
1893 seq_printf(m, " oe:%d", state->icount.overrun);
1894
1895 /*
1896 * Last thing is the RS-232 status lines
1897 */
1898 seq_printf(m, " %s\n", stat_buf+1);
1899}
1900
1901static int rs_proc_show(struct seq_file *m, void *v)
1902{
1903 seq_printf(m, "serinfo:1.0 driver:%s\n", serial_version);
1904 line_info(m, &rs_table[0]);
1905 return 0;
1906}
1907
1908static int rs_proc_open(struct inode *inode, struct file *file)
1909{
1910 return single_open(file, rs_proc_show, NULL);
1911}
1912
1913static const struct file_operations rs_proc_fops = {
1914 .owner = THIS_MODULE,
1915 .open = rs_proc_open,
1916 .read = seq_read,
1917 .llseek = seq_lseek,
1918 .release = single_release,
1919};
1920
1921/*
1922 * ---------------------------------------------------------------------
1923 * rs_init() and friends
1924 *
1925 * rs_init() is called at boot-time to initialize the serial driver.
1926 * ---------------------------------------------------------------------
1927 */
1928
1929/*
1930 * This routine prints out the appropriate serial driver version
1931 * number, and identifies which options were configured into this
1932 * driver.
1933 */
1934static void show_serial_version(void)
1935{
1936 printk(KERN_INFO "%s version %s\n", serial_name, serial_version);
1937}
1938
1939
1940static const struct tty_operations serial_ops = {
1941 .open = rs_open,
1942 .close = rs_close,
1943 .write = rs_write,
1944 .put_char = rs_put_char,
1945 .flush_chars = rs_flush_chars,
1946 .write_room = rs_write_room,
1947 .chars_in_buffer = rs_chars_in_buffer,
1948 .flush_buffer = rs_flush_buffer,
1949 .ioctl = rs_ioctl,
1950 .throttle = rs_throttle,
1951 .unthrottle = rs_unthrottle,
1952 .set_termios = rs_set_termios,
1953 .stop = rs_stop,
1954 .start = rs_start,
1955 .hangup = rs_hangup,
1956 .break_ctl = rs_break,
1957 .send_xchar = rs_send_xchar,
1958 .wait_until_sent = rs_wait_until_sent,
1959 .tiocmget = rs_tiocmget,
1960 .tiocmset = rs_tiocmset,
1961 .proc_fops = &rs_proc_fops,
1962};
1963
1964/*
1965 * The serial driver boot-time initialization code!
1966 */
1967static int __init amiga_serial_probe(struct platform_device *pdev)
1968{
1969 unsigned long flags;
1970 struct serial_state * state;
1971 int error;
1972
1973 serial_driver = alloc_tty_driver(1);
1974 if (!serial_driver)
1975 return -ENOMEM;
1976
1977 IRQ_ports = NULL;
1978
1979 show_serial_version();
1980
1981 /* Initialize the tty_driver structure */
1982
1983 serial_driver->owner = THIS_MODULE;
1984 serial_driver->driver_name = "amiserial";
1985 serial_driver->name = "ttyS";
1986 serial_driver->major = TTY_MAJOR;
1987 serial_driver->minor_start = 64;
1988 serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
1989 serial_driver->subtype = SERIAL_TYPE_NORMAL;
1990 serial_driver->init_termios = tty_std_termios;
1991 serial_driver->init_termios.c_cflag =
1992 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1993 serial_driver->flags = TTY_DRIVER_REAL_RAW;
1994 tty_set_operations(serial_driver, &serial_ops);
1995
1996 error = tty_register_driver(serial_driver);
1997 if (error)
1998 goto fail_put_tty_driver;
1999
2000 state = rs_table;
2001 state->magic = SSTATE_MAGIC;
2002 state->port = (int)&custom.serdatr; /* Just to give it a value */
2003 state->line = 0;
2004 state->custom_divisor = 0;
2005 state->close_delay = 5*HZ/10;
2006 state->closing_wait = 30*HZ;
2007 state->icount.cts = state->icount.dsr =
2008 state->icount.rng = state->icount.dcd = 0;
2009 state->icount.rx = state->icount.tx = 0;
2010 state->icount.frame = state->icount.parity = 0;
2011 state->icount.overrun = state->icount.brk = 0;
2012
2013 printk(KERN_INFO "ttyS%d is the amiga builtin serial port\n",
2014 state->line);
2015
2016 /* Hardware set up */
2017
2018 state->baud_base = amiga_colorclock;
2019 state->xmit_fifo_size = 1;
2020
2021 /* set ISRs, and then disable the rx interrupts */
2022 error = request_irq(IRQ_AMIGA_TBE, ser_tx_int, 0, "serial TX", state);
2023 if (error)
2024 goto fail_unregister;
2025
2026 error = request_irq(IRQ_AMIGA_RBF, ser_rx_int, IRQF_DISABLED,
2027 "serial RX", state);
2028 if (error)
2029 goto fail_free_irq;
2030
2031 local_irq_save(flags);
2032
2033 /* turn off Rx and Tx interrupts */
2034 custom.intena = IF_RBF | IF_TBE;
2035 mb();
2036
2037 /* clear any pending interrupt */
2038 custom.intreq = IF_RBF | IF_TBE;
2039 mb();
2040
2041 local_irq_restore(flags);
2042
2043 /*
2044 * set the appropriate directions for the modem control flags,
2045 * and clear RTS and DTR
2046 */
2047 ciab.ddra |= (SER_DTR | SER_RTS); /* outputs */
2048 ciab.ddra &= ~(SER_DCD | SER_CTS | SER_DSR); /* inputs */
2049
2050 platform_set_drvdata(pdev, state);
2051
2052 return 0;
2053
2054fail_free_irq:
2055 free_irq(IRQ_AMIGA_TBE, state);
2056fail_unregister:
2057 tty_unregister_driver(serial_driver);
2058fail_put_tty_driver:
2059 put_tty_driver(serial_driver);
2060 return error;
2061}
2062
2063static int __exit amiga_serial_remove(struct platform_device *pdev)
2064{
2065 int error;
2066 struct serial_state *state = platform_get_drvdata(pdev);
2067 struct async_struct *info = state->info;
2068
2069 /* printk("Unloading %s: version %s\n", serial_name, serial_version); */
2070 tasklet_kill(&info->tlet);
2071 if ((error = tty_unregister_driver(serial_driver)))
2072 printk("SERIAL: failed to unregister serial driver (%d)\n",
2073 error);
2074 put_tty_driver(serial_driver);
2075
2076 rs_table[0].info = NULL;
2077 kfree(info);
2078
2079 free_irq(IRQ_AMIGA_TBE, rs_table);
2080 free_irq(IRQ_AMIGA_RBF, rs_table);
2081
2082 platform_set_drvdata(pdev, NULL);
2083
2084 return error;
2085}
2086
2087static struct platform_driver amiga_serial_driver = {
2088 .remove = __exit_p(amiga_serial_remove),
2089 .driver = {
2090 .name = "amiga-serial",
2091 .owner = THIS_MODULE,
2092 },
2093};
2094
2095static int __init amiga_serial_init(void)
2096{
2097 return platform_driver_probe(&amiga_serial_driver, amiga_serial_probe);
2098}
2099
2100module_init(amiga_serial_init);
2101
2102static void __exit amiga_serial_exit(void)
2103{
2104 platform_driver_unregister(&amiga_serial_driver);
2105}
2106
2107module_exit(amiga_serial_exit);
2108
2109
2110#if defined(CONFIG_SERIAL_CONSOLE) && !defined(MODULE)
2111
2112/*
2113 * ------------------------------------------------------------
2114 * Serial console driver
2115 * ------------------------------------------------------------
2116 */
2117
2118static void amiga_serial_putc(char c)
2119{
2120 custom.serdat = (unsigned char)c | 0x100;
2121 while (!(custom.serdatr & 0x2000))
2122 barrier();
2123}
2124
2125/*
2126 * Print a string to the serial port trying not to disturb
2127 * any possible real use of the port...
2128 *
2129 * The console must be locked when we get here.
2130 */
2131static void serial_console_write(struct console *co, const char *s,
2132 unsigned count)
2133{
2134 unsigned short intena = custom.intenar;
2135
2136 custom.intena = IF_TBE;
2137
2138 while (count--) {
2139 if (*s == '\n')
2140 amiga_serial_putc('\r');
2141 amiga_serial_putc(*s++);
2142 }
2143
2144 custom.intena = IF_SETCLR | (intena & IF_TBE);
2145}
2146
2147static struct tty_driver *serial_console_device(struct console *c, int *index)
2148{
2149 *index = 0;
2150 return serial_driver;
2151}
2152
2153static struct console sercons = {
2154 .name = "ttyS",
2155 .write = serial_console_write,
2156 .device = serial_console_device,
2157 .flags = CON_PRINTBUFFER,
2158 .index = -1,
2159};
2160
2161/*
2162 * Register console.
2163 */
2164static int __init amiserial_console_init(void)
2165{
2166 register_console(&sercons);
2167 return 0;
2168}
2169console_initcall(amiserial_console_init);
2170
2171#endif /* CONFIG_SERIAL_CONSOLE && !MODULE */
2172
2173MODULE_LICENSE("GPL");
2174MODULE_ALIAS("platform:amiga-serial");
diff --git a/drivers/char/apm-emulation.c b/drivers/char/apm-emulation.c
index 033e1505fca9..548708c4b2b8 100644
--- a/drivers/char/apm-emulation.c
+++ b/drivers/char/apm-emulation.c
@@ -7,13 +7,13 @@
7 * Intel Corporation, Microsoft Corporation. Advanced Power Management 7 * Intel Corporation, Microsoft Corporation. Advanced Power Management
8 * (APM) BIOS Interface Specification, Revision 1.2, February 1996. 8 * (APM) BIOS Interface Specification, Revision 1.2, February 1996.
9 * 9 *
10 * [This document is available from Microsoft at: 10 * This document is available from Microsoft at:
11 * http://www.microsoft.com/hwdev/busbios/amp_12.htm] 11 * http://www.microsoft.com/whdc/archive/amp_12.mspx
12 */ 12 */
13#include <linux/module.h> 13#include <linux/module.h>
14#include <linux/poll.h> 14#include <linux/poll.h>
15#include <linux/slab.h> 15#include <linux/slab.h>
16#include <linux/smp_lock.h> 16#include <linux/mutex.h>
17#include <linux/proc_fs.h> 17#include <linux/proc_fs.h>
18#include <linux/seq_file.h> 18#include <linux/seq_file.h>
19#include <linux/miscdevice.h> 19#include <linux/miscdevice.h>
@@ -274,7 +274,6 @@ apm_ioctl(struct file *filp, u_int cmd, u_long arg)
274 if (!as->suser || !as->writer) 274 if (!as->suser || !as->writer)
275 return -EPERM; 275 return -EPERM;
276 276
277 lock_kernel();
278 switch (cmd) { 277 switch (cmd) {
279 case APM_IOC_SUSPEND: 278 case APM_IOC_SUSPEND:
280 mutex_lock(&state_lock); 279 mutex_lock(&state_lock);
@@ -335,7 +334,6 @@ apm_ioctl(struct file *filp, u_int cmd, u_long arg)
335 mutex_unlock(&state_lock); 334 mutex_unlock(&state_lock);
336 break; 335 break;
337 } 336 }
338 unlock_kernel();
339 337
340 return err; 338 return err;
341} 339}
@@ -370,7 +368,6 @@ static int apm_open(struct inode * inode, struct file * filp)
370{ 368{
371 struct apm_user *as; 369 struct apm_user *as;
372 370
373 lock_kernel();
374 as = kzalloc(sizeof(*as), GFP_KERNEL); 371 as = kzalloc(sizeof(*as), GFP_KERNEL);
375 if (as) { 372 if (as) {
376 /* 373 /*
@@ -390,7 +387,6 @@ static int apm_open(struct inode * inode, struct file * filp)
390 387
391 filp->private_data = as; 388 filp->private_data = as;
392 } 389 }
393 unlock_kernel();
394 390
395 return as ? 0 : -ENOMEM; 391 return as ? 0 : -ENOMEM;
396} 392}
@@ -402,6 +398,7 @@ static const struct file_operations apm_bios_fops = {
402 .unlocked_ioctl = apm_ioctl, 398 .unlocked_ioctl = apm_ioctl,
403 .open = apm_open, 399 .open = apm_open,
404 .release = apm_release, 400 .release = apm_release,
401 .llseek = noop_llseek,
405}; 402};
406 403
407static struct miscdevice apm_device = { 404static struct miscdevice apm_device = {
diff --git a/drivers/char/applicom.c b/drivers/char/applicom.c
index f4ae0e0fb631..25373df1dcf8 100644
--- a/drivers/char/applicom.c
+++ b/drivers/char/applicom.c
@@ -26,7 +26,7 @@
26#include <linux/sched.h> 26#include <linux/sched.h>
27#include <linux/slab.h> 27#include <linux/slab.h>
28#include <linux/errno.h> 28#include <linux/errno.h>
29#include <linux/smp_lock.h> 29#include <linux/mutex.h>
30#include <linux/miscdevice.h> 30#include <linux/miscdevice.h>
31#include <linux/pci.h> 31#include <linux/pci.h>
32#include <linux/wait.h> 32#include <linux/wait.h>
@@ -60,6 +60,7 @@
60#define PCI_DEVICE_ID_APPLICOM_PCI2000PFB 0x0003 60#define PCI_DEVICE_ID_APPLICOM_PCI2000PFB 0x0003
61#endif 61#endif
62 62
63static DEFINE_MUTEX(ac_mutex);
63static char *applicom_pci_devnames[] = { 64static char *applicom_pci_devnames[] = {
64 "PCI board", 65 "PCI board",
65 "PCI2000IBS / PCI2000CAN", 66 "PCI2000IBS / PCI2000CAN",
@@ -565,6 +566,7 @@ static ssize_t ac_read (struct file *filp, char __user *buf, size_t count, loff_
565 struct mailbox mailbox; 566 struct mailbox mailbox;
566 567
567 /* Got a packet for us */ 568 /* Got a packet for us */
569 memset(&st_loc, 0, sizeof(st_loc));
568 ret = do_ac_read(i, buf, &st_loc, &mailbox); 570 ret = do_ac_read(i, buf, &st_loc, &mailbox);
569 spin_unlock_irqrestore(&apbs[i].mutex, flags); 571 spin_unlock_irqrestore(&apbs[i].mutex, flags);
570 set_current_state(TASK_RUNNING); 572 set_current_state(TASK_RUNNING);
@@ -707,7 +709,7 @@ static long ac_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
707 if (IS_ERR(adgl)) 709 if (IS_ERR(adgl))
708 return PTR_ERR(adgl); 710 return PTR_ERR(adgl);
709 711
710 lock_kernel(); 712 mutex_lock(&ac_mutex);
711 IndexCard = adgl->num_card-1; 713 IndexCard = adgl->num_card-1;
712 714
713 if(cmd != 6 && ((IndexCard >= MAX_BOARD) || !apbs[IndexCard].RamIO)) { 715 if(cmd != 6 && ((IndexCard >= MAX_BOARD) || !apbs[IndexCard].RamIO)) {
@@ -717,7 +719,7 @@ static long ac_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
717 warncount--; 719 warncount--;
718 } 720 }
719 kfree(adgl); 721 kfree(adgl);
720 unlock_kernel(); 722 mutex_unlock(&ac_mutex);
721 return -EINVAL; 723 return -EINVAL;
722 } 724 }
723 725
@@ -835,7 +837,7 @@ static long ac_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
835 } 837 }
836 Dummy = readb(apbs[IndexCard].RamIO + VERS); 838 Dummy = readb(apbs[IndexCard].RamIO + VERS);
837 kfree(adgl); 839 kfree(adgl);
838 unlock_kernel(); 840 mutex_unlock(&ac_mutex);
839 return 0; 841 return 0;
840} 842}
841 843
diff --git a/drivers/char/bfin-otp.c b/drivers/char/bfin-otp.c
index 836d4f0a876f..44660f1c4849 100644
--- a/drivers/char/bfin-otp.c
+++ b/drivers/char/bfin-otp.c
@@ -222,6 +222,7 @@ static const struct file_operations bfin_otp_fops = {
222 .unlocked_ioctl = bfin_otp_ioctl, 222 .unlocked_ioctl = bfin_otp_ioctl,
223 .read = bfin_otp_read, 223 .read = bfin_otp_read,
224 .write = bfin_otp_write, 224 .write = bfin_otp_write,
225 .llseek = default_llseek,
225}; 226};
226 227
227static struct miscdevice bfin_otp_misc_device = { 228static struct miscdevice bfin_otp_misc_device = {
diff --git a/drivers/char/bfin_jtag_comm.c b/drivers/char/bfin_jtag_comm.c
deleted file mode 100644
index e397df3ad98e..000000000000
--- a/drivers/char/bfin_jtag_comm.c
+++ /dev/null
@@ -1,366 +0,0 @@
1/*
2 * TTY over Blackfin JTAG Communication
3 *
4 * Copyright 2008-2009 Analog Devices Inc.
5 *
6 * Enter bugs at http://blackfin.uclinux.org/
7 *
8 * Licensed under the GPL-2 or later.
9 */
10
11#define DRV_NAME "bfin-jtag-comm"
12#define DEV_NAME "ttyBFJC"
13#define pr_fmt(fmt) DRV_NAME ": " fmt
14
15#include <linux/circ_buf.h>
16#include <linux/console.h>
17#include <linux/delay.h>
18#include <linux/err.h>
19#include <linux/kernel.h>
20#include <linux/kthread.h>
21#include <linux/module.h>
22#include <linux/mutex.h>
23#include <linux/sched.h>
24#include <linux/slab.h>
25#include <linux/tty.h>
26#include <linux/tty_driver.h>
27#include <linux/tty_flip.h>
28#include <asm/atomic.h>
29
30#define pr_init(fmt, args...) ({ static const __initconst char __fmt[] = fmt; printk(__fmt, ## args); })
31
32/* See the Debug/Emulation chapter in the HRM */
33#define EMUDOF 0x00000001 /* EMUDAT_OUT full & valid */
34#define EMUDIF 0x00000002 /* EMUDAT_IN full & valid */
35#define EMUDOOVF 0x00000004 /* EMUDAT_OUT overflow */
36#define EMUDIOVF 0x00000008 /* EMUDAT_IN overflow */
37
38static inline uint32_t bfin_write_emudat(uint32_t emudat)
39{
40 __asm__ __volatile__("emudat = %0;" : : "d"(emudat));
41 return emudat;
42}
43
44static inline uint32_t bfin_read_emudat(void)
45{
46 uint32_t emudat;
47 __asm__ __volatile__("%0 = emudat;" : "=d"(emudat));
48 return emudat;
49}
50
51static inline uint32_t bfin_write_emudat_chars(char a, char b, char c, char d)
52{
53 return bfin_write_emudat((a << 0) | (b << 8) | (c << 16) | (d << 24));
54}
55
56#define CIRC_SIZE 2048 /* see comment in tty_io.c:do_tty_write() */
57#define CIRC_MASK (CIRC_SIZE - 1)
58#define circ_empty(circ) ((circ)->head == (circ)->tail)
59#define circ_free(circ) CIRC_SPACE((circ)->head, (circ)->tail, CIRC_SIZE)
60#define circ_cnt(circ) CIRC_CNT((circ)->head, (circ)->tail, CIRC_SIZE)
61#define circ_byte(circ, idx) ((circ)->buf[(idx) & CIRC_MASK])
62
63static struct tty_driver *bfin_jc_driver;
64static struct task_struct *bfin_jc_kthread;
65static struct tty_struct * volatile bfin_jc_tty;
66static unsigned long bfin_jc_count;
67static DEFINE_MUTEX(bfin_jc_tty_mutex);
68static volatile struct circ_buf bfin_jc_write_buf;
69
70static int
71bfin_jc_emudat_manager(void *arg)
72{
73 uint32_t inbound_len = 0, outbound_len = 0;
74
75 while (!kthread_should_stop()) {
76 /* no one left to give data to, so sleep */
77 if (bfin_jc_tty == NULL && circ_empty(&bfin_jc_write_buf)) {
78 pr_debug("waiting for readers\n");
79 __set_current_state(TASK_UNINTERRUPTIBLE);
80 schedule();
81 __set_current_state(TASK_RUNNING);
82 }
83
84 /* no data available, so just chill */
85 if (!(bfin_read_DBGSTAT() & EMUDIF) && circ_empty(&bfin_jc_write_buf)) {
86 pr_debug("waiting for data (in_len = %i) (circ: %i %i)\n",
87 inbound_len, bfin_jc_write_buf.tail, bfin_jc_write_buf.head);
88 if (inbound_len)
89 schedule();
90 else
91 schedule_timeout_interruptible(HZ);
92 continue;
93 }
94
95 /* if incoming data is ready, eat it */
96 if (bfin_read_DBGSTAT() & EMUDIF) {
97 struct tty_struct *tty;
98 mutex_lock(&bfin_jc_tty_mutex);
99 tty = (struct tty_struct *)bfin_jc_tty;
100 if (tty != NULL) {
101 uint32_t emudat = bfin_read_emudat();
102 if (inbound_len == 0) {
103 pr_debug("incoming length: 0x%08x\n", emudat);
104 inbound_len = emudat;
105 } else {
106 size_t num_chars = (4 <= inbound_len ? 4 : inbound_len);
107 pr_debug(" incoming data: 0x%08x (pushing %zu)\n", emudat, num_chars);
108 inbound_len -= num_chars;
109 tty_insert_flip_string(tty, (unsigned char *)&emudat, num_chars);
110 tty_flip_buffer_push(tty);
111 }
112 }
113 mutex_unlock(&bfin_jc_tty_mutex);
114 }
115
116 /* if outgoing data is ready, post it */
117 if (!(bfin_read_DBGSTAT() & EMUDOF) && !circ_empty(&bfin_jc_write_buf)) {
118 if (outbound_len == 0) {
119 outbound_len = circ_cnt(&bfin_jc_write_buf);
120 bfin_write_emudat(outbound_len);
121 pr_debug("outgoing length: 0x%08x\n", outbound_len);
122 } else {
123 struct tty_struct *tty;
124 int tail = bfin_jc_write_buf.tail;
125 size_t ate = (4 <= outbound_len ? 4 : outbound_len);
126 uint32_t emudat =
127 bfin_write_emudat_chars(
128 circ_byte(&bfin_jc_write_buf, tail + 0),
129 circ_byte(&bfin_jc_write_buf, tail + 1),
130 circ_byte(&bfin_jc_write_buf, tail + 2),
131 circ_byte(&bfin_jc_write_buf, tail + 3)
132 );
133 bfin_jc_write_buf.tail += ate;
134 outbound_len -= ate;
135 mutex_lock(&bfin_jc_tty_mutex);
136 tty = (struct tty_struct *)bfin_jc_tty;
137 if (tty)
138 tty_wakeup(tty);
139 mutex_unlock(&bfin_jc_tty_mutex);
140 pr_debug(" outgoing data: 0x%08x (pushing %zu)\n", emudat, ate);
141 }
142 }
143 }
144
145 __set_current_state(TASK_RUNNING);
146 return 0;
147}
148
149static int
150bfin_jc_open(struct tty_struct *tty, struct file *filp)
151{
152 mutex_lock(&bfin_jc_tty_mutex);
153 pr_debug("open %lu\n", bfin_jc_count);
154 ++bfin_jc_count;
155 bfin_jc_tty = tty;
156 wake_up_process(bfin_jc_kthread);
157 mutex_unlock(&bfin_jc_tty_mutex);
158 return 0;
159}
160
161static void
162bfin_jc_close(struct tty_struct *tty, struct file *filp)
163{
164 mutex_lock(&bfin_jc_tty_mutex);
165 pr_debug("close %lu\n", bfin_jc_count);
166 if (--bfin_jc_count == 0)
167 bfin_jc_tty = NULL;
168 wake_up_process(bfin_jc_kthread);
169 mutex_unlock(&bfin_jc_tty_mutex);
170}
171
172/* XXX: we dont handle the put_char() case where we must handle count = 1 */
173static int
174bfin_jc_circ_write(const unsigned char *buf, int count)
175{
176 int i;
177 count = min(count, circ_free(&bfin_jc_write_buf));
178 pr_debug("going to write chunk of %i bytes\n", count);
179 for (i = 0; i < count; ++i)
180 circ_byte(&bfin_jc_write_buf, bfin_jc_write_buf.head + i) = buf[i];
181 bfin_jc_write_buf.head += i;
182 return i;
183}
184
185#ifndef CONFIG_BFIN_JTAG_COMM_CONSOLE
186# define acquire_console_sem()
187# define release_console_sem()
188#endif
189static int
190bfin_jc_write(struct tty_struct *tty, const unsigned char *buf, int count)
191{
192 int i;
193 acquire_console_sem();
194 i = bfin_jc_circ_write(buf, count);
195 release_console_sem();
196 wake_up_process(bfin_jc_kthread);
197 return i;
198}
199
200static void
201bfin_jc_flush_chars(struct tty_struct *tty)
202{
203 wake_up_process(bfin_jc_kthread);
204}
205
206static int
207bfin_jc_write_room(struct tty_struct *tty)
208{
209 return circ_free(&bfin_jc_write_buf);
210}
211
212static int
213bfin_jc_chars_in_buffer(struct tty_struct *tty)
214{
215 return circ_cnt(&bfin_jc_write_buf);
216}
217
218static void
219bfin_jc_wait_until_sent(struct tty_struct *tty, int timeout)
220{
221 unsigned long expire = jiffies + timeout;
222 while (!circ_empty(&bfin_jc_write_buf)) {
223 if (signal_pending(current))
224 break;
225 if (time_after(jiffies, expire))
226 break;
227 }
228}
229
230static const struct tty_operations bfin_jc_ops = {
231 .open = bfin_jc_open,
232 .close = bfin_jc_close,
233 .write = bfin_jc_write,
234 /*.put_char = bfin_jc_put_char,*/
235 .flush_chars = bfin_jc_flush_chars,
236 .write_room = bfin_jc_write_room,
237 .chars_in_buffer = bfin_jc_chars_in_buffer,
238 .wait_until_sent = bfin_jc_wait_until_sent,
239};
240
241static int __init bfin_jc_init(void)
242{
243 int ret;
244
245 bfin_jc_kthread = kthread_create(bfin_jc_emudat_manager, NULL, DRV_NAME);
246 if (IS_ERR(bfin_jc_kthread))
247 return PTR_ERR(bfin_jc_kthread);
248
249 ret = -ENOMEM;
250
251 bfin_jc_write_buf.head = bfin_jc_write_buf.tail = 0;
252 bfin_jc_write_buf.buf = kmalloc(CIRC_SIZE, GFP_KERNEL);
253 if (!bfin_jc_write_buf.buf)
254 goto err;
255
256 bfin_jc_driver = alloc_tty_driver(1);
257 if (!bfin_jc_driver)
258 goto err;
259
260 bfin_jc_driver->owner = THIS_MODULE;
261 bfin_jc_driver->driver_name = DRV_NAME;
262 bfin_jc_driver->name = DEV_NAME;
263 bfin_jc_driver->type = TTY_DRIVER_TYPE_SERIAL;
264 bfin_jc_driver->subtype = SERIAL_TYPE_NORMAL;
265 bfin_jc_driver->init_termios = tty_std_termios;
266 tty_set_operations(bfin_jc_driver, &bfin_jc_ops);
267
268 ret = tty_register_driver(bfin_jc_driver);
269 if (ret)
270 goto err;
271
272 pr_init(KERN_INFO DRV_NAME ": initialized\n");
273
274 return 0;
275
276 err:
277 put_tty_driver(bfin_jc_driver);
278 kfree(bfin_jc_write_buf.buf);
279 kthread_stop(bfin_jc_kthread);
280 return ret;
281}
282module_init(bfin_jc_init);
283
284static void __exit bfin_jc_exit(void)
285{
286 kthread_stop(bfin_jc_kthread);
287 kfree(bfin_jc_write_buf.buf);
288 tty_unregister_driver(bfin_jc_driver);
289 put_tty_driver(bfin_jc_driver);
290}
291module_exit(bfin_jc_exit);
292
293#if defined(CONFIG_BFIN_JTAG_COMM_CONSOLE) || defined(CONFIG_EARLY_PRINTK)
294static void
295bfin_jc_straight_buffer_write(const char *buf, unsigned count)
296{
297 unsigned ate = 0;
298 while (bfin_read_DBGSTAT() & EMUDOF)
299 continue;
300 bfin_write_emudat(count);
301 while (ate < count) {
302 while (bfin_read_DBGSTAT() & EMUDOF)
303 continue;
304 bfin_write_emudat_chars(buf[ate], buf[ate+1], buf[ate+2], buf[ate+3]);
305 ate += 4;
306 }
307}
308#endif
309
310#ifdef CONFIG_BFIN_JTAG_COMM_CONSOLE
311static void
312bfin_jc_console_write(struct console *co, const char *buf, unsigned count)
313{
314 if (bfin_jc_kthread == NULL)
315 bfin_jc_straight_buffer_write(buf, count);
316 else
317 bfin_jc_circ_write(buf, count);
318}
319
320static struct tty_driver *
321bfin_jc_console_device(struct console *co, int *index)
322{
323 *index = co->index;
324 return bfin_jc_driver;
325}
326
327static struct console bfin_jc_console = {
328 .name = DEV_NAME,
329 .write = bfin_jc_console_write,
330 .device = bfin_jc_console_device,
331 .flags = CON_ANYTIME | CON_PRINTBUFFER,
332 .index = -1,
333};
334
335static int __init bfin_jc_console_init(void)
336{
337 register_console(&bfin_jc_console);
338 return 0;
339}
340console_initcall(bfin_jc_console_init);
341#endif
342
343#ifdef CONFIG_EARLY_PRINTK
344static void __init
345bfin_jc_early_write(struct console *co, const char *buf, unsigned int count)
346{
347 bfin_jc_straight_buffer_write(buf, count);
348}
349
350static struct __initdata console bfin_jc_early_console = {
351 .name = "early_BFJC",
352 .write = bfin_jc_early_write,
353 .flags = CON_ANYTIME | CON_PRINTBUFFER,
354 .index = -1,
355};
356
357struct console * __init
358bfin_jc_early_init(unsigned int port, unsigned int cflag)
359{
360 return &bfin_jc_early_console;
361}
362#endif
363
364MODULE_AUTHOR("Mike Frysinger <vapier@gentoo.org>");
365MODULE_DESCRIPTION("TTY over Blackfin JTAG Communication");
366MODULE_LICENSE("GPL");
diff --git a/drivers/char/briq_panel.c b/drivers/char/briq_panel.c
index d5fa113afe37..095ab90535ce 100644
--- a/drivers/char/briq_panel.c
+++ b/drivers/char/briq_panel.c
@@ -6,7 +6,6 @@
6 6
7#include <linux/module.h> 7#include <linux/module.h>
8 8
9#include <linux/smp_lock.h>
10#include <linux/types.h> 9#include <linux/types.h>
11#include <linux/errno.h> 10#include <linux/errno.h>
12#include <linux/tty.h> 11#include <linux/tty.h>
@@ -186,6 +185,7 @@ static const struct file_operations briq_panel_fops = {
186 .write = briq_panel_write, 185 .write = briq_panel_write,
187 .open = briq_panel_open, 186 .open = briq_panel_open,
188 .release = briq_panel_release, 187 .release = briq_panel_release,
188 .llseek = noop_llseek,
189}; 189};
190 190
191static struct miscdevice briq_panel_miscdev = { 191static struct miscdevice briq_panel_miscdev = {
diff --git a/drivers/char/bsr.c b/drivers/char/bsr.c
index 91917133ae0a..cf39bc08ce08 100644
--- a/drivers/char/bsr.c
+++ b/drivers/char/bsr.c
@@ -155,6 +155,7 @@ static const struct file_operations bsr_fops = {
155 .owner = THIS_MODULE, 155 .owner = THIS_MODULE,
156 .mmap = bsr_mmap, 156 .mmap = bsr_mmap,
157 .open = bsr_open, 157 .open = bsr_open,
158 .llseek = noop_llseek,
158}; 159};
159 160
160static void bsr_cleanup_devs(void) 161static void bsr_cleanup_devs(void)
@@ -294,7 +295,7 @@ static int bsr_create_devs(struct device_node *bn)
294static int __init bsr_init(void) 295static int __init bsr_init(void)
295{ 296{
296 struct device_node *np; 297 struct device_node *np;
297 dev_t bsr_dev = MKDEV(bsr_major, 0); 298 dev_t bsr_dev;
298 int ret = -ENODEV; 299 int ret = -ENODEV;
299 int result; 300 int result;
300 301
diff --git a/drivers/char/cd1865.h b/drivers/char/cd1865.h
deleted file mode 100644
index 9940966e7a1d..000000000000
--- a/drivers/char/cd1865.h
+++ /dev/null
@@ -1,263 +0,0 @@
1/*
2 * linux/drivers/char/cd1865.h -- Definitions relating to the CD1865
3 * for the Specialix IO8+ multiport serial driver.
4 *
5 * Copyright (C) 1997 Roger Wolff (R.E.Wolff@BitWizard.nl)
6 * Copyright (C) 1994-1996 Dmitry Gorodchanin (pgmdsg@ibi.com)
7 *
8 * Specialix pays for the development and support of this driver.
9 * Please DO contact io8-linux@specialix.co.uk if you require
10 * support.
11 *
12 * This driver was developped in the BitWizard linux device
13 * driver service. If you require a linux device driver for your
14 * product, please contact devices@BitWizard.nl for a quote.
15 *
16 */
17
18/*
19 * Definitions for Driving CD180/CD1864/CD1865 based eightport serial cards.
20 */
21
22
23/* Values of choice for Interrupt ACKs */
24/* These values are "obligatory" if you use the register based
25 * interrupt acknowledgements. See page 99-101 of V2.0 of the CD1865
26 * databook */
27#define SX_ACK_MINT 0x75 /* goes to PILR1 */
28#define SX_ACK_TINT 0x76 /* goes to PILR2 */
29#define SX_ACK_RINT 0x77 /* goes to PILR3 */
30
31/* Chip ID (is used when chips ar daisy chained.) */
32#define SX_ID 0x10
33
34/* Definitions for Cirrus Logic CL-CD186x 8-port async mux chip */
35
36#define CD186x_NCH 8 /* Total number of channels */
37#define CD186x_TPC 16 /* Ticks per character */
38#define CD186x_NFIFO 8 /* TX FIFO size */
39
40
41/* Global registers */
42
43#define CD186x_GIVR 0x40 /* Global Interrupt Vector Register */
44#define CD186x_GICR 0x41 /* Global Interrupting Channel Register */
45#define CD186x_PILR1 0x61 /* Priority Interrupt Level Register 1 */
46#define CD186x_PILR2 0x62 /* Priority Interrupt Level Register 2 */
47#define CD186x_PILR3 0x63 /* Priority Interrupt Level Register 3 */
48#define CD186x_CAR 0x64 /* Channel Access Register */
49#define CD186x_SRSR 0x65 /* Channel Access Register */
50#define CD186x_GFRCR 0x6b /* Global Firmware Revision Code Register */
51#define CD186x_PPRH 0x70 /* Prescaler Period Register High */
52#define CD186x_PPRL 0x71 /* Prescaler Period Register Low */
53#define CD186x_RDR 0x78 /* Receiver Data Register */
54#define CD186x_RCSR 0x7a /* Receiver Character Status Register */
55#define CD186x_TDR 0x7b /* Transmit Data Register */
56#define CD186x_EOIR 0x7f /* End of Interrupt Register */
57#define CD186x_MRAR 0x75 /* Modem Request Acknowledge register */
58#define CD186x_TRAR 0x76 /* Transmit Request Acknowledge register */
59#define CD186x_RRAR 0x77 /* Receive Request Acknowledge register */
60#define CD186x_SRCR 0x66 /* Service Request Configuration register */
61
62/* Channel Registers */
63
64#define CD186x_CCR 0x01 /* Channel Command Register */
65#define CD186x_IER 0x02 /* Interrupt Enable Register */
66#define CD186x_COR1 0x03 /* Channel Option Register 1 */
67#define CD186x_COR2 0x04 /* Channel Option Register 2 */
68#define CD186x_COR3 0x05 /* Channel Option Register 3 */
69#define CD186x_CCSR 0x06 /* Channel Control Status Register */
70#define CD186x_RDCR 0x07 /* Receive Data Count Register */
71#define CD186x_SCHR1 0x09 /* Special Character Register 1 */
72#define CD186x_SCHR2 0x0a /* Special Character Register 2 */
73#define CD186x_SCHR3 0x0b /* Special Character Register 3 */
74#define CD186x_SCHR4 0x0c /* Special Character Register 4 */
75#define CD186x_MCOR1 0x10 /* Modem Change Option 1 Register */
76#define CD186x_MCOR2 0x11 /* Modem Change Option 2 Register */
77#define CD186x_MCR 0x12 /* Modem Change Register */
78#define CD186x_RTPR 0x18 /* Receive Timeout Period Register */
79#define CD186x_MSVR 0x28 /* Modem Signal Value Register */
80#define CD186x_MSVRTS 0x29 /* Modem Signal Value Register */
81#define CD186x_MSVDTR 0x2a /* Modem Signal Value Register */
82#define CD186x_RBPRH 0x31 /* Receive Baud Rate Period Register High */
83#define CD186x_RBPRL 0x32 /* Receive Baud Rate Period Register Low */
84#define CD186x_TBPRH 0x39 /* Transmit Baud Rate Period Register High */
85#define CD186x_TBPRL 0x3a /* Transmit Baud Rate Period Register Low */
86
87
88/* Global Interrupt Vector Register (R/W) */
89
90#define GIVR_ITMASK 0x07 /* Interrupt type mask */
91#define GIVR_IT_MODEM 0x01 /* Modem Signal Change Interrupt */
92#define GIVR_IT_TX 0x02 /* Transmit Data Interrupt */
93#define GIVR_IT_RCV 0x03 /* Receive Good Data Interrupt */
94#define GIVR_IT_REXC 0x07 /* Receive Exception Interrupt */
95
96
97/* Global Interrupt Channel Register (R/W) */
98
99#define GICR_CHAN 0x1c /* Channel Number Mask */
100#define GICR_CHAN_OFF 2 /* Channel Number shift */
101
102
103/* Channel Address Register (R/W) */
104
105#define CAR_CHAN 0x07 /* Channel Number Mask */
106#define CAR_A7 0x08 /* A7 Address Extension (unused) */
107
108
109/* Receive Character Status Register (R/O) */
110
111#define RCSR_TOUT 0x80 /* Rx Timeout */
112#define RCSR_SCDET 0x70 /* Special Character Detected Mask */
113#define RCSR_NO_SC 0x00 /* No Special Characters Detected */
114#define RCSR_SC_1 0x10 /* Special Char 1 (or 1 & 3) Detected */
115#define RCSR_SC_2 0x20 /* Special Char 2 (or 2 & 4) Detected */
116#define RCSR_SC_3 0x30 /* Special Char 3 Detected */
117#define RCSR_SC_4 0x40 /* Special Char 4 Detected */
118#define RCSR_BREAK 0x08 /* Break has been detected */
119#define RCSR_PE 0x04 /* Parity Error */
120#define RCSR_FE 0x02 /* Frame Error */
121#define RCSR_OE 0x01 /* Overrun Error */
122
123
124/* Channel Command Register (R/W) (commands in groups can be OR-ed) */
125
126#define CCR_HARDRESET 0x81 /* Reset the chip */
127
128#define CCR_SOFTRESET 0x80 /* Soft Channel Reset */
129
130#define CCR_CORCHG1 0x42 /* Channel Option Register 1 Changed */
131#define CCR_CORCHG2 0x44 /* Channel Option Register 2 Changed */
132#define CCR_CORCHG3 0x48 /* Channel Option Register 3 Changed */
133
134#define CCR_SSCH1 0x21 /* Send Special Character 1 */
135
136#define CCR_SSCH2 0x22 /* Send Special Character 2 */
137
138#define CCR_SSCH3 0x23 /* Send Special Character 3 */
139
140#define CCR_SSCH4 0x24 /* Send Special Character 4 */
141
142#define CCR_TXEN 0x18 /* Enable Transmitter */
143#define CCR_RXEN 0x12 /* Enable Receiver */
144
145#define CCR_TXDIS 0x14 /* Disable Transmitter */
146#define CCR_RXDIS 0x11 /* Disable Receiver */
147
148
149/* Interrupt Enable Register (R/W) */
150
151#define IER_DSR 0x80 /* Enable interrupt on DSR change */
152#define IER_CD 0x40 /* Enable interrupt on CD change */
153#define IER_CTS 0x20 /* Enable interrupt on CTS change */
154#define IER_RXD 0x10 /* Enable interrupt on Receive Data */
155#define IER_RXSC 0x08 /* Enable interrupt on Receive Spec. Char */
156#define IER_TXRDY 0x04 /* Enable interrupt on TX FIFO empty */
157#define IER_TXEMPTY 0x02 /* Enable interrupt on TX completely empty */
158#define IER_RET 0x01 /* Enable interrupt on RX Exc. Timeout */
159
160
161/* Channel Option Register 1 (R/W) */
162
163#define COR1_ODDP 0x80 /* Odd Parity */
164#define COR1_PARMODE 0x60 /* Parity Mode mask */
165#define COR1_NOPAR 0x00 /* No Parity */
166#define COR1_FORCEPAR 0x20 /* Force Parity */
167#define COR1_NORMPAR 0x40 /* Normal Parity */
168#define COR1_IGNORE 0x10 /* Ignore Parity on RX */
169#define COR1_STOPBITS 0x0c /* Number of Stop Bits */
170#define COR1_1SB 0x00 /* 1 Stop Bit */
171#define COR1_15SB 0x04 /* 1.5 Stop Bits */
172#define COR1_2SB 0x08 /* 2 Stop Bits */
173#define COR1_CHARLEN 0x03 /* Character Length */
174#define COR1_5BITS 0x00 /* 5 bits */
175#define COR1_6BITS 0x01 /* 6 bits */
176#define COR1_7BITS 0x02 /* 7 bits */
177#define COR1_8BITS 0x03 /* 8 bits */
178
179
180/* Channel Option Register 2 (R/W) */
181
182#define COR2_IXM 0x80 /* Implied XON mode */
183#define COR2_TXIBE 0x40 /* Enable In-Band (XON/XOFF) Flow Control */
184#define COR2_ETC 0x20 /* Embedded Tx Commands Enable */
185#define COR2_LLM 0x10 /* Local Loopback Mode */
186#define COR2_RLM 0x08 /* Remote Loopback Mode */
187#define COR2_RTSAO 0x04 /* RTS Automatic Output Enable */
188#define COR2_CTSAE 0x02 /* CTS Automatic Enable */
189#define COR2_DSRAE 0x01 /* DSR Automatic Enable */
190
191
192/* Channel Option Register 3 (R/W) */
193
194#define COR3_XONCH 0x80 /* XON is a pair of characters (1 & 3) */
195#define COR3_XOFFCH 0x40 /* XOFF is a pair of characters (2 & 4) */
196#define COR3_FCT 0x20 /* Flow-Control Transparency Mode */
197#define COR3_SCDE 0x10 /* Special Character Detection Enable */
198#define COR3_RXTH 0x0f /* RX FIFO Threshold value (1-8) */
199
200
201/* Channel Control Status Register (R/O) */
202
203#define CCSR_RXEN 0x80 /* Receiver Enabled */
204#define CCSR_RXFLOFF 0x40 /* Receive Flow Off (XOFF was sent) */
205#define CCSR_RXFLON 0x20 /* Receive Flow On (XON was sent) */
206#define CCSR_TXEN 0x08 /* Transmitter Enabled */
207#define CCSR_TXFLOFF 0x04 /* Transmit Flow Off (got XOFF) */
208#define CCSR_TXFLON 0x02 /* Transmit Flow On (got XON) */
209
210
211/* Modem Change Option Register 1 (R/W) */
212
213#define MCOR1_DSRZD 0x80 /* Detect 0->1 transition of DSR */
214#define MCOR1_CDZD 0x40 /* Detect 0->1 transition of CD */
215#define MCOR1_CTSZD 0x20 /* Detect 0->1 transition of CTS */
216#define MCOR1_DTRTH 0x0f /* Auto DTR flow control Threshold (1-8) */
217#define MCOR1_NODTRFC 0x0 /* Automatic DTR flow control disabled */
218
219
220/* Modem Change Option Register 2 (R/W) */
221
222#define MCOR2_DSROD 0x80 /* Detect 1->0 transition of DSR */
223#define MCOR2_CDOD 0x40 /* Detect 1->0 transition of CD */
224#define MCOR2_CTSOD 0x20 /* Detect 1->0 transition of CTS */
225
226/* Modem Change Register (R/W) */
227
228#define MCR_DSRCHG 0x80 /* DSR Changed */
229#define MCR_CDCHG 0x40 /* CD Changed */
230#define MCR_CTSCHG 0x20 /* CTS Changed */
231
232
233/* Modem Signal Value Register (R/W) */
234
235#define MSVR_DSR 0x80 /* Current state of DSR input */
236#define MSVR_CD 0x40 /* Current state of CD input */
237#define MSVR_CTS 0x20 /* Current state of CTS input */
238#define MSVR_DTR 0x02 /* Current state of DTR output */
239#define MSVR_RTS 0x01 /* Current state of RTS output */
240
241
242/* Escape characters */
243
244#define CD186x_C_ESC 0x00 /* Escape character */
245#define CD186x_C_SBRK 0x81 /* Start sending BREAK */
246#define CD186x_C_DELAY 0x82 /* Delay output */
247#define CD186x_C_EBRK 0x83 /* Stop sending BREAK */
248
249#define SRSR_RREQint 0x10 /* This chip wants "rec" serviced */
250#define SRSR_TREQint 0x04 /* This chip wants "transmit" serviced */
251#define SRSR_MREQint 0x01 /* This chip wants "mdm change" serviced */
252
253
254
255#define SRCR_PKGTYPE 0x80
256#define SRCR_REGACKEN 0x40
257#define SRCR_DAISYEN 0x20
258#define SRCR_GLOBPRI 0x10
259#define SRCR_UNFAIR 0x08
260#define SRCR_AUTOPRI 0x02
261#define SRCR_PRISEL 0x01
262
263
diff --git a/drivers/char/consolemap.c b/drivers/char/consolemap.c
deleted file mode 100644
index 45d3e80156d4..000000000000
--- a/drivers/char/consolemap.c
+++ /dev/null
@@ -1,745 +0,0 @@
1/*
2 * consolemap.c
3 *
4 * Mapping from internal code (such as Latin-1 or Unicode or IBM PC code)
5 * to font positions.
6 *
7 * aeb, 950210
8 *
9 * Support for multiple unimaps by Jakub Jelinek <jj@ultra.linux.cz>, July 1998
10 *
11 * Fix bug in inverse translation. Stanislav Voronyi <stas@cnti.uanet.kharkov.ua>, Dec 1998
12 */
13
14#include <linux/module.h>
15#include <linux/kd.h>
16#include <linux/errno.h>
17#include <linux/mm.h>
18#include <linux/slab.h>
19#include <linux/init.h>
20#include <linux/tty.h>
21#include <asm/uaccess.h>
22#include <linux/consolemap.h>
23#include <linux/vt_kern.h>
24
25static unsigned short translations[][256] = {
26 /* 8-bit Latin-1 mapped to Unicode -- trivial mapping */
27 {
28 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007,
29 0x0008, 0x0009, 0x000a, 0x000b, 0x000c, 0x000d, 0x000e, 0x000f,
30 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017,
31 0x0018, 0x0019, 0x001a, 0x001b, 0x001c, 0x001d, 0x001e, 0x001f,
32 0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027,
33 0x0028, 0x0029, 0x002a, 0x002b, 0x002c, 0x002d, 0x002e, 0x002f,
34 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037,
35 0x0038, 0x0039, 0x003a, 0x003b, 0x003c, 0x003d, 0x003e, 0x003f,
36 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047,
37 0x0048, 0x0049, 0x004a, 0x004b, 0x004c, 0x004d, 0x004e, 0x004f,
38 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057,
39 0x0058, 0x0059, 0x005a, 0x005b, 0x005c, 0x005d, 0x005e, 0x005f,
40 0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067,
41 0x0068, 0x0069, 0x006a, 0x006b, 0x006c, 0x006d, 0x006e, 0x006f,
42 0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077,
43 0x0078, 0x0079, 0x007a, 0x007b, 0x007c, 0x007d, 0x007e, 0x007f,
44 0x0080, 0x0081, 0x0082, 0x0083, 0x0084, 0x0085, 0x0086, 0x0087,
45 0x0088, 0x0089, 0x008a, 0x008b, 0x008c, 0x008d, 0x008e, 0x008f,
46 0x0090, 0x0091, 0x0092, 0x0093, 0x0094, 0x0095, 0x0096, 0x0097,
47 0x0098, 0x0099, 0x009a, 0x009b, 0x009c, 0x009d, 0x009e, 0x009f,
48 0x00a0, 0x00a1, 0x00a2, 0x00a3, 0x00a4, 0x00a5, 0x00a6, 0x00a7,
49 0x00a8, 0x00a9, 0x00aa, 0x00ab, 0x00ac, 0x00ad, 0x00ae, 0x00af,
50 0x00b0, 0x00b1, 0x00b2, 0x00b3, 0x00b4, 0x00b5, 0x00b6, 0x00b7,
51 0x00b8, 0x00b9, 0x00ba, 0x00bb, 0x00bc, 0x00bd, 0x00be, 0x00bf,
52 0x00c0, 0x00c1, 0x00c2, 0x00c3, 0x00c4, 0x00c5, 0x00c6, 0x00c7,
53 0x00c8, 0x00c9, 0x00ca, 0x00cb, 0x00cc, 0x00cd, 0x00ce, 0x00cf,
54 0x00d0, 0x00d1, 0x00d2, 0x00d3, 0x00d4, 0x00d5, 0x00d6, 0x00d7,
55 0x00d8, 0x00d9, 0x00da, 0x00db, 0x00dc, 0x00dd, 0x00de, 0x00df,
56 0x00e0, 0x00e1, 0x00e2, 0x00e3, 0x00e4, 0x00e5, 0x00e6, 0x00e7,
57 0x00e8, 0x00e9, 0x00ea, 0x00eb, 0x00ec, 0x00ed, 0x00ee, 0x00ef,
58 0x00f0, 0x00f1, 0x00f2, 0x00f3, 0x00f4, 0x00f5, 0x00f6, 0x00f7,
59 0x00f8, 0x00f9, 0x00fa, 0x00fb, 0x00fc, 0x00fd, 0x00fe, 0x00ff
60 },
61 /* VT100 graphics mapped to Unicode */
62 {
63 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007,
64 0x0008, 0x0009, 0x000a, 0x000b, 0x000c, 0x000d, 0x000e, 0x000f,
65 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017,
66 0x0018, 0x0019, 0x001a, 0x001b, 0x001c, 0x001d, 0x001e, 0x001f,
67 0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027,
68 0x0028, 0x0029, 0x002a, 0x2192, 0x2190, 0x2191, 0x2193, 0x002f,
69 0x2588, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037,
70 0x0038, 0x0039, 0x003a, 0x003b, 0x003c, 0x003d, 0x003e, 0x003f,
71 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047,
72 0x0048, 0x0049, 0x004a, 0x004b, 0x004c, 0x004d, 0x004e, 0x004f,
73 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057,
74 0x0058, 0x0059, 0x005a, 0x005b, 0x005c, 0x005d, 0x005e, 0x00a0,
75 0x25c6, 0x2592, 0x2409, 0x240c, 0x240d, 0x240a, 0x00b0, 0x00b1,
76 0x2591, 0x240b, 0x2518, 0x2510, 0x250c, 0x2514, 0x253c, 0x23ba,
77 0x23bb, 0x2500, 0x23bc, 0x23bd, 0x251c, 0x2524, 0x2534, 0x252c,
78 0x2502, 0x2264, 0x2265, 0x03c0, 0x2260, 0x00a3, 0x00b7, 0x007f,
79 0x0080, 0x0081, 0x0082, 0x0083, 0x0084, 0x0085, 0x0086, 0x0087,
80 0x0088, 0x0089, 0x008a, 0x008b, 0x008c, 0x008d, 0x008e, 0x008f,
81 0x0090, 0x0091, 0x0092, 0x0093, 0x0094, 0x0095, 0x0096, 0x0097,
82 0x0098, 0x0099, 0x009a, 0x009b, 0x009c, 0x009d, 0x009e, 0x009f,
83 0x00a0, 0x00a1, 0x00a2, 0x00a3, 0x00a4, 0x00a5, 0x00a6, 0x00a7,
84 0x00a8, 0x00a9, 0x00aa, 0x00ab, 0x00ac, 0x00ad, 0x00ae, 0x00af,
85 0x00b0, 0x00b1, 0x00b2, 0x00b3, 0x00b4, 0x00b5, 0x00b6, 0x00b7,
86 0x00b8, 0x00b9, 0x00ba, 0x00bb, 0x00bc, 0x00bd, 0x00be, 0x00bf,
87 0x00c0, 0x00c1, 0x00c2, 0x00c3, 0x00c4, 0x00c5, 0x00c6, 0x00c7,
88 0x00c8, 0x00c9, 0x00ca, 0x00cb, 0x00cc, 0x00cd, 0x00ce, 0x00cf,
89 0x00d0, 0x00d1, 0x00d2, 0x00d3, 0x00d4, 0x00d5, 0x00d6, 0x00d7,
90 0x00d8, 0x00d9, 0x00da, 0x00db, 0x00dc, 0x00dd, 0x00de, 0x00df,
91 0x00e0, 0x00e1, 0x00e2, 0x00e3, 0x00e4, 0x00e5, 0x00e6, 0x00e7,
92 0x00e8, 0x00e9, 0x00ea, 0x00eb, 0x00ec, 0x00ed, 0x00ee, 0x00ef,
93 0x00f0, 0x00f1, 0x00f2, 0x00f3, 0x00f4, 0x00f5, 0x00f6, 0x00f7,
94 0x00f8, 0x00f9, 0x00fa, 0x00fb, 0x00fc, 0x00fd, 0x00fe, 0x00ff
95 },
96 /* IBM Codepage 437 mapped to Unicode */
97 {
98 0x0000, 0x263a, 0x263b, 0x2665, 0x2666, 0x2663, 0x2660, 0x2022,
99 0x25d8, 0x25cb, 0x25d9, 0x2642, 0x2640, 0x266a, 0x266b, 0x263c,
100 0x25b6, 0x25c0, 0x2195, 0x203c, 0x00b6, 0x00a7, 0x25ac, 0x21a8,
101 0x2191, 0x2193, 0x2192, 0x2190, 0x221f, 0x2194, 0x25b2, 0x25bc,
102 0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027,
103 0x0028, 0x0029, 0x002a, 0x002b, 0x002c, 0x002d, 0x002e, 0x002f,
104 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037,
105 0x0038, 0x0039, 0x003a, 0x003b, 0x003c, 0x003d, 0x003e, 0x003f,
106 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047,
107 0x0048, 0x0049, 0x004a, 0x004b, 0x004c, 0x004d, 0x004e, 0x004f,
108 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057,
109 0x0058, 0x0059, 0x005a, 0x005b, 0x005c, 0x005d, 0x005e, 0x005f,
110 0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067,
111 0x0068, 0x0069, 0x006a, 0x006b, 0x006c, 0x006d, 0x006e, 0x006f,
112 0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077,
113 0x0078, 0x0079, 0x007a, 0x007b, 0x007c, 0x007d, 0x007e, 0x2302,
114 0x00c7, 0x00fc, 0x00e9, 0x00e2, 0x00e4, 0x00e0, 0x00e5, 0x00e7,
115 0x00ea, 0x00eb, 0x00e8, 0x00ef, 0x00ee, 0x00ec, 0x00c4, 0x00c5,
116 0x00c9, 0x00e6, 0x00c6, 0x00f4, 0x00f6, 0x00f2, 0x00fb, 0x00f9,
117 0x00ff, 0x00d6, 0x00dc, 0x00a2, 0x00a3, 0x00a5, 0x20a7, 0x0192,
118 0x00e1, 0x00ed, 0x00f3, 0x00fa, 0x00f1, 0x00d1, 0x00aa, 0x00ba,
119 0x00bf, 0x2310, 0x00ac, 0x00bd, 0x00bc, 0x00a1, 0x00ab, 0x00bb,
120 0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x2561, 0x2562, 0x2556,
121 0x2555, 0x2563, 0x2551, 0x2557, 0x255d, 0x255c, 0x255b, 0x2510,
122 0x2514, 0x2534, 0x252c, 0x251c, 0x2500, 0x253c, 0x255e, 0x255f,
123 0x255a, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256c, 0x2567,
124 0x2568, 0x2564, 0x2565, 0x2559, 0x2558, 0x2552, 0x2553, 0x256b,
125 0x256a, 0x2518, 0x250c, 0x2588, 0x2584, 0x258c, 0x2590, 0x2580,
126 0x03b1, 0x00df, 0x0393, 0x03c0, 0x03a3, 0x03c3, 0x00b5, 0x03c4,
127 0x03a6, 0x0398, 0x03a9, 0x03b4, 0x221e, 0x03c6, 0x03b5, 0x2229,
128 0x2261, 0x00b1, 0x2265, 0x2264, 0x2320, 0x2321, 0x00f7, 0x2248,
129 0x00b0, 0x2219, 0x00b7, 0x221a, 0x207f, 0x00b2, 0x25a0, 0x00a0
130 },
131 /* User mapping -- default to codes for direct font mapping */
132 {
133 0xf000, 0xf001, 0xf002, 0xf003, 0xf004, 0xf005, 0xf006, 0xf007,
134 0xf008, 0xf009, 0xf00a, 0xf00b, 0xf00c, 0xf00d, 0xf00e, 0xf00f,
135 0xf010, 0xf011, 0xf012, 0xf013, 0xf014, 0xf015, 0xf016, 0xf017,
136 0xf018, 0xf019, 0xf01a, 0xf01b, 0xf01c, 0xf01d, 0xf01e, 0xf01f,
137 0xf020, 0xf021, 0xf022, 0xf023, 0xf024, 0xf025, 0xf026, 0xf027,
138 0xf028, 0xf029, 0xf02a, 0xf02b, 0xf02c, 0xf02d, 0xf02e, 0xf02f,
139 0xf030, 0xf031, 0xf032, 0xf033, 0xf034, 0xf035, 0xf036, 0xf037,
140 0xf038, 0xf039, 0xf03a, 0xf03b, 0xf03c, 0xf03d, 0xf03e, 0xf03f,
141 0xf040, 0xf041, 0xf042, 0xf043, 0xf044, 0xf045, 0xf046, 0xf047,
142 0xf048, 0xf049, 0xf04a, 0xf04b, 0xf04c, 0xf04d, 0xf04e, 0xf04f,
143 0xf050, 0xf051, 0xf052, 0xf053, 0xf054, 0xf055, 0xf056, 0xf057,
144 0xf058, 0xf059, 0xf05a, 0xf05b, 0xf05c, 0xf05d, 0xf05e, 0xf05f,
145 0xf060, 0xf061, 0xf062, 0xf063, 0xf064, 0xf065, 0xf066, 0xf067,
146 0xf068, 0xf069, 0xf06a, 0xf06b, 0xf06c, 0xf06d, 0xf06e, 0xf06f,
147 0xf070, 0xf071, 0xf072, 0xf073, 0xf074, 0xf075, 0xf076, 0xf077,
148 0xf078, 0xf079, 0xf07a, 0xf07b, 0xf07c, 0xf07d, 0xf07e, 0xf07f,
149 0xf080, 0xf081, 0xf082, 0xf083, 0xf084, 0xf085, 0xf086, 0xf087,
150 0xf088, 0xf089, 0xf08a, 0xf08b, 0xf08c, 0xf08d, 0xf08e, 0xf08f,
151 0xf090, 0xf091, 0xf092, 0xf093, 0xf094, 0xf095, 0xf096, 0xf097,
152 0xf098, 0xf099, 0xf09a, 0xf09b, 0xf09c, 0xf09d, 0xf09e, 0xf09f,
153 0xf0a0, 0xf0a1, 0xf0a2, 0xf0a3, 0xf0a4, 0xf0a5, 0xf0a6, 0xf0a7,
154 0xf0a8, 0xf0a9, 0xf0aa, 0xf0ab, 0xf0ac, 0xf0ad, 0xf0ae, 0xf0af,
155 0xf0b0, 0xf0b1, 0xf0b2, 0xf0b3, 0xf0b4, 0xf0b5, 0xf0b6, 0xf0b7,
156 0xf0b8, 0xf0b9, 0xf0ba, 0xf0bb, 0xf0bc, 0xf0bd, 0xf0be, 0xf0bf,
157 0xf0c0, 0xf0c1, 0xf0c2, 0xf0c3, 0xf0c4, 0xf0c5, 0xf0c6, 0xf0c7,
158 0xf0c8, 0xf0c9, 0xf0ca, 0xf0cb, 0xf0cc, 0xf0cd, 0xf0ce, 0xf0cf,
159 0xf0d0, 0xf0d1, 0xf0d2, 0xf0d3, 0xf0d4, 0xf0d5, 0xf0d6, 0xf0d7,
160 0xf0d8, 0xf0d9, 0xf0da, 0xf0db, 0xf0dc, 0xf0dd, 0xf0de, 0xf0df,
161 0xf0e0, 0xf0e1, 0xf0e2, 0xf0e3, 0xf0e4, 0xf0e5, 0xf0e6, 0xf0e7,
162 0xf0e8, 0xf0e9, 0xf0ea, 0xf0eb, 0xf0ec, 0xf0ed, 0xf0ee, 0xf0ef,
163 0xf0f0, 0xf0f1, 0xf0f2, 0xf0f3, 0xf0f4, 0xf0f5, 0xf0f6, 0xf0f7,
164 0xf0f8, 0xf0f9, 0xf0fa, 0xf0fb, 0xf0fc, 0xf0fd, 0xf0fe, 0xf0ff
165 }
166};
167
168/* The standard kernel character-to-font mappings are not invertible
169 -- this is just a best effort. */
170
171#define MAX_GLYPH 512 /* Max possible glyph value */
172
173static int inv_translate[MAX_NR_CONSOLES];
174
175struct uni_pagedir {
176 u16 **uni_pgdir[32];
177 unsigned long refcount;
178 unsigned long sum;
179 unsigned char *inverse_translations[4];
180 u16 *inverse_trans_unicode;
181 int readonly;
182};
183
184static struct uni_pagedir *dflt;
185
186static void set_inverse_transl(struct vc_data *conp, struct uni_pagedir *p, int i)
187{
188 int j, glyph;
189 unsigned short *t = translations[i];
190 unsigned char *q;
191
192 if (!p) return;
193 q = p->inverse_translations[i];
194
195 if (!q) {
196 q = p->inverse_translations[i] = (unsigned char *)
197 kmalloc(MAX_GLYPH, GFP_KERNEL);
198 if (!q) return;
199 }
200 memset(q, 0, MAX_GLYPH);
201
202 for (j = 0; j < E_TABSZ; j++) {
203 glyph = conv_uni_to_pc(conp, t[j]);
204 if (glyph >= 0 && glyph < MAX_GLYPH && q[glyph] < 32) {
205 /* prefer '-' above SHY etc. */
206 q[glyph] = j;
207 }
208 }
209}
210
211static void set_inverse_trans_unicode(struct vc_data *conp,
212 struct uni_pagedir *p)
213{
214 int i, j, k, glyph;
215 u16 **p1, *p2;
216 u16 *q;
217
218 if (!p) return;
219 q = p->inverse_trans_unicode;
220 if (!q) {
221 q = p->inverse_trans_unicode =
222 kmalloc(MAX_GLYPH * sizeof(u16), GFP_KERNEL);
223 if (!q)
224 return;
225 }
226 memset(q, 0, MAX_GLYPH * sizeof(u16));
227
228 for (i = 0; i < 32; i++) {
229 p1 = p->uni_pgdir[i];
230 if (!p1)
231 continue;
232 for (j = 0; j < 32; j++) {
233 p2 = p1[j];
234 if (!p2)
235 continue;
236 for (k = 0; k < 64; k++) {
237 glyph = p2[k];
238 if (glyph >= 0 && glyph < MAX_GLYPH
239 && q[glyph] < 32)
240 q[glyph] = (i << 11) + (j << 6) + k;
241 }
242 }
243 }
244}
245
246unsigned short *set_translate(int m, struct vc_data *vc)
247{
248 inv_translate[vc->vc_num] = m;
249 return translations[m];
250}
251
252/*
253 * Inverse translation is impossible for several reasons:
254 * 1. The font<->character maps are not 1-1.
255 * 2. The text may have been written while a different translation map
256 * was active.
257 * Still, it is now possible to a certain extent to cut and paste non-ASCII.
258 */
259u16 inverse_translate(struct vc_data *conp, int glyph, int use_unicode)
260{
261 struct uni_pagedir *p;
262 int m;
263 if (glyph < 0 || glyph >= MAX_GLYPH)
264 return 0;
265 else if (!(p = (struct uni_pagedir *)*conp->vc_uni_pagedir_loc))
266 return glyph;
267 else if (use_unicode) {
268 if (!p->inverse_trans_unicode)
269 return glyph;
270 else
271 return p->inverse_trans_unicode[glyph];
272 } else {
273 m = inv_translate[conp->vc_num];
274 if (!p->inverse_translations[m])
275 return glyph;
276 else
277 return p->inverse_translations[m][glyph];
278 }
279}
280EXPORT_SYMBOL_GPL(inverse_translate);
281
282static void update_user_maps(void)
283{
284 int i;
285 struct uni_pagedir *p, *q = NULL;
286
287 for (i = 0; i < MAX_NR_CONSOLES; i++) {
288 if (!vc_cons_allocated(i))
289 continue;
290 p = (struct uni_pagedir *)*vc_cons[i].d->vc_uni_pagedir_loc;
291 if (p && p != q) {
292 set_inverse_transl(vc_cons[i].d, p, USER_MAP);
293 set_inverse_trans_unicode(vc_cons[i].d, p);
294 q = p;
295 }
296 }
297}
298
299/*
300 * Load customizable translation table
301 * arg points to a 256 byte translation table.
302 *
303 * The "old" variants are for translation directly to font (using the
304 * 0xf000-0xf0ff "transparent" Unicodes) whereas the "new" variants set
305 * Unicodes explicitly.
306 */
307int con_set_trans_old(unsigned char __user * arg)
308{
309 int i;
310 unsigned short *p = translations[USER_MAP];
311
312 if (!access_ok(VERIFY_READ, arg, E_TABSZ))
313 return -EFAULT;
314
315 for (i=0; i<E_TABSZ ; i++) {
316 unsigned char uc;
317 __get_user(uc, arg+i);
318 p[i] = UNI_DIRECT_BASE | uc;
319 }
320
321 update_user_maps();
322 return 0;
323}
324
325int con_get_trans_old(unsigned char __user * arg)
326{
327 int i, ch;
328 unsigned short *p = translations[USER_MAP];
329
330 if (!access_ok(VERIFY_WRITE, arg, E_TABSZ))
331 return -EFAULT;
332
333 for (i=0; i<E_TABSZ ; i++)
334 {
335 ch = conv_uni_to_pc(vc_cons[fg_console].d, p[i]);
336 __put_user((ch & ~0xff) ? 0 : ch, arg+i);
337 }
338 return 0;
339}
340
341int con_set_trans_new(ushort __user * arg)
342{
343 int i;
344 unsigned short *p = translations[USER_MAP];
345
346 if (!access_ok(VERIFY_READ, arg, E_TABSZ*sizeof(unsigned short)))
347 return -EFAULT;
348
349 for (i=0; i<E_TABSZ ; i++) {
350 unsigned short us;
351 __get_user(us, arg+i);
352 p[i] = us;
353 }
354
355 update_user_maps();
356 return 0;
357}
358
359int con_get_trans_new(ushort __user * arg)
360{
361 int i;
362 unsigned short *p = translations[USER_MAP];
363
364 if (!access_ok(VERIFY_WRITE, arg, E_TABSZ*sizeof(unsigned short)))
365 return -EFAULT;
366
367 for (i=0; i<E_TABSZ ; i++)
368 __put_user(p[i], arg+i);
369
370 return 0;
371}
372
373/*
374 * Unicode -> current font conversion
375 *
376 * A font has at most 512 chars, usually 256.
377 * But one font position may represent several Unicode chars.
378 * A hashtable is somewhat of a pain to deal with, so use a
379 * "paged table" instead. Simulation has shown the memory cost of
380 * this 3-level paged table scheme to be comparable to a hash table.
381 */
382
383extern u8 dfont_unicount[]; /* Defined in console_defmap.c */
384extern u16 dfont_unitable[];
385
386static void con_release_unimap(struct uni_pagedir *p)
387{
388 u16 **p1;
389 int i, j;
390
391 if (p == dflt) dflt = NULL;
392 for (i = 0; i < 32; i++) {
393 if ((p1 = p->uni_pgdir[i]) != NULL) {
394 for (j = 0; j < 32; j++)
395 kfree(p1[j]);
396 kfree(p1);
397 }
398 p->uni_pgdir[i] = NULL;
399 }
400 for (i = 0; i < 4; i++) {
401 kfree(p->inverse_translations[i]);
402 p->inverse_translations[i] = NULL;
403 }
404 if (p->inverse_trans_unicode) {
405 kfree(p->inverse_trans_unicode);
406 p->inverse_trans_unicode = NULL;
407 }
408}
409
410void con_free_unimap(struct vc_data *vc)
411{
412 struct uni_pagedir *p;
413
414 p = (struct uni_pagedir *)*vc->vc_uni_pagedir_loc;
415 if (!p)
416 return;
417 *vc->vc_uni_pagedir_loc = 0;
418 if (--p->refcount)
419 return;
420 con_release_unimap(p);
421 kfree(p);
422}
423
424static int con_unify_unimap(struct vc_data *conp, struct uni_pagedir *p)
425{
426 int i, j, k;
427 struct uni_pagedir *q;
428
429 for (i = 0; i < MAX_NR_CONSOLES; i++) {
430 if (!vc_cons_allocated(i))
431 continue;
432 q = (struct uni_pagedir *)*vc_cons[i].d->vc_uni_pagedir_loc;
433 if (!q || q == p || q->sum != p->sum)
434 continue;
435 for (j = 0; j < 32; j++) {
436 u16 **p1, **q1;
437 p1 = p->uni_pgdir[j]; q1 = q->uni_pgdir[j];
438 if (!p1 && !q1)
439 continue;
440 if (!p1 || !q1)
441 break;
442 for (k = 0; k < 32; k++) {
443 if (!p1[k] && !q1[k])
444 continue;
445 if (!p1[k] || !q1[k])
446 break;
447 if (memcmp(p1[k], q1[k], 64*sizeof(u16)))
448 break;
449 }
450 if (k < 32)
451 break;
452 }
453 if (j == 32) {
454 q->refcount++;
455 *conp->vc_uni_pagedir_loc = (unsigned long)q;
456 con_release_unimap(p);
457 kfree(p);
458 return 1;
459 }
460 }
461 return 0;
462}
463
464static int
465con_insert_unipair(struct uni_pagedir *p, u_short unicode, u_short fontpos)
466{
467 int i, n;
468 u16 **p1, *p2;
469
470 if (!(p1 = p->uni_pgdir[n = unicode >> 11])) {
471 p1 = p->uni_pgdir[n] = kmalloc(32*sizeof(u16 *), GFP_KERNEL);
472 if (!p1) return -ENOMEM;
473 for (i = 0; i < 32; i++)
474 p1[i] = NULL;
475 }
476
477 if (!(p2 = p1[n = (unicode >> 6) & 0x1f])) {
478 p2 = p1[n] = kmalloc(64*sizeof(u16), GFP_KERNEL);
479 if (!p2) return -ENOMEM;
480 memset(p2, 0xff, 64*sizeof(u16)); /* No glyphs for the characters (yet) */
481 }
482
483 p2[unicode & 0x3f] = fontpos;
484
485 p->sum += (fontpos << 20) + unicode;
486
487 return 0;
488}
489
490/* ui is a leftover from using a hashtable, but might be used again */
491int con_clear_unimap(struct vc_data *vc, struct unimapinit *ui)
492{
493 struct uni_pagedir *p, *q;
494
495 p = (struct uni_pagedir *)*vc->vc_uni_pagedir_loc;
496 if (p && p->readonly) return -EIO;
497 if (!p || --p->refcount) {
498 q = kzalloc(sizeof(*p), GFP_KERNEL);
499 if (!q) {
500 if (p) p->refcount++;
501 return -ENOMEM;
502 }
503 q->refcount=1;
504 *vc->vc_uni_pagedir_loc = (unsigned long)q;
505 } else {
506 if (p == dflt) dflt = NULL;
507 p->refcount++;
508 p->sum = 0;
509 con_release_unimap(p);
510 }
511 return 0;
512}
513
514int con_set_unimap(struct vc_data *vc, ushort ct, struct unipair __user *list)
515{
516 int err = 0, err1, i;
517 struct uni_pagedir *p, *q;
518
519 p = (struct uni_pagedir *)*vc->vc_uni_pagedir_loc;
520 if (p->readonly) return -EIO;
521
522 if (!ct) return 0;
523
524 if (p->refcount > 1) {
525 int j, k;
526 u16 **p1, *p2, l;
527
528 err1 = con_clear_unimap(vc, NULL);
529 if (err1) return err1;
530
531 q = (struct uni_pagedir *)*vc->vc_uni_pagedir_loc;
532 for (i = 0, l = 0; i < 32; i++)
533 if ((p1 = p->uni_pgdir[i]))
534 for (j = 0; j < 32; j++)
535 if ((p2 = p1[j]))
536 for (k = 0; k < 64; k++, l++)
537 if (p2[k] != 0xffff) {
538 err1 = con_insert_unipair(q, l, p2[k]);
539 if (err1) {
540 p->refcount++;
541 *vc->vc_uni_pagedir_loc = (unsigned long)p;
542 con_release_unimap(q);
543 kfree(q);
544 return err1;
545 }
546 }
547 p = q;
548 } else if (p == dflt)
549 dflt = NULL;
550
551 while (ct--) {
552 unsigned short unicode, fontpos;
553 __get_user(unicode, &list->unicode);
554 __get_user(fontpos, &list->fontpos);
555 if ((err1 = con_insert_unipair(p, unicode,fontpos)) != 0)
556 err = err1;
557 list++;
558 }
559
560 if (con_unify_unimap(vc, p))
561 return err;
562
563 for (i = 0; i <= 3; i++)
564 set_inverse_transl(vc, p, i); /* Update all inverse translations */
565 set_inverse_trans_unicode(vc, p);
566
567 return err;
568}
569
570/* Loads the unimap for the hardware font, as defined in uni_hash.tbl.
571 The representation used was the most compact I could come up
572 with. This routine is executed at sys_setup time, and when the
573 PIO_FONTRESET ioctl is called. */
574
575int con_set_default_unimap(struct vc_data *vc)
576{
577 int i, j, err = 0, err1;
578 u16 *q;
579 struct uni_pagedir *p;
580
581 if (dflt) {
582 p = (struct uni_pagedir *)*vc->vc_uni_pagedir_loc;
583 if (p == dflt)
584 return 0;
585 dflt->refcount++;
586 *vc->vc_uni_pagedir_loc = (unsigned long)dflt;
587 if (p && --p->refcount) {
588 con_release_unimap(p);
589 kfree(p);
590 }
591 return 0;
592 }
593
594 /* The default font is always 256 characters */
595
596 err = con_clear_unimap(vc, NULL);
597 if (err) return err;
598
599 p = (struct uni_pagedir *)*vc->vc_uni_pagedir_loc;
600 q = dfont_unitable;
601
602 for (i = 0; i < 256; i++)
603 for (j = dfont_unicount[i]; j; j--) {
604 err1 = con_insert_unipair(p, *(q++), i);
605 if (err1)
606 err = err1;
607 }
608
609 if (con_unify_unimap(vc, p)) {
610 dflt = (struct uni_pagedir *)*vc->vc_uni_pagedir_loc;
611 return err;
612 }
613
614 for (i = 0; i <= 3; i++)
615 set_inverse_transl(vc, p, i); /* Update all inverse translations */
616 set_inverse_trans_unicode(vc, p);
617 dflt = p;
618 return err;
619}
620EXPORT_SYMBOL(con_set_default_unimap);
621
622int con_copy_unimap(struct vc_data *dst_vc, struct vc_data *src_vc)
623{
624 struct uni_pagedir *q;
625
626 if (!*src_vc->vc_uni_pagedir_loc)
627 return -EINVAL;
628 if (*dst_vc->vc_uni_pagedir_loc == *src_vc->vc_uni_pagedir_loc)
629 return 0;
630 con_free_unimap(dst_vc);
631 q = (struct uni_pagedir *)*src_vc->vc_uni_pagedir_loc;
632 q->refcount++;
633 *dst_vc->vc_uni_pagedir_loc = (long)q;
634 return 0;
635}
636
637int con_get_unimap(struct vc_data *vc, ushort ct, ushort __user *uct, struct unipair __user *list)
638{
639 int i, j, k, ect;
640 u16 **p1, *p2;
641 struct uni_pagedir *p;
642
643 ect = 0;
644 if (*vc->vc_uni_pagedir_loc) {
645 p = (struct uni_pagedir *)*vc->vc_uni_pagedir_loc;
646 for (i = 0; i < 32; i++)
647 if ((p1 = p->uni_pgdir[i]))
648 for (j = 0; j < 32; j++)
649 if ((p2 = *(p1++)))
650 for (k = 0; k < 64; k++) {
651 if (*p2 < MAX_GLYPH && ect++ < ct) {
652 __put_user((u_short)((i<<11)+(j<<6)+k),
653 &list->unicode);
654 __put_user((u_short) *p2,
655 &list->fontpos);
656 list++;
657 }
658 p2++;
659 }
660 }
661 __put_user(ect, uct);
662 return ((ect <= ct) ? 0 : -ENOMEM);
663}
664
665void con_protect_unimap(struct vc_data *vc, int rdonly)
666{
667 struct uni_pagedir *p = (struct uni_pagedir *)*vc->vc_uni_pagedir_loc;
668
669 if (p)
670 p->readonly = rdonly;
671}
672
673/*
674 * Always use USER_MAP. These functions are used by the keyboard,
675 * which shouldn't be affected by G0/G1 switching, etc.
676 * If the user map still contains default values, i.e. the
677 * direct-to-font mapping, then assume user is using Latin1.
678 */
679/* may be called during an interrupt */
680u32 conv_8bit_to_uni(unsigned char c)
681{
682 unsigned short uni = translations[USER_MAP][c];
683 return uni == (0xf000 | c) ? c : uni;
684}
685
686int conv_uni_to_8bit(u32 uni)
687{
688 int c;
689 for (c = 0; c < 0x100; c++)
690 if (translations[USER_MAP][c] == uni ||
691 (translations[USER_MAP][c] == (c | 0xf000) && uni == c))
692 return c;
693 return -1;
694}
695
696int
697conv_uni_to_pc(struct vc_data *conp, long ucs)
698{
699 int h;
700 u16 **p1, *p2;
701 struct uni_pagedir *p;
702
703 /* Only 16-bit codes supported at this time */
704 if (ucs > 0xffff)
705 return -4; /* Not found */
706 else if (ucs < 0x20)
707 return -1; /* Not a printable character */
708 else if (ucs == 0xfeff || (ucs >= 0x200b && ucs <= 0x200f))
709 return -2; /* Zero-width space */
710 /*
711 * UNI_DIRECT_BASE indicates the start of the region in the User Zone
712 * which always has a 1:1 mapping to the currently loaded font. The
713 * UNI_DIRECT_MASK indicates the bit span of the region.
714 */
715 else if ((ucs & ~UNI_DIRECT_MASK) == UNI_DIRECT_BASE)
716 return ucs & UNI_DIRECT_MASK;
717
718 if (!*conp->vc_uni_pagedir_loc)
719 return -3;
720
721 p = (struct uni_pagedir *)*conp->vc_uni_pagedir_loc;
722 if ((p1 = p->uni_pgdir[ucs >> 11]) &&
723 (p2 = p1[(ucs >> 6) & 0x1f]) &&
724 (h = p2[ucs & 0x3f]) < MAX_GLYPH)
725 return h;
726
727 return -4; /* not found */
728}
729
730/*
731 * This is called at sys_setup time, after memory and the console are
732 * initialized. It must be possible to call kmalloc(..., GFP_KERNEL)
733 * from this function, hence the call from sys_setup.
734 */
735void __init
736console_map_init(void)
737{
738 int i;
739
740 for (i = 0; i < MAX_NR_CONSOLES; i++)
741 if (vc_cons_allocated(i) && !*vc_cons[i].d->vc_uni_pagedir_loc)
742 con_set_default_unimap(vc_cons[i].d);
743}
744
745EXPORT_SYMBOL(con_copy_unimap);
diff --git a/drivers/char/cp437.uni b/drivers/char/cp437.uni
deleted file mode 100644
index bc6163484f62..000000000000
--- a/drivers/char/cp437.uni
+++ /dev/null
@@ -1,291 +0,0 @@
1#
2# Unicode table for IBM Codepage 437. Note that there are many more
3# substitutions that could be conceived (for example, thick-line
4# graphs probably should be replaced with double-line ones, accented
5# Latin characters should replaced with their nonaccented versions,
6# and some upper case Greek characters could be replaced by Latin), however,
7# I have limited myself to the Unicodes used by the kernel ISO 8859-1,
8# DEC VT, and IBM CP 437 tables.
9#
10# --------------------------------
11#
12# Basic IBM dingbats, some of which will never have a purpose clear
13# to mankind
14#
150x00 U+0000
160x01 U+263a
170x02 U+263b
180x03 U+2665
190x04 U+2666 U+25c6
200x05 U+2663
210x06 U+2660
220x07 U+2022
230x08 U+25d8
240x09 U+25cb
250x0a U+25d9
260x0b U+2642
270x0c U+2640
280x0d U+266a
290x0e U+266b
300x0f U+263c U+00a4
310x10 U+25b6 U+25ba
320x11 U+25c0 U+25c4
330x12 U+2195
340x13 U+203c
350x14 U+00b6
360x15 U+00a7
370x16 U+25ac
380x17 U+21a8
390x18 U+2191
400x19 U+2193
410x1a U+2192
420x1b U+2190
430x1c U+221f
440x1d U+2194
450x1e U+25b2
460x1f U+25bc
47#
48# The ASCII range is identity-mapped, but some of the characters also
49# have to act as substitutes, especially the upper-case characters.
50#
510x20 U+0020
520x21 U+0021
530x22 U+0022 U+00a8
540x23 U+0023
550x24 U+0024
560x25 U+0025
570x26 U+0026
580x27 U+0027 U+00b4
590x28 U+0028
600x29 U+0029
610x2a U+002a
620x2b U+002b
630x2c U+002c U+00b8
640x2d U+002d U+00ad
650x2e U+002e
660x2f U+002f
670x30 U+0030
680x31 U+0031
690x32 U+0032
700x33 U+0033
710x34 U+0034
720x35 U+0035
730x36 U+0036
740x37 U+0037
750x38 U+0038
760x39 U+0039
770x3a U+003a
780x3b U+003b
790x3c U+003c
800x3d U+003d
810x3e U+003e
820x3f U+003f
830x40 U+0040
840x41 U+0041 U+00c0 U+00c1 U+00c2 U+00c3
850x42 U+0042
860x43 U+0043 U+00a9
870x44 U+0044 U+00d0
880x45 U+0045 U+00c8 U+00ca U+00cb
890x46 U+0046
900x47 U+0047
910x48 U+0048
920x49 U+0049 U+00cc U+00cd U+00ce U+00cf
930x4a U+004a
940x4b U+004b U+212a
950x4c U+004c
960x4d U+004d
970x4e U+004e
980x4f U+004f U+00d2 U+00d3 U+00d4 U+00d5
990x50 U+0050
1000x51 U+0051
1010x52 U+0052 U+00ae
1020x53 U+0053
1030x54 U+0054
1040x55 U+0055 U+00d9 U+00da U+00db
1050x56 U+0056
1060x57 U+0057
1070x58 U+0058
1080x59 U+0059 U+00dd
1090x5a U+005a
1100x5b U+005b
1110x5c U+005c
1120x5d U+005d
1130x5e U+005e
1140x5f U+005f U+23bd U+f804
1150x60 U+0060
1160x61 U+0061 U+00e3
1170x62 U+0062
1180x63 U+0063
1190x64 U+0064
1200x65 U+0065
1210x66 U+0066
1220x67 U+0067
1230x68 U+0068
1240x69 U+0069
1250x6a U+006a
1260x6b U+006b
1270x6c U+006c
1280x6d U+006d
1290x6e U+006e
1300x6f U+006f U+00f5
1310x70 U+0070
1320x71 U+0071
1330x72 U+0072
1340x73 U+0073
1350x74 U+0074
1360x75 U+0075
1370x76 U+0076
1380x77 U+0077
1390x78 U+0078 U+00d7
1400x79 U+0079 U+00fd
1410x7a U+007a
1420x7b U+007b
1430x7c U+007c U+00a6
1440x7d U+007d
1450x7e U+007e
146#
147# Okay, what on Earth is this one supposed to be used for?
148#
1490x7f U+2302
150#
151# Non-English characters, mostly lower case letters...
152#
1530x80 U+00c7
1540x81 U+00fc
1550x82 U+00e9
1560x83 U+00e2
1570x84 U+00e4
1580x85 U+00e0
1590x86 U+00e5
1600x87 U+00e7
1610x88 U+00ea
1620x89 U+00eb
1630x8a U+00e8
1640x8b U+00ef
1650x8c U+00ee
1660x8d U+00ec
1670x8e U+00c4
1680x8f U+00c5 U+212b
1690x90 U+00c9
1700x91 U+00e6
1710x92 U+00c6
1720x93 U+00f4
1730x94 U+00f6
1740x95 U+00f2
1750x96 U+00fb
1760x97 U+00f9
1770x98 U+00ff
1780x99 U+00d6
1790x9a U+00dc
1800x9b U+00a2
1810x9c U+00a3
1820x9d U+00a5
1830x9e U+20a7
1840x9f U+0192
1850xa0 U+00e1
1860xa1 U+00ed
1870xa2 U+00f3
1880xa3 U+00fa
1890xa4 U+00f1
1900xa5 U+00d1
1910xa6 U+00aa
1920xa7 U+00ba
1930xa8 U+00bf
1940xa9 U+2310
1950xaa U+00ac
1960xab U+00bd
1970xac U+00bc
1980xad U+00a1
1990xae U+00ab
2000xaf U+00bb
201#
202# Block graphics
203#
2040xb0 U+2591
2050xb1 U+2592
2060xb2 U+2593
2070xb3 U+2502
2080xb4 U+2524
2090xb5 U+2561
2100xb6 U+2562
2110xb7 U+2556
2120xb8 U+2555
2130xb9 U+2563
2140xba U+2551
2150xbb U+2557
2160xbc U+255d
2170xbd U+255c
2180xbe U+255b
2190xbf U+2510
2200xc0 U+2514
2210xc1 U+2534
2220xc2 U+252c
2230xc3 U+251c
2240xc4 U+2500
2250xc5 U+253c
2260xc6 U+255e
2270xc7 U+255f
2280xc8 U+255a
2290xc9 U+2554
2300xca U+2569
2310xcb U+2566
2320xcc U+2560
2330xcd U+2550
2340xce U+256c
2350xcf U+2567
2360xd0 U+2568
2370xd1 U+2564
2380xd2 U+2565
2390xd3 U+2559
2400xd4 U+2558
2410xd5 U+2552
2420xd6 U+2553
2430xd7 U+256b
2440xd8 U+256a
2450xd9 U+2518
2460xda U+250c
2470xdb U+2588
2480xdc U+2584
2490xdd U+258c
2500xde U+2590
2510xdf U+2580
252#
253# Greek letters and mathematical symbols
254#
2550xe0 U+03b1
2560xe1 U+03b2 U+00df
2570xe2 U+0393
2580xe3 U+03c0
2590xe4 U+03a3
2600xe5 U+03c3
2610xe6 U+00b5 U+03bc
2620xe7 U+03c4
2630xe8 U+03a6 U+00d8
2640xe9 U+0398
2650xea U+03a9 U+2126
2660xeb U+03b4 U+00f0
2670xec U+221e
2680xed U+03c6 U+00f8
2690xee U+03b5 U+2208
2700xef U+2229
2710xf0 U+2261
2720xf1 U+00b1
2730xf2 U+2265
2740xf3 U+2264
2750xf4 U+2320
2760xf5 U+2321
2770xf6 U+00f7
2780xf7 U+2248
2790xf8 U+00b0
2800xf9 U+2219
2810xfa U+00b7
2820xfb U+221a
2830xfc U+207f
2840xfd U+00b2
285#
286# Square bullet, non-spacing blank
287# Mapping U+fffd to the square bullet means it is the substitution
288# character
289#
2900xfe U+25a0 U+fffd
2910xff U+00a0
diff --git a/drivers/char/cs5535_gpio.c b/drivers/char/cs5535_gpio.c
deleted file mode 100644
index 4d830dc482ef..000000000000
--- a/drivers/char/cs5535_gpio.c
+++ /dev/null
@@ -1,258 +0,0 @@
1/*
2 * AMD CS5535/CS5536 GPIO driver.
3 * Allows a user space process to play with the GPIO pins.
4 *
5 * Copyright (c) 2005 Ben Gardner <bgardner@wabtec.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the smems of the GNU General Public License as published by
9 * the Free Software Foundation; version 2 of the License.
10 */
11
12#include <linux/fs.h>
13#include <linux/module.h>
14#include <linux/errno.h>
15#include <linux/kernel.h>
16#include <linux/init.h>
17#include <linux/cdev.h>
18#include <linux/ioport.h>
19#include <linux/pci.h>
20
21#include <asm/uaccess.h>
22#include <asm/io.h>
23
24
25#define NAME "cs5535_gpio"
26
27MODULE_AUTHOR("Ben Gardner <bgardner@wabtec.com>");
28MODULE_DESCRIPTION("AMD CS5535/CS5536 GPIO Pin Driver");
29MODULE_LICENSE("GPL");
30
31static int major;
32module_param(major, int, 0);
33MODULE_PARM_DESC(major, "Major device number");
34
35static ulong mask;
36module_param(mask, ulong, 0);
37MODULE_PARM_DESC(mask, "GPIO channel mask");
38
39#define MSR_LBAR_GPIO 0x5140000C
40
41static u32 gpio_base;
42
43static struct pci_device_id divil_pci[] = {
44 { PCI_DEVICE(PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_CS5535_ISA) },
45 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_CS5536_ISA) },
46 { } /* NULL entry */
47};
48MODULE_DEVICE_TABLE(pci, divil_pci);
49
50static struct cdev cs5535_gpio_cdev;
51
52/* reserve 32 entries even though some aren't usable */
53#define CS5535_GPIO_COUNT 32
54
55/* IO block size */
56#define CS5535_GPIO_SIZE 256
57
58struct gpio_regmap {
59 u32 rd_offset;
60 u32 wr_offset;
61 char on;
62 char off;
63};
64static struct gpio_regmap rm[] =
65{
66 { 0x30, 0x00, '1', '0' }, /* GPIOx_READ_BACK / GPIOx_OUT_VAL */
67 { 0x20, 0x20, 'I', 'i' }, /* GPIOx_IN_EN */
68 { 0x04, 0x04, 'O', 'o' }, /* GPIOx_OUT_EN */
69 { 0x08, 0x08, 't', 'T' }, /* GPIOx_OUT_OD_EN */
70 { 0x18, 0x18, 'P', 'p' }, /* GPIOx_OUT_PU_EN */
71 { 0x1c, 0x1c, 'D', 'd' }, /* GPIOx_OUT_PD_EN */
72};
73
74
75/**
76 * Gets the register offset for the GPIO bank.
77 * Low (0-15) starts at 0x00, high (16-31) starts at 0x80
78 */
79static inline u32 cs5535_lowhigh_base(int reg)
80{
81 return (reg & 0x10) << 3;
82}
83
84static ssize_t cs5535_gpio_write(struct file *file, const char __user *data,
85 size_t len, loff_t *ppos)
86{
87 u32 m = iminor(file->f_path.dentry->d_inode);
88 int i, j;
89 u32 base = gpio_base + cs5535_lowhigh_base(m);
90 u32 m0, m1;
91 char c;
92
93 /**
94 * Creates the mask for atomic bit programming.
95 * The high 16 bits and the low 16 bits are used to set the mask.
96 * For example, GPIO 15 maps to 31,15: 0,1 => On; 1,0=> Off
97 */
98 m1 = 1 << (m & 0x0F);
99 m0 = m1 << 16;
100
101 for (i = 0; i < len; ++i) {
102 if (get_user(c, data+i))
103 return -EFAULT;
104
105 for (j = 0; j < ARRAY_SIZE(rm); j++) {
106 if (c == rm[j].on) {
107 outl(m1, base + rm[j].wr_offset);
108 /* If enabling output, turn off AUX 1 and AUX 2 */
109 if (c == 'O') {
110 outl(m0, base + 0x10);
111 outl(m0, base + 0x14);
112 }
113 break;
114 } else if (c == rm[j].off) {
115 outl(m0, base + rm[j].wr_offset);
116 break;
117 }
118 }
119 }
120 *ppos = 0;
121 return len;
122}
123
124static ssize_t cs5535_gpio_read(struct file *file, char __user *buf,
125 size_t len, loff_t *ppos)
126{
127 u32 m = iminor(file->f_path.dentry->d_inode);
128 u32 base = gpio_base + cs5535_lowhigh_base(m);
129 int rd_bit = 1 << (m & 0x0f);
130 int i;
131 char ch;
132 ssize_t count = 0;
133
134 if (*ppos >= ARRAY_SIZE(rm))
135 return 0;
136
137 for (i = *ppos; (i < (*ppos + len)) && (i < ARRAY_SIZE(rm)); i++) {
138 ch = (inl(base + rm[i].rd_offset) & rd_bit) ?
139 rm[i].on : rm[i].off;
140
141 if (put_user(ch, buf+count))
142 return -EFAULT;
143
144 count++;
145 }
146
147 /* add a line-feed if there is room */
148 if ((i == ARRAY_SIZE(rm)) && (count < len)) {
149 put_user('\n', buf + count);
150 count++;
151 }
152
153 *ppos += count;
154 return count;
155}
156
157static int cs5535_gpio_open(struct inode *inode, struct file *file)
158{
159 u32 m = iminor(inode);
160
161 /* the mask says which pins are usable by this driver */
162 if ((mask & (1 << m)) == 0)
163 return -EINVAL;
164
165 return nonseekable_open(inode, file);
166}
167
168static const struct file_operations cs5535_gpio_fops = {
169 .owner = THIS_MODULE,
170 .write = cs5535_gpio_write,
171 .read = cs5535_gpio_read,
172 .open = cs5535_gpio_open
173};
174
175static int __init cs5535_gpio_init(void)
176{
177 dev_t dev_id;
178 u32 low, hi;
179 int retval;
180
181 if (pci_dev_present(divil_pci) == 0) {
182 printk(KERN_WARNING NAME ": DIVIL not found\n");
183 return -ENODEV;
184 }
185
186 /* Grab the GPIO I/O range */
187 rdmsr(MSR_LBAR_GPIO, low, hi);
188
189 /* Check the mask and whether GPIO is enabled (sanity check) */
190 if (hi != 0x0000f001) {
191 printk(KERN_WARNING NAME ": GPIO not enabled\n");
192 return -ENODEV;
193 }
194
195 /* Mask off the IO base address */
196 gpio_base = low & 0x0000ff00;
197
198 /**
199 * Some GPIO pins
200 * 31-29,23 : reserved (always mask out)
201 * 28 : Power Button
202 * 26 : PME#
203 * 22-16 : LPC
204 * 14,15 : SMBus
205 * 9,8 : UART1
206 * 7 : PCI INTB
207 * 3,4 : UART2/DDC
208 * 2 : IDE_IRQ0
209 * 0 : PCI INTA
210 *
211 * If a mask was not specified, be conservative and only allow:
212 * 1,2,5,6,10-13,24,25,27
213 */
214 if (mask != 0)
215 mask &= 0x1f7fffff;
216 else
217 mask = 0x0b003c66;
218
219 if (!request_region(gpio_base, CS5535_GPIO_SIZE, NAME)) {
220 printk(KERN_ERR NAME ": can't allocate I/O for GPIO\n");
221 return -ENODEV;
222 }
223
224 if (major) {
225 dev_id = MKDEV(major, 0);
226 retval = register_chrdev_region(dev_id, CS5535_GPIO_COUNT,
227 NAME);
228 } else {
229 retval = alloc_chrdev_region(&dev_id, 0, CS5535_GPIO_COUNT,
230 NAME);
231 major = MAJOR(dev_id);
232 }
233
234 if (retval) {
235 release_region(gpio_base, CS5535_GPIO_SIZE);
236 return -1;
237 }
238
239 printk(KERN_DEBUG NAME ": base=%#x mask=%#lx major=%d\n",
240 gpio_base, mask, major);
241
242 cdev_init(&cs5535_gpio_cdev, &cs5535_gpio_fops);
243 cdev_add(&cs5535_gpio_cdev, dev_id, CS5535_GPIO_COUNT);
244
245 return 0;
246}
247
248static void __exit cs5535_gpio_cleanup(void)
249{
250 dev_t dev_id = MKDEV(major, 0);
251
252 cdev_del(&cs5535_gpio_cdev);
253 unregister_chrdev_region(dev_id, CS5535_GPIO_COUNT);
254 release_region(gpio_base, CS5535_GPIO_SIZE);
255}
256
257module_init(cs5535_gpio_init);
258module_exit(cs5535_gpio_cleanup);
diff --git a/drivers/char/cyclades.c b/drivers/char/cyclades.c
deleted file mode 100644
index 27aad9422332..000000000000
--- a/drivers/char/cyclades.c
+++ /dev/null
@@ -1,4197 +0,0 @@
1#undef BLOCKMOVE
2#define Z_WAKE
3#undef Z_EXT_CHARS_IN_BUFFER
4
5/*
6 * linux/drivers/char/cyclades.c
7 *
8 * This file contains the driver for the Cyclades async multiport
9 * serial boards.
10 *
11 * Initially written by Randolph Bentson <bentson@grieg.seaslug.org>.
12 * Modified and maintained by Marcio Saito <marcio@cyclades.com>.
13 *
14 * Copyright (C) 2007-2009 Jiri Slaby <jirislaby@gmail.com>
15 *
16 * Much of the design and some of the code came from serial.c
17 * which was copyright (C) 1991, 1992 Linus Torvalds. It was
18 * extensively rewritten by Theodore Ts'o, 8/16/92 -- 9/14/92,
19 * and then fixed as suggested by Michael K. Johnson 12/12/92.
20 * Converted to pci probing and cleaned up by Jiri Slaby.
21 *
22 */
23
24#define CY_VERSION "2.6"
25
26/* If you need to install more boards than NR_CARDS, change the constant
27 in the definition below. No other change is necessary to support up to
28 eight boards. Beyond that you'll have to extend cy_isa_addresses. */
29
30#define NR_CARDS 4
31
32/*
33 If the total number of ports is larger than NR_PORTS, change this
34 constant in the definition below. No other change is necessary to
35 support more boards/ports. */
36
37#define NR_PORTS 256
38
39#define ZO_V1 0
40#define ZO_V2 1
41#define ZE_V1 2
42
43#define SERIAL_PARANOIA_CHECK
44#undef CY_DEBUG_OPEN
45#undef CY_DEBUG_THROTTLE
46#undef CY_DEBUG_OTHER
47#undef CY_DEBUG_IO
48#undef CY_DEBUG_COUNT
49#undef CY_DEBUG_DTR
50#undef CY_DEBUG_WAIT_UNTIL_SENT
51#undef CY_DEBUG_INTERRUPTS
52#undef CY_16Y_HACK
53#undef CY_ENABLE_MONITORING
54#undef CY_PCI_DEBUG
55
56/*
57 * Include section
58 */
59#include <linux/module.h>
60#include <linux/errno.h>
61#include <linux/signal.h>
62#include <linux/sched.h>
63#include <linux/timer.h>
64#include <linux/interrupt.h>
65#include <linux/tty.h>
66#include <linux/tty_flip.h>
67#include <linux/serial.h>
68#include <linux/major.h>
69#include <linux/string.h>
70#include <linux/fcntl.h>
71#include <linux/ptrace.h>
72#include <linux/cyclades.h>
73#include <linux/mm.h>
74#include <linux/ioport.h>
75#include <linux/init.h>
76#include <linux/delay.h>
77#include <linux/spinlock.h>
78#include <linux/bitops.h>
79#include <linux/firmware.h>
80#include <linux/device.h>
81#include <linux/slab.h>
82
83#include <linux/io.h>
84#include <linux/uaccess.h>
85
86#include <linux/kernel.h>
87#include <linux/pci.h>
88
89#include <linux/stat.h>
90#include <linux/proc_fs.h>
91#include <linux/seq_file.h>
92
93static void cy_send_xchar(struct tty_struct *tty, char ch);
94
95#ifndef SERIAL_XMIT_SIZE
96#define SERIAL_XMIT_SIZE (min(PAGE_SIZE, 4096))
97#endif
98
99#define STD_COM_FLAGS (0)
100
101/* firmware stuff */
102#define ZL_MAX_BLOCKS 16
103#define DRIVER_VERSION 0x02010203
104#define RAM_SIZE 0x80000
105
106enum zblock_type {
107 ZBLOCK_PRG = 0,
108 ZBLOCK_FPGA = 1
109};
110
111struct zfile_header {
112 char name[64];
113 char date[32];
114 char aux[32];
115 u32 n_config;
116 u32 config_offset;
117 u32 n_blocks;
118 u32 block_offset;
119 u32 reserved[9];
120} __attribute__ ((packed));
121
122struct zfile_config {
123 char name[64];
124 u32 mailbox;
125 u32 function;
126 u32 n_blocks;
127 u32 block_list[ZL_MAX_BLOCKS];
128} __attribute__ ((packed));
129
130struct zfile_block {
131 u32 type;
132 u32 file_offset;
133 u32 ram_offset;
134 u32 size;
135} __attribute__ ((packed));
136
137static struct tty_driver *cy_serial_driver;
138
139#ifdef CONFIG_ISA
140/* This is the address lookup table. The driver will probe for
141 Cyclom-Y/ISA boards at all addresses in here. If you want the
142 driver to probe addresses at a different address, add it to
143 this table. If the driver is probing some other board and
144 causing problems, remove the offending address from this table.
145*/
146
147static unsigned int cy_isa_addresses[] = {
148 0xD0000,
149 0xD2000,
150 0xD4000,
151 0xD6000,
152 0xD8000,
153 0xDA000,
154 0xDC000,
155 0xDE000,
156 0, 0, 0, 0, 0, 0, 0, 0
157};
158
159#define NR_ISA_ADDRS ARRAY_SIZE(cy_isa_addresses)
160
161static long maddr[NR_CARDS];
162static int irq[NR_CARDS];
163
164module_param_array(maddr, long, NULL, 0);
165module_param_array(irq, int, NULL, 0);
166
167#endif /* CONFIG_ISA */
168
169/* This is the per-card data structure containing address, irq, number of
170 channels, etc. This driver supports a maximum of NR_CARDS cards.
171*/
172static struct cyclades_card cy_card[NR_CARDS];
173
174static int cy_next_channel; /* next minor available */
175
176/*
177 * This is used to look up the divisor speeds and the timeouts
178 * We're normally limited to 15 distinct baud rates. The extra
179 * are accessed via settings in info->port.flags.
180 * 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
181 * 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
182 * HI VHI
183 * 20
184 */
185static const int baud_table[] = {
186 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200,
187 1800, 2400, 4800, 9600, 19200, 38400, 57600, 76800, 115200, 150000,
188 230400, 0
189};
190
191static const char baud_co_25[] = { /* 25 MHz clock option table */
192 /* value => 00 01 02 03 04 */
193 /* divide by 8 32 128 512 2048 */
194 0x00, 0x04, 0x04, 0x04, 0x04, 0x04, 0x03, 0x03, 0x03, 0x02,
195 0x02, 0x02, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
196};
197
198static const char baud_bpr_25[] = { /* 25 MHz baud rate period table */
199 0x00, 0xf5, 0xa3, 0x6f, 0x5c, 0x51, 0xf5, 0xa3, 0x51, 0xa3,
200 0x6d, 0x51, 0xa3, 0x51, 0xa3, 0x51, 0x36, 0x29, 0x1b, 0x15
201};
202
203static const char baud_co_60[] = { /* 60 MHz clock option table (CD1400 J) */
204 /* value => 00 01 02 03 04 */
205 /* divide by 8 32 128 512 2048 */
206 0x00, 0x00, 0x00, 0x04, 0x04, 0x04, 0x04, 0x04, 0x03, 0x03,
207 0x03, 0x02, 0x02, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
208 0x00
209};
210
211static const char baud_bpr_60[] = { /* 60 MHz baud rate period table (CD1400 J) */
212 0x00, 0x82, 0x21, 0xff, 0xdb, 0xc3, 0x92, 0x62, 0xc3, 0x62,
213 0x41, 0xc3, 0x62, 0xc3, 0x62, 0xc3, 0x82, 0x62, 0x41, 0x32,
214 0x21
215};
216
217static const char baud_cor3[] = { /* receive threshold */
218 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a,
219 0x0a, 0x0a, 0x0a, 0x09, 0x09, 0x08, 0x08, 0x08, 0x08, 0x07,
220 0x07
221};
222
223/*
224 * The Cyclades driver implements HW flow control as any serial driver.
225 * The cyclades_port structure member rflow and the vector rflow_thr
226 * allows us to take advantage of a special feature in the CD1400 to avoid
227 * data loss even when the system interrupt latency is too high. These flags
228 * are to be used only with very special applications. Setting these flags
229 * requires the use of a special cable (DTR and RTS reversed). In the new
230 * CD1400-based boards (rev. 6.00 or later), there is no need for special
231 * cables.
232 */
233
234static const char rflow_thr[] = { /* rflow threshold */
235 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
236 0x00, 0x00, 0x00, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a,
237 0x0a
238};
239
240/* The Cyclom-Ye has placed the sequential chips in non-sequential
241 * address order. This look-up table overcomes that problem.
242 */
243static const unsigned int cy_chip_offset[] = { 0x0000,
244 0x0400,
245 0x0800,
246 0x0C00,
247 0x0200,
248 0x0600,
249 0x0A00,
250 0x0E00
251};
252
253/* PCI related definitions */
254
255#ifdef CONFIG_PCI
256static const struct pci_device_id cy_pci_dev_id[] = {
257 /* PCI < 1Mb */
258 { PCI_DEVICE(PCI_VENDOR_ID_CYCLADES, PCI_DEVICE_ID_CYCLOM_Y_Lo) },
259 /* PCI > 1Mb */
260 { PCI_DEVICE(PCI_VENDOR_ID_CYCLADES, PCI_DEVICE_ID_CYCLOM_Y_Hi) },
261 /* 4Y PCI < 1Mb */
262 { PCI_DEVICE(PCI_VENDOR_ID_CYCLADES, PCI_DEVICE_ID_CYCLOM_4Y_Lo) },
263 /* 4Y PCI > 1Mb */
264 { PCI_DEVICE(PCI_VENDOR_ID_CYCLADES, PCI_DEVICE_ID_CYCLOM_4Y_Hi) },
265 /* 8Y PCI < 1Mb */
266 { PCI_DEVICE(PCI_VENDOR_ID_CYCLADES, PCI_DEVICE_ID_CYCLOM_8Y_Lo) },
267 /* 8Y PCI > 1Mb */
268 { PCI_DEVICE(PCI_VENDOR_ID_CYCLADES, PCI_DEVICE_ID_CYCLOM_8Y_Hi) },
269 /* Z PCI < 1Mb */
270 { PCI_DEVICE(PCI_VENDOR_ID_CYCLADES, PCI_DEVICE_ID_CYCLOM_Z_Lo) },
271 /* Z PCI > 1Mb */
272 { PCI_DEVICE(PCI_VENDOR_ID_CYCLADES, PCI_DEVICE_ID_CYCLOM_Z_Hi) },
273 { } /* end of table */
274};
275MODULE_DEVICE_TABLE(pci, cy_pci_dev_id);
276#endif
277
278static void cy_start(struct tty_struct *);
279static void cy_set_line_char(struct cyclades_port *, struct tty_struct *);
280static int cyz_issue_cmd(struct cyclades_card *, __u32, __u8, __u32);
281#ifdef CONFIG_ISA
282static unsigned detect_isa_irq(void __iomem *);
283#endif /* CONFIG_ISA */
284
285#ifndef CONFIG_CYZ_INTR
286static void cyz_poll(unsigned long);
287
288/* The Cyclades-Z polling cycle is defined by this variable */
289static long cyz_polling_cycle = CZ_DEF_POLL;
290
291static DEFINE_TIMER(cyz_timerlist, cyz_poll, 0, 0);
292
293#else /* CONFIG_CYZ_INTR */
294static void cyz_rx_restart(unsigned long);
295static struct timer_list cyz_rx_full_timer[NR_PORTS];
296#endif /* CONFIG_CYZ_INTR */
297
298static inline void cyy_writeb(struct cyclades_port *port, u32 reg, u8 val)
299{
300 struct cyclades_card *card = port->card;
301
302 cy_writeb(port->u.cyy.base_addr + (reg << card->bus_index), val);
303}
304
305static inline u8 cyy_readb(struct cyclades_port *port, u32 reg)
306{
307 struct cyclades_card *card = port->card;
308
309 return readb(port->u.cyy.base_addr + (reg << card->bus_index));
310}
311
312static inline bool cy_is_Z(struct cyclades_card *card)
313{
314 return card->num_chips == (unsigned int)-1;
315}
316
317static inline bool __cyz_fpga_loaded(struct RUNTIME_9060 __iomem *ctl_addr)
318{
319 return readl(&ctl_addr->init_ctrl) & (1 << 17);
320}
321
322static inline bool cyz_fpga_loaded(struct cyclades_card *card)
323{
324 return __cyz_fpga_loaded(card->ctl_addr.p9060);
325}
326
327static inline bool cyz_is_loaded(struct cyclades_card *card)
328{
329 struct FIRM_ID __iomem *fw_id = card->base_addr + ID_ADDRESS;
330
331 return (card->hw_ver == ZO_V1 || cyz_fpga_loaded(card)) &&
332 readl(&fw_id->signature) == ZFIRM_ID;
333}
334
335static inline int serial_paranoia_check(struct cyclades_port *info,
336 const char *name, const char *routine)
337{
338#ifdef SERIAL_PARANOIA_CHECK
339 if (!info) {
340 printk(KERN_WARNING "cyc Warning: null cyclades_port for (%s) "
341 "in %s\n", name, routine);
342 return 1;
343 }
344
345 if (info->magic != CYCLADES_MAGIC) {
346 printk(KERN_WARNING "cyc Warning: bad magic number for serial "
347 "struct (%s) in %s\n", name, routine);
348 return 1;
349 }
350#endif
351 return 0;
352}
353
354/***********************************************************/
355/********* Start of block of Cyclom-Y specific code ********/
356
357/* This routine waits up to 1000 micro-seconds for the previous
358 command to the Cirrus chip to complete and then issues the
359 new command. An error is returned if the previous command
360 didn't finish within the time limit.
361
362 This function is only called from inside spinlock-protected code.
363 */
364static int __cyy_issue_cmd(void __iomem *base_addr, u8 cmd, int index)
365{
366 void __iomem *ccr = base_addr + (CyCCR << index);
367 unsigned int i;
368
369 /* Check to see that the previous command has completed */
370 for (i = 0; i < 100; i++) {
371 if (readb(ccr) == 0)
372 break;
373 udelay(10L);
374 }
375 /* if the CCR never cleared, the previous command
376 didn't finish within the "reasonable time" */
377 if (i == 100)
378 return -1;
379
380 /* Issue the new command */
381 cy_writeb(ccr, cmd);
382
383 return 0;
384}
385
386static inline int cyy_issue_cmd(struct cyclades_port *port, u8 cmd)
387{
388 return __cyy_issue_cmd(port->u.cyy.base_addr, cmd,
389 port->card->bus_index);
390}
391
392#ifdef CONFIG_ISA
393/* ISA interrupt detection code */
394static unsigned detect_isa_irq(void __iomem *address)
395{
396 int irq;
397 unsigned long irqs, flags;
398 int save_xir, save_car;
399 int index = 0; /* IRQ probing is only for ISA */
400
401 /* forget possible initially masked and pending IRQ */
402 irq = probe_irq_off(probe_irq_on());
403
404 /* Clear interrupts on the board first */
405 cy_writeb(address + (Cy_ClrIntr << index), 0);
406 /* Cy_ClrIntr is 0x1800 */
407
408 irqs = probe_irq_on();
409 /* Wait ... */
410 msleep(5);
411
412 /* Enable the Tx interrupts on the CD1400 */
413 local_irq_save(flags);
414 cy_writeb(address + (CyCAR << index), 0);
415 __cyy_issue_cmd(address, CyCHAN_CTL | CyENB_XMTR, index);
416
417 cy_writeb(address + (CyCAR << index), 0);
418 cy_writeb(address + (CySRER << index),
419 readb(address + (CySRER << index)) | CyTxRdy);
420 local_irq_restore(flags);
421
422 /* Wait ... */
423 msleep(5);
424
425 /* Check which interrupt is in use */
426 irq = probe_irq_off(irqs);
427
428 /* Clean up */
429 save_xir = (u_char) readb(address + (CyTIR << index));
430 save_car = readb(address + (CyCAR << index));
431 cy_writeb(address + (CyCAR << index), (save_xir & 0x3));
432 cy_writeb(address + (CySRER << index),
433 readb(address + (CySRER << index)) & ~CyTxRdy);
434 cy_writeb(address + (CyTIR << index), (save_xir & 0x3f));
435 cy_writeb(address + (CyCAR << index), (save_car));
436 cy_writeb(address + (Cy_ClrIntr << index), 0);
437 /* Cy_ClrIntr is 0x1800 */
438
439 return (irq > 0) ? irq : 0;
440}
441#endif /* CONFIG_ISA */
442
443static void cyy_chip_rx(struct cyclades_card *cinfo, int chip,
444 void __iomem *base_addr)
445{
446 struct cyclades_port *info;
447 struct tty_struct *tty;
448 int len, index = cinfo->bus_index;
449 u8 ivr, save_xir, channel, save_car, data, char_count;
450
451#ifdef CY_DEBUG_INTERRUPTS
452 printk(KERN_DEBUG "cyy_interrupt: rcvd intr, chip %d\n", chip);
453#endif
454 /* determine the channel & change to that context */
455 save_xir = readb(base_addr + (CyRIR << index));
456 channel = save_xir & CyIRChannel;
457 info = &cinfo->ports[channel + chip * 4];
458 save_car = cyy_readb(info, CyCAR);
459 cyy_writeb(info, CyCAR, save_xir);
460 ivr = cyy_readb(info, CyRIVR) & CyIVRMask;
461
462 tty = tty_port_tty_get(&info->port);
463 /* if there is nowhere to put the data, discard it */
464 if (tty == NULL) {
465 if (ivr == CyIVRRxEx) { /* exception */
466 data = cyy_readb(info, CyRDSR);
467 } else { /* normal character reception */
468 char_count = cyy_readb(info, CyRDCR);
469 while (char_count--)
470 data = cyy_readb(info, CyRDSR);
471 }
472 goto end;
473 }
474 /* there is an open port for this data */
475 if (ivr == CyIVRRxEx) { /* exception */
476 data = cyy_readb(info, CyRDSR);
477
478 /* For statistics only */
479 if (data & CyBREAK)
480 info->icount.brk++;
481 else if (data & CyFRAME)
482 info->icount.frame++;
483 else if (data & CyPARITY)
484 info->icount.parity++;
485 else if (data & CyOVERRUN)
486 info->icount.overrun++;
487
488 if (data & info->ignore_status_mask) {
489 info->icount.rx++;
490 tty_kref_put(tty);
491 return;
492 }
493 if (tty_buffer_request_room(tty, 1)) {
494 if (data & info->read_status_mask) {
495 if (data & CyBREAK) {
496 tty_insert_flip_char(tty,
497 cyy_readb(info, CyRDSR),
498 TTY_BREAK);
499 info->icount.rx++;
500 if (info->port.flags & ASYNC_SAK)
501 do_SAK(tty);
502 } else if (data & CyFRAME) {
503 tty_insert_flip_char(tty,
504 cyy_readb(info, CyRDSR),
505 TTY_FRAME);
506 info->icount.rx++;
507 info->idle_stats.frame_errs++;
508 } else if (data & CyPARITY) {
509 /* Pieces of seven... */
510 tty_insert_flip_char(tty,
511 cyy_readb(info, CyRDSR),
512 TTY_PARITY);
513 info->icount.rx++;
514 info->idle_stats.parity_errs++;
515 } else if (data & CyOVERRUN) {
516 tty_insert_flip_char(tty, 0,
517 TTY_OVERRUN);
518 info->icount.rx++;
519 /* If the flip buffer itself is
520 overflowing, we still lose
521 the next incoming character.
522 */
523 tty_insert_flip_char(tty,
524 cyy_readb(info, CyRDSR),
525 TTY_FRAME);
526 info->icount.rx++;
527 info->idle_stats.overruns++;
528 /* These two conditions may imply */
529 /* a normal read should be done. */
530 /* } else if(data & CyTIMEOUT) { */
531 /* } else if(data & CySPECHAR) { */
532 } else {
533 tty_insert_flip_char(tty, 0,
534 TTY_NORMAL);
535 info->icount.rx++;
536 }
537 } else {
538 tty_insert_flip_char(tty, 0, TTY_NORMAL);
539 info->icount.rx++;
540 }
541 } else {
542 /* there was a software buffer overrun and nothing
543 * could be done about it!!! */
544 info->icount.buf_overrun++;
545 info->idle_stats.overruns++;
546 }
547 } else { /* normal character reception */
548 /* load # chars available from the chip */
549 char_count = cyy_readb(info, CyRDCR);
550
551#ifdef CY_ENABLE_MONITORING
552 ++info->mon.int_count;
553 info->mon.char_count += char_count;
554 if (char_count > info->mon.char_max)
555 info->mon.char_max = char_count;
556 info->mon.char_last = char_count;
557#endif
558 len = tty_buffer_request_room(tty, char_count);
559 while (len--) {
560 data = cyy_readb(info, CyRDSR);
561 tty_insert_flip_char(tty, data, TTY_NORMAL);
562 info->idle_stats.recv_bytes++;
563 info->icount.rx++;
564#ifdef CY_16Y_HACK
565 udelay(10L);
566#endif
567 }
568 info->idle_stats.recv_idle = jiffies;
569 }
570 tty_schedule_flip(tty);
571 tty_kref_put(tty);
572end:
573 /* end of service */
574 cyy_writeb(info, CyRIR, save_xir & 0x3f);
575 cyy_writeb(info, CyCAR, save_car);
576}
577
578static void cyy_chip_tx(struct cyclades_card *cinfo, unsigned int chip,
579 void __iomem *base_addr)
580{
581 struct cyclades_port *info;
582 struct tty_struct *tty;
583 int char_count, index = cinfo->bus_index;
584 u8 save_xir, channel, save_car, outch;
585
586 /* Since we only get here when the transmit buffer
587 is empty, we know we can always stuff a dozen
588 characters. */
589#ifdef CY_DEBUG_INTERRUPTS
590 printk(KERN_DEBUG "cyy_interrupt: xmit intr, chip %d\n", chip);
591#endif
592
593 /* determine the channel & change to that context */
594 save_xir = readb(base_addr + (CyTIR << index));
595 channel = save_xir & CyIRChannel;
596 save_car = readb(base_addr + (CyCAR << index));
597 cy_writeb(base_addr + (CyCAR << index), save_xir);
598
599 info = &cinfo->ports[channel + chip * 4];
600 tty = tty_port_tty_get(&info->port);
601 if (tty == NULL) {
602 cyy_writeb(info, CySRER, cyy_readb(info, CySRER) & ~CyTxRdy);
603 goto end;
604 }
605
606 /* load the on-chip space for outbound data */
607 char_count = info->xmit_fifo_size;
608
609 if (info->x_char) { /* send special char */
610 outch = info->x_char;
611 cyy_writeb(info, CyTDR, outch);
612 char_count--;
613 info->icount.tx++;
614 info->x_char = 0;
615 }
616
617 if (info->breakon || info->breakoff) {
618 if (info->breakon) {
619 cyy_writeb(info, CyTDR, 0);
620 cyy_writeb(info, CyTDR, 0x81);
621 info->breakon = 0;
622 char_count -= 2;
623 }
624 if (info->breakoff) {
625 cyy_writeb(info, CyTDR, 0);
626 cyy_writeb(info, CyTDR, 0x83);
627 info->breakoff = 0;
628 char_count -= 2;
629 }
630 }
631
632 while (char_count-- > 0) {
633 if (!info->xmit_cnt) {
634 if (cyy_readb(info, CySRER) & CyTxMpty) {
635 cyy_writeb(info, CySRER,
636 cyy_readb(info, CySRER) & ~CyTxMpty);
637 } else {
638 cyy_writeb(info, CySRER, CyTxMpty |
639 (cyy_readb(info, CySRER) & ~CyTxRdy));
640 }
641 goto done;
642 }
643 if (info->port.xmit_buf == NULL) {
644 cyy_writeb(info, CySRER,
645 cyy_readb(info, CySRER) & ~CyTxRdy);
646 goto done;
647 }
648 if (tty->stopped || tty->hw_stopped) {
649 cyy_writeb(info, CySRER,
650 cyy_readb(info, CySRER) & ~CyTxRdy);
651 goto done;
652 }
653 /* Because the Embedded Transmit Commands have been enabled,
654 * we must check to see if the escape character, NULL, is being
655 * sent. If it is, we must ensure that there is room for it to
656 * be doubled in the output stream. Therefore we no longer
657 * advance the pointer when the character is fetched, but
658 * rather wait until after the check for a NULL output
659 * character. This is necessary because there may not be room
660 * for the two chars needed to send a NULL.)
661 */
662 outch = info->port.xmit_buf[info->xmit_tail];
663 if (outch) {
664 info->xmit_cnt--;
665 info->xmit_tail = (info->xmit_tail + 1) &
666 (SERIAL_XMIT_SIZE - 1);
667 cyy_writeb(info, CyTDR, outch);
668 info->icount.tx++;
669 } else {
670 if (char_count > 1) {
671 info->xmit_cnt--;
672 info->xmit_tail = (info->xmit_tail + 1) &
673 (SERIAL_XMIT_SIZE - 1);
674 cyy_writeb(info, CyTDR, outch);
675 cyy_writeb(info, CyTDR, 0);
676 info->icount.tx++;
677 char_count--;
678 }
679 }
680 }
681
682done:
683 tty_wakeup(tty);
684 tty_kref_put(tty);
685end:
686 /* end of service */
687 cyy_writeb(info, CyTIR, save_xir & 0x3f);
688 cyy_writeb(info, CyCAR, save_car);
689}
690
691static void cyy_chip_modem(struct cyclades_card *cinfo, int chip,
692 void __iomem *base_addr)
693{
694 struct cyclades_port *info;
695 struct tty_struct *tty;
696 int index = cinfo->bus_index;
697 u8 save_xir, channel, save_car, mdm_change, mdm_status;
698
699 /* determine the channel & change to that context */
700 save_xir = readb(base_addr + (CyMIR << index));
701 channel = save_xir & CyIRChannel;
702 info = &cinfo->ports[channel + chip * 4];
703 save_car = cyy_readb(info, CyCAR);
704 cyy_writeb(info, CyCAR, save_xir);
705
706 mdm_change = cyy_readb(info, CyMISR);
707 mdm_status = cyy_readb(info, CyMSVR1);
708
709 tty = tty_port_tty_get(&info->port);
710 if (!tty)
711 goto end;
712
713 if (mdm_change & CyANY_DELTA) {
714 /* For statistics only */
715 if (mdm_change & CyDCD)
716 info->icount.dcd++;
717 if (mdm_change & CyCTS)
718 info->icount.cts++;
719 if (mdm_change & CyDSR)
720 info->icount.dsr++;
721 if (mdm_change & CyRI)
722 info->icount.rng++;
723
724 wake_up_interruptible(&info->port.delta_msr_wait);
725 }
726
727 if ((mdm_change & CyDCD) && (info->port.flags & ASYNC_CHECK_CD)) {
728 if (mdm_status & CyDCD)
729 wake_up_interruptible(&info->port.open_wait);
730 else
731 tty_hangup(tty);
732 }
733 if ((mdm_change & CyCTS) && (info->port.flags & ASYNC_CTS_FLOW)) {
734 if (tty->hw_stopped) {
735 if (mdm_status & CyCTS) {
736 /* cy_start isn't used
737 because... !!! */
738 tty->hw_stopped = 0;
739 cyy_writeb(info, CySRER,
740 cyy_readb(info, CySRER) | CyTxRdy);
741 tty_wakeup(tty);
742 }
743 } else {
744 if (!(mdm_status & CyCTS)) {
745 /* cy_stop isn't used
746 because ... !!! */
747 tty->hw_stopped = 1;
748 cyy_writeb(info, CySRER,
749 cyy_readb(info, CySRER) & ~CyTxRdy);
750 }
751 }
752 }
753/* if (mdm_change & CyDSR) {
754 }
755 if (mdm_change & CyRI) {
756 }*/
757 tty_kref_put(tty);
758end:
759 /* end of service */
760 cyy_writeb(info, CyMIR, save_xir & 0x3f);
761 cyy_writeb(info, CyCAR, save_car);
762}
763
764/* The real interrupt service routine is called
765 whenever the card wants its hand held--chars
766 received, out buffer empty, modem change, etc.
767 */
768static irqreturn_t cyy_interrupt(int irq, void *dev_id)
769{
770 int status;
771 struct cyclades_card *cinfo = dev_id;
772 void __iomem *base_addr, *card_base_addr;
773 unsigned int chip, too_many, had_work;
774 int index;
775
776 if (unlikely(cinfo == NULL)) {
777#ifdef CY_DEBUG_INTERRUPTS
778 printk(KERN_DEBUG "cyy_interrupt: spurious interrupt %d\n",
779 irq);
780#endif
781 return IRQ_NONE; /* spurious interrupt */
782 }
783
784 card_base_addr = cinfo->base_addr;
785 index = cinfo->bus_index;
786
787 /* card was not initialized yet (e.g. DEBUG_SHIRQ) */
788 if (unlikely(card_base_addr == NULL))
789 return IRQ_HANDLED;
790
791 /* This loop checks all chips in the card. Make a note whenever
792 _any_ chip had some work to do, as this is considered an
793 indication that there will be more to do. Only when no chip
794 has any work does this outermost loop exit.
795 */
796 do {
797 had_work = 0;
798 for (chip = 0; chip < cinfo->num_chips; chip++) {
799 base_addr = cinfo->base_addr +
800 (cy_chip_offset[chip] << index);
801 too_many = 0;
802 while ((status = readb(base_addr +
803 (CySVRR << index))) != 0x00) {
804 had_work++;
805 /* The purpose of the following test is to ensure that
806 no chip can monopolize the driver. This forces the
807 chips to be checked in a round-robin fashion (after
808 draining each of a bunch (1000) of characters).
809 */
810 if (1000 < too_many++)
811 break;
812 spin_lock(&cinfo->card_lock);
813 if (status & CySRReceive) /* rx intr */
814 cyy_chip_rx(cinfo, chip, base_addr);
815 if (status & CySRTransmit) /* tx intr */
816 cyy_chip_tx(cinfo, chip, base_addr);
817 if (status & CySRModem) /* modem intr */
818 cyy_chip_modem(cinfo, chip, base_addr);
819 spin_unlock(&cinfo->card_lock);
820 }
821 }
822 } while (had_work);
823
824 /* clear interrupts */
825 spin_lock(&cinfo->card_lock);
826 cy_writeb(card_base_addr + (Cy_ClrIntr << index), 0);
827 /* Cy_ClrIntr is 0x1800 */
828 spin_unlock(&cinfo->card_lock);
829 return IRQ_HANDLED;
830} /* cyy_interrupt */
831
832static void cyy_change_rts_dtr(struct cyclades_port *info, unsigned int set,
833 unsigned int clear)
834{
835 struct cyclades_card *card = info->card;
836 int channel = info->line - card->first_line;
837 u32 rts, dtr, msvrr, msvrd;
838
839 channel &= 0x03;
840
841 if (info->rtsdtr_inv) {
842 msvrr = CyMSVR2;
843 msvrd = CyMSVR1;
844 rts = CyDTR;
845 dtr = CyRTS;
846 } else {
847 msvrr = CyMSVR1;
848 msvrd = CyMSVR2;
849 rts = CyRTS;
850 dtr = CyDTR;
851 }
852 if (set & TIOCM_RTS) {
853 cyy_writeb(info, CyCAR, channel);
854 cyy_writeb(info, msvrr, rts);
855 }
856 if (clear & TIOCM_RTS) {
857 cyy_writeb(info, CyCAR, channel);
858 cyy_writeb(info, msvrr, ~rts);
859 }
860 if (set & TIOCM_DTR) {
861 cyy_writeb(info, CyCAR, channel);
862 cyy_writeb(info, msvrd, dtr);
863#ifdef CY_DEBUG_DTR
864 printk(KERN_DEBUG "cyc:set_modem_info raising DTR\n");
865 printk(KERN_DEBUG " status: 0x%x, 0x%x\n",
866 cyy_readb(info, CyMSVR1),
867 cyy_readb(info, CyMSVR2));
868#endif
869 }
870 if (clear & TIOCM_DTR) {
871 cyy_writeb(info, CyCAR, channel);
872 cyy_writeb(info, msvrd, ~dtr);
873#ifdef CY_DEBUG_DTR
874 printk(KERN_DEBUG "cyc:set_modem_info dropping DTR\n");
875 printk(KERN_DEBUG " status: 0x%x, 0x%x\n",
876 cyy_readb(info, CyMSVR1),
877 cyy_readb(info, CyMSVR2));
878#endif
879 }
880}
881
882/***********************************************************/
883/********* End of block of Cyclom-Y specific code **********/
884/******** Start of block of Cyclades-Z specific code *******/
885/***********************************************************/
886
887static int
888cyz_fetch_msg(struct cyclades_card *cinfo,
889 __u32 *channel, __u8 *cmd, __u32 *param)
890{
891 struct BOARD_CTRL __iomem *board_ctrl = cinfo->board_ctrl;
892 unsigned long loc_doorbell;
893
894 loc_doorbell = readl(&cinfo->ctl_addr.p9060->loc_doorbell);
895 if (loc_doorbell) {
896 *cmd = (char)(0xff & loc_doorbell);
897 *channel = readl(&board_ctrl->fwcmd_channel);
898 *param = (__u32) readl(&board_ctrl->fwcmd_param);
899 cy_writel(&cinfo->ctl_addr.p9060->loc_doorbell, 0xffffffff);
900 return 1;
901 }
902 return 0;
903} /* cyz_fetch_msg */
904
905static int
906cyz_issue_cmd(struct cyclades_card *cinfo,
907 __u32 channel, __u8 cmd, __u32 param)
908{
909 struct BOARD_CTRL __iomem *board_ctrl = cinfo->board_ctrl;
910 __u32 __iomem *pci_doorbell;
911 unsigned int index;
912
913 if (!cyz_is_loaded(cinfo))
914 return -1;
915
916 index = 0;
917 pci_doorbell = &cinfo->ctl_addr.p9060->pci_doorbell;
918 while ((readl(pci_doorbell) & 0xff) != 0) {
919 if (index++ == 1000)
920 return (int)(readl(pci_doorbell) & 0xff);
921 udelay(50L);
922 }
923 cy_writel(&board_ctrl->hcmd_channel, channel);
924 cy_writel(&board_ctrl->hcmd_param, param);
925 cy_writel(pci_doorbell, (long)cmd);
926
927 return 0;
928} /* cyz_issue_cmd */
929
930static void cyz_handle_rx(struct cyclades_port *info, struct tty_struct *tty)
931{
932 struct BUF_CTRL __iomem *buf_ctrl = info->u.cyz.buf_ctrl;
933 struct cyclades_card *cinfo = info->card;
934 unsigned int char_count;
935 int len;
936#ifdef BLOCKMOVE
937 unsigned char *buf;
938#else
939 char data;
940#endif
941 __u32 rx_put, rx_get, new_rx_get, rx_bufsize, rx_bufaddr;
942
943 rx_get = new_rx_get = readl(&buf_ctrl->rx_get);
944 rx_put = readl(&buf_ctrl->rx_put);
945 rx_bufsize = readl(&buf_ctrl->rx_bufsize);
946 rx_bufaddr = readl(&buf_ctrl->rx_bufaddr);
947 if (rx_put >= rx_get)
948 char_count = rx_put - rx_get;
949 else
950 char_count = rx_put - rx_get + rx_bufsize;
951
952 if (char_count) {
953#ifdef CY_ENABLE_MONITORING
954 info->mon.int_count++;
955 info->mon.char_count += char_count;
956 if (char_count > info->mon.char_max)
957 info->mon.char_max = char_count;
958 info->mon.char_last = char_count;
959#endif
960 if (tty == NULL) {
961 /* flush received characters */
962 new_rx_get = (new_rx_get + char_count) &
963 (rx_bufsize - 1);
964 info->rflush_count++;
965 } else {
966#ifdef BLOCKMOVE
967 /* we'd like to use memcpy(t, f, n) and memset(s, c, count)
968 for performance, but because of buffer boundaries, there
969 may be several steps to the operation */
970 while (1) {
971 len = tty_prepare_flip_string(tty, &buf,
972 char_count);
973 if (!len)
974 break;
975
976 len = min_t(unsigned int, min(len, char_count),
977 rx_bufsize - new_rx_get);
978
979 memcpy_fromio(buf, cinfo->base_addr +
980 rx_bufaddr + new_rx_get, len);
981
982 new_rx_get = (new_rx_get + len) &
983 (rx_bufsize - 1);
984 char_count -= len;
985 info->icount.rx += len;
986 info->idle_stats.recv_bytes += len;
987 }
988#else
989 len = tty_buffer_request_room(tty, char_count);
990 while (len--) {
991 data = readb(cinfo->base_addr + rx_bufaddr +
992 new_rx_get);
993 new_rx_get = (new_rx_get + 1) &
994 (rx_bufsize - 1);
995 tty_insert_flip_char(tty, data, TTY_NORMAL);
996 info->idle_stats.recv_bytes++;
997 info->icount.rx++;
998 }
999#endif
1000#ifdef CONFIG_CYZ_INTR
1001 /* Recalculate the number of chars in the RX buffer and issue
1002 a cmd in case it's higher than the RX high water mark */
1003 rx_put = readl(&buf_ctrl->rx_put);
1004 if (rx_put >= rx_get)
1005 char_count = rx_put - rx_get;
1006 else
1007 char_count = rx_put - rx_get + rx_bufsize;
1008 if (char_count >= readl(&buf_ctrl->rx_threshold) &&
1009 !timer_pending(&cyz_rx_full_timer[
1010 info->line]))
1011 mod_timer(&cyz_rx_full_timer[info->line],
1012 jiffies + 1);
1013#endif
1014 info->idle_stats.recv_idle = jiffies;
1015 tty_schedule_flip(tty);
1016 }
1017 /* Update rx_get */
1018 cy_writel(&buf_ctrl->rx_get, new_rx_get);
1019 }
1020}
1021
1022static void cyz_handle_tx(struct cyclades_port *info, struct tty_struct *tty)
1023{
1024 struct BUF_CTRL __iomem *buf_ctrl = info->u.cyz.buf_ctrl;
1025 struct cyclades_card *cinfo = info->card;
1026 u8 data;
1027 unsigned int char_count;
1028#ifdef BLOCKMOVE
1029 int small_count;
1030#endif
1031 __u32 tx_put, tx_get, tx_bufsize, tx_bufaddr;
1032
1033 if (info->xmit_cnt <= 0) /* Nothing to transmit */
1034 return;
1035
1036 tx_get = readl(&buf_ctrl->tx_get);
1037 tx_put = readl(&buf_ctrl->tx_put);
1038 tx_bufsize = readl(&buf_ctrl->tx_bufsize);
1039 tx_bufaddr = readl(&buf_ctrl->tx_bufaddr);
1040 if (tx_put >= tx_get)
1041 char_count = tx_get - tx_put - 1 + tx_bufsize;
1042 else
1043 char_count = tx_get - tx_put - 1;
1044
1045 if (char_count) {
1046
1047 if (tty == NULL)
1048 goto ztxdone;
1049
1050 if (info->x_char) { /* send special char */
1051 data = info->x_char;
1052
1053 cy_writeb(cinfo->base_addr + tx_bufaddr + tx_put, data);
1054 tx_put = (tx_put + 1) & (tx_bufsize - 1);
1055 info->x_char = 0;
1056 char_count--;
1057 info->icount.tx++;
1058 }
1059#ifdef BLOCKMOVE
1060 while (0 < (small_count = min_t(unsigned int,
1061 tx_bufsize - tx_put, min_t(unsigned int,
1062 (SERIAL_XMIT_SIZE - info->xmit_tail),
1063 min_t(unsigned int, info->xmit_cnt,
1064 char_count))))) {
1065
1066 memcpy_toio((char *)(cinfo->base_addr + tx_bufaddr +
1067 tx_put),
1068 &info->port.xmit_buf[info->xmit_tail],
1069 small_count);
1070
1071 tx_put = (tx_put + small_count) & (tx_bufsize - 1);
1072 char_count -= small_count;
1073 info->icount.tx += small_count;
1074 info->xmit_cnt -= small_count;
1075 info->xmit_tail = (info->xmit_tail + small_count) &
1076 (SERIAL_XMIT_SIZE - 1);
1077 }
1078#else
1079 while (info->xmit_cnt && char_count) {
1080 data = info->port.xmit_buf[info->xmit_tail];
1081 info->xmit_cnt--;
1082 info->xmit_tail = (info->xmit_tail + 1) &
1083 (SERIAL_XMIT_SIZE - 1);
1084
1085 cy_writeb(cinfo->base_addr + tx_bufaddr + tx_put, data);
1086 tx_put = (tx_put + 1) & (tx_bufsize - 1);
1087 char_count--;
1088 info->icount.tx++;
1089 }
1090#endif
1091 tty_wakeup(tty);
1092ztxdone:
1093 /* Update tx_put */
1094 cy_writel(&buf_ctrl->tx_put, tx_put);
1095 }
1096}
1097
1098static void cyz_handle_cmd(struct cyclades_card *cinfo)
1099{
1100 struct BOARD_CTRL __iomem *board_ctrl = cinfo->board_ctrl;
1101 struct tty_struct *tty;
1102 struct cyclades_port *info;
1103 __u32 channel, param, fw_ver;
1104 __u8 cmd;
1105 int special_count;
1106 int delta_count;
1107
1108 fw_ver = readl(&board_ctrl->fw_version);
1109
1110 while (cyz_fetch_msg(cinfo, &channel, &cmd, &param) == 1) {
1111 special_count = 0;
1112 delta_count = 0;
1113 info = &cinfo->ports[channel];
1114 tty = tty_port_tty_get(&info->port);
1115 if (tty == NULL)
1116 continue;
1117
1118 switch (cmd) {
1119 case C_CM_PR_ERROR:
1120 tty_insert_flip_char(tty, 0, TTY_PARITY);
1121 info->icount.rx++;
1122 special_count++;
1123 break;
1124 case C_CM_FR_ERROR:
1125 tty_insert_flip_char(tty, 0, TTY_FRAME);
1126 info->icount.rx++;
1127 special_count++;
1128 break;
1129 case C_CM_RXBRK:
1130 tty_insert_flip_char(tty, 0, TTY_BREAK);
1131 info->icount.rx++;
1132 special_count++;
1133 break;
1134 case C_CM_MDCD:
1135 info->icount.dcd++;
1136 delta_count++;
1137 if (info->port.flags & ASYNC_CHECK_CD) {
1138 u32 dcd = fw_ver > 241 ? param :
1139 readl(&info->u.cyz.ch_ctrl->rs_status);
1140 if (dcd & C_RS_DCD)
1141 wake_up_interruptible(&info->port.open_wait);
1142 else
1143 tty_hangup(tty);
1144 }
1145 break;
1146 case C_CM_MCTS:
1147 info->icount.cts++;
1148 delta_count++;
1149 break;
1150 case C_CM_MRI:
1151 info->icount.rng++;
1152 delta_count++;
1153 break;
1154 case C_CM_MDSR:
1155 info->icount.dsr++;
1156 delta_count++;
1157 break;
1158#ifdef Z_WAKE
1159 case C_CM_IOCTLW:
1160 complete(&info->shutdown_wait);
1161 break;
1162#endif
1163#ifdef CONFIG_CYZ_INTR
1164 case C_CM_RXHIWM:
1165 case C_CM_RXNNDT:
1166 case C_CM_INTBACK2:
1167 /* Reception Interrupt */
1168#ifdef CY_DEBUG_INTERRUPTS
1169 printk(KERN_DEBUG "cyz_interrupt: rcvd intr, card %d, "
1170 "port %ld\n", info->card, channel);
1171#endif
1172 cyz_handle_rx(info, tty);
1173 break;
1174 case C_CM_TXBEMPTY:
1175 case C_CM_TXLOWWM:
1176 case C_CM_INTBACK:
1177 /* Transmission Interrupt */
1178#ifdef CY_DEBUG_INTERRUPTS
1179 printk(KERN_DEBUG "cyz_interrupt: xmit intr, card %d, "
1180 "port %ld\n", info->card, channel);
1181#endif
1182 cyz_handle_tx(info, tty);
1183 break;
1184#endif /* CONFIG_CYZ_INTR */
1185 case C_CM_FATAL:
1186 /* should do something with this !!! */
1187 break;
1188 default:
1189 break;
1190 }
1191 if (delta_count)
1192 wake_up_interruptible(&info->port.delta_msr_wait);
1193 if (special_count)
1194 tty_schedule_flip(tty);
1195 tty_kref_put(tty);
1196 }
1197}
1198
1199#ifdef CONFIG_CYZ_INTR
1200static irqreturn_t cyz_interrupt(int irq, void *dev_id)
1201{
1202 struct cyclades_card *cinfo = dev_id;
1203
1204 if (unlikely(!cyz_is_loaded(cinfo))) {
1205#ifdef CY_DEBUG_INTERRUPTS
1206 printk(KERN_DEBUG "cyz_interrupt: board not yet loaded "
1207 "(IRQ%d).\n", irq);
1208#endif
1209 return IRQ_NONE;
1210 }
1211
1212 /* Handle the interrupts */
1213 cyz_handle_cmd(cinfo);
1214
1215 return IRQ_HANDLED;
1216} /* cyz_interrupt */
1217
1218static void cyz_rx_restart(unsigned long arg)
1219{
1220 struct cyclades_port *info = (struct cyclades_port *)arg;
1221 struct cyclades_card *card = info->card;
1222 int retval;
1223 __u32 channel = info->line - card->first_line;
1224 unsigned long flags;
1225
1226 spin_lock_irqsave(&card->card_lock, flags);
1227 retval = cyz_issue_cmd(card, channel, C_CM_INTBACK2, 0L);
1228 if (retval != 0) {
1229 printk(KERN_ERR "cyc:cyz_rx_restart retval on ttyC%d was %x\n",
1230 info->line, retval);
1231 }
1232 spin_unlock_irqrestore(&card->card_lock, flags);
1233}
1234
1235#else /* CONFIG_CYZ_INTR */
1236
1237static void cyz_poll(unsigned long arg)
1238{
1239 struct cyclades_card *cinfo;
1240 struct cyclades_port *info;
1241 unsigned long expires = jiffies + HZ;
1242 unsigned int port, card;
1243
1244 for (card = 0; card < NR_CARDS; card++) {
1245 cinfo = &cy_card[card];
1246
1247 if (!cy_is_Z(cinfo))
1248 continue;
1249 if (!cyz_is_loaded(cinfo))
1250 continue;
1251
1252 /* Skip first polling cycle to avoid racing conditions with the FW */
1253 if (!cinfo->intr_enabled) {
1254 cinfo->intr_enabled = 1;
1255 continue;
1256 }
1257
1258 cyz_handle_cmd(cinfo);
1259
1260 for (port = 0; port < cinfo->nports; port++) {
1261 struct tty_struct *tty;
1262
1263 info = &cinfo->ports[port];
1264 tty = tty_port_tty_get(&info->port);
1265 /* OK to pass NULL to the handle functions below.
1266 They need to drop the data in that case. */
1267
1268 if (!info->throttle)
1269 cyz_handle_rx(info, tty);
1270 cyz_handle_tx(info, tty);
1271 tty_kref_put(tty);
1272 }
1273 /* poll every 'cyz_polling_cycle' period */
1274 expires = jiffies + cyz_polling_cycle;
1275 }
1276 mod_timer(&cyz_timerlist, expires);
1277} /* cyz_poll */
1278
1279#endif /* CONFIG_CYZ_INTR */
1280
1281/********** End of block of Cyclades-Z specific code *********/
1282/***********************************************************/
1283
1284/* This is called whenever a port becomes active;
1285 interrupts are enabled and DTR & RTS are turned on.
1286 */
1287static int cy_startup(struct cyclades_port *info, struct tty_struct *tty)
1288{
1289 struct cyclades_card *card;
1290 unsigned long flags;
1291 int retval = 0;
1292 int channel;
1293 unsigned long page;
1294
1295 card = info->card;
1296 channel = info->line - card->first_line;
1297
1298 page = get_zeroed_page(GFP_KERNEL);
1299 if (!page)
1300 return -ENOMEM;
1301
1302 spin_lock_irqsave(&card->card_lock, flags);
1303
1304 if (info->port.flags & ASYNC_INITIALIZED)
1305 goto errout;
1306
1307 if (!info->type) {
1308 set_bit(TTY_IO_ERROR, &tty->flags);
1309 goto errout;
1310 }
1311
1312 if (info->port.xmit_buf)
1313 free_page(page);
1314 else
1315 info->port.xmit_buf = (unsigned char *)page;
1316
1317 spin_unlock_irqrestore(&card->card_lock, flags);
1318
1319 cy_set_line_char(info, tty);
1320
1321 if (!cy_is_Z(card)) {
1322 channel &= 0x03;
1323
1324 spin_lock_irqsave(&card->card_lock, flags);
1325
1326 cyy_writeb(info, CyCAR, channel);
1327
1328 cyy_writeb(info, CyRTPR,
1329 (info->default_timeout ? info->default_timeout : 0x02));
1330 /* 10ms rx timeout */
1331
1332 cyy_issue_cmd(info, CyCHAN_CTL | CyENB_RCVR | CyENB_XMTR);
1333
1334 cyy_change_rts_dtr(info, TIOCM_RTS | TIOCM_DTR, 0);
1335
1336 cyy_writeb(info, CySRER, cyy_readb(info, CySRER) | CyRxData);
1337 } else {
1338 struct CH_CTRL __iomem *ch_ctrl = info->u.cyz.ch_ctrl;
1339
1340 if (!cyz_is_loaded(card))
1341 return -ENODEV;
1342
1343#ifdef CY_DEBUG_OPEN
1344 printk(KERN_DEBUG "cyc startup Z card %d, channel %d, "
1345 "base_addr %p\n", card, channel, card->base_addr);
1346#endif
1347 spin_lock_irqsave(&card->card_lock, flags);
1348
1349 cy_writel(&ch_ctrl->op_mode, C_CH_ENABLE);
1350#ifdef Z_WAKE
1351#ifdef CONFIG_CYZ_INTR
1352 cy_writel(&ch_ctrl->intr_enable,
1353 C_IN_TXBEMPTY | C_IN_TXLOWWM | C_IN_RXHIWM |
1354 C_IN_RXNNDT | C_IN_IOCTLW | C_IN_MDCD);
1355#else
1356 cy_writel(&ch_ctrl->intr_enable,
1357 C_IN_IOCTLW | C_IN_MDCD);
1358#endif /* CONFIG_CYZ_INTR */
1359#else
1360#ifdef CONFIG_CYZ_INTR
1361 cy_writel(&ch_ctrl->intr_enable,
1362 C_IN_TXBEMPTY | C_IN_TXLOWWM | C_IN_RXHIWM |
1363 C_IN_RXNNDT | C_IN_MDCD);
1364#else
1365 cy_writel(&ch_ctrl->intr_enable, C_IN_MDCD);
1366#endif /* CONFIG_CYZ_INTR */
1367#endif /* Z_WAKE */
1368
1369 retval = cyz_issue_cmd(card, channel, C_CM_IOCTL, 0L);
1370 if (retval != 0) {
1371 printk(KERN_ERR "cyc:startup(1) retval on ttyC%d was "
1372 "%x\n", info->line, retval);
1373 }
1374
1375 /* Flush RX buffers before raising DTR and RTS */
1376 retval = cyz_issue_cmd(card, channel, C_CM_FLUSH_RX, 0L);
1377 if (retval != 0) {
1378 printk(KERN_ERR "cyc:startup(2) retval on ttyC%d was "
1379 "%x\n", info->line, retval);
1380 }
1381
1382 /* set timeout !!! */
1383 /* set RTS and DTR !!! */
1384 tty_port_raise_dtr_rts(&info->port);
1385
1386 /* enable send, recv, modem !!! */
1387 }
1388
1389 info->port.flags |= ASYNC_INITIALIZED;
1390
1391 clear_bit(TTY_IO_ERROR, &tty->flags);
1392 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1393 info->breakon = info->breakoff = 0;
1394 memset((char *)&info->idle_stats, 0, sizeof(info->idle_stats));
1395 info->idle_stats.in_use =
1396 info->idle_stats.recv_idle =
1397 info->idle_stats.xmit_idle = jiffies;
1398
1399 spin_unlock_irqrestore(&card->card_lock, flags);
1400
1401#ifdef CY_DEBUG_OPEN
1402 printk(KERN_DEBUG "cyc startup done\n");
1403#endif
1404 return 0;
1405
1406errout:
1407 spin_unlock_irqrestore(&card->card_lock, flags);
1408 free_page(page);
1409 return retval;
1410} /* startup */
1411
1412static void start_xmit(struct cyclades_port *info)
1413{
1414 struct cyclades_card *card = info->card;
1415 unsigned long flags;
1416 int channel = info->line - card->first_line;
1417
1418 if (!cy_is_Z(card)) {
1419 spin_lock_irqsave(&card->card_lock, flags);
1420 cyy_writeb(info, CyCAR, channel & 0x03);
1421 cyy_writeb(info, CySRER, cyy_readb(info, CySRER) | CyTxRdy);
1422 spin_unlock_irqrestore(&card->card_lock, flags);
1423 } else {
1424#ifdef CONFIG_CYZ_INTR
1425 int retval;
1426
1427 spin_lock_irqsave(&card->card_lock, flags);
1428 retval = cyz_issue_cmd(card, channel, C_CM_INTBACK, 0L);
1429 if (retval != 0) {
1430 printk(KERN_ERR "cyc:start_xmit retval on ttyC%d was "
1431 "%x\n", info->line, retval);
1432 }
1433 spin_unlock_irqrestore(&card->card_lock, flags);
1434#else /* CONFIG_CYZ_INTR */
1435 /* Don't have to do anything at this time */
1436#endif /* CONFIG_CYZ_INTR */
1437 }
1438} /* start_xmit */
1439
1440/*
1441 * This routine shuts down a serial port; interrupts are disabled,
1442 * and DTR is dropped if the hangup on close termio flag is on.
1443 */
1444static void cy_shutdown(struct cyclades_port *info, struct tty_struct *tty)
1445{
1446 struct cyclades_card *card;
1447 unsigned long flags;
1448 int channel;
1449
1450 if (!(info->port.flags & ASYNC_INITIALIZED))
1451 return;
1452
1453 card = info->card;
1454 channel = info->line - card->first_line;
1455 if (!cy_is_Z(card)) {
1456 spin_lock_irqsave(&card->card_lock, flags);
1457
1458 /* Clear delta_msr_wait queue to avoid mem leaks. */
1459 wake_up_interruptible(&info->port.delta_msr_wait);
1460
1461 if (info->port.xmit_buf) {
1462 unsigned char *temp;
1463 temp = info->port.xmit_buf;
1464 info->port.xmit_buf = NULL;
1465 free_page((unsigned long)temp);
1466 }
1467 if (tty->termios->c_cflag & HUPCL)
1468 cyy_change_rts_dtr(info, 0, TIOCM_RTS | TIOCM_DTR);
1469
1470 cyy_issue_cmd(info, CyCHAN_CTL | CyDIS_RCVR);
1471 /* it may be appropriate to clear _XMIT at
1472 some later date (after testing)!!! */
1473
1474 set_bit(TTY_IO_ERROR, &tty->flags);
1475 info->port.flags &= ~ASYNC_INITIALIZED;
1476 spin_unlock_irqrestore(&card->card_lock, flags);
1477 } else {
1478#ifdef CY_DEBUG_OPEN
1479 printk(KERN_DEBUG "cyc shutdown Z card %d, channel %d, "
1480 "base_addr %p\n", card, channel, card->base_addr);
1481#endif
1482
1483 if (!cyz_is_loaded(card))
1484 return;
1485
1486 spin_lock_irqsave(&card->card_lock, flags);
1487
1488 if (info->port.xmit_buf) {
1489 unsigned char *temp;
1490 temp = info->port.xmit_buf;
1491 info->port.xmit_buf = NULL;
1492 free_page((unsigned long)temp);
1493 }
1494
1495 if (tty->termios->c_cflag & HUPCL)
1496 tty_port_lower_dtr_rts(&info->port);
1497
1498 set_bit(TTY_IO_ERROR, &tty->flags);
1499 info->port.flags &= ~ASYNC_INITIALIZED;
1500
1501 spin_unlock_irqrestore(&card->card_lock, flags);
1502 }
1503
1504#ifdef CY_DEBUG_OPEN
1505 printk(KERN_DEBUG "cyc shutdown done\n");
1506#endif
1507} /* shutdown */
1508
1509/*
1510 * ------------------------------------------------------------
1511 * cy_open() and friends
1512 * ------------------------------------------------------------
1513 */
1514
1515/*
1516 * This routine is called whenever a serial port is opened. It
1517 * performs the serial-specific initialization for the tty structure.
1518 */
1519static int cy_open(struct tty_struct *tty, struct file *filp)
1520{
1521 struct cyclades_port *info;
1522 unsigned int i, line;
1523 int retval;
1524
1525 line = tty->index;
1526 if (tty->index < 0 || NR_PORTS <= line)
1527 return -ENODEV;
1528
1529 for (i = 0; i < NR_CARDS; i++)
1530 if (line < cy_card[i].first_line + cy_card[i].nports &&
1531 line >= cy_card[i].first_line)
1532 break;
1533 if (i >= NR_CARDS)
1534 return -ENODEV;
1535 info = &cy_card[i].ports[line - cy_card[i].first_line];
1536 if (info->line < 0)
1537 return -ENODEV;
1538
1539 /* If the card's firmware hasn't been loaded,
1540 treat it as absent from the system. This
1541 will make the user pay attention.
1542 */
1543 if (cy_is_Z(info->card)) {
1544 struct cyclades_card *cinfo = info->card;
1545 struct FIRM_ID __iomem *firm_id = cinfo->base_addr + ID_ADDRESS;
1546
1547 if (!cyz_is_loaded(cinfo)) {
1548 if (cinfo->hw_ver == ZE_V1 && cyz_fpga_loaded(cinfo) &&
1549 readl(&firm_id->signature) ==
1550 ZFIRM_HLT) {
1551 printk(KERN_ERR "cyc:Cyclades-Z Error: you "
1552 "need an external power supply for "
1553 "this number of ports.\nFirmware "
1554 "halted.\n");
1555 } else {
1556 printk(KERN_ERR "cyc:Cyclades-Z firmware not "
1557 "yet loaded\n");
1558 }
1559 return -ENODEV;
1560 }
1561#ifdef CONFIG_CYZ_INTR
1562 else {
1563 /* In case this Z board is operating in interrupt mode, its
1564 interrupts should be enabled as soon as the first open
1565 happens to one of its ports. */
1566 if (!cinfo->intr_enabled) {
1567 u16 intr;
1568
1569 /* Enable interrupts on the PLX chip */
1570 intr = readw(&cinfo->ctl_addr.p9060->
1571 intr_ctrl_stat) | 0x0900;
1572 cy_writew(&cinfo->ctl_addr.p9060->
1573 intr_ctrl_stat, intr);
1574 /* Enable interrupts on the FW */
1575 retval = cyz_issue_cmd(cinfo, 0,
1576 C_CM_IRQ_ENBL, 0L);
1577 if (retval != 0) {
1578 printk(KERN_ERR "cyc:IRQ enable retval "
1579 "was %x\n", retval);
1580 }
1581 cinfo->intr_enabled = 1;
1582 }
1583 }
1584#endif /* CONFIG_CYZ_INTR */
1585 /* Make sure this Z port really exists in hardware */
1586 if (info->line > (cinfo->first_line + cinfo->nports - 1))
1587 return -ENODEV;
1588 }
1589#ifdef CY_DEBUG_OTHER
1590 printk(KERN_DEBUG "cyc:cy_open ttyC%d\n", info->line);
1591#endif
1592 tty->driver_data = info;
1593 if (serial_paranoia_check(info, tty->name, "cy_open"))
1594 return -ENODEV;
1595
1596#ifdef CY_DEBUG_OPEN
1597 printk(KERN_DEBUG "cyc:cy_open ttyC%d, count = %d\n", info->line,
1598 info->port.count);
1599#endif
1600 info->port.count++;
1601#ifdef CY_DEBUG_COUNT
1602 printk(KERN_DEBUG "cyc:cy_open (%d): incrementing count to %d\n",
1603 current->pid, info->port.count);
1604#endif
1605
1606 /*
1607 * If the port is the middle of closing, bail out now
1608 */
1609 if (tty_hung_up_p(filp) || (info->port.flags & ASYNC_CLOSING)) {
1610 wait_event_interruptible_tty(info->port.close_wait,
1611 !(info->port.flags & ASYNC_CLOSING));
1612 return (info->port.flags & ASYNC_HUP_NOTIFY) ? -EAGAIN: -ERESTARTSYS;
1613 }
1614
1615 /*
1616 * Start up serial port
1617 */
1618 retval = cy_startup(info, tty);
1619 if (retval)
1620 return retval;
1621
1622 retval = tty_port_block_til_ready(&info->port, tty, filp);
1623 if (retval) {
1624#ifdef CY_DEBUG_OPEN
1625 printk(KERN_DEBUG "cyc:cy_open returning after block_til_ready "
1626 "with %d\n", retval);
1627#endif
1628 return retval;
1629 }
1630
1631 info->throttle = 0;
1632 tty_port_tty_set(&info->port, tty);
1633
1634#ifdef CY_DEBUG_OPEN
1635 printk(KERN_DEBUG "cyc:cy_open done\n");
1636#endif
1637 return 0;
1638} /* cy_open */
1639
1640/*
1641 * cy_wait_until_sent() --- wait until the transmitter is empty
1642 */
1643static void cy_wait_until_sent(struct tty_struct *tty, int timeout)
1644{
1645 struct cyclades_card *card;
1646 struct cyclades_port *info = tty->driver_data;
1647 unsigned long orig_jiffies;
1648 int char_time;
1649
1650 if (serial_paranoia_check(info, tty->name, "cy_wait_until_sent"))
1651 return;
1652
1653 if (info->xmit_fifo_size == 0)
1654 return; /* Just in case.... */
1655
1656 orig_jiffies = jiffies;
1657 /*
1658 * Set the check interval to be 1/5 of the estimated time to
1659 * send a single character, and make it at least 1. The check
1660 * interval should also be less than the timeout.
1661 *
1662 * Note: we have to use pretty tight timings here to satisfy
1663 * the NIST-PCTS.
1664 */
1665 char_time = (info->timeout - HZ / 50) / info->xmit_fifo_size;
1666 char_time = char_time / 5;
1667 if (char_time <= 0)
1668 char_time = 1;
1669 if (timeout < 0)
1670 timeout = 0;
1671 if (timeout)
1672 char_time = min(char_time, timeout);
1673 /*
1674 * If the transmitter hasn't cleared in twice the approximate
1675 * amount of time to send the entire FIFO, it probably won't
1676 * ever clear. This assumes the UART isn't doing flow
1677 * control, which is currently the case. Hence, if it ever
1678 * takes longer than info->timeout, this is probably due to a
1679 * UART bug of some kind. So, we clamp the timeout parameter at
1680 * 2*info->timeout.
1681 */
1682 if (!timeout || timeout > 2 * info->timeout)
1683 timeout = 2 * info->timeout;
1684#ifdef CY_DEBUG_WAIT_UNTIL_SENT
1685 printk(KERN_DEBUG "In cy_wait_until_sent(%d) check=%d, jiff=%lu...",
1686 timeout, char_time, jiffies);
1687#endif
1688 card = info->card;
1689 if (!cy_is_Z(card)) {
1690 while (cyy_readb(info, CySRER) & CyTxRdy) {
1691#ifdef CY_DEBUG_WAIT_UNTIL_SENT
1692 printk(KERN_DEBUG "Not clean (jiff=%lu)...", jiffies);
1693#endif
1694 if (msleep_interruptible(jiffies_to_msecs(char_time)))
1695 break;
1696 if (timeout && time_after(jiffies, orig_jiffies +
1697 timeout))
1698 break;
1699 }
1700 }
1701 /* Run one more char cycle */
1702 msleep_interruptible(jiffies_to_msecs(char_time * 5));
1703#ifdef CY_DEBUG_WAIT_UNTIL_SENT
1704 printk(KERN_DEBUG "Clean (jiff=%lu)...done\n", jiffies);
1705#endif
1706}
1707
1708static void cy_flush_buffer(struct tty_struct *tty)
1709{
1710 struct cyclades_port *info = tty->driver_data;
1711 struct cyclades_card *card;
1712 int channel, retval;
1713 unsigned long flags;
1714
1715#ifdef CY_DEBUG_IO
1716 printk(KERN_DEBUG "cyc:cy_flush_buffer ttyC%d\n", info->line);
1717#endif
1718
1719 if (serial_paranoia_check(info, tty->name, "cy_flush_buffer"))
1720 return;
1721
1722 card = info->card;
1723 channel = info->line - card->first_line;
1724
1725 spin_lock_irqsave(&card->card_lock, flags);
1726 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1727 spin_unlock_irqrestore(&card->card_lock, flags);
1728
1729 if (cy_is_Z(card)) { /* If it is a Z card, flush the on-board
1730 buffers as well */
1731 spin_lock_irqsave(&card->card_lock, flags);
1732 retval = cyz_issue_cmd(card, channel, C_CM_FLUSH_TX, 0L);
1733 if (retval != 0) {
1734 printk(KERN_ERR "cyc: flush_buffer retval on ttyC%d "
1735 "was %x\n", info->line, retval);
1736 }
1737 spin_unlock_irqrestore(&card->card_lock, flags);
1738 }
1739 tty_wakeup(tty);
1740} /* cy_flush_buffer */
1741
1742
1743static void cy_do_close(struct tty_port *port)
1744{
1745 struct cyclades_port *info = container_of(port, struct cyclades_port,
1746 port);
1747 struct cyclades_card *card;
1748 unsigned long flags;
1749 int channel;
1750
1751 card = info->card;
1752 channel = info->line - card->first_line;
1753 spin_lock_irqsave(&card->card_lock, flags);
1754
1755 if (!cy_is_Z(card)) {
1756 /* Stop accepting input */
1757 cyy_writeb(info, CyCAR, channel & 0x03);
1758 cyy_writeb(info, CySRER, cyy_readb(info, CySRER) & ~CyRxData);
1759 if (info->port.flags & ASYNC_INITIALIZED) {
1760 /* Waiting for on-board buffers to be empty before
1761 closing the port */
1762 spin_unlock_irqrestore(&card->card_lock, flags);
1763 cy_wait_until_sent(port->tty, info->timeout);
1764 spin_lock_irqsave(&card->card_lock, flags);
1765 }
1766 } else {
1767#ifdef Z_WAKE
1768 /* Waiting for on-board buffers to be empty before closing
1769 the port */
1770 struct CH_CTRL __iomem *ch_ctrl = info->u.cyz.ch_ctrl;
1771 int retval;
1772
1773 if (readl(&ch_ctrl->flow_status) != C_FS_TXIDLE) {
1774 retval = cyz_issue_cmd(card, channel, C_CM_IOCTLW, 0L);
1775 if (retval != 0) {
1776 printk(KERN_DEBUG "cyc:cy_close retval on "
1777 "ttyC%d was %x\n", info->line, retval);
1778 }
1779 spin_unlock_irqrestore(&card->card_lock, flags);
1780 wait_for_completion_interruptible(&info->shutdown_wait);
1781 spin_lock_irqsave(&card->card_lock, flags);
1782 }
1783#endif
1784 }
1785 spin_unlock_irqrestore(&card->card_lock, flags);
1786 cy_shutdown(info, port->tty);
1787}
1788
1789/*
1790 * This routine is called when a particular tty device is closed.
1791 */
1792static void cy_close(struct tty_struct *tty, struct file *filp)
1793{
1794 struct cyclades_port *info = tty->driver_data;
1795 if (!info || serial_paranoia_check(info, tty->name, "cy_close"))
1796 return;
1797 tty_port_close(&info->port, tty, filp);
1798} /* cy_close */
1799
1800/* This routine gets called when tty_write has put something into
1801 * the write_queue. The characters may come from user space or
1802 * kernel space.
1803 *
1804 * This routine will return the number of characters actually
1805 * accepted for writing.
1806 *
1807 * If the port is not already transmitting stuff, start it off by
1808 * enabling interrupts. The interrupt service routine will then
1809 * ensure that the characters are sent.
1810 * If the port is already active, there is no need to kick it.
1811 *
1812 */
1813static int cy_write(struct tty_struct *tty, const unsigned char *buf, int count)
1814{
1815 struct cyclades_port *info = tty->driver_data;
1816 unsigned long flags;
1817 int c, ret = 0;
1818
1819#ifdef CY_DEBUG_IO
1820 printk(KERN_DEBUG "cyc:cy_write ttyC%d\n", info->line);
1821#endif
1822
1823 if (serial_paranoia_check(info, tty->name, "cy_write"))
1824 return 0;
1825
1826 if (!info->port.xmit_buf)
1827 return 0;
1828
1829 spin_lock_irqsave(&info->card->card_lock, flags);
1830 while (1) {
1831 c = min(count, (int)(SERIAL_XMIT_SIZE - info->xmit_cnt - 1));
1832 c = min(c, (int)(SERIAL_XMIT_SIZE - info->xmit_head));
1833
1834 if (c <= 0)
1835 break;
1836
1837 memcpy(info->port.xmit_buf + info->xmit_head, buf, c);
1838 info->xmit_head = (info->xmit_head + c) &
1839 (SERIAL_XMIT_SIZE - 1);
1840 info->xmit_cnt += c;
1841 buf += c;
1842 count -= c;
1843 ret += c;
1844 }
1845 spin_unlock_irqrestore(&info->card->card_lock, flags);
1846
1847 info->idle_stats.xmit_bytes += ret;
1848 info->idle_stats.xmit_idle = jiffies;
1849
1850 if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped)
1851 start_xmit(info);
1852
1853 return ret;
1854} /* cy_write */
1855
1856/*
1857 * This routine is called by the kernel to write a single
1858 * character to the tty device. If the kernel uses this routine,
1859 * it must call the flush_chars() routine (if defined) when it is
1860 * done stuffing characters into the driver. If there is no room
1861 * in the queue, the character is ignored.
1862 */
1863static int cy_put_char(struct tty_struct *tty, unsigned char ch)
1864{
1865 struct cyclades_port *info = tty->driver_data;
1866 unsigned long flags;
1867
1868#ifdef CY_DEBUG_IO
1869 printk(KERN_DEBUG "cyc:cy_put_char ttyC%d\n", info->line);
1870#endif
1871
1872 if (serial_paranoia_check(info, tty->name, "cy_put_char"))
1873 return 0;
1874
1875 if (!info->port.xmit_buf)
1876 return 0;
1877
1878 spin_lock_irqsave(&info->card->card_lock, flags);
1879 if (info->xmit_cnt >= (int)(SERIAL_XMIT_SIZE - 1)) {
1880 spin_unlock_irqrestore(&info->card->card_lock, flags);
1881 return 0;
1882 }
1883
1884 info->port.xmit_buf[info->xmit_head++] = ch;
1885 info->xmit_head &= SERIAL_XMIT_SIZE - 1;
1886 info->xmit_cnt++;
1887 info->idle_stats.xmit_bytes++;
1888 info->idle_stats.xmit_idle = jiffies;
1889 spin_unlock_irqrestore(&info->card->card_lock, flags);
1890 return 1;
1891} /* cy_put_char */
1892
1893/*
1894 * This routine is called by the kernel after it has written a
1895 * series of characters to the tty device using put_char().
1896 */
1897static void cy_flush_chars(struct tty_struct *tty)
1898{
1899 struct cyclades_port *info = tty->driver_data;
1900
1901#ifdef CY_DEBUG_IO
1902 printk(KERN_DEBUG "cyc:cy_flush_chars ttyC%d\n", info->line);
1903#endif
1904
1905 if (serial_paranoia_check(info, tty->name, "cy_flush_chars"))
1906 return;
1907
1908 if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
1909 !info->port.xmit_buf)
1910 return;
1911
1912 start_xmit(info);
1913} /* cy_flush_chars */
1914
1915/*
1916 * This routine returns the numbers of characters the tty driver
1917 * will accept for queuing to be written. This number is subject
1918 * to change as output buffers get emptied, or if the output flow
1919 * control is activated.
1920 */
1921static int cy_write_room(struct tty_struct *tty)
1922{
1923 struct cyclades_port *info = tty->driver_data;
1924 int ret;
1925
1926#ifdef CY_DEBUG_IO
1927 printk(KERN_DEBUG "cyc:cy_write_room ttyC%d\n", info->line);
1928#endif
1929
1930 if (serial_paranoia_check(info, tty->name, "cy_write_room"))
1931 return 0;
1932 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
1933 if (ret < 0)
1934 ret = 0;
1935 return ret;
1936} /* cy_write_room */
1937
1938static int cy_chars_in_buffer(struct tty_struct *tty)
1939{
1940 struct cyclades_port *info = tty->driver_data;
1941
1942 if (serial_paranoia_check(info, tty->name, "cy_chars_in_buffer"))
1943 return 0;
1944
1945#ifdef Z_EXT_CHARS_IN_BUFFER
1946 if (!cy_is_Z(info->card)) {
1947#endif /* Z_EXT_CHARS_IN_BUFFER */
1948#ifdef CY_DEBUG_IO
1949 printk(KERN_DEBUG "cyc:cy_chars_in_buffer ttyC%d %d\n",
1950 info->line, info->xmit_cnt);
1951#endif
1952 return info->xmit_cnt;
1953#ifdef Z_EXT_CHARS_IN_BUFFER
1954 } else {
1955 struct BUF_CTRL __iomem *buf_ctrl = info->u.cyz.buf_ctrl;
1956 int char_count;
1957 __u32 tx_put, tx_get, tx_bufsize;
1958
1959 tx_get = readl(&buf_ctrl->tx_get);
1960 tx_put = readl(&buf_ctrl->tx_put);
1961 tx_bufsize = readl(&buf_ctrl->tx_bufsize);
1962 if (tx_put >= tx_get)
1963 char_count = tx_put - tx_get;
1964 else
1965 char_count = tx_put - tx_get + tx_bufsize;
1966#ifdef CY_DEBUG_IO
1967 printk(KERN_DEBUG "cyc:cy_chars_in_buffer ttyC%d %d\n",
1968 info->line, info->xmit_cnt + char_count);
1969#endif
1970 return info->xmit_cnt + char_count;
1971 }
1972#endif /* Z_EXT_CHARS_IN_BUFFER */
1973} /* cy_chars_in_buffer */
1974
1975/*
1976 * ------------------------------------------------------------
1977 * cy_ioctl() and friends
1978 * ------------------------------------------------------------
1979 */
1980
1981static void cyy_baud_calc(struct cyclades_port *info, __u32 baud)
1982{
1983 int co, co_val, bpr;
1984 __u32 cy_clock = ((info->chip_rev >= CD1400_REV_J) ? 60000000 :
1985 25000000);
1986
1987 if (baud == 0) {
1988 info->tbpr = info->tco = info->rbpr = info->rco = 0;
1989 return;
1990 }
1991
1992 /* determine which prescaler to use */
1993 for (co = 4, co_val = 2048; co; co--, co_val >>= 2) {
1994 if (cy_clock / co_val / baud > 63)
1995 break;
1996 }
1997
1998 bpr = (cy_clock / co_val * 2 / baud + 1) / 2;
1999 if (bpr > 255)
2000 bpr = 255;
2001
2002 info->tbpr = info->rbpr = bpr;
2003 info->tco = info->rco = co;
2004}
2005
2006/*
2007 * This routine finds or computes the various line characteristics.
2008 * It used to be called config_setup
2009 */
2010static void cy_set_line_char(struct cyclades_port *info, struct tty_struct *tty)
2011{
2012 struct cyclades_card *card;
2013 unsigned long flags;
2014 int channel;
2015 unsigned cflag, iflag;
2016 int baud, baud_rate = 0;
2017 int i;
2018
2019 if (!tty->termios) /* XXX can this happen at all? */
2020 return;
2021
2022 if (info->line == -1)
2023 return;
2024
2025 cflag = tty->termios->c_cflag;
2026 iflag = tty->termios->c_iflag;
2027
2028 /*
2029 * Set up the tty->alt_speed kludge
2030 */
2031 if ((info->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
2032 tty->alt_speed = 57600;
2033 if ((info->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
2034 tty->alt_speed = 115200;
2035 if ((info->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
2036 tty->alt_speed = 230400;
2037 if ((info->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
2038 tty->alt_speed = 460800;
2039
2040 card = info->card;
2041 channel = info->line - card->first_line;
2042
2043 if (!cy_is_Z(card)) {
2044 u32 cflags;
2045
2046 /* baud rate */
2047 baud = tty_get_baud_rate(tty);
2048 if (baud == 38400 && (info->port.flags & ASYNC_SPD_MASK) ==
2049 ASYNC_SPD_CUST) {
2050 if (info->custom_divisor)
2051 baud_rate = info->baud / info->custom_divisor;
2052 else
2053 baud_rate = info->baud;
2054 } else if (baud > CD1400_MAX_SPEED) {
2055 baud = CD1400_MAX_SPEED;
2056 }
2057 /* find the baud index */
2058 for (i = 0; i < 20; i++) {
2059 if (baud == baud_table[i])
2060 break;
2061 }
2062 if (i == 20)
2063 i = 19; /* CD1400_MAX_SPEED */
2064
2065 if (baud == 38400 && (info->port.flags & ASYNC_SPD_MASK) ==
2066 ASYNC_SPD_CUST) {
2067 cyy_baud_calc(info, baud_rate);
2068 } else {
2069 if (info->chip_rev >= CD1400_REV_J) {
2070 /* It is a CD1400 rev. J or later */
2071 info->tbpr = baud_bpr_60[i]; /* Tx BPR */
2072 info->tco = baud_co_60[i]; /* Tx CO */
2073 info->rbpr = baud_bpr_60[i]; /* Rx BPR */
2074 info->rco = baud_co_60[i]; /* Rx CO */
2075 } else {
2076 info->tbpr = baud_bpr_25[i]; /* Tx BPR */
2077 info->tco = baud_co_25[i]; /* Tx CO */
2078 info->rbpr = baud_bpr_25[i]; /* Rx BPR */
2079 info->rco = baud_co_25[i]; /* Rx CO */
2080 }
2081 }
2082 if (baud_table[i] == 134) {
2083 /* get it right for 134.5 baud */
2084 info->timeout = (info->xmit_fifo_size * HZ * 30 / 269) +
2085 2;
2086 } else if (baud == 38400 && (info->port.flags & ASYNC_SPD_MASK) ==
2087 ASYNC_SPD_CUST) {
2088 info->timeout = (info->xmit_fifo_size * HZ * 15 /
2089 baud_rate) + 2;
2090 } else if (baud_table[i]) {
2091 info->timeout = (info->xmit_fifo_size * HZ * 15 /
2092 baud_table[i]) + 2;
2093 /* this needs to be propagated into the card info */
2094 } else {
2095 info->timeout = 0;
2096 }
2097 /* By tradition (is it a standard?) a baud rate of zero
2098 implies the line should be/has been closed. A bit
2099 later in this routine such a test is performed. */
2100
2101 /* byte size and parity */
2102 info->cor5 = 0;
2103 info->cor4 = 0;
2104 /* receive threshold */
2105 info->cor3 = (info->default_threshold ?
2106 info->default_threshold : baud_cor3[i]);
2107 info->cor2 = CyETC;
2108 switch (cflag & CSIZE) {
2109 case CS5:
2110 info->cor1 = Cy_5_BITS;
2111 break;
2112 case CS6:
2113 info->cor1 = Cy_6_BITS;
2114 break;
2115 case CS7:
2116 info->cor1 = Cy_7_BITS;
2117 break;
2118 case CS8:
2119 info->cor1 = Cy_8_BITS;
2120 break;
2121 }
2122 if (cflag & CSTOPB)
2123 info->cor1 |= Cy_2_STOP;
2124
2125 if (cflag & PARENB) {
2126 if (cflag & PARODD)
2127 info->cor1 |= CyPARITY_O;
2128 else
2129 info->cor1 |= CyPARITY_E;
2130 } else
2131 info->cor1 |= CyPARITY_NONE;
2132
2133 /* CTS flow control flag */
2134 if (cflag & CRTSCTS) {
2135 info->port.flags |= ASYNC_CTS_FLOW;
2136 info->cor2 |= CyCtsAE;
2137 } else {
2138 info->port.flags &= ~ASYNC_CTS_FLOW;
2139 info->cor2 &= ~CyCtsAE;
2140 }
2141 if (cflag & CLOCAL)
2142 info->port.flags &= ~ASYNC_CHECK_CD;
2143 else
2144 info->port.flags |= ASYNC_CHECK_CD;
2145
2146 /***********************************************
2147 The hardware option, CyRtsAO, presents RTS when
2148 the chip has characters to send. Since most modems
2149 use RTS as reverse (inbound) flow control, this
2150 option is not used. If inbound flow control is
2151 necessary, DTR can be programmed to provide the
2152 appropriate signals for use with a non-standard
2153 cable. Contact Marcio Saito for details.
2154 ***********************************************/
2155
2156 channel &= 0x03;
2157
2158 spin_lock_irqsave(&card->card_lock, flags);
2159 cyy_writeb(info, CyCAR, channel);
2160
2161 /* tx and rx baud rate */
2162
2163 cyy_writeb(info, CyTCOR, info->tco);
2164 cyy_writeb(info, CyTBPR, info->tbpr);
2165 cyy_writeb(info, CyRCOR, info->rco);
2166 cyy_writeb(info, CyRBPR, info->rbpr);
2167
2168 /* set line characteristics according configuration */
2169
2170 cyy_writeb(info, CySCHR1, START_CHAR(tty));
2171 cyy_writeb(info, CySCHR2, STOP_CHAR(tty));
2172 cyy_writeb(info, CyCOR1, info->cor1);
2173 cyy_writeb(info, CyCOR2, info->cor2);
2174 cyy_writeb(info, CyCOR3, info->cor3);
2175 cyy_writeb(info, CyCOR4, info->cor4);
2176 cyy_writeb(info, CyCOR5, info->cor5);
2177
2178 cyy_issue_cmd(info, CyCOR_CHANGE | CyCOR1ch | CyCOR2ch |
2179 CyCOR3ch);
2180
2181 /* !!! Is this needed? */
2182 cyy_writeb(info, CyCAR, channel);
2183 cyy_writeb(info, CyRTPR,
2184 (info->default_timeout ? info->default_timeout : 0x02));
2185 /* 10ms rx timeout */
2186
2187 cflags = CyCTS;
2188 if (!C_CLOCAL(tty))
2189 cflags |= CyDSR | CyRI | CyDCD;
2190 /* without modem intr */
2191 cyy_writeb(info, CySRER, cyy_readb(info, CySRER) | CyMdmCh);
2192 /* act on 1->0 modem transitions */
2193 if ((cflag & CRTSCTS) && info->rflow)
2194 cyy_writeb(info, CyMCOR1, cflags | rflow_thr[i]);
2195 else
2196 cyy_writeb(info, CyMCOR1, cflags);
2197 /* act on 0->1 modem transitions */
2198 cyy_writeb(info, CyMCOR2, cflags);
2199
2200 if (i == 0) /* baud rate is zero, turn off line */
2201 cyy_change_rts_dtr(info, 0, TIOCM_DTR);
2202 else
2203 cyy_change_rts_dtr(info, TIOCM_DTR, 0);
2204
2205 clear_bit(TTY_IO_ERROR, &tty->flags);
2206 spin_unlock_irqrestore(&card->card_lock, flags);
2207
2208 } else {
2209 struct CH_CTRL __iomem *ch_ctrl = info->u.cyz.ch_ctrl;
2210 __u32 sw_flow;
2211 int retval;
2212
2213 if (!cyz_is_loaded(card))
2214 return;
2215
2216 /* baud rate */
2217 baud = tty_get_baud_rate(tty);
2218 if (baud == 38400 && (info->port.flags & ASYNC_SPD_MASK) ==
2219 ASYNC_SPD_CUST) {
2220 if (info->custom_divisor)
2221 baud_rate = info->baud / info->custom_divisor;
2222 else
2223 baud_rate = info->baud;
2224 } else if (baud > CYZ_MAX_SPEED) {
2225 baud = CYZ_MAX_SPEED;
2226 }
2227 cy_writel(&ch_ctrl->comm_baud, baud);
2228
2229 if (baud == 134) {
2230 /* get it right for 134.5 baud */
2231 info->timeout = (info->xmit_fifo_size * HZ * 30 / 269) +
2232 2;
2233 } else if (baud == 38400 && (info->port.flags & ASYNC_SPD_MASK) ==
2234 ASYNC_SPD_CUST) {
2235 info->timeout = (info->xmit_fifo_size * HZ * 15 /
2236 baud_rate) + 2;
2237 } else if (baud) {
2238 info->timeout = (info->xmit_fifo_size * HZ * 15 /
2239 baud) + 2;
2240 /* this needs to be propagated into the card info */
2241 } else {
2242 info->timeout = 0;
2243 }
2244
2245 /* byte size and parity */
2246 switch (cflag & CSIZE) {
2247 case CS5:
2248 cy_writel(&ch_ctrl->comm_data_l, C_DL_CS5);
2249 break;
2250 case CS6:
2251 cy_writel(&ch_ctrl->comm_data_l, C_DL_CS6);
2252 break;
2253 case CS7:
2254 cy_writel(&ch_ctrl->comm_data_l, C_DL_CS7);
2255 break;
2256 case CS8:
2257 cy_writel(&ch_ctrl->comm_data_l, C_DL_CS8);
2258 break;
2259 }
2260 if (cflag & CSTOPB) {
2261 cy_writel(&ch_ctrl->comm_data_l,
2262 readl(&ch_ctrl->comm_data_l) | C_DL_2STOP);
2263 } else {
2264 cy_writel(&ch_ctrl->comm_data_l,
2265 readl(&ch_ctrl->comm_data_l) | C_DL_1STOP);
2266 }
2267 if (cflag & PARENB) {
2268 if (cflag & PARODD)
2269 cy_writel(&ch_ctrl->comm_parity, C_PR_ODD);
2270 else
2271 cy_writel(&ch_ctrl->comm_parity, C_PR_EVEN);
2272 } else
2273 cy_writel(&ch_ctrl->comm_parity, C_PR_NONE);
2274
2275 /* CTS flow control flag */
2276 if (cflag & CRTSCTS) {
2277 cy_writel(&ch_ctrl->hw_flow,
2278 readl(&ch_ctrl->hw_flow) | C_RS_CTS | C_RS_RTS);
2279 } else {
2280 cy_writel(&ch_ctrl->hw_flow, readl(&ch_ctrl->hw_flow) &
2281 ~(C_RS_CTS | C_RS_RTS));
2282 }
2283 /* As the HW flow control is done in firmware, the driver
2284 doesn't need to care about it */
2285 info->port.flags &= ~ASYNC_CTS_FLOW;
2286
2287 /* XON/XOFF/XANY flow control flags */
2288 sw_flow = 0;
2289 if (iflag & IXON) {
2290 sw_flow |= C_FL_OXX;
2291 if (iflag & IXANY)
2292 sw_flow |= C_FL_OIXANY;
2293 }
2294 cy_writel(&ch_ctrl->sw_flow, sw_flow);
2295
2296 retval = cyz_issue_cmd(card, channel, C_CM_IOCTL, 0L);
2297 if (retval != 0) {
2298 printk(KERN_ERR "cyc:set_line_char retval on ttyC%d "
2299 "was %x\n", info->line, retval);
2300 }
2301
2302 /* CD sensitivity */
2303 if (cflag & CLOCAL)
2304 info->port.flags &= ~ASYNC_CHECK_CD;
2305 else
2306 info->port.flags |= ASYNC_CHECK_CD;
2307
2308 if (baud == 0) { /* baud rate is zero, turn off line */
2309 cy_writel(&ch_ctrl->rs_control,
2310 readl(&ch_ctrl->rs_control) & ~C_RS_DTR);
2311#ifdef CY_DEBUG_DTR
2312 printk(KERN_DEBUG "cyc:set_line_char dropping Z DTR\n");
2313#endif
2314 } else {
2315 cy_writel(&ch_ctrl->rs_control,
2316 readl(&ch_ctrl->rs_control) | C_RS_DTR);
2317#ifdef CY_DEBUG_DTR
2318 printk(KERN_DEBUG "cyc:set_line_char raising Z DTR\n");
2319#endif
2320 }
2321
2322 retval = cyz_issue_cmd(card, channel, C_CM_IOCTLM, 0L);
2323 if (retval != 0) {
2324 printk(KERN_ERR "cyc:set_line_char(2) retval on ttyC%d "
2325 "was %x\n", info->line, retval);
2326 }
2327
2328 clear_bit(TTY_IO_ERROR, &tty->flags);
2329 }
2330} /* set_line_char */
2331
2332static int cy_get_serial_info(struct cyclades_port *info,
2333 struct serial_struct __user *retinfo)
2334{
2335 struct cyclades_card *cinfo = info->card;
2336 struct serial_struct tmp = {
2337 .type = info->type,
2338 .line = info->line,
2339 .port = (info->card - cy_card) * 0x100 + info->line -
2340 cinfo->first_line,
2341 .irq = cinfo->irq,
2342 .flags = info->port.flags,
2343 .close_delay = info->port.close_delay,
2344 .closing_wait = info->port.closing_wait,
2345 .baud_base = info->baud,
2346 .custom_divisor = info->custom_divisor,
2347 .hub6 = 0, /*!!! */
2348 };
2349 return copy_to_user(retinfo, &tmp, sizeof(*retinfo)) ? -EFAULT : 0;
2350}
2351
2352static int
2353cy_set_serial_info(struct cyclades_port *info, struct tty_struct *tty,
2354 struct serial_struct __user *new_info)
2355{
2356 struct serial_struct new_serial;
2357 int ret;
2358
2359 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
2360 return -EFAULT;
2361
2362 mutex_lock(&info->port.mutex);
2363 if (!capable(CAP_SYS_ADMIN)) {
2364 if (new_serial.close_delay != info->port.close_delay ||
2365 new_serial.baud_base != info->baud ||
2366 (new_serial.flags & ASYNC_FLAGS &
2367 ~ASYNC_USR_MASK) !=
2368 (info->port.flags & ASYNC_FLAGS & ~ASYNC_USR_MASK))
2369 {
2370 mutex_unlock(&info->port.mutex);
2371 return -EPERM;
2372 }
2373 info->port.flags = (info->port.flags & ~ASYNC_USR_MASK) |
2374 (new_serial.flags & ASYNC_USR_MASK);
2375 info->baud = new_serial.baud_base;
2376 info->custom_divisor = new_serial.custom_divisor;
2377 goto check_and_exit;
2378 }
2379
2380 /*
2381 * OK, past this point, all the error checking has been done.
2382 * At this point, we start making changes.....
2383 */
2384
2385 info->baud = new_serial.baud_base;
2386 info->custom_divisor = new_serial.custom_divisor;
2387 info->port.flags = (info->port.flags & ~ASYNC_FLAGS) |
2388 (new_serial.flags & ASYNC_FLAGS);
2389 info->port.close_delay = new_serial.close_delay * HZ / 100;
2390 info->port.closing_wait = new_serial.closing_wait * HZ / 100;
2391
2392check_and_exit:
2393 if (info->port.flags & ASYNC_INITIALIZED) {
2394 cy_set_line_char(info, tty);
2395 ret = 0;
2396 } else {
2397 ret = cy_startup(info, tty);
2398 }
2399 mutex_unlock(&info->port.mutex);
2400 return ret;
2401} /* set_serial_info */
2402
2403/*
2404 * get_lsr_info - get line status register info
2405 *
2406 * Purpose: Let user call ioctl() to get info when the UART physically
2407 * is emptied. On bus types like RS485, the transmitter must
2408 * release the bus after transmitting. This must be done when
2409 * the transmit shift register is empty, not be done when the
2410 * transmit holding register is empty. This functionality
2411 * allows an RS485 driver to be written in user space.
2412 */
2413static int get_lsr_info(struct cyclades_port *info, unsigned int __user *value)
2414{
2415 struct cyclades_card *card = info->card;
2416 unsigned int result;
2417 unsigned long flags;
2418 u8 status;
2419
2420 if (!cy_is_Z(card)) {
2421 spin_lock_irqsave(&card->card_lock, flags);
2422 status = cyy_readb(info, CySRER) & (CyTxRdy | CyTxMpty);
2423 spin_unlock_irqrestore(&card->card_lock, flags);
2424 result = (status ? 0 : TIOCSER_TEMT);
2425 } else {
2426 /* Not supported yet */
2427 return -EINVAL;
2428 }
2429 return put_user(result, (unsigned long __user *)value);
2430}
2431
2432static int cy_tiocmget(struct tty_struct *tty, struct file *file)
2433{
2434 struct cyclades_port *info = tty->driver_data;
2435 struct cyclades_card *card;
2436 int result;
2437
2438 if (serial_paranoia_check(info, tty->name, __func__))
2439 return -ENODEV;
2440
2441 card = info->card;
2442
2443 if (!cy_is_Z(card)) {
2444 unsigned long flags;
2445 int channel = info->line - card->first_line;
2446 u8 status;
2447
2448 spin_lock_irqsave(&card->card_lock, flags);
2449 cyy_writeb(info, CyCAR, channel & 0x03);
2450 status = cyy_readb(info, CyMSVR1);
2451 status |= cyy_readb(info, CyMSVR2);
2452 spin_unlock_irqrestore(&card->card_lock, flags);
2453
2454 if (info->rtsdtr_inv) {
2455 result = ((status & CyRTS) ? TIOCM_DTR : 0) |
2456 ((status & CyDTR) ? TIOCM_RTS : 0);
2457 } else {
2458 result = ((status & CyRTS) ? TIOCM_RTS : 0) |
2459 ((status & CyDTR) ? TIOCM_DTR : 0);
2460 }
2461 result |= ((status & CyDCD) ? TIOCM_CAR : 0) |
2462 ((status & CyRI) ? TIOCM_RNG : 0) |
2463 ((status & CyDSR) ? TIOCM_DSR : 0) |
2464 ((status & CyCTS) ? TIOCM_CTS : 0);
2465 } else {
2466 u32 lstatus;
2467
2468 if (!cyz_is_loaded(card)) {
2469 result = -ENODEV;
2470 goto end;
2471 }
2472
2473 lstatus = readl(&info->u.cyz.ch_ctrl->rs_status);
2474 result = ((lstatus & C_RS_RTS) ? TIOCM_RTS : 0) |
2475 ((lstatus & C_RS_DTR) ? TIOCM_DTR : 0) |
2476 ((lstatus & C_RS_DCD) ? TIOCM_CAR : 0) |
2477 ((lstatus & C_RS_RI) ? TIOCM_RNG : 0) |
2478 ((lstatus & C_RS_DSR) ? TIOCM_DSR : 0) |
2479 ((lstatus & C_RS_CTS) ? TIOCM_CTS : 0);
2480 }
2481end:
2482 return result;
2483} /* cy_tiomget */
2484
2485static int
2486cy_tiocmset(struct tty_struct *tty, struct file *file,
2487 unsigned int set, unsigned int clear)
2488{
2489 struct cyclades_port *info = tty->driver_data;
2490 struct cyclades_card *card;
2491 unsigned long flags;
2492
2493 if (serial_paranoia_check(info, tty->name, __func__))
2494 return -ENODEV;
2495
2496 card = info->card;
2497 if (!cy_is_Z(card)) {
2498 spin_lock_irqsave(&card->card_lock, flags);
2499 cyy_change_rts_dtr(info, set, clear);
2500 spin_unlock_irqrestore(&card->card_lock, flags);
2501 } else {
2502 struct CH_CTRL __iomem *ch_ctrl = info->u.cyz.ch_ctrl;
2503 int retval, channel = info->line - card->first_line;
2504 u32 rs;
2505
2506 if (!cyz_is_loaded(card))
2507 return -ENODEV;
2508
2509 spin_lock_irqsave(&card->card_lock, flags);
2510 rs = readl(&ch_ctrl->rs_control);
2511 if (set & TIOCM_RTS)
2512 rs |= C_RS_RTS;
2513 if (clear & TIOCM_RTS)
2514 rs &= ~C_RS_RTS;
2515 if (set & TIOCM_DTR) {
2516 rs |= C_RS_DTR;
2517#ifdef CY_DEBUG_DTR
2518 printk(KERN_DEBUG "cyc:set_modem_info raising Z DTR\n");
2519#endif
2520 }
2521 if (clear & TIOCM_DTR) {
2522 rs &= ~C_RS_DTR;
2523#ifdef CY_DEBUG_DTR
2524 printk(KERN_DEBUG "cyc:set_modem_info clearing "
2525 "Z DTR\n");
2526#endif
2527 }
2528 cy_writel(&ch_ctrl->rs_control, rs);
2529 retval = cyz_issue_cmd(card, channel, C_CM_IOCTLM, 0L);
2530 spin_unlock_irqrestore(&card->card_lock, flags);
2531 if (retval != 0) {
2532 printk(KERN_ERR "cyc:set_modem_info retval on ttyC%d "
2533 "was %x\n", info->line, retval);
2534 }
2535 }
2536 return 0;
2537}
2538
2539/*
2540 * cy_break() --- routine which turns the break handling on or off
2541 */
2542static int cy_break(struct tty_struct *tty, int break_state)
2543{
2544 struct cyclades_port *info = tty->driver_data;
2545 struct cyclades_card *card;
2546 unsigned long flags;
2547 int retval = 0;
2548
2549 if (serial_paranoia_check(info, tty->name, "cy_break"))
2550 return -EINVAL;
2551
2552 card = info->card;
2553
2554 spin_lock_irqsave(&card->card_lock, flags);
2555 if (!cy_is_Z(card)) {
2556 /* Let the transmit ISR take care of this (since it
2557 requires stuffing characters into the output stream).
2558 */
2559 if (break_state == -1) {
2560 if (!info->breakon) {
2561 info->breakon = 1;
2562 if (!info->xmit_cnt) {
2563 spin_unlock_irqrestore(&card->card_lock, flags);
2564 start_xmit(info);
2565 spin_lock_irqsave(&card->card_lock, flags);
2566 }
2567 }
2568 } else {
2569 if (!info->breakoff) {
2570 info->breakoff = 1;
2571 if (!info->xmit_cnt) {
2572 spin_unlock_irqrestore(&card->card_lock, flags);
2573 start_xmit(info);
2574 spin_lock_irqsave(&card->card_lock, flags);
2575 }
2576 }
2577 }
2578 } else {
2579 if (break_state == -1) {
2580 retval = cyz_issue_cmd(card,
2581 info->line - card->first_line,
2582 C_CM_SET_BREAK, 0L);
2583 if (retval != 0) {
2584 printk(KERN_ERR "cyc:cy_break (set) retval on "
2585 "ttyC%d was %x\n", info->line, retval);
2586 }
2587 } else {
2588 retval = cyz_issue_cmd(card,
2589 info->line - card->first_line,
2590 C_CM_CLR_BREAK, 0L);
2591 if (retval != 0) {
2592 printk(KERN_DEBUG "cyc:cy_break (clr) retval "
2593 "on ttyC%d was %x\n", info->line,
2594 retval);
2595 }
2596 }
2597 }
2598 spin_unlock_irqrestore(&card->card_lock, flags);
2599 return retval;
2600} /* cy_break */
2601
2602static int set_threshold(struct cyclades_port *info, unsigned long value)
2603{
2604 struct cyclades_card *card = info->card;
2605 unsigned long flags;
2606
2607 if (!cy_is_Z(card)) {
2608 info->cor3 &= ~CyREC_FIFO;
2609 info->cor3 |= value & CyREC_FIFO;
2610
2611 spin_lock_irqsave(&card->card_lock, flags);
2612 cyy_writeb(info, CyCOR3, info->cor3);
2613 cyy_issue_cmd(info, CyCOR_CHANGE | CyCOR3ch);
2614 spin_unlock_irqrestore(&card->card_lock, flags);
2615 }
2616 return 0;
2617} /* set_threshold */
2618
2619static int get_threshold(struct cyclades_port *info,
2620 unsigned long __user *value)
2621{
2622 struct cyclades_card *card = info->card;
2623
2624 if (!cy_is_Z(card)) {
2625 u8 tmp = cyy_readb(info, CyCOR3) & CyREC_FIFO;
2626 return put_user(tmp, value);
2627 }
2628 return 0;
2629} /* get_threshold */
2630
2631static int set_timeout(struct cyclades_port *info, unsigned long value)
2632{
2633 struct cyclades_card *card = info->card;
2634 unsigned long flags;
2635
2636 if (!cy_is_Z(card)) {
2637 spin_lock_irqsave(&card->card_lock, flags);
2638 cyy_writeb(info, CyRTPR, value & 0xff);
2639 spin_unlock_irqrestore(&card->card_lock, flags);
2640 }
2641 return 0;
2642} /* set_timeout */
2643
2644static int get_timeout(struct cyclades_port *info,
2645 unsigned long __user *value)
2646{
2647 struct cyclades_card *card = info->card;
2648
2649 if (!cy_is_Z(card)) {
2650 u8 tmp = cyy_readb(info, CyRTPR);
2651 return put_user(tmp, value);
2652 }
2653 return 0;
2654} /* get_timeout */
2655
2656static int cy_cflags_changed(struct cyclades_port *info, unsigned long arg,
2657 struct cyclades_icount *cprev)
2658{
2659 struct cyclades_icount cnow;
2660 unsigned long flags;
2661 int ret;
2662
2663 spin_lock_irqsave(&info->card->card_lock, flags);
2664 cnow = info->icount; /* atomic copy */
2665 spin_unlock_irqrestore(&info->card->card_lock, flags);
2666
2667 ret = ((arg & TIOCM_RNG) && (cnow.rng != cprev->rng)) ||
2668 ((arg & TIOCM_DSR) && (cnow.dsr != cprev->dsr)) ||
2669 ((arg & TIOCM_CD) && (cnow.dcd != cprev->dcd)) ||
2670 ((arg & TIOCM_CTS) && (cnow.cts != cprev->cts));
2671
2672 *cprev = cnow;
2673
2674 return ret;
2675}
2676
2677/*
2678 * This routine allows the tty driver to implement device-
2679 * specific ioctl's. If the ioctl number passed in cmd is
2680 * not recognized by the driver, it should return ENOIOCTLCMD.
2681 */
2682static int
2683cy_ioctl(struct tty_struct *tty, struct file *file,
2684 unsigned int cmd, unsigned long arg)
2685{
2686 struct cyclades_port *info = tty->driver_data;
2687 struct cyclades_icount cnow; /* kernel counter temps */
2688 int ret_val = 0;
2689 unsigned long flags;
2690 void __user *argp = (void __user *)arg;
2691
2692 if (serial_paranoia_check(info, tty->name, "cy_ioctl"))
2693 return -ENODEV;
2694
2695#ifdef CY_DEBUG_OTHER
2696 printk(KERN_DEBUG "cyc:cy_ioctl ttyC%d, cmd = %x arg = %lx\n",
2697 info->line, cmd, arg);
2698#endif
2699
2700 switch (cmd) {
2701 case CYGETMON:
2702 if (copy_to_user(argp, &info->mon, sizeof(info->mon))) {
2703 ret_val = -EFAULT;
2704 break;
2705 }
2706 memset(&info->mon, 0, sizeof(info->mon));
2707 break;
2708 case CYGETTHRESH:
2709 ret_val = get_threshold(info, argp);
2710 break;
2711 case CYSETTHRESH:
2712 ret_val = set_threshold(info, arg);
2713 break;
2714 case CYGETDEFTHRESH:
2715 ret_val = put_user(info->default_threshold,
2716 (unsigned long __user *)argp);
2717 break;
2718 case CYSETDEFTHRESH:
2719 info->default_threshold = arg & 0x0f;
2720 break;
2721 case CYGETTIMEOUT:
2722 ret_val = get_timeout(info, argp);
2723 break;
2724 case CYSETTIMEOUT:
2725 ret_val = set_timeout(info, arg);
2726 break;
2727 case CYGETDEFTIMEOUT:
2728 ret_val = put_user(info->default_timeout,
2729 (unsigned long __user *)argp);
2730 break;
2731 case CYSETDEFTIMEOUT:
2732 info->default_timeout = arg & 0xff;
2733 break;
2734 case CYSETRFLOW:
2735 info->rflow = (int)arg;
2736 break;
2737 case CYGETRFLOW:
2738 ret_val = info->rflow;
2739 break;
2740 case CYSETRTSDTR_INV:
2741 info->rtsdtr_inv = (int)arg;
2742 break;
2743 case CYGETRTSDTR_INV:
2744 ret_val = info->rtsdtr_inv;
2745 break;
2746 case CYGETCD1400VER:
2747 ret_val = info->chip_rev;
2748 break;
2749#ifndef CONFIG_CYZ_INTR
2750 case CYZSETPOLLCYCLE:
2751 cyz_polling_cycle = (arg * HZ) / 1000;
2752 break;
2753 case CYZGETPOLLCYCLE:
2754 ret_val = (cyz_polling_cycle * 1000) / HZ;
2755 break;
2756#endif /* CONFIG_CYZ_INTR */
2757 case CYSETWAIT:
2758 info->port.closing_wait = (unsigned short)arg * HZ / 100;
2759 break;
2760 case CYGETWAIT:
2761 ret_val = info->port.closing_wait / (HZ / 100);
2762 break;
2763 case TIOCGSERIAL:
2764 ret_val = cy_get_serial_info(info, argp);
2765 break;
2766 case TIOCSSERIAL:
2767 ret_val = cy_set_serial_info(info, tty, argp);
2768 break;
2769 case TIOCSERGETLSR: /* Get line status register */
2770 ret_val = get_lsr_info(info, argp);
2771 break;
2772 /*
2773 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
2774 * - mask passed in arg for lines of interest
2775 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
2776 * Caller should use TIOCGICOUNT to see which one it was
2777 */
2778 case TIOCMIWAIT:
2779 spin_lock_irqsave(&info->card->card_lock, flags);
2780 /* note the counters on entry */
2781 cnow = info->icount;
2782 spin_unlock_irqrestore(&info->card->card_lock, flags);
2783 ret_val = wait_event_interruptible(info->port.delta_msr_wait,
2784 cy_cflags_changed(info, arg, &cnow));
2785 break;
2786
2787 /*
2788 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
2789 * Return: write counters to the user passed counter struct
2790 * NB: both 1->0 and 0->1 transitions are counted except for
2791 * RI where only 0->1 is counted.
2792 */
2793 case TIOCGICOUNT: {
2794 struct serial_icounter_struct sic = { };
2795
2796 spin_lock_irqsave(&info->card->card_lock, flags);
2797 cnow = info->icount;
2798 spin_unlock_irqrestore(&info->card->card_lock, flags);
2799
2800 sic.cts = cnow.cts;
2801 sic.dsr = cnow.dsr;
2802 sic.rng = cnow.rng;
2803 sic.dcd = cnow.dcd;
2804 sic.rx = cnow.rx;
2805 sic.tx = cnow.tx;
2806 sic.frame = cnow.frame;
2807 sic.overrun = cnow.overrun;
2808 sic.parity = cnow.parity;
2809 sic.brk = cnow.brk;
2810 sic.buf_overrun = cnow.buf_overrun;
2811
2812 if (copy_to_user(argp, &sic, sizeof(sic)))
2813 ret_val = -EFAULT;
2814 break;
2815 }
2816 default:
2817 ret_val = -ENOIOCTLCMD;
2818 }
2819
2820#ifdef CY_DEBUG_OTHER
2821 printk(KERN_DEBUG "cyc:cy_ioctl done\n");
2822#endif
2823 return ret_val;
2824} /* cy_ioctl */
2825
2826/*
2827 * This routine allows the tty driver to be notified when
2828 * device's termios settings have changed. Note that a
2829 * well-designed tty driver should be prepared to accept the case
2830 * where old == NULL, and try to do something rational.
2831 */
2832static void cy_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
2833{
2834 struct cyclades_port *info = tty->driver_data;
2835
2836#ifdef CY_DEBUG_OTHER
2837 printk(KERN_DEBUG "cyc:cy_set_termios ttyC%d\n", info->line);
2838#endif
2839
2840 cy_set_line_char(info, tty);
2841
2842 if ((old_termios->c_cflag & CRTSCTS) &&
2843 !(tty->termios->c_cflag & CRTSCTS)) {
2844 tty->hw_stopped = 0;
2845 cy_start(tty);
2846 }
2847#if 0
2848 /*
2849 * No need to wake up processes in open wait, since they
2850 * sample the CLOCAL flag once, and don't recheck it.
2851 * XXX It's not clear whether the current behavior is correct
2852 * or not. Hence, this may change.....
2853 */
2854 if (!(old_termios->c_cflag & CLOCAL) &&
2855 (tty->termios->c_cflag & CLOCAL))
2856 wake_up_interruptible(&info->port.open_wait);
2857#endif
2858} /* cy_set_termios */
2859
2860/* This function is used to send a high-priority XON/XOFF character to
2861 the device.
2862*/
2863static void cy_send_xchar(struct tty_struct *tty, char ch)
2864{
2865 struct cyclades_port *info = tty->driver_data;
2866 struct cyclades_card *card;
2867 int channel;
2868
2869 if (serial_paranoia_check(info, tty->name, "cy_send_xchar"))
2870 return;
2871
2872 info->x_char = ch;
2873
2874 if (ch)
2875 cy_start(tty);
2876
2877 card = info->card;
2878 channel = info->line - card->first_line;
2879
2880 if (cy_is_Z(card)) {
2881 if (ch == STOP_CHAR(tty))
2882 cyz_issue_cmd(card, channel, C_CM_SENDXOFF, 0L);
2883 else if (ch == START_CHAR(tty))
2884 cyz_issue_cmd(card, channel, C_CM_SENDXON, 0L);
2885 }
2886}
2887
2888/* This routine is called by the upper-layer tty layer to signal
2889 that incoming characters should be throttled because the input
2890 buffers are close to full.
2891 */
2892static void cy_throttle(struct tty_struct *tty)
2893{
2894 struct cyclades_port *info = tty->driver_data;
2895 struct cyclades_card *card;
2896 unsigned long flags;
2897
2898#ifdef CY_DEBUG_THROTTLE
2899 char buf[64];
2900
2901 printk(KERN_DEBUG "cyc:throttle %s: %ld...ttyC%d\n", tty_name(tty, buf),
2902 tty->ldisc.chars_in_buffer(tty), info->line);
2903#endif
2904
2905 if (serial_paranoia_check(info, tty->name, "cy_throttle"))
2906 return;
2907
2908 card = info->card;
2909
2910 if (I_IXOFF(tty)) {
2911 if (!cy_is_Z(card))
2912 cy_send_xchar(tty, STOP_CHAR(tty));
2913 else
2914 info->throttle = 1;
2915 }
2916
2917 if (tty->termios->c_cflag & CRTSCTS) {
2918 if (!cy_is_Z(card)) {
2919 spin_lock_irqsave(&card->card_lock, flags);
2920 cyy_change_rts_dtr(info, 0, TIOCM_RTS);
2921 spin_unlock_irqrestore(&card->card_lock, flags);
2922 } else {
2923 info->throttle = 1;
2924 }
2925 }
2926} /* cy_throttle */
2927
2928/*
2929 * This routine notifies the tty driver that it should signal
2930 * that characters can now be sent to the tty without fear of
2931 * overrunning the input buffers of the line disciplines.
2932 */
2933static void cy_unthrottle(struct tty_struct *tty)
2934{
2935 struct cyclades_port *info = tty->driver_data;
2936 struct cyclades_card *card;
2937 unsigned long flags;
2938
2939#ifdef CY_DEBUG_THROTTLE
2940 char buf[64];
2941
2942 printk(KERN_DEBUG "cyc:unthrottle %s: %ld...ttyC%d\n",
2943 tty_name(tty, buf), tty_chars_in_buffer(tty), info->line);
2944#endif
2945
2946 if (serial_paranoia_check(info, tty->name, "cy_unthrottle"))
2947 return;
2948
2949 if (I_IXOFF(tty)) {
2950 if (info->x_char)
2951 info->x_char = 0;
2952 else
2953 cy_send_xchar(tty, START_CHAR(tty));
2954 }
2955
2956 if (tty->termios->c_cflag & CRTSCTS) {
2957 card = info->card;
2958 if (!cy_is_Z(card)) {
2959 spin_lock_irqsave(&card->card_lock, flags);
2960 cyy_change_rts_dtr(info, TIOCM_RTS, 0);
2961 spin_unlock_irqrestore(&card->card_lock, flags);
2962 } else {
2963 info->throttle = 0;
2964 }
2965 }
2966} /* cy_unthrottle */
2967
2968/* cy_start and cy_stop provide software output flow control as a
2969 function of XON/XOFF, software CTS, and other such stuff.
2970*/
2971static void cy_stop(struct tty_struct *tty)
2972{
2973 struct cyclades_card *cinfo;
2974 struct cyclades_port *info = tty->driver_data;
2975 int channel;
2976 unsigned long flags;
2977
2978#ifdef CY_DEBUG_OTHER
2979 printk(KERN_DEBUG "cyc:cy_stop ttyC%d\n", info->line);
2980#endif
2981
2982 if (serial_paranoia_check(info, tty->name, "cy_stop"))
2983 return;
2984
2985 cinfo = info->card;
2986 channel = info->line - cinfo->first_line;
2987 if (!cy_is_Z(cinfo)) {
2988 spin_lock_irqsave(&cinfo->card_lock, flags);
2989 cyy_writeb(info, CyCAR, channel & 0x03);
2990 cyy_writeb(info, CySRER, cyy_readb(info, CySRER) & ~CyTxRdy);
2991 spin_unlock_irqrestore(&cinfo->card_lock, flags);
2992 }
2993} /* cy_stop */
2994
2995static void cy_start(struct tty_struct *tty)
2996{
2997 struct cyclades_card *cinfo;
2998 struct cyclades_port *info = tty->driver_data;
2999 int channel;
3000 unsigned long flags;
3001
3002#ifdef CY_DEBUG_OTHER
3003 printk(KERN_DEBUG "cyc:cy_start ttyC%d\n", info->line);
3004#endif
3005
3006 if (serial_paranoia_check(info, tty->name, "cy_start"))
3007 return;
3008
3009 cinfo = info->card;
3010 channel = info->line - cinfo->first_line;
3011 if (!cy_is_Z(cinfo)) {
3012 spin_lock_irqsave(&cinfo->card_lock, flags);
3013 cyy_writeb(info, CyCAR, channel & 0x03);
3014 cyy_writeb(info, CySRER, cyy_readb(info, CySRER) | CyTxRdy);
3015 spin_unlock_irqrestore(&cinfo->card_lock, flags);
3016 }
3017} /* cy_start */
3018
3019/*
3020 * cy_hangup() --- called by tty_hangup() when a hangup is signaled.
3021 */
3022static void cy_hangup(struct tty_struct *tty)
3023{
3024 struct cyclades_port *info = tty->driver_data;
3025
3026#ifdef CY_DEBUG_OTHER
3027 printk(KERN_DEBUG "cyc:cy_hangup ttyC%d\n", info->line);
3028#endif
3029
3030 if (serial_paranoia_check(info, tty->name, "cy_hangup"))
3031 return;
3032
3033 cy_flush_buffer(tty);
3034 cy_shutdown(info, tty);
3035 tty_port_hangup(&info->port);
3036} /* cy_hangup */
3037
3038static int cyy_carrier_raised(struct tty_port *port)
3039{
3040 struct cyclades_port *info = container_of(port, struct cyclades_port,
3041 port);
3042 struct cyclades_card *cinfo = info->card;
3043 unsigned long flags;
3044 int channel = info->line - cinfo->first_line;
3045 u32 cd;
3046
3047 spin_lock_irqsave(&cinfo->card_lock, flags);
3048 cyy_writeb(info, CyCAR, channel & 0x03);
3049 cd = cyy_readb(info, CyMSVR1) & CyDCD;
3050 spin_unlock_irqrestore(&cinfo->card_lock, flags);
3051
3052 return cd;
3053}
3054
3055static void cyy_dtr_rts(struct tty_port *port, int raise)
3056{
3057 struct cyclades_port *info = container_of(port, struct cyclades_port,
3058 port);
3059 struct cyclades_card *cinfo = info->card;
3060 unsigned long flags;
3061
3062 spin_lock_irqsave(&cinfo->card_lock, flags);
3063 cyy_change_rts_dtr(info, raise ? TIOCM_RTS | TIOCM_DTR : 0,
3064 raise ? 0 : TIOCM_RTS | TIOCM_DTR);
3065 spin_unlock_irqrestore(&cinfo->card_lock, flags);
3066}
3067
3068static int cyz_carrier_raised(struct tty_port *port)
3069{
3070 struct cyclades_port *info = container_of(port, struct cyclades_port,
3071 port);
3072
3073 return readl(&info->u.cyz.ch_ctrl->rs_status) & C_RS_DCD;
3074}
3075
3076static void cyz_dtr_rts(struct tty_port *port, int raise)
3077{
3078 struct cyclades_port *info = container_of(port, struct cyclades_port,
3079 port);
3080 struct cyclades_card *cinfo = info->card;
3081 struct CH_CTRL __iomem *ch_ctrl = info->u.cyz.ch_ctrl;
3082 int ret, channel = info->line - cinfo->first_line;
3083 u32 rs;
3084
3085 rs = readl(&ch_ctrl->rs_control);
3086 if (raise)
3087 rs |= C_RS_RTS | C_RS_DTR;
3088 else
3089 rs &= ~(C_RS_RTS | C_RS_DTR);
3090 cy_writel(&ch_ctrl->rs_control, rs);
3091 ret = cyz_issue_cmd(cinfo, channel, C_CM_IOCTLM, 0L);
3092 if (ret != 0)
3093 printk(KERN_ERR "%s: retval on ttyC%d was %x\n",
3094 __func__, info->line, ret);
3095#ifdef CY_DEBUG_DTR
3096 printk(KERN_DEBUG "%s: raising Z DTR\n", __func__);
3097#endif
3098}
3099
3100static const struct tty_port_operations cyy_port_ops = {
3101 .carrier_raised = cyy_carrier_raised,
3102 .dtr_rts = cyy_dtr_rts,
3103 .shutdown = cy_do_close,
3104};
3105
3106static const struct tty_port_operations cyz_port_ops = {
3107 .carrier_raised = cyz_carrier_raised,
3108 .dtr_rts = cyz_dtr_rts,
3109 .shutdown = cy_do_close,
3110};
3111
3112/*
3113 * ---------------------------------------------------------------------
3114 * cy_init() and friends
3115 *
3116 * cy_init() is called at boot-time to initialize the serial driver.
3117 * ---------------------------------------------------------------------
3118 */
3119
3120static int __devinit cy_init_card(struct cyclades_card *cinfo)
3121{
3122 struct cyclades_port *info;
3123 unsigned int channel, port;
3124
3125 spin_lock_init(&cinfo->card_lock);
3126 cinfo->intr_enabled = 0;
3127
3128 cinfo->ports = kcalloc(cinfo->nports, sizeof(*cinfo->ports),
3129 GFP_KERNEL);
3130 if (cinfo->ports == NULL) {
3131 printk(KERN_ERR "Cyclades: cannot allocate ports\n");
3132 return -ENOMEM;
3133 }
3134
3135 for (channel = 0, port = cinfo->first_line; channel < cinfo->nports;
3136 channel++, port++) {
3137 info = &cinfo->ports[channel];
3138 tty_port_init(&info->port);
3139 info->magic = CYCLADES_MAGIC;
3140 info->card = cinfo;
3141 info->line = port;
3142
3143 info->port.closing_wait = CLOSING_WAIT_DELAY;
3144 info->port.close_delay = 5 * HZ / 10;
3145 info->port.flags = STD_COM_FLAGS;
3146 init_completion(&info->shutdown_wait);
3147
3148 if (cy_is_Z(cinfo)) {
3149 struct FIRM_ID *firm_id = cinfo->base_addr + ID_ADDRESS;
3150 struct ZFW_CTRL *zfw_ctrl;
3151
3152 info->port.ops = &cyz_port_ops;
3153 info->type = PORT_STARTECH;
3154
3155 zfw_ctrl = cinfo->base_addr +
3156 (readl(&firm_id->zfwctrl_addr) & 0xfffff);
3157 info->u.cyz.ch_ctrl = &zfw_ctrl->ch_ctrl[channel];
3158 info->u.cyz.buf_ctrl = &zfw_ctrl->buf_ctrl[channel];
3159
3160 if (cinfo->hw_ver == ZO_V1)
3161 info->xmit_fifo_size = CYZ_FIFO_SIZE;
3162 else
3163 info->xmit_fifo_size = 4 * CYZ_FIFO_SIZE;
3164#ifdef CONFIG_CYZ_INTR
3165 setup_timer(&cyz_rx_full_timer[port],
3166 cyz_rx_restart, (unsigned long)info);
3167#endif
3168 } else {
3169 unsigned short chip_number;
3170 int index = cinfo->bus_index;
3171
3172 info->port.ops = &cyy_port_ops;
3173 info->type = PORT_CIRRUS;
3174 info->xmit_fifo_size = CyMAX_CHAR_FIFO;
3175 info->cor1 = CyPARITY_NONE | Cy_1_STOP | Cy_8_BITS;
3176 info->cor2 = CyETC;
3177 info->cor3 = 0x08; /* _very_ small rcv threshold */
3178
3179 chip_number = channel / CyPORTS_PER_CHIP;
3180 info->u.cyy.base_addr = cinfo->base_addr +
3181 (cy_chip_offset[chip_number] << index);
3182 info->chip_rev = cyy_readb(info, CyGFRCR);
3183
3184 if (info->chip_rev >= CD1400_REV_J) {
3185 /* It is a CD1400 rev. J or later */
3186 info->tbpr = baud_bpr_60[13]; /* Tx BPR */
3187 info->tco = baud_co_60[13]; /* Tx CO */
3188 info->rbpr = baud_bpr_60[13]; /* Rx BPR */
3189 info->rco = baud_co_60[13]; /* Rx CO */
3190 info->rtsdtr_inv = 1;
3191 } else {
3192 info->tbpr = baud_bpr_25[13]; /* Tx BPR */
3193 info->tco = baud_co_25[13]; /* Tx CO */
3194 info->rbpr = baud_bpr_25[13]; /* Rx BPR */
3195 info->rco = baud_co_25[13]; /* Rx CO */
3196 info->rtsdtr_inv = 0;
3197 }
3198 info->read_status_mask = CyTIMEOUT | CySPECHAR |
3199 CyBREAK | CyPARITY | CyFRAME | CyOVERRUN;
3200 }
3201
3202 }
3203
3204#ifndef CONFIG_CYZ_INTR
3205 if (cy_is_Z(cinfo) && !timer_pending(&cyz_timerlist)) {
3206 mod_timer(&cyz_timerlist, jiffies + 1);
3207#ifdef CY_PCI_DEBUG
3208 printk(KERN_DEBUG "Cyclades-Z polling initialized\n");
3209#endif
3210 }
3211#endif
3212 return 0;
3213}
3214
3215/* initialize chips on Cyclom-Y card -- return number of valid
3216 chips (which is number of ports/4) */
3217static unsigned short __devinit cyy_init_card(void __iomem *true_base_addr,
3218 int index)
3219{
3220 unsigned int chip_number;
3221 void __iomem *base_addr;
3222
3223 cy_writeb(true_base_addr + (Cy_HwReset << index), 0);
3224 /* Cy_HwReset is 0x1400 */
3225 cy_writeb(true_base_addr + (Cy_ClrIntr << index), 0);
3226 /* Cy_ClrIntr is 0x1800 */
3227 udelay(500L);
3228
3229 for (chip_number = 0; chip_number < CyMAX_CHIPS_PER_CARD;
3230 chip_number++) {
3231 base_addr =
3232 true_base_addr + (cy_chip_offset[chip_number] << index);
3233 mdelay(1);
3234 if (readb(base_addr + (CyCCR << index)) != 0x00) {
3235 /*************
3236 printk(" chip #%d at %#6lx is never idle (CCR != 0)\n",
3237 chip_number, (unsigned long)base_addr);
3238 *************/
3239 return chip_number;
3240 }
3241
3242 cy_writeb(base_addr + (CyGFRCR << index), 0);
3243 udelay(10L);
3244
3245 /* The Cyclom-16Y does not decode address bit 9 and therefore
3246 cannot distinguish between references to chip 0 and a non-
3247 existent chip 4. If the preceding clearing of the supposed
3248 chip 4 GFRCR register appears at chip 0, there is no chip 4
3249 and this must be a Cyclom-16Y, not a Cyclom-32Ye.
3250 */
3251 if (chip_number == 4 && readb(true_base_addr +
3252 (cy_chip_offset[0] << index) +
3253 (CyGFRCR << index)) == 0) {
3254 return chip_number;
3255 }
3256
3257 cy_writeb(base_addr + (CyCCR << index), CyCHIP_RESET);
3258 mdelay(1);
3259
3260 if (readb(base_addr + (CyGFRCR << index)) == 0x00) {
3261 /*
3262 printk(" chip #%d at %#6lx is not responding ",
3263 chip_number, (unsigned long)base_addr);
3264 printk("(GFRCR stayed 0)\n",
3265 */
3266 return chip_number;
3267 }
3268 if ((0xf0 & (readb(base_addr + (CyGFRCR << index)))) !=
3269 0x40) {
3270 /*
3271 printk(" chip #%d at %#6lx is not valid (GFRCR == "
3272 "%#2x)\n",
3273 chip_number, (unsigned long)base_addr,
3274 base_addr[CyGFRCR<<index]);
3275 */
3276 return chip_number;
3277 }
3278 cy_writeb(base_addr + (CyGCR << index), CyCH0_SERIAL);
3279 if (readb(base_addr + (CyGFRCR << index)) >= CD1400_REV_J) {
3280 /* It is a CD1400 rev. J or later */
3281 /* Impossible to reach 5ms with this chip.
3282 Changed to 2ms instead (f = 500 Hz). */
3283 cy_writeb(base_addr + (CyPPR << index), CyCLOCK_60_2MS);
3284 } else {
3285 /* f = 200 Hz */
3286 cy_writeb(base_addr + (CyPPR << index), CyCLOCK_25_5MS);
3287 }
3288
3289 /*
3290 printk(" chip #%d at %#6lx is rev 0x%2x\n",
3291 chip_number, (unsigned long)base_addr,
3292 readb(base_addr+(CyGFRCR<<index)));
3293 */
3294 }
3295 return chip_number;
3296} /* cyy_init_card */
3297
3298/*
3299 * ---------------------------------------------------------------------
3300 * cy_detect_isa() - Probe for Cyclom-Y/ISA boards.
3301 * sets global variables and return the number of ISA boards found.
3302 * ---------------------------------------------------------------------
3303 */
3304static int __init cy_detect_isa(void)
3305{
3306#ifdef CONFIG_ISA
3307 unsigned short cy_isa_irq, nboard;
3308 void __iomem *cy_isa_address;
3309 unsigned short i, j, cy_isa_nchan;
3310 int isparam = 0;
3311
3312 nboard = 0;
3313
3314 /* Check for module parameters */
3315 for (i = 0; i < NR_CARDS; i++) {
3316 if (maddr[i] || i) {
3317 isparam = 1;
3318 cy_isa_addresses[i] = maddr[i];
3319 }
3320 if (!maddr[i])
3321 break;
3322 }
3323
3324 /* scan the address table probing for Cyclom-Y/ISA boards */
3325 for (i = 0; i < NR_ISA_ADDRS; i++) {
3326 unsigned int isa_address = cy_isa_addresses[i];
3327 if (isa_address == 0x0000)
3328 return nboard;
3329
3330 /* probe for CD1400... */
3331 cy_isa_address = ioremap_nocache(isa_address, CyISA_Ywin);
3332 if (cy_isa_address == NULL) {
3333 printk(KERN_ERR "Cyclom-Y/ISA: can't remap base "
3334 "address\n");
3335 continue;
3336 }
3337 cy_isa_nchan = CyPORTS_PER_CHIP *
3338 cyy_init_card(cy_isa_address, 0);
3339 if (cy_isa_nchan == 0) {
3340 iounmap(cy_isa_address);
3341 continue;
3342 }
3343
3344 if (isparam && i < NR_CARDS && irq[i])
3345 cy_isa_irq = irq[i];
3346 else
3347 /* find out the board's irq by probing */
3348 cy_isa_irq = detect_isa_irq(cy_isa_address);
3349 if (cy_isa_irq == 0) {
3350 printk(KERN_ERR "Cyclom-Y/ISA found at 0x%lx, but the "
3351 "IRQ could not be detected.\n",
3352 (unsigned long)cy_isa_address);
3353 iounmap(cy_isa_address);
3354 continue;
3355 }
3356
3357 if ((cy_next_channel + cy_isa_nchan) > NR_PORTS) {
3358 printk(KERN_ERR "Cyclom-Y/ISA found at 0x%lx, but no "
3359 "more channels are available. Change NR_PORTS "
3360 "in cyclades.c and recompile kernel.\n",
3361 (unsigned long)cy_isa_address);
3362 iounmap(cy_isa_address);
3363 return nboard;
3364 }
3365 /* fill the next cy_card structure available */
3366 for (j = 0; j < NR_CARDS; j++) {
3367 if (cy_card[j].base_addr == NULL)
3368 break;
3369 }
3370 if (j == NR_CARDS) { /* no more cy_cards available */
3371 printk(KERN_ERR "Cyclom-Y/ISA found at 0x%lx, but no "
3372 "more cards can be used. Change NR_CARDS in "
3373 "cyclades.c and recompile kernel.\n",
3374 (unsigned long)cy_isa_address);
3375 iounmap(cy_isa_address);
3376 return nboard;
3377 }
3378
3379 /* allocate IRQ */
3380 if (request_irq(cy_isa_irq, cyy_interrupt,
3381 IRQF_DISABLED, "Cyclom-Y", &cy_card[j])) {
3382 printk(KERN_ERR "Cyclom-Y/ISA found at 0x%lx, but "
3383 "could not allocate IRQ#%d.\n",
3384 (unsigned long)cy_isa_address, cy_isa_irq);
3385 iounmap(cy_isa_address);
3386 return nboard;
3387 }
3388
3389 /* set cy_card */
3390 cy_card[j].base_addr = cy_isa_address;
3391 cy_card[j].ctl_addr.p9050 = NULL;
3392 cy_card[j].irq = (int)cy_isa_irq;
3393 cy_card[j].bus_index = 0;
3394 cy_card[j].first_line = cy_next_channel;
3395 cy_card[j].num_chips = cy_isa_nchan / CyPORTS_PER_CHIP;
3396 cy_card[j].nports = cy_isa_nchan;
3397 if (cy_init_card(&cy_card[j])) {
3398 cy_card[j].base_addr = NULL;
3399 free_irq(cy_isa_irq, &cy_card[j]);
3400 iounmap(cy_isa_address);
3401 continue;
3402 }
3403 nboard++;
3404
3405 printk(KERN_INFO "Cyclom-Y/ISA #%d: 0x%lx-0x%lx, IRQ%d found: "
3406 "%d channels starting from port %d\n",
3407 j + 1, (unsigned long)cy_isa_address,
3408 (unsigned long)(cy_isa_address + (CyISA_Ywin - 1)),
3409 cy_isa_irq, cy_isa_nchan, cy_next_channel);
3410
3411 for (j = cy_next_channel;
3412 j < cy_next_channel + cy_isa_nchan; j++)
3413 tty_register_device(cy_serial_driver, j, NULL);
3414 cy_next_channel += cy_isa_nchan;
3415 }
3416 return nboard;
3417#else
3418 return 0;
3419#endif /* CONFIG_ISA */
3420} /* cy_detect_isa */
3421
3422#ifdef CONFIG_PCI
3423static inline int __devinit cyc_isfwstr(const char *str, unsigned int size)
3424{
3425 unsigned int a;
3426
3427 for (a = 0; a < size && *str; a++, str++)
3428 if (*str & 0x80)
3429 return -EINVAL;
3430
3431 for (; a < size; a++, str++)
3432 if (*str)
3433 return -EINVAL;
3434
3435 return 0;
3436}
3437
3438static inline void __devinit cyz_fpga_copy(void __iomem *fpga, const u8 *data,
3439 unsigned int size)
3440{
3441 for (; size > 0; size--) {
3442 cy_writel(fpga, *data++);
3443 udelay(10);
3444 }
3445}
3446
3447static void __devinit plx_init(struct pci_dev *pdev, int irq,
3448 struct RUNTIME_9060 __iomem *addr)
3449{
3450 /* Reset PLX */
3451 cy_writel(&addr->init_ctrl, readl(&addr->init_ctrl) | 0x40000000);
3452 udelay(100L);
3453 cy_writel(&addr->init_ctrl, readl(&addr->init_ctrl) & ~0x40000000);
3454
3455 /* Reload Config. Registers from EEPROM */
3456 cy_writel(&addr->init_ctrl, readl(&addr->init_ctrl) | 0x20000000);
3457 udelay(100L);
3458 cy_writel(&addr->init_ctrl, readl(&addr->init_ctrl) & ~0x20000000);
3459
3460 /* For some yet unknown reason, once the PLX9060 reloads the EEPROM,
3461 * the IRQ is lost and, thus, we have to re-write it to the PCI config.
3462 * registers. This will remain here until we find a permanent fix.
3463 */
3464 pci_write_config_byte(pdev, PCI_INTERRUPT_LINE, irq);
3465}
3466
3467static int __devinit __cyz_load_fw(const struct firmware *fw,
3468 const char *name, const u32 mailbox, void __iomem *base,
3469 void __iomem *fpga)
3470{
3471 const void *ptr = fw->data;
3472 const struct zfile_header *h = ptr;
3473 const struct zfile_config *c, *cs;
3474 const struct zfile_block *b, *bs;
3475 unsigned int a, tmp, len = fw->size;
3476#define BAD_FW KERN_ERR "Bad firmware: "
3477 if (len < sizeof(*h)) {
3478 printk(BAD_FW "too short: %u<%zu\n", len, sizeof(*h));
3479 return -EINVAL;
3480 }
3481
3482 cs = ptr + h->config_offset;
3483 bs = ptr + h->block_offset;
3484
3485 if ((void *)(cs + h->n_config) > ptr + len ||
3486 (void *)(bs + h->n_blocks) > ptr + len) {
3487 printk(BAD_FW "too short");
3488 return -EINVAL;
3489 }
3490
3491 if (cyc_isfwstr(h->name, sizeof(h->name)) ||
3492 cyc_isfwstr(h->date, sizeof(h->date))) {
3493 printk(BAD_FW "bad formatted header string\n");
3494 return -EINVAL;
3495 }
3496
3497 if (strncmp(name, h->name, sizeof(h->name))) {
3498 printk(BAD_FW "bad name '%s' (expected '%s')\n", h->name, name);
3499 return -EINVAL;
3500 }
3501
3502 tmp = 0;
3503 for (c = cs; c < cs + h->n_config; c++) {
3504 for (a = 0; a < c->n_blocks; a++)
3505 if (c->block_list[a] > h->n_blocks) {
3506 printk(BAD_FW "bad block ref number in cfgs\n");
3507 return -EINVAL;
3508 }
3509 if (c->mailbox == mailbox && c->function == 0) /* 0 is normal */
3510 tmp++;
3511 }
3512 if (!tmp) {
3513 printk(BAD_FW "nothing appropriate\n");
3514 return -EINVAL;
3515 }
3516
3517 for (b = bs; b < bs + h->n_blocks; b++)
3518 if (b->file_offset + b->size > len) {
3519 printk(BAD_FW "bad block data offset\n");
3520 return -EINVAL;
3521 }
3522
3523 /* everything is OK, let's seek'n'load it */
3524 for (c = cs; c < cs + h->n_config; c++)
3525 if (c->mailbox == mailbox && c->function == 0)
3526 break;
3527
3528 for (a = 0; a < c->n_blocks; a++) {
3529 b = &bs[c->block_list[a]];
3530 if (b->type == ZBLOCK_FPGA) {
3531 if (fpga != NULL)
3532 cyz_fpga_copy(fpga, ptr + b->file_offset,
3533 b->size);
3534 } else {
3535 if (base != NULL)
3536 memcpy_toio(base + b->ram_offset,
3537 ptr + b->file_offset, b->size);
3538 }
3539 }
3540#undef BAD_FW
3541 return 0;
3542}
3543
3544static int __devinit cyz_load_fw(struct pci_dev *pdev, void __iomem *base_addr,
3545 struct RUNTIME_9060 __iomem *ctl_addr, int irq)
3546{
3547 const struct firmware *fw;
3548 struct FIRM_ID __iomem *fid = base_addr + ID_ADDRESS;
3549 struct CUSTOM_REG __iomem *cust = base_addr;
3550 struct ZFW_CTRL __iomem *pt_zfwctrl;
3551 void __iomem *tmp;
3552 u32 mailbox, status, nchan;
3553 unsigned int i;
3554 int retval;
3555
3556 retval = request_firmware(&fw, "cyzfirm.bin", &pdev->dev);
3557 if (retval) {
3558 dev_err(&pdev->dev, "can't get firmware\n");
3559 goto err;
3560 }
3561
3562 /* Check whether the firmware is already loaded and running. If
3563 positive, skip this board */
3564 if (__cyz_fpga_loaded(ctl_addr) && readl(&fid->signature) == ZFIRM_ID) {
3565 u32 cntval = readl(base_addr + 0x190);
3566
3567 udelay(100);
3568 if (cntval != readl(base_addr + 0x190)) {
3569 /* FW counter is working, FW is running */
3570 dev_dbg(&pdev->dev, "Cyclades-Z FW already loaded. "
3571 "Skipping board.\n");
3572 retval = 0;
3573 goto err_rel;
3574 }
3575 }
3576
3577 /* start boot */
3578 cy_writel(&ctl_addr->intr_ctrl_stat, readl(&ctl_addr->intr_ctrl_stat) &
3579 ~0x00030800UL);
3580
3581 mailbox = readl(&ctl_addr->mail_box_0);
3582
3583 if (mailbox == 0 || __cyz_fpga_loaded(ctl_addr)) {
3584 /* stops CPU and set window to beginning of RAM */
3585 cy_writel(&ctl_addr->loc_addr_base, WIN_CREG);
3586 cy_writel(&cust->cpu_stop, 0);
3587 cy_writel(&ctl_addr->loc_addr_base, WIN_RAM);
3588 udelay(100);
3589 }
3590
3591 plx_init(pdev, irq, ctl_addr);
3592
3593 if (mailbox != 0) {
3594 /* load FPGA */
3595 retval = __cyz_load_fw(fw, "Cyclom-Z", mailbox, NULL,
3596 base_addr);
3597 if (retval)
3598 goto err_rel;
3599 if (!__cyz_fpga_loaded(ctl_addr)) {
3600 dev_err(&pdev->dev, "fw upload successful, but fw is "
3601 "not loaded\n");
3602 goto err_rel;
3603 }
3604 }
3605
3606 /* stops CPU and set window to beginning of RAM */
3607 cy_writel(&ctl_addr->loc_addr_base, WIN_CREG);
3608 cy_writel(&cust->cpu_stop, 0);
3609 cy_writel(&ctl_addr->loc_addr_base, WIN_RAM);
3610 udelay(100);
3611
3612 /* clear memory */
3613 for (tmp = base_addr; tmp < base_addr + RAM_SIZE; tmp++)
3614 cy_writeb(tmp, 255);
3615 if (mailbox != 0) {
3616 /* set window to last 512K of RAM */
3617 cy_writel(&ctl_addr->loc_addr_base, WIN_RAM + RAM_SIZE);
3618 for (tmp = base_addr; tmp < base_addr + RAM_SIZE; tmp++)
3619 cy_writeb(tmp, 255);
3620 /* set window to beginning of RAM */
3621 cy_writel(&ctl_addr->loc_addr_base, WIN_RAM);
3622 }
3623
3624 retval = __cyz_load_fw(fw, "Cyclom-Z", mailbox, base_addr, NULL);
3625 release_firmware(fw);
3626 if (retval)
3627 goto err;
3628
3629 /* finish boot and start boards */
3630 cy_writel(&ctl_addr->loc_addr_base, WIN_CREG);
3631 cy_writel(&cust->cpu_start, 0);
3632 cy_writel(&ctl_addr->loc_addr_base, WIN_RAM);
3633 i = 0;
3634 while ((status = readl(&fid->signature)) != ZFIRM_ID && i++ < 40)
3635 msleep(100);
3636 if (status != ZFIRM_ID) {
3637 if (status == ZFIRM_HLT) {
3638 dev_err(&pdev->dev, "you need an external power supply "
3639 "for this number of ports. Firmware halted and "
3640 "board reset.\n");
3641 retval = -EIO;
3642 goto err;
3643 }
3644 dev_warn(&pdev->dev, "fid->signature = 0x%x... Waiting "
3645 "some more time\n", status);
3646 while ((status = readl(&fid->signature)) != ZFIRM_ID &&
3647 i++ < 200)
3648 msleep(100);
3649 if (status != ZFIRM_ID) {
3650 dev_err(&pdev->dev, "Board not started in 20 seconds! "
3651 "Giving up. (fid->signature = 0x%x)\n",
3652 status);
3653 dev_info(&pdev->dev, "*** Warning ***: if you are "
3654 "upgrading the FW, please power cycle the "
3655 "system before loading the new FW to the "
3656 "Cyclades-Z.\n");
3657
3658 if (__cyz_fpga_loaded(ctl_addr))
3659 plx_init(pdev, irq, ctl_addr);
3660
3661 retval = -EIO;
3662 goto err;
3663 }
3664 dev_dbg(&pdev->dev, "Firmware started after %d seconds.\n",
3665 i / 10);
3666 }
3667 pt_zfwctrl = base_addr + readl(&fid->zfwctrl_addr);
3668
3669 dev_dbg(&pdev->dev, "fid=> %p, zfwctrl_addr=> %x, npt_zfwctrl=> %p\n",
3670 base_addr + ID_ADDRESS, readl(&fid->zfwctrl_addr),
3671 base_addr + readl(&fid->zfwctrl_addr));
3672
3673 nchan = readl(&pt_zfwctrl->board_ctrl.n_channel);
3674 dev_info(&pdev->dev, "Cyclades-Z FW loaded: version = %x, ports = %u\n",
3675 readl(&pt_zfwctrl->board_ctrl.fw_version), nchan);
3676
3677 if (nchan == 0) {
3678 dev_warn(&pdev->dev, "no Cyclades-Z ports were found. Please "
3679 "check the connection between the Z host card and the "
3680 "serial expanders.\n");
3681
3682 if (__cyz_fpga_loaded(ctl_addr))
3683 plx_init(pdev, irq, ctl_addr);
3684
3685 dev_info(&pdev->dev, "Null number of ports detected. Board "
3686 "reset.\n");
3687 retval = 0;
3688 goto err;
3689 }
3690
3691 cy_writel(&pt_zfwctrl->board_ctrl.op_system, C_OS_LINUX);
3692 cy_writel(&pt_zfwctrl->board_ctrl.dr_version, DRIVER_VERSION);
3693
3694 /*
3695 Early firmware failed to start looking for commands.
3696 This enables firmware interrupts for those commands.
3697 */
3698 cy_writel(&ctl_addr->intr_ctrl_stat, readl(&ctl_addr->intr_ctrl_stat) |
3699 (1 << 17));
3700 cy_writel(&ctl_addr->intr_ctrl_stat, readl(&ctl_addr->intr_ctrl_stat) |
3701 0x00030800UL);
3702
3703 return nchan;
3704err_rel:
3705 release_firmware(fw);
3706err:
3707 return retval;
3708}
3709
3710static int __devinit cy_pci_probe(struct pci_dev *pdev,
3711 const struct pci_device_id *ent)
3712{
3713 void __iomem *addr0 = NULL, *addr2 = NULL;
3714 char *card_name = NULL;
3715 u32 uninitialized_var(mailbox);
3716 unsigned int device_id, nchan = 0, card_no, i;
3717 unsigned char plx_ver;
3718 int retval, irq;
3719
3720 retval = pci_enable_device(pdev);
3721 if (retval) {
3722 dev_err(&pdev->dev, "cannot enable device\n");
3723 goto err;
3724 }
3725
3726 /* read PCI configuration area */
3727 irq = pdev->irq;
3728 device_id = pdev->device & ~PCI_DEVICE_ID_MASK;
3729
3730#if defined(__alpha__)
3731 if (device_id == PCI_DEVICE_ID_CYCLOM_Y_Lo) { /* below 1M? */
3732 dev_err(&pdev->dev, "Cyclom-Y/PCI not supported for low "
3733 "addresses on Alpha systems.\n");
3734 retval = -EIO;
3735 goto err_dis;
3736 }
3737#endif
3738 if (device_id == PCI_DEVICE_ID_CYCLOM_Z_Lo) {
3739 dev_err(&pdev->dev, "Cyclades-Z/PCI not supported for low "
3740 "addresses\n");
3741 retval = -EIO;
3742 goto err_dis;
3743 }
3744
3745 if (pci_resource_flags(pdev, 2) & IORESOURCE_IO) {
3746 dev_warn(&pdev->dev, "PCI I/O bit incorrectly set. Ignoring "
3747 "it...\n");
3748 pdev->resource[2].flags &= ~IORESOURCE_IO;
3749 }
3750
3751 retval = pci_request_regions(pdev, "cyclades");
3752 if (retval) {
3753 dev_err(&pdev->dev, "failed to reserve resources\n");
3754 goto err_dis;
3755 }
3756
3757 retval = -EIO;
3758 if (device_id == PCI_DEVICE_ID_CYCLOM_Y_Lo ||
3759 device_id == PCI_DEVICE_ID_CYCLOM_Y_Hi) {
3760 card_name = "Cyclom-Y";
3761
3762 addr0 = ioremap_nocache(pci_resource_start(pdev, 0),
3763 CyPCI_Yctl);
3764 if (addr0 == NULL) {
3765 dev_err(&pdev->dev, "can't remap ctl region\n");
3766 goto err_reg;
3767 }
3768 addr2 = ioremap_nocache(pci_resource_start(pdev, 2),
3769 CyPCI_Ywin);
3770 if (addr2 == NULL) {
3771 dev_err(&pdev->dev, "can't remap base region\n");
3772 goto err_unmap;
3773 }
3774
3775 nchan = CyPORTS_PER_CHIP * cyy_init_card(addr2, 1);
3776 if (nchan == 0) {
3777 dev_err(&pdev->dev, "Cyclom-Y PCI host card with no "
3778 "Serial-Modules\n");
3779 goto err_unmap;
3780 }
3781 } else if (device_id == PCI_DEVICE_ID_CYCLOM_Z_Hi) {
3782 struct RUNTIME_9060 __iomem *ctl_addr;
3783
3784 ctl_addr = addr0 = ioremap_nocache(pci_resource_start(pdev, 0),
3785 CyPCI_Zctl);
3786 if (addr0 == NULL) {
3787 dev_err(&pdev->dev, "can't remap ctl region\n");
3788 goto err_reg;
3789 }
3790
3791 /* Disable interrupts on the PLX before resetting it */
3792 cy_writew(&ctl_addr->intr_ctrl_stat,
3793 readw(&ctl_addr->intr_ctrl_stat) & ~0x0900);
3794
3795 plx_init(pdev, irq, addr0);
3796
3797 mailbox = readl(&ctl_addr->mail_box_0);
3798
3799 addr2 = ioremap_nocache(pci_resource_start(pdev, 2),
3800 mailbox == ZE_V1 ? CyPCI_Ze_win : CyPCI_Zwin);
3801 if (addr2 == NULL) {
3802 dev_err(&pdev->dev, "can't remap base region\n");
3803 goto err_unmap;
3804 }
3805
3806 if (mailbox == ZE_V1) {
3807 card_name = "Cyclades-Ze";
3808 } else {
3809 card_name = "Cyclades-8Zo";
3810#ifdef CY_PCI_DEBUG
3811 if (mailbox == ZO_V1) {
3812 cy_writel(&ctl_addr->loc_addr_base, WIN_CREG);
3813 dev_info(&pdev->dev, "Cyclades-8Zo/PCI: FPGA "
3814 "id %lx, ver %lx\n", (ulong)(0xff &
3815 readl(&((struct CUSTOM_REG *)addr2)->
3816 fpga_id)), (ulong)(0xff &
3817 readl(&((struct CUSTOM_REG *)addr2)->
3818 fpga_version)));
3819 cy_writel(&ctl_addr->loc_addr_base, WIN_RAM);
3820 } else {
3821 dev_info(&pdev->dev, "Cyclades-Z/PCI: New "
3822 "Cyclades-Z board. FPGA not loaded\n");
3823 }
3824#endif
3825 /* The following clears the firmware id word. This
3826 ensures that the driver will not attempt to talk to
3827 the board until it has been properly initialized.
3828 */
3829 if ((mailbox == ZO_V1) || (mailbox == ZO_V2))
3830 cy_writel(addr2 + ID_ADDRESS, 0L);
3831 }
3832
3833 retval = cyz_load_fw(pdev, addr2, addr0, irq);
3834 if (retval <= 0)
3835 goto err_unmap;
3836 nchan = retval;
3837 }
3838
3839 if ((cy_next_channel + nchan) > NR_PORTS) {
3840 dev_err(&pdev->dev, "Cyclades-8Zo/PCI found, but no "
3841 "channels are available. Change NR_PORTS in "
3842 "cyclades.c and recompile kernel.\n");
3843 goto err_unmap;
3844 }
3845 /* fill the next cy_card structure available */
3846 for (card_no = 0; card_no < NR_CARDS; card_no++) {
3847 if (cy_card[card_no].base_addr == NULL)
3848 break;
3849 }
3850 if (card_no == NR_CARDS) { /* no more cy_cards available */
3851 dev_err(&pdev->dev, "Cyclades-8Zo/PCI found, but no "
3852 "more cards can be used. Change NR_CARDS in "
3853 "cyclades.c and recompile kernel.\n");
3854 goto err_unmap;
3855 }
3856
3857 if (device_id == PCI_DEVICE_ID_CYCLOM_Y_Lo ||
3858 device_id == PCI_DEVICE_ID_CYCLOM_Y_Hi) {
3859 /* allocate IRQ */
3860 retval = request_irq(irq, cyy_interrupt,
3861 IRQF_SHARED, "Cyclom-Y", &cy_card[card_no]);
3862 if (retval) {
3863 dev_err(&pdev->dev, "could not allocate IRQ\n");
3864 goto err_unmap;
3865 }
3866 cy_card[card_no].num_chips = nchan / CyPORTS_PER_CHIP;
3867 } else {
3868 struct FIRM_ID __iomem *firm_id = addr2 + ID_ADDRESS;
3869 struct ZFW_CTRL __iomem *zfw_ctrl;
3870
3871 zfw_ctrl = addr2 + (readl(&firm_id->zfwctrl_addr) & 0xfffff);
3872
3873 cy_card[card_no].hw_ver = mailbox;
3874 cy_card[card_no].num_chips = (unsigned int)-1;
3875 cy_card[card_no].board_ctrl = &zfw_ctrl->board_ctrl;
3876#ifdef CONFIG_CYZ_INTR
3877 /* allocate IRQ only if board has an IRQ */
3878 if (irq != 0 && irq != 255) {
3879 retval = request_irq(irq, cyz_interrupt,
3880 IRQF_SHARED, "Cyclades-Z",
3881 &cy_card[card_no]);
3882 if (retval) {
3883 dev_err(&pdev->dev, "could not allocate IRQ\n");
3884 goto err_unmap;
3885 }
3886 }
3887#endif /* CONFIG_CYZ_INTR */
3888 }
3889
3890 /* set cy_card */
3891 cy_card[card_no].base_addr = addr2;
3892 cy_card[card_no].ctl_addr.p9050 = addr0;
3893 cy_card[card_no].irq = irq;
3894 cy_card[card_no].bus_index = 1;
3895 cy_card[card_no].first_line = cy_next_channel;
3896 cy_card[card_no].nports = nchan;
3897 retval = cy_init_card(&cy_card[card_no]);
3898 if (retval)
3899 goto err_null;
3900
3901 pci_set_drvdata(pdev, &cy_card[card_no]);
3902
3903 if (device_id == PCI_DEVICE_ID_CYCLOM_Y_Lo ||
3904 device_id == PCI_DEVICE_ID_CYCLOM_Y_Hi) {
3905 /* enable interrupts in the PCI interface */
3906 plx_ver = readb(addr2 + CyPLX_VER) & 0x0f;
3907 switch (plx_ver) {
3908 case PLX_9050:
3909 cy_writeb(addr0 + 0x4c, 0x43);
3910 break;
3911
3912 case PLX_9060:
3913 case PLX_9080:
3914 default: /* Old boards, use PLX_9060 */
3915 {
3916 struct RUNTIME_9060 __iomem *ctl_addr = addr0;
3917 plx_init(pdev, irq, ctl_addr);
3918 cy_writew(&ctl_addr->intr_ctrl_stat,
3919 readw(&ctl_addr->intr_ctrl_stat) | 0x0900);
3920 break;
3921 }
3922 }
3923 }
3924
3925 dev_info(&pdev->dev, "%s/PCI #%d found: %d channels starting from "
3926 "port %d.\n", card_name, card_no + 1, nchan, cy_next_channel);
3927 for (i = cy_next_channel; i < cy_next_channel + nchan; i++)
3928 tty_register_device(cy_serial_driver, i, &pdev->dev);
3929 cy_next_channel += nchan;
3930
3931 return 0;
3932err_null:
3933 cy_card[card_no].base_addr = NULL;
3934 free_irq(irq, &cy_card[card_no]);
3935err_unmap:
3936 iounmap(addr0);
3937 if (addr2)
3938 iounmap(addr2);
3939err_reg:
3940 pci_release_regions(pdev);
3941err_dis:
3942 pci_disable_device(pdev);
3943err:
3944 return retval;
3945}
3946
3947static void __devexit cy_pci_remove(struct pci_dev *pdev)
3948{
3949 struct cyclades_card *cinfo = pci_get_drvdata(pdev);
3950 unsigned int i;
3951
3952 /* non-Z with old PLX */
3953 if (!cy_is_Z(cinfo) && (readb(cinfo->base_addr + CyPLX_VER) & 0x0f) ==
3954 PLX_9050)
3955 cy_writeb(cinfo->ctl_addr.p9050 + 0x4c, 0);
3956 else
3957#ifndef CONFIG_CYZ_INTR
3958 if (!cy_is_Z(cinfo))
3959#endif
3960 cy_writew(&cinfo->ctl_addr.p9060->intr_ctrl_stat,
3961 readw(&cinfo->ctl_addr.p9060->intr_ctrl_stat) &
3962 ~0x0900);
3963
3964 iounmap(cinfo->base_addr);
3965 if (cinfo->ctl_addr.p9050)
3966 iounmap(cinfo->ctl_addr.p9050);
3967 if (cinfo->irq
3968#ifndef CONFIG_CYZ_INTR
3969 && !cy_is_Z(cinfo)
3970#endif /* CONFIG_CYZ_INTR */
3971 )
3972 free_irq(cinfo->irq, cinfo);
3973 pci_release_regions(pdev);
3974
3975 cinfo->base_addr = NULL;
3976 for (i = cinfo->first_line; i < cinfo->first_line +
3977 cinfo->nports; i++)
3978 tty_unregister_device(cy_serial_driver, i);
3979 cinfo->nports = 0;
3980 kfree(cinfo->ports);
3981}
3982
3983static struct pci_driver cy_pci_driver = {
3984 .name = "cyclades",
3985 .id_table = cy_pci_dev_id,
3986 .probe = cy_pci_probe,
3987 .remove = __devexit_p(cy_pci_remove)
3988};
3989#endif
3990
3991static int cyclades_proc_show(struct seq_file *m, void *v)
3992{
3993 struct cyclades_port *info;
3994 unsigned int i, j;
3995 __u32 cur_jifs = jiffies;
3996
3997 seq_puts(m, "Dev TimeOpen BytesOut IdleOut BytesIn "
3998 "IdleIn Overruns Ldisc\n");
3999
4000 /* Output one line for each known port */
4001 for (i = 0; i < NR_CARDS; i++)
4002 for (j = 0; j < cy_card[i].nports; j++) {
4003 info = &cy_card[i].ports[j];
4004
4005 if (info->port.count) {
4006 /* XXX is the ldisc num worth this? */
4007 struct tty_struct *tty;
4008 struct tty_ldisc *ld;
4009 int num = 0;
4010 tty = tty_port_tty_get(&info->port);
4011 if (tty) {
4012 ld = tty_ldisc_ref(tty);
4013 if (ld) {
4014 num = ld->ops->num;
4015 tty_ldisc_deref(ld);
4016 }
4017 tty_kref_put(tty);
4018 }
4019 seq_printf(m, "%3d %8lu %10lu %8lu "
4020 "%10lu %8lu %9lu %6d\n", info->line,
4021 (cur_jifs - info->idle_stats.in_use) /
4022 HZ, info->idle_stats.xmit_bytes,
4023 (cur_jifs - info->idle_stats.xmit_idle)/
4024 HZ, info->idle_stats.recv_bytes,
4025 (cur_jifs - info->idle_stats.recv_idle)/
4026 HZ, info->idle_stats.overruns,
4027 num);
4028 } else
4029 seq_printf(m, "%3d %8lu %10lu %8lu "
4030 "%10lu %8lu %9lu %6ld\n",
4031 info->line, 0L, 0L, 0L, 0L, 0L, 0L, 0L);
4032 }
4033 return 0;
4034}
4035
4036static int cyclades_proc_open(struct inode *inode, struct file *file)
4037{
4038 return single_open(file, cyclades_proc_show, NULL);
4039}
4040
4041static const struct file_operations cyclades_proc_fops = {
4042 .owner = THIS_MODULE,
4043 .open = cyclades_proc_open,
4044 .read = seq_read,
4045 .llseek = seq_lseek,
4046 .release = single_release,
4047};
4048
4049/* The serial driver boot-time initialization code!
4050 Hardware I/O ports are mapped to character special devices on a
4051 first found, first allocated manner. That is, this code searches
4052 for Cyclom cards in the system. As each is found, it is probed
4053 to discover how many chips (and thus how many ports) are present.
4054 These ports are mapped to the tty ports 32 and upward in monotonic
4055 fashion. If an 8-port card is replaced with a 16-port card, the
4056 port mapping on a following card will shift.
4057
4058 This approach is different from what is used in the other serial
4059 device driver because the Cyclom is more properly a multiplexer,
4060 not just an aggregation of serial ports on one card.
4061
4062 If there are more cards with more ports than have been
4063 statically allocated above, a warning is printed and the
4064 extra ports are ignored.
4065 */
4066
4067static const struct tty_operations cy_ops = {
4068 .open = cy_open,
4069 .close = cy_close,
4070 .write = cy_write,
4071 .put_char = cy_put_char,
4072 .flush_chars = cy_flush_chars,
4073 .write_room = cy_write_room,
4074 .chars_in_buffer = cy_chars_in_buffer,
4075 .flush_buffer = cy_flush_buffer,
4076 .ioctl = cy_ioctl,
4077 .throttle = cy_throttle,
4078 .unthrottle = cy_unthrottle,
4079 .set_termios = cy_set_termios,
4080 .stop = cy_stop,
4081 .start = cy_start,
4082 .hangup = cy_hangup,
4083 .break_ctl = cy_break,
4084 .wait_until_sent = cy_wait_until_sent,
4085 .tiocmget = cy_tiocmget,
4086 .tiocmset = cy_tiocmset,
4087 .proc_fops = &cyclades_proc_fops,
4088};
4089
4090static int __init cy_init(void)
4091{
4092 unsigned int nboards;
4093 int retval = -ENOMEM;
4094
4095 cy_serial_driver = alloc_tty_driver(NR_PORTS);
4096 if (!cy_serial_driver)
4097 goto err;
4098
4099 printk(KERN_INFO "Cyclades driver " CY_VERSION " (built %s %s)\n",
4100 __DATE__, __TIME__);
4101
4102 /* Initialize the tty_driver structure */
4103
4104 cy_serial_driver->owner = THIS_MODULE;
4105 cy_serial_driver->driver_name = "cyclades";
4106 cy_serial_driver->name = "ttyC";
4107 cy_serial_driver->major = CYCLADES_MAJOR;
4108 cy_serial_driver->minor_start = 0;
4109 cy_serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
4110 cy_serial_driver->subtype = SERIAL_TYPE_NORMAL;
4111 cy_serial_driver->init_termios = tty_std_termios;
4112 cy_serial_driver->init_termios.c_cflag =
4113 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
4114 cy_serial_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
4115 tty_set_operations(cy_serial_driver, &cy_ops);
4116
4117 retval = tty_register_driver(cy_serial_driver);
4118 if (retval) {
4119 printk(KERN_ERR "Couldn't register Cyclades serial driver\n");
4120 goto err_frtty;
4121 }
4122
4123 /* the code below is responsible to find the boards. Each different
4124 type of board has its own detection routine. If a board is found,
4125 the next cy_card structure available is set by the detection
4126 routine. These functions are responsible for checking the
4127 availability of cy_card and cy_port data structures and updating
4128 the cy_next_channel. */
4129
4130 /* look for isa boards */
4131 nboards = cy_detect_isa();
4132
4133#ifdef CONFIG_PCI
4134 /* look for pci boards */
4135 retval = pci_register_driver(&cy_pci_driver);
4136 if (retval && !nboards) {
4137 tty_unregister_driver(cy_serial_driver);
4138 goto err_frtty;
4139 }
4140#endif
4141
4142 return 0;
4143err_frtty:
4144 put_tty_driver(cy_serial_driver);
4145err:
4146 return retval;
4147} /* cy_init */
4148
4149static void __exit cy_cleanup_module(void)
4150{
4151 struct cyclades_card *card;
4152 unsigned int i, e1;
4153
4154#ifndef CONFIG_CYZ_INTR
4155 del_timer_sync(&cyz_timerlist);
4156#endif /* CONFIG_CYZ_INTR */
4157
4158 e1 = tty_unregister_driver(cy_serial_driver);
4159 if (e1)
4160 printk(KERN_ERR "failed to unregister Cyclades serial "
4161 "driver(%d)\n", e1);
4162
4163#ifdef CONFIG_PCI
4164 pci_unregister_driver(&cy_pci_driver);
4165#endif
4166
4167 for (i = 0; i < NR_CARDS; i++) {
4168 card = &cy_card[i];
4169 if (card->base_addr) {
4170 /* clear interrupt */
4171 cy_writeb(card->base_addr + Cy_ClrIntr, 0);
4172 iounmap(card->base_addr);
4173 if (card->ctl_addr.p9050)
4174 iounmap(card->ctl_addr.p9050);
4175 if (card->irq
4176#ifndef CONFIG_CYZ_INTR
4177 && !cy_is_Z(card)
4178#endif /* CONFIG_CYZ_INTR */
4179 )
4180 free_irq(card->irq, card);
4181 for (e1 = card->first_line; e1 < card->first_line +
4182 card->nports; e1++)
4183 tty_unregister_device(cy_serial_driver, e1);
4184 kfree(card->ports);
4185 }
4186 }
4187
4188 put_tty_driver(cy_serial_driver);
4189} /* cy_cleanup_module */
4190
4191module_init(cy_init);
4192module_exit(cy_cleanup_module);
4193
4194MODULE_LICENSE("GPL");
4195MODULE_VERSION(CY_VERSION);
4196MODULE_ALIAS_CHARDEV_MAJOR(CYCLADES_MAJOR);
4197MODULE_FIRMWARE("cyzfirm.bin");
diff --git a/drivers/char/defkeymap.c_shipped b/drivers/char/defkeymap.c_shipped
deleted file mode 100644
index d2208dfe3f67..000000000000
--- a/drivers/char/defkeymap.c_shipped
+++ /dev/null
@@ -1,262 +0,0 @@
1/* Do not edit this file! It was automatically generated by */
2/* loadkeys --mktable defkeymap.map > defkeymap.c */
3
4#include <linux/types.h>
5#include <linux/keyboard.h>
6#include <linux/kd.h>
7
8u_short plain_map[NR_KEYS] = {
9 0xf200, 0xf01b, 0xf031, 0xf032, 0xf033, 0xf034, 0xf035, 0xf036,
10 0xf037, 0xf038, 0xf039, 0xf030, 0xf02d, 0xf03d, 0xf07f, 0xf009,
11 0xfb71, 0xfb77, 0xfb65, 0xfb72, 0xfb74, 0xfb79, 0xfb75, 0xfb69,
12 0xfb6f, 0xfb70, 0xf05b, 0xf05d, 0xf201, 0xf702, 0xfb61, 0xfb73,
13 0xfb64, 0xfb66, 0xfb67, 0xfb68, 0xfb6a, 0xfb6b, 0xfb6c, 0xf03b,
14 0xf027, 0xf060, 0xf700, 0xf05c, 0xfb7a, 0xfb78, 0xfb63, 0xfb76,
15 0xfb62, 0xfb6e, 0xfb6d, 0xf02c, 0xf02e, 0xf02f, 0xf700, 0xf30c,
16 0xf703, 0xf020, 0xf207, 0xf100, 0xf101, 0xf102, 0xf103, 0xf104,
17 0xf105, 0xf106, 0xf107, 0xf108, 0xf109, 0xf208, 0xf209, 0xf307,
18 0xf308, 0xf309, 0xf30b, 0xf304, 0xf305, 0xf306, 0xf30a, 0xf301,
19 0xf302, 0xf303, 0xf300, 0xf310, 0xf206, 0xf200, 0xf03c, 0xf10a,
20 0xf10b, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200,
21 0xf30e, 0xf702, 0xf30d, 0xf01c, 0xf701, 0xf205, 0xf114, 0xf603,
22 0xf118, 0xf601, 0xf602, 0xf117, 0xf600, 0xf119, 0xf115, 0xf116,
23 0xf11a, 0xf10c, 0xf10d, 0xf11b, 0xf11c, 0xf110, 0xf311, 0xf11d,
24 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200,
25};
26
27u_short shift_map[NR_KEYS] = {
28 0xf200, 0xf01b, 0xf021, 0xf040, 0xf023, 0xf024, 0xf025, 0xf05e,
29 0xf026, 0xf02a, 0xf028, 0xf029, 0xf05f, 0xf02b, 0xf07f, 0xf009,
30 0xfb51, 0xfb57, 0xfb45, 0xfb52, 0xfb54, 0xfb59, 0xfb55, 0xfb49,
31 0xfb4f, 0xfb50, 0xf07b, 0xf07d, 0xf201, 0xf702, 0xfb41, 0xfb53,
32 0xfb44, 0xfb46, 0xfb47, 0xfb48, 0xfb4a, 0xfb4b, 0xfb4c, 0xf03a,
33 0xf022, 0xf07e, 0xf700, 0xf07c, 0xfb5a, 0xfb58, 0xfb43, 0xfb56,
34 0xfb42, 0xfb4e, 0xfb4d, 0xf03c, 0xf03e, 0xf03f, 0xf700, 0xf30c,
35 0xf703, 0xf020, 0xf207, 0xf10a, 0xf10b, 0xf10c, 0xf10d, 0xf10e,
36 0xf10f, 0xf110, 0xf111, 0xf112, 0xf113, 0xf213, 0xf203, 0xf307,
37 0xf308, 0xf309, 0xf30b, 0xf304, 0xf305, 0xf306, 0xf30a, 0xf301,
38 0xf302, 0xf303, 0xf300, 0xf310, 0xf206, 0xf200, 0xf03e, 0xf10a,
39 0xf10b, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200,
40 0xf30e, 0xf702, 0xf30d, 0xf200, 0xf701, 0xf205, 0xf114, 0xf603,
41 0xf20b, 0xf601, 0xf602, 0xf117, 0xf600, 0xf20a, 0xf115, 0xf116,
42 0xf11a, 0xf10c, 0xf10d, 0xf11b, 0xf11c, 0xf110, 0xf311, 0xf11d,
43 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200,
44};
45
46u_short altgr_map[NR_KEYS] = {
47 0xf200, 0xf200, 0xf200, 0xf040, 0xf200, 0xf024, 0xf200, 0xf200,
48 0xf07b, 0xf05b, 0xf05d, 0xf07d, 0xf05c, 0xf200, 0xf200, 0xf200,
49 0xfb71, 0xfb77, 0xf918, 0xfb72, 0xfb74, 0xfb79, 0xfb75, 0xfb69,
50 0xfb6f, 0xfb70, 0xf200, 0xf07e, 0xf201, 0xf702, 0xf914, 0xfb73,
51 0xf917, 0xf919, 0xfb67, 0xfb68, 0xfb6a, 0xfb6b, 0xfb6c, 0xf200,
52 0xf200, 0xf200, 0xf700, 0xf200, 0xfb7a, 0xfb78, 0xf916, 0xfb76,
53 0xf915, 0xfb6e, 0xfb6d, 0xf200, 0xf200, 0xf200, 0xf700, 0xf30c,
54 0xf703, 0xf200, 0xf207, 0xf50c, 0xf50d, 0xf50e, 0xf50f, 0xf510,
55 0xf511, 0xf512, 0xf513, 0xf514, 0xf515, 0xf208, 0xf202, 0xf911,
56 0xf912, 0xf913, 0xf30b, 0xf90e, 0xf90f, 0xf910, 0xf30a, 0xf90b,
57 0xf90c, 0xf90d, 0xf90a, 0xf310, 0xf206, 0xf200, 0xf07c, 0xf516,
58 0xf517, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200,
59 0xf30e, 0xf702, 0xf30d, 0xf200, 0xf701, 0xf205, 0xf114, 0xf603,
60 0xf118, 0xf601, 0xf602, 0xf117, 0xf600, 0xf119, 0xf115, 0xf116,
61 0xf11a, 0xf10c, 0xf10d, 0xf11b, 0xf11c, 0xf110, 0xf311, 0xf11d,
62 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200,
63};
64
65u_short ctrl_map[NR_KEYS] = {
66 0xf200, 0xf200, 0xf200, 0xf000, 0xf01b, 0xf01c, 0xf01d, 0xf01e,
67 0xf01f, 0xf07f, 0xf200, 0xf200, 0xf01f, 0xf200, 0xf008, 0xf200,
68 0xf011, 0xf017, 0xf005, 0xf012, 0xf014, 0xf019, 0xf015, 0xf009,
69 0xf00f, 0xf010, 0xf01b, 0xf01d, 0xf201, 0xf702, 0xf001, 0xf013,
70 0xf004, 0xf006, 0xf007, 0xf008, 0xf00a, 0xf00b, 0xf00c, 0xf200,
71 0xf007, 0xf000, 0xf700, 0xf01c, 0xf01a, 0xf018, 0xf003, 0xf016,
72 0xf002, 0xf00e, 0xf00d, 0xf200, 0xf20e, 0xf07f, 0xf700, 0xf30c,
73 0xf703, 0xf000, 0xf207, 0xf100, 0xf101, 0xf102, 0xf103, 0xf104,
74 0xf105, 0xf106, 0xf107, 0xf108, 0xf109, 0xf208, 0xf204, 0xf307,
75 0xf308, 0xf309, 0xf30b, 0xf304, 0xf305, 0xf306, 0xf30a, 0xf301,
76 0xf302, 0xf303, 0xf300, 0xf310, 0xf206, 0xf200, 0xf200, 0xf10a,
77 0xf10b, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200,
78 0xf30e, 0xf702, 0xf30d, 0xf01c, 0xf701, 0xf205, 0xf114, 0xf603,
79 0xf118, 0xf601, 0xf602, 0xf117, 0xf600, 0xf119, 0xf115, 0xf116,
80 0xf11a, 0xf10c, 0xf10d, 0xf11b, 0xf11c, 0xf110, 0xf311, 0xf11d,
81 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200,
82};
83
84u_short shift_ctrl_map[NR_KEYS] = {
85 0xf200, 0xf200, 0xf200, 0xf000, 0xf200, 0xf200, 0xf200, 0xf200,
86 0xf200, 0xf200, 0xf200, 0xf200, 0xf01f, 0xf200, 0xf200, 0xf200,
87 0xf011, 0xf017, 0xf005, 0xf012, 0xf014, 0xf019, 0xf015, 0xf009,
88 0xf00f, 0xf010, 0xf200, 0xf200, 0xf201, 0xf702, 0xf001, 0xf013,
89 0xf004, 0xf006, 0xf007, 0xf008, 0xf00a, 0xf00b, 0xf00c, 0xf200,
90 0xf200, 0xf200, 0xf700, 0xf200, 0xf01a, 0xf018, 0xf003, 0xf016,
91 0xf002, 0xf00e, 0xf00d, 0xf200, 0xf200, 0xf200, 0xf700, 0xf30c,
92 0xf703, 0xf200, 0xf207, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200,
93 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf208, 0xf200, 0xf307,
94 0xf308, 0xf309, 0xf30b, 0xf304, 0xf305, 0xf306, 0xf30a, 0xf301,
95 0xf302, 0xf303, 0xf300, 0xf310, 0xf206, 0xf200, 0xf200, 0xf200,
96 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200,
97 0xf30e, 0xf702, 0xf30d, 0xf200, 0xf701, 0xf205, 0xf114, 0xf603,
98 0xf118, 0xf601, 0xf602, 0xf117, 0xf600, 0xf119, 0xf115, 0xf116,
99 0xf11a, 0xf10c, 0xf10d, 0xf11b, 0xf11c, 0xf110, 0xf311, 0xf11d,
100 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200,
101};
102
103u_short alt_map[NR_KEYS] = {
104 0xf200, 0xf81b, 0xf831, 0xf832, 0xf833, 0xf834, 0xf835, 0xf836,
105 0xf837, 0xf838, 0xf839, 0xf830, 0xf82d, 0xf83d, 0xf87f, 0xf809,
106 0xf871, 0xf877, 0xf865, 0xf872, 0xf874, 0xf879, 0xf875, 0xf869,
107 0xf86f, 0xf870, 0xf85b, 0xf85d, 0xf80d, 0xf702, 0xf861, 0xf873,
108 0xf864, 0xf866, 0xf867, 0xf868, 0xf86a, 0xf86b, 0xf86c, 0xf83b,
109 0xf827, 0xf860, 0xf700, 0xf85c, 0xf87a, 0xf878, 0xf863, 0xf876,
110 0xf862, 0xf86e, 0xf86d, 0xf82c, 0xf82e, 0xf82f, 0xf700, 0xf30c,
111 0xf703, 0xf820, 0xf207, 0xf500, 0xf501, 0xf502, 0xf503, 0xf504,
112 0xf505, 0xf506, 0xf507, 0xf508, 0xf509, 0xf208, 0xf209, 0xf907,
113 0xf908, 0xf909, 0xf30b, 0xf904, 0xf905, 0xf906, 0xf30a, 0xf901,
114 0xf902, 0xf903, 0xf900, 0xf310, 0xf206, 0xf200, 0xf83c, 0xf50a,
115 0xf50b, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200,
116 0xf30e, 0xf702, 0xf30d, 0xf01c, 0xf701, 0xf205, 0xf114, 0xf603,
117 0xf118, 0xf210, 0xf211, 0xf117, 0xf600, 0xf119, 0xf115, 0xf116,
118 0xf11a, 0xf10c, 0xf10d, 0xf11b, 0xf11c, 0xf110, 0xf311, 0xf11d,
119 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200,
120};
121
122u_short ctrl_alt_map[NR_KEYS] = {
123 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200,
124 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200,
125 0xf811, 0xf817, 0xf805, 0xf812, 0xf814, 0xf819, 0xf815, 0xf809,
126 0xf80f, 0xf810, 0xf200, 0xf200, 0xf201, 0xf702, 0xf801, 0xf813,
127 0xf804, 0xf806, 0xf807, 0xf808, 0xf80a, 0xf80b, 0xf80c, 0xf200,
128 0xf200, 0xf200, 0xf700, 0xf200, 0xf81a, 0xf818, 0xf803, 0xf816,
129 0xf802, 0xf80e, 0xf80d, 0xf200, 0xf200, 0xf200, 0xf700, 0xf30c,
130 0xf703, 0xf200, 0xf207, 0xf500, 0xf501, 0xf502, 0xf503, 0xf504,
131 0xf505, 0xf506, 0xf507, 0xf508, 0xf509, 0xf208, 0xf200, 0xf307,
132 0xf308, 0xf309, 0xf30b, 0xf304, 0xf305, 0xf306, 0xf30a, 0xf301,
133 0xf302, 0xf303, 0xf300, 0xf20c, 0xf206, 0xf200, 0xf200, 0xf50a,
134 0xf50b, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200,
135 0xf30e, 0xf702, 0xf30d, 0xf200, 0xf701, 0xf205, 0xf114, 0xf603,
136 0xf118, 0xf601, 0xf602, 0xf117, 0xf600, 0xf119, 0xf115, 0xf20c,
137 0xf11a, 0xf10c, 0xf10d, 0xf11b, 0xf11c, 0xf110, 0xf311, 0xf11d,
138 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200, 0xf200,
139};
140
141ushort *key_maps[MAX_NR_KEYMAPS] = {
142 plain_map, shift_map, altgr_map, NULL,
143 ctrl_map, shift_ctrl_map, NULL, NULL,
144 alt_map, NULL, NULL, NULL,
145 ctrl_alt_map, NULL
146};
147
148unsigned int keymap_count = 7;
149
150/*
151 * Philosophy: most people do not define more strings, but they who do
152 * often want quite a lot of string space. So, we statically allocate
153 * the default and allocate dynamically in chunks of 512 bytes.
154 */
155
156char func_buf[] = {
157 '\033', '[', '[', 'A', 0,
158 '\033', '[', '[', 'B', 0,
159 '\033', '[', '[', 'C', 0,
160 '\033', '[', '[', 'D', 0,
161 '\033', '[', '[', 'E', 0,
162 '\033', '[', '1', '7', '~', 0,
163 '\033', '[', '1', '8', '~', 0,
164 '\033', '[', '1', '9', '~', 0,
165 '\033', '[', '2', '0', '~', 0,
166 '\033', '[', '2', '1', '~', 0,
167 '\033', '[', '2', '3', '~', 0,
168 '\033', '[', '2', '4', '~', 0,
169 '\033', '[', '2', '5', '~', 0,
170 '\033', '[', '2', '6', '~', 0,
171 '\033', '[', '2', '8', '~', 0,
172 '\033', '[', '2', '9', '~', 0,
173 '\033', '[', '3', '1', '~', 0,
174 '\033', '[', '3', '2', '~', 0,
175 '\033', '[', '3', '3', '~', 0,
176 '\033', '[', '3', '4', '~', 0,
177 '\033', '[', '1', '~', 0,
178 '\033', '[', '2', '~', 0,
179 '\033', '[', '3', '~', 0,
180 '\033', '[', '4', '~', 0,
181 '\033', '[', '5', '~', 0,
182 '\033', '[', '6', '~', 0,
183 '\033', '[', 'M', 0,
184 '\033', '[', 'P', 0,
185};
186
187char *funcbufptr = func_buf;
188int funcbufsize = sizeof(func_buf);
189int funcbufleft = 0; /* space left */
190
191char *func_table[MAX_NR_FUNC] = {
192 func_buf + 0,
193 func_buf + 5,
194 func_buf + 10,
195 func_buf + 15,
196 func_buf + 20,
197 func_buf + 25,
198 func_buf + 31,
199 func_buf + 37,
200 func_buf + 43,
201 func_buf + 49,
202 func_buf + 55,
203 func_buf + 61,
204 func_buf + 67,
205 func_buf + 73,
206 func_buf + 79,
207 func_buf + 85,
208 func_buf + 91,
209 func_buf + 97,
210 func_buf + 103,
211 func_buf + 109,
212 func_buf + 115,
213 func_buf + 120,
214 func_buf + 125,
215 func_buf + 130,
216 func_buf + 135,
217 func_buf + 140,
218 func_buf + 145,
219 NULL,
220 NULL,
221 func_buf + 149,
222 NULL,
223};
224
225struct kbdiacruc accent_table[MAX_DIACR] = {
226 {'`', 'A', 0300}, {'`', 'a', 0340},
227 {'\'', 'A', 0301}, {'\'', 'a', 0341},
228 {'^', 'A', 0302}, {'^', 'a', 0342},
229 {'~', 'A', 0303}, {'~', 'a', 0343},
230 {'"', 'A', 0304}, {'"', 'a', 0344},
231 {'O', 'A', 0305}, {'o', 'a', 0345},
232 {'0', 'A', 0305}, {'0', 'a', 0345},
233 {'A', 'A', 0305}, {'a', 'a', 0345},
234 {'A', 'E', 0306}, {'a', 'e', 0346},
235 {',', 'C', 0307}, {',', 'c', 0347},
236 {'`', 'E', 0310}, {'`', 'e', 0350},
237 {'\'', 'E', 0311}, {'\'', 'e', 0351},
238 {'^', 'E', 0312}, {'^', 'e', 0352},
239 {'"', 'E', 0313}, {'"', 'e', 0353},
240 {'`', 'I', 0314}, {'`', 'i', 0354},
241 {'\'', 'I', 0315}, {'\'', 'i', 0355},
242 {'^', 'I', 0316}, {'^', 'i', 0356},
243 {'"', 'I', 0317}, {'"', 'i', 0357},
244 {'-', 'D', 0320}, {'-', 'd', 0360},
245 {'~', 'N', 0321}, {'~', 'n', 0361},
246 {'`', 'O', 0322}, {'`', 'o', 0362},
247 {'\'', 'O', 0323}, {'\'', 'o', 0363},
248 {'^', 'O', 0324}, {'^', 'o', 0364},
249 {'~', 'O', 0325}, {'~', 'o', 0365},
250 {'"', 'O', 0326}, {'"', 'o', 0366},
251 {'/', 'O', 0330}, {'/', 'o', 0370},
252 {'`', 'U', 0331}, {'`', 'u', 0371},
253 {'\'', 'U', 0332}, {'\'', 'u', 0372},
254 {'^', 'U', 0333}, {'^', 'u', 0373},
255 {'"', 'U', 0334}, {'"', 'u', 0374},
256 {'\'', 'Y', 0335}, {'\'', 'y', 0375},
257 {'T', 'H', 0336}, {'t', 'h', 0376},
258 {'s', 's', 0337}, {'"', 'y', 0377},
259 {'s', 'z', 0337}, {'i', 'j', 0377},
260};
261
262unsigned int accent_table_size = 68;
diff --git a/drivers/char/defkeymap.map b/drivers/char/defkeymap.map
deleted file mode 100644
index 50b30cace261..000000000000
--- a/drivers/char/defkeymap.map
+++ /dev/null
@@ -1,357 +0,0 @@
1# Default kernel keymap. This uses 7 modifier combinations.
2keymaps 0-2,4-5,8,12
3# Change the above line into
4# keymaps 0-2,4-6,8,12
5# in case you want the entries
6# altgr control keycode 83 = Boot
7# altgr control keycode 111 = Boot
8# below.
9#
10# In fact AltGr is used very little, and one more keymap can
11# be saved by mapping AltGr to Alt (and adapting a few entries):
12# keycode 100 = Alt
13#
14keycode 1 = Escape Escape
15 alt keycode 1 = Meta_Escape
16keycode 2 = one exclam
17 alt keycode 2 = Meta_one
18keycode 3 = two at at
19 control keycode 3 = nul
20 shift control keycode 3 = nul
21 alt keycode 3 = Meta_two
22keycode 4 = three numbersign
23 control keycode 4 = Escape
24 alt keycode 4 = Meta_three
25keycode 5 = four dollar dollar
26 control keycode 5 = Control_backslash
27 alt keycode 5 = Meta_four
28keycode 6 = five percent
29 control keycode 6 = Control_bracketright
30 alt keycode 6 = Meta_five
31keycode 7 = six asciicircum
32 control keycode 7 = Control_asciicircum
33 alt keycode 7 = Meta_six
34keycode 8 = seven ampersand braceleft
35 control keycode 8 = Control_underscore
36 alt keycode 8 = Meta_seven
37keycode 9 = eight asterisk bracketleft
38 control keycode 9 = Delete
39 alt keycode 9 = Meta_eight
40keycode 10 = nine parenleft bracketright
41 alt keycode 10 = Meta_nine
42keycode 11 = zero parenright braceright
43 alt keycode 11 = Meta_zero
44keycode 12 = minus underscore backslash
45 control keycode 12 = Control_underscore
46 shift control keycode 12 = Control_underscore
47 alt keycode 12 = Meta_minus
48keycode 13 = equal plus
49 alt keycode 13 = Meta_equal
50keycode 14 = Delete Delete
51 control keycode 14 = BackSpace
52 alt keycode 14 = Meta_Delete
53keycode 15 = Tab Tab
54 alt keycode 15 = Meta_Tab
55keycode 16 = q
56keycode 17 = w
57keycode 18 = e
58 altgr keycode 18 = Hex_E
59keycode 19 = r
60keycode 20 = t
61keycode 21 = y
62keycode 22 = u
63keycode 23 = i
64keycode 24 = o
65keycode 25 = p
66keycode 26 = bracketleft braceleft
67 control keycode 26 = Escape
68 alt keycode 26 = Meta_bracketleft
69keycode 27 = bracketright braceright asciitilde
70 control keycode 27 = Control_bracketright
71 alt keycode 27 = Meta_bracketright
72keycode 28 = Return
73 alt keycode 28 = Meta_Control_m
74keycode 29 = Control
75keycode 30 = a
76 altgr keycode 30 = Hex_A
77keycode 31 = s
78keycode 32 = d
79 altgr keycode 32 = Hex_D
80keycode 33 = f
81 altgr keycode 33 = Hex_F
82keycode 34 = g
83keycode 35 = h
84keycode 36 = j
85keycode 37 = k
86keycode 38 = l
87keycode 39 = semicolon colon
88 alt keycode 39 = Meta_semicolon
89keycode 40 = apostrophe quotedbl
90 control keycode 40 = Control_g
91 alt keycode 40 = Meta_apostrophe
92keycode 41 = grave asciitilde
93 control keycode 41 = nul
94 alt keycode 41 = Meta_grave
95keycode 42 = Shift
96keycode 43 = backslash bar
97 control keycode 43 = Control_backslash
98 alt keycode 43 = Meta_backslash
99keycode 44 = z
100keycode 45 = x
101keycode 46 = c
102 altgr keycode 46 = Hex_C
103keycode 47 = v
104keycode 48 = b
105 altgr keycode 48 = Hex_B
106keycode 49 = n
107keycode 50 = m
108keycode 51 = comma less
109 alt keycode 51 = Meta_comma
110keycode 52 = period greater
111 control keycode 52 = Compose
112 alt keycode 52 = Meta_period
113keycode 53 = slash question
114 control keycode 53 = Delete
115 alt keycode 53 = Meta_slash
116keycode 54 = Shift
117keycode 55 = KP_Multiply
118keycode 56 = Alt
119keycode 57 = space space
120 control keycode 57 = nul
121 alt keycode 57 = Meta_space
122keycode 58 = Caps_Lock
123keycode 59 = F1 F11 Console_13
124 control keycode 59 = F1
125 alt keycode 59 = Console_1
126 control alt keycode 59 = Console_1
127keycode 60 = F2 F12 Console_14
128 control keycode 60 = F2
129 alt keycode 60 = Console_2
130 control alt keycode 60 = Console_2
131keycode 61 = F3 F13 Console_15
132 control keycode 61 = F3
133 alt keycode 61 = Console_3
134 control alt keycode 61 = Console_3
135keycode 62 = F4 F14 Console_16
136 control keycode 62 = F4
137 alt keycode 62 = Console_4
138 control alt keycode 62 = Console_4
139keycode 63 = F5 F15 Console_17
140 control keycode 63 = F5
141 alt keycode 63 = Console_5
142 control alt keycode 63 = Console_5
143keycode 64 = F6 F16 Console_18
144 control keycode 64 = F6
145 alt keycode 64 = Console_6
146 control alt keycode 64 = Console_6
147keycode 65 = F7 F17 Console_19
148 control keycode 65 = F7
149 alt keycode 65 = Console_7
150 control alt keycode 65 = Console_7
151keycode 66 = F8 F18 Console_20
152 control keycode 66 = F8
153 alt keycode 66 = Console_8
154 control alt keycode 66 = Console_8
155keycode 67 = F9 F19 Console_21
156 control keycode 67 = F9
157 alt keycode 67 = Console_9
158 control alt keycode 67 = Console_9
159keycode 68 = F10 F20 Console_22
160 control keycode 68 = F10
161 alt keycode 68 = Console_10
162 control alt keycode 68 = Console_10
163keycode 69 = Num_Lock
164 shift keycode 69 = Bare_Num_Lock
165keycode 70 = Scroll_Lock Show_Memory Show_Registers
166 control keycode 70 = Show_State
167 alt keycode 70 = Scroll_Lock
168keycode 71 = KP_7
169 alt keycode 71 = Ascii_7
170 altgr keycode 71 = Hex_7
171keycode 72 = KP_8
172 alt keycode 72 = Ascii_8
173 altgr keycode 72 = Hex_8
174keycode 73 = KP_9
175 alt keycode 73 = Ascii_9
176 altgr keycode 73 = Hex_9
177keycode 74 = KP_Subtract
178keycode 75 = KP_4
179 alt keycode 75 = Ascii_4
180 altgr keycode 75 = Hex_4
181keycode 76 = KP_5
182 alt keycode 76 = Ascii_5
183 altgr keycode 76 = Hex_5
184keycode 77 = KP_6
185 alt keycode 77 = Ascii_6
186 altgr keycode 77 = Hex_6
187keycode 78 = KP_Add
188keycode 79 = KP_1
189 alt keycode 79 = Ascii_1
190 altgr keycode 79 = Hex_1
191keycode 80 = KP_2
192 alt keycode 80 = Ascii_2
193 altgr keycode 80 = Hex_2
194keycode 81 = KP_3
195 alt keycode 81 = Ascii_3
196 altgr keycode 81 = Hex_3
197keycode 82 = KP_0
198 alt keycode 82 = Ascii_0
199 altgr keycode 82 = Hex_0
200keycode 83 = KP_Period
201# altgr control keycode 83 = Boot
202 control alt keycode 83 = Boot
203keycode 84 = Last_Console
204keycode 85 =
205keycode 86 = less greater bar
206 alt keycode 86 = Meta_less
207keycode 87 = F11 F11 Console_23
208 control keycode 87 = F11
209 alt keycode 87 = Console_11
210 control alt keycode 87 = Console_11
211keycode 88 = F12 F12 Console_24
212 control keycode 88 = F12
213 alt keycode 88 = Console_12
214 control alt keycode 88 = Console_12
215keycode 89 =
216keycode 90 =
217keycode 91 =
218keycode 92 =
219keycode 93 =
220keycode 94 =
221keycode 95 =
222keycode 96 = KP_Enter
223keycode 97 = Control
224keycode 98 = KP_Divide
225keycode 99 = Control_backslash
226 control keycode 99 = Control_backslash
227 alt keycode 99 = Control_backslash
228keycode 100 = AltGr
229keycode 101 = Break
230keycode 102 = Find
231keycode 103 = Up
232keycode 104 = Prior
233 shift keycode 104 = Scroll_Backward
234keycode 105 = Left
235 alt keycode 105 = Decr_Console
236keycode 106 = Right
237 alt keycode 106 = Incr_Console
238keycode 107 = Select
239keycode 108 = Down
240keycode 109 = Next
241 shift keycode 109 = Scroll_Forward
242keycode 110 = Insert
243keycode 111 = Remove
244# altgr control keycode 111 = Boot
245 control alt keycode 111 = Boot
246keycode 112 = Macro
247keycode 113 = F13
248keycode 114 = F14
249keycode 115 = Help
250keycode 116 = Do
251keycode 117 = F17
252keycode 118 = KP_MinPlus
253keycode 119 = Pause
254keycode 120 =
255keycode 121 =
256keycode 122 =
257keycode 123 =
258keycode 124 =
259keycode 125 =
260keycode 126 =
261keycode 127 =
262string F1 = "\033[[A"
263string F2 = "\033[[B"
264string F3 = "\033[[C"
265string F4 = "\033[[D"
266string F5 = "\033[[E"
267string F6 = "\033[17~"
268string F7 = "\033[18~"
269string F8 = "\033[19~"
270string F9 = "\033[20~"
271string F10 = "\033[21~"
272string F11 = "\033[23~"
273string F12 = "\033[24~"
274string F13 = "\033[25~"
275string F14 = "\033[26~"
276string F15 = "\033[28~"
277string F16 = "\033[29~"
278string F17 = "\033[31~"
279string F18 = "\033[32~"
280string F19 = "\033[33~"
281string F20 = "\033[34~"
282string Find = "\033[1~"
283string Insert = "\033[2~"
284string Remove = "\033[3~"
285string Select = "\033[4~"
286string Prior = "\033[5~"
287string Next = "\033[6~"
288string Macro = "\033[M"
289string Pause = "\033[P"
290compose '`' 'A' to 'À'
291compose '`' 'a' to 'à'
292compose '\'' 'A' to 'Á'
293compose '\'' 'a' to 'á'
294compose '^' 'A' to 'Â'
295compose '^' 'a' to 'â'
296compose '~' 'A' to 'Ã'
297compose '~' 'a' to 'ã'
298compose '"' 'A' to 'Ä'
299compose '"' 'a' to 'ä'
300compose 'O' 'A' to 'Å'
301compose 'o' 'a' to 'å'
302compose '0' 'A' to 'Å'
303compose '0' 'a' to 'å'
304compose 'A' 'A' to 'Å'
305compose 'a' 'a' to 'å'
306compose 'A' 'E' to 'Æ'
307compose 'a' 'e' to 'æ'
308compose ',' 'C' to 'Ç'
309compose ',' 'c' to 'ç'
310compose '`' 'E' to 'È'
311compose '`' 'e' to 'è'
312compose '\'' 'E' to 'É'
313compose '\'' 'e' to 'é'
314compose '^' 'E' to 'Ê'
315compose '^' 'e' to 'ê'
316compose '"' 'E' to 'Ë'
317compose '"' 'e' to 'ë'
318compose '`' 'I' to 'Ì'
319compose '`' 'i' to 'ì'
320compose '\'' 'I' to 'Í'
321compose '\'' 'i' to 'í'
322compose '^' 'I' to 'Î'
323compose '^' 'i' to 'î'
324compose '"' 'I' to 'Ï'
325compose '"' 'i' to 'ï'
326compose '-' 'D' to 'Ð'
327compose '-' 'd' to 'ð'
328compose '~' 'N' to 'Ñ'
329compose '~' 'n' to 'ñ'
330compose '`' 'O' to 'Ò'
331compose '`' 'o' to 'ò'
332compose '\'' 'O' to 'Ó'
333compose '\'' 'o' to 'ó'
334compose '^' 'O' to 'Ô'
335compose '^' 'o' to 'ô'
336compose '~' 'O' to 'Õ'
337compose '~' 'o' to 'õ'
338compose '"' 'O' to 'Ö'
339compose '"' 'o' to 'ö'
340compose '/' 'O' to 'Ø'
341compose '/' 'o' to 'ø'
342compose '`' 'U' to 'Ù'
343compose '`' 'u' to 'ù'
344compose '\'' 'U' to 'Ú'
345compose '\'' 'u' to 'ú'
346compose '^' 'U' to 'Û'
347compose '^' 'u' to 'û'
348compose '"' 'U' to 'Ü'
349compose '"' 'u' to 'ü'
350compose '\'' 'Y' to 'Ý'
351compose '\'' 'y' to 'ý'
352compose 'T' 'H' to 'Þ'
353compose 't' 'h' to 'þ'
354compose 's' 's' to 'ß'
355compose '"' 'y' to 'ÿ'
356compose 's' 'z' to 'ß'
357compose 'i' 'j' to 'ÿ'
diff --git a/drivers/char/digi1.h b/drivers/char/digi1.h
deleted file mode 100644
index 94d4eab5d3ca..000000000000
--- a/drivers/char/digi1.h
+++ /dev/null
@@ -1,100 +0,0 @@
1/* Definitions for DigiBoard ditty(1) command. */
2
3#if !defined(TIOCMODG)
4#define TIOCMODG (('d'<<8) | 250) /* get modem ctrl state */
5#define TIOCMODS (('d'<<8) | 251) /* set modem ctrl state */
6#endif
7
8#if !defined(TIOCMSET)
9#define TIOCMSET (('d'<<8) | 252) /* set modem ctrl state */
10#define TIOCMGET (('d'<<8) | 253) /* set modem ctrl state */
11#endif
12
13#if !defined(TIOCMBIC)
14#define TIOCMBIC (('d'<<8) | 254) /* set modem ctrl state */
15#define TIOCMBIS (('d'<<8) | 255) /* set modem ctrl state */
16#endif
17
18#if !defined(TIOCSDTR)
19#define TIOCSDTR (('e'<<8) | 0) /* set DTR */
20#define TIOCCDTR (('e'<<8) | 1) /* clear DTR */
21#endif
22
23/************************************************************************
24 * Ioctl command arguments for DIGI parameters.
25 ************************************************************************/
26#define DIGI_GETA (('e'<<8) | 94) /* Read params */
27
28#define DIGI_SETA (('e'<<8) | 95) /* Set params */
29#define DIGI_SETAW (('e'<<8) | 96) /* Drain & set params */
30#define DIGI_SETAF (('e'<<8) | 97) /* Drain, flush & set params */
31
32#define DIGI_GETFLOW (('e'<<8) | 99) /* Get startc/stopc flow */
33 /* control characters */
34#define DIGI_SETFLOW (('e'<<8) | 100) /* Set startc/stopc flow */
35 /* control characters */
36#define DIGI_GETAFLOW (('e'<<8) | 101) /* Get Aux. startc/stopc */
37 /* flow control chars */
38#define DIGI_SETAFLOW (('e'<<8) | 102) /* Set Aux. startc/stopc */
39 /* flow control chars */
40
41#define DIGI_GETINFO (('e'<<8) | 103) /* Fill in digi_info */
42#define DIGI_POLLER (('e'<<8) | 104) /* Turn on/off poller */
43#define DIGI_INIT (('e'<<8) | 105) /* Allow things to run. */
44
45struct digiflow_struct
46{
47 unsigned char startc; /* flow cntl start char */
48 unsigned char stopc; /* flow cntl stop char */
49};
50
51typedef struct digiflow_struct digiflow_t;
52
53
54/************************************************************************
55 * Values for digi_flags
56 ************************************************************************/
57#define DIGI_IXON 0x0001 /* Handle IXON in the FEP */
58#define DIGI_FAST 0x0002 /* Fast baud rates */
59#define RTSPACE 0x0004 /* RTS input flow control */
60#define CTSPACE 0x0008 /* CTS output flow control */
61#define DSRPACE 0x0010 /* DSR output flow control */
62#define DCDPACE 0x0020 /* DCD output flow control */
63#define DTRPACE 0x0040 /* DTR input flow control */
64#define DIGI_FORCEDCD 0x0100 /* Force carrier */
65#define DIGI_ALTPIN 0x0200 /* Alternate RJ-45 pin config */
66#define DIGI_AIXON 0x0400 /* Aux flow control in fep */
67
68
69/************************************************************************
70 * Values for digiDload
71 ************************************************************************/
72#define NORMAL 0
73#define PCI_CTL 1
74
75#define SIZE8 0
76#define SIZE16 1
77#define SIZE32 2
78
79/************************************************************************
80 * Structure used with ioctl commands for DIGI parameters.
81 ************************************************************************/
82struct digi_struct
83{
84 unsigned short digi_flags; /* Flags (see above) */
85};
86
87typedef struct digi_struct digi_t;
88
89struct digi_info
90{
91 unsigned long board; /* Which board is this ? */
92 unsigned char status; /* Alive or dead */
93 unsigned char type; /* see epca.h */
94 unsigned char subtype; /* For future XEM, XR, etc ... */
95 unsigned short numports; /* Number of ports configured */
96 unsigned char *port; /* I/O Address */
97 unsigned char *membase; /* DPR Address */
98 unsigned char *version; /* For future ... */
99 unsigned short windowData; /* For future ... */
100} ;
diff --git a/drivers/char/digiFep1.h b/drivers/char/digiFep1.h
deleted file mode 100644
index 3c1f1922c798..000000000000
--- a/drivers/char/digiFep1.h
+++ /dev/null
@@ -1,136 +0,0 @@
1
2#define CSTART 0x400L
3#define CMAX 0x800L
4#define ISTART 0x800L
5#define IMAX 0xC00L
6#define CIN 0xD10L
7#define GLOBAL 0xD10L
8#define EIN 0xD18L
9#define FEPSTAT 0xD20L
10#define CHANSTRUCT 0x1000L
11#define RXTXBUF 0x4000L
12
13
14struct global_data
15{
16 u16 cin;
17 u16 cout;
18 u16 cstart;
19 u16 cmax;
20 u16 ein;
21 u16 eout;
22 u16 istart;
23 u16 imax;
24};
25
26
27struct board_chan
28{
29 u32 filler1;
30 u32 filler2;
31 u16 tseg;
32 u16 tin;
33 u16 tout;
34 u16 tmax;
35
36 u16 rseg;
37 u16 rin;
38 u16 rout;
39 u16 rmax;
40
41 u16 tlow;
42 u16 rlow;
43 u16 rhigh;
44 u16 incr;
45
46 u16 etime;
47 u16 edelay;
48 unchar *dev;
49
50 u16 iflag;
51 u16 oflag;
52 u16 cflag;
53 u16 gmask;
54
55 u16 col;
56 u16 delay;
57 u16 imask;
58 u16 tflush;
59
60 u32 filler3;
61 u32 filler4;
62 u32 filler5;
63 u32 filler6;
64
65 u8 num;
66 u8 ract;
67 u8 bstat;
68 u8 tbusy;
69 u8 iempty;
70 u8 ilow;
71 u8 idata;
72 u8 eflag;
73
74 u8 tflag;
75 u8 rflag;
76 u8 xmask;
77 u8 xval;
78 u8 mstat;
79 u8 mchange;
80 u8 mint;
81 u8 lstat;
82
83 u8 mtran;
84 u8 orun;
85 u8 startca;
86 u8 stopca;
87 u8 startc;
88 u8 stopc;
89 u8 vnext;
90 u8 hflow;
91
92 u8 fillc;
93 u8 ochar;
94 u8 omask;
95
96 u8 filler7;
97 u8 filler8[28];
98};
99
100
101#define SRXLWATER 0xE0
102#define SRXHWATER 0xE1
103#define STOUT 0xE2
104#define PAUSETX 0xE3
105#define RESUMETX 0xE4
106#define SAUXONOFFC 0xE6
107#define SENDBREAK 0xE8
108#define SETMODEM 0xE9
109#define SETIFLAGS 0xEA
110#define SONOFFC 0xEB
111#define STXLWATER 0xEC
112#define PAUSERX 0xEE
113#define RESUMERX 0xEF
114#define SETBUFFER 0xF2
115#define SETCOOKED 0xF3
116#define SETHFLOW 0xF4
117#define SETCTRLFLAGS 0xF5
118#define SETVNEXT 0xF6
119
120
121
122#define BREAK_IND 0x01
123#define LOWTX_IND 0x02
124#define EMPTYTX_IND 0x04
125#define DATA_IND 0x08
126#define MODEMCHG_IND 0x20
127
128#define FEP_HUPCL 0002000
129#if 0
130#define RTS 0x02
131#define CD 0x08
132#define DSR 0x10
133#define CTS 0x20
134#define RI 0x40
135#define DTR 0x80
136#endif
diff --git a/drivers/char/digiPCI.h b/drivers/char/digiPCI.h
deleted file mode 100644
index 6ca7819e5069..000000000000
--- a/drivers/char/digiPCI.h
+++ /dev/null
@@ -1,42 +0,0 @@
1/*************************************************************************
2 * Defines and structure definitions for PCI BIOS Interface
3 *************************************************************************/
4#define PCIMAX 32 /* maximum number of PCI boards */
5
6
7#define PCI_VENDOR_DIGI 0x114F
8#define PCI_DEVICE_EPC 0x0002
9#define PCI_DEVICE_RIGHTSWITCH 0x0003 /* For testing */
10#define PCI_DEVICE_XEM 0x0004
11#define PCI_DEVICE_XR 0x0005
12#define PCI_DEVICE_CX 0x0006
13#define PCI_DEVICE_XRJ 0x0009 /* Jupiter boards with */
14#define PCI_DEVICE_EPCJ 0x000a /* PLX 9060 chip for PCI */
15
16
17/*
18 * On the PCI boards, there is no IO space allocated
19 * The I/O registers will be in the first 3 bytes of the
20 * upper 2MB of the 4MB memory space. The board memory
21 * will be mapped into the low 2MB of the 4MB memory space
22 */
23
24/* Potential location of PCI Bios from E0000 to FFFFF*/
25#define PCI_BIOS_SIZE 0x00020000
26
27/* Size of Memory and I/O for PCI (4MB) */
28#define PCI_RAM_SIZE 0x00400000
29
30/* Size of Memory (2MB) */
31#define PCI_MEM_SIZE 0x00200000
32
33/* Offset of I/0 in Memory (2MB) */
34#define PCI_IO_OFFSET 0x00200000
35
36#define MEMOUTB(basemem, pnum, setmemval) *(caddr_t)((basemem) + ( PCI_IO_OFFSET | pnum << 4 | pnum )) = (setmemval)
37#define MEMINB(basemem, pnum) *(caddr_t)((basemem) + (PCI_IO_OFFSET | pnum << 4 | pnum )) /* for PCI I/O */
38
39
40
41
42
diff --git a/drivers/char/ds1302.c b/drivers/char/ds1302.c
index 170693c93c73..ed8303f9890c 100644
--- a/drivers/char/ds1302.c
+++ b/drivers/char/ds1302.c
@@ -20,7 +20,7 @@
20#include <linux/miscdevice.h> 20#include <linux/miscdevice.h>
21#include <linux/delay.h> 21#include <linux/delay.h>
22#include <linux/bcd.h> 22#include <linux/bcd.h>
23#include <linux/smp_lock.h> 23#include <linux/mutex.h>
24#include <linux/uaccess.h> 24#include <linux/uaccess.h>
25#include <linux/io.h> 25#include <linux/io.h>
26 26
@@ -32,6 +32,7 @@
32 32
33#define RTC_MAJOR_NR 121 /* local major, change later */ 33#define RTC_MAJOR_NR 121 /* local major, change later */
34 34
35static DEFINE_MUTEX(rtc_mutex);
35static const char ds1302_name[] = "ds1302"; 36static const char ds1302_name[] = "ds1302";
36 37
37/* Send 8 bits. */ 38/* Send 8 bits. */
@@ -164,9 +165,9 @@ static long rtc_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
164 struct rtc_time rtc_tm; 165 struct rtc_time rtc_tm;
165 166
166 memset(&rtc_tm, 0, sizeof (struct rtc_time)); 167 memset(&rtc_tm, 0, sizeof (struct rtc_time));
167 lock_kernel(); 168 mutex_lock(&rtc_mutex);
168 get_rtc_time(&rtc_tm); 169 get_rtc_time(&rtc_tm);
169 unlock_kernel(); 170 mutex_unlock(&rtc_mutex);
170 if (copy_to_user((struct rtc_time*)arg, &rtc_tm, sizeof(struct rtc_time))) 171 if (copy_to_user((struct rtc_time*)arg, &rtc_tm, sizeof(struct rtc_time)))
171 return -EFAULT; 172 return -EFAULT;
172 return 0; 173 return 0;
@@ -218,7 +219,7 @@ static long rtc_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
218 mon = bin2bcd(mon); 219 mon = bin2bcd(mon);
219 yrs = bin2bcd(yrs); 220 yrs = bin2bcd(yrs);
220 221
221 lock_kernel(); 222 mutex_lock(&rtc_mutex);
222 local_irq_save(flags); 223 local_irq_save(flags);
223 CMOS_WRITE(yrs, RTC_YEAR); 224 CMOS_WRITE(yrs, RTC_YEAR);
224 CMOS_WRITE(mon, RTC_MONTH); 225 CMOS_WRITE(mon, RTC_MONTH);
@@ -227,7 +228,7 @@ static long rtc_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
227 CMOS_WRITE(min, RTC_MINUTES); 228 CMOS_WRITE(min, RTC_MINUTES);
228 CMOS_WRITE(sec, RTC_SECONDS); 229 CMOS_WRITE(sec, RTC_SECONDS);
229 local_irq_restore(flags); 230 local_irq_restore(flags);
230 unlock_kernel(); 231 mutex_unlock(&rtc_mutex);
231 232
232 /* Notice that at this point, the RTC is updated but 233 /* Notice that at this point, the RTC is updated but
233 * the kernel is still running with the old time. 234 * the kernel is still running with the old time.
@@ -247,10 +248,10 @@ static long rtc_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
247 if(copy_from_user(&tcs_val, (int*)arg, sizeof(int))) 248 if(copy_from_user(&tcs_val, (int*)arg, sizeof(int)))
248 return -EFAULT; 249 return -EFAULT;
249 250
250 lock_kernel(); 251 mutex_lock(&rtc_mutex);
251 tcs_val = RTC_TCR_PATTERN | (tcs_val & 0x0F); 252 tcs_val = RTC_TCR_PATTERN | (tcs_val & 0x0F);
252 ds1302_writereg(RTC_TRICKLECHARGER, tcs_val); 253 ds1302_writereg(RTC_TRICKLECHARGER, tcs_val);
253 unlock_kernel(); 254 mutex_unlock(&rtc_mutex);
254 return 0; 255 return 0;
255 } 256 }
256 default: 257 default:
@@ -288,6 +289,7 @@ get_rtc_status(char *buf)
288static const struct file_operations rtc_fops = { 289static const struct file_operations rtc_fops = {
289 .owner = THIS_MODULE, 290 .owner = THIS_MODULE,
290 .unlocked_ioctl = rtc_ioctl, 291 .unlocked_ioctl = rtc_ioctl,
292 .llseek = noop_llseek,
291}; 293};
292 294
293/* Probe for the chip by writing something to its RAM and try reading it back. */ 295/* Probe for the chip by writing something to its RAM and try reading it back. */
diff --git a/drivers/char/ds1620.c b/drivers/char/ds1620.c
index dbee8688f75c..aab9605f0b43 100644
--- a/drivers/char/ds1620.c
+++ b/drivers/char/ds1620.c
@@ -8,7 +8,7 @@
8#include <linux/proc_fs.h> 8#include <linux/proc_fs.h>
9#include <linux/capability.h> 9#include <linux/capability.h>
10#include <linux/init.h> 10#include <linux/init.h>
11#include <linux/smp_lock.h> 11#include <linux/mutex.h>
12 12
13#include <mach/hardware.h> 13#include <mach/hardware.h>
14#include <asm/mach-types.h> 14#include <asm/mach-types.h>
@@ -34,6 +34,7 @@
34#define CFG_CPU 2 34#define CFG_CPU 2
35#define CFG_1SHOT 1 35#define CFG_1SHOT 1
36 36
37static DEFINE_MUTEX(ds1620_mutex);
37static const char *fan_state[] = { "off", "on", "on (hardwired)" }; 38static const char *fan_state[] = { "off", "on", "on (hardwired)" };
38 39
39/* 40/*
@@ -210,7 +211,6 @@ static void ds1620_read_state(struct therm *therm)
210 211
211static int ds1620_open(struct inode *inode, struct file *file) 212static int ds1620_open(struct inode *inode, struct file *file)
212{ 213{
213 cycle_kernel_lock();
214 return nonseekable_open(inode, file); 214 return nonseekable_open(inode, file);
215} 215}
216 216
@@ -321,9 +321,9 @@ ds1620_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
321{ 321{
322 int ret; 322 int ret;
323 323
324 lock_kernel(); 324 mutex_lock(&ds1620_mutex);
325 ret = ds1620_ioctl(file, cmd, arg); 325 ret = ds1620_ioctl(file, cmd, arg);
326 unlock_kernel(); 326 mutex_unlock(&ds1620_mutex);
327 327
328 return ret; 328 return ret;
329} 329}
@@ -357,6 +357,7 @@ static const struct file_operations ds1620_fops = {
357 .open = ds1620_open, 357 .open = ds1620_open,
358 .read = ds1620_read, 358 .read = ds1620_read,
359 .unlocked_ioctl = ds1620_unlocked_ioctl, 359 .unlocked_ioctl = ds1620_unlocked_ioctl,
360 .llseek = no_llseek,
360}; 361};
361 362
362static struct miscdevice ds1620_miscdev = { 363static struct miscdevice ds1620_miscdev = {
diff --git a/drivers/char/dsp56k.c b/drivers/char/dsp56k.c
index 8a1b28a10ef0..052797b32bd3 100644
--- a/drivers/char/dsp56k.c
+++ b/drivers/char/dsp56k.c
@@ -32,7 +32,7 @@
32#include <linux/mm.h> 32#include <linux/mm.h>
33#include <linux/init.h> 33#include <linux/init.h>
34#include <linux/device.h> 34#include <linux/device.h>
35#include <linux/smp_lock.h> 35#include <linux/mutex.h>
36#include <linux/firmware.h> 36#include <linux/firmware.h>
37#include <linux/platform_device.h> 37#include <linux/platform_device.h>
38#include <linux/uaccess.h> /* For put_user and get_user */ 38#include <linux/uaccess.h> /* For put_user and get_user */
@@ -94,6 +94,7 @@
94 } \ 94 } \
95} 95}
96 96
97static DEFINE_MUTEX(dsp56k_mutex);
97static struct dsp56k_device { 98static struct dsp56k_device {
98 unsigned long in_use; 99 unsigned long in_use;
99 long maxio, timeout; 100 long maxio, timeout;
@@ -330,9 +331,9 @@ static long dsp56k_ioctl(struct file *file, unsigned int cmd,
330 if (len > DSP56K_MAX_BINARY_LENGTH) { 331 if (len > DSP56K_MAX_BINARY_LENGTH) {
331 return -EINVAL; 332 return -EINVAL;
332 } 333 }
333 lock_kernel(); 334 mutex_lock(&dsp56k_mutex);
334 r = dsp56k_upload(bin, len); 335 r = dsp56k_upload(bin, len);
335 unlock_kernel(); 336 mutex_unlock(&dsp56k_mutex);
336 if (r < 0) { 337 if (r < 0) {
337 return r; 338 return r;
338 } 339 }
@@ -342,16 +343,16 @@ static long dsp56k_ioctl(struct file *file, unsigned int cmd,
342 case DSP56K_SET_TX_WSIZE: 343 case DSP56K_SET_TX_WSIZE:
343 if (arg > 4 || arg < 1) 344 if (arg > 4 || arg < 1)
344 return -EINVAL; 345 return -EINVAL;
345 lock_kernel(); 346 mutex_lock(&dsp56k_mutex);
346 dsp56k.tx_wsize = (int) arg; 347 dsp56k.tx_wsize = (int) arg;
347 unlock_kernel(); 348 mutex_unlock(&dsp56k_mutex);
348 break; 349 break;
349 case DSP56K_SET_RX_WSIZE: 350 case DSP56K_SET_RX_WSIZE:
350 if (arg > 4 || arg < 1) 351 if (arg > 4 || arg < 1)
351 return -EINVAL; 352 return -EINVAL;
352 lock_kernel(); 353 mutex_lock(&dsp56k_mutex);
353 dsp56k.rx_wsize = (int) arg; 354 dsp56k.rx_wsize = (int) arg;
354 unlock_kernel(); 355 mutex_unlock(&dsp56k_mutex);
355 break; 356 break;
356 case DSP56K_HOST_FLAGS: 357 case DSP56K_HOST_FLAGS:
357 { 358 {
@@ -363,7 +364,7 @@ static long dsp56k_ioctl(struct file *file, unsigned int cmd,
363 if(get_user(out, &hf->out) < 0) 364 if(get_user(out, &hf->out) < 0)
364 return -EFAULT; 365 return -EFAULT;
365 366
366 lock_kernel(); 367 mutex_lock(&dsp56k_mutex);
367 if ((dir & 0x1) && (out & 0x1)) 368 if ((dir & 0x1) && (out & 0x1))
368 dsp56k_host_interface.icr |= DSP56K_ICR_HF0; 369 dsp56k_host_interface.icr |= DSP56K_ICR_HF0;
369 else if (dir & 0x1) 370 else if (dir & 0x1)
@@ -378,16 +379,16 @@ static long dsp56k_ioctl(struct file *file, unsigned int cmd,
378 if (dsp56k_host_interface.icr & DSP56K_ICR_HF1) status |= 0x2; 379 if (dsp56k_host_interface.icr & DSP56K_ICR_HF1) status |= 0x2;
379 if (dsp56k_host_interface.isr & DSP56K_ISR_HF2) status |= 0x4; 380 if (dsp56k_host_interface.isr & DSP56K_ISR_HF2) status |= 0x4;
380 if (dsp56k_host_interface.isr & DSP56K_ISR_HF3) status |= 0x8; 381 if (dsp56k_host_interface.isr & DSP56K_ISR_HF3) status |= 0x8;
381 unlock_kernel(); 382 mutex_unlock(&dsp56k_mutex);
382 return put_user(status, &hf->status); 383 return put_user(status, &hf->status);
383 } 384 }
384 case DSP56K_HOST_CMD: 385 case DSP56K_HOST_CMD:
385 if (arg > 31 || arg < 0) 386 if (arg > 31 || arg < 0)
386 return -EINVAL; 387 return -EINVAL;
387 lock_kernel(); 388 mutex_lock(&dsp56k_mutex);
388 dsp56k_host_interface.cvr = (u_char)((arg & DSP56K_CVR_HV_MASK) | 389 dsp56k_host_interface.cvr = (u_char)((arg & DSP56K_CVR_HV_MASK) |
389 DSP56K_CVR_HC); 390 DSP56K_CVR_HC);
390 unlock_kernel(); 391 mutex_unlock(&dsp56k_mutex);
391 break; 392 break;
392 default: 393 default:
393 return -EINVAL; 394 return -EINVAL;
@@ -427,7 +428,7 @@ static int dsp56k_open(struct inode *inode, struct file *file)
427 int dev = iminor(inode) & 0x0f; 428 int dev = iminor(inode) & 0x0f;
428 int ret = 0; 429 int ret = 0;
429 430
430 lock_kernel(); 431 mutex_lock(&dsp56k_mutex);
431 switch(dev) 432 switch(dev)
432 { 433 {
433 case DSP56K_DEV_56001: 434 case DSP56K_DEV_56001:
@@ -454,7 +455,7 @@ static int dsp56k_open(struct inode *inode, struct file *file)
454 ret = -ENODEV; 455 ret = -ENODEV;
455 } 456 }
456out: 457out:
457 unlock_kernel(); 458 mutex_unlock(&dsp56k_mutex);
458 return ret; 459 return ret;
459} 460}
460 461
@@ -482,6 +483,7 @@ static const struct file_operations dsp56k_fops = {
482 .unlocked_ioctl = dsp56k_ioctl, 483 .unlocked_ioctl = dsp56k_ioctl,
483 .open = dsp56k_open, 484 .open = dsp56k_open,
484 .release = dsp56k_release, 485 .release = dsp56k_release,
486 .llseek = noop_llseek,
485}; 487};
486 488
487 489
diff --git a/drivers/char/dtlk.c b/drivers/char/dtlk.c
index e3859d4eaead..85156dd0caee 100644
--- a/drivers/char/dtlk.c
+++ b/drivers/char/dtlk.c
@@ -57,7 +57,7 @@
57#include <linux/ioport.h> /* for request_region */ 57#include <linux/ioport.h> /* for request_region */
58#include <linux/delay.h> /* for loops_per_jiffy */ 58#include <linux/delay.h> /* for loops_per_jiffy */
59#include <linux/sched.h> 59#include <linux/sched.h>
60#include <linux/smp_lock.h> /* cycle_kernel_lock() */ 60#include <linux/mutex.h>
61#include <asm/io.h> /* for inb_p, outb_p, inb, outb, etc. */ 61#include <asm/io.h> /* for inb_p, outb_p, inb, outb, etc. */
62#include <asm/uaccess.h> /* for get_user, etc. */ 62#include <asm/uaccess.h> /* for get_user, etc. */
63#include <linux/wait.h> /* for wait_queue */ 63#include <linux/wait.h> /* for wait_queue */
@@ -73,6 +73,7 @@
73#define TRACE_RET ((void) 0) 73#define TRACE_RET ((void) 0)
74#endif /* TRACING */ 74#endif /* TRACING */
75 75
76static DEFINE_MUTEX(dtlk_mutex);
76static void dtlk_timer_tick(unsigned long data); 77static void dtlk_timer_tick(unsigned long data);
77 78
78static int dtlk_major; 79static int dtlk_major;
@@ -105,6 +106,7 @@ static const struct file_operations dtlk_fops =
105 .unlocked_ioctl = dtlk_ioctl, 106 .unlocked_ioctl = dtlk_ioctl,
106 .open = dtlk_open, 107 .open = dtlk_open,
107 .release = dtlk_release, 108 .release = dtlk_release,
109 .llseek = no_llseek,
108}; 110};
109 111
110/* local prototypes */ 112/* local prototypes */
@@ -275,9 +277,9 @@ static long dtlk_ioctl(struct file *file,
275 switch (cmd) { 277 switch (cmd) {
276 278
277 case DTLK_INTERROGATE: 279 case DTLK_INTERROGATE:
278 lock_kernel(); 280 mutex_lock(&dtlk_mutex);
279 sp = dtlk_interrogate(); 281 sp = dtlk_interrogate();
280 unlock_kernel(); 282 mutex_unlock(&dtlk_mutex);
281 if (copy_to_user(argp, sp, sizeof(struct dtlk_settings))) 283 if (copy_to_user(argp, sp, sizeof(struct dtlk_settings)))
282 return -EINVAL; 284 return -EINVAL;
283 return 0; 285 return 0;
@@ -296,7 +298,6 @@ static int dtlk_open(struct inode *inode, struct file *file)
296{ 298{
297 TRACE_TEXT("(dtlk_open"); 299 TRACE_TEXT("(dtlk_open");
298 300
299 cycle_kernel_lock();
300 nonseekable_open(inode, file); 301 nonseekable_open(inode, file);
301 switch (iminor(inode)) { 302 switch (iminor(inode)) {
302 case DTLK_MINOR: 303 case DTLK_MINOR:
diff --git a/drivers/char/epca.c b/drivers/char/epca.c
deleted file mode 100644
index d9df46aa0fba..000000000000
--- a/drivers/char/epca.c
+++ /dev/null
@@ -1,2784 +0,0 @@
1/*
2 Copyright (C) 1996 Digi International.
3
4 For technical support please email digiLinux@dgii.com or
5 call Digi tech support at (612) 912-3456
6
7 ** This driver is no longer supported by Digi **
8
9 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.
12
13 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.
17
18 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.
22
23 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*/
27/* See README.epca for change history --DAT*/
28
29#include <linux/module.h>
30#include <linux/kernel.h>
31#include <linux/types.h>
32#include <linux/init.h>
33#include <linux/sched.h>
34#include <linux/serial.h>
35#include <linux/delay.h>
36#include <linux/ctype.h>
37#include <linux/tty.h>
38#include <linux/tty_flip.h>
39#include <linux/slab.h>
40#include <linux/ioport.h>
41#include <linux/interrupt.h>
42#include <linux/uaccess.h>
43#include <linux/io.h>
44#include <linux/spinlock.h>
45#include <linux/pci.h>
46#include "digiPCI.h"
47
48
49#include "digi1.h"
50#include "digiFep1.h"
51#include "epca.h"
52#include "epcaconfig.h"
53
54#define VERSION "1.3.0.1-LK2.6"
55
56/* This major needs to be submitted to Linux to join the majors list */
57#define DIGIINFOMAJOR 35 /* For Digi specific ioctl */
58
59
60#define MAXCARDS 7
61#define epcaassert(x, msg) if (!(x)) epca_error(__LINE__, msg)
62
63#define PFX "epca: "
64
65static int nbdevs, num_cards, liloconfig;
66static int digi_poller_inhibited = 1 ;
67
68static int setup_error_code;
69static int invalid_lilo_config;
70
71/*
72 * The ISA boards do window flipping into the same spaces so its only sane with
73 * a single lock. It's still pretty efficient. This lock guards the hardware
74 * and the tty_port lock guards the kernel side stuff like use counts. Take
75 * this lock inside the port lock if you must take both.
76 */
77static DEFINE_SPINLOCK(epca_lock);
78
79/* MAXBOARDS is typically 12, but ISA and EISA cards are restricted
80 to 7 below. */
81static struct board_info boards[MAXBOARDS];
82
83static struct tty_driver *pc_driver;
84static struct tty_driver *pc_info;
85
86/* ------------------ Begin Digi specific structures -------------------- */
87
88/*
89 * digi_channels represents an array of structures that keep track of each
90 * channel of the Digi product. Information such as transmit and receive
91 * pointers, termio data, and signal definitions (DTR, CTS, etc ...) are stored
92 * here. This structure is NOT used to overlay the cards physical channel
93 * structure.
94 */
95static struct channel digi_channels[MAX_ALLOC];
96
97/*
98 * card_ptr is an array used to hold the address of the first channel structure
99 * of each card. This array will hold the addresses of various channels located
100 * in digi_channels.
101 */
102static struct channel *card_ptr[MAXCARDS];
103
104static struct timer_list epca_timer;
105
106/*
107 * Begin generic memory functions. These functions will be alias (point at)
108 * more specific functions dependent on the board being configured.
109 */
110static void memwinon(struct board_info *b, unsigned int win);
111static void memwinoff(struct board_info *b, unsigned int win);
112static void globalwinon(struct channel *ch);
113static void rxwinon(struct channel *ch);
114static void txwinon(struct channel *ch);
115static void memoff(struct channel *ch);
116static void assertgwinon(struct channel *ch);
117static void assertmemoff(struct channel *ch);
118
119/* ---- Begin more 'specific' memory functions for cx_like products --- */
120
121static void pcxem_memwinon(struct board_info *b, unsigned int win);
122static void pcxem_memwinoff(struct board_info *b, unsigned int win);
123static void pcxem_globalwinon(struct channel *ch);
124static void pcxem_rxwinon(struct channel *ch);
125static void pcxem_txwinon(struct channel *ch);
126static void pcxem_memoff(struct channel *ch);
127
128/* ------ Begin more 'specific' memory functions for the pcxe ------- */
129
130static void pcxe_memwinon(struct board_info *b, unsigned int win);
131static void pcxe_memwinoff(struct board_info *b, unsigned int win);
132static void pcxe_globalwinon(struct channel *ch);
133static void pcxe_rxwinon(struct channel *ch);
134static void pcxe_txwinon(struct channel *ch);
135static void pcxe_memoff(struct channel *ch);
136
137/* ---- Begin more 'specific' memory functions for the pc64xe and pcxi ---- */
138/* Note : pc64xe and pcxi share the same windowing routines */
139
140static void pcxi_memwinon(struct board_info *b, unsigned int win);
141static void pcxi_memwinoff(struct board_info *b, unsigned int win);
142static void pcxi_globalwinon(struct channel *ch);
143static void pcxi_rxwinon(struct channel *ch);
144static void pcxi_txwinon(struct channel *ch);
145static void pcxi_memoff(struct channel *ch);
146
147/* - Begin 'specific' do nothing memory functions needed for some cards - */
148
149static void dummy_memwinon(struct board_info *b, unsigned int win);
150static void dummy_memwinoff(struct board_info *b, unsigned int win);
151static void dummy_globalwinon(struct channel *ch);
152static void dummy_rxwinon(struct channel *ch);
153static void dummy_txwinon(struct channel *ch);
154static void dummy_memoff(struct channel *ch);
155static void dummy_assertgwinon(struct channel *ch);
156static void dummy_assertmemoff(struct channel *ch);
157
158static struct channel *verifyChannel(struct tty_struct *);
159static void pc_sched_event(struct channel *, int);
160static void epca_error(int, char *);
161static void pc_close(struct tty_struct *, struct file *);
162static void shutdown(struct channel *, struct tty_struct *tty);
163static void pc_hangup(struct tty_struct *);
164static int pc_write_room(struct tty_struct *);
165static int pc_chars_in_buffer(struct tty_struct *);
166static void pc_flush_buffer(struct tty_struct *);
167static void pc_flush_chars(struct tty_struct *);
168static int pc_open(struct tty_struct *, struct file *);
169static void post_fep_init(unsigned int crd);
170static void epcapoll(unsigned long);
171static void doevent(int);
172static void fepcmd(struct channel *, int, int, int, int, int);
173static unsigned termios2digi_h(struct channel *ch, unsigned);
174static unsigned termios2digi_i(struct channel *ch, unsigned);
175static unsigned termios2digi_c(struct channel *ch, unsigned);
176static void epcaparam(struct tty_struct *, struct channel *);
177static void receive_data(struct channel *, struct tty_struct *tty);
178static int pc_ioctl(struct tty_struct *, struct file *,
179 unsigned int, unsigned long);
180static int info_ioctl(struct tty_struct *, struct file *,
181 unsigned int, unsigned long);
182static void pc_set_termios(struct tty_struct *, struct ktermios *);
183static void do_softint(struct work_struct *work);
184static void pc_stop(struct tty_struct *);
185static void pc_start(struct tty_struct *);
186static void pc_throttle(struct tty_struct *tty);
187static void pc_unthrottle(struct tty_struct *tty);
188static int pc_send_break(struct tty_struct *tty, int msec);
189static void setup_empty_event(struct tty_struct *tty, struct channel *ch);
190
191static int pc_write(struct tty_struct *, const unsigned char *, int);
192static int pc_init(void);
193static int init_PCI(void);
194
195/*
196 * Table of functions for each board to handle memory. Mantaining parallelism
197 * is a *very* good idea here. The idea is for the runtime code to blindly call
198 * these functions, not knowing/caring about the underlying hardware. This
199 * stuff should contain no conditionals; if more functionality is needed a
200 * different entry should be established. These calls are the interface calls
201 * and are the only functions that should be accessed. Anyone caught making
202 * direct calls deserves what they get.
203 */
204static void memwinon(struct board_info *b, unsigned int win)
205{
206 b->memwinon(b, win);
207}
208
209static void memwinoff(struct board_info *b, unsigned int win)
210{
211 b->memwinoff(b, win);
212}
213
214static void globalwinon(struct channel *ch)
215{
216 ch->board->globalwinon(ch);
217}
218
219static void rxwinon(struct channel *ch)
220{
221 ch->board->rxwinon(ch);
222}
223
224static void txwinon(struct channel *ch)
225{
226 ch->board->txwinon(ch);
227}
228
229static void memoff(struct channel *ch)
230{
231 ch->board->memoff(ch);
232}
233static void assertgwinon(struct channel *ch)
234{
235 ch->board->assertgwinon(ch);
236}
237
238static void assertmemoff(struct channel *ch)
239{
240 ch->board->assertmemoff(ch);
241}
242
243/* PCXEM windowing is the same as that used in the PCXR and CX series cards. */
244static void pcxem_memwinon(struct board_info *b, unsigned int win)
245{
246 outb_p(FEPWIN | win, b->port + 1);
247}
248
249static void pcxem_memwinoff(struct board_info *b, unsigned int win)
250{
251 outb_p(0, b->port + 1);
252}
253
254static void pcxem_globalwinon(struct channel *ch)
255{
256 outb_p(FEPWIN, (int)ch->board->port + 1);
257}
258
259static void pcxem_rxwinon(struct channel *ch)
260{
261 outb_p(ch->rxwin, (int)ch->board->port + 1);
262}
263
264static void pcxem_txwinon(struct channel *ch)
265{
266 outb_p(ch->txwin, (int)ch->board->port + 1);
267}
268
269static void pcxem_memoff(struct channel *ch)
270{
271 outb_p(0, (int)ch->board->port + 1);
272}
273
274/* ----------------- Begin pcxe memory window stuff ------------------ */
275static void pcxe_memwinon(struct board_info *b, unsigned int win)
276{
277 outb_p(FEPWIN | win, b->port + 1);
278}
279
280static void pcxe_memwinoff(struct board_info *b, unsigned int win)
281{
282 outb_p(inb(b->port) & ~FEPMEM, b->port + 1);
283 outb_p(0, b->port + 1);
284}
285
286static void pcxe_globalwinon(struct channel *ch)
287{
288 outb_p(FEPWIN, (int)ch->board->port + 1);
289}
290
291static void pcxe_rxwinon(struct channel *ch)
292{
293 outb_p(ch->rxwin, (int)ch->board->port + 1);
294}
295
296static void pcxe_txwinon(struct channel *ch)
297{
298 outb_p(ch->txwin, (int)ch->board->port + 1);
299}
300
301static void pcxe_memoff(struct channel *ch)
302{
303 outb_p(0, (int)ch->board->port);
304 outb_p(0, (int)ch->board->port + 1);
305}
306
307/* ------------- Begin pc64xe and pcxi memory window stuff -------------- */
308static void pcxi_memwinon(struct board_info *b, unsigned int win)
309{
310 outb_p(inb(b->port) | FEPMEM, b->port);
311}
312
313static void pcxi_memwinoff(struct board_info *b, unsigned int win)
314{
315 outb_p(inb(b->port) & ~FEPMEM, b->port);
316}
317
318static void pcxi_globalwinon(struct channel *ch)
319{
320 outb_p(FEPMEM, ch->board->port);
321}
322
323static void pcxi_rxwinon(struct channel *ch)
324{
325 outb_p(FEPMEM, ch->board->port);
326}
327
328static void pcxi_txwinon(struct channel *ch)
329{
330 outb_p(FEPMEM, ch->board->port);
331}
332
333static void pcxi_memoff(struct channel *ch)
334{
335 outb_p(0, ch->board->port);
336}
337
338static void pcxi_assertgwinon(struct channel *ch)
339{
340 epcaassert(inb(ch->board->port) & FEPMEM, "Global memory off");
341}
342
343static void pcxi_assertmemoff(struct channel *ch)
344{
345 epcaassert(!(inb(ch->board->port) & FEPMEM), "Memory on");
346}
347
348/*
349 * Not all of the cards need specific memory windowing routines. Some cards
350 * (Such as PCI) needs no windowing routines at all. We provide these do
351 * nothing routines so that the same code base can be used. The driver will
352 * ALWAYS call a windowing routine if it thinks it needs to; regardless of the
353 * card. However, dependent on the card the routine may or may not do anything.
354 */
355static void dummy_memwinon(struct board_info *b, unsigned int win)
356{
357}
358
359static void dummy_memwinoff(struct board_info *b, unsigned int win)
360{
361}
362
363static void dummy_globalwinon(struct channel *ch)
364{
365}
366
367static void dummy_rxwinon(struct channel *ch)
368{
369}
370
371static void dummy_txwinon(struct channel *ch)
372{
373}
374
375static void dummy_memoff(struct channel *ch)
376{
377}
378
379static void dummy_assertgwinon(struct channel *ch)
380{
381}
382
383static void dummy_assertmemoff(struct channel *ch)
384{
385}
386
387static struct channel *verifyChannel(struct tty_struct *tty)
388{
389 /*
390 * This routine basically provides a sanity check. It insures that the
391 * channel returned is within the proper range of addresses as well as
392 * properly initialized. If some bogus info gets passed in
393 * through tty->driver_data this should catch it.
394 */
395 if (tty) {
396 struct channel *ch = tty->driver_data;
397 if (ch >= &digi_channels[0] && ch < &digi_channels[nbdevs]) {
398 if (ch->magic == EPCA_MAGIC)
399 return ch;
400 }
401 }
402 return NULL;
403}
404
405static void pc_sched_event(struct channel *ch, int event)
406{
407 /*
408 * We call this to schedule interrupt processing on some event. The
409 * kernel sees our request and calls the related routine in OUR driver.
410 */
411 ch->event |= 1 << event;
412 schedule_work(&ch->tqueue);
413}
414
415static void epca_error(int line, char *msg)
416{
417 printk(KERN_ERR "epca_error (Digi): line = %d %s\n", line, msg);
418}
419
420static void pc_close(struct tty_struct *tty, struct file *filp)
421{
422 struct channel *ch;
423 struct tty_port *port;
424 /*
425 * verifyChannel returns the channel from the tty struct if it is
426 * valid. This serves as a sanity check.
427 */
428 ch = verifyChannel(tty);
429 if (ch == NULL)
430 return;
431 port = &ch->port;
432
433 if (tty_port_close_start(port, tty, filp) == 0)
434 return;
435
436 pc_flush_buffer(tty);
437 shutdown(ch, tty);
438
439 tty_port_close_end(port, tty);
440 ch->event = 0; /* FIXME: review ch->event locking */
441 tty_port_tty_set(port, NULL);
442}
443
444static void shutdown(struct channel *ch, struct tty_struct *tty)
445{
446 unsigned long flags;
447 struct board_chan __iomem *bc;
448 struct tty_port *port = &ch->port;
449
450 if (!(port->flags & ASYNC_INITIALIZED))
451 return;
452
453 spin_lock_irqsave(&epca_lock, flags);
454
455 globalwinon(ch);
456 bc = ch->brdchan;
457
458 /*
459 * In order for an event to be generated on the receipt of data the
460 * idata flag must be set. Since we are shutting down, this is not
461 * necessary clear this flag.
462 */
463 if (bc)
464 writeb(0, &bc->idata);
465
466 /* If we're a modem control device and HUPCL is on, drop RTS & DTR. */
467 if (tty->termios->c_cflag & HUPCL) {
468 ch->omodem &= ~(ch->m_rts | ch->m_dtr);
469 fepcmd(ch, SETMODEM, 0, ch->m_dtr | ch->m_rts, 10, 1);
470 }
471 memoff(ch);
472
473 /*
474 * The channel has officialy been closed. The next time it is opened it
475 * will have to reinitialized. Set a flag to indicate this.
476 */
477 /* Prevent future Digi programmed interrupts from coming active */
478 port->flags &= ~ASYNC_INITIALIZED;
479 spin_unlock_irqrestore(&epca_lock, flags);
480}
481
482static void pc_hangup(struct tty_struct *tty)
483{
484 struct channel *ch;
485
486 /*
487 * verifyChannel returns the channel from the tty struct if it is
488 * valid. This serves as a sanity check.
489 */
490 ch = verifyChannel(tty);
491 if (ch != NULL) {
492 pc_flush_buffer(tty);
493 tty_ldisc_flush(tty);
494 shutdown(ch, tty);
495
496 ch->event = 0; /* FIXME: review locking of ch->event */
497 tty_port_hangup(&ch->port);
498 }
499}
500
501static int pc_write(struct tty_struct *tty,
502 const unsigned char *buf, int bytesAvailable)
503{
504 unsigned int head, tail;
505 int dataLen;
506 int size;
507 int amountCopied;
508 struct channel *ch;
509 unsigned long flags;
510 int remain;
511 struct board_chan __iomem *bc;
512
513 /*
514 * pc_write is primarily called directly by the kernel routine
515 * tty_write (Though it can also be called by put_char) found in
516 * tty_io.c. pc_write is passed a line discipline buffer where the data
517 * to be written out is stored. The line discipline implementation
518 * itself is done at the kernel level and is not brought into the
519 * driver.
520 */
521
522 /*
523 * verifyChannel returns the channel from the tty struct if it is
524 * valid. This serves as a sanity check.
525 */
526 ch = verifyChannel(tty);
527 if (ch == NULL)
528 return 0;
529
530 /* Make a pointer to the channel data structure found on the board. */
531 bc = ch->brdchan;
532 size = ch->txbufsize;
533 amountCopied = 0;
534
535 spin_lock_irqsave(&epca_lock, flags);
536 globalwinon(ch);
537
538 head = readw(&bc->tin) & (size - 1);
539 tail = readw(&bc->tout);
540
541 if (tail != readw(&bc->tout))
542 tail = readw(&bc->tout);
543 tail &= (size - 1);
544
545 if (head >= tail) {
546 /* head has not wrapped */
547 /*
548 * remain (much like dataLen above) represents the total amount
549 * of space available on the card for data. Here dataLen
550 * represents the space existing between the head pointer and
551 * the end of buffer. This is important because a memcpy cannot
552 * be told to automatically wrap around when it hits the buffer
553 * end.
554 */
555 dataLen = size - head;
556 remain = size - (head - tail) - 1;
557 } else {
558 /* head has wrapped around */
559 remain = tail - head - 1;
560 dataLen = remain;
561 }
562 /*
563 * Check the space on the card. If we have more data than space; reduce
564 * the amount of data to fit the space.
565 */
566 bytesAvailable = min(remain, bytesAvailable);
567 txwinon(ch);
568 while (bytesAvailable > 0) {
569 /* there is data to copy onto card */
570
571 /*
572 * If head is not wrapped, the below will make sure the first
573 * data copy fills to the end of card buffer.
574 */
575 dataLen = min(bytesAvailable, dataLen);
576 memcpy_toio(ch->txptr + head, buf, dataLen);
577 buf += dataLen;
578 head += dataLen;
579 amountCopied += dataLen;
580 bytesAvailable -= dataLen;
581
582 if (head >= size) {
583 head = 0;
584 dataLen = tail;
585 }
586 }
587 ch->statusflags |= TXBUSY;
588 globalwinon(ch);
589 writew(head, &bc->tin);
590
591 if ((ch->statusflags & LOWWAIT) == 0) {
592 ch->statusflags |= LOWWAIT;
593 writeb(1, &bc->ilow);
594 }
595 memoff(ch);
596 spin_unlock_irqrestore(&epca_lock, flags);
597 return amountCopied;
598}
599
600static int pc_write_room(struct tty_struct *tty)
601{
602 int remain = 0;
603 struct channel *ch;
604 unsigned long flags;
605 unsigned int head, tail;
606 struct board_chan __iomem *bc;
607 /*
608 * verifyChannel returns the channel from the tty struct if it is
609 * valid. This serves as a sanity check.
610 */
611 ch = verifyChannel(tty);
612 if (ch != NULL) {
613 spin_lock_irqsave(&epca_lock, flags);
614 globalwinon(ch);
615
616 bc = ch->brdchan;
617 head = readw(&bc->tin) & (ch->txbufsize - 1);
618 tail = readw(&bc->tout);
619
620 if (tail != readw(&bc->tout))
621 tail = readw(&bc->tout);
622 /* Wrap tail if necessary */
623 tail &= (ch->txbufsize - 1);
624 remain = tail - head - 1;
625 if (remain < 0)
626 remain += ch->txbufsize;
627
628 if (remain && (ch->statusflags & LOWWAIT) == 0) {
629 ch->statusflags |= LOWWAIT;
630 writeb(1, &bc->ilow);
631 }
632 memoff(ch);
633 spin_unlock_irqrestore(&epca_lock, flags);
634 }
635 /* Return how much room is left on card */
636 return remain;
637}
638
639static int pc_chars_in_buffer(struct tty_struct *tty)
640{
641 int chars;
642 unsigned int ctail, head, tail;
643 int remain;
644 unsigned long flags;
645 struct channel *ch;
646 struct board_chan __iomem *bc;
647 /*
648 * verifyChannel returns the channel from the tty struct if it is
649 * valid. This serves as a sanity check.
650 */
651 ch = verifyChannel(tty);
652 if (ch == NULL)
653 return 0;
654
655 spin_lock_irqsave(&epca_lock, flags);
656 globalwinon(ch);
657
658 bc = ch->brdchan;
659 tail = readw(&bc->tout);
660 head = readw(&bc->tin);
661 ctail = readw(&ch->mailbox->cout);
662
663 if (tail == head && readw(&ch->mailbox->cin) == ctail &&
664 readb(&bc->tbusy) == 0)
665 chars = 0;
666 else { /* Begin if some space on the card has been used */
667 head = readw(&bc->tin) & (ch->txbufsize - 1);
668 tail &= (ch->txbufsize - 1);
669 /*
670 * The logic here is basically opposite of the above
671 * pc_write_room here we are finding the amount of bytes in the
672 * buffer filled. Not the amount of bytes empty.
673 */
674 remain = tail - head - 1;
675 if (remain < 0)
676 remain += ch->txbufsize;
677 chars = (int)(ch->txbufsize - remain);
678 /*
679 * Make it possible to wakeup anything waiting for output in
680 * tty_ioctl.c, etc.
681 *
682 * If not already set. Setup an event to indicate when the
683 * transmit buffer empties.
684 */
685 if (!(ch->statusflags & EMPTYWAIT))
686 setup_empty_event(tty, ch);
687 } /* End if some space on the card has been used */
688 memoff(ch);
689 spin_unlock_irqrestore(&epca_lock, flags);
690 /* Return number of characters residing on card. */
691 return chars;
692}
693
694static void pc_flush_buffer(struct tty_struct *tty)
695{
696 unsigned int tail;
697 unsigned long flags;
698 struct channel *ch;
699 struct board_chan __iomem *bc;
700 /*
701 * verifyChannel returns the channel from the tty struct if it is
702 * valid. This serves as a sanity check.
703 */
704 ch = verifyChannel(tty);
705 if (ch == NULL)
706 return;
707
708 spin_lock_irqsave(&epca_lock, flags);
709 globalwinon(ch);
710 bc = ch->brdchan;
711 tail = readw(&bc->tout);
712 /* Have FEP move tout pointer; effectively flushing transmit buffer */
713 fepcmd(ch, STOUT, (unsigned) tail, 0, 0, 0);
714 memoff(ch);
715 spin_unlock_irqrestore(&epca_lock, flags);
716 tty_wakeup(tty);
717}
718
719static void pc_flush_chars(struct tty_struct *tty)
720{
721 struct channel *ch;
722 /*
723 * verifyChannel returns the channel from the tty struct if it is
724 * valid. This serves as a sanity check.
725 */
726 ch = verifyChannel(tty);
727 if (ch != NULL) {
728 unsigned long flags;
729 spin_lock_irqsave(&epca_lock, flags);
730 /*
731 * If not already set and the transmitter is busy setup an
732 * event to indicate when the transmit empties.
733 */
734 if ((ch->statusflags & TXBUSY) &&
735 !(ch->statusflags & EMPTYWAIT))
736 setup_empty_event(tty, ch);
737 spin_unlock_irqrestore(&epca_lock, flags);
738 }
739}
740
741static int epca_carrier_raised(struct tty_port *port)
742{
743 struct channel *ch = container_of(port, struct channel, port);
744 if (ch->imodem & ch->dcd)
745 return 1;
746 return 0;
747}
748
749static void epca_dtr_rts(struct tty_port *port, int onoff)
750{
751}
752
753static int pc_open(struct tty_struct *tty, struct file *filp)
754{
755 struct channel *ch;
756 struct tty_port *port;
757 unsigned long flags;
758 int line, retval, boardnum;
759 struct board_chan __iomem *bc;
760 unsigned int head;
761
762 line = tty->index;
763 if (line < 0 || line >= nbdevs)
764 return -ENODEV;
765
766 ch = &digi_channels[line];
767 port = &ch->port;
768 boardnum = ch->boardnum;
769
770 /* Check status of board configured in system. */
771
772 /*
773 * I check to see if the epca_setup routine detected a user error. It
774 * might be better to put this in pc_init, but for the moment it goes
775 * here.
776 */
777 if (invalid_lilo_config) {
778 if (setup_error_code & INVALID_BOARD_TYPE)
779 printk(KERN_ERR "epca: pc_open: Invalid board type specified in kernel options.\n");
780 if (setup_error_code & INVALID_NUM_PORTS)
781 printk(KERN_ERR "epca: pc_open: Invalid number of ports specified in kernel options.\n");
782 if (setup_error_code & INVALID_MEM_BASE)
783 printk(KERN_ERR "epca: pc_open: Invalid board memory address specified in kernel options.\n");
784 if (setup_error_code & INVALID_PORT_BASE)
785 printk(KERN_ERR "epca; pc_open: Invalid board port address specified in kernel options.\n");
786 if (setup_error_code & INVALID_BOARD_STATUS)
787 printk(KERN_ERR "epca: pc_open: Invalid board status specified in kernel options.\n");
788 if (setup_error_code & INVALID_ALTPIN)
789 printk(KERN_ERR "epca: pc_open: Invalid board altpin specified in kernel options;\n");
790 tty->driver_data = NULL; /* Mark this device as 'down' */
791 return -ENODEV;
792 }
793 if (boardnum >= num_cards || boards[boardnum].status == DISABLED) {
794 tty->driver_data = NULL; /* Mark this device as 'down' */
795 return(-ENODEV);
796 }
797
798 bc = ch->brdchan;
799 if (bc == NULL) {
800 tty->driver_data = NULL;
801 return -ENODEV;
802 }
803
804 spin_lock_irqsave(&port->lock, flags);
805 /*
806 * Every time a channel is opened, increment a counter. This is
807 * necessary because we do not wish to flush and shutdown the channel
808 * until the last app holding the channel open, closes it.
809 */
810 port->count++;
811 /*
812 * Set a kernel structures pointer to our local channel structure. This
813 * way we can get to it when passed only a tty struct.
814 */
815 tty->driver_data = ch;
816 port->tty = tty;
817 /*
818 * If this is the first time the channel has been opened, initialize
819 * the tty->termios struct otherwise let pc_close handle it.
820 */
821 spin_lock(&epca_lock);
822 globalwinon(ch);
823 ch->statusflags = 0;
824
825 /* Save boards current modem status */
826 ch->imodem = readb(&bc->mstat);
827
828 /*
829 * Set receive head and tail ptrs to each other. This indicates no data
830 * available to read.
831 */
832 head = readw(&bc->rin);
833 writew(head, &bc->rout);
834
835 /* Set the channels associated tty structure */
836
837 /*
838 * The below routine generally sets up parity, baud, flow control
839 * issues, etc.... It effect both control flags and input flags.
840 */
841 epcaparam(tty, ch);
842 memoff(ch);
843 spin_unlock(&epca_lock);
844 port->flags |= ASYNC_INITIALIZED;
845 spin_unlock_irqrestore(&port->lock, flags);
846
847 retval = tty_port_block_til_ready(port, tty, filp);
848 if (retval)
849 return retval;
850 /*
851 * Set this again in case a hangup set it to zero while this open() was
852 * waiting for the line...
853 */
854 spin_lock_irqsave(&port->lock, flags);
855 port->tty = tty;
856 spin_lock(&epca_lock);
857 globalwinon(ch);
858 /* Enable Digi Data events */
859 writeb(1, &bc->idata);
860 memoff(ch);
861 spin_unlock(&epca_lock);
862 spin_unlock_irqrestore(&port->lock, flags);
863 return 0;
864}
865
866static int __init epca_module_init(void)
867{
868 return pc_init();
869}
870module_init(epca_module_init);
871
872static struct pci_driver epca_driver;
873
874static void __exit epca_module_exit(void)
875{
876 int count, crd;
877 struct board_info *bd;
878 struct channel *ch;
879
880 del_timer_sync(&epca_timer);
881
882 if (tty_unregister_driver(pc_driver) ||
883 tty_unregister_driver(pc_info)) {
884 printk(KERN_WARNING "epca: cleanup_module failed to un-register tty driver\n");
885 return;
886 }
887 put_tty_driver(pc_driver);
888 put_tty_driver(pc_info);
889
890 for (crd = 0; crd < num_cards; crd++) {
891 bd = &boards[crd];
892 if (!bd) { /* sanity check */
893 printk(KERN_ERR "<Error> - Digi : cleanup_module failed\n");
894 return;
895 }
896 ch = card_ptr[crd];
897 for (count = 0; count < bd->numports; count++, ch++) {
898 struct tty_struct *tty = tty_port_tty_get(&ch->port);
899 if (tty) {
900 tty_hangup(tty);
901 tty_kref_put(tty);
902 }
903 }
904 }
905 pci_unregister_driver(&epca_driver);
906}
907module_exit(epca_module_exit);
908
909static const struct tty_operations pc_ops = {
910 .open = pc_open,
911 .close = pc_close,
912 .write = pc_write,
913 .write_room = pc_write_room,
914 .flush_buffer = pc_flush_buffer,
915 .chars_in_buffer = pc_chars_in_buffer,
916 .flush_chars = pc_flush_chars,
917 .ioctl = pc_ioctl,
918 .set_termios = pc_set_termios,
919 .stop = pc_stop,
920 .start = pc_start,
921 .throttle = pc_throttle,
922 .unthrottle = pc_unthrottle,
923 .hangup = pc_hangup,
924 .break_ctl = pc_send_break
925};
926
927static const struct tty_port_operations epca_port_ops = {
928 .carrier_raised = epca_carrier_raised,
929 .dtr_rts = epca_dtr_rts,
930};
931
932static int info_open(struct tty_struct *tty, struct file *filp)
933{
934 return 0;
935}
936
937static const struct tty_operations info_ops = {
938 .open = info_open,
939 .ioctl = info_ioctl,
940};
941
942static int __init pc_init(void)
943{
944 int crd;
945 struct board_info *bd;
946 unsigned char board_id = 0;
947 int err = -ENOMEM;
948
949 int pci_boards_found, pci_count;
950
951 pci_count = 0;
952
953 pc_driver = alloc_tty_driver(MAX_ALLOC);
954 if (!pc_driver)
955 goto out1;
956
957 pc_info = alloc_tty_driver(MAX_ALLOC);
958 if (!pc_info)
959 goto out2;
960
961 /*
962 * If epca_setup has not been ran by LILO set num_cards to defaults;
963 * copy board structure defined by digiConfig into drivers board
964 * structure. Note : If LILO has ran epca_setup then epca_setup will
965 * handle defining num_cards as well as copying the data into the board
966 * structure.
967 */
968 if (!liloconfig) {
969 /* driver has been configured via. epcaconfig */
970 nbdevs = NBDEVS;
971 num_cards = NUMCARDS;
972 memcpy(&boards, &static_boards,
973 sizeof(struct board_info) * NUMCARDS);
974 }
975
976 /*
977 * Note : If lilo was used to configure the driver and the ignore
978 * epcaconfig option was choosen (digiepca=2) then nbdevs and num_cards
979 * will equal 0 at this point. This is okay; PCI cards will still be
980 * picked up if detected.
981 */
982
983 /*
984 * Set up interrupt, we will worry about memory allocation in
985 * post_fep_init.
986 */
987 printk(KERN_INFO "DIGI epca driver version %s loaded.\n", VERSION);
988
989 /*
990 * NOTE : This code assumes that the number of ports found in the
991 * boards array is correct. This could be wrong if the card in question
992 * is PCI (And therefore has no ports entry in the boards structure.)
993 * The rest of the information will be valid for PCI because the
994 * beginning of pc_init scans for PCI and determines i/o and base
995 * memory addresses. I am not sure if it is possible to read the number
996 * of ports supported by the card prior to it being booted (Since that
997 * is the state it is in when pc_init is run). Because it is not
998 * possible to query the number of supported ports until after the card
999 * has booted; we are required to calculate the card_ptrs as the card
1000 * is initialized (Inside post_fep_init). The negative thing about this
1001 * approach is that digiDload's call to GET_INFO will have a bad port
1002 * value. (Since this is called prior to post_fep_init.)
1003 */
1004 pci_boards_found = 0;
1005 if (num_cards < MAXBOARDS)
1006 pci_boards_found += init_PCI();
1007 num_cards += pci_boards_found;
1008
1009 pc_driver->owner = THIS_MODULE;
1010 pc_driver->name = "ttyD";
1011 pc_driver->major = DIGI_MAJOR;
1012 pc_driver->minor_start = 0;
1013 pc_driver->type = TTY_DRIVER_TYPE_SERIAL;
1014 pc_driver->subtype = SERIAL_TYPE_NORMAL;
1015 pc_driver->init_termios = tty_std_termios;
1016 pc_driver->init_termios.c_iflag = 0;
1017 pc_driver->init_termios.c_oflag = 0;
1018 pc_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
1019 pc_driver->init_termios.c_lflag = 0;
1020 pc_driver->init_termios.c_ispeed = 9600;
1021 pc_driver->init_termios.c_ospeed = 9600;
1022 pc_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_HARDWARE_BREAK;
1023 tty_set_operations(pc_driver, &pc_ops);
1024
1025 pc_info->owner = THIS_MODULE;
1026 pc_info->name = "digi_ctl";
1027 pc_info->major = DIGIINFOMAJOR;
1028 pc_info->minor_start = 0;
1029 pc_info->type = TTY_DRIVER_TYPE_SERIAL;
1030 pc_info->subtype = SERIAL_TYPE_INFO;
1031 pc_info->init_termios = tty_std_termios;
1032 pc_info->init_termios.c_iflag = 0;
1033 pc_info->init_termios.c_oflag = 0;
1034 pc_info->init_termios.c_lflag = 0;
1035 pc_info->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL;
1036 pc_info->init_termios.c_ispeed = 9600;
1037 pc_info->init_termios.c_ospeed = 9600;
1038 pc_info->flags = TTY_DRIVER_REAL_RAW;
1039 tty_set_operations(pc_info, &info_ops);
1040
1041
1042 for (crd = 0; crd < num_cards; crd++) {
1043 /*
1044 * This is where the appropriate memory handlers for the
1045 * hardware is set. Everything at runtime blindly jumps through
1046 * these vectors.
1047 */
1048
1049 /* defined in epcaconfig.h */
1050 bd = &boards[crd];
1051
1052 switch (bd->type) {
1053 case PCXEM:
1054 case EISAXEM:
1055 bd->memwinon = pcxem_memwinon;
1056 bd->memwinoff = pcxem_memwinoff;
1057 bd->globalwinon = pcxem_globalwinon;
1058 bd->txwinon = pcxem_txwinon;
1059 bd->rxwinon = pcxem_rxwinon;
1060 bd->memoff = pcxem_memoff;
1061 bd->assertgwinon = dummy_assertgwinon;
1062 bd->assertmemoff = dummy_assertmemoff;
1063 break;
1064
1065 case PCIXEM:
1066 case PCIXRJ:
1067 case PCIXR:
1068 bd->memwinon = dummy_memwinon;
1069 bd->memwinoff = dummy_memwinoff;
1070 bd->globalwinon = dummy_globalwinon;
1071 bd->txwinon = dummy_txwinon;
1072 bd->rxwinon = dummy_rxwinon;
1073 bd->memoff = dummy_memoff;
1074 bd->assertgwinon = dummy_assertgwinon;
1075 bd->assertmemoff = dummy_assertmemoff;
1076 break;
1077
1078 case PCXE:
1079 case PCXEVE:
1080 bd->memwinon = pcxe_memwinon;
1081 bd->memwinoff = pcxe_memwinoff;
1082 bd->globalwinon = pcxe_globalwinon;
1083 bd->txwinon = pcxe_txwinon;
1084 bd->rxwinon = pcxe_rxwinon;
1085 bd->memoff = pcxe_memoff;
1086 bd->assertgwinon = dummy_assertgwinon;
1087 bd->assertmemoff = dummy_assertmemoff;
1088 break;
1089
1090 case PCXI:
1091 case PC64XE:
1092 bd->memwinon = pcxi_memwinon;
1093 bd->memwinoff = pcxi_memwinoff;
1094 bd->globalwinon = pcxi_globalwinon;
1095 bd->txwinon = pcxi_txwinon;
1096 bd->rxwinon = pcxi_rxwinon;
1097 bd->memoff = pcxi_memoff;
1098 bd->assertgwinon = pcxi_assertgwinon;
1099 bd->assertmemoff = pcxi_assertmemoff;
1100 break;
1101
1102 default:
1103 break;
1104 }
1105
1106 /*
1107 * Some cards need a memory segment to be defined for use in
1108 * transmit and receive windowing operations. These boards are
1109 * listed in the below switch. In the case of the XI the amount
1110 * of memory on the board is variable so the memory_seg is also
1111 * variable. This code determines what they segment should be.
1112 */
1113 switch (bd->type) {
1114 case PCXE:
1115 case PCXEVE:
1116 case PC64XE:
1117 bd->memory_seg = 0xf000;
1118 break;
1119
1120 case PCXI:
1121 board_id = inb((int)bd->port);
1122 if ((board_id & 0x1) == 0x1) {
1123 /* it's an XI card */
1124 /* Is it a 64K board */
1125 if ((board_id & 0x30) == 0)
1126 bd->memory_seg = 0xf000;
1127
1128 /* Is it a 128K board */
1129 if ((board_id & 0x30) == 0x10)
1130 bd->memory_seg = 0xe000;
1131
1132 /* Is is a 256K board */
1133 if ((board_id & 0x30) == 0x20)
1134 bd->memory_seg = 0xc000;
1135
1136 /* Is it a 512K board */
1137 if ((board_id & 0x30) == 0x30)
1138 bd->memory_seg = 0x8000;
1139 } else
1140 printk(KERN_ERR "epca: Board at 0x%x doesn't appear to be an XI\n", (int)bd->port);
1141 break;
1142 }
1143 }
1144
1145 err = tty_register_driver(pc_driver);
1146 if (err) {
1147 printk(KERN_ERR "Couldn't register Digi PC/ driver");
1148 goto out3;
1149 }
1150
1151 err = tty_register_driver(pc_info);
1152 if (err) {
1153 printk(KERN_ERR "Couldn't register Digi PC/ info ");
1154 goto out4;
1155 }
1156
1157 /* Start up the poller to check for events on all enabled boards */
1158 init_timer(&epca_timer);
1159 epca_timer.function = epcapoll;
1160 mod_timer(&epca_timer, jiffies + HZ/25);
1161 return 0;
1162
1163out4:
1164 tty_unregister_driver(pc_driver);
1165out3:
1166 put_tty_driver(pc_info);
1167out2:
1168 put_tty_driver(pc_driver);
1169out1:
1170 return err;
1171}
1172
1173static void post_fep_init(unsigned int crd)
1174{
1175 int i;
1176 void __iomem *memaddr;
1177 struct global_data __iomem *gd;
1178 struct board_info *bd;
1179 struct board_chan __iomem *bc;
1180 struct channel *ch;
1181 int shrinkmem = 0, lowwater;
1182
1183 /*
1184 * This call is made by the user via. the ioctl call DIGI_INIT. It is
1185 * responsible for setting up all the card specific stuff.
1186 */
1187 bd = &boards[crd];
1188
1189 /*
1190 * If this is a PCI board, get the port info. Remember PCI cards do not
1191 * have entries into the epcaconfig.h file, so we can't get the number
1192 * of ports from it. Unfortunetly, this means that anyone doing a
1193 * DIGI_GETINFO before the board has booted will get an invalid number
1194 * of ports returned (It should return 0). Calls to DIGI_GETINFO after
1195 * DIGI_INIT has been called will return the proper values.
1196 */
1197 if (bd->type >= PCIXEM) { /* Begin get PCI number of ports */
1198 /*
1199 * Below we use XEMPORTS as a memory offset regardless of which
1200 * PCI card it is. This is because all of the supported PCI
1201 * cards have the same memory offset for the channel data. This
1202 * will have to be changed if we ever develop a PCI/XE card.
1203 * NOTE : The FEP manual states that the port offset is 0xC22
1204 * as opposed to 0xC02. This is only true for PC/XE, and PC/XI
1205 * cards; not for the XEM, or CX series. On the PCI cards the
1206 * number of ports is determined by reading a ID PROM located
1207 * in the box attached to the card. The card can then determine
1208 * the index the id to determine the number of ports available.
1209 * (FYI - The id should be located at 0x1ac (And may use up to
1210 * 4 bytes if the box in question is a XEM or CX)).
1211 */
1212 /* PCI cards are already remapped at this point ISA are not */
1213 bd->numports = readw(bd->re_map_membase + XEMPORTS);
1214 epcaassert(bd->numports <= 64, "PCI returned a invalid number of ports");
1215 nbdevs += (bd->numports);
1216 } else {
1217 /* Fix up the mappings for ISA/EISA etc */
1218 /* FIXME: 64K - can we be smarter ? */
1219 bd->re_map_membase = ioremap_nocache(bd->membase, 0x10000);
1220 }
1221
1222 if (crd != 0)
1223 card_ptr[crd] = card_ptr[crd-1] + boards[crd-1].numports;
1224 else
1225 card_ptr[crd] = &digi_channels[crd]; /* <- For card 0 only */
1226
1227 ch = card_ptr[crd];
1228 epcaassert(ch <= &digi_channels[nbdevs - 1], "ch out of range");
1229
1230 memaddr = bd->re_map_membase;
1231
1232 /*
1233 * The below assignment will set bc to point at the BEGINING of the
1234 * cards channel structures. For 1 card there will be between 8 and 64
1235 * of these structures.
1236 */
1237 bc = memaddr + CHANSTRUCT;
1238
1239 /*
1240 * The below assignment will set gd to point at the BEGINING of global
1241 * memory address 0xc00. The first data in that global memory actually
1242 * starts at address 0xc1a. The command in pointer begins at 0xd10.
1243 */
1244 gd = memaddr + GLOBAL;
1245
1246 /*
1247 * XEPORTS (address 0xc22) points at the number of channels the card
1248 * supports. (For 64XE, XI, XEM, and XR use 0xc02)
1249 */
1250 if ((bd->type == PCXEVE || bd->type == PCXE) &&
1251 (readw(memaddr + XEPORTS) < 3))
1252 shrinkmem = 1;
1253 if (bd->type < PCIXEM)
1254 if (!request_region((int)bd->port, 4, board_desc[bd->type]))
1255 return;
1256 memwinon(bd, 0);
1257
1258 /*
1259 * Remember ch is the main drivers channels structure, while bc is the
1260 * cards channel structure.
1261 */
1262 for (i = 0; i < bd->numports; i++, ch++, bc++) {
1263 unsigned long flags;
1264 u16 tseg, rseg;
1265
1266 tty_port_init(&ch->port);
1267 ch->port.ops = &epca_port_ops;
1268 ch->brdchan = bc;
1269 ch->mailbox = gd;
1270 INIT_WORK(&ch->tqueue, do_softint);
1271 ch->board = &boards[crd];
1272
1273 spin_lock_irqsave(&epca_lock, flags);
1274 switch (bd->type) {
1275 /*
1276 * Since some of the boards use different bitmaps for
1277 * their control signals we cannot hard code these
1278 * values and retain portability. We virtualize this
1279 * data here.
1280 */
1281 case EISAXEM:
1282 case PCXEM:
1283 case PCIXEM:
1284 case PCIXRJ:
1285 case PCIXR:
1286 ch->m_rts = 0x02;
1287 ch->m_dcd = 0x80;
1288 ch->m_dsr = 0x20;
1289 ch->m_cts = 0x10;
1290 ch->m_ri = 0x40;
1291 ch->m_dtr = 0x01;
1292 break;
1293
1294 case PCXE:
1295 case PCXEVE:
1296 case PCXI:
1297 case PC64XE:
1298 ch->m_rts = 0x02;
1299 ch->m_dcd = 0x08;
1300 ch->m_dsr = 0x10;
1301 ch->m_cts = 0x20;
1302 ch->m_ri = 0x40;
1303 ch->m_dtr = 0x80;
1304 break;
1305 }
1306
1307 if (boards[crd].altpin) {
1308 ch->dsr = ch->m_dcd;
1309 ch->dcd = ch->m_dsr;
1310 ch->digiext.digi_flags |= DIGI_ALTPIN;
1311 } else {
1312 ch->dcd = ch->m_dcd;
1313 ch->dsr = ch->m_dsr;
1314 }
1315
1316 ch->boardnum = crd;
1317 ch->channelnum = i;
1318 ch->magic = EPCA_MAGIC;
1319 tty_port_tty_set(&ch->port, NULL);
1320
1321 if (shrinkmem) {
1322 fepcmd(ch, SETBUFFER, 32, 0, 0, 0);
1323 shrinkmem = 0;
1324 }
1325
1326 tseg = readw(&bc->tseg);
1327 rseg = readw(&bc->rseg);
1328
1329 switch (bd->type) {
1330 case PCIXEM:
1331 case PCIXRJ:
1332 case PCIXR:
1333 /* Cover all the 2MEG cards */
1334 ch->txptr = memaddr + ((tseg << 4) & 0x1fffff);
1335 ch->rxptr = memaddr + ((rseg << 4) & 0x1fffff);
1336 ch->txwin = FEPWIN | (tseg >> 11);
1337 ch->rxwin = FEPWIN | (rseg >> 11);
1338 break;
1339
1340 case PCXEM:
1341 case EISAXEM:
1342 /* Cover all the 32K windowed cards */
1343 /* Mask equal to window size - 1 */
1344 ch->txptr = memaddr + ((tseg << 4) & 0x7fff);
1345 ch->rxptr = memaddr + ((rseg << 4) & 0x7fff);
1346 ch->txwin = FEPWIN | (tseg >> 11);
1347 ch->rxwin = FEPWIN | (rseg >> 11);
1348 break;
1349
1350 case PCXEVE:
1351 case PCXE:
1352 ch->txptr = memaddr + (((tseg - bd->memory_seg) << 4)
1353 & 0x1fff);
1354 ch->txwin = FEPWIN | ((tseg - bd->memory_seg) >> 9);
1355 ch->rxptr = memaddr + (((rseg - bd->memory_seg) << 4)
1356 & 0x1fff);
1357 ch->rxwin = FEPWIN | ((rseg - bd->memory_seg) >> 9);
1358 break;
1359
1360 case PCXI:
1361 case PC64XE:
1362 ch->txptr = memaddr + ((tseg - bd->memory_seg) << 4);
1363 ch->rxptr = memaddr + ((rseg - bd->memory_seg) << 4);
1364 ch->txwin = ch->rxwin = 0;
1365 break;
1366 }
1367
1368 ch->txbufhead = 0;
1369 ch->txbufsize = readw(&bc->tmax) + 1;
1370
1371 ch->rxbufhead = 0;
1372 ch->rxbufsize = readw(&bc->rmax) + 1;
1373
1374 lowwater = ch->txbufsize >= 2000 ? 1024 : (ch->txbufsize / 2);
1375
1376 /* Set transmitter low water mark */
1377 fepcmd(ch, STXLWATER, lowwater, 0, 10, 0);
1378
1379 /* Set receiver low water mark */
1380 fepcmd(ch, SRXLWATER, (ch->rxbufsize / 4), 0, 10, 0);
1381
1382 /* Set receiver high water mark */
1383 fepcmd(ch, SRXHWATER, (3 * ch->rxbufsize / 4), 0, 10, 0);
1384
1385 writew(100, &bc->edelay);
1386 writeb(1, &bc->idata);
1387
1388 ch->startc = readb(&bc->startc);
1389 ch->stopc = readb(&bc->stopc);
1390 ch->startca = readb(&bc->startca);
1391 ch->stopca = readb(&bc->stopca);
1392
1393 ch->fepcflag = 0;
1394 ch->fepiflag = 0;
1395 ch->fepoflag = 0;
1396 ch->fepstartc = 0;
1397 ch->fepstopc = 0;
1398 ch->fepstartca = 0;
1399 ch->fepstopca = 0;
1400
1401 ch->port.close_delay = 50;
1402
1403 spin_unlock_irqrestore(&epca_lock, flags);
1404 }
1405
1406 printk(KERN_INFO
1407 "Digi PC/Xx Driver V%s: %s I/O = 0x%lx Mem = 0x%lx Ports = %d\n",
1408 VERSION, board_desc[bd->type], (long)bd->port,
1409 (long)bd->membase, bd->numports);
1410 memwinoff(bd, 0);
1411}
1412
1413static void epcapoll(unsigned long ignored)
1414{
1415 unsigned long flags;
1416 int crd;
1417 unsigned int head, tail;
1418 struct channel *ch;
1419 struct board_info *bd;
1420
1421 /*
1422 * This routine is called upon every timer interrupt. Even though the
1423 * Digi series cards are capable of generating interrupts this method
1424 * of non-looping polling is more efficient. This routine checks for
1425 * card generated events (Such as receive data, are transmit buffer
1426 * empty) and acts on those events.
1427 */
1428 for (crd = 0; crd < num_cards; crd++) {
1429 bd = &boards[crd];
1430 ch = card_ptr[crd];
1431
1432 if ((bd->status == DISABLED) || digi_poller_inhibited)
1433 continue;
1434
1435 /*
1436 * assertmemoff is not needed here; indeed it is an empty
1437 * subroutine. It is being kept because future boards may need
1438 * this as well as some legacy boards.
1439 */
1440 spin_lock_irqsave(&epca_lock, flags);
1441
1442 assertmemoff(ch);
1443
1444 globalwinon(ch);
1445
1446 /*
1447 * In this case head and tail actually refer to the event queue
1448 * not the transmit or receive queue.
1449 */
1450 head = readw(&ch->mailbox->ein);
1451 tail = readw(&ch->mailbox->eout);
1452
1453 /* If head isn't equal to tail we have an event */
1454 if (head != tail)
1455 doevent(crd);
1456 memoff(ch);
1457
1458 spin_unlock_irqrestore(&epca_lock, flags);
1459 } /* End for each card */
1460 mod_timer(&epca_timer, jiffies + (HZ / 25));
1461}
1462
1463static void doevent(int crd)
1464{
1465 void __iomem *eventbuf;
1466 struct channel *ch, *chan0;
1467 static struct tty_struct *tty;
1468 struct board_info *bd;
1469 struct board_chan __iomem *bc;
1470 unsigned int tail, head;
1471 int event, channel;
1472 int mstat, lstat;
1473
1474 /*
1475 * This subroutine is called by epcapoll when an event is detected
1476 * in the event queue. This routine responds to those events.
1477 */
1478 bd = &boards[crd];
1479
1480 chan0 = card_ptr[crd];
1481 epcaassert(chan0 <= &digi_channels[nbdevs - 1], "ch out of range");
1482 assertgwinon(chan0);
1483 while ((tail = readw(&chan0->mailbox->eout)) !=
1484 (head = readw(&chan0->mailbox->ein))) {
1485 /* Begin while something in event queue */
1486 assertgwinon(chan0);
1487 eventbuf = bd->re_map_membase + tail + ISTART;
1488 /* Get the channel the event occurred on */
1489 channel = readb(eventbuf);
1490 /* Get the actual event code that occurred */
1491 event = readb(eventbuf + 1);
1492 /*
1493 * The two assignments below get the current modem status
1494 * (mstat) and the previous modem status (lstat). These are
1495 * useful becuase an event could signal a change in modem
1496 * signals itself.
1497 */
1498 mstat = readb(eventbuf + 2);
1499 lstat = readb(eventbuf + 3);
1500
1501 ch = chan0 + channel;
1502 if ((unsigned)channel >= bd->numports || !ch) {
1503 if (channel >= bd->numports)
1504 ch = chan0;
1505 bc = ch->brdchan;
1506 goto next;
1507 }
1508
1509 bc = ch->brdchan;
1510 if (bc == NULL)
1511 goto next;
1512
1513 tty = tty_port_tty_get(&ch->port);
1514 if (event & DATA_IND) { /* Begin DATA_IND */
1515 receive_data(ch, tty);
1516 assertgwinon(ch);
1517 } /* End DATA_IND */
1518 /* else *//* Fix for DCD transition missed bug */
1519 if (event & MODEMCHG_IND) {
1520 /* A modem signal change has been indicated */
1521 ch->imodem = mstat;
1522 if (test_bit(ASYNCB_CHECK_CD, &ch->port.flags)) {
1523 /* We are now receiving dcd */
1524 if (mstat & ch->dcd)
1525 wake_up_interruptible(&ch->port.open_wait);
1526 else /* No dcd; hangup */
1527 pc_sched_event(ch, EPCA_EVENT_HANGUP);
1528 }
1529 }
1530 if (tty) {
1531 if (event & BREAK_IND) {
1532 /* A break has been indicated */
1533 tty_insert_flip_char(tty, 0, TTY_BREAK);
1534 tty_schedule_flip(tty);
1535 } else if (event & LOWTX_IND) {
1536 if (ch->statusflags & LOWWAIT) {
1537 ch->statusflags &= ~LOWWAIT;
1538 tty_wakeup(tty);
1539 }
1540 } else if (event & EMPTYTX_IND) {
1541 /* This event is generated by
1542 setup_empty_event */
1543 ch->statusflags &= ~TXBUSY;
1544 if (ch->statusflags & EMPTYWAIT) {
1545 ch->statusflags &= ~EMPTYWAIT;
1546 tty_wakeup(tty);
1547 }
1548 }
1549 tty_kref_put(tty);
1550 }
1551next:
1552 globalwinon(ch);
1553 BUG_ON(!bc);
1554 writew(1, &bc->idata);
1555 writew((tail + 4) & (IMAX - ISTART - 4), &chan0->mailbox->eout);
1556 globalwinon(chan0);
1557 } /* End while something in event queue */
1558}
1559
1560static void fepcmd(struct channel *ch, int cmd, int word_or_byte,
1561 int byte2, int ncmds, int bytecmd)
1562{
1563 unchar __iomem *memaddr;
1564 unsigned int head, cmdTail, cmdStart, cmdMax;
1565 long count;
1566 int n;
1567
1568 /* This is the routine in which commands may be passed to the card. */
1569
1570 if (ch->board->status == DISABLED)
1571 return;
1572 assertgwinon(ch);
1573 /* Remember head (As well as max) is just an offset not a base addr */
1574 head = readw(&ch->mailbox->cin);
1575 /* cmdStart is a base address */
1576 cmdStart = readw(&ch->mailbox->cstart);
1577 /*
1578 * We do the addition below because we do not want a max pointer
1579 * relative to cmdStart. We want a max pointer that points at the
1580 * physical end of the command queue.
1581 */
1582 cmdMax = (cmdStart + 4 + readw(&ch->mailbox->cmax));
1583 memaddr = ch->board->re_map_membase;
1584
1585 if (head >= (cmdMax - cmdStart) || (head & 03)) {
1586 printk(KERN_ERR "line %d: Out of range, cmd = %x, head = %x\n",
1587 __LINE__, cmd, head);
1588 printk(KERN_ERR "line %d: Out of range, cmdMax = %x, cmdStart = %x\n",
1589 __LINE__, cmdMax, cmdStart);
1590 return;
1591 }
1592 if (bytecmd) {
1593 writeb(cmd, memaddr + head + cmdStart + 0);
1594 writeb(ch->channelnum, memaddr + head + cmdStart + 1);
1595 /* Below word_or_byte is bits to set */
1596 writeb(word_or_byte, memaddr + head + cmdStart + 2);
1597 /* Below byte2 is bits to reset */
1598 writeb(byte2, memaddr + head + cmdStart + 3);
1599 } else {
1600 writeb(cmd, memaddr + head + cmdStart + 0);
1601 writeb(ch->channelnum, memaddr + head + cmdStart + 1);
1602 writeb(word_or_byte, memaddr + head + cmdStart + 2);
1603 }
1604 head = (head + 4) & (cmdMax - cmdStart - 4);
1605 writew(head, &ch->mailbox->cin);
1606 count = FEPTIMEOUT;
1607
1608 for (;;) {
1609 count--;
1610 if (count == 0) {
1611 printk(KERN_ERR "<Error> - Fep not responding in fepcmd()\n");
1612 return;
1613 }
1614 head = readw(&ch->mailbox->cin);
1615 cmdTail = readw(&ch->mailbox->cout);
1616 n = (head - cmdTail) & (cmdMax - cmdStart - 4);
1617 /*
1618 * Basically this will break when the FEP acknowledges the
1619 * command by incrementing cmdTail (Making it equal to head).
1620 */
1621 if (n <= ncmds * (sizeof(short) * 4))
1622 break;
1623 }
1624}
1625
1626/*
1627 * Digi products use fields in their channels structures that are very similar
1628 * to the c_cflag and c_iflag fields typically found in UNIX termios
1629 * structures. The below three routines allow mappings between these hardware
1630 * "flags" and their respective Linux flags.
1631 */
1632static unsigned termios2digi_h(struct channel *ch, unsigned cflag)
1633{
1634 unsigned res = 0;
1635
1636 if (cflag & CRTSCTS) {
1637 ch->digiext.digi_flags |= (RTSPACE | CTSPACE);
1638 res |= ((ch->m_cts) | (ch->m_rts));
1639 }
1640
1641 if (ch->digiext.digi_flags & RTSPACE)
1642 res |= ch->m_rts;
1643
1644 if (ch->digiext.digi_flags & DTRPACE)
1645 res |= ch->m_dtr;
1646
1647 if (ch->digiext.digi_flags & CTSPACE)
1648 res |= ch->m_cts;
1649
1650 if (ch->digiext.digi_flags & DSRPACE)
1651 res |= ch->dsr;
1652
1653 if (ch->digiext.digi_flags & DCDPACE)
1654 res |= ch->dcd;
1655
1656 if (res & (ch->m_rts))
1657 ch->digiext.digi_flags |= RTSPACE;
1658
1659 if (res & (ch->m_cts))
1660 ch->digiext.digi_flags |= CTSPACE;
1661
1662 return res;
1663}
1664
1665static unsigned termios2digi_i(struct channel *ch, unsigned iflag)
1666{
1667 unsigned res = iflag & (IGNBRK | BRKINT | IGNPAR | PARMRK |
1668 INPCK | ISTRIP | IXON | IXANY | IXOFF);
1669 if (ch->digiext.digi_flags & DIGI_AIXON)
1670 res |= IAIXON;
1671 return res;
1672}
1673
1674static unsigned termios2digi_c(struct channel *ch, unsigned cflag)
1675{
1676 unsigned res = 0;
1677 if (cflag & CBAUDEX) {
1678 ch->digiext.digi_flags |= DIGI_FAST;
1679 /*
1680 * HUPCL bit is used by FEP to indicate fast baud table is to
1681 * be used.
1682 */
1683 res |= FEP_HUPCL;
1684 } else
1685 ch->digiext.digi_flags &= ~DIGI_FAST;
1686 /*
1687 * CBAUD has bit position 0x1000 set these days to indicate Linux
1688 * baud rate remap. Digi hardware can't handle the bit assignment.
1689 * (We use a different bit assignment for high speed.). Clear this
1690 * bit out.
1691 */
1692 res |= cflag & ((CBAUD ^ CBAUDEX) | PARODD | PARENB | CSTOPB | CSIZE);
1693 /*
1694 * This gets a little confusing. The Digi cards have their own
1695 * representation of c_cflags controlling baud rate. For the most part
1696 * this is identical to the Linux implementation. However; Digi
1697 * supports one rate (76800) that Linux doesn't. This means that the
1698 * c_cflag entry that would normally mean 76800 for Digi actually means
1699 * 115200 under Linux. Without the below mapping, a stty 115200 would
1700 * only drive the board at 76800. Since the rate 230400 is also found
1701 * after 76800, the same problem afflicts us when we choose a rate of
1702 * 230400. Without the below modificiation stty 230400 would actually
1703 * give us 115200.
1704 *
1705 * There are two additional differences. The Linux value for CLOCAL
1706 * (0x800; 0004000) has no meaning to the Digi hardware. Also in later
1707 * releases of Linux; the CBAUD define has CBAUDEX (0x1000; 0010000)
1708 * ored into it (CBAUD = 0x100f as opposed to 0xf). CBAUDEX should be
1709 * checked for a screened out prior to termios2digi_c returning. Since
1710 * CLOCAL isn't used by the board this can be ignored as long as the
1711 * returned value is used only by Digi hardware.
1712 */
1713 if (cflag & CBAUDEX) {
1714 /*
1715 * The below code is trying to guarantee that only baud rates
1716 * 115200 and 230400 are remapped. We use exclusive or because
1717 * the various baud rates share common bit positions and
1718 * therefore can't be tested for easily.
1719 */
1720 if ((!((cflag & 0x7) ^ (B115200 & ~CBAUDEX))) ||
1721 (!((cflag & 0x7) ^ (B230400 & ~CBAUDEX))))
1722 res += 1;
1723 }
1724 return res;
1725}
1726
1727/* Caller must hold the locks */
1728static void epcaparam(struct tty_struct *tty, struct channel *ch)
1729{
1730 unsigned int cmdHead;
1731 struct ktermios *ts;
1732 struct board_chan __iomem *bc;
1733 unsigned mval, hflow, cflag, iflag;
1734
1735 bc = ch->brdchan;
1736 epcaassert(bc != NULL, "bc out of range");
1737
1738 assertgwinon(ch);
1739 ts = tty->termios;
1740 if ((ts->c_cflag & CBAUD) == 0) { /* Begin CBAUD detected */
1741 cmdHead = readw(&bc->rin);
1742 writew(cmdHead, &bc->rout);
1743 cmdHead = readw(&bc->tin);
1744 /* Changing baud in mid-stream transmission can be wonderful */
1745 /*
1746 * Flush current transmit buffer by setting cmdTail pointer
1747 * (tout) to cmdHead pointer (tin). Hopefully the transmit
1748 * buffer is empty.
1749 */
1750 fepcmd(ch, STOUT, (unsigned) cmdHead, 0, 0, 0);
1751 mval = 0;
1752 } else { /* Begin CBAUD not detected */
1753 /*
1754 * c_cflags have changed but that change had nothing to do with
1755 * BAUD. Propagate the change to the card.
1756 */
1757 cflag = termios2digi_c(ch, ts->c_cflag);
1758 if (cflag != ch->fepcflag) {
1759 ch->fepcflag = cflag;
1760 /* Set baud rate, char size, stop bits, parity */
1761 fepcmd(ch, SETCTRLFLAGS, (unsigned) cflag, 0, 0, 0);
1762 }
1763 /*
1764 * If the user has not forced CLOCAL and if the device is not a
1765 * CALLOUT device (Which is always CLOCAL) we set flags such
1766 * that the driver will wait on carrier detect.
1767 */
1768 if (ts->c_cflag & CLOCAL)
1769 clear_bit(ASYNCB_CHECK_CD, &ch->port.flags);
1770 else
1771 set_bit(ASYNCB_CHECK_CD, &ch->port.flags);
1772 mval = ch->m_dtr | ch->m_rts;
1773 } /* End CBAUD not detected */
1774 iflag = termios2digi_i(ch, ts->c_iflag);
1775 /* Check input mode flags */
1776 if (iflag != ch->fepiflag) {
1777 ch->fepiflag = iflag;
1778 /*
1779 * Command sets channels iflag structure on the board. Such
1780 * things as input soft flow control, handling of parity
1781 * errors, and break handling are all set here.
1782 *
1783 * break handling, parity handling, input stripping,
1784 * flow control chars
1785 */
1786 fepcmd(ch, SETIFLAGS, (unsigned int) ch->fepiflag, 0, 0, 0);
1787 }
1788 /*
1789 * Set the board mint value for this channel. This will cause hardware
1790 * events to be generated each time the DCD signal (Described in mint)
1791 * changes.
1792 */
1793 writeb(ch->dcd, &bc->mint);
1794 if ((ts->c_cflag & CLOCAL) || (ch->digiext.digi_flags & DIGI_FORCEDCD))
1795 if (ch->digiext.digi_flags & DIGI_FORCEDCD)
1796 writeb(0, &bc->mint);
1797 ch->imodem = readb(&bc->mstat);
1798 hflow = termios2digi_h(ch, ts->c_cflag);
1799 if (hflow != ch->hflow) {
1800 ch->hflow = hflow;
1801 /*
1802 * Hard flow control has been selected but the board is not
1803 * using it. Activate hard flow control now.
1804 */
1805 fepcmd(ch, SETHFLOW, hflow, 0xff, 0, 1);
1806 }
1807 mval ^= ch->modemfake & (mval ^ ch->modem);
1808
1809 if (ch->omodem ^ mval) {
1810 ch->omodem = mval;
1811 /*
1812 * The below command sets the DTR and RTS mstat structure. If
1813 * hard flow control is NOT active these changes will drive the
1814 * output of the actual DTR and RTS lines. If hard flow control
1815 * is active, the changes will be saved in the mstat structure
1816 * and only asserted when hard flow control is turned off.
1817 */
1818
1819 /* First reset DTR & RTS; then set them */
1820 fepcmd(ch, SETMODEM, 0, ((ch->m_dtr)|(ch->m_rts)), 0, 1);
1821 fepcmd(ch, SETMODEM, mval, 0, 0, 1);
1822 }
1823 if (ch->startc != ch->fepstartc || ch->stopc != ch->fepstopc) {
1824 ch->fepstartc = ch->startc;
1825 ch->fepstopc = ch->stopc;
1826 /*
1827 * The XON / XOFF characters have changed; propagate these
1828 * changes to the card.
1829 */
1830 fepcmd(ch, SONOFFC, ch->fepstartc, ch->fepstopc, 0, 1);
1831 }
1832 if (ch->startca != ch->fepstartca || ch->stopca != ch->fepstopca) {
1833 ch->fepstartca = ch->startca;
1834 ch->fepstopca = ch->stopca;
1835 /*
1836 * Similar to the above, this time the auxilarly XON / XOFF
1837 * characters have changed; propagate these changes to the card.
1838 */
1839 fepcmd(ch, SAUXONOFFC, ch->fepstartca, ch->fepstopca, 0, 1);
1840 }
1841}
1842
1843/* Caller holds lock */
1844static void receive_data(struct channel *ch, struct tty_struct *tty)
1845{
1846 unchar *rptr;
1847 struct ktermios *ts = NULL;
1848 struct board_chan __iomem *bc;
1849 int dataToRead, wrapgap, bytesAvailable;
1850 unsigned int tail, head;
1851 unsigned int wrapmask;
1852
1853 /*
1854 * This routine is called by doint when a receive data event has taken
1855 * place.
1856 */
1857 globalwinon(ch);
1858 if (ch->statusflags & RXSTOPPED)
1859 return;
1860 if (tty)
1861 ts = tty->termios;
1862 bc = ch->brdchan;
1863 BUG_ON(!bc);
1864 wrapmask = ch->rxbufsize - 1;
1865
1866 /*
1867 * Get the head and tail pointers to the receiver queue. Wrap the head
1868 * pointer if it has reached the end of the buffer.
1869 */
1870 head = readw(&bc->rin);
1871 head &= wrapmask;
1872 tail = readw(&bc->rout) & wrapmask;
1873
1874 bytesAvailable = (head - tail) & wrapmask;
1875 if (bytesAvailable == 0)
1876 return;
1877
1878 /* If CREAD bit is off or device not open, set TX tail to head */
1879 if (!tty || !ts || !(ts->c_cflag & CREAD)) {
1880 writew(head, &bc->rout);
1881 return;
1882 }
1883
1884 if (tty_buffer_request_room(tty, bytesAvailable + 1) == 0)
1885 return;
1886
1887 if (readb(&bc->orun)) {
1888 writeb(0, &bc->orun);
1889 printk(KERN_WARNING "epca; overrun! DigiBoard device %s\n",
1890 tty->name);
1891 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
1892 }
1893 rxwinon(ch);
1894 while (bytesAvailable > 0) {
1895 /* Begin while there is data on the card */
1896 wrapgap = (head >= tail) ? head - tail : ch->rxbufsize - tail;
1897 /*
1898 * Even if head has wrapped around only report the amount of
1899 * data to be equal to the size - tail. Remember memcpy can't
1900 * automaticly wrap around the receive buffer.
1901 */
1902 dataToRead = (wrapgap < bytesAvailable) ? wrapgap
1903 : bytesAvailable;
1904 /* Make sure we don't overflow the buffer */
1905 dataToRead = tty_prepare_flip_string(tty, &rptr, dataToRead);
1906 if (dataToRead == 0)
1907 break;
1908 /*
1909 * Move data read from our card into the line disciplines
1910 * buffer for translation if necessary.
1911 */
1912 memcpy_fromio(rptr, ch->rxptr + tail, dataToRead);
1913 tail = (tail + dataToRead) & wrapmask;
1914 bytesAvailable -= dataToRead;
1915 } /* End while there is data on the card */
1916 globalwinon(ch);
1917 writew(tail, &bc->rout);
1918 /* Must be called with global data */
1919 tty_schedule_flip(tty);
1920}
1921
1922static int info_ioctl(struct tty_struct *tty, struct file *file,
1923 unsigned int cmd, unsigned long arg)
1924{
1925 switch (cmd) {
1926 case DIGI_GETINFO:
1927 {
1928 struct digi_info di;
1929 int brd;
1930
1931 if (get_user(brd, (unsigned int __user *)arg))
1932 return -EFAULT;
1933 if (brd < 0 || brd >= num_cards || num_cards == 0)
1934 return -ENODEV;
1935
1936 memset(&di, 0, sizeof(di));
1937
1938 di.board = brd;
1939 di.status = boards[brd].status;
1940 di.type = boards[brd].type ;
1941 di.numports = boards[brd].numports ;
1942 /* Legacy fixups - just move along nothing to see */
1943 di.port = (unsigned char *)boards[brd].port ;
1944 di.membase = (unsigned char *)boards[brd].membase ;
1945
1946 if (copy_to_user((void __user *)arg, &di, sizeof(di)))
1947 return -EFAULT;
1948 break;
1949
1950 }
1951
1952 case DIGI_POLLER:
1953 {
1954 int brd = arg & 0xff000000 >> 16;
1955 unsigned char state = arg & 0xff;
1956
1957 if (brd < 0 || brd >= num_cards) {
1958 printk(KERN_ERR "epca: DIGI POLLER : brd not valid!\n");
1959 return -ENODEV;
1960 }
1961 digi_poller_inhibited = state;
1962 break;
1963 }
1964
1965 case DIGI_INIT:
1966 {
1967 /*
1968 * This call is made by the apps to complete the
1969 * initialization of the board(s). This routine is
1970 * responsible for setting the card to its initial
1971 * state and setting the drivers control fields to the
1972 * sutianle settings for the card in question.
1973 */
1974 int crd;
1975 for (crd = 0; crd < num_cards; crd++)
1976 post_fep_init(crd);
1977 break;
1978 }
1979 default:
1980 return -ENOTTY;
1981 }
1982 return 0;
1983}
1984
1985static int pc_tiocmget(struct tty_struct *tty, struct file *file)
1986{
1987 struct channel *ch = tty->driver_data;
1988 struct board_chan __iomem *bc;
1989 unsigned int mstat, mflag = 0;
1990 unsigned long flags;
1991
1992 if (ch)
1993 bc = ch->brdchan;
1994 else
1995 return -EINVAL;
1996
1997 spin_lock_irqsave(&epca_lock, flags);
1998 globalwinon(ch);
1999 mstat = readb(&bc->mstat);
2000 memoff(ch);
2001 spin_unlock_irqrestore(&epca_lock, flags);
2002
2003 if (mstat & ch->m_dtr)
2004 mflag |= TIOCM_DTR;
2005 if (mstat & ch->m_rts)
2006 mflag |= TIOCM_RTS;
2007 if (mstat & ch->m_cts)
2008 mflag |= TIOCM_CTS;
2009 if (mstat & ch->dsr)
2010 mflag |= TIOCM_DSR;
2011 if (mstat & ch->m_ri)
2012 mflag |= TIOCM_RI;
2013 if (mstat & ch->dcd)
2014 mflag |= TIOCM_CD;
2015 return mflag;
2016}
2017
2018static int pc_tiocmset(struct tty_struct *tty, struct file *file,
2019 unsigned int set, unsigned int clear)
2020{
2021 struct channel *ch = tty->driver_data;
2022 unsigned long flags;
2023
2024 if (!ch)
2025 return -EINVAL;
2026
2027 spin_lock_irqsave(&epca_lock, flags);
2028 /*
2029 * I think this modemfake stuff is broken. It doesn't correctly reflect
2030 * the behaviour desired by the TIOCM* ioctls. Therefore this is
2031 * probably broken.
2032 */
2033 if (set & TIOCM_RTS) {
2034 ch->modemfake |= ch->m_rts;
2035 ch->modem |= ch->m_rts;
2036 }
2037 if (set & TIOCM_DTR) {
2038 ch->modemfake |= ch->m_dtr;
2039 ch->modem |= ch->m_dtr;
2040 }
2041 if (clear & TIOCM_RTS) {
2042 ch->modemfake |= ch->m_rts;
2043 ch->modem &= ~ch->m_rts;
2044 }
2045 if (clear & TIOCM_DTR) {
2046 ch->modemfake |= ch->m_dtr;
2047 ch->modem &= ~ch->m_dtr;
2048 }
2049 globalwinon(ch);
2050 /*
2051 * The below routine generally sets up parity, baud, flow control
2052 * issues, etc.... It effect both control flags and input flags.
2053 */
2054 epcaparam(tty, ch);
2055 memoff(ch);
2056 spin_unlock_irqrestore(&epca_lock, flags);
2057 return 0;
2058}
2059
2060static int pc_ioctl(struct tty_struct *tty, struct file *file,
2061 unsigned int cmd, unsigned long arg)
2062{
2063 digiflow_t dflow;
2064 unsigned long flags;
2065 unsigned int mflag, mstat;
2066 unsigned char startc, stopc;
2067 struct board_chan __iomem *bc;
2068 struct channel *ch = tty->driver_data;
2069 void __user *argp = (void __user *)arg;
2070
2071 if (ch)
2072 bc = ch->brdchan;
2073 else
2074 return -EINVAL;
2075 switch (cmd) {
2076 case TIOCMODG:
2077 mflag = pc_tiocmget(tty, file);
2078 if (put_user(mflag, (unsigned long __user *)argp))
2079 return -EFAULT;
2080 break;
2081 case TIOCMODS:
2082 if (get_user(mstat, (unsigned __user *)argp))
2083 return -EFAULT;
2084 return pc_tiocmset(tty, file, mstat, ~mstat);
2085 case TIOCSDTR:
2086 spin_lock_irqsave(&epca_lock, flags);
2087 ch->omodem |= ch->m_dtr;
2088 globalwinon(ch);
2089 fepcmd(ch, SETMODEM, ch->m_dtr, 0, 10, 1);
2090 memoff(ch);
2091 spin_unlock_irqrestore(&epca_lock, flags);
2092 break;
2093
2094 case TIOCCDTR:
2095 spin_lock_irqsave(&epca_lock, flags);
2096 ch->omodem &= ~ch->m_dtr;
2097 globalwinon(ch);
2098 fepcmd(ch, SETMODEM, 0, ch->m_dtr, 10, 1);
2099 memoff(ch);
2100 spin_unlock_irqrestore(&epca_lock, flags);
2101 break;
2102 case DIGI_GETA:
2103 if (copy_to_user(argp, &ch->digiext, sizeof(digi_t)))
2104 return -EFAULT;
2105 break;
2106 case DIGI_SETAW:
2107 case DIGI_SETAF:
2108 if (cmd == DIGI_SETAW) {
2109 /* Setup an event to indicate when the transmit
2110 buffer empties */
2111 spin_lock_irqsave(&epca_lock, flags);
2112 setup_empty_event(tty, ch);
2113 spin_unlock_irqrestore(&epca_lock, flags);
2114 tty_wait_until_sent(tty, 0);
2115 } else {
2116 /* ldisc lock already held in ioctl */
2117 if (tty->ldisc->ops->flush_buffer)
2118 tty->ldisc->ops->flush_buffer(tty);
2119 }
2120 /* Fall Thru */
2121 case DIGI_SETA:
2122 if (copy_from_user(&ch->digiext, argp, sizeof(digi_t)))
2123 return -EFAULT;
2124
2125 if (ch->digiext.digi_flags & DIGI_ALTPIN) {
2126 ch->dcd = ch->m_dsr;
2127 ch->dsr = ch->m_dcd;
2128 } else {
2129 ch->dcd = ch->m_dcd;
2130 ch->dsr = ch->m_dsr;
2131 }
2132
2133 spin_lock_irqsave(&epca_lock, flags);
2134 globalwinon(ch);
2135
2136 /*
2137 * The below routine generally sets up parity, baud, flow
2138 * control issues, etc.... It effect both control flags and
2139 * input flags.
2140 */
2141 epcaparam(tty, ch);
2142 memoff(ch);
2143 spin_unlock_irqrestore(&epca_lock, flags);
2144 break;
2145
2146 case DIGI_GETFLOW:
2147 case DIGI_GETAFLOW:
2148 spin_lock_irqsave(&epca_lock, flags);
2149 globalwinon(ch);
2150 if (cmd == DIGI_GETFLOW) {
2151 dflow.startc = readb(&bc->startc);
2152 dflow.stopc = readb(&bc->stopc);
2153 } else {
2154 dflow.startc = readb(&bc->startca);
2155 dflow.stopc = readb(&bc->stopca);
2156 }
2157 memoff(ch);
2158 spin_unlock_irqrestore(&epca_lock, flags);
2159
2160 if (copy_to_user(argp, &dflow, sizeof(dflow)))
2161 return -EFAULT;
2162 break;
2163
2164 case DIGI_SETAFLOW:
2165 case DIGI_SETFLOW:
2166 if (cmd == DIGI_SETFLOW) {
2167 startc = ch->startc;
2168 stopc = ch->stopc;
2169 } else {
2170 startc = ch->startca;
2171 stopc = ch->stopca;
2172 }
2173
2174 if (copy_from_user(&dflow, argp, sizeof(dflow)))
2175 return -EFAULT;
2176
2177 if (dflow.startc != startc || dflow.stopc != stopc) {
2178 /* Begin if setflow toggled */
2179 spin_lock_irqsave(&epca_lock, flags);
2180 globalwinon(ch);
2181
2182 if (cmd == DIGI_SETFLOW) {
2183 ch->fepstartc = ch->startc = dflow.startc;
2184 ch->fepstopc = ch->stopc = dflow.stopc;
2185 fepcmd(ch, SONOFFC, ch->fepstartc,
2186 ch->fepstopc, 0, 1);
2187 } else {
2188 ch->fepstartca = ch->startca = dflow.startc;
2189 ch->fepstopca = ch->stopca = dflow.stopc;
2190 fepcmd(ch, SAUXONOFFC, ch->fepstartca,
2191 ch->fepstopca, 0, 1);
2192 }
2193
2194 if (ch->statusflags & TXSTOPPED)
2195 pc_start(tty);
2196
2197 memoff(ch);
2198 spin_unlock_irqrestore(&epca_lock, flags);
2199 } /* End if setflow toggled */
2200 break;
2201 default:
2202 return -ENOIOCTLCMD;
2203 }
2204 return 0;
2205}
2206
2207static void pc_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
2208{
2209 struct channel *ch;
2210 unsigned long flags;
2211 /*
2212 * verifyChannel returns the channel from the tty struct if it is
2213 * valid. This serves as a sanity check.
2214 */
2215 ch = verifyChannel(tty);
2216
2217 if (ch != NULL) { /* Begin if channel valid */
2218 spin_lock_irqsave(&epca_lock, flags);
2219 globalwinon(ch);
2220 epcaparam(tty, ch);
2221 memoff(ch);
2222 spin_unlock_irqrestore(&epca_lock, flags);
2223
2224 if ((old_termios->c_cflag & CRTSCTS) &&
2225 ((tty->termios->c_cflag & CRTSCTS) == 0))
2226 tty->hw_stopped = 0;
2227
2228 if (!(old_termios->c_cflag & CLOCAL) &&
2229 (tty->termios->c_cflag & CLOCAL))
2230 wake_up_interruptible(&ch->port.open_wait);
2231
2232 } /* End if channel valid */
2233}
2234
2235static void do_softint(struct work_struct *work)
2236{
2237 struct channel *ch = container_of(work, struct channel, tqueue);
2238 /* Called in response to a modem change event */
2239 if (ch && ch->magic == EPCA_MAGIC) {
2240 struct tty_struct *tty = tty_port_tty_get(&ch->port);
2241
2242 if (tty && tty->driver_data) {
2243 if (test_and_clear_bit(EPCA_EVENT_HANGUP, &ch->event)) {
2244 tty_hangup(tty);
2245 wake_up_interruptible(&ch->port.open_wait);
2246 clear_bit(ASYNCB_NORMAL_ACTIVE,
2247 &ch->port.flags);
2248 }
2249 }
2250 tty_kref_put(tty);
2251 }
2252}
2253
2254/*
2255 * pc_stop and pc_start provide software flow control to the routine and the
2256 * pc_ioctl routine.
2257 */
2258static void pc_stop(struct tty_struct *tty)
2259{
2260 struct channel *ch;
2261 unsigned long flags;
2262 /*
2263 * verifyChannel returns the channel from the tty struct if it is
2264 * valid. This serves as a sanity check.
2265 */
2266 ch = verifyChannel(tty);
2267 if (ch != NULL) {
2268 spin_lock_irqsave(&epca_lock, flags);
2269 if ((ch->statusflags & TXSTOPPED) == 0) {
2270 /* Begin if transmit stop requested */
2271 globalwinon(ch);
2272 /* STOP transmitting now !! */
2273 fepcmd(ch, PAUSETX, 0, 0, 0, 0);
2274 ch->statusflags |= TXSTOPPED;
2275 memoff(ch);
2276 } /* End if transmit stop requested */
2277 spin_unlock_irqrestore(&epca_lock, flags);
2278 }
2279}
2280
2281static void pc_start(struct tty_struct *tty)
2282{
2283 struct channel *ch;
2284 /*
2285 * verifyChannel returns the channel from the tty struct if it is
2286 * valid. This serves as a sanity check.
2287 */
2288 ch = verifyChannel(tty);
2289 if (ch != NULL) {
2290 unsigned long flags;
2291 spin_lock_irqsave(&epca_lock, flags);
2292 /* Just in case output was resumed because of a change
2293 in Digi-flow */
2294 if (ch->statusflags & TXSTOPPED) {
2295 /* Begin transmit resume requested */
2296 struct board_chan __iomem *bc;
2297 globalwinon(ch);
2298 bc = ch->brdchan;
2299 if (ch->statusflags & LOWWAIT)
2300 writeb(1, &bc->ilow);
2301 /* Okay, you can start transmitting again... */
2302 fepcmd(ch, RESUMETX, 0, 0, 0, 0);
2303 ch->statusflags &= ~TXSTOPPED;
2304 memoff(ch);
2305 } /* End transmit resume requested */
2306 spin_unlock_irqrestore(&epca_lock, flags);
2307 }
2308}
2309
2310/*
2311 * The below routines pc_throttle and pc_unthrottle are used to slow (And
2312 * resume) the receipt of data into the kernels receive buffers. The exact
2313 * occurrence of this depends on the size of the kernels receive buffer and
2314 * what the 'watermarks' are set to for that buffer. See the n_ttys.c file for
2315 * more details.
2316 */
2317static void pc_throttle(struct tty_struct *tty)
2318{
2319 struct channel *ch;
2320 unsigned long flags;
2321 /*
2322 * verifyChannel returns the channel from the tty struct if it is
2323 * valid. This serves as a sanity check.
2324 */
2325 ch = verifyChannel(tty);
2326 if (ch != NULL) {
2327 spin_lock_irqsave(&epca_lock, flags);
2328 if ((ch->statusflags & RXSTOPPED) == 0) {
2329 globalwinon(ch);
2330 fepcmd(ch, PAUSERX, 0, 0, 0, 0);
2331 ch->statusflags |= RXSTOPPED;
2332 memoff(ch);
2333 }
2334 spin_unlock_irqrestore(&epca_lock, flags);
2335 }
2336}
2337
2338static void pc_unthrottle(struct tty_struct *tty)
2339{
2340 struct channel *ch;
2341 unsigned long flags;
2342 /*
2343 * verifyChannel returns the channel from the tty struct if it is
2344 * valid. This serves as a sanity check.
2345 */
2346 ch = verifyChannel(tty);
2347 if (ch != NULL) {
2348 /* Just in case output was resumed because of a change
2349 in Digi-flow */
2350 spin_lock_irqsave(&epca_lock, flags);
2351 if (ch->statusflags & RXSTOPPED) {
2352 globalwinon(ch);
2353 fepcmd(ch, RESUMERX, 0, 0, 0, 0);
2354 ch->statusflags &= ~RXSTOPPED;
2355 memoff(ch);
2356 }
2357 spin_unlock_irqrestore(&epca_lock, flags);
2358 }
2359}
2360
2361static int pc_send_break(struct tty_struct *tty, int msec)
2362{
2363 struct channel *ch = tty->driver_data;
2364 unsigned long flags;
2365
2366 if (msec == -1)
2367 msec = 0xFFFF;
2368 else if (msec > 0xFFFE)
2369 msec = 0xFFFE;
2370 else if (msec < 1)
2371 msec = 1;
2372
2373 spin_lock_irqsave(&epca_lock, flags);
2374 globalwinon(ch);
2375 /*
2376 * Maybe I should send an infinite break here, schedule() for msec
2377 * amount of time, and then stop the break. This way, the user can't
2378 * screw up the FEP by causing digi_send_break() to be called (i.e. via
2379 * an ioctl()) more than once in msec amount of time.
2380 * Try this for now...
2381 */
2382 fepcmd(ch, SENDBREAK, msec, 0, 10, 0);
2383 memoff(ch);
2384 spin_unlock_irqrestore(&epca_lock, flags);
2385 return 0;
2386}
2387
2388/* Caller MUST hold the lock */
2389static void setup_empty_event(struct tty_struct *tty, struct channel *ch)
2390{
2391 struct board_chan __iomem *bc = ch->brdchan;
2392
2393 globalwinon(ch);
2394 ch->statusflags |= EMPTYWAIT;
2395 /*
2396 * When set the iempty flag request a event to be generated when the
2397 * transmit buffer is empty (If there is no BREAK in progress).
2398 */
2399 writeb(1, &bc->iempty);
2400 memoff(ch);
2401}
2402
2403#ifndef MODULE
2404static void __init epca_setup(char *str, int *ints)
2405{
2406 struct board_info board;
2407 int index, loop, last;
2408 char *temp, *t2;
2409 unsigned len;
2410
2411 /*
2412 * If this routine looks a little strange it is because it is only
2413 * called if a LILO append command is given to boot the kernel with
2414 * parameters. In this way, we can provide the user a method of
2415 * changing his board configuration without rebuilding the kernel.
2416 */
2417 if (!liloconfig)
2418 liloconfig = 1;
2419
2420 memset(&board, 0, sizeof(board));
2421
2422 /* Assume the data is int first, later we can change it */
2423 /* I think that array position 0 of ints holds the number of args */
2424 for (last = 0, index = 1; index <= ints[0]; index++)
2425 switch (index) { /* Begin parse switch */
2426 case 1:
2427 board.status = ints[index];
2428 /*
2429 * We check for 2 (As opposed to 1; because 2 is a flag
2430 * instructing the driver to ignore epcaconfig.) For
2431 * this reason we check for 2.
2432 */
2433 if (board.status == 2) {
2434 /* Begin ignore epcaconfig as well as lilo cmd line */
2435 nbdevs = 0;
2436 num_cards = 0;
2437 return;
2438 } /* End ignore epcaconfig as well as lilo cmd line */
2439
2440 if (board.status > 2) {
2441 printk(KERN_ERR "epca_setup: Invalid board status 0x%x\n",
2442 board.status);
2443 invalid_lilo_config = 1;
2444 setup_error_code |= INVALID_BOARD_STATUS;
2445 return;
2446 }
2447 last = index;
2448 break;
2449 case 2:
2450 board.type = ints[index];
2451 if (board.type >= PCIXEM) {
2452 printk(KERN_ERR "epca_setup: Invalid board type 0x%x\n", board.type);
2453 invalid_lilo_config = 1;
2454 setup_error_code |= INVALID_BOARD_TYPE;
2455 return;
2456 }
2457 last = index;
2458 break;
2459 case 3:
2460 board.altpin = ints[index];
2461 if (board.altpin > 1) {
2462 printk(KERN_ERR "epca_setup: Invalid board altpin 0x%x\n", board.altpin);
2463 invalid_lilo_config = 1;
2464 setup_error_code |= INVALID_ALTPIN;
2465 return;
2466 }
2467 last = index;
2468 break;
2469
2470 case 4:
2471 board.numports = ints[index];
2472 if (board.numports < 2 || board.numports > 256) {
2473 printk(KERN_ERR "epca_setup: Invalid board numports 0x%x\n", board.numports);
2474 invalid_lilo_config = 1;
2475 setup_error_code |= INVALID_NUM_PORTS;
2476 return;
2477 }
2478 nbdevs += board.numports;
2479 last = index;
2480 break;
2481
2482 case 5:
2483 board.port = ints[index];
2484 if (ints[index] <= 0) {
2485 printk(KERN_ERR "epca_setup: Invalid io port 0x%x\n", (unsigned int)board.port);
2486 invalid_lilo_config = 1;
2487 setup_error_code |= INVALID_PORT_BASE;
2488 return;
2489 }
2490 last = index;
2491 break;
2492
2493 case 6:
2494 board.membase = ints[index];
2495 if (ints[index] <= 0) {
2496 printk(KERN_ERR "epca_setup: Invalid memory base 0x%x\n",
2497 (unsigned int)board.membase);
2498 invalid_lilo_config = 1;
2499 setup_error_code |= INVALID_MEM_BASE;
2500 return;
2501 }
2502 last = index;
2503 break;
2504
2505 default:
2506 printk(KERN_ERR "<Error> - epca_setup: Too many integer parms\n");
2507 return;
2508
2509 } /* End parse switch */
2510
2511 while (str && *str) { /* Begin while there is a string arg */
2512 /* find the next comma or terminator */
2513 temp = str;
2514 /* While string is not null, and a comma hasn't been found */
2515 while (*temp && (*temp != ','))
2516 temp++;
2517 if (!*temp)
2518 temp = NULL;
2519 else
2520 *temp++ = 0;
2521 /* Set index to the number of args + 1 */
2522 index = last + 1;
2523
2524 switch (index) {
2525 case 1:
2526 len = strlen(str);
2527 if (strncmp("Disable", str, len) == 0)
2528 board.status = 0;
2529 else if (strncmp("Enable", str, len) == 0)
2530 board.status = 1;
2531 else {
2532 printk(KERN_ERR "epca_setup: Invalid status %s\n", str);
2533 invalid_lilo_config = 1;
2534 setup_error_code |= INVALID_BOARD_STATUS;
2535 return;
2536 }
2537 last = index;
2538 break;
2539
2540 case 2:
2541 for (loop = 0; loop < EPCA_NUM_TYPES; loop++)
2542 if (strcmp(board_desc[loop], str) == 0)
2543 break;
2544 /*
2545 * If the index incremented above refers to a
2546 * legitamate board type set it here.
2547 */
2548 if (index < EPCA_NUM_TYPES)
2549 board.type = loop;
2550 else {
2551 printk(KERN_ERR "epca_setup: Invalid board type: %s\n", str);
2552 invalid_lilo_config = 1;
2553 setup_error_code |= INVALID_BOARD_TYPE;
2554 return;
2555 }
2556 last = index;
2557 break;
2558
2559 case 3:
2560 len = strlen(str);
2561 if (strncmp("Disable", str, len) == 0)
2562 board.altpin = 0;
2563 else if (strncmp("Enable", str, len) == 0)
2564 board.altpin = 1;
2565 else {
2566 printk(KERN_ERR "epca_setup: Invalid altpin %s\n", str);
2567 invalid_lilo_config = 1;
2568 setup_error_code |= INVALID_ALTPIN;
2569 return;
2570 }
2571 last = index;
2572 break;
2573
2574 case 4:
2575 t2 = str;
2576 while (isdigit(*t2))
2577 t2++;
2578
2579 if (*t2) {
2580 printk(KERN_ERR "epca_setup: Invalid port count %s\n", str);
2581 invalid_lilo_config = 1;
2582 setup_error_code |= INVALID_NUM_PORTS;
2583 return;
2584 }
2585
2586 /*
2587 * There is not a man page for simple_strtoul but the
2588 * code can be found in vsprintf.c. The first argument
2589 * is the string to translate (To an unsigned long
2590 * obviously), the second argument can be the address
2591 * of any character variable or a NULL. If a variable
2592 * is given, the end pointer of the string will be
2593 * stored in that variable; if a NULL is given the end
2594 * pointer will not be returned. The last argument is
2595 * the base to use. If a 0 is indicated, the routine
2596 * will attempt to determine the proper base by looking
2597 * at the values prefix (A '0' for octal, a 'x' for
2598 * hex, etc ... If a value is given it will use that
2599 * value as the base.
2600 */
2601 board.numports = simple_strtoul(str, NULL, 0);
2602 nbdevs += board.numports;
2603 last = index;
2604 break;
2605
2606 case 5:
2607 t2 = str;
2608 while (isxdigit(*t2))
2609 t2++;
2610
2611 if (*t2) {
2612 printk(KERN_ERR "epca_setup: Invalid i/o address %s\n", str);
2613 invalid_lilo_config = 1;
2614 setup_error_code |= INVALID_PORT_BASE;
2615 return;
2616 }
2617
2618 board.port = simple_strtoul(str, NULL, 16);
2619 last = index;
2620 break;
2621
2622 case 6:
2623 t2 = str;
2624 while (isxdigit(*t2))
2625 t2++;
2626
2627 if (*t2) {
2628 printk(KERN_ERR "epca_setup: Invalid memory base %s\n", str);
2629 invalid_lilo_config = 1;
2630 setup_error_code |= INVALID_MEM_BASE;
2631 return;
2632 }
2633 board.membase = simple_strtoul(str, NULL, 16);
2634 last = index;
2635 break;
2636 default:
2637 printk(KERN_ERR "epca: Too many string parms\n");
2638 return;
2639 }
2640 str = temp;
2641 } /* End while there is a string arg */
2642
2643 if (last < 6) {
2644 printk(KERN_ERR "epca: Insufficient parms specified\n");
2645 return;
2646 }
2647
2648 /* I should REALLY validate the stuff here */
2649 /* Copies our local copy of board into boards */
2650 memcpy((void *)&boards[num_cards], (void *)&board, sizeof(board));
2651 /* Does this get called once per lilo arg are what ? */
2652 printk(KERN_INFO "PC/Xx: Added board %i, %s %i ports at 0x%4.4X base 0x%6.6X\n",
2653 num_cards, board_desc[board.type],
2654 board.numports, (int)board.port, (unsigned int) board.membase);
2655 num_cards++;
2656}
2657
2658static int __init epca_real_setup(char *str)
2659{
2660 int ints[11];
2661
2662 epca_setup(get_options(str, 11, ints), ints);
2663 return 1;
2664}
2665
2666__setup("digiepca", epca_real_setup);
2667#endif
2668
2669enum epic_board_types {
2670 brd_xr = 0,
2671 brd_xem,
2672 brd_cx,
2673 brd_xrj,
2674};
2675
2676/* indexed directly by epic_board_types enum */
2677static struct {
2678 unsigned char board_type;
2679 unsigned bar_idx; /* PCI base address region */
2680} epca_info_tbl[] = {
2681 { PCIXR, 0, },
2682 { PCIXEM, 0, },
2683 { PCICX, 0, },
2684 { PCIXRJ, 2, },
2685};
2686
2687static int __devinit epca_init_one(struct pci_dev *pdev,
2688 const struct pci_device_id *ent)
2689{
2690 static int board_num = -1;
2691 int board_idx, info_idx = ent->driver_data;
2692 unsigned long addr;
2693
2694 if (pci_enable_device(pdev))
2695 return -EIO;
2696
2697 board_num++;
2698 board_idx = board_num + num_cards;
2699 if (board_idx >= MAXBOARDS)
2700 goto err_out;
2701
2702 addr = pci_resource_start(pdev, epca_info_tbl[info_idx].bar_idx);
2703 if (!addr) {
2704 printk(KERN_ERR PFX "PCI region #%d not available (size 0)\n",
2705 epca_info_tbl[info_idx].bar_idx);
2706 goto err_out;
2707 }
2708
2709 boards[board_idx].status = ENABLED;
2710 boards[board_idx].type = epca_info_tbl[info_idx].board_type;
2711 boards[board_idx].numports = 0x0;
2712 boards[board_idx].port = addr + PCI_IO_OFFSET;
2713 boards[board_idx].membase = addr;
2714
2715 if (!request_mem_region(addr + PCI_IO_OFFSET, 0x200000, "epca")) {
2716 printk(KERN_ERR PFX "resource 0x%x @ 0x%lx unavailable\n",
2717 0x200000, addr + PCI_IO_OFFSET);
2718 goto err_out;
2719 }
2720
2721 boards[board_idx].re_map_port = ioremap_nocache(addr + PCI_IO_OFFSET,
2722 0x200000);
2723 if (!boards[board_idx].re_map_port) {
2724 printk(KERN_ERR PFX "cannot map 0x%x @ 0x%lx\n",
2725 0x200000, addr + PCI_IO_OFFSET);
2726 goto err_out_free_pciio;
2727 }
2728
2729 if (!request_mem_region(addr, 0x200000, "epca")) {
2730 printk(KERN_ERR PFX "resource 0x%x @ 0x%lx unavailable\n",
2731 0x200000, addr);
2732 goto err_out_free_iounmap;
2733 }
2734
2735 boards[board_idx].re_map_membase = ioremap_nocache(addr, 0x200000);
2736 if (!boards[board_idx].re_map_membase) {
2737 printk(KERN_ERR PFX "cannot map 0x%x @ 0x%lx\n",
2738 0x200000, addr + PCI_IO_OFFSET);
2739 goto err_out_free_memregion;
2740 }
2741
2742 /*
2743 * I don't know what the below does, but the hardware guys say its
2744 * required on everything except PLX (In this case XRJ).
2745 */
2746 if (info_idx != brd_xrj) {
2747 pci_write_config_byte(pdev, 0x40, 0);
2748 pci_write_config_byte(pdev, 0x46, 0);
2749 }
2750
2751 return 0;
2752
2753err_out_free_memregion:
2754 release_mem_region(addr, 0x200000);
2755err_out_free_iounmap:
2756 iounmap(boards[board_idx].re_map_port);
2757err_out_free_pciio:
2758 release_mem_region(addr + PCI_IO_OFFSET, 0x200000);
2759err_out:
2760 return -ENODEV;
2761}
2762
2763
2764static struct pci_device_id epca_pci_tbl[] = {
2765 { PCI_VENDOR_DIGI, PCI_DEVICE_XR, PCI_ANY_ID, PCI_ANY_ID, 0, 0, brd_xr },
2766 { PCI_VENDOR_DIGI, PCI_DEVICE_XEM, PCI_ANY_ID, PCI_ANY_ID, 0, 0, brd_xem },
2767 { PCI_VENDOR_DIGI, PCI_DEVICE_CX, PCI_ANY_ID, PCI_ANY_ID, 0, 0, brd_cx },
2768 { PCI_VENDOR_DIGI, PCI_DEVICE_XRJ, PCI_ANY_ID, PCI_ANY_ID, 0, 0, brd_xrj },
2769 { 0, }
2770};
2771
2772MODULE_DEVICE_TABLE(pci, epca_pci_tbl);
2773
2774static int __init init_PCI(void)
2775{
2776 memset(&epca_driver, 0, sizeof(epca_driver));
2777 epca_driver.name = "epca";
2778 epca_driver.id_table = epca_pci_tbl;
2779 epca_driver.probe = epca_init_one;
2780
2781 return pci_register_driver(&epca_driver);
2782}
2783
2784MODULE_LICENSE("GPL");
diff --git a/drivers/char/epca.h b/drivers/char/epca.h
deleted file mode 100644
index d414bf2dbf7c..000000000000
--- a/drivers/char/epca.h
+++ /dev/null
@@ -1,158 +0,0 @@
1#define XEMPORTS 0xC02
2#define XEPORTS 0xC22
3
4#define MAX_ALLOC 0x100
5
6#define MAXBOARDS 12
7#define FEPCODESEG 0x0200L
8#define FEPCODE 0x2000L
9#define BIOSCODE 0xf800L
10
11#define MISCGLOBAL 0x0C00L
12#define NPORT 0x0C22L
13#define MBOX 0x0C40L
14#define PORTBASE 0x0C90L
15
16/* Begin code defines used for epca_setup */
17
18#define INVALID_BOARD_TYPE 0x1
19#define INVALID_NUM_PORTS 0x2
20#define INVALID_MEM_BASE 0x4
21#define INVALID_PORT_BASE 0x8
22#define INVALID_BOARD_STATUS 0x10
23#define INVALID_ALTPIN 0x20
24
25/* End code defines used for epca_setup */
26
27
28#define FEPCLR 0x00
29#define FEPMEM 0x02
30#define FEPRST 0x04
31#define FEPINT 0x08
32#define FEPMASK 0x0e
33#define FEPWIN 0x80
34
35#define PCXE 0
36#define PCXEVE 1
37#define PCXEM 2
38#define EISAXEM 3
39#define PC64XE 4
40#define PCXI 5
41#define PCIXEM 7
42#define PCICX 8
43#define PCIXR 9
44#define PCIXRJ 10
45#define EPCA_NUM_TYPES 6
46
47
48static char *board_desc[] =
49{
50 "PC/Xe",
51 "PC/Xeve",
52 "PC/Xem",
53 "EISA/Xem",
54 "PC/64Xe",
55 "PC/Xi",
56 "unknown",
57 "PCI/Xem",
58 "PCI/CX",
59 "PCI/Xr",
60 "PCI/Xrj",
61};
62
63#define STARTC 021
64#define STOPC 023
65#define IAIXON 0x2000
66
67
68#define TXSTOPPED 0x1
69#define LOWWAIT 0x2
70#define EMPTYWAIT 0x4
71#define RXSTOPPED 0x8
72#define TXBUSY 0x10
73
74#define DISABLED 0
75#define ENABLED 1
76#define OFF 0
77#define ON 1
78
79#define FEPTIMEOUT 200000
80#define SERIAL_TYPE_INFO 3
81#define EPCA_EVENT_HANGUP 1
82#define EPCA_MAGIC 0x5c6df104L
83
84struct channel
85{
86 long magic;
87 struct tty_port port;
88 unsigned char boardnum;
89 unsigned char channelnum;
90 unsigned char omodem; /* FEP output modem status */
91 unsigned char imodem; /* FEP input modem status */
92 unsigned char modemfake; /* Modem values to be forced */
93 unsigned char modem; /* Force values */
94 unsigned char hflow;
95 unsigned char dsr;
96 unsigned char dcd;
97 unsigned char m_rts ; /* The bits used in whatever FEP */
98 unsigned char m_dcd ; /* is indiginous to this board to */
99 unsigned char m_dsr ; /* represent each of the physical */
100 unsigned char m_cts ; /* handshake lines */
101 unsigned char m_ri ;
102 unsigned char m_dtr ;
103 unsigned char stopc;
104 unsigned char startc;
105 unsigned char stopca;
106 unsigned char startca;
107 unsigned char fepstopc;
108 unsigned char fepstartc;
109 unsigned char fepstopca;
110 unsigned char fepstartca;
111 unsigned char txwin;
112 unsigned char rxwin;
113 unsigned short fepiflag;
114 unsigned short fepcflag;
115 unsigned short fepoflag;
116 unsigned short txbufhead;
117 unsigned short txbufsize;
118 unsigned short rxbufhead;
119 unsigned short rxbufsize;
120 int close_delay;
121 unsigned long event;
122 uint dev;
123 unsigned long statusflags;
124 unsigned long c_iflag;
125 unsigned long c_cflag;
126 unsigned long c_lflag;
127 unsigned long c_oflag;
128 unsigned char __iomem *txptr;
129 unsigned char __iomem *rxptr;
130 struct board_info *board;
131 struct board_chan __iomem *brdchan;
132 struct digi_struct digiext;
133 struct work_struct tqueue;
134 struct global_data __iomem *mailbox;
135};
136
137struct board_info
138{
139 unsigned char status;
140 unsigned char type;
141 unsigned char altpin;
142 unsigned short numports;
143 unsigned long port;
144 unsigned long membase;
145 void __iomem *re_map_port;
146 void __iomem *re_map_membase;
147 unsigned long memory_seg;
148 void ( * memwinon ) (struct board_info *, unsigned int) ;
149 void ( * memwinoff ) (struct board_info *, unsigned int) ;
150 void ( * globalwinon ) (struct channel *) ;
151 void ( * txwinon ) (struct channel *) ;
152 void ( * rxwinon ) (struct channel *) ;
153 void ( * memoff ) (struct channel *) ;
154 void ( * assertgwinon ) (struct channel *) ;
155 void ( * assertmemoff ) (struct channel *) ;
156 unsigned char poller_inhibited ;
157};
158
diff --git a/drivers/char/epcaconfig.h b/drivers/char/epcaconfig.h
deleted file mode 100644
index 55dec067078e..000000000000
--- a/drivers/char/epcaconfig.h
+++ /dev/null
@@ -1,7 +0,0 @@
1#define NUMCARDS 0
2#define NBDEVS 0
3
4struct board_info static_boards[NUMCARDS]={
5};
6
7/* DO NOT HAND EDIT THIS FILE! */
diff --git a/drivers/char/generic_nvram.c b/drivers/char/generic_nvram.c
index 82b5a88a82d7..0e941b57482e 100644
--- a/drivers/char/generic_nvram.c
+++ b/drivers/char/generic_nvram.c
@@ -19,7 +19,7 @@
19#include <linux/miscdevice.h> 19#include <linux/miscdevice.h>
20#include <linux/fcntl.h> 20#include <linux/fcntl.h>
21#include <linux/init.h> 21#include <linux/init.h>
22#include <linux/smp_lock.h> 22#include <linux/mutex.h>
23#include <asm/uaccess.h> 23#include <asm/uaccess.h>
24#include <asm/nvram.h> 24#include <asm/nvram.h>
25#ifdef CONFIG_PPC_PMAC 25#ifdef CONFIG_PPC_PMAC
@@ -28,6 +28,7 @@
28 28
29#define NVRAM_SIZE 8192 29#define NVRAM_SIZE 8192
30 30
31static DEFINE_MUTEX(nvram_mutex);
31static ssize_t nvram_len; 32static ssize_t nvram_len;
32 33
33static loff_t nvram_llseek(struct file *file, loff_t offset, int origin) 34static loff_t nvram_llseek(struct file *file, loff_t offset, int origin)
@@ -120,9 +121,9 @@ static long nvram_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned l
120{ 121{
121 int ret; 122 int ret;
122 123
123 lock_kernel(); 124 mutex_lock(&nvram_mutex);
124 ret = nvram_ioctl(file, cmd, arg); 125 ret = nvram_ioctl(file, cmd, arg);
125 unlock_kernel(); 126 mutex_unlock(&nvram_mutex);
126 127
127 return ret; 128 return ret;
128} 129}
diff --git a/drivers/char/generic_serial.c b/drivers/char/generic_serial.c
deleted file mode 100644
index 5954ee1dc953..000000000000
--- a/drivers/char/generic_serial.c
+++ /dev/null
@@ -1,844 +0,0 @@
1/*
2 * generic_serial.c
3 *
4 * Copyright (C) 1998/1999 R.E.Wolff@BitWizard.nl
5 *
6 * written for the SX serial driver.
7 * Contains the code that should be shared over all the serial drivers.
8 *
9 * Credit for the idea to do it this way might go to Alan Cox.
10 *
11 *
12 * Version 0.1 -- December, 1998. Initial version.
13 * Version 0.2 -- March, 1999. Some more routines. Bugfixes. Etc.
14 * Version 0.5 -- August, 1999. Some more fixes. Reformat for Linus.
15 *
16 * BitWizard is actively maintaining this file. We sometimes find
17 * that someone submitted changes to this file. We really appreciate
18 * your help, but please submit changes through us. We're doing our
19 * best to be responsive. -- REW
20 * */
21
22#include <linux/module.h>
23#include <linux/kernel.h>
24#include <linux/tty.h>
25#include <linux/sched.h>
26#include <linux/serial.h>
27#include <linux/mm.h>
28#include <linux/generic_serial.h>
29#include <linux/interrupt.h>
30#include <linux/tty_flip.h>
31#include <linux/delay.h>
32#include <linux/gfp.h>
33#include <asm/uaccess.h>
34
35#define DEBUG
36
37static int gs_debug;
38
39#ifdef DEBUG
40#define gs_dprintk(f, str...) if (gs_debug & f) printk (str)
41#else
42#define gs_dprintk(f, str...) /* nothing */
43#endif
44
45#define func_enter() gs_dprintk (GS_DEBUG_FLOW, "gs: enter %s\n", __func__)
46#define func_exit() gs_dprintk (GS_DEBUG_FLOW, "gs: exit %s\n", __func__)
47
48#define RS_EVENT_WRITE_WAKEUP 1
49
50module_param(gs_debug, int, 0644);
51
52
53int gs_put_char(struct tty_struct * tty, unsigned char ch)
54{
55 struct gs_port *port;
56
57 func_enter ();
58
59 port = tty->driver_data;
60
61 if (!port) return 0;
62
63 if (! (port->port.flags & ASYNC_INITIALIZED)) return 0;
64
65 /* Take a lock on the serial tranmit buffer! */
66 mutex_lock(& port->port_write_mutex);
67
68 if (port->xmit_cnt >= SERIAL_XMIT_SIZE - 1) {
69 /* Sorry, buffer is full, drop character. Update statistics???? -- REW */
70 mutex_unlock(&port->port_write_mutex);
71 return 0;
72 }
73
74 port->xmit_buf[port->xmit_head++] = ch;
75 port->xmit_head &= SERIAL_XMIT_SIZE - 1;
76 port->xmit_cnt++; /* Characters in buffer */
77
78 mutex_unlock(&port->port_write_mutex);
79 func_exit ();
80 return 1;
81}
82
83
84/*
85> Problems to take into account are:
86> -1- Interrupts that empty part of the buffer.
87> -2- page faults on the access to userspace.
88> -3- Other processes that are also trying to do a "write".
89*/
90
91int gs_write(struct tty_struct * tty,
92 const unsigned char *buf, int count)
93{
94 struct gs_port *port;
95 int c, total = 0;
96 int t;
97
98 func_enter ();
99
100 port = tty->driver_data;
101
102 if (!port) return 0;
103
104 if (! (port->port.flags & ASYNC_INITIALIZED))
105 return 0;
106
107 /* get exclusive "write" access to this port (problem 3) */
108 /* This is not a spinlock because we can have a disk access (page
109 fault) in copy_from_user */
110 mutex_lock(& port->port_write_mutex);
111
112 while (1) {
113
114 c = count;
115
116 /* This is safe because we "OWN" the "head". Noone else can
117 change the "head": we own the port_write_mutex. */
118 /* Don't overrun the end of the buffer */
119 t = SERIAL_XMIT_SIZE - port->xmit_head;
120 if (t < c) c = t;
121
122 /* This is safe because the xmit_cnt can only decrease. This
123 would increase "t", so we might copy too little chars. */
124 /* Don't copy past the "head" of the buffer */
125 t = SERIAL_XMIT_SIZE - 1 - port->xmit_cnt;
126 if (t < c) c = t;
127
128 /* Can't copy more? break out! */
129 if (c <= 0) break;
130
131 memcpy (port->xmit_buf + port->xmit_head, buf, c);
132
133 port -> xmit_cnt += c;
134 port -> xmit_head = (port->xmit_head + c) & (SERIAL_XMIT_SIZE -1);
135 buf += c;
136 count -= c;
137 total += c;
138 }
139 mutex_unlock(& port->port_write_mutex);
140
141 gs_dprintk (GS_DEBUG_WRITE, "write: interrupts are %s\n",
142 (port->port.flags & GS_TX_INTEN)?"enabled": "disabled");
143
144 if (port->xmit_cnt &&
145 !tty->stopped &&
146 !tty->hw_stopped &&
147 !(port->port.flags & GS_TX_INTEN)) {
148 port->port.flags |= GS_TX_INTEN;
149 port->rd->enable_tx_interrupts (port);
150 }
151 func_exit ();
152 return total;
153}
154
155
156
157int gs_write_room(struct tty_struct * tty)
158{
159 struct gs_port *port = tty->driver_data;
160 int ret;
161
162 func_enter ();
163 ret = SERIAL_XMIT_SIZE - port->xmit_cnt - 1;
164 if (ret < 0)
165 ret = 0;
166 func_exit ();
167 return ret;
168}
169
170
171int gs_chars_in_buffer(struct tty_struct *tty)
172{
173 struct gs_port *port = tty->driver_data;
174 func_enter ();
175
176 func_exit ();
177 return port->xmit_cnt;
178}
179
180
181static int gs_real_chars_in_buffer(struct tty_struct *tty)
182{
183 struct gs_port *port;
184 func_enter ();
185
186 port = tty->driver_data;
187
188 if (!port->rd) return 0;
189 if (!port->rd->chars_in_buffer) return 0;
190
191 func_exit ();
192 return port->xmit_cnt + port->rd->chars_in_buffer (port);
193}
194
195
196static int gs_wait_tx_flushed (void * ptr, unsigned long timeout)
197{
198 struct gs_port *port = ptr;
199 unsigned long end_jiffies;
200 int jiffies_to_transmit, charsleft = 0, rv = 0;
201 int rcib;
202
203 func_enter();
204
205 gs_dprintk (GS_DEBUG_FLUSH, "port=%p.\n", port);
206 if (port) {
207 gs_dprintk (GS_DEBUG_FLUSH, "xmit_cnt=%x, xmit_buf=%p, tty=%p.\n",
208 port->xmit_cnt, port->xmit_buf, port->port.tty);
209 }
210
211 if (!port || port->xmit_cnt < 0 || !port->xmit_buf) {
212 gs_dprintk (GS_DEBUG_FLUSH, "ERROR: !port, !port->xmit_buf or prot->xmit_cnt < 0.\n");
213 func_exit();
214 return -EINVAL; /* This is an error which we don't know how to handle. */
215 }
216
217 rcib = gs_real_chars_in_buffer(port->port.tty);
218
219 if(rcib <= 0) {
220 gs_dprintk (GS_DEBUG_FLUSH, "nothing to wait for.\n");
221 func_exit();
222 return rv;
223 }
224 /* stop trying: now + twice the time it would normally take + seconds */
225 if (timeout == 0) timeout = MAX_SCHEDULE_TIMEOUT;
226 end_jiffies = jiffies;
227 if (timeout != MAX_SCHEDULE_TIMEOUT)
228 end_jiffies += port->baud?(2 * rcib * 10 * HZ / port->baud):0;
229 end_jiffies += timeout;
230
231 gs_dprintk (GS_DEBUG_FLUSH, "now=%lx, end=%lx (%ld).\n",
232 jiffies, end_jiffies, end_jiffies-jiffies);
233
234 /* the expression is actually jiffies < end_jiffies, but that won't
235 work around the wraparound. Tricky eh? */
236 while ((charsleft = gs_real_chars_in_buffer (port->port.tty)) &&
237 time_after (end_jiffies, jiffies)) {
238 /* Units check:
239 chars * (bits/char) * (jiffies /sec) / (bits/sec) = jiffies!
240 check! */
241
242 charsleft += 16; /* Allow 16 chars more to be transmitted ... */
243 jiffies_to_transmit = port->baud?(1 + charsleft * 10 * HZ / port->baud):0;
244 /* ^^^ Round up.... */
245 if (jiffies_to_transmit <= 0) jiffies_to_transmit = 1;
246
247 gs_dprintk (GS_DEBUG_FLUSH, "Expect to finish in %d jiffies "
248 "(%d chars).\n", jiffies_to_transmit, charsleft);
249
250 msleep_interruptible(jiffies_to_msecs(jiffies_to_transmit));
251 if (signal_pending (current)) {
252 gs_dprintk (GS_DEBUG_FLUSH, "Signal pending. Bombing out: ");
253 rv = -EINTR;
254 break;
255 }
256 }
257
258 gs_dprintk (GS_DEBUG_FLUSH, "charsleft = %d.\n", charsleft);
259 set_current_state (TASK_RUNNING);
260
261 func_exit();
262 return rv;
263}
264
265
266
267void gs_flush_buffer(struct tty_struct *tty)
268{
269 struct gs_port *port;
270 unsigned long flags;
271
272 func_enter ();
273
274 port = tty->driver_data;
275
276 if (!port) return;
277
278 /* XXX Would the write semaphore do? */
279 spin_lock_irqsave (&port->driver_lock, flags);
280 port->xmit_cnt = port->xmit_head = port->xmit_tail = 0;
281 spin_unlock_irqrestore (&port->driver_lock, flags);
282
283 tty_wakeup(tty);
284 func_exit ();
285}
286
287
288void gs_flush_chars(struct tty_struct * tty)
289{
290 struct gs_port *port;
291
292 func_enter ();
293
294 port = tty->driver_data;
295
296 if (!port) return;
297
298 if (port->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
299 !port->xmit_buf) {
300 func_exit ();
301 return;
302 }
303
304 /* Beats me -- REW */
305 port->port.flags |= GS_TX_INTEN;
306 port->rd->enable_tx_interrupts (port);
307 func_exit ();
308}
309
310
311void gs_stop(struct tty_struct * tty)
312{
313 struct gs_port *port;
314
315 func_enter ();
316
317 port = tty->driver_data;
318
319 if (!port) return;
320
321 if (port->xmit_cnt &&
322 port->xmit_buf &&
323 (port->port.flags & GS_TX_INTEN) ) {
324 port->port.flags &= ~GS_TX_INTEN;
325 port->rd->disable_tx_interrupts (port);
326 }
327 func_exit ();
328}
329
330
331void gs_start(struct tty_struct * tty)
332{
333 struct gs_port *port;
334
335 port = tty->driver_data;
336
337 if (!port) return;
338
339 if (port->xmit_cnt &&
340 port->xmit_buf &&
341 !(port->port.flags & GS_TX_INTEN) ) {
342 port->port.flags |= GS_TX_INTEN;
343 port->rd->enable_tx_interrupts (port);
344 }
345 func_exit ();
346}
347
348
349static void gs_shutdown_port (struct gs_port *port)
350{
351 unsigned long flags;
352
353 func_enter();
354
355 if (!port) return;
356
357 if (!(port->port.flags & ASYNC_INITIALIZED))
358 return;
359
360 spin_lock_irqsave(&port->driver_lock, flags);
361
362 if (port->xmit_buf) {
363 free_page((unsigned long) port->xmit_buf);
364 port->xmit_buf = NULL;
365 }
366
367 if (port->port.tty)
368 set_bit(TTY_IO_ERROR, &port->port.tty->flags);
369
370 port->rd->shutdown_port (port);
371
372 port->port.flags &= ~ASYNC_INITIALIZED;
373 spin_unlock_irqrestore(&port->driver_lock, flags);
374
375 func_exit();
376}
377
378
379void gs_hangup(struct tty_struct *tty)
380{
381 struct gs_port *port;
382 unsigned long flags;
383
384 func_enter ();
385
386 port = tty->driver_data;
387 tty = port->port.tty;
388 if (!tty)
389 return;
390
391 gs_shutdown_port (port);
392 spin_lock_irqsave(&port->port.lock, flags);
393 port->port.flags &= ~(ASYNC_NORMAL_ACTIVE|GS_ACTIVE);
394 port->port.tty = NULL;
395 port->port.count = 0;
396 spin_unlock_irqrestore(&port->port.lock, flags);
397
398 wake_up_interruptible(&port->port.open_wait);
399 func_exit ();
400}
401
402
403int gs_block_til_ready(void *port_, struct file * filp)
404{
405 struct gs_port *gp = port_;
406 struct tty_port *port = &gp->port;
407 DECLARE_WAITQUEUE(wait, current);
408 int retval;
409 int do_clocal = 0;
410 int CD;
411 struct tty_struct *tty;
412 unsigned long flags;
413
414 func_enter ();
415
416 if (!port) return 0;
417
418 tty = port->tty;
419
420 gs_dprintk (GS_DEBUG_BTR, "Entering gs_block_till_ready.\n");
421 /*
422 * If the device is in the middle of being closed, then block
423 * until it's done, and then try again.
424 */
425 if (tty_hung_up_p(filp) || port->flags & ASYNC_CLOSING) {
426 interruptible_sleep_on(&port->close_wait);
427 if (port->flags & ASYNC_HUP_NOTIFY)
428 return -EAGAIN;
429 else
430 return -ERESTARTSYS;
431 }
432
433 gs_dprintk (GS_DEBUG_BTR, "after hung up\n");
434
435 /*
436 * If non-blocking mode is set, or the port is not enabled,
437 * then make the check up front and then exit.
438 */
439 if ((filp->f_flags & O_NONBLOCK) ||
440 (tty->flags & (1 << TTY_IO_ERROR))) {
441 port->flags |= ASYNC_NORMAL_ACTIVE;
442 return 0;
443 }
444
445 gs_dprintk (GS_DEBUG_BTR, "after nonblock\n");
446
447 if (C_CLOCAL(tty))
448 do_clocal = 1;
449
450 /*
451 * Block waiting for the carrier detect and the line to become
452 * free (i.e., not in use by the callout). While we are in
453 * this loop, port->count is dropped by one, so that
454 * rs_close() knows when to free things. We restore it upon
455 * exit, either normal or abnormal.
456 */
457 retval = 0;
458
459 add_wait_queue(&port->open_wait, &wait);
460
461 gs_dprintk (GS_DEBUG_BTR, "after add waitq.\n");
462 spin_lock_irqsave(&port->lock, flags);
463 if (!tty_hung_up_p(filp)) {
464 port->count--;
465 }
466 port->blocked_open++;
467 spin_unlock_irqrestore(&port->lock, flags);
468 while (1) {
469 CD = tty_port_carrier_raised(port);
470 gs_dprintk (GS_DEBUG_BTR, "CD is now %d.\n", CD);
471 set_current_state (TASK_INTERRUPTIBLE);
472 if (tty_hung_up_p(filp) ||
473 !(port->flags & ASYNC_INITIALIZED)) {
474 if (port->flags & ASYNC_HUP_NOTIFY)
475 retval = -EAGAIN;
476 else
477 retval = -ERESTARTSYS;
478 break;
479 }
480 if (!(port->flags & ASYNC_CLOSING) &&
481 (do_clocal || CD))
482 break;
483 gs_dprintk (GS_DEBUG_BTR, "signal_pending is now: %d (%lx)\n",
484 (int)signal_pending (current), *(long*)(&current->blocked));
485 if (signal_pending(current)) {
486 retval = -ERESTARTSYS;
487 break;
488 }
489 schedule();
490 }
491 gs_dprintk (GS_DEBUG_BTR, "Got out of the loop. (%d)\n",
492 port->blocked_open);
493 set_current_state (TASK_RUNNING);
494 remove_wait_queue(&port->open_wait, &wait);
495
496 spin_lock_irqsave(&port->lock, flags);
497 if (!tty_hung_up_p(filp)) {
498 port->count++;
499 }
500 port->blocked_open--;
501 if (retval == 0)
502 port->flags |= ASYNC_NORMAL_ACTIVE;
503 spin_unlock_irqrestore(&port->lock, flags);
504 func_exit ();
505 return retval;
506}
507
508
509void gs_close(struct tty_struct * tty, struct file * filp)
510{
511 unsigned long flags;
512 struct gs_port *port;
513
514 func_enter ();
515
516 port = tty->driver_data;
517
518 if (!port) return;
519
520 if (!port->port.tty) {
521 /* This seems to happen when this is called from vhangup. */
522 gs_dprintk (GS_DEBUG_CLOSE, "gs: Odd: port->port.tty is NULL\n");
523 port->port.tty = tty;
524 }
525
526 spin_lock_irqsave(&port->port.lock, flags);
527
528 if (tty_hung_up_p(filp)) {
529 spin_unlock_irqrestore(&port->port.lock, flags);
530 if (port->rd->hungup)
531 port->rd->hungup (port);
532 func_exit ();
533 return;
534 }
535
536 if ((tty->count == 1) && (port->port.count != 1)) {
537 printk(KERN_ERR "gs: gs_close port %p: bad port count;"
538 " tty->count is 1, port count is %d\n", port, port->port.count);
539 port->port.count = 1;
540 }
541 if (--port->port.count < 0) {
542 printk(KERN_ERR "gs: gs_close port %p: bad port count: %d\n", port, port->port.count);
543 port->port.count = 0;
544 }
545
546 if (port->port.count) {
547 gs_dprintk(GS_DEBUG_CLOSE, "gs_close port %p: count: %d\n", port, port->port.count);
548 spin_unlock_irqrestore(&port->port.lock, flags);
549 func_exit ();
550 return;
551 }
552 port->port.flags |= ASYNC_CLOSING;
553
554 /*
555 * Now we wait for the transmit buffer to clear; and we notify
556 * the line discipline to only process XON/XOFF characters.
557 */
558 tty->closing = 1;
559 /* if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
560 tty_wait_until_sent(tty, port->closing_wait); */
561
562 /*
563 * At this point we stop accepting input. To do this, we
564 * disable the receive line status interrupts, and tell the
565 * interrupt driver to stop checking the data ready bit in the
566 * line status register.
567 */
568
569 spin_lock_irqsave(&port->driver_lock, flags);
570 port->rd->disable_rx_interrupts (port);
571 spin_unlock_irqrestore(&port->driver_lock, flags);
572 spin_unlock_irqrestore(&port->port.lock, flags);
573
574 /* close has no way of returning "EINTR", so discard return value */
575 if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
576 gs_wait_tx_flushed (port, port->closing_wait);
577
578 port->port.flags &= ~GS_ACTIVE;
579
580 gs_flush_buffer(tty);
581
582 tty_ldisc_flush(tty);
583 tty->closing = 0;
584
585 spin_lock_irqsave(&port->driver_lock, flags);
586 port->event = 0;
587 port->rd->close (port);
588 port->rd->shutdown_port (port);
589 spin_unlock_irqrestore(&port->driver_lock, flags);
590
591 spin_lock_irqsave(&port->port.lock, flags);
592 port->port.tty = NULL;
593
594 if (port->port.blocked_open) {
595 if (port->close_delay) {
596 spin_unlock_irqrestore(&port->port.lock, flags);
597 msleep_interruptible(jiffies_to_msecs(port->close_delay));
598 spin_lock_irqsave(&port->port.lock, flags);
599 }
600 wake_up_interruptible(&port->port.open_wait);
601 }
602 port->port.flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING | ASYNC_INITIALIZED);
603 spin_unlock_irqrestore(&port->port.lock, flags);
604 wake_up_interruptible(&port->port.close_wait);
605
606 func_exit ();
607}
608
609
610void gs_set_termios (struct tty_struct * tty,
611 struct ktermios * old_termios)
612{
613 struct gs_port *port;
614 int baudrate, tmp, rv;
615 struct ktermios *tiosp;
616
617 func_enter();
618
619 port = tty->driver_data;
620
621 if (!port) return;
622 if (!port->port.tty) {
623 /* This seems to happen when this is called after gs_close. */
624 gs_dprintk (GS_DEBUG_TERMIOS, "gs: Odd: port->port.tty is NULL\n");
625 port->port.tty = tty;
626 }
627
628
629 tiosp = tty->termios;
630
631 if (gs_debug & GS_DEBUG_TERMIOS) {
632 gs_dprintk (GS_DEBUG_TERMIOS, "termios structure (%p):\n", tiosp);
633 }
634
635 if(old_termios && (gs_debug & GS_DEBUG_TERMIOS)) {
636 if(tiosp->c_iflag != old_termios->c_iflag) printk("c_iflag changed\n");
637 if(tiosp->c_oflag != old_termios->c_oflag) printk("c_oflag changed\n");
638 if(tiosp->c_cflag != old_termios->c_cflag) printk("c_cflag changed\n");
639 if(tiosp->c_lflag != old_termios->c_lflag) printk("c_lflag changed\n");
640 if(tiosp->c_line != old_termios->c_line) printk("c_line changed\n");
641 if(!memcmp(tiosp->c_cc, old_termios->c_cc, NCC)) printk("c_cc changed\n");
642 }
643
644 baudrate = tty_get_baud_rate(tty);
645
646 if ((tiosp->c_cflag & CBAUD) == B38400) {
647 if ( (port->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
648 baudrate = 57600;
649 else if ((port->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
650 baudrate = 115200;
651 else if ((port->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
652 baudrate = 230400;
653 else if ((port->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
654 baudrate = 460800;
655 else if ((port->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST)
656 baudrate = (port->baud_base / port->custom_divisor);
657 }
658
659 /* I recommend using THIS instead of the mess in termios (and
660 duplicating the above code). Next we should create a clean
661 interface towards this variable. If your card supports arbitrary
662 baud rates, (e.g. CD1400 or 16550 based cards) then everything
663 will be very easy..... */
664 port->baud = baudrate;
665
666 /* Two timer ticks seems enough to wakeup something like SLIP driver */
667 /* Baudrate/10 is cps. Divide by HZ to get chars per tick. */
668 tmp = (baudrate / 10 / HZ) * 2;
669
670 if (tmp < 0) tmp = 0;
671 if (tmp >= SERIAL_XMIT_SIZE) tmp = SERIAL_XMIT_SIZE-1;
672
673 port->wakeup_chars = tmp;
674
675 /* We should really wait for the characters to be all sent before
676 changing the settings. -- CAL */
677 rv = gs_wait_tx_flushed (port, MAX_SCHEDULE_TIMEOUT);
678 if (rv < 0) return /* rv */;
679
680 rv = port->rd->set_real_termios(port);
681 if (rv < 0) return /* rv */;
682
683 if ((!old_termios ||
684 (old_termios->c_cflag & CRTSCTS)) &&
685 !( tiosp->c_cflag & CRTSCTS)) {
686 tty->stopped = 0;
687 gs_start(tty);
688 }
689
690#ifdef tytso_patch_94Nov25_1726
691 /* This "makes sense", Why is it commented out? */
692
693 if (!(old_termios->c_cflag & CLOCAL) &&
694 (tty->termios->c_cflag & CLOCAL))
695 wake_up_interruptible(&port->gs.open_wait);
696#endif
697
698 func_exit();
699 return /* 0 */;
700}
701
702
703
704/* Must be called with interrupts enabled */
705int gs_init_port(struct gs_port *port)
706{
707 unsigned long flags;
708
709 func_enter ();
710
711 if (port->port.flags & ASYNC_INITIALIZED) {
712 func_exit ();
713 return 0;
714 }
715 if (!port->xmit_buf) {
716 /* We may sleep in get_zeroed_page() */
717 unsigned long tmp;
718
719 tmp = get_zeroed_page(GFP_KERNEL);
720 spin_lock_irqsave (&port->driver_lock, flags);
721 if (port->xmit_buf)
722 free_page (tmp);
723 else
724 port->xmit_buf = (unsigned char *) tmp;
725 spin_unlock_irqrestore(&port->driver_lock, flags);
726 if (!port->xmit_buf) {
727 func_exit ();
728 return -ENOMEM;
729 }
730 }
731
732 spin_lock_irqsave (&port->driver_lock, flags);
733 if (port->port.tty)
734 clear_bit(TTY_IO_ERROR, &port->port.tty->flags);
735 mutex_init(&port->port_write_mutex);
736 port->xmit_cnt = port->xmit_head = port->xmit_tail = 0;
737 spin_unlock_irqrestore(&port->driver_lock, flags);
738 gs_set_termios(port->port.tty, NULL);
739 spin_lock_irqsave (&port->driver_lock, flags);
740 port->port.flags |= ASYNC_INITIALIZED;
741 port->port.flags &= ~GS_TX_INTEN;
742
743 spin_unlock_irqrestore(&port->driver_lock, flags);
744 func_exit ();
745 return 0;
746}
747
748
749int gs_setserial(struct gs_port *port, struct serial_struct __user *sp)
750{
751 struct serial_struct sio;
752
753 if (copy_from_user(&sio, sp, sizeof(struct serial_struct)))
754 return(-EFAULT);
755
756 if (!capable(CAP_SYS_ADMIN)) {
757 if ((sio.baud_base != port->baud_base) ||
758 (sio.close_delay != port->close_delay) ||
759 ((sio.flags & ~ASYNC_USR_MASK) !=
760 (port->port.flags & ~ASYNC_USR_MASK)))
761 return(-EPERM);
762 }
763
764 port->port.flags = (port->port.flags & ~ASYNC_USR_MASK) |
765 (sio.flags & ASYNC_USR_MASK);
766
767 port->baud_base = sio.baud_base;
768 port->close_delay = sio.close_delay;
769 port->closing_wait = sio.closing_wait;
770 port->custom_divisor = sio.custom_divisor;
771
772 gs_set_termios (port->port.tty, NULL);
773
774 return 0;
775}
776
777
778/*****************************************************************************/
779
780/*
781 * Generate the serial struct info.
782 */
783
784int gs_getserial(struct gs_port *port, struct serial_struct __user *sp)
785{
786 struct serial_struct sio;
787
788 memset(&sio, 0, sizeof(struct serial_struct));
789 sio.flags = port->port.flags;
790 sio.baud_base = port->baud_base;
791 sio.close_delay = port->close_delay;
792 sio.closing_wait = port->closing_wait;
793 sio.custom_divisor = port->custom_divisor;
794 sio.hub6 = 0;
795
796 /* If you want you can override these. */
797 sio.type = PORT_UNKNOWN;
798 sio.xmit_fifo_size = -1;
799 sio.line = -1;
800 sio.port = -1;
801 sio.irq = -1;
802
803 if (port->rd->getserial)
804 port->rd->getserial (port, &sio);
805
806 if (copy_to_user(sp, &sio, sizeof(struct serial_struct)))
807 return -EFAULT;
808 return 0;
809
810}
811
812
813void gs_got_break(struct gs_port *port)
814{
815 func_enter ();
816
817 tty_insert_flip_char(port->port.tty, 0, TTY_BREAK);
818 tty_schedule_flip(port->port.tty);
819 if (port->port.flags & ASYNC_SAK) {
820 do_SAK (port->port.tty);
821 }
822
823 func_exit ();
824}
825
826
827EXPORT_SYMBOL(gs_put_char);
828EXPORT_SYMBOL(gs_write);
829EXPORT_SYMBOL(gs_write_room);
830EXPORT_SYMBOL(gs_chars_in_buffer);
831EXPORT_SYMBOL(gs_flush_buffer);
832EXPORT_SYMBOL(gs_flush_chars);
833EXPORT_SYMBOL(gs_stop);
834EXPORT_SYMBOL(gs_start);
835EXPORT_SYMBOL(gs_hangup);
836EXPORT_SYMBOL(gs_block_til_ready);
837EXPORT_SYMBOL(gs_close);
838EXPORT_SYMBOL(gs_set_termios);
839EXPORT_SYMBOL(gs_init_port);
840EXPORT_SYMBOL(gs_setserial);
841EXPORT_SYMBOL(gs_getserial);
842EXPORT_SYMBOL(gs_got_break);
843
844MODULE_LICENSE("GPL");
diff --git a/drivers/char/genrtc.c b/drivers/char/genrtc.c
index b6c2cc167c11..f773a9dd14f3 100644
--- a/drivers/char/genrtc.c
+++ b/drivers/char/genrtc.c
@@ -52,7 +52,7 @@
52#include <linux/init.h> 52#include <linux/init.h>
53#include <linux/poll.h> 53#include <linux/poll.h>
54#include <linux/proc_fs.h> 54#include <linux/proc_fs.h>
55#include <linux/smp_lock.h> 55#include <linux/mutex.h>
56#include <linux/workqueue.h> 56#include <linux/workqueue.h>
57 57
58#include <asm/uaccess.h> 58#include <asm/uaccess.h>
@@ -66,6 +66,7 @@
66 * ioctls. 66 * ioctls.
67 */ 67 */
68 68
69static DEFINE_MUTEX(gen_rtc_mutex);
69static DECLARE_WAIT_QUEUE_HEAD(gen_rtc_wait); 70static DECLARE_WAIT_QUEUE_HEAD(gen_rtc_wait);
70 71
71/* 72/*
@@ -337,9 +338,9 @@ static long gen_rtc_unlocked_ioctl(struct file *file, unsigned int cmd,
337{ 338{
338 int ret; 339 int ret;
339 340
340 lock_kernel(); 341 mutex_lock(&gen_rtc_mutex);
341 ret = gen_rtc_ioctl(file, cmd, arg); 342 ret = gen_rtc_ioctl(file, cmd, arg);
342 unlock_kernel(); 343 mutex_unlock(&gen_rtc_mutex);
343 344
344 return ret; 345 return ret;
345} 346}
@@ -352,16 +353,16 @@ static long gen_rtc_unlocked_ioctl(struct file *file, unsigned int cmd,
352 353
353static int gen_rtc_open(struct inode *inode, struct file *file) 354static int gen_rtc_open(struct inode *inode, struct file *file)
354{ 355{
355 lock_kernel(); 356 mutex_lock(&gen_rtc_mutex);
356 if (gen_rtc_status & RTC_IS_OPEN) { 357 if (gen_rtc_status & RTC_IS_OPEN) {
357 unlock_kernel(); 358 mutex_unlock(&gen_rtc_mutex);
358 return -EBUSY; 359 return -EBUSY;
359 } 360 }
360 361
361 gen_rtc_status |= RTC_IS_OPEN; 362 gen_rtc_status |= RTC_IS_OPEN;
362 gen_rtc_irq_data = 0; 363 gen_rtc_irq_data = 0;
363 irq_active = 0; 364 irq_active = 0;
364 unlock_kernel(); 365 mutex_unlock(&gen_rtc_mutex);
365 366
366 return 0; 367 return 0;
367} 368}
@@ -497,6 +498,7 @@ static const struct file_operations gen_rtc_fops = {
497 .unlocked_ioctl = gen_rtc_unlocked_ioctl, 498 .unlocked_ioctl = gen_rtc_unlocked_ioctl,
498 .open = gen_rtc_open, 499 .open = gen_rtc_open,
499 .release = gen_rtc_release, 500 .release = gen_rtc_release,
501 .llseek = noop_llseek,
500}; 502};
501 503
502static struct miscdevice rtc_gen_dev = 504static struct miscdevice rtc_gen_dev =
diff --git a/drivers/char/hpet.c b/drivers/char/hpet.c
index a0a1829d3198..34d6a1cab8de 100644
--- a/drivers/char/hpet.c
+++ b/drivers/char/hpet.c
@@ -14,7 +14,6 @@
14#include <linux/interrupt.h> 14#include <linux/interrupt.h>
15#include <linux/module.h> 15#include <linux/module.h>
16#include <linux/kernel.h> 16#include <linux/kernel.h>
17#include <linux/smp_lock.h>
18#include <linux/types.h> 17#include <linux/types.h>
19#include <linux/miscdevice.h> 18#include <linux/miscdevice.h>
20#include <linux/major.h> 19#include <linux/major.h>
@@ -30,13 +29,14 @@
30#include <linux/bcd.h> 29#include <linux/bcd.h>
31#include <linux/seq_file.h> 30#include <linux/seq_file.h>
32#include <linux/bitops.h> 31#include <linux/bitops.h>
32#include <linux/compat.h>
33#include <linux/clocksource.h> 33#include <linux/clocksource.h>
34#include <linux/uaccess.h>
34#include <linux/slab.h> 35#include <linux/slab.h>
36#include <linux/io.h>
35 37
36#include <asm/current.h> 38#include <asm/current.h>
37#include <asm/uaccess.h>
38#include <asm/system.h> 39#include <asm/system.h>
39#include <asm/io.h>
40#include <asm/irq.h> 40#include <asm/irq.h>
41#include <asm/div64.h> 41#include <asm/div64.h>
42 42
@@ -67,6 +67,7 @@
67#define read_counter(MC) readl(MC) 67#define read_counter(MC) readl(MC)
68#endif 68#endif
69 69
70static DEFINE_MUTEX(hpet_mutex); /* replaces BKL */
70static u32 hpet_nhpet, hpet_max_freq = HPET_USER_FREQ; 71static u32 hpet_nhpet, hpet_max_freq = HPET_USER_FREQ;
71 72
72/* This clocksource driver currently only works on ia64 */ 73/* This clocksource driver currently only works on ia64 */
@@ -79,13 +80,11 @@ static cycle_t read_hpet(struct clocksource *cs)
79} 80}
80 81
81static struct clocksource clocksource_hpet = { 82static struct clocksource clocksource_hpet = {
82 .name = "hpet", 83 .name = "hpet",
83 .rating = 250, 84 .rating = 250,
84 .read = read_hpet, 85 .read = read_hpet,
85 .mask = CLOCKSOURCE_MASK(64), 86 .mask = CLOCKSOURCE_MASK(64),
86 .mult = 0, /* to be calculated */ 87 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
87 .shift = 10,
88 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
89}; 88};
90static struct clocksource *hpet_clocksource; 89static struct clocksource *hpet_clocksource;
91#endif 90#endif
@@ -164,11 +163,32 @@ static irqreturn_t hpet_interrupt(int irq, void *data)
164 * This has the effect of treating non-periodic like periodic. 163 * This has the effect of treating non-periodic like periodic.
165 */ 164 */
166 if ((devp->hd_flags & (HPET_IE | HPET_PERIODIC)) == HPET_IE) { 165 if ((devp->hd_flags & (HPET_IE | HPET_PERIODIC)) == HPET_IE) {
167 unsigned long m, t; 166 unsigned long m, t, mc, base, k;
167 struct hpet __iomem *hpet = devp->hd_hpet;
168 struct hpets *hpetp = devp->hd_hpets;
168 169
169 t = devp->hd_ireqfreq; 170 t = devp->hd_ireqfreq;
170 m = read_counter(&devp->hd_timer->hpet_compare); 171 m = read_counter(&devp->hd_timer->hpet_compare);
171 write_counter(t + m, &devp->hd_timer->hpet_compare); 172 mc = read_counter(&hpet->hpet_mc);
173 /* The time for the next interrupt would logically be t + m,
174 * however, if we are very unlucky and the interrupt is delayed
175 * for longer than t then we will completely miss the next
176 * interrupt if we set t + m and an application will hang.
177 * Therefore we need to make a more complex computation assuming
178 * that there exists a k for which the following is true:
179 * k * t + base < mc + delta
180 * (k + 1) * t + base > mc + delta
181 * where t is the interval in hpet ticks for the given freq,
182 * base is the theoretical start value 0 < base < t,
183 * mc is the main counter value at the time of the interrupt,
184 * delta is the time it takes to write the a value to the
185 * comparator.
186 * k may then be computed as (mc - base + delta) / t .
187 */
188 base = mc % t;
189 k = (mc - base + hpetp->hp_delta) / t;
190 write_counter(t * (k + 1) + base,
191 &devp->hd_timer->hpet_compare);
172 } 192 }
173 193
174 if (devp->hd_flags & HPET_SHARED_IRQ) 194 if (devp->hd_flags & HPET_SHARED_IRQ)
@@ -250,7 +270,7 @@ static int hpet_open(struct inode *inode, struct file *file)
250 if (file->f_mode & FMODE_WRITE) 270 if (file->f_mode & FMODE_WRITE)
251 return -EINVAL; 271 return -EINVAL;
252 272
253 lock_kernel(); 273 mutex_lock(&hpet_mutex);
254 spin_lock_irq(&hpet_lock); 274 spin_lock_irq(&hpet_lock);
255 275
256 for (devp = NULL, hpetp = hpets; hpetp && !devp; hpetp = hpetp->hp_next) 276 for (devp = NULL, hpetp = hpets; hpetp && !devp; hpetp = hpetp->hp_next)
@@ -264,7 +284,7 @@ static int hpet_open(struct inode *inode, struct file *file)
264 284
265 if (!devp) { 285 if (!devp) {
266 spin_unlock_irq(&hpet_lock); 286 spin_unlock_irq(&hpet_lock);
267 unlock_kernel(); 287 mutex_unlock(&hpet_mutex);
268 return -EBUSY; 288 return -EBUSY;
269 } 289 }
270 290
@@ -272,7 +292,7 @@ static int hpet_open(struct inode *inode, struct file *file)
272 devp->hd_irqdata = 0; 292 devp->hd_irqdata = 0;
273 devp->hd_flags |= HPET_OPEN; 293 devp->hd_flags |= HPET_OPEN;
274 spin_unlock_irq(&hpet_lock); 294 spin_unlock_irq(&hpet_lock);
275 unlock_kernel(); 295 mutex_unlock(&hpet_mutex);
276 296
277 hpet_timer_set_irq(devp); 297 hpet_timer_set_irq(devp);
278 298
@@ -429,22 +449,6 @@ static int hpet_release(struct inode *inode, struct file *file)
429 return 0; 449 return 0;
430} 450}
431 451
432static int hpet_ioctl_common(struct hpet_dev *, int, unsigned long, int);
433
434static long hpet_ioctl(struct file *file, unsigned int cmd,
435 unsigned long arg)
436{
437 struct hpet_dev *devp;
438 int ret;
439
440 devp = file->private_data;
441 lock_kernel();
442 ret = hpet_ioctl_common(devp, cmd, arg, 0);
443 unlock_kernel();
444
445 return ret;
446}
447
448static int hpet_ioctl_ieon(struct hpet_dev *devp) 452static int hpet_ioctl_ieon(struct hpet_dev *devp)
449{ 453{
450 struct hpet_timer __iomem *timer; 454 struct hpet_timer __iomem *timer;
@@ -479,6 +483,21 @@ static int hpet_ioctl_ieon(struct hpet_dev *devp)
479 if (irq) { 483 if (irq) {
480 unsigned long irq_flags; 484 unsigned long irq_flags;
481 485
486 if (devp->hd_flags & HPET_SHARED_IRQ) {
487 /*
488 * To prevent the interrupt handler from seeing an
489 * unwanted interrupt status bit, program the timer
490 * so that it will not fire in the near future ...
491 */
492 writel(readl(&timer->hpet_config) & ~Tn_TYPE_CNF_MASK,
493 &timer->hpet_config);
494 write_counter(read_counter(&hpet->hpet_mc),
495 &timer->hpet_compare);
496 /* ... and clear any left-over status. */
497 isr = 1 << (devp - devp->hd_hpets->hp_dev);
498 writel(isr, &hpet->hpet_isr);
499 }
500
482 sprintf(devp->hd_name, "hpet%d", (int)(devp - hpetp->hp_dev)); 501 sprintf(devp->hd_name, "hpet%d", (int)(devp - hpetp->hp_dev));
483 irq_flags = devp->hd_flags & HPET_SHARED_IRQ 502 irq_flags = devp->hd_flags & HPET_SHARED_IRQ
484 ? IRQF_SHARED : IRQF_DISABLED; 503 ? IRQF_SHARED : IRQF_DISABLED;
@@ -553,7 +572,8 @@ static inline unsigned long hpet_time_div(struct hpets *hpets,
553} 572}
554 573
555static int 574static int
556hpet_ioctl_common(struct hpet_dev *devp, int cmd, unsigned long arg, int kernel) 575hpet_ioctl_common(struct hpet_dev *devp, int cmd, unsigned long arg,
576 struct hpet_info *info)
557{ 577{
558 struct hpet_timer __iomem *timer; 578 struct hpet_timer __iomem *timer;
559 struct hpet __iomem *hpet; 579 struct hpet __iomem *hpet;
@@ -594,23 +614,14 @@ hpet_ioctl_common(struct hpet_dev *devp, int cmd, unsigned long arg, int kernel)
594 break; 614 break;
595 case HPET_INFO: 615 case HPET_INFO:
596 { 616 {
597 struct hpet_info info; 617 memset(info, 0, sizeof(*info));
598
599 if (devp->hd_ireqfreq) 618 if (devp->hd_ireqfreq)
600 info.hi_ireqfreq = 619 info->hi_ireqfreq =
601 hpet_time_div(hpetp, devp->hd_ireqfreq); 620 hpet_time_div(hpetp, devp->hd_ireqfreq);
602 else 621 info->hi_flags =
603 info.hi_ireqfreq = 0;
604 info.hi_flags =
605 readq(&timer->hpet_config) & Tn_PER_INT_CAP_MASK; 622 readq(&timer->hpet_config) & Tn_PER_INT_CAP_MASK;
606 info.hi_hpet = hpetp->hp_which; 623 info->hi_hpet = hpetp->hp_which;
607 info.hi_timer = devp - hpetp->hp_dev; 624 info->hi_timer = devp - hpetp->hp_dev;
608 if (kernel)
609 memcpy((void *)arg, &info, sizeof(info));
610 else
611 if (copy_to_user((void __user *)arg, &info,
612 sizeof(info)))
613 err = -EFAULT;
614 break; 625 break;
615 } 626 }
616 case HPET_EPI: 627 case HPET_EPI:
@@ -636,7 +647,7 @@ hpet_ioctl_common(struct hpet_dev *devp, int cmd, unsigned long arg, int kernel)
636 devp->hd_flags &= ~HPET_PERIODIC; 647 devp->hd_flags &= ~HPET_PERIODIC;
637 break; 648 break;
638 case HPET_IRQFREQ: 649 case HPET_IRQFREQ:
639 if (!kernel && (arg > hpet_max_freq) && 650 if ((arg > hpet_max_freq) &&
640 !capable(CAP_SYS_RESOURCE)) { 651 !capable(CAP_SYS_RESOURCE)) {
641 err = -EACCES; 652 err = -EACCES;
642 break; 653 break;
@@ -653,12 +664,63 @@ hpet_ioctl_common(struct hpet_dev *devp, int cmd, unsigned long arg, int kernel)
653 return err; 664 return err;
654} 665}
655 666
667static long
668hpet_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
669{
670 struct hpet_info info;
671 int err;
672
673 mutex_lock(&hpet_mutex);
674 err = hpet_ioctl_common(file->private_data, cmd, arg, &info);
675 mutex_unlock(&hpet_mutex);
676
677 if ((cmd == HPET_INFO) && !err &&
678 (copy_to_user((void __user *)arg, &info, sizeof(info))))
679 err = -EFAULT;
680
681 return err;
682}
683
684#ifdef CONFIG_COMPAT
685struct compat_hpet_info {
686 compat_ulong_t hi_ireqfreq; /* Hz */
687 compat_ulong_t hi_flags; /* information */
688 unsigned short hi_hpet;
689 unsigned short hi_timer;
690};
691
692static long
693hpet_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
694{
695 struct hpet_info info;
696 int err;
697
698 mutex_lock(&hpet_mutex);
699 err = hpet_ioctl_common(file->private_data, cmd, arg, &info);
700 mutex_unlock(&hpet_mutex);
701
702 if ((cmd == HPET_INFO) && !err) {
703 struct compat_hpet_info __user *u = compat_ptr(arg);
704 if (put_user(info.hi_ireqfreq, &u->hi_ireqfreq) ||
705 put_user(info.hi_flags, &u->hi_flags) ||
706 put_user(info.hi_hpet, &u->hi_hpet) ||
707 put_user(info.hi_timer, &u->hi_timer))
708 err = -EFAULT;
709 }
710
711 return err;
712}
713#endif
714
656static const struct file_operations hpet_fops = { 715static const struct file_operations hpet_fops = {
657 .owner = THIS_MODULE, 716 .owner = THIS_MODULE,
658 .llseek = no_llseek, 717 .llseek = no_llseek,
659 .read = hpet_read, 718 .read = hpet_read,
660 .poll = hpet_poll, 719 .poll = hpet_poll,
661 .unlocked_ioctl = hpet_ioctl, 720 .unlocked_ioctl = hpet_ioctl,
721#ifdef CONFIG_COMPAT
722 .compat_ioctl = hpet_compat_ioctl,
723#endif
662 .open = hpet_open, 724 .open = hpet_open,
663 .release = hpet_release, 725 .release = hpet_release,
664 .fasync = hpet_fasync, 726 .fasync = hpet_fasync,
@@ -781,7 +843,7 @@ int hpet_alloc(struct hpet_data *hdp)
781 struct hpets *hpetp; 843 struct hpets *hpetp;
782 size_t siz; 844 size_t siz;
783 struct hpet __iomem *hpet; 845 struct hpet __iomem *hpet;
784 static struct hpets *last = NULL; 846 static struct hpets *last;
785 unsigned long period; 847 unsigned long period;
786 unsigned long long temp; 848 unsigned long long temp;
787 u32 remainder; 849 u32 remainder;
@@ -891,9 +953,7 @@ int hpet_alloc(struct hpet_data *hdp)
891 if (!hpet_clocksource) { 953 if (!hpet_clocksource) {
892 hpet_mctr = (void __iomem *)&hpetp->hp_hpet->hpet_mc; 954 hpet_mctr = (void __iomem *)&hpetp->hp_hpet->hpet_mc;
893 CLKSRC_FSYS_MMIO_SET(clocksource_hpet.fsys_mmio, hpet_mctr); 955 CLKSRC_FSYS_MMIO_SET(clocksource_hpet.fsys_mmio, hpet_mctr);
894 clocksource_hpet.mult = clocksource_hz2mult(hpetp->hp_tick_freq, 956 clocksource_register_hz(&clocksource_hpet, hpetp->hp_tick_freq);
895 clocksource_hpet.shift);
896 clocksource_register(&clocksource_hpet);
897 hpetp->hp_clocksource = &clocksource_hpet; 957 hpetp->hp_clocksource = &clocksource_hpet;
898 hpet_clocksource = &clocksource_hpet; 958 hpet_clocksource = &clocksource_hpet;
899 } 959 }
@@ -970,6 +1030,8 @@ static int hpet_acpi_add(struct acpi_device *device)
970 return -ENODEV; 1030 return -ENODEV;
971 1031
972 if (!data.hd_address || !data.hd_nirqs) { 1032 if (!data.hd_address || !data.hd_nirqs) {
1033 if (data.hd_address)
1034 iounmap(data.hd_address);
973 printk("%s: no address or irqs in _CRS\n", __func__); 1035 printk("%s: no address or irqs in _CRS\n", __func__);
974 return -ENODEV; 1036 return -ENODEV;
975 } 1037 }
diff --git a/drivers/char/hvc_beat.c b/drivers/char/hvc_beat.c
deleted file mode 100644
index 5fe4631e2a61..000000000000
--- a/drivers/char/hvc_beat.c
+++ /dev/null
@@ -1,134 +0,0 @@
1/*
2 * Beat hypervisor console driver
3 *
4 * (C) Copyright 2006 TOSHIBA CORPORATION
5 *
6 * This code is based on drivers/char/hvc_rtas.c:
7 * (C) Copyright IBM Corporation 2001-2005
8 * (C) Copyright Red Hat, Inc. 2005
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 */
24
25#include <linux/module.h>
26#include <linux/init.h>
27#include <linux/err.h>
28#include <linux/string.h>
29#include <linux/console.h>
30#include <asm/prom.h>
31#include <asm/hvconsole.h>
32#include <asm/firmware.h>
33
34#include "hvc_console.h"
35
36extern int64_t beat_get_term_char(uint64_t, uint64_t *, uint64_t *, uint64_t *);
37extern int64_t beat_put_term_char(uint64_t, uint64_t, uint64_t, uint64_t);
38
39struct hvc_struct *hvc_beat_dev = NULL;
40
41/* bug: only one queue is available regardless of vtermno */
42static int hvc_beat_get_chars(uint32_t vtermno, char *buf, int cnt)
43{
44 static unsigned char q[sizeof(unsigned long) * 2]
45 __attribute__((aligned(sizeof(unsigned long))));
46 static int qlen = 0;
47 u64 got;
48
49again:
50 if (qlen) {
51 if (qlen > cnt) {
52 memcpy(buf, q, cnt);
53 qlen -= cnt;
54 memmove(q + cnt, q, qlen);
55 return cnt;
56 } else { /* qlen <= cnt */
57 int r;
58
59 memcpy(buf, q, qlen);
60 r = qlen;
61 qlen = 0;
62 return r;
63 }
64 }
65 if (beat_get_term_char(vtermno, &got,
66 ((u64 *)q), ((u64 *)q) + 1) == 0) {
67 qlen = got;
68 goto again;
69 }
70 return 0;
71}
72
73static int hvc_beat_put_chars(uint32_t vtermno, const char *buf, int cnt)
74{
75 unsigned long kb[2];
76 int rest, nlen;
77
78 for (rest = cnt; rest > 0; rest -= nlen) {
79 nlen = (rest > 16) ? 16 : rest;
80 memcpy(kb, buf, nlen);
81 beat_put_term_char(vtermno, nlen, kb[0], kb[1]);
82 buf += nlen;
83 }
84 return cnt;
85}
86
87static const struct hv_ops hvc_beat_get_put_ops = {
88 .get_chars = hvc_beat_get_chars,
89 .put_chars = hvc_beat_put_chars,
90};
91
92static int hvc_beat_useit = 1;
93
94static int hvc_beat_config(char *p)
95{
96 hvc_beat_useit = simple_strtoul(p, NULL, 0);
97 return 0;
98}
99
100static int __init hvc_beat_console_init(void)
101{
102 if (hvc_beat_useit && of_machine_is_compatible("Beat")) {
103 hvc_instantiate(0, 0, &hvc_beat_get_put_ops);
104 }
105 return 0;
106}
107
108/* temp */
109static int __init hvc_beat_init(void)
110{
111 struct hvc_struct *hp;
112
113 if (!firmware_has_feature(FW_FEATURE_BEAT))
114 return -ENODEV;
115
116 hp = hvc_alloc(0, NO_IRQ, &hvc_beat_get_put_ops, 16);
117 if (IS_ERR(hp))
118 return PTR_ERR(hp);
119 hvc_beat_dev = hp;
120 return 0;
121}
122
123static void __exit hvc_beat_exit(void)
124{
125 if (hvc_beat_dev)
126 hvc_remove(hvc_beat_dev);
127}
128
129module_init(hvc_beat_init);
130module_exit(hvc_beat_exit);
131
132__setup("hvc_beat=", hvc_beat_config);
133
134console_initcall(hvc_beat_console_init);
diff --git a/drivers/char/hvc_console.c b/drivers/char/hvc_console.c
deleted file mode 100644
index 3afd62e856eb..000000000000
--- a/drivers/char/hvc_console.c
+++ /dev/null
@@ -1,915 +0,0 @@
1/*
2 * Copyright (C) 2001 Anton Blanchard <anton@au.ibm.com>, IBM
3 * Copyright (C) 2001 Paul Mackerras <paulus@au.ibm.com>, IBM
4 * Copyright (C) 2004 Benjamin Herrenschmidt <benh@kernel.crashing.org>, IBM Corp.
5 * Copyright (C) 2004 IBM Corporation
6 *
7 * Additional Author(s):
8 * Ryan S. Arnold <rsa@us.ibm.com>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24
25#include <linux/console.h>
26#include <linux/cpumask.h>
27#include <linux/init.h>
28#include <linux/kbd_kern.h>
29#include <linux/kernel.h>
30#include <linux/kthread.h>
31#include <linux/list.h>
32#include <linux/module.h>
33#include <linux/major.h>
34#include <linux/sysrq.h>
35#include <linux/tty.h>
36#include <linux/tty_flip.h>
37#include <linux/sched.h>
38#include <linux/spinlock.h>
39#include <linux/delay.h>
40#include <linux/freezer.h>
41#include <linux/slab.h>
42
43#include <asm/uaccess.h>
44
45#include "hvc_console.h"
46
47#define HVC_MAJOR 229
48#define HVC_MINOR 0
49
50/*
51 * Wait this long per iteration while trying to push buffered data to the
52 * hypervisor before allowing the tty to complete a close operation.
53 */
54#define HVC_CLOSE_WAIT (HZ/100) /* 1/10 of a second */
55
56/*
57 * These sizes are most efficient for vio, because they are the
58 * native transfer size. We could make them selectable in the
59 * future to better deal with backends that want other buffer sizes.
60 */
61#define N_OUTBUF 16
62#define N_INBUF 16
63
64#define __ALIGNED__ __attribute__((__aligned__(sizeof(long))))
65
66static struct tty_driver *hvc_driver;
67static struct task_struct *hvc_task;
68
69/* Picks up late kicks after list walk but before schedule() */
70static int hvc_kicked;
71
72static int hvc_init(void);
73
74#ifdef CONFIG_MAGIC_SYSRQ
75static int sysrq_pressed;
76#endif
77
78/* dynamic list of hvc_struct instances */
79static LIST_HEAD(hvc_structs);
80
81/*
82 * Protect the list of hvc_struct instances from inserts and removals during
83 * list traversal.
84 */
85static DEFINE_SPINLOCK(hvc_structs_lock);
86
87/*
88 * This value is used to assign a tty->index value to a hvc_struct based
89 * upon order of exposure via hvc_probe(), when we can not match it to
90 * a console candidate registered with hvc_instantiate().
91 */
92static int last_hvc = -1;
93
94/*
95 * Do not call this function with either the hvc_structs_lock or the hvc_struct
96 * lock held. If successful, this function increments the kref reference
97 * count against the target hvc_struct so it should be released when finished.
98 */
99static struct hvc_struct *hvc_get_by_index(int index)
100{
101 struct hvc_struct *hp;
102 unsigned long flags;
103
104 spin_lock(&hvc_structs_lock);
105
106 list_for_each_entry(hp, &hvc_structs, next) {
107 spin_lock_irqsave(&hp->lock, flags);
108 if (hp->index == index) {
109 kref_get(&hp->kref);
110 spin_unlock_irqrestore(&hp->lock, flags);
111 spin_unlock(&hvc_structs_lock);
112 return hp;
113 }
114 spin_unlock_irqrestore(&hp->lock, flags);
115 }
116 hp = NULL;
117
118 spin_unlock(&hvc_structs_lock);
119 return hp;
120}
121
122
123/*
124 * Initial console vtermnos for console API usage prior to full console
125 * initialization. Any vty adapter outside this range will not have usable
126 * console interfaces but can still be used as a tty device. This has to be
127 * static because kmalloc will not work during early console init.
128 */
129static const struct hv_ops *cons_ops[MAX_NR_HVC_CONSOLES];
130static uint32_t vtermnos[MAX_NR_HVC_CONSOLES] =
131 {[0 ... MAX_NR_HVC_CONSOLES - 1] = -1};
132
133/*
134 * Console APIs, NOT TTY. These APIs are available immediately when
135 * hvc_console_setup() finds adapters.
136 */
137
138static void hvc_console_print(struct console *co, const char *b,
139 unsigned count)
140{
141 char c[N_OUTBUF] __ALIGNED__;
142 unsigned i = 0, n = 0;
143 int r, donecr = 0, index = co->index;
144
145 /* Console access attempt outside of acceptable console range. */
146 if (index >= MAX_NR_HVC_CONSOLES)
147 return;
148
149 /* This console adapter was removed so it is not usable. */
150 if (vtermnos[index] == -1)
151 return;
152
153 while (count > 0 || i > 0) {
154 if (count > 0 && i < sizeof(c)) {
155 if (b[n] == '\n' && !donecr) {
156 c[i++] = '\r';
157 donecr = 1;
158 } else {
159 c[i++] = b[n++];
160 donecr = 0;
161 --count;
162 }
163 } else {
164 r = cons_ops[index]->put_chars(vtermnos[index], c, i);
165 if (r <= 0) {
166 /* throw away chars on error */
167 i = 0;
168 } else if (r > 0) {
169 i -= r;
170 if (i > 0)
171 memmove(c, c+r, i);
172 }
173 }
174 }
175}
176
177static struct tty_driver *hvc_console_device(struct console *c, int *index)
178{
179 if (vtermnos[c->index] == -1)
180 return NULL;
181
182 *index = c->index;
183 return hvc_driver;
184}
185
186static int __init hvc_console_setup(struct console *co, char *options)
187{
188 if (co->index < 0 || co->index >= MAX_NR_HVC_CONSOLES)
189 return -ENODEV;
190
191 if (vtermnos[co->index] == -1)
192 return -ENODEV;
193
194 return 0;
195}
196
197static struct console hvc_console = {
198 .name = "hvc",
199 .write = hvc_console_print,
200 .device = hvc_console_device,
201 .setup = hvc_console_setup,
202 .flags = CON_PRINTBUFFER,
203 .index = -1,
204};
205
206/*
207 * Early console initialization. Precedes driver initialization.
208 *
209 * (1) we are first, and the user specified another driver
210 * -- index will remain -1
211 * (2) we are first and the user specified no driver
212 * -- index will be set to 0, then we will fail setup.
213 * (3) we are first and the user specified our driver
214 * -- index will be set to user specified driver, and we will fail
215 * (4) we are after driver, and this initcall will register us
216 * -- if the user didn't specify a driver then the console will match
217 *
218 * Note that for cases 2 and 3, we will match later when the io driver
219 * calls hvc_instantiate() and call register again.
220 */
221static int __init hvc_console_init(void)
222{
223 register_console(&hvc_console);
224 return 0;
225}
226console_initcall(hvc_console_init);
227
228/* callback when the kboject ref count reaches zero. */
229static void destroy_hvc_struct(struct kref *kref)
230{
231 struct hvc_struct *hp = container_of(kref, struct hvc_struct, kref);
232 unsigned long flags;
233
234 spin_lock(&hvc_structs_lock);
235
236 spin_lock_irqsave(&hp->lock, flags);
237 list_del(&(hp->next));
238 spin_unlock_irqrestore(&hp->lock, flags);
239
240 spin_unlock(&hvc_structs_lock);
241
242 kfree(hp);
243}
244
245/*
246 * hvc_instantiate() is an early console discovery method which locates
247 * consoles * prior to the vio subsystem discovering them. Hotplugged
248 * vty adapters do NOT get an hvc_instantiate() callback since they
249 * appear after early console init.
250 */
251int hvc_instantiate(uint32_t vtermno, int index, const struct hv_ops *ops)
252{
253 struct hvc_struct *hp;
254
255 if (index < 0 || index >= MAX_NR_HVC_CONSOLES)
256 return -1;
257
258 if (vtermnos[index] != -1)
259 return -1;
260
261 /* make sure no no tty has been registered in this index */
262 hp = hvc_get_by_index(index);
263 if (hp) {
264 kref_put(&hp->kref, destroy_hvc_struct);
265 return -1;
266 }
267
268 vtermnos[index] = vtermno;
269 cons_ops[index] = ops;
270
271 /* reserve all indices up to and including this index */
272 if (last_hvc < index)
273 last_hvc = index;
274
275 /* if this index is what the user requested, then register
276 * now (setup won't fail at this point). It's ok to just
277 * call register again if previously .setup failed.
278 */
279 if (index == hvc_console.index)
280 register_console(&hvc_console);
281
282 return 0;
283}
284EXPORT_SYMBOL_GPL(hvc_instantiate);
285
286/* Wake the sleeping khvcd */
287void hvc_kick(void)
288{
289 hvc_kicked = 1;
290 wake_up_process(hvc_task);
291}
292EXPORT_SYMBOL_GPL(hvc_kick);
293
294static void hvc_unthrottle(struct tty_struct *tty)
295{
296 hvc_kick();
297}
298
299/*
300 * The TTY interface won't be used until after the vio layer has exposed the vty
301 * adapter to the kernel.
302 */
303static int hvc_open(struct tty_struct *tty, struct file * filp)
304{
305 struct hvc_struct *hp;
306 unsigned long flags;
307 int rc = 0;
308
309 /* Auto increments kref reference if found. */
310 if (!(hp = hvc_get_by_index(tty->index)))
311 return -ENODEV;
312
313 spin_lock_irqsave(&hp->lock, flags);
314 /* Check and then increment for fast path open. */
315 if (hp->count++ > 0) {
316 tty_kref_get(tty);
317 spin_unlock_irqrestore(&hp->lock, flags);
318 hvc_kick();
319 return 0;
320 } /* else count == 0 */
321
322 tty->driver_data = hp;
323
324 hp->tty = tty_kref_get(tty);
325
326 spin_unlock_irqrestore(&hp->lock, flags);
327
328 if (hp->ops->notifier_add)
329 rc = hp->ops->notifier_add(hp, hp->data);
330
331 /*
332 * If the notifier fails we return an error. The tty layer
333 * will call hvc_close() after a failed open but we don't want to clean
334 * up there so we'll clean up here and clear out the previously set
335 * tty fields and return the kref reference.
336 */
337 if (rc) {
338 spin_lock_irqsave(&hp->lock, flags);
339 hp->tty = NULL;
340 spin_unlock_irqrestore(&hp->lock, flags);
341 tty_kref_put(tty);
342 tty->driver_data = NULL;
343 kref_put(&hp->kref, destroy_hvc_struct);
344 printk(KERN_ERR "hvc_open: request_irq failed with rc %d.\n", rc);
345 }
346 /* Force wakeup of the polling thread */
347 hvc_kick();
348
349 return rc;
350}
351
352static void hvc_close(struct tty_struct *tty, struct file * filp)
353{
354 struct hvc_struct *hp;
355 unsigned long flags;
356
357 if (tty_hung_up_p(filp))
358 return;
359
360 /*
361 * No driver_data means that this close was issued after a failed
362 * hvc_open by the tty layer's release_dev() function and we can just
363 * exit cleanly because the kref reference wasn't made.
364 */
365 if (!tty->driver_data)
366 return;
367
368 hp = tty->driver_data;
369
370 spin_lock_irqsave(&hp->lock, flags);
371
372 if (--hp->count == 0) {
373 /* We are done with the tty pointer now. */
374 hp->tty = NULL;
375 spin_unlock_irqrestore(&hp->lock, flags);
376
377 if (hp->ops->notifier_del)
378 hp->ops->notifier_del(hp, hp->data);
379
380 /* cancel pending tty resize work */
381 cancel_work_sync(&hp->tty_resize);
382
383 /*
384 * Chain calls chars_in_buffer() and returns immediately if
385 * there is no buffered data otherwise sleeps on a wait queue
386 * waking periodically to check chars_in_buffer().
387 */
388 tty_wait_until_sent(tty, HVC_CLOSE_WAIT);
389 } else {
390 if (hp->count < 0)
391 printk(KERN_ERR "hvc_close %X: oops, count is %d\n",
392 hp->vtermno, hp->count);
393 spin_unlock_irqrestore(&hp->lock, flags);
394 }
395
396 tty_kref_put(tty);
397 kref_put(&hp->kref, destroy_hvc_struct);
398}
399
400static void hvc_hangup(struct tty_struct *tty)
401{
402 struct hvc_struct *hp = tty->driver_data;
403 unsigned long flags;
404 int temp_open_count;
405
406 if (!hp)
407 return;
408
409 /* cancel pending tty resize work */
410 cancel_work_sync(&hp->tty_resize);
411
412 spin_lock_irqsave(&hp->lock, flags);
413
414 /*
415 * The N_TTY line discipline has problems such that in a close vs
416 * open->hangup case this can be called after the final close so prevent
417 * that from happening for now.
418 */
419 if (hp->count <= 0) {
420 spin_unlock_irqrestore(&hp->lock, flags);
421 return;
422 }
423
424 temp_open_count = hp->count;
425 hp->count = 0;
426 hp->n_outbuf = 0;
427 hp->tty = NULL;
428
429 spin_unlock_irqrestore(&hp->lock, flags);
430
431 if (hp->ops->notifier_hangup)
432 hp->ops->notifier_hangup(hp, hp->data);
433
434 while(temp_open_count) {
435 --temp_open_count;
436 tty_kref_put(tty);
437 kref_put(&hp->kref, destroy_hvc_struct);
438 }
439}
440
441/*
442 * Push buffered characters whether they were just recently buffered or waiting
443 * on a blocked hypervisor. Call this function with hp->lock held.
444 */
445static int hvc_push(struct hvc_struct *hp)
446{
447 int n;
448
449 n = hp->ops->put_chars(hp->vtermno, hp->outbuf, hp->n_outbuf);
450 if (n <= 0) {
451 if (n == 0) {
452 hp->do_wakeup = 1;
453 return 0;
454 }
455 /* throw away output on error; this happens when
456 there is no session connected to the vterm. */
457 hp->n_outbuf = 0;
458 } else
459 hp->n_outbuf -= n;
460 if (hp->n_outbuf > 0)
461 memmove(hp->outbuf, hp->outbuf + n, hp->n_outbuf);
462 else
463 hp->do_wakeup = 1;
464
465 return n;
466}
467
468static int hvc_write(struct tty_struct *tty, const unsigned char *buf, int count)
469{
470 struct hvc_struct *hp = tty->driver_data;
471 unsigned long flags;
472 int rsize, written = 0;
473
474 /* This write was probably executed during a tty close. */
475 if (!hp)
476 return -EPIPE;
477
478 if (hp->count <= 0)
479 return -EIO;
480
481 spin_lock_irqsave(&hp->lock, flags);
482
483 /* Push pending writes */
484 if (hp->n_outbuf > 0)
485 hvc_push(hp);
486
487 while (count > 0 && (rsize = hp->outbuf_size - hp->n_outbuf) > 0) {
488 if (rsize > count)
489 rsize = count;
490 memcpy(hp->outbuf + hp->n_outbuf, buf, rsize);
491 count -= rsize;
492 buf += rsize;
493 hp->n_outbuf += rsize;
494 written += rsize;
495 hvc_push(hp);
496 }
497 spin_unlock_irqrestore(&hp->lock, flags);
498
499 /*
500 * Racy, but harmless, kick thread if there is still pending data.
501 */
502 if (hp->n_outbuf)
503 hvc_kick();
504
505 return written;
506}
507
508/**
509 * hvc_set_winsz() - Resize the hvc tty terminal window.
510 * @work: work structure.
511 *
512 * The routine shall not be called within an atomic context because it
513 * might sleep.
514 *
515 * Locking: hp->lock
516 */
517static void hvc_set_winsz(struct work_struct *work)
518{
519 struct hvc_struct *hp;
520 unsigned long hvc_flags;
521 struct tty_struct *tty;
522 struct winsize ws;
523
524 hp = container_of(work, struct hvc_struct, tty_resize);
525
526 spin_lock_irqsave(&hp->lock, hvc_flags);
527 if (!hp->tty) {
528 spin_unlock_irqrestore(&hp->lock, hvc_flags);
529 return;
530 }
531 ws = hp->ws;
532 tty = tty_kref_get(hp->tty);
533 spin_unlock_irqrestore(&hp->lock, hvc_flags);
534
535 tty_do_resize(tty, &ws);
536 tty_kref_put(tty);
537}
538
539/*
540 * This is actually a contract between the driver and the tty layer outlining
541 * how much write room the driver can guarantee will be sent OR BUFFERED. This
542 * driver MUST honor the return value.
543 */
544static int hvc_write_room(struct tty_struct *tty)
545{
546 struct hvc_struct *hp = tty->driver_data;
547
548 if (!hp)
549 return -1;
550
551 return hp->outbuf_size - hp->n_outbuf;
552}
553
554static int hvc_chars_in_buffer(struct tty_struct *tty)
555{
556 struct hvc_struct *hp = tty->driver_data;
557
558 if (!hp)
559 return 0;
560 return hp->n_outbuf;
561}
562
563/*
564 * timeout will vary between the MIN and MAX values defined here. By default
565 * and during console activity we will use a default MIN_TIMEOUT of 10. When
566 * the console is idle, we increase the timeout value on each pass through
567 * msleep until we reach the max. This may be noticeable as a brief (average
568 * one second) delay on the console before the console responds to input when
569 * there has been no input for some time.
570 */
571#define MIN_TIMEOUT (10)
572#define MAX_TIMEOUT (2000)
573static u32 timeout = MIN_TIMEOUT;
574
575#define HVC_POLL_READ 0x00000001
576#define HVC_POLL_WRITE 0x00000002
577
578int hvc_poll(struct hvc_struct *hp)
579{
580 struct tty_struct *tty;
581 int i, n, poll_mask = 0;
582 char buf[N_INBUF] __ALIGNED__;
583 unsigned long flags;
584 int read_total = 0;
585 int written_total = 0;
586
587 spin_lock_irqsave(&hp->lock, flags);
588
589 /* Push pending writes */
590 if (hp->n_outbuf > 0)
591 written_total = hvc_push(hp);
592
593 /* Reschedule us if still some write pending */
594 if (hp->n_outbuf > 0) {
595 poll_mask |= HVC_POLL_WRITE;
596 /* If hvc_push() was not able to write, sleep a few msecs */
597 timeout = (written_total) ? 0 : MIN_TIMEOUT;
598 }
599
600 /* No tty attached, just skip */
601 tty = tty_kref_get(hp->tty);
602 if (tty == NULL)
603 goto bail;
604
605 /* Now check if we can get data (are we throttled ?) */
606 if (test_bit(TTY_THROTTLED, &tty->flags))
607 goto throttled;
608
609 /* If we aren't notifier driven and aren't throttled, we always
610 * request a reschedule
611 */
612 if (!hp->irq_requested)
613 poll_mask |= HVC_POLL_READ;
614
615 /* Read data if any */
616 for (;;) {
617 int count = tty_buffer_request_room(tty, N_INBUF);
618
619 /* If flip is full, just reschedule a later read */
620 if (count == 0) {
621 poll_mask |= HVC_POLL_READ;
622 break;
623 }
624
625 n = hp->ops->get_chars(hp->vtermno, buf, count);
626 if (n <= 0) {
627 /* Hangup the tty when disconnected from host */
628 if (n == -EPIPE) {
629 spin_unlock_irqrestore(&hp->lock, flags);
630 tty_hangup(tty);
631 spin_lock_irqsave(&hp->lock, flags);
632 } else if ( n == -EAGAIN ) {
633 /*
634 * Some back-ends can only ensure a certain min
635 * num of bytes read, which may be > 'count'.
636 * Let the tty clear the flip buff to make room.
637 */
638 poll_mask |= HVC_POLL_READ;
639 }
640 break;
641 }
642 for (i = 0; i < n; ++i) {
643#ifdef CONFIG_MAGIC_SYSRQ
644 if (hp->index == hvc_console.index) {
645 /* Handle the SysRq Hack */
646 /* XXX should support a sequence */
647 if (buf[i] == '\x0f') { /* ^O */
648 /* if ^O is pressed again, reset
649 * sysrq_pressed and flip ^O char */
650 sysrq_pressed = !sysrq_pressed;
651 if (sysrq_pressed)
652 continue;
653 } else if (sysrq_pressed) {
654 handle_sysrq(buf[i]);
655 sysrq_pressed = 0;
656 continue;
657 }
658 }
659#endif /* CONFIG_MAGIC_SYSRQ */
660 tty_insert_flip_char(tty, buf[i], 0);
661 }
662
663 read_total += n;
664 }
665 throttled:
666 /* Wakeup write queue if necessary */
667 if (hp->do_wakeup) {
668 hp->do_wakeup = 0;
669 tty_wakeup(tty);
670 }
671 bail:
672 spin_unlock_irqrestore(&hp->lock, flags);
673
674 if (read_total) {
675 /* Activity is occurring, so reset the polling backoff value to
676 a minimum for performance. */
677 timeout = MIN_TIMEOUT;
678
679 tty_flip_buffer_push(tty);
680 }
681 if (tty)
682 tty_kref_put(tty);
683
684 return poll_mask;
685}
686EXPORT_SYMBOL_GPL(hvc_poll);
687
688/**
689 * __hvc_resize() - Update terminal window size information.
690 * @hp: HVC console pointer
691 * @ws: Terminal window size structure
692 *
693 * Stores the specified window size information in the hvc structure of @hp.
694 * The function schedule the tty resize update.
695 *
696 * Locking: Locking free; the function MUST be called holding hp->lock
697 */
698void __hvc_resize(struct hvc_struct *hp, struct winsize ws)
699{
700 hp->ws = ws;
701 schedule_work(&hp->tty_resize);
702}
703EXPORT_SYMBOL_GPL(__hvc_resize);
704
705/*
706 * This kthread is either polling or interrupt driven. This is determined by
707 * calling hvc_poll() who determines whether a console adapter support
708 * interrupts.
709 */
710static int khvcd(void *unused)
711{
712 int poll_mask;
713 struct hvc_struct *hp;
714
715 set_freezable();
716 __set_current_state(TASK_RUNNING);
717 do {
718 poll_mask = 0;
719 hvc_kicked = 0;
720 try_to_freeze();
721 wmb();
722 if (!cpus_are_in_xmon()) {
723 spin_lock(&hvc_structs_lock);
724 list_for_each_entry(hp, &hvc_structs, next) {
725 poll_mask |= hvc_poll(hp);
726 }
727 spin_unlock(&hvc_structs_lock);
728 } else
729 poll_mask |= HVC_POLL_READ;
730 if (hvc_kicked)
731 continue;
732 set_current_state(TASK_INTERRUPTIBLE);
733 if (!hvc_kicked) {
734 if (poll_mask == 0)
735 schedule();
736 else {
737 if (timeout < MAX_TIMEOUT)
738 timeout += (timeout >> 6) + 1;
739
740 msleep_interruptible(timeout);
741 }
742 }
743 __set_current_state(TASK_RUNNING);
744 } while (!kthread_should_stop());
745
746 return 0;
747}
748
749static const struct tty_operations hvc_ops = {
750 .open = hvc_open,
751 .close = hvc_close,
752 .write = hvc_write,
753 .hangup = hvc_hangup,
754 .unthrottle = hvc_unthrottle,
755 .write_room = hvc_write_room,
756 .chars_in_buffer = hvc_chars_in_buffer,
757};
758
759struct hvc_struct *hvc_alloc(uint32_t vtermno, int data,
760 const struct hv_ops *ops,
761 int outbuf_size)
762{
763 struct hvc_struct *hp;
764 int i;
765
766 /* We wait until a driver actually comes along */
767 if (!hvc_driver) {
768 int err = hvc_init();
769 if (err)
770 return ERR_PTR(err);
771 }
772
773 hp = kzalloc(ALIGN(sizeof(*hp), sizeof(long)) + outbuf_size,
774 GFP_KERNEL);
775 if (!hp)
776 return ERR_PTR(-ENOMEM);
777
778 hp->vtermno = vtermno;
779 hp->data = data;
780 hp->ops = ops;
781 hp->outbuf_size = outbuf_size;
782 hp->outbuf = &((char *)hp)[ALIGN(sizeof(*hp), sizeof(long))];
783
784 kref_init(&hp->kref);
785
786 INIT_WORK(&hp->tty_resize, hvc_set_winsz);
787 spin_lock_init(&hp->lock);
788 spin_lock(&hvc_structs_lock);
789
790 /*
791 * find index to use:
792 * see if this vterm id matches one registered for console.
793 */
794 for (i=0; i < MAX_NR_HVC_CONSOLES; i++)
795 if (vtermnos[i] == hp->vtermno &&
796 cons_ops[i] == hp->ops)
797 break;
798
799 /* no matching slot, just use a counter */
800 if (i >= MAX_NR_HVC_CONSOLES)
801 i = ++last_hvc;
802
803 hp->index = i;
804
805 list_add_tail(&(hp->next), &hvc_structs);
806 spin_unlock(&hvc_structs_lock);
807
808 return hp;
809}
810EXPORT_SYMBOL_GPL(hvc_alloc);
811
812int hvc_remove(struct hvc_struct *hp)
813{
814 unsigned long flags;
815 struct tty_struct *tty;
816
817 spin_lock_irqsave(&hp->lock, flags);
818 tty = tty_kref_get(hp->tty);
819
820 if (hp->index < MAX_NR_HVC_CONSOLES)
821 vtermnos[hp->index] = -1;
822
823 /* Don't whack hp->irq because tty_hangup() will need to free the irq. */
824
825 spin_unlock_irqrestore(&hp->lock, flags);
826
827 /*
828 * We 'put' the instance that was grabbed when the kref instance
829 * was initialized using kref_init(). Let the last holder of this
830 * kref cause it to be removed, which will probably be the tty_vhangup
831 * below.
832 */
833 kref_put(&hp->kref, destroy_hvc_struct);
834
835 /*
836 * This function call will auto chain call hvc_hangup.
837 */
838 if (tty) {
839 tty_vhangup(tty);
840 tty_kref_put(tty);
841 }
842 return 0;
843}
844EXPORT_SYMBOL_GPL(hvc_remove);
845
846/* Driver initialization: called as soon as someone uses hvc_alloc(). */
847static int hvc_init(void)
848{
849 struct tty_driver *drv;
850 int err;
851
852 /* We need more than hvc_count adapters due to hotplug additions. */
853 drv = alloc_tty_driver(HVC_ALLOC_TTY_ADAPTERS);
854 if (!drv) {
855 err = -ENOMEM;
856 goto out;
857 }
858
859 drv->owner = THIS_MODULE;
860 drv->driver_name = "hvc";
861 drv->name = "hvc";
862 drv->major = HVC_MAJOR;
863 drv->minor_start = HVC_MINOR;
864 drv->type = TTY_DRIVER_TYPE_SYSTEM;
865 drv->init_termios = tty_std_termios;
866 drv->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_RESET_TERMIOS;
867 tty_set_operations(drv, &hvc_ops);
868
869 /* Always start the kthread because there can be hotplug vty adapters
870 * added later. */
871 hvc_task = kthread_run(khvcd, NULL, "khvcd");
872 if (IS_ERR(hvc_task)) {
873 printk(KERN_ERR "Couldn't create kthread for console.\n");
874 err = PTR_ERR(hvc_task);
875 goto put_tty;
876 }
877
878 err = tty_register_driver(drv);
879 if (err) {
880 printk(KERN_ERR "Couldn't register hvc console driver\n");
881 goto stop_thread;
882 }
883
884 /*
885 * Make sure tty is fully registered before allowing it to be
886 * found by hvc_console_device.
887 */
888 smp_mb();
889 hvc_driver = drv;
890 return 0;
891
892stop_thread:
893 kthread_stop(hvc_task);
894 hvc_task = NULL;
895put_tty:
896 put_tty_driver(drv);
897out:
898 return err;
899}
900
901/* This isn't particularly necessary due to this being a console driver
902 * but it is nice to be thorough.
903 */
904static void __exit hvc_exit(void)
905{
906 if (hvc_driver) {
907 kthread_stop(hvc_task);
908
909 tty_unregister_driver(hvc_driver);
910 /* return tty_struct instances allocated in hvc_init(). */
911 put_tty_driver(hvc_driver);
912 unregister_console(&hvc_console);
913 }
914}
915module_exit(hvc_exit);
diff --git a/drivers/char/hvc_console.h b/drivers/char/hvc_console.h
deleted file mode 100644
index 54381eba4e4a..000000000000
--- a/drivers/char/hvc_console.h
+++ /dev/null
@@ -1,119 +0,0 @@
1/*
2 * hvc_console.h
3 * Copyright (C) 2005 IBM Corporation
4 *
5 * Author(s):
6 * Ryan S. Arnold <rsa@us.ibm.com>
7 *
8 * hvc_console header information:
9 * moved here from arch/powerpc/include/asm/hvconsole.h
10 * and drivers/char/hvc_console.c
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 */
26
27#ifndef HVC_CONSOLE_H
28#define HVC_CONSOLE_H
29#include <linux/kref.h>
30#include <linux/tty.h>
31#include <linux/spinlock.h>
32
33/*
34 * This is the max number of console adapters that can/will be found as
35 * console devices on first stage console init. Any number beyond this range
36 * can't be used as a console device but is still a valid tty device.
37 */
38#define MAX_NR_HVC_CONSOLES 16
39
40/*
41 * The Linux TTY code does not support dynamic addition of tty derived devices
42 * so we need to know how many tty devices we might need when space is allocated
43 * for the tty device. Since this driver supports hotplug of vty adapters we
44 * need to make sure we have enough allocated.
45 */
46#define HVC_ALLOC_TTY_ADAPTERS 8
47
48struct hvc_struct {
49 spinlock_t lock;
50 int index;
51 struct tty_struct *tty;
52 int count;
53 int do_wakeup;
54 char *outbuf;
55 int outbuf_size;
56 int n_outbuf;
57 uint32_t vtermno;
58 const struct hv_ops *ops;
59 int irq_requested;
60 int data;
61 struct winsize ws;
62 struct work_struct tty_resize;
63 struct list_head next;
64 struct kref kref; /* ref count & hvc_struct lifetime */
65};
66
67/* implemented by a low level driver */
68struct hv_ops {
69 int (*get_chars)(uint32_t vtermno, char *buf, int count);
70 int (*put_chars)(uint32_t vtermno, const char *buf, int count);
71
72 /* Callbacks for notification. Called in open, close and hangup */
73 int (*notifier_add)(struct hvc_struct *hp, int irq);
74 void (*notifier_del)(struct hvc_struct *hp, int irq);
75 void (*notifier_hangup)(struct hvc_struct *hp, int irq);
76};
77
78/* Register a vterm and a slot index for use as a console (console_init) */
79extern int hvc_instantiate(uint32_t vtermno, int index,
80 const struct hv_ops *ops);
81
82/* register a vterm for hvc tty operation (module_init or hotplug add) */
83extern struct hvc_struct * hvc_alloc(uint32_t vtermno, int data,
84 const struct hv_ops *ops, int outbuf_size);
85/* remove a vterm from hvc tty operation (module_exit or hotplug remove) */
86extern int hvc_remove(struct hvc_struct *hp);
87
88/* data available */
89int hvc_poll(struct hvc_struct *hp);
90void hvc_kick(void);
91
92/* Resize hvc tty terminal window */
93extern void __hvc_resize(struct hvc_struct *hp, struct winsize ws);
94
95static inline void hvc_resize(struct hvc_struct *hp, struct winsize ws)
96{
97 unsigned long flags;
98
99 spin_lock_irqsave(&hp->lock, flags);
100 __hvc_resize(hp, ws);
101 spin_unlock_irqrestore(&hp->lock, flags);
102}
103
104/* default notifier for irq based notification */
105extern int notifier_add_irq(struct hvc_struct *hp, int data);
106extern void notifier_del_irq(struct hvc_struct *hp, int data);
107extern void notifier_hangup_irq(struct hvc_struct *hp, int data);
108
109
110#if defined(CONFIG_XMON) && defined(CONFIG_SMP)
111#include <asm/xmon.h>
112#else
113static inline int cpus_are_in_xmon(void)
114{
115 return 0;
116}
117#endif
118
119#endif // HVC_CONSOLE_H
diff --git a/drivers/char/hvc_irq.c b/drivers/char/hvc_irq.c
deleted file mode 100644
index 2623e177e8d6..000000000000
--- a/drivers/char/hvc_irq.c
+++ /dev/null
@@ -1,49 +0,0 @@
1/*
2 * Copyright IBM Corp. 2001,2008
3 *
4 * This file contains the IRQ specific code for hvc_console
5 *
6 */
7
8#include <linux/interrupt.h>
9
10#include "hvc_console.h"
11
12static irqreturn_t hvc_handle_interrupt(int irq, void *dev_instance)
13{
14 /* if hvc_poll request a repoll, then kick the hvcd thread */
15 if (hvc_poll(dev_instance))
16 hvc_kick();
17 return IRQ_HANDLED;
18}
19
20/*
21 * For IRQ based systems these callbacks can be used
22 */
23int notifier_add_irq(struct hvc_struct *hp, int irq)
24{
25 int rc;
26
27 if (!irq) {
28 hp->irq_requested = 0;
29 return 0;
30 }
31 rc = request_irq(irq, hvc_handle_interrupt, IRQF_DISABLED,
32 "hvc_console", hp);
33 if (!rc)
34 hp->irq_requested = 1;
35 return rc;
36}
37
38void notifier_del_irq(struct hvc_struct *hp, int irq)
39{
40 if (!hp->irq_requested)
41 return;
42 free_irq(irq, hp);
43 hp->irq_requested = 0;
44}
45
46void notifier_hangup_irq(struct hvc_struct *hp, int irq)
47{
48 notifier_del_irq(hp, irq);
49}
diff --git a/drivers/char/hvc_iseries.c b/drivers/char/hvc_iseries.c
deleted file mode 100644
index 21c54955084e..000000000000
--- a/drivers/char/hvc_iseries.c
+++ /dev/null
@@ -1,598 +0,0 @@
1/*
2 * iSeries vio driver interface to hvc_console.c
3 *
4 * This code is based heavily on hvc_vio.c and viocons.c
5 *
6 * Copyright (C) 2006 Stephen Rothwell, IBM Corporation
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22#include <stdarg.h>
23#include <linux/types.h>
24#include <linux/init.h>
25#include <linux/kernel.h>
26#include <linux/spinlock.h>
27#include <linux/console.h>
28
29#include <asm/hvconsole.h>
30#include <asm/vio.h>
31#include <asm/prom.h>
32#include <asm/firmware.h>
33#include <asm/iseries/vio.h>
34#include <asm/iseries/hv_call.h>
35#include <asm/iseries/hv_lp_config.h>
36#include <asm/iseries/hv_lp_event.h>
37
38#include "hvc_console.h"
39
40#define VTTY_PORTS 10
41
42static DEFINE_SPINLOCK(consolelock);
43static DEFINE_SPINLOCK(consoleloglock);
44
45static const char hvc_driver_name[] = "hvc_console";
46
47#define IN_BUF_SIZE 200
48
49/*
50 * Our port information.
51 */
52static struct port_info {
53 HvLpIndex lp;
54 u64 seq; /* sequence number of last HV send */
55 u64 ack; /* last ack from HV */
56 struct hvc_struct *hp;
57 int in_start;
58 int in_end;
59 unsigned char in_buf[IN_BUF_SIZE];
60} port_info[VTTY_PORTS] = {
61 [ 0 ... VTTY_PORTS - 1 ] = {
62 .lp = HvLpIndexInvalid
63 }
64};
65
66#define viochar_is_console(pi) ((pi) == &port_info[0])
67
68static struct vio_device_id hvc_driver_table[] __devinitdata = {
69 {"serial", "IBM,iSeries-vty"},
70 { "", "" }
71};
72MODULE_DEVICE_TABLE(vio, hvc_driver_table);
73
74static void hvlog(char *fmt, ...)
75{
76 int i;
77 unsigned long flags;
78 va_list args;
79 static char buf[256];
80
81 spin_lock_irqsave(&consoleloglock, flags);
82 va_start(args, fmt);
83 i = vscnprintf(buf, sizeof(buf) - 1, fmt, args);
84 va_end(args);
85 buf[i++] = '\r';
86 HvCall_writeLogBuffer(buf, i);
87 spin_unlock_irqrestore(&consoleloglock, flags);
88}
89
90/*
91 * Initialize the common fields in a charLpEvent
92 */
93static void init_data_event(struct viocharlpevent *viochar, HvLpIndex lp)
94{
95 struct HvLpEvent *hev = &viochar->event;
96
97 memset(viochar, 0, sizeof(struct viocharlpevent));
98
99 hev->flags = HV_LP_EVENT_VALID | HV_LP_EVENT_DEFERRED_ACK |
100 HV_LP_EVENT_INT;
101 hev->xType = HvLpEvent_Type_VirtualIo;
102 hev->xSubtype = viomajorsubtype_chario | viochardata;
103 hev->xSourceLp = HvLpConfig_getLpIndex();
104 hev->xTargetLp = lp;
105 hev->xSizeMinus1 = sizeof(struct viocharlpevent);
106 hev->xSourceInstanceId = viopath_sourceinst(lp);
107 hev->xTargetInstanceId = viopath_targetinst(lp);
108}
109
110static int get_chars(uint32_t vtermno, char *buf, int count)
111{
112 struct port_info *pi;
113 int n = 0;
114 unsigned long flags;
115
116 if (vtermno >= VTTY_PORTS)
117 return -EINVAL;
118 if (count == 0)
119 return 0;
120
121 pi = &port_info[vtermno];
122 spin_lock_irqsave(&consolelock, flags);
123
124 if (pi->in_end == 0)
125 goto done;
126
127 n = pi->in_end - pi->in_start;
128 if (n > count)
129 n = count;
130 memcpy(buf, &pi->in_buf[pi->in_start], n);
131 pi->in_start += n;
132 if (pi->in_start == pi->in_end) {
133 pi->in_start = 0;
134 pi->in_end = 0;
135 }
136done:
137 spin_unlock_irqrestore(&consolelock, flags);
138 return n;
139}
140
141static int put_chars(uint32_t vtermno, const char *buf, int count)
142{
143 struct viocharlpevent *viochar;
144 struct port_info *pi;
145 HvLpEvent_Rc hvrc;
146 unsigned long flags;
147 int sent = 0;
148
149 if (vtermno >= VTTY_PORTS)
150 return -EINVAL;
151
152 pi = &port_info[vtermno];
153
154 spin_lock_irqsave(&consolelock, flags);
155
156 if (viochar_is_console(pi) && !viopath_isactive(pi->lp)) {
157 HvCall_writeLogBuffer(buf, count);
158 sent = count;
159 goto done;
160 }
161
162 viochar = vio_get_event_buffer(viomajorsubtype_chario);
163 if (viochar == NULL) {
164 hvlog("\n\rviocons: Can't get viochar buffer.");
165 goto done;
166 }
167
168 while ((count > 0) && ((pi->seq - pi->ack) < VIOCHAR_WINDOW)) {
169 int len;
170
171 len = (count > VIOCHAR_MAX_DATA) ? VIOCHAR_MAX_DATA : count;
172
173 if (viochar_is_console(pi))
174 HvCall_writeLogBuffer(buf, len);
175
176 init_data_event(viochar, pi->lp);
177
178 viochar->len = len;
179 viochar->event.xCorrelationToken = pi->seq++;
180 viochar->event.xSizeMinus1 =
181 offsetof(struct viocharlpevent, data) + len;
182
183 memcpy(viochar->data, buf, len);
184
185 hvrc = HvCallEvent_signalLpEvent(&viochar->event);
186 if (hvrc)
187 hvlog("\n\rerror sending event! return code %d\n\r",
188 (int)hvrc);
189 sent += len;
190 count -= len;
191 buf += len;
192 }
193
194 vio_free_event_buffer(viomajorsubtype_chario, viochar);
195done:
196 spin_unlock_irqrestore(&consolelock, flags);
197 return sent;
198}
199
200static const struct hv_ops hvc_get_put_ops = {
201 .get_chars = get_chars,
202 .put_chars = put_chars,
203 .notifier_add = notifier_add_irq,
204 .notifier_del = notifier_del_irq,
205 .notifier_hangup = notifier_hangup_irq,
206};
207
208static int __devinit hvc_vio_probe(struct vio_dev *vdev,
209 const struct vio_device_id *id)
210{
211 struct hvc_struct *hp;
212 struct port_info *pi;
213
214 /* probed with invalid parameters. */
215 if (!vdev || !id)
216 return -EPERM;
217
218 if (vdev->unit_address >= VTTY_PORTS)
219 return -ENODEV;
220
221 pi = &port_info[vdev->unit_address];
222
223 hp = hvc_alloc(vdev->unit_address, vdev->irq, &hvc_get_put_ops,
224 VIOCHAR_MAX_DATA);
225 if (IS_ERR(hp))
226 return PTR_ERR(hp);
227 pi->hp = hp;
228 dev_set_drvdata(&vdev->dev, pi);
229
230 return 0;
231}
232
233static int __devexit hvc_vio_remove(struct vio_dev *vdev)
234{
235 struct port_info *pi = dev_get_drvdata(&vdev->dev);
236 struct hvc_struct *hp = pi->hp;
237
238 return hvc_remove(hp);
239}
240
241static struct vio_driver hvc_vio_driver = {
242 .id_table = hvc_driver_table,
243 .probe = hvc_vio_probe,
244 .remove = __devexit_p(hvc_vio_remove),
245 .driver = {
246 .name = hvc_driver_name,
247 .owner = THIS_MODULE,
248 }
249};
250
251static void hvc_open_event(struct HvLpEvent *event)
252{
253 unsigned long flags;
254 struct viocharlpevent *cevent = (struct viocharlpevent *)event;
255 u8 port = cevent->virtual_device;
256 struct port_info *pi;
257 int reject = 0;
258
259 if (hvlpevent_is_ack(event)) {
260 if (port >= VTTY_PORTS)
261 return;
262
263 spin_lock_irqsave(&consolelock, flags);
264
265 pi = &port_info[port];
266 if (event->xRc == HvLpEvent_Rc_Good) {
267 pi->seq = pi->ack = 0;
268 /*
269 * This line allows connections from the primary
270 * partition but once one is connected from the
271 * primary partition nothing short of a reboot
272 * of linux will allow access from the hosting
273 * partition again without a required iSeries fix.
274 */
275 pi->lp = event->xTargetLp;
276 }
277
278 spin_unlock_irqrestore(&consolelock, flags);
279 if (event->xRc != HvLpEvent_Rc_Good)
280 printk(KERN_WARNING
281 "hvc: handle_open_event: event->xRc == (%d).\n",
282 event->xRc);
283
284 if (event->xCorrelationToken != 0) {
285 atomic_t *aptr= (atomic_t *)event->xCorrelationToken;
286 atomic_set(aptr, 1);
287 } else
288 printk(KERN_WARNING
289 "hvc: weird...got open ack without atomic\n");
290 return;
291 }
292
293 /* This had better require an ack, otherwise complain */
294 if (!hvlpevent_need_ack(event)) {
295 printk(KERN_WARNING "hvc: viocharopen without ack bit!\n");
296 return;
297 }
298
299 spin_lock_irqsave(&consolelock, flags);
300
301 /* Make sure this is a good virtual tty */
302 if (port >= VTTY_PORTS) {
303 event->xRc = HvLpEvent_Rc_SubtypeError;
304 cevent->subtype_result_code = viorc_openRejected;
305 /*
306 * Flag state here since we can't printk while holding
307 * the consolelock spinlock.
308 */
309 reject = 1;
310 } else {
311 pi = &port_info[port];
312 if ((pi->lp != HvLpIndexInvalid) &&
313 (pi->lp != event->xSourceLp)) {
314 /*
315 * If this is tty is already connected to a different
316 * partition, fail.
317 */
318 event->xRc = HvLpEvent_Rc_SubtypeError;
319 cevent->subtype_result_code = viorc_openRejected;
320 reject = 2;
321 } else {
322 pi->lp = event->xSourceLp;
323 event->xRc = HvLpEvent_Rc_Good;
324 cevent->subtype_result_code = viorc_good;
325 pi->seq = pi->ack = 0;
326 }
327 }
328
329 spin_unlock_irqrestore(&consolelock, flags);
330
331 if (reject == 1)
332 printk(KERN_WARNING "hvc: open rejected: bad virtual tty.\n");
333 else if (reject == 2)
334 printk(KERN_WARNING "hvc: open rejected: console in exclusive "
335 "use by another partition.\n");
336
337 /* Return the acknowledgement */
338 HvCallEvent_ackLpEvent(event);
339}
340
341/*
342 * Handle a close charLpEvent. This should ONLY be an Interrupt because the
343 * virtual console should never actually issue a close event to the hypervisor
344 * because the virtual console never goes away. A close event coming from the
345 * hypervisor simply means that there are no client consoles connected to the
346 * virtual console.
347 */
348static void hvc_close_event(struct HvLpEvent *event)
349{
350 unsigned long flags;
351 struct viocharlpevent *cevent = (struct viocharlpevent *)event;
352 u8 port = cevent->virtual_device;
353
354 if (!hvlpevent_is_int(event)) {
355 printk(KERN_WARNING
356 "hvc: got unexpected close acknowledgement\n");
357 return;
358 }
359
360 if (port >= VTTY_PORTS) {
361 printk(KERN_WARNING
362 "hvc: close message from invalid virtual device.\n");
363 return;
364 }
365
366 /* For closes, just mark the console partition invalid */
367 spin_lock_irqsave(&consolelock, flags);
368
369 if (port_info[port].lp == event->xSourceLp)
370 port_info[port].lp = HvLpIndexInvalid;
371
372 spin_unlock_irqrestore(&consolelock, flags);
373}
374
375static void hvc_data_event(struct HvLpEvent *event)
376{
377 unsigned long flags;
378 struct viocharlpevent *cevent = (struct viocharlpevent *)event;
379 struct port_info *pi;
380 int n;
381 u8 port = cevent->virtual_device;
382
383 if (port >= VTTY_PORTS) {
384 printk(KERN_WARNING "hvc: data on invalid virtual device %d\n",
385 port);
386 return;
387 }
388 if (cevent->len == 0)
389 return;
390
391 /*
392 * Change 05/01/2003 - Ryan Arnold: If a partition other than
393 * the current exclusive partition tries to send us data
394 * events then just drop them on the floor because we don't
395 * want his stinking data. He isn't authorized to receive
396 * data because he wasn't the first one to get the console,
397 * therefore he shouldn't be allowed to send data either.
398 * This will work without an iSeries fix.
399 */
400 pi = &port_info[port];
401 if (pi->lp != event->xSourceLp)
402 return;
403
404 spin_lock_irqsave(&consolelock, flags);
405
406 n = IN_BUF_SIZE - pi->in_end;
407 if (n > cevent->len)
408 n = cevent->len;
409 if (n > 0) {
410 memcpy(&pi->in_buf[pi->in_end], cevent->data, n);
411 pi->in_end += n;
412 }
413 spin_unlock_irqrestore(&consolelock, flags);
414 if (n == 0)
415 printk(KERN_WARNING "hvc: input buffer overflow\n");
416}
417
418static void hvc_ack_event(struct HvLpEvent *event)
419{
420 struct viocharlpevent *cevent = (struct viocharlpevent *)event;
421 unsigned long flags;
422 u8 port = cevent->virtual_device;
423
424 if (port >= VTTY_PORTS) {
425 printk(KERN_WARNING "hvc: data on invalid virtual device\n");
426 return;
427 }
428
429 spin_lock_irqsave(&consolelock, flags);
430 port_info[port].ack = event->xCorrelationToken;
431 spin_unlock_irqrestore(&consolelock, flags);
432}
433
434static void hvc_config_event(struct HvLpEvent *event)
435{
436 struct viocharlpevent *cevent = (struct viocharlpevent *)event;
437
438 if (cevent->data[0] == 0x01)
439 printk(KERN_INFO "hvc: window resized to %d: %d: %d: %d\n",
440 cevent->data[1], cevent->data[2],
441 cevent->data[3], cevent->data[4]);
442 else
443 printk(KERN_WARNING "hvc: unknown config event\n");
444}
445
446static void hvc_handle_event(struct HvLpEvent *event)
447{
448 int charminor;
449
450 if (event == NULL)
451 return;
452
453 charminor = event->xSubtype & VIOMINOR_SUBTYPE_MASK;
454 switch (charminor) {
455 case viocharopen:
456 hvc_open_event(event);
457 break;
458 case viocharclose:
459 hvc_close_event(event);
460 break;
461 case viochardata:
462 hvc_data_event(event);
463 break;
464 case viocharack:
465 hvc_ack_event(event);
466 break;
467 case viocharconfig:
468 hvc_config_event(event);
469 break;
470 default:
471 if (hvlpevent_is_int(event) && hvlpevent_need_ack(event)) {
472 event->xRc = HvLpEvent_Rc_InvalidSubtype;
473 HvCallEvent_ackLpEvent(event);
474 }
475 }
476}
477
478static int __init send_open(HvLpIndex remoteLp, void *sem)
479{
480 return HvCallEvent_signalLpEventFast(remoteLp,
481 HvLpEvent_Type_VirtualIo,
482 viomajorsubtype_chario | viocharopen,
483 HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
484 viopath_sourceinst(remoteLp),
485 viopath_targetinst(remoteLp),
486 (u64)(unsigned long)sem, VIOVERSION << 16,
487 0, 0, 0, 0);
488}
489
490static int __init hvc_vio_init(void)
491{
492 atomic_t wait_flag;
493 int rc;
494
495 if (!firmware_has_feature(FW_FEATURE_ISERIES))
496 return -EIO;
497
498 /* +2 for fudge */
499 rc = viopath_open(HvLpConfig_getPrimaryLpIndex(),
500 viomajorsubtype_chario, VIOCHAR_WINDOW + 2);
501 if (rc)
502 printk(KERN_WARNING "hvc: error opening to primary %d\n", rc);
503
504 if (viopath_hostLp == HvLpIndexInvalid)
505 vio_set_hostlp();
506
507 /*
508 * And if the primary is not the same as the hosting LP, open to the
509 * hosting lp
510 */
511 if ((viopath_hostLp != HvLpIndexInvalid) &&
512 (viopath_hostLp != HvLpConfig_getPrimaryLpIndex())) {
513 printk(KERN_INFO "hvc: open path to hosting (%d)\n",
514 viopath_hostLp);
515 rc = viopath_open(viopath_hostLp, viomajorsubtype_chario,
516 VIOCHAR_WINDOW + 2); /* +2 for fudge */
517 if (rc)
518 printk(KERN_WARNING
519 "error opening to partition %d: %d\n",
520 viopath_hostLp, rc);
521 }
522
523 if (vio_setHandler(viomajorsubtype_chario, hvc_handle_event) < 0)
524 printk(KERN_WARNING
525 "hvc: error seting handler for console events!\n");
526
527 /*
528 * First, try to open the console to the hosting lp.
529 * Wait on a semaphore for the response.
530 */
531 atomic_set(&wait_flag, 0);
532 if ((viopath_isactive(viopath_hostLp)) &&
533 (send_open(viopath_hostLp, &wait_flag) == 0)) {
534 printk(KERN_INFO "hvc: hosting partition %d\n", viopath_hostLp);
535 while (atomic_read(&wait_flag) == 0)
536 mb();
537 atomic_set(&wait_flag, 0);
538 }
539
540 /*
541 * If we don't have an active console, try the primary
542 */
543 if ((!viopath_isactive(port_info[0].lp)) &&
544 (viopath_isactive(HvLpConfig_getPrimaryLpIndex())) &&
545 (send_open(HvLpConfig_getPrimaryLpIndex(), &wait_flag) == 0)) {
546 printk(KERN_INFO "hvc: opening console to primary partition\n");
547 while (atomic_read(&wait_flag) == 0)
548 mb();
549 }
550
551 /* Register as a vio device to receive callbacks */
552 rc = vio_register_driver(&hvc_vio_driver);
553
554 return rc;
555}
556module_init(hvc_vio_init); /* after drivers/char/hvc_console.c */
557
558static void __exit hvc_vio_exit(void)
559{
560 vio_unregister_driver(&hvc_vio_driver);
561}
562module_exit(hvc_vio_exit);
563
564/* the device tree order defines our numbering */
565static int __init hvc_find_vtys(void)
566{
567 struct device_node *vty;
568 int num_found = 0;
569
570 for (vty = of_find_node_by_name(NULL, "vty"); vty != NULL;
571 vty = of_find_node_by_name(vty, "vty")) {
572 const uint32_t *vtermno;
573
574 /* We have statically defined space for only a certain number
575 * of console adapters.
576 */
577 if ((num_found >= MAX_NR_HVC_CONSOLES) ||
578 (num_found >= VTTY_PORTS)) {
579 of_node_put(vty);
580 break;
581 }
582
583 vtermno = of_get_property(vty, "reg", NULL);
584 if (!vtermno)
585 continue;
586
587 if (!of_device_is_compatible(vty, "IBM,iSeries-vty"))
588 continue;
589
590 if (num_found == 0)
591 add_preferred_console("hvc", 0, NULL);
592 hvc_instantiate(*vtermno, num_found, &hvc_get_put_ops);
593 ++num_found;
594 }
595
596 return num_found;
597}
598console_initcall(hvc_find_vtys);
diff --git a/drivers/char/hvc_iucv.c b/drivers/char/hvc_iucv.c
deleted file mode 100644
index 7b01bc609de3..000000000000
--- a/drivers/char/hvc_iucv.c
+++ /dev/null
@@ -1,1339 +0,0 @@
1/*
2 * hvc_iucv.c - z/VM IUCV hypervisor console (HVC) device driver
3 *
4 * This HVC device driver provides terminal access using
5 * z/VM IUCV communication paths.
6 *
7 * Copyright IBM Corp. 2008, 2009
8 *
9 * Author(s): Hendrik Brueckner <brueckner@linux.vnet.ibm.com>
10 */
11#define KMSG_COMPONENT "hvc_iucv"
12#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
13
14#include <linux/types.h>
15#include <linux/slab.h>
16#include <asm/ebcdic.h>
17#include <linux/ctype.h>
18#include <linux/delay.h>
19#include <linux/device.h>
20#include <linux/init.h>
21#include <linux/mempool.h>
22#include <linux/moduleparam.h>
23#include <linux/tty.h>
24#include <linux/wait.h>
25#include <net/iucv/iucv.h>
26
27#include "hvc_console.h"
28
29
30/* General device driver settings */
31#define HVC_IUCV_MAGIC 0xc9e4c3e5
32#define MAX_HVC_IUCV_LINES HVC_ALLOC_TTY_ADAPTERS
33#define MEMPOOL_MIN_NR (PAGE_SIZE / sizeof(struct iucv_tty_buffer)/4)
34
35/* IUCV TTY message */
36#define MSG_VERSION 0x02 /* Message version */
37#define MSG_TYPE_ERROR 0x01 /* Error message */
38#define MSG_TYPE_TERMENV 0x02 /* Terminal environment variable */
39#define MSG_TYPE_TERMIOS 0x04 /* Terminal IO struct update */
40#define MSG_TYPE_WINSIZE 0x08 /* Terminal window size update */
41#define MSG_TYPE_DATA 0x10 /* Terminal data */
42
43struct iucv_tty_msg {
44 u8 version; /* Message version */
45 u8 type; /* Message type */
46#define MSG_MAX_DATALEN ((u16)(~0))
47 u16 datalen; /* Payload length */
48 u8 data[]; /* Payload buffer */
49} __attribute__((packed));
50#define MSG_SIZE(s) ((s) + offsetof(struct iucv_tty_msg, data))
51
52enum iucv_state_t {
53 IUCV_DISCONN = 0,
54 IUCV_CONNECTED = 1,
55 IUCV_SEVERED = 2,
56};
57
58enum tty_state_t {
59 TTY_CLOSED = 0,
60 TTY_OPENED = 1,
61};
62
63struct hvc_iucv_private {
64 struct hvc_struct *hvc; /* HVC struct reference */
65 u8 srv_name[8]; /* IUCV service name (ebcdic) */
66 unsigned char is_console; /* Linux console usage flag */
67 enum iucv_state_t iucv_state; /* IUCV connection status */
68 enum tty_state_t tty_state; /* TTY status */
69 struct iucv_path *path; /* IUCV path pointer */
70 spinlock_t lock; /* hvc_iucv_private lock */
71#define SNDBUF_SIZE (PAGE_SIZE) /* must be < MSG_MAX_DATALEN */
72 void *sndbuf; /* send buffer */
73 size_t sndbuf_len; /* length of send buffer */
74#define QUEUE_SNDBUF_DELAY (HZ / 25)
75 struct delayed_work sndbuf_work; /* work: send iucv msg(s) */
76 wait_queue_head_t sndbuf_waitq; /* wait for send completion */
77 struct list_head tty_outqueue; /* outgoing IUCV messages */
78 struct list_head tty_inqueue; /* incoming IUCV messages */
79 struct device *dev; /* device structure */
80};
81
82struct iucv_tty_buffer {
83 struct list_head list; /* list pointer */
84 struct iucv_message msg; /* store an IUCV message */
85 size_t offset; /* data buffer offset */
86 struct iucv_tty_msg *mbuf; /* buffer to store input/output data */
87};
88
89/* IUCV callback handler */
90static int hvc_iucv_path_pending(struct iucv_path *, u8[8], u8[16]);
91static void hvc_iucv_path_severed(struct iucv_path *, u8[16]);
92static void hvc_iucv_msg_pending(struct iucv_path *, struct iucv_message *);
93static void hvc_iucv_msg_complete(struct iucv_path *, struct iucv_message *);
94
95
96/* Kernel module parameter: use one terminal device as default */
97static unsigned long hvc_iucv_devices = 1;
98
99/* Array of allocated hvc iucv tty lines... */
100static struct hvc_iucv_private *hvc_iucv_table[MAX_HVC_IUCV_LINES];
101#define IUCV_HVC_CON_IDX (0)
102/* List of z/VM user ID filter entries (struct iucv_vmid_filter) */
103#define MAX_VMID_FILTER (500)
104static size_t hvc_iucv_filter_size;
105static void *hvc_iucv_filter;
106static const char *hvc_iucv_filter_string;
107static DEFINE_RWLOCK(hvc_iucv_filter_lock);
108
109/* Kmem cache and mempool for iucv_tty_buffer elements */
110static struct kmem_cache *hvc_iucv_buffer_cache;
111static mempool_t *hvc_iucv_mempool;
112
113/* IUCV handler callback functions */
114static struct iucv_handler hvc_iucv_handler = {
115 .path_pending = hvc_iucv_path_pending,
116 .path_severed = hvc_iucv_path_severed,
117 .message_complete = hvc_iucv_msg_complete,
118 .message_pending = hvc_iucv_msg_pending,
119};
120
121
122/**
123 * hvc_iucv_get_private() - Return a struct hvc_iucv_private instance.
124 * @num: The HVC virtual terminal number (vtermno)
125 *
126 * This function returns the struct hvc_iucv_private instance that corresponds
127 * to the HVC virtual terminal number specified as parameter @num.
128 */
129struct hvc_iucv_private *hvc_iucv_get_private(uint32_t num)
130{
131 if ((num < HVC_IUCV_MAGIC) || (num - HVC_IUCV_MAGIC > hvc_iucv_devices))
132 return NULL;
133 return hvc_iucv_table[num - HVC_IUCV_MAGIC];
134}
135
136/**
137 * alloc_tty_buffer() - Return a new struct iucv_tty_buffer element.
138 * @size: Size of the internal buffer used to store data.
139 * @flags: Memory allocation flags passed to mempool.
140 *
141 * This function allocates a new struct iucv_tty_buffer element and, optionally,
142 * allocates an internal data buffer with the specified size @size.
143 * The internal data buffer is always allocated with GFP_DMA which is
144 * required for receiving and sending data with IUCV.
145 * Note: The total message size arises from the internal buffer size and the
146 * members of the iucv_tty_msg structure.
147 * The function returns NULL if memory allocation has failed.
148 */
149static struct iucv_tty_buffer *alloc_tty_buffer(size_t size, gfp_t flags)
150{
151 struct iucv_tty_buffer *bufp;
152
153 bufp = mempool_alloc(hvc_iucv_mempool, flags);
154 if (!bufp)
155 return NULL;
156 memset(bufp, 0, sizeof(*bufp));
157
158 if (size > 0) {
159 bufp->msg.length = MSG_SIZE(size);
160 bufp->mbuf = kmalloc(bufp->msg.length, flags | GFP_DMA);
161 if (!bufp->mbuf) {
162 mempool_free(bufp, hvc_iucv_mempool);
163 return NULL;
164 }
165 bufp->mbuf->version = MSG_VERSION;
166 bufp->mbuf->type = MSG_TYPE_DATA;
167 bufp->mbuf->datalen = (u16) size;
168 }
169 return bufp;
170}
171
172/**
173 * destroy_tty_buffer() - destroy struct iucv_tty_buffer element.
174 * @bufp: Pointer to a struct iucv_tty_buffer element, SHALL NOT be NULL.
175 */
176static void destroy_tty_buffer(struct iucv_tty_buffer *bufp)
177{
178 kfree(bufp->mbuf);
179 mempool_free(bufp, hvc_iucv_mempool);
180}
181
182/**
183 * destroy_tty_buffer_list() - call destroy_tty_buffer() for each list element.
184 * @list: List containing struct iucv_tty_buffer elements.
185 */
186static void destroy_tty_buffer_list(struct list_head *list)
187{
188 struct iucv_tty_buffer *ent, *next;
189
190 list_for_each_entry_safe(ent, next, list, list) {
191 list_del(&ent->list);
192 destroy_tty_buffer(ent);
193 }
194}
195
196/**
197 * hvc_iucv_write() - Receive IUCV message & write data to HVC buffer.
198 * @priv: Pointer to struct hvc_iucv_private
199 * @buf: HVC buffer for writing received terminal data.
200 * @count: HVC buffer size.
201 * @has_more_data: Pointer to an int variable.
202 *
203 * The function picks up pending messages from the input queue and receives
204 * the message data that is then written to the specified buffer @buf.
205 * If the buffer size @count is less than the data message size, the
206 * message is kept on the input queue and @has_more_data is set to 1.
207 * If all message data has been written, the message is removed from
208 * the input queue.
209 *
210 * The function returns the number of bytes written to the terminal, zero if
211 * there are no pending data messages available or if there is no established
212 * IUCV path.
213 * If the IUCV path has been severed, then -EPIPE is returned to cause a
214 * hang up (that is issued by the HVC layer).
215 */
216static int hvc_iucv_write(struct hvc_iucv_private *priv,
217 char *buf, int count, int *has_more_data)
218{
219 struct iucv_tty_buffer *rb;
220 int written;
221 int rc;
222
223 /* immediately return if there is no IUCV connection */
224 if (priv->iucv_state == IUCV_DISCONN)
225 return 0;
226
227 /* if the IUCV path has been severed, return -EPIPE to inform the
228 * HVC layer to hang up the tty device. */
229 if (priv->iucv_state == IUCV_SEVERED)
230 return -EPIPE;
231
232 /* check if there are pending messages */
233 if (list_empty(&priv->tty_inqueue))
234 return 0;
235
236 /* receive an iucv message and flip data to the tty (ldisc) */
237 rb = list_first_entry(&priv->tty_inqueue, struct iucv_tty_buffer, list);
238
239 written = 0;
240 if (!rb->mbuf) { /* message not yet received ... */
241 /* allocate mem to store msg data; if no memory is available
242 * then leave the buffer on the list and re-try later */
243 rb->mbuf = kmalloc(rb->msg.length, GFP_ATOMIC | GFP_DMA);
244 if (!rb->mbuf)
245 return -ENOMEM;
246
247 rc = __iucv_message_receive(priv->path, &rb->msg, 0,
248 rb->mbuf, rb->msg.length, NULL);
249 switch (rc) {
250 case 0: /* Successful */
251 break;
252 case 2: /* No message found */
253 case 9: /* Message purged */
254 break;
255 default:
256 written = -EIO;
257 }
258 /* remove buffer if an error has occured or received data
259 * is not correct */
260 if (rc || (rb->mbuf->version != MSG_VERSION) ||
261 (rb->msg.length != MSG_SIZE(rb->mbuf->datalen)))
262 goto out_remove_buffer;
263 }
264
265 switch (rb->mbuf->type) {
266 case MSG_TYPE_DATA:
267 written = min_t(int, rb->mbuf->datalen - rb->offset, count);
268 memcpy(buf, rb->mbuf->data + rb->offset, written);
269 if (written < (rb->mbuf->datalen - rb->offset)) {
270 rb->offset += written;
271 *has_more_data = 1;
272 goto out_written;
273 }
274 break;
275
276 case MSG_TYPE_WINSIZE:
277 if (rb->mbuf->datalen != sizeof(struct winsize))
278 break;
279 /* The caller must ensure that the hvc is locked, which
280 * is the case when called from hvc_iucv_get_chars() */
281 __hvc_resize(priv->hvc, *((struct winsize *) rb->mbuf->data));
282 break;
283
284 case MSG_TYPE_ERROR: /* ignored ... */
285 case MSG_TYPE_TERMENV: /* ignored ... */
286 case MSG_TYPE_TERMIOS: /* ignored ... */
287 break;
288 }
289
290out_remove_buffer:
291 list_del(&rb->list);
292 destroy_tty_buffer(rb);
293 *has_more_data = !list_empty(&priv->tty_inqueue);
294
295out_written:
296 return written;
297}
298
299/**
300 * hvc_iucv_get_chars() - HVC get_chars operation.
301 * @vtermno: HVC virtual terminal number.
302 * @buf: Pointer to a buffer to store data
303 * @count: Size of buffer available for writing
304 *
305 * The HVC thread calls this method to read characters from the back-end.
306 * If an IUCV communication path has been established, pending IUCV messages
307 * are received and data is copied into buffer @buf up to @count bytes.
308 *
309 * Locking: The routine gets called under an irqsave() spinlock; and
310 * the routine locks the struct hvc_iucv_private->lock to call
311 * helper functions.
312 */
313static int hvc_iucv_get_chars(uint32_t vtermno, char *buf, int count)
314{
315 struct hvc_iucv_private *priv = hvc_iucv_get_private(vtermno);
316 int written;
317 int has_more_data;
318
319 if (count <= 0)
320 return 0;
321
322 if (!priv)
323 return -ENODEV;
324
325 spin_lock(&priv->lock);
326 has_more_data = 0;
327 written = hvc_iucv_write(priv, buf, count, &has_more_data);
328 spin_unlock(&priv->lock);
329
330 /* if there are still messages on the queue... schedule another run */
331 if (has_more_data)
332 hvc_kick();
333
334 return written;
335}
336
337/**
338 * hvc_iucv_queue() - Buffer terminal data for sending.
339 * @priv: Pointer to struct hvc_iucv_private instance.
340 * @buf: Buffer containing data to send.
341 * @count: Size of buffer and amount of data to send.
342 *
343 * The function queues data for sending. To actually send the buffered data,
344 * a work queue function is scheduled (with QUEUE_SNDBUF_DELAY).
345 * The function returns the number of data bytes that has been buffered.
346 *
347 * If the device is not connected, data is ignored and the function returns
348 * @count.
349 * If the buffer is full, the function returns 0.
350 * If an existing IUCV communicaton path has been severed, -EPIPE is returned
351 * (that can be passed to HVC layer to cause a tty hangup).
352 */
353static int hvc_iucv_queue(struct hvc_iucv_private *priv, const char *buf,
354 int count)
355{
356 size_t len;
357
358 if (priv->iucv_state == IUCV_DISCONN)
359 return count; /* ignore data */
360
361 if (priv->iucv_state == IUCV_SEVERED)
362 return -EPIPE;
363
364 len = min_t(size_t, count, SNDBUF_SIZE - priv->sndbuf_len);
365 if (!len)
366 return 0;
367
368 memcpy(priv->sndbuf + priv->sndbuf_len, buf, len);
369 priv->sndbuf_len += len;
370
371 if (priv->iucv_state == IUCV_CONNECTED)
372 schedule_delayed_work(&priv->sndbuf_work, QUEUE_SNDBUF_DELAY);
373
374 return len;
375}
376
377/**
378 * hvc_iucv_send() - Send an IUCV message containing terminal data.
379 * @priv: Pointer to struct hvc_iucv_private instance.
380 *
381 * If an IUCV communication path has been established, the buffered output data
382 * is sent via an IUCV message and the number of bytes sent is returned.
383 * Returns 0 if there is no established IUCV communication path or
384 * -EPIPE if an existing IUCV communicaton path has been severed.
385 */
386static int hvc_iucv_send(struct hvc_iucv_private *priv)
387{
388 struct iucv_tty_buffer *sb;
389 int rc, len;
390
391 if (priv->iucv_state == IUCV_SEVERED)
392 return -EPIPE;
393
394 if (priv->iucv_state == IUCV_DISCONN)
395 return -EIO;
396
397 if (!priv->sndbuf_len)
398 return 0;
399
400 /* allocate internal buffer to store msg data and also compute total
401 * message length */
402 sb = alloc_tty_buffer(priv->sndbuf_len, GFP_ATOMIC);
403 if (!sb)
404 return -ENOMEM;
405
406 memcpy(sb->mbuf->data, priv->sndbuf, priv->sndbuf_len);
407 sb->mbuf->datalen = (u16) priv->sndbuf_len;
408 sb->msg.length = MSG_SIZE(sb->mbuf->datalen);
409
410 list_add_tail(&sb->list, &priv->tty_outqueue);
411
412 rc = __iucv_message_send(priv->path, &sb->msg, 0, 0,
413 (void *) sb->mbuf, sb->msg.length);
414 if (rc) {
415 /* drop the message here; however we might want to handle
416 * 0x03 (msg limit reached) by trying again... */
417 list_del(&sb->list);
418 destroy_tty_buffer(sb);
419 }
420 len = priv->sndbuf_len;
421 priv->sndbuf_len = 0;
422
423 return len;
424}
425
426/**
427 * hvc_iucv_sndbuf_work() - Send buffered data over IUCV
428 * @work: Work structure.
429 *
430 * This work queue function sends buffered output data over IUCV and,
431 * if not all buffered data could be sent, reschedules itself.
432 */
433static void hvc_iucv_sndbuf_work(struct work_struct *work)
434{
435 struct hvc_iucv_private *priv;
436
437 priv = container_of(work, struct hvc_iucv_private, sndbuf_work.work);
438 if (!priv)
439 return;
440
441 spin_lock_bh(&priv->lock);
442 hvc_iucv_send(priv);
443 spin_unlock_bh(&priv->lock);
444}
445
446/**
447 * hvc_iucv_put_chars() - HVC put_chars operation.
448 * @vtermno: HVC virtual terminal number.
449 * @buf: Pointer to an buffer to read data from
450 * @count: Size of buffer available for reading
451 *
452 * The HVC thread calls this method to write characters to the back-end.
453 * The function calls hvc_iucv_queue() to queue terminal data for sending.
454 *
455 * Locking: The method gets called under an irqsave() spinlock; and
456 * locks struct hvc_iucv_private->lock.
457 */
458static int hvc_iucv_put_chars(uint32_t vtermno, const char *buf, int count)
459{
460 struct hvc_iucv_private *priv = hvc_iucv_get_private(vtermno);
461 int queued;
462
463 if (count <= 0)
464 return 0;
465
466 if (!priv)
467 return -ENODEV;
468
469 spin_lock(&priv->lock);
470 queued = hvc_iucv_queue(priv, buf, count);
471 spin_unlock(&priv->lock);
472
473 return queued;
474}
475
476/**
477 * hvc_iucv_notifier_add() - HVC notifier for opening a TTY for the first time.
478 * @hp: Pointer to the HVC device (struct hvc_struct)
479 * @id: Additional data (originally passed to hvc_alloc): the index of an struct
480 * hvc_iucv_private instance.
481 *
482 * The function sets the tty state to TTY_OPENED for the struct hvc_iucv_private
483 * instance that is derived from @id. Always returns 0.
484 *
485 * Locking: struct hvc_iucv_private->lock, spin_lock_bh
486 */
487static int hvc_iucv_notifier_add(struct hvc_struct *hp, int id)
488{
489 struct hvc_iucv_private *priv;
490
491 priv = hvc_iucv_get_private(id);
492 if (!priv)
493 return 0;
494
495 spin_lock_bh(&priv->lock);
496 priv->tty_state = TTY_OPENED;
497 spin_unlock_bh(&priv->lock);
498
499 return 0;
500}
501
502/**
503 * hvc_iucv_cleanup() - Clean up and reset a z/VM IUCV HVC instance.
504 * @priv: Pointer to the struct hvc_iucv_private instance.
505 */
506static void hvc_iucv_cleanup(struct hvc_iucv_private *priv)
507{
508 destroy_tty_buffer_list(&priv->tty_outqueue);
509 destroy_tty_buffer_list(&priv->tty_inqueue);
510
511 priv->tty_state = TTY_CLOSED;
512 priv->iucv_state = IUCV_DISCONN;
513
514 priv->sndbuf_len = 0;
515}
516
517/**
518 * tty_outqueue_empty() - Test if the tty outq is empty
519 * @priv: Pointer to struct hvc_iucv_private instance.
520 */
521static inline int tty_outqueue_empty(struct hvc_iucv_private *priv)
522{
523 int rc;
524
525 spin_lock_bh(&priv->lock);
526 rc = list_empty(&priv->tty_outqueue);
527 spin_unlock_bh(&priv->lock);
528
529 return rc;
530}
531
532/**
533 * flush_sndbuf_sync() - Flush send buffer and wait for completion
534 * @priv: Pointer to struct hvc_iucv_private instance.
535 *
536 * The routine cancels a pending sndbuf work, calls hvc_iucv_send()
537 * to flush any buffered terminal output data and waits for completion.
538 */
539static void flush_sndbuf_sync(struct hvc_iucv_private *priv)
540{
541 int sync_wait;
542
543 cancel_delayed_work_sync(&priv->sndbuf_work);
544
545 spin_lock_bh(&priv->lock);
546 hvc_iucv_send(priv); /* force sending buffered data */
547 sync_wait = !list_empty(&priv->tty_outqueue); /* anything queued ? */
548 spin_unlock_bh(&priv->lock);
549
550 if (sync_wait)
551 wait_event_timeout(priv->sndbuf_waitq,
552 tty_outqueue_empty(priv), HZ/10);
553}
554
555/**
556 * hvc_iucv_hangup() - Sever IUCV path and schedule hvc tty hang up
557 * @priv: Pointer to hvc_iucv_private structure
558 *
559 * This routine severs an existing IUCV communication path and hangs
560 * up the underlying HVC terminal device.
561 * The hang-up occurs only if an IUCV communication path is established;
562 * otherwise there is no need to hang up the terminal device.
563 *
564 * The IUCV HVC hang-up is separated into two steps:
565 * 1. After the IUCV path has been severed, the iucv_state is set to
566 * IUCV_SEVERED.
567 * 2. Later, when the HVC thread calls hvc_iucv_get_chars(), the
568 * IUCV_SEVERED state causes the tty hang-up in the HVC layer.
569 *
570 * If the tty has not yet been opened, clean up the hvc_iucv_private
571 * structure to allow re-connects.
572 * If the tty has been opened, let get_chars() return -EPIPE to signal
573 * the HVC layer to hang up the tty and, if so, wake up the HVC thread
574 * to call get_chars()...
575 *
576 * Special notes on hanging up a HVC terminal instantiated as console:
577 * Hang-up: 1. do_tty_hangup() replaces file ops (= hung_up_tty_fops)
578 * 2. do_tty_hangup() calls tty->ops->close() for console_filp
579 * => no hangup notifier is called by HVC (default)
580 * 2. hvc_close() returns because of tty_hung_up_p(filp)
581 * => no delete notifier is called!
582 * Finally, the back-end is not being notified, thus, the tty session is
583 * kept active (TTY_OPEN) to be ready for re-connects.
584 *
585 * Locking: spin_lock(&priv->lock) w/o disabling bh
586 */
587static void hvc_iucv_hangup(struct hvc_iucv_private *priv)
588{
589 struct iucv_path *path;
590
591 path = NULL;
592 spin_lock(&priv->lock);
593 if (priv->iucv_state == IUCV_CONNECTED) {
594 path = priv->path;
595 priv->path = NULL;
596 priv->iucv_state = IUCV_SEVERED;
597 if (priv->tty_state == TTY_CLOSED)
598 hvc_iucv_cleanup(priv);
599 else
600 /* console is special (see above) */
601 if (priv->is_console) {
602 hvc_iucv_cleanup(priv);
603 priv->tty_state = TTY_OPENED;
604 } else
605 hvc_kick();
606 }
607 spin_unlock(&priv->lock);
608
609 /* finally sever path (outside of priv->lock due to lock ordering) */
610 if (path) {
611 iucv_path_sever(path, NULL);
612 iucv_path_free(path);
613 }
614}
615
616/**
617 * hvc_iucv_notifier_hangup() - HVC notifier for TTY hangups.
618 * @hp: Pointer to the HVC device (struct hvc_struct)
619 * @id: Additional data (originally passed to hvc_alloc):
620 * the index of an struct hvc_iucv_private instance.
621 *
622 * This routine notifies the HVC back-end that a tty hangup (carrier loss,
623 * virtual or otherwise) has occured.
624 * The z/VM IUCV HVC device driver ignores virtual hangups (vhangup())
625 * to keep an existing IUCV communication path established.
626 * (Background: vhangup() is called from user space (by getty or login) to
627 * disable writing to the tty by other applications).
628 * If the tty has been opened and an established IUCV path has been severed
629 * (we caused the tty hangup), the function calls hvc_iucv_cleanup().
630 *
631 * Locking: struct hvc_iucv_private->lock
632 */
633static void hvc_iucv_notifier_hangup(struct hvc_struct *hp, int id)
634{
635 struct hvc_iucv_private *priv;
636
637 priv = hvc_iucv_get_private(id);
638 if (!priv)
639 return;
640
641 flush_sndbuf_sync(priv);
642
643 spin_lock_bh(&priv->lock);
644 /* NOTE: If the hangup was scheduled by ourself (from the iucv
645 * path_servered callback [IUCV_SEVERED]), we have to clean up
646 * our structure and to set state to TTY_CLOSED.
647 * If the tty was hung up otherwise (e.g. vhangup()), then we
648 * ignore this hangup and keep an established IUCV path open...
649 * (...the reason is that we are not able to connect back to the
650 * client if we disconnect on hang up) */
651 priv->tty_state = TTY_CLOSED;
652
653 if (priv->iucv_state == IUCV_SEVERED)
654 hvc_iucv_cleanup(priv);
655 spin_unlock_bh(&priv->lock);
656}
657
658/**
659 * hvc_iucv_notifier_del() - HVC notifier for closing a TTY for the last time.
660 * @hp: Pointer to the HVC device (struct hvc_struct)
661 * @id: Additional data (originally passed to hvc_alloc):
662 * the index of an struct hvc_iucv_private instance.
663 *
664 * This routine notifies the HVC back-end that the last tty device fd has been
665 * closed. The function calls hvc_iucv_cleanup() to clean up the struct
666 * hvc_iucv_private instance.
667 *
668 * Locking: struct hvc_iucv_private->lock
669 */
670static void hvc_iucv_notifier_del(struct hvc_struct *hp, int id)
671{
672 struct hvc_iucv_private *priv;
673 struct iucv_path *path;
674
675 priv = hvc_iucv_get_private(id);
676 if (!priv)
677 return;
678
679 flush_sndbuf_sync(priv);
680
681 spin_lock_bh(&priv->lock);
682 path = priv->path; /* save reference to IUCV path */
683 priv->path = NULL;
684 hvc_iucv_cleanup(priv);
685 spin_unlock_bh(&priv->lock);
686
687 /* sever IUCV path outside of priv->lock due to lock ordering of:
688 * priv->lock <--> iucv_table_lock */
689 if (path) {
690 iucv_path_sever(path, NULL);
691 iucv_path_free(path);
692 }
693}
694
695/**
696 * hvc_iucv_filter_connreq() - Filter connection request based on z/VM user ID
697 * @ipvmid: Originating z/VM user ID (right padded with blanks)
698 *
699 * Returns 0 if the z/VM user ID @ipvmid is allowed to connection, otherwise
700 * non-zero.
701 */
702static int hvc_iucv_filter_connreq(u8 ipvmid[8])
703{
704 size_t i;
705
706 /* Note: default policy is ACCEPT if no filter is set */
707 if (!hvc_iucv_filter_size)
708 return 0;
709
710 for (i = 0; i < hvc_iucv_filter_size; i++)
711 if (0 == memcmp(ipvmid, hvc_iucv_filter + (8 * i), 8))
712 return 0;
713 return 1;
714}
715
716/**
717 * hvc_iucv_path_pending() - IUCV handler to process a connection request.
718 * @path: Pending path (struct iucv_path)
719 * @ipvmid: z/VM system identifier of originator
720 * @ipuser: User specified data for this path
721 * (AF_IUCV: port/service name and originator port)
722 *
723 * The function uses the @ipuser data to determine if the pending path belongs
724 * to a terminal managed by this device driver.
725 * If the path belongs to this driver, ensure that the terminal is not accessed
726 * multiple times (only one connection to a terminal is allowed).
727 * If the terminal is not yet connected, the pending path is accepted and is
728 * associated to the appropriate struct hvc_iucv_private instance.
729 *
730 * Returns 0 if @path belongs to a terminal managed by the this device driver;
731 * otherwise returns -ENODEV in order to dispatch this path to other handlers.
732 *
733 * Locking: struct hvc_iucv_private->lock
734 */
735static int hvc_iucv_path_pending(struct iucv_path *path,
736 u8 ipvmid[8], u8 ipuser[16])
737{
738 struct hvc_iucv_private *priv;
739 u8 nuser_data[16];
740 u8 vm_user_id[9];
741 int i, rc;
742
743 priv = NULL;
744 for (i = 0; i < hvc_iucv_devices; i++)
745 if (hvc_iucv_table[i] &&
746 (0 == memcmp(hvc_iucv_table[i]->srv_name, ipuser, 8))) {
747 priv = hvc_iucv_table[i];
748 break;
749 }
750 if (!priv)
751 return -ENODEV;
752
753 /* Enforce that ipvmid is allowed to connect to us */
754 read_lock(&hvc_iucv_filter_lock);
755 rc = hvc_iucv_filter_connreq(ipvmid);
756 read_unlock(&hvc_iucv_filter_lock);
757 if (rc) {
758 iucv_path_sever(path, ipuser);
759 iucv_path_free(path);
760 memcpy(vm_user_id, ipvmid, 8);
761 vm_user_id[8] = 0;
762 pr_info("A connection request from z/VM user ID %s "
763 "was refused\n", vm_user_id);
764 return 0;
765 }
766
767 spin_lock(&priv->lock);
768
769 /* If the terminal is already connected or being severed, then sever
770 * this path to enforce that there is only ONE established communication
771 * path per terminal. */
772 if (priv->iucv_state != IUCV_DISCONN) {
773 iucv_path_sever(path, ipuser);
774 iucv_path_free(path);
775 goto out_path_handled;
776 }
777
778 /* accept path */
779 memcpy(nuser_data, ipuser + 8, 8); /* remote service (for af_iucv) */
780 memcpy(nuser_data + 8, ipuser, 8); /* local service (for af_iucv) */
781 path->msglim = 0xffff; /* IUCV MSGLIMIT */
782 path->flags &= ~IUCV_IPRMDATA; /* TODO: use IUCV_IPRMDATA */
783 rc = iucv_path_accept(path, &hvc_iucv_handler, nuser_data, priv);
784 if (rc) {
785 iucv_path_sever(path, ipuser);
786 iucv_path_free(path);
787 goto out_path_handled;
788 }
789 priv->path = path;
790 priv->iucv_state = IUCV_CONNECTED;
791
792 /* flush buffered output data... */
793 schedule_delayed_work(&priv->sndbuf_work, 5);
794
795out_path_handled:
796 spin_unlock(&priv->lock);
797 return 0;
798}
799
800/**
801 * hvc_iucv_path_severed() - IUCV handler to process a path sever.
802 * @path: Pending path (struct iucv_path)
803 * @ipuser: User specified data for this path
804 * (AF_IUCV: port/service name and originator port)
805 *
806 * This function calls the hvc_iucv_hangup() function for the
807 * respective IUCV HVC terminal.
808 *
809 * Locking: struct hvc_iucv_private->lock
810 */
811static void hvc_iucv_path_severed(struct iucv_path *path, u8 ipuser[16])
812{
813 struct hvc_iucv_private *priv = path->private;
814
815 hvc_iucv_hangup(priv);
816}
817
818/**
819 * hvc_iucv_msg_pending() - IUCV handler to process an incoming IUCV message.
820 * @path: Pending path (struct iucv_path)
821 * @msg: Pointer to the IUCV message
822 *
823 * The function puts an incoming message on the input queue for later
824 * processing (by hvc_iucv_get_chars() / hvc_iucv_write()).
825 * If the tty has not yet been opened, the message is rejected.
826 *
827 * Locking: struct hvc_iucv_private->lock
828 */
829static void hvc_iucv_msg_pending(struct iucv_path *path,
830 struct iucv_message *msg)
831{
832 struct hvc_iucv_private *priv = path->private;
833 struct iucv_tty_buffer *rb;
834
835 /* reject messages that exceed max size of iucv_tty_msg->datalen */
836 if (msg->length > MSG_SIZE(MSG_MAX_DATALEN)) {
837 iucv_message_reject(path, msg);
838 return;
839 }
840
841 spin_lock(&priv->lock);
842
843 /* reject messages if tty has not yet been opened */
844 if (priv->tty_state == TTY_CLOSED) {
845 iucv_message_reject(path, msg);
846 goto unlock_return;
847 }
848
849 /* allocate tty buffer to save iucv msg only */
850 rb = alloc_tty_buffer(0, GFP_ATOMIC);
851 if (!rb) {
852 iucv_message_reject(path, msg);
853 goto unlock_return; /* -ENOMEM */
854 }
855 rb->msg = *msg;
856
857 list_add_tail(&rb->list, &priv->tty_inqueue);
858
859 hvc_kick(); /* wake up hvc thread */
860
861unlock_return:
862 spin_unlock(&priv->lock);
863}
864
865/**
866 * hvc_iucv_msg_complete() - IUCV handler to process message completion
867 * @path: Pending path (struct iucv_path)
868 * @msg: Pointer to the IUCV message
869 *
870 * The function is called upon completion of message delivery to remove the
871 * message from the outqueue. Additional delivery information can be found
872 * msg->audit: rejected messages (0x040000 (IPADRJCT)), and
873 * purged messages (0x010000 (IPADPGNR)).
874 *
875 * Locking: struct hvc_iucv_private->lock
876 */
877static void hvc_iucv_msg_complete(struct iucv_path *path,
878 struct iucv_message *msg)
879{
880 struct hvc_iucv_private *priv = path->private;
881 struct iucv_tty_buffer *ent, *next;
882 LIST_HEAD(list_remove);
883
884 spin_lock(&priv->lock);
885 list_for_each_entry_safe(ent, next, &priv->tty_outqueue, list)
886 if (ent->msg.id == msg->id) {
887 list_move(&ent->list, &list_remove);
888 break;
889 }
890 wake_up(&priv->sndbuf_waitq);
891 spin_unlock(&priv->lock);
892 destroy_tty_buffer_list(&list_remove);
893}
894
895/**
896 * hvc_iucv_pm_freeze() - Freeze PM callback
897 * @dev: IUVC HVC terminal device
898 *
899 * Sever an established IUCV communication path and
900 * trigger a hang-up of the underlying HVC terminal.
901 */
902static int hvc_iucv_pm_freeze(struct device *dev)
903{
904 struct hvc_iucv_private *priv = dev_get_drvdata(dev);
905
906 local_bh_disable();
907 hvc_iucv_hangup(priv);
908 local_bh_enable();
909
910 return 0;
911}
912
913/**
914 * hvc_iucv_pm_restore_thaw() - Thaw and restore PM callback
915 * @dev: IUVC HVC terminal device
916 *
917 * Wake up the HVC thread to trigger hang-up and respective
918 * HVC back-end notifier invocations.
919 */
920static int hvc_iucv_pm_restore_thaw(struct device *dev)
921{
922 hvc_kick();
923 return 0;
924}
925
926
927/* HVC operations */
928static const struct hv_ops hvc_iucv_ops = {
929 .get_chars = hvc_iucv_get_chars,
930 .put_chars = hvc_iucv_put_chars,
931 .notifier_add = hvc_iucv_notifier_add,
932 .notifier_del = hvc_iucv_notifier_del,
933 .notifier_hangup = hvc_iucv_notifier_hangup,
934};
935
936/* Suspend / resume device operations */
937static const struct dev_pm_ops hvc_iucv_pm_ops = {
938 .freeze = hvc_iucv_pm_freeze,
939 .thaw = hvc_iucv_pm_restore_thaw,
940 .restore = hvc_iucv_pm_restore_thaw,
941};
942
943/* IUCV HVC device driver */
944static struct device_driver hvc_iucv_driver = {
945 .name = KMSG_COMPONENT,
946 .bus = &iucv_bus,
947 .pm = &hvc_iucv_pm_ops,
948};
949
950/**
951 * hvc_iucv_alloc() - Allocates a new struct hvc_iucv_private instance
952 * @id: hvc_iucv_table index
953 * @is_console: Flag if the instance is used as Linux console
954 *
955 * This function allocates a new hvc_iucv_private structure and stores
956 * the instance in hvc_iucv_table at index @id.
957 * Returns 0 on success; otherwise non-zero.
958 */
959static int __init hvc_iucv_alloc(int id, unsigned int is_console)
960{
961 struct hvc_iucv_private *priv;
962 char name[9];
963 int rc;
964
965 priv = kzalloc(sizeof(struct hvc_iucv_private), GFP_KERNEL);
966 if (!priv)
967 return -ENOMEM;
968
969 spin_lock_init(&priv->lock);
970 INIT_LIST_HEAD(&priv->tty_outqueue);
971 INIT_LIST_HEAD(&priv->tty_inqueue);
972 INIT_DELAYED_WORK(&priv->sndbuf_work, hvc_iucv_sndbuf_work);
973 init_waitqueue_head(&priv->sndbuf_waitq);
974
975 priv->sndbuf = (void *) get_zeroed_page(GFP_KERNEL);
976 if (!priv->sndbuf) {
977 kfree(priv);
978 return -ENOMEM;
979 }
980
981 /* set console flag */
982 priv->is_console = is_console;
983
984 /* allocate hvc device */
985 priv->hvc = hvc_alloc(HVC_IUCV_MAGIC + id, /* PAGE_SIZE */
986 HVC_IUCV_MAGIC + id, &hvc_iucv_ops, 256);
987 if (IS_ERR(priv->hvc)) {
988 rc = PTR_ERR(priv->hvc);
989 goto out_error_hvc;
990 }
991
992 /* notify HVC thread instead of using polling */
993 priv->hvc->irq_requested = 1;
994
995 /* setup iucv related information */
996 snprintf(name, 9, "lnxhvc%-2d", id);
997 memcpy(priv->srv_name, name, 8);
998 ASCEBC(priv->srv_name, 8);
999
1000 /* create and setup device */
1001 priv->dev = kzalloc(sizeof(*priv->dev), GFP_KERNEL);
1002 if (!priv->dev) {
1003 rc = -ENOMEM;
1004 goto out_error_dev;
1005 }
1006 dev_set_name(priv->dev, "hvc_iucv%d", id);
1007 dev_set_drvdata(priv->dev, priv);
1008 priv->dev->bus = &iucv_bus;
1009 priv->dev->parent = iucv_root;
1010 priv->dev->driver = &hvc_iucv_driver;
1011 priv->dev->release = (void (*)(struct device *)) kfree;
1012 rc = device_register(priv->dev);
1013 if (rc) {
1014 put_device(priv->dev);
1015 goto out_error_dev;
1016 }
1017
1018 hvc_iucv_table[id] = priv;
1019 return 0;
1020
1021out_error_dev:
1022 hvc_remove(priv->hvc);
1023out_error_hvc:
1024 free_page((unsigned long) priv->sndbuf);
1025 kfree(priv);
1026
1027 return rc;
1028}
1029
1030/**
1031 * hvc_iucv_destroy() - Destroy and free hvc_iucv_private instances
1032 */
1033static void __init hvc_iucv_destroy(struct hvc_iucv_private *priv)
1034{
1035 hvc_remove(priv->hvc);
1036 device_unregister(priv->dev);
1037 free_page((unsigned long) priv->sndbuf);
1038 kfree(priv);
1039}
1040
1041/**
1042 * hvc_iucv_parse_filter() - Parse filter for a single z/VM user ID
1043 * @filter: String containing a comma-separated list of z/VM user IDs
1044 */
1045static const char *hvc_iucv_parse_filter(const char *filter, char *dest)
1046{
1047 const char *nextdelim, *residual;
1048 size_t len;
1049
1050 nextdelim = strchr(filter, ',');
1051 if (nextdelim) {
1052 len = nextdelim - filter;
1053 residual = nextdelim + 1;
1054 } else {
1055 len = strlen(filter);
1056 residual = filter + len;
1057 }
1058
1059 if (len == 0)
1060 return ERR_PTR(-EINVAL);
1061
1062 /* check for '\n' (if called from sysfs) */
1063 if (filter[len - 1] == '\n')
1064 len--;
1065
1066 if (len > 8)
1067 return ERR_PTR(-EINVAL);
1068
1069 /* pad with blanks and save upper case version of user ID */
1070 memset(dest, ' ', 8);
1071 while (len--)
1072 dest[len] = toupper(filter[len]);
1073 return residual;
1074}
1075
1076/**
1077 * hvc_iucv_setup_filter() - Set up z/VM user ID filter
1078 * @filter: String consisting of a comma-separated list of z/VM user IDs
1079 *
1080 * The function parses the @filter string and creates an array containing
1081 * the list of z/VM user ID filter entries.
1082 * Return code 0 means success, -EINVAL if the filter is syntactically
1083 * incorrect, -ENOMEM if there was not enough memory to allocate the
1084 * filter list array, or -ENOSPC if too many z/VM user IDs have been specified.
1085 */
1086static int hvc_iucv_setup_filter(const char *val)
1087{
1088 const char *residual;
1089 int err;
1090 size_t size, count;
1091 void *array, *old_filter;
1092
1093 count = strlen(val);
1094 if (count == 0 || (count == 1 && val[0] == '\n')) {
1095 size = 0;
1096 array = NULL;
1097 goto out_replace_filter; /* clear filter */
1098 }
1099
1100 /* count user IDs in order to allocate sufficient memory */
1101 size = 1;
1102 residual = val;
1103 while ((residual = strchr(residual, ',')) != NULL) {
1104 residual++;
1105 size++;
1106 }
1107
1108 /* check if the specified list exceeds the filter limit */
1109 if (size > MAX_VMID_FILTER)
1110 return -ENOSPC;
1111
1112 array = kzalloc(size * 8, GFP_KERNEL);
1113 if (!array)
1114 return -ENOMEM;
1115
1116 count = size;
1117 residual = val;
1118 while (*residual && count) {
1119 residual = hvc_iucv_parse_filter(residual,
1120 array + ((size - count) * 8));
1121 if (IS_ERR(residual)) {
1122 err = PTR_ERR(residual);
1123 kfree(array);
1124 goto out_err;
1125 }
1126 count--;
1127 }
1128
1129out_replace_filter:
1130 write_lock_bh(&hvc_iucv_filter_lock);
1131 old_filter = hvc_iucv_filter;
1132 hvc_iucv_filter_size = size;
1133 hvc_iucv_filter = array;
1134 write_unlock_bh(&hvc_iucv_filter_lock);
1135 kfree(old_filter);
1136
1137 err = 0;
1138out_err:
1139 return err;
1140}
1141
1142/**
1143 * param_set_vmidfilter() - Set z/VM user ID filter parameter
1144 * @val: String consisting of a comma-separated list of z/VM user IDs
1145 * @kp: Kernel parameter pointing to hvc_iucv_filter array
1146 *
1147 * The function sets up the z/VM user ID filter specified as comma-separated
1148 * list of user IDs in @val.
1149 * Note: If it is called early in the boot process, @val is stored and
1150 * parsed later in hvc_iucv_init().
1151 */
1152static int param_set_vmidfilter(const char *val, const struct kernel_param *kp)
1153{
1154 int rc;
1155
1156 if (!MACHINE_IS_VM || !hvc_iucv_devices)
1157 return -ENODEV;
1158
1159 if (!val)
1160 return -EINVAL;
1161
1162 rc = 0;
1163 if (slab_is_available())
1164 rc = hvc_iucv_setup_filter(val);
1165 else
1166 hvc_iucv_filter_string = val; /* defer... */
1167 return rc;
1168}
1169
1170/**
1171 * param_get_vmidfilter() - Get z/VM user ID filter
1172 * @buffer: Buffer to store z/VM user ID filter,
1173 * (buffer size assumption PAGE_SIZE)
1174 * @kp: Kernel parameter pointing to the hvc_iucv_filter array
1175 *
1176 * The function stores the filter as a comma-separated list of z/VM user IDs
1177 * in @buffer. Typically, sysfs routines call this function for attr show.
1178 */
1179static int param_get_vmidfilter(char *buffer, const struct kernel_param *kp)
1180{
1181 int rc;
1182 size_t index, len;
1183 void *start, *end;
1184
1185 if (!MACHINE_IS_VM || !hvc_iucv_devices)
1186 return -ENODEV;
1187
1188 rc = 0;
1189 read_lock_bh(&hvc_iucv_filter_lock);
1190 for (index = 0; index < hvc_iucv_filter_size; index++) {
1191 start = hvc_iucv_filter + (8 * index);
1192 end = memchr(start, ' ', 8);
1193 len = (end) ? end - start : 8;
1194 memcpy(buffer + rc, start, len);
1195 rc += len;
1196 buffer[rc++] = ',';
1197 }
1198 read_unlock_bh(&hvc_iucv_filter_lock);
1199 if (rc)
1200 buffer[--rc] = '\0'; /* replace last comma and update rc */
1201 return rc;
1202}
1203
1204#define param_check_vmidfilter(name, p) __param_check(name, p, void)
1205
1206static struct kernel_param_ops param_ops_vmidfilter = {
1207 .set = param_set_vmidfilter,
1208 .get = param_get_vmidfilter,
1209};
1210
1211/**
1212 * hvc_iucv_init() - z/VM IUCV HVC device driver initialization
1213 */
1214static int __init hvc_iucv_init(void)
1215{
1216 int rc;
1217 unsigned int i;
1218
1219 if (!hvc_iucv_devices)
1220 return -ENODEV;
1221
1222 if (!MACHINE_IS_VM) {
1223 pr_notice("The z/VM IUCV HVC device driver cannot "
1224 "be used without z/VM\n");
1225 rc = -ENODEV;
1226 goto out_error;
1227 }
1228
1229 if (hvc_iucv_devices > MAX_HVC_IUCV_LINES) {
1230 pr_err("%lu is not a valid value for the hvc_iucv= "
1231 "kernel parameter\n", hvc_iucv_devices);
1232 rc = -EINVAL;
1233 goto out_error;
1234 }
1235
1236 /* register IUCV HVC device driver */
1237 rc = driver_register(&hvc_iucv_driver);
1238 if (rc)
1239 goto out_error;
1240
1241 /* parse hvc_iucv_allow string and create z/VM user ID filter list */
1242 if (hvc_iucv_filter_string) {
1243 rc = hvc_iucv_setup_filter(hvc_iucv_filter_string);
1244 switch (rc) {
1245 case 0:
1246 break;
1247 case -ENOMEM:
1248 pr_err("Allocating memory failed with "
1249 "reason code=%d\n", 3);
1250 goto out_error;
1251 case -EINVAL:
1252 pr_err("hvc_iucv_allow= does not specify a valid "
1253 "z/VM user ID list\n");
1254 goto out_error;
1255 case -ENOSPC:
1256 pr_err("hvc_iucv_allow= specifies too many "
1257 "z/VM user IDs\n");
1258 goto out_error;
1259 default:
1260 goto out_error;
1261 }
1262 }
1263
1264 hvc_iucv_buffer_cache = kmem_cache_create(KMSG_COMPONENT,
1265 sizeof(struct iucv_tty_buffer),
1266 0, 0, NULL);
1267 if (!hvc_iucv_buffer_cache) {
1268 pr_err("Allocating memory failed with reason code=%d\n", 1);
1269 rc = -ENOMEM;
1270 goto out_error;
1271 }
1272
1273 hvc_iucv_mempool = mempool_create_slab_pool(MEMPOOL_MIN_NR,
1274 hvc_iucv_buffer_cache);
1275 if (!hvc_iucv_mempool) {
1276 pr_err("Allocating memory failed with reason code=%d\n", 2);
1277 kmem_cache_destroy(hvc_iucv_buffer_cache);
1278 rc = -ENOMEM;
1279 goto out_error;
1280 }
1281
1282 /* register the first terminal device as console
1283 * (must be done before allocating hvc terminal devices) */
1284 rc = hvc_instantiate(HVC_IUCV_MAGIC, IUCV_HVC_CON_IDX, &hvc_iucv_ops);
1285 if (rc) {
1286 pr_err("Registering HVC terminal device as "
1287 "Linux console failed\n");
1288 goto out_error_memory;
1289 }
1290
1291 /* allocate hvc_iucv_private structs */
1292 for (i = 0; i < hvc_iucv_devices; i++) {
1293 rc = hvc_iucv_alloc(i, (i == IUCV_HVC_CON_IDX) ? 1 : 0);
1294 if (rc) {
1295 pr_err("Creating a new HVC terminal device "
1296 "failed with error code=%d\n", rc);
1297 goto out_error_hvc;
1298 }
1299 }
1300
1301 /* register IUCV callback handler */
1302 rc = iucv_register(&hvc_iucv_handler, 0);
1303 if (rc) {
1304 pr_err("Registering IUCV handlers failed with error code=%d\n",
1305 rc);
1306 goto out_error_iucv;
1307 }
1308
1309 return 0;
1310
1311out_error_iucv:
1312 iucv_unregister(&hvc_iucv_handler, 0);
1313out_error_hvc:
1314 for (i = 0; i < hvc_iucv_devices; i++)
1315 if (hvc_iucv_table[i])
1316 hvc_iucv_destroy(hvc_iucv_table[i]);
1317out_error_memory:
1318 mempool_destroy(hvc_iucv_mempool);
1319 kmem_cache_destroy(hvc_iucv_buffer_cache);
1320out_error:
1321 if (hvc_iucv_filter)
1322 kfree(hvc_iucv_filter);
1323 hvc_iucv_devices = 0; /* ensure that we do not provide any device */
1324 return rc;
1325}
1326
1327/**
1328 * hvc_iucv_config() - Parsing of hvc_iucv= kernel command line parameter
1329 * @val: Parameter value (numeric)
1330 */
1331static int __init hvc_iucv_config(char *val)
1332{
1333 return strict_strtoul(val, 10, &hvc_iucv_devices);
1334}
1335
1336
1337device_initcall(hvc_iucv_init);
1338__setup("hvc_iucv=", hvc_iucv_config);
1339core_param(hvc_iucv_allow, hvc_iucv_filter, vmidfilter, 0640);
diff --git a/drivers/char/hvc_rtas.c b/drivers/char/hvc_rtas.c
deleted file mode 100644
index 61c4a61558d9..000000000000
--- a/drivers/char/hvc_rtas.c
+++ /dev/null
@@ -1,133 +0,0 @@
1/*
2 * IBM RTAS driver interface to hvc_console.c
3 *
4 * (C) Copyright IBM Corporation 2001-2005
5 * (C) Copyright Red Hat, Inc. 2005
6 *
7 * Author(s): Maximino Augilar <IBM STI Design Center>
8 * : Ryan S. Arnold <rsa@us.ibm.com>
9 * : Utz Bacher <utz.bacher@de.ibm.com>
10 * : David Woodhouse <dwmw2@infradead.org>
11 *
12 * inspired by drivers/char/hvc_console.c
13 * written by Anton Blanchard and Paul Mackerras
14 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 */
29
30#include <linux/console.h>
31#include <linux/delay.h>
32#include <linux/err.h>
33#include <linux/init.h>
34#include <linux/moduleparam.h>
35#include <linux/types.h>
36
37#include <asm/irq.h>
38#include <asm/rtas.h>
39#include "hvc_console.h"
40
41#define hvc_rtas_cookie 0x67781e15
42struct hvc_struct *hvc_rtas_dev;
43
44static int rtascons_put_char_token = RTAS_UNKNOWN_SERVICE;
45static int rtascons_get_char_token = RTAS_UNKNOWN_SERVICE;
46
47static inline int hvc_rtas_write_console(uint32_t vtermno, const char *buf,
48 int count)
49{
50 int i;
51
52 for (i = 0; i < count; i++) {
53 if (rtas_call(rtascons_put_char_token, 1, 1, NULL, buf[i]))
54 break;
55 }
56
57 return i;
58}
59
60static int hvc_rtas_read_console(uint32_t vtermno, char *buf, int count)
61{
62 int i, c;
63
64 for (i = 0; i < count; i++) {
65 if (rtas_call(rtascons_get_char_token, 0, 2, &c))
66 break;
67
68 buf[i] = c;
69 }
70
71 return i;
72}
73
74static const struct hv_ops hvc_rtas_get_put_ops = {
75 .get_chars = hvc_rtas_read_console,
76 .put_chars = hvc_rtas_write_console,
77};
78
79static int __init hvc_rtas_init(void)
80{
81 struct hvc_struct *hp;
82
83 if (rtascons_put_char_token == RTAS_UNKNOWN_SERVICE)
84 rtascons_put_char_token = rtas_token("put-term-char");
85 if (rtascons_put_char_token == RTAS_UNKNOWN_SERVICE)
86 return -EIO;
87
88 if (rtascons_get_char_token == RTAS_UNKNOWN_SERVICE)
89 rtascons_get_char_token = rtas_token("get-term-char");
90 if (rtascons_get_char_token == RTAS_UNKNOWN_SERVICE)
91 return -EIO;
92
93 BUG_ON(hvc_rtas_dev);
94
95 /* Allocate an hvc_struct for the console device we instantiated
96 * earlier. Save off hp so that we can return it on exit */
97 hp = hvc_alloc(hvc_rtas_cookie, NO_IRQ, &hvc_rtas_get_put_ops, 16);
98 if (IS_ERR(hp))
99 return PTR_ERR(hp);
100
101 hvc_rtas_dev = hp;
102
103 return 0;
104}
105module_init(hvc_rtas_init);
106
107/* This will tear down the tty portion of the driver */
108static void __exit hvc_rtas_exit(void)
109{
110 /* Really the fun isn't over until the worker thread breaks down and
111 * the tty cleans up */
112 if (hvc_rtas_dev)
113 hvc_remove(hvc_rtas_dev);
114}
115module_exit(hvc_rtas_exit);
116
117/* This will happen prior to module init. There is no tty at this time? */
118static int __init hvc_rtas_console_init(void)
119{
120 rtascons_put_char_token = rtas_token("put-term-char");
121 if (rtascons_put_char_token == RTAS_UNKNOWN_SERVICE)
122 return -EIO;
123
124 rtascons_get_char_token = rtas_token("get-term-char");
125 if (rtascons_get_char_token == RTAS_UNKNOWN_SERVICE)
126 return -EIO;
127
128 hvc_instantiate(hvc_rtas_cookie, 0, &hvc_rtas_get_put_ops);
129 add_preferred_console("hvc", 0, NULL);
130
131 return 0;
132}
133console_initcall(hvc_rtas_console_init);
diff --git a/drivers/char/hvc_tile.c b/drivers/char/hvc_tile.c
deleted file mode 100644
index c4efb55cbc03..000000000000
--- a/drivers/char/hvc_tile.c
+++ /dev/null
@@ -1,67 +0,0 @@
1/*
2 * Copyright 2010 Tilera Corporation. All Rights Reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation, version 2.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
11 * NON INFRINGEMENT. See the GNU General Public License for
12 * more details.
13 *
14 * Tilera TILE Processor hypervisor console
15 */
16
17#include <linux/console.h>
18#include <linux/delay.h>
19#include <linux/err.h>
20#include <linux/init.h>
21#include <linux/moduleparam.h>
22#include <linux/types.h>
23
24#include <hv/hypervisor.h>
25
26#include "hvc_console.h"
27
28static int hvc_tile_put_chars(uint32_t vt, const char *buf, int count)
29{
30 return hv_console_write((HV_VirtAddr)buf, count);
31}
32
33static int hvc_tile_get_chars(uint32_t vt, char *buf, int count)
34{
35 int i, c;
36
37 for (i = 0; i < count; ++i) {
38 c = hv_console_read_if_ready();
39 if (c < 0)
40 break;
41 buf[i] = c;
42 }
43
44 return i;
45}
46
47static const struct hv_ops hvc_tile_get_put_ops = {
48 .get_chars = hvc_tile_get_chars,
49 .put_chars = hvc_tile_put_chars,
50};
51
52static int __init hvc_tile_console_init(void)
53{
54 extern void disable_early_printk(void);
55 hvc_instantiate(0, 0, &hvc_tile_get_put_ops);
56 add_preferred_console("hvc", 0, NULL);
57 disable_early_printk();
58 return 0;
59}
60console_initcall(hvc_tile_console_init);
61
62static int __init hvc_tile_init(void)
63{
64 hvc_alloc(0, 0, &hvc_tile_get_put_ops, 128);
65 return 0;
66}
67device_initcall(hvc_tile_init);
diff --git a/drivers/char/hvc_udbg.c b/drivers/char/hvc_udbg.c
deleted file mode 100644
index b0957e61a7be..000000000000
--- a/drivers/char/hvc_udbg.c
+++ /dev/null
@@ -1,96 +0,0 @@
1/*
2 * udbg interface to hvc_console.c
3 *
4 * (C) Copyright David Gibson, IBM Corporation 2008.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21#include <linux/console.h>
22#include <linux/delay.h>
23#include <linux/err.h>
24#include <linux/init.h>
25#include <linux/moduleparam.h>
26#include <linux/types.h>
27#include <linux/irq.h>
28
29#include <asm/udbg.h>
30
31#include "hvc_console.h"
32
33struct hvc_struct *hvc_udbg_dev;
34
35static int hvc_udbg_put(uint32_t vtermno, const char *buf, int count)
36{
37 int i;
38
39 for (i = 0; i < count; i++)
40 udbg_putc(buf[i]);
41
42 return i;
43}
44
45static int hvc_udbg_get(uint32_t vtermno, char *buf, int count)
46{
47 int i, c;
48
49 if (!udbg_getc_poll)
50 return 0;
51
52 for (i = 0; i < count; i++) {
53 if ((c = udbg_getc_poll()) == -1)
54 break;
55 buf[i] = c;
56 }
57
58 return i;
59}
60
61static const struct hv_ops hvc_udbg_ops = {
62 .get_chars = hvc_udbg_get,
63 .put_chars = hvc_udbg_put,
64};
65
66static int __init hvc_udbg_init(void)
67{
68 struct hvc_struct *hp;
69
70 BUG_ON(hvc_udbg_dev);
71
72 hp = hvc_alloc(0, NO_IRQ, &hvc_udbg_ops, 16);
73 if (IS_ERR(hp))
74 return PTR_ERR(hp);
75
76 hvc_udbg_dev = hp;
77
78 return 0;
79}
80module_init(hvc_udbg_init);
81
82static void __exit hvc_udbg_exit(void)
83{
84 if (hvc_udbg_dev)
85 hvc_remove(hvc_udbg_dev);
86}
87module_exit(hvc_udbg_exit);
88
89static int __init hvc_udbg_console_init(void)
90{
91 hvc_instantiate(0, 0, &hvc_udbg_ops);
92 add_preferred_console("hvc", 0, NULL);
93
94 return 0;
95}
96console_initcall(hvc_udbg_console_init);
diff --git a/drivers/char/hvc_vio.c b/drivers/char/hvc_vio.c
deleted file mode 100644
index 27370e99c66f..000000000000
--- a/drivers/char/hvc_vio.c
+++ /dev/null
@@ -1,173 +0,0 @@
1/*
2 * vio driver interface to hvc_console.c
3 *
4 * This code was moved here to allow the remaing code to be reused as a
5 * generic polling mode with semi-reliable transport driver core to the
6 * console and tty subsystems.
7 *
8 *
9 * Copyright (C) 2001 Anton Blanchard <anton@au.ibm.com>, IBM
10 * Copyright (C) 2001 Paul Mackerras <paulus@au.ibm.com>, IBM
11 * Copyright (C) 2004 Benjamin Herrenschmidt <benh@kernel.crashing.org>, IBM Corp.
12 * Copyright (C) 2004 IBM Corporation
13 *
14 * Additional Author(s):
15 * Ryan S. Arnold <rsa@us.ibm.com>
16 *
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
21 *
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 */
31
32#include <linux/types.h>
33#include <linux/init.h>
34
35#include <asm/hvconsole.h>
36#include <asm/vio.h>
37#include <asm/prom.h>
38#include <asm/firmware.h>
39
40#include "hvc_console.h"
41
42char hvc_driver_name[] = "hvc_console";
43
44static struct vio_device_id hvc_driver_table[] __devinitdata = {
45 {"serial", "hvterm1"},
46 { "", "" }
47};
48MODULE_DEVICE_TABLE(vio, hvc_driver_table);
49
50static int filtered_get_chars(uint32_t vtermno, char *buf, int count)
51{
52 unsigned long got;
53 int i;
54
55 /*
56 * Vio firmware will read up to SIZE_VIO_GET_CHARS at its own discretion
57 * so we play safe and avoid the situation where got > count which could
58 * overload the flip buffer.
59 */
60 if (count < SIZE_VIO_GET_CHARS)
61 return -EAGAIN;
62
63 got = hvc_get_chars(vtermno, buf, count);
64
65 /*
66 * Work around a HV bug where it gives us a null
67 * after every \r. -- paulus
68 */
69 for (i = 1; i < got; ++i) {
70 if (buf[i] == 0 && buf[i-1] == '\r') {
71 --got;
72 if (i < got)
73 memmove(&buf[i], &buf[i+1],
74 got - i);
75 }
76 }
77 return got;
78}
79
80static const struct hv_ops hvc_get_put_ops = {
81 .get_chars = filtered_get_chars,
82 .put_chars = hvc_put_chars,
83 .notifier_add = notifier_add_irq,
84 .notifier_del = notifier_del_irq,
85 .notifier_hangup = notifier_hangup_irq,
86};
87
88static int __devinit hvc_vio_probe(struct vio_dev *vdev,
89 const struct vio_device_id *id)
90{
91 struct hvc_struct *hp;
92
93 /* probed with invalid parameters. */
94 if (!vdev || !id)
95 return -EPERM;
96
97 hp = hvc_alloc(vdev->unit_address, vdev->irq, &hvc_get_put_ops,
98 MAX_VIO_PUT_CHARS);
99 if (IS_ERR(hp))
100 return PTR_ERR(hp);
101 dev_set_drvdata(&vdev->dev, hp);
102
103 return 0;
104}
105
106static int __devexit hvc_vio_remove(struct vio_dev *vdev)
107{
108 struct hvc_struct *hp = dev_get_drvdata(&vdev->dev);
109
110 return hvc_remove(hp);
111}
112
113static struct vio_driver hvc_vio_driver = {
114 .id_table = hvc_driver_table,
115 .probe = hvc_vio_probe,
116 .remove = __devexit_p(hvc_vio_remove),
117 .driver = {
118 .name = hvc_driver_name,
119 .owner = THIS_MODULE,
120 }
121};
122
123static int __init hvc_vio_init(void)
124{
125 int rc;
126
127 if (firmware_has_feature(FW_FEATURE_ISERIES))
128 return -EIO;
129
130 /* Register as a vio device to receive callbacks */
131 rc = vio_register_driver(&hvc_vio_driver);
132
133 return rc;
134}
135module_init(hvc_vio_init); /* after drivers/char/hvc_console.c */
136
137static void __exit hvc_vio_exit(void)
138{
139 vio_unregister_driver(&hvc_vio_driver);
140}
141module_exit(hvc_vio_exit);
142
143/* the device tree order defines our numbering */
144static int hvc_find_vtys(void)
145{
146 struct device_node *vty;
147 int num_found = 0;
148
149 for (vty = of_find_node_by_name(NULL, "vty"); vty != NULL;
150 vty = of_find_node_by_name(vty, "vty")) {
151 const uint32_t *vtermno;
152
153 /* We have statically defined space for only a certain number
154 * of console adapters.
155 */
156 if (num_found >= MAX_NR_HVC_CONSOLES) {
157 of_node_put(vty);
158 break;
159 }
160
161 vtermno = of_get_property(vty, "reg", NULL);
162 if (!vtermno)
163 continue;
164
165 if (of_device_is_compatible(vty, "hvterm1")) {
166 hvc_instantiate(*vtermno, num_found, &hvc_get_put_ops);
167 ++num_found;
168 }
169 }
170
171 return num_found;
172}
173console_initcall(hvc_find_vtys);
diff --git a/drivers/char/hvc_xen.c b/drivers/char/hvc_xen.c
deleted file mode 100644
index 60446f82a3fc..000000000000
--- a/drivers/char/hvc_xen.c
+++ /dev/null
@@ -1,238 +0,0 @@
1/*
2 * xen console driver interface to hvc_console.c
3 *
4 * (c) 2007 Gerd Hoffmann <kraxel@suse.de>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21#include <linux/console.h>
22#include <linux/delay.h>
23#include <linux/err.h>
24#include <linux/init.h>
25#include <linux/types.h>
26
27#include <asm/xen/hypervisor.h>
28
29#include <xen/xen.h>
30#include <xen/page.h>
31#include <xen/events.h>
32#include <xen/interface/io/console.h>
33#include <xen/hvc-console.h>
34
35#include "hvc_console.h"
36
37#define HVC_COOKIE 0x58656e /* "Xen" in hex */
38
39static struct hvc_struct *hvc;
40static int xencons_irq;
41
42/* ------------------------------------------------------------------ */
43
44static unsigned long console_pfn = ~0ul;
45
46static inline struct xencons_interface *xencons_interface(void)
47{
48 if (console_pfn == ~0ul)
49 return mfn_to_virt(xen_start_info->console.domU.mfn);
50 else
51 return __va(console_pfn << PAGE_SHIFT);
52}
53
54static inline void notify_daemon(void)
55{
56 /* Use evtchn: this is called early, before irq is set up. */
57 notify_remote_via_evtchn(xen_start_info->console.domU.evtchn);
58}
59
60static int __write_console(const char *data, int len)
61{
62 struct xencons_interface *intf = xencons_interface();
63 XENCONS_RING_IDX cons, prod;
64 int sent = 0;
65
66 cons = intf->out_cons;
67 prod = intf->out_prod;
68 mb(); /* update queue values before going on */
69 BUG_ON((prod - cons) > sizeof(intf->out));
70
71 while ((sent < len) && ((prod - cons) < sizeof(intf->out)))
72 intf->out[MASK_XENCONS_IDX(prod++, intf->out)] = data[sent++];
73
74 wmb(); /* write ring before updating pointer */
75 intf->out_prod = prod;
76
77 notify_daemon();
78 return sent;
79}
80
81static int write_console(uint32_t vtermno, const char *data, int len)
82{
83 int ret = len;
84
85 /*
86 * Make sure the whole buffer is emitted, polling if
87 * necessary. We don't ever want to rely on the hvc daemon
88 * because the most interesting console output is when the
89 * kernel is crippled.
90 */
91 while (len) {
92 int sent = __write_console(data, len);
93
94 data += sent;
95 len -= sent;
96
97 if (unlikely(len))
98 HYPERVISOR_sched_op(SCHEDOP_yield, NULL);
99 }
100
101 return ret;
102}
103
104static int read_console(uint32_t vtermno, char *buf, int len)
105{
106 struct xencons_interface *intf = xencons_interface();
107 XENCONS_RING_IDX cons, prod;
108 int recv = 0;
109
110 cons = intf->in_cons;
111 prod = intf->in_prod;
112 mb(); /* get pointers before reading ring */
113 BUG_ON((prod - cons) > sizeof(intf->in));
114
115 while (cons != prod && recv < len)
116 buf[recv++] = intf->in[MASK_XENCONS_IDX(cons++, intf->in)];
117
118 mb(); /* read ring before consuming */
119 intf->in_cons = cons;
120
121 notify_daemon();
122 return recv;
123}
124
125static const struct hv_ops hvc_ops = {
126 .get_chars = read_console,
127 .put_chars = write_console,
128 .notifier_add = notifier_add_irq,
129 .notifier_del = notifier_del_irq,
130 .notifier_hangup = notifier_hangup_irq,
131};
132
133static int __init xen_init(void)
134{
135 struct hvc_struct *hp;
136
137 if (!xen_pv_domain() ||
138 xen_initial_domain() ||
139 !xen_start_info->console.domU.evtchn)
140 return -ENODEV;
141
142 xencons_irq = bind_evtchn_to_irq(xen_start_info->console.domU.evtchn);
143 if (xencons_irq < 0)
144 xencons_irq = 0; /* NO_IRQ */
145
146 hp = hvc_alloc(HVC_COOKIE, xencons_irq, &hvc_ops, 256);
147 if (IS_ERR(hp))
148 return PTR_ERR(hp);
149
150 hvc = hp;
151
152 console_pfn = mfn_to_pfn(xen_start_info->console.domU.mfn);
153
154 return 0;
155}
156
157void xen_console_resume(void)
158{
159 if (xencons_irq)
160 rebind_evtchn_irq(xen_start_info->console.domU.evtchn, xencons_irq);
161}
162
163static void __exit xen_fini(void)
164{
165 if (hvc)
166 hvc_remove(hvc);
167}
168
169static int xen_cons_init(void)
170{
171 if (!xen_pv_domain())
172 return 0;
173
174 hvc_instantiate(HVC_COOKIE, 0, &hvc_ops);
175 return 0;
176}
177
178module_init(xen_init);
179module_exit(xen_fini);
180console_initcall(xen_cons_init);
181
182static void raw_console_write(const char *str, int len)
183{
184 while(len > 0) {
185 int rc = HYPERVISOR_console_io(CONSOLEIO_write, len, (char *)str);
186 if (rc <= 0)
187 break;
188
189 str += rc;
190 len -= rc;
191 }
192}
193
194#ifdef CONFIG_EARLY_PRINTK
195static void xenboot_write_console(struct console *console, const char *string,
196 unsigned len)
197{
198 unsigned int linelen, off = 0;
199 const char *pos;
200
201 raw_console_write(string, len);
202
203 write_console(0, "(early) ", 8);
204 while (off < len && NULL != (pos = strchr(string+off, '\n'))) {
205 linelen = pos-string+off;
206 if (off + linelen > len)
207 break;
208 write_console(0, string+off, linelen);
209 write_console(0, "\r\n", 2);
210 off += linelen + 1;
211 }
212 if (off < len)
213 write_console(0, string+off, len-off);
214}
215
216struct console xenboot_console = {
217 .name = "xenboot",
218 .write = xenboot_write_console,
219 .flags = CON_PRINTBUFFER | CON_BOOT | CON_ANYTIME,
220};
221#endif /* CONFIG_EARLY_PRINTK */
222
223void xen_raw_console_write(const char *str)
224{
225 raw_console_write(str, strlen(str));
226}
227
228void xen_raw_printk(const char *fmt, ...)
229{
230 static char buf[512];
231 va_list ap;
232
233 va_start(ap, fmt);
234 vsnprintf(buf, sizeof(buf), fmt, ap);
235 va_end(ap);
236
237 xen_raw_console_write(buf);
238}
diff --git a/drivers/char/hvcs.c b/drivers/char/hvcs.c
deleted file mode 100644
index bedc6c1b6fa5..000000000000
--- a/drivers/char/hvcs.c
+++ /dev/null
@@ -1,1604 +0,0 @@
1/*
2 * IBM eServer Hypervisor Virtual Console Server Device Driver
3 * Copyright (C) 2003, 2004 IBM Corp.
4 * Ryan S. Arnold (rsa@us.ibm.com)
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 * Author(s) : Ryan S. Arnold <rsa@us.ibm.com>
21 *
22 * This is the device driver for the IBM Hypervisor Virtual Console Server,
23 * "hvcs". The IBM hvcs provides a tty driver interface to allow Linux
24 * user space applications access to the system consoles of logically
25 * partitioned operating systems, e.g. Linux, running on the same partitioned
26 * Power5 ppc64 system. Physical hardware consoles per partition are not
27 * practical on this hardware so system consoles are accessed by this driver
28 * using inter-partition firmware interfaces to virtual terminal devices.
29 *
30 * A vty is known to the HMC as a "virtual serial server adapter". It is a
31 * virtual terminal device that is created by firmware upon partition creation
32 * to act as a partitioned OS's console device.
33 *
34 * Firmware dynamically (via hotplug) exposes vty-servers to a running ppc64
35 * Linux system upon their creation by the HMC or their exposure during boot.
36 * The non-user interactive backend of this driver is implemented as a vio
37 * device driver so that it can receive notification of vty-server lifetimes
38 * after it registers with the vio bus to handle vty-server probe and remove
39 * callbacks.
40 *
41 * Many vty-servers can be configured to connect to one vty, but a vty can
42 * only be actively connected to by a single vty-server, in any manner, at one
43 * time. If the HMC is currently hosting the console for a target Linux
44 * partition; attempts to open the tty device to the partition's console using
45 * the hvcs on any partition will return -EBUSY with every open attempt until
46 * the HMC frees the connection between its vty-server and the desired
47 * partition's vty device. Conversely, a vty-server may only be connected to
48 * a single vty at one time even though it may have several configured vty
49 * partner possibilities.
50 *
51 * Firmware does not provide notification of vty partner changes to this
52 * driver. This means that an HMC Super Admin may add or remove partner vtys
53 * from a vty-server's partner list but the changes will not be signaled to
54 * the vty-server. Firmware only notifies the driver when a vty-server is
55 * added or removed from the system. To compensate for this deficiency, this
56 * driver implements a sysfs update attribute which provides a method for
57 * rescanning partner information upon a user's request.
58 *
59 * Each vty-server, prior to being exposed to this driver is reference counted
60 * using the 2.6 Linux kernel kref construct.
61 *
62 * For direction on installation and usage of this driver please reference
63 * Documentation/powerpc/hvcs.txt.
64 */
65
66#include <linux/device.h>
67#include <linux/init.h>
68#include <linux/interrupt.h>
69#include <linux/kernel.h>
70#include <linux/kref.h>
71#include <linux/kthread.h>
72#include <linux/list.h>
73#include <linux/major.h>
74#include <linux/module.h>
75#include <linux/moduleparam.h>
76#include <linux/sched.h>
77#include <linux/slab.h>
78#include <linux/spinlock.h>
79#include <linux/stat.h>
80#include <linux/tty.h>
81#include <linux/tty_flip.h>
82#include <asm/hvconsole.h>
83#include <asm/hvcserver.h>
84#include <asm/uaccess.h>
85#include <asm/vio.h>
86
87/*
88 * 1.3.0 -> 1.3.1 In hvcs_open memset(..,0x00,..) instead of memset(..,0x3F,00).
89 * Removed braces around single statements following conditionals. Removed '=
90 * 0' after static int declarations since these default to zero. Removed
91 * list_for_each_safe() and replaced with list_for_each_entry() in
92 * hvcs_get_by_index(). The 'safe' version is un-needed now that the driver is
93 * using spinlocks. Changed spin_lock_irqsave() to spin_lock() when locking
94 * hvcs_structs_lock and hvcs_pi_lock since these are not touched in an int
95 * handler. Initialized hvcs_structs_lock and hvcs_pi_lock to
96 * SPIN_LOCK_UNLOCKED at declaration time rather than in hvcs_module_init().
97 * Added spin_lock around list_del() in destroy_hvcs_struct() to protect the
98 * list traversals from a deletion. Removed '= NULL' from pointer declaration
99 * statements since they are initialized NULL by default. Removed wmb()
100 * instances from hvcs_try_write(). They probably aren't needed with locking in
101 * place. Added check and cleanup for hvcs_pi_buff = kmalloc() in
102 * hvcs_module_init(). Exposed hvcs_struct.index via a sysfs attribute so that
103 * the coupling between /dev/hvcs* and a vty-server can be automatically
104 * determined. Moved kobject_put() in hvcs_open outside of the
105 * spin_unlock_irqrestore().
106 *
107 * 1.3.1 -> 1.3.2 Changed method for determining hvcs_struct->index and had it
108 * align with how the tty layer always assigns the lowest index available. This
109 * change resulted in a list of ints that denotes which indexes are available.
110 * Device additions and removals use the new hvcs_get_index() and
111 * hvcs_return_index() helper functions. The list is created with
112 * hvsc_alloc_index_list() and it is destroyed with hvcs_free_index_list().
113 * Without these fixes hotplug vty-server adapter support goes crazy with this
114 * driver if the user removes a vty-server adapter. Moved free_irq() outside of
115 * the hvcs_final_close() function in order to get it out of the spinlock.
116 * Rearranged hvcs_close(). Cleaned up some printks and did some housekeeping
117 * on the changelog. Removed local CLC_LENGTH and used HVCS_CLC_LENGTH from
118 * arch/powerepc/include/asm/hvcserver.h
119 *
120 * 1.3.2 -> 1.3.3 Replaced yield() in hvcs_close() with tty_wait_until_sent() to
121 * prevent possible lockup with realtime scheduling as similarily pointed out by
122 * akpm in hvc_console. Changed resulted in the removal of hvcs_final_close()
123 * to reorder cleanup operations and prevent discarding of pending data during
124 * an hvcs_close(). Removed spinlock protection of hvcs_struct data members in
125 * hvcs_write_room() and hvcs_chars_in_buffer() because they aren't needed.
126 */
127
128#define HVCS_DRIVER_VERSION "1.3.3"
129
130MODULE_AUTHOR("Ryan S. Arnold <rsa@us.ibm.com>");
131MODULE_DESCRIPTION("IBM hvcs (Hypervisor Virtual Console Server) Driver");
132MODULE_LICENSE("GPL");
133MODULE_VERSION(HVCS_DRIVER_VERSION);
134
135/*
136 * Wait this long per iteration while trying to push buffered data to the
137 * hypervisor before allowing the tty to complete a close operation.
138 */
139#define HVCS_CLOSE_WAIT (HZ/100) /* 1/10 of a second */
140
141/*
142 * Since the Linux TTY code does not currently (2-04-2004) support dynamic
143 * addition of tty derived devices and we shouldn't allocate thousands of
144 * tty_device pointers when the number of vty-server & vty partner connections
145 * will most often be much lower than this, we'll arbitrarily allocate
146 * HVCS_DEFAULT_SERVER_ADAPTERS tty_structs and cdev's by default when we
147 * register the tty_driver. This can be overridden using an insmod parameter.
148 */
149#define HVCS_DEFAULT_SERVER_ADAPTERS 64
150
151/*
152 * The user can't insmod with more than HVCS_MAX_SERVER_ADAPTERS hvcs device
153 * nodes as a sanity check. Theoretically there can be over 1 Billion
154 * vty-server & vty partner connections.
155 */
156#define HVCS_MAX_SERVER_ADAPTERS 1024
157
158/*
159 * We let Linux assign us a major number and we start the minors at zero. There
160 * is no intuitive mapping between minor number and the target vty-server
161 * adapter except that each new vty-server adapter is always assigned to the
162 * smallest minor number available.
163 */
164#define HVCS_MINOR_START 0
165
166/*
167 * The hcall interface involves putting 8 chars into each of two registers.
168 * We load up those 2 registers (in arch/powerpc/platforms/pseries/hvconsole.c)
169 * by casting char[16] to long[2]. It would work without __ALIGNED__, but a
170 * little (tiny) bit slower because an unaligned load is slower than aligned
171 * load.
172 */
173#define __ALIGNED__ __attribute__((__aligned__(8)))
174
175/*
176 * How much data can firmware send with each hvc_put_chars()? Maybe this
177 * should be moved into an architecture specific area.
178 */
179#define HVCS_BUFF_LEN 16
180
181/*
182 * This is the maximum amount of data we'll let the user send us (hvcs_write) at
183 * once in a chunk as a sanity check.
184 */
185#define HVCS_MAX_FROM_USER 4096
186
187/*
188 * Be careful when adding flags to this line discipline. Don't add anything
189 * that will cause echoing or we'll go into recursive loop echoing chars back
190 * and forth with the console drivers.
191 */
192static struct ktermios hvcs_tty_termios = {
193 .c_iflag = IGNBRK | IGNPAR,
194 .c_oflag = OPOST,
195 .c_cflag = B38400 | CS8 | CREAD | HUPCL,
196 .c_cc = INIT_C_CC,
197 .c_ispeed = 38400,
198 .c_ospeed = 38400
199};
200
201/*
202 * This value is used to take the place of a command line parameter when the
203 * module is inserted. It starts as -1 and stays as such if the user doesn't
204 * specify a module insmod parameter. If they DO specify one then it is set to
205 * the value of the integer passed in.
206 */
207static int hvcs_parm_num_devs = -1;
208module_param(hvcs_parm_num_devs, int, 0);
209
210static const char hvcs_driver_name[] = "hvcs";
211static const char hvcs_device_node[] = "hvcs";
212static const char hvcs_driver_string[]
213 = "IBM hvcs (Hypervisor Virtual Console Server) Driver";
214
215/* Status of partner info rescan triggered via sysfs. */
216static int hvcs_rescan_status;
217
218static struct tty_driver *hvcs_tty_driver;
219
220/*
221 * In order to be somewhat sane this driver always associates the hvcs_struct
222 * index element with the numerically equal tty->index. This means that a
223 * hotplugged vty-server adapter will always map to the lowest index valued
224 * device node. If vty-servers were hotplug removed from the system and then
225 * new ones added the new vty-server may have the largest slot number of all
226 * the vty-server adapters in the partition but it may have the lowest dev node
227 * index of all the adapters due to the hole left by the hotplug removed
228 * adapter. There are a set of functions provided to get the lowest index for
229 * a new device as well as return the index to the list. This list is allocated
230 * with a number of elements equal to the number of device nodes requested when
231 * the module was inserted.
232 */
233static int *hvcs_index_list;
234
235/*
236 * How large is the list? This is kept for traversal since the list is
237 * dynamically created.
238 */
239static int hvcs_index_count;
240
241/*
242 * Used by the khvcsd to pick up I/O operations when the kernel_thread is
243 * already awake but potentially shifted to TASK_INTERRUPTIBLE state.
244 */
245static int hvcs_kicked;
246
247/*
248 * Use by the kthread construct for task operations like waking the sleeping
249 * thread and stopping the kthread.
250 */
251static struct task_struct *hvcs_task;
252
253/*
254 * We allocate this for the use of all of the hvcs_structs when they fetch
255 * partner info.
256 */
257static unsigned long *hvcs_pi_buff;
258
259/* Only allow one hvcs_struct to use the hvcs_pi_buff at a time. */
260static DEFINE_SPINLOCK(hvcs_pi_lock);
261
262/* One vty-server per hvcs_struct */
263struct hvcs_struct {
264 spinlock_t lock;
265
266 /*
267 * This index identifies this hvcs device as the complement to a
268 * specific tty index.
269 */
270 unsigned int index;
271
272 struct tty_struct *tty;
273 int open_count;
274
275 /*
276 * Used to tell the driver kernel_thread what operations need to take
277 * place upon this hvcs_struct instance.
278 */
279 int todo_mask;
280
281 /*
282 * This buffer is required so that when hvcs_write_room() reports that
283 * it can send HVCS_BUFF_LEN characters that it will buffer the full
284 * HVCS_BUFF_LEN characters if need be. This is essential for opost
285 * writes since they do not do high level buffering and expect to be
286 * able to send what the driver commits to sending buffering
287 * [e.g. tab to space conversions in n_tty.c opost()].
288 */
289 char buffer[HVCS_BUFF_LEN];
290 int chars_in_buffer;
291
292 /*
293 * Any variable below the kref is valid before a tty is connected and
294 * stays valid after the tty is disconnected. These shouldn't be
295 * whacked until the koject refcount reaches zero though some entries
296 * may be changed via sysfs initiatives.
297 */
298 struct kref kref; /* ref count & hvcs_struct lifetime */
299 int connected; /* is the vty-server currently connected to a vty? */
300 uint32_t p_unit_address; /* partner unit address */
301 uint32_t p_partition_ID; /* partner partition ID */
302 char p_location_code[HVCS_CLC_LENGTH + 1]; /* CLC + Null Term */
303 struct list_head next; /* list management */
304 struct vio_dev *vdev;
305};
306
307/* Required to back map a kref to its containing object */
308#define from_kref(k) container_of(k, struct hvcs_struct, kref)
309
310static LIST_HEAD(hvcs_structs);
311static DEFINE_SPINLOCK(hvcs_structs_lock);
312
313static void hvcs_unthrottle(struct tty_struct *tty);
314static void hvcs_throttle(struct tty_struct *tty);
315static irqreturn_t hvcs_handle_interrupt(int irq, void *dev_instance);
316
317static int hvcs_write(struct tty_struct *tty,
318 const unsigned char *buf, int count);
319static int hvcs_write_room(struct tty_struct *tty);
320static int hvcs_chars_in_buffer(struct tty_struct *tty);
321
322static int hvcs_has_pi(struct hvcs_struct *hvcsd);
323static void hvcs_set_pi(struct hvcs_partner_info *pi,
324 struct hvcs_struct *hvcsd);
325static int hvcs_get_pi(struct hvcs_struct *hvcsd);
326static int hvcs_rescan_devices_list(void);
327
328static int hvcs_partner_connect(struct hvcs_struct *hvcsd);
329static void hvcs_partner_free(struct hvcs_struct *hvcsd);
330
331static int hvcs_enable_device(struct hvcs_struct *hvcsd,
332 uint32_t unit_address, unsigned int irq, struct vio_dev *dev);
333
334static int hvcs_open(struct tty_struct *tty, struct file *filp);
335static void hvcs_close(struct tty_struct *tty, struct file *filp);
336static void hvcs_hangup(struct tty_struct * tty);
337
338static int __devinit hvcs_probe(struct vio_dev *dev,
339 const struct vio_device_id *id);
340static int __devexit hvcs_remove(struct vio_dev *dev);
341static int __init hvcs_module_init(void);
342static void __exit hvcs_module_exit(void);
343
344#define HVCS_SCHED_READ 0x00000001
345#define HVCS_QUICK_READ 0x00000002
346#define HVCS_TRY_WRITE 0x00000004
347#define HVCS_READ_MASK (HVCS_SCHED_READ | HVCS_QUICK_READ)
348
349static inline struct hvcs_struct *from_vio_dev(struct vio_dev *viod)
350{
351 return dev_get_drvdata(&viod->dev);
352}
353/* The sysfs interface for the driver and devices */
354
355static ssize_t hvcs_partner_vtys_show(struct device *dev, struct device_attribute *attr, char *buf)
356{
357 struct vio_dev *viod = to_vio_dev(dev);
358 struct hvcs_struct *hvcsd = from_vio_dev(viod);
359 unsigned long flags;
360 int retval;
361
362 spin_lock_irqsave(&hvcsd->lock, flags);
363 retval = sprintf(buf, "%X\n", hvcsd->p_unit_address);
364 spin_unlock_irqrestore(&hvcsd->lock, flags);
365 return retval;
366}
367static DEVICE_ATTR(partner_vtys, S_IRUGO, hvcs_partner_vtys_show, NULL);
368
369static ssize_t hvcs_partner_clcs_show(struct device *dev, struct device_attribute *attr, char *buf)
370{
371 struct vio_dev *viod = to_vio_dev(dev);
372 struct hvcs_struct *hvcsd = from_vio_dev(viod);
373 unsigned long flags;
374 int retval;
375
376 spin_lock_irqsave(&hvcsd->lock, flags);
377 retval = sprintf(buf, "%s\n", &hvcsd->p_location_code[0]);
378 spin_unlock_irqrestore(&hvcsd->lock, flags);
379 return retval;
380}
381static DEVICE_ATTR(partner_clcs, S_IRUGO, hvcs_partner_clcs_show, NULL);
382
383static ssize_t hvcs_current_vty_store(struct device *dev, struct device_attribute *attr, const char * buf,
384 size_t count)
385{
386 /*
387 * Don't need this feature at the present time because firmware doesn't
388 * yet support multiple partners.
389 */
390 printk(KERN_INFO "HVCS: Denied current_vty change: -EPERM.\n");
391 return -EPERM;
392}
393
394static ssize_t hvcs_current_vty_show(struct device *dev, struct device_attribute *attr, char *buf)
395{
396 struct vio_dev *viod = to_vio_dev(dev);
397 struct hvcs_struct *hvcsd = from_vio_dev(viod);
398 unsigned long flags;
399 int retval;
400
401 spin_lock_irqsave(&hvcsd->lock, flags);
402 retval = sprintf(buf, "%s\n", &hvcsd->p_location_code[0]);
403 spin_unlock_irqrestore(&hvcsd->lock, flags);
404 return retval;
405}
406
407static DEVICE_ATTR(current_vty,
408 S_IRUGO | S_IWUSR, hvcs_current_vty_show, hvcs_current_vty_store);
409
410static ssize_t hvcs_vterm_state_store(struct device *dev, struct device_attribute *attr, const char *buf,
411 size_t count)
412{
413 struct vio_dev *viod = to_vio_dev(dev);
414 struct hvcs_struct *hvcsd = from_vio_dev(viod);
415 unsigned long flags;
416
417 /* writing a '0' to this sysfs entry will result in the disconnect. */
418 if (simple_strtol(buf, NULL, 0) != 0)
419 return -EINVAL;
420
421 spin_lock_irqsave(&hvcsd->lock, flags);
422
423 if (hvcsd->open_count > 0) {
424 spin_unlock_irqrestore(&hvcsd->lock, flags);
425 printk(KERN_INFO "HVCS: vterm state unchanged. "
426 "The hvcs device node is still in use.\n");
427 return -EPERM;
428 }
429
430 if (hvcsd->connected == 0) {
431 spin_unlock_irqrestore(&hvcsd->lock, flags);
432 printk(KERN_INFO "HVCS: vterm state unchanged. The"
433 " vty-server is not connected to a vty.\n");
434 return -EPERM;
435 }
436
437 hvcs_partner_free(hvcsd);
438 printk(KERN_INFO "HVCS: Closed vty-server@%X and"
439 " partner vty@%X:%d connection.\n",
440 hvcsd->vdev->unit_address,
441 hvcsd->p_unit_address,
442 (uint32_t)hvcsd->p_partition_ID);
443
444 spin_unlock_irqrestore(&hvcsd->lock, flags);
445 return count;
446}
447
448static ssize_t hvcs_vterm_state_show(struct device *dev, struct device_attribute *attr, char *buf)
449{
450 struct vio_dev *viod = to_vio_dev(dev);
451 struct hvcs_struct *hvcsd = from_vio_dev(viod);
452 unsigned long flags;
453 int retval;
454
455 spin_lock_irqsave(&hvcsd->lock, flags);
456 retval = sprintf(buf, "%d\n", hvcsd->connected);
457 spin_unlock_irqrestore(&hvcsd->lock, flags);
458 return retval;
459}
460static DEVICE_ATTR(vterm_state, S_IRUGO | S_IWUSR,
461 hvcs_vterm_state_show, hvcs_vterm_state_store);
462
463static ssize_t hvcs_index_show(struct device *dev, struct device_attribute *attr, char *buf)
464{
465 struct vio_dev *viod = to_vio_dev(dev);
466 struct hvcs_struct *hvcsd = from_vio_dev(viod);
467 unsigned long flags;
468 int retval;
469
470 spin_lock_irqsave(&hvcsd->lock, flags);
471 retval = sprintf(buf, "%d\n", hvcsd->index);
472 spin_unlock_irqrestore(&hvcsd->lock, flags);
473 return retval;
474}
475
476static DEVICE_ATTR(index, S_IRUGO, hvcs_index_show, NULL);
477
478static struct attribute *hvcs_attrs[] = {
479 &dev_attr_partner_vtys.attr,
480 &dev_attr_partner_clcs.attr,
481 &dev_attr_current_vty.attr,
482 &dev_attr_vterm_state.attr,
483 &dev_attr_index.attr,
484 NULL,
485};
486
487static struct attribute_group hvcs_attr_group = {
488 .attrs = hvcs_attrs,
489};
490
491static ssize_t hvcs_rescan_show(struct device_driver *ddp, char *buf)
492{
493 /* A 1 means it is updating, a 0 means it is done updating */
494 return snprintf(buf, PAGE_SIZE, "%d\n", hvcs_rescan_status);
495}
496
497static ssize_t hvcs_rescan_store(struct device_driver *ddp, const char * buf,
498 size_t count)
499{
500 if ((simple_strtol(buf, NULL, 0) != 1)
501 && (hvcs_rescan_status != 0))
502 return -EINVAL;
503
504 hvcs_rescan_status = 1;
505 printk(KERN_INFO "HVCS: rescanning partner info for all"
506 " vty-servers.\n");
507 hvcs_rescan_devices_list();
508 hvcs_rescan_status = 0;
509 return count;
510}
511
512static DRIVER_ATTR(rescan,
513 S_IRUGO | S_IWUSR, hvcs_rescan_show, hvcs_rescan_store);
514
515static void hvcs_kick(void)
516{
517 hvcs_kicked = 1;
518 wmb();
519 wake_up_process(hvcs_task);
520}
521
522static void hvcs_unthrottle(struct tty_struct *tty)
523{
524 struct hvcs_struct *hvcsd = tty->driver_data;
525 unsigned long flags;
526
527 spin_lock_irqsave(&hvcsd->lock, flags);
528 hvcsd->todo_mask |= HVCS_SCHED_READ;
529 spin_unlock_irqrestore(&hvcsd->lock, flags);
530 hvcs_kick();
531}
532
533static void hvcs_throttle(struct tty_struct *tty)
534{
535 struct hvcs_struct *hvcsd = tty->driver_data;
536 unsigned long flags;
537
538 spin_lock_irqsave(&hvcsd->lock, flags);
539 vio_disable_interrupts(hvcsd->vdev);
540 spin_unlock_irqrestore(&hvcsd->lock, flags);
541}
542
543/*
544 * If the device is being removed we don't have to worry about this interrupt
545 * handler taking any further interrupts because they are disabled which means
546 * the hvcs_struct will always be valid in this handler.
547 */
548static irqreturn_t hvcs_handle_interrupt(int irq, void *dev_instance)
549{
550 struct hvcs_struct *hvcsd = dev_instance;
551
552 spin_lock(&hvcsd->lock);
553 vio_disable_interrupts(hvcsd->vdev);
554 hvcsd->todo_mask |= HVCS_SCHED_READ;
555 spin_unlock(&hvcsd->lock);
556 hvcs_kick();
557
558 return IRQ_HANDLED;
559}
560
561/* This function must be called with the hvcsd->lock held */
562static void hvcs_try_write(struct hvcs_struct *hvcsd)
563{
564 uint32_t unit_address = hvcsd->vdev->unit_address;
565 struct tty_struct *tty = hvcsd->tty;
566 int sent;
567
568 if (hvcsd->todo_mask & HVCS_TRY_WRITE) {
569 /* won't send partial writes */
570 sent = hvc_put_chars(unit_address,
571 &hvcsd->buffer[0],
572 hvcsd->chars_in_buffer );
573 if (sent > 0) {
574 hvcsd->chars_in_buffer = 0;
575 /* wmb(); */
576 hvcsd->todo_mask &= ~(HVCS_TRY_WRITE);
577 /* wmb(); */
578
579 /*
580 * We are still obligated to deliver the data to the
581 * hypervisor even if the tty has been closed because
582 * we commited to delivering it. But don't try to wake
583 * a non-existent tty.
584 */
585 if (tty) {
586 tty_wakeup(tty);
587 }
588 }
589 }
590}
591
592static int hvcs_io(struct hvcs_struct *hvcsd)
593{
594 uint32_t unit_address;
595 struct tty_struct *tty;
596 char buf[HVCS_BUFF_LEN] __ALIGNED__;
597 unsigned long flags;
598 int got = 0;
599
600 spin_lock_irqsave(&hvcsd->lock, flags);
601
602 unit_address = hvcsd->vdev->unit_address;
603 tty = hvcsd->tty;
604
605 hvcs_try_write(hvcsd);
606
607 if (!tty || test_bit(TTY_THROTTLED, &tty->flags)) {
608 hvcsd->todo_mask &= ~(HVCS_READ_MASK);
609 goto bail;
610 } else if (!(hvcsd->todo_mask & (HVCS_READ_MASK)))
611 goto bail;
612
613 /* remove the read masks */
614 hvcsd->todo_mask &= ~(HVCS_READ_MASK);
615
616 if (tty_buffer_request_room(tty, HVCS_BUFF_LEN) >= HVCS_BUFF_LEN) {
617 got = hvc_get_chars(unit_address,
618 &buf[0],
619 HVCS_BUFF_LEN);
620 tty_insert_flip_string(tty, buf, got);
621 }
622
623 /* Give the TTY time to process the data we just sent. */
624 if (got)
625 hvcsd->todo_mask |= HVCS_QUICK_READ;
626
627 spin_unlock_irqrestore(&hvcsd->lock, flags);
628 /* This is synch because tty->low_latency == 1 */
629 if(got)
630 tty_flip_buffer_push(tty);
631
632 if (!got) {
633 /* Do this _after_ the flip_buffer_push */
634 spin_lock_irqsave(&hvcsd->lock, flags);
635 vio_enable_interrupts(hvcsd->vdev);
636 spin_unlock_irqrestore(&hvcsd->lock, flags);
637 }
638
639 return hvcsd->todo_mask;
640
641 bail:
642 spin_unlock_irqrestore(&hvcsd->lock, flags);
643 return hvcsd->todo_mask;
644}
645
646static int khvcsd(void *unused)
647{
648 struct hvcs_struct *hvcsd;
649 int hvcs_todo_mask;
650
651 __set_current_state(TASK_RUNNING);
652
653 do {
654 hvcs_todo_mask = 0;
655 hvcs_kicked = 0;
656 wmb();
657
658 spin_lock(&hvcs_structs_lock);
659 list_for_each_entry(hvcsd, &hvcs_structs, next) {
660 hvcs_todo_mask |= hvcs_io(hvcsd);
661 }
662 spin_unlock(&hvcs_structs_lock);
663
664 /*
665 * If any of the hvcs adapters want to try a write or quick read
666 * don't schedule(), yield a smidgen then execute the hvcs_io
667 * thread again for those that want the write.
668 */
669 if (hvcs_todo_mask & (HVCS_TRY_WRITE | HVCS_QUICK_READ)) {
670 yield();
671 continue;
672 }
673
674 set_current_state(TASK_INTERRUPTIBLE);
675 if (!hvcs_kicked)
676 schedule();
677 __set_current_state(TASK_RUNNING);
678 } while (!kthread_should_stop());
679
680 return 0;
681}
682
683static struct vio_device_id hvcs_driver_table[] __devinitdata= {
684 {"serial-server", "hvterm2"},
685 { "", "" }
686};
687MODULE_DEVICE_TABLE(vio, hvcs_driver_table);
688
689static void hvcs_return_index(int index)
690{
691 /* Paranoia check */
692 if (!hvcs_index_list)
693 return;
694 if (index < 0 || index >= hvcs_index_count)
695 return;
696 if (hvcs_index_list[index] == -1)
697 return;
698 else
699 hvcs_index_list[index] = -1;
700}
701
702/* callback when the kref ref count reaches zero */
703static void destroy_hvcs_struct(struct kref *kref)
704{
705 struct hvcs_struct *hvcsd = from_kref(kref);
706 struct vio_dev *vdev;
707 unsigned long flags;
708
709 spin_lock(&hvcs_structs_lock);
710 spin_lock_irqsave(&hvcsd->lock, flags);
711
712 /* the list_del poisons the pointers */
713 list_del(&(hvcsd->next));
714
715 if (hvcsd->connected == 1) {
716 hvcs_partner_free(hvcsd);
717 printk(KERN_INFO "HVCS: Closed vty-server@%X and"
718 " partner vty@%X:%d connection.\n",
719 hvcsd->vdev->unit_address,
720 hvcsd->p_unit_address,
721 (uint32_t)hvcsd->p_partition_ID);
722 }
723 printk(KERN_INFO "HVCS: Destroyed hvcs_struct for vty-server@%X.\n",
724 hvcsd->vdev->unit_address);
725
726 vdev = hvcsd->vdev;
727 hvcsd->vdev = NULL;
728
729 hvcsd->p_unit_address = 0;
730 hvcsd->p_partition_ID = 0;
731 hvcs_return_index(hvcsd->index);
732 memset(&hvcsd->p_location_code[0], 0x00, HVCS_CLC_LENGTH + 1);
733
734 spin_unlock_irqrestore(&hvcsd->lock, flags);
735 spin_unlock(&hvcs_structs_lock);
736
737 sysfs_remove_group(&vdev->dev.kobj, &hvcs_attr_group);
738
739 kfree(hvcsd);
740}
741
742static int hvcs_get_index(void)
743{
744 int i;
745 /* Paranoia check */
746 if (!hvcs_index_list) {
747 printk(KERN_ERR "HVCS: hvcs_index_list NOT valid!.\n");
748 return -EFAULT;
749 }
750 /* Find the numerically lowest first free index. */
751 for(i = 0; i < hvcs_index_count; i++) {
752 if (hvcs_index_list[i] == -1) {
753 hvcs_index_list[i] = 0;
754 return i;
755 }
756 }
757 return -1;
758}
759
760static int __devinit hvcs_probe(
761 struct vio_dev *dev,
762 const struct vio_device_id *id)
763{
764 struct hvcs_struct *hvcsd;
765 int index;
766 int retval;
767
768 if (!dev || !id) {
769 printk(KERN_ERR "HVCS: probed with invalid parameter.\n");
770 return -EPERM;
771 }
772
773 /* early to avoid cleanup on failure */
774 index = hvcs_get_index();
775 if (index < 0) {
776 return -EFAULT;
777 }
778
779 hvcsd = kzalloc(sizeof(*hvcsd), GFP_KERNEL);
780 if (!hvcsd)
781 return -ENODEV;
782
783
784 spin_lock_init(&hvcsd->lock);
785 /* Automatically incs the refcount the first time */
786 kref_init(&hvcsd->kref);
787
788 hvcsd->vdev = dev;
789 dev_set_drvdata(&dev->dev, hvcsd);
790
791 hvcsd->index = index;
792
793 /* hvcsd->index = ++hvcs_struct_count; */
794 hvcsd->chars_in_buffer = 0;
795 hvcsd->todo_mask = 0;
796 hvcsd->connected = 0;
797
798 /*
799 * This will populate the hvcs_struct's partner info fields for the
800 * first time.
801 */
802 if (hvcs_get_pi(hvcsd)) {
803 printk(KERN_ERR "HVCS: Failed to fetch partner"
804 " info for vty-server@%X on device probe.\n",
805 hvcsd->vdev->unit_address);
806 }
807
808 /*
809 * If a user app opens a tty that corresponds to this vty-server before
810 * the hvcs_struct has been added to the devices list then the user app
811 * will get -ENODEV.
812 */
813 spin_lock(&hvcs_structs_lock);
814 list_add_tail(&(hvcsd->next), &hvcs_structs);
815 spin_unlock(&hvcs_structs_lock);
816
817 retval = sysfs_create_group(&dev->dev.kobj, &hvcs_attr_group);
818 if (retval) {
819 printk(KERN_ERR "HVCS: Can't create sysfs attrs for vty-server@%X\n",
820 hvcsd->vdev->unit_address);
821 return retval;
822 }
823
824 printk(KERN_INFO "HVCS: vty-server@%X added to the vio bus.\n", dev->unit_address);
825
826 /*
827 * DON'T enable interrupts here because there is no user to receive the
828 * data.
829 */
830 return 0;
831}
832
833static int __devexit hvcs_remove(struct vio_dev *dev)
834{
835 struct hvcs_struct *hvcsd = dev_get_drvdata(&dev->dev);
836 unsigned long flags;
837 struct tty_struct *tty;
838
839 if (!hvcsd)
840 return -ENODEV;
841
842 /* By this time the vty-server won't be getting any more interrupts */
843
844 spin_lock_irqsave(&hvcsd->lock, flags);
845
846 tty = hvcsd->tty;
847
848 spin_unlock_irqrestore(&hvcsd->lock, flags);
849
850 /*
851 * Let the last holder of this object cause it to be removed, which
852 * would probably be tty_hangup below.
853 */
854 kref_put(&hvcsd->kref, destroy_hvcs_struct);
855
856 /*
857 * The hangup is a scheduled function which will auto chain call
858 * hvcs_hangup. The tty should always be valid at this time unless a
859 * simultaneous tty close already cleaned up the hvcs_struct.
860 */
861 if (tty)
862 tty_hangup(tty);
863
864 printk(KERN_INFO "HVCS: vty-server@%X removed from the"
865 " vio bus.\n", dev->unit_address);
866 return 0;
867};
868
869static struct vio_driver hvcs_vio_driver = {
870 .id_table = hvcs_driver_table,
871 .probe = hvcs_probe,
872 .remove = __devexit_p(hvcs_remove),
873 .driver = {
874 .name = hvcs_driver_name,
875 .owner = THIS_MODULE,
876 }
877};
878
879/* Only called from hvcs_get_pi please */
880static void hvcs_set_pi(struct hvcs_partner_info *pi, struct hvcs_struct *hvcsd)
881{
882 int clclength;
883
884 hvcsd->p_unit_address = pi->unit_address;
885 hvcsd->p_partition_ID = pi->partition_ID;
886 clclength = strlen(&pi->location_code[0]);
887 if (clclength > HVCS_CLC_LENGTH)
888 clclength = HVCS_CLC_LENGTH;
889
890 /* copy the null-term char too */
891 strncpy(&hvcsd->p_location_code[0],
892 &pi->location_code[0], clclength + 1);
893}
894
895/*
896 * Traverse the list and add the partner info that is found to the hvcs_struct
897 * struct entry. NOTE: At this time I know that partner info will return a
898 * single entry but in the future there may be multiple partner info entries per
899 * vty-server and you'll want to zero out that list and reset it. If for some
900 * reason you have an old version of this driver but there IS more than one
901 * partner info then hvcsd->p_* will hold the last partner info data from the
902 * firmware query. A good way to update this code would be to replace the three
903 * partner info fields in hvcs_struct with a list of hvcs_partner_info
904 * instances.
905 *
906 * This function must be called with the hvcsd->lock held.
907 */
908static int hvcs_get_pi(struct hvcs_struct *hvcsd)
909{
910 struct hvcs_partner_info *pi;
911 uint32_t unit_address = hvcsd->vdev->unit_address;
912 struct list_head head;
913 int retval;
914
915 spin_lock(&hvcs_pi_lock);
916 if (!hvcs_pi_buff) {
917 spin_unlock(&hvcs_pi_lock);
918 return -EFAULT;
919 }
920 retval = hvcs_get_partner_info(unit_address, &head, hvcs_pi_buff);
921 spin_unlock(&hvcs_pi_lock);
922 if (retval) {
923 printk(KERN_ERR "HVCS: Failed to fetch partner"
924 " info for vty-server@%x.\n", unit_address);
925 return retval;
926 }
927
928 /* nixes the values if the partner vty went away */
929 hvcsd->p_unit_address = 0;
930 hvcsd->p_partition_ID = 0;
931
932 list_for_each_entry(pi, &head, node)
933 hvcs_set_pi(pi, hvcsd);
934
935 hvcs_free_partner_info(&head);
936 return 0;
937}
938
939/*
940 * This function is executed by the driver "rescan" sysfs entry. It shouldn't
941 * be executed elsewhere, in order to prevent deadlock issues.
942 */
943static int hvcs_rescan_devices_list(void)
944{
945 struct hvcs_struct *hvcsd;
946 unsigned long flags;
947
948 spin_lock(&hvcs_structs_lock);
949
950 list_for_each_entry(hvcsd, &hvcs_structs, next) {
951 spin_lock_irqsave(&hvcsd->lock, flags);
952 hvcs_get_pi(hvcsd);
953 spin_unlock_irqrestore(&hvcsd->lock, flags);
954 }
955
956 spin_unlock(&hvcs_structs_lock);
957
958 return 0;
959}
960
961/*
962 * Farm this off into its own function because it could be more complex once
963 * multiple partners support is added. This function should be called with
964 * the hvcsd->lock held.
965 */
966static int hvcs_has_pi(struct hvcs_struct *hvcsd)
967{
968 if ((!hvcsd->p_unit_address) || (!hvcsd->p_partition_ID))
969 return 0;
970 return 1;
971}
972
973/*
974 * NOTE: It is possible that the super admin removed a partner vty and then
975 * added a different vty as the new partner.
976 *
977 * This function must be called with the hvcsd->lock held.
978 */
979static int hvcs_partner_connect(struct hvcs_struct *hvcsd)
980{
981 int retval;
982 unsigned int unit_address = hvcsd->vdev->unit_address;
983
984 /*
985 * If there wasn't any pi when the device was added it doesn't meant
986 * there isn't any now. This driver isn't notified when a new partner
987 * vty is added to a vty-server so we discover changes on our own.
988 * Please see comments in hvcs_register_connection() for justification
989 * of this bizarre code.
990 */
991 retval = hvcs_register_connection(unit_address,
992 hvcsd->p_partition_ID,
993 hvcsd->p_unit_address);
994 if (!retval) {
995 hvcsd->connected = 1;
996 return 0;
997 } else if (retval != -EINVAL)
998 return retval;
999
1000 /*
1001 * As per the spec re-get the pi and try again if -EINVAL after the
1002 * first connection attempt.
1003 */
1004 if (hvcs_get_pi(hvcsd))
1005 return -ENOMEM;
1006
1007 if (!hvcs_has_pi(hvcsd))
1008 return -ENODEV;
1009
1010 retval = hvcs_register_connection(unit_address,
1011 hvcsd->p_partition_ID,
1012 hvcsd->p_unit_address);
1013 if (retval != -EINVAL) {
1014 hvcsd->connected = 1;
1015 return retval;
1016 }
1017
1018 /*
1019 * EBUSY is the most likely scenario though the vty could have been
1020 * removed or there really could be an hcall error due to the parameter
1021 * data but thanks to ambiguous firmware return codes we can't really
1022 * tell.
1023 */
1024 printk(KERN_INFO "HVCS: vty-server or partner"
1025 " vty is busy. Try again later.\n");
1026 return -EBUSY;
1027}
1028
1029/* This function must be called with the hvcsd->lock held */
1030static void hvcs_partner_free(struct hvcs_struct *hvcsd)
1031{
1032 int retval;
1033 do {
1034 retval = hvcs_free_connection(hvcsd->vdev->unit_address);
1035 } while (retval == -EBUSY);
1036 hvcsd->connected = 0;
1037}
1038
1039/* This helper function must be called WITHOUT the hvcsd->lock held */
1040static int hvcs_enable_device(struct hvcs_struct *hvcsd, uint32_t unit_address,
1041 unsigned int irq, struct vio_dev *vdev)
1042{
1043 unsigned long flags;
1044 int rc;
1045
1046 /*
1047 * It is possible that the vty-server was removed between the time that
1048 * the conn was registered and now.
1049 */
1050 if (!(rc = request_irq(irq, &hvcs_handle_interrupt,
1051 IRQF_DISABLED, "ibmhvcs", hvcsd))) {
1052 /*
1053 * It is possible the vty-server was removed after the irq was
1054 * requested but before we have time to enable interrupts.
1055 */
1056 if (vio_enable_interrupts(vdev) == H_SUCCESS)
1057 return 0;
1058 else {
1059 printk(KERN_ERR "HVCS: int enable failed for"
1060 " vty-server@%X.\n", unit_address);
1061 free_irq(irq, hvcsd);
1062 }
1063 } else
1064 printk(KERN_ERR "HVCS: irq req failed for"
1065 " vty-server@%X.\n", unit_address);
1066
1067 spin_lock_irqsave(&hvcsd->lock, flags);
1068 hvcs_partner_free(hvcsd);
1069 spin_unlock_irqrestore(&hvcsd->lock, flags);
1070
1071 return rc;
1072
1073}
1074
1075/*
1076 * This always increments the kref ref count if the call is successful.
1077 * Please remember to dec when you are done with the instance.
1078 *
1079 * NOTICE: Do NOT hold either the hvcs_struct.lock or hvcs_structs_lock when
1080 * calling this function or you will get deadlock.
1081 */
1082static struct hvcs_struct *hvcs_get_by_index(int index)
1083{
1084 struct hvcs_struct *hvcsd = NULL;
1085 unsigned long flags;
1086
1087 spin_lock(&hvcs_structs_lock);
1088 /* We can immediately discard OOB requests */
1089 if (index >= 0 && index < HVCS_MAX_SERVER_ADAPTERS) {
1090 list_for_each_entry(hvcsd, &hvcs_structs, next) {
1091 spin_lock_irqsave(&hvcsd->lock, flags);
1092 if (hvcsd->index == index) {
1093 kref_get(&hvcsd->kref);
1094 spin_unlock_irqrestore(&hvcsd->lock, flags);
1095 spin_unlock(&hvcs_structs_lock);
1096 return hvcsd;
1097 }
1098 spin_unlock_irqrestore(&hvcsd->lock, flags);
1099 }
1100 hvcsd = NULL;
1101 }
1102
1103 spin_unlock(&hvcs_structs_lock);
1104 return hvcsd;
1105}
1106
1107/*
1108 * This is invoked via the tty_open interface when a user app connects to the
1109 * /dev node.
1110 */
1111static int hvcs_open(struct tty_struct *tty, struct file *filp)
1112{
1113 struct hvcs_struct *hvcsd;
1114 int rc, retval = 0;
1115 unsigned long flags;
1116 unsigned int irq;
1117 struct vio_dev *vdev;
1118 unsigned long unit_address;
1119
1120 if (tty->driver_data)
1121 goto fast_open;
1122
1123 /*
1124 * Is there a vty-server that shares the same index?
1125 * This function increments the kref index.
1126 */
1127 if (!(hvcsd = hvcs_get_by_index(tty->index))) {
1128 printk(KERN_WARNING "HVCS: open failed, no device associated"
1129 " with tty->index %d.\n", tty->index);
1130 return -ENODEV;
1131 }
1132
1133 spin_lock_irqsave(&hvcsd->lock, flags);
1134
1135 if (hvcsd->connected == 0)
1136 if ((retval = hvcs_partner_connect(hvcsd)))
1137 goto error_release;
1138
1139 hvcsd->open_count = 1;
1140 hvcsd->tty = tty;
1141 tty->driver_data = hvcsd;
1142
1143 memset(&hvcsd->buffer[0], 0x00, HVCS_BUFF_LEN);
1144
1145 /*
1146 * Save these in the spinlock for the enable operations that need them
1147 * outside of the spinlock.
1148 */
1149 irq = hvcsd->vdev->irq;
1150 vdev = hvcsd->vdev;
1151 unit_address = hvcsd->vdev->unit_address;
1152
1153 hvcsd->todo_mask |= HVCS_SCHED_READ;
1154 spin_unlock_irqrestore(&hvcsd->lock, flags);
1155
1156 /*
1157 * This must be done outside of the spinlock because it requests irqs
1158 * and will grab the spinlock and free the connection if it fails.
1159 */
1160 if (((rc = hvcs_enable_device(hvcsd, unit_address, irq, vdev)))) {
1161 kref_put(&hvcsd->kref, destroy_hvcs_struct);
1162 printk(KERN_WARNING "HVCS: enable device failed.\n");
1163 return rc;
1164 }
1165
1166 goto open_success;
1167
1168fast_open:
1169 hvcsd = tty->driver_data;
1170
1171 spin_lock_irqsave(&hvcsd->lock, flags);
1172 kref_get(&hvcsd->kref);
1173 hvcsd->open_count++;
1174 hvcsd->todo_mask |= HVCS_SCHED_READ;
1175 spin_unlock_irqrestore(&hvcsd->lock, flags);
1176
1177open_success:
1178 hvcs_kick();
1179
1180 printk(KERN_INFO "HVCS: vty-server@%X connection opened.\n",
1181 hvcsd->vdev->unit_address );
1182
1183 return 0;
1184
1185error_release:
1186 spin_unlock_irqrestore(&hvcsd->lock, flags);
1187 kref_put(&hvcsd->kref, destroy_hvcs_struct);
1188
1189 printk(KERN_WARNING "HVCS: partner connect failed.\n");
1190 return retval;
1191}
1192
1193static void hvcs_close(struct tty_struct *tty, struct file *filp)
1194{
1195 struct hvcs_struct *hvcsd;
1196 unsigned long flags;
1197 int irq = NO_IRQ;
1198
1199 /*
1200 * Is someone trying to close the file associated with this device after
1201 * we have hung up? If so tty->driver_data wouldn't be valid.
1202 */
1203 if (tty_hung_up_p(filp))
1204 return;
1205
1206 /*
1207 * No driver_data means that this close was probably issued after a
1208 * failed hvcs_open by the tty layer's release_dev() api and we can just
1209 * exit cleanly.
1210 */
1211 if (!tty->driver_data)
1212 return;
1213
1214 hvcsd = tty->driver_data;
1215
1216 spin_lock_irqsave(&hvcsd->lock, flags);
1217 if (--hvcsd->open_count == 0) {
1218
1219 vio_disable_interrupts(hvcsd->vdev);
1220
1221 /*
1222 * NULL this early so that the kernel_thread doesn't try to
1223 * execute any operations on the TTY even though it is obligated
1224 * to deliver any pending I/O to the hypervisor.
1225 */
1226 hvcsd->tty = NULL;
1227
1228 irq = hvcsd->vdev->irq;
1229 spin_unlock_irqrestore(&hvcsd->lock, flags);
1230
1231 tty_wait_until_sent(tty, HVCS_CLOSE_WAIT);
1232
1233 /*
1234 * This line is important because it tells hvcs_open that this
1235 * device needs to be re-configured the next time hvcs_open is
1236 * called.
1237 */
1238 tty->driver_data = NULL;
1239
1240 free_irq(irq, hvcsd);
1241 kref_put(&hvcsd->kref, destroy_hvcs_struct);
1242 return;
1243 } else if (hvcsd->open_count < 0) {
1244 printk(KERN_ERR "HVCS: vty-server@%X open_count: %d"
1245 " is missmanaged.\n",
1246 hvcsd->vdev->unit_address, hvcsd->open_count);
1247 }
1248
1249 spin_unlock_irqrestore(&hvcsd->lock, flags);
1250 kref_put(&hvcsd->kref, destroy_hvcs_struct);
1251}
1252
1253static void hvcs_hangup(struct tty_struct * tty)
1254{
1255 struct hvcs_struct *hvcsd = tty->driver_data;
1256 unsigned long flags;
1257 int temp_open_count;
1258 int irq = NO_IRQ;
1259
1260 spin_lock_irqsave(&hvcsd->lock, flags);
1261 /* Preserve this so that we know how many kref refs to put */
1262 temp_open_count = hvcsd->open_count;
1263
1264 /*
1265 * Don't kref put inside the spinlock because the destruction
1266 * callback may use the spinlock and it may get called before the
1267 * spinlock has been released.
1268 */
1269 vio_disable_interrupts(hvcsd->vdev);
1270
1271 hvcsd->todo_mask = 0;
1272
1273 /* I don't think the tty needs the hvcs_struct pointer after a hangup */
1274 hvcsd->tty->driver_data = NULL;
1275 hvcsd->tty = NULL;
1276
1277 hvcsd->open_count = 0;
1278
1279 /* This will drop any buffered data on the floor which is OK in a hangup
1280 * scenario. */
1281 memset(&hvcsd->buffer[0], 0x00, HVCS_BUFF_LEN);
1282 hvcsd->chars_in_buffer = 0;
1283
1284 irq = hvcsd->vdev->irq;
1285
1286 spin_unlock_irqrestore(&hvcsd->lock, flags);
1287
1288 free_irq(irq, hvcsd);
1289
1290 /*
1291 * We need to kref_put() for every open_count we have since the
1292 * tty_hangup() function doesn't invoke a close per open connection on a
1293 * non-console device.
1294 */
1295 while(temp_open_count) {
1296 --temp_open_count;
1297 /*
1298 * The final put will trigger destruction of the hvcs_struct.
1299 * NOTE: If this hangup was signaled from user space then the
1300 * final put will never happen.
1301 */
1302 kref_put(&hvcsd->kref, destroy_hvcs_struct);
1303 }
1304}
1305
1306/*
1307 * NOTE: This is almost always from_user since user level apps interact with the
1308 * /dev nodes. I'm trusting that if hvcs_write gets called and interrupted by
1309 * hvcs_remove (which removes the target device and executes tty_hangup()) that
1310 * tty_hangup will allow hvcs_write time to complete execution before it
1311 * terminates our device.
1312 */
1313static int hvcs_write(struct tty_struct *tty,
1314 const unsigned char *buf, int count)
1315{
1316 struct hvcs_struct *hvcsd = tty->driver_data;
1317 unsigned int unit_address;
1318 const unsigned char *charbuf;
1319 unsigned long flags;
1320 int total_sent = 0;
1321 int tosend = 0;
1322 int result = 0;
1323
1324 /*
1325 * If they don't check the return code off of their open they may
1326 * attempt this even if there is no connected device.
1327 */
1328 if (!hvcsd)
1329 return -ENODEV;
1330
1331 /* Reasonable size to prevent user level flooding */
1332 if (count > HVCS_MAX_FROM_USER) {
1333 printk(KERN_WARNING "HVCS write: count being truncated to"
1334 " HVCS_MAX_FROM_USER.\n");
1335 count = HVCS_MAX_FROM_USER;
1336 }
1337
1338 charbuf = buf;
1339
1340 spin_lock_irqsave(&hvcsd->lock, flags);
1341
1342 /*
1343 * Somehow an open succedded but the device was removed or the
1344 * connection terminated between the vty-server and partner vty during
1345 * the middle of a write operation? This is a crummy place to do this
1346 * but we want to keep it all in the spinlock.
1347 */
1348 if (hvcsd->open_count <= 0) {
1349 spin_unlock_irqrestore(&hvcsd->lock, flags);
1350 return -ENODEV;
1351 }
1352
1353 unit_address = hvcsd->vdev->unit_address;
1354
1355 while (count > 0) {
1356 tosend = min(count, (HVCS_BUFF_LEN - hvcsd->chars_in_buffer));
1357 /*
1358 * No more space, this probably means that the last call to
1359 * hvcs_write() didn't succeed and the buffer was filled up.
1360 */
1361 if (!tosend)
1362 break;
1363
1364 memcpy(&hvcsd->buffer[hvcsd->chars_in_buffer],
1365 &charbuf[total_sent],
1366 tosend);
1367
1368 hvcsd->chars_in_buffer += tosend;
1369
1370 result = 0;
1371
1372 /*
1373 * If this is true then we don't want to try writing to the
1374 * hypervisor because that is the kernel_threads job now. We'll
1375 * just add to the buffer.
1376 */
1377 if (!(hvcsd->todo_mask & HVCS_TRY_WRITE))
1378 /* won't send partial writes */
1379 result = hvc_put_chars(unit_address,
1380 &hvcsd->buffer[0],
1381 hvcsd->chars_in_buffer);
1382
1383 /*
1384 * Since we know we have enough room in hvcsd->buffer for
1385 * tosend we record that it was sent regardless of whether the
1386 * hypervisor actually took it because we have it buffered.
1387 */
1388 total_sent+=tosend;
1389 count-=tosend;
1390 if (result == 0) {
1391 hvcsd->todo_mask |= HVCS_TRY_WRITE;
1392 hvcs_kick();
1393 break;
1394 }
1395
1396 hvcsd->chars_in_buffer = 0;
1397 /*
1398 * Test after the chars_in_buffer reset otherwise this could
1399 * deadlock our writes if hvc_put_chars fails.
1400 */
1401 if (result < 0)
1402 break;
1403 }
1404
1405 spin_unlock_irqrestore(&hvcsd->lock, flags);
1406
1407 if (result == -1)
1408 return -EIO;
1409 else
1410 return total_sent;
1411}
1412
1413/*
1414 * This is really asking how much can we guarentee that we can send or that we
1415 * absolutely WILL BUFFER if we can't send it. This driver MUST honor the
1416 * return value, hence the reason for hvcs_struct buffering.
1417 */
1418static int hvcs_write_room(struct tty_struct *tty)
1419{
1420 struct hvcs_struct *hvcsd = tty->driver_data;
1421
1422 if (!hvcsd || hvcsd->open_count <= 0)
1423 return 0;
1424
1425 return HVCS_BUFF_LEN - hvcsd->chars_in_buffer;
1426}
1427
1428static int hvcs_chars_in_buffer(struct tty_struct *tty)
1429{
1430 struct hvcs_struct *hvcsd = tty->driver_data;
1431
1432 return hvcsd->chars_in_buffer;
1433}
1434
1435static const struct tty_operations hvcs_ops = {
1436 .open = hvcs_open,
1437 .close = hvcs_close,
1438 .hangup = hvcs_hangup,
1439 .write = hvcs_write,
1440 .write_room = hvcs_write_room,
1441 .chars_in_buffer = hvcs_chars_in_buffer,
1442 .unthrottle = hvcs_unthrottle,
1443 .throttle = hvcs_throttle,
1444};
1445
1446static int hvcs_alloc_index_list(int n)
1447{
1448 int i;
1449
1450 hvcs_index_list = kmalloc(n * sizeof(hvcs_index_count),GFP_KERNEL);
1451 if (!hvcs_index_list)
1452 return -ENOMEM;
1453 hvcs_index_count = n;
1454 for (i = 0; i < hvcs_index_count; i++)
1455 hvcs_index_list[i] = -1;
1456 return 0;
1457}
1458
1459static void hvcs_free_index_list(void)
1460{
1461 /* Paranoia check to be thorough. */
1462 kfree(hvcs_index_list);
1463 hvcs_index_list = NULL;
1464 hvcs_index_count = 0;
1465}
1466
1467static int __init hvcs_module_init(void)
1468{
1469 int rc;
1470 int num_ttys_to_alloc;
1471
1472 printk(KERN_INFO "Initializing %s\n", hvcs_driver_string);
1473
1474 /* Has the user specified an overload with an insmod param? */
1475 if (hvcs_parm_num_devs <= 0 ||
1476 (hvcs_parm_num_devs > HVCS_MAX_SERVER_ADAPTERS)) {
1477 num_ttys_to_alloc = HVCS_DEFAULT_SERVER_ADAPTERS;
1478 } else
1479 num_ttys_to_alloc = hvcs_parm_num_devs;
1480
1481 hvcs_tty_driver = alloc_tty_driver(num_ttys_to_alloc);
1482 if (!hvcs_tty_driver)
1483 return -ENOMEM;
1484
1485 if (hvcs_alloc_index_list(num_ttys_to_alloc)) {
1486 rc = -ENOMEM;
1487 goto index_fail;
1488 }
1489
1490 hvcs_tty_driver->owner = THIS_MODULE;
1491
1492 hvcs_tty_driver->driver_name = hvcs_driver_name;
1493 hvcs_tty_driver->name = hvcs_device_node;
1494
1495 /*
1496 * We'll let the system assign us a major number, indicated by leaving
1497 * it blank.
1498 */
1499
1500 hvcs_tty_driver->minor_start = HVCS_MINOR_START;
1501 hvcs_tty_driver->type = TTY_DRIVER_TYPE_SYSTEM;
1502
1503 /*
1504 * We role our own so that we DONT ECHO. We can't echo because the
1505 * device we are connecting to already echoes by default and this would
1506 * throw us into a horrible recursive echo-echo-echo loop.
1507 */
1508 hvcs_tty_driver->init_termios = hvcs_tty_termios;
1509 hvcs_tty_driver->flags = TTY_DRIVER_REAL_RAW;
1510
1511 tty_set_operations(hvcs_tty_driver, &hvcs_ops);
1512
1513 /*
1514 * The following call will result in sysfs entries that denote the
1515 * dynamically assigned major and minor numbers for our devices.
1516 */
1517 if (tty_register_driver(hvcs_tty_driver)) {
1518 printk(KERN_ERR "HVCS: registration as a tty driver failed.\n");
1519 rc = -EIO;
1520 goto register_fail;
1521 }
1522
1523 hvcs_pi_buff = kmalloc(PAGE_SIZE, GFP_KERNEL);
1524 if (!hvcs_pi_buff) {
1525 rc = -ENOMEM;
1526 goto buff_alloc_fail;
1527 }
1528
1529 hvcs_task = kthread_run(khvcsd, NULL, "khvcsd");
1530 if (IS_ERR(hvcs_task)) {
1531 printk(KERN_ERR "HVCS: khvcsd creation failed. Driver not loaded.\n");
1532 rc = -EIO;
1533 goto kthread_fail;
1534 }
1535
1536 rc = vio_register_driver(&hvcs_vio_driver);
1537 if (rc) {
1538 printk(KERN_ERR "HVCS: can't register vio driver\n");
1539 goto vio_fail;
1540 }
1541
1542 /*
1543 * This needs to be done AFTER the vio_register_driver() call or else
1544 * the kobjects won't be initialized properly.
1545 */
1546 rc = driver_create_file(&(hvcs_vio_driver.driver), &driver_attr_rescan);
1547 if (rc) {
1548 printk(KERN_ERR "HVCS: sysfs attr create failed\n");
1549 goto attr_fail;
1550 }
1551
1552 printk(KERN_INFO "HVCS: driver module inserted.\n");
1553
1554 return 0;
1555
1556attr_fail:
1557 vio_unregister_driver(&hvcs_vio_driver);
1558vio_fail:
1559 kthread_stop(hvcs_task);
1560kthread_fail:
1561 kfree(hvcs_pi_buff);
1562buff_alloc_fail:
1563 tty_unregister_driver(hvcs_tty_driver);
1564register_fail:
1565 hvcs_free_index_list();
1566index_fail:
1567 put_tty_driver(hvcs_tty_driver);
1568 hvcs_tty_driver = NULL;
1569 return rc;
1570}
1571
1572static void __exit hvcs_module_exit(void)
1573{
1574 /*
1575 * This driver receives hvcs_remove callbacks for each device upon
1576 * module removal.
1577 */
1578
1579 /*
1580 * This synchronous operation will wake the khvcsd kthread if it is
1581 * asleep and will return when khvcsd has terminated.
1582 */
1583 kthread_stop(hvcs_task);
1584
1585 spin_lock(&hvcs_pi_lock);
1586 kfree(hvcs_pi_buff);
1587 hvcs_pi_buff = NULL;
1588 spin_unlock(&hvcs_pi_lock);
1589
1590 driver_remove_file(&hvcs_vio_driver.driver, &driver_attr_rescan);
1591
1592 vio_unregister_driver(&hvcs_vio_driver);
1593
1594 tty_unregister_driver(hvcs_tty_driver);
1595
1596 hvcs_free_index_list();
1597
1598 put_tty_driver(hvcs_tty_driver);
1599
1600 printk(KERN_INFO "HVCS: driver module removed.\n");
1601}
1602
1603module_init(hvcs_module_init);
1604module_exit(hvcs_module_exit);
diff --git a/drivers/char/hvsi.c b/drivers/char/hvsi.c
deleted file mode 100644
index a2bc885ce60a..000000000000
--- a/drivers/char/hvsi.c
+++ /dev/null
@@ -1,1314 +0,0 @@
1/*
2 * Copyright (C) 2004 Hollis Blanchard <hollisb@us.ibm.com>, IBM
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18
19/* Host Virtual Serial Interface (HVSI) is a protocol between the hosted OS
20 * and the service processor on IBM pSeries servers. On these servers, there
21 * are no serial ports under the OS's control, and sometimes there is no other
22 * console available either. However, the service processor has two standard
23 * serial ports, so this over-complicated protocol allows the OS to control
24 * those ports by proxy.
25 *
26 * Besides data, the procotol supports the reading/writing of the serial
27 * port's DTR line, and the reading of the CD line. This is to allow the OS to
28 * control a modem attached to the service processor's serial port. Note that
29 * the OS cannot change the speed of the port through this protocol.
30 */
31
32#undef DEBUG
33
34#include <linux/console.h>
35#include <linux/ctype.h>
36#include <linux/delay.h>
37#include <linux/init.h>
38#include <linux/interrupt.h>
39#include <linux/module.h>
40#include <linux/major.h>
41#include <linux/kernel.h>
42#include <linux/spinlock.h>
43#include <linux/sysrq.h>
44#include <linux/tty.h>
45#include <linux/tty_flip.h>
46#include <asm/hvcall.h>
47#include <asm/hvconsole.h>
48#include <asm/prom.h>
49#include <asm/uaccess.h>
50#include <asm/vio.h>
51#include <asm/param.h>
52
53#define HVSI_MAJOR 229
54#define HVSI_MINOR 128
55#define MAX_NR_HVSI_CONSOLES 4
56
57#define HVSI_TIMEOUT (5*HZ)
58#define HVSI_VERSION 1
59#define HVSI_MAX_PACKET 256
60#define HVSI_MAX_READ 16
61#define HVSI_MAX_OUTGOING_DATA 12
62#define N_OUTBUF 12
63
64/*
65 * we pass data via two 8-byte registers, so we would like our char arrays
66 * properly aligned for those loads.
67 */
68#define __ALIGNED__ __attribute__((__aligned__(sizeof(long))))
69
70struct hvsi_struct {
71 struct delayed_work writer;
72 struct work_struct handshaker;
73 wait_queue_head_t emptyq; /* woken when outbuf is emptied */
74 wait_queue_head_t stateq; /* woken when HVSI state changes */
75 spinlock_t lock;
76 int index;
77 struct tty_struct *tty;
78 int count;
79 uint8_t throttle_buf[128];
80 uint8_t outbuf[N_OUTBUF]; /* to implement write_room and chars_in_buffer */
81 /* inbuf is for packet reassembly. leave a little room for leftovers. */
82 uint8_t inbuf[HVSI_MAX_PACKET + HVSI_MAX_READ];
83 uint8_t *inbuf_end;
84 int n_throttle;
85 int n_outbuf;
86 uint32_t vtermno;
87 uint32_t virq;
88 atomic_t seqno; /* HVSI packet sequence number */
89 uint16_t mctrl;
90 uint8_t state; /* HVSI protocol state */
91 uint8_t flags;
92#ifdef CONFIG_MAGIC_SYSRQ
93 uint8_t sysrq;
94#endif /* CONFIG_MAGIC_SYSRQ */
95};
96static struct hvsi_struct hvsi_ports[MAX_NR_HVSI_CONSOLES];
97
98static struct tty_driver *hvsi_driver;
99static int hvsi_count;
100static int (*hvsi_wait)(struct hvsi_struct *hp, int state);
101
102enum HVSI_PROTOCOL_STATE {
103 HVSI_CLOSED,
104 HVSI_WAIT_FOR_VER_RESPONSE,
105 HVSI_WAIT_FOR_VER_QUERY,
106 HVSI_OPEN,
107 HVSI_WAIT_FOR_MCTRL_RESPONSE,
108 HVSI_FSP_DIED,
109};
110#define HVSI_CONSOLE 0x1
111
112#define VS_DATA_PACKET_HEADER 0xff
113#define VS_CONTROL_PACKET_HEADER 0xfe
114#define VS_QUERY_PACKET_HEADER 0xfd
115#define VS_QUERY_RESPONSE_PACKET_HEADER 0xfc
116
117/* control verbs */
118#define VSV_SET_MODEM_CTL 1 /* to service processor only */
119#define VSV_MODEM_CTL_UPDATE 2 /* from service processor only */
120#define VSV_CLOSE_PROTOCOL 3
121
122/* query verbs */
123#define VSV_SEND_VERSION_NUMBER 1
124#define VSV_SEND_MODEM_CTL_STATUS 2
125
126/* yes, these masks are not consecutive. */
127#define HVSI_TSDTR 0x01
128#define HVSI_TSCD 0x20
129
130struct hvsi_header {
131 uint8_t type;
132 uint8_t len;
133 uint16_t seqno;
134} __attribute__((packed));
135
136struct hvsi_data {
137 uint8_t type;
138 uint8_t len;
139 uint16_t seqno;
140 uint8_t data[HVSI_MAX_OUTGOING_DATA];
141} __attribute__((packed));
142
143struct hvsi_control {
144 uint8_t type;
145 uint8_t len;
146 uint16_t seqno;
147 uint16_t verb;
148 /* optional depending on verb: */
149 uint32_t word;
150 uint32_t mask;
151} __attribute__((packed));
152
153struct hvsi_query {
154 uint8_t type;
155 uint8_t len;
156 uint16_t seqno;
157 uint16_t verb;
158} __attribute__((packed));
159
160struct hvsi_query_response {
161 uint8_t type;
162 uint8_t len;
163 uint16_t seqno;
164 uint16_t verb;
165 uint16_t query_seqno;
166 union {
167 uint8_t version;
168 uint32_t mctrl_word;
169 } u;
170} __attribute__((packed));
171
172
173
174static inline int is_console(struct hvsi_struct *hp)
175{
176 return hp->flags & HVSI_CONSOLE;
177}
178
179static inline int is_open(struct hvsi_struct *hp)
180{
181 /* if we're waiting for an mctrl then we're already open */
182 return (hp->state == HVSI_OPEN)
183 || (hp->state == HVSI_WAIT_FOR_MCTRL_RESPONSE);
184}
185
186static inline void print_state(struct hvsi_struct *hp)
187{
188#ifdef DEBUG
189 static const char *state_names[] = {
190 "HVSI_CLOSED",
191 "HVSI_WAIT_FOR_VER_RESPONSE",
192 "HVSI_WAIT_FOR_VER_QUERY",
193 "HVSI_OPEN",
194 "HVSI_WAIT_FOR_MCTRL_RESPONSE",
195 "HVSI_FSP_DIED",
196 };
197 const char *name = (hp->state < ARRAY_SIZE(state_names))
198 ? state_names[hp->state] : "UNKNOWN";
199
200 pr_debug("hvsi%i: state = %s\n", hp->index, name);
201#endif /* DEBUG */
202}
203
204static inline void __set_state(struct hvsi_struct *hp, int state)
205{
206 hp->state = state;
207 print_state(hp);
208 wake_up_all(&hp->stateq);
209}
210
211static inline void set_state(struct hvsi_struct *hp, int state)
212{
213 unsigned long flags;
214
215 spin_lock_irqsave(&hp->lock, flags);
216 __set_state(hp, state);
217 spin_unlock_irqrestore(&hp->lock, flags);
218}
219
220static inline int len_packet(const uint8_t *packet)
221{
222 return (int)((struct hvsi_header *)packet)->len;
223}
224
225static inline int is_header(const uint8_t *packet)
226{
227 struct hvsi_header *header = (struct hvsi_header *)packet;
228 return header->type >= VS_QUERY_RESPONSE_PACKET_HEADER;
229}
230
231static inline int got_packet(const struct hvsi_struct *hp, uint8_t *packet)
232{
233 if (hp->inbuf_end < packet + sizeof(struct hvsi_header))
234 return 0; /* don't even have the packet header */
235
236 if (hp->inbuf_end < (packet + len_packet(packet)))
237 return 0; /* don't have the rest of the packet */
238
239 return 1;
240}
241
242/* shift remaining bytes in packetbuf down */
243static void compact_inbuf(struct hvsi_struct *hp, uint8_t *read_to)
244{
245 int remaining = (int)(hp->inbuf_end - read_to);
246
247 pr_debug("%s: %i chars remain\n", __func__, remaining);
248
249 if (read_to != hp->inbuf)
250 memmove(hp->inbuf, read_to, remaining);
251
252 hp->inbuf_end = hp->inbuf + remaining;
253}
254
255#ifdef DEBUG
256#define dbg_dump_packet(packet) dump_packet(packet)
257#define dbg_dump_hex(data, len) dump_hex(data, len)
258#else
259#define dbg_dump_packet(packet) do { } while (0)
260#define dbg_dump_hex(data, len) do { } while (0)
261#endif
262
263static void dump_hex(const uint8_t *data, int len)
264{
265 int i;
266
267 printk(" ");
268 for (i=0; i < len; i++)
269 printk("%.2x", data[i]);
270
271 printk("\n ");
272 for (i=0; i < len; i++) {
273 if (isprint(data[i]))
274 printk("%c", data[i]);
275 else
276 printk(".");
277 }
278 printk("\n");
279}
280
281static void dump_packet(uint8_t *packet)
282{
283 struct hvsi_header *header = (struct hvsi_header *)packet;
284
285 printk("type 0x%x, len %i, seqno %i:\n", header->type, header->len,
286 header->seqno);
287
288 dump_hex(packet, header->len);
289}
290
291static int hvsi_read(struct hvsi_struct *hp, char *buf, int count)
292{
293 unsigned long got;
294
295 got = hvc_get_chars(hp->vtermno, buf, count);
296
297 return got;
298}
299
300static void hvsi_recv_control(struct hvsi_struct *hp, uint8_t *packet,
301 struct tty_struct **to_hangup, struct hvsi_struct **to_handshake)
302{
303 struct hvsi_control *header = (struct hvsi_control *)packet;
304
305 switch (header->verb) {
306 case VSV_MODEM_CTL_UPDATE:
307 if ((header->word & HVSI_TSCD) == 0) {
308 /* CD went away; no more connection */
309 pr_debug("hvsi%i: CD dropped\n", hp->index);
310 hp->mctrl &= TIOCM_CD;
311 /* If userland hasn't done an open(2) yet, hp->tty is NULL. */
312 if (hp->tty && !(hp->tty->flags & CLOCAL))
313 *to_hangup = hp->tty;
314 }
315 break;
316 case VSV_CLOSE_PROTOCOL:
317 pr_debug("hvsi%i: service processor came back\n", hp->index);
318 if (hp->state != HVSI_CLOSED) {
319 *to_handshake = hp;
320 }
321 break;
322 default:
323 printk(KERN_WARNING "hvsi%i: unknown HVSI control packet: ",
324 hp->index);
325 dump_packet(packet);
326 break;
327 }
328}
329
330static void hvsi_recv_response(struct hvsi_struct *hp, uint8_t *packet)
331{
332 struct hvsi_query_response *resp = (struct hvsi_query_response *)packet;
333
334 switch (hp->state) {
335 case HVSI_WAIT_FOR_VER_RESPONSE:
336 __set_state(hp, HVSI_WAIT_FOR_VER_QUERY);
337 break;
338 case HVSI_WAIT_FOR_MCTRL_RESPONSE:
339 hp->mctrl = 0;
340 if (resp->u.mctrl_word & HVSI_TSDTR)
341 hp->mctrl |= TIOCM_DTR;
342 if (resp->u.mctrl_word & HVSI_TSCD)
343 hp->mctrl |= TIOCM_CD;
344 __set_state(hp, HVSI_OPEN);
345 break;
346 default:
347 printk(KERN_ERR "hvsi%i: unexpected query response: ", hp->index);
348 dump_packet(packet);
349 break;
350 }
351}
352
353/* respond to service processor's version query */
354static int hvsi_version_respond(struct hvsi_struct *hp, uint16_t query_seqno)
355{
356 struct hvsi_query_response packet __ALIGNED__;
357 int wrote;
358
359 packet.type = VS_QUERY_RESPONSE_PACKET_HEADER;
360 packet.len = sizeof(struct hvsi_query_response);
361 packet.seqno = atomic_inc_return(&hp->seqno);
362 packet.verb = VSV_SEND_VERSION_NUMBER;
363 packet.u.version = HVSI_VERSION;
364 packet.query_seqno = query_seqno+1;
365
366 pr_debug("%s: sending %i bytes\n", __func__, packet.len);
367 dbg_dump_hex((uint8_t*)&packet, packet.len);
368
369 wrote = hvc_put_chars(hp->vtermno, (char *)&packet, packet.len);
370 if (wrote != packet.len) {
371 printk(KERN_ERR "hvsi%i: couldn't send query response!\n",
372 hp->index);
373 return -EIO;
374 }
375
376 return 0;
377}
378
379static void hvsi_recv_query(struct hvsi_struct *hp, uint8_t *packet)
380{
381 struct hvsi_query *query = (struct hvsi_query *)packet;
382
383 switch (hp->state) {
384 case HVSI_WAIT_FOR_VER_QUERY:
385 hvsi_version_respond(hp, query->seqno);
386 __set_state(hp, HVSI_OPEN);
387 break;
388 default:
389 printk(KERN_ERR "hvsi%i: unexpected query: ", hp->index);
390 dump_packet(packet);
391 break;
392 }
393}
394
395static void hvsi_insert_chars(struct hvsi_struct *hp, const char *buf, int len)
396{
397 int i;
398
399 for (i=0; i < len; i++) {
400 char c = buf[i];
401#ifdef CONFIG_MAGIC_SYSRQ
402 if (c == '\0') {
403 hp->sysrq = 1;
404 continue;
405 } else if (hp->sysrq) {
406 handle_sysrq(c);
407 hp->sysrq = 0;
408 continue;
409 }
410#endif /* CONFIG_MAGIC_SYSRQ */
411 tty_insert_flip_char(hp->tty, c, 0);
412 }
413}
414
415/*
416 * We could get 252 bytes of data at once here. But the tty layer only
417 * throttles us at TTY_THRESHOLD_THROTTLE (128) bytes, so we could overflow
418 * it. Accordingly we won't send more than 128 bytes at a time to the flip
419 * buffer, which will give the tty buffer a chance to throttle us. Should the
420 * value of TTY_THRESHOLD_THROTTLE change in n_tty.c, this code should be
421 * revisited.
422 */
423#define TTY_THRESHOLD_THROTTLE 128
424static struct tty_struct *hvsi_recv_data(struct hvsi_struct *hp,
425 const uint8_t *packet)
426{
427 const struct hvsi_header *header = (const struct hvsi_header *)packet;
428 const uint8_t *data = packet + sizeof(struct hvsi_header);
429 int datalen = header->len - sizeof(struct hvsi_header);
430 int overflow = datalen - TTY_THRESHOLD_THROTTLE;
431
432 pr_debug("queueing %i chars '%.*s'\n", datalen, datalen, data);
433
434 if (datalen == 0)
435 return NULL;
436
437 if (overflow > 0) {
438 pr_debug("%s: got >TTY_THRESHOLD_THROTTLE bytes\n", __func__);
439 datalen = TTY_THRESHOLD_THROTTLE;
440 }
441
442 hvsi_insert_chars(hp, data, datalen);
443
444 if (overflow > 0) {
445 /*
446 * we still have more data to deliver, so we need to save off the
447 * overflow and send it later
448 */
449 pr_debug("%s: deferring overflow\n", __func__);
450 memcpy(hp->throttle_buf, data + TTY_THRESHOLD_THROTTLE, overflow);
451 hp->n_throttle = overflow;
452 }
453
454 return hp->tty;
455}
456
457/*
458 * Returns true/false indicating data successfully read from hypervisor.
459 * Used both to get packets for tty connections and to advance the state
460 * machine during console handshaking (in which case tty = NULL and we ignore
461 * incoming data).
462 */
463static int hvsi_load_chunk(struct hvsi_struct *hp, struct tty_struct **flip,
464 struct tty_struct **hangup, struct hvsi_struct **handshake)
465{
466 uint8_t *packet = hp->inbuf;
467 int chunklen;
468
469 *flip = NULL;
470 *hangup = NULL;
471 *handshake = NULL;
472
473 chunklen = hvsi_read(hp, hp->inbuf_end, HVSI_MAX_READ);
474 if (chunklen == 0) {
475 pr_debug("%s: 0-length read\n", __func__);
476 return 0;
477 }
478
479 pr_debug("%s: got %i bytes\n", __func__, chunklen);
480 dbg_dump_hex(hp->inbuf_end, chunklen);
481
482 hp->inbuf_end += chunklen;
483
484 /* handle all completed packets */
485 while ((packet < hp->inbuf_end) && got_packet(hp, packet)) {
486 struct hvsi_header *header = (struct hvsi_header *)packet;
487
488 if (!is_header(packet)) {
489 printk(KERN_ERR "hvsi%i: got malformed packet\n", hp->index);
490 /* skip bytes until we find a header or run out of data */
491 while ((packet < hp->inbuf_end) && (!is_header(packet)))
492 packet++;
493 continue;
494 }
495
496 pr_debug("%s: handling %i-byte packet\n", __func__,
497 len_packet(packet));
498 dbg_dump_packet(packet);
499
500 switch (header->type) {
501 case VS_DATA_PACKET_HEADER:
502 if (!is_open(hp))
503 break;
504 if (hp->tty == NULL)
505 break; /* no tty buffer to put data in */
506 *flip = hvsi_recv_data(hp, packet);
507 break;
508 case VS_CONTROL_PACKET_HEADER:
509 hvsi_recv_control(hp, packet, hangup, handshake);
510 break;
511 case VS_QUERY_RESPONSE_PACKET_HEADER:
512 hvsi_recv_response(hp, packet);
513 break;
514 case VS_QUERY_PACKET_HEADER:
515 hvsi_recv_query(hp, packet);
516 break;
517 default:
518 printk(KERN_ERR "hvsi%i: unknown HVSI packet type 0x%x\n",
519 hp->index, header->type);
520 dump_packet(packet);
521 break;
522 }
523
524 packet += len_packet(packet);
525
526 if (*hangup || *handshake) {
527 pr_debug("%s: hangup or handshake\n", __func__);
528 /*
529 * we need to send the hangup now before receiving any more data.
530 * If we get "data, hangup, data", we can't deliver the second
531 * data before the hangup.
532 */
533 break;
534 }
535 }
536
537 compact_inbuf(hp, packet);
538
539 return 1;
540}
541
542static void hvsi_send_overflow(struct hvsi_struct *hp)
543{
544 pr_debug("%s: delivering %i bytes overflow\n", __func__,
545 hp->n_throttle);
546
547 hvsi_insert_chars(hp, hp->throttle_buf, hp->n_throttle);
548 hp->n_throttle = 0;
549}
550
551/*
552 * must get all pending data because we only get an irq on empty->non-empty
553 * transition
554 */
555static irqreturn_t hvsi_interrupt(int irq, void *arg)
556{
557 struct hvsi_struct *hp = (struct hvsi_struct *)arg;
558 struct tty_struct *flip;
559 struct tty_struct *hangup;
560 struct hvsi_struct *handshake;
561 unsigned long flags;
562 int again = 1;
563
564 pr_debug("%s\n", __func__);
565
566 while (again) {
567 spin_lock_irqsave(&hp->lock, flags);
568 again = hvsi_load_chunk(hp, &flip, &hangup, &handshake);
569 spin_unlock_irqrestore(&hp->lock, flags);
570
571 /*
572 * we have to call tty_flip_buffer_push() and tty_hangup() outside our
573 * spinlock. But we also have to keep going until we've read all the
574 * available data.
575 */
576
577 if (flip) {
578 /* there was data put in the tty flip buffer */
579 tty_flip_buffer_push(flip);
580 flip = NULL;
581 }
582
583 if (hangup) {
584 tty_hangup(hangup);
585 }
586
587 if (handshake) {
588 pr_debug("hvsi%i: attempting re-handshake\n", handshake->index);
589 schedule_work(&handshake->handshaker);
590 }
591 }
592
593 spin_lock_irqsave(&hp->lock, flags);
594 if (hp->tty && hp->n_throttle
595 && (!test_bit(TTY_THROTTLED, &hp->tty->flags))) {
596 /* we weren't hung up and we weren't throttled, so we can deliver the
597 * rest now */
598 flip = hp->tty;
599 hvsi_send_overflow(hp);
600 }
601 spin_unlock_irqrestore(&hp->lock, flags);
602
603 if (flip) {
604 tty_flip_buffer_push(flip);
605 }
606
607 return IRQ_HANDLED;
608}
609
610/* for boot console, before the irq handler is running */
611static int __init poll_for_state(struct hvsi_struct *hp, int state)
612{
613 unsigned long end_jiffies = jiffies + HVSI_TIMEOUT;
614
615 for (;;) {
616 hvsi_interrupt(hp->virq, (void *)hp); /* get pending data */
617
618 if (hp->state == state)
619 return 0;
620
621 mdelay(5);
622 if (time_after(jiffies, end_jiffies))
623 return -EIO;
624 }
625}
626
627/* wait for irq handler to change our state */
628static int wait_for_state(struct hvsi_struct *hp, int state)
629{
630 int ret = 0;
631
632 if (!wait_event_timeout(hp->stateq, (hp->state == state), HVSI_TIMEOUT))
633 ret = -EIO;
634
635 return ret;
636}
637
638static int hvsi_query(struct hvsi_struct *hp, uint16_t verb)
639{
640 struct hvsi_query packet __ALIGNED__;
641 int wrote;
642
643 packet.type = VS_QUERY_PACKET_HEADER;
644 packet.len = sizeof(struct hvsi_query);
645 packet.seqno = atomic_inc_return(&hp->seqno);
646 packet.verb = verb;
647
648 pr_debug("%s: sending %i bytes\n", __func__, packet.len);
649 dbg_dump_hex((uint8_t*)&packet, packet.len);
650
651 wrote = hvc_put_chars(hp->vtermno, (char *)&packet, packet.len);
652 if (wrote != packet.len) {
653 printk(KERN_ERR "hvsi%i: couldn't send query (%i)!\n", hp->index,
654 wrote);
655 return -EIO;
656 }
657
658 return 0;
659}
660
661static int hvsi_get_mctrl(struct hvsi_struct *hp)
662{
663 int ret;
664
665 set_state(hp, HVSI_WAIT_FOR_MCTRL_RESPONSE);
666 hvsi_query(hp, VSV_SEND_MODEM_CTL_STATUS);
667
668 ret = hvsi_wait(hp, HVSI_OPEN);
669 if (ret < 0) {
670 printk(KERN_ERR "hvsi%i: didn't get modem flags\n", hp->index);
671 set_state(hp, HVSI_OPEN);
672 return ret;
673 }
674
675 pr_debug("%s: mctrl 0x%x\n", __func__, hp->mctrl);
676
677 return 0;
678}
679
680/* note that we can only set DTR */
681static int hvsi_set_mctrl(struct hvsi_struct *hp, uint16_t mctrl)
682{
683 struct hvsi_control packet __ALIGNED__;
684 int wrote;
685
686 packet.type = VS_CONTROL_PACKET_HEADER,
687 packet.seqno = atomic_inc_return(&hp->seqno);
688 packet.len = sizeof(struct hvsi_control);
689 packet.verb = VSV_SET_MODEM_CTL;
690 packet.mask = HVSI_TSDTR;
691
692 if (mctrl & TIOCM_DTR)
693 packet.word = HVSI_TSDTR;
694
695 pr_debug("%s: sending %i bytes\n", __func__, packet.len);
696 dbg_dump_hex((uint8_t*)&packet, packet.len);
697
698 wrote = hvc_put_chars(hp->vtermno, (char *)&packet, packet.len);
699 if (wrote != packet.len) {
700 printk(KERN_ERR "hvsi%i: couldn't set DTR!\n", hp->index);
701 return -EIO;
702 }
703
704 return 0;
705}
706
707static void hvsi_drain_input(struct hvsi_struct *hp)
708{
709 uint8_t buf[HVSI_MAX_READ] __ALIGNED__;
710 unsigned long end_jiffies = jiffies + HVSI_TIMEOUT;
711
712 while (time_before(end_jiffies, jiffies))
713 if (0 == hvsi_read(hp, buf, HVSI_MAX_READ))
714 break;
715}
716
717static int hvsi_handshake(struct hvsi_struct *hp)
718{
719 int ret;
720
721 /*
722 * We could have a CLOSE or other data waiting for us before we even try
723 * to open; try to throw it all away so we don't get confused. (CLOSE
724 * is the first message sent up the pipe when the FSP comes online. We
725 * need to distinguish between "it came up a while ago and we're the first
726 * user" and "it was just reset before it saw our handshake packet".)
727 */
728 hvsi_drain_input(hp);
729
730 set_state(hp, HVSI_WAIT_FOR_VER_RESPONSE);
731 ret = hvsi_query(hp, VSV_SEND_VERSION_NUMBER);
732 if (ret < 0) {
733 printk(KERN_ERR "hvsi%i: couldn't send version query\n", hp->index);
734 return ret;
735 }
736
737 ret = hvsi_wait(hp, HVSI_OPEN);
738 if (ret < 0)
739 return ret;
740
741 return 0;
742}
743
744static void hvsi_handshaker(struct work_struct *work)
745{
746 struct hvsi_struct *hp =
747 container_of(work, struct hvsi_struct, handshaker);
748
749 if (hvsi_handshake(hp) >= 0)
750 return;
751
752 printk(KERN_ERR "hvsi%i: re-handshaking failed\n", hp->index);
753 if (is_console(hp)) {
754 /*
755 * ttys will re-attempt the handshake via hvsi_open, but
756 * the console will not.
757 */
758 printk(KERN_ERR "hvsi%i: lost console!\n", hp->index);
759 }
760}
761
762static int hvsi_put_chars(struct hvsi_struct *hp, const char *buf, int count)
763{
764 struct hvsi_data packet __ALIGNED__;
765 int ret;
766
767 BUG_ON(count > HVSI_MAX_OUTGOING_DATA);
768
769 packet.type = VS_DATA_PACKET_HEADER;
770 packet.seqno = atomic_inc_return(&hp->seqno);
771 packet.len = count + sizeof(struct hvsi_header);
772 memcpy(&packet.data, buf, count);
773
774 ret = hvc_put_chars(hp->vtermno, (char *)&packet, packet.len);
775 if (ret == packet.len) {
776 /* return the number of chars written, not the packet length */
777 return count;
778 }
779 return ret; /* return any errors */
780}
781
782static void hvsi_close_protocol(struct hvsi_struct *hp)
783{
784 struct hvsi_control packet __ALIGNED__;
785
786 packet.type = VS_CONTROL_PACKET_HEADER;
787 packet.seqno = atomic_inc_return(&hp->seqno);
788 packet.len = 6;
789 packet.verb = VSV_CLOSE_PROTOCOL;
790
791 pr_debug("%s: sending %i bytes\n", __func__, packet.len);
792 dbg_dump_hex((uint8_t*)&packet, packet.len);
793
794 hvc_put_chars(hp->vtermno, (char *)&packet, packet.len);
795}
796
797static int hvsi_open(struct tty_struct *tty, struct file *filp)
798{
799 struct hvsi_struct *hp;
800 unsigned long flags;
801 int line = tty->index;
802 int ret;
803
804 pr_debug("%s\n", __func__);
805
806 if (line < 0 || line >= hvsi_count)
807 return -ENODEV;
808 hp = &hvsi_ports[line];
809
810 tty->driver_data = hp;
811
812 mb();
813 if (hp->state == HVSI_FSP_DIED)
814 return -EIO;
815
816 spin_lock_irqsave(&hp->lock, flags);
817 hp->tty = tty;
818 hp->count++;
819 atomic_set(&hp->seqno, 0);
820 h_vio_signal(hp->vtermno, VIO_IRQ_ENABLE);
821 spin_unlock_irqrestore(&hp->lock, flags);
822
823 if (is_console(hp))
824 return 0; /* this has already been handshaked as the console */
825
826 ret = hvsi_handshake(hp);
827 if (ret < 0) {
828 printk(KERN_ERR "%s: HVSI handshaking failed\n", tty->name);
829 return ret;
830 }
831
832 ret = hvsi_get_mctrl(hp);
833 if (ret < 0) {
834 printk(KERN_ERR "%s: couldn't get initial modem flags\n", tty->name);
835 return ret;
836 }
837
838 ret = hvsi_set_mctrl(hp, hp->mctrl | TIOCM_DTR);
839 if (ret < 0) {
840 printk(KERN_ERR "%s: couldn't set DTR\n", tty->name);
841 return ret;
842 }
843
844 return 0;
845}
846
847/* wait for hvsi_write_worker to empty hp->outbuf */
848static void hvsi_flush_output(struct hvsi_struct *hp)
849{
850 wait_event_timeout(hp->emptyq, (hp->n_outbuf <= 0), HVSI_TIMEOUT);
851
852 /* 'writer' could still be pending if it didn't see n_outbuf = 0 yet */
853 cancel_delayed_work(&hp->writer);
854 flush_scheduled_work();
855
856 /*
857 * it's also possible that our timeout expired and hvsi_write_worker
858 * didn't manage to push outbuf. poof.
859 */
860 hp->n_outbuf = 0;
861}
862
863static void hvsi_close(struct tty_struct *tty, struct file *filp)
864{
865 struct hvsi_struct *hp = tty->driver_data;
866 unsigned long flags;
867
868 pr_debug("%s\n", __func__);
869
870 if (tty_hung_up_p(filp))
871 return;
872
873 spin_lock_irqsave(&hp->lock, flags);
874
875 if (--hp->count == 0) {
876 hp->tty = NULL;
877 hp->inbuf_end = hp->inbuf; /* discard remaining partial packets */
878
879 /* only close down connection if it is not the console */
880 if (!is_console(hp)) {
881 h_vio_signal(hp->vtermno, VIO_IRQ_DISABLE); /* no more irqs */
882 __set_state(hp, HVSI_CLOSED);
883 /*
884 * any data delivered to the tty layer after this will be
885 * discarded (except for XON/XOFF)
886 */
887 tty->closing = 1;
888
889 spin_unlock_irqrestore(&hp->lock, flags);
890
891 /* let any existing irq handlers finish. no more will start. */
892 synchronize_irq(hp->virq);
893
894 /* hvsi_write_worker will re-schedule until outbuf is empty. */
895 hvsi_flush_output(hp);
896
897 /* tell FSP to stop sending data */
898 hvsi_close_protocol(hp);
899
900 /*
901 * drain anything FSP is still in the middle of sending, and let
902 * hvsi_handshake drain the rest on the next open.
903 */
904 hvsi_drain_input(hp);
905
906 spin_lock_irqsave(&hp->lock, flags);
907 }
908 } else if (hp->count < 0)
909 printk(KERN_ERR "hvsi_close %lu: oops, count is %d\n",
910 hp - hvsi_ports, hp->count);
911
912 spin_unlock_irqrestore(&hp->lock, flags);
913}
914
915static void hvsi_hangup(struct tty_struct *tty)
916{
917 struct hvsi_struct *hp = tty->driver_data;
918 unsigned long flags;
919
920 pr_debug("%s\n", __func__);
921
922 spin_lock_irqsave(&hp->lock, flags);
923
924 hp->count = 0;
925 hp->n_outbuf = 0;
926 hp->tty = NULL;
927
928 spin_unlock_irqrestore(&hp->lock, flags);
929}
930
931/* called with hp->lock held */
932static void hvsi_push(struct hvsi_struct *hp)
933{
934 int n;
935
936 if (hp->n_outbuf <= 0)
937 return;
938
939 n = hvsi_put_chars(hp, hp->outbuf, hp->n_outbuf);
940 if (n > 0) {
941 /* success */
942 pr_debug("%s: wrote %i chars\n", __func__, n);
943 hp->n_outbuf = 0;
944 } else if (n == -EIO) {
945 __set_state(hp, HVSI_FSP_DIED);
946 printk(KERN_ERR "hvsi%i: service processor died\n", hp->index);
947 }
948}
949
950/* hvsi_write_worker will keep rescheduling itself until outbuf is empty */
951static void hvsi_write_worker(struct work_struct *work)
952{
953 struct hvsi_struct *hp =
954 container_of(work, struct hvsi_struct, writer.work);
955 unsigned long flags;
956#ifdef DEBUG
957 static long start_j = 0;
958
959 if (start_j == 0)
960 start_j = jiffies;
961#endif /* DEBUG */
962
963 spin_lock_irqsave(&hp->lock, flags);
964
965 pr_debug("%s: %i chars in buffer\n", __func__, hp->n_outbuf);
966
967 if (!is_open(hp)) {
968 /*
969 * We could have a non-open connection if the service processor died
970 * while we were busily scheduling ourselves. In that case, it could
971 * be minutes before the service processor comes back, so only try
972 * again once a second.
973 */
974 schedule_delayed_work(&hp->writer, HZ);
975 goto out;
976 }
977
978 hvsi_push(hp);
979 if (hp->n_outbuf > 0)
980 schedule_delayed_work(&hp->writer, 10);
981 else {
982#ifdef DEBUG
983 pr_debug("%s: outbuf emptied after %li jiffies\n", __func__,
984 jiffies - start_j);
985 start_j = 0;
986#endif /* DEBUG */
987 wake_up_all(&hp->emptyq);
988 tty_wakeup(hp->tty);
989 }
990
991out:
992 spin_unlock_irqrestore(&hp->lock, flags);
993}
994
995static int hvsi_write_room(struct tty_struct *tty)
996{
997 struct hvsi_struct *hp = tty->driver_data;
998
999 return N_OUTBUF - hp->n_outbuf;
1000}
1001
1002static int hvsi_chars_in_buffer(struct tty_struct *tty)
1003{
1004 struct hvsi_struct *hp = tty->driver_data;
1005
1006 return hp->n_outbuf;
1007}
1008
1009static int hvsi_write(struct tty_struct *tty,
1010 const unsigned char *buf, int count)
1011{
1012 struct hvsi_struct *hp = tty->driver_data;
1013 const char *source = buf;
1014 unsigned long flags;
1015 int total = 0;
1016 int origcount = count;
1017
1018 spin_lock_irqsave(&hp->lock, flags);
1019
1020 pr_debug("%s: %i chars in buffer\n", __func__, hp->n_outbuf);
1021
1022 if (!is_open(hp)) {
1023 /* we're either closing or not yet open; don't accept data */
1024 pr_debug("%s: not open\n", __func__);
1025 goto out;
1026 }
1027
1028 /*
1029 * when the hypervisor buffer (16K) fills, data will stay in hp->outbuf
1030 * and hvsi_write_worker will be scheduled. subsequent hvsi_write() calls
1031 * will see there is no room in outbuf and return.
1032 */
1033 while ((count > 0) && (hvsi_write_room(hp->tty) > 0)) {
1034 int chunksize = min(count, hvsi_write_room(hp->tty));
1035
1036 BUG_ON(hp->n_outbuf < 0);
1037 memcpy(hp->outbuf + hp->n_outbuf, source, chunksize);
1038 hp->n_outbuf += chunksize;
1039
1040 total += chunksize;
1041 source += chunksize;
1042 count -= chunksize;
1043 hvsi_push(hp);
1044 }
1045
1046 if (hp->n_outbuf > 0) {
1047 /*
1048 * we weren't able to write it all to the hypervisor.
1049 * schedule another push attempt.
1050 */
1051 schedule_delayed_work(&hp->writer, 10);
1052 }
1053
1054out:
1055 spin_unlock_irqrestore(&hp->lock, flags);
1056
1057 if (total != origcount)
1058 pr_debug("%s: wanted %i, only wrote %i\n", __func__, origcount,
1059 total);
1060
1061 return total;
1062}
1063
1064/*
1065 * I have never seen throttle or unthrottle called, so this little throttle
1066 * buffering scheme may or may not work.
1067 */
1068static void hvsi_throttle(struct tty_struct *tty)
1069{
1070 struct hvsi_struct *hp = tty->driver_data;
1071
1072 pr_debug("%s\n", __func__);
1073
1074 h_vio_signal(hp->vtermno, VIO_IRQ_DISABLE);
1075}
1076
1077static void hvsi_unthrottle(struct tty_struct *tty)
1078{
1079 struct hvsi_struct *hp = tty->driver_data;
1080 unsigned long flags;
1081 int shouldflip = 0;
1082
1083 pr_debug("%s\n", __func__);
1084
1085 spin_lock_irqsave(&hp->lock, flags);
1086 if (hp->n_throttle) {
1087 hvsi_send_overflow(hp);
1088 shouldflip = 1;
1089 }
1090 spin_unlock_irqrestore(&hp->lock, flags);
1091
1092 if (shouldflip)
1093 tty_flip_buffer_push(hp->tty);
1094
1095 h_vio_signal(hp->vtermno, VIO_IRQ_ENABLE);
1096}
1097
1098static int hvsi_tiocmget(struct tty_struct *tty, struct file *file)
1099{
1100 struct hvsi_struct *hp = tty->driver_data;
1101
1102 hvsi_get_mctrl(hp);
1103 return hp->mctrl;
1104}
1105
1106static int hvsi_tiocmset(struct tty_struct *tty, struct file *file,
1107 unsigned int set, unsigned int clear)
1108{
1109 struct hvsi_struct *hp = tty->driver_data;
1110 unsigned long flags;
1111 uint16_t new_mctrl;
1112
1113 /* we can only alter DTR */
1114 clear &= TIOCM_DTR;
1115 set &= TIOCM_DTR;
1116
1117 spin_lock_irqsave(&hp->lock, flags);
1118
1119 new_mctrl = (hp->mctrl & ~clear) | set;
1120
1121 if (hp->mctrl != new_mctrl) {
1122 hvsi_set_mctrl(hp, new_mctrl);
1123 hp->mctrl = new_mctrl;
1124 }
1125 spin_unlock_irqrestore(&hp->lock, flags);
1126
1127 return 0;
1128}
1129
1130
1131static const struct tty_operations hvsi_ops = {
1132 .open = hvsi_open,
1133 .close = hvsi_close,
1134 .write = hvsi_write,
1135 .hangup = hvsi_hangup,
1136 .write_room = hvsi_write_room,
1137 .chars_in_buffer = hvsi_chars_in_buffer,
1138 .throttle = hvsi_throttle,
1139 .unthrottle = hvsi_unthrottle,
1140 .tiocmget = hvsi_tiocmget,
1141 .tiocmset = hvsi_tiocmset,
1142};
1143
1144static int __init hvsi_init(void)
1145{
1146 int i;
1147
1148 hvsi_driver = alloc_tty_driver(hvsi_count);
1149 if (!hvsi_driver)
1150 return -ENOMEM;
1151
1152 hvsi_driver->owner = THIS_MODULE;
1153 hvsi_driver->driver_name = "hvsi";
1154 hvsi_driver->name = "hvsi";
1155 hvsi_driver->major = HVSI_MAJOR;
1156 hvsi_driver->minor_start = HVSI_MINOR;
1157 hvsi_driver->type = TTY_DRIVER_TYPE_SYSTEM;
1158 hvsi_driver->init_termios = tty_std_termios;
1159 hvsi_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL;
1160 hvsi_driver->init_termios.c_ispeed = 9600;
1161 hvsi_driver->init_termios.c_ospeed = 9600;
1162 hvsi_driver->flags = TTY_DRIVER_REAL_RAW;
1163 tty_set_operations(hvsi_driver, &hvsi_ops);
1164
1165 for (i=0; i < hvsi_count; i++) {
1166 struct hvsi_struct *hp = &hvsi_ports[i];
1167 int ret = 1;
1168
1169 ret = request_irq(hp->virq, hvsi_interrupt, IRQF_DISABLED, "hvsi", hp);
1170 if (ret)
1171 printk(KERN_ERR "HVSI: couldn't reserve irq 0x%x (error %i)\n",
1172 hp->virq, ret);
1173 }
1174 hvsi_wait = wait_for_state; /* irqs active now */
1175
1176 if (tty_register_driver(hvsi_driver))
1177 panic("Couldn't register hvsi console driver\n");
1178
1179 printk(KERN_DEBUG "HVSI: registered %i devices\n", hvsi_count);
1180
1181 return 0;
1182}
1183device_initcall(hvsi_init);
1184
1185/***** console (not tty) code: *****/
1186
1187static void hvsi_console_print(struct console *console, const char *buf,
1188 unsigned int count)
1189{
1190 struct hvsi_struct *hp = &hvsi_ports[console->index];
1191 char c[HVSI_MAX_OUTGOING_DATA] __ALIGNED__;
1192 unsigned int i = 0, n = 0;
1193 int ret, donecr = 0;
1194
1195 mb();
1196 if (!is_open(hp))
1197 return;
1198
1199 /*
1200 * ugh, we have to translate LF -> CRLF ourselves, in place.
1201 * copied from hvc_console.c:
1202 */
1203 while (count > 0 || i > 0) {
1204 if (count > 0 && i < sizeof(c)) {
1205 if (buf[n] == '\n' && !donecr) {
1206 c[i++] = '\r';
1207 donecr = 1;
1208 } else {
1209 c[i++] = buf[n++];
1210 donecr = 0;
1211 --count;
1212 }
1213 } else {
1214 ret = hvsi_put_chars(hp, c, i);
1215 if (ret < 0)
1216 i = 0;
1217 i -= ret;
1218 }
1219 }
1220}
1221
1222static struct tty_driver *hvsi_console_device(struct console *console,
1223 int *index)
1224{
1225 *index = console->index;
1226 return hvsi_driver;
1227}
1228
1229static int __init hvsi_console_setup(struct console *console, char *options)
1230{
1231 struct hvsi_struct *hp;
1232 int ret;
1233
1234 if (console->index < 0 || console->index >= hvsi_count)
1235 return -1;
1236 hp = &hvsi_ports[console->index];
1237
1238 /* give the FSP a chance to change the baud rate when we re-open */
1239 hvsi_close_protocol(hp);
1240
1241 ret = hvsi_handshake(hp);
1242 if (ret < 0)
1243 return ret;
1244
1245 ret = hvsi_get_mctrl(hp);
1246 if (ret < 0)
1247 return ret;
1248
1249 ret = hvsi_set_mctrl(hp, hp->mctrl | TIOCM_DTR);
1250 if (ret < 0)
1251 return ret;
1252
1253 hp->flags |= HVSI_CONSOLE;
1254
1255 return 0;
1256}
1257
1258static struct console hvsi_console = {
1259 .name = "hvsi",
1260 .write = hvsi_console_print,
1261 .device = hvsi_console_device,
1262 .setup = hvsi_console_setup,
1263 .flags = CON_PRINTBUFFER,
1264 .index = -1,
1265};
1266
1267static int __init hvsi_console_init(void)
1268{
1269 struct device_node *vty;
1270
1271 hvsi_wait = poll_for_state; /* no irqs yet; must poll */
1272
1273 /* search device tree for vty nodes */
1274 for (vty = of_find_compatible_node(NULL, "serial", "hvterm-protocol");
1275 vty != NULL;
1276 vty = of_find_compatible_node(vty, "serial", "hvterm-protocol")) {
1277 struct hvsi_struct *hp;
1278 const uint32_t *vtermno, *irq;
1279
1280 vtermno = of_get_property(vty, "reg", NULL);
1281 irq = of_get_property(vty, "interrupts", NULL);
1282 if (!vtermno || !irq)
1283 continue;
1284
1285 if (hvsi_count >= MAX_NR_HVSI_CONSOLES) {
1286 of_node_put(vty);
1287 break;
1288 }
1289
1290 hp = &hvsi_ports[hvsi_count];
1291 INIT_DELAYED_WORK(&hp->writer, hvsi_write_worker);
1292 INIT_WORK(&hp->handshaker, hvsi_handshaker);
1293 init_waitqueue_head(&hp->emptyq);
1294 init_waitqueue_head(&hp->stateq);
1295 spin_lock_init(&hp->lock);
1296 hp->index = hvsi_count;
1297 hp->inbuf_end = hp->inbuf;
1298 hp->state = HVSI_CLOSED;
1299 hp->vtermno = *vtermno;
1300 hp->virq = irq_create_mapping(NULL, irq[0]);
1301 if (hp->virq == NO_IRQ) {
1302 printk(KERN_ERR "%s: couldn't create irq mapping for 0x%x\n",
1303 __func__, irq[0]);
1304 continue;
1305 }
1306
1307 hvsi_count++;
1308 }
1309
1310 if (hvsi_count)
1311 register_console(&hvsi_console);
1312 return 0;
1313}
1314console_initcall(hvsi_console_init);
diff --git a/drivers/char/hw_random/Kconfig b/drivers/char/hw_random/Kconfig
index d31483c54883..a60043b3e409 100644
--- a/drivers/char/hw_random/Kconfig
+++ b/drivers/char/hw_random/Kconfig
@@ -49,7 +49,7 @@ config HW_RANDOM_INTEL
49 49
50config HW_RANDOM_AMD 50config HW_RANDOM_AMD
51 tristate "AMD HW Random Number Generator support" 51 tristate "AMD HW Random Number Generator support"
52 depends on HW_RANDOM && X86 && PCI 52 depends on HW_RANDOM && (X86 || PPC_MAPLE) && PCI
53 default HW_RANDOM 53 default HW_RANDOM
54 ---help--- 54 ---help---
55 This driver provides kernel-side support for the Random Number 55 This driver provides kernel-side support for the Random Number
@@ -198,3 +198,15 @@ config HW_RANDOM_NOMADIK
198 module will be called nomadik-rng. 198 module will be called nomadik-rng.
199 199
200 If unsure, say Y. 200 If unsure, say Y.
201
202config HW_RANDOM_PICOXCELL
203 tristate "Picochip picoXcell true random number generator support"
204 depends on HW_RANDOM && ARCH_PICOXCELL && PICOXCELL_PC3X3
205 ---help---
206 This driver provides kernel-side support for the Random Number
207 Generator hardware found on Picochip PC3x3 and later devices.
208
209 To compile this driver as a module, choose M here: the
210 module will be called picoxcell-rng.
211
212 If unsure, say Y.
diff --git a/drivers/char/hw_random/Makefile b/drivers/char/hw_random/Makefile
index 4273308aa1e3..3db4eb8b19c0 100644
--- a/drivers/char/hw_random/Makefile
+++ b/drivers/char/hw_random/Makefile
@@ -19,3 +19,4 @@ obj-$(CONFIG_HW_RANDOM_TX4939) += tx4939-rng.o
19obj-$(CONFIG_HW_RANDOM_MXC_RNGA) += mxc-rnga.o 19obj-$(CONFIG_HW_RANDOM_MXC_RNGA) += mxc-rnga.o
20obj-$(CONFIG_HW_RANDOM_OCTEON) += octeon-rng.o 20obj-$(CONFIG_HW_RANDOM_OCTEON) += octeon-rng.o
21obj-$(CONFIG_HW_RANDOM_NOMADIK) += nomadik-rng.o 21obj-$(CONFIG_HW_RANDOM_NOMADIK) += nomadik-rng.o
22obj-$(CONFIG_HW_RANDOM_PICOXCELL) += picoxcell-rng.o
diff --git a/drivers/char/hw_random/amd-rng.c b/drivers/char/hw_random/amd-rng.c
index 0d8c5788b8e4..c6af038682f1 100644
--- a/drivers/char/hw_random/amd-rng.c
+++ b/drivers/char/hw_random/amd-rng.c
@@ -133,6 +133,12 @@ found:
133 pmbase &= 0x0000FF00; 133 pmbase &= 0x0000FF00;
134 if (pmbase == 0) 134 if (pmbase == 0)
135 goto out; 135 goto out;
136 if (!request_region(pmbase + 0xF0, 8, "AMD HWRNG")) {
137 dev_err(&pdev->dev, "AMD HWRNG region 0x%x already in use!\n",
138 pmbase + 0xF0);
139 err = -EBUSY;
140 goto out;
141 }
136 amd_rng.priv = (unsigned long)pmbase; 142 amd_rng.priv = (unsigned long)pmbase;
137 amd_pdev = pdev; 143 amd_pdev = pdev;
138 144
@@ -141,6 +147,7 @@ found:
141 if (err) { 147 if (err) {
142 printk(KERN_ERR PFX "RNG registering failed (%d)\n", 148 printk(KERN_ERR PFX "RNG registering failed (%d)\n",
143 err); 149 err);
150 release_region(pmbase + 0xF0, 8);
144 goto out; 151 goto out;
145 } 152 }
146out: 153out:
@@ -149,6 +156,8 @@ out:
149 156
150static void __exit mod_exit(void) 157static void __exit mod_exit(void)
151{ 158{
159 u32 pmbase = (unsigned long)amd_rng.priv;
160 release_region(pmbase + 0xF0, 8);
152 hwrng_unregister(&amd_rng); 161 hwrng_unregister(&amd_rng);
153} 162}
154 163
diff --git a/drivers/char/hw_random/core.c b/drivers/char/hw_random/core.c
index 3d9c61e5acbf..2016aad85203 100644
--- a/drivers/char/hw_random/core.c
+++ b/drivers/char/hw_random/core.c
@@ -37,7 +37,6 @@
37#include <linux/kernel.h> 37#include <linux/kernel.h>
38#include <linux/fs.h> 38#include <linux/fs.h>
39#include <linux/sched.h> 39#include <linux/sched.h>
40#include <linux/smp_lock.h>
41#include <linux/init.h> 40#include <linux/init.h>
42#include <linux/miscdevice.h> 41#include <linux/miscdevice.h>
43#include <linux/delay.h> 42#include <linux/delay.h>
@@ -170,6 +169,7 @@ static const struct file_operations rng_chrdev_ops = {
170 .owner = THIS_MODULE, 169 .owner = THIS_MODULE,
171 .open = rng_dev_open, 170 .open = rng_dev_open,
172 .read = rng_dev_read, 171 .read = rng_dev_read,
172 .llseek = noop_llseek,
173}; 173};
174 174
175static struct miscdevice rng_miscdev = { 175static struct miscdevice rng_miscdev = {
diff --git a/drivers/char/hw_random/n2-drv.c b/drivers/char/hw_random/n2-drv.c
index a3f5e381e746..ac6739e085e3 100644
--- a/drivers/char/hw_random/n2-drv.c
+++ b/drivers/char/hw_random/n2-drv.c
@@ -619,15 +619,20 @@ static void __devinit n2rng_driver_version(void)
619 pr_info("%s", version); 619 pr_info("%s", version);
620} 620}
621 621
622static int __devinit n2rng_probe(struct platform_device *op, 622static const struct of_device_id n2rng_match[];
623 const struct of_device_id *match) 623static int __devinit n2rng_probe(struct platform_device *op)
624{ 624{
625 int victoria_falls = (match->data != NULL); 625 const struct of_device_id *match;
626 int victoria_falls;
626 int err = -ENOMEM; 627 int err = -ENOMEM;
627 struct n2rng *np; 628 struct n2rng *np;
628 629
629 n2rng_driver_version(); 630 match = of_match_device(n2rng_match, &op->dev);
631 if (!match)
632 return -EINVAL;
633 victoria_falls = (match->data != NULL);
630 634
635 n2rng_driver_version();
631 np = kzalloc(sizeof(*np), GFP_KERNEL); 636 np = kzalloc(sizeof(*np), GFP_KERNEL);
632 if (!np) 637 if (!np)
633 goto out; 638 goto out;
@@ -750,7 +755,7 @@ static const struct of_device_id n2rng_match[] = {
750}; 755};
751MODULE_DEVICE_TABLE(of, n2rng_match); 756MODULE_DEVICE_TABLE(of, n2rng_match);
752 757
753static struct of_platform_driver n2rng_driver = { 758static struct platform_driver n2rng_driver = {
754 .driver = { 759 .driver = {
755 .name = "n2rng", 760 .name = "n2rng",
756 .owner = THIS_MODULE, 761 .owner = THIS_MODULE,
@@ -762,12 +767,12 @@ static struct of_platform_driver n2rng_driver = {
762 767
763static int __init n2rng_init(void) 768static int __init n2rng_init(void)
764{ 769{
765 return of_register_platform_driver(&n2rng_driver); 770 return platform_driver_register(&n2rng_driver);
766} 771}
767 772
768static void __exit n2rng_exit(void) 773static void __exit n2rng_exit(void)
769{ 774{
770 of_unregister_platform_driver(&n2rng_driver); 775 platform_driver_unregister(&n2rng_driver);
771} 776}
772 777
773module_init(n2rng_init); 778module_init(n2rng_init);
diff --git a/drivers/char/hw_random/nomadik-rng.c b/drivers/char/hw_random/nomadik-rng.c
index a348c7e9aa0b..dd1d143eb8ea 100644
--- a/drivers/char/hw_random/nomadik-rng.c
+++ b/drivers/char/hw_random/nomadik-rng.c
@@ -39,7 +39,7 @@ static struct hwrng nmk_rng = {
39 .read = nmk_rng_read, 39 .read = nmk_rng_read,
40}; 40};
41 41
42static int nmk_rng_probe(struct amba_device *dev, struct amba_id *id) 42static int nmk_rng_probe(struct amba_device *dev, const struct amba_id *id)
43{ 43{
44 void __iomem *base; 44 void __iomem *base;
45 int ret; 45 int ret;
diff --git a/drivers/char/hw_random/omap-rng.c b/drivers/char/hw_random/omap-rng.c
index 06aad0831c73..2cc755a64302 100644
--- a/drivers/char/hw_random/omap-rng.c
+++ b/drivers/char/hw_random/omap-rng.c
@@ -91,7 +91,7 @@ static struct hwrng omap_rng_ops = {
91 91
92static int __devinit omap_rng_probe(struct platform_device *pdev) 92static int __devinit omap_rng_probe(struct platform_device *pdev)
93{ 93{
94 struct resource *res, *mem; 94 struct resource *res;
95 int ret; 95 int ret;
96 96
97 /* 97 /*
@@ -116,14 +116,12 @@ static int __devinit omap_rng_probe(struct platform_device *pdev)
116 if (!res) 116 if (!res)
117 return -ENOENT; 117 return -ENOENT;
118 118
119 mem = request_mem_region(res->start, resource_size(res), 119 if (!request_mem_region(res->start, resource_size(res), pdev->name)) {
120 pdev->name);
121 if (mem == NULL) {
122 ret = -EBUSY; 120 ret = -EBUSY;
123 goto err_region; 121 goto err_region;
124 } 122 }
125 123
126 dev_set_drvdata(&pdev->dev, mem); 124 dev_set_drvdata(&pdev->dev, res);
127 rng_base = ioremap(res->start, resource_size(res)); 125 rng_base = ioremap(res->start, resource_size(res));
128 if (!rng_base) { 126 if (!rng_base) {
129 ret = -ENOMEM; 127 ret = -ENOMEM;
@@ -146,7 +144,7 @@ err_register:
146 iounmap(rng_base); 144 iounmap(rng_base);
147 rng_base = NULL; 145 rng_base = NULL;
148err_ioremap: 146err_ioremap:
149 release_resource(mem); 147 release_mem_region(res->start, resource_size(res));
150err_region: 148err_region:
151 if (cpu_is_omap24xx()) { 149 if (cpu_is_omap24xx()) {
152 clk_disable(rng_ick); 150 clk_disable(rng_ick);
@@ -157,7 +155,7 @@ err_region:
157 155
158static int __exit omap_rng_remove(struct platform_device *pdev) 156static int __exit omap_rng_remove(struct platform_device *pdev)
159{ 157{
160 struct resource *mem = dev_get_drvdata(&pdev->dev); 158 struct resource *res = dev_get_drvdata(&pdev->dev);
161 159
162 hwrng_unregister(&omap_rng_ops); 160 hwrng_unregister(&omap_rng_ops);
163 161
@@ -170,7 +168,7 @@ static int __exit omap_rng_remove(struct platform_device *pdev)
170 clk_put(rng_ick); 168 clk_put(rng_ick);
171 } 169 }
172 170
173 release_resource(mem); 171 release_mem_region(res->start, resource_size(res));
174 rng_base = NULL; 172 rng_base = NULL;
175 173
176 return 0; 174 return 0;
diff --git a/drivers/char/hw_random/pasemi-rng.c b/drivers/char/hw_random/pasemi-rng.c
index a31c830ca8cd..1d504815e6db 100644
--- a/drivers/char/hw_random/pasemi-rng.c
+++ b/drivers/char/hw_random/pasemi-rng.c
@@ -94,8 +94,7 @@ static struct hwrng pasemi_rng = {
94 .data_read = pasemi_rng_data_read, 94 .data_read = pasemi_rng_data_read,
95}; 95};
96 96
97static int __devinit rng_probe(struct platform_device *ofdev, 97static int __devinit rng_probe(struct platform_device *ofdev)
98 const struct of_device_id *match)
99{ 98{
100 void __iomem *rng_regs; 99 void __iomem *rng_regs;
101 struct device_node *rng_np = ofdev->dev.of_node; 100 struct device_node *rng_np = ofdev->dev.of_node;
@@ -139,7 +138,7 @@ static struct of_device_id rng_match[] = {
139 { }, 138 { },
140}; 139};
141 140
142static struct of_platform_driver rng_driver = { 141static struct platform_driver rng_driver = {
143 .driver = { 142 .driver = {
144 .name = "pasemi-rng", 143 .name = "pasemi-rng",
145 .owner = THIS_MODULE, 144 .owner = THIS_MODULE,
@@ -151,13 +150,13 @@ static struct of_platform_driver rng_driver = {
151 150
152static int __init rng_init(void) 151static int __init rng_init(void)
153{ 152{
154 return of_register_platform_driver(&rng_driver); 153 return platform_driver_register(&rng_driver);
155} 154}
156module_init(rng_init); 155module_init(rng_init);
157 156
158static void __exit rng_exit(void) 157static void __exit rng_exit(void)
159{ 158{
160 of_unregister_platform_driver(&rng_driver); 159 platform_driver_unregister(&rng_driver);
161} 160}
162module_exit(rng_exit); 161module_exit(rng_exit);
163 162
diff --git a/drivers/char/hw_random/picoxcell-rng.c b/drivers/char/hw_random/picoxcell-rng.c
new file mode 100644
index 000000000000..990d55a5e3e8
--- /dev/null
+++ b/drivers/char/hw_random/picoxcell-rng.c
@@ -0,0 +1,208 @@
1/*
2 * Copyright (c) 2010-2011 Picochip Ltd., Jamie Iles
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * All enquiries to support@picochip.com
9 */
10#include <linux/clk.h>
11#include <linux/delay.h>
12#include <linux/err.h>
13#include <linux/hw_random.h>
14#include <linux/io.h>
15#include <linux/kernel.h>
16#include <linux/module.h>
17#include <linux/platform_device.h>
18
19#define DATA_REG_OFFSET 0x0200
20#define CSR_REG_OFFSET 0x0278
21#define CSR_OUT_EMPTY_MASK (1 << 24)
22#define CSR_FAULT_MASK (1 << 1)
23#define TRNG_BLOCK_RESET_MASK (1 << 0)
24#define TAI_REG_OFFSET 0x0380
25
26/*
27 * The maximum amount of time in microseconds to spend waiting for data if the
28 * core wants us to wait. The TRNG should generate 32 bits every 320ns so a
29 * timeout of 20us seems reasonable. The TRNG does builtin tests of the data
30 * for randomness so we can't always assume there is data present.
31 */
32#define PICO_TRNG_TIMEOUT 20
33
34static void __iomem *rng_base;
35static struct clk *rng_clk;
36struct device *rng_dev;
37
38static inline u32 picoxcell_trng_read_csr(void)
39{
40 return __raw_readl(rng_base + CSR_REG_OFFSET);
41}
42
43static inline bool picoxcell_trng_is_empty(void)
44{
45 return picoxcell_trng_read_csr() & CSR_OUT_EMPTY_MASK;
46}
47
48/*
49 * Take the random number generator out of reset and make sure the interrupts
50 * are masked. We shouldn't need to get large amounts of random bytes so just
51 * poll the status register. The hardware generates 32 bits every 320ns so we
52 * shouldn't have to wait long enough to warrant waiting for an IRQ.
53 */
54static void picoxcell_trng_start(void)
55{
56 __raw_writel(0, rng_base + TAI_REG_OFFSET);
57 __raw_writel(0, rng_base + CSR_REG_OFFSET);
58}
59
60static void picoxcell_trng_reset(void)
61{
62 __raw_writel(TRNG_BLOCK_RESET_MASK, rng_base + CSR_REG_OFFSET);
63 __raw_writel(TRNG_BLOCK_RESET_MASK, rng_base + TAI_REG_OFFSET);
64 picoxcell_trng_start();
65}
66
67/*
68 * Get some random data from the random number generator. The hw_random core
69 * layer provides us with locking.
70 */
71static int picoxcell_trng_read(struct hwrng *rng, void *buf, size_t max,
72 bool wait)
73{
74 int i;
75
76 /* Wait for some data to become available. */
77 for (i = 0; i < PICO_TRNG_TIMEOUT && picoxcell_trng_is_empty(); ++i) {
78 if (!wait)
79 return 0;
80
81 udelay(1);
82 }
83
84 if (picoxcell_trng_read_csr() & CSR_FAULT_MASK) {
85 dev_err(rng_dev, "fault detected, resetting TRNG\n");
86 picoxcell_trng_reset();
87 return -EIO;
88 }
89
90 if (i == PICO_TRNG_TIMEOUT)
91 return 0;
92
93 *(u32 *)buf = __raw_readl(rng_base + DATA_REG_OFFSET);
94 return sizeof(u32);
95}
96
97static struct hwrng picoxcell_trng = {
98 .name = "picoxcell",
99 .read = picoxcell_trng_read,
100};
101
102static int picoxcell_trng_probe(struct platform_device *pdev)
103{
104 int ret;
105 struct resource *mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
106
107 if (!mem) {
108 dev_warn(&pdev->dev, "no memory resource\n");
109 return -ENOMEM;
110 }
111
112 if (!devm_request_mem_region(&pdev->dev, mem->start, resource_size(mem),
113 "picoxcell_trng")) {
114 dev_warn(&pdev->dev, "unable to request io mem\n");
115 return -EBUSY;
116 }
117
118 rng_base = devm_ioremap(&pdev->dev, mem->start, resource_size(mem));
119 if (!rng_base) {
120 dev_warn(&pdev->dev, "unable to remap io mem\n");
121 return -ENOMEM;
122 }
123
124 rng_clk = clk_get(&pdev->dev, NULL);
125 if (IS_ERR(rng_clk)) {
126 dev_warn(&pdev->dev, "no clk\n");
127 return PTR_ERR(rng_clk);
128 }
129
130 ret = clk_enable(rng_clk);
131 if (ret) {
132 dev_warn(&pdev->dev, "unable to enable clk\n");
133 goto err_enable;
134 }
135
136 picoxcell_trng_start();
137 ret = hwrng_register(&picoxcell_trng);
138 if (ret)
139 goto err_register;
140
141 rng_dev = &pdev->dev;
142 dev_info(&pdev->dev, "pixoxcell random number generator active\n");
143
144 return 0;
145
146err_register:
147 clk_disable(rng_clk);
148err_enable:
149 clk_put(rng_clk);
150
151 return ret;
152}
153
154static int __devexit picoxcell_trng_remove(struct platform_device *pdev)
155{
156 hwrng_unregister(&picoxcell_trng);
157 clk_disable(rng_clk);
158 clk_put(rng_clk);
159
160 return 0;
161}
162
163#ifdef CONFIG_PM
164static int picoxcell_trng_suspend(struct device *dev)
165{
166 clk_disable(rng_clk);
167
168 return 0;
169}
170
171static int picoxcell_trng_resume(struct device *dev)
172{
173 return clk_enable(rng_clk);
174}
175
176static const struct dev_pm_ops picoxcell_trng_pm_ops = {
177 .suspend = picoxcell_trng_suspend,
178 .resume = picoxcell_trng_resume,
179};
180#endif /* CONFIG_PM */
181
182static struct platform_driver picoxcell_trng_driver = {
183 .probe = picoxcell_trng_probe,
184 .remove = __devexit_p(picoxcell_trng_remove),
185 .driver = {
186 .name = "picoxcell-trng",
187 .owner = THIS_MODULE,
188#ifdef CONFIG_PM
189 .pm = &picoxcell_trng_pm_ops,
190#endif /* CONFIG_PM */
191 },
192};
193
194static int __init picoxcell_trng_init(void)
195{
196 return platform_driver_register(&picoxcell_trng_driver);
197}
198module_init(picoxcell_trng_init);
199
200static void __exit picoxcell_trng_exit(void)
201{
202 platform_driver_unregister(&picoxcell_trng_driver);
203}
204module_exit(picoxcell_trng_exit);
205
206MODULE_LICENSE("GPL");
207MODULE_AUTHOR("Jamie Iles");
208MODULE_DESCRIPTION("Picochip picoXcell TRNG driver");
diff --git a/drivers/char/hw_random/via-rng.c b/drivers/char/hw_random/via-rng.c
index 794aacb715c1..d0387a84eec1 100644
--- a/drivers/char/hw_random/via-rng.c
+++ b/drivers/char/hw_random/via-rng.c
@@ -24,6 +24,7 @@
24 * warranty of any kind, whether express or implied. 24 * warranty of any kind, whether express or implied.
25 */ 25 */
26 26
27#include <crypto/padlock.h>
27#include <linux/module.h> 28#include <linux/module.h>
28#include <linux/kernel.h> 29#include <linux/kernel.h>
29#include <linux/hw_random.h> 30#include <linux/hw_random.h>
@@ -34,7 +35,6 @@
34#include <asm/i387.h> 35#include <asm/i387.h>
35 36
36 37
37#define PFX KBUILD_MODNAME ": "
38 38
39 39
40enum { 40enum {
@@ -81,8 +81,7 @@ static inline u32 xstore(u32 *addr, u32 edx_in)
81 ts_state = irq_ts_save(); 81 ts_state = irq_ts_save();
82 82
83 asm(".byte 0x0F,0xA7,0xC0 /* xstore %%edi (addr=%0) */" 83 asm(".byte 0x0F,0xA7,0xC0 /* xstore %%edi (addr=%0) */"
84 :"=m"(*addr), "=a"(eax_out) 84 : "=m" (*addr), "=a" (eax_out), "+d" (edx_in), "+D" (addr));
85 :"D"(addr), "d"(edx_in));
86 85
87 irq_ts_restore(ts_state); 86 irq_ts_restore(ts_state);
88 return eax_out; 87 return eax_out;
@@ -90,8 +89,10 @@ static inline u32 xstore(u32 *addr, u32 edx_in)
90 89
91static int via_rng_data_present(struct hwrng *rng, int wait) 90static int via_rng_data_present(struct hwrng *rng, int wait)
92{ 91{
92 char buf[16 + PADLOCK_ALIGNMENT - STACK_ALIGN] __attribute__
93 ((aligned(STACK_ALIGN)));
94 u32 *via_rng_datum = (u32 *)PTR_ALIGN(&buf[0], PADLOCK_ALIGNMENT);
93 u32 bytes_out; 95 u32 bytes_out;
94 u32 *via_rng_datum = (u32 *)(&rng->priv);
95 int i; 96 int i;
96 97
97 /* We choose the recommended 1-byte-per-instruction RNG rate, 98 /* We choose the recommended 1-byte-per-instruction RNG rate,
@@ -115,6 +116,7 @@ static int via_rng_data_present(struct hwrng *rng, int wait)
115 break; 116 break;
116 udelay(10); 117 udelay(10);
117 } 118 }
119 rng->priv = *via_rng_datum;
118 return bytes_out ? 1 : 0; 120 return bytes_out ? 1 : 0;
119} 121}
120 122
diff --git a/drivers/char/i8k.c b/drivers/char/i8k.c
index 4cd8b227c11f..6e40072fbf67 100644
--- a/drivers/char/i8k.c
+++ b/drivers/char/i8k.c
@@ -5,6 +5,9 @@
5 * 5 *
6 * Copyright (C) 2001 Massimo Dal Zotto <dz@debian.org> 6 * Copyright (C) 2001 Massimo Dal Zotto <dz@debian.org>
7 * 7 *
8 * Hwmon integration:
9 * Copyright (C) 2011 Jean Delvare <khali@linux-fr.org>
10 *
8 * This program is free software; you can redistribute it and/or modify it 11 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the 12 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2, or (at your option) any 13 * Free Software Foundation; either version 2, or (at your option) any
@@ -23,7 +26,9 @@
23#include <linux/seq_file.h> 26#include <linux/seq_file.h>
24#include <linux/dmi.h> 27#include <linux/dmi.h>
25#include <linux/capability.h> 28#include <linux/capability.h>
26#include <linux/smp_lock.h> 29#include <linux/mutex.h>
30#include <linux/hwmon.h>
31#include <linux/hwmon-sysfs.h>
27#include <asm/uaccess.h> 32#include <asm/uaccess.h>
28#include <asm/io.h> 33#include <asm/io.h>
29 34
@@ -56,7 +61,9 @@
56 61
57#define I8K_TEMPERATURE_BUG 1 62#define I8K_TEMPERATURE_BUG 1
58 63
64static DEFINE_MUTEX(i8k_mutex);
59static char bios_version[4]; 65static char bios_version[4];
66static struct device *i8k_hwmon_dev;
60 67
61MODULE_AUTHOR("Massimo Dal Zotto (dz@debian.org)"); 68MODULE_AUTHOR("Massimo Dal Zotto (dz@debian.org)");
62MODULE_DESCRIPTION("Driver for accessing SMM BIOS on Dell laptops"); 69MODULE_DESCRIPTION("Driver for accessing SMM BIOS on Dell laptops");
@@ -119,7 +126,7 @@ static int i8k_smm(struct smm_regs *regs)
119 int eax = regs->eax; 126 int eax = regs->eax;
120 127
121#if defined(CONFIG_X86_64) 128#if defined(CONFIG_X86_64)
122 asm("pushq %%rax\n\t" 129 asm volatile("pushq %%rax\n\t"
123 "movl 0(%%rax),%%edx\n\t" 130 "movl 0(%%rax),%%edx\n\t"
124 "pushq %%rdx\n\t" 131 "pushq %%rdx\n\t"
125 "movl 4(%%rax),%%ebx\n\t" 132 "movl 4(%%rax),%%ebx\n\t"
@@ -138,14 +145,14 @@ static int i8k_smm(struct smm_regs *regs)
138 "movl %%edi,20(%%rax)\n\t" 145 "movl %%edi,20(%%rax)\n\t"
139 "popq %%rdx\n\t" 146 "popq %%rdx\n\t"
140 "movl %%edx,0(%%rax)\n\t" 147 "movl %%edx,0(%%rax)\n\t"
141 "lahf\n\t" 148 "pushfq\n\t"
142 "shrl $8,%%eax\n\t" 149 "popq %%rax\n\t"
143 "andl $1,%%eax\n" 150 "andl $1,%%eax\n"
144 :"=a"(rc) 151 :"=a"(rc)
145 : "a"(regs) 152 : "a"(regs)
146 : "%ebx", "%ecx", "%edx", "%esi", "%edi", "memory"); 153 : "%ebx", "%ecx", "%edx", "%esi", "%edi", "memory");
147#else 154#else
148 asm("pushl %%eax\n\t" 155 asm volatile("pushl %%eax\n\t"
149 "movl 0(%%eax),%%edx\n\t" 156 "movl 0(%%eax),%%edx\n\t"
150 "push %%edx\n\t" 157 "push %%edx\n\t"
151 "movl 4(%%eax),%%ebx\n\t" 158 "movl 4(%%eax),%%ebx\n\t"
@@ -166,7 +173,8 @@ static int i8k_smm(struct smm_regs *regs)
166 "movl %%edx,0(%%eax)\n\t" 173 "movl %%edx,0(%%eax)\n\t"
167 "lahf\n\t" 174 "lahf\n\t"
168 "shrl $8,%%eax\n\t" 175 "shrl $8,%%eax\n\t"
169 "andl $1,%%eax\n":"=a"(rc) 176 "andl $1,%%eax\n"
177 :"=a"(rc)
170 : "a"(regs) 178 : "a"(regs)
171 : "%ebx", "%ecx", "%edx", "%esi", "%edi", "memory"); 179 : "%ebx", "%ecx", "%edx", "%esi", "%edi", "memory");
172#endif 180#endif
@@ -399,9 +407,9 @@ static long i8k_ioctl(struct file *fp, unsigned int cmd, unsigned long arg)
399{ 407{
400 long ret; 408 long ret;
401 409
402 lock_kernel(); 410 mutex_lock(&i8k_mutex);
403 ret = i8k_ioctl_unlocked(fp, cmd, arg); 411 ret = i8k_ioctl_unlocked(fp, cmd, arg);
404 unlock_kernel(); 412 mutex_unlock(&i8k_mutex);
405 413
406 return ret; 414 return ret;
407} 415}
@@ -453,6 +461,152 @@ static int i8k_open_fs(struct inode *inode, struct file *file)
453 return single_open(file, i8k_proc_show, NULL); 461 return single_open(file, i8k_proc_show, NULL);
454} 462}
455 463
464
465/*
466 * Hwmon interface
467 */
468
469static ssize_t i8k_hwmon_show_temp(struct device *dev,
470 struct device_attribute *devattr,
471 char *buf)
472{
473 int cpu_temp;
474
475 cpu_temp = i8k_get_temp(0);
476 if (cpu_temp < 0)
477 return cpu_temp;
478 return sprintf(buf, "%d\n", cpu_temp * 1000);
479}
480
481static ssize_t i8k_hwmon_show_fan(struct device *dev,
482 struct device_attribute *devattr,
483 char *buf)
484{
485 int index = to_sensor_dev_attr(devattr)->index;
486 int fan_speed;
487
488 fan_speed = i8k_get_fan_speed(index);
489 if (fan_speed < 0)
490 return fan_speed;
491 return sprintf(buf, "%d\n", fan_speed);
492}
493
494static ssize_t i8k_hwmon_show_label(struct device *dev,
495 struct device_attribute *devattr,
496 char *buf)
497{
498 static const char *labels[4] = {
499 "i8k",
500 "CPU",
501 "Left Fan",
502 "Right Fan",
503 };
504 int index = to_sensor_dev_attr(devattr)->index;
505
506 return sprintf(buf, "%s\n", labels[index]);
507}
508
509static DEVICE_ATTR(temp1_input, S_IRUGO, i8k_hwmon_show_temp, NULL);
510static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, i8k_hwmon_show_fan, NULL,
511 I8K_FAN_LEFT);
512static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, i8k_hwmon_show_fan, NULL,
513 I8K_FAN_RIGHT);
514static SENSOR_DEVICE_ATTR(name, S_IRUGO, i8k_hwmon_show_label, NULL, 0);
515static SENSOR_DEVICE_ATTR(temp1_label, S_IRUGO, i8k_hwmon_show_label, NULL, 1);
516static SENSOR_DEVICE_ATTR(fan1_label, S_IRUGO, i8k_hwmon_show_label, NULL, 2);
517static SENSOR_DEVICE_ATTR(fan2_label, S_IRUGO, i8k_hwmon_show_label, NULL, 3);
518
519static void i8k_hwmon_remove_files(struct device *dev)
520{
521 device_remove_file(dev, &dev_attr_temp1_input);
522 device_remove_file(dev, &sensor_dev_attr_fan1_input.dev_attr);
523 device_remove_file(dev, &sensor_dev_attr_fan2_input.dev_attr);
524 device_remove_file(dev, &sensor_dev_attr_temp1_label.dev_attr);
525 device_remove_file(dev, &sensor_dev_attr_fan1_label.dev_attr);
526 device_remove_file(dev, &sensor_dev_attr_fan2_label.dev_attr);
527 device_remove_file(dev, &sensor_dev_attr_name.dev_attr);
528}
529
530static int __init i8k_init_hwmon(void)
531{
532 int err;
533
534 i8k_hwmon_dev = hwmon_device_register(NULL);
535 if (IS_ERR(i8k_hwmon_dev)) {
536 err = PTR_ERR(i8k_hwmon_dev);
537 i8k_hwmon_dev = NULL;
538 printk(KERN_ERR "i8k: hwmon registration failed (%d)\n", err);
539 return err;
540 }
541
542 /* Required name attribute */
543 err = device_create_file(i8k_hwmon_dev,
544 &sensor_dev_attr_name.dev_attr);
545 if (err)
546 goto exit_unregister;
547
548 /* CPU temperature attributes, if temperature reading is OK */
549 err = i8k_get_temp(0);
550 if (err < 0) {
551 dev_dbg(i8k_hwmon_dev,
552 "Not creating temperature attributes (%d)\n", err);
553 } else {
554 err = device_create_file(i8k_hwmon_dev, &dev_attr_temp1_input);
555 if (err)
556 goto exit_remove_files;
557 err = device_create_file(i8k_hwmon_dev,
558 &sensor_dev_attr_temp1_label.dev_attr);
559 if (err)
560 goto exit_remove_files;
561 }
562
563 /* Left fan attributes, if left fan is present */
564 err = i8k_get_fan_status(I8K_FAN_LEFT);
565 if (err < 0) {
566 dev_dbg(i8k_hwmon_dev,
567 "Not creating %s fan attributes (%d)\n", "left", err);
568 } else {
569 err = device_create_file(i8k_hwmon_dev,
570 &sensor_dev_attr_fan1_input.dev_attr);
571 if (err)
572 goto exit_remove_files;
573 err = device_create_file(i8k_hwmon_dev,
574 &sensor_dev_attr_fan1_label.dev_attr);
575 if (err)
576 goto exit_remove_files;
577 }
578
579 /* Right fan attributes, if right fan is present */
580 err = i8k_get_fan_status(I8K_FAN_RIGHT);
581 if (err < 0) {
582 dev_dbg(i8k_hwmon_dev,
583 "Not creating %s fan attributes (%d)\n", "right", err);
584 } else {
585 err = device_create_file(i8k_hwmon_dev,
586 &sensor_dev_attr_fan2_input.dev_attr);
587 if (err)
588 goto exit_remove_files;
589 err = device_create_file(i8k_hwmon_dev,
590 &sensor_dev_attr_fan2_label.dev_attr);
591 if (err)
592 goto exit_remove_files;
593 }
594
595 return 0;
596
597 exit_remove_files:
598 i8k_hwmon_remove_files(i8k_hwmon_dev);
599 exit_unregister:
600 hwmon_device_unregister(i8k_hwmon_dev);
601 return err;
602}
603
604static void __exit i8k_exit_hwmon(void)
605{
606 i8k_hwmon_remove_files(i8k_hwmon_dev);
607 hwmon_device_unregister(i8k_hwmon_dev);
608}
609
456static struct dmi_system_id __initdata i8k_dmi_table[] = { 610static struct dmi_system_id __initdata i8k_dmi_table[] = {
457 { 611 {
458 .ident = "Dell Inspiron", 612 .ident = "Dell Inspiron",
@@ -578,6 +732,7 @@ static int __init i8k_probe(void)
578static int __init i8k_init(void) 732static int __init i8k_init(void)
579{ 733{
580 struct proc_dir_entry *proc_i8k; 734 struct proc_dir_entry *proc_i8k;
735 int err;
581 736
582 /* Are we running on an supported laptop? */ 737 /* Are we running on an supported laptop? */
583 if (i8k_probe()) 738 if (i8k_probe())
@@ -588,15 +743,24 @@ static int __init i8k_init(void)
588 if (!proc_i8k) 743 if (!proc_i8k)
589 return -ENOENT; 744 return -ENOENT;
590 745
746 err = i8k_init_hwmon();
747 if (err)
748 goto exit_remove_proc;
749
591 printk(KERN_INFO 750 printk(KERN_INFO
592 "Dell laptop SMM driver v%s Massimo Dal Zotto (dz@debian.org)\n", 751 "Dell laptop SMM driver v%s Massimo Dal Zotto (dz@debian.org)\n",
593 I8K_VERSION); 752 I8K_VERSION);
594 753
595 return 0; 754 return 0;
755
756 exit_remove_proc:
757 remove_proc_entry("i8k", NULL);
758 return err;
596} 759}
597 760
598static void __exit i8k_exit(void) 761static void __exit i8k_exit(void)
599{ 762{
763 i8k_exit_hwmon();
600 remove_proc_entry("i8k", NULL); 764 remove_proc_entry("i8k", NULL);
601} 765}
602 766
diff --git a/drivers/char/ip2/Makefile b/drivers/char/ip2/Makefile
deleted file mode 100644
index bc397d92b499..000000000000
--- a/drivers/char/ip2/Makefile
+++ /dev/null
@@ -1,8 +0,0 @@
1#
2# Makefile for the Computone IntelliPort Plus Driver
3#
4
5obj-$(CONFIG_COMPUTONE) += ip2.o
6
7ip2-objs := ip2main.o
8
diff --git a/drivers/char/ip2/i2cmd.c b/drivers/char/ip2/i2cmd.c
deleted file mode 100644
index e7af647800b6..000000000000
--- a/drivers/char/ip2/i2cmd.c
+++ /dev/null
@@ -1,210 +0,0 @@
1/*******************************************************************************
2*
3* (c) 1998 by Computone Corporation
4*
5********************************************************************************
6*
7*
8* PACKAGE: Linux tty Device Driver for IntelliPort family of multiport
9* serial I/O controllers.
10*
11* DESCRIPTION: Definition table for In-line and Bypass commands. Applicable
12* only when the standard loadware is active. (This is included
13* source code, not a separate compilation module.)
14*
15*******************************************************************************/
16
17//------------------------------------------------------------------------------
18//
19// Revision History:
20//
21// 10 October 1991 MAG First Draft
22// 7 November 1991 MAG Reflects additional commands.
23// 24 February 1992 MAG Additional commands for 1.4.x loadware
24// 11 March 1992 MAG Additional commands
25// 30 March 1992 MAG Additional command: CMD_DSS_NOW
26// 18 May 1992 MAG Discovered commands 39 & 40 must be at the end of a
27// packet: affects implementation.
28//------------------------------------------------------------------------------
29
30//************
31//* Includes *
32//************
33
34#include "i2cmd.h" /* To get some bit-defines */
35
36//------------------------------------------------------------------------------
37// Here is the table of global arrays which represent each type of command
38// supported in the IntelliPort standard loadware. See also i2cmd.h
39// for a more complete explanation of what is going on.
40//------------------------------------------------------------------------------
41
42// Here are the various globals: note that the names are not used except through
43// the macros defined in i2cmd.h. Also note that although they are character
44// arrays here (for extendability) they are cast to structure pointers in the
45// i2cmd.h macros. See i2cmd.h for flags definitions.
46
47// Length Flags Command
48static UCHAR ct02[] = { 1, BTH, 0x02 }; // DTR UP
49static UCHAR ct03[] = { 1, BTH, 0x03 }; // DTR DN
50static UCHAR ct04[] = { 1, BTH, 0x04 }; // RTS UP
51static UCHAR ct05[] = { 1, BTH, 0x05 }; // RTS DN
52static UCHAR ct06[] = { 1, BYP, 0x06 }; // START FL
53static UCHAR ct07[] = { 2, BTH, 0x07,0 }; // BAUD
54static UCHAR ct08[] = { 2, BTH, 0x08,0 }; // BITS
55static UCHAR ct09[] = { 2, BTH, 0x09,0 }; // STOP
56static UCHAR ct10[] = { 2, BTH, 0x0A,0 }; // PARITY
57static UCHAR ct11[] = { 2, BTH, 0x0B,0 }; // XON
58static UCHAR ct12[] = { 2, BTH, 0x0C,0 }; // XOFF
59static UCHAR ct13[] = { 1, BTH, 0x0D }; // STOP FL
60static UCHAR ct14[] = { 1, BYP|VIP, 0x0E }; // ACK HOTK
61//static UCHAR ct15[]={ 2, BTH|VIP, 0x0F,0 }; // IRQ SET
62static UCHAR ct16[] = { 2, INL, 0x10,0 }; // IXONOPTS
63static UCHAR ct17[] = { 2, INL, 0x11,0 }; // OXONOPTS
64static UCHAR ct18[] = { 1, INL, 0x12 }; // CTSENAB
65static UCHAR ct19[] = { 1, BTH, 0x13 }; // CTSDSAB
66static UCHAR ct20[] = { 1, INL, 0x14 }; // DCDENAB
67static UCHAR ct21[] = { 1, BTH, 0x15 }; // DCDDSAB
68static UCHAR ct22[] = { 1, BTH, 0x16 }; // DSRENAB
69static UCHAR ct23[] = { 1, BTH, 0x17 }; // DSRDSAB
70static UCHAR ct24[] = { 1, BTH, 0x18 }; // RIENAB
71static UCHAR ct25[] = { 1, BTH, 0x19 }; // RIDSAB
72static UCHAR ct26[] = { 2, BTH, 0x1A,0 }; // BRKENAB
73static UCHAR ct27[] = { 1, BTH, 0x1B }; // BRKDSAB
74//static UCHAR ct28[]={ 2, BTH, 0x1C,0 }; // MAXBLOKSIZE
75//static UCHAR ct29[]={ 2, 0, 0x1D,0 }; // reserved
76static UCHAR ct30[] = { 1, INL, 0x1E }; // CTSFLOWENAB
77static UCHAR ct31[] = { 1, INL, 0x1F }; // CTSFLOWDSAB
78static UCHAR ct32[] = { 1, INL, 0x20 }; // RTSFLOWENAB
79static UCHAR ct33[] = { 1, INL, 0x21 }; // RTSFLOWDSAB
80static UCHAR ct34[] = { 2, BTH, 0x22,0 }; // ISTRIPMODE
81static UCHAR ct35[] = { 2, BTH|END, 0x23,0 }; // SENDBREAK
82static UCHAR ct36[] = { 2, BTH, 0x24,0 }; // SETERRMODE
83//static UCHAR ct36a[]={ 3, INL, 0x24,0,0 }; // SET_REPLACE
84
85// The following is listed for completeness, but should never be sent directly
86// by user-level code. It is sent only by library routines in response to data
87// movement.
88//static UCHAR ct37[]={ 5, BYP|VIP, 0x25,0,0,0,0 }; // FLOW PACKET
89
90// Back to normal
91//static UCHAR ct38[] = {11, BTH|VAR, 0x26,0,0,0,0,0,0,0,0,0,0 }; // DEF KEY SEQ
92//static UCHAR ct39[]={ 3, BTH|END, 0x27,0,0 }; // OPOSTON
93//static UCHAR ct40[]={ 1, BTH|END, 0x28 }; // OPOSTOFF
94static UCHAR ct41[] = { 1, BYP, 0x29 }; // RESUME
95//static UCHAR ct42[]={ 2, BTH, 0x2A,0 }; // TXBAUD
96//static UCHAR ct43[]={ 2, BTH, 0x2B,0 }; // RXBAUD
97//static UCHAR ct44[]={ 2, BTH, 0x2C,0 }; // MS PING
98//static UCHAR ct45[]={ 1, BTH, 0x2D }; // HOTENAB
99//static UCHAR ct46[]={ 1, BTH, 0x2E }; // HOTDSAB
100//static UCHAR ct47[]={ 7, BTH, 0x2F,0,0,0,0,0,0 }; // UNIX FLAGS
101//static UCHAR ct48[]={ 1, BTH, 0x30 }; // DSRFLOWENAB
102//static UCHAR ct49[]={ 1, BTH, 0x31 }; // DSRFLOWDSAB
103//static UCHAR ct50[]={ 1, BTH, 0x32 }; // DTRFLOWENAB
104//static UCHAR ct51[]={ 1, BTH, 0x33 }; // DTRFLOWDSAB
105//static UCHAR ct52[]={ 1, BTH, 0x34 }; // BAUDTABRESET
106//static UCHAR ct53[] = { 3, BTH, 0x35,0,0 }; // BAUDREMAP
107static UCHAR ct54[] = { 3, BTH, 0x36,0,0 }; // CUSTOMBAUD1
108static UCHAR ct55[] = { 3, BTH, 0x37,0,0 }; // CUSTOMBAUD2
109static UCHAR ct56[] = { 2, BTH|END, 0x38,0 }; // PAUSE
110static UCHAR ct57[] = { 1, BYP, 0x39 }; // SUSPEND
111static UCHAR ct58[] = { 1, BYP, 0x3A }; // UNSUSPEND
112static UCHAR ct59[] = { 2, BTH, 0x3B,0 }; // PARITYCHK
113static UCHAR ct60[] = { 1, INL|VIP, 0x3C }; // BOOKMARKREQ
114//static UCHAR ct61[]={ 2, BTH, 0x3D,0 }; // INTERNALLOOP
115//static UCHAR ct62[]={ 2, BTH, 0x3E,0 }; // HOTKTIMEOUT
116static UCHAR ct63[] = { 2, INL, 0x3F,0 }; // SETTXON
117static UCHAR ct64[] = { 2, INL, 0x40,0 }; // SETTXOFF
118//static UCHAR ct65[]={ 2, BTH, 0x41,0 }; // SETAUTORTS
119//static UCHAR ct66[]={ 2, BTH, 0x42,0 }; // SETHIGHWAT
120//static UCHAR ct67[]={ 2, BYP, 0x43,0 }; // STARTSELFL
121//static UCHAR ct68[]={ 2, INL, 0x44,0 }; // ENDSELFL
122//static UCHAR ct69[]={ 1, BYP, 0x45 }; // HWFLOW_OFF
123//static UCHAR ct70[]={ 1, BTH, 0x46 }; // ODSRFL_ENAB
124//static UCHAR ct71[]={ 1, BTH, 0x47 }; // ODSRFL_DSAB
125//static UCHAR ct72[]={ 1, BTH, 0x48 }; // ODCDFL_ENAB
126//static UCHAR ct73[]={ 1, BTH, 0x49 }; // ODCDFL_DSAB
127//static UCHAR ct74[]={ 2, BTH, 0x4A,0 }; // LOADLEVEL
128//static UCHAR ct75[]={ 2, BTH, 0x4B,0 }; // STATDATA
129//static UCHAR ct76[]={ 1, BYP, 0x4C }; // BREAK_ON
130//static UCHAR ct77[]={ 1, BYP, 0x4D }; // BREAK_OFF
131//static UCHAR ct78[]={ 1, BYP, 0x4E }; // GETFC
132static UCHAR ct79[] = { 2, BYP, 0x4F,0 }; // XMIT_NOW
133//static UCHAR ct80[]={ 4, BTH, 0x50,0,0,0 }; // DIVISOR_LATCH
134//static UCHAR ct81[]={ 1, BYP, 0x51 }; // GET_STATUS
135//static UCHAR ct82[]={ 1, BYP, 0x52 }; // GET_TXCNT
136//static UCHAR ct83[]={ 1, BYP, 0x53 }; // GET_RXCNT
137//static UCHAR ct84[]={ 1, BYP, 0x54 }; // GET_BOXIDS
138//static UCHAR ct85[]={10, BYP, 0x55,0,0,0,0,0,0,0,0,0 }; // ENAB_MULT
139//static UCHAR ct86[]={ 2, BTH, 0x56,0 }; // RCV_ENABLE
140static UCHAR ct87[] = { 1, BYP, 0x57 }; // HW_TEST
141//static UCHAR ct88[]={ 3, BTH, 0x58,0,0 }; // RCV_THRESHOLD
142//static UCHAR ct90[]={ 3, BYP, 0x5A,0,0 }; // Set SILO
143//static UCHAR ct91[]={ 2, BYP, 0x5B,0 }; // timed break
144
145// Some composite commands as well
146//static UCHAR cc01[]={ 2, BTH, 0x02,0x04 }; // DTR & RTS UP
147//static UCHAR cc02[]={ 2, BTH, 0x03,0x05 }; // DTR & RTS DN
148
149//********
150//* Code *
151//********
152
153//******************************************************************************
154// Function: i2cmdUnixFlags(iflag, cflag, lflag)
155// Parameters: Unix tty flags
156//
157// Returns: Pointer to command structure
158//
159// Description:
160//
161// This routine sets the parameters of command 47 and returns a pointer to the
162// appropriate structure.
163//******************************************************************************
164#if 0
165cmdSyntaxPtr
166i2cmdUnixFlags(unsigned short iflag,unsigned short cflag,unsigned short lflag)
167{
168 cmdSyntaxPtr pCM = (cmdSyntaxPtr) ct47;
169
170 pCM->cmd[1] = (unsigned char) iflag;
171 pCM->cmd[2] = (unsigned char) (iflag >> 8);
172 pCM->cmd[3] = (unsigned char) cflag;
173 pCM->cmd[4] = (unsigned char) (cflag >> 8);
174 pCM->cmd[5] = (unsigned char) lflag;
175 pCM->cmd[6] = (unsigned char) (lflag >> 8);
176 return pCM;
177}
178#endif /* 0 */
179
180//******************************************************************************
181// Function: i2cmdBaudDef(which, rate)
182// Parameters: ?
183//
184// Returns: Pointer to command structure
185//
186// Description:
187//
188// This routine sets the parameters of commands 54 or 55 (according to the
189// argument which), and returns a pointer to the appropriate structure.
190//******************************************************************************
191static cmdSyntaxPtr
192i2cmdBaudDef(int which, unsigned short rate)
193{
194 cmdSyntaxPtr pCM;
195
196 switch(which)
197 {
198 case 1:
199 pCM = (cmdSyntaxPtr) ct54;
200 break;
201 default:
202 case 2:
203 pCM = (cmdSyntaxPtr) ct55;
204 break;
205 }
206 pCM->cmd[1] = (unsigned char) rate;
207 pCM->cmd[2] = (unsigned char) (rate >> 8);
208 return pCM;
209}
210
diff --git a/drivers/char/ip2/i2cmd.h b/drivers/char/ip2/i2cmd.h
deleted file mode 100644
index 29277ec6b8ed..000000000000
--- a/drivers/char/ip2/i2cmd.h
+++ /dev/null
@@ -1,630 +0,0 @@
1/*******************************************************************************
2*
3* (c) 1999 by Computone Corporation
4*
5********************************************************************************
6*
7*
8* PACKAGE: Linux tty Device Driver for IntelliPort II family of multiport
9* serial I/O controllers.
10*
11* DESCRIPTION: Definitions and support for In-line and Bypass commands.
12* Applicable only when the standard loadware is active.
13*
14*******************************************************************************/
15//------------------------------------------------------------------------------
16// Revision History:
17//
18// 10 October 1991 MAG First Draft
19// 7 November 1991 MAG Reflects some new commands
20// 20 February 1992 MAG CMD_HOTACK corrected: no argument.
21// 24 February 1992 MAG Support added for new commands for 1.4.x loadware.
22// 11 March 1992 MAG Additional commands.
23// 16 March 1992 MAG Additional commands.
24// 30 March 1992 MAG Additional command: CMD_DSS_NOW
25// 18 May 1992 MAG Changed CMD_OPOST
26//
27//------------------------------------------------------------------------------
28#ifndef I2CMD_H // To prevent multiple includes
29#define I2CMD_H 1
30
31#include "ip2types.h"
32
33// This module is designed to provide a uniform method of sending commands to
34// the board through command packets. The difficulty is, some commands take
35// parameters, others do not. Furthermore, it is often useful to send several
36// commands to the same channel as part of the same packet. (See also i2pack.h.)
37//
38// This module is designed so that the caller should not be responsible for
39// remembering the exact syntax of each command, or at least so that the
40// compiler could check things somewhat. I'll explain as we go...
41//
42// First, a structure which can embody the syntax of each type of command.
43//
44typedef struct _cmdSyntax
45{
46 UCHAR length; // Number of bytes in the command
47 UCHAR flags; // Information about the command (see below)
48
49 // The command and its parameters, which may be of arbitrary length. Don't
50 // worry yet how the parameters will be initialized; macros later take care
51 // of it. Also, don't worry about the arbitrary length issue; this structure
52 // is never used to allocate space (see i2cmd.c).
53 UCHAR cmd[2];
54} cmdSyntax, *cmdSyntaxPtr;
55
56// Bit assignments for flags
57
58#define INL 1 // Set if suitable for inline commands
59#define BYP 2 // Set if suitable for bypass commands
60#define BTH (INL|BYP) // suitable for either!
61#define END 4 // Set if this must be the last command in a block
62#define VIP 8 // Set if this command is special in some way and really
63 // should only be sent from the library-level and not
64 // directly from user-level
65#define VAR 0x10 // This command is of variable length!
66
67// Declarations for the global arrays used to bear the commands and their
68// arguments.
69//
70// Note: Since these are globals and the arguments might change, it is important
71// that the library routine COPY these into buffers from whence they would be
72// sent, rather than merely storing the pointers. In multi-threaded
73// environments, important that the copy should obtain before any context switch
74// is allowed. Also, for parameterized commands, DO NOT ISSUE THE SAME COMMAND
75// MORE THAN ONCE WITH THE SAME PARAMETERS in the same call.
76//
77static UCHAR ct02[];
78static UCHAR ct03[];
79static UCHAR ct04[];
80static UCHAR ct05[];
81static UCHAR ct06[];
82static UCHAR ct07[];
83static UCHAR ct08[];
84static UCHAR ct09[];
85static UCHAR ct10[];
86static UCHAR ct11[];
87static UCHAR ct12[];
88static UCHAR ct13[];
89static UCHAR ct14[];
90static UCHAR ct15[];
91static UCHAR ct16[];
92static UCHAR ct17[];
93static UCHAR ct18[];
94static UCHAR ct19[];
95static UCHAR ct20[];
96static UCHAR ct21[];
97static UCHAR ct22[];
98static UCHAR ct23[];
99static UCHAR ct24[];
100static UCHAR ct25[];
101static UCHAR ct26[];
102static UCHAR ct27[];
103static UCHAR ct28[];
104static UCHAR ct29[];
105static UCHAR ct30[];
106static UCHAR ct31[];
107static UCHAR ct32[];
108static UCHAR ct33[];
109static UCHAR ct34[];
110static UCHAR ct35[];
111static UCHAR ct36[];
112static UCHAR ct36a[];
113static UCHAR ct41[];
114static UCHAR ct42[];
115static UCHAR ct43[];
116static UCHAR ct44[];
117static UCHAR ct45[];
118static UCHAR ct46[];
119static UCHAR ct48[];
120static UCHAR ct49[];
121static UCHAR ct50[];
122static UCHAR ct51[];
123static UCHAR ct52[];
124static UCHAR ct56[];
125static UCHAR ct57[];
126static UCHAR ct58[];
127static UCHAR ct59[];
128static UCHAR ct60[];
129static UCHAR ct61[];
130static UCHAR ct62[];
131static UCHAR ct63[];
132static UCHAR ct64[];
133static UCHAR ct65[];
134static UCHAR ct66[];
135static UCHAR ct67[];
136static UCHAR ct68[];
137static UCHAR ct69[];
138static UCHAR ct70[];
139static UCHAR ct71[];
140static UCHAR ct72[];
141static UCHAR ct73[];
142static UCHAR ct74[];
143static UCHAR ct75[];
144static UCHAR ct76[];
145static UCHAR ct77[];
146static UCHAR ct78[];
147static UCHAR ct79[];
148static UCHAR ct80[];
149static UCHAR ct81[];
150static UCHAR ct82[];
151static UCHAR ct83[];
152static UCHAR ct84[];
153static UCHAR ct85[];
154static UCHAR ct86[];
155static UCHAR ct87[];
156static UCHAR ct88[];
157static UCHAR ct89[];
158static UCHAR ct90[];
159static UCHAR ct91[];
160static UCHAR cc01[];
161static UCHAR cc02[];
162
163// Now, refer to i2cmd.c, and see the character arrays defined there. They are
164// cast here to cmdSyntaxPtr.
165//
166// There are library functions for issuing bypass or inline commands. These
167// functions take one or more arguments of the type cmdSyntaxPtr. The routine
168// then can figure out how long each command is supposed to be and easily add it
169// to the list.
170//
171// For ease of use, we define manifests which return pointers to appropriate
172// cmdSyntaxPtr things. But some commands also take arguments. If a single
173// argument is used, we define a macro which performs the single assignment and
174// (through the expedient of a comma expression) references the appropriate
175// pointer. For commands requiring several arguments, we actually define a
176// function to perform the assignments.
177
178#define CMD_DTRUP (cmdSyntaxPtr)(ct02) // Raise DTR
179#define CMD_DTRDN (cmdSyntaxPtr)(ct03) // Lower DTR
180#define CMD_RTSUP (cmdSyntaxPtr)(ct04) // Raise RTS
181#define CMD_RTSDN (cmdSyntaxPtr)(ct05) // Lower RTS
182#define CMD_STARTFL (cmdSyntaxPtr)(ct06) // Start Flushing Data
183
184#define CMD_DTRRTS_UP (cmdSyntaxPtr)(cc01) // Raise DTR and RTS
185#define CMD_DTRRTS_DN (cmdSyntaxPtr)(cc02) // Lower DTR and RTS
186
187// Set Baud Rate for transmit and receive
188#define CMD_SETBAUD(arg) \
189 (((cmdSyntaxPtr)(ct07))->cmd[1] = (arg),(cmdSyntaxPtr)(ct07))
190
191#define CBR_50 1
192#define CBR_75 2
193#define CBR_110 3
194#define CBR_134 4
195#define CBR_150 5
196#define CBR_200 6
197#define CBR_300 7
198#define CBR_600 8
199#define CBR_1200 9
200#define CBR_1800 10
201#define CBR_2400 11
202#define CBR_4800 12
203#define CBR_9600 13
204#define CBR_19200 14
205#define CBR_38400 15
206#define CBR_2000 16
207#define CBR_3600 17
208#define CBR_7200 18
209#define CBR_56000 19
210#define CBR_57600 20
211#define CBR_64000 21
212#define CBR_76800 22
213#define CBR_115200 23
214#define CBR_C1 24 // Custom baud rate 1
215#define CBR_C2 25 // Custom baud rate 2
216#define CBR_153600 26
217#define CBR_230400 27
218#define CBR_307200 28
219#define CBR_460800 29
220#define CBR_921600 30
221
222// Set Character size
223//
224#define CMD_SETBITS(arg) \
225 (((cmdSyntaxPtr)(ct08))->cmd[1] = (arg),(cmdSyntaxPtr)(ct08))
226
227#define CSZ_5 0
228#define CSZ_6 1
229#define CSZ_7 2
230#define CSZ_8 3
231
232// Set number of stop bits
233//
234#define CMD_SETSTOP(arg) \
235 (((cmdSyntaxPtr)(ct09))->cmd[1] = (arg),(cmdSyntaxPtr)(ct09))
236
237#define CST_1 0
238#define CST_15 1 // 1.5 stop bits
239#define CST_2 2
240
241// Set parity option
242//
243#define CMD_SETPAR(arg) \
244 (((cmdSyntaxPtr)(ct10))->cmd[1] = (arg),(cmdSyntaxPtr)(ct10))
245
246#define CSP_NP 0 // no parity
247#define CSP_OD 1 // odd parity
248#define CSP_EV 2 // Even parity
249#define CSP_SP 3 // Space parity
250#define CSP_MK 4 // Mark parity
251
252// Define xon char for transmitter flow control
253//
254#define CMD_DEF_IXON(arg) \
255 (((cmdSyntaxPtr)(ct11))->cmd[1] = (arg),(cmdSyntaxPtr)(ct11))
256
257// Define xoff char for transmitter flow control
258//
259#define CMD_DEF_IXOFF(arg) \
260 (((cmdSyntaxPtr)(ct12))->cmd[1] = (arg),(cmdSyntaxPtr)(ct12))
261
262#define CMD_STOPFL (cmdSyntaxPtr)(ct13) // Stop Flushing data
263
264// Acknowledge receipt of hotkey signal
265//
266#define CMD_HOTACK (cmdSyntaxPtr)(ct14)
267
268// Define irq level to use. Should actually be sent by library-level code, not
269// directly from user...
270//
271#define CMDVALUE_IRQ 15 // For library use at initialization. Until this command
272 // is sent, board processing doesn't really start.
273#define CMD_SET_IRQ(arg) \
274 (((cmdSyntaxPtr)(ct15))->cmd[1] = (arg),(cmdSyntaxPtr)(ct15))
275
276#define CIR_POLL 0 // No IRQ - Poll
277#define CIR_3 3 // IRQ 3
278#define CIR_4 4 // IRQ 4
279#define CIR_5 5 // IRQ 5
280#define CIR_7 7 // IRQ 7
281#define CIR_10 10 // IRQ 10
282#define CIR_11 11 // IRQ 11
283#define CIR_12 12 // IRQ 12
284#define CIR_15 15 // IRQ 15
285
286// Select transmit flow xon/xoff options
287//
288#define CMD_IXON_OPT(arg) \
289 (((cmdSyntaxPtr)(ct16))->cmd[1] = (arg),(cmdSyntaxPtr)(ct16))
290
291#define CIX_NONE 0 // Incoming Xon/Xoff characters not special
292#define CIX_XON 1 // Xoff disable, Xon enable
293#define CIX_XANY 2 // Xoff disable, any key enable
294
295// Select receive flow xon/xoff options
296//
297#define CMD_OXON_OPT(arg) \
298 (((cmdSyntaxPtr)(ct17))->cmd[1] = (arg),(cmdSyntaxPtr)(ct17))
299
300#define COX_NONE 0 // Don't send Xon/Xoff
301#define COX_XON 1 // Send xon/xoff to start/stop incoming data
302
303
304#define CMD_CTS_REP (cmdSyntaxPtr)(ct18) // Enable CTS reporting
305#define CMD_CTS_NREP (cmdSyntaxPtr)(ct19) // Disable CTS reporting
306
307#define CMD_DCD_REP (cmdSyntaxPtr)(ct20) // Enable DCD reporting
308#define CMD_DCD_NREP (cmdSyntaxPtr)(ct21) // Disable DCD reporting
309
310#define CMD_DSR_REP (cmdSyntaxPtr)(ct22) // Enable DSR reporting
311#define CMD_DSR_NREP (cmdSyntaxPtr)(ct23) // Disable DSR reporting
312
313#define CMD_RI_REP (cmdSyntaxPtr)(ct24) // Enable RI reporting
314#define CMD_RI_NREP (cmdSyntaxPtr)(ct25) // Disable RI reporting
315
316// Enable break reporting and select style
317//
318#define CMD_BRK_REP(arg) \
319 (((cmdSyntaxPtr)(ct26))->cmd[1] = (arg),(cmdSyntaxPtr)(ct26))
320
321#define CBK_STAT 0x00 // Report breaks as a status (exception,irq)
322#define CBK_NULL 0x01 // Report breaks as a good null
323#define CBK_STAT_SEQ 0x02 // Report breaks as a status AND as in-band character
324 // sequence FFh, 01h, 10h
325#define CBK_SEQ 0x03 // Report breaks as the in-band
326 //sequence FFh, 01h, 10h ONLY.
327#define CBK_FLSH 0x04 // if this bit set also flush input data
328#define CBK_POSIX 0x08 // if this bit set report as FF,0,0 sequence
329#define CBK_SINGLE 0x10 // if this bit set with CBK_SEQ or CBK_STAT_SEQ
330 //then reports single null instead of triple
331
332#define CMD_BRK_NREP (cmdSyntaxPtr)(ct27) // Disable break reporting
333
334// Specify maximum block size for received data
335//
336#define CMD_MAX_BLOCK(arg) \
337 (((cmdSyntaxPtr)(ct28))->cmd[1] = (arg),(cmdSyntaxPtr)(ct28))
338
339// -- COMMAND 29 is reserved --
340
341#define CMD_CTSFL_ENAB (cmdSyntaxPtr)(ct30) // Enable CTS flow control
342#define CMD_CTSFL_DSAB (cmdSyntaxPtr)(ct31) // Disable CTS flow control
343#define CMD_RTSFL_ENAB (cmdSyntaxPtr)(ct32) // Enable RTS flow control
344#define CMD_RTSFL_DSAB (cmdSyntaxPtr)(ct33) // Disable RTS flow control
345
346// Specify istrip option
347//
348#define CMD_ISTRIP_OPT(arg) \
349 (((cmdSyntaxPtr)(ct34))->cmd[1] = (arg),(cmdSyntaxPtr)(ct34))
350
351#define CIS_NOSTRIP 0 // Strip characters to character size
352#define CIS_STRIP 1 // Strip any 8-bit characters to 7 bits
353
354// Send a break of arg milliseconds
355//
356#define CMD_SEND_BRK(arg) \
357 (((cmdSyntaxPtr)(ct35))->cmd[1] = (arg),(cmdSyntaxPtr)(ct35))
358
359// Set error reporting mode
360//
361#define CMD_SET_ERROR(arg) \
362 (((cmdSyntaxPtr)(ct36))->cmd[1] = (arg),(cmdSyntaxPtr)(ct36))
363
364#define CSE_ESTAT 0 // Report error in a status packet
365#define CSE_NOREP 1 // Treat character as though it were good
366#define CSE_DROP 2 // Discard the character
367#define CSE_NULL 3 // Replace with a null
368#define CSE_MARK 4 // Replace with a 3-character sequence (as Unix)
369
370#define CSE_REPLACE 0x8 // Replace the errored character with the
371 // replacement character defined here
372
373#define CSE_STAT_REPLACE 0x18 // Replace the errored character with the
374 // replacement character defined here AND
375 // report the error as a status packet (as in
376 // CSE_ESTAT).
377
378
379// COMMAND 37, to send flow control packets, is handled only by low-level
380// library code in response to data movement and shouldn't ever be sent by the
381// user code. See i2pack.h and the body of i2lib.c for details.
382
383// Enable on-board post-processing, using options given in oflag argument.
384// Formerly, this command was automatically preceded by a CMD_OPOST_OFF command
385// because the loadware does not permit sending back-to-back CMD_OPOST_ON
386// commands without an intervening CMD_OPOST_OFF. BUT, WE LEARN 18 MAY 92, that
387// CMD_OPOST_ON and CMD_OPOST_OFF must each be at the end of a packet (or in a
388// solo packet). This means the caller must specify separately CMD_OPOST_OFF,
389// CMD_OPOST_ON(parm) when he calls i2QueueCommands(). That function will ensure
390// each gets a separate packet. Extra CMD_OPOST_OFF's are always ok.
391//
392#define CMD_OPOST_ON(oflag) \
393 (*(USHORT *)(((cmdSyntaxPtr)(ct39))->cmd[1]) = (oflag), \
394 (cmdSyntaxPtr)(ct39))
395
396#define CMD_OPOST_OFF (cmdSyntaxPtr)(ct40) // Disable on-board post-proc
397
398#define CMD_RESUME (cmdSyntaxPtr)(ct41) // Resume: behave as though an XON
399 // were received;
400
401// Set Transmit baud rate (see command 7 for arguments)
402//
403#define CMD_SETBAUD_TX(arg) \
404 (((cmdSyntaxPtr)(ct42))->cmd[1] = (arg),(cmdSyntaxPtr)(ct42))
405
406// Set Receive baud rate (see command 7 for arguments)
407//
408#define CMD_SETBAUD_RX(arg) \
409 (((cmdSyntaxPtr)(ct43))->cmd[1] = (arg),(cmdSyntaxPtr)(ct43))
410
411// Request interrupt from board each arg milliseconds. Interrupt will specify
412// "received data", even though there may be no data present. If arg == 0,
413// disables any such interrupts.
414//
415#define CMD_PING_REQ(arg) \
416 (((cmdSyntaxPtr)(ct44))->cmd[1] = (arg),(cmdSyntaxPtr)(ct44))
417
418#define CMD_HOT_ENAB (cmdSyntaxPtr)(ct45) // Enable Hot-key checking
419#define CMD_HOT_DSAB (cmdSyntaxPtr)(ct46) // Disable Hot-key checking
420
421#if 0
422// COMMAND 47: Send Protocol info via Unix flags:
423// iflag = Unix tty t_iflag
424// cflag = Unix tty t_cflag
425// lflag = Unix tty t_lflag
426// See System V Unix/Xenix documentation for the meanings of the bit fields
427// within these flags
428//
429#define CMD_UNIX_FLAGS(iflag,cflag,lflag) i2cmdUnixFlags(iflag,cflag,lflag)
430#endif /* 0 */
431
432#define CMD_DSRFL_ENAB (cmdSyntaxPtr)(ct48) // Enable DSR receiver ctrl
433#define CMD_DSRFL_DSAB (cmdSyntaxPtr)(ct49) // Disable DSR receiver ctrl
434#define CMD_DTRFL_ENAB (cmdSyntaxPtr)(ct50) // Enable DTR flow control
435#define CMD_DTRFL_DSAB (cmdSyntaxPtr)(ct51) // Disable DTR flow control
436#define CMD_BAUD_RESET (cmdSyntaxPtr)(ct52) // Reset baudrate table
437
438// COMMAND 54: Define custom rate #1
439// rate = (short) 1/10 of the desired baud rate
440//
441#define CMD_BAUD_DEF1(rate) i2cmdBaudDef(1,rate)
442
443// COMMAND 55: Define custom rate #2
444// rate = (short) 1/10 of the desired baud rate
445//
446#define CMD_BAUD_DEF2(rate) i2cmdBaudDef(2,rate)
447
448// Pause arg hundredths of seconds. (Note, this is NOT milliseconds.)
449//
450#define CMD_PAUSE(arg) \
451 (((cmdSyntaxPtr)(ct56))->cmd[1] = (arg),(cmdSyntaxPtr)(ct56))
452
453#define CMD_SUSPEND (cmdSyntaxPtr)(ct57) // Suspend output
454#define CMD_UNSUSPEND (cmdSyntaxPtr)(ct58) // Un-Suspend output
455
456// Set parity-checking options
457//
458#define CMD_PARCHK(arg) \
459 (((cmdSyntaxPtr)(ct59))->cmd[1] = (arg),(cmdSyntaxPtr)(ct59))
460
461#define CPK_ENAB 0 // Enable parity checking on input
462#define CPK_DSAB 1 // Disable parity checking on input
463
464#define CMD_BMARK_REQ (cmdSyntaxPtr)(ct60) // Bookmark request
465
466
467// Enable/Disable internal loopback mode
468//
469#define CMD_INLOOP(arg) \
470 (((cmdSyntaxPtr)(ct61))->cmd[1] = (arg),(cmdSyntaxPtr)(ct61))
471
472#define CIN_DISABLE 0 // Normal operation (default)
473#define CIN_ENABLE 1 // Internal (local) loopback
474#define CIN_REMOTE 2 // Remote loopback
475
476// Specify timeout for hotkeys: Delay will be (arg x 10) milliseconds, arg == 0
477// --> no timeout: wait forever.
478//
479#define CMD_HOT_TIME(arg) \
480 (((cmdSyntaxPtr)(ct62))->cmd[1] = (arg),(cmdSyntaxPtr)(ct62))
481
482
483// Define (outgoing) xon for receive flow control
484//
485#define CMD_DEF_OXON(arg) \
486 (((cmdSyntaxPtr)(ct63))->cmd[1] = (arg),(cmdSyntaxPtr)(ct63))
487
488// Define (outgoing) xoff for receiver flow control
489//
490#define CMD_DEF_OXOFF(arg) \
491 (((cmdSyntaxPtr)(ct64))->cmd[1] = (arg),(cmdSyntaxPtr)(ct64))
492
493// Enable/Disable RTS on transmit (1/2 duplex-style)
494//
495#define CMD_RTS_XMIT(arg) \
496 (((cmdSyntaxPtr)(ct65))->cmd[1] = (arg),(cmdSyntaxPtr)(ct65))
497
498#define CHD_DISABLE 0
499#define CHD_ENABLE 1
500
501// Set high-water-mark level (debugging use only)
502//
503#define CMD_SETHIGHWAT(arg) \
504 (((cmdSyntaxPtr)(ct66))->cmd[1] = (arg),(cmdSyntaxPtr)(ct66))
505
506// Start flushing tagged data (tag = 0-14)
507//
508#define CMD_START_SELFL(tag) \
509 (((cmdSyntaxPtr)(ct67))->cmd[1] = (tag),(cmdSyntaxPtr)(ct67))
510
511// End flushing tagged data (tag = 0-14)
512//
513#define CMD_END_SELFL(tag) \
514 (((cmdSyntaxPtr)(ct68))->cmd[1] = (tag),(cmdSyntaxPtr)(ct68))
515
516#define CMD_HWFLOW_OFF (cmdSyntaxPtr)(ct69) // Disable HW TX flow control
517#define CMD_ODSRFL_ENAB (cmdSyntaxPtr)(ct70) // Enable DSR output f/c
518#define CMD_ODSRFL_DSAB (cmdSyntaxPtr)(ct71) // Disable DSR output f/c
519#define CMD_ODCDFL_ENAB (cmdSyntaxPtr)(ct72) // Enable DCD output f/c
520#define CMD_ODCDFL_DSAB (cmdSyntaxPtr)(ct73) // Disable DCD output f/c
521
522// Set transmit interrupt load level. Count should be an even value 2-12
523//
524#define CMD_LOADLEVEL(count) \
525 (((cmdSyntaxPtr)(ct74))->cmd[1] = (count),(cmdSyntaxPtr)(ct74))
526
527// If reporting DSS changes, map to character sequence FFh, 2, MSR
528//
529#define CMD_STATDATA(arg) \
530 (((cmdSyntaxPtr)(ct75))->cmd[1] = (arg),(cmdSyntaxPtr)(ct75))
531
532#define CSTD_DISABLE// Report DSS changes as status packets only (default)
533#define CSTD_ENABLE // Report DSS changes as in-band data sequence as well as
534 // by status packet.
535
536#define CMD_BREAK_ON (cmdSyntaxPtr)(ct76)// Set break and stop xmit
537#define CMD_BREAK_OFF (cmdSyntaxPtr)(ct77)// End break and restart xmit
538#define CMD_GETFC (cmdSyntaxPtr)(ct78)// Request for flow control packet
539 // from board.
540
541// Transmit this character immediately
542//
543#define CMD_XMIT_NOW(ch) \
544 (((cmdSyntaxPtr)(ct79))->cmd[1] = (ch),(cmdSyntaxPtr)(ct79))
545
546// Set baud rate via "divisor latch"
547//
548#define CMD_DIVISOR_LATCH(which,value) \
549 (((cmdSyntaxPtr)(ct80))->cmd[1] = (which), \
550 *(USHORT *)(((cmdSyntaxPtr)(ct80))->cmd[2]) = (value), \
551 (cmdSyntaxPtr)(ct80))
552
553#define CDL_RX 1 // Set receiver rate
554#define CDL_TX 2 // Set transmit rate
555 // (CDL_TX | CDL_RX) Set both rates
556
557// Request for special diagnostic status pkt from the board.
558//
559#define CMD_GET_STATUS (cmdSyntaxPtr)(ct81)
560
561// Request time-stamped transmit character count packet.
562//
563#define CMD_GET_TXCNT (cmdSyntaxPtr)(ct82)
564
565// Request time-stamped receive character count packet.
566//
567#define CMD_GET_RXCNT (cmdSyntaxPtr)(ct83)
568
569// Request for box/board I.D. packet.
570#define CMD_GET_BOXIDS (cmdSyntaxPtr)(ct84)
571
572// Enable or disable multiple channels according to bit-mapped ushorts box 1-4
573//
574#define CMD_ENAB_MULT(enable, box1, box2, box3, box4) \
575 (((cmdSytaxPtr)(ct85))->cmd[1] = (enable), \
576 *(USHORT *)(((cmdSyntaxPtr)(ct85))->cmd[2]) = (box1), \
577 *(USHORT *)(((cmdSyntaxPtr)(ct85))->cmd[4]) = (box2), \
578 *(USHORT *)(((cmdSyntaxPtr)(ct85))->cmd[6]) = (box3), \
579 *(USHORT *)(((cmdSyntaxPtr)(ct85))->cmd[8]) = (box4), \
580 (cmdSyntaxPtr)(ct85))
581
582#define CEM_DISABLE 0
583#define CEM_ENABLE 1
584
585// Enable or disable receiver or receiver interrupts (default both enabled)
586//
587#define CMD_RCV_ENABLE(ch) \
588 (((cmdSyntaxPtr)(ct86))->cmd[1] = (ch),(cmdSyntaxPtr)(ct86))
589
590#define CRE_OFF 0 // Disable the receiver
591#define CRE_ON 1 // Enable the receiver
592#define CRE_INTOFF 2 // Disable receiver interrupts (to loadware)
593#define CRE_INTON 3 // Enable receiver interrupts (to loadware)
594
595// Starts up a hardware test process, which runs transparently, and sends a
596// STAT_HWFAIL packet in case a hardware failure is detected.
597//
598#define CMD_HW_TEST (cmdSyntaxPtr)(ct87)
599
600// Change receiver threshold and timeout value:
601// Defaults: timeout = 20mS
602// threshold count = 8 when DTRflow not in use,
603// threshold count = 5 when DTRflow in use.
604//
605#define CMD_RCV_THRESHOLD(count,ms) \
606 (((cmdSyntaxPtr)(ct88))->cmd[1] = (count), \
607 ((cmdSyntaxPtr)(ct88))->cmd[2] = (ms), \
608 (cmdSyntaxPtr)(ct88))
609
610// Makes the loadware report DSS signals for this channel immediately.
611//
612#define CMD_DSS_NOW (cmdSyntaxPtr)(ct89)
613
614// Set the receive silo parameters
615// timeout is ms idle wait until delivery (~VTIME)
616// threshold is max characters cause interrupt (~VMIN)
617//
618#define CMD_SET_SILO(timeout,threshold) \
619 (((cmdSyntaxPtr)(ct90))->cmd[1] = (timeout), \
620 ((cmdSyntaxPtr)(ct90))->cmd[2] = (threshold), \
621 (cmdSyntaxPtr)(ct90))
622
623// Set timed break in decisecond (1/10s)
624//
625#define CMD_LBREAK(ds) \
626 (((cmdSyntaxPtr)(ct91))->cmd[1] = (ds),(cmdSyntaxPtr)(ct66))
627
628
629
630#endif // I2CMD_H
diff --git a/drivers/char/ip2/i2ellis.c b/drivers/char/ip2/i2ellis.c
deleted file mode 100644
index 29db44de399f..000000000000
--- a/drivers/char/ip2/i2ellis.c
+++ /dev/null
@@ -1,1403 +0,0 @@
1/*******************************************************************************
2*
3* (c) 1998 by Computone Corporation
4*
5********************************************************************************
6*
7*
8* PACKAGE: Linux tty Device Driver for IntelliPort family of multiport
9* serial I/O controllers.
10*
11* DESCRIPTION: Low-level interface code for the device driver
12* (This is included source code, not a separate compilation
13* module.)
14*
15*******************************************************************************/
16//---------------------------------------------
17// Function declarations private to this module
18//---------------------------------------------
19// Functions called only indirectly through i2eBordStr entries.
20
21static int iiWriteBuf16(i2eBordStrPtr, unsigned char *, int);
22static int iiWriteBuf8(i2eBordStrPtr, unsigned char *, int);
23static int iiReadBuf16(i2eBordStrPtr, unsigned char *, int);
24static int iiReadBuf8(i2eBordStrPtr, unsigned char *, int);
25
26static unsigned short iiReadWord16(i2eBordStrPtr);
27static unsigned short iiReadWord8(i2eBordStrPtr);
28static void iiWriteWord16(i2eBordStrPtr, unsigned short);
29static void iiWriteWord8(i2eBordStrPtr, unsigned short);
30
31static int iiWaitForTxEmptyII(i2eBordStrPtr, int);
32static int iiWaitForTxEmptyIIEX(i2eBordStrPtr, int);
33static int iiTxMailEmptyII(i2eBordStrPtr);
34static int iiTxMailEmptyIIEX(i2eBordStrPtr);
35static int iiTrySendMailII(i2eBordStrPtr, unsigned char);
36static int iiTrySendMailIIEX(i2eBordStrPtr, unsigned char);
37
38static unsigned short iiGetMailII(i2eBordStrPtr);
39static unsigned short iiGetMailIIEX(i2eBordStrPtr);
40
41static void iiEnableMailIrqII(i2eBordStrPtr);
42static void iiEnableMailIrqIIEX(i2eBordStrPtr);
43static void iiWriteMaskII(i2eBordStrPtr, unsigned char);
44static void iiWriteMaskIIEX(i2eBordStrPtr, unsigned char);
45
46static void ii2Nop(void);
47
48//***************
49//* Static Data *
50//***************
51
52static int ii2Safe; // Safe I/O address for delay routine
53
54static int iiDelayed; // Set when the iiResetDelay function is
55 // called. Cleared when ANY board is reset.
56static DEFINE_RWLOCK(Dl_spinlock);
57
58//********
59//* Code *
60//********
61
62//=======================================================
63// Initialization Routines
64//
65// iiSetAddress
66// iiReset
67// iiResetDelay
68// iiInitialize
69//=======================================================
70
71//******************************************************************************
72// Function: iiSetAddress(pB, address, delay)
73// Parameters: pB - pointer to the board structure
74// address - the purported I/O address of the board
75// delay - pointer to the 1-ms delay function to use
76// in this and any future operations to this board
77//
78// Returns: True if everything appears copacetic.
79// False if there is any error: the pB->i2eError field has the error
80//
81// Description:
82//
83// This routine (roughly) checks for address validity, sets the i2eValid OK and
84// sets the state to II_STATE_COLD which means that we haven't even sent a reset
85// yet.
86//
87//******************************************************************************
88static int
89iiSetAddress( i2eBordStrPtr pB, int address, delayFunc_t delay )
90{
91 // Should any failure occur before init is finished...
92 pB->i2eValid = I2E_INCOMPLETE;
93
94 // Cannot check upper limit except extremely: Might be microchannel
95 // Address must be on an 8-byte boundary
96
97 if ((unsigned int)address <= 0x100
98 || (unsigned int)address >= 0xfff8
99 || (address & 0x7)
100 )
101 {
102 I2_COMPLETE(pB, I2EE_BADADDR);
103 }
104
105 // Initialize accelerators
106 pB->i2eBase = address;
107 pB->i2eData = address + FIFO_DATA;
108 pB->i2eStatus = address + FIFO_STATUS;
109 pB->i2ePointer = address + FIFO_PTR;
110 pB->i2eXMail = address + FIFO_MAIL;
111 pB->i2eXMask = address + FIFO_MASK;
112
113 // Initialize i/o address for ii2DelayIO
114 ii2Safe = address + FIFO_NOP;
115
116 // Initialize the delay routine
117 pB->i2eDelay = ((delay != (delayFunc_t)NULL) ? delay : (delayFunc_t)ii2Nop);
118
119 pB->i2eValid = I2E_MAGIC;
120 pB->i2eState = II_STATE_COLD;
121
122 I2_COMPLETE(pB, I2EE_GOOD);
123}
124
125//******************************************************************************
126// Function: iiReset(pB)
127// Parameters: pB - pointer to the board structure
128//
129// Returns: True if everything appears copacetic.
130// False if there is any error: the pB->i2eError field has the error
131//
132// Description:
133//
134// Attempts to reset the board (see also i2hw.h). Normally, we would use this to
135// reset a board immediately after iiSetAddress(), but it is valid to reset a
136// board from any state, say, in order to change or re-load loadware. (Under
137// such circumstances, no reason to re-run iiSetAddress(), which is why it is a
138// separate routine and not included in this routine.
139//
140//******************************************************************************
141static int
142iiReset(i2eBordStrPtr pB)
143{
144 // Magic number should be set, else even the address is suspect
145 if (pB->i2eValid != I2E_MAGIC)
146 {
147 I2_COMPLETE(pB, I2EE_BADMAGIC);
148 }
149
150 outb(0, pB->i2eBase + FIFO_RESET); /* Any data will do */
151 iiDelay(pB, 50); // Pause between resets
152 outb(0, pB->i2eBase + FIFO_RESET); /* Second reset */
153
154 // We must wait before even attempting to read anything from the FIFO: the
155 // board's P.O.S.T may actually attempt to read and write its end of the
156 // FIFO in order to check flags, loop back (where supported), etc. On
157 // completion of this testing it would reset the FIFO, and on completion
158 // of all // P.O.S.T., write the message. We must not mistake data which
159 // might have been sent for testing as part of the reset message. To
160 // better utilize time, say, when resetting several boards, we allow the
161 // delay to be performed externally; in this way the caller can reset
162 // several boards, delay a single time, then call the initialization
163 // routine for all.
164
165 pB->i2eState = II_STATE_RESET;
166
167 iiDelayed = 0; // i.e., the delay routine hasn't been called since the most
168 // recent reset.
169
170 // Ensure anything which would have been of use to standard loadware is
171 // blanked out, since board has now forgotten everything!.
172
173 pB->i2eUsingIrq = I2_IRQ_UNDEFINED; /* to not use an interrupt so far */
174 pB->i2eWaitingForEmptyFifo = 0;
175 pB->i2eOutMailWaiting = 0;
176 pB->i2eChannelPtr = NULL;
177 pB->i2eChannelCnt = 0;
178
179 pB->i2eLeadoffWord[0] = 0;
180 pB->i2eFifoInInts = 0;
181 pB->i2eFifoOutInts = 0;
182 pB->i2eFatalTrap = NULL;
183 pB->i2eFatal = 0;
184
185 I2_COMPLETE(pB, I2EE_GOOD);
186}
187
188//******************************************************************************
189// Function: iiResetDelay(pB)
190// Parameters: pB - pointer to the board structure
191//
192// Returns: True if everything appears copacetic.
193// False if there is any error: the pB->i2eError field has the error
194//
195// Description:
196//
197// Using the delay defined in board structure, waits two seconds (for board to
198// reset).
199//
200//******************************************************************************
201static int
202iiResetDelay(i2eBordStrPtr pB)
203{
204 if (pB->i2eValid != I2E_MAGIC) {
205 I2_COMPLETE(pB, I2EE_BADMAGIC);
206 }
207 if (pB->i2eState != II_STATE_RESET) {
208 I2_COMPLETE(pB, I2EE_BADSTATE);
209 }
210 iiDelay(pB,2000); /* Now we wait for two seconds. */
211 iiDelayed = 1; /* Delay has been called: ok to initialize */
212 I2_COMPLETE(pB, I2EE_GOOD);
213}
214
215//******************************************************************************
216// Function: iiInitialize(pB)
217// Parameters: pB - pointer to the board structure
218//
219// Returns: True if everything appears copacetic.
220// False if there is any error: the pB->i2eError field has the error
221//
222// Description:
223//
224// Attempts to read the Power-on reset message. Initializes any remaining fields
225// in the pB structure.
226//
227// This should be called as the third step of a process beginning with
228// iiReset(), then iiResetDelay(). This routine checks to see that the structure
229// is "valid" and in the reset state, also confirms that the delay routine has
230// been called since the latest reset (to any board! overly strong!).
231//
232//******************************************************************************
233static int
234iiInitialize(i2eBordStrPtr pB)
235{
236 int itemp;
237 unsigned char c;
238 unsigned short utemp;
239 unsigned int ilimit;
240
241 if (pB->i2eValid != I2E_MAGIC)
242 {
243 I2_COMPLETE(pB, I2EE_BADMAGIC);
244 }
245
246 if (pB->i2eState != II_STATE_RESET || !iiDelayed)
247 {
248 I2_COMPLETE(pB, I2EE_BADSTATE);
249 }
250
251 // In case there is a failure short of our completely reading the power-up
252 // message.
253 pB->i2eValid = I2E_INCOMPLETE;
254
255
256 // Now attempt to read the message.
257
258 for (itemp = 0; itemp < sizeof(porStr); itemp++)
259 {
260 // We expect the entire message is ready.
261 if (!I2_HAS_INPUT(pB)) {
262 pB->i2ePomSize = itemp;
263 I2_COMPLETE(pB, I2EE_PORM_SHORT);
264 }
265
266 pB->i2ePom.c[itemp] = c = inb(pB->i2eData);
267
268 // We check the magic numbers as soon as they are supposed to be read
269 // (rather than after) to minimize effect of reading something we
270 // already suspect can't be "us".
271 if ( (itemp == POR_1_INDEX && c != POR_MAGIC_1) ||
272 (itemp == POR_2_INDEX && c != POR_MAGIC_2))
273 {
274 pB->i2ePomSize = itemp+1;
275 I2_COMPLETE(pB, I2EE_BADMAGIC);
276 }
277 }
278
279 pB->i2ePomSize = itemp;
280
281 // Ensure that this was all the data...
282 if (I2_HAS_INPUT(pB))
283 I2_COMPLETE(pB, I2EE_PORM_LONG);
284
285 // For now, we'll fail to initialize if P.O.S.T reports bad chip mapper:
286 // Implying we will not be able to download any code either: That's ok: the
287 // condition is pretty explicit.
288 if (pB->i2ePom.e.porDiag1 & POR_BAD_MAPPER)
289 {
290 I2_COMPLETE(pB, I2EE_POSTERR);
291 }
292
293 // Determine anything which must be done differently depending on the family
294 // of boards!
295 switch (pB->i2ePom.e.porID & POR_ID_FAMILY)
296 {
297 case POR_ID_FII: // IntelliPort-II
298
299 pB->i2eFifoStyle = FIFO_II;
300 pB->i2eFifoSize = 512; // 512 bytes, always
301 pB->i2eDataWidth16 = false;
302
303 pB->i2eMaxIrq = 15; // Because board cannot tell us it is in an 8-bit
304 // slot, we do allow it to be done (documentation!)
305
306 pB->i2eGoodMap[1] =
307 pB->i2eGoodMap[2] =
308 pB->i2eGoodMap[3] =
309 pB->i2eChannelMap[1] =
310 pB->i2eChannelMap[2] =
311 pB->i2eChannelMap[3] = 0;
312
313 switch (pB->i2ePom.e.porID & POR_ID_SIZE)
314 {
315 case POR_ID_II_4:
316 pB->i2eGoodMap[0] =
317 pB->i2eChannelMap[0] = 0x0f; // four-port
318
319 // Since porPorts1 is based on the Hardware ID register, the numbers
320 // should always be consistent for IntelliPort-II. Ditto below...
321 if (pB->i2ePom.e.porPorts1 != 4)
322 {
323 I2_COMPLETE(pB, I2EE_INCONSIST);
324 }
325 break;
326
327 case POR_ID_II_8:
328 case POR_ID_II_8R:
329 pB->i2eGoodMap[0] =
330 pB->i2eChannelMap[0] = 0xff; // Eight port
331 if (pB->i2ePom.e.porPorts1 != 8)
332 {
333 I2_COMPLETE(pB, I2EE_INCONSIST);
334 }
335 break;
336
337 case POR_ID_II_6:
338 pB->i2eGoodMap[0] =
339 pB->i2eChannelMap[0] = 0x3f; // Six Port
340 if (pB->i2ePom.e.porPorts1 != 6)
341 {
342 I2_COMPLETE(pB, I2EE_INCONSIST);
343 }
344 break;
345 }
346
347 // Fix up the "good channel list based on any errors reported.
348 if (pB->i2ePom.e.porDiag1 & POR_BAD_UART1)
349 {
350 pB->i2eGoodMap[0] &= ~0x0f;
351 }
352
353 if (pB->i2ePom.e.porDiag1 & POR_BAD_UART2)
354 {
355 pB->i2eGoodMap[0] &= ~0xf0;
356 }
357
358 break; // POR_ID_FII case
359
360 case POR_ID_FIIEX: // IntelliPort-IIEX
361
362 pB->i2eFifoStyle = FIFO_IIEX;
363
364 itemp = pB->i2ePom.e.porFifoSize;
365
366 // Implicit assumption that fifo would not grow beyond 32k,
367 // nor would ever be less than 256.
368
369 if (itemp < 8 || itemp > 15)
370 {
371 I2_COMPLETE(pB, I2EE_INCONSIST);
372 }
373 pB->i2eFifoSize = (1 << itemp);
374
375 // These are based on what P.O.S.T thinks should be there, based on
376 // box ID registers
377 ilimit = pB->i2ePom.e.porNumBoxes;
378 if (ilimit > ABS_MAX_BOXES)
379 {
380 ilimit = ABS_MAX_BOXES;
381 }
382
383 // For as many boxes as EXIST, gives the type of box.
384 // Added 8/6/93: check for the ISA-4 (asic) which looks like an
385 // expandable but for whom "8 or 16?" is not the right question.
386
387 utemp = pB->i2ePom.e.porFlags;
388 if (utemp & POR_CEX4)
389 {
390 pB->i2eChannelMap[0] = 0x000f;
391 } else {
392 utemp &= POR_BOXES;
393 for (itemp = 0; itemp < ilimit; itemp++)
394 {
395 pB->i2eChannelMap[itemp] =
396 ((utemp & POR_BOX_16) ? 0xffff : 0x00ff);
397 utemp >>= 1;
398 }
399 }
400
401 // These are based on what P.O.S.T actually found.
402
403 utemp = (pB->i2ePom.e.porPorts2 << 8) + pB->i2ePom.e.porPorts1;
404
405 for (itemp = 0; itemp < ilimit; itemp++)
406 {
407 pB->i2eGoodMap[itemp] = 0;
408 if (utemp & 1) pB->i2eGoodMap[itemp] |= 0x000f;
409 if (utemp & 2) pB->i2eGoodMap[itemp] |= 0x00f0;
410 if (utemp & 4) pB->i2eGoodMap[itemp] |= 0x0f00;
411 if (utemp & 8) pB->i2eGoodMap[itemp] |= 0xf000;
412 utemp >>= 4;
413 }
414
415 // Now determine whether we should transfer in 8 or 16-bit mode.
416 switch (pB->i2ePom.e.porBus & (POR_BUS_SLOT16 | POR_BUS_DIP16) )
417 {
418 case POR_BUS_SLOT16 | POR_BUS_DIP16:
419 pB->i2eDataWidth16 = true;
420 pB->i2eMaxIrq = 15;
421 break;
422
423 case POR_BUS_SLOT16:
424 pB->i2eDataWidth16 = false;
425 pB->i2eMaxIrq = 15;
426 break;
427
428 case 0:
429 case POR_BUS_DIP16: // In an 8-bit slot, DIP switch don't care.
430 default:
431 pB->i2eDataWidth16 = false;
432 pB->i2eMaxIrq = 7;
433 break;
434 }
435 break; // POR_ID_FIIEX case
436
437 default: // Unknown type of board
438 I2_COMPLETE(pB, I2EE_BAD_FAMILY);
439 break;
440 } // End the switch based on family
441
442 // Temporarily, claim there is no room in the outbound fifo.
443 // We will maintain this whenever we check for an empty outbound FIFO.
444 pB->i2eFifoRemains = 0;
445
446 // Now, based on the bus type, should we expect to be able to re-configure
447 // interrupts (say, for testing purposes).
448 switch (pB->i2ePom.e.porBus & POR_BUS_TYPE)
449 {
450 case POR_BUS_T_ISA:
451 case POR_BUS_T_UNK: // If the type of bus is undeclared, assume ok.
452 case POR_BUS_T_MCA:
453 case POR_BUS_T_EISA:
454 break;
455 default:
456 I2_COMPLETE(pB, I2EE_BADBUS);
457 }
458
459 if (pB->i2eDataWidth16)
460 {
461 pB->i2eWriteBuf = iiWriteBuf16;
462 pB->i2eReadBuf = iiReadBuf16;
463 pB->i2eWriteWord = iiWriteWord16;
464 pB->i2eReadWord = iiReadWord16;
465 } else {
466 pB->i2eWriteBuf = iiWriteBuf8;
467 pB->i2eReadBuf = iiReadBuf8;
468 pB->i2eWriteWord = iiWriteWord8;
469 pB->i2eReadWord = iiReadWord8;
470 }
471
472 switch(pB->i2eFifoStyle)
473 {
474 case FIFO_II:
475 pB->i2eWaitForTxEmpty = iiWaitForTxEmptyII;
476 pB->i2eTxMailEmpty = iiTxMailEmptyII;
477 pB->i2eTrySendMail = iiTrySendMailII;
478 pB->i2eGetMail = iiGetMailII;
479 pB->i2eEnableMailIrq = iiEnableMailIrqII;
480 pB->i2eWriteMask = iiWriteMaskII;
481
482 break;
483
484 case FIFO_IIEX:
485 pB->i2eWaitForTxEmpty = iiWaitForTxEmptyIIEX;
486 pB->i2eTxMailEmpty = iiTxMailEmptyIIEX;
487 pB->i2eTrySendMail = iiTrySendMailIIEX;
488 pB->i2eGetMail = iiGetMailIIEX;
489 pB->i2eEnableMailIrq = iiEnableMailIrqIIEX;
490 pB->i2eWriteMask = iiWriteMaskIIEX;
491
492 break;
493
494 default:
495 I2_COMPLETE(pB, I2EE_INCONSIST);
496 }
497
498 // Initialize state information.
499 pB->i2eState = II_STATE_READY; // Ready to load loadware.
500
501 // Some Final cleanup:
502 // For some boards, the bootstrap firmware may perform some sort of test
503 // resulting in a stray character pending in the incoming mailbox. If one is
504 // there, it should be read and discarded, especially since for the standard
505 // firmware, it's the mailbox that interrupts the host.
506
507 pB->i2eStartMail = iiGetMail(pB);
508
509 // Throw it away and clear the mailbox structure element
510 pB->i2eStartMail = NO_MAIL_HERE;
511
512 // Everything is ok now, return with good status/
513
514 pB->i2eValid = I2E_MAGIC;
515 I2_COMPLETE(pB, I2EE_GOOD);
516}
517
518//******************************************************************************
519// Function: ii2DelayTimer(mseconds)
520// Parameters: mseconds - number of milliseconds to delay
521//
522// Returns: Nothing
523//
524// Description:
525//
526// This routine delays for approximately mseconds milliseconds and is intended
527// to be called indirectly through i2Delay field in i2eBordStr. It uses the
528// Linux timer_list mechanism.
529//
530// The Linux timers use a unit called "jiffies" which are 10mS in the Intel
531// architecture. This function rounds the delay period up to the next "jiffy".
532// In the Alpha architecture the "jiffy" is 1mS, but this driver is not intended
533// for Alpha platforms at this time.
534//
535//******************************************************************************
536static void
537ii2DelayTimer(unsigned int mseconds)
538{
539 msleep_interruptible(mseconds);
540}
541
542#if 0
543//static void ii2DelayIO(unsigned int);
544//******************************************************************************
545// !!! Not Used, this is DOS crap, some of you young folks may be interested in
546// in how things were done in the stone age of caculating machines !!!
547// Function: ii2DelayIO(mseconds)
548// Parameters: mseconds - number of milliseconds to delay
549//
550// Returns: Nothing
551//
552// Description:
553//
554// This routine delays for approximately mseconds milliseconds and is intended
555// to be called indirectly through i2Delay field in i2eBordStr. It is intended
556// for use where a clock-based function is impossible: for example, DOS drivers.
557//
558// This function uses the IN instruction to place bounds on the timing and
559// assumes that ii2Safe has been set. This is because I/O instructions are not
560// subject to caching and will therefore take a certain minimum time. To ensure
561// the delay is at least long enough on fast machines, it is based on some
562// fastest-case calculations. On slower machines this may cause VERY long
563// delays. (3 x fastest case). In the fastest case, everything is cached except
564// the I/O instruction itself.
565//
566// Timing calculations:
567// The fastest bus speed for I/O operations is likely to be 10 MHz. The I/O
568// operation in question is a byte operation to an odd address. For 8-bit
569// operations, the architecture generally enforces two wait states. At 10 MHz, a
570// single cycle time is 100nS. A read operation at two wait states takes 6
571// cycles for a total time of 600nS. Therefore approximately 1666 iterations
572// would be required to generate a single millisecond delay. The worst
573// (reasonable) case would be an 8MHz system with no cacheing. In this case, the
574// I/O instruction would take 125nS x 6 cyles = 750 nS. More importantly, code
575// fetch of other instructions in the loop would take time (zero wait states,
576// however) and would be hard to estimate. This is minimized by using in-line
577// assembler for the in inner loop of IN instructions. This consists of just a
578// few bytes. So we'll guess about four code fetches per loop. Each code fetch
579// should take four cycles, so we have 125nS * 8 = 1000nS. Worst case then is
580// that what should have taken 1 mS takes instead 1666 * (1750) = 2.9 mS.
581//
582// So much for theoretical timings: results using 1666 value on some actual
583// machines:
584// IBM 286 6MHz 3.15 mS
585// Zenith 386 33MHz 2.45 mS
586// (brandX) 386 33MHz 1.90 mS (has cache)
587// (brandY) 486 33MHz 2.35 mS
588// NCR 486 ?? 1.65 mS (microchannel)
589//
590// For most machines, it is probably safe to scale this number back (remember,
591// for robust operation use an actual timed delay if possible), so we are using
592// a value of 1190. This yields 1.17 mS for the fastest machine in our sample,
593// 1.75 mS for typical 386 machines, and 2.25 mS the absolute slowest machine.
594//
595// 1/29/93:
596// The above timings are too slow. Actual cycle times might be faster. ISA cycle
597// times could approach 500 nS, and ...
598// The IBM model 77 being microchannel has no wait states for 8-bit reads and
599// seems to be accessing the I/O at 440 nS per access (from start of one to
600// start of next). This would imply we need 1000/.440 = 2272 iterations to
601// guarantee we are fast enough. In actual testing, we see that 2 * 1190 are in
602// fact enough. For diagnostics, we keep the level at 1190, but developers note
603// this needs tuning.
604//
605// Safe assumption: 2270 i/o reads = 1 millisecond
606//
607//******************************************************************************
608
609
610static int ii2DelValue = 1190; // See timing calculations below
611 // 1666 for fastest theoretical machine
612 // 1190 safe for most fast 386 machines
613 // 1000 for fastest machine tested here
614 // 540 (sic) for AT286/6Mhz
615static void
616ii2DelayIO(unsigned int mseconds)
617{
618 if (!ii2Safe)
619 return; /* Do nothing if this variable uninitialized */
620
621 while(mseconds--) {
622 int i = ii2DelValue;
623 while ( i-- ) {
624 inb(ii2Safe);
625 }
626 }
627}
628#endif
629
630//******************************************************************************
631// Function: ii2Nop()
632// Parameters: None
633//
634// Returns: Nothing
635//
636// Description:
637//
638// iiInitialize will set i2eDelay to this if the delay parameter is NULL. This
639// saves checking for a NULL pointer at every call.
640//******************************************************************************
641static void
642ii2Nop(void)
643{
644 return; // no mystery here
645}
646
647//=======================================================
648// Routines which are available in 8/16-bit versions, or
649// in different fifo styles. These are ALL called
650// indirectly through the board structure.
651//=======================================================
652
653//******************************************************************************
654// Function: iiWriteBuf16(pB, address, count)
655// Parameters: pB - pointer to board structure
656// address - address of data to write
657// count - number of data bytes to write
658//
659// Returns: True if everything appears copacetic.
660// False if there is any error: the pB->i2eError field has the error
661//
662// Description:
663//
664// Writes 'count' bytes from 'address' to the data fifo specified by the board
665// structure pointer pB. Should count happen to be odd, an extra pad byte is
666// sent (identity unknown...). Uses 16-bit (word) operations. Is called
667// indirectly through pB->i2eWriteBuf.
668//
669//******************************************************************************
670static int
671iiWriteBuf16(i2eBordStrPtr pB, unsigned char *address, int count)
672{
673 // Rudimentary sanity checking here.
674 if (pB->i2eValid != I2E_MAGIC)
675 I2_COMPLETE(pB, I2EE_INVALID);
676
677 I2_OUTSW(pB->i2eData, address, count);
678
679 I2_COMPLETE(pB, I2EE_GOOD);
680}
681
682//******************************************************************************
683// Function: iiWriteBuf8(pB, address, count)
684// Parameters: pB - pointer to board structure
685// address - address of data to write
686// count - number of data bytes to write
687//
688// Returns: True if everything appears copacetic.
689// False if there is any error: the pB->i2eError field has the error
690//
691// Description:
692//
693// Writes 'count' bytes from 'address' to the data fifo specified by the board
694// structure pointer pB. Should count happen to be odd, an extra pad byte is
695// sent (identity unknown...). This is to be consistent with the 16-bit version.
696// Uses 8-bit (byte) operations. Is called indirectly through pB->i2eWriteBuf.
697//
698//******************************************************************************
699static int
700iiWriteBuf8(i2eBordStrPtr pB, unsigned char *address, int count)
701{
702 /* Rudimentary sanity checking here */
703 if (pB->i2eValid != I2E_MAGIC)
704 I2_COMPLETE(pB, I2EE_INVALID);
705
706 I2_OUTSB(pB->i2eData, address, count);
707
708 I2_COMPLETE(pB, I2EE_GOOD);
709}
710
711//******************************************************************************
712// Function: iiReadBuf16(pB, address, count)
713// Parameters: pB - pointer to board structure
714// address - address to put data read
715// count - number of data bytes to read
716//
717// Returns: True if everything appears copacetic.
718// False if there is any error: the pB->i2eError field has the error
719//
720// Description:
721//
722// Reads 'count' bytes into 'address' from the data fifo specified by the board
723// structure pointer pB. Should count happen to be odd, an extra pad byte is
724// received (identity unknown...). Uses 16-bit (word) operations. Is called
725// indirectly through pB->i2eReadBuf.
726//
727//******************************************************************************
728static int
729iiReadBuf16(i2eBordStrPtr pB, unsigned char *address, int count)
730{
731 // Rudimentary sanity checking here.
732 if (pB->i2eValid != I2E_MAGIC)
733 I2_COMPLETE(pB, I2EE_INVALID);
734
735 I2_INSW(pB->i2eData, address, count);
736
737 I2_COMPLETE(pB, I2EE_GOOD);
738}
739
740//******************************************************************************
741// Function: iiReadBuf8(pB, address, count)
742// Parameters: pB - pointer to board structure
743// address - address to put data read
744// count - number of data bytes to read
745//
746// Returns: True if everything appears copacetic.
747// False if there is any error: the pB->i2eError field has the error
748//
749// Description:
750//
751// Reads 'count' bytes into 'address' from the data fifo specified by the board
752// structure pointer pB. Should count happen to be odd, an extra pad byte is
753// received (identity unknown...). This to match the 16-bit behaviour. Uses
754// 8-bit (byte) operations. Is called indirectly through pB->i2eReadBuf.
755//
756//******************************************************************************
757static int
758iiReadBuf8(i2eBordStrPtr pB, unsigned char *address, int count)
759{
760 // Rudimentary sanity checking here.
761 if (pB->i2eValid != I2E_MAGIC)
762 I2_COMPLETE(pB, I2EE_INVALID);
763
764 I2_INSB(pB->i2eData, address, count);
765
766 I2_COMPLETE(pB, I2EE_GOOD);
767}
768
769//******************************************************************************
770// Function: iiReadWord16(pB)
771// Parameters: pB - pointer to board structure
772//
773// Returns: True if everything appears copacetic.
774// False if there is any error: the pB->i2eError field has the error
775//
776// Description:
777//
778// Returns the word read from the data fifo specified by the board-structure
779// pointer pB. Uses a 16-bit operation. Is called indirectly through
780// pB->i2eReadWord.
781//
782//******************************************************************************
783static unsigned short
784iiReadWord16(i2eBordStrPtr pB)
785{
786 return inw(pB->i2eData);
787}
788
789//******************************************************************************
790// Function: iiReadWord8(pB)
791// Parameters: pB - pointer to board structure
792//
793// Returns: True if everything appears copacetic.
794// False if there is any error: the pB->i2eError field has the error
795//
796// Description:
797//
798// Returns the word read from the data fifo specified by the board-structure
799// pointer pB. Uses two 8-bit operations. Bytes are assumed to be LSB first. Is
800// called indirectly through pB->i2eReadWord.
801//
802//******************************************************************************
803static unsigned short
804iiReadWord8(i2eBordStrPtr pB)
805{
806 unsigned short urs;
807
808 urs = inb(pB->i2eData);
809
810 return (inb(pB->i2eData) << 8) | urs;
811}
812
813//******************************************************************************
814// Function: iiWriteWord16(pB, value)
815// Parameters: pB - pointer to board structure
816// value - data to write
817//
818// Returns: True if everything appears copacetic.
819// False if there is any error: the pB->i2eError field has the error
820//
821// Description:
822//
823// Writes the word 'value' to the data fifo specified by the board-structure
824// pointer pB. Uses 16-bit operation. Is called indirectly through
825// pB->i2eWriteWord.
826//
827//******************************************************************************
828static void
829iiWriteWord16(i2eBordStrPtr pB, unsigned short value)
830{
831 outw((int)value, pB->i2eData);
832}
833
834//******************************************************************************
835// Function: iiWriteWord8(pB, value)
836// Parameters: pB - pointer to board structure
837// value - data to write
838//
839// Returns: True if everything appears copacetic.
840// False if there is any error: the pB->i2eError field has the error
841//
842// Description:
843//
844// Writes the word 'value' to the data fifo specified by the board-structure
845// pointer pB. Uses two 8-bit operations (writes LSB first). Is called
846// indirectly through pB->i2eWriteWord.
847//
848//******************************************************************************
849static void
850iiWriteWord8(i2eBordStrPtr pB, unsigned short value)
851{
852 outb((char)value, pB->i2eData);
853 outb((char)(value >> 8), pB->i2eData);
854}
855
856//******************************************************************************
857// Function: iiWaitForTxEmptyII(pB, mSdelay)
858// Parameters: pB - pointer to board structure
859// mSdelay - period to wait before returning
860//
861// Returns: True if the FIFO is empty.
862// False if it not empty in the required time: the pB->i2eError
863// field has the error.
864//
865// Description:
866//
867// Waits up to "mSdelay" milliseconds for the outgoing FIFO to become empty; if
868// not empty by the required time, returns false and error in pB->i2eError,
869// otherwise returns true.
870//
871// mSdelay == 0 is taken to mean must be empty on the first test.
872//
873// This version operates on IntelliPort-II - style FIFO's
874//
875// Note this routine is organized so that if status is ok there is no delay at
876// all called either before or after the test. Is called indirectly through
877// pB->i2eWaitForTxEmpty.
878//
879//******************************************************************************
880static int
881iiWaitForTxEmptyII(i2eBordStrPtr pB, int mSdelay)
882{
883 unsigned long flags;
884 int itemp;
885
886 for (;;)
887 {
888 // This routine hinges on being able to see the "other" status register
889 // (as seen by the local processor). His incoming fifo is our outgoing
890 // FIFO.
891 //
892 // By the nature of this routine, you would be using this as part of a
893 // larger atomic context: i.e., you would use this routine to ensure the
894 // fifo empty, then act on this information. Between these two halves,
895 // you will generally not want to service interrupts or in any way
896 // disrupt the assumptions implicit in the larger context.
897 //
898 // Even worse, however, this routine "shifts" the status register to
899 // point to the local status register which is not the usual situation.
900 // Therefore for extra safety, we force the critical section to be
901 // completely atomic, and pick up after ourselves before allowing any
902 // interrupts of any kind.
903
904
905 write_lock_irqsave(&Dl_spinlock, flags);
906 outb(SEL_COMMAND, pB->i2ePointer);
907 outb(SEL_CMD_SH, pB->i2ePointer);
908
909 itemp = inb(pB->i2eStatus);
910
911 outb(SEL_COMMAND, pB->i2ePointer);
912 outb(SEL_CMD_UNSH, pB->i2ePointer);
913
914 if (itemp & ST_IN_EMPTY)
915 {
916 I2_UPDATE_FIFO_ROOM(pB);
917 write_unlock_irqrestore(&Dl_spinlock, flags);
918 I2_COMPLETE(pB, I2EE_GOOD);
919 }
920
921 write_unlock_irqrestore(&Dl_spinlock, flags);
922
923 if (mSdelay-- == 0)
924 break;
925
926 iiDelay(pB, 1); /* 1 mS granularity on checking condition */
927 }
928 I2_COMPLETE(pB, I2EE_TXE_TIME);
929}
930
931//******************************************************************************
932// Function: iiWaitForTxEmptyIIEX(pB, mSdelay)
933// Parameters: pB - pointer to board structure
934// mSdelay - period to wait before returning
935//
936// Returns: True if the FIFO is empty.
937// False if it not empty in the required time: the pB->i2eError
938// field has the error.
939//
940// Description:
941//
942// Waits up to "mSdelay" milliseconds for the outgoing FIFO to become empty; if
943// not empty by the required time, returns false and error in pB->i2eError,
944// otherwise returns true.
945//
946// mSdelay == 0 is taken to mean must be empty on the first test.
947//
948// This version operates on IntelliPort-IIEX - style FIFO's
949//
950// Note this routine is organized so that if status is ok there is no delay at
951// all called either before or after the test. Is called indirectly through
952// pB->i2eWaitForTxEmpty.
953//
954//******************************************************************************
955static int
956iiWaitForTxEmptyIIEX(i2eBordStrPtr pB, int mSdelay)
957{
958 unsigned long flags;
959
960 for (;;)
961 {
962 // By the nature of this routine, you would be using this as part of a
963 // larger atomic context: i.e., you would use this routine to ensure the
964 // fifo empty, then act on this information. Between these two halves,
965 // you will generally not want to service interrupts or in any way
966 // disrupt the assumptions implicit in the larger context.
967
968 write_lock_irqsave(&Dl_spinlock, flags);
969
970 if (inb(pB->i2eStatus) & STE_OUT_MT) {
971 I2_UPDATE_FIFO_ROOM(pB);
972 write_unlock_irqrestore(&Dl_spinlock, flags);
973 I2_COMPLETE(pB, I2EE_GOOD);
974 }
975 write_unlock_irqrestore(&Dl_spinlock, flags);
976
977 if (mSdelay-- == 0)
978 break;
979
980 iiDelay(pB, 1); // 1 mS granularity on checking condition
981 }
982 I2_COMPLETE(pB, I2EE_TXE_TIME);
983}
984
985//******************************************************************************
986// Function: iiTxMailEmptyII(pB)
987// Parameters: pB - pointer to board structure
988//
989// Returns: True if the transmit mailbox is empty.
990// False if it not empty.
991//
992// Description:
993//
994// Returns true or false according to whether the transmit mailbox is empty (and
995// therefore able to accept more mail)
996//
997// This version operates on IntelliPort-II - style FIFO's
998//
999//******************************************************************************
1000static int
1001iiTxMailEmptyII(i2eBordStrPtr pB)
1002{
1003 int port = pB->i2ePointer;
1004 outb(SEL_OUTMAIL, port);
1005 return inb(port) == 0;
1006}
1007
1008//******************************************************************************
1009// Function: iiTxMailEmptyIIEX(pB)
1010// Parameters: pB - pointer to board structure
1011//
1012// Returns: True if the transmit mailbox is empty.
1013// False if it not empty.
1014//
1015// Description:
1016//
1017// Returns true or false according to whether the transmit mailbox is empty (and
1018// therefore able to accept more mail)
1019//
1020// This version operates on IntelliPort-IIEX - style FIFO's
1021//
1022//******************************************************************************
1023static int
1024iiTxMailEmptyIIEX(i2eBordStrPtr pB)
1025{
1026 return !(inb(pB->i2eStatus) & STE_OUT_MAIL);
1027}
1028
1029//******************************************************************************
1030// Function: iiTrySendMailII(pB,mail)
1031// Parameters: pB - pointer to board structure
1032// mail - value to write to mailbox
1033//
1034// Returns: True if the transmit mailbox is empty, and mail is sent.
1035// False if it not empty.
1036//
1037// Description:
1038//
1039// If outgoing mailbox is empty, sends mail and returns true. If outgoing
1040// mailbox is not empty, returns false.
1041//
1042// This version operates on IntelliPort-II - style FIFO's
1043//
1044//******************************************************************************
1045static int
1046iiTrySendMailII(i2eBordStrPtr pB, unsigned char mail)
1047{
1048 int port = pB->i2ePointer;
1049
1050 outb(SEL_OUTMAIL, port);
1051 if (inb(port) == 0) {
1052 outb(SEL_OUTMAIL, port);
1053 outb(mail, port);
1054 return 1;
1055 }
1056 return 0;
1057}
1058
1059//******************************************************************************
1060// Function: iiTrySendMailIIEX(pB,mail)
1061// Parameters: pB - pointer to board structure
1062// mail - value to write to mailbox
1063//
1064// Returns: True if the transmit mailbox is empty, and mail is sent.
1065// False if it not empty.
1066//
1067// Description:
1068//
1069// If outgoing mailbox is empty, sends mail and returns true. If outgoing
1070// mailbox is not empty, returns false.
1071//
1072// This version operates on IntelliPort-IIEX - style FIFO's
1073//
1074//******************************************************************************
1075static int
1076iiTrySendMailIIEX(i2eBordStrPtr pB, unsigned char mail)
1077{
1078 if (inb(pB->i2eStatus) & STE_OUT_MAIL)
1079 return 0;
1080 outb(mail, pB->i2eXMail);
1081 return 1;
1082}
1083
1084//******************************************************************************
1085// Function: iiGetMailII(pB,mail)
1086// Parameters: pB - pointer to board structure
1087//
1088// Returns: Mailbox data or NO_MAIL_HERE.
1089//
1090// Description:
1091//
1092// If no mail available, returns NO_MAIL_HERE otherwise returns the data from
1093// the mailbox, which is guaranteed != NO_MAIL_HERE.
1094//
1095// This version operates on IntelliPort-II - style FIFO's
1096//
1097//******************************************************************************
1098static unsigned short
1099iiGetMailII(i2eBordStrPtr pB)
1100{
1101 if (I2_HAS_MAIL(pB)) {
1102 outb(SEL_INMAIL, pB->i2ePointer);
1103 return inb(pB->i2ePointer);
1104 } else {
1105 return NO_MAIL_HERE;
1106 }
1107}
1108
1109//******************************************************************************
1110// Function: iiGetMailIIEX(pB,mail)
1111// Parameters: pB - pointer to board structure
1112//
1113// Returns: Mailbox data or NO_MAIL_HERE.
1114//
1115// Description:
1116//
1117// If no mail available, returns NO_MAIL_HERE otherwise returns the data from
1118// the mailbox, which is guaranteed != NO_MAIL_HERE.
1119//
1120// This version operates on IntelliPort-IIEX - style FIFO's
1121//
1122//******************************************************************************
1123static unsigned short
1124iiGetMailIIEX(i2eBordStrPtr pB)
1125{
1126 if (I2_HAS_MAIL(pB))
1127 return inb(pB->i2eXMail);
1128 else
1129 return NO_MAIL_HERE;
1130}
1131
1132//******************************************************************************
1133// Function: iiEnableMailIrqII(pB)
1134// Parameters: pB - pointer to board structure
1135//
1136// Returns: Nothing
1137//
1138// Description:
1139//
1140// Enables board to interrupt host (only) by writing to host's in-bound mailbox.
1141//
1142// This version operates on IntelliPort-II - style FIFO's
1143//
1144//******************************************************************************
1145static void
1146iiEnableMailIrqII(i2eBordStrPtr pB)
1147{
1148 outb(SEL_MASK, pB->i2ePointer);
1149 outb(ST_IN_MAIL, pB->i2ePointer);
1150}
1151
1152//******************************************************************************
1153// Function: iiEnableMailIrqIIEX(pB)
1154// Parameters: pB - pointer to board structure
1155//
1156// Returns: Nothing
1157//
1158// Description:
1159//
1160// Enables board to interrupt host (only) by writing to host's in-bound mailbox.
1161//
1162// This version operates on IntelliPort-IIEX - style FIFO's
1163//
1164//******************************************************************************
1165static void
1166iiEnableMailIrqIIEX(i2eBordStrPtr pB)
1167{
1168 outb(MX_IN_MAIL, pB->i2eXMask);
1169}
1170
1171//******************************************************************************
1172// Function: iiWriteMaskII(pB)
1173// Parameters: pB - pointer to board structure
1174//
1175// Returns: Nothing
1176//
1177// Description:
1178//
1179// Writes arbitrary value to the mask register.
1180//
1181// This version operates on IntelliPort-II - style FIFO's
1182//
1183//******************************************************************************
1184static void
1185iiWriteMaskII(i2eBordStrPtr pB, unsigned char value)
1186{
1187 outb(SEL_MASK, pB->i2ePointer);
1188 outb(value, pB->i2ePointer);
1189}
1190
1191//******************************************************************************
1192// Function: iiWriteMaskIIEX(pB)
1193// Parameters: pB - pointer to board structure
1194//
1195// Returns: Nothing
1196//
1197// Description:
1198//
1199// Writes arbitrary value to the mask register.
1200//
1201// This version operates on IntelliPort-IIEX - style FIFO's
1202//
1203//******************************************************************************
1204static void
1205iiWriteMaskIIEX(i2eBordStrPtr pB, unsigned char value)
1206{
1207 outb(value, pB->i2eXMask);
1208}
1209
1210//******************************************************************************
1211// Function: iiDownloadBlock(pB, pSource, isStandard)
1212// Parameters: pB - pointer to board structure
1213// pSource - loadware block to download
1214// isStandard - True if "standard" loadware, else false.
1215//
1216// Returns: Success or Failure
1217//
1218// Description:
1219//
1220// Downloads a single block (at pSource)to the board referenced by pB. Caller
1221// sets isStandard to true/false according to whether the "standard" loadware is
1222// what's being loaded. The normal process, then, is to perform an iiInitialize
1223// to the board, then perform some number of iiDownloadBlocks using the returned
1224// state to determine when download is complete.
1225//
1226// Possible return values: (see I2ELLIS.H)
1227// II_DOWN_BADVALID
1228// II_DOWN_BADFILE
1229// II_DOWN_CONTINUING
1230// II_DOWN_GOOD
1231// II_DOWN_BAD
1232// II_DOWN_BADSTATE
1233// II_DOWN_TIMEOUT
1234//
1235// Uses the i2eState and i2eToLoad fields (initialized at iiInitialize) to
1236// determine whether this is the first block, whether to check for magic
1237// numbers, how many blocks there are to go...
1238//
1239//******************************************************************************
1240static int
1241iiDownloadBlock ( i2eBordStrPtr pB, loadHdrStrPtr pSource, int isStandard)
1242{
1243 int itemp;
1244 int loadedFirst;
1245
1246 if (pB->i2eValid != I2E_MAGIC) return II_DOWN_BADVALID;
1247
1248 switch(pB->i2eState)
1249 {
1250 case II_STATE_READY:
1251
1252 // Loading the first block after reset. Must check the magic number of the
1253 // loadfile, store the number of blocks we expect to load.
1254 if (pSource->e.loadMagic != MAGIC_LOADFILE)
1255 {
1256 return II_DOWN_BADFILE;
1257 }
1258
1259 // Next we store the total number of blocks to load, including this one.
1260 pB->i2eToLoad = 1 + pSource->e.loadBlocksMore;
1261
1262 // Set the state, store the version numbers. ('Cause this may have come
1263 // from a file - we might want to report these versions and revisions in
1264 // case of an error!
1265 pB->i2eState = II_STATE_LOADING;
1266 pB->i2eLVersion = pSource->e.loadVersion;
1267 pB->i2eLRevision = pSource->e.loadRevision;
1268 pB->i2eLSub = pSource->e.loadSubRevision;
1269
1270 // The time and date of compilation is also available but don't bother
1271 // storing it for normal purposes.
1272 loadedFirst = 1;
1273 break;
1274
1275 case II_STATE_LOADING:
1276 loadedFirst = 0;
1277 break;
1278
1279 default:
1280 return II_DOWN_BADSTATE;
1281 }
1282
1283 // Now we must be in the II_STATE_LOADING state, and we assume i2eToLoad
1284 // must be positive still, because otherwise we would have cleaned up last
1285 // time and set the state to II_STATE_LOADED.
1286 if (!iiWaitForTxEmpty(pB, MAX_DLOAD_READ_TIME)) {
1287 return II_DOWN_TIMEOUT;
1288 }
1289
1290 if (!iiWriteBuf(pB, pSource->c, LOADWARE_BLOCK_SIZE)) {
1291 return II_DOWN_BADVALID;
1292 }
1293
1294 // If we just loaded the first block, wait for the fifo to empty an extra
1295 // long time to allow for any special startup code in the firmware, like
1296 // sending status messages to the LCD's.
1297
1298 if (loadedFirst) {
1299 if (!iiWaitForTxEmpty(pB, MAX_DLOAD_START_TIME)) {
1300 return II_DOWN_TIMEOUT;
1301 }
1302 }
1303
1304 // Determine whether this was our last block!
1305 if (--(pB->i2eToLoad)) {
1306 return II_DOWN_CONTINUING; // more to come...
1307 }
1308
1309 // It WAS our last block: Clean up operations...
1310 // ...Wait for last buffer to drain from the board...
1311 if (!iiWaitForTxEmpty(pB, MAX_DLOAD_READ_TIME)) {
1312 return II_DOWN_TIMEOUT;
1313 }
1314 // If there were only a single block written, this would come back
1315 // immediately and be harmless, though not strictly necessary.
1316 itemp = MAX_DLOAD_ACK_TIME/10;
1317 while (--itemp) {
1318 if (I2_HAS_INPUT(pB)) {
1319 switch (inb(pB->i2eData)) {
1320 case LOADWARE_OK:
1321 pB->i2eState =
1322 isStandard ? II_STATE_STDLOADED :II_STATE_LOADED;
1323
1324 // Some revisions of the bootstrap firmware (e.g. ISA-8 1.0.2)
1325 // will, // if there is a debug port attached, require some
1326 // time to send information to the debug port now. It will do
1327 // this before // executing any of the code we just downloaded.
1328 // It may take up to 700 milliseconds.
1329 if (pB->i2ePom.e.porDiag2 & POR_DEBUG_PORT) {
1330 iiDelay(pB, 700);
1331 }
1332
1333 return II_DOWN_GOOD;
1334
1335 case LOADWARE_BAD:
1336 default:
1337 return II_DOWN_BAD;
1338 }
1339 }
1340
1341 iiDelay(pB, 10); // 10 mS granularity on checking condition
1342 }
1343
1344 // Drop-through --> timed out waiting for firmware confirmation
1345
1346 pB->i2eState = II_STATE_BADLOAD;
1347 return II_DOWN_TIMEOUT;
1348}
1349
1350//******************************************************************************
1351// Function: iiDownloadAll(pB, pSource, isStandard, size)
1352// Parameters: pB - pointer to board structure
1353// pSource - loadware block to download
1354// isStandard - True if "standard" loadware, else false.
1355// size - size of data to download (in bytes)
1356//
1357// Returns: Success or Failure
1358//
1359// Description:
1360//
1361// Given a pointer to a board structure, a pointer to the beginning of some
1362// loadware, whether it is considered the "standard loadware", and the size of
1363// the array in bytes loads the entire array to the board as loadware.
1364//
1365// Assumes the board has been freshly reset and the power-up reset message read.
1366// (i.e., in II_STATE_READY). Complains if state is bad, or if there seems to be
1367// too much or too little data to load, or if iiDownloadBlock complains.
1368//******************************************************************************
1369static int
1370iiDownloadAll(i2eBordStrPtr pB, loadHdrStrPtr pSource, int isStandard, int size)
1371{
1372 int status;
1373
1374 // We know (from context) board should be ready for the first block of
1375 // download. Complain if not.
1376 if (pB->i2eState != II_STATE_READY) return II_DOWN_BADSTATE;
1377
1378 while (size > 0) {
1379 size -= LOADWARE_BLOCK_SIZE; // How much data should there be left to
1380 // load after the following operation ?
1381
1382 // Note we just bump pSource by "one", because its size is actually that
1383 // of an entire block, same as LOADWARE_BLOCK_SIZE.
1384 status = iiDownloadBlock(pB, pSource++, isStandard);
1385
1386 switch(status)
1387 {
1388 case II_DOWN_GOOD:
1389 return ( (size > 0) ? II_DOWN_OVER : II_DOWN_GOOD);
1390
1391 case II_DOWN_CONTINUING:
1392 break;
1393
1394 default:
1395 return status;
1396 }
1397 }
1398
1399 // We shouldn't drop out: it means "while" caught us with nothing left to
1400 // download, yet the previous DownloadBlock did not return complete. Ergo,
1401 // not enough data to match the size byte in the header.
1402 return II_DOWN_UNDER;
1403}
diff --git a/drivers/char/ip2/i2ellis.h b/drivers/char/ip2/i2ellis.h
deleted file mode 100644
index fb6df2456018..000000000000
--- a/drivers/char/ip2/i2ellis.h
+++ /dev/null
@@ -1,566 +0,0 @@
1/*******************************************************************************
2*
3* (c) 1999 by Computone Corporation
4*
5********************************************************************************
6*
7*
8* PACKAGE: Linux tty Device Driver for IntelliPort II family of multiport
9* serial I/O controllers.
10*
11* DESCRIPTION: Mainline code for the device driver
12*
13*******************************************************************************/
14//------------------------------------------------------------------------------
15// i2ellis.h
16//
17// IntelliPort-II and IntelliPort-IIEX
18//
19// Extremely
20// Low
21// Level
22// Interface
23// Services
24//
25// Structure Definitions and declarations for "ELLIS" service routines found in
26// i2ellis.c
27//
28// These routines are based on properties of the IntelliPort-II and -IIEX
29// hardware and bootstrap firmware, and are not sensitive to particular
30// conventions of any particular loadware.
31//
32// Unlike i2hw.h, which provides IRONCLAD hardware definitions, the material
33// here and in i2ellis.c is intended to provice a useful, but not required,
34// layer of insulation from the hardware specifics.
35//------------------------------------------------------------------------------
36#ifndef I2ELLIS_H /* To prevent multiple includes */
37#define I2ELLIS_H 1
38//------------------------------------------------
39// Revision History:
40//
41// 30 September 1991 MAG First Draft Started
42// 12 October 1991 ...continued...
43//
44// 20 December 1996 AKM Linux version
45//-------------------------------------------------
46
47//----------------------
48// Mandatory Includes:
49//----------------------
50#include "ip2types.h"
51#include "i2hw.h" // The hardware definitions
52
53//------------------------------------------
54// STAT_BOXIDS packets
55//------------------------------------------
56#define MAX_BOX 4
57
58typedef struct _bidStat
59{
60 unsigned char bid_value[MAX_BOX];
61} bidStat, *bidStatPtr;
62
63// This packet is sent in response to a CMD_GET_BOXIDS bypass command. For -IIEX
64// boards, reports the hardware-specific "asynchronous resource register" on
65// each expansion box. Boxes not present report 0xff. For -II boards, the first
66// element contains 0x80 for 8-port, 0x40 for 4-port boards.
67
68// Box IDs aka ARR or Async Resource Register (more than you want to know)
69// 7 6 5 4 3 2 1 0
70// F F N N L S S S
71// =============================
72// F F - Product Family Designator
73// =====+++++++++++++++++++++++++++++++
74// 0 0 - Intelliport II EX / ISA-8
75// 1 0 - IntelliServer
76// 0 1 - SAC - Port Device (Intelliport III ??? )
77// =====+++++++++++++++++++++++++++++++++++++++
78// N N - Number of Ports
79// 0 0 - 8 (eight)
80// 0 1 - 4 (four)
81// 1 0 - 12 (twelve)
82// 1 1 - 16 (sixteen)
83// =++++++++++++++++++++++++++++++++++
84// L - LCD Display Module Present
85// 0 - No
86// 1 - LCD module present
87// =========+++++++++++++++++++++++++++++++++++++
88// S S S - Async Signals Supported Designator
89// 0 0 0 - 8dss, Mod DCE DB25 Female
90// 0 0 1 - 6dss, RJ-45
91// 0 1 0 - RS-232/422 dss, DB25 Female
92// 0 1 1 - RS-232/422 dss, separate 232/422 DB25 Female
93// 1 0 0 - 6dss, 921.6 I/F with ST654's
94// 1 0 1 - RS-423/232 8dss, RJ-45 10Pin
95// 1 1 0 - 6dss, Mod DCE DB25 Female
96// 1 1 1 - NO BOX PRESENT
97
98#define FF(c) ((c & 0xC0) >> 6)
99#define NN(c) ((c & 0x30) >> 4)
100#define L(c) ((c & 0x08) >> 3)
101#define SSS(c) (c & 0x07)
102
103#define BID_HAS_654(x) (SSS(x) == 0x04)
104#define BID_NO_BOX 0xff /* no box */
105#define BID_8PORT 0x80 /* IP2-8 port */
106#define BID_4PORT 0x81 /* IP2-4 port */
107#define BID_EXP_MASK 0x30 /* IP2-EX */
108#define BID_EXP_8PORT 0x00 /* 8, */
109#define BID_EXP_4PORT 0x10 /* 4, */
110#define BID_EXP_UNDEF 0x20 /* UNDEF, */
111#define BID_EXP_16PORT 0x30 /* 16, */
112#define BID_LCD_CTRL 0x08 /* LCD Controller */
113#define BID_LCD_NONE 0x00 /* - no controller present */
114#define BID_LCD_PRES 0x08 /* - controller present */
115#define BID_CON_MASK 0x07 /* - connector pinouts */
116#define BID_CON_DB25 0x00 /* - DB-25 F */
117#define BID_CON_RJ45 0x01 /* - rj45 */
118
119//------------------------------------------------------------------------------
120// i2eBordStr
121//
122// This structure contains all the information the ELLIS routines require in
123// dealing with a particular board.
124//------------------------------------------------------------------------------
125// There are some queues here which are guaranteed to never contain the entry
126// for a single channel twice. So they must be slightly larger to allow
127// unambiguous full/empty management
128//
129#define CH_QUEUE_SIZE ABS_MOST_PORTS+2
130
131typedef struct _i2eBordStr
132{
133 porStr i2ePom; // Structure containing the power-on message.
134
135 unsigned short i2ePomSize;
136 // The number of bytes actually read if
137 // different from sizeof i2ePom, indicates
138 // there is an error!
139
140 unsigned short i2eStartMail;
141 // Contains whatever inbound mailbox data
142 // present at startup. NO_MAIL_HERE indicates
143 // nothing was present. No special
144 // significance as of this writing, but may be
145 // useful for diagnostic reasons.
146
147 unsigned short i2eValid;
148 // Indicates validity of the structure; if
149 // i2eValid == I2E_MAGIC, then we can trust
150 // the other fields. Some (especially
151 // initialization) functions are good about
152 // checking for validity. Many functions do
153 // not, it being assumed that the larger
154 // context assures we are using a valid
155 // i2eBordStrPtr.
156
157 unsigned short i2eError;
158 // Used for returning an error condition from
159 // several functions which use i2eBordStrPtr
160 // as an argument.
161
162 // Accelerators to characterize separate features of a board, derived from a
163 // number of sources.
164
165 unsigned short i2eFifoSize;
166 // Always, the size of the FIFO. For
167 // IntelliPort-II, always the same, for -IIEX
168 // taken from the Power-On reset message.
169
170 volatile
171 unsigned short i2eFifoRemains;
172 // Used during normal operation to indicate a
173 // lower bound on the amount of data which
174 // might be in the outbound fifo.
175
176 unsigned char i2eFifoStyle;
177 // Accelerator which tells which style (-II or
178 // -IIEX) FIFO we are using.
179
180 unsigned char i2eDataWidth16;
181 // Accelerator which tells whether we should
182 // do 8 or 16-bit data transfers.
183
184 unsigned char i2eMaxIrq;
185 // The highest allowable IRQ, based on the
186 // slot size.
187
188 // Accelerators for various addresses on the board
189 int i2eBase; // I/O Address of the Board
190 int i2eData; // From here data transfers happen
191 int i2eStatus; // From here status reads happen
192 int i2ePointer; // (IntelliPort-II: pointer/commands)
193 int i2eXMail; // (IntelliPOrt-IIEX: mailboxes
194 int i2eXMask; // (IntelliPort-IIEX: mask write
195
196 //-------------------------------------------------------
197 // Information presented in a common format across boards
198 // For each box, bit map of the channels present. Box closest to
199 // the host is box 0. LSB is channel 0. IntelliPort-II (non-expandable)
200 // is taken to be box 0. These are derived from product i.d. registers.
201
202 unsigned short i2eChannelMap[ABS_MAX_BOXES];
203
204 // Same as above, except each is derived from firmware attempting to detect
205 // the uart presence (by reading a valid GFRCR register). If bits are set in
206 // i2eChannelMap and not in i2eGoodMap, there is a potential problem.
207
208 unsigned short i2eGoodMap[ABS_MAX_BOXES];
209
210 // ---------------------------
211 // For indirect function calls
212
213 // Routine to cause an N-millisecond delay: Patched by the ii2Initialize
214 // function.
215
216 void (*i2eDelay)(unsigned int);
217
218 // Routine to write N bytes to the board through the FIFO. Returns true if
219 // all copacetic, otherwise returns false and error is in i2eError field.
220 // IF COUNT IS ODD, ROUNDS UP TO THE NEXT EVEN NUMBER.
221
222 int (*i2eWriteBuf)(struct _i2eBordStr *, unsigned char *, int);
223
224 // Routine to read N bytes from the board through the FIFO. Returns true if
225 // copacetic, otherwise returns false and error in i2eError.
226 // IF COUNT IS ODD, ROUNDS UP TO THE NEXT EVEN NUMBER.
227
228 int (*i2eReadBuf)(struct _i2eBordStr *, unsigned char *, int);
229
230 // Returns a word from FIFO. Will use 2 byte operations if needed.
231
232 unsigned short (*i2eReadWord)(struct _i2eBordStr *);
233
234 // Writes a word to FIFO. Will use 2 byte operations if needed.
235
236 void (*i2eWriteWord)(struct _i2eBordStr *, unsigned short);
237
238 // Waits specified time for the Transmit FIFO to go empty. Returns true if
239 // ok, otherwise returns false and error in i2eError.
240
241 int (*i2eWaitForTxEmpty)(struct _i2eBordStr *, int);
242
243 // Returns true or false according to whether the outgoing mailbox is empty.
244
245 int (*i2eTxMailEmpty)(struct _i2eBordStr *);
246
247 // Checks whether outgoing mailbox is empty. If so, sends mail and returns
248 // true. Otherwise returns false.
249
250 int (*i2eTrySendMail)(struct _i2eBordStr *, unsigned char);
251
252 // If no mail available, returns NO_MAIL_HERE, else returns the value in the
253 // mailbox (guaranteed can't be NO_MAIL_HERE).
254
255 unsigned short (*i2eGetMail)(struct _i2eBordStr *);
256
257 // Enables the board to interrupt the host when it writes to the mailbox.
258 // Irqs will not occur, however, until the loadware separately enables
259 // interrupt generation to the host. The standard loadware does this in
260 // response to a command packet sent by the host. (Also, disables
261 // any other potential interrupt sources from the board -- other than the
262 // inbound mailbox).
263
264 void (*i2eEnableMailIrq)(struct _i2eBordStr *);
265
266 // Writes an arbitrary value to the mask register.
267
268 void (*i2eWriteMask)(struct _i2eBordStr *, unsigned char);
269
270
271 // State information
272
273 // During downloading, indicates the number of blocks remaining to download
274 // to the board.
275
276 short i2eToLoad;
277
278 // State of board (see manifests below) (e.g., whether in reset condition,
279 // whether standard loadware is installed, etc.
280
281 unsigned char i2eState;
282
283 // These three fields are only valid when there is loadware running on the
284 // board. (i2eState == II_STATE_LOADED or i2eState == II_STATE_STDLOADED )
285
286 unsigned char i2eLVersion; // Loadware version
287 unsigned char i2eLRevision; // Loadware revision
288 unsigned char i2eLSub; // Loadware subrevision
289
290 // Flags which only have meaning in the context of the standard loadware.
291 // Somewhat violates the layering concept, but there is so little additional
292 // needed at the board level (while much additional at the channel level),
293 // that this beats maintaining two different per-board structures.
294
295 // Indicates which IRQ the board has been initialized (from software) to use
296 // For MicroChannel boards, any value different from IRQ_UNDEFINED means
297 // that the software command has been sent to enable interrupts (or specify
298 // they are disabled). Special value: IRQ_UNDEFINED indicates that the
299 // software command to select the interrupt has not yet been sent, therefore
300 // (since the standard loadware insists that it be sent before any other
301 // packets are sent) no other packets should be sent yet.
302
303 unsigned short i2eUsingIrq;
304
305 // This is set when we hit the MB_OUT_STUFFED mailbox, which prevents us
306 // putting more in the mailbox until an appropriate mailbox message is
307 // received.
308
309 unsigned char i2eWaitingForEmptyFifo;
310
311 // Any mailbox bits waiting to be sent to the board are OR'ed in here.
312
313 unsigned char i2eOutMailWaiting;
314
315 // The head of any incoming packet is read into here, is then examined and
316 // we dispatch accordingly.
317
318 unsigned short i2eLeadoffWord[1];
319
320 // Running counter of interrupts where the mailbox indicated incoming data.
321
322 unsigned short i2eFifoInInts;
323
324 // Running counter of interrupts where the mailbox indicated outgoing data
325 // had been stripped.
326
327 unsigned short i2eFifoOutInts;
328
329 // If not void, gives the address of a routine to call if fatal board error
330 // is found (only applies to standard l/w).
331
332 void (*i2eFatalTrap)(struct _i2eBordStr *);
333
334 // Will point to an array of some sort of channel structures (whose format
335 // is unknown at this level, being a function of what loadware is
336 // installed and the code configuration (max sizes of buffers, etc.)).
337
338 void *i2eChannelPtr;
339
340 // Set indicates that the board has gone fatal.
341
342 unsigned short i2eFatal;
343
344 // The number of elements pointed to by i2eChannelPtr.
345
346 unsigned short i2eChannelCnt;
347
348 // Ring-buffers of channel structures whose channels have particular needs.
349
350 rwlock_t Fbuf_spinlock;
351 volatile
352 unsigned short i2Fbuf_strip; // Strip index
353 volatile
354 unsigned short i2Fbuf_stuff; // Stuff index
355 void *i2Fbuf[CH_QUEUE_SIZE]; // An array of channel pointers
356 // of channels who need to send
357 // flow control packets.
358 rwlock_t Dbuf_spinlock;
359 volatile
360 unsigned short i2Dbuf_strip; // Strip index
361 volatile
362 unsigned short i2Dbuf_stuff; // Stuff index
363 void *i2Dbuf[CH_QUEUE_SIZE]; // An array of channel pointers
364 // of channels who need to send
365 // data or in-line command packets.
366 rwlock_t Bbuf_spinlock;
367 volatile
368 unsigned short i2Bbuf_strip; // Strip index
369 volatile
370 unsigned short i2Bbuf_stuff; // Stuff index
371 void *i2Bbuf[CH_QUEUE_SIZE]; // An array of channel pointers
372 // of channels who need to send
373 // bypass command packets.
374
375 /*
376 * A set of flags to indicate that certain events have occurred on at least
377 * one of the ports on this board. We use this to decide whether to spin
378 * through the channels looking for breaks, etc.
379 */
380 int got_input;
381 int status_change;
382 bidStat channelBtypes;
383
384 /*
385 * Debugging counters, etc.
386 */
387 unsigned long debugFlowQueued;
388 unsigned long debugInlineQueued;
389 unsigned long debugDataQueued;
390 unsigned long debugBypassQueued;
391 unsigned long debugFlowCount;
392 unsigned long debugInlineCount;
393 unsigned long debugBypassCount;
394
395 rwlock_t read_fifo_spinlock;
396 rwlock_t write_fifo_spinlock;
397
398// For queuing interrupt bottom half handlers. /\/\|=mhw=|\/\/
399 struct work_struct tqueue_interrupt;
400
401 struct timer_list SendPendingTimer; // Used by iiSendPending
402 unsigned int SendPendingRetry;
403} i2eBordStr, *i2eBordStrPtr;
404
405//-------------------------------------------------------------------
406// Macro Definitions for the indirect calls defined in the i2eBordStr
407//-------------------------------------------------------------------
408//
409#define iiDelay(a,b) (*(a)->i2eDelay)(b)
410#define iiWriteBuf(a,b,c) (*(a)->i2eWriteBuf)(a,b,c)
411#define iiReadBuf(a,b,c) (*(a)->i2eReadBuf)(a,b,c)
412
413#define iiWriteWord(a,b) (*(a)->i2eWriteWord)(a,b)
414#define iiReadWord(a) (*(a)->i2eReadWord)(a)
415
416#define iiWaitForTxEmpty(a,b) (*(a)->i2eWaitForTxEmpty)(a,b)
417
418#define iiTxMailEmpty(a) (*(a)->i2eTxMailEmpty)(a)
419#define iiTrySendMail(a,b) (*(a)->i2eTrySendMail)(a,b)
420
421#define iiGetMail(a) (*(a)->i2eGetMail)(a)
422#define iiEnableMailIrq(a) (*(a)->i2eEnableMailIrq)(a)
423#define iiDisableMailIrq(a) (*(a)->i2eWriteMask)(a,0)
424#define iiWriteMask(a,b) (*(a)->i2eWriteMask)(a,b)
425
426//-------------------------------------------
427// Manifests for i2eBordStr:
428//-------------------------------------------
429
430typedef void (*delayFunc_t)(unsigned int);
431
432// i2eValid
433//
434#define I2E_MAGIC 0x4251 // Structure is valid.
435#define I2E_INCOMPLETE 0x1122 // Structure failed during init.
436
437
438// i2eError
439//
440#define I2EE_GOOD 0 // Operation successful
441#define I2EE_BADADDR 1 // Address out of range
442#define I2EE_BADSTATE 2 // Attempt to perform a function when the board
443 // structure was in the incorrect state
444#define I2EE_BADMAGIC 3 // Bad magic number from Power On test (i2ePomSize
445 // reflects what was read
446#define I2EE_PORM_SHORT 4 // Power On message too short
447#define I2EE_PORM_LONG 5 // Power On message too long
448#define I2EE_BAD_FAMILY 6 // Un-supported board family type
449#define I2EE_INCONSIST 7 // Firmware reports something impossible,
450 // e.g. unexpected number of ports... Almost no
451 // excuse other than bad FIFO...
452#define I2EE_POSTERR 8 // Power-On self test reported a bad error
453#define I2EE_BADBUS 9 // Unknown Bus type declared in message
454#define I2EE_TXE_TIME 10 // Timed out waiting for TX Fifo to empty
455#define I2EE_INVALID 11 // i2eValid field does not indicate a valid and
456 // complete board structure (for functions which
457 // require this be so.)
458#define I2EE_BAD_PORT 12 // Discrepancy between channels actually found and
459 // what the product is supposed to have. Check
460 // i2eGoodMap vs i2eChannelMap for details.
461#define I2EE_BAD_IRQ 13 // Someone specified an unsupported IRQ
462#define I2EE_NOCHANNELS 14 // No channel structures have been defined (for
463 // functions requiring this).
464
465// i2eFifoStyle
466//
467#define FIFO_II 0 /* IntelliPort-II style: see also i2hw.h */
468#define FIFO_IIEX 1 /* IntelliPort-IIEX style */
469
470// i2eGetMail
471//
472#define NO_MAIL_HERE 0x1111 // Since mail is unsigned char, cannot possibly
473 // promote to 0x1111.
474// i2eState
475//
476#define II_STATE_COLD 0 // Addresses have been defined, but board not even
477 // reset yet.
478#define II_STATE_RESET 1 // Board,if it exists, has just been reset
479#define II_STATE_READY 2 // Board ready for its first block
480#define II_STATE_LOADING 3 // Board continuing load
481#define II_STATE_LOADED 4 // Board has finished load: status ok
482#define II_STATE_BADLOAD 5 // Board has finished load: failed!
483#define II_STATE_STDLOADED 6 // Board has finished load: standard firmware
484
485// i2eUsingIrq
486//
487#define I2_IRQ_UNDEFINED 0x1352 /* No valid irq (or polling = 0) can
488 * ever promote to this! */
489//------------------------------------------
490// Handy Macros for i2ellis.c and others
491// Note these are common to -II and -IIEX
492//------------------------------------------
493
494// Given a pointer to the board structure, does the input FIFO have any data or
495// not?
496//
497#define I2_HAS_INPUT(pB) !(inb(pB->i2eStatus) & ST_IN_EMPTY)
498
499// Given a pointer to the board structure, is there anything in the incoming
500// mailbox?
501//
502#define I2_HAS_MAIL(pB) (inb(pB->i2eStatus) & ST_IN_MAIL)
503
504#define I2_UPDATE_FIFO_ROOM(pB) ((pB)->i2eFifoRemains = (pB)->i2eFifoSize)
505
506//------------------------------------------
507// Function Declarations for i2ellis.c
508//------------------------------------------
509//
510// Functions called directly
511//
512// Initialization of a board & structure is in four (five!) parts:
513//
514// 1) iiSetAddress() - Define the board address & delay function for a board.
515// 2) iiReset() - Reset the board (provided it exists)
516// -- Note you may do this to several boards --
517// 3) iiResetDelay() - Delay for 2 seconds (once for all boards)
518// 4) iiInitialize() - Attempt to read Power-up message; further initialize
519// accelerators
520//
521// Then you may use iiDownloadAll() or iiDownloadFile() (in i2file.c) to write
522// loadware. To change loadware, you must begin again with step 2, resetting
523// the board again (step 1 not needed).
524
525static int iiSetAddress(i2eBordStrPtr, int, delayFunc_t );
526static int iiReset(i2eBordStrPtr);
527static int iiResetDelay(i2eBordStrPtr);
528static int iiInitialize(i2eBordStrPtr);
529
530// Routine to validate that all channels expected are there.
531//
532extern int iiValidateChannels(i2eBordStrPtr);
533
534// Routine used to download a block of loadware.
535//
536static int iiDownloadBlock(i2eBordStrPtr, loadHdrStrPtr, int);
537
538// Return values given by iiDownloadBlock, iiDownloadAll, iiDownloadFile:
539//
540#define II_DOWN_BADVALID 0 // board structure is invalid
541#define II_DOWN_CONTINUING 1 // So far, so good, firmware expects more
542#define II_DOWN_GOOD 2 // Download complete, CRC good
543#define II_DOWN_BAD 3 // Download complete, but CRC bad
544#define II_DOWN_BADFILE 4 // Bad magic number in loadware file
545#define II_DOWN_BADSTATE 5 // Board is in an inappropriate state for
546 // downloading loadware. (see i2eState)
547#define II_DOWN_TIMEOUT 6 // Timeout waiting for firmware
548#define II_DOWN_OVER 7 // Too much data
549#define II_DOWN_UNDER 8 // Not enough data
550#define II_DOWN_NOFILE 9 // Loadware file not found
551
552// Routine to download an entire loadware module: Return values are a subset of
553// iiDownloadBlock's, excluding, of course, II_DOWN_CONTINUING
554//
555static int iiDownloadAll(i2eBordStrPtr, loadHdrStrPtr, int, int);
556
557// Many functions defined here return True if good, False otherwise, with an
558// error code in i2eError field. Here is a handy macro for setting the error
559// code and returning.
560//
561#define I2_COMPLETE(pB,code) do { \
562 pB->i2eError = code; \
563 return (code == I2EE_GOOD);\
564 } while (0)
565
566#endif // I2ELLIS_H
diff --git a/drivers/char/ip2/i2hw.h b/drivers/char/ip2/i2hw.h
deleted file mode 100644
index c0ba6c05f0cd..000000000000
--- a/drivers/char/ip2/i2hw.h
+++ /dev/null
@@ -1,652 +0,0 @@
1/*******************************************************************************
2*
3* (c) 1999 by Computone Corporation
4*
5********************************************************************************
6*
7*
8* PACKAGE: Linux tty Device Driver for IntelliPort II family of multiport
9* serial I/O controllers.
10*
11* DESCRIPTION: Definitions limited to properties of the hardware or the
12* bootstrap firmware. As such, they are applicable regardless of
13* operating system or loadware (standard or diagnostic).
14*
15*******************************************************************************/
16#ifndef I2HW_H
17#define I2HW_H 1
18//------------------------------------------------------------------------------
19// Revision History:
20//
21// 23 September 1991 MAG First Draft Started...through...
22// 11 October 1991 ... Continuing development...
23// 6 August 1993 Added support for ISA-4 (asic) which is architected
24// as an ISA-CEX with a single 4-port box.
25//
26// 20 December 1996 AKM Version for Linux
27//
28//------------------------------------------------------------------------------
29/*------------------------------------------------------------------------------
30
31HARDWARE DESCRIPTION:
32
33Introduction:
34
35The IntelliPort-II and IntelliPort-IIEX products occupy a block of eight (8)
36addresses in the host's I/O space.
37
38Some addresses are used to transfer data to/from the board, some to transfer
39so-called "mailbox" messages, and some to read bit-mapped status information.
40While all the products in the line are functionally similar, some use a 16-bit
41data path to transfer data while others use an 8-bit path. Also, the use of
42command /status/mailbox registers differs slightly between the II and IIEX
43branches of the family.
44
45The host determines what type of board it is dealing with by reading a string of
46sixteen characters from the board. These characters are always placed in the
47fifo by the board's local processor whenever the board is reset (either from
48power-on or under software control) and are known as the "Power-on Reset
49Message." In order that this message can be read from either type of board, the
50hardware registers used in reading this message are the same. Once this message
51has been read by the host, then it has the information required to operate.
52
53General Differences between boards:
54
55The greatest structural difference is between the -II and -IIEX families of
56product. The -II boards use the Am4701 dual 512x8 bidirectional fifo to support
57the data path, mailbox registers, and status registers. This chip contains some
58features which are not used in the IntelliPort-II products; a description of
59these is omitted here. Because of these many features, it contains many
60registers, too many to access directly within a small address space. They are
61accessed by first writing a value to a "pointer" register. This value selects
62the register to be accessed. The next read or write to that address accesses
63the selected register rather than the pointer register.
64
65The -IIEX boards use a proprietary design similar to the Am4701 in function. But
66because of a simpler, more streamlined design it doesn't require so many
67registers. This means they can be accessed directly in single operations rather
68than through a pointer register.
69
70Besides these differences, there are differences in whether 8-bit or 16-bit
71transfers are used to move data to the board.
72
73The -II boards are capable only of 8-bit data transfers, while the -IIEX boards
74may be configured for either 8-bit or 16-bit data transfers. If the on-board DIP
75switch #8 is ON, and the card has been installed in a 16-bit slot, 16-bit
76transfers are supported (and will be expected by the standard loadware). The
77on-board firmware can determine the position of the switch, and whether the
78board is installed in a 16-bit slot; it supplies this information to the host as
79part of the power-up reset message.
80
81The configuration switch (#8) and slot selection do not directly configure the
82hardware. It is up to the on-board loadware and host-based drivers to act
83according to the selected options. That is, loadware and drivers could be
84written to perform 8-bit transfers regardless of the state of the DIP switch or
85slot (and in a diagnostic environment might well do so). Likewise, 16-bit
86transfers could be performed as long as the card is in a 16-bit slot.
87
88Note the slot selection and DIP switch selection are provided separately: a
89board running in 8-bit mode in a 16-bit slot has a greater range of possible
90interrupts to choose from; information of potential use to the host.
91
92All 8-bit data transfers are done in the same way, regardless of whether on a
93-II board or a -IIEX board.
94
95The host must consider two things then: 1) whether a -II or -IIEX product is
96being used, and 2) whether an 8-bit or 16-bit data path is used.
97
98A further difference is that -II boards always have a 512-byte fifo operating in
99each direction. -IIEX boards may use fifos of varying size; this size is
100reported as part of the power-up message.
101
102I/O Map Of IntelliPort-II and IntelliPort-IIEX boards:
103(Relative to the chosen base address)
104
105Addr R/W IntelliPort-II IntelliPort-IIEX
106---- --- -------------- ----------------
1070 R/W Data Port (byte) Data Port (byte or word)
1081 R/W (Not used) (MSB of word-wide data written to Data Port)
1092 R Status Register Status Register
1102 W Pointer Register Interrupt Mask Register
1113 R/W (Not used) Mailbox Registers (6 bits: 11111100)
1124,5 -- Reserved for future products
1136 -- Reserved for future products
1147 R Guaranteed to have no effect
1157 W Hardware reset of board.
116
117
118Rules:
119All data transfers are performed using the even i/o address. If byte-wide data
120transfers are being used, do INB/OUTB operations on the data port. If word-wide
121transfers are used, do INW/OUTW operations. In some circumstances (such as
122reading the power-up message) you will do INB from the data port, but in this
123case the MSB of each word read is lost. When accessing all other unreserved
124registers, use byte operations only.
125------------------------------------------------------------------------------*/
126
127//------------------------------------------------
128// Mandatory Includes:
129//------------------------------------------------
130//
131#include "ip2types.h"
132
133//-------------------------------------------------------------------------
134// Manifests for the I/O map:
135//-------------------------------------------------------------------------
136// R/W: Data port (byte) for IntelliPort-II,
137// R/W: Data port (byte or word) for IntelliPort-IIEX
138// Incoming or outgoing data passes through a FIFO, the status of which is
139// available in some of the bits in FIFO_STATUS. This (bidirectional) FIFO is
140// the primary means of transferring data, commands, flow-control, and status
141// information between the host and board.
142//
143#define FIFO_DATA 0
144
145// Another way of passing information between the board and the host is
146// through "mailboxes". Unlike a FIFO, a mailbox holds only a single byte of
147// data. Writing data to the mailbox causes a status bit to be set, and
148// potentially interrupting the intended receiver. The sender has some way to
149// determine whether the data has been read yet; as soon as it has, it may send
150// more. The mailboxes are handled differently on -II and -IIEX products, as
151// suggested below.
152//------------------------------------------------------------------------------
153// Read: Status Register for IntelliPort-II or -IIEX
154// The presence of any bit set here will cause an interrupt to the host,
155// provided the corresponding bit has been unmasked in the interrupt mask
156// register. Furthermore, interrupts to the host are disabled globally until the
157// loadware selects the irq line to use. With the exception of STN_MR, the bits
158// remain set so long as the associated condition is true.
159//
160#define FIFO_STATUS 2
161
162// Bit map of status bits which are identical for -II and -IIEX
163//
164#define ST_OUT_FULL 0x40 // Outbound FIFO full
165#define ST_IN_EMPTY 0x20 // Inbound FIFO empty
166#define ST_IN_MAIL 0x04 // Inbound Mailbox full
167
168// The following exists only on the Intelliport-IIEX, and indicates that the
169// board has not read the last outgoing mailbox data yet. In the IntelliPort-II,
170// the outgoing mailbox may be read back: a zero indicates the board has read
171// the data.
172//
173#define STE_OUT_MAIL 0x80 // Outbound mailbox full (!)
174
175// The following bits are defined differently for -II and -IIEX boards. Code
176// which relies on these bits will need to be functionally different for the two
177// types of boards and should be generally avoided because of the additional
178// complexity this creates:
179
180// Bit map of status bits only on -II
181
182// Fifo has been RESET (cleared when the status register is read). Note that
183// this condition cannot be masked and would always interrupt the host, except
184// that the hardware reset also disables interrupts globally from the board
185// until re-enabled by loadware. This could also arise from the
186// Am4701-supported command to reset the chip, but this command is generally not
187// used here.
188//
189#define STN_MR 0x80
190
191// See the AMD Am4701 data sheet for details on the following four bits. They
192// are not presently used by Computone drivers.
193//
194#define STN_OUT_AF 0x10 // Outbound FIFO almost full (programmable)
195#define STN_IN_AE 0x08 // Inbound FIFO almost empty (programmable)
196#define STN_BD 0x02 // Inbound byte detected
197#define STN_PE 0x01 // Parity/Framing condition detected
198
199// Bit-map of status bits only on -IIEX
200//
201#define STE_OUT_HF 0x10 // Outbound FIFO half full
202#define STE_IN_HF 0x08 // Inbound FIFO half full
203#define STE_IN_FULL 0x02 // Inbound FIFO full
204#define STE_OUT_MT 0x01 // Outbound FIFO empty
205
206//------------------------------------------------------------------------------
207
208// Intelliport-II -- Write Only: the pointer register.
209// Values are written to this register to select the Am4701 internal register to
210// be accessed on the next operation.
211//
212#define FIFO_PTR 0x02
213
214// Values for the pointer register
215//
216#define SEL_COMMAND 0x1 // Selects the Am4701 command register
217
218// Some possible commands:
219//
220#define SEL_CMD_MR 0x80 // Am4701 command to reset the chip
221#define SEL_CMD_SH 0x40 // Am4701 command to map the "other" port into the
222 // status register.
223#define SEL_CMD_UNSH 0 // Am4701 command to "unshift": port maps into its
224 // own status register.
225#define SEL_MASK 0x2 // Selects the Am4701 interrupt mask register. The
226 // interrupt mask register is bit-mapped to match
227 // the status register (FIFO_STATUS) except for
228 // STN_MR. (See above.)
229#define SEL_BYTE_DET 0x3 // Selects the Am4701 byte-detect register. (Not
230 // normally used except in diagnostics.)
231#define SEL_OUTMAIL 0x4 // Selects the outbound mailbox (R/W). Reading back
232 // a value of zero indicates that the mailbox has
233 // been read by the board and is available for more
234 // data./ Writing to the mailbox optionally
235 // interrupts the board, depending on the loadware's
236 // setting of its interrupt mask register.
237#define SEL_AEAF 0x5 // Selects AE/AF threshold register.
238#define SEL_INMAIL 0x6 // Selects the inbound mailbox (Read)
239
240//------------------------------------------------------------------------------
241// IntelliPort-IIEX -- Write Only: interrupt mask (and misc flags) register:
242// Unlike IntelliPort-II, bit assignments do NOT match those of the status
243// register.
244//
245#define FIFO_MASK 0x2
246
247// Mailbox readback select:
248// If set, reads to FIFO_MAIL will read the OUTBOUND mailbox (host to board). If
249// clear (default on reset) reads to FIFO_MAIL will read the INBOUND mailbox.
250// This is the normal situation. The clearing of a mailbox is determined on
251// -IIEX boards by waiting for the STE_OUT_MAIL bit to clear. Readback
252// capability is provided for diagnostic purposes only.
253//
254#define MX_OUTMAIL_RSEL 0x80
255
256#define MX_IN_MAIL 0x40 // Enables interrupts when incoming mailbox goes
257 // full (ST_IN_MAIL set).
258#define MX_IN_FULL 0x20 // Enables interrupts when incoming FIFO goes full
259 // (STE_IN_FULL).
260#define MX_IN_MT 0x08 // Enables interrupts when incoming FIFO goes empty
261 // (ST_IN_MT).
262#define MX_OUT_FULL 0x04 // Enables interrupts when outgoing FIFO goes full
263 // (ST_OUT_FULL).
264#define MX_OUT_MT 0x01 // Enables interrupts when outgoing FIFO goes empty
265 // (STE_OUT_MT).
266
267// Any remaining bits are reserved, and should be written to ZERO for
268// compatibility with future Computone products.
269
270//------------------------------------------------------------------------------
271// IntelliPort-IIEX: -- These are only 6-bit mailboxes !!! -- 11111100 (low two
272// bits always read back 0).
273// Read: One of the mailboxes, usually Inbound.
274// Inbound Mailbox (MX_OUTMAIL_RSEL = 0)
275// Outbound Mailbox (MX_OUTMAIL_RSEL = 1)
276// Write: Outbound Mailbox
277// For the IntelliPort-II boards, the outbound mailbox is read back to determine
278// whether the board has read the data (0 --> data has been read). For the
279// IntelliPort-IIEX, this is done by reading a status register. To determine
280// whether mailbox is available for more outbound data, use the STE_OUT_MAIL bit
281// in FIFO_STATUS. Moreover, although the Outbound Mailbox can be read back by
282// setting MX_OUTMAIL_RSEL, it is NOT cleared when the board reads it, as is the
283// case with the -II boards. For this reason, FIFO_MAIL is normally used to read
284// the inbound FIFO, and MX_OUTMAIL_RSEL kept clear. (See above for
285// MX_OUTMAIL_RSEL description.)
286//
287#define FIFO_MAIL 0x3
288
289//------------------------------------------------------------------------------
290// WRITE ONLY: Resets the board. (Data doesn't matter).
291//
292#define FIFO_RESET 0x7
293
294//------------------------------------------------------------------------------
295// READ ONLY: Will have no effect. (Data is undefined.)
296// Actually, there will be an effect, in that the operation is sure to generate
297// a bus cycle: viz., an I/O byte Read. This fact can be used to enforce short
298// delays when no comparable time constant is available.
299//
300#define FIFO_NOP 0x7
301
302//------------------------------------------------------------------------------
303// RESET & POWER-ON RESET MESSAGE
304/*------------------------------------------------------------------------------
305RESET:
306
307The IntelliPort-II and -IIEX boards are reset in three ways: Power-up, channel
308reset, and via a write to the reset register described above. For products using
309the ISA bus, these three sources of reset are equvalent. For MCA and EISA buses,
310the Power-up and channel reset sources cause additional hardware initialization
311which should only occur at system startup time.
312
313The third type of reset, called a "command reset", is done by writing any data
314to the FIFO_RESET address described above. This resets the on-board processor,
315FIFO, UARTS, and associated hardware.
316
317This passes control of the board to the bootstrap firmware, which performs a
318Power-On Self Test and which detects its current configuration. For example,
319-IIEX products determine the size of FIFO which has been installed, and the
320number and type of expansion boxes attached.
321
322This and other information is then written to the FIFO in a 16-byte data block
323to be read by the host. This block is guaranteed to be present within two (2)
324seconds of having received the command reset. The firmware is now ready to
325receive loadware from the host.
326
327It is good practice to perform a command reset to the board explicitly as part
328of your software initialization. This allows your code to properly restart from
329a soft boot. (Many systems do not issue channel reset on soft boot).
330
331Because of a hardware reset problem on some of the Cirrus Logic 1400's which are
332used on the product, it is recommended that you reset the board twice, separated
333by an approximately 50 milliseconds delay. (VERY approximately: probably ok to
334be off by a factor of five. The important point is that the first command reset
335in fact generates a reset pulse on the board. This pulse is guaranteed to last
336less than 10 milliseconds. The additional delay ensures the 1400 has had the
337chance to respond sufficiently to the first reset. Why not a longer delay? Much
338more than 50 milliseconds gets to be noticable, but the board would still work.
339
340Once all 16 bytes of the Power-on Reset Message have been read, the bootstrap
341firmware is ready to receive loadware.
342
343Note on Power-on Reset Message format:
344The various fields have been designed with future expansion in view.
345Combinations of bitfields and values have been defined which define products
346which may not currently exist. This has been done to allow drivers to anticipate
347the possible introduction of products in a systematic fashion. This is not
348intended to suggest that each potential product is actually under consideration.
349------------------------------------------------------------------------------*/
350
351//----------------------------------------
352// Format of Power-on Reset Message
353//----------------------------------------
354
355typedef union _porStr // "por" stands for Power On Reset
356{
357 unsigned char c[16]; // array used when considering the message as a
358 // string of undifferentiated characters
359
360 struct // Elements used when considering values
361 {
362 // The first two bytes out of the FIFO are two magic numbers. These are
363 // intended to establish that there is indeed a member of the
364 // IntelliPort-II(EX) family present. The remaining bytes may be
365 // expected // to be valid. When reading the Power-on Reset message,
366 // if the magic numbers do not match it is probably best to stop
367 // reading immediately. You are certainly not reading our board (unless
368 // hardware is faulty), and may in fact be reading some other piece of
369 // hardware.
370
371 unsigned char porMagic1; // magic number: first byte == POR_MAGIC_1
372 unsigned char porMagic2; // magic number: second byte == POR_MAGIC_2
373
374 // The Version, Revision, and Subrevision are stored as absolute numbers
375 // and would normally be displayed in the format V.R.S (e.g. 1.0.2)
376
377 unsigned char porVersion; // Bootstrap firmware version number
378 unsigned char porRevision; // Bootstrap firmware revision number
379 unsigned char porSubRev; // Bootstrap firmware sub-revision number
380
381 unsigned char porID; // Product ID: Bit-mapped according to
382 // conventions described below. Among other
383 // things, this allows us to distinguish
384 // IntelliPort-II boards from IntelliPort-IIEX
385 // boards.
386
387 unsigned char porBus; // IntelliPort-II: Unused
388 // IntelliPort-IIEX: Bus Information:
389 // Bit-mapped below
390
391 unsigned char porMemory; // On-board DRAM size: in 32k blocks
392
393 // porPorts1 (and porPorts2) are used to determine the ports which are
394 // available to the board. For non-expandable product, a single number
395 // is sufficient. For expandable product, the board may be connected
396 // to as many as four boxes. Each box may be (so far) either a 16-port
397 // or an 8-port size. Whenever an 8-port box is used, the remaining 8
398 // ports leave gaps between existing channels. For that reason,
399 // expandable products must report a MAP of available channels. Since
400 // each UART supports four ports, we represent each UART found by a
401 // single bit. Using two bytes to supply the mapping information we
402 // report the presense or absense of up to 16 UARTS, or 64 ports in
403 // steps of 4 ports. For -IIEX products, the ports are numbered
404 // starting at the box closest to the controller in the "chain".
405
406 // Interpreted Differently for IntelliPort-II and -IIEX.
407 // -II: Number of ports (Derived actually from product ID). See
408 // Diag1&2 to indicate if uart was actually detected.
409 // -IIEX: Bit-map of UARTS found, LSB (see below for MSB of this). This
410 // bitmap is based on detecting the uarts themselves;
411 // see porFlags for information from the box i.d's.
412 unsigned char porPorts1;
413
414 unsigned char porDiag1; // Results of on-board P.O.S.T, 1st byte
415 unsigned char porDiag2; // Results of on-board P.O.S.T, 2nd byte
416 unsigned char porSpeed; // Speed of local CPU: given as MHz x10
417 // e.g., 16.0 MHz CPU is reported as 160
418 unsigned char porFlags; // Misc information (see manifests below)
419 // Bit-mapped: CPU type, UART's present
420
421 unsigned char porPorts2; // -II: Undefined
422 // -IIEX: Bit-map of UARTS found, MSB (see
423 // above for LSB)
424
425 // IntelliPort-II: undefined
426 // IntelliPort-IIEX: 1 << porFifoSize gives the size, in bytes, of the
427 // host interface FIFO, in each direction. When running the -IIEX in
428 // 8-bit mode, fifo capacity is halved. The bootstrap firmware will
429 // have already accounted for this fact in generating this number.
430 unsigned char porFifoSize;
431
432 // IntelliPort-II: undefined
433 // IntelliPort-IIEX: The number of boxes connected. (Presently 1-4)
434 unsigned char porNumBoxes;
435 } e;
436} porStr, *porStrPtr;
437
438//--------------------------
439// Values for porStr fields
440//--------------------------
441
442//---------------------
443// porMagic1, porMagic2
444//----------------------
445//
446#define POR_MAGIC_1 0x96 // The only valid value for porMagic1
447#define POR_MAGIC_2 0x35 // The only valid value for porMagic2
448#define POR_1_INDEX 0 // Byte position of POR_MAGIC_1
449#define POR_2_INDEX 1 // Ditto for POR_MAGIC_2
450
451//----------------------
452// porID
453//----------------------
454//
455#define POR_ID_FAMILY 0xc0 // These bits indicate the general family of
456 // product.
457#define POR_ID_FII 0x00 // Family is "IntelliPort-II"
458#define POR_ID_FIIEX 0x40 // Family is "IntelliPort-IIEX"
459
460// These bits are reserved, presently zero. May be used at a later date to
461// convey other product information.
462//
463#define POR_ID_RESERVED 0x3c
464
465#define POR_ID_SIZE 0x03 // Remaining bits indicate number of ports &
466 // Connector information.
467#define POR_ID_II_8 0x00 // For IntelliPort-II, indicates 8-port using
468 // standard brick.
469#define POR_ID_II_8R 0x01 // For IntelliPort-II, indicates 8-port using
470 // RJ11's (no CTS)
471#define POR_ID_II_6 0x02 // For IntelliPort-II, indicates 6-port using
472 // RJ45's
473#define POR_ID_II_4 0x03 // For IntelliPort-II, indicates 4-port using
474 // 4xRJ45 connectors
475#define POR_ID_EX 0x00 // For IntelliPort-IIEX, indicates standard
476 // expandable controller (other values reserved)
477
478//----------------------
479// porBus
480//----------------------
481
482// IntelliPort-IIEX only: Board is installed in a 16-bit slot
483//
484#define POR_BUS_SLOT16 0x20
485
486// IntelliPort-IIEX only: DIP switch #8 is on, selecting 16-bit host interface
487// operation.
488//
489#define POR_BUS_DIP16 0x10
490
491// Bits 0-2 indicate type of bus: This information is stored in the bootstrap
492// loadware, different loadware being used on different products for different
493// buses. For most situations, the drivers do not need this information; but it
494// is handy in a diagnostic environment. For example, on microchannel boards,
495// you would not want to try to test several interrupts, only the one for which
496// you were configured.
497//
498#define POR_BUS_TYPE 0x07
499
500// Unknown: this product doesn't know what bus it is running in. (e.g. if same
501// bootstrap firmware were wanted for two different buses.)
502//
503#define POR_BUS_T_UNK 0
504
505// Note: existing firmware for ISA-8 and MC-8 currently report the POR_BUS_T_UNK
506// state, since the same bootstrap firmware is used for each.
507
508#define POR_BUS_T_MCA 1 // MCA BUS */
509#define POR_BUS_T_EISA 2 // EISA BUS */
510#define POR_BUS_T_ISA 3 // ISA BUS */
511
512// Values 4-7 Reserved
513
514// Remaining bits are reserved
515
516//----------------------
517// porDiag1
518//----------------------
519
520#define POR_BAD_MAPPER 0x80 // HW failure on P.O.S.T: Chip mapper failed
521
522// These two bits valid only for the IntelliPort-II
523//
524#define POR_BAD_UART1 0x01 // First 1400 bad
525#define POR_BAD_UART2 0x02 // Second 1400 bad
526
527//----------------------
528// porDiag2
529//----------------------
530
531#define POR_DEBUG_PORT 0x80 // debug port was detected by the P.O.S.T
532#define POR_DIAG_OK 0x00 // Indicates passage: Failure codes not yet
533 // available.
534 // Other bits undefined.
535//----------------------
536// porFlags
537//----------------------
538
539#define POR_CPU 0x03 // These bits indicate supposed CPU type
540#define POR_CPU_8 0x01 // Board uses an 80188 (no such thing yet)
541#define POR_CPU_6 0x02 // Board uses an 80186 (all existing products)
542#define POR_CEX4 0x04 // If set, this is an ISA-CEX/4: An ISA-4 (asic)
543 // which is architected like an ISA-CEX connected
544 // to a (hitherto impossible) 4-port box.
545#define POR_BOXES 0xf0 // Valid for IntelliPort-IIEX only: Map of Box
546 // sizes based on box I.D.
547#define POR_BOX_16 0x10 // Set indicates 16-port, clear 8-port
548
549//-------------------------------------
550// LOADWARE and DOWNLOADING CODE
551//-------------------------------------
552
553/*
554Loadware may be sent to the board in two ways:
5551) It may be read from a (binary image) data file block by block as each block
556 is sent to the board. This is only possible when the initialization is
557 performed by code which can access your file system. This is most suitable
558 for diagnostics and appications which use the interface library directly.
559
5602) It may be hard-coded into your source by including a .h file (typically
561 supplied by Computone), which declares a data array and initializes every
562 element. This achieves the same result as if an entire loadware file had
563 been read into the array.
564
565 This requires more data space in your program, but access to the file system
566 is not required. This method is more suited to driver code, which typically
567 is running at a level too low to access the file system directly.
568
569At present, loadware can only be generated at Computone.
570
571All Loadware begins with a header area which has a particular format. This
572includes a magic number which identifies the file as being (purportedly)
573loadware, CRC (for the loader), and version information.
574*/
575
576
577//-----------------------------------------------------------------------------
578// Format of loadware block
579//
580// This is defined as a union so we can pass a pointer to one of these items
581// and (if it is the first block) pick out the version information, etc.
582//
583// Otherwise, to deal with this as a simple character array
584//------------------------------------------------------------------------------
585
586#define LOADWARE_BLOCK_SIZE 512 // Number of bytes in each block of loadware
587
588typedef union _loadHdrStr
589{
590 unsigned char c[LOADWARE_BLOCK_SIZE]; // Valid for every block
591
592 struct // These fields are valid for only the first block of loadware.
593 {
594 unsigned char loadMagic; // Magic number: see below
595 unsigned char loadBlocksMore; // How many more blocks?
596 unsigned char loadCRC[2]; // Two CRC bytes: used by loader
597 unsigned char loadVersion; // Version number
598 unsigned char loadRevision; // Revision number
599 unsigned char loadSubRevision; // Sub-revision number
600 unsigned char loadSpares[9]; // Presently unused
601 unsigned char loadDates[32]; // Null-terminated string which can give
602 // date and time of compilation
603 } e;
604} loadHdrStr, *loadHdrStrPtr;
605
606//------------------------------------
607// Defines for downloading code:
608//------------------------------------
609
610// The loadMagic field in the first block of the loadfile must be this, else the
611// file is not valid.
612//
613#define MAGIC_LOADFILE 0x3c
614
615// How do we know the load was successful? On completion of the load, the
616// bootstrap firmware returns a code to indicate whether it thought the download
617// was valid and intends to execute it. These are the only possible valid codes:
618//
619#define LOADWARE_OK 0xc3 // Download was ok
620#define LOADWARE_BAD 0x5a // Download was bad (CRC error)
621
622// Constants applicable to writing blocks of loadware:
623// The first block of loadware might take 600 mS to load, in extreme cases.
624// (Expandable board: worst case for sending startup messages to the LCD's).
625// The 600mS figure is not really a calculation, but a conservative
626// guess/guarantee. Usually this will be within 100 mS, like subsequent blocks.
627//
628#define MAX_DLOAD_START_TIME 1000 // 1000 mS
629#define MAX_DLOAD_READ_TIME 100 // 100 mS
630
631// Firmware should respond with status (see above) within this long of host
632// having sent the final block.
633//
634#define MAX_DLOAD_ACK_TIME 100 // 100 mS, again!
635
636//------------------------------------------------------
637// MAXIMUM NUMBER OF PORTS PER BOARD:
638// This is fixed for now (with the expandable), but may
639// be expanding according to even newer products.
640//------------------------------------------------------
641//
642#define ABS_MAX_BOXES 4 // Absolute most boxes per board
643#define ABS_BIGGEST_BOX 16 // Absolute the most ports per box
644#define ABS_MOST_PORTS (ABS_MAX_BOXES * ABS_BIGGEST_BOX)
645
646#define I2_OUTSW(port, addr, count) outsw((port), (addr), (((count)+1)/2))
647#define I2_OUTSB(port, addr, count) outsb((port), (addr), (((count)+1))&-2)
648#define I2_INSW(port, addr, count) insw((port), (addr), (((count)+1)/2))
649#define I2_INSB(port, addr, count) insb((port), (addr), (((count)+1))&-2)
650
651#endif // I2HW_H
652
diff --git a/drivers/char/ip2/i2lib.c b/drivers/char/ip2/i2lib.c
deleted file mode 100644
index 0d10b89218ed..000000000000
--- a/drivers/char/ip2/i2lib.c
+++ /dev/null
@@ -1,2214 +0,0 @@
1/*******************************************************************************
2*
3* (c) 1999 by Computone Corporation
4*
5********************************************************************************
6*
7*
8* PACKAGE: Linux tty Device Driver for IntelliPort family of multiport
9* serial I/O controllers.
10*
11* DESCRIPTION: High-level interface code for the device driver. Uses the
12* Extremely Low Level Interface Support (i2ellis.c). Provides an
13* interface to the standard loadware, to support drivers or
14* application code. (This is included source code, not a separate
15* compilation module.)
16*
17*******************************************************************************/
18//------------------------------------------------------------------------------
19// Note on Strategy:
20// Once the board has been initialized, it will interrupt us when:
21// 1) It has something in the fifo for us to read (incoming data, flow control
22// packets, or whatever).
23// 2) It has stripped whatever we have sent last time in the FIFO (and
24// consequently is ready for more).
25//
26// Note also that the buffer sizes declared in i2lib.h are VERY SMALL. This
27// worsens performance considerably, but is done so that a great many channels
28// might use only a little memory.
29//------------------------------------------------------------------------------
30
31//------------------------------------------------------------------------------
32// Revision History:
33//
34// 0.00 - 4/16/91 --- First Draft
35// 0.01 - 4/29/91 --- 1st beta release
36// 0.02 - 6/14/91 --- Changes to allow small model compilation
37// 0.03 - 6/17/91 MAG Break reporting protected from interrupts routines with
38// in-line asm added for moving data to/from ring buffers,
39// replacing a variety of methods used previously.
40// 0.04 - 6/21/91 MAG Initial flow-control packets not queued until
41// i2_enable_interrupts time. Former versions would enqueue
42// them at i2_init_channel time, before we knew how many
43// channels were supposed to exist!
44// 0.05 - 10/12/91 MAG Major changes: works through the ellis.c routines now;
45// supports new 16-bit protocol and expandable boards.
46// - 10/24/91 MAG Most changes in place and stable.
47// 0.06 - 2/20/92 MAG Format of CMD_HOTACK corrected: the command takes no
48// argument.
49// 0.07 -- 3/11/92 MAG Support added to store special packet types at interrupt
50// level (mostly responses to specific commands.)
51// 0.08 -- 3/30/92 MAG Support added for STAT_MODEM packet
52// 0.09 -- 6/24/93 MAG i2Link... needed to update number of boards BEFORE
53// turning on the interrupt.
54// 0.10 -- 6/25/93 MAG To avoid gruesome death from a bad board, we sanity check
55// some incoming.
56//
57// 1.1 - 12/25/96 AKM Linux version.
58// - 10/09/98 DMC Revised Linux version.
59//------------------------------------------------------------------------------
60
61//************
62//* Includes *
63//************
64
65#include <linux/sched.h>
66#include "i2lib.h"
67
68
69//***********************
70//* Function Prototypes *
71//***********************
72static void i2QueueNeeds(i2eBordStrPtr, i2ChanStrPtr, int);
73static i2ChanStrPtr i2DeQueueNeeds(i2eBordStrPtr, int );
74static void i2StripFifo(i2eBordStrPtr);
75static void i2StuffFifoBypass(i2eBordStrPtr);
76static void i2StuffFifoFlow(i2eBordStrPtr);
77static void i2StuffFifoInline(i2eBordStrPtr);
78static int i2RetryFlushOutput(i2ChanStrPtr);
79
80// Not a documented part of the library routines (careful...) but the Diagnostic
81// i2diag.c finds them useful to help the throughput in certain limited
82// single-threaded operations.
83static void iiSendPendingMail(i2eBordStrPtr);
84static void serviceOutgoingFifo(i2eBordStrPtr);
85
86// Functions defined in ip2.c as part of interrupt handling
87static void do_input(struct work_struct *);
88static void do_status(struct work_struct *);
89
90//***************
91//* Debug Data *
92//***************
93#ifdef DEBUG_FIFO
94
95unsigned char DBGBuf[0x4000];
96unsigned short I = 0;
97
98static void
99WriteDBGBuf(char *s, unsigned char *src, unsigned short n )
100{
101 char *p = src;
102
103 // XXX: We need a spin lock here if we ever use this again
104
105 while (*s) { // copy label
106 DBGBuf[I] = *s++;
107 I = I++ & 0x3fff;
108 }
109 while (n--) { // copy data
110 DBGBuf[I] = *p++;
111 I = I++ & 0x3fff;
112 }
113}
114
115static void
116fatality(i2eBordStrPtr pB )
117{
118 int i;
119
120 for (i=0;i<sizeof(DBGBuf);i++) {
121 if ((i%16) == 0)
122 printk("\n%4x:",i);
123 printk("%02x ",DBGBuf[i]);
124 }
125 printk("\n");
126 for (i=0;i<sizeof(DBGBuf);i++) {
127 if ((i%16) == 0)
128 printk("\n%4x:",i);
129 if (DBGBuf[i] >= ' ' && DBGBuf[i] <= '~') {
130 printk(" %c ",DBGBuf[i]);
131 } else {
132 printk(" . ");
133 }
134 }
135 printk("\n");
136 printk("Last index %x\n",I);
137}
138#endif /* DEBUG_FIFO */
139
140//********
141//* Code *
142//********
143
144static inline int
145i2Validate ( i2ChanStrPtr pCh )
146{
147 //ip2trace(pCh->port_index, ITRC_VERIFY,ITRC_ENTER,2,pCh->validity,
148 // (CHANNEL_MAGIC | CHANNEL_SUPPORT));
149 return ((pCh->validity & (CHANNEL_MAGIC_BITS | CHANNEL_SUPPORT))
150 == (CHANNEL_MAGIC | CHANNEL_SUPPORT));
151}
152
153static void iiSendPendingMail_t(unsigned long data)
154{
155 i2eBordStrPtr pB = (i2eBordStrPtr)data;
156
157 iiSendPendingMail(pB);
158}
159
160//******************************************************************************
161// Function: iiSendPendingMail(pB)
162// Parameters: Pointer to a board structure
163// Returns: Nothing
164//
165// Description:
166// If any outgoing mail bits are set and there is outgoing mailbox is empty,
167// send the mail and clear the bits.
168//******************************************************************************
169static void
170iiSendPendingMail(i2eBordStrPtr pB)
171{
172 if (pB->i2eOutMailWaiting && (!pB->i2eWaitingForEmptyFifo) )
173 {
174 if (iiTrySendMail(pB, pB->i2eOutMailWaiting))
175 {
176 /* If we were already waiting for fifo to empty,
177 * or just sent MB_OUT_STUFFED, then we are
178 * still waiting for it to empty, until we should
179 * receive an MB_IN_STRIPPED from the board.
180 */
181 pB->i2eWaitingForEmptyFifo |=
182 (pB->i2eOutMailWaiting & MB_OUT_STUFFED);
183 pB->i2eOutMailWaiting = 0;
184 pB->SendPendingRetry = 0;
185 } else {
186/* The only time we hit this area is when "iiTrySendMail" has
187 failed. That only occurs when the outbound mailbox is
188 still busy with the last message. We take a short breather
189 to let the board catch up with itself and then try again.
190 16 Retries is the limit - then we got a borked board.
191 /\/\|=mhw=|\/\/ */
192
193 if( ++pB->SendPendingRetry < 16 ) {
194 setup_timer(&pB->SendPendingTimer,
195 iiSendPendingMail_t, (unsigned long)pB);
196 mod_timer(&pB->SendPendingTimer, jiffies + 1);
197 } else {
198 printk( KERN_ERR "IP2: iiSendPendingMail unable to queue outbound mail\n" );
199 }
200 }
201 }
202}
203
204//******************************************************************************
205// Function: i2InitChannels(pB, nChannels, pCh)
206// Parameters: Pointer to Ellis Board structure
207// Number of channels to initialize
208// Pointer to first element in an array of channel structures
209// Returns: Success or failure
210//
211// Description:
212//
213// This function patches pointers, back-pointers, and initializes all the
214// elements in the channel structure array.
215//
216// This should be run after the board structure is initialized, through having
217// loaded the standard loadware (otherwise it complains).
218//
219// In any case, it must be done before any serious work begins initializing the
220// irq's or sending commands...
221//
222//******************************************************************************
223static int
224i2InitChannels ( i2eBordStrPtr pB, int nChannels, i2ChanStrPtr pCh)
225{
226 int index, stuffIndex;
227 i2ChanStrPtr *ppCh;
228
229 if (pB->i2eValid != I2E_MAGIC) {
230 I2_COMPLETE(pB, I2EE_BADMAGIC);
231 }
232 if (pB->i2eState != II_STATE_STDLOADED) {
233 I2_COMPLETE(pB, I2EE_BADSTATE);
234 }
235
236 rwlock_init(&pB->read_fifo_spinlock);
237 rwlock_init(&pB->write_fifo_spinlock);
238 rwlock_init(&pB->Dbuf_spinlock);
239 rwlock_init(&pB->Bbuf_spinlock);
240 rwlock_init(&pB->Fbuf_spinlock);
241
242 // NO LOCK needed yet - this is init
243
244 pB->i2eChannelPtr = pCh;
245 pB->i2eChannelCnt = nChannels;
246
247 pB->i2Fbuf_strip = pB->i2Fbuf_stuff = 0;
248 pB->i2Dbuf_strip = pB->i2Dbuf_stuff = 0;
249 pB->i2Bbuf_strip = pB->i2Bbuf_stuff = 0;
250
251 pB->SendPendingRetry = 0;
252
253 memset ( pCh, 0, sizeof (i2ChanStr) * nChannels );
254
255 for (index = stuffIndex = 0, ppCh = (i2ChanStrPtr *)(pB->i2Fbuf);
256 nChannels && index < ABS_MOST_PORTS;
257 index++)
258 {
259 if ( !(pB->i2eChannelMap[index >> 4] & (1 << (index & 0xf)) ) ) {
260 continue;
261 }
262 rwlock_init(&pCh->Ibuf_spinlock);
263 rwlock_init(&pCh->Obuf_spinlock);
264 rwlock_init(&pCh->Cbuf_spinlock);
265 rwlock_init(&pCh->Pbuf_spinlock);
266 // NO LOCK needed yet - this is init
267 // Set up validity flag according to support level
268 if (pB->i2eGoodMap[index >> 4] & (1 << (index & 0xf)) ) {
269 pCh->validity = CHANNEL_MAGIC | CHANNEL_SUPPORT;
270 } else {
271 pCh->validity = CHANNEL_MAGIC;
272 }
273 pCh->pMyBord = pB; /* Back-pointer */
274
275 // Prepare an outgoing flow-control packet to send as soon as the chance
276 // occurs.
277 if ( pCh->validity & CHANNEL_SUPPORT ) {
278 pCh->infl.hd.i2sChannel = index;
279 pCh->infl.hd.i2sCount = 5;
280 pCh->infl.hd.i2sType = PTYPE_BYPASS;
281 pCh->infl.fcmd = 37;
282 pCh->infl.asof = 0;
283 pCh->infl.room = IBUF_SIZE - 1;
284
285 pCh->whenSendFlow = (IBUF_SIZE/5)*4; // when 80% full
286
287 // The following is similar to calling i2QueueNeeds, except that this
288 // is done in longhand, since we are setting up initial conditions on
289 // many channels at once.
290 pCh->channelNeeds = NEED_FLOW; // Since starting from scratch
291 pCh->sinceLastFlow = 0; // No bytes received since last flow
292 // control packet was queued
293 stuffIndex++;
294 *ppCh++ = pCh; // List this channel as needing
295 // initial flow control packet sent
296 }
297
298 // Don't allow anything to be sent until the status packets come in from
299 // the board.
300
301 pCh->outfl.asof = 0;
302 pCh->outfl.room = 0;
303
304 // Initialize all the ring buffers
305
306 pCh->Ibuf_stuff = pCh->Ibuf_strip = 0;
307 pCh->Obuf_stuff = pCh->Obuf_strip = 0;
308 pCh->Cbuf_stuff = pCh->Cbuf_strip = 0;
309
310 memset( &pCh->icount, 0, sizeof (struct async_icount) );
311 pCh->hotKeyIn = HOT_CLEAR;
312 pCh->channelOptions = 0;
313 pCh->bookMarks = 0;
314 init_waitqueue_head(&pCh->pBookmarkWait);
315
316 init_waitqueue_head(&pCh->open_wait);
317 init_waitqueue_head(&pCh->close_wait);
318 init_waitqueue_head(&pCh->delta_msr_wait);
319
320 // Set base and divisor so default custom rate is 9600
321 pCh->BaudBase = 921600; // MAX for ST654, changed after we get
322 pCh->BaudDivisor = 96; // the boxids (UART types) later
323
324 pCh->dataSetIn = 0;
325 pCh->dataSetOut = 0;
326
327 pCh->wopen = 0;
328 pCh->throttled = 0;
329
330 pCh->speed = CBR_9600;
331
332 pCh->flags = 0;
333
334 pCh->ClosingDelay = 5*HZ/10;
335 pCh->ClosingWaitTime = 30*HZ;
336
337 // Initialize task queue objects
338 INIT_WORK(&pCh->tqueue_input, do_input);
339 INIT_WORK(&pCh->tqueue_status, do_status);
340
341#ifdef IP2DEBUG_TRACE
342 pCh->trace = ip2trace;
343#endif
344
345 ++pCh;
346 --nChannels;
347 }
348 // No need to check for wrap here; this is initialization.
349 pB->i2Fbuf_stuff = stuffIndex;
350 I2_COMPLETE(pB, I2EE_GOOD);
351
352}
353
354//******************************************************************************
355// Function: i2DeQueueNeeds(pB, type)
356// Parameters: Pointer to a board structure
357// type bit map: may include NEED_INLINE, NEED_BYPASS, or NEED_FLOW
358// Returns:
359// Pointer to a channel structure
360//
361// Description: Returns pointer struct of next channel that needs service of
362// the type specified. Otherwise returns a NULL reference.
363//
364//******************************************************************************
365static i2ChanStrPtr
366i2DeQueueNeeds(i2eBordStrPtr pB, int type)
367{
368 unsigned short queueIndex;
369 unsigned long flags;
370
371 i2ChanStrPtr pCh = NULL;
372
373 switch(type) {
374
375 case NEED_INLINE:
376
377 write_lock_irqsave(&pB->Dbuf_spinlock, flags);
378 if ( pB->i2Dbuf_stuff != pB->i2Dbuf_strip)
379 {
380 queueIndex = pB->i2Dbuf_strip;
381 pCh = pB->i2Dbuf[queueIndex];
382 queueIndex++;
383 if (queueIndex >= CH_QUEUE_SIZE) {
384 queueIndex = 0;
385 }
386 pB->i2Dbuf_strip = queueIndex;
387 pCh->channelNeeds &= ~NEED_INLINE;
388 }
389 write_unlock_irqrestore(&pB->Dbuf_spinlock, flags);
390 break;
391
392 case NEED_BYPASS:
393
394 write_lock_irqsave(&pB->Bbuf_spinlock, flags);
395 if (pB->i2Bbuf_stuff != pB->i2Bbuf_strip)
396 {
397 queueIndex = pB->i2Bbuf_strip;
398 pCh = pB->i2Bbuf[queueIndex];
399 queueIndex++;
400 if (queueIndex >= CH_QUEUE_SIZE) {
401 queueIndex = 0;
402 }
403 pB->i2Bbuf_strip = queueIndex;
404 pCh->channelNeeds &= ~NEED_BYPASS;
405 }
406 write_unlock_irqrestore(&pB->Bbuf_spinlock, flags);
407 break;
408
409 case NEED_FLOW:
410
411 write_lock_irqsave(&pB->Fbuf_spinlock, flags);
412 if (pB->i2Fbuf_stuff != pB->i2Fbuf_strip)
413 {
414 queueIndex = pB->i2Fbuf_strip;
415 pCh = pB->i2Fbuf[queueIndex];
416 queueIndex++;
417 if (queueIndex >= CH_QUEUE_SIZE) {
418 queueIndex = 0;
419 }
420 pB->i2Fbuf_strip = queueIndex;
421 pCh->channelNeeds &= ~NEED_FLOW;
422 }
423 write_unlock_irqrestore(&pB->Fbuf_spinlock, flags);
424 break;
425 default:
426 printk(KERN_ERR "i2DeQueueNeeds called with bad type:%x\n",type);
427 break;
428 }
429 return pCh;
430}
431
432//******************************************************************************
433// Function: i2QueueNeeds(pB, pCh, type)
434// Parameters: Pointer to a board structure
435// Pointer to a channel structure
436// type bit map: may include NEED_INLINE, NEED_BYPASS, or NEED_FLOW
437// Returns: Nothing
438//
439// Description:
440// For each type of need selected, if the given channel is not already in the
441// queue, adds it, and sets the flag indicating it is in the queue.
442//******************************************************************************
443static void
444i2QueueNeeds(i2eBordStrPtr pB, i2ChanStrPtr pCh, int type)
445{
446 unsigned short queueIndex;
447 unsigned long flags;
448
449 // We turn off all the interrupts during this brief process, since the
450 // interrupt-level code might want to put things on the queue as well.
451
452 switch (type) {
453
454 case NEED_INLINE:
455
456 write_lock_irqsave(&pB->Dbuf_spinlock, flags);
457 if ( !(pCh->channelNeeds & NEED_INLINE) )
458 {
459 pCh->channelNeeds |= NEED_INLINE;
460 queueIndex = pB->i2Dbuf_stuff;
461 pB->i2Dbuf[queueIndex++] = pCh;
462 if (queueIndex >= CH_QUEUE_SIZE)
463 queueIndex = 0;
464 pB->i2Dbuf_stuff = queueIndex;
465 }
466 write_unlock_irqrestore(&pB->Dbuf_spinlock, flags);
467 break;
468
469 case NEED_BYPASS:
470
471 write_lock_irqsave(&pB->Bbuf_spinlock, flags);
472 if ((type & NEED_BYPASS) && !(pCh->channelNeeds & NEED_BYPASS))
473 {
474 pCh->channelNeeds |= NEED_BYPASS;
475 queueIndex = pB->i2Bbuf_stuff;
476 pB->i2Bbuf[queueIndex++] = pCh;
477 if (queueIndex >= CH_QUEUE_SIZE)
478 queueIndex = 0;
479 pB->i2Bbuf_stuff = queueIndex;
480 }
481 write_unlock_irqrestore(&pB->Bbuf_spinlock, flags);
482 break;
483
484 case NEED_FLOW:
485
486 write_lock_irqsave(&pB->Fbuf_spinlock, flags);
487 if ((type & NEED_FLOW) && !(pCh->channelNeeds & NEED_FLOW))
488 {
489 pCh->channelNeeds |= NEED_FLOW;
490 queueIndex = pB->i2Fbuf_stuff;
491 pB->i2Fbuf[queueIndex++] = pCh;
492 if (queueIndex >= CH_QUEUE_SIZE)
493 queueIndex = 0;
494 pB->i2Fbuf_stuff = queueIndex;
495 }
496 write_unlock_irqrestore(&pB->Fbuf_spinlock, flags);
497 break;
498
499 case NEED_CREDIT:
500 pCh->channelNeeds |= NEED_CREDIT;
501 break;
502 default:
503 printk(KERN_ERR "i2QueueNeeds called with bad type:%x\n",type);
504 break;
505 }
506 return;
507}
508
509//******************************************************************************
510// Function: i2QueueCommands(type, pCh, timeout, nCommands, pCs,...)
511// Parameters: type - PTYPE_BYPASS or PTYPE_INLINE
512// pointer to the channel structure
513// maximum period to wait
514// number of commands (n)
515// n commands
516// Returns: Number of commands sent, or -1 for error
517//
518// get board lock before calling
519//
520// Description:
521// Queues up some commands to be sent to a channel. To send possibly several
522// bypass or inline commands to the given channel. The timeout parameter
523// indicates how many HUNDREDTHS OF SECONDS to wait until there is room:
524// 0 = return immediately if no room, -ive = wait forever, +ive = number of
525// 1/100 seconds to wait. Return values:
526// -1 Some kind of nasty error: bad channel structure or invalid arguments.
527// 0 No room to send all the commands
528// (+) Number of commands sent
529//******************************************************************************
530static int
531i2QueueCommands(int type, i2ChanStrPtr pCh, int timeout, int nCommands,
532 cmdSyntaxPtr pCs0,...)
533{
534 int totalsize = 0;
535 int blocksize;
536 int lastended;
537 cmdSyntaxPtr *ppCs;
538 cmdSyntaxPtr pCs;
539 int count;
540 int flag;
541 i2eBordStrPtr pB;
542
543 unsigned short maxBlock;
544 unsigned short maxBuff;
545 short bufroom;
546 unsigned short stuffIndex;
547 unsigned char *pBuf;
548 unsigned char *pInsert;
549 unsigned char *pDest, *pSource;
550 unsigned short channel;
551 int cnt;
552 unsigned long flags = 0;
553 rwlock_t *lock_var_p = NULL;
554
555 // Make sure the channel exists, otherwise do nothing
556 if ( !i2Validate ( pCh ) ) {
557 return -1;
558 }
559
560 ip2trace (CHANN, ITRC_QUEUE, ITRC_ENTER, 0 );
561
562 pB = pCh->pMyBord;
563
564 // Board must also exist, and THE INTERRUPT COMMAND ALREADY SENT
565 if (pB->i2eValid != I2E_MAGIC || pB->i2eUsingIrq == I2_IRQ_UNDEFINED)
566 return -2;
567 // If the board has gone fatal, return bad, and also hit the trap routine if
568 // it exists.
569 if (pB->i2eFatal) {
570 if ( pB->i2eFatalTrap ) {
571 (*(pB)->i2eFatalTrap)(pB);
572 }
573 return -3;
574 }
575 // Set up some variables, Which buffers are we using? How big are they?
576 switch(type)
577 {
578 case PTYPE_INLINE:
579 flag = INL;
580 maxBlock = MAX_OBUF_BLOCK;
581 maxBuff = OBUF_SIZE;
582 pBuf = pCh->Obuf;
583 break;
584 case PTYPE_BYPASS:
585 flag = BYP;
586 maxBlock = MAX_CBUF_BLOCK;
587 maxBuff = CBUF_SIZE;
588 pBuf = pCh->Cbuf;
589 break;
590 default:
591 return -4;
592 }
593 // Determine the total size required for all the commands
594 totalsize = blocksize = sizeof(i2CmdHeader);
595 lastended = 0;
596 ppCs = &pCs0;
597 for ( count = nCommands; count; count--, ppCs++)
598 {
599 pCs = *ppCs;
600 cnt = pCs->length;
601 // Will a new block be needed for this one?
602 // Two possible reasons: too
603 // big or previous command has to be at the end of a packet.
604 if ((blocksize + cnt > maxBlock) || lastended) {
605 blocksize = sizeof(i2CmdHeader);
606 totalsize += sizeof(i2CmdHeader);
607 }
608 totalsize += cnt;
609 blocksize += cnt;
610
611 // If this command had to end a block, then we will make sure to
612 // account for it should there be any more blocks.
613 lastended = pCs->flags & END;
614 }
615 for (;;) {
616 // Make sure any pending flush commands go out before we add more data.
617 if ( !( pCh->flush_flags && i2RetryFlushOutput( pCh ) ) ) {
618 // How much room (this time through) ?
619 switch(type) {
620 case PTYPE_INLINE:
621 lock_var_p = &pCh->Obuf_spinlock;
622 write_lock_irqsave(lock_var_p, flags);
623 stuffIndex = pCh->Obuf_stuff;
624 bufroom = pCh->Obuf_strip - stuffIndex;
625 break;
626 case PTYPE_BYPASS:
627 lock_var_p = &pCh->Cbuf_spinlock;
628 write_lock_irqsave(lock_var_p, flags);
629 stuffIndex = pCh->Cbuf_stuff;
630 bufroom = pCh->Cbuf_strip - stuffIndex;
631 break;
632 default:
633 return -5;
634 }
635 if (--bufroom < 0) {
636 bufroom += maxBuff;
637 }
638
639 ip2trace (CHANN, ITRC_QUEUE, 2, 1, bufroom );
640
641 // Check for overflow
642 if (totalsize <= bufroom) {
643 // Normal Expected path - We still hold LOCK
644 break; /* from for()- Enough room: goto proceed */
645 }
646 ip2trace(CHANN, ITRC_QUEUE, 3, 1, totalsize);
647 write_unlock_irqrestore(lock_var_p, flags);
648 } else
649 ip2trace(CHANN, ITRC_QUEUE, 3, 1, totalsize);
650
651 /* Prepare to wait for buffers to empty */
652 serviceOutgoingFifo(pB); // Dump what we got
653
654 if (timeout == 0) {
655 return 0; // Tired of waiting
656 }
657 if (timeout > 0)
658 timeout--; // So negative values == forever
659
660 if (!in_interrupt()) {
661 schedule_timeout_interruptible(1); // short nap
662 } else {
663 // we cannot sched/sleep in interrupt silly
664 return 0;
665 }
666 if (signal_pending(current)) {
667 return 0; // Wake up! Time to die!!!
668 }
669
670 ip2trace (CHANN, ITRC_QUEUE, 4, 0 );
671
672 } // end of for(;;)
673
674 // At this point we have room and the lock - stick them in.
675 channel = pCh->infl.hd.i2sChannel;
676 pInsert = &pBuf[stuffIndex]; // Pointer to start of packet
677 pDest = CMD_OF(pInsert); // Pointer to start of command
678
679 // When we start counting, the block is the size of the header
680 for (blocksize = sizeof(i2CmdHeader), count = nCommands,
681 lastended = 0, ppCs = &pCs0;
682 count;
683 count--, ppCs++)
684 {
685 pCs = *ppCs; // Points to command protocol structure
686
687 // If this is a bookmark request command, post the fact that a bookmark
688 // request is pending. NOTE THIS TRICK ONLY WORKS BECAUSE CMD_BMARK_REQ
689 // has no parameters! The more general solution would be to reference
690 // pCs->cmd[0].
691 if (pCs == CMD_BMARK_REQ) {
692 pCh->bookMarks++;
693
694 ip2trace (CHANN, ITRC_DRAIN, 30, 1, pCh->bookMarks );
695
696 }
697 cnt = pCs->length;
698
699 // If this command would put us over the maximum block size or
700 // if the last command had to be at the end of a block, we end
701 // the existing block here and start a new one.
702 if ((blocksize + cnt > maxBlock) || lastended) {
703
704 ip2trace (CHANN, ITRC_QUEUE, 5, 0 );
705
706 PTYPE_OF(pInsert) = type;
707 CHANNEL_OF(pInsert) = channel;
708 // count here does not include the header
709 CMD_COUNT_OF(pInsert) = blocksize - sizeof(i2CmdHeader);
710 stuffIndex += blocksize;
711 if(stuffIndex >= maxBuff) {
712 stuffIndex = 0;
713 pInsert = pBuf;
714 }
715 pInsert = &pBuf[stuffIndex]; // Pointer to start of next pkt
716 pDest = CMD_OF(pInsert);
717 blocksize = sizeof(i2CmdHeader);
718 }
719 // Now we know there is room for this one in the current block
720
721 blocksize += cnt; // Total bytes in this command
722 pSource = pCs->cmd; // Copy the command into the buffer
723 while (cnt--) {
724 *pDest++ = *pSource++;
725 }
726 // If this command had to end a block, then we will make sure to account
727 // for it should there be any more blocks.
728 lastended = pCs->flags & END;
729 } // end for
730 // Clean up the final block by writing header, etc
731
732 PTYPE_OF(pInsert) = type;
733 CHANNEL_OF(pInsert) = channel;
734 // count here does not include the header
735 CMD_COUNT_OF(pInsert) = blocksize - sizeof(i2CmdHeader);
736 stuffIndex += blocksize;
737 if(stuffIndex >= maxBuff) {
738 stuffIndex = 0;
739 pInsert = pBuf;
740 }
741 // Updates the index, and post the need for service. When adding these to
742 // the queue of channels, we turn off the interrupt while doing so,
743 // because at interrupt level we might want to push a channel back to the
744 // end of the queue.
745 switch(type)
746 {
747 case PTYPE_INLINE:
748 pCh->Obuf_stuff = stuffIndex; // Store buffer pointer
749 write_unlock_irqrestore(&pCh->Obuf_spinlock, flags);
750
751 pB->debugInlineQueued++;
752 // Add the channel pointer to list of channels needing service (first
753 // come...), if it's not already there.
754 i2QueueNeeds(pB, pCh, NEED_INLINE);
755 break;
756
757 case PTYPE_BYPASS:
758 pCh->Cbuf_stuff = stuffIndex; // Store buffer pointer
759 write_unlock_irqrestore(&pCh->Cbuf_spinlock, flags);
760
761 pB->debugBypassQueued++;
762 // Add the channel pointer to list of channels needing service (first
763 // come...), if it's not already there.
764 i2QueueNeeds(pB, pCh, NEED_BYPASS);
765 break;
766 }
767
768 ip2trace (CHANN, ITRC_QUEUE, ITRC_RETURN, 1, nCommands );
769
770 return nCommands; // Good status: number of commands sent
771}
772
773//******************************************************************************
774// Function: i2GetStatus(pCh,resetBits)
775// Parameters: Pointer to a channel structure
776// Bit map of status bits to clear
777// Returns: Bit map of current status bits
778//
779// Description:
780// Returns the state of data set signals, and whether a break has been received,
781// (see i2lib.h for bit-mapped result). resetBits is a bit-map of any status
782// bits to be cleared: I2_BRK, I2_PAR, I2_FRA, I2_OVR,... These are cleared
783// AFTER the condition is passed. If pCh does not point to a valid channel,
784// returns -1 (which would be impossible otherwise.
785//******************************************************************************
786static int
787i2GetStatus(i2ChanStrPtr pCh, int resetBits)
788{
789 unsigned short status;
790 i2eBordStrPtr pB;
791
792 ip2trace (CHANN, ITRC_STATUS, ITRC_ENTER, 2, pCh->dataSetIn, resetBits );
793
794 // Make sure the channel exists, otherwise do nothing */
795 if ( !i2Validate ( pCh ) )
796 return -1;
797
798 pB = pCh->pMyBord;
799
800 status = pCh->dataSetIn;
801
802 // Clear any specified error bits: but note that only actual error bits can
803 // be cleared, regardless of the value passed.
804 if (resetBits)
805 {
806 pCh->dataSetIn &= ~(resetBits & (I2_BRK | I2_PAR | I2_FRA | I2_OVR));
807 pCh->dataSetIn &= ~(I2_DDCD | I2_DCTS | I2_DDSR | I2_DRI);
808 }
809
810 ip2trace (CHANN, ITRC_STATUS, ITRC_RETURN, 1, pCh->dataSetIn );
811
812 return status;
813}
814
815//******************************************************************************
816// Function: i2Input(pChpDest,count)
817// Parameters: Pointer to a channel structure
818// Pointer to data buffer
819// Number of bytes to read
820// Returns: Number of bytes read, or -1 for error
821//
822// Description:
823// Strips data from the input buffer and writes it to pDest. If there is a
824// collosal blunder, (invalid structure pointers or the like), returns -1.
825// Otherwise, returns the number of bytes read.
826//******************************************************************************
827static int
828i2Input(i2ChanStrPtr pCh)
829{
830 int amountToMove;
831 unsigned short stripIndex;
832 int count;
833 unsigned long flags = 0;
834
835 ip2trace (CHANN, ITRC_INPUT, ITRC_ENTER, 0);
836
837 // Ensure channel structure seems real
838 if ( !i2Validate( pCh ) ) {
839 count = -1;
840 goto i2Input_exit;
841 }
842 write_lock_irqsave(&pCh->Ibuf_spinlock, flags);
843
844 // initialize some accelerators and private copies
845 stripIndex = pCh->Ibuf_strip;
846
847 count = pCh->Ibuf_stuff - stripIndex;
848
849 // If buffer is empty or requested data count was 0, (trivial case) return
850 // without any further thought.
851 if ( count == 0 ) {
852 write_unlock_irqrestore(&pCh->Ibuf_spinlock, flags);
853 goto i2Input_exit;
854 }
855 // Adjust for buffer wrap
856 if ( count < 0 ) {
857 count += IBUF_SIZE;
858 }
859 // Don't give more than can be taken by the line discipline
860 amountToMove = pCh->pTTY->receive_room;
861 if (count > amountToMove) {
862 count = amountToMove;
863 }
864 // How much could we copy without a wrap?
865 amountToMove = IBUF_SIZE - stripIndex;
866
867 if (amountToMove > count) {
868 amountToMove = count;
869 }
870 // Move the first block
871 pCh->pTTY->ldisc->ops->receive_buf( pCh->pTTY,
872 &(pCh->Ibuf[stripIndex]), NULL, amountToMove );
873 // If we needed to wrap, do the second data move
874 if (count > amountToMove) {
875 pCh->pTTY->ldisc->ops->receive_buf( pCh->pTTY,
876 pCh->Ibuf, NULL, count - amountToMove );
877 }
878 // Bump and wrap the stripIndex all at once by the amount of data read. This
879 // method is good regardless of whether the data was in one or two pieces.
880 stripIndex += count;
881 if (stripIndex >= IBUF_SIZE) {
882 stripIndex -= IBUF_SIZE;
883 }
884 pCh->Ibuf_strip = stripIndex;
885
886 // Update our flow control information and possibly queue ourselves to send
887 // it, depending on how much data has been stripped since the last time a
888 // packet was sent.
889 pCh->infl.asof += count;
890
891 if ((pCh->sinceLastFlow += count) >= pCh->whenSendFlow) {
892 pCh->sinceLastFlow -= pCh->whenSendFlow;
893 write_unlock_irqrestore(&pCh->Ibuf_spinlock, flags);
894 i2QueueNeeds(pCh->pMyBord, pCh, NEED_FLOW);
895 } else {
896 write_unlock_irqrestore(&pCh->Ibuf_spinlock, flags);
897 }
898
899i2Input_exit:
900
901 ip2trace (CHANN, ITRC_INPUT, ITRC_RETURN, 1, count);
902
903 return count;
904}
905
906//******************************************************************************
907// Function: i2InputFlush(pCh)
908// Parameters: Pointer to a channel structure
909// Returns: Number of bytes stripped, or -1 for error
910//
911// Description:
912// Strips any data from the input buffer. If there is a collosal blunder,
913// (invalid structure pointers or the like), returns -1. Otherwise, returns the
914// number of bytes stripped.
915//******************************************************************************
916static int
917i2InputFlush(i2ChanStrPtr pCh)
918{
919 int count;
920 unsigned long flags;
921
922 // Ensure channel structure seems real
923 if ( !i2Validate ( pCh ) )
924 return -1;
925
926 ip2trace (CHANN, ITRC_INPUT, 10, 0);
927
928 write_lock_irqsave(&pCh->Ibuf_spinlock, flags);
929 count = pCh->Ibuf_stuff - pCh->Ibuf_strip;
930
931 // Adjust for buffer wrap
932 if (count < 0) {
933 count += IBUF_SIZE;
934 }
935
936 // Expedient way to zero out the buffer
937 pCh->Ibuf_strip = pCh->Ibuf_stuff;
938
939
940 // Update our flow control information and possibly queue ourselves to send
941 // it, depending on how much data has been stripped since the last time a
942 // packet was sent.
943
944 pCh->infl.asof += count;
945
946 if ( (pCh->sinceLastFlow += count) >= pCh->whenSendFlow )
947 {
948 pCh->sinceLastFlow -= pCh->whenSendFlow;
949 write_unlock_irqrestore(&pCh->Ibuf_spinlock, flags);
950 i2QueueNeeds(pCh->pMyBord, pCh, NEED_FLOW);
951 } else {
952 write_unlock_irqrestore(&pCh->Ibuf_spinlock, flags);
953 }
954
955 ip2trace (CHANN, ITRC_INPUT, 19, 1, count);
956
957 return count;
958}
959
960//******************************************************************************
961// Function: i2InputAvailable(pCh)
962// Parameters: Pointer to a channel structure
963// Returns: Number of bytes available, or -1 for error
964//
965// Description:
966// If there is a collosal blunder, (invalid structure pointers or the like),
967// returns -1. Otherwise, returns the number of bytes stripped. Otherwise,
968// returns the number of bytes available in the buffer.
969//******************************************************************************
970#if 0
971static int
972i2InputAvailable(i2ChanStrPtr pCh)
973{
974 int count;
975
976 // Ensure channel structure seems real
977 if ( !i2Validate ( pCh ) ) return -1;
978
979
980 // initialize some accelerators and private copies
981 read_lock_irqsave(&pCh->Ibuf_spinlock, flags);
982 count = pCh->Ibuf_stuff - pCh->Ibuf_strip;
983 read_unlock_irqrestore(&pCh->Ibuf_spinlock, flags);
984
985 // Adjust for buffer wrap
986 if (count < 0)
987 {
988 count += IBUF_SIZE;
989 }
990
991 return count;
992}
993#endif
994
995//******************************************************************************
996// Function: i2Output(pCh, pSource, count)
997// Parameters: Pointer to channel structure
998// Pointer to source data
999// Number of bytes to send
1000// Returns: Number of bytes sent, or -1 for error
1001//
1002// Description:
1003// Queues the data at pSource to be sent as data packets to the board. If there
1004// is a collosal blunder, (invalid structure pointers or the like), returns -1.
1005// Otherwise, returns the number of bytes written. What if there is not enough
1006// room for all the data? If pCh->channelOptions & CO_NBLOCK_WRITE is set, then
1007// we transfer as many characters as we can now, then return. If this bit is
1008// clear (default), routine will spin along until all the data is buffered.
1009// Should this occur, the 1-ms delay routine is called while waiting to avoid
1010// applications that one cannot break out of.
1011//******************************************************************************
1012static int
1013i2Output(i2ChanStrPtr pCh, const char *pSource, int count)
1014{
1015 i2eBordStrPtr pB;
1016 unsigned char *pInsert;
1017 int amountToMove;
1018 int countOriginal = count;
1019 unsigned short channel;
1020 unsigned short stuffIndex;
1021 unsigned long flags;
1022
1023 int bailout = 10;
1024
1025 ip2trace (CHANN, ITRC_OUTPUT, ITRC_ENTER, 2, count, 0 );
1026
1027 // Ensure channel structure seems real
1028 if ( !i2Validate ( pCh ) )
1029 return -1;
1030
1031 // initialize some accelerators and private copies
1032 pB = pCh->pMyBord;
1033 channel = pCh->infl.hd.i2sChannel;
1034
1035 // If the board has gone fatal, return bad, and also hit the trap routine if
1036 // it exists.
1037 if (pB->i2eFatal) {
1038 if (pB->i2eFatalTrap) {
1039 (*(pB)->i2eFatalTrap)(pB);
1040 }
1041 return -1;
1042 }
1043 // Proceed as though we would do everything
1044 while ( count > 0 ) {
1045
1046 // How much room in output buffer is there?
1047 read_lock_irqsave(&pCh->Obuf_spinlock, flags);
1048 amountToMove = pCh->Obuf_strip - pCh->Obuf_stuff - 1;
1049 read_unlock_irqrestore(&pCh->Obuf_spinlock, flags);
1050 if (amountToMove < 0) {
1051 amountToMove += OBUF_SIZE;
1052 }
1053 // Subtract off the headers size and see how much room there is for real
1054 // data. If this is negative, we will discover later.
1055 amountToMove -= sizeof (i2DataHeader);
1056
1057 // Don't move more (now) than can go in a single packet
1058 if ( amountToMove > (int)(MAX_OBUF_BLOCK - sizeof(i2DataHeader)) ) {
1059 amountToMove = MAX_OBUF_BLOCK - sizeof(i2DataHeader);
1060 }
1061 // Don't move more than the count we were given
1062 if (amountToMove > count) {
1063 amountToMove = count;
1064 }
1065 // Now we know how much we must move: NB because the ring buffers have
1066 // an overflow area at the end, we needn't worry about wrapping in the
1067 // middle of a packet.
1068
1069// Small WINDOW here with no LOCK but I can't call Flush with LOCK
1070// We would be flushing (or ending flush) anyway
1071
1072 ip2trace (CHANN, ITRC_OUTPUT, 10, 1, amountToMove );
1073
1074 if ( !(pCh->flush_flags && i2RetryFlushOutput(pCh) )
1075 && amountToMove > 0 )
1076 {
1077 write_lock_irqsave(&pCh->Obuf_spinlock, flags);
1078 stuffIndex = pCh->Obuf_stuff;
1079
1080 // Had room to move some data: don't know whether the block size,
1081 // buffer space, or what was the limiting factor...
1082 pInsert = &(pCh->Obuf[stuffIndex]);
1083
1084 // Set up the header
1085 CHANNEL_OF(pInsert) = channel;
1086 PTYPE_OF(pInsert) = PTYPE_DATA;
1087 TAG_OF(pInsert) = 0;
1088 ID_OF(pInsert) = ID_ORDINARY_DATA;
1089 DATA_COUNT_OF(pInsert) = amountToMove;
1090
1091 // Move the data
1092 memcpy( (char*)(DATA_OF(pInsert)), pSource, amountToMove );
1093 // Adjust pointers and indices
1094 pSource += amountToMove;
1095 pCh->Obuf_char_count += amountToMove;
1096 stuffIndex += amountToMove + sizeof(i2DataHeader);
1097 count -= amountToMove;
1098
1099 if (stuffIndex >= OBUF_SIZE) {
1100 stuffIndex = 0;
1101 }
1102 pCh->Obuf_stuff = stuffIndex;
1103
1104 write_unlock_irqrestore(&pCh->Obuf_spinlock, flags);
1105
1106 ip2trace (CHANN, ITRC_OUTPUT, 13, 1, stuffIndex );
1107
1108 } else {
1109
1110 // Cannot move data
1111 // becuz we need to stuff a flush
1112 // or amount to move is <= 0
1113
1114 ip2trace(CHANN, ITRC_OUTPUT, 14, 3,
1115 amountToMove, pB->i2eFifoRemains,
1116 pB->i2eWaitingForEmptyFifo );
1117
1118 // Put this channel back on queue
1119 // this ultimatly gets more data or wakes write output
1120 i2QueueNeeds(pB, pCh, NEED_INLINE);
1121
1122 if ( pB->i2eWaitingForEmptyFifo ) {
1123
1124 ip2trace (CHANN, ITRC_OUTPUT, 16, 0 );
1125
1126 // or schedule
1127 if (!in_interrupt()) {
1128
1129 ip2trace (CHANN, ITRC_OUTPUT, 61, 0 );
1130
1131 schedule_timeout_interruptible(2);
1132 if (signal_pending(current)) {
1133 break;
1134 }
1135 continue;
1136 } else {
1137
1138 ip2trace (CHANN, ITRC_OUTPUT, 62, 0 );
1139
1140 // let interrupt in = WAS restore_flags()
1141 // We hold no lock nor is irq off anymore???
1142
1143 break;
1144 }
1145 break; // from while(count)
1146 }
1147 else if ( pB->i2eFifoRemains < 32 && !pB->i2eTxMailEmpty ( pB ) )
1148 {
1149 ip2trace (CHANN, ITRC_OUTPUT, 19, 2,
1150 pB->i2eFifoRemains,
1151 pB->i2eTxMailEmpty );
1152
1153 break; // from while(count)
1154 } else if ( pCh->channelNeeds & NEED_CREDIT ) {
1155
1156 ip2trace (CHANN, ITRC_OUTPUT, 22, 0 );
1157
1158 break; // from while(count)
1159 } else if ( --bailout) {
1160
1161 // Try to throw more things (maybe not us) in the fifo if we're
1162 // not already waiting for it.
1163
1164 ip2trace (CHANN, ITRC_OUTPUT, 20, 0 );
1165
1166 serviceOutgoingFifo(pB);
1167 //break; CONTINUE;
1168 } else {
1169 ip2trace (CHANN, ITRC_OUTPUT, 21, 3,
1170 pB->i2eFifoRemains,
1171 pB->i2eOutMailWaiting,
1172 pB->i2eWaitingForEmptyFifo );
1173
1174 break; // from while(count)
1175 }
1176 }
1177 } // End of while(count)
1178
1179 i2QueueNeeds(pB, pCh, NEED_INLINE);
1180
1181 // We drop through either when the count expires, or when there is some
1182 // count left, but there was a non-blocking write.
1183 if (countOriginal > count) {
1184
1185 ip2trace (CHANN, ITRC_OUTPUT, 17, 2, countOriginal, count );
1186
1187 serviceOutgoingFifo( pB );
1188 }
1189
1190 ip2trace (CHANN, ITRC_OUTPUT, ITRC_RETURN, 2, countOriginal, count );
1191
1192 return countOriginal - count;
1193}
1194
1195//******************************************************************************
1196// Function: i2FlushOutput(pCh)
1197// Parameters: Pointer to a channel structure
1198// Returns: Nothing
1199//
1200// Description:
1201// Sends bypass command to start flushing (waiting possibly forever until there
1202// is room), then sends inline command to stop flushing output, (again waiting
1203// possibly forever).
1204//******************************************************************************
1205static inline void
1206i2FlushOutput(i2ChanStrPtr pCh)
1207{
1208
1209 ip2trace (CHANN, ITRC_FLUSH, 1, 1, pCh->flush_flags );
1210
1211 if (pCh->flush_flags)
1212 return;
1213
1214 if ( 1 != i2QueueCommands(PTYPE_BYPASS, pCh, 0, 1, CMD_STARTFL) ) {
1215 pCh->flush_flags = STARTFL_FLAG; // Failed - flag for later
1216
1217 ip2trace (CHANN, ITRC_FLUSH, 2, 0 );
1218
1219 } else if ( 1 != i2QueueCommands(PTYPE_INLINE, pCh, 0, 1, CMD_STOPFL) ) {
1220 pCh->flush_flags = STOPFL_FLAG; // Failed - flag for later
1221
1222 ip2trace (CHANN, ITRC_FLUSH, 3, 0 );
1223 }
1224}
1225
1226static int
1227i2RetryFlushOutput(i2ChanStrPtr pCh)
1228{
1229 int old_flags = pCh->flush_flags;
1230
1231 ip2trace (CHANN, ITRC_FLUSH, 14, 1, old_flags );
1232
1233 pCh->flush_flags = 0; // Clear flag so we can avoid recursion
1234 // and queue the commands
1235
1236 if ( old_flags & STARTFL_FLAG ) {
1237 if ( 1 == i2QueueCommands(PTYPE_BYPASS, pCh, 0, 1, CMD_STARTFL) ) {
1238 old_flags = STOPFL_FLAG; //Success - send stop flush
1239 } else {
1240 old_flags = STARTFL_FLAG; //Failure - Flag for retry later
1241 }
1242
1243 ip2trace (CHANN, ITRC_FLUSH, 15, 1, old_flags );
1244
1245 }
1246 if ( old_flags & STOPFL_FLAG ) {
1247 if (1 == i2QueueCommands(PTYPE_INLINE, pCh, 0, 1, CMD_STOPFL)) {
1248 old_flags = 0; // Success - clear flags
1249 }
1250
1251 ip2trace (CHANN, ITRC_FLUSH, 16, 1, old_flags );
1252 }
1253 pCh->flush_flags = old_flags;
1254
1255 ip2trace (CHANN, ITRC_FLUSH, 17, 1, old_flags );
1256
1257 return old_flags;
1258}
1259
1260//******************************************************************************
1261// Function: i2DrainOutput(pCh,timeout)
1262// Parameters: Pointer to a channel structure
1263// Maximum period to wait
1264// Returns: ?
1265//
1266// Description:
1267// Uses the bookmark request command to ask the board to send a bookmark back as
1268// soon as all the data is completely sent.
1269//******************************************************************************
1270static void
1271i2DrainWakeup(unsigned long d)
1272{
1273 i2ChanStrPtr pCh = (i2ChanStrPtr)d;
1274
1275 ip2trace (CHANN, ITRC_DRAIN, 10, 1, pCh->BookmarkTimer.expires );
1276
1277 pCh->BookmarkTimer.expires = 0;
1278 wake_up_interruptible( &pCh->pBookmarkWait );
1279}
1280
1281static void
1282i2DrainOutput(i2ChanStrPtr pCh, int timeout)
1283{
1284 wait_queue_t wait;
1285 i2eBordStrPtr pB;
1286
1287 ip2trace (CHANN, ITRC_DRAIN, ITRC_ENTER, 1, pCh->BookmarkTimer.expires);
1288
1289 pB = pCh->pMyBord;
1290 // If the board has gone fatal, return bad,
1291 // and also hit the trap routine if it exists.
1292 if (pB->i2eFatal) {
1293 if (pB->i2eFatalTrap) {
1294 (*(pB)->i2eFatalTrap)(pB);
1295 }
1296 return;
1297 }
1298 if ((timeout > 0) && (pCh->BookmarkTimer.expires == 0 )) {
1299 // One per customer (channel)
1300 setup_timer(&pCh->BookmarkTimer, i2DrainWakeup,
1301 (unsigned long)pCh);
1302
1303 ip2trace (CHANN, ITRC_DRAIN, 1, 1, pCh->BookmarkTimer.expires );
1304
1305 mod_timer(&pCh->BookmarkTimer, jiffies + timeout);
1306 }
1307
1308 i2QueueCommands( PTYPE_INLINE, pCh, -1, 1, CMD_BMARK_REQ );
1309
1310 init_waitqueue_entry(&wait, current);
1311 add_wait_queue(&(pCh->pBookmarkWait), &wait);
1312 set_current_state( TASK_INTERRUPTIBLE );
1313
1314 serviceOutgoingFifo( pB );
1315
1316 schedule(); // Now we take our interruptible sleep on
1317
1318 // Clean up the queue
1319 set_current_state( TASK_RUNNING );
1320 remove_wait_queue(&(pCh->pBookmarkWait), &wait);
1321
1322 // if expires == 0 then timer poped, then do not need to del_timer
1323 if ((timeout > 0) && pCh->BookmarkTimer.expires &&
1324 time_before(jiffies, pCh->BookmarkTimer.expires)) {
1325 del_timer( &(pCh->BookmarkTimer) );
1326 pCh->BookmarkTimer.expires = 0;
1327
1328 ip2trace (CHANN, ITRC_DRAIN, 3, 1, pCh->BookmarkTimer.expires );
1329
1330 }
1331 ip2trace (CHANN, ITRC_DRAIN, ITRC_RETURN, 1, pCh->BookmarkTimer.expires );
1332 return;
1333}
1334
1335//******************************************************************************
1336// Function: i2OutputFree(pCh)
1337// Parameters: Pointer to a channel structure
1338// Returns: Space in output buffer
1339//
1340// Description:
1341// Returns -1 if very gross error. Otherwise returns the amount of bytes still
1342// free in the output buffer.
1343//******************************************************************************
1344static int
1345i2OutputFree(i2ChanStrPtr pCh)
1346{
1347 int amountToMove;
1348 unsigned long flags;
1349
1350 // Ensure channel structure seems real
1351 if ( !i2Validate ( pCh ) ) {
1352 return -1;
1353 }
1354 read_lock_irqsave(&pCh->Obuf_spinlock, flags);
1355 amountToMove = pCh->Obuf_strip - pCh->Obuf_stuff - 1;
1356 read_unlock_irqrestore(&pCh->Obuf_spinlock, flags);
1357
1358 if (amountToMove < 0) {
1359 amountToMove += OBUF_SIZE;
1360 }
1361 // If this is negative, we will discover later
1362 amountToMove -= sizeof(i2DataHeader);
1363
1364 return (amountToMove < 0) ? 0 : amountToMove;
1365}
1366static void
1367
1368ip2_owake( PTTY tp)
1369{
1370 i2ChanStrPtr pCh;
1371
1372 if (tp == NULL) return;
1373
1374 pCh = tp->driver_data;
1375
1376 ip2trace (CHANN, ITRC_SICMD, 10, 2, tp->flags,
1377 (1 << TTY_DO_WRITE_WAKEUP) );
1378
1379 tty_wakeup(tp);
1380}
1381
1382static inline void
1383set_baud_params(i2eBordStrPtr pB)
1384{
1385 int i,j;
1386 i2ChanStrPtr *pCh;
1387
1388 pCh = (i2ChanStrPtr *) pB->i2eChannelPtr;
1389
1390 for (i = 0; i < ABS_MAX_BOXES; i++) {
1391 if (pB->channelBtypes.bid_value[i]) {
1392 if (BID_HAS_654(pB->channelBtypes.bid_value[i])) {
1393 for (j = 0; j < ABS_BIGGEST_BOX; j++) {
1394 if (pCh[i*16+j] == NULL)
1395 break;
1396 (pCh[i*16+j])->BaudBase = 921600; // MAX for ST654
1397 (pCh[i*16+j])->BaudDivisor = 96;
1398 }
1399 } else { // has cirrus cd1400
1400 for (j = 0; j < ABS_BIGGEST_BOX; j++) {
1401 if (pCh[i*16+j] == NULL)
1402 break;
1403 (pCh[i*16+j])->BaudBase = 115200; // MAX for CD1400
1404 (pCh[i*16+j])->BaudDivisor = 12;
1405 }
1406 }
1407 }
1408 }
1409}
1410
1411//******************************************************************************
1412// Function: i2StripFifo(pB)
1413// Parameters: Pointer to a board structure
1414// Returns: ?
1415//
1416// Description:
1417// Strips all the available data from the incoming FIFO, identifies the type of
1418// packet, and either buffers the data or does what needs to be done.
1419//
1420// Note there is no overflow checking here: if the board sends more data than it
1421// ought to, we will not detect it here, but blindly overflow...
1422//******************************************************************************
1423
1424// A buffer for reading in blocks for unknown channels
1425static unsigned char junkBuffer[IBUF_SIZE];
1426
1427// A buffer to read in a status packet. Because of the size of the count field
1428// for these things, the maximum packet size must be less than MAX_CMD_PACK_SIZE
1429static unsigned char cmdBuffer[MAX_CMD_PACK_SIZE + 4];
1430
1431// This table changes the bit order from MSR order given by STAT_MODEM packet to
1432// status bits used in our library.
1433static char xlatDss[16] = {
14340 | 0 | 0 | 0 ,
14350 | 0 | 0 | I2_CTS ,
14360 | 0 | I2_DSR | 0 ,
14370 | 0 | I2_DSR | I2_CTS ,
14380 | I2_RI | 0 | 0 ,
14390 | I2_RI | 0 | I2_CTS ,
14400 | I2_RI | I2_DSR | 0 ,
14410 | I2_RI | I2_DSR | I2_CTS ,
1442I2_DCD | 0 | 0 | 0 ,
1443I2_DCD | 0 | 0 | I2_CTS ,
1444I2_DCD | 0 | I2_DSR | 0 ,
1445I2_DCD | 0 | I2_DSR | I2_CTS ,
1446I2_DCD | I2_RI | 0 | 0 ,
1447I2_DCD | I2_RI | 0 | I2_CTS ,
1448I2_DCD | I2_RI | I2_DSR | 0 ,
1449I2_DCD | I2_RI | I2_DSR | I2_CTS };
1450
1451static inline void
1452i2StripFifo(i2eBordStrPtr pB)
1453{
1454 i2ChanStrPtr pCh;
1455 int channel;
1456 int count;
1457 unsigned short stuffIndex;
1458 int amountToRead;
1459 unsigned char *pc, *pcLimit;
1460 unsigned char uc;
1461 unsigned char dss_change;
1462 unsigned long bflags,cflags;
1463
1464// ip2trace (ITRC_NO_PORT, ITRC_SFIFO, ITRC_ENTER, 0 );
1465
1466 while (I2_HAS_INPUT(pB)) {
1467// ip2trace (ITRC_NO_PORT, ITRC_SFIFO, 2, 0 );
1468
1469 // Process packet from fifo a one atomic unit
1470 write_lock_irqsave(&pB->read_fifo_spinlock, bflags);
1471
1472 // The first word (or two bytes) will have channel number and type of
1473 // packet, possibly other information
1474 pB->i2eLeadoffWord[0] = iiReadWord(pB);
1475
1476 switch(PTYPE_OF(pB->i2eLeadoffWord))
1477 {
1478 case PTYPE_DATA:
1479 pB->got_input = 1;
1480
1481// ip2trace (ITRC_NO_PORT, ITRC_SFIFO, 3, 0 );
1482
1483 channel = CHANNEL_OF(pB->i2eLeadoffWord); /* Store channel */
1484 count = iiReadWord(pB); /* Count is in the next word */
1485
1486// NEW: Check the count for sanity! Should the hardware fail, our death
1487// is more pleasant. While an oversize channel is acceptable (just more
1488// than the driver supports), an over-length count clearly means we are
1489// sick!
1490 if ( ((unsigned int)count) > IBUF_SIZE ) {
1491 pB->i2eFatal = 2;
1492 write_unlock_irqrestore(&pB->read_fifo_spinlock,
1493 bflags);
1494 return; /* Bail out ASAP */
1495 }
1496 // Channel is illegally big ?
1497 if ((channel >= pB->i2eChannelCnt) ||
1498 (NULL==(pCh = ((i2ChanStrPtr*)pB->i2eChannelPtr)[channel])))
1499 {
1500 iiReadBuf(pB, junkBuffer, count);
1501 write_unlock_irqrestore(&pB->read_fifo_spinlock,
1502 bflags);
1503 break; /* From switch: ready for next packet */
1504 }
1505
1506 // Channel should be valid, then
1507
1508 // If this is a hot-key, merely post its receipt for now. These are
1509 // always supposed to be 1-byte packets, so we won't even check the
1510 // count. Also we will post an acknowledgement to the board so that
1511 // more data can be forthcoming. Note that we are not trying to use
1512 // these sequences in this driver, merely to robustly ignore them.
1513 if(ID_OF(pB->i2eLeadoffWord) == ID_HOT_KEY)
1514 {
1515 pCh->hotKeyIn = iiReadWord(pB) & 0xff;
1516 write_unlock_irqrestore(&pB->read_fifo_spinlock,
1517 bflags);
1518 i2QueueCommands(PTYPE_BYPASS, pCh, 0, 1, CMD_HOTACK);
1519 break; /* From the switch: ready for next packet */
1520 }
1521
1522 // Normal data! We crudely assume there is room for the data in our
1523 // buffer because the board wouldn't have exceeded his credit limit.
1524 write_lock_irqsave(&pCh->Ibuf_spinlock, cflags);
1525 // We have 2 locks now
1526 stuffIndex = pCh->Ibuf_stuff;
1527 amountToRead = IBUF_SIZE - stuffIndex;
1528 if (amountToRead > count)
1529 amountToRead = count;
1530
1531 // stuffIndex would have been already adjusted so there would
1532 // always be room for at least one, and count is always at least
1533 // one.
1534
1535 iiReadBuf(pB, &(pCh->Ibuf[stuffIndex]), amountToRead);
1536 pCh->icount.rx += amountToRead;
1537
1538 // Update the stuffIndex by the amount of data moved. Note we could
1539 // never ask for more data than would just fit. However, we might
1540 // have read in one more byte than we wanted because the read
1541 // rounds up to even bytes. If this byte is on the end of the
1542 // packet, and is padding, we ignore it. If the byte is part of
1543 // the actual data, we need to move it.
1544
1545 stuffIndex += amountToRead;
1546
1547 if (stuffIndex >= IBUF_SIZE) {
1548 if ((amountToRead & 1) && (count > amountToRead)) {
1549 pCh->Ibuf[0] = pCh->Ibuf[IBUF_SIZE];
1550 amountToRead++;
1551 stuffIndex = 1;
1552 } else {
1553 stuffIndex = 0;
1554 }
1555 }
1556
1557 // If there is anything left over, read it as well
1558 if (count > amountToRead) {
1559 amountToRead = count - amountToRead;
1560 iiReadBuf(pB, &(pCh->Ibuf[stuffIndex]), amountToRead);
1561 pCh->icount.rx += amountToRead;
1562 stuffIndex += amountToRead;
1563 }
1564
1565 // Update stuff index
1566 pCh->Ibuf_stuff = stuffIndex;
1567 write_unlock_irqrestore(&pCh->Ibuf_spinlock, cflags);
1568 write_unlock_irqrestore(&pB->read_fifo_spinlock,
1569 bflags);
1570
1571#ifdef USE_IQ
1572 schedule_work(&pCh->tqueue_input);
1573#else
1574 do_input(&pCh->tqueue_input);
1575#endif
1576
1577 // Note we do not need to maintain any flow-control credits at this
1578 // time: if we were to increment .asof and decrement .room, there
1579 // would be no net effect. Instead, when we strip data, we will
1580 // increment .asof and leave .room unchanged.
1581
1582 break; // From switch: ready for next packet
1583
1584 case PTYPE_STATUS:
1585 ip2trace (ITRC_NO_PORT, ITRC_SFIFO, 4, 0 );
1586
1587 count = CMD_COUNT_OF(pB->i2eLeadoffWord);
1588
1589 iiReadBuf(pB, cmdBuffer, count);
1590 // We can release early with buffer grab
1591 write_unlock_irqrestore(&pB->read_fifo_spinlock,
1592 bflags);
1593
1594 pc = cmdBuffer;
1595 pcLimit = &(cmdBuffer[count]);
1596
1597 while (pc < pcLimit) {
1598 channel = *pc++;
1599
1600 ip2trace (channel, ITRC_SFIFO, 7, 2, channel, *pc );
1601
1602 /* check for valid channel */
1603 if (channel < pB->i2eChannelCnt
1604 &&
1605 (pCh = (((i2ChanStrPtr*)pB->i2eChannelPtr)[channel])) != NULL
1606 )
1607 {
1608 dss_change = 0;
1609
1610 switch (uc = *pc++)
1611 {
1612 /* Breaks and modem signals are easy: just update status */
1613 case STAT_CTS_UP:
1614 if ( !(pCh->dataSetIn & I2_CTS) )
1615 {
1616 pCh->dataSetIn |= I2_DCTS;
1617 pCh->icount.cts++;
1618 dss_change = 1;
1619 }
1620 pCh->dataSetIn |= I2_CTS;
1621 break;
1622
1623 case STAT_CTS_DN:
1624 if ( pCh->dataSetIn & I2_CTS )
1625 {
1626 pCh->dataSetIn |= I2_DCTS;
1627 pCh->icount.cts++;
1628 dss_change = 1;
1629 }
1630 pCh->dataSetIn &= ~I2_CTS;
1631 break;
1632
1633 case STAT_DCD_UP:
1634 ip2trace (channel, ITRC_MODEM, 1, 1, pCh->dataSetIn );
1635
1636 if ( !(pCh->dataSetIn & I2_DCD) )
1637 {
1638 ip2trace (CHANN, ITRC_MODEM, 2, 0 );
1639 pCh->dataSetIn |= I2_DDCD;
1640 pCh->icount.dcd++;
1641 dss_change = 1;
1642 }
1643 pCh->dataSetIn |= I2_DCD;
1644
1645 ip2trace (channel, ITRC_MODEM, 3, 1, pCh->dataSetIn );
1646 break;
1647
1648 case STAT_DCD_DN:
1649 ip2trace (channel, ITRC_MODEM, 4, 1, pCh->dataSetIn );
1650 if ( pCh->dataSetIn & I2_DCD )
1651 {
1652 ip2trace (channel, ITRC_MODEM, 5, 0 );
1653 pCh->dataSetIn |= I2_DDCD;
1654 pCh->icount.dcd++;
1655 dss_change = 1;
1656 }
1657 pCh->dataSetIn &= ~I2_DCD;
1658
1659 ip2trace (channel, ITRC_MODEM, 6, 1, pCh->dataSetIn );
1660 break;
1661
1662 case STAT_DSR_UP:
1663 if ( !(pCh->dataSetIn & I2_DSR) )
1664 {
1665 pCh->dataSetIn |= I2_DDSR;
1666 pCh->icount.dsr++;
1667 dss_change = 1;
1668 }
1669 pCh->dataSetIn |= I2_DSR;
1670 break;
1671
1672 case STAT_DSR_DN:
1673 if ( pCh->dataSetIn & I2_DSR )
1674 {
1675 pCh->dataSetIn |= I2_DDSR;
1676 pCh->icount.dsr++;
1677 dss_change = 1;
1678 }
1679 pCh->dataSetIn &= ~I2_DSR;
1680 break;
1681
1682 case STAT_RI_UP:
1683 if ( !(pCh->dataSetIn & I2_RI) )
1684 {
1685 pCh->dataSetIn |= I2_DRI;
1686 pCh->icount.rng++;
1687 dss_change = 1;
1688 }
1689 pCh->dataSetIn |= I2_RI ;
1690 break;
1691
1692 case STAT_RI_DN:
1693 // to be compat with serial.c
1694 //if ( pCh->dataSetIn & I2_RI )
1695 //{
1696 // pCh->dataSetIn |= I2_DRI;
1697 // pCh->icount.rng++;
1698 // dss_change = 1;
1699 //}
1700 pCh->dataSetIn &= ~I2_RI ;
1701 break;
1702
1703 case STAT_BRK_DET:
1704 pCh->dataSetIn |= I2_BRK;
1705 pCh->icount.brk++;
1706 dss_change = 1;
1707 break;
1708
1709 // Bookmarks? one less request we're waiting for
1710 case STAT_BMARK:
1711 pCh->bookMarks--;
1712 if (pCh->bookMarks <= 0 ) {
1713 pCh->bookMarks = 0;
1714 wake_up_interruptible( &pCh->pBookmarkWait );
1715
1716 ip2trace (channel, ITRC_DRAIN, 20, 1, pCh->BookmarkTimer.expires );
1717 }
1718 break;
1719
1720 // Flow control packets? Update the new credits, and if
1721 // someone was waiting for output, queue him up again.
1722 case STAT_FLOW:
1723 pCh->outfl.room =
1724 ((flowStatPtr)pc)->room -
1725 (pCh->outfl.asof - ((flowStatPtr)pc)->asof);
1726
1727 ip2trace (channel, ITRC_STFLW, 1, 1, pCh->outfl.room );
1728
1729 if (pCh->channelNeeds & NEED_CREDIT)
1730 {
1731 ip2trace (channel, ITRC_STFLW, 2, 1, pCh->channelNeeds);
1732
1733 pCh->channelNeeds &= ~NEED_CREDIT;
1734 i2QueueNeeds(pB, pCh, NEED_INLINE);
1735 if ( pCh->pTTY )
1736 ip2_owake(pCh->pTTY);
1737 }
1738
1739 ip2trace (channel, ITRC_STFLW, 3, 1, pCh->channelNeeds);
1740
1741 pc += sizeof(flowStat);
1742 break;
1743
1744 /* Special packets: */
1745 /* Just copy the information into the channel structure */
1746
1747 case STAT_STATUS:
1748
1749 pCh->channelStatus = *((debugStatPtr)pc);
1750 pc += sizeof(debugStat);
1751 break;
1752
1753 case STAT_TXCNT:
1754
1755 pCh->channelTcount = *((cntStatPtr)pc);
1756 pc += sizeof(cntStat);
1757 break;
1758
1759 case STAT_RXCNT:
1760
1761 pCh->channelRcount = *((cntStatPtr)pc);
1762 pc += sizeof(cntStat);
1763 break;
1764
1765 case STAT_BOXIDS:
1766 pB->channelBtypes = *((bidStatPtr)pc);
1767 pc += sizeof(bidStat);
1768 set_baud_params(pB);
1769 break;
1770
1771 case STAT_HWFAIL:
1772 i2QueueCommands (PTYPE_INLINE, pCh, 0, 1, CMD_HW_TEST);
1773 pCh->channelFail = *((failStatPtr)pc);
1774 pc += sizeof(failStat);
1775 break;
1776
1777 /* No explicit match? then
1778 * Might be an error packet...
1779 */
1780 default:
1781 switch (uc & STAT_MOD_ERROR)
1782 {
1783 case STAT_ERROR:
1784 if (uc & STAT_E_PARITY) {
1785 pCh->dataSetIn |= I2_PAR;
1786 pCh->icount.parity++;
1787 }
1788 if (uc & STAT_E_FRAMING){
1789 pCh->dataSetIn |= I2_FRA;
1790 pCh->icount.frame++;
1791 }
1792 if (uc & STAT_E_OVERRUN){
1793 pCh->dataSetIn |= I2_OVR;
1794 pCh->icount.overrun++;
1795 }
1796 break;
1797
1798 case STAT_MODEM:
1799 // the answer to DSS_NOW request (not change)
1800 pCh->dataSetIn = (pCh->dataSetIn
1801 & ~(I2_RI | I2_CTS | I2_DCD | I2_DSR) )
1802 | xlatDss[uc & 0xf];
1803 wake_up_interruptible ( &pCh->dss_now_wait );
1804 default:
1805 break;
1806 }
1807 } /* End of switch on status type */
1808 if (dss_change) {
1809#ifdef USE_IQ
1810 schedule_work(&pCh->tqueue_status);
1811#else
1812 do_status(&pCh->tqueue_status);
1813#endif
1814 }
1815 }
1816 else /* Or else, channel is invalid */
1817 {
1818 // Even though the channel is invalid, we must test the
1819 // status to see how much additional data it has (to be
1820 // skipped)
1821 switch (*pc++)
1822 {
1823 case STAT_FLOW:
1824 pc += 4; /* Skip the data */
1825 break;
1826
1827 default:
1828 break;
1829 }
1830 }
1831 } // End of while (there is still some status packet left)
1832 break;
1833
1834 default: // Neither packet? should be impossible
1835 ip2trace (ITRC_NO_PORT, ITRC_SFIFO, 5, 1,
1836 PTYPE_OF(pB->i2eLeadoffWord) );
1837 write_unlock_irqrestore(&pB->read_fifo_spinlock,
1838 bflags);
1839
1840 break;
1841 } // End of switch on type of packets
1842 } /*while(board I2_HAS_INPUT)*/
1843
1844 ip2trace (ITRC_NO_PORT, ITRC_SFIFO, ITRC_RETURN, 0 );
1845
1846 // Send acknowledgement to the board even if there was no data!
1847 pB->i2eOutMailWaiting |= MB_IN_STRIPPED;
1848 return;
1849}
1850
1851//******************************************************************************
1852// Function: i2Write2Fifo(pB,address,count)
1853// Parameters: Pointer to a board structure, source address, byte count
1854// Returns: bytes written
1855//
1856// Description:
1857// Writes count bytes to board io address(implied) from source
1858// Adjusts count, leaves reserve for next time around bypass cmds
1859//******************************************************************************
1860static int
1861i2Write2Fifo(i2eBordStrPtr pB, unsigned char *source, int count,int reserve)
1862{
1863 int rc = 0;
1864 unsigned long flags;
1865 write_lock_irqsave(&pB->write_fifo_spinlock, flags);
1866 if (!pB->i2eWaitingForEmptyFifo) {
1867 if (pB->i2eFifoRemains > (count+reserve)) {
1868 pB->i2eFifoRemains -= count;
1869 iiWriteBuf(pB, source, count);
1870 pB->i2eOutMailWaiting |= MB_OUT_STUFFED;
1871 rc = count;
1872 }
1873 }
1874 write_unlock_irqrestore(&pB->write_fifo_spinlock, flags);
1875 return rc;
1876}
1877//******************************************************************************
1878// Function: i2StuffFifoBypass(pB)
1879// Parameters: Pointer to a board structure
1880// Returns: Nothing
1881//
1882// Description:
1883// Stuffs as many bypass commands into the fifo as possible. This is simpler
1884// than stuffing data or inline commands to fifo, since we do not have
1885// flow-control to deal with.
1886//******************************************************************************
1887static inline void
1888i2StuffFifoBypass(i2eBordStrPtr pB)
1889{
1890 i2ChanStrPtr pCh;
1891 unsigned char *pRemove;
1892 unsigned short stripIndex;
1893 unsigned short packetSize;
1894 unsigned short paddedSize;
1895 unsigned short notClogged = 1;
1896 unsigned long flags;
1897
1898 int bailout = 1000;
1899
1900 // Continue processing so long as there are entries, or there is room in the
1901 // fifo. Each entry represents a channel with something to do.
1902 while ( --bailout && notClogged &&
1903 (NULL != (pCh = i2DeQueueNeeds(pB,NEED_BYPASS))))
1904 {
1905 write_lock_irqsave(&pCh->Cbuf_spinlock, flags);
1906 stripIndex = pCh->Cbuf_strip;
1907
1908 // as long as there are packets for this channel...
1909
1910 while (stripIndex != pCh->Cbuf_stuff) {
1911 pRemove = &(pCh->Cbuf[stripIndex]);
1912 packetSize = CMD_COUNT_OF(pRemove) + sizeof(i2CmdHeader);
1913 paddedSize = roundup(packetSize, 2);
1914
1915 if (paddedSize > 0) {
1916 if ( 0 == i2Write2Fifo(pB, pRemove, paddedSize,0)) {
1917 notClogged = 0; /* fifo full */
1918 i2QueueNeeds(pB, pCh, NEED_BYPASS); // Put back on queue
1919 break; // Break from the channel
1920 }
1921 }
1922#ifdef DEBUG_FIFO
1923WriteDBGBuf("BYPS", pRemove, paddedSize);
1924#endif /* DEBUG_FIFO */
1925 pB->debugBypassCount++;
1926
1927 pRemove += packetSize;
1928 stripIndex += packetSize;
1929 if (stripIndex >= CBUF_SIZE) {
1930 stripIndex = 0;
1931 pRemove = pCh->Cbuf;
1932 }
1933 }
1934 // Done with this channel. Move to next, removing this one from
1935 // the queue of channels if we cleaned it out (i.e., didn't get clogged.
1936 pCh->Cbuf_strip = stripIndex;
1937 write_unlock_irqrestore(&pCh->Cbuf_spinlock, flags);
1938 } // Either clogged or finished all the work
1939
1940#ifdef IP2DEBUG_TRACE
1941 if ( !bailout ) {
1942 ip2trace (ITRC_NO_PORT, ITRC_ERROR, 1, 0 );
1943 }
1944#endif
1945}
1946
1947//******************************************************************************
1948// Function: i2StuffFifoFlow(pB)
1949// Parameters: Pointer to a board structure
1950// Returns: Nothing
1951//
1952// Description:
1953// Stuffs as many flow control packets into the fifo as possible. This is easier
1954// even than doing normal bypass commands, because there is always at most one
1955// packet, already assembled, for each channel.
1956//******************************************************************************
1957static inline void
1958i2StuffFifoFlow(i2eBordStrPtr pB)
1959{
1960 i2ChanStrPtr pCh;
1961 unsigned short paddedSize = roundup(sizeof(flowIn), 2);
1962
1963 ip2trace (ITRC_NO_PORT, ITRC_SFLOW, ITRC_ENTER, 2,
1964 pB->i2eFifoRemains, paddedSize );
1965
1966 // Continue processing so long as there are entries, or there is room in the
1967 // fifo. Each entry represents a channel with something to do.
1968 while ( (NULL != (pCh = i2DeQueueNeeds(pB,NEED_FLOW)))) {
1969 pB->debugFlowCount++;
1970
1971 // NO Chan LOCK needed ???
1972 if ( 0 == i2Write2Fifo(pB,(unsigned char *)&(pCh->infl),paddedSize,0)) {
1973 break;
1974 }
1975#ifdef DEBUG_FIFO
1976 WriteDBGBuf("FLOW",(unsigned char *) &(pCh->infl), paddedSize);
1977#endif /* DEBUG_FIFO */
1978
1979 } // Either clogged or finished all the work
1980
1981 ip2trace (ITRC_NO_PORT, ITRC_SFLOW, ITRC_RETURN, 0 );
1982}
1983
1984//******************************************************************************
1985// Function: i2StuffFifoInline(pB)
1986// Parameters: Pointer to a board structure
1987// Returns: Nothing
1988//
1989// Description:
1990// Stuffs as much data and inline commands into the fifo as possible. This is
1991// the most complex fifo-stuffing operation, since there if now the channel
1992// flow-control issue to deal with.
1993//******************************************************************************
1994static inline void
1995i2StuffFifoInline(i2eBordStrPtr pB)
1996{
1997 i2ChanStrPtr pCh;
1998 unsigned char *pRemove;
1999 unsigned short stripIndex;
2000 unsigned short packetSize;
2001 unsigned short paddedSize;
2002 unsigned short notClogged = 1;
2003 unsigned short flowsize;
2004 unsigned long flags;
2005
2006 int bailout = 1000;
2007 int bailout2;
2008
2009 ip2trace (ITRC_NO_PORT, ITRC_SICMD, ITRC_ENTER, 3, pB->i2eFifoRemains,
2010 pB->i2Dbuf_strip, pB->i2Dbuf_stuff );
2011
2012 // Continue processing so long as there are entries, or there is room in the
2013 // fifo. Each entry represents a channel with something to do.
2014 while ( --bailout && notClogged &&
2015 (NULL != (pCh = i2DeQueueNeeds(pB,NEED_INLINE))) )
2016 {
2017 write_lock_irqsave(&pCh->Obuf_spinlock, flags);
2018 stripIndex = pCh->Obuf_strip;
2019
2020 ip2trace (CHANN, ITRC_SICMD, 3, 2, stripIndex, pCh->Obuf_stuff );
2021
2022 // as long as there are packets for this channel...
2023 bailout2 = 1000;
2024 while ( --bailout2 && stripIndex != pCh->Obuf_stuff) {
2025 pRemove = &(pCh->Obuf[stripIndex]);
2026
2027 // Must determine whether this be a data or command packet to
2028 // calculate correctly the header size and the amount of
2029 // flow-control credit this type of packet will use.
2030 if (PTYPE_OF(pRemove) == PTYPE_DATA) {
2031 flowsize = DATA_COUNT_OF(pRemove);
2032 packetSize = flowsize + sizeof(i2DataHeader);
2033 } else {
2034 flowsize = CMD_COUNT_OF(pRemove);
2035 packetSize = flowsize + sizeof(i2CmdHeader);
2036 }
2037 flowsize = CREDIT_USAGE(flowsize);
2038 paddedSize = roundup(packetSize, 2);
2039
2040 ip2trace (CHANN, ITRC_SICMD, 4, 2, pB->i2eFifoRemains, paddedSize );
2041
2042 // If we don't have enough credits from the board to send the data,
2043 // flag the channel that we are waiting for flow control credit, and
2044 // break out. This will clean up this channel and remove us from the
2045 // queue of hot things to do.
2046
2047 ip2trace (CHANN, ITRC_SICMD, 5, 2, pCh->outfl.room, flowsize );
2048
2049 if (pCh->outfl.room <= flowsize) {
2050 // Do Not have the credits to send this packet.
2051 i2QueueNeeds(pB, pCh, NEED_CREDIT);
2052 notClogged = 0;
2053 break; // So to do next channel
2054 }
2055 if ( (paddedSize > 0)
2056 && ( 0 == i2Write2Fifo(pB, pRemove, paddedSize, 128))) {
2057 // Do Not have room in fifo to send this packet.
2058 notClogged = 0;
2059 i2QueueNeeds(pB, pCh, NEED_INLINE);
2060 break; // Break from the channel
2061 }
2062#ifdef DEBUG_FIFO
2063WriteDBGBuf("DATA", pRemove, paddedSize);
2064#endif /* DEBUG_FIFO */
2065 pB->debugInlineCount++;
2066
2067 pCh->icount.tx += flowsize;
2068 // Update current credits
2069 pCh->outfl.room -= flowsize;
2070 pCh->outfl.asof += flowsize;
2071 if (PTYPE_OF(pRemove) == PTYPE_DATA) {
2072 pCh->Obuf_char_count -= DATA_COUNT_OF(pRemove);
2073 }
2074 pRemove += packetSize;
2075 stripIndex += packetSize;
2076
2077 ip2trace (CHANN, ITRC_SICMD, 6, 2, stripIndex, pCh->Obuf_strip);
2078
2079 if (stripIndex >= OBUF_SIZE) {
2080 stripIndex = 0;
2081 pRemove = pCh->Obuf;
2082
2083 ip2trace (CHANN, ITRC_SICMD, 7, 1, stripIndex );
2084
2085 }
2086 } /* while */
2087 if ( !bailout2 ) {
2088 ip2trace (CHANN, ITRC_ERROR, 3, 0 );
2089 }
2090 // Done with this channel. Move to next, removing this one from the
2091 // queue of channels if we cleaned it out (i.e., didn't get clogged.
2092 pCh->Obuf_strip = stripIndex;
2093 write_unlock_irqrestore(&pCh->Obuf_spinlock, flags);
2094 if ( notClogged )
2095 {
2096
2097 ip2trace (CHANN, ITRC_SICMD, 8, 0 );
2098
2099 if ( pCh->pTTY ) {
2100 ip2_owake(pCh->pTTY);
2101 }
2102 }
2103 } // Either clogged or finished all the work
2104
2105 if ( !bailout ) {
2106 ip2trace (ITRC_NO_PORT, ITRC_ERROR, 4, 0 );
2107 }
2108
2109 ip2trace (ITRC_NO_PORT, ITRC_SICMD, ITRC_RETURN, 1,pB->i2Dbuf_strip);
2110}
2111
2112//******************************************************************************
2113// Function: serviceOutgoingFifo(pB)
2114// Parameters: Pointer to a board structure
2115// Returns: Nothing
2116//
2117// Description:
2118// Helper routine to put data in the outgoing fifo, if we aren't already waiting
2119// for something to be there. If the fifo has only room for a very little data,
2120// go head and hit the board with a mailbox hit immediately. Otherwise, it will
2121// have to happen later in the interrupt processing. Since this routine may be
2122// called both at interrupt and foreground time, we must turn off interrupts
2123// during the entire process.
2124//******************************************************************************
2125static void
2126serviceOutgoingFifo(i2eBordStrPtr pB)
2127{
2128 // If we aren't currently waiting for the board to empty our fifo, service
2129 // everything that is pending, in priority order (especially, Bypass before
2130 // Inline).
2131 if ( ! pB->i2eWaitingForEmptyFifo )
2132 {
2133 i2StuffFifoFlow(pB);
2134 i2StuffFifoBypass(pB);
2135 i2StuffFifoInline(pB);
2136
2137 iiSendPendingMail(pB);
2138 }
2139}
2140
2141//******************************************************************************
2142// Function: i2ServiceBoard(pB)
2143// Parameters: Pointer to a board structure
2144// Returns: Nothing
2145//
2146// Description:
2147// Normally this is called from interrupt level, but there is deliberately
2148// nothing in here specific to being called from interrupt level. All the
2149// hardware-specific, interrupt-specific things happen at the outer levels.
2150//
2151// For example, a timer interrupt could drive this routine for some sort of
2152// polled operation. The only requirement is that the programmer deal with any
2153// atomiticity/concurrency issues that result.
2154//
2155// This routine responds to the board's having sent mailbox information to the
2156// host (which would normally cause an interrupt). This routine reads the
2157// incoming mailbox. If there is no data in it, this board did not create the
2158// interrupt and/or has nothing to be done to it. (Except, if we have been
2159// waiting to write mailbox data to it, we may do so.
2160//
2161// Based on the value in the mailbox, we may take various actions.
2162//
2163// No checking here of pB validity: after all, it shouldn't have been called by
2164// the handler unless pB were on the list.
2165//******************************************************************************
2166static inline int
2167i2ServiceBoard ( i2eBordStrPtr pB )
2168{
2169 unsigned inmail;
2170 unsigned long flags;
2171
2172
2173 /* This should be atomic because of the way we are called... */
2174 if (NO_MAIL_HERE == ( inmail = pB->i2eStartMail ) ) {
2175 inmail = iiGetMail(pB);
2176 }
2177 pB->i2eStartMail = NO_MAIL_HERE;
2178
2179 ip2trace (ITRC_NO_PORT, ITRC_INTR, 2, 1, inmail );
2180
2181 if (inmail != NO_MAIL_HERE) {
2182 // If the board has gone fatal, nothing to do but hit a bit that will
2183 // alert foreground tasks to protest!
2184 if ( inmail & MB_FATAL_ERROR ) {
2185 pB->i2eFatal = 1;
2186 goto exit_i2ServiceBoard;
2187 }
2188
2189 /* Assuming no fatal condition, we proceed to do work */
2190 if ( inmail & MB_IN_STUFFED ) {
2191 pB->i2eFifoInInts++;
2192 i2StripFifo(pB); /* There might be incoming packets */
2193 }
2194
2195 if (inmail & MB_OUT_STRIPPED) {
2196 pB->i2eFifoOutInts++;
2197 write_lock_irqsave(&pB->write_fifo_spinlock, flags);
2198 pB->i2eFifoRemains = pB->i2eFifoSize;
2199 pB->i2eWaitingForEmptyFifo = 0;
2200 write_unlock_irqrestore(&pB->write_fifo_spinlock,
2201 flags);
2202
2203 ip2trace (ITRC_NO_PORT, ITRC_INTR, 30, 1, pB->i2eFifoRemains );
2204
2205 }
2206 serviceOutgoingFifo(pB);
2207 }
2208
2209 ip2trace (ITRC_NO_PORT, ITRC_INTR, 8, 0 );
2210
2211exit_i2ServiceBoard:
2212
2213 return 0;
2214}
diff --git a/drivers/char/ip2/i2lib.h b/drivers/char/ip2/i2lib.h
deleted file mode 100644
index e559e9bac06d..000000000000
--- a/drivers/char/ip2/i2lib.h
+++ /dev/null
@@ -1,351 +0,0 @@
1/*******************************************************************************
2*
3* (c) 1998 by Computone Corporation
4*
5********************************************************************************
6*
7*
8* PACKAGE: Linux tty Device Driver for IntelliPort II family of multiport
9* serial I/O controllers.
10*
11* DESCRIPTION: Header file for high level library functions
12*
13*******************************************************************************/
14#ifndef I2LIB_H
15#define I2LIB_H 1
16//------------------------------------------------------------------------------
17// I2LIB.H
18//
19// IntelliPort-II and IntelliPort-IIEX
20//
21// Defines, structure definitions, and external declarations for i2lib.c
22//------------------------------------------------------------------------------
23//--------------------------------------
24// Mandatory Includes:
25//--------------------------------------
26#include "ip2types.h"
27#include "i2ellis.h"
28#include "i2pack.h"
29#include "i2cmd.h"
30#include <linux/workqueue.h>
31
32//------------------------------------------------------------------------------
33// i2ChanStr -- Channel Structure:
34// Used to track per-channel information for the library routines using standard
35// loadware. Note also, a pointer to an array of these structures is patched
36// into the i2eBordStr (see i2ellis.h)
37//------------------------------------------------------------------------------
38//
39// If we make some limits on the maximum block sizes, we can avoid dealing with
40// buffer wrap. The wrapping of the buffer is based on where the start of the
41// packet is. Then there is always room for the packet contiguously.
42//
43// Maximum total length of an outgoing data or in-line command block. The limit
44// of 36 on data is quite arbitrary and based more on DOS memory limitations
45// than the board interface. However, for commands, the maximum packet length is
46// MAX_CMD_PACK_SIZE, because the field size for the count is only a few bits
47// (see I2PACK.H) in such packets. For data packets, the count field size is not
48// the limiting factor. As of this writing, MAX_OBUF_BLOCK < MAX_CMD_PACK_SIZE,
49// but be careful if wanting to modify either.
50//
51#define MAX_OBUF_BLOCK 36
52
53// Another note on maximum block sizes: we are buffering packets here. Data is
54// put into the buffer (if there is room) regardless of the credits from the
55// board. The board sends new credits whenever it has removed from his buffers a
56// number of characters equal to 80% of total buffer size. (Of course, the total
57// buffer size is what is reported when the very first set of flow control
58// status packets are received from the board. Therefore, to be robust, you must
59// always fill the board to at least 80% of the current credit limit, else you
60// might not give it enough to trigger a new report. These conditions are
61// obtained here so long as the maximum output block size is less than 20% the
62// size of the board's output buffers. This is true at present by "coincidence"
63// or "infernal knowledge": the board's output buffers are at least 700 bytes
64// long (20% = 140 bytes, at least). The 80% figure is "official", so the safest
65// strategy might be to trap the first flow control report and guarantee that
66// the effective maxObufBlock is the minimum of MAX_OBUF_BLOCK and 20% of first
67// reported buffer credit.
68//
69#define MAX_CBUF_BLOCK 6 // Maximum total length of a bypass command block
70
71#define IBUF_SIZE 512 // character capacity of input buffer per channel
72#define OBUF_SIZE 1024// character capacity of output buffer per channel
73#define CBUF_SIZE 10 // character capacity of output bypass buffer
74
75typedef struct _i2ChanStr
76{
77 // First, back-pointers so that given a pointer to this structure, you can
78 // determine the correct board and channel number to reference, (say, when
79 // issuing commands, etc. (Note, channel number is in infl.hd.i2sChannel.)
80
81 int port_index; // Index of port in channel structure array attached
82 // to board structure.
83 PTTY pTTY; // Pointer to tty structure for port (OS specific)
84 USHORT validity; // Indicates whether the given channel has been
85 // initialized, really exists (or is a missing
86 // channel, e.g. channel 9 on an 8-port box.)
87
88 i2eBordStrPtr pMyBord; // Back-pointer to this channel's board structure
89
90 int wopen; // waiting fer carrier
91
92 int throttled; // Set if upper layer can take no data
93
94 int flags; // Defined in tty.h
95
96 PWAITQ open_wait; // Pointer for OS sleep function.
97 PWAITQ close_wait; // Pointer for OS sleep function.
98 PWAITQ delta_msr_wait;// Pointer for OS sleep function.
99 PWAITQ dss_now_wait; // Pointer for OS sleep function.
100
101 struct timer_list BookmarkTimer; // Used by i2DrainOutput
102 wait_queue_head_t pBookmarkWait; // Used by i2DrainOutput
103
104 int BaudBase;
105 int BaudDivisor;
106
107 USHORT ClosingDelay;
108 USHORT ClosingWaitTime;
109
110 volatile
111 flowIn infl; // This structure is initialized as a completely
112 // formed flow-control command packet, and as such
113 // has the channel number, also the capacity and
114 // "as-of" data needed continuously.
115
116 USHORT sinceLastFlow; // Counts the number of characters read from input
117 // buffers, since the last time flow control info
118 // was sent.
119
120 USHORT whenSendFlow; // Determines when new flow control is to be sent to
121 // the board. Note unlike earlier manifestations of
122 // the driver, these packets can be sent from
123 // in-place.
124
125 USHORT channelNeeds; // Bit map of important things which must be done
126 // for this channel. (See bits below )
127
128 volatile
129 flowStat outfl; // Same type of structure is used to hold current
130 // flow control information used to control our
131 // output. "asof" is kept updated as data is sent,
132 // and "room" never goes to zero.
133
134 // The incoming ring buffer
135 // Unlike the outgoing buffers, this holds raw data, not packets. The two
136 // extra bytes are used to hold the byte-padding when there is room for an
137 // odd number of bytes before we must wrap.
138 //
139 UCHAR Ibuf[IBUF_SIZE + 2];
140 volatile
141 USHORT Ibuf_stuff; // Stuffing index
142 volatile
143 USHORT Ibuf_strip; // Stripping index
144
145 // The outgoing ring-buffer: Holds Data and command packets. N.B., even
146 // though these are in the channel structure, the channel is also written
147 // here, the easier to send it to the fifo when ready. HOWEVER, individual
148 // packets here are NOT padded to even length: the routines for writing
149 // blocks to the fifo will pad to even byte counts.
150 //
151 UCHAR Obuf[OBUF_SIZE+MAX_OBUF_BLOCK+4];
152 volatile
153 USHORT Obuf_stuff; // Stuffing index
154 volatile
155 USHORT Obuf_strip; // Stripping index
156 int Obuf_char_count;
157
158 // The outgoing bypass-command buffer. Unlike earlier manifestations, the
159 // flow control packets are sent directly from the structures. As above, the
160 // channel number is included in the packet, but they are NOT padded to even
161 // size.
162 //
163 UCHAR Cbuf[CBUF_SIZE+MAX_CBUF_BLOCK+2];
164 volatile
165 USHORT Cbuf_stuff; // Stuffing index
166 volatile
167 USHORT Cbuf_strip; // Stripping index
168
169 // The temporary buffer for the Linux tty driver PutChar entry.
170 //
171 UCHAR Pbuf[MAX_OBUF_BLOCK - sizeof (i2DataHeader)];
172 volatile
173 USHORT Pbuf_stuff; // Stuffing index
174
175 // The state of incoming data-set signals
176 //
177 USHORT dataSetIn; // Bit-mapped according to below. Also indicates
178 // whether a break has been detected since last
179 // inquiry.
180
181 // The state of outcoming data-set signals (as far as we can tell!)
182 //
183 USHORT dataSetOut; // Bit-mapped according to below.
184
185 // Most recent hot-key identifier detected
186 //
187 USHORT hotKeyIn; // Hot key as sent by the board, HOT_CLEAR indicates
188 // no hot key detected since last examined.
189
190 // Counter of outstanding requests for bookmarks
191 //
192 short bookMarks; // Number of outstanding bookmark requests, (+ive
193 // whenever a bookmark request if queued up, -ive
194 // whenever a bookmark is received).
195
196 // Misc options
197 //
198 USHORT channelOptions; // See below
199
200 // To store various incoming special packets
201 //
202 debugStat channelStatus;
203 cntStat channelRcount;
204 cntStat channelTcount;
205 failStat channelFail;
206
207 // To store the last values for line characteristics we sent to the board.
208 //
209 int speed;
210
211 int flush_flags;
212
213 void (*trace)(unsigned short,unsigned char,unsigned char,unsigned long,...);
214
215 /*
216 * Kernel counters for the 4 input interrupts
217 */
218 struct async_icount icount;
219
220 /*
221 * Task queues for processing input packets from the board.
222 */
223 struct work_struct tqueue_input;
224 struct work_struct tqueue_status;
225 struct work_struct tqueue_hangup;
226
227 rwlock_t Ibuf_spinlock;
228 rwlock_t Obuf_spinlock;
229 rwlock_t Cbuf_spinlock;
230 rwlock_t Pbuf_spinlock;
231
232} i2ChanStr, *i2ChanStrPtr;
233
234//---------------------------------------------------
235// Manifests and bit-maps for elements in i2ChanStr
236//---------------------------------------------------
237//
238// flush flags
239//
240#define STARTFL_FLAG 1
241#define STOPFL_FLAG 2
242
243// validity
244//
245#define CHANNEL_MAGIC_BITS 0xff00
246#define CHANNEL_MAGIC 0x5300 // (validity & CHANNEL_MAGIC_BITS) ==
247 // CHANNEL_MAGIC --> structure good
248
249#define CHANNEL_SUPPORT 0x0001 // Indicates channel is supported, exists,
250 // and passed P.O.S.T.
251
252// channelNeeds
253//
254#define NEED_FLOW 1 // Indicates flow control has been queued
255#define NEED_INLINE 2 // Indicates inline commands or data queued
256#define NEED_BYPASS 4 // Indicates bypass commands queued
257#define NEED_CREDIT 8 // Indicates would be sending except has not sufficient
258 // credit. The data is still in the channel structure,
259 // but the channel is not enqueued in the board
260 // structure again until there is a credit received from
261 // the board.
262
263// dataSetIn (Also the bits for i2GetStatus return value)
264//
265#define I2_DCD 1
266#define I2_CTS 2
267#define I2_DSR 4
268#define I2_RI 8
269
270// dataSetOut (Also the bits for i2GetStatus return value)
271//
272#define I2_DTR 1
273#define I2_RTS 2
274
275// i2GetStatus() can optionally clear these bits
276//
277#define I2_BRK 0x10 // A break was detected
278#define I2_PAR 0x20 // A parity error was received
279#define I2_FRA 0x40 // A framing error was received
280#define I2_OVR 0x80 // An overrun error was received
281
282// i2GetStatus() automatically clears these bits */
283//
284#define I2_DDCD 0x100 // DCD changed from its former value
285#define I2_DCTS 0x200 // CTS changed from its former value
286#define I2_DDSR 0x400 // DSR changed from its former value
287#define I2_DRI 0x800 // RI changed from its former value
288
289// hotKeyIn
290//
291#define HOT_CLEAR 0x1322 // Indicates that no hot-key has been detected
292
293// channelOptions
294//
295#define CO_NBLOCK_WRITE 1 // Writes don't block waiting for buffer. (Default
296 // is, they do wait.)
297
298// fcmodes
299//
300#define I2_OUTFLOW_CTS 0x0001
301#define I2_INFLOW_RTS 0x0002
302#define I2_INFLOW_DSR 0x0004
303#define I2_INFLOW_DTR 0x0008
304#define I2_OUTFLOW_DSR 0x0010
305#define I2_OUTFLOW_DTR 0x0020
306#define I2_OUTFLOW_XON 0x0040
307#define I2_OUTFLOW_XANY 0x0080
308#define I2_INFLOW_XON 0x0100
309
310#define I2_CRTSCTS (I2_OUTFLOW_CTS|I2_INFLOW_RTS)
311#define I2_IXANY_MODE (I2_OUTFLOW_XON|I2_OUTFLOW_XANY)
312
313//-------------------------------------------
314// Macros used from user level like functions
315//-------------------------------------------
316
317// Macros to set and clear channel options
318//
319#define i2SetOption(pCh, option) pCh->channelOptions |= option
320#define i2ClrOption(pCh, option) pCh->channelOptions &= ~option
321
322// Macro to set fatal-error trap
323//
324#define i2SetFatalTrap(pB, routine) pB->i2eFatalTrap = routine
325
326//--------------------------------------------
327// Declarations and prototypes for i2lib.c
328//--------------------------------------------
329//
330static int i2InitChannels(i2eBordStrPtr, int, i2ChanStrPtr);
331static int i2QueueCommands(int, i2ChanStrPtr, int, int, cmdSyntaxPtr,...);
332static int i2GetStatus(i2ChanStrPtr, int);
333static int i2Input(i2ChanStrPtr);
334static int i2InputFlush(i2ChanStrPtr);
335static int i2Output(i2ChanStrPtr, const char *, int);
336static int i2OutputFree(i2ChanStrPtr);
337static int i2ServiceBoard(i2eBordStrPtr);
338static void i2DrainOutput(i2ChanStrPtr, int);
339
340#ifdef IP2DEBUG_TRACE
341void ip2trace(unsigned short,unsigned char,unsigned char,unsigned long,...);
342#else
343#define ip2trace(a,b,c,d...) do {} while (0)
344#endif
345
346// Argument to i2QueueCommands
347//
348#define C_IN_LINE 1
349#define C_BYPASS 0
350
351#endif // I2LIB_H
diff --git a/drivers/char/ip2/i2pack.h b/drivers/char/ip2/i2pack.h
deleted file mode 100644
index 00342a677c90..000000000000
--- a/drivers/char/ip2/i2pack.h
+++ /dev/null
@@ -1,364 +0,0 @@
1/*******************************************************************************
2*
3* (c) 1998 by Computone Corporation
4*
5********************************************************************************
6*
7*
8* PACKAGE: Linux tty Device Driver for IntelliPort II family of multiport
9* serial I/O controllers.
10*
11* DESCRIPTION: Definitions of the packets used to transfer data and commands
12* Host <--> Board. Information provided here is only applicable
13* when the standard loadware is active.
14*
15*******************************************************************************/
16#ifndef I2PACK_H
17#define I2PACK_H 1
18
19//-----------------------------------------------
20// Revision History:
21//
22// 10 October 1991 MAG First draft
23// 24 February 1992 MAG Additions for 1.4.x loadware
24// 11 March 1992 MAG New status packets
25//
26//-----------------------------------------------
27
28//------------------------------------------------------------------------------
29// Packet Formats:
30//
31// Information passes between the host and board through the FIFO in packets.
32// These have headers which indicate the type of packet. Because the fifo data
33// path may be 16-bits wide, the protocol is constrained such that each packet
34// is always padded to an even byte count. (The lower-level interface routines
35// -- i2ellis.c -- are designed to do this).
36//
37// The sender (be it host or board) must place some number of complete packets
38// in the fifo, then place a message in the mailbox that packets are available.
39// Placing such a message interrupts the "receiver" (be it board or host), who
40// reads the mailbox message and determines that there are incoming packets
41// ready. Since there are no partial packets, and the length of a packet is
42// given in the header, the remainder of the packet can be read without checking
43// for FIFO empty condition. The process is repeated, packet by packet, until
44// the incoming FIFO is empty. Then the receiver uses the outbound mailbox to
45// signal the board that it has read the data. Only then can the sender place
46// additional data in the fifo.
47//------------------------------------------------------------------------------
48//
49//------------------------------------------------
50// Definition of Packet Header Area
51//------------------------------------------------
52//
53// Caution: these only define header areas. In actual use the data runs off
54// beyond the end of these structures.
55//
56// Since these structures are based on sequences of bytes which go to the board,
57// there cannot be ANY padding between the elements.
58#pragma pack(1)
59
60//----------------------------
61// DATA PACKETS
62//----------------------------
63
64typedef struct _i2DataHeader
65{
66 unsigned char i2sChannel; /* The channel number: 0-255 */
67
68 // -- Bitfields are allocated LSB first --
69
70 // For incoming data, indicates whether this is an ordinary packet or a
71 // special one (e.g., hot key hit).
72 unsigned i2sId : 2 __attribute__ ((__packed__));
73
74 // For tagging data packets. There are flush commands which flush only data
75 // packets bearing a particular tag. (used in implementing IntelliView and
76 // IntelliPrint). THE TAG VALUE 0xf is RESERVED and must not be used (it has
77 // meaning internally to the loadware).
78 unsigned i2sTag : 4;
79
80 // These two bits determine the type of packet sent/received.
81 unsigned i2sType : 2;
82
83 // The count of data to follow: does not include the possible additional
84 // padding byte. MAXIMUM COUNT: 4094. The top four bits must be 0.
85 unsigned short i2sCount;
86
87} i2DataHeader, *i2DataHeaderPtr;
88
89// Structure is immediately followed by the data, proper.
90
91//----------------------------
92// NON-DATA PACKETS
93//----------------------------
94
95typedef struct _i2CmdHeader
96{
97 unsigned char i2sChannel; // The channel number: 0-255 (Except where noted
98 // - see below
99
100 // Number of bytes of commands, status or whatever to follow
101 unsigned i2sCount : 6;
102
103 // These two bits determine the type of packet sent/received.
104 unsigned i2sType : 2;
105
106} i2CmdHeader, *i2CmdHeaderPtr;
107
108// Structure is immediately followed by the applicable data.
109
110//---------------------------------------
111// Flow Control Packets (Outbound)
112//---------------------------------------
113
114// One type of outbound command packet is so important that the entire structure
115// is explicitly defined here. That is the flow-control packet. This is never
116// sent by user-level code (as would be the commands to raise/lower DTR, for
117// example). These are only sent by the library routines in response to reading
118// incoming data into the buffers.
119//
120// The parameters inside the command block are maintained in place, then the
121// block is sent at the appropriate time.
122
123typedef struct _flowIn
124{
125 i2CmdHeader hd; // Channel #, count, type (see above)
126 unsigned char fcmd; // The flow control command (37)
127 unsigned short asof; // As of byte number "asof" (LSB first!) I have room
128 // for "room" bytes
129 unsigned short room;
130} flowIn, *flowInPtr;
131
132//----------------------------------------
133// (Incoming) Status Packets
134//----------------------------------------
135
136// Incoming packets which are non-data packets are status packets. In this case,
137// the channel number in the header is unimportant. What follows are one or more
138// sub-packets, the first word of which consists of the channel (first or low
139// byte) and the status indicator (second or high byte), followed by possibly
140// more data.
141
142#define STAT_CTS_UP 0 /* CTS raised (no other bytes) */
143#define STAT_CTS_DN 1 /* CTS dropped (no other bytes) */
144#define STAT_DCD_UP 2 /* DCD raised (no other bytes) */
145#define STAT_DCD_DN 3 /* DCD dropped (no other bytes) */
146#define STAT_DSR_UP 4 /* DSR raised (no other bytes) */
147#define STAT_DSR_DN 5 /* DSR dropped (no other bytes) */
148#define STAT_RI_UP 6 /* RI raised (no other bytes) */
149#define STAT_RI_DN 7 /* RI dropped (no other bytes) */
150#define STAT_BRK_DET 8 /* BRK detect (no other bytes) */
151#define STAT_FLOW 9 /* Flow control(-- more: see below */
152#define STAT_BMARK 10 /* Bookmark (no other bytes)
153 * Bookmark is sent as a response to
154 * a command 60: request for bookmark
155 */
156#define STAT_STATUS 11 /* Special packet: see below */
157#define STAT_TXCNT 12 /* Special packet: see below */
158#define STAT_RXCNT 13 /* Special packet: see below */
159#define STAT_BOXIDS 14 /* Special packet: see below */
160#define STAT_HWFAIL 15 /* Special packet: see below */
161
162#define STAT_MOD_ERROR 0xc0
163#define STAT_MODEM 0xc0/* If status & STAT_MOD_ERROR:
164 * == STAT_MODEM, then this is a modem
165 * status packet, given in response to a
166 * CMD_DSS_NOW command.
167 * The low nibble has each data signal:
168 */
169#define STAT_MOD_DCD 0x8
170#define STAT_MOD_RI 0x4
171#define STAT_MOD_DSR 0x2
172#define STAT_MOD_CTS 0x1
173
174#define STAT_ERROR 0x80/* If status & STAT_MOD_ERROR
175 * == STAT_ERROR, then
176 * sort of error on the channel.
177 * The remaining seven bits indicate
178 * what sort of error it is.
179 */
180/* The low three bits indicate parity, framing, or overrun errors */
181
182#define STAT_E_PARITY 4 /* Parity error */
183#define STAT_E_FRAMING 2 /* Framing error */
184#define STAT_E_OVERRUN 1 /* (uxart) overrun error */
185
186//---------------------------------------
187// STAT_FLOW packets
188//---------------------------------------
189
190typedef struct _flowStat
191{
192 unsigned short asof;
193 unsigned short room;
194}flowStat, *flowStatPtr;
195
196// flowStat packets are received from the board to regulate the flow of outgoing
197// data. A local copy of this structure is also kept to track the amount of
198// credits used and credits remaining. "room" is the amount of space in the
199// board's buffers, "as of" having received a certain byte number. When sending
200// data to the fifo, you must calculate how much buffer space your packet will
201// use. Add this to the current "asof" and subtract it from the current "room".
202//
203// The calculation for the board's buffer is given by CREDIT_USAGE, where size
204// is the un-rounded count of either data characters or command characters.
205// (Which is to say, the count rounded up, plus two).
206
207#define CREDIT_USAGE(size) (((size) + 3) & ~1)
208
209//---------------------------------------
210// STAT_STATUS packets
211//---------------------------------------
212
213typedef struct _debugStat
214{
215 unsigned char d_ccsr;
216 unsigned char d_txinh;
217 unsigned char d_stat1;
218 unsigned char d_stat2;
219} debugStat, *debugStatPtr;
220
221// debugStat packets are sent to the host in response to a CMD_GET_STATUS
222// command. Each byte is bit-mapped as described below:
223
224#define D_CCSR_XON 2 /* Has received XON, ready to transmit */
225#define D_CCSR_XOFF 4 /* Has received XOFF, not transmitting */
226#define D_CCSR_TXENAB 8 /* Transmitter is enabled */
227#define D_CCSR_RXENAB 0x80 /* Receiver is enabled */
228
229#define D_TXINH_BREAK 1 /* We are sending a break */
230#define D_TXINH_EMPTY 2 /* No data to send */
231#define D_TXINH_SUSP 4 /* Output suspended via command 57 */
232#define D_TXINH_CMD 8 /* We are processing an in-line command */
233#define D_TXINH_LCD 0x10 /* LCD diagnostics are running */
234#define D_TXINH_PAUSE 0x20 /* We are processing a PAUSE command */
235#define D_TXINH_DCD 0x40 /* DCD is low, preventing transmission */
236#define D_TXINH_DSR 0x80 /* DSR is low, preventing transmission */
237
238#define D_STAT1_TXEN 1 /* Transmit INTERRUPTS enabled */
239#define D_STAT1_RXEN 2 /* Receiver INTERRUPTS enabled */
240#define D_STAT1_MDEN 4 /* Modem (data set sigs) interrupts enabled */
241#define D_STAT1_RLM 8 /* Remote loopback mode selected */
242#define D_STAT1_LLM 0x10 /* Local internal loopback mode selected */
243#define D_STAT1_CTS 0x20 /* CTS is low, preventing transmission */
244#define D_STAT1_DTR 0x40 /* DTR is low, to stop remote transmission */
245#define D_STAT1_RTS 0x80 /* RTS is low, to stop remote transmission */
246
247#define D_STAT2_TXMT 1 /* Transmit buffers are all empty */
248#define D_STAT2_RXMT 2 /* Receive buffers are all empty */
249#define D_STAT2_RXINH 4 /* Loadware has tried to inhibit remote
250 * transmission: dropped DTR, sent XOFF,
251 * whatever...
252 */
253#define D_STAT2_RXFLO 8 /* Loadware can send no more data to host
254 * until it receives a flow-control packet
255 */
256//-----------------------------------------
257// STAT_TXCNT and STAT_RXCNT packets
258//----------------------------------------
259
260typedef struct _cntStat
261{
262 unsigned short cs_time; // (Assumes host is little-endian!)
263 unsigned short cs_count;
264} cntStat, *cntStatPtr;
265
266// These packets are sent in response to a CMD_GET_RXCNT or a CMD_GET_TXCNT
267// bypass command. cs_time is a running 1 Millisecond counter which acts as a
268// time stamp. cs_count is a running counter of data sent or received from the
269// uxarts. (Not including data added by the chip itself, as with CRLF
270// processing).
271//------------------------------------------
272// STAT_HWFAIL packets
273//------------------------------------------
274
275typedef struct _failStat
276{
277 unsigned char fs_written;
278 unsigned char fs_read;
279 unsigned short fs_address;
280} failStat, *failStatPtr;
281
282// This packet is sent whenever the on-board diagnostic process detects an
283// error. At startup, this process is dormant. The host can wake it up by
284// issuing the bypass command CMD_HW_TEST. The process runs at low priority and
285// performs continuous hardware verification; writing data to certain on-board
286// registers, reading it back, and comparing. If it detects an error, this
287// packet is sent to the host, and the process goes dormant again until the host
288// sends another CMD_HW_TEST. It then continues with the next register to be
289// tested.
290
291//------------------------------------------------------------------------------
292// Macros to deal with the headers more easily! Note that these are defined so
293// they may be used as "left" as well as "right" expressions.
294//------------------------------------------------------------------------------
295
296// Given a pointer to the packet, reference the channel number
297//
298#define CHANNEL_OF(pP) ((i2DataHeaderPtr)(pP))->i2sChannel
299
300// Given a pointer to the packet, reference the Packet type
301//
302#define PTYPE_OF(pP) ((i2DataHeaderPtr)(pP))->i2sType
303
304// The possible types of packets
305//
306#define PTYPE_DATA 0 /* Host <--> Board */
307#define PTYPE_BYPASS 1 /* Host ---> Board */
308#define PTYPE_INLINE 2 /* Host ---> Board */
309#define PTYPE_STATUS 2 /* Host <--- Board */
310
311// Given a pointer to a Data packet, reference the Tag
312//
313#define TAG_OF(pP) ((i2DataHeaderPtr)(pP))->i2sTag
314
315// Given a pointer to a Data packet, reference the data i.d.
316//
317#define ID_OF(pP) ((i2DataHeaderPtr)(pP))->i2sId
318
319// The possible types of ID's
320//
321#define ID_ORDINARY_DATA 0
322#define ID_HOT_KEY 1
323
324// Given a pointer to a Data packet, reference the count
325//
326#define DATA_COUNT_OF(pP) ((i2DataHeaderPtr)(pP))->i2sCount
327
328// Given a pointer to a Data packet, reference the beginning of data
329//
330#define DATA_OF(pP) &((unsigned char *)(pP))[4] // 4 = size of header
331
332// Given a pointer to a Non-Data packet, reference the count
333//
334#define CMD_COUNT_OF(pP) ((i2CmdHeaderPtr)(pP))->i2sCount
335
336#define MAX_CMD_PACK_SIZE 62 // Maximum size of such a count
337
338// Given a pointer to a Non-Data packet, reference the beginning of data
339//
340#define CMD_OF(pP) &((unsigned char *)(pP))[2] // 2 = size of header
341
342//--------------------------------
343// MailBox Bits:
344//--------------------------------
345
346//--------------------------
347// Outgoing (host to board)
348//--------------------------
349//
350#define MB_OUT_STUFFED 0x80 // Host has placed output in fifo
351#define MB_IN_STRIPPED 0x40 // Host has read in all input from fifo
352
353//--------------------------
354// Incoming (board to host)
355//--------------------------
356//
357#define MB_IN_STUFFED 0x80 // Board has placed input in fifo
358#define MB_OUT_STRIPPED 0x40 // Board has read all output from fifo
359#define MB_FATAL_ERROR 0x20 // Board has encountered a fatal error
360
361#pragma pack() // Reset padding to command-line default
362
363#endif // I2PACK_H
364
diff --git a/drivers/char/ip2/ip2.h b/drivers/char/ip2/ip2.h
deleted file mode 100644
index 936ccc533949..000000000000
--- a/drivers/char/ip2/ip2.h
+++ /dev/null
@@ -1,107 +0,0 @@
1/*******************************************************************************
2*
3* (c) 1998 by Computone Corporation
4*
5********************************************************************************
6*
7*
8* PACKAGE: Linux tty Device Driver for IntelliPort II family of multiport
9* serial I/O controllers.
10*
11* DESCRIPTION: Driver constants for configuration and tuning
12*
13* NOTES:
14*
15*******************************************************************************/
16#ifndef IP2_H
17#define IP2_H
18
19#include "ip2types.h"
20#include "i2cmd.h"
21
22/*************/
23/* Constants */
24/*************/
25
26/* Device major numbers - since version 2.0.26. */
27#define IP2_TTY_MAJOR 71
28#define IP2_CALLOUT_MAJOR 72
29#define IP2_IPL_MAJOR 73
30
31/* Board configuration array.
32 * This array defines the hardware irq and address for up to IP2_MAX_BOARDS
33 * (4 supported per ip2_types.h) ISA board addresses and irqs MUST be specified,
34 * PCI and EISA boards are probed for and automagicly configed
35 * iff the addresses are set to 1 and 2 respectivily.
36 * 0x0100 - 0x03f0 == ISA
37 * 1 == PCI
38 * 2 == EISA
39 * 0 == (skip this board)
40 * This array defines the hardware addresses for them. Special
41 * addresses are EISA and PCI which go sniffing for boards.
42
43 * In a multiboard system the position in the array determines which port
44 * devices are assigned to each board:
45 * board 0 is assigned ttyF0.. to ttyF63,
46 * board 1 is assigned ttyF64 to ttyF127,
47 * board 2 is assigned ttyF128 to ttyF191,
48 * board 3 is assigned ttyF192 to ttyF255.
49 *
50 * In PCI and EISA bus systems each range is mapped to card in
51 * monotonically increasing slot number order, ISA position is as specified
52 * here.
53
54 * If the irqs are ALL set to 0,0,0,0 all boards operate in
55 * polled mode. For interrupt operation ISA boards require that the IRQ be
56 * specified, while PCI and EISA boards any nonzero entry
57 * will enable interrupts using the BIOS configured irq for the board.
58 * An invalid irq entry will default to polled mode for that card and print
59 * console warning.
60
61 * When the driver is loaded as a module these setting can be overridden on the
62 * modprobe command line or on an option line in /etc/modprobe.conf.
63 * If the driver is built-in the configuration must be
64 * set here for ISA cards and address set to 1 and 2 for PCI and EISA.
65 *
66 * Here is an example that shows most if not all possibe combinations:
67
68 *static ip2config_t ip2config =
69 *{
70 * {11,1,0,0}, // irqs
71 * { // Addresses
72 * 0x0308, // Board 0, ttyF0 - ttyF63// ISA card at io=0x308, irq=11
73 * 0x0001, // Board 1, ttyF64 - ttyF127//PCI card configured by BIOS
74 * 0x0000, // Board 2, ttyF128 - ttyF191// Slot skipped
75 * 0x0002 // Board 3, ttyF192 - ttyF255//EISA card configured by BIOS
76 * // but polled not irq driven
77 * }
78 *};
79 */
80
81 /* this structure is zeroed out because the suggested method is to configure
82 * the driver as a module, set up the parameters with an options line in
83 * /etc/modprobe.conf and load with modprobe or kmod, the kernel
84 * module loader
85 */
86
87 /* This structure is NOW always initialized when the driver is initialized.
88 * Compiled in defaults MUST be added to the io and irq arrays in
89 * ip2.c. Those values are configurable from insmod parameters in the
90 * case of modules or from command line parameters (ip2=io,irq) when
91 * compiled in.
92 */
93
94static ip2config_t ip2config =
95{
96 {0,0,0,0}, // irqs
97 { // Addresses
98 /* Do NOT set compile time defaults HERE! Use the arrays in
99 ip2.c! These WILL be overwritten! =mhw= */
100 0x0000, // Board 0, ttyF0 - ttyF63
101 0x0000, // Board 1, ttyF64 - ttyF127
102 0x0000, // Board 2, ttyF128 - ttyF191
103 0x0000 // Board 3, ttyF192 - ttyF255
104 }
105};
106
107#endif
diff --git a/drivers/char/ip2/ip2ioctl.h b/drivers/char/ip2/ip2ioctl.h
deleted file mode 100644
index aa0a9da85e05..000000000000
--- a/drivers/char/ip2/ip2ioctl.h
+++ /dev/null
@@ -1,35 +0,0 @@
1/*******************************************************************************
2*
3* (c) 1998 by Computone Corporation
4*
5********************************************************************************
6*
7*
8* PACKAGE: Linux tty Device Driver for IntelliPort II family of multiport
9* serial I/O controllers.
10*
11* DESCRIPTION: Driver constants for configuration and tuning
12*
13* NOTES:
14*
15*******************************************************************************/
16
17#ifndef IP2IOCTL_H
18#define IP2IOCTL_H
19
20//*************
21//* Constants *
22//*************
23
24// High baud rates (if not defined elsewhere.
25#ifndef B153600
26# define B153600 0010005
27#endif
28#ifndef B307200
29# define B307200 0010006
30#endif
31#ifndef B921600
32# define B921600 0010007
33#endif
34
35#endif
diff --git a/drivers/char/ip2/ip2main.c b/drivers/char/ip2/ip2main.c
deleted file mode 100644
index d4b71e8d0d23..000000000000
--- a/drivers/char/ip2/ip2main.c
+++ /dev/null
@@ -1,3219 +0,0 @@
1/*
2*
3* (c) 1999 by Computone Corporation
4*
5********************************************************************************
6*
7* PACKAGE: Linux tty Device Driver for IntelliPort family of multiport
8* serial I/O controllers.
9*
10* DESCRIPTION: Mainline code for the device driver
11*
12*******************************************************************************/
13// ToDo:
14//
15// Fix the immediate DSS_NOW problem.
16// Work over the channel stats return logic in ip2_ipl_ioctl so they
17// make sense for all 256 possible channels and so the user space
18// utilities will compile and work properly.
19//
20// Done:
21//
22// 1.2.14 /\/\|=mhw=|\/\/
23// Added bounds checking to ip2_ipl_ioctl to avoid potential terroristic acts.
24// Changed the definition of ip2trace to be more consistent with kernel style
25// Thanks to Andreas Dilger <adilger@turbolabs.com> for these updates
26//
27// 1.2.13 /\/\|=mhw=|\/\/
28// DEVFS: Renamed ttf/{n} to tts/F{n} and cuf/{n} to cua/F{n} to conform
29// to agreed devfs serial device naming convention.
30//
31// 1.2.12 /\/\|=mhw=|\/\/
32// Cleaned up some remove queue cut and paste errors
33//
34// 1.2.11 /\/\|=mhw=|\/\/
35// Clean up potential NULL pointer dereferences
36// Clean up devfs registration
37// Add kernel command line parsing for io and irq
38// Compile defaults for io and irq are now set in ip2.c not ip2.h!
39// Reworked poll_only hack for explicit parameter setting
40// You must now EXPLICITLY set poll_only = 1 or set all irqs to 0
41// Merged ip2_loadmain and old_ip2_init
42// Converted all instances of interruptible_sleep_on into queue calls
43// Most of these had no race conditions but better to clean up now
44//
45// 1.2.10 /\/\|=mhw=|\/\/
46// Fixed the bottom half interrupt handler and enabled USE_IQI
47// to split the interrupt handler into a formal top-half / bottom-half
48// Fixed timing window on high speed processors that queued messages to
49// the outbound mail fifo faster than the board could handle.
50//
51// 1.2.9
52// Four box EX was barfing on >128k kmalloc, made structure smaller by
53// reducing output buffer size
54//
55// 1.2.8
56// Device file system support (MHW)
57//
58// 1.2.7
59// Fixed
60// Reload of ip2 without unloading ip2main hangs system on cat of /proc/modules
61//
62// 1.2.6
63//Fixes DCD problems
64// DCD was not reported when CLOCAL was set on call to TIOCMGET
65//
66//Enhancements:
67// TIOCMGET requests and waits for status return
68// No DSS interrupts enabled except for DCD when needed
69//
70// For internal use only
71//
72//#define IP2DEBUG_INIT
73//#define IP2DEBUG_OPEN
74//#define IP2DEBUG_WRITE
75//#define IP2DEBUG_READ
76//#define IP2DEBUG_IOCTL
77//#define IP2DEBUG_IPL
78
79//#define IP2DEBUG_TRACE
80//#define DEBUG_FIFO
81
82/************/
83/* Includes */
84/************/
85
86#include <linux/ctype.h>
87#include <linux/string.h>
88#include <linux/fcntl.h>
89#include <linux/errno.h>
90#include <linux/module.h>
91#include <linux/signal.h>
92#include <linux/sched.h>
93#include <linux/timer.h>
94#include <linux/interrupt.h>
95#include <linux/pci.h>
96#include <linux/mm.h>
97#include <linux/slab.h>
98#include <linux/major.h>
99#include <linux/wait.h>
100#include <linux/device.h>
101#include <linux/smp_lock.h>
102#include <linux/firmware.h>
103#include <linux/platform_device.h>
104
105#include <linux/tty.h>
106#include <linux/tty_flip.h>
107#include <linux/termios.h>
108#include <linux/tty_driver.h>
109#include <linux/serial.h>
110#include <linux/ptrace.h>
111#include <linux/ioport.h>
112
113#include <linux/cdk.h>
114#include <linux/comstats.h>
115#include <linux/delay.h>
116#include <linux/bitops.h>
117
118#include <asm/system.h>
119#include <asm/io.h>
120#include <asm/irq.h>
121
122#include <linux/vmalloc.h>
123#include <linux/init.h>
124
125#include <asm/uaccess.h>
126
127#include "ip2types.h"
128#include "ip2trace.h"
129#include "ip2ioctl.h"
130#include "ip2.h"
131#include "i2ellis.h"
132#include "i2lib.h"
133
134/*****************
135 * /proc/ip2mem *
136 *****************/
137
138#include <linux/proc_fs.h>
139#include <linux/seq_file.h>
140
141static const struct file_operations ip2mem_proc_fops;
142static const struct file_operations ip2_proc_fops;
143
144/********************/
145/* Type Definitions */
146/********************/
147
148/*************/
149/* Constants */
150/*************/
151
152/* String constants to identify ourselves */
153static const char pcName[] = "Computone IntelliPort Plus multiport driver";
154static const char pcVersion[] = "1.2.14";
155
156/* String constants for port names */
157static const char pcDriver_name[] = "ip2";
158static const char pcIpl[] = "ip2ipl";
159
160/***********************/
161/* Function Prototypes */
162/***********************/
163
164/* Global module entry functions */
165
166/* Private (static) functions */
167static int ip2_open(PTTY, struct file *);
168static void ip2_close(PTTY, struct file *);
169static int ip2_write(PTTY, const unsigned char *, int);
170static int ip2_putchar(PTTY, unsigned char);
171static void ip2_flush_chars(PTTY);
172static int ip2_write_room(PTTY);
173static int ip2_chars_in_buf(PTTY);
174static void ip2_flush_buffer(PTTY);
175static int ip2_ioctl(PTTY, struct file *, UINT, ULONG);
176static void ip2_set_termios(PTTY, struct ktermios *);
177static void ip2_set_line_discipline(PTTY);
178static void ip2_throttle(PTTY);
179static void ip2_unthrottle(PTTY);
180static void ip2_stop(PTTY);
181static void ip2_start(PTTY);
182static void ip2_hangup(PTTY);
183static int ip2_tiocmget(struct tty_struct *tty, struct file *file);
184static int ip2_tiocmset(struct tty_struct *tty, struct file *file,
185 unsigned int set, unsigned int clear);
186
187static void set_irq(int, int);
188static void ip2_interrupt_bh(struct work_struct *work);
189static irqreturn_t ip2_interrupt(int irq, void *dev_id);
190static void ip2_poll(unsigned long arg);
191static inline void service_all_boards(void);
192static void do_input(struct work_struct *);
193static void do_status(struct work_struct *);
194
195static void ip2_wait_until_sent(PTTY,int);
196
197static void set_params (i2ChanStrPtr, struct ktermios *);
198static int get_serial_info(i2ChanStrPtr, struct serial_struct __user *);
199static int set_serial_info(i2ChanStrPtr, struct serial_struct __user *);
200
201static ssize_t ip2_ipl_read(struct file *, char __user *, size_t, loff_t *);
202static ssize_t ip2_ipl_write(struct file *, const char __user *, size_t, loff_t *);
203static long ip2_ipl_ioctl(struct file *, UINT, ULONG);
204static int ip2_ipl_open(struct inode *, struct file *);
205
206static int DumpTraceBuffer(char __user *, int);
207static int DumpFifoBuffer( char __user *, int);
208
209static void ip2_init_board(int, const struct firmware *);
210static unsigned short find_eisa_board(int);
211static int ip2_setup(char *str);
212
213/***************/
214/* Static Data */
215/***************/
216
217static struct tty_driver *ip2_tty_driver;
218
219/* Here, then is a table of board pointers which the interrupt routine should
220 * scan through to determine who it must service.
221 */
222static unsigned short i2nBoards; // Number of boards here
223
224static i2eBordStrPtr i2BoardPtrTable[IP2_MAX_BOARDS];
225
226static i2ChanStrPtr DevTable[IP2_MAX_PORTS];
227//DevTableMem just used to save addresses for kfree
228static void *DevTableMem[IP2_MAX_BOARDS];
229
230/* This is the driver descriptor for the ip2ipl device, which is used to
231 * download the loadware to the boards.
232 */
233static const struct file_operations ip2_ipl = {
234 .owner = THIS_MODULE,
235 .read = ip2_ipl_read,
236 .write = ip2_ipl_write,
237 .unlocked_ioctl = ip2_ipl_ioctl,
238 .open = ip2_ipl_open,
239};
240
241static unsigned long irq_counter;
242static unsigned long bh_counter;
243
244// Use immediate queue to service interrupts
245#define USE_IQI
246//#define USE_IQ // PCI&2.2 needs work
247
248/* The timer_list entry for our poll routine. If interrupt operation is not
249 * selected, the board is serviced periodically to see if anything needs doing.
250 */
251#define POLL_TIMEOUT (jiffies + 1)
252static DEFINE_TIMER(PollTimer, ip2_poll, 0, 0);
253
254#ifdef IP2DEBUG_TRACE
255/* Trace (debug) buffer data */
256#define TRACEMAX 1000
257static unsigned long tracebuf[TRACEMAX];
258static int tracestuff;
259static int tracestrip;
260static int tracewrap;
261#endif
262
263/**********/
264/* Macros */
265/**********/
266
267#ifdef IP2DEBUG_OPEN
268#define DBG_CNT(s) printk(KERN_DEBUG "(%s): [%x] ttyc=%d, modc=%x -> %s\n", \
269 tty->name,(pCh->flags), \
270 tty->count,/*GET_USE_COUNT(module)*/0,s)
271#else
272#define DBG_CNT(s)
273#endif
274
275/********/
276/* Code */
277/********/
278
279#include "i2ellis.c" /* Extremely low-level interface services */
280#include "i2cmd.c" /* Standard loadware command definitions */
281#include "i2lib.c" /* High level interface services */
282
283/* Configuration area for modprobe */
284
285MODULE_AUTHOR("Doug McNash");
286MODULE_DESCRIPTION("Computone IntelliPort Plus Driver");
287MODULE_LICENSE("GPL");
288
289#define MAX_CMD_STR 50
290
291static int poll_only;
292static char cmd[MAX_CMD_STR];
293
294static int Eisa_irq;
295static int Eisa_slot;
296
297static int iindx;
298static char rirqs[IP2_MAX_BOARDS];
299static int Valid_Irqs[] = { 3, 4, 5, 7, 10, 11, 12, 15, 0};
300
301/* Note: Add compiled in defaults to these arrays, not to the structure
302 in ip2.h any longer. That structure WILL get overridden
303 by these values, or command line values, or insmod values!!! =mhw=
304*/
305static int io[IP2_MAX_BOARDS];
306static int irq[IP2_MAX_BOARDS] = { -1, -1, -1, -1 };
307
308MODULE_AUTHOR("Doug McNash");
309MODULE_DESCRIPTION("Computone IntelliPort Plus Driver");
310module_param_array(irq, int, NULL, 0);
311MODULE_PARM_DESC(irq, "Interrupts for IntelliPort Cards");
312module_param_array(io, int, NULL, 0);
313MODULE_PARM_DESC(io, "I/O ports for IntelliPort Cards");
314module_param(poll_only, bool, 0);
315MODULE_PARM_DESC(poll_only, "Do not use card interrupts");
316module_param_string(ip2, cmd, MAX_CMD_STR, 0);
317MODULE_PARM_DESC(ip2, "Contains module parameter passed with 'ip2='");
318
319/* for sysfs class support */
320static struct class *ip2_class;
321
322/* Some functions to keep track of what irqs we have */
323
324static int __init is_valid_irq(int irq)
325{
326 int *i = Valid_Irqs;
327
328 while (*i != 0 && *i != irq)
329 i++;
330
331 return *i;
332}
333
334static void __init mark_requested_irq(char irq)
335{
336 rirqs[iindx++] = irq;
337}
338
339static int __exit clear_requested_irq(char irq)
340{
341 int i;
342 for (i = 0; i < IP2_MAX_BOARDS; ++i) {
343 if (rirqs[i] == irq) {
344 rirqs[i] = 0;
345 return 1;
346 }
347 }
348 return 0;
349}
350
351static int have_requested_irq(char irq)
352{
353 /* array init to zeros so 0 irq will not be requested as a side
354 * effect */
355 int i;
356 for (i = 0; i < IP2_MAX_BOARDS; ++i)
357 if (rirqs[i] == irq)
358 return 1;
359 return 0;
360}
361
362/******************************************************************************/
363/* Function: cleanup_module() */
364/* Parameters: None */
365/* Returns: Nothing */
366/* */
367/* Description: */
368/* This is a required entry point for an installable module. It has to return */
369/* the device and the driver to a passive state. It should not be necessary */
370/* to reset the board fully, especially as the loadware is downloaded */
371/* externally rather than in the driver. We just want to disable the board */
372/* and clear the loadware to a reset state. To allow this there has to be a */
373/* way to detect whether the board has the loadware running at init time to */
374/* handle subsequent installations of the driver. All memory allocated by the */
375/* driver should be returned since it may be unloaded from memory. */
376/******************************************************************************/
377static void __exit ip2_cleanup_module(void)
378{
379 int err;
380 int i;
381
382 del_timer_sync(&PollTimer);
383
384 /* Reset the boards we have. */
385 for (i = 0; i < IP2_MAX_BOARDS; i++)
386 if (i2BoardPtrTable[i])
387 iiReset(i2BoardPtrTable[i]);
388
389 /* The following is done at most once, if any boards were installed. */
390 for (i = 0; i < IP2_MAX_BOARDS; i++) {
391 if (i2BoardPtrTable[i]) {
392 iiResetDelay(i2BoardPtrTable[i]);
393 /* free io addresses and Tibet */
394 release_region(ip2config.addr[i], 8);
395 device_destroy(ip2_class, MKDEV(IP2_IPL_MAJOR, 4 * i));
396 device_destroy(ip2_class, MKDEV(IP2_IPL_MAJOR,
397 4 * i + 1));
398 }
399 /* Disable and remove interrupt handler. */
400 if (ip2config.irq[i] > 0 &&
401 have_requested_irq(ip2config.irq[i])) {
402 free_irq(ip2config.irq[i], (void *)&pcName);
403 clear_requested_irq(ip2config.irq[i]);
404 }
405 }
406 class_destroy(ip2_class);
407 err = tty_unregister_driver(ip2_tty_driver);
408 if (err)
409 printk(KERN_ERR "IP2: failed to unregister tty driver (%d)\n",
410 err);
411 put_tty_driver(ip2_tty_driver);
412 unregister_chrdev(IP2_IPL_MAJOR, pcIpl);
413 remove_proc_entry("ip2mem", NULL);
414
415 /* free memory */
416 for (i = 0; i < IP2_MAX_BOARDS; i++) {
417 void *pB;
418#ifdef CONFIG_PCI
419 if (ip2config.type[i] == PCI && ip2config.pci_dev[i]) {
420 pci_disable_device(ip2config.pci_dev[i]);
421 pci_dev_put(ip2config.pci_dev[i]);
422 ip2config.pci_dev[i] = NULL;
423 }
424#endif
425 pB = i2BoardPtrTable[i];
426 if (pB != NULL) {
427 kfree(pB);
428 i2BoardPtrTable[i] = NULL;
429 }
430 if (DevTableMem[i] != NULL) {
431 kfree(DevTableMem[i]);
432 DevTableMem[i] = NULL;
433 }
434 }
435}
436module_exit(ip2_cleanup_module);
437
438static const struct tty_operations ip2_ops = {
439 .open = ip2_open,
440 .close = ip2_close,
441 .write = ip2_write,
442 .put_char = ip2_putchar,
443 .flush_chars = ip2_flush_chars,
444 .write_room = ip2_write_room,
445 .chars_in_buffer = ip2_chars_in_buf,
446 .flush_buffer = ip2_flush_buffer,
447 .ioctl = ip2_ioctl,
448 .throttle = ip2_throttle,
449 .unthrottle = ip2_unthrottle,
450 .set_termios = ip2_set_termios,
451 .set_ldisc = ip2_set_line_discipline,
452 .stop = ip2_stop,
453 .start = ip2_start,
454 .hangup = ip2_hangup,
455 .tiocmget = ip2_tiocmget,
456 .tiocmset = ip2_tiocmset,
457 .proc_fops = &ip2_proc_fops,
458};
459
460/******************************************************************************/
461/* Function: ip2_loadmain() */
462/* Parameters: irq, io from command line of insmod et. al. */
463/* pointer to fip firmware and firmware size for boards */
464/* Returns: Success (0) */
465/* */
466/* Description: */
467/* This was the required entry point for all drivers (now in ip2.c) */
468/* It performs all */
469/* initialisation of the devices and driver structures, and registers itself */
470/* with the relevant kernel modules. */
471/******************************************************************************/
472/* IRQF_DISABLED - if set blocks all interrupts else only this line */
473/* IRQF_SHARED - for shared irq PCI or maybe EISA only */
474/* SA_RANDOM - can be source for cert. random number generators */
475#define IP2_SA_FLAGS 0
476
477
478static const struct firmware *ip2_request_firmware(void)
479{
480 struct platform_device *pdev;
481 const struct firmware *fw;
482
483 pdev = platform_device_register_simple("ip2", 0, NULL, 0);
484 if (IS_ERR(pdev)) {
485 printk(KERN_ERR "Failed to register platform device for ip2\n");
486 return NULL;
487 }
488 if (request_firmware(&fw, "intelliport2.bin", &pdev->dev)) {
489 printk(KERN_ERR "Failed to load firmware 'intelliport2.bin'\n");
490 fw = NULL;
491 }
492 platform_device_unregister(pdev);
493 return fw;
494}
495
496/******************************************************************************
497 * ip2_setup:
498 * str: kernel command line string
499 *
500 * Can't autoprobe the boards so user must specify configuration on
501 * kernel command line. Sane people build it modular but the others
502 * come here.
503 *
504 * Alternating pairs of io,irq for up to 4 boards.
505 * ip2=io0,irq0,io1,irq1,io2,irq2,io3,irq3
506 *
507 * io=0 => No board
508 * io=1 => PCI
509 * io=2 => EISA
510 * else => ISA I/O address
511 *
512 * irq=0 or invalid for ISA will revert to polling mode
513 *
514 * Any value = -1, do not overwrite compiled in value.
515 *
516 ******************************************************************************/
517static int __init ip2_setup(char *str)
518{
519 int j, ints[10]; /* 4 boards, 2 parameters + 2 */
520 unsigned int i;
521
522 str = get_options(str, ARRAY_SIZE(ints), ints);
523
524 for (i = 0, j = 1; i < 4; i++) {
525 if (j > ints[0])
526 break;
527 if (ints[j] >= 0)
528 io[i] = ints[j];
529 j++;
530 if (j > ints[0])
531 break;
532 if (ints[j] >= 0)
533 irq[i] = ints[j];
534 j++;
535 }
536 return 1;
537}
538__setup("ip2=", ip2_setup);
539
540static int __init ip2_loadmain(void)
541{
542 int i, j, box;
543 int err = 0;
544 i2eBordStrPtr pB = NULL;
545 int rc = -1;
546 const struct firmware *fw = NULL;
547 char *str;
548
549 str = cmd;
550
551 if (poll_only) {
552 /* Hard lock the interrupts to zero */
553 irq[0] = irq[1] = irq[2] = irq[3] = poll_only = 0;
554 }
555
556 /* Check module parameter with 'ip2=' has been passed or not */
557 if (!poll_only && (!strncmp(str, "ip2=", 4)))
558 ip2_setup(str);
559
560 ip2trace(ITRC_NO_PORT, ITRC_INIT, ITRC_ENTER, 0);
561
562 /* process command line arguments to modprobe or
563 insmod i.e. iop & irqp */
564 /* irqp and iop should ALWAYS be specified now... But we check
565 them individually just to be sure, anyways... */
566 for (i = 0; i < IP2_MAX_BOARDS; ++i) {
567 ip2config.addr[i] = io[i];
568 if (irq[i] >= 0)
569 ip2config.irq[i] = irq[i];
570 else
571 ip2config.irq[i] = 0;
572 /* This is a little bit of a hack. If poll_only=1 on command
573 line back in ip2.c OR all IRQs on all specified boards are
574 explicitly set to 0, then drop to poll only mode and override
575 PCI or EISA interrupts. This superceeds the old hack of
576 triggering if all interrupts were zero (like da default).
577 Still a hack but less prone to random acts of terrorism.
578
579 What we really should do, now that the IRQ default is set
580 to -1, is to use 0 as a hard coded, do not probe.
581
582 /\/\|=mhw=|\/\/
583 */
584 poll_only |= irq[i];
585 }
586 poll_only = !poll_only;
587
588 /* Announce our presence */
589 printk(KERN_INFO "%s version %s\n", pcName, pcVersion);
590
591 ip2_tty_driver = alloc_tty_driver(IP2_MAX_PORTS);
592 if (!ip2_tty_driver)
593 return -ENOMEM;
594
595 /* Initialise all the boards we can find (up to the maximum). */
596 for (i = 0; i < IP2_MAX_BOARDS; ++i) {
597 switch (ip2config.addr[i]) {
598 case 0: /* skip this slot even if card is present */
599 break;
600 default: /* ISA */
601 /* ISA address must be specified */
602 if (ip2config.addr[i] < 0x100 ||
603 ip2config.addr[i] > 0x3f8) {
604 printk(KERN_ERR "IP2: Bad ISA board %d "
605 "address %x\n", i,
606 ip2config.addr[i]);
607 ip2config.addr[i] = 0;
608 break;
609 }
610 ip2config.type[i] = ISA;
611
612 /* Check for valid irq argument, set for polling if
613 * invalid */
614 if (ip2config.irq[i] &&
615 !is_valid_irq(ip2config.irq[i])) {
616 printk(KERN_ERR "IP2: Bad IRQ(%d) specified\n",
617 ip2config.irq[i]);
618 /* 0 is polling and is valid in that sense */
619 ip2config.irq[i] = 0;
620 }
621 break;
622 case PCI:
623#ifdef CONFIG_PCI
624 {
625 struct pci_dev *pdev = NULL;
626 u32 addr;
627 int status;
628
629 pdev = pci_get_device(PCI_VENDOR_ID_COMPUTONE,
630 PCI_DEVICE_ID_COMPUTONE_IP2EX, pdev);
631 if (pdev == NULL) {
632 ip2config.addr[i] = 0;
633 printk(KERN_ERR "IP2: PCI board %d not "
634 "found\n", i);
635 break;
636 }
637
638 if (pci_enable_device(pdev)) {
639 dev_err(&pdev->dev, "can't enable device\n");
640 goto out;
641 }
642 ip2config.type[i] = PCI;
643 ip2config.pci_dev[i] = pci_dev_get(pdev);
644 status = pci_read_config_dword(pdev, PCI_BASE_ADDRESS_1,
645 &addr);
646 if (addr & 1)
647 ip2config.addr[i] = (USHORT)(addr & 0xfffe);
648 else
649 dev_err(&pdev->dev, "I/O address error\n");
650
651 ip2config.irq[i] = pdev->irq;
652out:
653 pci_dev_put(pdev);
654 }
655#else
656 printk(KERN_ERR "IP2: PCI card specified but PCI "
657 "support not enabled.\n");
658 printk(KERN_ERR "IP2: Recompile kernel with CONFIG_PCI "
659 "defined!\n");
660#endif /* CONFIG_PCI */
661 break;
662 case EISA:
663 ip2config.addr[i] = find_eisa_board(Eisa_slot + 1);
664 if (ip2config.addr[i] != 0) {
665 /* Eisa_irq set as side effect, boo */
666 ip2config.type[i] = EISA;
667 }
668 ip2config.irq[i] = Eisa_irq;
669 break;
670 } /* switch */
671 } /* for */
672
673 for (i = 0; i < IP2_MAX_BOARDS; ++i) {
674 if (ip2config.addr[i]) {
675 pB = kzalloc(sizeof(i2eBordStr), GFP_KERNEL);
676 if (pB) {
677 i2BoardPtrTable[i] = pB;
678 iiSetAddress(pB, ip2config.addr[i],
679 ii2DelayTimer);
680 iiReset(pB);
681 } else
682 printk(KERN_ERR "IP2: board memory allocation "
683 "error\n");
684 }
685 }
686 for (i = 0; i < IP2_MAX_BOARDS; ++i) {
687 pB = i2BoardPtrTable[i];
688 if (pB != NULL) {
689 iiResetDelay(pB);
690 break;
691 }
692 }
693 for (i = 0; i < IP2_MAX_BOARDS; ++i) {
694 /* We don't want to request the firmware unless we have at
695 least one board */
696 if (i2BoardPtrTable[i] != NULL) {
697 if (!fw)
698 fw = ip2_request_firmware();
699 if (!fw)
700 break;
701 ip2_init_board(i, fw);
702 }
703 }
704 if (fw)
705 release_firmware(fw);
706
707 ip2trace(ITRC_NO_PORT, ITRC_INIT, 2, 0);
708
709 ip2_tty_driver->owner = THIS_MODULE;
710 ip2_tty_driver->name = "ttyF";
711 ip2_tty_driver->driver_name = pcDriver_name;
712 ip2_tty_driver->major = IP2_TTY_MAJOR;
713 ip2_tty_driver->minor_start = 0;
714 ip2_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
715 ip2_tty_driver->subtype = SERIAL_TYPE_NORMAL;
716 ip2_tty_driver->init_termios = tty_std_termios;
717 ip2_tty_driver->init_termios.c_cflag = B9600|CS8|CREAD|HUPCL|CLOCAL;
718 ip2_tty_driver->flags = TTY_DRIVER_REAL_RAW |
719 TTY_DRIVER_DYNAMIC_DEV;
720 tty_set_operations(ip2_tty_driver, &ip2_ops);
721
722 ip2trace(ITRC_NO_PORT, ITRC_INIT, 3, 0);
723
724 err = tty_register_driver(ip2_tty_driver);
725 if (err) {
726 printk(KERN_ERR "IP2: failed to register tty driver\n");
727 put_tty_driver(ip2_tty_driver);
728 return err; /* leaking resources */
729 }
730
731 err = register_chrdev(IP2_IPL_MAJOR, pcIpl, &ip2_ipl);
732 if (err) {
733 printk(KERN_ERR "IP2: failed to register IPL device (%d)\n",
734 err);
735 } else {
736 /* create the sysfs class */
737 ip2_class = class_create(THIS_MODULE, "ip2");
738 if (IS_ERR(ip2_class)) {
739 err = PTR_ERR(ip2_class);
740 goto out_chrdev;
741 }
742 }
743 /* Register the read_procmem thing */
744 if (!proc_create("ip2mem",0,NULL,&ip2mem_proc_fops)) {
745 printk(KERN_ERR "IP2: failed to register read_procmem\n");
746 return -EIO; /* leaking resources */
747 }
748
749 ip2trace(ITRC_NO_PORT, ITRC_INIT, 4, 0);
750 /* Register the interrupt handler or poll handler, depending upon the
751 * specified interrupt.
752 */
753
754 for (i = 0; i < IP2_MAX_BOARDS; ++i) {
755 if (ip2config.addr[i] == 0)
756 continue;
757
758 pB = i2BoardPtrTable[i];
759 if (pB != NULL) {
760 device_create(ip2_class, NULL,
761 MKDEV(IP2_IPL_MAJOR, 4 * i),
762 NULL, "ipl%d", i);
763 device_create(ip2_class, NULL,
764 MKDEV(IP2_IPL_MAJOR, 4 * i + 1),
765 NULL, "stat%d", i);
766
767 for (box = 0; box < ABS_MAX_BOXES; box++)
768 for (j = 0; j < ABS_BIGGEST_BOX; j++)
769 if (pB->i2eChannelMap[box] & (1 << j))
770 tty_register_device(
771 ip2_tty_driver,
772 j + ABS_BIGGEST_BOX *
773 (box+i*ABS_MAX_BOXES),
774 NULL);
775 }
776
777 if (poll_only) {
778 /* Poll only forces driver to only use polling and
779 to ignore the probed PCI or EISA interrupts. */
780 ip2config.irq[i] = CIR_POLL;
781 }
782 if (ip2config.irq[i] == CIR_POLL) {
783retry:
784 if (!timer_pending(&PollTimer)) {
785 mod_timer(&PollTimer, POLL_TIMEOUT);
786 printk(KERN_INFO "IP2: polling\n");
787 }
788 } else {
789 if (have_requested_irq(ip2config.irq[i]))
790 continue;
791 rc = request_irq(ip2config.irq[i], ip2_interrupt,
792 IP2_SA_FLAGS |
793 (ip2config.type[i] == PCI ? IRQF_SHARED : 0),
794 pcName, i2BoardPtrTable[i]);
795 if (rc) {
796 printk(KERN_ERR "IP2: request_irq failed: "
797 "error %d\n", rc);
798 ip2config.irq[i] = CIR_POLL;
799 printk(KERN_INFO "IP2: Polling %ld/sec.\n",
800 (POLL_TIMEOUT - jiffies));
801 goto retry;
802 }
803 mark_requested_irq(ip2config.irq[i]);
804 /* Initialise the interrupt handler bottom half
805 * (aka slih). */
806 }
807 }
808
809 for (i = 0; i < IP2_MAX_BOARDS; ++i) {
810 if (i2BoardPtrTable[i]) {
811 /* set and enable board interrupt */
812 set_irq(i, ip2config.irq[i]);
813 }
814 }
815
816 ip2trace(ITRC_NO_PORT, ITRC_INIT, ITRC_RETURN, 0);
817
818 return 0;
819
820out_chrdev:
821 unregister_chrdev(IP2_IPL_MAJOR, "ip2");
822 /* unregister and put tty here */
823 return err;
824}
825module_init(ip2_loadmain);
826
827/******************************************************************************/
828/* Function: ip2_init_board() */
829/* Parameters: Index of board in configuration structure */
830/* Returns: Success (0) */
831/* */
832/* Description: */
833/* This function initializes the specified board. The loadware is copied to */
834/* the board, the channel structures are initialized, and the board details */
835/* are reported on the console. */
836/******************************************************************************/
837static void
838ip2_init_board(int boardnum, const struct firmware *fw)
839{
840 int i;
841 int nports = 0, nboxes = 0;
842 i2ChanStrPtr pCh;
843 i2eBordStrPtr pB = i2BoardPtrTable[boardnum];
844
845 if ( !iiInitialize ( pB ) ) {
846 printk ( KERN_ERR "IP2: Failed to initialize board at 0x%x, error %d\n",
847 pB->i2eBase, pB->i2eError );
848 goto err_initialize;
849 }
850 printk(KERN_INFO "IP2: Board %d: addr=0x%x irq=%d\n", boardnum + 1,
851 ip2config.addr[boardnum], ip2config.irq[boardnum] );
852
853 if (!request_region( ip2config.addr[boardnum], 8, pcName )) {
854 printk(KERN_ERR "IP2: bad addr=0x%x\n", ip2config.addr[boardnum]);
855 goto err_initialize;
856 }
857
858 if ( iiDownloadAll ( pB, (loadHdrStrPtr)fw->data, 1, fw->size )
859 != II_DOWN_GOOD ) {
860 printk ( KERN_ERR "IP2: failed to download loadware\n" );
861 goto err_release_region;
862 } else {
863 printk ( KERN_INFO "IP2: fv=%d.%d.%d lv=%d.%d.%d\n",
864 pB->i2ePom.e.porVersion,
865 pB->i2ePom.e.porRevision,
866 pB->i2ePom.e.porSubRev, pB->i2eLVersion,
867 pB->i2eLRevision, pB->i2eLSub );
868 }
869
870 switch ( pB->i2ePom.e.porID & ~POR_ID_RESERVED ) {
871
872 default:
873 printk( KERN_ERR "IP2: Unknown board type, ID = %x\n",
874 pB->i2ePom.e.porID );
875 nports = 0;
876 goto err_release_region;
877 break;
878
879 case POR_ID_II_4: /* IntelliPort-II, ISA-4 (4xRJ45) */
880 printk ( KERN_INFO "IP2: ISA-4\n" );
881 nports = 4;
882 break;
883
884 case POR_ID_II_8: /* IntelliPort-II, 8-port using standard brick. */
885 printk ( KERN_INFO "IP2: ISA-8 std\n" );
886 nports = 8;
887 break;
888
889 case POR_ID_II_8R: /* IntelliPort-II, 8-port using RJ11's (no CTS) */
890 printk ( KERN_INFO "IP2: ISA-8 RJ11\n" );
891 nports = 8;
892 break;
893
894 case POR_ID_FIIEX: /* IntelliPort IIEX */
895 {
896 int portnum = IP2_PORTS_PER_BOARD * boardnum;
897 int box;
898
899 for( box = 0; box < ABS_MAX_BOXES; ++box ) {
900 if ( pB->i2eChannelMap[box] != 0 ) {
901 ++nboxes;
902 }
903 for( i = 0; i < ABS_BIGGEST_BOX; ++i ) {
904 if ( pB->i2eChannelMap[box] & 1<< i ) {
905 ++nports;
906 }
907 }
908 }
909 DevTableMem[boardnum] = pCh =
910 kmalloc( sizeof(i2ChanStr) * nports, GFP_KERNEL );
911 if ( !pCh ) {
912 printk ( KERN_ERR "IP2: (i2_init_channel:) Out of memory.\n");
913 goto err_release_region;
914 }
915 if ( !i2InitChannels( pB, nports, pCh ) ) {
916 printk(KERN_ERR "IP2: i2InitChannels failed: %d\n",pB->i2eError);
917 kfree ( pCh );
918 goto err_release_region;
919 }
920 pB->i2eChannelPtr = &DevTable[portnum];
921 pB->i2eChannelCnt = ABS_MOST_PORTS;
922
923 for( box = 0; box < ABS_MAX_BOXES; ++box, portnum += ABS_BIGGEST_BOX ) {
924 for( i = 0; i < ABS_BIGGEST_BOX; ++i ) {
925 if ( pB->i2eChannelMap[box] & (1 << i) ) {
926 DevTable[portnum + i] = pCh;
927 pCh->port_index = portnum + i;
928 pCh++;
929 }
930 }
931 }
932 printk(KERN_INFO "IP2: EX box=%d ports=%d %d bit\n",
933 nboxes, nports, pB->i2eDataWidth16 ? 16 : 8 );
934 }
935 goto ex_exit;
936 }
937 DevTableMem[boardnum] = pCh =
938 kmalloc ( sizeof (i2ChanStr) * nports, GFP_KERNEL );
939 if ( !pCh ) {
940 printk ( KERN_ERR "IP2: (i2_init_channel:) Out of memory.\n");
941 goto err_release_region;
942 }
943 pB->i2eChannelPtr = pCh;
944 pB->i2eChannelCnt = nports;
945 if ( !i2InitChannels( pB, nports, pCh ) ) {
946 printk(KERN_ERR "IP2: i2InitChannels failed: %d\n",pB->i2eError);
947 kfree ( pCh );
948 goto err_release_region;
949 }
950 pB->i2eChannelPtr = &DevTable[IP2_PORTS_PER_BOARD * boardnum];
951
952 for( i = 0; i < pB->i2eChannelCnt; ++i ) {
953 DevTable[IP2_PORTS_PER_BOARD * boardnum + i] = pCh;
954 pCh->port_index = (IP2_PORTS_PER_BOARD * boardnum) + i;
955 pCh++;
956 }
957ex_exit:
958 INIT_WORK(&pB->tqueue_interrupt, ip2_interrupt_bh);
959 return;
960
961err_release_region:
962 release_region(ip2config.addr[boardnum], 8);
963err_initialize:
964 kfree ( pB );
965 i2BoardPtrTable[boardnum] = NULL;
966 return;
967}
968
969/******************************************************************************/
970/* Function: find_eisa_board ( int start_slot ) */
971/* Parameters: First slot to check */
972/* Returns: Address of EISA IntelliPort II controller */
973/* */
974/* Description: */
975/* This function searches for an EISA IntelliPort controller, starting */
976/* from the specified slot number. If the motherboard is not identified as an */
977/* EISA motherboard, or no valid board ID is selected it returns 0. Otherwise */
978/* it returns the base address of the controller. */
979/******************************************************************************/
980static unsigned short
981find_eisa_board( int start_slot )
982{
983 int i, j;
984 unsigned int idm = 0;
985 unsigned int idp = 0;
986 unsigned int base = 0;
987 unsigned int value;
988 int setup_address;
989 int setup_irq;
990 int ismine = 0;
991
992 /*
993 * First a check for an EISA motherboard, which we do by comparing the
994 * EISA ID registers for the system board and the first couple of slots.
995 * No slot ID should match the system board ID, but on an ISA or PCI
996 * machine the odds are that an empty bus will return similar values for
997 * each slot.
998 */
999 i = 0x0c80;
1000 value = (inb(i) << 24) + (inb(i+1) << 16) + (inb(i+2) << 8) + inb(i+3);
1001 for( i = 0x1c80; i <= 0x4c80; i += 0x1000 ) {
1002 j = (inb(i)<<24)+(inb(i+1)<<16)+(inb(i+2)<<8)+inb(i+3);
1003 if ( value == j )
1004 return 0;
1005 }
1006
1007 /*
1008 * OK, so we are inclined to believe that this is an EISA machine. Find
1009 * an IntelliPort controller.
1010 */
1011 for( i = start_slot; i < 16; i++ ) {
1012 base = i << 12;
1013 idm = (inb(base + 0xc80) << 8) | (inb(base + 0xc81) & 0xff);
1014 idp = (inb(base + 0xc82) << 8) | (inb(base + 0xc83) & 0xff);
1015 ismine = 0;
1016 if ( idm == 0x0e8e ) {
1017 if ( idp == 0x0281 || idp == 0x0218 ) {
1018 ismine = 1;
1019 } else if ( idp == 0x0282 || idp == 0x0283 ) {
1020 ismine = 3; /* Can do edge-trigger */
1021 }
1022 if ( ismine ) {
1023 Eisa_slot = i;
1024 break;
1025 }
1026 }
1027 }
1028 if ( !ismine )
1029 return 0;
1030
1031 /* It's some sort of EISA card, but at what address is it configured? */
1032
1033 setup_address = base + 0xc88;
1034 value = inb(base + 0xc86);
1035 setup_irq = (value & 8) ? Valid_Irqs[value & 7] : 0;
1036
1037 if ( (ismine & 2) && !(value & 0x10) ) {
1038 ismine = 1; /* Could be edging, but not */
1039 }
1040
1041 if ( Eisa_irq == 0 ) {
1042 Eisa_irq = setup_irq;
1043 } else if ( Eisa_irq != setup_irq ) {
1044 printk ( KERN_ERR "IP2: EISA irq mismatch between EISA controllers\n" );
1045 }
1046
1047#ifdef IP2DEBUG_INIT
1048printk(KERN_DEBUG "Computone EISA board in slot %d, I.D. 0x%x%x, Address 0x%x",
1049 base >> 12, idm, idp, setup_address);
1050 if ( Eisa_irq ) {
1051 printk(KERN_DEBUG ", Interrupt %d %s\n",
1052 setup_irq, (ismine & 2) ? "(edge)" : "(level)");
1053 } else {
1054 printk(KERN_DEBUG ", (polled)\n");
1055 }
1056#endif
1057 return setup_address;
1058}
1059
1060/******************************************************************************/
1061/* Function: set_irq() */
1062/* Parameters: index to board in board table */
1063/* IRQ to use */
1064/* Returns: Success (0) */
1065/* */
1066/* Description: */
1067/******************************************************************************/
1068static void
1069set_irq( int boardnum, int boardIrq )
1070{
1071 unsigned char tempCommand[16];
1072 i2eBordStrPtr pB = i2BoardPtrTable[boardnum];
1073 unsigned long flags;
1074
1075 /*
1076 * Notify the boards they may generate interrupts. This is done by
1077 * sending an in-line command to channel 0 on each board. This is why
1078 * the channels have to be defined already. For each board, if the
1079 * interrupt has never been defined, we must do so NOW, directly, since
1080 * board will not send flow control or even give an interrupt until this
1081 * is done. If polling we must send 0 as the interrupt parameter.
1082 */
1083
1084 // We will get an interrupt here at the end of this function
1085
1086 iiDisableMailIrq(pB);
1087
1088 /* We build up the entire packet header. */
1089 CHANNEL_OF(tempCommand) = 0;
1090 PTYPE_OF(tempCommand) = PTYPE_INLINE;
1091 CMD_COUNT_OF(tempCommand) = 2;
1092 (CMD_OF(tempCommand))[0] = CMDVALUE_IRQ;
1093 (CMD_OF(tempCommand))[1] = boardIrq;
1094 /*
1095 * Write to FIFO; don't bother to adjust fifo capacity for this, since
1096 * board will respond almost immediately after SendMail hit.
1097 */
1098 write_lock_irqsave(&pB->write_fifo_spinlock, flags);
1099 iiWriteBuf(pB, tempCommand, 4);
1100 write_unlock_irqrestore(&pB->write_fifo_spinlock, flags);
1101 pB->i2eUsingIrq = boardIrq;
1102 pB->i2eOutMailWaiting |= MB_OUT_STUFFED;
1103
1104 /* Need to update number of boards before you enable mailbox int */
1105 ++i2nBoards;
1106
1107 CHANNEL_OF(tempCommand) = 0;
1108 PTYPE_OF(tempCommand) = PTYPE_BYPASS;
1109 CMD_COUNT_OF(tempCommand) = 6;
1110 (CMD_OF(tempCommand))[0] = 88; // SILO
1111 (CMD_OF(tempCommand))[1] = 64; // chars
1112 (CMD_OF(tempCommand))[2] = 32; // ms
1113
1114 (CMD_OF(tempCommand))[3] = 28; // MAX_BLOCK
1115 (CMD_OF(tempCommand))[4] = 64; // chars
1116
1117 (CMD_OF(tempCommand))[5] = 87; // HW_TEST
1118 write_lock_irqsave(&pB->write_fifo_spinlock, flags);
1119 iiWriteBuf(pB, tempCommand, 8);
1120 write_unlock_irqrestore(&pB->write_fifo_spinlock, flags);
1121
1122 CHANNEL_OF(tempCommand) = 0;
1123 PTYPE_OF(tempCommand) = PTYPE_BYPASS;
1124 CMD_COUNT_OF(tempCommand) = 1;
1125 (CMD_OF(tempCommand))[0] = 84; /* get BOX_IDS */
1126 iiWriteBuf(pB, tempCommand, 3);
1127
1128#ifdef XXX
1129 // enable heartbeat for test porpoises
1130 CHANNEL_OF(tempCommand) = 0;
1131 PTYPE_OF(tempCommand) = PTYPE_BYPASS;
1132 CMD_COUNT_OF(tempCommand) = 2;
1133 (CMD_OF(tempCommand))[0] = 44; /* get ping */
1134 (CMD_OF(tempCommand))[1] = 200; /* 200 ms */
1135 write_lock_irqsave(&pB->write_fifo_spinlock, flags);
1136 iiWriteBuf(pB, tempCommand, 4);
1137 write_unlock_irqrestore(&pB->write_fifo_spinlock, flags);
1138#endif
1139
1140 iiEnableMailIrq(pB);
1141 iiSendPendingMail(pB);
1142}
1143
1144/******************************************************************************/
1145/* Interrupt Handler Section */
1146/******************************************************************************/
1147
1148static inline void
1149service_all_boards(void)
1150{
1151 int i;
1152 i2eBordStrPtr pB;
1153
1154 /* Service every board on the list */
1155 for( i = 0; i < IP2_MAX_BOARDS; ++i ) {
1156 pB = i2BoardPtrTable[i];
1157 if ( pB ) {
1158 i2ServiceBoard( pB );
1159 }
1160 }
1161}
1162
1163
1164/******************************************************************************/
1165/* Function: ip2_interrupt_bh(work) */
1166/* Parameters: work - pointer to the board structure */
1167/* Returns: Nothing */
1168/* */
1169/* Description: */
1170/* Service the board in a bottom half interrupt handler and then */
1171/* reenable the board's interrupts if it has an IRQ number */
1172/* */
1173/******************************************************************************/
1174static void
1175ip2_interrupt_bh(struct work_struct *work)
1176{
1177 i2eBordStrPtr pB = container_of(work, i2eBordStr, tqueue_interrupt);
1178// pB better well be set or we have a problem! We can only get
1179// here from the IMMEDIATE queue. Here, we process the boards.
1180// Checking pB doesn't cost much and it saves us from the sanity checkers.
1181
1182 bh_counter++;
1183
1184 if ( pB ) {
1185 i2ServiceBoard( pB );
1186 if( pB->i2eUsingIrq ) {
1187// Re-enable his interrupts
1188 iiEnableMailIrq(pB);
1189 }
1190 }
1191}
1192
1193
1194/******************************************************************************/
1195/* Function: ip2_interrupt(int irq, void *dev_id) */
1196/* Parameters: irq - interrupt number */
1197/* pointer to optional device ID structure */
1198/* Returns: Nothing */
1199/* */
1200/* Description: */
1201/* */
1202/* Our task here is simply to identify each board which needs servicing. */
1203/* If we are queuing then, queue it to be serviced, and disable its irq */
1204/* mask otherwise process the board directly. */
1205/* */
1206/* We could queue by IRQ but that just complicates things on both ends */
1207/* with very little gain in performance (how many instructions does */
1208/* it take to iterate on the immediate queue). */
1209/* */
1210/* */
1211/******************************************************************************/
1212static void
1213ip2_irq_work(i2eBordStrPtr pB)
1214{
1215#ifdef USE_IQI
1216 if (NO_MAIL_HERE != ( pB->i2eStartMail = iiGetMail(pB))) {
1217// Disable his interrupt (will be enabled when serviced)
1218// This is mostly to protect from reentrancy.
1219 iiDisableMailIrq(pB);
1220
1221// Park the board on the immediate queue for processing.
1222 schedule_work(&pB->tqueue_interrupt);
1223
1224// Make sure the immediate queue is flagged to fire.
1225 }
1226#else
1227
1228// We are using immediate servicing here. This sucks and can
1229// cause all sorts of havoc with ppp and others. The failsafe
1230// check on iiSendPendingMail could also throw a hairball.
1231
1232 i2ServiceBoard( pB );
1233
1234#endif /* USE_IQI */
1235}
1236
1237static void
1238ip2_polled_interrupt(void)
1239{
1240 int i;
1241 i2eBordStrPtr pB;
1242
1243 ip2trace(ITRC_NO_PORT, ITRC_INTR, 99, 1, 0);
1244
1245 /* Service just the boards on the list using this irq */
1246 for( i = 0; i < i2nBoards; ++i ) {
1247 pB = i2BoardPtrTable[i];
1248
1249// Only process those boards which match our IRQ.
1250// IRQ = 0 for polled boards, we won't poll "IRQ" boards
1251
1252 if (pB && pB->i2eUsingIrq == 0)
1253 ip2_irq_work(pB);
1254 }
1255
1256 ++irq_counter;
1257
1258 ip2trace (ITRC_NO_PORT, ITRC_INTR, ITRC_RETURN, 0 );
1259}
1260
1261static irqreturn_t
1262ip2_interrupt(int irq, void *dev_id)
1263{
1264 i2eBordStrPtr pB = dev_id;
1265
1266 ip2trace (ITRC_NO_PORT, ITRC_INTR, 99, 1, pB->i2eUsingIrq );
1267
1268 ip2_irq_work(pB);
1269
1270 ++irq_counter;
1271
1272 ip2trace (ITRC_NO_PORT, ITRC_INTR, ITRC_RETURN, 0 );
1273 return IRQ_HANDLED;
1274}
1275
1276/******************************************************************************/
1277/* Function: ip2_poll(unsigned long arg) */
1278/* Parameters: ? */
1279/* Returns: Nothing */
1280/* */
1281/* Description: */
1282/* This function calls the library routine i2ServiceBoard for each board in */
1283/* the board table. This is used instead of the interrupt routine when polled */
1284/* mode is specified. */
1285/******************************************************************************/
1286static void
1287ip2_poll(unsigned long arg)
1288{
1289 ip2trace (ITRC_NO_PORT, ITRC_INTR, 100, 0 );
1290
1291 // Just polled boards, IRQ = 0 will hit all non-interrupt boards.
1292 // It will NOT poll boards handled by hard interrupts.
1293 // The issue of queued BH interrupts is handled in ip2_interrupt().
1294 ip2_polled_interrupt();
1295
1296 mod_timer(&PollTimer, POLL_TIMEOUT);
1297
1298 ip2trace (ITRC_NO_PORT, ITRC_INTR, ITRC_RETURN, 0 );
1299}
1300
1301static void do_input(struct work_struct *work)
1302{
1303 i2ChanStrPtr pCh = container_of(work, i2ChanStr, tqueue_input);
1304 unsigned long flags;
1305
1306 ip2trace(CHANN, ITRC_INPUT, 21, 0 );
1307
1308 // Data input
1309 if ( pCh->pTTY != NULL ) {
1310 read_lock_irqsave(&pCh->Ibuf_spinlock, flags);
1311 if (!pCh->throttled && (pCh->Ibuf_stuff != pCh->Ibuf_strip)) {
1312 read_unlock_irqrestore(&pCh->Ibuf_spinlock, flags);
1313 i2Input( pCh );
1314 } else
1315 read_unlock_irqrestore(&pCh->Ibuf_spinlock, flags);
1316 } else {
1317 ip2trace(CHANN, ITRC_INPUT, 22, 0 );
1318
1319 i2InputFlush( pCh );
1320 }
1321}
1322
1323// code duplicated from n_tty (ldisc)
1324static inline void isig(int sig, struct tty_struct *tty, int flush)
1325{
1326 /* FIXME: This is completely bogus */
1327 if (tty->pgrp)
1328 kill_pgrp(tty->pgrp, sig, 1);
1329 if (flush || !L_NOFLSH(tty)) {
1330 if ( tty->ldisc->ops->flush_buffer )
1331 tty->ldisc->ops->flush_buffer(tty);
1332 i2InputFlush( tty->driver_data );
1333 }
1334}
1335
1336static void do_status(struct work_struct *work)
1337{
1338 i2ChanStrPtr pCh = container_of(work, i2ChanStr, tqueue_status);
1339 int status;
1340
1341 status = i2GetStatus( pCh, (I2_BRK|I2_PAR|I2_FRA|I2_OVR) );
1342
1343 ip2trace (CHANN, ITRC_STATUS, 21, 1, status );
1344
1345 if (pCh->pTTY && (status & (I2_BRK|I2_PAR|I2_FRA|I2_OVR)) ) {
1346 if ( (status & I2_BRK) ) {
1347 // code duplicated from n_tty (ldisc)
1348 if (I_IGNBRK(pCh->pTTY))
1349 goto skip_this;
1350 if (I_BRKINT(pCh->pTTY)) {
1351 isig(SIGINT, pCh->pTTY, 1);
1352 goto skip_this;
1353 }
1354 wake_up_interruptible(&pCh->pTTY->read_wait);
1355 }
1356#ifdef NEVER_HAPPENS_AS_SETUP_XXX
1357 // and can't work because we don't know the_char
1358 // as the_char is reported on a separate path
1359 // The intelligent board does this stuff as setup
1360 {
1361 char brkf = TTY_NORMAL;
1362 unsigned char brkc = '\0';
1363 unsigned char tmp;
1364 if ( (status & I2_BRK) ) {
1365 brkf = TTY_BREAK;
1366 brkc = '\0';
1367 }
1368 else if (status & I2_PAR) {
1369 brkf = TTY_PARITY;
1370 brkc = the_char;
1371 } else if (status & I2_FRA) {
1372 brkf = TTY_FRAME;
1373 brkc = the_char;
1374 } else if (status & I2_OVR) {
1375 brkf = TTY_OVERRUN;
1376 brkc = the_char;
1377 }
1378 tmp = pCh->pTTY->real_raw;
1379 pCh->pTTY->real_raw = 0;
1380 pCh->pTTY->ldisc->ops.receive_buf( pCh->pTTY, &brkc, &brkf, 1 );
1381 pCh->pTTY->real_raw = tmp;
1382 }
1383#endif /* NEVER_HAPPENS_AS_SETUP_XXX */
1384 }
1385skip_this:
1386
1387 if ( status & (I2_DDCD | I2_DDSR | I2_DCTS | I2_DRI) ) {
1388 wake_up_interruptible(&pCh->delta_msr_wait);
1389
1390 if ( (pCh->flags & ASYNC_CHECK_CD) && (status & I2_DDCD) ) {
1391 if ( status & I2_DCD ) {
1392 if ( pCh->wopen ) {
1393 wake_up_interruptible ( &pCh->open_wait );
1394 }
1395 } else {
1396 if (pCh->pTTY && (!(pCh->pTTY->termios->c_cflag & CLOCAL)) ) {
1397 tty_hangup( pCh->pTTY );
1398 }
1399 }
1400 }
1401 }
1402
1403 ip2trace (CHANN, ITRC_STATUS, 26, 0 );
1404}
1405
1406/******************************************************************************/
1407/* Device Open/Close/Ioctl Entry Point Section */
1408/******************************************************************************/
1409
1410/******************************************************************************/
1411/* Function: open_sanity_check() */
1412/* Parameters: Pointer to tty structure */
1413/* Pointer to file structure */
1414/* Returns: Success or failure */
1415/* */
1416/* Description: */
1417/* Verifies the structure magic numbers and cross links. */
1418/******************************************************************************/
1419#ifdef IP2DEBUG_OPEN
1420static void
1421open_sanity_check( i2ChanStrPtr pCh, i2eBordStrPtr pBrd )
1422{
1423 if ( pBrd->i2eValid != I2E_MAGIC ) {
1424 printk(KERN_ERR "IP2: invalid board structure\n" );
1425 } else if ( pBrd != pCh->pMyBord ) {
1426 printk(KERN_ERR "IP2: board structure pointer mismatch (%p)\n",
1427 pCh->pMyBord );
1428 } else if ( pBrd->i2eChannelCnt < pCh->port_index ) {
1429 printk(KERN_ERR "IP2: bad device index (%d)\n", pCh->port_index );
1430 } else if (&((i2ChanStrPtr)pBrd->i2eChannelPtr)[pCh->port_index] != pCh) {
1431 } else {
1432 printk(KERN_INFO "IP2: all pointers check out!\n" );
1433 }
1434}
1435#endif
1436
1437
1438/******************************************************************************/
1439/* Function: ip2_open() */
1440/* Parameters: Pointer to tty structure */
1441/* Pointer to file structure */
1442/* Returns: Success or failure */
1443/* */
1444/* Description: (MANDATORY) */
1445/* A successful device open has to run a gauntlet of checks before it */
1446/* completes. After some sanity checking and pointer setup, the function */
1447/* blocks until all conditions are satisfied. It then initialises the port to */
1448/* the default characteristics and returns. */
1449/******************************************************************************/
1450static int
1451ip2_open( PTTY tty, struct file *pFile )
1452{
1453 wait_queue_t wait;
1454 int rc = 0;
1455 int do_clocal = 0;
1456 i2ChanStrPtr pCh = DevTable[tty->index];
1457
1458 ip2trace (tty->index, ITRC_OPEN, ITRC_ENTER, 0 );
1459
1460 if ( pCh == NULL ) {
1461 return -ENODEV;
1462 }
1463 /* Setup pointer links in device and tty structures */
1464 pCh->pTTY = tty;
1465 tty->driver_data = pCh;
1466
1467#ifdef IP2DEBUG_OPEN
1468 printk(KERN_DEBUG \
1469 "IP2:open(tty=%p,pFile=%p):dev=%s,ch=%d,idx=%d\n",
1470 tty, pFile, tty->name, pCh->infl.hd.i2sChannel, pCh->port_index);
1471 open_sanity_check ( pCh, pCh->pMyBord );
1472#endif
1473
1474 i2QueueCommands(PTYPE_INLINE, pCh, 100, 3, CMD_DTRUP,CMD_RTSUP,CMD_DCD_REP);
1475 pCh->dataSetOut |= (I2_DTR | I2_RTS);
1476 serviceOutgoingFifo( pCh->pMyBord );
1477
1478 /* Block here until the port is ready (per serial and istallion) */
1479 /*
1480 * 1. If the port is in the middle of closing wait for the completion
1481 * and then return the appropriate error.
1482 */
1483 init_waitqueue_entry(&wait, current);
1484 add_wait_queue(&pCh->close_wait, &wait);
1485 set_current_state( TASK_INTERRUPTIBLE );
1486
1487 if ( tty_hung_up_p(pFile) || ( pCh->flags & ASYNC_CLOSING )) {
1488 if ( pCh->flags & ASYNC_CLOSING ) {
1489 tty_unlock();
1490 schedule();
1491 tty_lock();
1492 }
1493 if ( tty_hung_up_p(pFile) ) {
1494 set_current_state( TASK_RUNNING );
1495 remove_wait_queue(&pCh->close_wait, &wait);
1496 return( pCh->flags & ASYNC_HUP_NOTIFY ) ? -EAGAIN : -ERESTARTSYS;
1497 }
1498 }
1499 set_current_state( TASK_RUNNING );
1500 remove_wait_queue(&pCh->close_wait, &wait);
1501
1502 /*
1503 * 3. Handle a non-blocking open of a normal port.
1504 */
1505 if ( (pFile->f_flags & O_NONBLOCK) || (tty->flags & (1<<TTY_IO_ERROR) )) {
1506 pCh->flags |= ASYNC_NORMAL_ACTIVE;
1507 goto noblock;
1508 }
1509 /*
1510 * 4. Now loop waiting for the port to be free and carrier present
1511 * (if required).
1512 */
1513 if ( tty->termios->c_cflag & CLOCAL )
1514 do_clocal = 1;
1515
1516#ifdef IP2DEBUG_OPEN
1517 printk(KERN_DEBUG "OpenBlock: do_clocal = %d\n", do_clocal);
1518#endif
1519
1520 ++pCh->wopen;
1521
1522 init_waitqueue_entry(&wait, current);
1523 add_wait_queue(&pCh->open_wait, &wait);
1524
1525 for(;;) {
1526 i2QueueCommands(PTYPE_INLINE, pCh, 100, 2, CMD_DTRUP, CMD_RTSUP);
1527 pCh->dataSetOut |= (I2_DTR | I2_RTS);
1528 set_current_state( TASK_INTERRUPTIBLE );
1529 serviceOutgoingFifo( pCh->pMyBord );
1530 if ( tty_hung_up_p(pFile) ) {
1531 set_current_state( TASK_RUNNING );
1532 remove_wait_queue(&pCh->open_wait, &wait);
1533 return ( pCh->flags & ASYNC_HUP_NOTIFY ) ? -EBUSY : -ERESTARTSYS;
1534 }
1535 if (!(pCh->flags & ASYNC_CLOSING) &&
1536 (do_clocal || (pCh->dataSetIn & I2_DCD) )) {
1537 rc = 0;
1538 break;
1539 }
1540
1541#ifdef IP2DEBUG_OPEN
1542 printk(KERN_DEBUG "ASYNC_CLOSING = %s\n",
1543 (pCh->flags & ASYNC_CLOSING)?"True":"False");
1544 printk(KERN_DEBUG "OpenBlock: waiting for CD or signal\n");
1545#endif
1546 ip2trace (CHANN, ITRC_OPEN, 3, 2, 0,
1547 (pCh->flags & ASYNC_CLOSING) );
1548 /* check for signal */
1549 if (signal_pending(current)) {
1550 rc = (( pCh->flags & ASYNC_HUP_NOTIFY ) ? -EAGAIN : -ERESTARTSYS);
1551 break;
1552 }
1553 tty_unlock();
1554 schedule();
1555 tty_lock();
1556 }
1557 set_current_state( TASK_RUNNING );
1558 remove_wait_queue(&pCh->open_wait, &wait);
1559
1560 --pCh->wopen; //why count?
1561
1562 ip2trace (CHANN, ITRC_OPEN, 4, 0 );
1563
1564 if (rc != 0 ) {
1565 return rc;
1566 }
1567 pCh->flags |= ASYNC_NORMAL_ACTIVE;
1568
1569noblock:
1570
1571 /* first open - Assign termios structure to port */
1572 if ( tty->count == 1 ) {
1573 i2QueueCommands(PTYPE_INLINE, pCh, 0, 2, CMD_CTSFL_DSAB, CMD_RTSFL_DSAB);
1574 /* Now we must send the termios settings to the loadware */
1575 set_params( pCh, NULL );
1576 }
1577
1578 /*
1579 * Now set any i2lib options. These may go away if the i2lib code ends
1580 * up rolled into the mainline.
1581 */
1582 pCh->channelOptions |= CO_NBLOCK_WRITE;
1583
1584#ifdef IP2DEBUG_OPEN
1585 printk (KERN_DEBUG "IP2: open completed\n" );
1586#endif
1587 serviceOutgoingFifo( pCh->pMyBord );
1588
1589 ip2trace (CHANN, ITRC_OPEN, ITRC_RETURN, 0 );
1590
1591 return 0;
1592}
1593
1594/******************************************************************************/
1595/* Function: ip2_close() */
1596/* Parameters: Pointer to tty structure */
1597/* Pointer to file structure */
1598/* Returns: Nothing */
1599/* */
1600/* Description: */
1601/* */
1602/* */
1603/******************************************************************************/
1604static void
1605ip2_close( PTTY tty, struct file *pFile )
1606{
1607 i2ChanStrPtr pCh = tty->driver_data;
1608
1609 if ( !pCh ) {
1610 return;
1611 }
1612
1613 ip2trace (CHANN, ITRC_CLOSE, ITRC_ENTER, 0 );
1614
1615#ifdef IP2DEBUG_OPEN
1616 printk(KERN_DEBUG "IP2:close %s:\n",tty->name);
1617#endif
1618
1619 if ( tty_hung_up_p ( pFile ) ) {
1620
1621 ip2trace (CHANN, ITRC_CLOSE, 2, 1, 2 );
1622
1623 return;
1624 }
1625 if ( tty->count > 1 ) { /* not the last close */
1626
1627 ip2trace (CHANN, ITRC_CLOSE, 2, 1, 3 );
1628
1629 return;
1630 }
1631 pCh->flags |= ASYNC_CLOSING; // last close actually
1632
1633 tty->closing = 1;
1634
1635 if (pCh->ClosingWaitTime != ASYNC_CLOSING_WAIT_NONE) {
1636 /*
1637 * Before we drop DTR, make sure the transmitter has completely drained.
1638 * This uses an timeout, after which the close
1639 * completes.
1640 */
1641 ip2_wait_until_sent(tty, pCh->ClosingWaitTime );
1642 }
1643 /*
1644 * At this point we stop accepting input. Here we flush the channel
1645 * input buffer which will allow the board to send up more data. Any
1646 * additional input is tossed at interrupt/poll time.
1647 */
1648 i2InputFlush( pCh );
1649
1650 /* disable DSS reporting */
1651 i2QueueCommands(PTYPE_INLINE, pCh, 100, 4,
1652 CMD_DCD_NREP, CMD_CTS_NREP, CMD_DSR_NREP, CMD_RI_NREP);
1653 if (tty->termios->c_cflag & HUPCL) {
1654 i2QueueCommands(PTYPE_INLINE, pCh, 100, 2, CMD_RTSDN, CMD_DTRDN);
1655 pCh->dataSetOut &= ~(I2_DTR | I2_RTS);
1656 i2QueueCommands( PTYPE_INLINE, pCh, 100, 1, CMD_PAUSE(25));
1657 }
1658
1659 serviceOutgoingFifo ( pCh->pMyBord );
1660
1661 tty_ldisc_flush(tty);
1662 tty_driver_flush_buffer(tty);
1663 tty->closing = 0;
1664
1665 pCh->pTTY = NULL;
1666
1667 if (pCh->wopen) {
1668 if (pCh->ClosingDelay) {
1669 msleep_interruptible(jiffies_to_msecs(pCh->ClosingDelay));
1670 }
1671 wake_up_interruptible(&pCh->open_wait);
1672 }
1673
1674 pCh->flags &=~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
1675 wake_up_interruptible(&pCh->close_wait);
1676
1677#ifdef IP2DEBUG_OPEN
1678 DBG_CNT("ip2_close: after wakeups--");
1679#endif
1680
1681
1682 ip2trace (CHANN, ITRC_CLOSE, ITRC_RETURN, 1, 1 );
1683
1684 return;
1685}
1686
1687/******************************************************************************/
1688/* Function: ip2_hangup() */
1689/* Parameters: Pointer to tty structure */
1690/* Returns: Nothing */
1691/* */
1692/* Description: */
1693/* */
1694/* */
1695/******************************************************************************/
1696static void
1697ip2_hangup ( PTTY tty )
1698{
1699 i2ChanStrPtr pCh = tty->driver_data;
1700
1701 if( !pCh ) {
1702 return;
1703 }
1704
1705 ip2trace (CHANN, ITRC_HANGUP, ITRC_ENTER, 0 );
1706
1707 ip2_flush_buffer(tty);
1708
1709 /* disable DSS reporting */
1710
1711 i2QueueCommands(PTYPE_BYPASS, pCh, 0, 1, CMD_DCD_NREP);
1712 i2QueueCommands(PTYPE_INLINE, pCh, 0, 2, CMD_CTSFL_DSAB, CMD_RTSFL_DSAB);
1713 if ( (tty->termios->c_cflag & HUPCL) ) {
1714 i2QueueCommands(PTYPE_BYPASS, pCh, 0, 2, CMD_RTSDN, CMD_DTRDN);
1715 pCh->dataSetOut &= ~(I2_DTR | I2_RTS);
1716 i2QueueCommands( PTYPE_INLINE, pCh, 100, 1, CMD_PAUSE(25));
1717 }
1718 i2QueueCommands(PTYPE_INLINE, pCh, 1, 3,
1719 CMD_CTS_NREP, CMD_DSR_NREP, CMD_RI_NREP);
1720 serviceOutgoingFifo ( pCh->pMyBord );
1721
1722 wake_up_interruptible ( &pCh->delta_msr_wait );
1723
1724 pCh->flags &= ~ASYNC_NORMAL_ACTIVE;
1725 pCh->pTTY = NULL;
1726 wake_up_interruptible ( &pCh->open_wait );
1727
1728 ip2trace (CHANN, ITRC_HANGUP, ITRC_RETURN, 0 );
1729}
1730
1731/******************************************************************************/
1732/******************************************************************************/
1733/* Device Output Section */
1734/******************************************************************************/
1735/******************************************************************************/
1736
1737/******************************************************************************/
1738/* Function: ip2_write() */
1739/* Parameters: Pointer to tty structure */
1740/* Flag denoting data is in user (1) or kernel (0) space */
1741/* Pointer to data */
1742/* Number of bytes to write */
1743/* Returns: Number of bytes actually written */
1744/* */
1745/* Description: (MANDATORY) */
1746/* */
1747/* */
1748/******************************************************************************/
1749static int
1750ip2_write( PTTY tty, const unsigned char *pData, int count)
1751{
1752 i2ChanStrPtr pCh = tty->driver_data;
1753 int bytesSent = 0;
1754 unsigned long flags;
1755
1756 ip2trace (CHANN, ITRC_WRITE, ITRC_ENTER, 2, count, -1 );
1757
1758 /* Flush out any buffered data left over from ip2_putchar() calls. */
1759 ip2_flush_chars( tty );
1760
1761 /* This is the actual move bit. Make sure it does what we need!!!!! */
1762 write_lock_irqsave(&pCh->Pbuf_spinlock, flags);
1763 bytesSent = i2Output( pCh, pData, count);
1764 write_unlock_irqrestore(&pCh->Pbuf_spinlock, flags);
1765
1766 ip2trace (CHANN, ITRC_WRITE, ITRC_RETURN, 1, bytesSent );
1767
1768 return bytesSent > 0 ? bytesSent : 0;
1769}
1770
1771/******************************************************************************/
1772/* Function: ip2_putchar() */
1773/* Parameters: Pointer to tty structure */
1774/* Character to write */
1775/* Returns: Nothing */
1776/* */
1777/* Description: */
1778/* */
1779/* */
1780/******************************************************************************/
1781static int
1782ip2_putchar( PTTY tty, unsigned char ch )
1783{
1784 i2ChanStrPtr pCh = tty->driver_data;
1785 unsigned long flags;
1786
1787// ip2trace (CHANN, ITRC_PUTC, ITRC_ENTER, 1, ch );
1788
1789 write_lock_irqsave(&pCh->Pbuf_spinlock, flags);
1790 pCh->Pbuf[pCh->Pbuf_stuff++] = ch;
1791 if ( pCh->Pbuf_stuff == sizeof pCh->Pbuf ) {
1792 write_unlock_irqrestore(&pCh->Pbuf_spinlock, flags);
1793 ip2_flush_chars( tty );
1794 } else
1795 write_unlock_irqrestore(&pCh->Pbuf_spinlock, flags);
1796 return 1;
1797
1798// ip2trace (CHANN, ITRC_PUTC, ITRC_RETURN, 1, ch );
1799}
1800
1801/******************************************************************************/
1802/* Function: ip2_flush_chars() */
1803/* Parameters: Pointer to tty structure */
1804/* Returns: Nothing */
1805/* */
1806/* Description: */
1807/* */
1808/******************************************************************************/
1809static void
1810ip2_flush_chars( PTTY tty )
1811{
1812 int strip;
1813 i2ChanStrPtr pCh = tty->driver_data;
1814 unsigned long flags;
1815
1816 write_lock_irqsave(&pCh->Pbuf_spinlock, flags);
1817 if ( pCh->Pbuf_stuff ) {
1818
1819// ip2trace (CHANN, ITRC_PUTC, 10, 1, strip );
1820
1821 //
1822 // We may need to restart i2Output if it does not fullfill this request
1823 //
1824 strip = i2Output( pCh, pCh->Pbuf, pCh->Pbuf_stuff);
1825 if ( strip != pCh->Pbuf_stuff ) {
1826 memmove( pCh->Pbuf, &pCh->Pbuf[strip], pCh->Pbuf_stuff - strip );
1827 }
1828 pCh->Pbuf_stuff -= strip;
1829 }
1830 write_unlock_irqrestore(&pCh->Pbuf_spinlock, flags);
1831}
1832
1833/******************************************************************************/
1834/* Function: ip2_write_room() */
1835/* Parameters: Pointer to tty structure */
1836/* Returns: Number of bytes that the driver can accept */
1837/* */
1838/* Description: */
1839/* */
1840/******************************************************************************/
1841static int
1842ip2_write_room ( PTTY tty )
1843{
1844 int bytesFree;
1845 i2ChanStrPtr pCh = tty->driver_data;
1846 unsigned long flags;
1847
1848 read_lock_irqsave(&pCh->Pbuf_spinlock, flags);
1849 bytesFree = i2OutputFree( pCh ) - pCh->Pbuf_stuff;
1850 read_unlock_irqrestore(&pCh->Pbuf_spinlock, flags);
1851
1852 ip2trace (CHANN, ITRC_WRITE, 11, 1, bytesFree );
1853
1854 return ((bytesFree > 0) ? bytesFree : 0);
1855}
1856
1857/******************************************************************************/
1858/* Function: ip2_chars_in_buf() */
1859/* Parameters: Pointer to tty structure */
1860/* Returns: Number of bytes queued for transmission */
1861/* */
1862/* Description: */
1863/* */
1864/* */
1865/******************************************************************************/
1866static int
1867ip2_chars_in_buf ( PTTY tty )
1868{
1869 i2ChanStrPtr pCh = tty->driver_data;
1870 int rc;
1871 unsigned long flags;
1872
1873 ip2trace (CHANN, ITRC_WRITE, 12, 1, pCh->Obuf_char_count + pCh->Pbuf_stuff );
1874
1875#ifdef IP2DEBUG_WRITE
1876 printk (KERN_DEBUG "IP2: chars in buffer = %d (%d,%d)\n",
1877 pCh->Obuf_char_count + pCh->Pbuf_stuff,
1878 pCh->Obuf_char_count, pCh->Pbuf_stuff );
1879#endif
1880 read_lock_irqsave(&pCh->Obuf_spinlock, flags);
1881 rc = pCh->Obuf_char_count;
1882 read_unlock_irqrestore(&pCh->Obuf_spinlock, flags);
1883 read_lock_irqsave(&pCh->Pbuf_spinlock, flags);
1884 rc += pCh->Pbuf_stuff;
1885 read_unlock_irqrestore(&pCh->Pbuf_spinlock, flags);
1886 return rc;
1887}
1888
1889/******************************************************************************/
1890/* Function: ip2_flush_buffer() */
1891/* Parameters: Pointer to tty structure */
1892/* Returns: Nothing */
1893/* */
1894/* Description: */
1895/* */
1896/* */
1897/******************************************************************************/
1898static void
1899ip2_flush_buffer( PTTY tty )
1900{
1901 i2ChanStrPtr pCh = tty->driver_data;
1902 unsigned long flags;
1903
1904 ip2trace (CHANN, ITRC_FLUSH, ITRC_ENTER, 0 );
1905
1906#ifdef IP2DEBUG_WRITE
1907 printk (KERN_DEBUG "IP2: flush buffer\n" );
1908#endif
1909 write_lock_irqsave(&pCh->Pbuf_spinlock, flags);
1910 pCh->Pbuf_stuff = 0;
1911 write_unlock_irqrestore(&pCh->Pbuf_spinlock, flags);
1912 i2FlushOutput( pCh );
1913 ip2_owake(tty);
1914
1915 ip2trace (CHANN, ITRC_FLUSH, ITRC_RETURN, 0 );
1916
1917}
1918
1919/******************************************************************************/
1920/* Function: ip2_wait_until_sent() */
1921/* Parameters: Pointer to tty structure */
1922/* Timeout for wait. */
1923/* Returns: Nothing */
1924/* */
1925/* Description: */
1926/* This function is used in place of the normal tty_wait_until_sent, which */
1927/* only waits for the driver buffers to be empty (or rather, those buffers */
1928/* reported by chars_in_buffer) which doesn't work for IP2 due to the */
1929/* indeterminate number of bytes buffered on the board. */
1930/******************************************************************************/
1931static void
1932ip2_wait_until_sent ( PTTY tty, int timeout )
1933{
1934 int i = jiffies;
1935 i2ChanStrPtr pCh = tty->driver_data;
1936
1937 tty_wait_until_sent(tty, timeout );
1938 if ( (i = timeout - (jiffies -i)) > 0)
1939 i2DrainOutput( pCh, i );
1940}
1941
1942/******************************************************************************/
1943/******************************************************************************/
1944/* Device Input Section */
1945/******************************************************************************/
1946/******************************************************************************/
1947
1948/******************************************************************************/
1949/* Function: ip2_throttle() */
1950/* Parameters: Pointer to tty structure */
1951/* Returns: Nothing */
1952/* */
1953/* Description: */
1954/* */
1955/* */
1956/******************************************************************************/
1957static void
1958ip2_throttle ( PTTY tty )
1959{
1960 i2ChanStrPtr pCh = tty->driver_data;
1961
1962#ifdef IP2DEBUG_READ
1963 printk (KERN_DEBUG "IP2: throttle\n" );
1964#endif
1965 /*
1966 * Signal the poll/interrupt handlers not to forward incoming data to
1967 * the line discipline. This will cause the buffers to fill up in the
1968 * library and thus cause the library routines to send the flow control
1969 * stuff.
1970 */
1971 pCh->throttled = 1;
1972}
1973
1974/******************************************************************************/
1975/* Function: ip2_unthrottle() */
1976/* Parameters: Pointer to tty structure */
1977/* Returns: Nothing */
1978/* */
1979/* Description: */
1980/* */
1981/* */
1982/******************************************************************************/
1983static void
1984ip2_unthrottle ( PTTY tty )
1985{
1986 i2ChanStrPtr pCh = tty->driver_data;
1987 unsigned long flags;
1988
1989#ifdef IP2DEBUG_READ
1990 printk (KERN_DEBUG "IP2: unthrottle\n" );
1991#endif
1992
1993 /* Pass incoming data up to the line discipline again. */
1994 pCh->throttled = 0;
1995 i2QueueCommands(PTYPE_BYPASS, pCh, 0, 1, CMD_RESUME);
1996 serviceOutgoingFifo( pCh->pMyBord );
1997 read_lock_irqsave(&pCh->Ibuf_spinlock, flags);
1998 if ( pCh->Ibuf_stuff != pCh->Ibuf_strip ) {
1999 read_unlock_irqrestore(&pCh->Ibuf_spinlock, flags);
2000#ifdef IP2DEBUG_READ
2001 printk (KERN_DEBUG "i2Input called from unthrottle\n" );
2002#endif
2003 i2Input( pCh );
2004 } else
2005 read_unlock_irqrestore(&pCh->Ibuf_spinlock, flags);
2006}
2007
2008static void
2009ip2_start ( PTTY tty )
2010{
2011 i2ChanStrPtr pCh = DevTable[tty->index];
2012
2013 i2QueueCommands(PTYPE_BYPASS, pCh, 0, 1, CMD_RESUME);
2014 i2QueueCommands(PTYPE_BYPASS, pCh, 100, 1, CMD_UNSUSPEND);
2015 i2QueueCommands(PTYPE_BYPASS, pCh, 100, 1, CMD_RESUME);
2016#ifdef IP2DEBUG_WRITE
2017 printk (KERN_DEBUG "IP2: start tx\n" );
2018#endif
2019}
2020
2021static void
2022ip2_stop ( PTTY tty )
2023{
2024 i2ChanStrPtr pCh = DevTable[tty->index];
2025
2026 i2QueueCommands(PTYPE_BYPASS, pCh, 100, 1, CMD_SUSPEND);
2027#ifdef IP2DEBUG_WRITE
2028 printk (KERN_DEBUG "IP2: stop tx\n" );
2029#endif
2030}
2031
2032/******************************************************************************/
2033/* Device Ioctl Section */
2034/******************************************************************************/
2035
2036static int ip2_tiocmget(struct tty_struct *tty, struct file *file)
2037{
2038 i2ChanStrPtr pCh = DevTable[tty->index];
2039#ifdef ENABLE_DSSNOW
2040 wait_queue_t wait;
2041#endif
2042
2043 if (pCh == NULL)
2044 return -ENODEV;
2045
2046/*
2047 FIXME - the following code is causing a NULL pointer dereference in
2048 2.3.51 in an interrupt handler. It's suppose to prompt the board
2049 to return the DSS signal status immediately. Why doesn't it do
2050 the same thing in 2.2.14?
2051*/
2052
2053/* This thing is still busted in the 1.2.12 driver on 2.4.x
2054 and even hoses the serial console so the oops can be trapped.
2055 /\/\|=mhw=|\/\/ */
2056
2057#ifdef ENABLE_DSSNOW
2058 i2QueueCommands(PTYPE_BYPASS, pCh, 100, 1, CMD_DSS_NOW);
2059
2060 init_waitqueue_entry(&wait, current);
2061 add_wait_queue(&pCh->dss_now_wait, &wait);
2062 set_current_state( TASK_INTERRUPTIBLE );
2063
2064 serviceOutgoingFifo( pCh->pMyBord );
2065
2066 schedule();
2067
2068 set_current_state( TASK_RUNNING );
2069 remove_wait_queue(&pCh->dss_now_wait, &wait);
2070
2071 if (signal_pending(current)) {
2072 return -EINTR;
2073 }
2074#endif
2075 return ((pCh->dataSetOut & I2_RTS) ? TIOCM_RTS : 0)
2076 | ((pCh->dataSetOut & I2_DTR) ? TIOCM_DTR : 0)
2077 | ((pCh->dataSetIn & I2_DCD) ? TIOCM_CAR : 0)
2078 | ((pCh->dataSetIn & I2_RI) ? TIOCM_RNG : 0)
2079 | ((pCh->dataSetIn & I2_DSR) ? TIOCM_DSR : 0)
2080 | ((pCh->dataSetIn & I2_CTS) ? TIOCM_CTS : 0);
2081}
2082
2083static int ip2_tiocmset(struct tty_struct *tty, struct file *file,
2084 unsigned int set, unsigned int clear)
2085{
2086 i2ChanStrPtr pCh = DevTable[tty->index];
2087
2088 if (pCh == NULL)
2089 return -ENODEV;
2090
2091 if (set & TIOCM_RTS) {
2092 i2QueueCommands(PTYPE_INLINE, pCh, 100, 1, CMD_RTSUP);
2093 pCh->dataSetOut |= I2_RTS;
2094 }
2095 if (set & TIOCM_DTR) {
2096 i2QueueCommands(PTYPE_INLINE, pCh, 100, 1, CMD_DTRUP);
2097 pCh->dataSetOut |= I2_DTR;
2098 }
2099
2100 if (clear & TIOCM_RTS) {
2101 i2QueueCommands(PTYPE_INLINE, pCh, 100, 1, CMD_RTSDN);
2102 pCh->dataSetOut &= ~I2_RTS;
2103 }
2104 if (clear & TIOCM_DTR) {
2105 i2QueueCommands(PTYPE_INLINE, pCh, 100, 1, CMD_DTRDN);
2106 pCh->dataSetOut &= ~I2_DTR;
2107 }
2108 serviceOutgoingFifo( pCh->pMyBord );
2109 return 0;
2110}
2111
2112/******************************************************************************/
2113/* Function: ip2_ioctl() */
2114/* Parameters: Pointer to tty structure */
2115/* Pointer to file structure */
2116/* Command */
2117/* Argument */
2118/* Returns: Success or failure */
2119/* */
2120/* Description: */
2121/* */
2122/* */
2123/******************************************************************************/
2124static int
2125ip2_ioctl ( PTTY tty, struct file *pFile, UINT cmd, ULONG arg )
2126{
2127 wait_queue_t wait;
2128 i2ChanStrPtr pCh = DevTable[tty->index];
2129 i2eBordStrPtr pB;
2130 struct async_icount cprev, cnow; /* kernel counter temps */
2131 struct serial_icounter_struct __user *p_cuser;
2132 int rc = 0;
2133 unsigned long flags;
2134 void __user *argp = (void __user *)arg;
2135
2136 if ( pCh == NULL )
2137 return -ENODEV;
2138
2139 pB = pCh->pMyBord;
2140
2141 ip2trace (CHANN, ITRC_IOCTL, ITRC_ENTER, 2, cmd, arg );
2142
2143#ifdef IP2DEBUG_IOCTL
2144 printk(KERN_DEBUG "IP2: ioctl cmd (%x), arg (%lx)\n", cmd, arg );
2145#endif
2146
2147 switch(cmd) {
2148 case TIOCGSERIAL:
2149
2150 ip2trace (CHANN, ITRC_IOCTL, 2, 1, rc );
2151
2152 rc = get_serial_info(pCh, argp);
2153 if (rc)
2154 return rc;
2155 break;
2156
2157 case TIOCSSERIAL:
2158
2159 ip2trace (CHANN, ITRC_IOCTL, 3, 1, rc );
2160
2161 rc = set_serial_info(pCh, argp);
2162 if (rc)
2163 return rc;
2164 break;
2165
2166 case TCXONC:
2167 rc = tty_check_change(tty);
2168 if (rc)
2169 return rc;
2170 switch (arg) {
2171 case TCOOFF:
2172 //return -ENOIOCTLCMD;
2173 break;
2174 case TCOON:
2175 //return -ENOIOCTLCMD;
2176 break;
2177 case TCIOFF:
2178 if (STOP_CHAR(tty) != __DISABLED_CHAR) {
2179 i2QueueCommands( PTYPE_BYPASS, pCh, 100, 1,
2180 CMD_XMIT_NOW(STOP_CHAR(tty)));
2181 }
2182 break;
2183 case TCION:
2184 if (START_CHAR(tty) != __DISABLED_CHAR) {
2185 i2QueueCommands( PTYPE_BYPASS, pCh, 100, 1,
2186 CMD_XMIT_NOW(START_CHAR(tty)));
2187 }
2188 break;
2189 default:
2190 return -EINVAL;
2191 }
2192 return 0;
2193
2194 case TCSBRK: /* SVID version: non-zero arg --> no break */
2195 rc = tty_check_change(tty);
2196
2197 ip2trace (CHANN, ITRC_IOCTL, 4, 1, rc );
2198
2199 if (!rc) {
2200 ip2_wait_until_sent(tty,0);
2201 if (!arg) {
2202 i2QueueCommands(PTYPE_INLINE, pCh, 100, 1, CMD_SEND_BRK(250));
2203 serviceOutgoingFifo( pCh->pMyBord );
2204 }
2205 }
2206 break;
2207
2208 case TCSBRKP: /* support for POSIX tcsendbreak() */
2209 rc = tty_check_change(tty);
2210
2211 ip2trace (CHANN, ITRC_IOCTL, 5, 1, rc );
2212
2213 if (!rc) {
2214 ip2_wait_until_sent(tty,0);
2215 i2QueueCommands(PTYPE_INLINE, pCh, 100, 1,
2216 CMD_SEND_BRK(arg ? arg*100 : 250));
2217 serviceOutgoingFifo ( pCh->pMyBord );
2218 }
2219 break;
2220
2221 case TIOCGSOFTCAR:
2222
2223 ip2trace (CHANN, ITRC_IOCTL, 6, 1, rc );
2224
2225 rc = put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long __user *)argp);
2226 if (rc)
2227 return rc;
2228 break;
2229
2230 case TIOCSSOFTCAR:
2231
2232 ip2trace (CHANN, ITRC_IOCTL, 7, 1, rc );
2233
2234 rc = get_user(arg,(unsigned long __user *) argp);
2235 if (rc)
2236 return rc;
2237 tty->termios->c_cflag = ((tty->termios->c_cflag & ~CLOCAL)
2238 | (arg ? CLOCAL : 0));
2239
2240 break;
2241
2242 /*
2243 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change - mask
2244 * passed in arg for lines of interest (use |'ed TIOCM_RNG/DSR/CD/CTS
2245 * for masking). Caller should use TIOCGICOUNT to see which one it was
2246 */
2247 case TIOCMIWAIT:
2248 write_lock_irqsave(&pB->read_fifo_spinlock, flags);
2249 cprev = pCh->icount; /* note the counters on entry */
2250 write_unlock_irqrestore(&pB->read_fifo_spinlock, flags);
2251 i2QueueCommands(PTYPE_BYPASS, pCh, 100, 4,
2252 CMD_DCD_REP, CMD_CTS_REP, CMD_DSR_REP, CMD_RI_REP);
2253 init_waitqueue_entry(&wait, current);
2254 add_wait_queue(&pCh->delta_msr_wait, &wait);
2255 set_current_state( TASK_INTERRUPTIBLE );
2256
2257 serviceOutgoingFifo( pCh->pMyBord );
2258 for(;;) {
2259 ip2trace (CHANN, ITRC_IOCTL, 10, 0 );
2260
2261 schedule();
2262
2263 ip2trace (CHANN, ITRC_IOCTL, 11, 0 );
2264
2265 /* see if a signal did it */
2266 if (signal_pending(current)) {
2267 rc = -ERESTARTSYS;
2268 break;
2269 }
2270 write_lock_irqsave(&pB->read_fifo_spinlock, flags);
2271 cnow = pCh->icount; /* atomic copy */
2272 write_unlock_irqrestore(&pB->read_fifo_spinlock, flags);
2273 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
2274 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts) {
2275 rc = -EIO; /* no change => rc */
2276 break;
2277 }
2278 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
2279 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
2280 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
2281 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
2282 rc = 0;
2283 break;
2284 }
2285 cprev = cnow;
2286 }
2287 set_current_state( TASK_RUNNING );
2288 remove_wait_queue(&pCh->delta_msr_wait, &wait);
2289
2290 i2QueueCommands(PTYPE_BYPASS, pCh, 100, 3,
2291 CMD_CTS_NREP, CMD_DSR_NREP, CMD_RI_NREP);
2292 if ( ! (pCh->flags & ASYNC_CHECK_CD)) {
2293 i2QueueCommands(PTYPE_BYPASS, pCh, 100, 1, CMD_DCD_NREP);
2294 }
2295 serviceOutgoingFifo( pCh->pMyBord );
2296 return rc;
2297 break;
2298
2299 /*
2300 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
2301 * Return: write counters to the user passed counter struct
2302 * NB: both 1->0 and 0->1 transitions are counted except for RI where
2303 * only 0->1 is counted. The controller is quite capable of counting
2304 * both, but this done to preserve compatibility with the standard
2305 * serial driver.
2306 */
2307 case TIOCGICOUNT:
2308 ip2trace (CHANN, ITRC_IOCTL, 11, 1, rc );
2309
2310 write_lock_irqsave(&pB->read_fifo_spinlock, flags);
2311 cnow = pCh->icount;
2312 write_unlock_irqrestore(&pB->read_fifo_spinlock, flags);
2313 p_cuser = argp;
2314 rc = put_user(cnow.cts, &p_cuser->cts);
2315 rc = put_user(cnow.dsr, &p_cuser->dsr);
2316 rc = put_user(cnow.rng, &p_cuser->rng);
2317 rc = put_user(cnow.dcd, &p_cuser->dcd);
2318 rc = put_user(cnow.rx, &p_cuser->rx);
2319 rc = put_user(cnow.tx, &p_cuser->tx);
2320 rc = put_user(cnow.frame, &p_cuser->frame);
2321 rc = put_user(cnow.overrun, &p_cuser->overrun);
2322 rc = put_user(cnow.parity, &p_cuser->parity);
2323 rc = put_user(cnow.brk, &p_cuser->brk);
2324 rc = put_user(cnow.buf_overrun, &p_cuser->buf_overrun);
2325 break;
2326
2327 /*
2328 * The rest are not supported by this driver. By returning -ENOIOCTLCMD they
2329 * will be passed to the line discipline for it to handle.
2330 */
2331 case TIOCSERCONFIG:
2332 case TIOCSERGWILD:
2333 case TIOCSERGETLSR:
2334 case TIOCSERSWILD:
2335 case TIOCSERGSTRUCT:
2336 case TIOCSERGETMULTI:
2337 case TIOCSERSETMULTI:
2338
2339 default:
2340 ip2trace (CHANN, ITRC_IOCTL, 12, 0 );
2341
2342 rc = -ENOIOCTLCMD;
2343 break;
2344 }
2345
2346 ip2trace (CHANN, ITRC_IOCTL, ITRC_RETURN, 0 );
2347
2348 return rc;
2349}
2350
2351/******************************************************************************/
2352/* Function: GetSerialInfo() */
2353/* Parameters: Pointer to channel structure */
2354/* Pointer to old termios structure */
2355/* Returns: Nothing */
2356/* */
2357/* Description: */
2358/* This is to support the setserial command, and requires processing of the */
2359/* standard Linux serial structure. */
2360/******************************************************************************/
2361static int
2362get_serial_info ( i2ChanStrPtr pCh, struct serial_struct __user *retinfo )
2363{
2364 struct serial_struct tmp;
2365
2366 memset ( &tmp, 0, sizeof(tmp) );
2367 tmp.type = pCh->pMyBord->channelBtypes.bid_value[(pCh->port_index & (IP2_PORTS_PER_BOARD-1))/16];
2368 if (BID_HAS_654(tmp.type)) {
2369 tmp.type = PORT_16650;
2370 } else {
2371 tmp.type = PORT_CIRRUS;
2372 }
2373 tmp.line = pCh->port_index;
2374 tmp.port = pCh->pMyBord->i2eBase;
2375 tmp.irq = ip2config.irq[pCh->port_index/64];
2376 tmp.flags = pCh->flags;
2377 tmp.baud_base = pCh->BaudBase;
2378 tmp.close_delay = pCh->ClosingDelay;
2379 tmp.closing_wait = pCh->ClosingWaitTime;
2380 tmp.custom_divisor = pCh->BaudDivisor;
2381 return copy_to_user(retinfo,&tmp,sizeof(*retinfo));
2382}
2383
2384/******************************************************************************/
2385/* Function: SetSerialInfo() */
2386/* Parameters: Pointer to channel structure */
2387/* Pointer to old termios structure */
2388/* Returns: Nothing */
2389/* */
2390/* Description: */
2391/* This function provides support for setserial, which uses the TIOCSSERIAL */
2392/* ioctl. Not all setserial parameters are relevant. If the user attempts to */
2393/* change the IRQ, address or type of the port the ioctl fails. */
2394/******************************************************************************/
2395static int
2396set_serial_info( i2ChanStrPtr pCh, struct serial_struct __user *new_info )
2397{
2398 struct serial_struct ns;
2399 int old_flags, old_baud_divisor;
2400
2401 if (copy_from_user(&ns, new_info, sizeof (ns)))
2402 return -EFAULT;
2403
2404 /*
2405 * We don't allow setserial to change IRQ, board address, type or baud
2406 * base. Also line nunber as such is meaningless but we use it for our
2407 * array index so it is fixed also.
2408 */
2409 if ( (ns.irq != ip2config.irq[pCh->port_index])
2410 || ((int) ns.port != ((int) (pCh->pMyBord->i2eBase)))
2411 || (ns.baud_base != pCh->BaudBase)
2412 || (ns.line != pCh->port_index) ) {
2413 return -EINVAL;
2414 }
2415
2416 old_flags = pCh->flags;
2417 old_baud_divisor = pCh->BaudDivisor;
2418
2419 if ( !capable(CAP_SYS_ADMIN) ) {
2420 if ( ( ns.close_delay != pCh->ClosingDelay ) ||
2421 ( (ns.flags & ~ASYNC_USR_MASK) !=
2422 (pCh->flags & ~ASYNC_USR_MASK) ) ) {
2423 return -EPERM;
2424 }
2425
2426 pCh->flags = (pCh->flags & ~ASYNC_USR_MASK) |
2427 (ns.flags & ASYNC_USR_MASK);
2428 pCh->BaudDivisor = ns.custom_divisor;
2429 } else {
2430 pCh->flags = (pCh->flags & ~ASYNC_FLAGS) |
2431 (ns.flags & ASYNC_FLAGS);
2432 pCh->BaudDivisor = ns.custom_divisor;
2433 pCh->ClosingDelay = ns.close_delay * HZ/100;
2434 pCh->ClosingWaitTime = ns.closing_wait * HZ/100;
2435 }
2436
2437 if ( ( (old_flags & ASYNC_SPD_MASK) != (pCh->flags & ASYNC_SPD_MASK) )
2438 || (old_baud_divisor != pCh->BaudDivisor) ) {
2439 // Invalidate speed and reset parameters
2440 set_params( pCh, NULL );
2441 }
2442
2443 return 0;
2444}
2445
2446/******************************************************************************/
2447/* Function: ip2_set_termios() */
2448/* Parameters: Pointer to tty structure */
2449/* Pointer to old termios structure */
2450/* Returns: Nothing */
2451/* */
2452/* Description: */
2453/* */
2454/* */
2455/******************************************************************************/
2456static void
2457ip2_set_termios( PTTY tty, struct ktermios *old_termios )
2458{
2459 i2ChanStrPtr pCh = (i2ChanStrPtr)tty->driver_data;
2460
2461#ifdef IP2DEBUG_IOCTL
2462 printk (KERN_DEBUG "IP2: set termios %p\n", old_termios );
2463#endif
2464
2465 set_params( pCh, old_termios );
2466}
2467
2468/******************************************************************************/
2469/* Function: ip2_set_line_discipline() */
2470/* Parameters: Pointer to tty structure */
2471/* Returns: Nothing */
2472/* */
2473/* Description: Does nothing */
2474/* */
2475/* */
2476/******************************************************************************/
2477static void
2478ip2_set_line_discipline ( PTTY tty )
2479{
2480#ifdef IP2DEBUG_IOCTL
2481 printk (KERN_DEBUG "IP2: set line discipline\n" );
2482#endif
2483
2484 ip2trace (((i2ChanStrPtr)tty->driver_data)->port_index, ITRC_IOCTL, 16, 0 );
2485
2486}
2487
2488/******************************************************************************/
2489/* Function: SetLine Characteristics() */
2490/* Parameters: Pointer to channel structure */
2491/* Returns: Nothing */
2492/* */
2493/* Description: */
2494/* This routine is called to update the channel structure with the new line */
2495/* characteristics, and send the appropriate commands to the board when they */
2496/* change. */
2497/******************************************************************************/
2498static void
2499set_params( i2ChanStrPtr pCh, struct ktermios *o_tios )
2500{
2501 tcflag_t cflag, iflag, lflag;
2502 char stop_char, start_char;
2503 struct ktermios dummy;
2504
2505 lflag = pCh->pTTY->termios->c_lflag;
2506 cflag = pCh->pTTY->termios->c_cflag;
2507 iflag = pCh->pTTY->termios->c_iflag;
2508
2509 if (o_tios == NULL) {
2510 dummy.c_lflag = ~lflag;
2511 dummy.c_cflag = ~cflag;
2512 dummy.c_iflag = ~iflag;
2513 o_tios = &dummy;
2514 }
2515
2516 {
2517 switch ( cflag & CBAUD ) {
2518 case B0:
2519 i2QueueCommands( PTYPE_BYPASS, pCh, 100, 2, CMD_RTSDN, CMD_DTRDN);
2520 pCh->dataSetOut &= ~(I2_DTR | I2_RTS);
2521 i2QueueCommands( PTYPE_INLINE, pCh, 100, 1, CMD_PAUSE(25));
2522 pCh->pTTY->termios->c_cflag |= (CBAUD & o_tios->c_cflag);
2523 goto service_it;
2524 break;
2525 case B38400:
2526 /*
2527 * This is the speed that is overloaded with all the other high
2528 * speeds, depending upon the flag settings.
2529 */
2530 if ( ( pCh->flags & ASYNC_SPD_MASK ) == ASYNC_SPD_HI ) {
2531 pCh->speed = CBR_57600;
2532 } else if ( (pCh->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI ) {
2533 pCh->speed = CBR_115200;
2534 } else if ( (pCh->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST ) {
2535 pCh->speed = CBR_C1;
2536 } else {
2537 pCh->speed = CBR_38400;
2538 }
2539 break;
2540 case B50: pCh->speed = CBR_50; break;
2541 case B75: pCh->speed = CBR_75; break;
2542 case B110: pCh->speed = CBR_110; break;
2543 case B134: pCh->speed = CBR_134; break;
2544 case B150: pCh->speed = CBR_150; break;
2545 case B200: pCh->speed = CBR_200; break;
2546 case B300: pCh->speed = CBR_300; break;
2547 case B600: pCh->speed = CBR_600; break;
2548 case B1200: pCh->speed = CBR_1200; break;
2549 case B1800: pCh->speed = CBR_1800; break;
2550 case B2400: pCh->speed = CBR_2400; break;
2551 case B4800: pCh->speed = CBR_4800; break;
2552 case B9600: pCh->speed = CBR_9600; break;
2553 case B19200: pCh->speed = CBR_19200; break;
2554 case B57600: pCh->speed = CBR_57600; break;
2555 case B115200: pCh->speed = CBR_115200; break;
2556 case B153600: pCh->speed = CBR_153600; break;
2557 case B230400: pCh->speed = CBR_230400; break;
2558 case B307200: pCh->speed = CBR_307200; break;
2559 case B460800: pCh->speed = CBR_460800; break;
2560 case B921600: pCh->speed = CBR_921600; break;
2561 default: pCh->speed = CBR_9600; break;
2562 }
2563 if ( pCh->speed == CBR_C1 ) {
2564 // Process the custom speed parameters.
2565 int bps = pCh->BaudBase / pCh->BaudDivisor;
2566 if ( bps == 921600 ) {
2567 pCh->speed = CBR_921600;
2568 } else {
2569 bps = bps/10;
2570 i2QueueCommands( PTYPE_INLINE, pCh, 100, 1, CMD_BAUD_DEF1(bps) );
2571 }
2572 }
2573 i2QueueCommands( PTYPE_INLINE, pCh, 100, 1, CMD_SETBAUD(pCh->speed));
2574
2575 i2QueueCommands ( PTYPE_INLINE, pCh, 100, 2, CMD_DTRUP, CMD_RTSUP);
2576 pCh->dataSetOut |= (I2_DTR | I2_RTS);
2577 }
2578 if ( (CSTOPB & cflag) ^ (CSTOPB & o_tios->c_cflag))
2579 {
2580 i2QueueCommands ( PTYPE_INLINE, pCh, 100, 1,
2581 CMD_SETSTOP( ( cflag & CSTOPB ) ? CST_2 : CST_1));
2582 }
2583 if (((PARENB|PARODD) & cflag) ^ ((PARENB|PARODD) & o_tios->c_cflag))
2584 {
2585 i2QueueCommands ( PTYPE_INLINE, pCh, 100, 1,
2586 CMD_SETPAR(
2587 (cflag & PARENB ? (cflag & PARODD ? CSP_OD : CSP_EV) : CSP_NP)
2588 )
2589 );
2590 }
2591 /* byte size and parity */
2592 if ( (CSIZE & cflag)^(CSIZE & o_tios->c_cflag))
2593 {
2594 int datasize;
2595 switch ( cflag & CSIZE ) {
2596 case CS5: datasize = CSZ_5; break;
2597 case CS6: datasize = CSZ_6; break;
2598 case CS7: datasize = CSZ_7; break;
2599 case CS8: datasize = CSZ_8; break;
2600 default: datasize = CSZ_5; break; /* as per serial.c */
2601 }
2602 i2QueueCommands ( PTYPE_INLINE, pCh, 100, 1, CMD_SETBITS(datasize) );
2603 }
2604 /* Process CTS flow control flag setting */
2605 if ( (cflag & CRTSCTS) ) {
2606 i2QueueCommands(PTYPE_INLINE, pCh, 100,
2607 2, CMD_CTSFL_ENAB, CMD_RTSFL_ENAB);
2608 } else {
2609 i2QueueCommands(PTYPE_INLINE, pCh, 100,
2610 2, CMD_CTSFL_DSAB, CMD_RTSFL_DSAB);
2611 }
2612 //
2613 // Process XON/XOFF flow control flags settings
2614 //
2615 stop_char = STOP_CHAR(pCh->pTTY);
2616 start_char = START_CHAR(pCh->pTTY);
2617
2618 //////////// can't be \000
2619 if (stop_char == __DISABLED_CHAR )
2620 {
2621 stop_char = ~__DISABLED_CHAR;
2622 }
2623 if (start_char == __DISABLED_CHAR )
2624 {
2625 start_char = ~__DISABLED_CHAR;
2626 }
2627 /////////////////////////////////
2628
2629 if ( o_tios->c_cc[VSTART] != start_char )
2630 {
2631 i2QueueCommands(PTYPE_BYPASS, pCh, 100, 1, CMD_DEF_IXON(start_char));
2632 i2QueueCommands(PTYPE_INLINE, pCh, 100, 1, CMD_DEF_OXON(start_char));
2633 }
2634 if ( o_tios->c_cc[VSTOP] != stop_char )
2635 {
2636 i2QueueCommands(PTYPE_BYPASS, pCh, 100, 1, CMD_DEF_IXOFF(stop_char));
2637 i2QueueCommands(PTYPE_INLINE, pCh, 100, 1, CMD_DEF_OXOFF(stop_char));
2638 }
2639 if (stop_char == __DISABLED_CHAR )
2640 {
2641 stop_char = ~__DISABLED_CHAR; //TEST123
2642 goto no_xoff;
2643 }
2644 if ((iflag & (IXOFF))^(o_tios->c_iflag & (IXOFF)))
2645 {
2646 if ( iflag & IXOFF ) { // Enable XOFF output flow control
2647 i2QueueCommands(PTYPE_INLINE, pCh, 100, 1, CMD_OXON_OPT(COX_XON));
2648 } else { // Disable XOFF output flow control
2649no_xoff:
2650 i2QueueCommands(PTYPE_INLINE, pCh, 100, 1, CMD_OXON_OPT(COX_NONE));
2651 }
2652 }
2653 if (start_char == __DISABLED_CHAR )
2654 {
2655 goto no_xon;
2656 }
2657 if ((iflag & (IXON|IXANY)) ^ (o_tios->c_iflag & (IXON|IXANY)))
2658 {
2659 if ( iflag & IXON ) {
2660 if ( iflag & IXANY ) { // Enable XON/XANY output flow control
2661 i2QueueCommands(PTYPE_INLINE, pCh, 100, 1, CMD_IXON_OPT(CIX_XANY));
2662 } else { // Enable XON output flow control
2663 i2QueueCommands(PTYPE_INLINE, pCh, 100, 1, CMD_IXON_OPT(CIX_XON));
2664 }
2665 } else { // Disable XON output flow control
2666no_xon:
2667 i2QueueCommands(PTYPE_INLINE, pCh, 100, 1, CMD_IXON_OPT(CIX_NONE));
2668 }
2669 }
2670 if ( (iflag & ISTRIP) ^ ( o_tios->c_iflag & (ISTRIP)) )
2671 {
2672 i2QueueCommands(PTYPE_INLINE, pCh, 100, 1,
2673 CMD_ISTRIP_OPT((iflag & ISTRIP ? 1 : 0)));
2674 }
2675 if ( (iflag & INPCK) ^ ( o_tios->c_iflag & (INPCK)) )
2676 {
2677 i2QueueCommands(PTYPE_INLINE, pCh, 100, 1,
2678 CMD_PARCHK((iflag & INPCK) ? CPK_ENAB : CPK_DSAB));
2679 }
2680
2681 if ( (iflag & (IGNBRK|PARMRK|BRKINT|IGNPAR))
2682 ^ ( o_tios->c_iflag & (IGNBRK|PARMRK|BRKINT|IGNPAR)) )
2683 {
2684 char brkrpt = 0;
2685 char parrpt = 0;
2686
2687 if ( iflag & IGNBRK ) { /* Ignore breaks altogether */
2688 /* Ignore breaks altogether */
2689 i2QueueCommands(PTYPE_INLINE, pCh, 100, 1, CMD_BRK_NREP);
2690 } else {
2691 if ( iflag & BRKINT ) {
2692 if ( iflag & PARMRK ) {
2693 brkrpt = 0x0a; // exception an inline triple
2694 } else {
2695 brkrpt = 0x1a; // exception and NULL
2696 }
2697 brkrpt |= 0x04; // flush input
2698 } else {
2699 if ( iflag & PARMRK ) {
2700 brkrpt = 0x0b; //POSIX triple \0377 \0 \0
2701 } else {
2702 brkrpt = 0x01; // Null only
2703 }
2704 }
2705 i2QueueCommands(PTYPE_INLINE, pCh, 100, 1, CMD_BRK_REP(brkrpt));
2706 }
2707
2708 if (iflag & IGNPAR) {
2709 parrpt = 0x20;
2710 /* would be 2 for not cirrus bug */
2711 /* would be 0x20 cept for cirrus bug */
2712 } else {
2713 if ( iflag & PARMRK ) {
2714 /*
2715 * Replace error characters with 3-byte sequence (\0377,\0,char)
2716 */
2717 parrpt = 0x04 ;
2718 i2QueueCommands(PTYPE_INLINE, pCh, 100, 1, CMD_ISTRIP_OPT((char)0));
2719 } else {
2720 parrpt = 0x03;
2721 }
2722 }
2723 i2QueueCommands(PTYPE_INLINE, pCh, 100, 1, CMD_SET_ERROR(parrpt));
2724 }
2725 if (cflag & CLOCAL) {
2726 // Status reporting fails for DCD if this is off
2727 i2QueueCommands(PTYPE_INLINE, pCh, 100, 1, CMD_DCD_NREP);
2728 pCh->flags &= ~ASYNC_CHECK_CD;
2729 } else {
2730 i2QueueCommands(PTYPE_INLINE, pCh, 100, 1, CMD_DCD_REP);
2731 pCh->flags |= ASYNC_CHECK_CD;
2732 }
2733
2734service_it:
2735 i2DrainOutput( pCh, 100 );
2736}
2737
2738/******************************************************************************/
2739/* IPL Device Section */
2740/******************************************************************************/
2741
2742/******************************************************************************/
2743/* Function: ip2_ipl_read() */
2744/* Parameters: Pointer to device inode */
2745/* Pointer to file structure */
2746/* Pointer to data */
2747/* Number of bytes to read */
2748/* Returns: Success or failure */
2749/* */
2750/* Description: Ugly */
2751/* */
2752/* */
2753/******************************************************************************/
2754
2755static
2756ssize_t
2757ip2_ipl_read(struct file *pFile, char __user *pData, size_t count, loff_t *off )
2758{
2759 unsigned int minor = iminor(pFile->f_path.dentry->d_inode);
2760 int rc = 0;
2761
2762#ifdef IP2DEBUG_IPL
2763 printk (KERN_DEBUG "IP2IPL: read %p, %d bytes\n", pData, count );
2764#endif
2765
2766 switch( minor ) {
2767 case 0: // IPL device
2768 rc = -EINVAL;
2769 break;
2770 case 1: // Status dump
2771 rc = -EINVAL;
2772 break;
2773 case 2: // Ping device
2774 rc = -EINVAL;
2775 break;
2776 case 3: // Trace device
2777 rc = DumpTraceBuffer ( pData, count );
2778 break;
2779 case 4: // Trace device
2780 rc = DumpFifoBuffer ( pData, count );
2781 break;
2782 default:
2783 rc = -ENODEV;
2784 break;
2785 }
2786 return rc;
2787}
2788
2789static int
2790DumpFifoBuffer ( char __user *pData, int count )
2791{
2792#ifdef DEBUG_FIFO
2793 int rc;
2794 rc = copy_to_user(pData, DBGBuf, count);
2795
2796 printk(KERN_DEBUG "Last index %d\n", I );
2797
2798 return count;
2799#endif /* DEBUG_FIFO */
2800 return 0;
2801}
2802
2803static int
2804DumpTraceBuffer ( char __user *pData, int count )
2805{
2806#ifdef IP2DEBUG_TRACE
2807 int rc;
2808 int dumpcount;
2809 int chunk;
2810 int *pIndex = (int __user *)pData;
2811
2812 if ( count < (sizeof(int) * 6) ) {
2813 return -EIO;
2814 }
2815 rc = put_user(tracewrap, pIndex );
2816 rc = put_user(TRACEMAX, ++pIndex );
2817 rc = put_user(tracestrip, ++pIndex );
2818 rc = put_user(tracestuff, ++pIndex );
2819 pData += sizeof(int) * 6;
2820 count -= sizeof(int) * 6;
2821
2822 dumpcount = tracestuff - tracestrip;
2823 if ( dumpcount < 0 ) {
2824 dumpcount += TRACEMAX;
2825 }
2826 if ( dumpcount > count ) {
2827 dumpcount = count;
2828 }
2829 chunk = TRACEMAX - tracestrip;
2830 if ( dumpcount > chunk ) {
2831 rc = copy_to_user(pData, &tracebuf[tracestrip],
2832 chunk * sizeof(tracebuf[0]) );
2833 pData += chunk * sizeof(tracebuf[0]);
2834 tracestrip = 0;
2835 chunk = dumpcount - chunk;
2836 } else {
2837 chunk = dumpcount;
2838 }
2839 rc = copy_to_user(pData, &tracebuf[tracestrip],
2840 chunk * sizeof(tracebuf[0]) );
2841 tracestrip += chunk;
2842 tracewrap = 0;
2843
2844 rc = put_user(tracestrip, ++pIndex );
2845 rc = put_user(tracestuff, ++pIndex );
2846
2847 return dumpcount;
2848#else
2849 return 0;
2850#endif
2851}
2852
2853/******************************************************************************/
2854/* Function: ip2_ipl_write() */
2855/* Parameters: */
2856/* Pointer to file structure */
2857/* Pointer to data */
2858/* Number of bytes to write */
2859/* Returns: Success or failure */
2860/* */
2861/* Description: */
2862/* */
2863/* */
2864/******************************************************************************/
2865static ssize_t
2866ip2_ipl_write(struct file *pFile, const char __user *pData, size_t count, loff_t *off)
2867{
2868#ifdef IP2DEBUG_IPL
2869 printk (KERN_DEBUG "IP2IPL: write %p, %d bytes\n", pData, count );
2870#endif
2871 return 0;
2872}
2873
2874/******************************************************************************/
2875/* Function: ip2_ipl_ioctl() */
2876/* Parameters: Pointer to device inode */
2877/* Pointer to file structure */
2878/* Command */
2879/* Argument */
2880/* Returns: Success or failure */
2881/* */
2882/* Description: */
2883/* */
2884/* */
2885/******************************************************************************/
2886static long
2887ip2_ipl_ioctl (struct file *pFile, UINT cmd, ULONG arg )
2888{
2889 unsigned int iplminor = iminor(pFile->f_path.dentry->d_inode);
2890 int rc = 0;
2891 void __user *argp = (void __user *)arg;
2892 ULONG __user *pIndex = argp;
2893 i2eBordStrPtr pB = i2BoardPtrTable[iplminor / 4];
2894 i2ChanStrPtr pCh;
2895
2896#ifdef IP2DEBUG_IPL
2897 printk (KERN_DEBUG "IP2IPL: ioctl cmd %d, arg %ld\n", cmd, arg );
2898#endif
2899
2900 lock_kernel();
2901
2902 switch ( iplminor ) {
2903 case 0: // IPL device
2904 rc = -EINVAL;
2905 break;
2906 case 1: // Status dump
2907 case 5:
2908 case 9:
2909 case 13:
2910 switch ( cmd ) {
2911 case 64: /* Driver - ip2stat */
2912 rc = put_user(-1, pIndex++ );
2913 rc = put_user(irq_counter, pIndex++ );
2914 rc = put_user(bh_counter, pIndex++ );
2915 break;
2916
2917 case 65: /* Board - ip2stat */
2918 if ( pB ) {
2919 rc = copy_to_user(argp, pB, sizeof(i2eBordStr));
2920 rc = put_user(inb(pB->i2eStatus),
2921 (ULONG __user *)(arg + (ULONG)(&pB->i2eStatus) - (ULONG)pB ) );
2922 } else {
2923 rc = -ENODEV;
2924 }
2925 break;
2926
2927 default:
2928 if (cmd < IP2_MAX_PORTS) {
2929 pCh = DevTable[cmd];
2930 if ( pCh )
2931 {
2932 rc = copy_to_user(argp, pCh, sizeof(i2ChanStr));
2933 if (rc)
2934 rc = -EFAULT;
2935 } else {
2936 rc = -ENODEV;
2937 }
2938 } else {
2939 rc = -EINVAL;
2940 }
2941 }
2942 break;
2943
2944 case 2: // Ping device
2945 rc = -EINVAL;
2946 break;
2947 case 3: // Trace device
2948 /*
2949 * akpm: This used to write a whole bunch of function addresses
2950 * to userspace, which generated lots of put_user() warnings.
2951 * I killed it all. Just return "success" and don't do
2952 * anything.
2953 */
2954 if (cmd == 1)
2955 rc = 0;
2956 else
2957 rc = -EINVAL;
2958 break;
2959
2960 default:
2961 rc = -ENODEV;
2962 break;
2963 }
2964 unlock_kernel();
2965 return rc;
2966}
2967
2968/******************************************************************************/
2969/* Function: ip2_ipl_open() */
2970/* Parameters: Pointer to device inode */
2971/* Pointer to file structure */
2972/* Returns: Success or failure */
2973/* */
2974/* Description: */
2975/* */
2976/* */
2977/******************************************************************************/
2978static int
2979ip2_ipl_open( struct inode *pInode, struct file *pFile )
2980{
2981
2982#ifdef IP2DEBUG_IPL
2983 printk (KERN_DEBUG "IP2IPL: open\n" );
2984#endif
2985 cycle_kernel_lock();
2986 return 0;
2987}
2988
2989static int
2990proc_ip2mem_show(struct seq_file *m, void *v)
2991{
2992 i2eBordStrPtr pB;
2993 i2ChanStrPtr pCh;
2994 PTTY tty;
2995 int i;
2996
2997#define FMTLINE "%3d: 0x%08x 0x%08x 0%011o 0%011o\n"
2998#define FMTLIN2 " 0x%04x 0x%04x tx flow 0x%x\n"
2999#define FMTLIN3 " 0x%04x 0x%04x rc flow\n"
3000
3001 seq_printf(m,"\n");
3002
3003 for( i = 0; i < IP2_MAX_BOARDS; ++i ) {
3004 pB = i2BoardPtrTable[i];
3005 if ( pB ) {
3006 seq_printf(m,"board %d:\n",i);
3007 seq_printf(m,"\tFifo rem: %d mty: %x outM %x\n",
3008 pB->i2eFifoRemains,pB->i2eWaitingForEmptyFifo,pB->i2eOutMailWaiting);
3009 }
3010 }
3011
3012 seq_printf(m,"#: tty flags, port flags, cflags, iflags\n");
3013 for (i=0; i < IP2_MAX_PORTS; i++) {
3014 pCh = DevTable[i];
3015 if (pCh) {
3016 tty = pCh->pTTY;
3017 if (tty && tty->count) {
3018 seq_printf(m,FMTLINE,i,(int)tty->flags,pCh->flags,
3019 tty->termios->c_cflag,tty->termios->c_iflag);
3020
3021 seq_printf(m,FMTLIN2,
3022 pCh->outfl.asof,pCh->outfl.room,pCh->channelNeeds);
3023 seq_printf(m,FMTLIN3,pCh->infl.asof,pCh->infl.room);
3024 }
3025 }
3026 }
3027 return 0;
3028}
3029
3030static int proc_ip2mem_open(struct inode *inode, struct file *file)
3031{
3032 return single_open(file, proc_ip2mem_show, NULL);
3033}
3034
3035static const struct file_operations ip2mem_proc_fops = {
3036 .owner = THIS_MODULE,
3037 .open = proc_ip2mem_open,
3038 .read = seq_read,
3039 .llseek = seq_lseek,
3040 .release = single_release,
3041};
3042
3043/*
3044 * This is the handler for /proc/tty/driver/ip2
3045 *
3046 * This stretch of code has been largely plagerized from at least three
3047 * different sources including ip2mkdev.c and a couple of other drivers.
3048 * The bugs are all mine. :-) =mhw=
3049 */
3050static int ip2_proc_show(struct seq_file *m, void *v)
3051{
3052 int i, j, box;
3053 int boxes = 0;
3054 int ports = 0;
3055 int tports = 0;
3056 i2eBordStrPtr pB;
3057 char *sep;
3058
3059 seq_printf(m, "ip2info: 1.0 driver: %s\n", pcVersion);
3060 seq_printf(m, "Driver: SMajor=%d CMajor=%d IMajor=%d MaxBoards=%d MaxBoxes=%d MaxPorts=%d\n",
3061 IP2_TTY_MAJOR, IP2_CALLOUT_MAJOR, IP2_IPL_MAJOR,
3062 IP2_MAX_BOARDS, ABS_MAX_BOXES, ABS_BIGGEST_BOX);
3063
3064 for( i = 0; i < IP2_MAX_BOARDS; ++i ) {
3065 /* This need to be reset for a board by board count... */
3066 boxes = 0;
3067 pB = i2BoardPtrTable[i];
3068 if( pB ) {
3069 switch( pB->i2ePom.e.porID & ~POR_ID_RESERVED )
3070 {
3071 case POR_ID_FIIEX:
3072 seq_printf(m, "Board %d: EX ports=", i);
3073 sep = "";
3074 for( box = 0; box < ABS_MAX_BOXES; ++box )
3075 {
3076 ports = 0;
3077
3078 if( pB->i2eChannelMap[box] != 0 ) ++boxes;
3079 for( j = 0; j < ABS_BIGGEST_BOX; ++j )
3080 {
3081 if( pB->i2eChannelMap[box] & 1<< j ) {
3082 ++ports;
3083 }
3084 }
3085 seq_printf(m, "%s%d", sep, ports);
3086 sep = ",";
3087 tports += ports;
3088 }
3089 seq_printf(m, " boxes=%d width=%d", boxes, pB->i2eDataWidth16 ? 16 : 8);
3090 break;
3091
3092 case POR_ID_II_4:
3093 seq_printf(m, "Board %d: ISA-4 ports=4 boxes=1", i);
3094 tports = ports = 4;
3095 break;
3096
3097 case POR_ID_II_8:
3098 seq_printf(m, "Board %d: ISA-8-std ports=8 boxes=1", i);
3099 tports = ports = 8;
3100 break;
3101
3102 case POR_ID_II_8R:
3103 seq_printf(m, "Board %d: ISA-8-RJ11 ports=8 boxes=1", i);
3104 tports = ports = 8;
3105 break;
3106
3107 default:
3108 seq_printf(m, "Board %d: unknown", i);
3109 /* Don't try and probe for minor numbers */
3110 tports = ports = 0;
3111 }
3112
3113 } else {
3114 /* Don't try and probe for minor numbers */
3115 seq_printf(m, "Board %d: vacant", i);
3116 tports = ports = 0;
3117 }
3118
3119 if( tports ) {
3120 seq_puts(m, " minors=");
3121 sep = "";
3122 for ( box = 0; box < ABS_MAX_BOXES; ++box )
3123 {
3124 for ( j = 0; j < ABS_BIGGEST_BOX; ++j )
3125 {
3126 if ( pB->i2eChannelMap[box] & (1 << j) )
3127 {
3128 seq_printf(m, "%s%d", sep,
3129 j + ABS_BIGGEST_BOX *
3130 (box+i*ABS_MAX_BOXES));
3131 sep = ",";
3132 }
3133 }
3134 }
3135 }
3136 seq_putc(m, '\n');
3137 }
3138 return 0;
3139 }
3140
3141static int ip2_proc_open(struct inode *inode, struct file *file)
3142{
3143 return single_open(file, ip2_proc_show, NULL);
3144}
3145
3146static const struct file_operations ip2_proc_fops = {
3147 .owner = THIS_MODULE,
3148 .open = ip2_proc_open,
3149 .read = seq_read,
3150 .llseek = seq_lseek,
3151 .release = single_release,
3152};
3153
3154/******************************************************************************/
3155/* Function: ip2trace() */
3156/* Parameters: Value to add to trace buffer */
3157/* Returns: Nothing */
3158/* */
3159/* Description: */
3160/* */
3161/* */
3162/******************************************************************************/
3163#ifdef IP2DEBUG_TRACE
3164void
3165ip2trace (unsigned short pn, unsigned char cat, unsigned char label, unsigned long codes, ...)
3166{
3167 long flags;
3168 unsigned long *pCode = &codes;
3169 union ip2breadcrumb bc;
3170 i2ChanStrPtr pCh;
3171
3172
3173 tracebuf[tracestuff++] = jiffies;
3174 if ( tracestuff == TRACEMAX ) {
3175 tracestuff = 0;
3176 }
3177 if ( tracestuff == tracestrip ) {
3178 if ( ++tracestrip == TRACEMAX ) {
3179 tracestrip = 0;
3180 }
3181 ++tracewrap;
3182 }
3183
3184 bc.hdr.port = 0xff & pn;
3185 bc.hdr.cat = cat;
3186 bc.hdr.codes = (unsigned char)( codes & 0xff );
3187 bc.hdr.label = label;
3188 tracebuf[tracestuff++] = bc.value;
3189
3190 for (;;) {
3191 if ( tracestuff == TRACEMAX ) {
3192 tracestuff = 0;
3193 }
3194 if ( tracestuff == tracestrip ) {
3195 if ( ++tracestrip == TRACEMAX ) {
3196 tracestrip = 0;
3197 }
3198 ++tracewrap;
3199 }
3200
3201 if ( !codes-- )
3202 break;
3203
3204 tracebuf[tracestuff++] = *++pCode;
3205 }
3206}
3207#endif
3208
3209
3210MODULE_LICENSE("GPL");
3211
3212static struct pci_device_id ip2main_pci_tbl[] __devinitdata = {
3213 { PCI_DEVICE(PCI_VENDOR_ID_COMPUTONE, PCI_DEVICE_ID_COMPUTONE_IP2EX) },
3214 { }
3215};
3216
3217MODULE_DEVICE_TABLE(pci, ip2main_pci_tbl);
3218
3219MODULE_FIRMWARE("intelliport2.bin");
diff --git a/drivers/char/ip2/ip2trace.h b/drivers/char/ip2/ip2trace.h
deleted file mode 100644
index da20435dc8a6..000000000000
--- a/drivers/char/ip2/ip2trace.h
+++ /dev/null
@@ -1,42 +0,0 @@
1
2//
3union ip2breadcrumb
4{
5 struct {
6 unsigned char port, cat, codes, label;
7 } __attribute__ ((packed)) hdr;
8 unsigned long value;
9};
10
11#define ITRC_NO_PORT 0xFF
12#define CHANN (pCh->port_index)
13
14#define ITRC_ERROR '!'
15#define ITRC_INIT 'A'
16#define ITRC_OPEN 'B'
17#define ITRC_CLOSE 'C'
18#define ITRC_DRAIN 'D'
19#define ITRC_IOCTL 'E'
20#define ITRC_FLUSH 'F'
21#define ITRC_STATUS 'G'
22#define ITRC_HANGUP 'H'
23#define ITRC_INTR 'I'
24#define ITRC_SFLOW 'J'
25#define ITRC_SBCMD 'K'
26#define ITRC_SICMD 'L'
27#define ITRC_MODEM 'M'
28#define ITRC_INPUT 'N'
29#define ITRC_OUTPUT 'O'
30#define ITRC_PUTC 'P'
31#define ITRC_QUEUE 'Q'
32#define ITRC_STFLW 'R'
33#define ITRC_SFIFO 'S'
34#define ITRC_VERIFY 'V'
35#define ITRC_WRITE 'W'
36
37#define ITRC_ENTER 0x00
38#define ITRC_RETURN 0xFF
39
40#define ITRC_QUEUE_ROOM 2
41#define ITRC_QUEUE_CMD 6
42
diff --git a/drivers/char/ip2/ip2types.h b/drivers/char/ip2/ip2types.h
deleted file mode 100644
index 9d67b260b2f6..000000000000
--- a/drivers/char/ip2/ip2types.h
+++ /dev/null
@@ -1,57 +0,0 @@
1/*******************************************************************************
2*
3* (c) 1998 by Computone Corporation
4*
5********************************************************************************
6*
7*
8* PACKAGE: Linux tty Device Driver for IntelliPort II family of multiport
9* serial I/O controllers.
10*
11* DESCRIPTION: Driver constants and type definitions.
12*
13* NOTES:
14*
15*******************************************************************************/
16#ifndef IP2TYPES_H
17#define IP2TYPES_H
18
19//*************
20//* Constants *
21//*************
22
23// Define some limits for this driver. Ports per board is a hardware limitation
24// that will not change. Current hardware limits this to 64 ports per board.
25// Boards per driver is a self-imposed limit.
26//
27#define IP2_MAX_BOARDS 4
28#define IP2_PORTS_PER_BOARD ABS_MOST_PORTS
29#define IP2_MAX_PORTS (IP2_MAX_BOARDS*IP2_PORTS_PER_BOARD)
30
31#define ISA 0
32#define PCI 1
33#define EISA 2
34
35//********************
36//* Type Definitions *
37//********************
38
39typedef struct tty_struct * PTTY;
40typedef wait_queue_head_t PWAITQ;
41
42typedef unsigned char UCHAR;
43typedef unsigned int UINT;
44typedef unsigned short USHORT;
45typedef unsigned long ULONG;
46
47typedef struct
48{
49 short irq[IP2_MAX_BOARDS];
50 unsigned short addr[IP2_MAX_BOARDS];
51 int type[IP2_MAX_BOARDS];
52#ifdef CONFIG_PCI
53 struct pci_dev *pci_dev[IP2_MAX_BOARDS];
54#endif
55} ip2config_t;
56
57#endif
diff --git a/drivers/char/ipmi/Makefile b/drivers/char/ipmi/Makefile
index eb8a1a8c188e..16a93648d54e 100644
--- a/drivers/char/ipmi/Makefile
+++ b/drivers/char/ipmi/Makefile
@@ -2,7 +2,7 @@
2# Makefile for the ipmi drivers. 2# Makefile for the ipmi drivers.
3# 3#
4 4
5ipmi_si-objs := ipmi_si_intf.o ipmi_kcs_sm.o ipmi_smic_sm.o ipmi_bt_sm.o 5ipmi_si-y := ipmi_si_intf.o ipmi_kcs_sm.o ipmi_smic_sm.o ipmi_bt_sm.o
6 6
7obj-$(CONFIG_IPMI_HANDLER) += ipmi_msghandler.o 7obj-$(CONFIG_IPMI_HANDLER) += ipmi_msghandler.o
8obj-$(CONFIG_IPMI_DEVICE_INTERFACE) += ipmi_devintf.o 8obj-$(CONFIG_IPMI_DEVICE_INTERFACE) += ipmi_devintf.o
diff --git a/drivers/char/ipmi/ipmi_bt_sm.c b/drivers/char/ipmi/ipmi_bt_sm.c
index 7b98c067190a..3ed20e8abc0d 100644
--- a/drivers/char/ipmi/ipmi_bt_sm.c
+++ b/drivers/char/ipmi/ipmi_bt_sm.c
@@ -2,7 +2,7 @@
2 * ipmi_bt_sm.c 2 * ipmi_bt_sm.c
3 * 3 *
4 * The state machine for an Open IPMI BT sub-driver under ipmi_si.c, part 4 * The state machine for an Open IPMI BT sub-driver under ipmi_si.c, part
5 * of the driver architecture at http://sourceforge.net/project/openipmi 5 * of the driver architecture at http://sourceforge.net/projects/openipmi
6 * 6 *
7 * Author: Rocky Craig <first.last@hp.com> 7 * Author: Rocky Craig <first.last@hp.com>
8 * 8 *
diff --git a/drivers/char/ipmi/ipmi_devintf.c b/drivers/char/ipmi/ipmi_devintf.c
index d8ec92a38980..2aa3977aae5e 100644
--- a/drivers/char/ipmi/ipmi_devintf.c
+++ b/drivers/char/ipmi/ipmi_devintf.c
@@ -44,7 +44,6 @@
44#include <linux/init.h> 44#include <linux/init.h>
45#include <linux/device.h> 45#include <linux/device.h>
46#include <linux/compat.h> 46#include <linux/compat.h>
47#include <linux/smp_lock.h>
48 47
49struct ipmi_file_private 48struct ipmi_file_private
50{ 49{
@@ -59,6 +58,7 @@ struct ipmi_file_private
59 unsigned int default_retry_time_ms; 58 unsigned int default_retry_time_ms;
60}; 59};
61 60
61static DEFINE_MUTEX(ipmi_mutex);
62static void file_receive_handler(struct ipmi_recv_msg *msg, 62static void file_receive_handler(struct ipmi_recv_msg *msg,
63 void *handler_data) 63 void *handler_data)
64{ 64{
@@ -102,9 +102,9 @@ static int ipmi_fasync(int fd, struct file *file, int on)
102 struct ipmi_file_private *priv = file->private_data; 102 struct ipmi_file_private *priv = file->private_data;
103 int result; 103 int result;
104 104
105 lock_kernel(); /* could race against open() otherwise */ 105 mutex_lock(&ipmi_mutex); /* could race against open() otherwise */
106 result = fasync_helper(fd, file, on, &priv->fasync_queue); 106 result = fasync_helper(fd, file, on, &priv->fasync_queue);
107 unlock_kernel(); 107 mutex_unlock(&ipmi_mutex);
108 108
109 return (result); 109 return (result);
110} 110}
@@ -125,7 +125,7 @@ static int ipmi_open(struct inode *inode, struct file *file)
125 if (!priv) 125 if (!priv)
126 return -ENOMEM; 126 return -ENOMEM;
127 127
128 lock_kernel(); 128 mutex_lock(&ipmi_mutex);
129 priv->file = file; 129 priv->file = file;
130 130
131 rv = ipmi_create_user(if_num, 131 rv = ipmi_create_user(if_num,
@@ -150,7 +150,7 @@ static int ipmi_open(struct inode *inode, struct file *file)
150 priv->default_retry_time_ms = 0; 150 priv->default_retry_time_ms = 0;
151 151
152out: 152out:
153 unlock_kernel(); 153 mutex_unlock(&ipmi_mutex);
154 return rv; 154 return rv;
155} 155}
156 156
@@ -639,9 +639,9 @@ static long ipmi_unlocked_ioctl(struct file *file,
639{ 639{
640 int ret; 640 int ret;
641 641
642 lock_kernel(); 642 mutex_lock(&ipmi_mutex);
643 ret = ipmi_ioctl(file, cmd, data); 643 ret = ipmi_ioctl(file, cmd, data);
644 unlock_kernel(); 644 mutex_unlock(&ipmi_mutex);
645 645
646 return ret; 646 return ret;
647} 647}
@@ -850,6 +850,7 @@ static const struct file_operations ipmi_fops = {
850 .release = ipmi_release, 850 .release = ipmi_release,
851 .fasync = ipmi_fasync, 851 .fasync = ipmi_fasync,
852 .poll = ipmi_poll, 852 .poll = ipmi_poll,
853 .llseek = noop_llseek,
853}; 854};
854 855
855#define DEVICE_NAME "ipmidev" 856#define DEVICE_NAME "ipmidev"
@@ -915,7 +916,7 @@ static struct ipmi_smi_watcher smi_watcher =
915 .smi_gone = ipmi_smi_gone, 916 .smi_gone = ipmi_smi_gone,
916}; 917};
917 918
918static __init int init_ipmi_devintf(void) 919static int __init init_ipmi_devintf(void)
919{ 920{
920 int rv; 921 int rv;
921 922
@@ -953,7 +954,7 @@ static __init int init_ipmi_devintf(void)
953} 954}
954module_init(init_ipmi_devintf); 955module_init(init_ipmi_devintf);
955 956
956static __exit void cleanup_ipmi(void) 957static void __exit cleanup_ipmi(void)
957{ 958{
958 struct ipmi_reg_list *entry, *entry2; 959 struct ipmi_reg_list *entry, *entry2;
959 mutex_lock(&reg_list_mutex); 960 mutex_lock(&reg_list_mutex);
diff --git a/drivers/char/ipmi/ipmi_msghandler.c b/drivers/char/ipmi/ipmi_msghandler.c
index 4f3f8c9ec262..58c0e6387cf7 100644
--- a/drivers/char/ipmi/ipmi_msghandler.c
+++ b/drivers/char/ipmi/ipmi_msghandler.c
@@ -36,6 +36,7 @@
36#include <asm/system.h> 36#include <asm/system.h>
37#include <linux/poll.h> 37#include <linux/poll.h>
38#include <linux/sched.h> 38#include <linux/sched.h>
39#include <linux/seq_file.h>
39#include <linux/spinlock.h> 40#include <linux/spinlock.h>
40#include <linux/mutex.h> 41#include <linux/mutex.h>
41#include <linux/slab.h> 42#include <linux/slab.h>
@@ -970,6 +971,33 @@ out_kfree:
970} 971}
971EXPORT_SYMBOL(ipmi_create_user); 972EXPORT_SYMBOL(ipmi_create_user);
972 973
974int ipmi_get_smi_info(int if_num, struct ipmi_smi_info *data)
975{
976 int rv = 0;
977 ipmi_smi_t intf;
978 struct ipmi_smi_handlers *handlers;
979
980 mutex_lock(&ipmi_interfaces_mutex);
981 list_for_each_entry_rcu(intf, &ipmi_interfaces, link) {
982 if (intf->intf_num == if_num)
983 goto found;
984 }
985 /* Not found, return an error */
986 rv = -EINVAL;
987 mutex_unlock(&ipmi_interfaces_mutex);
988 return rv;
989
990found:
991 handlers = intf->handlers;
992 rv = -ENOSYS;
993 if (handlers->get_smi_info)
994 rv = handlers->get_smi_info(intf->send_info, data);
995 mutex_unlock(&ipmi_interfaces_mutex);
996
997 return rv;
998}
999EXPORT_SYMBOL(ipmi_get_smi_info);
1000
973static void free_user(struct kref *ref) 1001static void free_user(struct kref *ref)
974{ 1002{
975 ipmi_user_t user = container_of(ref, struct ipmi_user, refcount); 1003 ipmi_user_t user = container_of(ref, struct ipmi_user, refcount);
@@ -1869,102 +1897,128 @@ int ipmi_request_supply_msgs(ipmi_user_t user,
1869EXPORT_SYMBOL(ipmi_request_supply_msgs); 1897EXPORT_SYMBOL(ipmi_request_supply_msgs);
1870 1898
1871#ifdef CONFIG_PROC_FS 1899#ifdef CONFIG_PROC_FS
1872static int ipmb_file_read_proc(char *page, char **start, off_t off, 1900static int smi_ipmb_proc_show(struct seq_file *m, void *v)
1873 int count, int *eof, void *data)
1874{ 1901{
1875 char *out = (char *) page; 1902 ipmi_smi_t intf = m->private;
1876 ipmi_smi_t intf = data;
1877 int i; 1903 int i;
1878 int rv = 0;
1879 1904
1880 for (i = 0; i < IPMI_MAX_CHANNELS; i++) 1905 seq_printf(m, "%x", intf->channels[0].address);
1881 rv += sprintf(out+rv, "%x ", intf->channels[i].address); 1906 for (i = 1; i < IPMI_MAX_CHANNELS; i++)
1882 out[rv-1] = '\n'; /* Replace the final space with a newline */ 1907 seq_printf(m, " %x", intf->channels[i].address);
1883 out[rv] = '\0'; 1908 return seq_putc(m, '\n');
1884 rv++; 1909}
1885 return rv; 1910
1911static int smi_ipmb_proc_open(struct inode *inode, struct file *file)
1912{
1913 return single_open(file, smi_ipmb_proc_show, PDE(inode)->data);
1886} 1914}
1887 1915
1888static int version_file_read_proc(char *page, char **start, off_t off, 1916static const struct file_operations smi_ipmb_proc_ops = {
1889 int count, int *eof, void *data) 1917 .open = smi_ipmb_proc_open,
1918 .read = seq_read,
1919 .llseek = seq_lseek,
1920 .release = single_release,
1921};
1922
1923static int smi_version_proc_show(struct seq_file *m, void *v)
1890{ 1924{
1891 char *out = (char *) page; 1925 ipmi_smi_t intf = m->private;
1892 ipmi_smi_t intf = data;
1893 1926
1894 return sprintf(out, "%u.%u\n", 1927 return seq_printf(m, "%u.%u\n",
1895 ipmi_version_major(&intf->bmc->id), 1928 ipmi_version_major(&intf->bmc->id),
1896 ipmi_version_minor(&intf->bmc->id)); 1929 ipmi_version_minor(&intf->bmc->id));
1897} 1930}
1898 1931
1899static int stat_file_read_proc(char *page, char **start, off_t off, 1932static int smi_version_proc_open(struct inode *inode, struct file *file)
1900 int count, int *eof, void *data)
1901{ 1933{
1902 char *out = (char *) page; 1934 return single_open(file, smi_version_proc_show, PDE(inode)->data);
1903 ipmi_smi_t intf = data; 1935}
1904 1936
1905 out += sprintf(out, "sent_invalid_commands: %u\n", 1937static const struct file_operations smi_version_proc_ops = {
1938 .open = smi_version_proc_open,
1939 .read = seq_read,
1940 .llseek = seq_lseek,
1941 .release = single_release,
1942};
1943
1944static int smi_stats_proc_show(struct seq_file *m, void *v)
1945{
1946 ipmi_smi_t intf = m->private;
1947
1948 seq_printf(m, "sent_invalid_commands: %u\n",
1906 ipmi_get_stat(intf, sent_invalid_commands)); 1949 ipmi_get_stat(intf, sent_invalid_commands));
1907 out += sprintf(out, "sent_local_commands: %u\n", 1950 seq_printf(m, "sent_local_commands: %u\n",
1908 ipmi_get_stat(intf, sent_local_commands)); 1951 ipmi_get_stat(intf, sent_local_commands));
1909 out += sprintf(out, "handled_local_responses: %u\n", 1952 seq_printf(m, "handled_local_responses: %u\n",
1910 ipmi_get_stat(intf, handled_local_responses)); 1953 ipmi_get_stat(intf, handled_local_responses));
1911 out += sprintf(out, "unhandled_local_responses: %u\n", 1954 seq_printf(m, "unhandled_local_responses: %u\n",
1912 ipmi_get_stat(intf, unhandled_local_responses)); 1955 ipmi_get_stat(intf, unhandled_local_responses));
1913 out += sprintf(out, "sent_ipmb_commands: %u\n", 1956 seq_printf(m, "sent_ipmb_commands: %u\n",
1914 ipmi_get_stat(intf, sent_ipmb_commands)); 1957 ipmi_get_stat(intf, sent_ipmb_commands));
1915 out += sprintf(out, "sent_ipmb_command_errs: %u\n", 1958 seq_printf(m, "sent_ipmb_command_errs: %u\n",
1916 ipmi_get_stat(intf, sent_ipmb_command_errs)); 1959 ipmi_get_stat(intf, sent_ipmb_command_errs));
1917 out += sprintf(out, "retransmitted_ipmb_commands: %u\n", 1960 seq_printf(m, "retransmitted_ipmb_commands: %u\n",
1918 ipmi_get_stat(intf, retransmitted_ipmb_commands)); 1961 ipmi_get_stat(intf, retransmitted_ipmb_commands));
1919 out += sprintf(out, "timed_out_ipmb_commands: %u\n", 1962 seq_printf(m, "timed_out_ipmb_commands: %u\n",
1920 ipmi_get_stat(intf, timed_out_ipmb_commands)); 1963 ipmi_get_stat(intf, timed_out_ipmb_commands));
1921 out += sprintf(out, "timed_out_ipmb_broadcasts: %u\n", 1964 seq_printf(m, "timed_out_ipmb_broadcasts: %u\n",
1922 ipmi_get_stat(intf, timed_out_ipmb_broadcasts)); 1965 ipmi_get_stat(intf, timed_out_ipmb_broadcasts));
1923 out += sprintf(out, "sent_ipmb_responses: %u\n", 1966 seq_printf(m, "sent_ipmb_responses: %u\n",
1924 ipmi_get_stat(intf, sent_ipmb_responses)); 1967 ipmi_get_stat(intf, sent_ipmb_responses));
1925 out += sprintf(out, "handled_ipmb_responses: %u\n", 1968 seq_printf(m, "handled_ipmb_responses: %u\n",
1926 ipmi_get_stat(intf, handled_ipmb_responses)); 1969 ipmi_get_stat(intf, handled_ipmb_responses));
1927 out += sprintf(out, "invalid_ipmb_responses: %u\n", 1970 seq_printf(m, "invalid_ipmb_responses: %u\n",
1928 ipmi_get_stat(intf, invalid_ipmb_responses)); 1971 ipmi_get_stat(intf, invalid_ipmb_responses));
1929 out += sprintf(out, "unhandled_ipmb_responses: %u\n", 1972 seq_printf(m, "unhandled_ipmb_responses: %u\n",
1930 ipmi_get_stat(intf, unhandled_ipmb_responses)); 1973 ipmi_get_stat(intf, unhandled_ipmb_responses));
1931 out += sprintf(out, "sent_lan_commands: %u\n", 1974 seq_printf(m, "sent_lan_commands: %u\n",
1932 ipmi_get_stat(intf, sent_lan_commands)); 1975 ipmi_get_stat(intf, sent_lan_commands));
1933 out += sprintf(out, "sent_lan_command_errs: %u\n", 1976 seq_printf(m, "sent_lan_command_errs: %u\n",
1934 ipmi_get_stat(intf, sent_lan_command_errs)); 1977 ipmi_get_stat(intf, sent_lan_command_errs));
1935 out += sprintf(out, "retransmitted_lan_commands: %u\n", 1978 seq_printf(m, "retransmitted_lan_commands: %u\n",
1936 ipmi_get_stat(intf, retransmitted_lan_commands)); 1979 ipmi_get_stat(intf, retransmitted_lan_commands));
1937 out += sprintf(out, "timed_out_lan_commands: %u\n", 1980 seq_printf(m, "timed_out_lan_commands: %u\n",
1938 ipmi_get_stat(intf, timed_out_lan_commands)); 1981 ipmi_get_stat(intf, timed_out_lan_commands));
1939 out += sprintf(out, "sent_lan_responses: %u\n", 1982 seq_printf(m, "sent_lan_responses: %u\n",
1940 ipmi_get_stat(intf, sent_lan_responses)); 1983 ipmi_get_stat(intf, sent_lan_responses));
1941 out += sprintf(out, "handled_lan_responses: %u\n", 1984 seq_printf(m, "handled_lan_responses: %u\n",
1942 ipmi_get_stat(intf, handled_lan_responses)); 1985 ipmi_get_stat(intf, handled_lan_responses));
1943 out += sprintf(out, "invalid_lan_responses: %u\n", 1986 seq_printf(m, "invalid_lan_responses: %u\n",
1944 ipmi_get_stat(intf, invalid_lan_responses)); 1987 ipmi_get_stat(intf, invalid_lan_responses));
1945 out += sprintf(out, "unhandled_lan_responses: %u\n", 1988 seq_printf(m, "unhandled_lan_responses: %u\n",
1946 ipmi_get_stat(intf, unhandled_lan_responses)); 1989 ipmi_get_stat(intf, unhandled_lan_responses));
1947 out += sprintf(out, "handled_commands: %u\n", 1990 seq_printf(m, "handled_commands: %u\n",
1948 ipmi_get_stat(intf, handled_commands)); 1991 ipmi_get_stat(intf, handled_commands));
1949 out += sprintf(out, "invalid_commands: %u\n", 1992 seq_printf(m, "invalid_commands: %u\n",
1950 ipmi_get_stat(intf, invalid_commands)); 1993 ipmi_get_stat(intf, invalid_commands));
1951 out += sprintf(out, "unhandled_commands: %u\n", 1994 seq_printf(m, "unhandled_commands: %u\n",
1952 ipmi_get_stat(intf, unhandled_commands)); 1995 ipmi_get_stat(intf, unhandled_commands));
1953 out += sprintf(out, "invalid_events: %u\n", 1996 seq_printf(m, "invalid_events: %u\n",
1954 ipmi_get_stat(intf, invalid_events)); 1997 ipmi_get_stat(intf, invalid_events));
1955 out += sprintf(out, "events: %u\n", 1998 seq_printf(m, "events: %u\n",
1956 ipmi_get_stat(intf, events)); 1999 ipmi_get_stat(intf, events));
1957 out += sprintf(out, "failed rexmit LAN msgs: %u\n", 2000 seq_printf(m, "failed rexmit LAN msgs: %u\n",
1958 ipmi_get_stat(intf, dropped_rexmit_lan_commands)); 2001 ipmi_get_stat(intf, dropped_rexmit_lan_commands));
1959 out += sprintf(out, "failed rexmit IPMB msgs: %u\n", 2002 seq_printf(m, "failed rexmit IPMB msgs: %u\n",
1960 ipmi_get_stat(intf, dropped_rexmit_ipmb_commands)); 2003 ipmi_get_stat(intf, dropped_rexmit_ipmb_commands));
2004 return 0;
2005}
1961 2006
1962 return (out - ((char *) page)); 2007static int smi_stats_proc_open(struct inode *inode, struct file *file)
2008{
2009 return single_open(file, smi_stats_proc_show, PDE(inode)->data);
1963} 2010}
2011
2012static const struct file_operations smi_stats_proc_ops = {
2013 .open = smi_stats_proc_open,
2014 .read = seq_read,
2015 .llseek = seq_lseek,
2016 .release = single_release,
2017};
1964#endif /* CONFIG_PROC_FS */ 2018#endif /* CONFIG_PROC_FS */
1965 2019
1966int ipmi_smi_add_proc_entry(ipmi_smi_t smi, char *name, 2020int ipmi_smi_add_proc_entry(ipmi_smi_t smi, char *name,
1967 read_proc_t *read_proc, 2021 const struct file_operations *proc_ops,
1968 void *data) 2022 void *data)
1969{ 2023{
1970 int rv = 0; 2024 int rv = 0;
@@ -1983,15 +2037,12 @@ int ipmi_smi_add_proc_entry(ipmi_smi_t smi, char *name,
1983 } 2037 }
1984 strcpy(entry->name, name); 2038 strcpy(entry->name, name);
1985 2039
1986 file = create_proc_entry(name, 0, smi->proc_dir); 2040 file = proc_create_data(name, 0, smi->proc_dir, proc_ops, data);
1987 if (!file) { 2041 if (!file) {
1988 kfree(entry->name); 2042 kfree(entry->name);
1989 kfree(entry); 2043 kfree(entry);
1990 rv = -ENOMEM; 2044 rv = -ENOMEM;
1991 } else { 2045 } else {
1992 file->data = data;
1993 file->read_proc = read_proc;
1994
1995 mutex_lock(&smi->proc_entry_lock); 2046 mutex_lock(&smi->proc_entry_lock);
1996 /* Stick it on the list. */ 2047 /* Stick it on the list. */
1997 entry->next = smi->proc_entries; 2048 entry->next = smi->proc_entries;
@@ -2016,17 +2067,17 @@ static int add_proc_entries(ipmi_smi_t smi, int num)
2016 2067
2017 if (rv == 0) 2068 if (rv == 0)
2018 rv = ipmi_smi_add_proc_entry(smi, "stats", 2069 rv = ipmi_smi_add_proc_entry(smi, "stats",
2019 stat_file_read_proc, 2070 &smi_stats_proc_ops,
2020 smi); 2071 smi);
2021 2072
2022 if (rv == 0) 2073 if (rv == 0)
2023 rv = ipmi_smi_add_proc_entry(smi, "ipmb", 2074 rv = ipmi_smi_add_proc_entry(smi, "ipmb",
2024 ipmb_file_read_proc, 2075 &smi_ipmb_proc_ops,
2025 smi); 2076 smi);
2026 2077
2027 if (rv == 0) 2078 if (rv == 0)
2028 rv = ipmi_smi_add_proc_entry(smi, "version", 2079 rv = ipmi_smi_add_proc_entry(smi, "version",
2029 version_file_read_proc, 2080 &smi_version_proc_ops,
2030 smi); 2081 smi);
2031#endif /* CONFIG_PROC_FS */ 2082#endif /* CONFIG_PROC_FS */
2032 2083
@@ -4442,13 +4493,13 @@ static int ipmi_init_msghandler(void)
4442 return 0; 4493 return 0;
4443} 4494}
4444 4495
4445static __init int ipmi_init_msghandler_mod(void) 4496static int __init ipmi_init_msghandler_mod(void)
4446{ 4497{
4447 ipmi_init_msghandler(); 4498 ipmi_init_msghandler();
4448 return 0; 4499 return 0;
4449} 4500}
4450 4501
4451static __exit void cleanup_ipmi(void) 4502static void __exit cleanup_ipmi(void)
4452{ 4503{
4453 int count; 4504 int count;
4454 4505
diff --git a/drivers/char/ipmi/ipmi_poweroff.c b/drivers/char/ipmi/ipmi_poweroff.c
index 0dec5da000ef..2efa176beab0 100644
--- a/drivers/char/ipmi/ipmi_poweroff.c
+++ b/drivers/char/ipmi/ipmi_poweroff.c
@@ -122,7 +122,7 @@ static struct ipmi_recv_msg halt_recv_msg = {
122 122
123 123
124/* 124/*
125 * Code to send a message and wait for the reponse. 125 * Code to send a message and wait for the response.
126 */ 126 */
127 127
128static void receive_handler(struct ipmi_recv_msg *recv_msg, void *handler_data) 128static void receive_handler(struct ipmi_recv_msg *recv_msg, void *handler_data)
diff --git a/drivers/char/ipmi/ipmi_si_intf.c b/drivers/char/ipmi/ipmi_si_intf.c
index 7bd7c45b53ef..9397ab49b72e 100644
--- a/drivers/char/ipmi/ipmi_si_intf.c
+++ b/drivers/char/ipmi/ipmi_si_intf.c
@@ -43,6 +43,7 @@
43#include <linux/moduleparam.h> 43#include <linux/moduleparam.h>
44#include <asm/system.h> 44#include <asm/system.h>
45#include <linux/sched.h> 45#include <linux/sched.h>
46#include <linux/seq_file.h>
46#include <linux/timer.h> 47#include <linux/timer.h>
47#include <linux/errno.h> 48#include <linux/errno.h>
48#include <linux/spinlock.h> 49#include <linux/spinlock.h>
@@ -57,6 +58,7 @@
57#include <asm/irq.h> 58#include <asm/irq.h>
58#include <linux/interrupt.h> 59#include <linux/interrupt.h>
59#include <linux/rcupdate.h> 60#include <linux/rcupdate.h>
61#include <linux/ipmi.h>
60#include <linux/ipmi_smi.h> 62#include <linux/ipmi_smi.h>
61#include <asm/io.h> 63#include <asm/io.h>
62#include "ipmi_si_sm.h" 64#include "ipmi_si_sm.h"
@@ -65,11 +67,10 @@
65#include <linux/string.h> 67#include <linux/string.h>
66#include <linux/ctype.h> 68#include <linux/ctype.h>
67#include <linux/pnp.h> 69#include <linux/pnp.h>
68
69#ifdef CONFIG_PPC_OF
70#include <linux/of_device.h> 70#include <linux/of_device.h>
71#include <linux/of_platform.h> 71#include <linux/of_platform.h>
72#endif 72#include <linux/of_address.h>
73#include <linux/of_irq.h>
73 74
74#define PFX "ipmi_si: " 75#define PFX "ipmi_si: "
75 76
@@ -107,23 +108,13 @@ enum si_type {
107}; 108};
108static char *si_to_str[] = { "kcs", "smic", "bt" }; 109static char *si_to_str[] = { "kcs", "smic", "bt" };
109 110
110enum ipmi_addr_src {
111 SI_INVALID = 0, SI_HOTMOD, SI_HARDCODED, SI_SPMI, SI_ACPI, SI_SMBIOS,
112 SI_PCI, SI_DEVICETREE, SI_DEFAULT
113};
114static char *ipmi_addr_src_to_str[] = { NULL, "hotmod", "hardcoded", "SPMI", 111static char *ipmi_addr_src_to_str[] = { NULL, "hotmod", "hardcoded", "SPMI",
115 "ACPI", "SMBIOS", "PCI", 112 "ACPI", "SMBIOS", "PCI",
116 "device-tree", "default" }; 113 "device-tree", "default" };
117 114
118#define DEVICE_NAME "ipmi_si" 115#define DEVICE_NAME "ipmi_si"
119 116
120static struct platform_driver ipmi_driver = { 117static struct platform_driver ipmi_driver;
121 .driver = {
122 .name = DEVICE_NAME,
123 .bus = &platform_bus_type
124 }
125};
126
127 118
128/* 119/*
129 * Indexes into stats[] in smi_info below. 120 * Indexes into stats[] in smi_info below.
@@ -291,6 +282,7 @@ struct smi_info {
291 struct task_struct *thread; 282 struct task_struct *thread;
292 283
293 struct list_head link; 284 struct list_head link;
285 union ipmi_smi_info_union addr_info;
294}; 286};
295 287
296#define smi_inc_stat(smi, stat) \ 288#define smi_inc_stat(smi, stat) \
@@ -308,9 +300,6 @@ static int pci_registered;
308#ifdef CONFIG_ACPI 300#ifdef CONFIG_ACPI
309static int pnp_registered; 301static int pnp_registered;
310#endif 302#endif
311#ifdef CONFIG_PPC_OF
312static int of_registered;
313#endif
314 303
315static unsigned int kipmid_max_busy_us[SI_MAX_PARMS]; 304static unsigned int kipmid_max_busy_us[SI_MAX_PARMS];
316static int num_max_busy_us; 305static int num_max_busy_us;
@@ -320,6 +309,7 @@ static int unload_when_empty = 1;
320static int add_smi(struct smi_info *smi); 309static int add_smi(struct smi_info *smi);
321static int try_smi_init(struct smi_info *smi); 310static int try_smi_init(struct smi_info *smi);
322static void cleanup_one_si(struct smi_info *to_clean); 311static void cleanup_one_si(struct smi_info *to_clean);
312static void cleanup_ipmi_si(void);
323 313
324static ATOMIC_NOTIFIER_HEAD(xaction_notifier_list); 314static ATOMIC_NOTIFIER_HEAD(xaction_notifier_list);
325static int register_xaction_notifier(struct notifier_block *nb) 315static int register_xaction_notifier(struct notifier_block *nb)
@@ -350,7 +340,7 @@ static void return_hosed_msg(struct smi_info *smi_info, int cCode)
350 cCode = IPMI_ERR_UNSPECIFIED; 340 cCode = IPMI_ERR_UNSPECIFIED;
351 /* else use it as is */ 341 /* else use it as is */
352 342
353 /* Make it a reponse */ 343 /* Make it a response */
354 msg->rsp[0] = msg->data[0] | 4; 344 msg->rsp[0] = msg->data[0] | 4;
355 msg->rsp[1] = msg->data[1]; 345 msg->rsp[1] = msg->data[1];
356 msg->rsp[2] = cCode; 346 msg->rsp[2] = cCode;
@@ -899,6 +889,14 @@ static void sender(void *send_info,
899 printk("**Enqueue: %d.%9.9d\n", t.tv_sec, t.tv_usec); 889 printk("**Enqueue: %d.%9.9d\n", t.tv_sec, t.tv_usec);
900#endif 890#endif
901 891
892 /*
893 * last_timeout_jiffies is updated here to avoid
894 * smi_timeout() handler passing very large time_diff
895 * value to smi_event_handler() that causes
896 * the send command to abort.
897 */
898 smi_info->last_timeout_jiffies = jiffies;
899
902 mod_timer(&smi_info->si_timer, jiffies + SI_TIMEOUT_JIFFIES); 900 mod_timer(&smi_info->si_timer, jiffies + SI_TIMEOUT_JIFFIES);
903 901
904 if (smi_info->thread) 902 if (smi_info->thread)
@@ -1186,6 +1184,18 @@ static int smi_start_processing(void *send_info,
1186 return 0; 1184 return 0;
1187} 1185}
1188 1186
1187static int get_smi_info(void *send_info, struct ipmi_smi_info *data)
1188{
1189 struct smi_info *smi = send_info;
1190
1191 data->addr_src = smi->addr_source;
1192 data->dev = smi->dev;
1193 data->addr_info = smi->addr_info;
1194 get_device(smi->dev);
1195
1196 return 0;
1197}
1198
1189static void set_maintenance_mode(void *send_info, int enable) 1199static void set_maintenance_mode(void *send_info, int enable)
1190{ 1200{
1191 struct smi_info *smi_info = send_info; 1201 struct smi_info *smi_info = send_info;
@@ -1197,6 +1207,7 @@ static void set_maintenance_mode(void *send_info, int enable)
1197static struct ipmi_smi_handlers handlers = { 1207static struct ipmi_smi_handlers handlers = {
1198 .owner = THIS_MODULE, 1208 .owner = THIS_MODULE,
1199 .start_processing = smi_start_processing, 1209 .start_processing = smi_start_processing,
1210 .get_smi_info = get_smi_info,
1200 .sender = sender, 1211 .sender = sender,
1201 .request_events = request_events, 1212 .request_events = request_events,
1202 .set_maintenance_mode = set_maintenance_mode, 1213 .set_maintenance_mode = set_maintenance_mode,
@@ -1665,6 +1676,17 @@ static int check_hotmod_int_op(const char *curr, const char *option,
1665 return 0; 1676 return 0;
1666} 1677}
1667 1678
1679static struct smi_info *smi_info_alloc(void)
1680{
1681 struct smi_info *info = kzalloc(sizeof(*info), GFP_KERNEL);
1682
1683 if (info) {
1684 spin_lock_init(&info->si_lock);
1685 spin_lock_init(&info->msg_lock);
1686 }
1687 return info;
1688}
1689
1668static int hotmod_handler(const char *val, struct kernel_param *kp) 1690static int hotmod_handler(const char *val, struct kernel_param *kp)
1669{ 1691{
1670 char *str = kstrdup(val, GFP_KERNEL); 1692 char *str = kstrdup(val, GFP_KERNEL);
@@ -1779,7 +1801,7 @@ static int hotmod_handler(const char *val, struct kernel_param *kp)
1779 } 1801 }
1780 1802
1781 if (op == HM_ADD) { 1803 if (op == HM_ADD) {
1782 info = kzalloc(sizeof(*info), GFP_KERNEL); 1804 info = smi_info_alloc();
1783 if (!info) { 1805 if (!info) {
1784 rv = -ENOMEM; 1806 rv = -ENOMEM;
1785 goto out; 1807 goto out;
@@ -1835,8 +1857,9 @@ static int hotmod_handler(const char *val, struct kernel_param *kp)
1835 return rv; 1857 return rv;
1836} 1858}
1837 1859
1838static __devinit void hardcode_find_bmc(void) 1860static int __devinit hardcode_find_bmc(void)
1839{ 1861{
1862 int ret = -ENODEV;
1840 int i; 1863 int i;
1841 struct smi_info *info; 1864 struct smi_info *info;
1842 1865
@@ -1844,9 +1867,9 @@ static __devinit void hardcode_find_bmc(void)
1844 if (!ports[i] && !addrs[i]) 1867 if (!ports[i] && !addrs[i])
1845 continue; 1868 continue;
1846 1869
1847 info = kzalloc(sizeof(*info), GFP_KERNEL); 1870 info = smi_info_alloc();
1848 if (!info) 1871 if (!info)
1849 return; 1872 return -ENOMEM;
1850 1873
1851 info->addr_source = SI_HARDCODED; 1874 info->addr_source = SI_HARDCODED;
1852 printk(KERN_INFO PFX "probing via hardcoded address\n"); 1875 printk(KERN_INFO PFX "probing via hardcoded address\n");
@@ -1899,10 +1922,12 @@ static __devinit void hardcode_find_bmc(void)
1899 if (!add_smi(info)) { 1922 if (!add_smi(info)) {
1900 if (try_smi_init(info)) 1923 if (try_smi_init(info))
1901 cleanup_one_si(info); 1924 cleanup_one_si(info);
1925 ret = 0;
1902 } else { 1926 } else {
1903 kfree(info); 1927 kfree(info);
1904 } 1928 }
1905 } 1929 }
1930 return ret;
1906} 1931}
1907 1932
1908#ifdef CONFIG_ACPI 1933#ifdef CONFIG_ACPI
@@ -1917,7 +1942,8 @@ static __devinit void hardcode_find_bmc(void)
1917static int acpi_failure; 1942static int acpi_failure;
1918 1943
1919/* For GPE-type interrupts. */ 1944/* For GPE-type interrupts. */
1920static u32 ipmi_acpi_gpe(void *context) 1945static u32 ipmi_acpi_gpe(acpi_handle gpe_device,
1946 u32 gpe_number, void *context)
1921{ 1947{
1922 struct smi_info *smi_info = context; 1948 struct smi_info *smi_info = context;
1923 unsigned long flags; 1949 unsigned long flags;
@@ -1974,8 +2000,7 @@ static int acpi_gpe_irq_setup(struct smi_info *info)
1974 2000
1975/* 2001/*
1976 * Defined at 2002 * Defined at
1977 * http://h21007.www2.hp.com/portal/download/files 2003 * http://h21007.www2.hp.com/portal/download/files/unprot/hpspmi.pdf
1978 * /unprot/hpspmi.pdf
1979 */ 2004 */
1980struct SPMITable { 2005struct SPMITable {
1981 s8 Signature[4]; 2006 s8 Signature[4];
@@ -2019,7 +2044,7 @@ struct SPMITable {
2019 s8 spmi_id[1]; /* A '\0' terminated array starts here. */ 2044 s8 spmi_id[1]; /* A '\0' terminated array starts here. */
2020}; 2045};
2021 2046
2022static __devinit int try_init_spmi(struct SPMITable *spmi) 2047static int __devinit try_init_spmi(struct SPMITable *spmi)
2023{ 2048{
2024 struct smi_info *info; 2049 struct smi_info *info;
2025 2050
@@ -2028,7 +2053,7 @@ static __devinit int try_init_spmi(struct SPMITable *spmi)
2028 return -ENODEV; 2053 return -ENODEV;
2029 } 2054 }
2030 2055
2031 info = kzalloc(sizeof(*info), GFP_KERNEL); 2056 info = smi_info_alloc();
2032 if (!info) { 2057 if (!info) {
2033 printk(KERN_ERR PFX "Could not allocate SI data (3)\n"); 2058 printk(KERN_ERR PFX "Could not allocate SI data (3)\n");
2034 return -ENOMEM; 2059 return -ENOMEM;
@@ -2102,7 +2127,7 @@ static __devinit int try_init_spmi(struct SPMITable *spmi)
2102 return 0; 2127 return 0;
2103} 2128}
2104 2129
2105static __devinit void spmi_find_bmc(void) 2130static void __devinit spmi_find_bmc(void)
2106{ 2131{
2107 acpi_status status; 2132 acpi_status status;
2108 struct SPMITable *spmi; 2133 struct SPMITable *spmi;
@@ -2138,7 +2163,7 @@ static int __devinit ipmi_pnp_probe(struct pnp_dev *dev,
2138 if (!acpi_dev) 2163 if (!acpi_dev)
2139 return -ENODEV; 2164 return -ENODEV;
2140 2165
2141 info = kzalloc(sizeof(*info), GFP_KERNEL); 2166 info = smi_info_alloc();
2142 if (!info) 2167 if (!info)
2143 return -ENOMEM; 2168 return -ENOMEM;
2144 2169
@@ -2146,6 +2171,7 @@ static int __devinit ipmi_pnp_probe(struct pnp_dev *dev,
2146 printk(KERN_INFO PFX "probing via ACPI\n"); 2171 printk(KERN_INFO PFX "probing via ACPI\n");
2147 2172
2148 handle = acpi_dev->handle; 2173 handle = acpi_dev->handle;
2174 info->addr_info.acpi_info.acpi_handle = handle;
2149 2175
2150 /* _IFT tells us the interface type: KCS, BT, etc */ 2176 /* _IFT tells us the interface type: KCS, BT, etc */
2151 status = acpi_evaluate_integer(handle, "_IFT", NULL, &tmp); 2177 status = acpi_evaluate_integer(handle, "_IFT", NULL, &tmp);
@@ -2315,11 +2341,11 @@ static int __devinit decode_dmi(const struct dmi_header *dm,
2315 return 0; 2341 return 0;
2316} 2342}
2317 2343
2318static __devinit void try_init_dmi(struct dmi_ipmi_data *ipmi_data) 2344static void __devinit try_init_dmi(struct dmi_ipmi_data *ipmi_data)
2319{ 2345{
2320 struct smi_info *info; 2346 struct smi_info *info;
2321 2347
2322 info = kzalloc(sizeof(*info), GFP_KERNEL); 2348 info = smi_info_alloc();
2323 if (!info) { 2349 if (!info) {
2324 printk(KERN_ERR PFX "Could not allocate SI data\n"); 2350 printk(KERN_ERR PFX "Could not allocate SI data\n");
2325 return; 2351 return;
@@ -2426,7 +2452,7 @@ static int __devinit ipmi_pci_probe(struct pci_dev *pdev,
2426 int class_type = pdev->class & PCI_ERMC_CLASSCODE_TYPE_MASK; 2452 int class_type = pdev->class & PCI_ERMC_CLASSCODE_TYPE_MASK;
2427 struct smi_info *info; 2453 struct smi_info *info;
2428 2454
2429 info = kzalloc(sizeof(*info), GFP_KERNEL); 2455 info = smi_info_alloc();
2430 if (!info) 2456 if (!info)
2431 return -ENOMEM; 2457 return -ENOMEM;
2432 2458
@@ -2529,20 +2555,24 @@ static struct pci_driver ipmi_pci_driver = {
2529}; 2555};
2530#endif /* CONFIG_PCI */ 2556#endif /* CONFIG_PCI */
2531 2557
2532 2558static struct of_device_id ipmi_match[];
2533#ifdef CONFIG_PPC_OF 2559static int __devinit ipmi_probe(struct platform_device *dev)
2534static int __devinit ipmi_of_probe(struct platform_device *dev,
2535 const struct of_device_id *match)
2536{ 2560{
2561#ifdef CONFIG_OF
2562 const struct of_device_id *match;
2537 struct smi_info *info; 2563 struct smi_info *info;
2538 struct resource resource; 2564 struct resource resource;
2539 const int *regsize, *regspacing, *regshift; 2565 const __be32 *regsize, *regspacing, *regshift;
2540 struct device_node *np = dev->dev.of_node; 2566 struct device_node *np = dev->dev.of_node;
2541 int ret; 2567 int ret;
2542 int proplen; 2568 int proplen;
2543 2569
2544 dev_info(&dev->dev, "probing via device tree\n"); 2570 dev_info(&dev->dev, "probing via device tree\n");
2545 2571
2572 match = of_match_device(ipmi_match, &dev->dev);
2573 if (!match)
2574 return -EINVAL;
2575
2546 ret = of_address_to_resource(np, 0, &resource); 2576 ret = of_address_to_resource(np, 0, &resource);
2547 if (ret) { 2577 if (ret) {
2548 dev_warn(&dev->dev, PFX "invalid address from OF\n"); 2578 dev_warn(&dev->dev, PFX "invalid address from OF\n");
@@ -2567,7 +2597,7 @@ static int __devinit ipmi_of_probe(struct platform_device *dev,
2567 return -EINVAL; 2597 return -EINVAL;
2568 } 2598 }
2569 2599
2570 info = kzalloc(sizeof(*info), GFP_KERNEL); 2600 info = smi_info_alloc();
2571 2601
2572 if (!info) { 2602 if (!info) {
2573 dev_err(&dev->dev, 2603 dev_err(&dev->dev,
@@ -2589,9 +2619,9 @@ static int __devinit ipmi_of_probe(struct platform_device *dev,
2589 2619
2590 info->io.addr_data = resource.start; 2620 info->io.addr_data = resource.start;
2591 2621
2592 info->io.regsize = regsize ? *regsize : DEFAULT_REGSIZE; 2622 info->io.regsize = regsize ? be32_to_cpup(regsize) : DEFAULT_REGSIZE;
2593 info->io.regspacing = regspacing ? *regspacing : DEFAULT_REGSPACING; 2623 info->io.regspacing = regspacing ? be32_to_cpup(regspacing) : DEFAULT_REGSPACING;
2594 info->io.regshift = regshift ? *regshift : 0; 2624 info->io.regshift = regshift ? be32_to_cpup(regshift) : 0;
2595 2625
2596 info->irq = irq_of_parse_and_map(dev->dev.of_node, 0); 2626 info->irq = irq_of_parse_and_map(dev->dev.of_node, 0);
2597 info->dev = &dev->dev; 2627 info->dev = &dev->dev;
@@ -2606,13 +2636,15 @@ static int __devinit ipmi_of_probe(struct platform_device *dev,
2606 kfree(info); 2636 kfree(info);
2607 return -EBUSY; 2637 return -EBUSY;
2608 } 2638 }
2609 2639#endif
2610 return 0; 2640 return 0;
2611} 2641}
2612 2642
2613static int __devexit ipmi_of_remove(struct platform_device *dev) 2643static int __devexit ipmi_remove(struct platform_device *dev)
2614{ 2644{
2645#ifdef CONFIG_OF
2615 cleanup_one_si(dev_get_drvdata(&dev->dev)); 2646 cleanup_one_si(dev_get_drvdata(&dev->dev));
2647#endif
2616 return 0; 2648 return 0;
2617} 2649}
2618 2650
@@ -2627,16 +2659,15 @@ static struct of_device_id ipmi_match[] =
2627 {}, 2659 {},
2628}; 2660};
2629 2661
2630static struct of_platform_driver ipmi_of_platform_driver = { 2662static struct platform_driver ipmi_driver = {
2631 .driver = { 2663 .driver = {
2632 .name = "ipmi", 2664 .name = DEVICE_NAME,
2633 .owner = THIS_MODULE, 2665 .owner = THIS_MODULE,
2634 .of_match_table = ipmi_match, 2666 .of_match_table = ipmi_match,
2635 }, 2667 },
2636 .probe = ipmi_of_probe, 2668 .probe = ipmi_probe,
2637 .remove = __devexit_p(ipmi_of_remove), 2669 .remove = __devexit_p(ipmi_remove),
2638}; 2670};
2639#endif /* CONFIG_PPC_OF */
2640 2671
2641static int wait_for_msg_done(struct smi_info *smi_info) 2672static int wait_for_msg_done(struct smi_info *smi_info)
2642{ 2673{
@@ -2775,54 +2806,73 @@ static int try_enable_event_buffer(struct smi_info *smi_info)
2775 return rv; 2806 return rv;
2776} 2807}
2777 2808
2778static int type_file_read_proc(char *page, char **start, off_t off, 2809static int smi_type_proc_show(struct seq_file *m, void *v)
2779 int count, int *eof, void *data)
2780{ 2810{
2781 struct smi_info *smi = data; 2811 struct smi_info *smi = m->private;
2812
2813 return seq_printf(m, "%s\n", si_to_str[smi->si_type]);
2814}
2782 2815
2783 return sprintf(page, "%s\n", si_to_str[smi->si_type]); 2816static int smi_type_proc_open(struct inode *inode, struct file *file)
2817{
2818 return single_open(file, smi_type_proc_show, PDE(inode)->data);
2784} 2819}
2785 2820
2786static int stat_file_read_proc(char *page, char **start, off_t off, 2821static const struct file_operations smi_type_proc_ops = {
2787 int count, int *eof, void *data) 2822 .open = smi_type_proc_open,
2823 .read = seq_read,
2824 .llseek = seq_lseek,
2825 .release = single_release,
2826};
2827
2828static int smi_si_stats_proc_show(struct seq_file *m, void *v)
2788{ 2829{
2789 char *out = (char *) page; 2830 struct smi_info *smi = m->private;
2790 struct smi_info *smi = data;
2791 2831
2792 out += sprintf(out, "interrupts_enabled: %d\n", 2832 seq_printf(m, "interrupts_enabled: %d\n",
2793 smi->irq && !smi->interrupt_disabled); 2833 smi->irq && !smi->interrupt_disabled);
2794 out += sprintf(out, "short_timeouts: %u\n", 2834 seq_printf(m, "short_timeouts: %u\n",
2795 smi_get_stat(smi, short_timeouts)); 2835 smi_get_stat(smi, short_timeouts));
2796 out += sprintf(out, "long_timeouts: %u\n", 2836 seq_printf(m, "long_timeouts: %u\n",
2797 smi_get_stat(smi, long_timeouts)); 2837 smi_get_stat(smi, long_timeouts));
2798 out += sprintf(out, "idles: %u\n", 2838 seq_printf(m, "idles: %u\n",
2799 smi_get_stat(smi, idles)); 2839 smi_get_stat(smi, idles));
2800 out += sprintf(out, "interrupts: %u\n", 2840 seq_printf(m, "interrupts: %u\n",
2801 smi_get_stat(smi, interrupts)); 2841 smi_get_stat(smi, interrupts));
2802 out += sprintf(out, "attentions: %u\n", 2842 seq_printf(m, "attentions: %u\n",
2803 smi_get_stat(smi, attentions)); 2843 smi_get_stat(smi, attentions));
2804 out += sprintf(out, "flag_fetches: %u\n", 2844 seq_printf(m, "flag_fetches: %u\n",
2805 smi_get_stat(smi, flag_fetches)); 2845 smi_get_stat(smi, flag_fetches));
2806 out += sprintf(out, "hosed_count: %u\n", 2846 seq_printf(m, "hosed_count: %u\n",
2807 smi_get_stat(smi, hosed_count)); 2847 smi_get_stat(smi, hosed_count));
2808 out += sprintf(out, "complete_transactions: %u\n", 2848 seq_printf(m, "complete_transactions: %u\n",
2809 smi_get_stat(smi, complete_transactions)); 2849 smi_get_stat(smi, complete_transactions));
2810 out += sprintf(out, "events: %u\n", 2850 seq_printf(m, "events: %u\n",
2811 smi_get_stat(smi, events)); 2851 smi_get_stat(smi, events));
2812 out += sprintf(out, "watchdog_pretimeouts: %u\n", 2852 seq_printf(m, "watchdog_pretimeouts: %u\n",
2813 smi_get_stat(smi, watchdog_pretimeouts)); 2853 smi_get_stat(smi, watchdog_pretimeouts));
2814 out += sprintf(out, "incoming_messages: %u\n", 2854 seq_printf(m, "incoming_messages: %u\n",
2815 smi_get_stat(smi, incoming_messages)); 2855 smi_get_stat(smi, incoming_messages));
2856 return 0;
2857}
2816 2858
2817 return out - page; 2859static int smi_si_stats_proc_open(struct inode *inode, struct file *file)
2860{
2861 return single_open(file, smi_si_stats_proc_show, PDE(inode)->data);
2818} 2862}
2819 2863
2820static int param_read_proc(char *page, char **start, off_t off, 2864static const struct file_operations smi_si_stats_proc_ops = {
2821 int count, int *eof, void *data) 2865 .open = smi_si_stats_proc_open,
2866 .read = seq_read,
2867 .llseek = seq_lseek,
2868 .release = single_release,
2869};
2870
2871static int smi_params_proc_show(struct seq_file *m, void *v)
2822{ 2872{
2823 struct smi_info *smi = data; 2873 struct smi_info *smi = m->private;
2824 2874
2825 return sprintf(page, 2875 return seq_printf(m,
2826 "%s,%s,0x%lx,rsp=%d,rsi=%d,rsh=%d,irq=%d,ipmb=%d\n", 2876 "%s,%s,0x%lx,rsp=%d,rsi=%d,rsh=%d,irq=%d,ipmb=%d\n",
2827 si_to_str[smi->si_type], 2877 si_to_str[smi->si_type],
2828 addr_space_to_str[smi->io.addr_type], 2878 addr_space_to_str[smi->io.addr_type],
@@ -2834,6 +2884,18 @@ static int param_read_proc(char *page, char **start, off_t off,
2834 smi->slave_addr); 2884 smi->slave_addr);
2835} 2885}
2836 2886
2887static int smi_params_proc_open(struct inode *inode, struct file *file)
2888{
2889 return single_open(file, smi_params_proc_show, PDE(inode)->data);
2890}
2891
2892static const struct file_operations smi_params_proc_ops = {
2893 .open = smi_params_proc_open,
2894 .read = seq_read,
2895 .llseek = seq_lseek,
2896 .release = single_release,
2897};
2898
2837/* 2899/*
2838 * oem_data_avail_to_receive_msg_avail 2900 * oem_data_avail_to_receive_msg_avail
2839 * @info - smi_info structure with msg_flags set 2901 * @info - smi_info structure with msg_flags set
@@ -2900,7 +2962,7 @@ static void return_hosed_msg_badsize(struct smi_info *smi_info)
2900{ 2962{
2901 struct ipmi_smi_msg *msg = smi_info->curr_msg; 2963 struct ipmi_smi_msg *msg = smi_info->curr_msg;
2902 2964
2903 /* Make it a reponse */ 2965 /* Make it a response */
2904 msg->rsp[0] = msg->data[0] | 4; 2966 msg->rsp[0] = msg->data[0] | 4;
2905 msg->rsp[1] = msg->data[1]; 2967 msg->rsp[1] = msg->data[1];
2906 msg->rsp[2] = CANNOT_RETURN_REQUESTED_LENGTH; 2968 msg->rsp[2] = CANNOT_RETURN_REQUESTED_LENGTH;
@@ -3002,7 +3064,7 @@ static __devinitdata struct ipmi_default_vals
3002 { .port = 0 } 3064 { .port = 0 }
3003}; 3065};
3004 3066
3005static __devinit void default_find_bmc(void) 3067static void __devinit default_find_bmc(void)
3006{ 3068{
3007 struct smi_info *info; 3069 struct smi_info *info;
3008 int i; 3070 int i;
@@ -3014,7 +3076,7 @@ static __devinit void default_find_bmc(void)
3014 if (check_legacy_ioport(ipmi_defaults[i].port)) 3076 if (check_legacy_ioport(ipmi_defaults[i].port))
3015 continue; 3077 continue;
3016#endif 3078#endif
3017 info = kzalloc(sizeof(*info), GFP_KERNEL); 3079 info = smi_info_alloc();
3018 if (!info) 3080 if (!info)
3019 return; 3081 return;
3020 3082
@@ -3139,9 +3201,6 @@ static int try_smi_init(struct smi_info *new_smi)
3139 goto out_err; 3201 goto out_err;
3140 } 3202 }
3141 3203
3142 spin_lock_init(&(new_smi->si_lock));
3143 spin_lock_init(&(new_smi->msg_lock));
3144
3145 /* Do low-level detection first. */ 3204 /* Do low-level detection first. */
3146 if (new_smi->handlers->detect(new_smi->si_sm)) { 3205 if (new_smi->handlers->detect(new_smi->si_sm)) {
3147 if (new_smi->addr_source) 3206 if (new_smi->addr_source)
@@ -3230,7 +3289,7 @@ static int try_smi_init(struct smi_info *new_smi)
3230 } 3289 }
3231 3290
3232 rv = ipmi_smi_add_proc_entry(new_smi->intf, "type", 3291 rv = ipmi_smi_add_proc_entry(new_smi->intf, "type",
3233 type_file_read_proc, 3292 &smi_type_proc_ops,
3234 new_smi); 3293 new_smi);
3235 if (rv) { 3294 if (rv) {
3236 dev_err(new_smi->dev, "Unable to create proc entry: %d\n", rv); 3295 dev_err(new_smi->dev, "Unable to create proc entry: %d\n", rv);
@@ -3238,7 +3297,7 @@ static int try_smi_init(struct smi_info *new_smi)
3238 } 3297 }
3239 3298
3240 rv = ipmi_smi_add_proc_entry(new_smi->intf, "si_stats", 3299 rv = ipmi_smi_add_proc_entry(new_smi->intf, "si_stats",
3241 stat_file_read_proc, 3300 &smi_si_stats_proc_ops,
3242 new_smi); 3301 new_smi);
3243 if (rv) { 3302 if (rv) {
3244 dev_err(new_smi->dev, "Unable to create proc entry: %d\n", rv); 3303 dev_err(new_smi->dev, "Unable to create proc entry: %d\n", rv);
@@ -3246,7 +3305,7 @@ static int try_smi_init(struct smi_info *new_smi)
3246 } 3305 }
3247 3306
3248 rv = ipmi_smi_add_proc_entry(new_smi->intf, "params", 3307 rv = ipmi_smi_add_proc_entry(new_smi->intf, "params",
3249 param_read_proc, 3308 &smi_params_proc_ops,
3250 new_smi); 3309 new_smi);
3251 if (rv) { 3310 if (rv) {
3252 dev_err(new_smi->dev, "Unable to create proc entry: %d\n", rv); 3311 dev_err(new_smi->dev, "Unable to create proc entry: %d\n", rv);
@@ -3305,7 +3364,7 @@ static int try_smi_init(struct smi_info *new_smi)
3305 return rv; 3364 return rv;
3306} 3365}
3307 3366
3308static __devinit int init_ipmi_si(void) 3367static int __devinit init_ipmi_si(void)
3309{ 3368{
3310 int i; 3369 int i;
3311 char *str; 3370 char *str;
@@ -3317,8 +3376,7 @@ static __devinit int init_ipmi_si(void)
3317 return 0; 3376 return 0;
3318 initialized = 1; 3377 initialized = 1;
3319 3378
3320 /* Register the device drivers. */ 3379 rv = platform_driver_register(&ipmi_driver);
3321 rv = driver_register(&ipmi_driver.driver);
3322 if (rv) { 3380 if (rv) {
3323 printk(KERN_ERR PFX "Unable to register driver: %d\n", rv); 3381 printk(KERN_ERR PFX "Unable to register driver: %d\n", rv);
3324 return rv; 3382 return rv;
@@ -3342,15 +3400,9 @@ static __devinit int init_ipmi_si(void)
3342 3400
3343 printk(KERN_INFO "IPMI System Interface driver.\n"); 3401 printk(KERN_INFO "IPMI System Interface driver.\n");
3344 3402
3345 hardcode_find_bmc();
3346
3347 /* If the user gave us a device, they presumably want us to use it */ 3403 /* If the user gave us a device, they presumably want us to use it */
3348 mutex_lock(&smi_infos_lock); 3404 if (!hardcode_find_bmc())
3349 if (!list_empty(&smi_infos)) {
3350 mutex_unlock(&smi_infos_lock);
3351 return 0; 3405 return 0;
3352 }
3353 mutex_unlock(&smi_infos_lock);
3354 3406
3355#ifdef CONFIG_PCI 3407#ifdef CONFIG_PCI
3356 rv = pci_register_driver(&ipmi_pci_driver); 3408 rv = pci_register_driver(&ipmi_pci_driver);
@@ -3373,11 +3425,6 @@ static __devinit int init_ipmi_si(void)
3373 spmi_find_bmc(); 3425 spmi_find_bmc();
3374#endif 3426#endif
3375 3427
3376#ifdef CONFIG_PPC_OF
3377 of_register_platform_driver(&ipmi_of_platform_driver);
3378 of_registered = 1;
3379#endif
3380
3381 /* We prefer devices with interrupts, but in the case of a machine 3428 /* We prefer devices with interrupts, but in the case of a machine
3382 with multiple BMCs we assume that there will be several instances 3429 with multiple BMCs we assume that there will be several instances
3383 of a given type so if we succeed in registering a type then also 3430 of a given type so if we succeed in registering a type then also
@@ -3428,16 +3475,7 @@ static __devinit int init_ipmi_si(void)
3428 mutex_lock(&smi_infos_lock); 3475 mutex_lock(&smi_infos_lock);
3429 if (unload_when_empty && list_empty(&smi_infos)) { 3476 if (unload_when_empty && list_empty(&smi_infos)) {
3430 mutex_unlock(&smi_infos_lock); 3477 mutex_unlock(&smi_infos_lock);
3431#ifdef CONFIG_PCI 3478 cleanup_ipmi_si();
3432 if (pci_registered)
3433 pci_unregister_driver(&ipmi_pci_driver);
3434#endif
3435
3436#ifdef CONFIG_PPC_OF
3437 if (of_registered)
3438 of_unregister_platform_driver(&ipmi_of_platform_driver);
3439#endif
3440 driver_unregister(&ipmi_driver.driver);
3441 printk(KERN_WARNING PFX 3479 printk(KERN_WARNING PFX
3442 "Unable to find any System Interface(s)\n"); 3480 "Unable to find any System Interface(s)\n");
3443 return -ENODEV; 3481 return -ENODEV;
@@ -3518,7 +3556,7 @@ static void cleanup_one_si(struct smi_info *to_clean)
3518 kfree(to_clean); 3556 kfree(to_clean);
3519} 3557}
3520 3558
3521static __exit void cleanup_ipmi_si(void) 3559static void cleanup_ipmi_si(void)
3522{ 3560{
3523 struct smi_info *e, *tmp_e; 3561 struct smi_info *e, *tmp_e;
3524 3562
@@ -3534,17 +3572,12 @@ static __exit void cleanup_ipmi_si(void)
3534 pnp_unregister_driver(&ipmi_pnp_driver); 3572 pnp_unregister_driver(&ipmi_pnp_driver);
3535#endif 3573#endif
3536 3574
3537#ifdef CONFIG_PPC_OF 3575 platform_driver_unregister(&ipmi_driver);
3538 if (of_registered)
3539 of_unregister_platform_driver(&ipmi_of_platform_driver);
3540#endif
3541 3576
3542 mutex_lock(&smi_infos_lock); 3577 mutex_lock(&smi_infos_lock);
3543 list_for_each_entry_safe(e, tmp_e, &smi_infos, link) 3578 list_for_each_entry_safe(e, tmp_e, &smi_infos, link)
3544 cleanup_one_si(e); 3579 cleanup_one_si(e);
3545 mutex_unlock(&smi_infos_lock); 3580 mutex_unlock(&smi_infos_lock);
3546
3547 driver_unregister(&ipmi_driver.driver);
3548} 3581}
3549module_exit(cleanup_ipmi_si); 3582module_exit(cleanup_ipmi_si);
3550 3583
diff --git a/drivers/char/ipmi/ipmi_watchdog.c b/drivers/char/ipmi/ipmi_watchdog.c
index 654d566ca57c..320668f4c3aa 100644
--- a/drivers/char/ipmi/ipmi_watchdog.c
+++ b/drivers/char/ipmi/ipmi_watchdog.c
@@ -35,7 +35,7 @@
35#include <linux/moduleparam.h> 35#include <linux/moduleparam.h>
36#include <linux/ipmi.h> 36#include <linux/ipmi.h>
37#include <linux/ipmi_smi.h> 37#include <linux/ipmi_smi.h>
38#include <linux/smp_lock.h> 38#include <linux/mutex.h>
39#include <linux/watchdog.h> 39#include <linux/watchdog.h>
40#include <linux/miscdevice.h> 40#include <linux/miscdevice.h>
41#include <linux/init.h> 41#include <linux/init.h>
@@ -149,6 +149,7 @@
149#define WDIOC_GET_PRETIMEOUT _IOW(WATCHDOG_IOCTL_BASE, 22, int) 149#define WDIOC_GET_PRETIMEOUT _IOW(WATCHDOG_IOCTL_BASE, 22, int)
150#endif 150#endif
151 151
152static DEFINE_MUTEX(ipmi_watchdog_mutex);
152static int nowayout = WATCHDOG_NOWAYOUT; 153static int nowayout = WATCHDOG_NOWAYOUT;
153 154
154static ipmi_user_t watchdog_user; 155static ipmi_user_t watchdog_user;
@@ -748,9 +749,9 @@ static long ipmi_unlocked_ioctl(struct file *file,
748{ 749{
749 int ret; 750 int ret;
750 751
751 lock_kernel(); 752 mutex_lock(&ipmi_watchdog_mutex);
752 ret = ipmi_ioctl(file, cmd, arg); 753 ret = ipmi_ioctl(file, cmd, arg);
753 unlock_kernel(); 754 mutex_unlock(&ipmi_watchdog_mutex);
754 755
755 return ret; 756 return ret;
756} 757}
@@ -844,7 +845,6 @@ static int ipmi_open(struct inode *ino, struct file *filep)
844 if (test_and_set_bit(0, &ipmi_wdog_open)) 845 if (test_and_set_bit(0, &ipmi_wdog_open))
845 return -EBUSY; 846 return -EBUSY;
846 847
847 cycle_kernel_lock();
848 848
849 /* 849 /*
850 * Don't start the timer now, let it start on the 850 * Don't start the timer now, let it start on the
@@ -909,6 +909,7 @@ static const struct file_operations ipmi_wdog_fops = {
909 .open = ipmi_open, 909 .open = ipmi_open,
910 .release = ipmi_close, 910 .release = ipmi_close,
911 .fasync = ipmi_fasync, 911 .fasync = ipmi_fasync,
912 .llseek = no_llseek,
912}; 913};
913 914
914static struct miscdevice ipmi_wdog_miscdev = { 915static struct miscdevice ipmi_wdog_miscdev = {
@@ -1080,7 +1081,7 @@ ipmi_nmi(struct notifier_block *self, unsigned long val, void *data)
1080{ 1081{
1081 struct die_args *args = data; 1082 struct die_args *args = data;
1082 1083
1083 if (val != DIE_NMI) 1084 if (val != DIE_NMIUNKNOWN)
1084 return NOTIFY_OK; 1085 return NOTIFY_OK;
1085 1086
1086 /* Hack, if it's a memory or I/O error, ignore it. */ 1087 /* Hack, if it's a memory or I/O error, ignore it. */
diff --git a/drivers/char/isicom.c b/drivers/char/isicom.c
deleted file mode 100644
index c27e9d21fea9..000000000000
--- a/drivers/char/isicom.c
+++ /dev/null
@@ -1,1736 +0,0 @@
1/*
2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public License
4 * as published by the Free Software Foundation; either version
5 * 2 of the License, or (at your option) any later version.
6 *
7 * Original driver code supplied by Multi-Tech
8 *
9 * Changes
10 * 1/9/98 alan@lxorguk.ukuu.org.uk
11 * Merge to 2.0.x kernel tree
12 * Obtain and use official major/minors
13 * Loader switched to a misc device
14 * (fixed range check bug as a side effect)
15 * Printk clean up
16 * 9/12/98 alan@lxorguk.ukuu.org.uk
17 * Rough port to 2.1.x
18 *
19 * 10/6/99 sameer Merged the ISA and PCI drivers to
20 * a new unified driver.
21 *
22 * 3/9/99 sameer Added support for ISI4616 cards.
23 *
24 * 16/9/99 sameer We do not force RTS low anymore.
25 * This is to prevent the firmware
26 * from getting confused.
27 *
28 * 26/10/99 sameer Cosmetic changes:The driver now
29 * dumps the Port Count information
30 * along with I/O address and IRQ.
31 *
32 * 13/12/99 sameer Fixed the problem with IRQ sharing.
33 *
34 * 10/5/00 sameer Fixed isicom_shutdown_board()
35 * to not lower DTR on all the ports
36 * when the last port on the card is
37 * closed.
38 *
39 * 10/5/00 sameer Signal mask setup command added
40 * to isicom_setup_port and
41 * isicom_shutdown_port.
42 *
43 * 24/5/00 sameer The driver is now SMP aware.
44 *
45 *
46 * 27/11/00 Vinayak P Risbud Fixed the Driver Crash Problem
47 *
48 *
49 * 03/01/01 anil .s Added support for resetting the
50 * internal modems on ISI cards.
51 *
52 * 08/02/01 anil .s Upgraded the driver for kernel
53 * 2.4.x
54 *
55 * 11/04/01 Kevin Fixed firmware load problem with
56 * ISIHP-4X card
57 *
58 * 30/04/01 anil .s Fixed the remote login through
59 * ISI port problem. Now the link
60 * does not go down before password
61 * prompt.
62 *
63 * 03/05/01 anil .s Fixed the problem with IRQ sharing
64 * among ISI-PCI cards.
65 *
66 * 03/05/01 anil .s Added support to display the version
67 * info during insmod as well as module
68 * listing by lsmod.
69 *
70 * 10/05/01 anil .s Done the modifications to the source
71 * file and Install script so that the
72 * same installation can be used for
73 * 2.2.x and 2.4.x kernel.
74 *
75 * 06/06/01 anil .s Now we drop both dtr and rts during
76 * shutdown_port as well as raise them
77 * during isicom_config_port.
78 *
79 * 09/06/01 acme@conectiva.com.br use capable, not suser, do
80 * restore_flags on failure in
81 * isicom_send_break, verify put_user
82 * result
83 *
84 * 11/02/03 ranjeeth Added support for 230 Kbps and 460 Kbps
85 * Baud index extended to 21
86 *
87 * 20/03/03 ranjeeth Made to work for Linux Advanced server.
88 * Taken care of license warning.
89 *
90 * 10/12/03 Ravindra Made to work for Fedora Core 1 of
91 * Red Hat Distribution
92 *
93 * 06/01/05 Alan Cox Merged the ISI and base kernel strands
94 * into a single 2.6 driver
95 *
96 * ***********************************************************
97 *
98 * To use this driver you also need the support package. You
99 * can find this in RPM format on
100 * ftp://ftp.linux.org.uk/pub/linux/alan
101 *
102 * You can find the original tools for this direct from Multitech
103 * ftp://ftp.multitech.com/ISI-Cards/
104 *
105 * Having installed the cards the module options (/etc/modprobe.conf)
106 *
107 * options isicom io=card1,card2,card3,card4 irq=card1,card2,card3,card4
108 *
109 * Omit those entries for boards you don't have installed.
110 *
111 * TODO
112 * Merge testing
113 * 64-bit verification
114 */
115
116#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
117
118#include <linux/module.h>
119#include <linux/firmware.h>
120#include <linux/kernel.h>
121#include <linux/tty.h>
122#include <linux/tty_flip.h>
123#include <linux/termios.h>
124#include <linux/fs.h>
125#include <linux/sched.h>
126#include <linux/serial.h>
127#include <linux/mm.h>
128#include <linux/interrupt.h>
129#include <linux/timer.h>
130#include <linux/delay.h>
131#include <linux/ioport.h>
132#include <linux/slab.h>
133
134#include <linux/uaccess.h>
135#include <linux/io.h>
136#include <asm/system.h>
137
138#include <linux/pci.h>
139
140#include <linux/isicom.h>
141
142#define InterruptTheCard(base) outw(0, (base) + 0xc)
143#define ClearInterrupt(base) inw((base) + 0x0a)
144
145#ifdef DEBUG
146#define isicom_paranoia_check(a, b, c) __isicom_paranoia_check((a), (b), (c))
147#else
148#define isicom_paranoia_check(a, b, c) 0
149#endif
150
151static int isicom_probe(struct pci_dev *, const struct pci_device_id *);
152static void __devexit isicom_remove(struct pci_dev *);
153
154static struct pci_device_id isicom_pci_tbl[] = {
155 { PCI_DEVICE(VENDOR_ID, 0x2028) },
156 { PCI_DEVICE(VENDOR_ID, 0x2051) },
157 { PCI_DEVICE(VENDOR_ID, 0x2052) },
158 { PCI_DEVICE(VENDOR_ID, 0x2053) },
159 { PCI_DEVICE(VENDOR_ID, 0x2054) },
160 { PCI_DEVICE(VENDOR_ID, 0x2055) },
161 { PCI_DEVICE(VENDOR_ID, 0x2056) },
162 { PCI_DEVICE(VENDOR_ID, 0x2057) },
163 { PCI_DEVICE(VENDOR_ID, 0x2058) },
164 { 0 }
165};
166MODULE_DEVICE_TABLE(pci, isicom_pci_tbl);
167
168static struct pci_driver isicom_driver = {
169 .name = "isicom",
170 .id_table = isicom_pci_tbl,
171 .probe = isicom_probe,
172 .remove = __devexit_p(isicom_remove)
173};
174
175static int prev_card = 3; /* start servicing isi_card[0] */
176static struct tty_driver *isicom_normal;
177
178static void isicom_tx(unsigned long _data);
179static void isicom_start(struct tty_struct *tty);
180
181static DEFINE_TIMER(tx, isicom_tx, 0, 0);
182
183/* baud index mappings from linux defns to isi */
184
185static signed char linuxb_to_isib[] = {
186 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 13, 15, 16, 17, 18, 19, 20, 21
187};
188
189struct isi_board {
190 unsigned long base;
191 int irq;
192 unsigned char port_count;
193 unsigned short status;
194 unsigned short port_status; /* each bit for each port */
195 unsigned short shift_count;
196 struct isi_port *ports;
197 signed char count;
198 spinlock_t card_lock; /* Card wide lock 11/5/00 -sameer */
199 unsigned long flags;
200 unsigned int index;
201};
202
203struct isi_port {
204 unsigned short magic;
205 struct tty_port port;
206 u16 channel;
207 u16 status;
208 struct isi_board *card;
209 unsigned char *xmit_buf;
210 int xmit_head;
211 int xmit_tail;
212 int xmit_cnt;
213};
214
215static struct isi_board isi_card[BOARD_COUNT];
216static struct isi_port isi_ports[PORT_COUNT];
217
218/*
219 * Locking functions for card level locking. We need to own both
220 * the kernel lock for the card and have the card in a position that
221 * it wants to talk.
222 */
223
224static inline int WaitTillCardIsFree(unsigned long base)
225{
226 unsigned int count = 0;
227 unsigned int a = in_atomic(); /* do we run under spinlock? */
228
229 while (!(inw(base + 0xe) & 0x1) && count++ < 100)
230 if (a)
231 mdelay(1);
232 else
233 msleep(1);
234
235 return !(inw(base + 0xe) & 0x1);
236}
237
238static int lock_card(struct isi_board *card)
239{
240 unsigned long base = card->base;
241 unsigned int retries, a;
242
243 for (retries = 0; retries < 10; retries++) {
244 spin_lock_irqsave(&card->card_lock, card->flags);
245 for (a = 0; a < 10; a++) {
246 if (inw(base + 0xe) & 0x1)
247 return 1;
248 udelay(10);
249 }
250 spin_unlock_irqrestore(&card->card_lock, card->flags);
251 msleep(10);
252 }
253 pr_warning("Failed to lock Card (0x%lx)\n", card->base);
254
255 return 0; /* Failed to acquire the card! */
256}
257
258static void unlock_card(struct isi_board *card)
259{
260 spin_unlock_irqrestore(&card->card_lock, card->flags);
261}
262
263/*
264 * ISI Card specific ops ...
265 */
266
267/* card->lock HAS to be held */
268static void raise_dtr(struct isi_port *port)
269{
270 struct isi_board *card = port->card;
271 unsigned long base = card->base;
272 u16 channel = port->channel;
273
274 if (WaitTillCardIsFree(base))
275 return;
276
277 outw(0x8000 | (channel << card->shift_count) | 0x02, base);
278 outw(0x0504, base);
279 InterruptTheCard(base);
280 port->status |= ISI_DTR;
281}
282
283/* card->lock HAS to be held */
284static inline void drop_dtr(struct isi_port *port)
285{
286 struct isi_board *card = port->card;
287 unsigned long base = card->base;
288 u16 channel = port->channel;
289
290 if (WaitTillCardIsFree(base))
291 return;
292
293 outw(0x8000 | (channel << card->shift_count) | 0x02, base);
294 outw(0x0404, base);
295 InterruptTheCard(base);
296 port->status &= ~ISI_DTR;
297}
298
299/* card->lock HAS to be held */
300static inline void raise_rts(struct isi_port *port)
301{
302 struct isi_board *card = port->card;
303 unsigned long base = card->base;
304 u16 channel = port->channel;
305
306 if (WaitTillCardIsFree(base))
307 return;
308
309 outw(0x8000 | (channel << card->shift_count) | 0x02, base);
310 outw(0x0a04, base);
311 InterruptTheCard(base);
312 port->status |= ISI_RTS;
313}
314
315/* card->lock HAS to be held */
316static inline void drop_rts(struct isi_port *port)
317{
318 struct isi_board *card = port->card;
319 unsigned long base = card->base;
320 u16 channel = port->channel;
321
322 if (WaitTillCardIsFree(base))
323 return;
324
325 outw(0x8000 | (channel << card->shift_count) | 0x02, base);
326 outw(0x0804, base);
327 InterruptTheCard(base);
328 port->status &= ~ISI_RTS;
329}
330
331/* card->lock MUST NOT be held */
332
333static void isicom_dtr_rts(struct tty_port *port, int on)
334{
335 struct isi_port *ip = container_of(port, struct isi_port, port);
336 struct isi_board *card = ip->card;
337 unsigned long base = card->base;
338 u16 channel = ip->channel;
339
340 if (!lock_card(card))
341 return;
342
343 if (on) {
344 outw(0x8000 | (channel << card->shift_count) | 0x02, base);
345 outw(0x0f04, base);
346 InterruptTheCard(base);
347 ip->status |= (ISI_DTR | ISI_RTS);
348 } else {
349 outw(0x8000 | (channel << card->shift_count) | 0x02, base);
350 outw(0x0C04, base);
351 InterruptTheCard(base);
352 ip->status &= ~(ISI_DTR | ISI_RTS);
353 }
354 unlock_card(card);
355}
356
357/* card->lock HAS to be held */
358static void drop_dtr_rts(struct isi_port *port)
359{
360 struct isi_board *card = port->card;
361 unsigned long base = card->base;
362 u16 channel = port->channel;
363
364 if (WaitTillCardIsFree(base))
365 return;
366
367 outw(0x8000 | (channel << card->shift_count) | 0x02, base);
368 outw(0x0c04, base);
369 InterruptTheCard(base);
370 port->status &= ~(ISI_RTS | ISI_DTR);
371}
372
373/*
374 * ISICOM Driver specific routines ...
375 *
376 */
377
378static inline int __isicom_paranoia_check(struct isi_port const *port,
379 char *name, const char *routine)
380{
381 if (!port) {
382 pr_warning("Warning: bad isicom magic for dev %s in %s.\n",
383 name, routine);
384 return 1;
385 }
386 if (port->magic != ISICOM_MAGIC) {
387 pr_warning("Warning: NULL isicom port for dev %s in %s.\n",
388 name, routine);
389 return 1;
390 }
391
392 return 0;
393}
394
395/*
396 * Transmitter.
397 *
398 * We shovel data into the card buffers on a regular basis. The card
399 * will do the rest of the work for us.
400 */
401
402static void isicom_tx(unsigned long _data)
403{
404 unsigned long flags, base;
405 unsigned int retries;
406 short count = (BOARD_COUNT-1), card;
407 short txcount, wrd, residue, word_count, cnt;
408 struct isi_port *port;
409 struct tty_struct *tty;
410
411 /* find next active board */
412 card = (prev_card + 1) & 0x0003;
413 while (count-- > 0) {
414 if (isi_card[card].status & BOARD_ACTIVE)
415 break;
416 card = (card + 1) & 0x0003;
417 }
418 if (!(isi_card[card].status & BOARD_ACTIVE))
419 goto sched_again;
420
421 prev_card = card;
422
423 count = isi_card[card].port_count;
424 port = isi_card[card].ports;
425 base = isi_card[card].base;
426
427 spin_lock_irqsave(&isi_card[card].card_lock, flags);
428 for (retries = 0; retries < 100; retries++) {
429 if (inw(base + 0xe) & 0x1)
430 break;
431 udelay(2);
432 }
433 if (retries >= 100)
434 goto unlock;
435
436 tty = tty_port_tty_get(&port->port);
437 if (tty == NULL)
438 goto put_unlock;
439
440 for (; count > 0; count--, port++) {
441 /* port not active or tx disabled to force flow control */
442 if (!(port->port.flags & ASYNC_INITIALIZED) ||
443 !(port->status & ISI_TXOK))
444 continue;
445
446 txcount = min_t(short, TX_SIZE, port->xmit_cnt);
447 if (txcount <= 0 || tty->stopped || tty->hw_stopped)
448 continue;
449
450 if (!(inw(base + 0x02) & (1 << port->channel)))
451 continue;
452
453 pr_debug("txing %d bytes, port%d.\n",
454 txcount, port->channel + 1);
455 outw((port->channel << isi_card[card].shift_count) | txcount,
456 base);
457 residue = NO;
458 wrd = 0;
459 while (1) {
460 cnt = min_t(int, txcount, (SERIAL_XMIT_SIZE
461 - port->xmit_tail));
462 if (residue == YES) {
463 residue = NO;
464 if (cnt > 0) {
465 wrd |= (port->port.xmit_buf[port->xmit_tail]
466 << 8);
467 port->xmit_tail = (port->xmit_tail + 1)
468 & (SERIAL_XMIT_SIZE - 1);
469 port->xmit_cnt--;
470 txcount--;
471 cnt--;
472 outw(wrd, base);
473 } else {
474 outw(wrd, base);
475 break;
476 }
477 }
478 if (cnt <= 0)
479 break;
480 word_count = cnt >> 1;
481 outsw(base, port->port.xmit_buf+port->xmit_tail, word_count);
482 port->xmit_tail = (port->xmit_tail
483 + (word_count << 1)) & (SERIAL_XMIT_SIZE - 1);
484 txcount -= (word_count << 1);
485 port->xmit_cnt -= (word_count << 1);
486 if (cnt & 0x0001) {
487 residue = YES;
488 wrd = port->port.xmit_buf[port->xmit_tail];
489 port->xmit_tail = (port->xmit_tail + 1)
490 & (SERIAL_XMIT_SIZE - 1);
491 port->xmit_cnt--;
492 txcount--;
493 }
494 }
495
496 InterruptTheCard(base);
497 if (port->xmit_cnt <= 0)
498 port->status &= ~ISI_TXOK;
499 if (port->xmit_cnt <= WAKEUP_CHARS)
500 tty_wakeup(tty);
501 }
502
503put_unlock:
504 tty_kref_put(tty);
505unlock:
506 spin_unlock_irqrestore(&isi_card[card].card_lock, flags);
507 /* schedule another tx for hopefully in about 10ms */
508sched_again:
509 mod_timer(&tx, jiffies + msecs_to_jiffies(10));
510}
511
512/*
513 * Main interrupt handler routine
514 */
515
516static irqreturn_t isicom_interrupt(int irq, void *dev_id)
517{
518 struct isi_board *card = dev_id;
519 struct isi_port *port;
520 struct tty_struct *tty;
521 unsigned long base;
522 u16 header, word_count, count, channel;
523 short byte_count;
524 unsigned char *rp;
525
526 if (!card || !(card->status & FIRMWARE_LOADED))
527 return IRQ_NONE;
528
529 base = card->base;
530
531 /* did the card interrupt us? */
532 if (!(inw(base + 0x0e) & 0x02))
533 return IRQ_NONE;
534
535 spin_lock(&card->card_lock);
536
537 /*
538 * disable any interrupts from the PCI card and lower the
539 * interrupt line
540 */
541 outw(0x8000, base+0x04);
542 ClearInterrupt(base);
543
544 inw(base); /* get the dummy word out */
545 header = inw(base);
546 channel = (header & 0x7800) >> card->shift_count;
547 byte_count = header & 0xff;
548
549 if (channel + 1 > card->port_count) {
550 pr_warning("%s(0x%lx): %d(channel) > port_count.\n",
551 __func__, base, channel+1);
552 outw(0x0000, base+0x04); /* enable interrupts */
553 spin_unlock(&card->card_lock);
554 return IRQ_HANDLED;
555 }
556 port = card->ports + channel;
557 if (!(port->port.flags & ASYNC_INITIALIZED)) {
558 outw(0x0000, base+0x04); /* enable interrupts */
559 spin_unlock(&card->card_lock);
560 return IRQ_HANDLED;
561 }
562
563 tty = tty_port_tty_get(&port->port);
564 if (tty == NULL) {
565 word_count = byte_count >> 1;
566 while (byte_count > 1) {
567 inw(base);
568 byte_count -= 2;
569 }
570 if (byte_count & 0x01)
571 inw(base);
572 outw(0x0000, base+0x04); /* enable interrupts */
573 spin_unlock(&card->card_lock);
574 return IRQ_HANDLED;
575 }
576
577 if (header & 0x8000) { /* Status Packet */
578 header = inw(base);
579 switch (header & 0xff) {
580 case 0: /* Change in EIA signals */
581 if (port->port.flags & ASYNC_CHECK_CD) {
582 if (port->status & ISI_DCD) {
583 if (!(header & ISI_DCD)) {
584 /* Carrier has been lost */
585 pr_debug("%s: DCD->low.\n",
586 __func__);
587 port->status &= ~ISI_DCD;
588 tty_hangup(tty);
589 }
590 } else if (header & ISI_DCD) {
591 /* Carrier has been detected */
592 pr_debug("%s: DCD->high.\n",
593 __func__);
594 port->status |= ISI_DCD;
595 wake_up_interruptible(&port->port.open_wait);
596 }
597 } else {
598 if (header & ISI_DCD)
599 port->status |= ISI_DCD;
600 else
601 port->status &= ~ISI_DCD;
602 }
603
604 if (port->port.flags & ASYNC_CTS_FLOW) {
605 if (tty->hw_stopped) {
606 if (header & ISI_CTS) {
607 port->port.tty->hw_stopped = 0;
608 /* start tx ing */
609 port->status |= (ISI_TXOK
610 | ISI_CTS);
611 tty_wakeup(tty);
612 }
613 } else if (!(header & ISI_CTS)) {
614 tty->hw_stopped = 1;
615 /* stop tx ing */
616 port->status &= ~(ISI_TXOK | ISI_CTS);
617 }
618 } else {
619 if (header & ISI_CTS)
620 port->status |= ISI_CTS;
621 else
622 port->status &= ~ISI_CTS;
623 }
624
625 if (header & ISI_DSR)
626 port->status |= ISI_DSR;
627 else
628 port->status &= ~ISI_DSR;
629
630 if (header & ISI_RI)
631 port->status |= ISI_RI;
632 else
633 port->status &= ~ISI_RI;
634
635 break;
636
637 case 1: /* Received Break !!! */
638 tty_insert_flip_char(tty, 0, TTY_BREAK);
639 if (port->port.flags & ASYNC_SAK)
640 do_SAK(tty);
641 tty_flip_buffer_push(tty);
642 break;
643
644 case 2: /* Statistics */
645 pr_debug("%s: stats!!!\n", __func__);
646 break;
647
648 default:
649 pr_debug("%s: Unknown code in status packet.\n",
650 __func__);
651 break;
652 }
653 } else { /* Data Packet */
654
655 count = tty_prepare_flip_string(tty, &rp, byte_count & ~1);
656 pr_debug("%s: Can rx %d of %d bytes.\n",
657 __func__, count, byte_count);
658 word_count = count >> 1;
659 insw(base, rp, word_count);
660 byte_count -= (word_count << 1);
661 if (count & 0x0001) {
662 tty_insert_flip_char(tty, inw(base) & 0xff,
663 TTY_NORMAL);
664 byte_count -= 2;
665 }
666 if (byte_count > 0) {
667 pr_debug("%s(0x%lx:%d): Flip buffer overflow! dropping bytes...\n",
668 __func__, base, channel + 1);
669 /* drain out unread xtra data */
670 while (byte_count > 0) {
671 inw(base);
672 byte_count -= 2;
673 }
674 }
675 tty_flip_buffer_push(tty);
676 }
677 outw(0x0000, base+0x04); /* enable interrupts */
678 spin_unlock(&card->card_lock);
679 tty_kref_put(tty);
680
681 return IRQ_HANDLED;
682}
683
684static void isicom_config_port(struct tty_struct *tty)
685{
686 struct isi_port *port = tty->driver_data;
687 struct isi_board *card = port->card;
688 unsigned long baud;
689 unsigned long base = card->base;
690 u16 channel_setup, channel = port->channel,
691 shift_count = card->shift_count;
692 unsigned char flow_ctrl;
693
694 /* FIXME: Switch to new tty baud API */
695 baud = C_BAUD(tty);
696 if (baud & CBAUDEX) {
697 baud &= ~CBAUDEX;
698
699 /* if CBAUDEX bit is on and the baud is set to either 50 or 75
700 * then the card is programmed for 57.6Kbps or 115Kbps
701 * respectively.
702 */
703
704 /* 1,2,3,4 => 57.6, 115.2, 230, 460 kbps resp. */
705 if (baud < 1 || baud > 4)
706 tty->termios->c_cflag &= ~CBAUDEX;
707 else
708 baud += 15;
709 }
710 if (baud == 15) {
711
712 /* the ASYNC_SPD_HI and ASYNC_SPD_VHI options are set
713 * by the set_serial_info ioctl ... this is done by
714 * the 'setserial' utility.
715 */
716
717 if ((port->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
718 baud++; /* 57.6 Kbps */
719 if ((port->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
720 baud += 2; /* 115 Kbps */
721 if ((port->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
722 baud += 3; /* 230 kbps*/
723 if ((port->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
724 baud += 4; /* 460 kbps*/
725 }
726 if (linuxb_to_isib[baud] == -1) {
727 /* hang up */
728 drop_dtr(port);
729 return;
730 } else
731 raise_dtr(port);
732
733 if (WaitTillCardIsFree(base) == 0) {
734 outw(0x8000 | (channel << shift_count) | 0x03, base);
735 outw(linuxb_to_isib[baud] << 8 | 0x03, base);
736 channel_setup = 0;
737 switch (C_CSIZE(tty)) {
738 case CS5:
739 channel_setup |= ISICOM_CS5;
740 break;
741 case CS6:
742 channel_setup |= ISICOM_CS6;
743 break;
744 case CS7:
745 channel_setup |= ISICOM_CS7;
746 break;
747 case CS8:
748 channel_setup |= ISICOM_CS8;
749 break;
750 }
751
752 if (C_CSTOPB(tty))
753 channel_setup |= ISICOM_2SB;
754 if (C_PARENB(tty)) {
755 channel_setup |= ISICOM_EVPAR;
756 if (C_PARODD(tty))
757 channel_setup |= ISICOM_ODPAR;
758 }
759 outw(channel_setup, base);
760 InterruptTheCard(base);
761 }
762 if (C_CLOCAL(tty))
763 port->port.flags &= ~ASYNC_CHECK_CD;
764 else
765 port->port.flags |= ASYNC_CHECK_CD;
766
767 /* flow control settings ...*/
768 flow_ctrl = 0;
769 port->port.flags &= ~ASYNC_CTS_FLOW;
770 if (C_CRTSCTS(tty)) {
771 port->port.flags |= ASYNC_CTS_FLOW;
772 flow_ctrl |= ISICOM_CTSRTS;
773 }
774 if (I_IXON(tty))
775 flow_ctrl |= ISICOM_RESPOND_XONXOFF;
776 if (I_IXOFF(tty))
777 flow_ctrl |= ISICOM_INITIATE_XONXOFF;
778
779 if (WaitTillCardIsFree(base) == 0) {
780 outw(0x8000 | (channel << shift_count) | 0x04, base);
781 outw(flow_ctrl << 8 | 0x05, base);
782 outw((STOP_CHAR(tty)) << 8 | (START_CHAR(tty)), base);
783 InterruptTheCard(base);
784 }
785
786 /* rx enabled -> enable port for rx on the card */
787 if (C_CREAD(tty)) {
788 card->port_status |= (1 << channel);
789 outw(card->port_status, base + 0x02);
790 }
791}
792
793/* open et all */
794
795static inline void isicom_setup_board(struct isi_board *bp)
796{
797 int channel;
798 struct isi_port *port;
799
800 bp->count++;
801 if (!(bp->status & BOARD_INIT)) {
802 port = bp->ports;
803 for (channel = 0; channel < bp->port_count; channel++, port++)
804 drop_dtr_rts(port);
805 }
806 bp->status |= BOARD_ACTIVE | BOARD_INIT;
807}
808
809/* Activate and thus setup board are protected from races against shutdown
810 by the tty_port mutex */
811
812static int isicom_activate(struct tty_port *tport, struct tty_struct *tty)
813{
814 struct isi_port *port = container_of(tport, struct isi_port, port);
815 struct isi_board *card = port->card;
816 unsigned long flags;
817
818 if (tty_port_alloc_xmit_buf(tport) < 0)
819 return -ENOMEM;
820
821 spin_lock_irqsave(&card->card_lock, flags);
822 isicom_setup_board(card);
823
824 port->xmit_cnt = port->xmit_head = port->xmit_tail = 0;
825
826 /* discard any residual data */
827 if (WaitTillCardIsFree(card->base) == 0) {
828 outw(0x8000 | (port->channel << card->shift_count) | 0x02,
829 card->base);
830 outw(((ISICOM_KILLTX | ISICOM_KILLRX) << 8) | 0x06, card->base);
831 InterruptTheCard(card->base);
832 }
833 isicom_config_port(tty);
834 spin_unlock_irqrestore(&card->card_lock, flags);
835
836 return 0;
837}
838
839static int isicom_carrier_raised(struct tty_port *port)
840{
841 struct isi_port *ip = container_of(port, struct isi_port, port);
842 return (ip->status & ISI_DCD)?1 : 0;
843}
844
845static struct tty_port *isicom_find_port(struct tty_struct *tty)
846{
847 struct isi_port *port;
848 struct isi_board *card;
849 unsigned int board;
850 int line = tty->index;
851
852 if (line < 0 || line > PORT_COUNT-1)
853 return NULL;
854 board = BOARD(line);
855 card = &isi_card[board];
856
857 if (!(card->status & FIRMWARE_LOADED))
858 return NULL;
859
860 /* open on a port greater than the port count for the card !!! */
861 if (line > ((board * 16) + card->port_count - 1))
862 return NULL;
863
864 port = &isi_ports[line];
865 if (isicom_paranoia_check(port, tty->name, "isicom_open"))
866 return NULL;
867
868 return &port->port;
869}
870
871static int isicom_open(struct tty_struct *tty, struct file *filp)
872{
873 struct isi_port *port;
874 struct tty_port *tport;
875
876 tport = isicom_find_port(tty);
877 if (tport == NULL)
878 return -ENODEV;
879 port = container_of(tport, struct isi_port, port);
880
881 tty->driver_data = port;
882 return tty_port_open(tport, tty, filp);
883}
884
885/* close et all */
886
887/* card->lock HAS to be held */
888static void isicom_shutdown_port(struct isi_port *port)
889{
890 struct isi_board *card = port->card;
891
892 if (--card->count < 0) {
893 pr_debug("%s: bad board(0x%lx) count %d.\n",
894 __func__, card->base, card->count);
895 card->count = 0;
896 }
897 /* last port was closed, shutdown that board too */
898 if (!card->count)
899 card->status &= BOARD_ACTIVE;
900}
901
902static void isicom_flush_buffer(struct tty_struct *tty)
903{
904 struct isi_port *port = tty->driver_data;
905 struct isi_board *card = port->card;
906 unsigned long flags;
907
908 if (isicom_paranoia_check(port, tty->name, "isicom_flush_buffer"))
909 return;
910
911 spin_lock_irqsave(&card->card_lock, flags);
912 port->xmit_cnt = port->xmit_head = port->xmit_tail = 0;
913 spin_unlock_irqrestore(&card->card_lock, flags);
914
915 tty_wakeup(tty);
916}
917
918static void isicom_shutdown(struct tty_port *port)
919{
920 struct isi_port *ip = container_of(port, struct isi_port, port);
921 struct isi_board *card = ip->card;
922 unsigned long flags;
923
924 /* indicate to the card that no more data can be received
925 on this port */
926 spin_lock_irqsave(&card->card_lock, flags);
927 card->port_status &= ~(1 << ip->channel);
928 outw(card->port_status, card->base + 0x02);
929 isicom_shutdown_port(ip);
930 spin_unlock_irqrestore(&card->card_lock, flags);
931 tty_port_free_xmit_buf(port);
932}
933
934static void isicom_close(struct tty_struct *tty, struct file *filp)
935{
936 struct isi_port *ip = tty->driver_data;
937 struct tty_port *port;
938
939 if (ip == NULL)
940 return;
941
942 port = &ip->port;
943 if (isicom_paranoia_check(ip, tty->name, "isicom_close"))
944 return;
945 tty_port_close(port, tty, filp);
946}
947
948/* write et all */
949static int isicom_write(struct tty_struct *tty, const unsigned char *buf,
950 int count)
951{
952 struct isi_port *port = tty->driver_data;
953 struct isi_board *card = port->card;
954 unsigned long flags;
955 int cnt, total = 0;
956
957 if (isicom_paranoia_check(port, tty->name, "isicom_write"))
958 return 0;
959
960 spin_lock_irqsave(&card->card_lock, flags);
961
962 while (1) {
963 cnt = min_t(int, count, min(SERIAL_XMIT_SIZE - port->xmit_cnt
964 - 1, SERIAL_XMIT_SIZE - port->xmit_head));
965 if (cnt <= 0)
966 break;
967
968 memcpy(port->port.xmit_buf + port->xmit_head, buf, cnt);
969 port->xmit_head = (port->xmit_head + cnt) & (SERIAL_XMIT_SIZE
970 - 1);
971 port->xmit_cnt += cnt;
972 buf += cnt;
973 count -= cnt;
974 total += cnt;
975 }
976 if (port->xmit_cnt && !tty->stopped && !tty->hw_stopped)
977 port->status |= ISI_TXOK;
978 spin_unlock_irqrestore(&card->card_lock, flags);
979 return total;
980}
981
982/* put_char et all */
983static int isicom_put_char(struct tty_struct *tty, unsigned char ch)
984{
985 struct isi_port *port = tty->driver_data;
986 struct isi_board *card = port->card;
987 unsigned long flags;
988
989 if (isicom_paranoia_check(port, tty->name, "isicom_put_char"))
990 return 0;
991
992 spin_lock_irqsave(&card->card_lock, flags);
993 if (port->xmit_cnt >= SERIAL_XMIT_SIZE - 1) {
994 spin_unlock_irqrestore(&card->card_lock, flags);
995 return 0;
996 }
997
998 port->port.xmit_buf[port->xmit_head++] = ch;
999 port->xmit_head &= (SERIAL_XMIT_SIZE - 1);
1000 port->xmit_cnt++;
1001 spin_unlock_irqrestore(&card->card_lock, flags);
1002 return 1;
1003}
1004
1005/* flush_chars et all */
1006static void isicom_flush_chars(struct tty_struct *tty)
1007{
1008 struct isi_port *port = tty->driver_data;
1009
1010 if (isicom_paranoia_check(port, tty->name, "isicom_flush_chars"))
1011 return;
1012
1013 if (port->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
1014 !port->port.xmit_buf)
1015 return;
1016
1017 /* this tells the transmitter to consider this port for
1018 data output to the card ... that's the best we can do. */
1019 port->status |= ISI_TXOK;
1020}
1021
1022/* write_room et all */
1023static int isicom_write_room(struct tty_struct *tty)
1024{
1025 struct isi_port *port = tty->driver_data;
1026 int free;
1027
1028 if (isicom_paranoia_check(port, tty->name, "isicom_write_room"))
1029 return 0;
1030
1031 free = SERIAL_XMIT_SIZE - port->xmit_cnt - 1;
1032 if (free < 0)
1033 free = 0;
1034 return free;
1035}
1036
1037/* chars_in_buffer et all */
1038static int isicom_chars_in_buffer(struct tty_struct *tty)
1039{
1040 struct isi_port *port = tty->driver_data;
1041 if (isicom_paranoia_check(port, tty->name, "isicom_chars_in_buffer"))
1042 return 0;
1043 return port->xmit_cnt;
1044}
1045
1046/* ioctl et all */
1047static int isicom_send_break(struct tty_struct *tty, int length)
1048{
1049 struct isi_port *port = tty->driver_data;
1050 struct isi_board *card = port->card;
1051 unsigned long base = card->base;
1052
1053 if (length == -1)
1054 return -EOPNOTSUPP;
1055
1056 if (!lock_card(card))
1057 return -EINVAL;
1058
1059 outw(0x8000 | ((port->channel) << (card->shift_count)) | 0x3, base);
1060 outw((length & 0xff) << 8 | 0x00, base);
1061 outw((length & 0xff00), base);
1062 InterruptTheCard(base);
1063
1064 unlock_card(card);
1065 return 0;
1066}
1067
1068static int isicom_tiocmget(struct tty_struct *tty, struct file *file)
1069{
1070 struct isi_port *port = tty->driver_data;
1071 /* just send the port status */
1072 u16 status = port->status;
1073
1074 if (isicom_paranoia_check(port, tty->name, "isicom_ioctl"))
1075 return -ENODEV;
1076
1077 return ((status & ISI_RTS) ? TIOCM_RTS : 0) |
1078 ((status & ISI_DTR) ? TIOCM_DTR : 0) |
1079 ((status & ISI_DCD) ? TIOCM_CAR : 0) |
1080 ((status & ISI_DSR) ? TIOCM_DSR : 0) |
1081 ((status & ISI_CTS) ? TIOCM_CTS : 0) |
1082 ((status & ISI_RI ) ? TIOCM_RI : 0);
1083}
1084
1085static int isicom_tiocmset(struct tty_struct *tty, struct file *file,
1086 unsigned int set, unsigned int clear)
1087{
1088 struct isi_port *port = tty->driver_data;
1089 unsigned long flags;
1090
1091 if (isicom_paranoia_check(port, tty->name, "isicom_ioctl"))
1092 return -ENODEV;
1093
1094 spin_lock_irqsave(&port->card->card_lock, flags);
1095 if (set & TIOCM_RTS)
1096 raise_rts(port);
1097 if (set & TIOCM_DTR)
1098 raise_dtr(port);
1099
1100 if (clear & TIOCM_RTS)
1101 drop_rts(port);
1102 if (clear & TIOCM_DTR)
1103 drop_dtr(port);
1104 spin_unlock_irqrestore(&port->card->card_lock, flags);
1105
1106 return 0;
1107}
1108
1109static int isicom_set_serial_info(struct tty_struct *tty,
1110 struct serial_struct __user *info)
1111{
1112 struct isi_port *port = tty->driver_data;
1113 struct serial_struct newinfo;
1114 int reconfig_port;
1115
1116 if (copy_from_user(&newinfo, info, sizeof(newinfo)))
1117 return -EFAULT;
1118
1119 mutex_lock(&port->port.mutex);
1120 reconfig_port = ((port->port.flags & ASYNC_SPD_MASK) !=
1121 (newinfo.flags & ASYNC_SPD_MASK));
1122
1123 if (!capable(CAP_SYS_ADMIN)) {
1124 if ((newinfo.close_delay != port->port.close_delay) ||
1125 (newinfo.closing_wait != port->port.closing_wait) ||
1126 ((newinfo.flags & ~ASYNC_USR_MASK) !=
1127 (port->port.flags & ~ASYNC_USR_MASK))) {
1128 mutex_unlock(&port->port.mutex);
1129 return -EPERM;
1130 }
1131 port->port.flags = ((port->port.flags & ~ASYNC_USR_MASK) |
1132 (newinfo.flags & ASYNC_USR_MASK));
1133 } else {
1134 port->port.close_delay = newinfo.close_delay;
1135 port->port.closing_wait = newinfo.closing_wait;
1136 port->port.flags = ((port->port.flags & ~ASYNC_FLAGS) |
1137 (newinfo.flags & ASYNC_FLAGS));
1138 }
1139 if (reconfig_port) {
1140 unsigned long flags;
1141 spin_lock_irqsave(&port->card->card_lock, flags);
1142 isicom_config_port(tty);
1143 spin_unlock_irqrestore(&port->card->card_lock, flags);
1144 }
1145 mutex_unlock(&port->port.mutex);
1146 return 0;
1147}
1148
1149static int isicom_get_serial_info(struct isi_port *port,
1150 struct serial_struct __user *info)
1151{
1152 struct serial_struct out_info;
1153
1154 mutex_lock(&port->port.mutex);
1155 memset(&out_info, 0, sizeof(out_info));
1156/* out_info.type = ? */
1157 out_info.line = port - isi_ports;
1158 out_info.port = port->card->base;
1159 out_info.irq = port->card->irq;
1160 out_info.flags = port->port.flags;
1161/* out_info.baud_base = ? */
1162 out_info.close_delay = port->port.close_delay;
1163 out_info.closing_wait = port->port.closing_wait;
1164 mutex_unlock(&port->port.mutex);
1165 if (copy_to_user(info, &out_info, sizeof(out_info)))
1166 return -EFAULT;
1167 return 0;
1168}
1169
1170static int isicom_ioctl(struct tty_struct *tty, struct file *filp,
1171 unsigned int cmd, unsigned long arg)
1172{
1173 struct isi_port *port = tty->driver_data;
1174 void __user *argp = (void __user *)arg;
1175
1176 if (isicom_paranoia_check(port, tty->name, "isicom_ioctl"))
1177 return -ENODEV;
1178
1179 switch (cmd) {
1180 case TIOCGSERIAL:
1181 return isicom_get_serial_info(port, argp);
1182
1183 case TIOCSSERIAL:
1184 return isicom_set_serial_info(tty, argp);
1185
1186 default:
1187 return -ENOIOCTLCMD;
1188 }
1189 return 0;
1190}
1191
1192/* set_termios et all */
1193static void isicom_set_termios(struct tty_struct *tty,
1194 struct ktermios *old_termios)
1195{
1196 struct isi_port *port = tty->driver_data;
1197 unsigned long flags;
1198
1199 if (isicom_paranoia_check(port, tty->name, "isicom_set_termios"))
1200 return;
1201
1202 if (tty->termios->c_cflag == old_termios->c_cflag &&
1203 tty->termios->c_iflag == old_termios->c_iflag)
1204 return;
1205
1206 spin_lock_irqsave(&port->card->card_lock, flags);
1207 isicom_config_port(tty);
1208 spin_unlock_irqrestore(&port->card->card_lock, flags);
1209
1210 if ((old_termios->c_cflag & CRTSCTS) &&
1211 !(tty->termios->c_cflag & CRTSCTS)) {
1212 tty->hw_stopped = 0;
1213 isicom_start(tty);
1214 }
1215}
1216
1217/* throttle et all */
1218static void isicom_throttle(struct tty_struct *tty)
1219{
1220 struct isi_port *port = tty->driver_data;
1221 struct isi_board *card = port->card;
1222
1223 if (isicom_paranoia_check(port, tty->name, "isicom_throttle"))
1224 return;
1225
1226 /* tell the card that this port cannot handle any more data for now */
1227 card->port_status &= ~(1 << port->channel);
1228 outw(card->port_status, card->base + 0x02);
1229}
1230
1231/* unthrottle et all */
1232static void isicom_unthrottle(struct tty_struct *tty)
1233{
1234 struct isi_port *port = tty->driver_data;
1235 struct isi_board *card = port->card;
1236
1237 if (isicom_paranoia_check(port, tty->name, "isicom_unthrottle"))
1238 return;
1239
1240 /* tell the card that this port is ready to accept more data */
1241 card->port_status |= (1 << port->channel);
1242 outw(card->port_status, card->base + 0x02);
1243}
1244
1245/* stop et all */
1246static void isicom_stop(struct tty_struct *tty)
1247{
1248 struct isi_port *port = tty->driver_data;
1249
1250 if (isicom_paranoia_check(port, tty->name, "isicom_stop"))
1251 return;
1252
1253 /* this tells the transmitter not to consider this port for
1254 data output to the card. */
1255 port->status &= ~ISI_TXOK;
1256}
1257
1258/* start et all */
1259static void isicom_start(struct tty_struct *tty)
1260{
1261 struct isi_port *port = tty->driver_data;
1262
1263 if (isicom_paranoia_check(port, tty->name, "isicom_start"))
1264 return;
1265
1266 /* this tells the transmitter to consider this port for
1267 data output to the card. */
1268 port->status |= ISI_TXOK;
1269}
1270
1271static void isicom_hangup(struct tty_struct *tty)
1272{
1273 struct isi_port *port = tty->driver_data;
1274
1275 if (isicom_paranoia_check(port, tty->name, "isicom_hangup"))
1276 return;
1277 tty_port_hangup(&port->port);
1278}
1279
1280
1281/*
1282 * Driver init and deinit functions
1283 */
1284
1285static const struct tty_operations isicom_ops = {
1286 .open = isicom_open,
1287 .close = isicom_close,
1288 .write = isicom_write,
1289 .put_char = isicom_put_char,
1290 .flush_chars = isicom_flush_chars,
1291 .write_room = isicom_write_room,
1292 .chars_in_buffer = isicom_chars_in_buffer,
1293 .ioctl = isicom_ioctl,
1294 .set_termios = isicom_set_termios,
1295 .throttle = isicom_throttle,
1296 .unthrottle = isicom_unthrottle,
1297 .stop = isicom_stop,
1298 .start = isicom_start,
1299 .hangup = isicom_hangup,
1300 .flush_buffer = isicom_flush_buffer,
1301 .tiocmget = isicom_tiocmget,
1302 .tiocmset = isicom_tiocmset,
1303 .break_ctl = isicom_send_break,
1304};
1305
1306static const struct tty_port_operations isicom_port_ops = {
1307 .carrier_raised = isicom_carrier_raised,
1308 .dtr_rts = isicom_dtr_rts,
1309 .activate = isicom_activate,
1310 .shutdown = isicom_shutdown,
1311};
1312
1313static int __devinit reset_card(struct pci_dev *pdev,
1314 const unsigned int card, unsigned int *signature)
1315{
1316 struct isi_board *board = pci_get_drvdata(pdev);
1317 unsigned long base = board->base;
1318 unsigned int sig, portcount = 0;
1319 int retval = 0;
1320
1321 dev_dbg(&pdev->dev, "ISILoad:Resetting Card%d at 0x%lx\n", card + 1,
1322 base);
1323
1324 inw(base + 0x8);
1325
1326 msleep(10);
1327
1328 outw(0, base + 0x8); /* Reset */
1329
1330 msleep(1000);
1331
1332 sig = inw(base + 0x4) & 0xff;
1333
1334 if (sig != 0xa5 && sig != 0xbb && sig != 0xcc && sig != 0xdd &&
1335 sig != 0xee) {
1336 dev_warn(&pdev->dev, "ISILoad:Card%u reset failure (Possible "
1337 "bad I/O Port Address 0x%lx).\n", card + 1, base);
1338 dev_dbg(&pdev->dev, "Sig=0x%x\n", sig);
1339 retval = -EIO;
1340 goto end;
1341 }
1342
1343 msleep(10);
1344
1345 portcount = inw(base + 0x2);
1346 if (!(inw(base + 0xe) & 0x1) || (portcount != 0 && portcount != 4 &&
1347 portcount != 8 && portcount != 16)) {
1348 dev_err(&pdev->dev, "ISILoad:PCI Card%d reset failure.\n",
1349 card + 1);
1350 retval = -EIO;
1351 goto end;
1352 }
1353
1354 switch (sig) {
1355 case 0xa5:
1356 case 0xbb:
1357 case 0xdd:
1358 board->port_count = (portcount == 4) ? 4 : 8;
1359 board->shift_count = 12;
1360 break;
1361 case 0xcc:
1362 case 0xee:
1363 board->port_count = 16;
1364 board->shift_count = 11;
1365 break;
1366 }
1367 dev_info(&pdev->dev, "-Done\n");
1368 *signature = sig;
1369
1370end:
1371 return retval;
1372}
1373
1374static int __devinit load_firmware(struct pci_dev *pdev,
1375 const unsigned int index, const unsigned int signature)
1376{
1377 struct isi_board *board = pci_get_drvdata(pdev);
1378 const struct firmware *fw;
1379 unsigned long base = board->base;
1380 unsigned int a;
1381 u16 word_count, status;
1382 int retval = -EIO;
1383 char *name;
1384 u8 *data;
1385
1386 struct stframe {
1387 u16 addr;
1388 u16 count;
1389 u8 data[0];
1390 } *frame;
1391
1392 switch (signature) {
1393 case 0xa5:
1394 name = "isi608.bin";
1395 break;
1396 case 0xbb:
1397 name = "isi608em.bin";
1398 break;
1399 case 0xcc:
1400 name = "isi616em.bin";
1401 break;
1402 case 0xdd:
1403 name = "isi4608.bin";
1404 break;
1405 case 0xee:
1406 name = "isi4616.bin";
1407 break;
1408 default:
1409 dev_err(&pdev->dev, "Unknown signature.\n");
1410 goto end;
1411 }
1412
1413 retval = request_firmware(&fw, name, &pdev->dev);
1414 if (retval)
1415 goto end;
1416
1417 retval = -EIO;
1418
1419 for (frame = (struct stframe *)fw->data;
1420 frame < (struct stframe *)(fw->data + fw->size);
1421 frame = (struct stframe *)((u8 *)(frame + 1) +
1422 frame->count)) {
1423 if (WaitTillCardIsFree(base))
1424 goto errrelfw;
1425
1426 outw(0xf0, base); /* start upload sequence */
1427 outw(0x00, base);
1428 outw(frame->addr, base); /* lsb of address */
1429
1430 word_count = frame->count / 2 + frame->count % 2;
1431 outw(word_count, base);
1432 InterruptTheCard(base);
1433
1434 udelay(100); /* 0x2f */
1435
1436 if (WaitTillCardIsFree(base))
1437 goto errrelfw;
1438
1439 status = inw(base + 0x4);
1440 if (status != 0) {
1441 dev_warn(&pdev->dev, "Card%d rejected load header:\n"
1442 "Address:0x%x\n"
1443 "Count:0x%x\n"
1444 "Status:0x%x\n",
1445 index + 1, frame->addr, frame->count, status);
1446 goto errrelfw;
1447 }
1448 outsw(base, frame->data, word_count);
1449
1450 InterruptTheCard(base);
1451
1452 udelay(50); /* 0x0f */
1453
1454 if (WaitTillCardIsFree(base))
1455 goto errrelfw;
1456
1457 status = inw(base + 0x4);
1458 if (status != 0) {
1459 dev_err(&pdev->dev, "Card%d got out of sync.Card "
1460 "Status:0x%x\n", index + 1, status);
1461 goto errrelfw;
1462 }
1463 }
1464
1465/* XXX: should we test it by reading it back and comparing with original like
1466 * in load firmware package? */
1467 for (frame = (struct stframe *)fw->data;
1468 frame < (struct stframe *)(fw->data + fw->size);
1469 frame = (struct stframe *)((u8 *)(frame + 1) +
1470 frame->count)) {
1471 if (WaitTillCardIsFree(base))
1472 goto errrelfw;
1473
1474 outw(0xf1, base); /* start download sequence */
1475 outw(0x00, base);
1476 outw(frame->addr, base); /* lsb of address */
1477
1478 word_count = (frame->count >> 1) + frame->count % 2;
1479 outw(word_count + 1, base);
1480 InterruptTheCard(base);
1481
1482 udelay(50); /* 0xf */
1483
1484 if (WaitTillCardIsFree(base))
1485 goto errrelfw;
1486
1487 status = inw(base + 0x4);
1488 if (status != 0) {
1489 dev_warn(&pdev->dev, "Card%d rejected verify header:\n"
1490 "Address:0x%x\n"
1491 "Count:0x%x\n"
1492 "Status: 0x%x\n",
1493 index + 1, frame->addr, frame->count, status);
1494 goto errrelfw;
1495 }
1496
1497 data = kmalloc(word_count * 2, GFP_KERNEL);
1498 if (data == NULL) {
1499 dev_err(&pdev->dev, "Card%d, firmware upload "
1500 "failed, not enough memory\n", index + 1);
1501 goto errrelfw;
1502 }
1503 inw(base);
1504 insw(base, data, word_count);
1505 InterruptTheCard(base);
1506
1507 for (a = 0; a < frame->count; a++)
1508 if (data[a] != frame->data[a]) {
1509 kfree(data);
1510 dev_err(&pdev->dev, "Card%d, firmware upload "
1511 "failed\n", index + 1);
1512 goto errrelfw;
1513 }
1514 kfree(data);
1515
1516 udelay(50); /* 0xf */
1517
1518 if (WaitTillCardIsFree(base))
1519 goto errrelfw;
1520
1521 status = inw(base + 0x4);
1522 if (status != 0) {
1523 dev_err(&pdev->dev, "Card%d verify got out of sync. "
1524 "Card Status:0x%x\n", index + 1, status);
1525 goto errrelfw;
1526 }
1527 }
1528
1529 /* xfer ctrl */
1530 if (WaitTillCardIsFree(base))
1531 goto errrelfw;
1532
1533 outw(0xf2, base);
1534 outw(0x800, base);
1535 outw(0x0, base);
1536 outw(0x0, base);
1537 InterruptTheCard(base);
1538 outw(0x0, base + 0x4); /* for ISI4608 cards */
1539
1540 board->status |= FIRMWARE_LOADED;
1541 retval = 0;
1542
1543errrelfw:
1544 release_firmware(fw);
1545end:
1546 return retval;
1547}
1548
1549/*
1550 * Insmod can set static symbols so keep these static
1551 */
1552static unsigned int card_count;
1553
1554static int __devinit isicom_probe(struct pci_dev *pdev,
1555 const struct pci_device_id *ent)
1556{
1557 unsigned int uninitialized_var(signature), index;
1558 int retval = -EPERM;
1559 struct isi_board *board = NULL;
1560
1561 if (card_count >= BOARD_COUNT)
1562 goto err;
1563
1564 retval = pci_enable_device(pdev);
1565 if (retval) {
1566 dev_err(&pdev->dev, "failed to enable\n");
1567 goto err;
1568 }
1569
1570 dev_info(&pdev->dev, "ISI PCI Card(Device ID 0x%x)\n", ent->device);
1571
1572 /* allot the first empty slot in the array */
1573 for (index = 0; index < BOARD_COUNT; index++) {
1574 if (isi_card[index].base == 0) {
1575 board = &isi_card[index];
1576 break;
1577 }
1578 }
1579 if (index == BOARD_COUNT) {
1580 retval = -ENODEV;
1581 goto err_disable;
1582 }
1583
1584 board->index = index;
1585 board->base = pci_resource_start(pdev, 3);
1586 board->irq = pdev->irq;
1587 card_count++;
1588
1589 pci_set_drvdata(pdev, board);
1590
1591 retval = pci_request_region(pdev, 3, ISICOM_NAME);
1592 if (retval) {
1593 dev_err(&pdev->dev, "I/O Region 0x%lx-0x%lx is busy. Card%d "
1594 "will be disabled.\n", board->base, board->base + 15,
1595 index + 1);
1596 retval = -EBUSY;
1597 goto errdec;
1598 }
1599
1600 retval = request_irq(board->irq, isicom_interrupt,
1601 IRQF_SHARED | IRQF_DISABLED, ISICOM_NAME, board);
1602 if (retval < 0) {
1603 dev_err(&pdev->dev, "Could not install handler at Irq %d. "
1604 "Card%d will be disabled.\n", board->irq, index + 1);
1605 goto errunrr;
1606 }
1607
1608 retval = reset_card(pdev, index, &signature);
1609 if (retval < 0)
1610 goto errunri;
1611
1612 retval = load_firmware(pdev, index, signature);
1613 if (retval < 0)
1614 goto errunri;
1615
1616 for (index = 0; index < board->port_count; index++)
1617 tty_register_device(isicom_normal, board->index * 16 + index,
1618 &pdev->dev);
1619
1620 return 0;
1621
1622errunri:
1623 free_irq(board->irq, board);
1624errunrr:
1625 pci_release_region(pdev, 3);
1626errdec:
1627 board->base = 0;
1628 card_count--;
1629err_disable:
1630 pci_disable_device(pdev);
1631err:
1632 return retval;
1633}
1634
1635static void __devexit isicom_remove(struct pci_dev *pdev)
1636{
1637 struct isi_board *board = pci_get_drvdata(pdev);
1638 unsigned int i;
1639
1640 for (i = 0; i < board->port_count; i++)
1641 tty_unregister_device(isicom_normal, board->index * 16 + i);
1642
1643 free_irq(board->irq, board);
1644 pci_release_region(pdev, 3);
1645 board->base = 0;
1646 card_count--;
1647 pci_disable_device(pdev);
1648}
1649
1650static int __init isicom_init(void)
1651{
1652 int retval, idx, channel;
1653 struct isi_port *port;
1654
1655 for (idx = 0; idx < BOARD_COUNT; idx++) {
1656 port = &isi_ports[idx * 16];
1657 isi_card[idx].ports = port;
1658 spin_lock_init(&isi_card[idx].card_lock);
1659 for (channel = 0; channel < 16; channel++, port++) {
1660 tty_port_init(&port->port);
1661 port->port.ops = &isicom_port_ops;
1662 port->magic = ISICOM_MAGIC;
1663 port->card = &isi_card[idx];
1664 port->channel = channel;
1665 port->port.close_delay = 50 * HZ/100;
1666 port->port.closing_wait = 3000 * HZ/100;
1667 port->status = 0;
1668 /* . . . */
1669 }
1670 isi_card[idx].base = 0;
1671 isi_card[idx].irq = 0;
1672 }
1673
1674 /* tty driver structure initialization */
1675 isicom_normal = alloc_tty_driver(PORT_COUNT);
1676 if (!isicom_normal) {
1677 retval = -ENOMEM;
1678 goto error;
1679 }
1680
1681 isicom_normal->owner = THIS_MODULE;
1682 isicom_normal->name = "ttyM";
1683 isicom_normal->major = ISICOM_NMAJOR;
1684 isicom_normal->minor_start = 0;
1685 isicom_normal->type = TTY_DRIVER_TYPE_SERIAL;
1686 isicom_normal->subtype = SERIAL_TYPE_NORMAL;
1687 isicom_normal->init_termios = tty_std_termios;
1688 isicom_normal->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL |
1689 CLOCAL;
1690 isicom_normal->flags = TTY_DRIVER_REAL_RAW |
1691 TTY_DRIVER_DYNAMIC_DEV | TTY_DRIVER_HARDWARE_BREAK;
1692 tty_set_operations(isicom_normal, &isicom_ops);
1693
1694 retval = tty_register_driver(isicom_normal);
1695 if (retval) {
1696 pr_debug("Couldn't register the dialin driver\n");
1697 goto err_puttty;
1698 }
1699
1700 retval = pci_register_driver(&isicom_driver);
1701 if (retval < 0) {
1702 pr_err("Unable to register pci driver.\n");
1703 goto err_unrtty;
1704 }
1705
1706 mod_timer(&tx, jiffies + 1);
1707
1708 return 0;
1709err_unrtty:
1710 tty_unregister_driver(isicom_normal);
1711err_puttty:
1712 put_tty_driver(isicom_normal);
1713error:
1714 return retval;
1715}
1716
1717static void __exit isicom_exit(void)
1718{
1719 del_timer_sync(&tx);
1720
1721 pci_unregister_driver(&isicom_driver);
1722 tty_unregister_driver(isicom_normal);
1723 put_tty_driver(isicom_normal);
1724}
1725
1726module_init(isicom_init);
1727module_exit(isicom_exit);
1728
1729MODULE_AUTHOR("MultiTech");
1730MODULE_DESCRIPTION("Driver for the ISI series of cards by MultiTech");
1731MODULE_LICENSE("GPL");
1732MODULE_FIRMWARE("isi608.bin");
1733MODULE_FIRMWARE("isi608em.bin");
1734MODULE_FIRMWARE("isi616em.bin");
1735MODULE_FIRMWARE("isi4608.bin");
1736MODULE_FIRMWARE("isi4616.bin");
diff --git a/drivers/char/istallion.c b/drivers/char/istallion.c
deleted file mode 100644
index be28391adb79..000000000000
--- a/drivers/char/istallion.c
+++ /dev/null
@@ -1,4507 +0,0 @@
1/*****************************************************************************/
2
3/*
4 * istallion.c -- stallion intelligent multiport serial driver.
5 *
6 * Copyright (C) 1996-1999 Stallion Technologies
7 * Copyright (C) 1994-1996 Greg Ungerer.
8 *
9 * This code is loosely based on the Linux serial driver, written by
10 * Linus Torvalds, Theodore T'so and others.
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 */
18
19/*****************************************************************************/
20
21#include <linux/module.h>
22#include <linux/sched.h>
23#include <linux/slab.h>
24#include <linux/smp_lock.h>
25#include <linux/interrupt.h>
26#include <linux/tty.h>
27#include <linux/tty_flip.h>
28#include <linux/serial.h>
29#include <linux/seq_file.h>
30#include <linux/cdk.h>
31#include <linux/comstats.h>
32#include <linux/istallion.h>
33#include <linux/ioport.h>
34#include <linux/delay.h>
35#include <linux/init.h>
36#include <linux/device.h>
37#include <linux/wait.h>
38#include <linux/eisa.h>
39#include <linux/ctype.h>
40
41#include <asm/io.h>
42#include <asm/uaccess.h>
43
44#include <linux/pci.h>
45
46/*****************************************************************************/
47
48/*
49 * Define different board types. Not all of the following board types
50 * are supported by this driver. But I will use the standard "assigned"
51 * board numbers. Currently supported boards are abbreviated as:
52 * ECP = EasyConnection 8/64, ONB = ONboard, BBY = Brumby and
53 * STAL = Stallion.
54 */
55#define BRD_UNKNOWN 0
56#define BRD_STALLION 1
57#define BRD_BRUMBY4 2
58#define BRD_ONBOARD2 3
59#define BRD_ONBOARD 4
60#define BRD_ONBOARDE 7
61#define BRD_ECP 23
62#define BRD_ECPE 24
63#define BRD_ECPMC 25
64#define BRD_ECPPCI 29
65
66#define BRD_BRUMBY BRD_BRUMBY4
67
68/*
69 * Define a configuration structure to hold the board configuration.
70 * Need to set this up in the code (for now) with the boards that are
71 * to be configured into the system. This is what needs to be modified
72 * when adding/removing/modifying boards. Each line entry in the
73 * stli_brdconf[] array is a board. Each line contains io/irq/memory
74 * ranges for that board (as well as what type of board it is).
75 * Some examples:
76 * { BRD_ECP, 0x2a0, 0, 0xcc000, 0, 0 },
77 * This line will configure an EasyConnection 8/64 at io address 2a0,
78 * and shared memory address of cc000. Multiple EasyConnection 8/64
79 * boards can share the same shared memory address space. No interrupt
80 * is required for this board type.
81 * Another example:
82 * { BRD_ECPE, 0x5000, 0, 0x80000000, 0, 0 },
83 * This line will configure an EasyConnection 8/64 EISA in slot 5 and
84 * shared memory address of 0x80000000 (2 GByte). Multiple
85 * EasyConnection 8/64 EISA boards can share the same shared memory
86 * address space. No interrupt is required for this board type.
87 * Another example:
88 * { BRD_ONBOARD, 0x240, 0, 0xd0000, 0, 0 },
89 * This line will configure an ONboard (ISA type) at io address 240,
90 * and shared memory address of d0000. Multiple ONboards can share
91 * the same shared memory address space. No interrupt required.
92 * Another example:
93 * { BRD_BRUMBY4, 0x360, 0, 0xc8000, 0, 0 },
94 * This line will configure a Brumby board (any number of ports!) at
95 * io address 360 and shared memory address of c8000. All Brumby boards
96 * configured into a system must have their own separate io and memory
97 * addresses. No interrupt is required.
98 * Another example:
99 * { BRD_STALLION, 0x330, 0, 0xd0000, 0, 0 },
100 * This line will configure an original Stallion board at io address 330
101 * and shared memory address d0000 (this would only be valid for a "V4.0"
102 * or Rev.O Stallion board). All Stallion boards configured into the
103 * system must have their own separate io and memory addresses. No
104 * interrupt is required.
105 */
106
107struct stlconf {
108 int brdtype;
109 int ioaddr1;
110 int ioaddr2;
111 unsigned long memaddr;
112 int irq;
113 int irqtype;
114};
115
116static unsigned int stli_nrbrds;
117
118/* stli_lock must NOT be taken holding brd_lock */
119static spinlock_t stli_lock; /* TTY logic lock */
120static spinlock_t brd_lock; /* Board logic lock */
121
122/*
123 * There is some experimental EISA board detection code in this driver.
124 * By default it is disabled, but for those that want to try it out,
125 * then set the define below to be 1.
126 */
127#define STLI_EISAPROBE 0
128
129/*****************************************************************************/
130
131/*
132 * Define some important driver characteristics. Device major numbers
133 * allocated as per Linux Device Registry.
134 */
135#ifndef STL_SIOMEMMAJOR
136#define STL_SIOMEMMAJOR 28
137#endif
138#ifndef STL_SERIALMAJOR
139#define STL_SERIALMAJOR 24
140#endif
141#ifndef STL_CALLOUTMAJOR
142#define STL_CALLOUTMAJOR 25
143#endif
144
145/*****************************************************************************/
146
147/*
148 * Define our local driver identity first. Set up stuff to deal with
149 * all the local structures required by a serial tty driver.
150 */
151static char *stli_drvtitle = "Stallion Intelligent Multiport Serial Driver";
152static char *stli_drvname = "istallion";
153static char *stli_drvversion = "5.6.0";
154static char *stli_serialname = "ttyE";
155
156static struct tty_driver *stli_serial;
157static const struct tty_port_operations stli_port_ops;
158
159#define STLI_TXBUFSIZE 4096
160
161/*
162 * Use a fast local buffer for cooked characters. Typically a whole
163 * bunch of cooked characters come in for a port, 1 at a time. So we
164 * save those up into a local buffer, then write out the whole lot
165 * with a large memcpy. Just use 1 buffer for all ports, since its
166 * use it is only need for short periods of time by each port.
167 */
168static char *stli_txcookbuf;
169static int stli_txcooksize;
170static int stli_txcookrealsize;
171static struct tty_struct *stli_txcooktty;
172
173/*
174 * Define a local default termios struct. All ports will be created
175 * with this termios initially. Basically all it defines is a raw port
176 * at 9600 baud, 8 data bits, no parity, 1 stop bit.
177 */
178static struct ktermios stli_deftermios = {
179 .c_cflag = (B9600 | CS8 | CREAD | HUPCL | CLOCAL),
180 .c_cc = INIT_C_CC,
181 .c_ispeed = 9600,
182 .c_ospeed = 9600,
183};
184
185/*
186 * Define global stats structures. Not used often, and can be
187 * re-used for each stats call.
188 */
189static comstats_t stli_comstats;
190static combrd_t stli_brdstats;
191static struct asystats stli_cdkstats;
192
193/*****************************************************************************/
194
195static DEFINE_MUTEX(stli_brdslock);
196static struct stlibrd *stli_brds[STL_MAXBRDS];
197
198static int stli_shared;
199
200/*
201 * Per board state flags. Used with the state field of the board struct.
202 * Not really much here... All we need to do is keep track of whether
203 * the board has been detected, and whether it is actually running a slave
204 * or not.
205 */
206#define BST_FOUND 0
207#define BST_STARTED 1
208#define BST_PROBED 2
209
210/*
211 * Define the set of port state flags. These are marked for internal
212 * state purposes only, usually to do with the state of communications
213 * with the slave. Most of them need to be updated atomically, so always
214 * use the bit setting operations (unless protected by cli/sti).
215 */
216#define ST_OPENING 2
217#define ST_CLOSING 3
218#define ST_CMDING 4
219#define ST_TXBUSY 5
220#define ST_RXING 6
221#define ST_DOFLUSHRX 7
222#define ST_DOFLUSHTX 8
223#define ST_DOSIGS 9
224#define ST_RXSTOP 10
225#define ST_GETSIGS 11
226
227/*
228 * Define an array of board names as printable strings. Handy for
229 * referencing boards when printing trace and stuff.
230 */
231static char *stli_brdnames[] = {
232 "Unknown",
233 "Stallion",
234 "Brumby",
235 "ONboard-MC",
236 "ONboard",
237 "Brumby",
238 "Brumby",
239 "ONboard-EI",
240 NULL,
241 "ONboard",
242 "ONboard-MC",
243 "ONboard-MC",
244 NULL,
245 NULL,
246 NULL,
247 NULL,
248 NULL,
249 NULL,
250 NULL,
251 NULL,
252 "EasyIO",
253 "EC8/32-AT",
254 "EC8/32-MC",
255 "EC8/64-AT",
256 "EC8/64-EI",
257 "EC8/64-MC",
258 "EC8/32-PCI",
259 "EC8/64-PCI",
260 "EasyIO-PCI",
261 "EC/RA-PCI",
262};
263
264/*****************************************************************************/
265
266/*
267 * Define some string labels for arguments passed from the module
268 * load line. These allow for easy board definitions, and easy
269 * modification of the io, memory and irq resoucres.
270 */
271
272static char *board0[8];
273static char *board1[8];
274static char *board2[8];
275static char *board3[8];
276
277static char **stli_brdsp[] = {
278 (char **) &board0,
279 (char **) &board1,
280 (char **) &board2,
281 (char **) &board3
282};
283
284/*
285 * Define a set of common board names, and types. This is used to
286 * parse any module arguments.
287 */
288
289static struct stlibrdtype {
290 char *name;
291 int type;
292} stli_brdstr[] = {
293 { "stallion", BRD_STALLION },
294 { "1", BRD_STALLION },
295 { "brumby", BRD_BRUMBY },
296 { "brumby4", BRD_BRUMBY },
297 { "brumby/4", BRD_BRUMBY },
298 { "brumby-4", BRD_BRUMBY },
299 { "brumby8", BRD_BRUMBY },
300 { "brumby/8", BRD_BRUMBY },
301 { "brumby-8", BRD_BRUMBY },
302 { "brumby16", BRD_BRUMBY },
303 { "brumby/16", BRD_BRUMBY },
304 { "brumby-16", BRD_BRUMBY },
305 { "2", BRD_BRUMBY },
306 { "onboard2", BRD_ONBOARD2 },
307 { "onboard-2", BRD_ONBOARD2 },
308 { "onboard/2", BRD_ONBOARD2 },
309 { "onboard-mc", BRD_ONBOARD2 },
310 { "onboard/mc", BRD_ONBOARD2 },
311 { "onboard-mca", BRD_ONBOARD2 },
312 { "onboard/mca", BRD_ONBOARD2 },
313 { "3", BRD_ONBOARD2 },
314 { "onboard", BRD_ONBOARD },
315 { "onboardat", BRD_ONBOARD },
316 { "4", BRD_ONBOARD },
317 { "onboarde", BRD_ONBOARDE },
318 { "onboard-e", BRD_ONBOARDE },
319 { "onboard/e", BRD_ONBOARDE },
320 { "onboard-ei", BRD_ONBOARDE },
321 { "onboard/ei", BRD_ONBOARDE },
322 { "7", BRD_ONBOARDE },
323 { "ecp", BRD_ECP },
324 { "ecpat", BRD_ECP },
325 { "ec8/64", BRD_ECP },
326 { "ec8/64-at", BRD_ECP },
327 { "ec8/64-isa", BRD_ECP },
328 { "23", BRD_ECP },
329 { "ecpe", BRD_ECPE },
330 { "ecpei", BRD_ECPE },
331 { "ec8/64-e", BRD_ECPE },
332 { "ec8/64-ei", BRD_ECPE },
333 { "24", BRD_ECPE },
334 { "ecpmc", BRD_ECPMC },
335 { "ec8/64-mc", BRD_ECPMC },
336 { "ec8/64-mca", BRD_ECPMC },
337 { "25", BRD_ECPMC },
338 { "ecppci", BRD_ECPPCI },
339 { "ec/ra", BRD_ECPPCI },
340 { "ec/ra-pc", BRD_ECPPCI },
341 { "ec/ra-pci", BRD_ECPPCI },
342 { "29", BRD_ECPPCI },
343};
344
345/*
346 * Define the module agruments.
347 */
348MODULE_AUTHOR("Greg Ungerer");
349MODULE_DESCRIPTION("Stallion Intelligent Multiport Serial Driver");
350MODULE_LICENSE("GPL");
351
352
353module_param_array(board0, charp, NULL, 0);
354MODULE_PARM_DESC(board0, "Board 0 config -> name[,ioaddr[,memaddr]");
355module_param_array(board1, charp, NULL, 0);
356MODULE_PARM_DESC(board1, "Board 1 config -> name[,ioaddr[,memaddr]");
357module_param_array(board2, charp, NULL, 0);
358MODULE_PARM_DESC(board2, "Board 2 config -> name[,ioaddr[,memaddr]");
359module_param_array(board3, charp, NULL, 0);
360MODULE_PARM_DESC(board3, "Board 3 config -> name[,ioaddr[,memaddr]");
361
362#if STLI_EISAPROBE != 0
363/*
364 * Set up a default memory address table for EISA board probing.
365 * The default addresses are all bellow 1Mbyte, which has to be the
366 * case anyway. They should be safe, since we only read values from
367 * them, and interrupts are disabled while we do it. If the higher
368 * memory support is compiled in then we also try probing around
369 * the 1Gb, 2Gb and 3Gb areas as well...
370 */
371static unsigned long stli_eisamemprobeaddrs[] = {
372 0xc0000, 0xd0000, 0xe0000, 0xf0000,
373 0x80000000, 0x80010000, 0x80020000, 0x80030000,
374 0x40000000, 0x40010000, 0x40020000, 0x40030000,
375 0xc0000000, 0xc0010000, 0xc0020000, 0xc0030000,
376 0xff000000, 0xff010000, 0xff020000, 0xff030000,
377};
378
379static int stli_eisamempsize = ARRAY_SIZE(stli_eisamemprobeaddrs);
380#endif
381
382/*
383 * Define the Stallion PCI vendor and device IDs.
384 */
385#ifndef PCI_DEVICE_ID_ECRA
386#define PCI_DEVICE_ID_ECRA 0x0004
387#endif
388
389static struct pci_device_id istallion_pci_tbl[] = {
390 { PCI_DEVICE(PCI_VENDOR_ID_STALLION, PCI_DEVICE_ID_ECRA), },
391 { 0 }
392};
393MODULE_DEVICE_TABLE(pci, istallion_pci_tbl);
394
395static struct pci_driver stli_pcidriver;
396
397/*****************************************************************************/
398
399/*
400 * Hardware configuration info for ECP boards. These defines apply
401 * to the directly accessible io ports of the ECP. There is a set of
402 * defines for each ECP board type, ISA, EISA, MCA and PCI.
403 */
404#define ECP_IOSIZE 4
405
406#define ECP_MEMSIZE (128 * 1024)
407#define ECP_PCIMEMSIZE (256 * 1024)
408
409#define ECP_ATPAGESIZE (4 * 1024)
410#define ECP_MCPAGESIZE (4 * 1024)
411#define ECP_EIPAGESIZE (64 * 1024)
412#define ECP_PCIPAGESIZE (64 * 1024)
413
414#define STL_EISAID 0x8c4e
415
416/*
417 * Important defines for the ISA class of ECP board.
418 */
419#define ECP_ATIREG 0
420#define ECP_ATCONFR 1
421#define ECP_ATMEMAR 2
422#define ECP_ATMEMPR 3
423#define ECP_ATSTOP 0x1
424#define ECP_ATINTENAB 0x10
425#define ECP_ATENABLE 0x20
426#define ECP_ATDISABLE 0x00
427#define ECP_ATADDRMASK 0x3f000
428#define ECP_ATADDRSHFT 12
429
430/*
431 * Important defines for the EISA class of ECP board.
432 */
433#define ECP_EIIREG 0
434#define ECP_EIMEMARL 1
435#define ECP_EICONFR 2
436#define ECP_EIMEMARH 3
437#define ECP_EIENABLE 0x1
438#define ECP_EIDISABLE 0x0
439#define ECP_EISTOP 0x4
440#define ECP_EIEDGE 0x00
441#define ECP_EILEVEL 0x80
442#define ECP_EIADDRMASKL 0x00ff0000
443#define ECP_EIADDRSHFTL 16
444#define ECP_EIADDRMASKH 0xff000000
445#define ECP_EIADDRSHFTH 24
446#define ECP_EIBRDENAB 0xc84
447
448#define ECP_EISAID 0x4
449
450/*
451 * Important defines for the Micro-channel class of ECP board.
452 * (It has a lot in common with the ISA boards.)
453 */
454#define ECP_MCIREG 0
455#define ECP_MCCONFR 1
456#define ECP_MCSTOP 0x20
457#define ECP_MCENABLE 0x80
458#define ECP_MCDISABLE 0x00
459
460/*
461 * Important defines for the PCI class of ECP board.
462 * (It has a lot in common with the other ECP boards.)
463 */
464#define ECP_PCIIREG 0
465#define ECP_PCICONFR 1
466#define ECP_PCISTOP 0x01
467
468/*
469 * Hardware configuration info for ONboard and Brumby boards. These
470 * defines apply to the directly accessible io ports of these boards.
471 */
472#define ONB_IOSIZE 16
473#define ONB_MEMSIZE (64 * 1024)
474#define ONB_ATPAGESIZE (64 * 1024)
475#define ONB_MCPAGESIZE (64 * 1024)
476#define ONB_EIMEMSIZE (128 * 1024)
477#define ONB_EIPAGESIZE (64 * 1024)
478
479/*
480 * Important defines for the ISA class of ONboard board.
481 */
482#define ONB_ATIREG 0
483#define ONB_ATMEMAR 1
484#define ONB_ATCONFR 2
485#define ONB_ATSTOP 0x4
486#define ONB_ATENABLE 0x01
487#define ONB_ATDISABLE 0x00
488#define ONB_ATADDRMASK 0xff0000
489#define ONB_ATADDRSHFT 16
490
491#define ONB_MEMENABLO 0
492#define ONB_MEMENABHI 0x02
493
494/*
495 * Important defines for the EISA class of ONboard board.
496 */
497#define ONB_EIIREG 0
498#define ONB_EIMEMARL 1
499#define ONB_EICONFR 2
500#define ONB_EIMEMARH 3
501#define ONB_EIENABLE 0x1
502#define ONB_EIDISABLE 0x0
503#define ONB_EISTOP 0x4
504#define ONB_EIEDGE 0x00
505#define ONB_EILEVEL 0x80
506#define ONB_EIADDRMASKL 0x00ff0000
507#define ONB_EIADDRSHFTL 16
508#define ONB_EIADDRMASKH 0xff000000
509#define ONB_EIADDRSHFTH 24
510#define ONB_EIBRDENAB 0xc84
511
512#define ONB_EISAID 0x1
513
514/*
515 * Important defines for the Brumby boards. They are pretty simple,
516 * there is not much that is programmably configurable.
517 */
518#define BBY_IOSIZE 16
519#define BBY_MEMSIZE (64 * 1024)
520#define BBY_PAGESIZE (16 * 1024)
521
522#define BBY_ATIREG 0
523#define BBY_ATCONFR 1
524#define BBY_ATSTOP 0x4
525
526/*
527 * Important defines for the Stallion boards. They are pretty simple,
528 * there is not much that is programmably configurable.
529 */
530#define STAL_IOSIZE 16
531#define STAL_MEMSIZE (64 * 1024)
532#define STAL_PAGESIZE (64 * 1024)
533
534/*
535 * Define the set of status register values for EasyConnection panels.
536 * The signature will return with the status value for each panel. From
537 * this we can determine what is attached to the board - before we have
538 * actually down loaded any code to it.
539 */
540#define ECH_PNLSTATUS 2
541#define ECH_PNL16PORT 0x20
542#define ECH_PNLIDMASK 0x07
543#define ECH_PNLXPID 0x40
544#define ECH_PNLINTRPEND 0x80
545
546/*
547 * Define some macros to do things to the board. Even those these boards
548 * are somewhat related there is often significantly different ways of
549 * doing some operation on it (like enable, paging, reset, etc). So each
550 * board class has a set of functions which do the commonly required
551 * operations. The macros below basically just call these functions,
552 * generally checking for a NULL function - which means that the board
553 * needs nothing done to it to achieve this operation!
554 */
555#define EBRDINIT(brdp) \
556 if (brdp->init != NULL) \
557 (* brdp->init)(brdp)
558
559#define EBRDENABLE(brdp) \
560 if (brdp->enable != NULL) \
561 (* brdp->enable)(brdp);
562
563#define EBRDDISABLE(brdp) \
564 if (brdp->disable != NULL) \
565 (* brdp->disable)(brdp);
566
567#define EBRDINTR(brdp) \
568 if (brdp->intr != NULL) \
569 (* brdp->intr)(brdp);
570
571#define EBRDRESET(brdp) \
572 if (brdp->reset != NULL) \
573 (* brdp->reset)(brdp);
574
575#define EBRDGETMEMPTR(brdp,offset) \
576 (* brdp->getmemptr)(brdp, offset, __LINE__)
577
578/*
579 * Define the maximal baud rate, and the default baud base for ports.
580 */
581#define STL_MAXBAUD 460800
582#define STL_BAUDBASE 115200
583#define STL_CLOSEDELAY (5 * HZ / 10)
584
585/*****************************************************************************/
586
587/*
588 * Define macros to extract a brd or port number from a minor number.
589 */
590#define MINOR2BRD(min) (((min) & 0xc0) >> 6)
591#define MINOR2PORT(min) ((min) & 0x3f)
592
593/*****************************************************************************/
594
595/*
596 * Prototype all functions in this driver!
597 */
598
599static int stli_parsebrd(struct stlconf *confp, char **argp);
600static int stli_open(struct tty_struct *tty, struct file *filp);
601static void stli_close(struct tty_struct *tty, struct file *filp);
602static int stli_write(struct tty_struct *tty, const unsigned char *buf, int count);
603static int stli_putchar(struct tty_struct *tty, unsigned char ch);
604static void stli_flushchars(struct tty_struct *tty);
605static int stli_writeroom(struct tty_struct *tty);
606static int stli_charsinbuffer(struct tty_struct *tty);
607static int stli_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg);
608static void stli_settermios(struct tty_struct *tty, struct ktermios *old);
609static void stli_throttle(struct tty_struct *tty);
610static void stli_unthrottle(struct tty_struct *tty);
611static void stli_stop(struct tty_struct *tty);
612static void stli_start(struct tty_struct *tty);
613static void stli_flushbuffer(struct tty_struct *tty);
614static int stli_breakctl(struct tty_struct *tty, int state);
615static void stli_waituntilsent(struct tty_struct *tty, int timeout);
616static void stli_sendxchar(struct tty_struct *tty, char ch);
617static void stli_hangup(struct tty_struct *tty);
618
619static int stli_brdinit(struct stlibrd *brdp);
620static int stli_startbrd(struct stlibrd *brdp);
621static ssize_t stli_memread(struct file *fp, char __user *buf, size_t count, loff_t *offp);
622static ssize_t stli_memwrite(struct file *fp, const char __user *buf, size_t count, loff_t *offp);
623static long stli_memioctl(struct file *fp, unsigned int cmd, unsigned long arg);
624static void stli_brdpoll(struct stlibrd *brdp, cdkhdr_t __iomem *hdrp);
625static void stli_poll(unsigned long arg);
626static int stli_hostcmd(struct stlibrd *brdp, struct stliport *portp);
627static int stli_initopen(struct tty_struct *tty, struct stlibrd *brdp, struct stliport *portp);
628static int stli_rawopen(struct stlibrd *brdp, struct stliport *portp, unsigned long arg, int wait);
629static int stli_rawclose(struct stlibrd *brdp, struct stliport *portp, unsigned long arg, int wait);
630static int stli_setport(struct tty_struct *tty);
631static int stli_cmdwait(struct stlibrd *brdp, struct stliport *portp, unsigned long cmd, void *arg, int size, int copyback);
632static void stli_sendcmd(struct stlibrd *brdp, struct stliport *portp, unsigned long cmd, void *arg, int size, int copyback);
633static void __stli_sendcmd(struct stlibrd *brdp, struct stliport *portp, unsigned long cmd, void *arg, int size, int copyback);
634static void stli_dodelaycmd(struct stliport *portp, cdkctrl_t __iomem *cp);
635static void stli_mkasyport(struct tty_struct *tty, struct stliport *portp, asyport_t *pp, struct ktermios *tiosp);
636static void stli_mkasysigs(asysigs_t *sp, int dtr, int rts);
637static long stli_mktiocm(unsigned long sigvalue);
638static void stli_read(struct stlibrd *brdp, struct stliport *portp);
639static int stli_getserial(struct stliport *portp, struct serial_struct __user *sp);
640static int stli_setserial(struct tty_struct *tty, struct serial_struct __user *sp);
641static int stli_getbrdstats(combrd_t __user *bp);
642static int stli_getportstats(struct tty_struct *tty, struct stliport *portp, comstats_t __user *cp);
643static int stli_portcmdstats(struct tty_struct *tty, struct stliport *portp);
644static int stli_clrportstats(struct stliport *portp, comstats_t __user *cp);
645static int stli_getportstruct(struct stliport __user *arg);
646static int stli_getbrdstruct(struct stlibrd __user *arg);
647static struct stlibrd *stli_allocbrd(void);
648
649static void stli_ecpinit(struct stlibrd *brdp);
650static void stli_ecpenable(struct stlibrd *brdp);
651static void stli_ecpdisable(struct stlibrd *brdp);
652static void __iomem *stli_ecpgetmemptr(struct stlibrd *brdp, unsigned long offset, int line);
653static void stli_ecpreset(struct stlibrd *brdp);
654static void stli_ecpintr(struct stlibrd *brdp);
655static void stli_ecpeiinit(struct stlibrd *brdp);
656static void stli_ecpeienable(struct stlibrd *brdp);
657static void stli_ecpeidisable(struct stlibrd *brdp);
658static void __iomem *stli_ecpeigetmemptr(struct stlibrd *brdp, unsigned long offset, int line);
659static void stli_ecpeireset(struct stlibrd *brdp);
660static void stli_ecpmcenable(struct stlibrd *brdp);
661static void stli_ecpmcdisable(struct stlibrd *brdp);
662static void __iomem *stli_ecpmcgetmemptr(struct stlibrd *brdp, unsigned long offset, int line);
663static void stli_ecpmcreset(struct stlibrd *brdp);
664static void stli_ecppciinit(struct stlibrd *brdp);
665static void __iomem *stli_ecppcigetmemptr(struct stlibrd *brdp, unsigned long offset, int line);
666static void stli_ecppcireset(struct stlibrd *brdp);
667
668static void stli_onbinit(struct stlibrd *brdp);
669static void stli_onbenable(struct stlibrd *brdp);
670static void stli_onbdisable(struct stlibrd *brdp);
671static void __iomem *stli_onbgetmemptr(struct stlibrd *brdp, unsigned long offset, int line);
672static void stli_onbreset(struct stlibrd *brdp);
673static void stli_onbeinit(struct stlibrd *brdp);
674static void stli_onbeenable(struct stlibrd *brdp);
675static void stli_onbedisable(struct stlibrd *brdp);
676static void __iomem *stli_onbegetmemptr(struct stlibrd *brdp, unsigned long offset, int line);
677static void stli_onbereset(struct stlibrd *brdp);
678static void stli_bbyinit(struct stlibrd *brdp);
679static void __iomem *stli_bbygetmemptr(struct stlibrd *brdp, unsigned long offset, int line);
680static void stli_bbyreset(struct stlibrd *brdp);
681static void stli_stalinit(struct stlibrd *brdp);
682static void __iomem *stli_stalgetmemptr(struct stlibrd *brdp, unsigned long offset, int line);
683static void stli_stalreset(struct stlibrd *brdp);
684
685static struct stliport *stli_getport(unsigned int brdnr, unsigned int panelnr, unsigned int portnr);
686
687static int stli_initecp(struct stlibrd *brdp);
688static int stli_initonb(struct stlibrd *brdp);
689#if STLI_EISAPROBE != 0
690static int stli_eisamemprobe(struct stlibrd *brdp);
691#endif
692static int stli_initports(struct stlibrd *brdp);
693
694/*****************************************************************************/
695
696/*
697 * Define the driver info for a user level shared memory device. This
698 * device will work sort of like the /dev/kmem device - except that it
699 * will give access to the shared memory on the Stallion intelligent
700 * board. This is also a very useful debugging tool.
701 */
702static const struct file_operations stli_fsiomem = {
703 .owner = THIS_MODULE,
704 .read = stli_memread,
705 .write = stli_memwrite,
706 .unlocked_ioctl = stli_memioctl,
707};
708
709/*****************************************************************************/
710
711/*
712 * Define a timer_list entry for our poll routine. The slave board
713 * is polled every so often to see if anything needs doing. This is
714 * much cheaper on host cpu than using interrupts. It turns out to
715 * not increase character latency by much either...
716 */
717static DEFINE_TIMER(stli_timerlist, stli_poll, 0, 0);
718
719static int stli_timeron;
720
721/*
722 * Define the calculation for the timeout routine.
723 */
724#define STLI_TIMEOUT (jiffies + 1)
725
726/*****************************************************************************/
727
728static struct class *istallion_class;
729
730static void stli_cleanup_ports(struct stlibrd *brdp)
731{
732 struct stliport *portp;
733 unsigned int j;
734 struct tty_struct *tty;
735
736 for (j = 0; j < STL_MAXPORTS; j++) {
737 portp = brdp->ports[j];
738 if (portp != NULL) {
739 tty = tty_port_tty_get(&portp->port);
740 if (tty != NULL) {
741 tty_hangup(tty);
742 tty_kref_put(tty);
743 }
744 kfree(portp);
745 }
746 }
747}
748
749/*****************************************************************************/
750
751/*
752 * Parse the supplied argument string, into the board conf struct.
753 */
754
755static int stli_parsebrd(struct stlconf *confp, char **argp)
756{
757 unsigned int i;
758 char *sp;
759
760 if (argp[0] == NULL || *argp[0] == 0)
761 return 0;
762
763 for (sp = argp[0], i = 0; ((*sp != 0) && (i < 25)); sp++, i++)
764 *sp = tolower(*sp);
765
766 for (i = 0; i < ARRAY_SIZE(stli_brdstr); i++) {
767 if (strcmp(stli_brdstr[i].name, argp[0]) == 0)
768 break;
769 }
770 if (i == ARRAY_SIZE(stli_brdstr)) {
771 printk(KERN_WARNING "istallion: unknown board name, %s?\n", argp[0]);
772 return 0;
773 }
774
775 confp->brdtype = stli_brdstr[i].type;
776 if (argp[1] != NULL && *argp[1] != 0)
777 confp->ioaddr1 = simple_strtoul(argp[1], NULL, 0);
778 if (argp[2] != NULL && *argp[2] != 0)
779 confp->memaddr = simple_strtoul(argp[2], NULL, 0);
780 return(1);
781}
782
783/*****************************************************************************/
784
785/*
786 * On the first open of the device setup the port hardware, and
787 * initialize the per port data structure. Since initializing the port
788 * requires several commands to the board we will need to wait for any
789 * other open that is already initializing the port.
790 *
791 * Locking: protected by the port mutex.
792 */
793
794static int stli_activate(struct tty_port *port, struct tty_struct *tty)
795{
796 struct stliport *portp = container_of(port, struct stliport, port);
797 struct stlibrd *brdp = stli_brds[portp->brdnr];
798 int rc;
799
800 if ((rc = stli_initopen(tty, brdp, portp)) >= 0)
801 clear_bit(TTY_IO_ERROR, &tty->flags);
802 wake_up_interruptible(&portp->raw_wait);
803 return rc;
804}
805
806static int stli_open(struct tty_struct *tty, struct file *filp)
807{
808 struct stlibrd *brdp;
809 struct stliport *portp;
810 unsigned int minordev, brdnr, portnr;
811
812 minordev = tty->index;
813 brdnr = MINOR2BRD(minordev);
814 if (brdnr >= stli_nrbrds)
815 return -ENODEV;
816 brdp = stli_brds[brdnr];
817 if (brdp == NULL)
818 return -ENODEV;
819 if (!test_bit(BST_STARTED, &brdp->state))
820 return -ENODEV;
821 portnr = MINOR2PORT(minordev);
822 if (portnr > brdp->nrports)
823 return -ENODEV;
824
825 portp = brdp->ports[portnr];
826 if (portp == NULL)
827 return -ENODEV;
828 if (portp->devnr < 1)
829 return -ENODEV;
830
831 tty->driver_data = portp;
832 return tty_port_open(&portp->port, tty, filp);
833}
834
835
836/*****************************************************************************/
837
838static void stli_shutdown(struct tty_port *port)
839{
840 struct stlibrd *brdp;
841 unsigned long ftype;
842 unsigned long flags;
843 struct stliport *portp = container_of(port, struct stliport, port);
844
845 if (portp->brdnr >= stli_nrbrds)
846 return;
847 brdp = stli_brds[portp->brdnr];
848 if (brdp == NULL)
849 return;
850
851 /*
852 * May want to wait for data to drain before closing. The BUSY
853 * flag keeps track of whether we are still transmitting or not.
854 * It is updated by messages from the slave - indicating when all
855 * chars really have drained.
856 */
857
858 if (!test_bit(ST_CLOSING, &portp->state))
859 stli_rawclose(brdp, portp, 0, 0);
860
861 spin_lock_irqsave(&stli_lock, flags);
862 clear_bit(ST_TXBUSY, &portp->state);
863 clear_bit(ST_RXSTOP, &portp->state);
864 spin_unlock_irqrestore(&stli_lock, flags);
865
866 ftype = FLUSHTX | FLUSHRX;
867 stli_cmdwait(brdp, portp, A_FLUSH, &ftype, sizeof(u32), 0);
868}
869
870static void stli_close(struct tty_struct *tty, struct file *filp)
871{
872 struct stliport *portp = tty->driver_data;
873 unsigned long flags;
874 if (portp == NULL)
875 return;
876 spin_lock_irqsave(&stli_lock, flags);
877 /* Flush any internal buffering out first */
878 if (tty == stli_txcooktty)
879 stli_flushchars(tty);
880 spin_unlock_irqrestore(&stli_lock, flags);
881 tty_port_close(&portp->port, tty, filp);
882}
883
884/*****************************************************************************/
885
886/*
887 * Carry out first open operations on a port. This involves a number of
888 * commands to be sent to the slave. We need to open the port, set the
889 * notification events, set the initial port settings, get and set the
890 * initial signal values. We sleep and wait in between each one. But
891 * this still all happens pretty quickly.
892 */
893
894static int stli_initopen(struct tty_struct *tty,
895 struct stlibrd *brdp, struct stliport *portp)
896{
897 asynotify_t nt;
898 asyport_t aport;
899 int rc;
900
901 if ((rc = stli_rawopen(brdp, portp, 0, 1)) < 0)
902 return rc;
903
904 memset(&nt, 0, sizeof(asynotify_t));
905 nt.data = (DT_TXLOW | DT_TXEMPTY | DT_RXBUSY | DT_RXBREAK);
906 nt.signal = SG_DCD;
907 if ((rc = stli_cmdwait(brdp, portp, A_SETNOTIFY, &nt,
908 sizeof(asynotify_t), 0)) < 0)
909 return rc;
910
911 stli_mkasyport(tty, portp, &aport, tty->termios);
912 if ((rc = stli_cmdwait(brdp, portp, A_SETPORT, &aport,
913 sizeof(asyport_t), 0)) < 0)
914 return rc;
915
916 set_bit(ST_GETSIGS, &portp->state);
917 if ((rc = stli_cmdwait(brdp, portp, A_GETSIGNALS, &portp->asig,
918 sizeof(asysigs_t), 1)) < 0)
919 return rc;
920 if (test_and_clear_bit(ST_GETSIGS, &portp->state))
921 portp->sigs = stli_mktiocm(portp->asig.sigvalue);
922 stli_mkasysigs(&portp->asig, 1, 1);
923 if ((rc = stli_cmdwait(brdp, portp, A_SETSIGNALS, &portp->asig,
924 sizeof(asysigs_t), 0)) < 0)
925 return rc;
926
927 return 0;
928}
929
930/*****************************************************************************/
931
932/*
933 * Send an open message to the slave. This will sleep waiting for the
934 * acknowledgement, so must have user context. We need to co-ordinate
935 * with close events here, since we don't want open and close events
936 * to overlap.
937 */
938
939static int stli_rawopen(struct stlibrd *brdp, struct stliport *portp, unsigned long arg, int wait)
940{
941 cdkhdr_t __iomem *hdrp;
942 cdkctrl_t __iomem *cp;
943 unsigned char __iomem *bits;
944 unsigned long flags;
945 int rc;
946
947/*
948 * Send a message to the slave to open this port.
949 */
950
951/*
952 * Slave is already closing this port. This can happen if a hangup
953 * occurs on this port. So we must wait until it is complete. The
954 * order of opens and closes may not be preserved across shared
955 * memory, so we must wait until it is complete.
956 */
957 wait_event_interruptible_tty(portp->raw_wait,
958 !test_bit(ST_CLOSING, &portp->state));
959 if (signal_pending(current)) {
960 return -ERESTARTSYS;
961 }
962
963/*
964 * Everything is ready now, so write the open message into shared
965 * memory. Once the message is in set the service bits to say that
966 * this port wants service.
967 */
968 spin_lock_irqsave(&brd_lock, flags);
969 EBRDENABLE(brdp);
970 cp = &((cdkasy_t __iomem *) EBRDGETMEMPTR(brdp, portp->addr))->ctrl;
971 writel(arg, &cp->openarg);
972 writeb(1, &cp->open);
973 hdrp = (cdkhdr_t __iomem *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
974 bits = ((unsigned char __iomem *) hdrp) + brdp->slaveoffset +
975 portp->portidx;
976 writeb(readb(bits) | portp->portbit, bits);
977 EBRDDISABLE(brdp);
978
979 if (wait == 0) {
980 spin_unlock_irqrestore(&brd_lock, flags);
981 return 0;
982 }
983
984/*
985 * Slave is in action, so now we must wait for the open acknowledgment
986 * to come back.
987 */
988 rc = 0;
989 set_bit(ST_OPENING, &portp->state);
990 spin_unlock_irqrestore(&brd_lock, flags);
991
992 wait_event_interruptible_tty(portp->raw_wait,
993 !test_bit(ST_OPENING, &portp->state));
994 if (signal_pending(current))
995 rc = -ERESTARTSYS;
996
997 if ((rc == 0) && (portp->rc != 0))
998 rc = -EIO;
999 return rc;
1000}
1001
1002/*****************************************************************************/
1003
1004/*
1005 * Send a close message to the slave. Normally this will sleep waiting
1006 * for the acknowledgement, but if wait parameter is 0 it will not. If
1007 * wait is true then must have user context (to sleep).
1008 */
1009
1010static int stli_rawclose(struct stlibrd *brdp, struct stliport *portp, unsigned long arg, int wait)
1011{
1012 cdkhdr_t __iomem *hdrp;
1013 cdkctrl_t __iomem *cp;
1014 unsigned char __iomem *bits;
1015 unsigned long flags;
1016 int rc;
1017
1018/*
1019 * Slave is already closing this port. This can happen if a hangup
1020 * occurs on this port.
1021 */
1022 if (wait) {
1023 wait_event_interruptible_tty(portp->raw_wait,
1024 !test_bit(ST_CLOSING, &portp->state));
1025 if (signal_pending(current)) {
1026 return -ERESTARTSYS;
1027 }
1028 }
1029
1030/*
1031 * Write the close command into shared memory.
1032 */
1033 spin_lock_irqsave(&brd_lock, flags);
1034 EBRDENABLE(brdp);
1035 cp = &((cdkasy_t __iomem *) EBRDGETMEMPTR(brdp, portp->addr))->ctrl;
1036 writel(arg, &cp->closearg);
1037 writeb(1, &cp->close);
1038 hdrp = (cdkhdr_t __iomem *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
1039 bits = ((unsigned char __iomem *) hdrp) + brdp->slaveoffset +
1040 portp->portidx;
1041 writeb(readb(bits) |portp->portbit, bits);
1042 EBRDDISABLE(brdp);
1043
1044 set_bit(ST_CLOSING, &portp->state);
1045 spin_unlock_irqrestore(&brd_lock, flags);
1046
1047 if (wait == 0)
1048 return 0;
1049
1050/*
1051 * Slave is in action, so now we must wait for the open acknowledgment
1052 * to come back.
1053 */
1054 rc = 0;
1055 wait_event_interruptible_tty(portp->raw_wait,
1056 !test_bit(ST_CLOSING, &portp->state));
1057 if (signal_pending(current))
1058 rc = -ERESTARTSYS;
1059
1060 if ((rc == 0) && (portp->rc != 0))
1061 rc = -EIO;
1062 return rc;
1063}
1064
1065/*****************************************************************************/
1066
1067/*
1068 * Send a command to the slave and wait for the response. This must
1069 * have user context (it sleeps). This routine is generic in that it
1070 * can send any type of command. Its purpose is to wait for that command
1071 * to complete (as opposed to initiating the command then returning).
1072 */
1073
1074static int stli_cmdwait(struct stlibrd *brdp, struct stliport *portp, unsigned long cmd, void *arg, int size, int copyback)
1075{
1076 /*
1077 * no need for wait_event_tty because clearing ST_CMDING cannot block
1078 * on BTM
1079 */
1080 wait_event_interruptible(portp->raw_wait,
1081 !test_bit(ST_CMDING, &portp->state));
1082 if (signal_pending(current))
1083 return -ERESTARTSYS;
1084
1085 stli_sendcmd(brdp, portp, cmd, arg, size, copyback);
1086
1087 wait_event_interruptible(portp->raw_wait,
1088 !test_bit(ST_CMDING, &portp->state));
1089 if (signal_pending(current))
1090 return -ERESTARTSYS;
1091
1092 if (portp->rc != 0)
1093 return -EIO;
1094 return 0;
1095}
1096
1097/*****************************************************************************/
1098
1099/*
1100 * Send the termios settings for this port to the slave. This sleeps
1101 * waiting for the command to complete - so must have user context.
1102 */
1103
1104static int stli_setport(struct tty_struct *tty)
1105{
1106 struct stliport *portp = tty->driver_data;
1107 struct stlibrd *brdp;
1108 asyport_t aport;
1109
1110 if (portp == NULL)
1111 return -ENODEV;
1112 if (portp->brdnr >= stli_nrbrds)
1113 return -ENODEV;
1114 brdp = stli_brds[portp->brdnr];
1115 if (brdp == NULL)
1116 return -ENODEV;
1117
1118 stli_mkasyport(tty, portp, &aport, tty->termios);
1119 return(stli_cmdwait(brdp, portp, A_SETPORT, &aport, sizeof(asyport_t), 0));
1120}
1121
1122/*****************************************************************************/
1123
1124static int stli_carrier_raised(struct tty_port *port)
1125{
1126 struct stliport *portp = container_of(port, struct stliport, port);
1127 return (portp->sigs & TIOCM_CD) ? 1 : 0;
1128}
1129
1130static void stli_dtr_rts(struct tty_port *port, int on)
1131{
1132 struct stliport *portp = container_of(port, struct stliport, port);
1133 struct stlibrd *brdp = stli_brds[portp->brdnr];
1134 stli_mkasysigs(&portp->asig, on, on);
1135 if (stli_cmdwait(brdp, portp, A_SETSIGNALS, &portp->asig,
1136 sizeof(asysigs_t), 0) < 0)
1137 printk(KERN_WARNING "istallion: dtr set failed.\n");
1138}
1139
1140
1141/*****************************************************************************/
1142
1143/*
1144 * Write routine. Take the data and put it in the shared memory ring
1145 * queue. If port is not already sending chars then need to mark the
1146 * service bits for this port.
1147 */
1148
1149static int stli_write(struct tty_struct *tty, const unsigned char *buf, int count)
1150{
1151 cdkasy_t __iomem *ap;
1152 cdkhdr_t __iomem *hdrp;
1153 unsigned char __iomem *bits;
1154 unsigned char __iomem *shbuf;
1155 unsigned char *chbuf;
1156 struct stliport *portp;
1157 struct stlibrd *brdp;
1158 unsigned int len, stlen, head, tail, size;
1159 unsigned long flags;
1160
1161 if (tty == stli_txcooktty)
1162 stli_flushchars(tty);
1163 portp = tty->driver_data;
1164 if (portp == NULL)
1165 return 0;
1166 if (portp->brdnr >= stli_nrbrds)
1167 return 0;
1168 brdp = stli_brds[portp->brdnr];
1169 if (brdp == NULL)
1170 return 0;
1171 chbuf = (unsigned char *) buf;
1172
1173/*
1174 * All data is now local, shove as much as possible into shared memory.
1175 */
1176 spin_lock_irqsave(&brd_lock, flags);
1177 EBRDENABLE(brdp);
1178 ap = (cdkasy_t __iomem *) EBRDGETMEMPTR(brdp, portp->addr);
1179 head = (unsigned int) readw(&ap->txq.head);
1180 tail = (unsigned int) readw(&ap->txq.tail);
1181 if (tail != ((unsigned int) readw(&ap->txq.tail)))
1182 tail = (unsigned int) readw(&ap->txq.tail);
1183 size = portp->txsize;
1184 if (head >= tail) {
1185 len = size - (head - tail) - 1;
1186 stlen = size - head;
1187 } else {
1188 len = tail - head - 1;
1189 stlen = len;
1190 }
1191
1192 len = min(len, (unsigned int)count);
1193 count = 0;
1194 shbuf = (char __iomem *) EBRDGETMEMPTR(brdp, portp->txoffset);
1195
1196 while (len > 0) {
1197 stlen = min(len, stlen);
1198 memcpy_toio(shbuf + head, chbuf, stlen);
1199 chbuf += stlen;
1200 len -= stlen;
1201 count += stlen;
1202 head += stlen;
1203 if (head >= size) {
1204 head = 0;
1205 stlen = tail;
1206 }
1207 }
1208
1209 ap = (cdkasy_t __iomem *) EBRDGETMEMPTR(brdp, portp->addr);
1210 writew(head, &ap->txq.head);
1211 if (test_bit(ST_TXBUSY, &portp->state)) {
1212 if (readl(&ap->changed.data) & DT_TXEMPTY)
1213 writel(readl(&ap->changed.data) & ~DT_TXEMPTY, &ap->changed.data);
1214 }
1215 hdrp = (cdkhdr_t __iomem *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
1216 bits = ((unsigned char __iomem *) hdrp) + brdp->slaveoffset +
1217 portp->portidx;
1218 writeb(readb(bits) | portp->portbit, bits);
1219 set_bit(ST_TXBUSY, &portp->state);
1220 EBRDDISABLE(brdp);
1221 spin_unlock_irqrestore(&brd_lock, flags);
1222
1223 return(count);
1224}
1225
1226/*****************************************************************************/
1227
1228/*
1229 * Output a single character. We put it into a temporary local buffer
1230 * (for speed) then write out that buffer when the flushchars routine
1231 * is called. There is a safety catch here so that if some other port
1232 * writes chars before the current buffer has been, then we write them
1233 * first them do the new ports.
1234 */
1235
1236static int stli_putchar(struct tty_struct *tty, unsigned char ch)
1237{
1238 if (tty != stli_txcooktty) {
1239 if (stli_txcooktty != NULL)
1240 stli_flushchars(stli_txcooktty);
1241 stli_txcooktty = tty;
1242 }
1243
1244 stli_txcookbuf[stli_txcooksize++] = ch;
1245 return 0;
1246}
1247
1248/*****************************************************************************/
1249
1250/*
1251 * Transfer characters from the local TX cooking buffer to the board.
1252 * We sort of ignore the tty that gets passed in here. We rely on the
1253 * info stored with the TX cook buffer to tell us which port to flush
1254 * the data on. In any case we clean out the TX cook buffer, for re-use
1255 * by someone else.
1256 */
1257
1258static void stli_flushchars(struct tty_struct *tty)
1259{
1260 cdkhdr_t __iomem *hdrp;
1261 unsigned char __iomem *bits;
1262 cdkasy_t __iomem *ap;
1263 struct tty_struct *cooktty;
1264 struct stliport *portp;
1265 struct stlibrd *brdp;
1266 unsigned int len, stlen, head, tail, size, count, cooksize;
1267 unsigned char *buf;
1268 unsigned char __iomem *shbuf;
1269 unsigned long flags;
1270
1271 cooksize = stli_txcooksize;
1272 cooktty = stli_txcooktty;
1273 stli_txcooksize = 0;
1274 stli_txcookrealsize = 0;
1275 stli_txcooktty = NULL;
1276
1277 if (cooktty == NULL)
1278 return;
1279 if (tty != cooktty)
1280 tty = cooktty;
1281 if (cooksize == 0)
1282 return;
1283
1284 portp = tty->driver_data;
1285 if (portp == NULL)
1286 return;
1287 if (portp->brdnr >= stli_nrbrds)
1288 return;
1289 brdp = stli_brds[portp->brdnr];
1290 if (brdp == NULL)
1291 return;
1292
1293 spin_lock_irqsave(&brd_lock, flags);
1294 EBRDENABLE(brdp);
1295
1296 ap = (cdkasy_t __iomem *) EBRDGETMEMPTR(brdp, portp->addr);
1297 head = (unsigned int) readw(&ap->txq.head);
1298 tail = (unsigned int) readw(&ap->txq.tail);
1299 if (tail != ((unsigned int) readw(&ap->txq.tail)))
1300 tail = (unsigned int) readw(&ap->txq.tail);
1301 size = portp->txsize;
1302 if (head >= tail) {
1303 len = size - (head - tail) - 1;
1304 stlen = size - head;
1305 } else {
1306 len = tail - head - 1;
1307 stlen = len;
1308 }
1309
1310 len = min(len, cooksize);
1311 count = 0;
1312 shbuf = EBRDGETMEMPTR(brdp, portp->txoffset);
1313 buf = stli_txcookbuf;
1314
1315 while (len > 0) {
1316 stlen = min(len, stlen);
1317 memcpy_toio(shbuf + head, buf, stlen);
1318 buf += stlen;
1319 len -= stlen;
1320 count += stlen;
1321 head += stlen;
1322 if (head >= size) {
1323 head = 0;
1324 stlen = tail;
1325 }
1326 }
1327
1328 ap = (cdkasy_t __iomem *) EBRDGETMEMPTR(brdp, portp->addr);
1329 writew(head, &ap->txq.head);
1330
1331 if (test_bit(ST_TXBUSY, &portp->state)) {
1332 if (readl(&ap->changed.data) & DT_TXEMPTY)
1333 writel(readl(&ap->changed.data) & ~DT_TXEMPTY, &ap->changed.data);
1334 }
1335 hdrp = (cdkhdr_t __iomem *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
1336 bits = ((unsigned char __iomem *) hdrp) + brdp->slaveoffset +
1337 portp->portidx;
1338 writeb(readb(bits) | portp->portbit, bits);
1339 set_bit(ST_TXBUSY, &portp->state);
1340
1341 EBRDDISABLE(brdp);
1342 spin_unlock_irqrestore(&brd_lock, flags);
1343}
1344
1345/*****************************************************************************/
1346
1347static int stli_writeroom(struct tty_struct *tty)
1348{
1349 cdkasyrq_t __iomem *rp;
1350 struct stliport *portp;
1351 struct stlibrd *brdp;
1352 unsigned int head, tail, len;
1353 unsigned long flags;
1354
1355 if (tty == stli_txcooktty) {
1356 if (stli_txcookrealsize != 0) {
1357 len = stli_txcookrealsize - stli_txcooksize;
1358 return len;
1359 }
1360 }
1361
1362 portp = tty->driver_data;
1363 if (portp == NULL)
1364 return 0;
1365 if (portp->brdnr >= stli_nrbrds)
1366 return 0;
1367 brdp = stli_brds[portp->brdnr];
1368 if (brdp == NULL)
1369 return 0;
1370
1371 spin_lock_irqsave(&brd_lock, flags);
1372 EBRDENABLE(brdp);
1373 rp = &((cdkasy_t __iomem *) EBRDGETMEMPTR(brdp, portp->addr))->txq;
1374 head = (unsigned int) readw(&rp->head);
1375 tail = (unsigned int) readw(&rp->tail);
1376 if (tail != ((unsigned int) readw(&rp->tail)))
1377 tail = (unsigned int) readw(&rp->tail);
1378 len = (head >= tail) ? (portp->txsize - (head - tail)) : (tail - head);
1379 len--;
1380 EBRDDISABLE(brdp);
1381 spin_unlock_irqrestore(&brd_lock, flags);
1382
1383 if (tty == stli_txcooktty) {
1384 stli_txcookrealsize = len;
1385 len -= stli_txcooksize;
1386 }
1387 return len;
1388}
1389
1390/*****************************************************************************/
1391
1392/*
1393 * Return the number of characters in the transmit buffer. Normally we
1394 * will return the number of chars in the shared memory ring queue.
1395 * We need to kludge around the case where the shared memory buffer is
1396 * empty but not all characters have drained yet, for this case just
1397 * return that there is 1 character in the buffer!
1398 */
1399
1400static int stli_charsinbuffer(struct tty_struct *tty)
1401{
1402 cdkasyrq_t __iomem *rp;
1403 struct stliport *portp;
1404 struct stlibrd *brdp;
1405 unsigned int head, tail, len;
1406 unsigned long flags;
1407
1408 if (tty == stli_txcooktty)
1409 stli_flushchars(tty);
1410 portp = tty->driver_data;
1411 if (portp == NULL)
1412 return 0;
1413 if (portp->brdnr >= stli_nrbrds)
1414 return 0;
1415 brdp = stli_brds[portp->brdnr];
1416 if (brdp == NULL)
1417 return 0;
1418
1419 spin_lock_irqsave(&brd_lock, flags);
1420 EBRDENABLE(brdp);
1421 rp = &((cdkasy_t __iomem *) EBRDGETMEMPTR(brdp, portp->addr))->txq;
1422 head = (unsigned int) readw(&rp->head);
1423 tail = (unsigned int) readw(&rp->tail);
1424 if (tail != ((unsigned int) readw(&rp->tail)))
1425 tail = (unsigned int) readw(&rp->tail);
1426 len = (head >= tail) ? (head - tail) : (portp->txsize - (tail - head));
1427 if ((len == 0) && test_bit(ST_TXBUSY, &portp->state))
1428 len = 1;
1429 EBRDDISABLE(brdp);
1430 spin_unlock_irqrestore(&brd_lock, flags);
1431
1432 return len;
1433}
1434
1435/*****************************************************************************/
1436
1437/*
1438 * Generate the serial struct info.
1439 */
1440
1441static int stli_getserial(struct stliport *portp, struct serial_struct __user *sp)
1442{
1443 struct serial_struct sio;
1444 struct stlibrd *brdp;
1445
1446 memset(&sio, 0, sizeof(struct serial_struct));
1447 sio.type = PORT_UNKNOWN;
1448 sio.line = portp->portnr;
1449 sio.irq = 0;
1450 sio.flags = portp->port.flags;
1451 sio.baud_base = portp->baud_base;
1452 sio.close_delay = portp->port.close_delay;
1453 sio.closing_wait = portp->closing_wait;
1454 sio.custom_divisor = portp->custom_divisor;
1455 sio.xmit_fifo_size = 0;
1456 sio.hub6 = 0;
1457
1458 brdp = stli_brds[portp->brdnr];
1459 if (brdp != NULL)
1460 sio.port = brdp->iobase;
1461
1462 return copy_to_user(sp, &sio, sizeof(struct serial_struct)) ?
1463 -EFAULT : 0;
1464}
1465
1466/*****************************************************************************/
1467
1468/*
1469 * Set port according to the serial struct info.
1470 * At this point we do not do any auto-configure stuff, so we will
1471 * just quietly ignore any requests to change irq, etc.
1472 */
1473
1474static int stli_setserial(struct tty_struct *tty, struct serial_struct __user *sp)
1475{
1476 struct serial_struct sio;
1477 int rc;
1478 struct stliport *portp = tty->driver_data;
1479
1480 if (copy_from_user(&sio, sp, sizeof(struct serial_struct)))
1481 return -EFAULT;
1482 if (!capable(CAP_SYS_ADMIN)) {
1483 if ((sio.baud_base != portp->baud_base) ||
1484 (sio.close_delay != portp->port.close_delay) ||
1485 ((sio.flags & ~ASYNC_USR_MASK) !=
1486 (portp->port.flags & ~ASYNC_USR_MASK)))
1487 return -EPERM;
1488 }
1489
1490 portp->port.flags = (portp->port.flags & ~ASYNC_USR_MASK) |
1491 (sio.flags & ASYNC_USR_MASK);
1492 portp->baud_base = sio.baud_base;
1493 portp->port.close_delay = sio.close_delay;
1494 portp->closing_wait = sio.closing_wait;
1495 portp->custom_divisor = sio.custom_divisor;
1496
1497 if ((rc = stli_setport(tty)) < 0)
1498 return rc;
1499 return 0;
1500}
1501
1502/*****************************************************************************/
1503
1504static int stli_tiocmget(struct tty_struct *tty, struct file *file)
1505{
1506 struct stliport *portp = tty->driver_data;
1507 struct stlibrd *brdp;
1508 int rc;
1509
1510 if (portp == NULL)
1511 return -ENODEV;
1512 if (portp->brdnr >= stli_nrbrds)
1513 return 0;
1514 brdp = stli_brds[portp->brdnr];
1515 if (brdp == NULL)
1516 return 0;
1517 if (tty->flags & (1 << TTY_IO_ERROR))
1518 return -EIO;
1519
1520 if ((rc = stli_cmdwait(brdp, portp, A_GETSIGNALS,
1521 &portp->asig, sizeof(asysigs_t), 1)) < 0)
1522 return rc;
1523
1524 return stli_mktiocm(portp->asig.sigvalue);
1525}
1526
1527static int stli_tiocmset(struct tty_struct *tty, struct file *file,
1528 unsigned int set, unsigned int clear)
1529{
1530 struct stliport *portp = tty->driver_data;
1531 struct stlibrd *brdp;
1532 int rts = -1, dtr = -1;
1533
1534 if (portp == NULL)
1535 return -ENODEV;
1536 if (portp->brdnr >= stli_nrbrds)
1537 return 0;
1538 brdp = stli_brds[portp->brdnr];
1539 if (brdp == NULL)
1540 return 0;
1541 if (tty->flags & (1 << TTY_IO_ERROR))
1542 return -EIO;
1543
1544 if (set & TIOCM_RTS)
1545 rts = 1;
1546 if (set & TIOCM_DTR)
1547 dtr = 1;
1548 if (clear & TIOCM_RTS)
1549 rts = 0;
1550 if (clear & TIOCM_DTR)
1551 dtr = 0;
1552
1553 stli_mkasysigs(&portp->asig, dtr, rts);
1554
1555 return stli_cmdwait(brdp, portp, A_SETSIGNALS, &portp->asig,
1556 sizeof(asysigs_t), 0);
1557}
1558
1559static int stli_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
1560{
1561 struct stliport *portp;
1562 struct stlibrd *brdp;
1563 int rc;
1564 void __user *argp = (void __user *)arg;
1565
1566 portp = tty->driver_data;
1567 if (portp == NULL)
1568 return -ENODEV;
1569 if (portp->brdnr >= stli_nrbrds)
1570 return 0;
1571 brdp = stli_brds[portp->brdnr];
1572 if (brdp == NULL)
1573 return 0;
1574
1575 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1576 (cmd != COM_GETPORTSTATS) && (cmd != COM_CLRPORTSTATS)) {
1577 if (tty->flags & (1 << TTY_IO_ERROR))
1578 return -EIO;
1579 }
1580
1581 rc = 0;
1582
1583 switch (cmd) {
1584 case TIOCGSERIAL:
1585 rc = stli_getserial(portp, argp);
1586 break;
1587 case TIOCSSERIAL:
1588 rc = stli_setserial(tty, argp);
1589 break;
1590 case STL_GETPFLAG:
1591 rc = put_user(portp->pflag, (unsigned __user *)argp);
1592 break;
1593 case STL_SETPFLAG:
1594 if ((rc = get_user(portp->pflag, (unsigned __user *)argp)) == 0)
1595 stli_setport(tty);
1596 break;
1597 case COM_GETPORTSTATS:
1598 rc = stli_getportstats(tty, portp, argp);
1599 break;
1600 case COM_CLRPORTSTATS:
1601 rc = stli_clrportstats(portp, argp);
1602 break;
1603 case TIOCSERCONFIG:
1604 case TIOCSERGWILD:
1605 case TIOCSERSWILD:
1606 case TIOCSERGETLSR:
1607 case TIOCSERGSTRUCT:
1608 case TIOCSERGETMULTI:
1609 case TIOCSERSETMULTI:
1610 default:
1611 rc = -ENOIOCTLCMD;
1612 break;
1613 }
1614
1615 return rc;
1616}
1617
1618/*****************************************************************************/
1619
1620/*
1621 * This routine assumes that we have user context and can sleep.
1622 * Looks like it is true for the current ttys implementation..!!
1623 */
1624
1625static void stli_settermios(struct tty_struct *tty, struct ktermios *old)
1626{
1627 struct stliport *portp;
1628 struct stlibrd *brdp;
1629 struct ktermios *tiosp;
1630 asyport_t aport;
1631
1632 portp = tty->driver_data;
1633 if (portp == NULL)
1634 return;
1635 if (portp->brdnr >= stli_nrbrds)
1636 return;
1637 brdp = stli_brds[portp->brdnr];
1638 if (brdp == NULL)
1639 return;
1640
1641 tiosp = tty->termios;
1642
1643 stli_mkasyport(tty, portp, &aport, tiosp);
1644 stli_cmdwait(brdp, portp, A_SETPORT, &aport, sizeof(asyport_t), 0);
1645 stli_mkasysigs(&portp->asig, ((tiosp->c_cflag & CBAUD) ? 1 : 0), -1);
1646 stli_cmdwait(brdp, portp, A_SETSIGNALS, &portp->asig,
1647 sizeof(asysigs_t), 0);
1648 if ((old->c_cflag & CRTSCTS) && ((tiosp->c_cflag & CRTSCTS) == 0))
1649 tty->hw_stopped = 0;
1650 if (((old->c_cflag & CLOCAL) == 0) && (tiosp->c_cflag & CLOCAL))
1651 wake_up_interruptible(&portp->port.open_wait);
1652}
1653
1654/*****************************************************************************/
1655
1656/*
1657 * Attempt to flow control who ever is sending us data. We won't really
1658 * do any flow control action here. We can't directly, and even if we
1659 * wanted to we would have to send a command to the slave. The slave
1660 * knows how to flow control, and will do so when its buffers reach its
1661 * internal high water marks. So what we will do is set a local state
1662 * bit that will stop us sending any RX data up from the poll routine
1663 * (which is the place where RX data from the slave is handled).
1664 */
1665
1666static void stli_throttle(struct tty_struct *tty)
1667{
1668 struct stliport *portp = tty->driver_data;
1669 if (portp == NULL)
1670 return;
1671 set_bit(ST_RXSTOP, &portp->state);
1672}
1673
1674/*****************************************************************************/
1675
1676/*
1677 * Unflow control the device sending us data... That means that all
1678 * we have to do is clear the RXSTOP state bit. The next poll call
1679 * will then be able to pass the RX data back up.
1680 */
1681
1682static void stli_unthrottle(struct tty_struct *tty)
1683{
1684 struct stliport *portp = tty->driver_data;
1685 if (portp == NULL)
1686 return;
1687 clear_bit(ST_RXSTOP, &portp->state);
1688}
1689
1690/*****************************************************************************/
1691
1692/*
1693 * Stop the transmitter.
1694 */
1695
1696static void stli_stop(struct tty_struct *tty)
1697{
1698}
1699
1700/*****************************************************************************/
1701
1702/*
1703 * Start the transmitter again.
1704 */
1705
1706static void stli_start(struct tty_struct *tty)
1707{
1708}
1709
1710/*****************************************************************************/
1711
1712
1713/*
1714 * Hangup this port. This is pretty much like closing the port, only
1715 * a little more brutal. No waiting for data to drain. Shutdown the
1716 * port and maybe drop signals. This is rather tricky really. We want
1717 * to close the port as well.
1718 */
1719
1720static void stli_hangup(struct tty_struct *tty)
1721{
1722 struct stliport *portp = tty->driver_data;
1723 tty_port_hangup(&portp->port);
1724}
1725
1726/*****************************************************************************/
1727
1728/*
1729 * Flush characters from the lower buffer. We may not have user context
1730 * so we cannot sleep waiting for it to complete. Also we need to check
1731 * if there is chars for this port in the TX cook buffer, and flush them
1732 * as well.
1733 */
1734
1735static void stli_flushbuffer(struct tty_struct *tty)
1736{
1737 struct stliport *portp;
1738 struct stlibrd *brdp;
1739 unsigned long ftype, flags;
1740
1741 portp = tty->driver_data;
1742 if (portp == NULL)
1743 return;
1744 if (portp->brdnr >= stli_nrbrds)
1745 return;
1746 brdp = stli_brds[portp->brdnr];
1747 if (brdp == NULL)
1748 return;
1749
1750 spin_lock_irqsave(&brd_lock, flags);
1751 if (tty == stli_txcooktty) {
1752 stli_txcooktty = NULL;
1753 stli_txcooksize = 0;
1754 stli_txcookrealsize = 0;
1755 }
1756 if (test_bit(ST_CMDING, &portp->state)) {
1757 set_bit(ST_DOFLUSHTX, &portp->state);
1758 } else {
1759 ftype = FLUSHTX;
1760 if (test_bit(ST_DOFLUSHRX, &portp->state)) {
1761 ftype |= FLUSHRX;
1762 clear_bit(ST_DOFLUSHRX, &portp->state);
1763 }
1764 __stli_sendcmd(brdp, portp, A_FLUSH, &ftype, sizeof(u32), 0);
1765 }
1766 spin_unlock_irqrestore(&brd_lock, flags);
1767 tty_wakeup(tty);
1768}
1769
1770/*****************************************************************************/
1771
1772static int stli_breakctl(struct tty_struct *tty, int state)
1773{
1774 struct stlibrd *brdp;
1775 struct stliport *portp;
1776 long arg;
1777
1778 portp = tty->driver_data;
1779 if (portp == NULL)
1780 return -EINVAL;
1781 if (portp->brdnr >= stli_nrbrds)
1782 return -EINVAL;
1783 brdp = stli_brds[portp->brdnr];
1784 if (brdp == NULL)
1785 return -EINVAL;
1786
1787 arg = (state == -1) ? BREAKON : BREAKOFF;
1788 stli_cmdwait(brdp, portp, A_BREAK, &arg, sizeof(long), 0);
1789 return 0;
1790}
1791
1792/*****************************************************************************/
1793
1794static void stli_waituntilsent(struct tty_struct *tty, int timeout)
1795{
1796 struct stliport *portp;
1797 unsigned long tend;
1798
1799 portp = tty->driver_data;
1800 if (portp == NULL)
1801 return;
1802
1803 if (timeout == 0)
1804 timeout = HZ;
1805 tend = jiffies + timeout;
1806
1807 while (test_bit(ST_TXBUSY, &portp->state)) {
1808 if (signal_pending(current))
1809 break;
1810 msleep_interruptible(20);
1811 if (time_after_eq(jiffies, tend))
1812 break;
1813 }
1814}
1815
1816/*****************************************************************************/
1817
1818static void stli_sendxchar(struct tty_struct *tty, char ch)
1819{
1820 struct stlibrd *brdp;
1821 struct stliport *portp;
1822 asyctrl_t actrl;
1823
1824 portp = tty->driver_data;
1825 if (portp == NULL)
1826 return;
1827 if (portp->brdnr >= stli_nrbrds)
1828 return;
1829 brdp = stli_brds[portp->brdnr];
1830 if (brdp == NULL)
1831 return;
1832
1833 memset(&actrl, 0, sizeof(asyctrl_t));
1834 if (ch == STOP_CHAR(tty)) {
1835 actrl.rxctrl = CT_STOPFLOW;
1836 } else if (ch == START_CHAR(tty)) {
1837 actrl.rxctrl = CT_STARTFLOW;
1838 } else {
1839 actrl.txctrl = CT_SENDCHR;
1840 actrl.tximdch = ch;
1841 }
1842 stli_cmdwait(brdp, portp, A_PORTCTRL, &actrl, sizeof(asyctrl_t), 0);
1843}
1844
1845static void stli_portinfo(struct seq_file *m, struct stlibrd *brdp, struct stliport *portp, int portnr)
1846{
1847 char *uart;
1848 int rc;
1849
1850 rc = stli_portcmdstats(NULL, portp);
1851
1852 uart = "UNKNOWN";
1853 if (test_bit(BST_STARTED, &brdp->state)) {
1854 switch (stli_comstats.hwid) {
1855 case 0: uart = "2681"; break;
1856 case 1: uart = "SC26198"; break;
1857 default:uart = "CD1400"; break;
1858 }
1859 }
1860 seq_printf(m, "%d: uart:%s ", portnr, uart);
1861
1862 if (test_bit(BST_STARTED, &brdp->state) && rc >= 0) {
1863 char sep;
1864
1865 seq_printf(m, "tx:%d rx:%d", (int) stli_comstats.txtotal,
1866 (int) stli_comstats.rxtotal);
1867
1868 if (stli_comstats.rxframing)
1869 seq_printf(m, " fe:%d",
1870 (int) stli_comstats.rxframing);
1871 if (stli_comstats.rxparity)
1872 seq_printf(m, " pe:%d",
1873 (int) stli_comstats.rxparity);
1874 if (stli_comstats.rxbreaks)
1875 seq_printf(m, " brk:%d",
1876 (int) stli_comstats.rxbreaks);
1877 if (stli_comstats.rxoverrun)
1878 seq_printf(m, " oe:%d",
1879 (int) stli_comstats.rxoverrun);
1880
1881 sep = ' ';
1882 if (stli_comstats.signals & TIOCM_RTS) {
1883 seq_printf(m, "%c%s", sep, "RTS");
1884 sep = '|';
1885 }
1886 if (stli_comstats.signals & TIOCM_CTS) {
1887 seq_printf(m, "%c%s", sep, "CTS");
1888 sep = '|';
1889 }
1890 if (stli_comstats.signals & TIOCM_DTR) {
1891 seq_printf(m, "%c%s", sep, "DTR");
1892 sep = '|';
1893 }
1894 if (stli_comstats.signals & TIOCM_CD) {
1895 seq_printf(m, "%c%s", sep, "DCD");
1896 sep = '|';
1897 }
1898 if (stli_comstats.signals & TIOCM_DSR) {
1899 seq_printf(m, "%c%s", sep, "DSR");
1900 sep = '|';
1901 }
1902 }
1903 seq_putc(m, '\n');
1904}
1905
1906/*****************************************************************************/
1907
1908/*
1909 * Port info, read from the /proc file system.
1910 */
1911
1912static int stli_proc_show(struct seq_file *m, void *v)
1913{
1914 struct stlibrd *brdp;
1915 struct stliport *portp;
1916 unsigned int brdnr, portnr, totalport;
1917
1918 totalport = 0;
1919
1920 seq_printf(m, "%s: version %s\n", stli_drvtitle, stli_drvversion);
1921
1922/*
1923 * We scan through for each board, panel and port. The offset is
1924 * calculated on the fly, and irrelevant ports are skipped.
1925 */
1926 for (brdnr = 0; (brdnr < stli_nrbrds); brdnr++) {
1927 brdp = stli_brds[brdnr];
1928 if (brdp == NULL)
1929 continue;
1930 if (brdp->state == 0)
1931 continue;
1932
1933 totalport = brdnr * STL_MAXPORTS;
1934 for (portnr = 0; (portnr < brdp->nrports); portnr++,
1935 totalport++) {
1936 portp = brdp->ports[portnr];
1937 if (portp == NULL)
1938 continue;
1939 stli_portinfo(m, brdp, portp, totalport);
1940 }
1941 }
1942 return 0;
1943}
1944
1945static int stli_proc_open(struct inode *inode, struct file *file)
1946{
1947 return single_open(file, stli_proc_show, NULL);
1948}
1949
1950static const struct file_operations stli_proc_fops = {
1951 .owner = THIS_MODULE,
1952 .open = stli_proc_open,
1953 .read = seq_read,
1954 .llseek = seq_lseek,
1955 .release = single_release,
1956};
1957
1958/*****************************************************************************/
1959
1960/*
1961 * Generic send command routine. This will send a message to the slave,
1962 * of the specified type with the specified argument. Must be very
1963 * careful of data that will be copied out from shared memory -
1964 * containing command results. The command completion is all done from
1965 * a poll routine that does not have user context. Therefore you cannot
1966 * copy back directly into user space, or to the kernel stack of a
1967 * process. This routine does not sleep, so can be called from anywhere.
1968 *
1969 * The caller must hold the brd_lock (see also stli_sendcmd the usual
1970 * entry point)
1971 */
1972
1973static void __stli_sendcmd(struct stlibrd *brdp, struct stliport *portp, unsigned long cmd, void *arg, int size, int copyback)
1974{
1975 cdkhdr_t __iomem *hdrp;
1976 cdkctrl_t __iomem *cp;
1977 unsigned char __iomem *bits;
1978
1979 if (test_bit(ST_CMDING, &portp->state)) {
1980 printk(KERN_ERR "istallion: command already busy, cmd=%x!\n",
1981 (int) cmd);
1982 return;
1983 }
1984
1985 EBRDENABLE(brdp);
1986 cp = &((cdkasy_t __iomem *) EBRDGETMEMPTR(brdp, portp->addr))->ctrl;
1987 if (size > 0) {
1988 memcpy_toio((void __iomem *) &(cp->args[0]), arg, size);
1989 if (copyback) {
1990 portp->argp = arg;
1991 portp->argsize = size;
1992 }
1993 }
1994 writel(0, &cp->status);
1995 writel(cmd, &cp->cmd);
1996 hdrp = (cdkhdr_t __iomem *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
1997 bits = ((unsigned char __iomem *) hdrp) + brdp->slaveoffset +
1998 portp->portidx;
1999 writeb(readb(bits) | portp->portbit, bits);
2000 set_bit(ST_CMDING, &portp->state);
2001 EBRDDISABLE(brdp);
2002}
2003
2004static void stli_sendcmd(struct stlibrd *brdp, struct stliport *portp, unsigned long cmd, void *arg, int size, int copyback)
2005{
2006 unsigned long flags;
2007
2008 spin_lock_irqsave(&brd_lock, flags);
2009 __stli_sendcmd(brdp, portp, cmd, arg, size, copyback);
2010 spin_unlock_irqrestore(&brd_lock, flags);
2011}
2012
2013/*****************************************************************************/
2014
2015/*
2016 * Read data from shared memory. This assumes that the shared memory
2017 * is enabled and that interrupts are off. Basically we just empty out
2018 * the shared memory buffer into the tty buffer. Must be careful to
2019 * handle the case where we fill up the tty buffer, but still have
2020 * more chars to unload.
2021 */
2022
2023static void stli_read(struct stlibrd *brdp, struct stliport *portp)
2024{
2025 cdkasyrq_t __iomem *rp;
2026 char __iomem *shbuf;
2027 struct tty_struct *tty;
2028 unsigned int head, tail, size;
2029 unsigned int len, stlen;
2030
2031 if (test_bit(ST_RXSTOP, &portp->state))
2032 return;
2033 tty = tty_port_tty_get(&portp->port);
2034 if (tty == NULL)
2035 return;
2036
2037 rp = &((cdkasy_t __iomem *) EBRDGETMEMPTR(brdp, portp->addr))->rxq;
2038 head = (unsigned int) readw(&rp->head);
2039 if (head != ((unsigned int) readw(&rp->head)))
2040 head = (unsigned int) readw(&rp->head);
2041 tail = (unsigned int) readw(&rp->tail);
2042 size = portp->rxsize;
2043 if (head >= tail) {
2044 len = head - tail;
2045 stlen = len;
2046 } else {
2047 len = size - (tail - head);
2048 stlen = size - tail;
2049 }
2050
2051 len = tty_buffer_request_room(tty, len);
2052
2053 shbuf = (char __iomem *) EBRDGETMEMPTR(brdp, portp->rxoffset);
2054
2055 while (len > 0) {
2056 unsigned char *cptr;
2057
2058 stlen = min(len, stlen);
2059 tty_prepare_flip_string(tty, &cptr, stlen);
2060 memcpy_fromio(cptr, shbuf + tail, stlen);
2061 len -= stlen;
2062 tail += stlen;
2063 if (tail >= size) {
2064 tail = 0;
2065 stlen = head;
2066 }
2067 }
2068 rp = &((cdkasy_t __iomem *) EBRDGETMEMPTR(brdp, portp->addr))->rxq;
2069 writew(tail, &rp->tail);
2070
2071 if (head != tail)
2072 set_bit(ST_RXING, &portp->state);
2073
2074 tty_schedule_flip(tty);
2075 tty_kref_put(tty);
2076}
2077
2078/*****************************************************************************/
2079
2080/*
2081 * Set up and carry out any delayed commands. There is only a small set
2082 * of slave commands that can be done "off-level". So it is not too
2083 * difficult to deal with them here.
2084 */
2085
2086static void stli_dodelaycmd(struct stliport *portp, cdkctrl_t __iomem *cp)
2087{
2088 int cmd;
2089
2090 if (test_bit(ST_DOSIGS, &portp->state)) {
2091 if (test_bit(ST_DOFLUSHTX, &portp->state) &&
2092 test_bit(ST_DOFLUSHRX, &portp->state))
2093 cmd = A_SETSIGNALSF;
2094 else if (test_bit(ST_DOFLUSHTX, &portp->state))
2095 cmd = A_SETSIGNALSFTX;
2096 else if (test_bit(ST_DOFLUSHRX, &portp->state))
2097 cmd = A_SETSIGNALSFRX;
2098 else
2099 cmd = A_SETSIGNALS;
2100 clear_bit(ST_DOFLUSHTX, &portp->state);
2101 clear_bit(ST_DOFLUSHRX, &portp->state);
2102 clear_bit(ST_DOSIGS, &portp->state);
2103 memcpy_toio((void __iomem *) &(cp->args[0]), (void *) &portp->asig,
2104 sizeof(asysigs_t));
2105 writel(0, &cp->status);
2106 writel(cmd, &cp->cmd);
2107 set_bit(ST_CMDING, &portp->state);
2108 } else if (test_bit(ST_DOFLUSHTX, &portp->state) ||
2109 test_bit(ST_DOFLUSHRX, &portp->state)) {
2110 cmd = ((test_bit(ST_DOFLUSHTX, &portp->state)) ? FLUSHTX : 0);
2111 cmd |= ((test_bit(ST_DOFLUSHRX, &portp->state)) ? FLUSHRX : 0);
2112 clear_bit(ST_DOFLUSHTX, &portp->state);
2113 clear_bit(ST_DOFLUSHRX, &portp->state);
2114 memcpy_toio((void __iomem *) &(cp->args[0]), (void *) &cmd, sizeof(int));
2115 writel(0, &cp->status);
2116 writel(A_FLUSH, &cp->cmd);
2117 set_bit(ST_CMDING, &portp->state);
2118 }
2119}
2120
2121/*****************************************************************************/
2122
2123/*
2124 * Host command service checking. This handles commands or messages
2125 * coming from the slave to the host. Must have board shared memory
2126 * enabled and interrupts off when called. Notice that by servicing the
2127 * read data last we don't need to change the shared memory pointer
2128 * during processing (which is a slow IO operation).
2129 * Return value indicates if this port is still awaiting actions from
2130 * the slave (like open, command, or even TX data being sent). If 0
2131 * then port is still busy, otherwise no longer busy.
2132 */
2133
2134static int stli_hostcmd(struct stlibrd *brdp, struct stliport *portp)
2135{
2136 cdkasy_t __iomem *ap;
2137 cdkctrl_t __iomem *cp;
2138 struct tty_struct *tty;
2139 asynotify_t nt;
2140 unsigned long oldsigs;
2141 int rc, donerx;
2142
2143 ap = (cdkasy_t __iomem *) EBRDGETMEMPTR(brdp, portp->addr);
2144 cp = &ap->ctrl;
2145
2146/*
2147 * Check if we are waiting for an open completion message.
2148 */
2149 if (test_bit(ST_OPENING, &portp->state)) {
2150 rc = readl(&cp->openarg);
2151 if (readb(&cp->open) == 0 && rc != 0) {
2152 if (rc > 0)
2153 rc--;
2154 writel(0, &cp->openarg);
2155 portp->rc = rc;
2156 clear_bit(ST_OPENING, &portp->state);
2157 wake_up_interruptible(&portp->raw_wait);
2158 }
2159 }
2160
2161/*
2162 * Check if we are waiting for a close completion message.
2163 */
2164 if (test_bit(ST_CLOSING, &portp->state)) {
2165 rc = (int) readl(&cp->closearg);
2166 if (readb(&cp->close) == 0 && rc != 0) {
2167 if (rc > 0)
2168 rc--;
2169 writel(0, &cp->closearg);
2170 portp->rc = rc;
2171 clear_bit(ST_CLOSING, &portp->state);
2172 wake_up_interruptible(&portp->raw_wait);
2173 }
2174 }
2175
2176/*
2177 * Check if we are waiting for a command completion message. We may
2178 * need to copy out the command results associated with this command.
2179 */
2180 if (test_bit(ST_CMDING, &portp->state)) {
2181 rc = readl(&cp->status);
2182 if (readl(&cp->cmd) == 0 && rc != 0) {
2183 if (rc > 0)
2184 rc--;
2185 if (portp->argp != NULL) {
2186 memcpy_fromio(portp->argp, (void __iomem *) &(cp->args[0]),
2187 portp->argsize);
2188 portp->argp = NULL;
2189 }
2190 writel(0, &cp->status);
2191 portp->rc = rc;
2192 clear_bit(ST_CMDING, &portp->state);
2193 stli_dodelaycmd(portp, cp);
2194 wake_up_interruptible(&portp->raw_wait);
2195 }
2196 }
2197
2198/*
2199 * Check for any notification messages ready. This includes lots of
2200 * different types of events - RX chars ready, RX break received,
2201 * TX data low or empty in the slave, modem signals changed state.
2202 */
2203 donerx = 0;
2204
2205 if (ap->notify) {
2206 nt = ap->changed;
2207 ap->notify = 0;
2208 tty = tty_port_tty_get(&portp->port);
2209
2210 if (nt.signal & SG_DCD) {
2211 oldsigs = portp->sigs;
2212 portp->sigs = stli_mktiocm(nt.sigvalue);
2213 clear_bit(ST_GETSIGS, &portp->state);
2214 if ((portp->sigs & TIOCM_CD) &&
2215 ((oldsigs & TIOCM_CD) == 0))
2216 wake_up_interruptible(&portp->port.open_wait);
2217 if ((oldsigs & TIOCM_CD) &&
2218 ((portp->sigs & TIOCM_CD) == 0)) {
2219 if (portp->port.flags & ASYNC_CHECK_CD) {
2220 if (tty)
2221 tty_hangup(tty);
2222 }
2223 }
2224 }
2225
2226 if (nt.data & DT_TXEMPTY)
2227 clear_bit(ST_TXBUSY, &portp->state);
2228 if (nt.data & (DT_TXEMPTY | DT_TXLOW)) {
2229 if (tty != NULL) {
2230 tty_wakeup(tty);
2231 EBRDENABLE(brdp);
2232 }
2233 }
2234
2235 if ((nt.data & DT_RXBREAK) && (portp->rxmarkmsk & BRKINT)) {
2236 if (tty != NULL) {
2237 tty_insert_flip_char(tty, 0, TTY_BREAK);
2238 if (portp->port.flags & ASYNC_SAK) {
2239 do_SAK(tty);
2240 EBRDENABLE(brdp);
2241 }
2242 tty_schedule_flip(tty);
2243 }
2244 }
2245 tty_kref_put(tty);
2246
2247 if (nt.data & DT_RXBUSY) {
2248 donerx++;
2249 stli_read(brdp, portp);
2250 }
2251 }
2252
2253/*
2254 * It might seem odd that we are checking for more RX chars here.
2255 * But, we need to handle the case where the tty buffer was previously
2256 * filled, but we had more characters to pass up. The slave will not
2257 * send any more RX notify messages until the RX buffer has been emptied.
2258 * But it will leave the service bits on (since the buffer is not empty).
2259 * So from here we can try to process more RX chars.
2260 */
2261 if ((!donerx) && test_bit(ST_RXING, &portp->state)) {
2262 clear_bit(ST_RXING, &portp->state);
2263 stli_read(brdp, portp);
2264 }
2265
2266 return((test_bit(ST_OPENING, &portp->state) ||
2267 test_bit(ST_CLOSING, &portp->state) ||
2268 test_bit(ST_CMDING, &portp->state) ||
2269 test_bit(ST_TXBUSY, &portp->state) ||
2270 test_bit(ST_RXING, &portp->state)) ? 0 : 1);
2271}
2272
2273/*****************************************************************************/
2274
2275/*
2276 * Service all ports on a particular board. Assumes that the boards
2277 * shared memory is enabled, and that the page pointer is pointed
2278 * at the cdk header structure.
2279 */
2280
2281static void stli_brdpoll(struct stlibrd *brdp, cdkhdr_t __iomem *hdrp)
2282{
2283 struct stliport *portp;
2284 unsigned char hostbits[(STL_MAXCHANS / 8) + 1];
2285 unsigned char slavebits[(STL_MAXCHANS / 8) + 1];
2286 unsigned char __iomem *slavep;
2287 int bitpos, bitat, bitsize;
2288 int channr, nrdevs, slavebitchange;
2289
2290 bitsize = brdp->bitsize;
2291 nrdevs = brdp->nrdevs;
2292
2293/*
2294 * Check if slave wants any service. Basically we try to do as
2295 * little work as possible here. There are 2 levels of service
2296 * bits. So if there is nothing to do we bail early. We check
2297 * 8 service bits at a time in the inner loop, so we can bypass
2298 * the lot if none of them want service.
2299 */
2300 memcpy_fromio(&hostbits[0], (((unsigned char __iomem *) hdrp) + brdp->hostoffset),
2301 bitsize);
2302
2303 memset(&slavebits[0], 0, bitsize);
2304 slavebitchange = 0;
2305
2306 for (bitpos = 0; (bitpos < bitsize); bitpos++) {
2307 if (hostbits[bitpos] == 0)
2308 continue;
2309 channr = bitpos * 8;
2310 for (bitat = 0x1; (channr < nrdevs); channr++, bitat <<= 1) {
2311 if (hostbits[bitpos] & bitat) {
2312 portp = brdp->ports[(channr - 1)];
2313 if (stli_hostcmd(brdp, portp)) {
2314 slavebitchange++;
2315 slavebits[bitpos] |= bitat;
2316 }
2317 }
2318 }
2319 }
2320
2321/*
2322 * If any of the ports are no longer busy then update them in the
2323 * slave request bits. We need to do this after, since a host port
2324 * service may initiate more slave requests.
2325 */
2326 if (slavebitchange) {
2327 hdrp = (cdkhdr_t __iomem *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
2328 slavep = ((unsigned char __iomem *) hdrp) + brdp->slaveoffset;
2329 for (bitpos = 0; (bitpos < bitsize); bitpos++) {
2330 if (readb(slavebits + bitpos))
2331 writeb(readb(slavep + bitpos) & ~slavebits[bitpos], slavebits + bitpos);
2332 }
2333 }
2334}
2335
2336/*****************************************************************************/
2337
2338/*
2339 * Driver poll routine. This routine polls the boards in use and passes
2340 * messages back up to host when necessary. This is actually very
2341 * CPU efficient, since we will always have the kernel poll clock, it
2342 * adds only a few cycles when idle (since board service can be
2343 * determined very easily), but when loaded generates no interrupts
2344 * (with their expensive associated context change).
2345 */
2346
2347static void stli_poll(unsigned long arg)
2348{
2349 cdkhdr_t __iomem *hdrp;
2350 struct stlibrd *brdp;
2351 unsigned int brdnr;
2352
2353 mod_timer(&stli_timerlist, STLI_TIMEOUT);
2354
2355/*
2356 * Check each board and do any servicing required.
2357 */
2358 for (brdnr = 0; (brdnr < stli_nrbrds); brdnr++) {
2359 brdp = stli_brds[brdnr];
2360 if (brdp == NULL)
2361 continue;
2362 if (!test_bit(BST_STARTED, &brdp->state))
2363 continue;
2364
2365 spin_lock(&brd_lock);
2366 EBRDENABLE(brdp);
2367 hdrp = (cdkhdr_t __iomem *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
2368 if (readb(&hdrp->hostreq))
2369 stli_brdpoll(brdp, hdrp);
2370 EBRDDISABLE(brdp);
2371 spin_unlock(&brd_lock);
2372 }
2373}
2374
2375/*****************************************************************************/
2376
2377/*
2378 * Translate the termios settings into the port setting structure of
2379 * the slave.
2380 */
2381
2382static void stli_mkasyport(struct tty_struct *tty, struct stliport *portp,
2383 asyport_t *pp, struct ktermios *tiosp)
2384{
2385 memset(pp, 0, sizeof(asyport_t));
2386
2387/*
2388 * Start of by setting the baud, char size, parity and stop bit info.
2389 */
2390 pp->baudout = tty_get_baud_rate(tty);
2391 if ((tiosp->c_cflag & CBAUD) == B38400) {
2392 if ((portp->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
2393 pp->baudout = 57600;
2394 else if ((portp->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
2395 pp->baudout = 115200;
2396 else if ((portp->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
2397 pp->baudout = 230400;
2398 else if ((portp->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
2399 pp->baudout = 460800;
2400 else if ((portp->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST)
2401 pp->baudout = (portp->baud_base / portp->custom_divisor);
2402 }
2403 if (pp->baudout > STL_MAXBAUD)
2404 pp->baudout = STL_MAXBAUD;
2405 pp->baudin = pp->baudout;
2406
2407 switch (tiosp->c_cflag & CSIZE) {
2408 case CS5:
2409 pp->csize = 5;
2410 break;
2411 case CS6:
2412 pp->csize = 6;
2413 break;
2414 case CS7:
2415 pp->csize = 7;
2416 break;
2417 default:
2418 pp->csize = 8;
2419 break;
2420 }
2421
2422 if (tiosp->c_cflag & CSTOPB)
2423 pp->stopbs = PT_STOP2;
2424 else
2425 pp->stopbs = PT_STOP1;
2426
2427 if (tiosp->c_cflag & PARENB) {
2428 if (tiosp->c_cflag & PARODD)
2429 pp->parity = PT_ODDPARITY;
2430 else
2431 pp->parity = PT_EVENPARITY;
2432 } else {
2433 pp->parity = PT_NOPARITY;
2434 }
2435
2436/*
2437 * Set up any flow control options enabled.
2438 */
2439 if (tiosp->c_iflag & IXON) {
2440 pp->flow |= F_IXON;
2441 if (tiosp->c_iflag & IXANY)
2442 pp->flow |= F_IXANY;
2443 }
2444 if (tiosp->c_cflag & CRTSCTS)
2445 pp->flow |= (F_RTSFLOW | F_CTSFLOW);
2446
2447 pp->startin = tiosp->c_cc[VSTART];
2448 pp->stopin = tiosp->c_cc[VSTOP];
2449 pp->startout = tiosp->c_cc[VSTART];
2450 pp->stopout = tiosp->c_cc[VSTOP];
2451
2452/*
2453 * Set up the RX char marking mask with those RX error types we must
2454 * catch. We can get the slave to help us out a little here, it will
2455 * ignore parity errors and breaks for us, and mark parity errors in
2456 * the data stream.
2457 */
2458 if (tiosp->c_iflag & IGNPAR)
2459 pp->iflag |= FI_IGNRXERRS;
2460 if (tiosp->c_iflag & IGNBRK)
2461 pp->iflag |= FI_IGNBREAK;
2462
2463 portp->rxmarkmsk = 0;
2464 if (tiosp->c_iflag & (INPCK | PARMRK))
2465 pp->iflag |= FI_1MARKRXERRS;
2466 if (tiosp->c_iflag & BRKINT)
2467 portp->rxmarkmsk |= BRKINT;
2468
2469/*
2470 * Set up clocal processing as required.
2471 */
2472 if (tiosp->c_cflag & CLOCAL)
2473 portp->port.flags &= ~ASYNC_CHECK_CD;
2474 else
2475 portp->port.flags |= ASYNC_CHECK_CD;
2476
2477/*
2478 * Transfer any persistent flags into the asyport structure.
2479 */
2480 pp->pflag = (portp->pflag & 0xffff);
2481 pp->vmin = (portp->pflag & P_RXIMIN) ? 1 : 0;
2482 pp->vtime = (portp->pflag & P_RXITIME) ? 1 : 0;
2483 pp->cc[1] = (portp->pflag & P_RXTHOLD) ? 1 : 0;
2484}
2485
2486/*****************************************************************************/
2487
2488/*
2489 * Construct a slave signals structure for setting the DTR and RTS
2490 * signals as specified.
2491 */
2492
2493static void stli_mkasysigs(asysigs_t *sp, int dtr, int rts)
2494{
2495 memset(sp, 0, sizeof(asysigs_t));
2496 if (dtr >= 0) {
2497 sp->signal |= SG_DTR;
2498 sp->sigvalue |= ((dtr > 0) ? SG_DTR : 0);
2499 }
2500 if (rts >= 0) {
2501 sp->signal |= SG_RTS;
2502 sp->sigvalue |= ((rts > 0) ? SG_RTS : 0);
2503 }
2504}
2505
2506/*****************************************************************************/
2507
2508/*
2509 * Convert the signals returned from the slave into a local TIOCM type
2510 * signals value. We keep them locally in TIOCM format.
2511 */
2512
2513static long stli_mktiocm(unsigned long sigvalue)
2514{
2515 long tiocm = 0;
2516 tiocm |= ((sigvalue & SG_DCD) ? TIOCM_CD : 0);
2517 tiocm |= ((sigvalue & SG_CTS) ? TIOCM_CTS : 0);
2518 tiocm |= ((sigvalue & SG_RI) ? TIOCM_RI : 0);
2519 tiocm |= ((sigvalue & SG_DSR) ? TIOCM_DSR : 0);
2520 tiocm |= ((sigvalue & SG_DTR) ? TIOCM_DTR : 0);
2521 tiocm |= ((sigvalue & SG_RTS) ? TIOCM_RTS : 0);
2522 return(tiocm);
2523}
2524
2525/*****************************************************************************/
2526
2527/*
2528 * All panels and ports actually attached have been worked out. All
2529 * we need to do here is set up the appropriate per port data structures.
2530 */
2531
2532static int stli_initports(struct stlibrd *brdp)
2533{
2534 struct stliport *portp;
2535 unsigned int i, panelnr, panelport;
2536
2537 for (i = 0, panelnr = 0, panelport = 0; (i < brdp->nrports); i++) {
2538 portp = kzalloc(sizeof(struct stliport), GFP_KERNEL);
2539 if (!portp) {
2540 printk(KERN_WARNING "istallion: failed to allocate port structure\n");
2541 continue;
2542 }
2543 tty_port_init(&portp->port);
2544 portp->port.ops = &stli_port_ops;
2545 portp->magic = STLI_PORTMAGIC;
2546 portp->portnr = i;
2547 portp->brdnr = brdp->brdnr;
2548 portp->panelnr = panelnr;
2549 portp->baud_base = STL_BAUDBASE;
2550 portp->port.close_delay = STL_CLOSEDELAY;
2551 portp->closing_wait = 30 * HZ;
2552 init_waitqueue_head(&portp->port.open_wait);
2553 init_waitqueue_head(&portp->port.close_wait);
2554 init_waitqueue_head(&portp->raw_wait);
2555 panelport++;
2556 if (panelport >= brdp->panels[panelnr]) {
2557 panelport = 0;
2558 panelnr++;
2559 }
2560 brdp->ports[i] = portp;
2561 }
2562
2563 return 0;
2564}
2565
2566/*****************************************************************************/
2567
2568/*
2569 * All the following routines are board specific hardware operations.
2570 */
2571
2572static void stli_ecpinit(struct stlibrd *brdp)
2573{
2574 unsigned long memconf;
2575
2576 outb(ECP_ATSTOP, (brdp->iobase + ECP_ATCONFR));
2577 udelay(10);
2578 outb(ECP_ATDISABLE, (brdp->iobase + ECP_ATCONFR));
2579 udelay(100);
2580
2581 memconf = (brdp->memaddr & ECP_ATADDRMASK) >> ECP_ATADDRSHFT;
2582 outb(memconf, (brdp->iobase + ECP_ATMEMAR));
2583}
2584
2585/*****************************************************************************/
2586
2587static void stli_ecpenable(struct stlibrd *brdp)
2588{
2589 outb(ECP_ATENABLE, (brdp->iobase + ECP_ATCONFR));
2590}
2591
2592/*****************************************************************************/
2593
2594static void stli_ecpdisable(struct stlibrd *brdp)
2595{
2596 outb(ECP_ATDISABLE, (brdp->iobase + ECP_ATCONFR));
2597}
2598
2599/*****************************************************************************/
2600
2601static void __iomem *stli_ecpgetmemptr(struct stlibrd *brdp, unsigned long offset, int line)
2602{
2603 void __iomem *ptr;
2604 unsigned char val;
2605
2606 if (offset > brdp->memsize) {
2607 printk(KERN_ERR "istallion: shared memory pointer=%x out of "
2608 "range at line=%d(%d), brd=%d\n",
2609 (int) offset, line, __LINE__, brdp->brdnr);
2610 ptr = NULL;
2611 val = 0;
2612 } else {
2613 ptr = brdp->membase + (offset % ECP_ATPAGESIZE);
2614 val = (unsigned char) (offset / ECP_ATPAGESIZE);
2615 }
2616 outb(val, (brdp->iobase + ECP_ATMEMPR));
2617 return(ptr);
2618}
2619
2620/*****************************************************************************/
2621
2622static void stli_ecpreset(struct stlibrd *brdp)
2623{
2624 outb(ECP_ATSTOP, (brdp->iobase + ECP_ATCONFR));
2625 udelay(10);
2626 outb(ECP_ATDISABLE, (brdp->iobase + ECP_ATCONFR));
2627 udelay(500);
2628}
2629
2630/*****************************************************************************/
2631
2632static void stli_ecpintr(struct stlibrd *brdp)
2633{
2634 outb(0x1, brdp->iobase);
2635}
2636
2637/*****************************************************************************/
2638
2639/*
2640 * The following set of functions act on ECP EISA boards.
2641 */
2642
2643static void stli_ecpeiinit(struct stlibrd *brdp)
2644{
2645 unsigned long memconf;
2646
2647 outb(0x1, (brdp->iobase + ECP_EIBRDENAB));
2648 outb(ECP_EISTOP, (brdp->iobase + ECP_EICONFR));
2649 udelay(10);
2650 outb(ECP_EIDISABLE, (brdp->iobase + ECP_EICONFR));
2651 udelay(500);
2652
2653 memconf = (brdp->memaddr & ECP_EIADDRMASKL) >> ECP_EIADDRSHFTL;
2654 outb(memconf, (brdp->iobase + ECP_EIMEMARL));
2655 memconf = (brdp->memaddr & ECP_EIADDRMASKH) >> ECP_EIADDRSHFTH;
2656 outb(memconf, (brdp->iobase + ECP_EIMEMARH));
2657}
2658
2659/*****************************************************************************/
2660
2661static void stli_ecpeienable(struct stlibrd *brdp)
2662{
2663 outb(ECP_EIENABLE, (brdp->iobase + ECP_EICONFR));
2664}
2665
2666/*****************************************************************************/
2667
2668static void stli_ecpeidisable(struct stlibrd *brdp)
2669{
2670 outb(ECP_EIDISABLE, (brdp->iobase + ECP_EICONFR));
2671}
2672
2673/*****************************************************************************/
2674
2675static void __iomem *stli_ecpeigetmemptr(struct stlibrd *brdp, unsigned long offset, int line)
2676{
2677 void __iomem *ptr;
2678 unsigned char val;
2679
2680 if (offset > brdp->memsize) {
2681 printk(KERN_ERR "istallion: shared memory pointer=%x out of "
2682 "range at line=%d(%d), brd=%d\n",
2683 (int) offset, line, __LINE__, brdp->brdnr);
2684 ptr = NULL;
2685 val = 0;
2686 } else {
2687 ptr = brdp->membase + (offset % ECP_EIPAGESIZE);
2688 if (offset < ECP_EIPAGESIZE)
2689 val = ECP_EIENABLE;
2690 else
2691 val = ECP_EIENABLE | 0x40;
2692 }
2693 outb(val, (brdp->iobase + ECP_EICONFR));
2694 return(ptr);
2695}
2696
2697/*****************************************************************************/
2698
2699static void stli_ecpeireset(struct stlibrd *brdp)
2700{
2701 outb(ECP_EISTOP, (brdp->iobase + ECP_EICONFR));
2702 udelay(10);
2703 outb(ECP_EIDISABLE, (brdp->iobase + ECP_EICONFR));
2704 udelay(500);
2705}
2706
2707/*****************************************************************************/
2708
2709/*
2710 * The following set of functions act on ECP MCA boards.
2711 */
2712
2713static void stli_ecpmcenable(struct stlibrd *brdp)
2714{
2715 outb(ECP_MCENABLE, (brdp->iobase + ECP_MCCONFR));
2716}
2717
2718/*****************************************************************************/
2719
2720static void stli_ecpmcdisable(struct stlibrd *brdp)
2721{
2722 outb(ECP_MCDISABLE, (brdp->iobase + ECP_MCCONFR));
2723}
2724
2725/*****************************************************************************/
2726
2727static void __iomem *stli_ecpmcgetmemptr(struct stlibrd *brdp, unsigned long offset, int line)
2728{
2729 void __iomem *ptr;
2730 unsigned char val;
2731
2732 if (offset > brdp->memsize) {
2733 printk(KERN_ERR "istallion: shared memory pointer=%x out of "
2734 "range at line=%d(%d), brd=%d\n",
2735 (int) offset, line, __LINE__, brdp->brdnr);
2736 ptr = NULL;
2737 val = 0;
2738 } else {
2739 ptr = brdp->membase + (offset % ECP_MCPAGESIZE);
2740 val = ((unsigned char) (offset / ECP_MCPAGESIZE)) | ECP_MCENABLE;
2741 }
2742 outb(val, (brdp->iobase + ECP_MCCONFR));
2743 return(ptr);
2744}
2745
2746/*****************************************************************************/
2747
2748static void stli_ecpmcreset(struct stlibrd *brdp)
2749{
2750 outb(ECP_MCSTOP, (brdp->iobase + ECP_MCCONFR));
2751 udelay(10);
2752 outb(ECP_MCDISABLE, (brdp->iobase + ECP_MCCONFR));
2753 udelay(500);
2754}
2755
2756/*****************************************************************************/
2757
2758/*
2759 * The following set of functions act on ECP PCI boards.
2760 */
2761
2762static void stli_ecppciinit(struct stlibrd *brdp)
2763{
2764 outb(ECP_PCISTOP, (brdp->iobase + ECP_PCICONFR));
2765 udelay(10);
2766 outb(0, (brdp->iobase + ECP_PCICONFR));
2767 udelay(500);
2768}
2769
2770/*****************************************************************************/
2771
2772static void __iomem *stli_ecppcigetmemptr(struct stlibrd *brdp, unsigned long offset, int line)
2773{
2774 void __iomem *ptr;
2775 unsigned char val;
2776
2777 if (offset > brdp->memsize) {
2778 printk(KERN_ERR "istallion: shared memory pointer=%x out of "
2779 "range at line=%d(%d), board=%d\n",
2780 (int) offset, line, __LINE__, brdp->brdnr);
2781 ptr = NULL;
2782 val = 0;
2783 } else {
2784 ptr = brdp->membase + (offset % ECP_PCIPAGESIZE);
2785 val = (offset / ECP_PCIPAGESIZE) << 1;
2786 }
2787 outb(val, (brdp->iobase + ECP_PCICONFR));
2788 return(ptr);
2789}
2790
2791/*****************************************************************************/
2792
2793static void stli_ecppcireset(struct stlibrd *brdp)
2794{
2795 outb(ECP_PCISTOP, (brdp->iobase + ECP_PCICONFR));
2796 udelay(10);
2797 outb(0, (brdp->iobase + ECP_PCICONFR));
2798 udelay(500);
2799}
2800
2801/*****************************************************************************/
2802
2803/*
2804 * The following routines act on ONboards.
2805 */
2806
2807static void stli_onbinit(struct stlibrd *brdp)
2808{
2809 unsigned long memconf;
2810
2811 outb(ONB_ATSTOP, (brdp->iobase + ONB_ATCONFR));
2812 udelay(10);
2813 outb(ONB_ATDISABLE, (brdp->iobase + ONB_ATCONFR));
2814 mdelay(1000);
2815
2816 memconf = (brdp->memaddr & ONB_ATADDRMASK) >> ONB_ATADDRSHFT;
2817 outb(memconf, (brdp->iobase + ONB_ATMEMAR));
2818 outb(0x1, brdp->iobase);
2819 mdelay(1);
2820}
2821
2822/*****************************************************************************/
2823
2824static void stli_onbenable(struct stlibrd *brdp)
2825{
2826 outb((brdp->enabval | ONB_ATENABLE), (brdp->iobase + ONB_ATCONFR));
2827}
2828
2829/*****************************************************************************/
2830
2831static void stli_onbdisable(struct stlibrd *brdp)
2832{
2833 outb((brdp->enabval | ONB_ATDISABLE), (brdp->iobase + ONB_ATCONFR));
2834}
2835
2836/*****************************************************************************/
2837
2838static void __iomem *stli_onbgetmemptr(struct stlibrd *brdp, unsigned long offset, int line)
2839{
2840 void __iomem *ptr;
2841
2842 if (offset > brdp->memsize) {
2843 printk(KERN_ERR "istallion: shared memory pointer=%x out of "
2844 "range at line=%d(%d), brd=%d\n",
2845 (int) offset, line, __LINE__, brdp->brdnr);
2846 ptr = NULL;
2847 } else {
2848 ptr = brdp->membase + (offset % ONB_ATPAGESIZE);
2849 }
2850 return(ptr);
2851}
2852
2853/*****************************************************************************/
2854
2855static void stli_onbreset(struct stlibrd *brdp)
2856{
2857 outb(ONB_ATSTOP, (brdp->iobase + ONB_ATCONFR));
2858 udelay(10);
2859 outb(ONB_ATDISABLE, (brdp->iobase + ONB_ATCONFR));
2860 mdelay(1000);
2861}
2862
2863/*****************************************************************************/
2864
2865/*
2866 * The following routines act on ONboard EISA.
2867 */
2868
2869static void stli_onbeinit(struct stlibrd *brdp)
2870{
2871 unsigned long memconf;
2872
2873 outb(0x1, (brdp->iobase + ONB_EIBRDENAB));
2874 outb(ONB_EISTOP, (brdp->iobase + ONB_EICONFR));
2875 udelay(10);
2876 outb(ONB_EIDISABLE, (brdp->iobase + ONB_EICONFR));
2877 mdelay(1000);
2878
2879 memconf = (brdp->memaddr & ONB_EIADDRMASKL) >> ONB_EIADDRSHFTL;
2880 outb(memconf, (brdp->iobase + ONB_EIMEMARL));
2881 memconf = (brdp->memaddr & ONB_EIADDRMASKH) >> ONB_EIADDRSHFTH;
2882 outb(memconf, (brdp->iobase + ONB_EIMEMARH));
2883 outb(0x1, brdp->iobase);
2884 mdelay(1);
2885}
2886
2887/*****************************************************************************/
2888
2889static void stli_onbeenable(struct stlibrd *brdp)
2890{
2891 outb(ONB_EIENABLE, (brdp->iobase + ONB_EICONFR));
2892}
2893
2894/*****************************************************************************/
2895
2896static void stli_onbedisable(struct stlibrd *brdp)
2897{
2898 outb(ONB_EIDISABLE, (brdp->iobase + ONB_EICONFR));
2899}
2900
2901/*****************************************************************************/
2902
2903static void __iomem *stli_onbegetmemptr(struct stlibrd *brdp, unsigned long offset, int line)
2904{
2905 void __iomem *ptr;
2906 unsigned char val;
2907
2908 if (offset > brdp->memsize) {
2909 printk(KERN_ERR "istallion: shared memory pointer=%x out of "
2910 "range at line=%d(%d), brd=%d\n",
2911 (int) offset, line, __LINE__, brdp->brdnr);
2912 ptr = NULL;
2913 val = 0;
2914 } else {
2915 ptr = brdp->membase + (offset % ONB_EIPAGESIZE);
2916 if (offset < ONB_EIPAGESIZE)
2917 val = ONB_EIENABLE;
2918 else
2919 val = ONB_EIENABLE | 0x40;
2920 }
2921 outb(val, (brdp->iobase + ONB_EICONFR));
2922 return(ptr);
2923}
2924
2925/*****************************************************************************/
2926
2927static void stli_onbereset(struct stlibrd *brdp)
2928{
2929 outb(ONB_EISTOP, (brdp->iobase + ONB_EICONFR));
2930 udelay(10);
2931 outb(ONB_EIDISABLE, (brdp->iobase + ONB_EICONFR));
2932 mdelay(1000);
2933}
2934
2935/*****************************************************************************/
2936
2937/*
2938 * The following routines act on Brumby boards.
2939 */
2940
2941static void stli_bbyinit(struct stlibrd *brdp)
2942{
2943 outb(BBY_ATSTOP, (brdp->iobase + BBY_ATCONFR));
2944 udelay(10);
2945 outb(0, (brdp->iobase + BBY_ATCONFR));
2946 mdelay(1000);
2947 outb(0x1, brdp->iobase);
2948 mdelay(1);
2949}
2950
2951/*****************************************************************************/
2952
2953static void __iomem *stli_bbygetmemptr(struct stlibrd *brdp, unsigned long offset, int line)
2954{
2955 void __iomem *ptr;
2956 unsigned char val;
2957
2958 BUG_ON(offset > brdp->memsize);
2959
2960 ptr = brdp->membase + (offset % BBY_PAGESIZE);
2961 val = (unsigned char) (offset / BBY_PAGESIZE);
2962 outb(val, (brdp->iobase + BBY_ATCONFR));
2963 return(ptr);
2964}
2965
2966/*****************************************************************************/
2967
2968static void stli_bbyreset(struct stlibrd *brdp)
2969{
2970 outb(BBY_ATSTOP, (brdp->iobase + BBY_ATCONFR));
2971 udelay(10);
2972 outb(0, (brdp->iobase + BBY_ATCONFR));
2973 mdelay(1000);
2974}
2975
2976/*****************************************************************************/
2977
2978/*
2979 * The following routines act on original old Stallion boards.
2980 */
2981
2982static void stli_stalinit(struct stlibrd *brdp)
2983{
2984 outb(0x1, brdp->iobase);
2985 mdelay(1000);
2986}
2987
2988/*****************************************************************************/
2989
2990static void __iomem *stli_stalgetmemptr(struct stlibrd *brdp, unsigned long offset, int line)
2991{
2992 BUG_ON(offset > brdp->memsize);
2993 return brdp->membase + (offset % STAL_PAGESIZE);
2994}
2995
2996/*****************************************************************************/
2997
2998static void stli_stalreset(struct stlibrd *brdp)
2999{
3000 u32 __iomem *vecp;
3001
3002 vecp = (u32 __iomem *) (brdp->membase + 0x30);
3003 writel(0xffff0000, vecp);
3004 outb(0, brdp->iobase);
3005 mdelay(1000);
3006}
3007
3008/*****************************************************************************/
3009
3010/*
3011 * Try to find an ECP board and initialize it. This handles only ECP
3012 * board types.
3013 */
3014
3015static int stli_initecp(struct stlibrd *brdp)
3016{
3017 cdkecpsig_t sig;
3018 cdkecpsig_t __iomem *sigsp;
3019 unsigned int status, nxtid;
3020 char *name;
3021 int retval, panelnr, nrports;
3022
3023 if ((brdp->iobase == 0) || (brdp->memaddr == 0)) {
3024 retval = -ENODEV;
3025 goto err;
3026 }
3027
3028 brdp->iosize = ECP_IOSIZE;
3029
3030 if (!request_region(brdp->iobase, brdp->iosize, "istallion")) {
3031 retval = -EIO;
3032 goto err;
3033 }
3034
3035/*
3036 * Based on the specific board type setup the common vars to access
3037 * and enable shared memory. Set all board specific information now
3038 * as well.
3039 */
3040 switch (brdp->brdtype) {
3041 case BRD_ECP:
3042 brdp->memsize = ECP_MEMSIZE;
3043 brdp->pagesize = ECP_ATPAGESIZE;
3044 brdp->init = stli_ecpinit;
3045 brdp->enable = stli_ecpenable;
3046 brdp->reenable = stli_ecpenable;
3047 brdp->disable = stli_ecpdisable;
3048 brdp->getmemptr = stli_ecpgetmemptr;
3049 brdp->intr = stli_ecpintr;
3050 brdp->reset = stli_ecpreset;
3051 name = "serial(EC8/64)";
3052 break;
3053
3054 case BRD_ECPE:
3055 brdp->memsize = ECP_MEMSIZE;
3056 brdp->pagesize = ECP_EIPAGESIZE;
3057 brdp->init = stli_ecpeiinit;
3058 brdp->enable = stli_ecpeienable;
3059 brdp->reenable = stli_ecpeienable;
3060 brdp->disable = stli_ecpeidisable;
3061 brdp->getmemptr = stli_ecpeigetmemptr;
3062 brdp->intr = stli_ecpintr;
3063 brdp->reset = stli_ecpeireset;
3064 name = "serial(EC8/64-EI)";
3065 break;
3066
3067 case BRD_ECPMC:
3068 brdp->memsize = ECP_MEMSIZE;
3069 brdp->pagesize = ECP_MCPAGESIZE;
3070 brdp->init = NULL;
3071 brdp->enable = stli_ecpmcenable;
3072 brdp->reenable = stli_ecpmcenable;
3073 brdp->disable = stli_ecpmcdisable;
3074 brdp->getmemptr = stli_ecpmcgetmemptr;
3075 brdp->intr = stli_ecpintr;
3076 brdp->reset = stli_ecpmcreset;
3077 name = "serial(EC8/64-MCA)";
3078 break;
3079
3080 case BRD_ECPPCI:
3081 brdp->memsize = ECP_PCIMEMSIZE;
3082 brdp->pagesize = ECP_PCIPAGESIZE;
3083 brdp->init = stli_ecppciinit;
3084 brdp->enable = NULL;
3085 brdp->reenable = NULL;
3086 brdp->disable = NULL;
3087 brdp->getmemptr = stli_ecppcigetmemptr;
3088 brdp->intr = stli_ecpintr;
3089 brdp->reset = stli_ecppcireset;
3090 name = "serial(EC/RA-PCI)";
3091 break;
3092
3093 default:
3094 retval = -EINVAL;
3095 goto err_reg;
3096 }
3097
3098/*
3099 * The per-board operations structure is all set up, so now let's go
3100 * and get the board operational. Firstly initialize board configuration
3101 * registers. Set the memory mapping info so we can get at the boards
3102 * shared memory.
3103 */
3104 EBRDINIT(brdp);
3105
3106 brdp->membase = ioremap_nocache(brdp->memaddr, brdp->memsize);
3107 if (brdp->membase == NULL) {
3108 retval = -ENOMEM;
3109 goto err_reg;
3110 }
3111
3112/*
3113 * Now that all specific code is set up, enable the shared memory and
3114 * look for the a signature area that will tell us exactly what board
3115 * this is, and what it is connected to it.
3116 */
3117 EBRDENABLE(brdp);
3118 sigsp = (cdkecpsig_t __iomem *) EBRDGETMEMPTR(brdp, CDK_SIGADDR);
3119 memcpy_fromio(&sig, sigsp, sizeof(cdkecpsig_t));
3120 EBRDDISABLE(brdp);
3121
3122 if (sig.magic != cpu_to_le32(ECP_MAGIC)) {
3123 retval = -ENODEV;
3124 goto err_unmap;
3125 }
3126
3127/*
3128 * Scan through the signature looking at the panels connected to the
3129 * board. Calculate the total number of ports as we go.
3130 */
3131 for (panelnr = 0, nxtid = 0; (panelnr < STL_MAXPANELS); panelnr++) {
3132 status = sig.panelid[nxtid];
3133 if ((status & ECH_PNLIDMASK) != nxtid)
3134 break;
3135
3136 brdp->panelids[panelnr] = status;
3137 nrports = (status & ECH_PNL16PORT) ? 16 : 8;
3138 if ((nrports == 16) && ((status & ECH_PNLXPID) == 0))
3139 nxtid++;
3140 brdp->panels[panelnr] = nrports;
3141 brdp->nrports += nrports;
3142 nxtid++;
3143 brdp->nrpanels++;
3144 }
3145
3146
3147 set_bit(BST_FOUND, &brdp->state);
3148 return 0;
3149err_unmap:
3150 iounmap(brdp->membase);
3151 brdp->membase = NULL;
3152err_reg:
3153 release_region(brdp->iobase, brdp->iosize);
3154err:
3155 return retval;
3156}
3157
3158/*****************************************************************************/
3159
3160/*
3161 * Try to find an ONboard, Brumby or Stallion board and initialize it.
3162 * This handles only these board types.
3163 */
3164
3165static int stli_initonb(struct stlibrd *brdp)
3166{
3167 cdkonbsig_t sig;
3168 cdkonbsig_t __iomem *sigsp;
3169 char *name;
3170 int i, retval;
3171
3172/*
3173 * Do a basic sanity check on the IO and memory addresses.
3174 */
3175 if (brdp->iobase == 0 || brdp->memaddr == 0) {
3176 retval = -ENODEV;
3177 goto err;
3178 }
3179
3180 brdp->iosize = ONB_IOSIZE;
3181
3182 if (!request_region(brdp->iobase, brdp->iosize, "istallion")) {
3183 retval = -EIO;
3184 goto err;
3185 }
3186
3187/*
3188 * Based on the specific board type setup the common vars to access
3189 * and enable shared memory. Set all board specific information now
3190 * as well.
3191 */
3192 switch (brdp->brdtype) {
3193 case BRD_ONBOARD:
3194 case BRD_ONBOARD2:
3195 brdp->memsize = ONB_MEMSIZE;
3196 brdp->pagesize = ONB_ATPAGESIZE;
3197 brdp->init = stli_onbinit;
3198 brdp->enable = stli_onbenable;
3199 brdp->reenable = stli_onbenable;
3200 brdp->disable = stli_onbdisable;
3201 brdp->getmemptr = stli_onbgetmemptr;
3202 brdp->intr = stli_ecpintr;
3203 brdp->reset = stli_onbreset;
3204 if (brdp->memaddr > 0x100000)
3205 brdp->enabval = ONB_MEMENABHI;
3206 else
3207 brdp->enabval = ONB_MEMENABLO;
3208 name = "serial(ONBoard)";
3209 break;
3210
3211 case BRD_ONBOARDE:
3212 brdp->memsize = ONB_EIMEMSIZE;
3213 brdp->pagesize = ONB_EIPAGESIZE;
3214 brdp->init = stli_onbeinit;
3215 brdp->enable = stli_onbeenable;
3216 brdp->reenable = stli_onbeenable;
3217 brdp->disable = stli_onbedisable;
3218 brdp->getmemptr = stli_onbegetmemptr;
3219 brdp->intr = stli_ecpintr;
3220 brdp->reset = stli_onbereset;
3221 name = "serial(ONBoard/E)";
3222 break;
3223
3224 case BRD_BRUMBY4:
3225 brdp->memsize = BBY_MEMSIZE;
3226 brdp->pagesize = BBY_PAGESIZE;
3227 brdp->init = stli_bbyinit;
3228 brdp->enable = NULL;
3229 brdp->reenable = NULL;
3230 brdp->disable = NULL;
3231 brdp->getmemptr = stli_bbygetmemptr;
3232 brdp->intr = stli_ecpintr;
3233 brdp->reset = stli_bbyreset;
3234 name = "serial(Brumby)";
3235 break;
3236
3237 case BRD_STALLION:
3238 brdp->memsize = STAL_MEMSIZE;
3239 brdp->pagesize = STAL_PAGESIZE;
3240 brdp->init = stli_stalinit;
3241 brdp->enable = NULL;
3242 brdp->reenable = NULL;
3243 brdp->disable = NULL;
3244 brdp->getmemptr = stli_stalgetmemptr;
3245 brdp->intr = stli_ecpintr;
3246 brdp->reset = stli_stalreset;
3247 name = "serial(Stallion)";
3248 break;
3249
3250 default:
3251 retval = -EINVAL;
3252 goto err_reg;
3253 }
3254
3255/*
3256 * The per-board operations structure is all set up, so now let's go
3257 * and get the board operational. Firstly initialize board configuration
3258 * registers. Set the memory mapping info so we can get at the boards
3259 * shared memory.
3260 */
3261 EBRDINIT(brdp);
3262
3263 brdp->membase = ioremap_nocache(brdp->memaddr, brdp->memsize);
3264 if (brdp->membase == NULL) {
3265 retval = -ENOMEM;
3266 goto err_reg;
3267 }
3268
3269/*
3270 * Now that all specific code is set up, enable the shared memory and
3271 * look for the a signature area that will tell us exactly what board
3272 * this is, and how many ports.
3273 */
3274 EBRDENABLE(brdp);
3275 sigsp = (cdkonbsig_t __iomem *) EBRDGETMEMPTR(brdp, CDK_SIGADDR);
3276 memcpy_fromio(&sig, sigsp, sizeof(cdkonbsig_t));
3277 EBRDDISABLE(brdp);
3278
3279 if (sig.magic0 != cpu_to_le16(ONB_MAGIC0) ||
3280 sig.magic1 != cpu_to_le16(ONB_MAGIC1) ||
3281 sig.magic2 != cpu_to_le16(ONB_MAGIC2) ||
3282 sig.magic3 != cpu_to_le16(ONB_MAGIC3)) {
3283 retval = -ENODEV;
3284 goto err_unmap;
3285 }
3286
3287/*
3288 * Scan through the signature alive mask and calculate how many ports
3289 * there are on this board.
3290 */
3291 brdp->nrpanels = 1;
3292 if (sig.amask1) {
3293 brdp->nrports = 32;
3294 } else {
3295 for (i = 0; (i < 16); i++) {
3296 if (((sig.amask0 << i) & 0x8000) == 0)
3297 break;
3298 }
3299 brdp->nrports = i;
3300 }
3301 brdp->panels[0] = brdp->nrports;
3302
3303
3304 set_bit(BST_FOUND, &brdp->state);
3305 return 0;
3306err_unmap:
3307 iounmap(brdp->membase);
3308 brdp->membase = NULL;
3309err_reg:
3310 release_region(brdp->iobase, brdp->iosize);
3311err:
3312 return retval;
3313}
3314
3315/*****************************************************************************/
3316
3317/*
3318 * Start up a running board. This routine is only called after the
3319 * code has been down loaded to the board and is operational. It will
3320 * read in the memory map, and get the show on the road...
3321 */
3322
3323static int stli_startbrd(struct stlibrd *brdp)
3324{
3325 cdkhdr_t __iomem *hdrp;
3326 cdkmem_t __iomem *memp;
3327 cdkasy_t __iomem *ap;
3328 unsigned long flags;
3329 unsigned int portnr, nrdevs, i;
3330 struct stliport *portp;
3331 int rc = 0;
3332 u32 memoff;
3333
3334 spin_lock_irqsave(&brd_lock, flags);
3335 EBRDENABLE(brdp);
3336 hdrp = (cdkhdr_t __iomem *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
3337 nrdevs = hdrp->nrdevs;
3338
3339#if 0
3340 printk("%s(%d): CDK version %d.%d.%d --> "
3341 "nrdevs=%d memp=%x hostp=%x slavep=%x\n",
3342 __FILE__, __LINE__, readb(&hdrp->ver_release), readb(&hdrp->ver_modification),
3343 readb(&hdrp->ver_fix), nrdevs, (int) readl(&hdrp->memp), readl(&hdrp->hostp),
3344 readl(&hdrp->slavep));
3345#endif
3346
3347 if (nrdevs < (brdp->nrports + 1)) {
3348 printk(KERN_ERR "istallion: slave failed to allocate memory for "
3349 "all devices, devices=%d\n", nrdevs);
3350 brdp->nrports = nrdevs - 1;
3351 }
3352 brdp->nrdevs = nrdevs;
3353 brdp->hostoffset = hdrp->hostp - CDK_CDKADDR;
3354 brdp->slaveoffset = hdrp->slavep - CDK_CDKADDR;
3355 brdp->bitsize = (nrdevs + 7) / 8;
3356 memoff = readl(&hdrp->memp);
3357 if (memoff > brdp->memsize) {
3358 printk(KERN_ERR "istallion: corrupted shared memory region?\n");
3359 rc = -EIO;
3360 goto stli_donestartup;
3361 }
3362 memp = (cdkmem_t __iomem *) EBRDGETMEMPTR(brdp, memoff);
3363 if (readw(&memp->dtype) != TYP_ASYNCTRL) {
3364 printk(KERN_ERR "istallion: no slave control device found\n");
3365 goto stli_donestartup;
3366 }
3367 memp++;
3368
3369/*
3370 * Cycle through memory allocation of each port. We are guaranteed to
3371 * have all ports inside the first page of slave window, so no need to
3372 * change pages while reading memory map.
3373 */
3374 for (i = 1, portnr = 0; (i < nrdevs); i++, portnr++, memp++) {
3375 if (readw(&memp->dtype) != TYP_ASYNC)
3376 break;
3377 portp = brdp->ports[portnr];
3378 if (portp == NULL)
3379 break;
3380 portp->devnr = i;
3381 portp->addr = readl(&memp->offset);
3382 portp->reqbit = (unsigned char) (0x1 << (i * 8 / nrdevs));
3383 portp->portidx = (unsigned char) (i / 8);
3384 portp->portbit = (unsigned char) (0x1 << (i % 8));
3385 }
3386
3387 writeb(0xff, &hdrp->slavereq);
3388
3389/*
3390 * For each port setup a local copy of the RX and TX buffer offsets
3391 * and sizes. We do this separate from the above, because we need to
3392 * move the shared memory page...
3393 */
3394 for (i = 1, portnr = 0; (i < nrdevs); i++, portnr++) {
3395 portp = brdp->ports[portnr];
3396 if (portp == NULL)
3397 break;
3398 if (portp->addr == 0)
3399 break;
3400 ap = (cdkasy_t __iomem *) EBRDGETMEMPTR(brdp, portp->addr);
3401 if (ap != NULL) {
3402 portp->rxsize = readw(&ap->rxq.size);
3403 portp->txsize = readw(&ap->txq.size);
3404 portp->rxoffset = readl(&ap->rxq.offset);
3405 portp->txoffset = readl(&ap->txq.offset);
3406 }
3407 }
3408
3409stli_donestartup:
3410 EBRDDISABLE(brdp);
3411 spin_unlock_irqrestore(&brd_lock, flags);
3412
3413 if (rc == 0)
3414 set_bit(BST_STARTED, &brdp->state);
3415
3416 if (! stli_timeron) {
3417 stli_timeron++;
3418 mod_timer(&stli_timerlist, STLI_TIMEOUT);
3419 }
3420
3421 return rc;
3422}
3423
3424/*****************************************************************************/
3425
3426/*
3427 * Probe and initialize the specified board.
3428 */
3429
3430static int __devinit stli_brdinit(struct stlibrd *brdp)
3431{
3432 int retval;
3433
3434 switch (brdp->brdtype) {
3435 case BRD_ECP:
3436 case BRD_ECPE:
3437 case BRD_ECPMC:
3438 case BRD_ECPPCI:
3439 retval = stli_initecp(brdp);
3440 break;
3441 case BRD_ONBOARD:
3442 case BRD_ONBOARDE:
3443 case BRD_ONBOARD2:
3444 case BRD_BRUMBY4:
3445 case BRD_STALLION:
3446 retval = stli_initonb(brdp);
3447 break;
3448 default:
3449 printk(KERN_ERR "istallion: board=%d is unknown board "
3450 "type=%d\n", brdp->brdnr, brdp->brdtype);
3451 retval = -ENODEV;
3452 }
3453
3454 if (retval)
3455 return retval;
3456
3457 stli_initports(brdp);
3458 printk(KERN_INFO "istallion: %s found, board=%d io=%x mem=%x "
3459 "nrpanels=%d nrports=%d\n", stli_brdnames[brdp->brdtype],
3460 brdp->brdnr, brdp->iobase, (int) brdp->memaddr,
3461 brdp->nrpanels, brdp->nrports);
3462 return 0;
3463}
3464
3465#if STLI_EISAPROBE != 0
3466/*****************************************************************************/
3467
3468/*
3469 * Probe around trying to find where the EISA boards shared memory
3470 * might be. This is a bit if hack, but it is the best we can do.
3471 */
3472
3473static int stli_eisamemprobe(struct stlibrd *brdp)
3474{
3475 cdkecpsig_t ecpsig, __iomem *ecpsigp;
3476 cdkonbsig_t onbsig, __iomem *onbsigp;
3477 int i, foundit;
3478
3479/*
3480 * First up we reset the board, to get it into a known state. There
3481 * is only 2 board types here we need to worry about. Don;t use the
3482 * standard board init routine here, it programs up the shared
3483 * memory address, and we don't know it yet...
3484 */
3485 if (brdp->brdtype == BRD_ECPE) {
3486 outb(0x1, (brdp->iobase + ECP_EIBRDENAB));
3487 outb(ECP_EISTOP, (brdp->iobase + ECP_EICONFR));
3488 udelay(10);
3489 outb(ECP_EIDISABLE, (brdp->iobase + ECP_EICONFR));
3490 udelay(500);
3491 stli_ecpeienable(brdp);
3492 } else if (brdp->brdtype == BRD_ONBOARDE) {
3493 outb(0x1, (brdp->iobase + ONB_EIBRDENAB));
3494 outb(ONB_EISTOP, (brdp->iobase + ONB_EICONFR));
3495 udelay(10);
3496 outb(ONB_EIDISABLE, (brdp->iobase + ONB_EICONFR));
3497 mdelay(100);
3498 outb(0x1, brdp->iobase);
3499 mdelay(1);
3500 stli_onbeenable(brdp);
3501 } else {
3502 return -ENODEV;
3503 }
3504
3505 foundit = 0;
3506 brdp->memsize = ECP_MEMSIZE;
3507
3508/*
3509 * Board shared memory is enabled, so now we have a poke around and
3510 * see if we can find it.
3511 */
3512 for (i = 0; (i < stli_eisamempsize); i++) {
3513 brdp->memaddr = stli_eisamemprobeaddrs[i];
3514 brdp->membase = ioremap_nocache(brdp->memaddr, brdp->memsize);
3515 if (brdp->membase == NULL)
3516 continue;
3517
3518 if (brdp->brdtype == BRD_ECPE) {
3519 ecpsigp = stli_ecpeigetmemptr(brdp,
3520 CDK_SIGADDR, __LINE__);
3521 memcpy_fromio(&ecpsig, ecpsigp, sizeof(cdkecpsig_t));
3522 if (ecpsig.magic == cpu_to_le32(ECP_MAGIC))
3523 foundit = 1;
3524 } else {
3525 onbsigp = (cdkonbsig_t __iomem *) stli_onbegetmemptr(brdp,
3526 CDK_SIGADDR, __LINE__);
3527 memcpy_fromio(&onbsig, onbsigp, sizeof(cdkonbsig_t));
3528 if ((onbsig.magic0 == cpu_to_le16(ONB_MAGIC0)) &&
3529 (onbsig.magic1 == cpu_to_le16(ONB_MAGIC1)) &&
3530 (onbsig.magic2 == cpu_to_le16(ONB_MAGIC2)) &&
3531 (onbsig.magic3 == cpu_to_le16(ONB_MAGIC3)))
3532 foundit = 1;
3533 }
3534
3535 iounmap(brdp->membase);
3536 if (foundit)
3537 break;
3538 }
3539
3540/*
3541 * Regardless of whether we found the shared memory or not we must
3542 * disable the region. After that return success or failure.
3543 */
3544 if (brdp->brdtype == BRD_ECPE)
3545 stli_ecpeidisable(brdp);
3546 else
3547 stli_onbedisable(brdp);
3548
3549 if (! foundit) {
3550 brdp->memaddr = 0;
3551 brdp->membase = NULL;
3552 printk(KERN_ERR "istallion: failed to probe shared memory "
3553 "region for %s in EISA slot=%d\n",
3554 stli_brdnames[brdp->brdtype], (brdp->iobase >> 12));
3555 return -ENODEV;
3556 }
3557 return 0;
3558}
3559#endif
3560
3561static int stli_getbrdnr(void)
3562{
3563 unsigned int i;
3564
3565 for (i = 0; i < STL_MAXBRDS; i++) {
3566 if (!stli_brds[i]) {
3567 if (i >= stli_nrbrds)
3568 stli_nrbrds = i + 1;
3569 return i;
3570 }
3571 }
3572 return -1;
3573}
3574
3575#if STLI_EISAPROBE != 0
3576/*****************************************************************************/
3577
3578/*
3579 * Probe around and try to find any EISA boards in system. The biggest
3580 * problem here is finding out what memory address is associated with
3581 * an EISA board after it is found. The registers of the ECPE and
3582 * ONboardE are not readable - so we can't read them from there. We
3583 * don't have access to the EISA CMOS (or EISA BIOS) so we don't
3584 * actually have any way to find out the real value. The best we can
3585 * do is go probing around in the usual places hoping we can find it.
3586 */
3587
3588static int __init stli_findeisabrds(void)
3589{
3590 struct stlibrd *brdp;
3591 unsigned int iobase, eid, i;
3592 int brdnr, found = 0;
3593
3594/*
3595 * Firstly check if this is an EISA system. If this is not an EISA system then
3596 * don't bother going any further!
3597 */
3598 if (EISA_bus)
3599 return 0;
3600
3601/*
3602 * Looks like an EISA system, so go searching for EISA boards.
3603 */
3604 for (iobase = 0x1000; (iobase <= 0xc000); iobase += 0x1000) {
3605 outb(0xff, (iobase + 0xc80));
3606 eid = inb(iobase + 0xc80);
3607 eid |= inb(iobase + 0xc81) << 8;
3608 if (eid != STL_EISAID)
3609 continue;
3610
3611/*
3612 * We have found a board. Need to check if this board was
3613 * statically configured already (just in case!).
3614 */
3615 for (i = 0; (i < STL_MAXBRDS); i++) {
3616 brdp = stli_brds[i];
3617 if (brdp == NULL)
3618 continue;
3619 if (brdp->iobase == iobase)
3620 break;
3621 }
3622 if (i < STL_MAXBRDS)
3623 continue;
3624
3625/*
3626 * We have found a Stallion board and it is not configured already.
3627 * Allocate a board structure and initialize it.
3628 */
3629 if ((brdp = stli_allocbrd()) == NULL)
3630 return found ? : -ENOMEM;
3631 brdnr = stli_getbrdnr();
3632 if (brdnr < 0)
3633 return found ? : -ENOMEM;
3634 brdp->brdnr = (unsigned int)brdnr;
3635 eid = inb(iobase + 0xc82);
3636 if (eid == ECP_EISAID)
3637 brdp->brdtype = BRD_ECPE;
3638 else if (eid == ONB_EISAID)
3639 brdp->brdtype = BRD_ONBOARDE;
3640 else
3641 brdp->brdtype = BRD_UNKNOWN;
3642 brdp->iobase = iobase;
3643 outb(0x1, (iobase + 0xc84));
3644 if (stli_eisamemprobe(brdp))
3645 outb(0, (iobase + 0xc84));
3646 if (stli_brdinit(brdp) < 0) {
3647 kfree(brdp);
3648 continue;
3649 }
3650
3651 stli_brds[brdp->brdnr] = brdp;
3652 found++;
3653
3654 for (i = 0; i < brdp->nrports; i++)
3655 tty_register_device(stli_serial,
3656 brdp->brdnr * STL_MAXPORTS + i, NULL);
3657 }
3658
3659 return found;
3660}
3661#else
3662static inline int stli_findeisabrds(void) { return 0; }
3663#endif
3664
3665/*****************************************************************************/
3666
3667/*
3668 * Find the next available board number that is free.
3669 */
3670
3671/*****************************************************************************/
3672
3673/*
3674 * We have a Stallion board. Allocate a board structure and
3675 * initialize it. Read its IO and MEMORY resources from PCI
3676 * configuration space.
3677 */
3678
3679static int __devinit stli_pciprobe(struct pci_dev *pdev,
3680 const struct pci_device_id *ent)
3681{
3682 struct stlibrd *brdp;
3683 unsigned int i;
3684 int brdnr, retval = -EIO;
3685
3686 retval = pci_enable_device(pdev);
3687 if (retval)
3688 goto err;
3689 brdp = stli_allocbrd();
3690 if (brdp == NULL) {
3691 retval = -ENOMEM;
3692 goto err;
3693 }
3694 mutex_lock(&stli_brdslock);
3695 brdnr = stli_getbrdnr();
3696 if (brdnr < 0) {
3697 printk(KERN_INFO "istallion: too many boards found, "
3698 "maximum supported %d\n", STL_MAXBRDS);
3699 mutex_unlock(&stli_brdslock);
3700 retval = -EIO;
3701 goto err_fr;
3702 }
3703 brdp->brdnr = (unsigned int)brdnr;
3704 stli_brds[brdp->brdnr] = brdp;
3705 mutex_unlock(&stli_brdslock);
3706 brdp->brdtype = BRD_ECPPCI;
3707/*
3708 * We have all resources from the board, so lets setup the actual
3709 * board structure now.
3710 */
3711 brdp->iobase = pci_resource_start(pdev, 3);
3712 brdp->memaddr = pci_resource_start(pdev, 2);
3713 retval = stli_brdinit(brdp);
3714 if (retval)
3715 goto err_null;
3716
3717 set_bit(BST_PROBED, &brdp->state);
3718 pci_set_drvdata(pdev, brdp);
3719
3720 EBRDENABLE(brdp);
3721 brdp->enable = NULL;
3722 brdp->disable = NULL;
3723
3724 for (i = 0; i < brdp->nrports; i++)
3725 tty_register_device(stli_serial, brdp->brdnr * STL_MAXPORTS + i,
3726 &pdev->dev);
3727
3728 return 0;
3729err_null:
3730 stli_brds[brdp->brdnr] = NULL;
3731err_fr:
3732 kfree(brdp);
3733err:
3734 return retval;
3735}
3736
3737static void __devexit stli_pciremove(struct pci_dev *pdev)
3738{
3739 struct stlibrd *brdp = pci_get_drvdata(pdev);
3740
3741 stli_cleanup_ports(brdp);
3742
3743 iounmap(brdp->membase);
3744 if (brdp->iosize > 0)
3745 release_region(brdp->iobase, brdp->iosize);
3746
3747 stli_brds[brdp->brdnr] = NULL;
3748 kfree(brdp);
3749}
3750
3751static struct pci_driver stli_pcidriver = {
3752 .name = "istallion",
3753 .id_table = istallion_pci_tbl,
3754 .probe = stli_pciprobe,
3755 .remove = __devexit_p(stli_pciremove)
3756};
3757/*****************************************************************************/
3758
3759/*
3760 * Allocate a new board structure. Fill out the basic info in it.
3761 */
3762
3763static struct stlibrd *stli_allocbrd(void)
3764{
3765 struct stlibrd *brdp;
3766
3767 brdp = kzalloc(sizeof(struct stlibrd), GFP_KERNEL);
3768 if (!brdp) {
3769 printk(KERN_ERR "istallion: failed to allocate memory "
3770 "(size=%Zd)\n", sizeof(struct stlibrd));
3771 return NULL;
3772 }
3773 brdp->magic = STLI_BOARDMAGIC;
3774 return brdp;
3775}
3776
3777/*****************************************************************************/
3778
3779/*
3780 * Scan through all the boards in the configuration and see what we
3781 * can find.
3782 */
3783
3784static int __init stli_initbrds(void)
3785{
3786 struct stlibrd *brdp, *nxtbrdp;
3787 struct stlconf conf;
3788 unsigned int i, j, found = 0;
3789 int retval;
3790
3791 for (stli_nrbrds = 0; stli_nrbrds < ARRAY_SIZE(stli_brdsp);
3792 stli_nrbrds++) {
3793 memset(&conf, 0, sizeof(conf));
3794 if (stli_parsebrd(&conf, stli_brdsp[stli_nrbrds]) == 0)
3795 continue;
3796 if ((brdp = stli_allocbrd()) == NULL)
3797 continue;
3798 brdp->brdnr = stli_nrbrds;
3799 brdp->brdtype = conf.brdtype;
3800 brdp->iobase = conf.ioaddr1;
3801 brdp->memaddr = conf.memaddr;
3802 if (stli_brdinit(brdp) < 0) {
3803 kfree(brdp);
3804 continue;
3805 }
3806 stli_brds[brdp->brdnr] = brdp;
3807 found++;
3808
3809 for (i = 0; i < brdp->nrports; i++)
3810 tty_register_device(stli_serial,
3811 brdp->brdnr * STL_MAXPORTS + i, NULL);
3812 }
3813
3814 retval = stli_findeisabrds();
3815 if (retval > 0)
3816 found += retval;
3817
3818/*
3819 * All found boards are initialized. Now for a little optimization, if
3820 * no boards are sharing the "shared memory" regions then we can just
3821 * leave them all enabled. This is in fact the usual case.
3822 */
3823 stli_shared = 0;
3824 if (stli_nrbrds > 1) {
3825 for (i = 0; (i < stli_nrbrds); i++) {
3826 brdp = stli_brds[i];
3827 if (brdp == NULL)
3828 continue;
3829 for (j = i + 1; (j < stli_nrbrds); j++) {
3830 nxtbrdp = stli_brds[j];
3831 if (nxtbrdp == NULL)
3832 continue;
3833 if ((brdp->membase >= nxtbrdp->membase) &&
3834 (brdp->membase <= (nxtbrdp->membase +
3835 nxtbrdp->memsize - 1))) {
3836 stli_shared++;
3837 break;
3838 }
3839 }
3840 }
3841 }
3842
3843 if (stli_shared == 0) {
3844 for (i = 0; (i < stli_nrbrds); i++) {
3845 brdp = stli_brds[i];
3846 if (brdp == NULL)
3847 continue;
3848 if (test_bit(BST_FOUND, &brdp->state)) {
3849 EBRDENABLE(brdp);
3850 brdp->enable = NULL;
3851 brdp->disable = NULL;
3852 }
3853 }
3854 }
3855
3856 retval = pci_register_driver(&stli_pcidriver);
3857 if (retval && found == 0) {
3858 printk(KERN_ERR "Neither isa nor eisa cards found nor pci "
3859 "driver can be registered!\n");
3860 goto err;
3861 }
3862
3863 return 0;
3864err:
3865 return retval;
3866}
3867
3868/*****************************************************************************/
3869
3870/*
3871 * Code to handle an "staliomem" read operation. This device is the
3872 * contents of the board shared memory. It is used for down loading
3873 * the slave image (and debugging :-)
3874 */
3875
3876static ssize_t stli_memread(struct file *fp, char __user *buf, size_t count, loff_t *offp)
3877{
3878 unsigned long flags;
3879 void __iomem *memptr;
3880 struct stlibrd *brdp;
3881 unsigned int brdnr;
3882 int size, n;
3883 void *p;
3884 loff_t off = *offp;
3885
3886 brdnr = iminor(fp->f_path.dentry->d_inode);
3887 if (brdnr >= stli_nrbrds)
3888 return -ENODEV;
3889 brdp = stli_brds[brdnr];
3890 if (brdp == NULL)
3891 return -ENODEV;
3892 if (brdp->state == 0)
3893 return -ENODEV;
3894 if (off >= brdp->memsize || off + count < off)
3895 return 0;
3896
3897 size = min(count, (size_t)(brdp->memsize - off));
3898
3899 /*
3900 * Copy the data a page at a time
3901 */
3902
3903 p = (void *)__get_free_page(GFP_KERNEL);
3904 if(p == NULL)
3905 return -ENOMEM;
3906
3907 while (size > 0) {
3908 spin_lock_irqsave(&brd_lock, flags);
3909 EBRDENABLE(brdp);
3910 memptr = EBRDGETMEMPTR(brdp, off);
3911 n = min(size, (int)(brdp->pagesize - (((unsigned long) off) % brdp->pagesize)));
3912 n = min(n, (int)PAGE_SIZE);
3913 memcpy_fromio(p, memptr, n);
3914 EBRDDISABLE(brdp);
3915 spin_unlock_irqrestore(&brd_lock, flags);
3916 if (copy_to_user(buf, p, n)) {
3917 count = -EFAULT;
3918 goto out;
3919 }
3920 off += n;
3921 buf += n;
3922 size -= n;
3923 }
3924out:
3925 *offp = off;
3926 free_page((unsigned long)p);
3927 return count;
3928}
3929
3930/*****************************************************************************/
3931
3932/*
3933 * Code to handle an "staliomem" write operation. This device is the
3934 * contents of the board shared memory. It is used for down loading
3935 * the slave image (and debugging :-)
3936 *
3937 * FIXME: copy under lock
3938 */
3939
3940static ssize_t stli_memwrite(struct file *fp, const char __user *buf, size_t count, loff_t *offp)
3941{
3942 unsigned long flags;
3943 void __iomem *memptr;
3944 struct stlibrd *brdp;
3945 char __user *chbuf;
3946 unsigned int brdnr;
3947 int size, n;
3948 void *p;
3949 loff_t off = *offp;
3950
3951 brdnr = iminor(fp->f_path.dentry->d_inode);
3952
3953 if (brdnr >= stli_nrbrds)
3954 return -ENODEV;
3955 brdp = stli_brds[brdnr];
3956 if (brdp == NULL)
3957 return -ENODEV;
3958 if (brdp->state == 0)
3959 return -ENODEV;
3960 if (off >= brdp->memsize || off + count < off)
3961 return 0;
3962
3963 chbuf = (char __user *) buf;
3964 size = min(count, (size_t)(brdp->memsize - off));
3965
3966 /*
3967 * Copy the data a page at a time
3968 */
3969
3970 p = (void *)__get_free_page(GFP_KERNEL);
3971 if(p == NULL)
3972 return -ENOMEM;
3973
3974 while (size > 0) {
3975 n = min(size, (int)(brdp->pagesize - (((unsigned long) off) % brdp->pagesize)));
3976 n = min(n, (int)PAGE_SIZE);
3977 if (copy_from_user(p, chbuf, n)) {
3978 if (count == 0)
3979 count = -EFAULT;
3980 goto out;
3981 }
3982 spin_lock_irqsave(&brd_lock, flags);
3983 EBRDENABLE(brdp);
3984 memptr = EBRDGETMEMPTR(brdp, off);
3985 memcpy_toio(memptr, p, n);
3986 EBRDDISABLE(brdp);
3987 spin_unlock_irqrestore(&brd_lock, flags);
3988 off += n;
3989 chbuf += n;
3990 size -= n;
3991 }
3992out:
3993 free_page((unsigned long) p);
3994 *offp = off;
3995 return count;
3996}
3997
3998/*****************************************************************************/
3999
4000/*
4001 * Return the board stats structure to user app.
4002 */
4003
4004static int stli_getbrdstats(combrd_t __user *bp)
4005{
4006 struct stlibrd *brdp;
4007 unsigned int i;
4008
4009 if (copy_from_user(&stli_brdstats, bp, sizeof(combrd_t)))
4010 return -EFAULT;
4011 if (stli_brdstats.brd >= STL_MAXBRDS)
4012 return -ENODEV;
4013 brdp = stli_brds[stli_brdstats.brd];
4014 if (brdp == NULL)
4015 return -ENODEV;
4016
4017 memset(&stli_brdstats, 0, sizeof(combrd_t));
4018
4019 stli_brdstats.brd = brdp->brdnr;
4020 stli_brdstats.type = brdp->brdtype;
4021 stli_brdstats.hwid = 0;
4022 stli_brdstats.state = brdp->state;
4023 stli_brdstats.ioaddr = brdp->iobase;
4024 stli_brdstats.memaddr = brdp->memaddr;
4025 stli_brdstats.nrpanels = brdp->nrpanels;
4026 stli_brdstats.nrports = brdp->nrports;
4027 for (i = 0; (i < brdp->nrpanels); i++) {
4028 stli_brdstats.panels[i].panel = i;
4029 stli_brdstats.panels[i].hwid = brdp->panelids[i];
4030 stli_brdstats.panels[i].nrports = brdp->panels[i];
4031 }
4032
4033 if (copy_to_user(bp, &stli_brdstats, sizeof(combrd_t)))
4034 return -EFAULT;
4035 return 0;
4036}
4037
4038/*****************************************************************************/
4039
4040/*
4041 * Resolve the referenced port number into a port struct pointer.
4042 */
4043
4044static struct stliport *stli_getport(unsigned int brdnr, unsigned int panelnr,
4045 unsigned int portnr)
4046{
4047 struct stlibrd *brdp;
4048 unsigned int i;
4049
4050 if (brdnr >= STL_MAXBRDS)
4051 return NULL;
4052 brdp = stli_brds[brdnr];
4053 if (brdp == NULL)
4054 return NULL;
4055 for (i = 0; (i < panelnr); i++)
4056 portnr += brdp->panels[i];
4057 if (portnr >= brdp->nrports)
4058 return NULL;
4059 return brdp->ports[portnr];
4060}
4061
4062/*****************************************************************************/
4063
4064/*
4065 * Return the port stats structure to user app. A NULL port struct
4066 * pointer passed in means that we need to find out from the app
4067 * what port to get stats for (used through board control device).
4068 */
4069
4070static int stli_portcmdstats(struct tty_struct *tty, struct stliport *portp)
4071{
4072 unsigned long flags;
4073 struct stlibrd *brdp;
4074 int rc;
4075
4076 memset(&stli_comstats, 0, sizeof(comstats_t));
4077
4078 if (portp == NULL)
4079 return -ENODEV;
4080 brdp = stli_brds[portp->brdnr];
4081 if (brdp == NULL)
4082 return -ENODEV;
4083
4084 mutex_lock(&portp->port.mutex);
4085 if (test_bit(BST_STARTED, &brdp->state)) {
4086 if ((rc = stli_cmdwait(brdp, portp, A_GETSTATS,
4087 &stli_cdkstats, sizeof(asystats_t), 1)) < 0) {
4088 mutex_unlock(&portp->port.mutex);
4089 return rc;
4090 }
4091 } else {
4092 memset(&stli_cdkstats, 0, sizeof(asystats_t));
4093 }
4094
4095 stli_comstats.brd = portp->brdnr;
4096 stli_comstats.panel = portp->panelnr;
4097 stli_comstats.port = portp->portnr;
4098 stli_comstats.state = portp->state;
4099 stli_comstats.flags = portp->port.flags;
4100
4101 spin_lock_irqsave(&brd_lock, flags);
4102 if (tty != NULL) {
4103 if (portp->port.tty == tty) {
4104 stli_comstats.ttystate = tty->flags;
4105 stli_comstats.rxbuffered = -1;
4106 if (tty->termios != NULL) {
4107 stli_comstats.cflags = tty->termios->c_cflag;
4108 stli_comstats.iflags = tty->termios->c_iflag;
4109 stli_comstats.oflags = tty->termios->c_oflag;
4110 stli_comstats.lflags = tty->termios->c_lflag;
4111 }
4112 }
4113 }
4114 spin_unlock_irqrestore(&brd_lock, flags);
4115
4116 stli_comstats.txtotal = stli_cdkstats.txchars;
4117 stli_comstats.rxtotal = stli_cdkstats.rxchars + stli_cdkstats.ringover;
4118 stli_comstats.txbuffered = stli_cdkstats.txringq;
4119 stli_comstats.rxbuffered += stli_cdkstats.rxringq;
4120 stli_comstats.rxoverrun = stli_cdkstats.overruns;
4121 stli_comstats.rxparity = stli_cdkstats.parity;
4122 stli_comstats.rxframing = stli_cdkstats.framing;
4123 stli_comstats.rxlost = stli_cdkstats.ringover;
4124 stli_comstats.rxbreaks = stli_cdkstats.rxbreaks;
4125 stli_comstats.txbreaks = stli_cdkstats.txbreaks;
4126 stli_comstats.txxon = stli_cdkstats.txstart;
4127 stli_comstats.txxoff = stli_cdkstats.txstop;
4128 stli_comstats.rxxon = stli_cdkstats.rxstart;
4129 stli_comstats.rxxoff = stli_cdkstats.rxstop;
4130 stli_comstats.rxrtsoff = stli_cdkstats.rtscnt / 2;
4131 stli_comstats.rxrtson = stli_cdkstats.rtscnt - stli_comstats.rxrtsoff;
4132 stli_comstats.modem = stli_cdkstats.dcdcnt;
4133 stli_comstats.hwid = stli_cdkstats.hwid;
4134 stli_comstats.signals = stli_mktiocm(stli_cdkstats.signals);
4135 mutex_unlock(&portp->port.mutex);
4136
4137 return 0;
4138}
4139
4140/*****************************************************************************/
4141
4142/*
4143 * Return the port stats structure to user app. A NULL port struct
4144 * pointer passed in means that we need to find out from the app
4145 * what port to get stats for (used through board control device).
4146 */
4147
4148static int stli_getportstats(struct tty_struct *tty, struct stliport *portp,
4149 comstats_t __user *cp)
4150{
4151 struct stlibrd *brdp;
4152 int rc;
4153
4154 if (!portp) {
4155 if (copy_from_user(&stli_comstats, cp, sizeof(comstats_t)))
4156 return -EFAULT;
4157 portp = stli_getport(stli_comstats.brd, stli_comstats.panel,
4158 stli_comstats.port);
4159 if (!portp)
4160 return -ENODEV;
4161 }
4162
4163 brdp = stli_brds[portp->brdnr];
4164 if (!brdp)
4165 return -ENODEV;
4166
4167 if ((rc = stli_portcmdstats(tty, portp)) < 0)
4168 return rc;
4169
4170 return copy_to_user(cp, &stli_comstats, sizeof(comstats_t)) ?
4171 -EFAULT : 0;
4172}
4173
4174/*****************************************************************************/
4175
4176/*
4177 * Clear the port stats structure. We also return it zeroed out...
4178 */
4179
4180static int stli_clrportstats(struct stliport *portp, comstats_t __user *cp)
4181{
4182 struct stlibrd *brdp;
4183 int rc;
4184
4185 if (!portp) {
4186 if (copy_from_user(&stli_comstats, cp, sizeof(comstats_t)))
4187 return -EFAULT;
4188 portp = stli_getport(stli_comstats.brd, stli_comstats.panel,
4189 stli_comstats.port);
4190 if (!portp)
4191 return -ENODEV;
4192 }
4193
4194 brdp = stli_brds[portp->brdnr];
4195 if (!brdp)
4196 return -ENODEV;
4197
4198 mutex_lock(&portp->port.mutex);
4199
4200 if (test_bit(BST_STARTED, &brdp->state)) {
4201 if ((rc = stli_cmdwait(brdp, portp, A_CLEARSTATS, NULL, 0, 0)) < 0) {
4202 mutex_unlock(&portp->port.mutex);
4203 return rc;
4204 }
4205 }
4206
4207 memset(&stli_comstats, 0, sizeof(comstats_t));
4208 stli_comstats.brd = portp->brdnr;
4209 stli_comstats.panel = portp->panelnr;
4210 stli_comstats.port = portp->portnr;
4211 mutex_unlock(&portp->port.mutex);
4212
4213 if (copy_to_user(cp, &stli_comstats, sizeof(comstats_t)))
4214 return -EFAULT;
4215 return 0;
4216}
4217
4218/*****************************************************************************/
4219
4220/*
4221 * Return the entire driver ports structure to a user app.
4222 */
4223
4224static int stli_getportstruct(struct stliport __user *arg)
4225{
4226 struct stliport stli_dummyport;
4227 struct stliport *portp;
4228
4229 if (copy_from_user(&stli_dummyport, arg, sizeof(struct stliport)))
4230 return -EFAULT;
4231 portp = stli_getport(stli_dummyport.brdnr, stli_dummyport.panelnr,
4232 stli_dummyport.portnr);
4233 if (!portp)
4234 return -ENODEV;
4235 if (copy_to_user(arg, portp, sizeof(struct stliport)))
4236 return -EFAULT;
4237 return 0;
4238}
4239
4240/*****************************************************************************/
4241
4242/*
4243 * Return the entire driver board structure to a user app.
4244 */
4245
4246static int stli_getbrdstruct(struct stlibrd __user *arg)
4247{
4248 struct stlibrd stli_dummybrd;
4249 struct stlibrd *brdp;
4250
4251 if (copy_from_user(&stli_dummybrd, arg, sizeof(struct stlibrd)))
4252 return -EFAULT;
4253 if (stli_dummybrd.brdnr >= STL_MAXBRDS)
4254 return -ENODEV;
4255 brdp = stli_brds[stli_dummybrd.brdnr];
4256 if (!brdp)
4257 return -ENODEV;
4258 if (copy_to_user(arg, brdp, sizeof(struct stlibrd)))
4259 return -EFAULT;
4260 return 0;
4261}
4262
4263/*****************************************************************************/
4264
4265/*
4266 * The "staliomem" device is also required to do some special operations on
4267 * the board. We need to be able to send an interrupt to the board,
4268 * reset it, and start/stop it.
4269 */
4270
4271static long stli_memioctl(struct file *fp, unsigned int cmd, unsigned long arg)
4272{
4273 struct stlibrd *brdp;
4274 int brdnr, rc, done;
4275 void __user *argp = (void __user *)arg;
4276
4277/*
4278 * First up handle the board independent ioctls.
4279 */
4280 done = 0;
4281 rc = 0;
4282
4283 switch (cmd) {
4284 case COM_GETPORTSTATS:
4285 rc = stli_getportstats(NULL, NULL, argp);
4286 done++;
4287 break;
4288 case COM_CLRPORTSTATS:
4289 rc = stli_clrportstats(NULL, argp);
4290 done++;
4291 break;
4292 case COM_GETBRDSTATS:
4293 rc = stli_getbrdstats(argp);
4294 done++;
4295 break;
4296 case COM_READPORT:
4297 rc = stli_getportstruct(argp);
4298 done++;
4299 break;
4300 case COM_READBOARD:
4301 rc = stli_getbrdstruct(argp);
4302 done++;
4303 break;
4304 }
4305 if (done)
4306 return rc;
4307
4308/*
4309 * Now handle the board specific ioctls. These all depend on the
4310 * minor number of the device they were called from.
4311 */
4312 brdnr = iminor(fp->f_dentry->d_inode);
4313 if (brdnr >= STL_MAXBRDS)
4314 return -ENODEV;
4315 brdp = stli_brds[brdnr];
4316 if (!brdp)
4317 return -ENODEV;
4318 if (brdp->state == 0)
4319 return -ENODEV;
4320
4321 switch (cmd) {
4322 case STL_BINTR:
4323 EBRDINTR(brdp);
4324 break;
4325 case STL_BSTART:
4326 rc = stli_startbrd(brdp);
4327 break;
4328 case STL_BSTOP:
4329 clear_bit(BST_STARTED, &brdp->state);
4330 break;
4331 case STL_BRESET:
4332 clear_bit(BST_STARTED, &brdp->state);
4333 EBRDRESET(brdp);
4334 if (stli_shared == 0) {
4335 if (brdp->reenable != NULL)
4336 (* brdp->reenable)(brdp);
4337 }
4338 break;
4339 default:
4340 rc = -ENOIOCTLCMD;
4341 break;
4342 }
4343 return rc;
4344}
4345
4346static const struct tty_operations stli_ops = {
4347 .open = stli_open,
4348 .close = stli_close,
4349 .write = stli_write,
4350 .put_char = stli_putchar,
4351 .flush_chars = stli_flushchars,
4352 .write_room = stli_writeroom,
4353 .chars_in_buffer = stli_charsinbuffer,
4354 .ioctl = stli_ioctl,
4355 .set_termios = stli_settermios,
4356 .throttle = stli_throttle,
4357 .unthrottle = stli_unthrottle,
4358 .stop = stli_stop,
4359 .start = stli_start,
4360 .hangup = stli_hangup,
4361 .flush_buffer = stli_flushbuffer,
4362 .break_ctl = stli_breakctl,
4363 .wait_until_sent = stli_waituntilsent,
4364 .send_xchar = stli_sendxchar,
4365 .tiocmget = stli_tiocmget,
4366 .tiocmset = stli_tiocmset,
4367 .proc_fops = &stli_proc_fops,
4368};
4369
4370static const struct tty_port_operations stli_port_ops = {
4371 .carrier_raised = stli_carrier_raised,
4372 .dtr_rts = stli_dtr_rts,
4373 .activate = stli_activate,
4374 .shutdown = stli_shutdown,
4375};
4376
4377/*****************************************************************************/
4378/*
4379 * Loadable module initialization stuff.
4380 */
4381
4382static void istallion_cleanup_isa(void)
4383{
4384 struct stlibrd *brdp;
4385 unsigned int j;
4386
4387 for (j = 0; (j < stli_nrbrds); j++) {
4388 if ((brdp = stli_brds[j]) == NULL ||
4389 test_bit(BST_PROBED, &brdp->state))
4390 continue;
4391
4392 stli_cleanup_ports(brdp);
4393
4394 iounmap(brdp->membase);
4395 if (brdp->iosize > 0)
4396 release_region(brdp->iobase, brdp->iosize);
4397 kfree(brdp);
4398 stli_brds[j] = NULL;
4399 }
4400}
4401
4402static int __init istallion_module_init(void)
4403{
4404 unsigned int i;
4405 int retval;
4406
4407 printk(KERN_INFO "%s: version %s\n", stli_drvtitle, stli_drvversion);
4408
4409 spin_lock_init(&stli_lock);
4410 spin_lock_init(&brd_lock);
4411
4412 stli_txcookbuf = kmalloc(STLI_TXBUFSIZE, GFP_KERNEL);
4413 if (!stli_txcookbuf) {
4414 printk(KERN_ERR "istallion: failed to allocate memory "
4415 "(size=%d)\n", STLI_TXBUFSIZE);
4416 retval = -ENOMEM;
4417 goto err;
4418 }
4419
4420 stli_serial = alloc_tty_driver(STL_MAXBRDS * STL_MAXPORTS);
4421 if (!stli_serial) {
4422 retval = -ENOMEM;
4423 goto err_free;
4424 }
4425
4426 stli_serial->owner = THIS_MODULE;
4427 stli_serial->driver_name = stli_drvname;
4428 stli_serial->name = stli_serialname;
4429 stli_serial->major = STL_SERIALMAJOR;
4430 stli_serial->minor_start = 0;
4431 stli_serial->type = TTY_DRIVER_TYPE_SERIAL;
4432 stli_serial->subtype = SERIAL_TYPE_NORMAL;
4433 stli_serial->init_termios = stli_deftermios;
4434 stli_serial->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
4435 tty_set_operations(stli_serial, &stli_ops);
4436
4437 retval = tty_register_driver(stli_serial);
4438 if (retval) {
4439 printk(KERN_ERR "istallion: failed to register serial driver\n");
4440 goto err_ttyput;
4441 }
4442
4443 retval = stli_initbrds();
4444 if (retval)
4445 goto err_ttyunr;
4446
4447/*
4448 * Set up a character driver for the shared memory region. We need this
4449 * to down load the slave code image. Also it is a useful debugging tool.
4450 */
4451 retval = register_chrdev(STL_SIOMEMMAJOR, "staliomem", &stli_fsiomem);
4452 if (retval) {
4453 printk(KERN_ERR "istallion: failed to register serial memory "
4454 "device\n");
4455 goto err_deinit;
4456 }
4457
4458 istallion_class = class_create(THIS_MODULE, "staliomem");
4459 for (i = 0; i < 4; i++)
4460 device_create(istallion_class, NULL, MKDEV(STL_SIOMEMMAJOR, i),
4461 NULL, "staliomem%d", i);
4462
4463 return 0;
4464err_deinit:
4465 pci_unregister_driver(&stli_pcidriver);
4466 istallion_cleanup_isa();
4467err_ttyunr:
4468 tty_unregister_driver(stli_serial);
4469err_ttyput:
4470 put_tty_driver(stli_serial);
4471err_free:
4472 kfree(stli_txcookbuf);
4473err:
4474 return retval;
4475}
4476
4477/*****************************************************************************/
4478
4479static void __exit istallion_module_exit(void)
4480{
4481 unsigned int j;
4482
4483 printk(KERN_INFO "Unloading %s: version %s\n", stli_drvtitle,
4484 stli_drvversion);
4485
4486 if (stli_timeron) {
4487 stli_timeron = 0;
4488 del_timer_sync(&stli_timerlist);
4489 }
4490
4491 unregister_chrdev(STL_SIOMEMMAJOR, "staliomem");
4492
4493 for (j = 0; j < 4; j++)
4494 device_destroy(istallion_class, MKDEV(STL_SIOMEMMAJOR, j));
4495 class_destroy(istallion_class);
4496
4497 pci_unregister_driver(&stli_pcidriver);
4498 istallion_cleanup_isa();
4499
4500 tty_unregister_driver(stli_serial);
4501 put_tty_driver(stli_serial);
4502
4503 kfree(stli_txcookbuf);
4504}
4505
4506module_init(istallion_module_init);
4507module_exit(istallion_module_exit);
diff --git a/drivers/char/keyboard.c b/drivers/char/keyboard.c
deleted file mode 100644
index a7ca75212bfe..000000000000
--- a/drivers/char/keyboard.c
+++ /dev/null
@@ -1,1437 +0,0 @@
1/*
2 * linux/drivers/char/keyboard.c
3 *
4 * Written for linux by Johan Myreen as a translation from
5 * the assembly version by Linus (with diacriticals added)
6 *
7 * Some additional features added by Christoph Niemann (ChN), March 1993
8 *
9 * Loadable keymaps by Risto Kankkunen, May 1993
10 *
11 * Diacriticals redone & other small changes, aeb@cwi.nl, June 1993
12 * Added decr/incr_console, dynamic keymaps, Unicode support,
13 * dynamic function/string keys, led setting, Sept 1994
14 * `Sticky' modifier keys, 951006.
15 *
16 * 11-11-96: SAK should now work in the raw mode (Martin Mares)
17 *
18 * Modified to provide 'generic' keyboard support by Hamish Macdonald
19 * Merge with the m68k keyboard driver and split-off of the PC low-level
20 * parts by Geert Uytterhoeven, May 1997
21 *
22 * 27-05-97: Added support for the Magic SysRq Key (Martin Mares)
23 * 30-07-98: Dead keys redone, aeb@cwi.nl.
24 * 21-08-02: Converted to input API, major cleanup. (Vojtech Pavlik)
25 */
26
27#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
28
29#include <linux/consolemap.h>
30#include <linux/module.h>
31#include <linux/sched.h>
32#include <linux/tty.h>
33#include <linux/tty_flip.h>
34#include <linux/mm.h>
35#include <linux/string.h>
36#include <linux/init.h>
37#include <linux/slab.h>
38#include <linux/irq.h>
39
40#include <linux/kbd_kern.h>
41#include <linux/kbd_diacr.h>
42#include <linux/vt_kern.h>
43#include <linux/input.h>
44#include <linux/reboot.h>
45#include <linux/notifier.h>
46#include <linux/jiffies.h>
47
48extern void ctrl_alt_del(void);
49
50/*
51 * Exported functions/variables
52 */
53
54#define KBD_DEFMODE ((1 << VC_REPEAT) | (1 << VC_META))
55
56/*
57 * Some laptops take the 789uiojklm,. keys as number pad when NumLock is on.
58 * This seems a good reason to start with NumLock off. On HIL keyboards
59 * of PARISC machines however there is no NumLock key and everyone expects the keypad
60 * to be used for numbers.
61 */
62
63#if defined(CONFIG_PARISC) && (defined(CONFIG_KEYBOARD_HIL) || defined(CONFIG_KEYBOARD_HIL_OLD))
64#define KBD_DEFLEDS (1 << VC_NUMLOCK)
65#else
66#define KBD_DEFLEDS 0
67#endif
68
69#define KBD_DEFLOCK 0
70
71void compute_shiftstate(void);
72
73/*
74 * Handler Tables.
75 */
76
77#define K_HANDLERS\
78 k_self, k_fn, k_spec, k_pad,\
79 k_dead, k_cons, k_cur, k_shift,\
80 k_meta, k_ascii, k_lock, k_lowercase,\
81 k_slock, k_dead2, k_brl, k_ignore
82
83typedef void (k_handler_fn)(struct vc_data *vc, unsigned char value,
84 char up_flag);
85static k_handler_fn K_HANDLERS;
86static k_handler_fn *k_handler[16] = { K_HANDLERS };
87
88#define FN_HANDLERS\
89 fn_null, fn_enter, fn_show_ptregs, fn_show_mem,\
90 fn_show_state, fn_send_intr, fn_lastcons, fn_caps_toggle,\
91 fn_num, fn_hold, fn_scroll_forw, fn_scroll_back,\
92 fn_boot_it, fn_caps_on, fn_compose, fn_SAK,\
93 fn_dec_console, fn_inc_console, fn_spawn_con, fn_bare_num
94
95typedef void (fn_handler_fn)(struct vc_data *vc);
96static fn_handler_fn FN_HANDLERS;
97static fn_handler_fn *fn_handler[] = { FN_HANDLERS };
98
99/*
100 * Variables exported for vt_ioctl.c
101 */
102
103/* maximum values each key_handler can handle */
104const int max_vals[] = {
105 255, ARRAY_SIZE(func_table) - 1, ARRAY_SIZE(fn_handler) - 1, NR_PAD - 1,
106 NR_DEAD - 1, 255, 3, NR_SHIFT - 1, 255, NR_ASCII - 1, NR_LOCK - 1,
107 255, NR_LOCK - 1, 255, NR_BRL - 1
108};
109
110const int NR_TYPES = ARRAY_SIZE(max_vals);
111
112struct kbd_struct kbd_table[MAX_NR_CONSOLES];
113EXPORT_SYMBOL_GPL(kbd_table);
114static struct kbd_struct *kbd = kbd_table;
115
116struct vt_spawn_console vt_spawn_con = {
117 .lock = __SPIN_LOCK_UNLOCKED(vt_spawn_con.lock),
118 .pid = NULL,
119 .sig = 0,
120};
121
122/*
123 * Variables exported for vt.c
124 */
125
126int shift_state = 0;
127
128/*
129 * Internal Data.
130 */
131
132static struct input_handler kbd_handler;
133static DEFINE_SPINLOCK(kbd_event_lock);
134static unsigned long key_down[BITS_TO_LONGS(KEY_CNT)]; /* keyboard key bitmap */
135static unsigned char shift_down[NR_SHIFT]; /* shift state counters.. */
136static bool dead_key_next;
137static int npadch = -1; /* -1 or number assembled on pad */
138static unsigned int diacr;
139static char rep; /* flag telling character repeat */
140
141static unsigned char ledstate = 0xff; /* undefined */
142static unsigned char ledioctl;
143
144static struct ledptr {
145 unsigned int *addr;
146 unsigned int mask;
147 unsigned char valid:1;
148} ledptrs[3];
149
150/*
151 * Notifier list for console keyboard events
152 */
153static ATOMIC_NOTIFIER_HEAD(keyboard_notifier_list);
154
155int register_keyboard_notifier(struct notifier_block *nb)
156{
157 return atomic_notifier_chain_register(&keyboard_notifier_list, nb);
158}
159EXPORT_SYMBOL_GPL(register_keyboard_notifier);
160
161int unregister_keyboard_notifier(struct notifier_block *nb)
162{
163 return atomic_notifier_chain_unregister(&keyboard_notifier_list, nb);
164}
165EXPORT_SYMBOL_GPL(unregister_keyboard_notifier);
166
167/*
168 * Translation of scancodes to keycodes. We set them on only the first
169 * keyboard in the list that accepts the scancode and keycode.
170 * Explanation for not choosing the first attached keyboard anymore:
171 * USB keyboards for example have two event devices: one for all "normal"
172 * keys and one for extra function keys (like "volume up", "make coffee",
173 * etc.). So this means that scancodes for the extra function keys won't
174 * be valid for the first event device, but will be for the second.
175 */
176
177struct getset_keycode_data {
178 unsigned int scancode;
179 unsigned int keycode;
180 int error;
181};
182
183static int getkeycode_helper(struct input_handle *handle, void *data)
184{
185 struct getset_keycode_data *d = data;
186
187 d->error = input_get_keycode(handle->dev, d->scancode, &d->keycode);
188
189 return d->error == 0; /* stop as soon as we successfully get one */
190}
191
192int getkeycode(unsigned int scancode)
193{
194 struct getset_keycode_data d = { scancode, 0, -ENODEV };
195
196 input_handler_for_each_handle(&kbd_handler, &d, getkeycode_helper);
197
198 return d.error ?: d.keycode;
199}
200
201static int setkeycode_helper(struct input_handle *handle, void *data)
202{
203 struct getset_keycode_data *d = data;
204
205 d->error = input_set_keycode(handle->dev, d->scancode, d->keycode);
206
207 return d->error == 0; /* stop as soon as we successfully set one */
208}
209
210int setkeycode(unsigned int scancode, unsigned int keycode)
211{
212 struct getset_keycode_data d = { scancode, keycode, -ENODEV };
213
214 input_handler_for_each_handle(&kbd_handler, &d, setkeycode_helper);
215
216 return d.error;
217}
218
219/*
220 * Making beeps and bells. Note that we prefer beeps to bells, but when
221 * shutting the sound off we do both.
222 */
223
224static int kd_sound_helper(struct input_handle *handle, void *data)
225{
226 unsigned int *hz = data;
227 struct input_dev *dev = handle->dev;
228
229 if (test_bit(EV_SND, dev->evbit)) {
230 if (test_bit(SND_TONE, dev->sndbit)) {
231 input_inject_event(handle, EV_SND, SND_TONE, *hz);
232 if (*hz)
233 return 0;
234 }
235 if (test_bit(SND_BELL, dev->sndbit))
236 input_inject_event(handle, EV_SND, SND_BELL, *hz ? 1 : 0);
237 }
238
239 return 0;
240}
241
242static void kd_nosound(unsigned long ignored)
243{
244 static unsigned int zero;
245
246 input_handler_for_each_handle(&kbd_handler, &zero, kd_sound_helper);
247}
248
249static DEFINE_TIMER(kd_mksound_timer, kd_nosound, 0, 0);
250
251void kd_mksound(unsigned int hz, unsigned int ticks)
252{
253 del_timer_sync(&kd_mksound_timer);
254
255 input_handler_for_each_handle(&kbd_handler, &hz, kd_sound_helper);
256
257 if (hz && ticks)
258 mod_timer(&kd_mksound_timer, jiffies + ticks);
259}
260EXPORT_SYMBOL(kd_mksound);
261
262/*
263 * Setting the keyboard rate.
264 */
265
266static int kbd_rate_helper(struct input_handle *handle, void *data)
267{
268 struct input_dev *dev = handle->dev;
269 struct kbd_repeat *rep = data;
270
271 if (test_bit(EV_REP, dev->evbit)) {
272
273 if (rep[0].delay > 0)
274 input_inject_event(handle,
275 EV_REP, REP_DELAY, rep[0].delay);
276 if (rep[0].period > 0)
277 input_inject_event(handle,
278 EV_REP, REP_PERIOD, rep[0].period);
279
280 rep[1].delay = dev->rep[REP_DELAY];
281 rep[1].period = dev->rep[REP_PERIOD];
282 }
283
284 return 0;
285}
286
287int kbd_rate(struct kbd_repeat *rep)
288{
289 struct kbd_repeat data[2] = { *rep };
290
291 input_handler_for_each_handle(&kbd_handler, data, kbd_rate_helper);
292 *rep = data[1]; /* Copy currently used settings */
293
294 return 0;
295}
296
297/*
298 * Helper Functions.
299 */
300static void put_queue(struct vc_data *vc, int ch)
301{
302 struct tty_struct *tty = vc->port.tty;
303
304 if (tty) {
305 tty_insert_flip_char(tty, ch, 0);
306 con_schedule_flip(tty);
307 }
308}
309
310static void puts_queue(struct vc_data *vc, char *cp)
311{
312 struct tty_struct *tty = vc->port.tty;
313
314 if (!tty)
315 return;
316
317 while (*cp) {
318 tty_insert_flip_char(tty, *cp, 0);
319 cp++;
320 }
321 con_schedule_flip(tty);
322}
323
324static void applkey(struct vc_data *vc, int key, char mode)
325{
326 static char buf[] = { 0x1b, 'O', 0x00, 0x00 };
327
328 buf[1] = (mode ? 'O' : '[');
329 buf[2] = key;
330 puts_queue(vc, buf);
331}
332
333/*
334 * Many other routines do put_queue, but I think either
335 * they produce ASCII, or they produce some user-assigned
336 * string, and in both cases we might assume that it is
337 * in utf-8 already.
338 */
339static void to_utf8(struct vc_data *vc, uint c)
340{
341 if (c < 0x80)
342 /* 0******* */
343 put_queue(vc, c);
344 else if (c < 0x800) {
345 /* 110***** 10****** */
346 put_queue(vc, 0xc0 | (c >> 6));
347 put_queue(vc, 0x80 | (c & 0x3f));
348 } else if (c < 0x10000) {
349 if (c >= 0xD800 && c < 0xE000)
350 return;
351 if (c == 0xFFFF)
352 return;
353 /* 1110**** 10****** 10****** */
354 put_queue(vc, 0xe0 | (c >> 12));
355 put_queue(vc, 0x80 | ((c >> 6) & 0x3f));
356 put_queue(vc, 0x80 | (c & 0x3f));
357 } else if (c < 0x110000) {
358 /* 11110*** 10****** 10****** 10****** */
359 put_queue(vc, 0xf0 | (c >> 18));
360 put_queue(vc, 0x80 | ((c >> 12) & 0x3f));
361 put_queue(vc, 0x80 | ((c >> 6) & 0x3f));
362 put_queue(vc, 0x80 | (c & 0x3f));
363 }
364}
365
366/*
367 * Called after returning from RAW mode or when changing consoles - recompute
368 * shift_down[] and shift_state from key_down[] maybe called when keymap is
369 * undefined, so that shiftkey release is seen
370 */
371void compute_shiftstate(void)
372{
373 unsigned int i, j, k, sym, val;
374
375 shift_state = 0;
376 memset(shift_down, 0, sizeof(shift_down));
377
378 for (i = 0; i < ARRAY_SIZE(key_down); i++) {
379
380 if (!key_down[i])
381 continue;
382
383 k = i * BITS_PER_LONG;
384
385 for (j = 0; j < BITS_PER_LONG; j++, k++) {
386
387 if (!test_bit(k, key_down))
388 continue;
389
390 sym = U(key_maps[0][k]);
391 if (KTYP(sym) != KT_SHIFT && KTYP(sym) != KT_SLOCK)
392 continue;
393
394 val = KVAL(sym);
395 if (val == KVAL(K_CAPSSHIFT))
396 val = KVAL(K_SHIFT);
397
398 shift_down[val]++;
399 shift_state |= (1 << val);
400 }
401 }
402}
403
404/*
405 * We have a combining character DIACR here, followed by the character CH.
406 * If the combination occurs in the table, return the corresponding value.
407 * Otherwise, if CH is a space or equals DIACR, return DIACR.
408 * Otherwise, conclude that DIACR was not combining after all,
409 * queue it and return CH.
410 */
411static unsigned int handle_diacr(struct vc_data *vc, unsigned int ch)
412{
413 unsigned int d = diacr;
414 unsigned int i;
415
416 diacr = 0;
417
418 if ((d & ~0xff) == BRL_UC_ROW) {
419 if ((ch & ~0xff) == BRL_UC_ROW)
420 return d | ch;
421 } else {
422 for (i = 0; i < accent_table_size; i++)
423 if (accent_table[i].diacr == d && accent_table[i].base == ch)
424 return accent_table[i].result;
425 }
426
427 if (ch == ' ' || ch == (BRL_UC_ROW|0) || ch == d)
428 return d;
429
430 if (kbd->kbdmode == VC_UNICODE)
431 to_utf8(vc, d);
432 else {
433 int c = conv_uni_to_8bit(d);
434 if (c != -1)
435 put_queue(vc, c);
436 }
437
438 return ch;
439}
440
441/*
442 * Special function handlers
443 */
444static void fn_enter(struct vc_data *vc)
445{
446 if (diacr) {
447 if (kbd->kbdmode == VC_UNICODE)
448 to_utf8(vc, diacr);
449 else {
450 int c = conv_uni_to_8bit(diacr);
451 if (c != -1)
452 put_queue(vc, c);
453 }
454 diacr = 0;
455 }
456
457 put_queue(vc, 13);
458 if (vc_kbd_mode(kbd, VC_CRLF))
459 put_queue(vc, 10);
460}
461
462static void fn_caps_toggle(struct vc_data *vc)
463{
464 if (rep)
465 return;
466
467 chg_vc_kbd_led(kbd, VC_CAPSLOCK);
468}
469
470static void fn_caps_on(struct vc_data *vc)
471{
472 if (rep)
473 return;
474
475 set_vc_kbd_led(kbd, VC_CAPSLOCK);
476}
477
478static void fn_show_ptregs(struct vc_data *vc)
479{
480 struct pt_regs *regs = get_irq_regs();
481
482 if (regs)
483 show_regs(regs);
484}
485
486static void fn_hold(struct vc_data *vc)
487{
488 struct tty_struct *tty = vc->port.tty;
489
490 if (rep || !tty)
491 return;
492
493 /*
494 * Note: SCROLLOCK will be set (cleared) by stop_tty (start_tty);
495 * these routines are also activated by ^S/^Q.
496 * (And SCROLLOCK can also be set by the ioctl KDSKBLED.)
497 */
498 if (tty->stopped)
499 start_tty(tty);
500 else
501 stop_tty(tty);
502}
503
504static void fn_num(struct vc_data *vc)
505{
506 if (vc_kbd_mode(kbd, VC_APPLIC))
507 applkey(vc, 'P', 1);
508 else
509 fn_bare_num(vc);
510}
511
512/*
513 * Bind this to Shift-NumLock if you work in application keypad mode
514 * but want to be able to change the NumLock flag.
515 * Bind this to NumLock if you prefer that the NumLock key always
516 * changes the NumLock flag.
517 */
518static void fn_bare_num(struct vc_data *vc)
519{
520 if (!rep)
521 chg_vc_kbd_led(kbd, VC_NUMLOCK);
522}
523
524static void fn_lastcons(struct vc_data *vc)
525{
526 /* switch to the last used console, ChN */
527 set_console(last_console);
528}
529
530static void fn_dec_console(struct vc_data *vc)
531{
532 int i, cur = fg_console;
533
534 /* Currently switching? Queue this next switch relative to that. */
535 if (want_console != -1)
536 cur = want_console;
537
538 for (i = cur - 1; i != cur; i--) {
539 if (i == -1)
540 i = MAX_NR_CONSOLES - 1;
541 if (vc_cons_allocated(i))
542 break;
543 }
544 set_console(i);
545}
546
547static void fn_inc_console(struct vc_data *vc)
548{
549 int i, cur = fg_console;
550
551 /* Currently switching? Queue this next switch relative to that. */
552 if (want_console != -1)
553 cur = want_console;
554
555 for (i = cur+1; i != cur; i++) {
556 if (i == MAX_NR_CONSOLES)
557 i = 0;
558 if (vc_cons_allocated(i))
559 break;
560 }
561 set_console(i);
562}
563
564static void fn_send_intr(struct vc_data *vc)
565{
566 struct tty_struct *tty = vc->port.tty;
567
568 if (!tty)
569 return;
570 tty_insert_flip_char(tty, 0, TTY_BREAK);
571 con_schedule_flip(tty);
572}
573
574static void fn_scroll_forw(struct vc_data *vc)
575{
576 scrollfront(vc, 0);
577}
578
579static void fn_scroll_back(struct vc_data *vc)
580{
581 scrollback(vc, 0);
582}
583
584static void fn_show_mem(struct vc_data *vc)
585{
586 show_mem();
587}
588
589static void fn_show_state(struct vc_data *vc)
590{
591 show_state();
592}
593
594static void fn_boot_it(struct vc_data *vc)
595{
596 ctrl_alt_del();
597}
598
599static void fn_compose(struct vc_data *vc)
600{
601 dead_key_next = true;
602}
603
604static void fn_spawn_con(struct vc_data *vc)
605{
606 spin_lock(&vt_spawn_con.lock);
607 if (vt_spawn_con.pid)
608 if (kill_pid(vt_spawn_con.pid, vt_spawn_con.sig, 1)) {
609 put_pid(vt_spawn_con.pid);
610 vt_spawn_con.pid = NULL;
611 }
612 spin_unlock(&vt_spawn_con.lock);
613}
614
615static void fn_SAK(struct vc_data *vc)
616{
617 struct work_struct *SAK_work = &vc_cons[fg_console].SAK_work;
618 schedule_work(SAK_work);
619}
620
621static void fn_null(struct vc_data *vc)
622{
623 compute_shiftstate();
624}
625
626/*
627 * Special key handlers
628 */
629static void k_ignore(struct vc_data *vc, unsigned char value, char up_flag)
630{
631}
632
633static void k_spec(struct vc_data *vc, unsigned char value, char up_flag)
634{
635 if (up_flag)
636 return;
637 if (value >= ARRAY_SIZE(fn_handler))
638 return;
639 if ((kbd->kbdmode == VC_RAW ||
640 kbd->kbdmode == VC_MEDIUMRAW) &&
641 value != KVAL(K_SAK))
642 return; /* SAK is allowed even in raw mode */
643 fn_handler[value](vc);
644}
645
646static void k_lowercase(struct vc_data *vc, unsigned char value, char up_flag)
647{
648 pr_err("k_lowercase was called - impossible\n");
649}
650
651static void k_unicode(struct vc_data *vc, unsigned int value, char up_flag)
652{
653 if (up_flag)
654 return; /* no action, if this is a key release */
655
656 if (diacr)
657 value = handle_diacr(vc, value);
658
659 if (dead_key_next) {
660 dead_key_next = false;
661 diacr = value;
662 return;
663 }
664 if (kbd->kbdmode == VC_UNICODE)
665 to_utf8(vc, value);
666 else {
667 int c = conv_uni_to_8bit(value);
668 if (c != -1)
669 put_queue(vc, c);
670 }
671}
672
673/*
674 * Handle dead key. Note that we now may have several
675 * dead keys modifying the same character. Very useful
676 * for Vietnamese.
677 */
678static void k_deadunicode(struct vc_data *vc, unsigned int value, char up_flag)
679{
680 if (up_flag)
681 return;
682
683 diacr = (diacr ? handle_diacr(vc, value) : value);
684}
685
686static void k_self(struct vc_data *vc, unsigned char value, char up_flag)
687{
688 k_unicode(vc, conv_8bit_to_uni(value), up_flag);
689}
690
691static void k_dead2(struct vc_data *vc, unsigned char value, char up_flag)
692{
693 k_deadunicode(vc, value, up_flag);
694}
695
696/*
697 * Obsolete - for backwards compatibility only
698 */
699static void k_dead(struct vc_data *vc, unsigned char value, char up_flag)
700{
701 static const unsigned char ret_diacr[NR_DEAD] = {'`', '\'', '^', '~', '"', ',' };
702
703 k_deadunicode(vc, ret_diacr[value], up_flag);
704}
705
706static void k_cons(struct vc_data *vc, unsigned char value, char up_flag)
707{
708 if (up_flag)
709 return;
710
711 set_console(value);
712}
713
714static void k_fn(struct vc_data *vc, unsigned char value, char up_flag)
715{
716 if (up_flag)
717 return;
718
719 if ((unsigned)value < ARRAY_SIZE(func_table)) {
720 if (func_table[value])
721 puts_queue(vc, func_table[value]);
722 } else
723 pr_err("k_fn called with value=%d\n", value);
724}
725
726static void k_cur(struct vc_data *vc, unsigned char value, char up_flag)
727{
728 static const char cur_chars[] = "BDCA";
729
730 if (up_flag)
731 return;
732
733 applkey(vc, cur_chars[value], vc_kbd_mode(kbd, VC_CKMODE));
734}
735
736static void k_pad(struct vc_data *vc, unsigned char value, char up_flag)
737{
738 static const char pad_chars[] = "0123456789+-*/\015,.?()#";
739 static const char app_map[] = "pqrstuvwxylSRQMnnmPQS";
740
741 if (up_flag)
742 return; /* no action, if this is a key release */
743
744 /* kludge... shift forces cursor/number keys */
745 if (vc_kbd_mode(kbd, VC_APPLIC) && !shift_down[KG_SHIFT]) {
746 applkey(vc, app_map[value], 1);
747 return;
748 }
749
750 if (!vc_kbd_led(kbd, VC_NUMLOCK)) {
751
752 switch (value) {
753 case KVAL(K_PCOMMA):
754 case KVAL(K_PDOT):
755 k_fn(vc, KVAL(K_REMOVE), 0);
756 return;
757 case KVAL(K_P0):
758 k_fn(vc, KVAL(K_INSERT), 0);
759 return;
760 case KVAL(K_P1):
761 k_fn(vc, KVAL(K_SELECT), 0);
762 return;
763 case KVAL(K_P2):
764 k_cur(vc, KVAL(K_DOWN), 0);
765 return;
766 case KVAL(K_P3):
767 k_fn(vc, KVAL(K_PGDN), 0);
768 return;
769 case KVAL(K_P4):
770 k_cur(vc, KVAL(K_LEFT), 0);
771 return;
772 case KVAL(K_P6):
773 k_cur(vc, KVAL(K_RIGHT), 0);
774 return;
775 case KVAL(K_P7):
776 k_fn(vc, KVAL(K_FIND), 0);
777 return;
778 case KVAL(K_P8):
779 k_cur(vc, KVAL(K_UP), 0);
780 return;
781 case KVAL(K_P9):
782 k_fn(vc, KVAL(K_PGUP), 0);
783 return;
784 case KVAL(K_P5):
785 applkey(vc, 'G', vc_kbd_mode(kbd, VC_APPLIC));
786 return;
787 }
788 }
789
790 put_queue(vc, pad_chars[value]);
791 if (value == KVAL(K_PENTER) && vc_kbd_mode(kbd, VC_CRLF))
792 put_queue(vc, 10);
793}
794
795static void k_shift(struct vc_data *vc, unsigned char value, char up_flag)
796{
797 int old_state = shift_state;
798
799 if (rep)
800 return;
801 /*
802 * Mimic typewriter:
803 * a CapsShift key acts like Shift but undoes CapsLock
804 */
805 if (value == KVAL(K_CAPSSHIFT)) {
806 value = KVAL(K_SHIFT);
807 if (!up_flag)
808 clr_vc_kbd_led(kbd, VC_CAPSLOCK);
809 }
810
811 if (up_flag) {
812 /*
813 * handle the case that two shift or control
814 * keys are depressed simultaneously
815 */
816 if (shift_down[value])
817 shift_down[value]--;
818 } else
819 shift_down[value]++;
820
821 if (shift_down[value])
822 shift_state |= (1 << value);
823 else
824 shift_state &= ~(1 << value);
825
826 /* kludge */
827 if (up_flag && shift_state != old_state && npadch != -1) {
828 if (kbd->kbdmode == VC_UNICODE)
829 to_utf8(vc, npadch);
830 else
831 put_queue(vc, npadch & 0xff);
832 npadch = -1;
833 }
834}
835
836static void k_meta(struct vc_data *vc, unsigned char value, char up_flag)
837{
838 if (up_flag)
839 return;
840
841 if (vc_kbd_mode(kbd, VC_META)) {
842 put_queue(vc, '\033');
843 put_queue(vc, value);
844 } else
845 put_queue(vc, value | 0x80);
846}
847
848static void k_ascii(struct vc_data *vc, unsigned char value, char up_flag)
849{
850 int base;
851
852 if (up_flag)
853 return;
854
855 if (value < 10) {
856 /* decimal input of code, while Alt depressed */
857 base = 10;
858 } else {
859 /* hexadecimal input of code, while AltGr depressed */
860 value -= 10;
861 base = 16;
862 }
863
864 if (npadch == -1)
865 npadch = value;
866 else
867 npadch = npadch * base + value;
868}
869
870static void k_lock(struct vc_data *vc, unsigned char value, char up_flag)
871{
872 if (up_flag || rep)
873 return;
874
875 chg_vc_kbd_lock(kbd, value);
876}
877
878static void k_slock(struct vc_data *vc, unsigned char value, char up_flag)
879{
880 k_shift(vc, value, up_flag);
881 if (up_flag || rep)
882 return;
883
884 chg_vc_kbd_slock(kbd, value);
885 /* try to make Alt, oops, AltGr and such work */
886 if (!key_maps[kbd->lockstate ^ kbd->slockstate]) {
887 kbd->slockstate = 0;
888 chg_vc_kbd_slock(kbd, value);
889 }
890}
891
892/* by default, 300ms interval for combination release */
893static unsigned brl_timeout = 300;
894MODULE_PARM_DESC(brl_timeout, "Braille keys release delay in ms (0 for commit on first key release)");
895module_param(brl_timeout, uint, 0644);
896
897static unsigned brl_nbchords = 1;
898MODULE_PARM_DESC(brl_nbchords, "Number of chords that produce a braille pattern (0 for dead chords)");
899module_param(brl_nbchords, uint, 0644);
900
901static void k_brlcommit(struct vc_data *vc, unsigned int pattern, char up_flag)
902{
903 static unsigned long chords;
904 static unsigned committed;
905
906 if (!brl_nbchords)
907 k_deadunicode(vc, BRL_UC_ROW | pattern, up_flag);
908 else {
909 committed |= pattern;
910 chords++;
911 if (chords == brl_nbchords) {
912 k_unicode(vc, BRL_UC_ROW | committed, up_flag);
913 chords = 0;
914 committed = 0;
915 }
916 }
917}
918
919static void k_brl(struct vc_data *vc, unsigned char value, char up_flag)
920{
921 static unsigned pressed, committing;
922 static unsigned long releasestart;
923
924 if (kbd->kbdmode != VC_UNICODE) {
925 if (!up_flag)
926 pr_warning("keyboard mode must be unicode for braille patterns\n");
927 return;
928 }
929
930 if (!value) {
931 k_unicode(vc, BRL_UC_ROW, up_flag);
932 return;
933 }
934
935 if (value > 8)
936 return;
937
938 if (!up_flag) {
939 pressed |= 1 << (value - 1);
940 if (!brl_timeout)
941 committing = pressed;
942 } else if (brl_timeout) {
943 if (!committing ||
944 time_after(jiffies,
945 releasestart + msecs_to_jiffies(brl_timeout))) {
946 committing = pressed;
947 releasestart = jiffies;
948 }
949 pressed &= ~(1 << (value - 1));
950 if (!pressed && committing) {
951 k_brlcommit(vc, committing, 0);
952 committing = 0;
953 }
954 } else {
955 if (committing) {
956 k_brlcommit(vc, committing, 0);
957 committing = 0;
958 }
959 pressed &= ~(1 << (value - 1));
960 }
961}
962
963/*
964 * The leds display either (i) the status of NumLock, CapsLock, ScrollLock,
965 * or (ii) whatever pattern of lights people want to show using KDSETLED,
966 * or (iii) specified bits of specified words in kernel memory.
967 */
968unsigned char getledstate(void)
969{
970 return ledstate;
971}
972
973void setledstate(struct kbd_struct *kbd, unsigned int led)
974{
975 if (!(led & ~7)) {
976 ledioctl = led;
977 kbd->ledmode = LED_SHOW_IOCTL;
978 } else
979 kbd->ledmode = LED_SHOW_FLAGS;
980
981 set_leds();
982}
983
984static inline unsigned char getleds(void)
985{
986 struct kbd_struct *kbd = kbd_table + fg_console;
987 unsigned char leds;
988 int i;
989
990 if (kbd->ledmode == LED_SHOW_IOCTL)
991 return ledioctl;
992
993 leds = kbd->ledflagstate;
994
995 if (kbd->ledmode == LED_SHOW_MEM) {
996 for (i = 0; i < 3; i++)
997 if (ledptrs[i].valid) {
998 if (*ledptrs[i].addr & ledptrs[i].mask)
999 leds |= (1 << i);
1000 else
1001 leds &= ~(1 << i);
1002 }
1003 }
1004 return leds;
1005}
1006
1007static int kbd_update_leds_helper(struct input_handle *handle, void *data)
1008{
1009 unsigned char leds = *(unsigned char *)data;
1010
1011 if (test_bit(EV_LED, handle->dev->evbit)) {
1012 input_inject_event(handle, EV_LED, LED_SCROLLL, !!(leds & 0x01));
1013 input_inject_event(handle, EV_LED, LED_NUML, !!(leds & 0x02));
1014 input_inject_event(handle, EV_LED, LED_CAPSL, !!(leds & 0x04));
1015 input_inject_event(handle, EV_SYN, SYN_REPORT, 0);
1016 }
1017
1018 return 0;
1019}
1020
1021/*
1022 * This is the tasklet that updates LED state on all keyboards
1023 * attached to the box. The reason we use tasklet is that we
1024 * need to handle the scenario when keyboard handler is not
1025 * registered yet but we already getting updates form VT to
1026 * update led state.
1027 */
1028static void kbd_bh(unsigned long dummy)
1029{
1030 unsigned char leds = getleds();
1031
1032 if (leds != ledstate) {
1033 input_handler_for_each_handle(&kbd_handler, &leds,
1034 kbd_update_leds_helper);
1035 ledstate = leds;
1036 }
1037}
1038
1039DECLARE_TASKLET_DISABLED(keyboard_tasklet, kbd_bh, 0);
1040
1041#if defined(CONFIG_X86) || defined(CONFIG_IA64) || defined(CONFIG_ALPHA) ||\
1042 defined(CONFIG_MIPS) || defined(CONFIG_PPC) || defined(CONFIG_SPARC) ||\
1043 defined(CONFIG_PARISC) || defined(CONFIG_SUPERH) ||\
1044 (defined(CONFIG_ARM) && defined(CONFIG_KEYBOARD_ATKBD) && !defined(CONFIG_ARCH_RPC)) ||\
1045 defined(CONFIG_AVR32)
1046
1047#define HW_RAW(dev) (test_bit(EV_MSC, dev->evbit) && test_bit(MSC_RAW, dev->mscbit) &&\
1048 ((dev)->id.bustype == BUS_I8042) && ((dev)->id.vendor == 0x0001) && ((dev)->id.product == 0x0001))
1049
1050static const unsigned short x86_keycodes[256] =
1051 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
1052 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
1053 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
1054 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
1055 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
1056 80, 81, 82, 83, 84,118, 86, 87, 88,115,120,119,121,112,123, 92,
1057 284,285,309, 0,312, 91,327,328,329,331,333,335,336,337,338,339,
1058 367,288,302,304,350, 89,334,326,267,126,268,269,125,347,348,349,
1059 360,261,262,263,268,376,100,101,321,316,373,286,289,102,351,355,
1060 103,104,105,275,287,279,258,106,274,107,294,364,358,363,362,361,
1061 291,108,381,281,290,272,292,305,280, 99,112,257,306,359,113,114,
1062 264,117,271,374,379,265,266, 93, 94, 95, 85,259,375,260, 90,116,
1063 377,109,111,277,278,282,283,295,296,297,299,300,301,293,303,307,
1064 308,310,313,314,315,317,318,319,320,357,322,323,324,325,276,330,
1065 332,340,365,342,343,344,345,346,356,270,341,368,369,370,371,372 };
1066
1067#ifdef CONFIG_SPARC
1068static int sparc_l1_a_state;
1069extern void sun_do_break(void);
1070#endif
1071
1072static int emulate_raw(struct vc_data *vc, unsigned int keycode,
1073 unsigned char up_flag)
1074{
1075 int code;
1076
1077 switch (keycode) {
1078
1079 case KEY_PAUSE:
1080 put_queue(vc, 0xe1);
1081 put_queue(vc, 0x1d | up_flag);
1082 put_queue(vc, 0x45 | up_flag);
1083 break;
1084
1085 case KEY_HANGEUL:
1086 if (!up_flag)
1087 put_queue(vc, 0xf2);
1088 break;
1089
1090 case KEY_HANJA:
1091 if (!up_flag)
1092 put_queue(vc, 0xf1);
1093 break;
1094
1095 case KEY_SYSRQ:
1096 /*
1097 * Real AT keyboards (that's what we're trying
1098 * to emulate here emit 0xe0 0x2a 0xe0 0x37 when
1099 * pressing PrtSc/SysRq alone, but simply 0x54
1100 * when pressing Alt+PrtSc/SysRq.
1101 */
1102 if (test_bit(KEY_LEFTALT, key_down) ||
1103 test_bit(KEY_RIGHTALT, key_down)) {
1104 put_queue(vc, 0x54 | up_flag);
1105 } else {
1106 put_queue(vc, 0xe0);
1107 put_queue(vc, 0x2a | up_flag);
1108 put_queue(vc, 0xe0);
1109 put_queue(vc, 0x37 | up_flag);
1110 }
1111 break;
1112
1113 default:
1114 if (keycode > 255)
1115 return -1;
1116
1117 code = x86_keycodes[keycode];
1118 if (!code)
1119 return -1;
1120
1121 if (code & 0x100)
1122 put_queue(vc, 0xe0);
1123 put_queue(vc, (code & 0x7f) | up_flag);
1124
1125 break;
1126 }
1127
1128 return 0;
1129}
1130
1131#else
1132
1133#define HW_RAW(dev) 0
1134
1135static int emulate_raw(struct vc_data *vc, unsigned int keycode, unsigned char up_flag)
1136{
1137 if (keycode > 127)
1138 return -1;
1139
1140 put_queue(vc, keycode | up_flag);
1141 return 0;
1142}
1143#endif
1144
1145static void kbd_rawcode(unsigned char data)
1146{
1147 struct vc_data *vc = vc_cons[fg_console].d;
1148
1149 kbd = kbd_table + vc->vc_num;
1150 if (kbd->kbdmode == VC_RAW)
1151 put_queue(vc, data);
1152}
1153
1154static void kbd_keycode(unsigned int keycode, int down, int hw_raw)
1155{
1156 struct vc_data *vc = vc_cons[fg_console].d;
1157 unsigned short keysym, *key_map;
1158 unsigned char type;
1159 bool raw_mode;
1160 struct tty_struct *tty;
1161 int shift_final;
1162 struct keyboard_notifier_param param = { .vc = vc, .value = keycode, .down = down };
1163 int rc;
1164
1165 tty = vc->port.tty;
1166
1167 if (tty && (!tty->driver_data)) {
1168 /* No driver data? Strange. Okay we fix it then. */
1169 tty->driver_data = vc;
1170 }
1171
1172 kbd = kbd_table + vc->vc_num;
1173
1174#ifdef CONFIG_SPARC
1175 if (keycode == KEY_STOP)
1176 sparc_l1_a_state = down;
1177#endif
1178
1179 rep = (down == 2);
1180
1181 raw_mode = (kbd->kbdmode == VC_RAW);
1182 if (raw_mode && !hw_raw)
1183 if (emulate_raw(vc, keycode, !down << 7))
1184 if (keycode < BTN_MISC && printk_ratelimit())
1185 pr_warning("can't emulate rawmode for keycode %d\n",
1186 keycode);
1187
1188#ifdef CONFIG_SPARC
1189 if (keycode == KEY_A && sparc_l1_a_state) {
1190 sparc_l1_a_state = false;
1191 sun_do_break();
1192 }
1193#endif
1194
1195 if (kbd->kbdmode == VC_MEDIUMRAW) {
1196 /*
1197 * This is extended medium raw mode, with keys above 127
1198 * encoded as 0, high 7 bits, low 7 bits, with the 0 bearing
1199 * the 'up' flag if needed. 0 is reserved, so this shouldn't
1200 * interfere with anything else. The two bytes after 0 will
1201 * always have the up flag set not to interfere with older
1202 * applications. This allows for 16384 different keycodes,
1203 * which should be enough.
1204 */
1205 if (keycode < 128) {
1206 put_queue(vc, keycode | (!down << 7));
1207 } else {
1208 put_queue(vc, !down << 7);
1209 put_queue(vc, (keycode >> 7) | 0x80);
1210 put_queue(vc, keycode | 0x80);
1211 }
1212 raw_mode = true;
1213 }
1214
1215 if (down)
1216 set_bit(keycode, key_down);
1217 else
1218 clear_bit(keycode, key_down);
1219
1220 if (rep &&
1221 (!vc_kbd_mode(kbd, VC_REPEAT) ||
1222 (tty && !L_ECHO(tty) && tty_chars_in_buffer(tty)))) {
1223 /*
1224 * Don't repeat a key if the input buffers are not empty and the
1225 * characters get aren't echoed locally. This makes key repeat
1226 * usable with slow applications and under heavy loads.
1227 */
1228 return;
1229 }
1230
1231 param.shift = shift_final = (shift_state | kbd->slockstate) ^ kbd->lockstate;
1232 param.ledstate = kbd->ledflagstate;
1233 key_map = key_maps[shift_final];
1234
1235 rc = atomic_notifier_call_chain(&keyboard_notifier_list,
1236 KBD_KEYCODE, &param);
1237 if (rc == NOTIFY_STOP || !key_map) {
1238 atomic_notifier_call_chain(&keyboard_notifier_list,
1239 KBD_UNBOUND_KEYCODE, &param);
1240 compute_shiftstate();
1241 kbd->slockstate = 0;
1242 return;
1243 }
1244
1245 if (keycode < NR_KEYS)
1246 keysym = key_map[keycode];
1247 else if (keycode >= KEY_BRL_DOT1 && keycode <= KEY_BRL_DOT8)
1248 keysym = U(K(KT_BRL, keycode - KEY_BRL_DOT1 + 1));
1249 else
1250 return;
1251
1252 type = KTYP(keysym);
1253
1254 if (type < 0xf0) {
1255 param.value = keysym;
1256 rc = atomic_notifier_call_chain(&keyboard_notifier_list,
1257 KBD_UNICODE, &param);
1258 if (rc != NOTIFY_STOP)
1259 if (down && !raw_mode)
1260 to_utf8(vc, keysym);
1261 return;
1262 }
1263
1264 type -= 0xf0;
1265
1266 if (type == KT_LETTER) {
1267 type = KT_LATIN;
1268 if (vc_kbd_led(kbd, VC_CAPSLOCK)) {
1269 key_map = key_maps[shift_final ^ (1 << KG_SHIFT)];
1270 if (key_map)
1271 keysym = key_map[keycode];
1272 }
1273 }
1274
1275 param.value = keysym;
1276 rc = atomic_notifier_call_chain(&keyboard_notifier_list,
1277 KBD_KEYSYM, &param);
1278 if (rc == NOTIFY_STOP)
1279 return;
1280
1281 if (raw_mode && type != KT_SPEC && type != KT_SHIFT)
1282 return;
1283
1284 (*k_handler[type])(vc, keysym & 0xff, !down);
1285
1286 param.ledstate = kbd->ledflagstate;
1287 atomic_notifier_call_chain(&keyboard_notifier_list, KBD_POST_KEYSYM, &param);
1288
1289 if (type != KT_SLOCK)
1290 kbd->slockstate = 0;
1291}
1292
1293static void kbd_event(struct input_handle *handle, unsigned int event_type,
1294 unsigned int event_code, int value)
1295{
1296 /* We are called with interrupts disabled, just take the lock */
1297 spin_lock(&kbd_event_lock);
1298
1299 if (event_type == EV_MSC && event_code == MSC_RAW && HW_RAW(handle->dev))
1300 kbd_rawcode(value);
1301 if (event_type == EV_KEY)
1302 kbd_keycode(event_code, value, HW_RAW(handle->dev));
1303
1304 spin_unlock(&kbd_event_lock);
1305
1306 tasklet_schedule(&keyboard_tasklet);
1307 do_poke_blanked_console = 1;
1308 schedule_console_callback();
1309}
1310
1311static bool kbd_match(struct input_handler *handler, struct input_dev *dev)
1312{
1313 int i;
1314
1315 if (test_bit(EV_SND, dev->evbit))
1316 return true;
1317
1318 if (test_bit(EV_KEY, dev->evbit)) {
1319 for (i = KEY_RESERVED; i < BTN_MISC; i++)
1320 if (test_bit(i, dev->keybit))
1321 return true;
1322 for (i = KEY_BRL_DOT1; i <= KEY_BRL_DOT10; i++)
1323 if (test_bit(i, dev->keybit))
1324 return true;
1325 }
1326
1327 return false;
1328}
1329
1330/*
1331 * When a keyboard (or other input device) is found, the kbd_connect
1332 * function is called. The function then looks at the device, and if it
1333 * likes it, it can open it and get events from it. In this (kbd_connect)
1334 * function, we should decide which VT to bind that keyboard to initially.
1335 */
1336static int kbd_connect(struct input_handler *handler, struct input_dev *dev,
1337 const struct input_device_id *id)
1338{
1339 struct input_handle *handle;
1340 int error;
1341
1342 handle = kzalloc(sizeof(struct input_handle), GFP_KERNEL);
1343 if (!handle)
1344 return -ENOMEM;
1345
1346 handle->dev = dev;
1347 handle->handler = handler;
1348 handle->name = "kbd";
1349
1350 error = input_register_handle(handle);
1351 if (error)
1352 goto err_free_handle;
1353
1354 error = input_open_device(handle);
1355 if (error)
1356 goto err_unregister_handle;
1357
1358 return 0;
1359
1360 err_unregister_handle:
1361 input_unregister_handle(handle);
1362 err_free_handle:
1363 kfree(handle);
1364 return error;
1365}
1366
1367static void kbd_disconnect(struct input_handle *handle)
1368{
1369 input_close_device(handle);
1370 input_unregister_handle(handle);
1371 kfree(handle);
1372}
1373
1374/*
1375 * Start keyboard handler on the new keyboard by refreshing LED state to
1376 * match the rest of the system.
1377 */
1378static void kbd_start(struct input_handle *handle)
1379{
1380 tasklet_disable(&keyboard_tasklet);
1381
1382 if (ledstate != 0xff)
1383 kbd_update_leds_helper(handle, &ledstate);
1384
1385 tasklet_enable(&keyboard_tasklet);
1386}
1387
1388static const struct input_device_id kbd_ids[] = {
1389 {
1390 .flags = INPUT_DEVICE_ID_MATCH_EVBIT,
1391 .evbit = { BIT_MASK(EV_KEY) },
1392 },
1393
1394 {
1395 .flags = INPUT_DEVICE_ID_MATCH_EVBIT,
1396 .evbit = { BIT_MASK(EV_SND) },
1397 },
1398
1399 { }, /* Terminating entry */
1400};
1401
1402MODULE_DEVICE_TABLE(input, kbd_ids);
1403
1404static struct input_handler kbd_handler = {
1405 .event = kbd_event,
1406 .match = kbd_match,
1407 .connect = kbd_connect,
1408 .disconnect = kbd_disconnect,
1409 .start = kbd_start,
1410 .name = "kbd",
1411 .id_table = kbd_ids,
1412};
1413
1414int __init kbd_init(void)
1415{
1416 int i;
1417 int error;
1418
1419 for (i = 0; i < MAX_NR_CONSOLES; i++) {
1420 kbd_table[i].ledflagstate = KBD_DEFLEDS;
1421 kbd_table[i].default_ledflagstate = KBD_DEFLEDS;
1422 kbd_table[i].ledmode = LED_SHOW_FLAGS;
1423 kbd_table[i].lockstate = KBD_DEFLOCK;
1424 kbd_table[i].slockstate = 0;
1425 kbd_table[i].modeflags = KBD_DEFMODE;
1426 kbd_table[i].kbdmode = default_utf8 ? VC_UNICODE : VC_XLATE;
1427 }
1428
1429 error = input_register_handler(&kbd_handler);
1430 if (error)
1431 return error;
1432
1433 tasklet_enable(&keyboard_tasklet);
1434 tasklet_schedule(&keyboard_tasklet);
1435
1436 return 0;
1437}
diff --git a/drivers/char/lp.c b/drivers/char/lp.c
index 938a3a273886..97c3edb95ae7 100644
--- a/drivers/char/lp.c
+++ b/drivers/char/lp.c
@@ -126,7 +126,7 @@
126#include <linux/device.h> 126#include <linux/device.h>
127#include <linux/wait.h> 127#include <linux/wait.h>
128#include <linux/jiffies.h> 128#include <linux/jiffies.h>
129#include <linux/smp_lock.h> 129#include <linux/mutex.h>
130#include <linux/compat.h> 130#include <linux/compat.h>
131 131
132#include <linux/parport.h> 132#include <linux/parport.h>
@@ -140,6 +140,7 @@
140/* if you have more than 8 printers, remember to increase LP_NO */ 140/* if you have more than 8 printers, remember to increase LP_NO */
141#define LP_NO 8 141#define LP_NO 8
142 142
143static DEFINE_MUTEX(lp_mutex);
143static struct lp_struct lp_table[LP_NO]; 144static struct lp_struct lp_table[LP_NO];
144 145
145static unsigned int lp_count = 0; 146static unsigned int lp_count = 0;
@@ -493,7 +494,7 @@ static int lp_open(struct inode * inode, struct file * file)
493 unsigned int minor = iminor(inode); 494 unsigned int minor = iminor(inode);
494 int ret = 0; 495 int ret = 0;
495 496
496 lock_kernel(); 497 mutex_lock(&lp_mutex);
497 if (minor >= LP_NO) { 498 if (minor >= LP_NO) {
498 ret = -ENXIO; 499 ret = -ENXIO;
499 goto out; 500 goto out;
@@ -554,7 +555,7 @@ static int lp_open(struct inode * inode, struct file * file)
554 lp_release_parport (&lp_table[minor]); 555 lp_release_parport (&lp_table[minor]);
555 lp_table[minor].current_mode = IEEE1284_MODE_COMPAT; 556 lp_table[minor].current_mode = IEEE1284_MODE_COMPAT;
556out: 557out:
557 unlock_kernel(); 558 mutex_unlock(&lp_mutex);
558 return ret; 559 return ret;
559} 560}
560 561
@@ -680,7 +681,7 @@ static long lp_ioctl(struct file *file, unsigned int cmd,
680 int ret; 681 int ret;
681 682
682 minor = iminor(file->f_path.dentry->d_inode); 683 minor = iminor(file->f_path.dentry->d_inode);
683 lock_kernel(); 684 mutex_lock(&lp_mutex);
684 switch (cmd) { 685 switch (cmd) {
685 case LPSETTIMEOUT: 686 case LPSETTIMEOUT:
686 if (copy_from_user(&par_timeout, (void __user *)arg, 687 if (copy_from_user(&par_timeout, (void __user *)arg,
@@ -694,7 +695,7 @@ static long lp_ioctl(struct file *file, unsigned int cmd,
694 ret = lp_do_ioctl(minor, cmd, arg, (void __user *)arg); 695 ret = lp_do_ioctl(minor, cmd, arg, (void __user *)arg);
695 break; 696 break;
696 } 697 }
697 unlock_kernel(); 698 mutex_unlock(&lp_mutex);
698 699
699 return ret; 700 return ret;
700} 701}
@@ -709,7 +710,7 @@ static long lp_compat_ioctl(struct file *file, unsigned int cmd,
709 int ret; 710 int ret;
710 711
711 minor = iminor(file->f_path.dentry->d_inode); 712 minor = iminor(file->f_path.dentry->d_inode);
712 lock_kernel(); 713 mutex_lock(&lp_mutex);
713 switch (cmd) { 714 switch (cmd) {
714 case LPSETTIMEOUT: 715 case LPSETTIMEOUT:
715 tc = compat_ptr(arg); 716 tc = compat_ptr(arg);
@@ -730,7 +731,7 @@ static long lp_compat_ioctl(struct file *file, unsigned int cmd,
730 ret = lp_do_ioctl(minor, cmd, arg, compat_ptr(arg)); 731 ret = lp_do_ioctl(minor, cmd, arg, compat_ptr(arg));
731 break; 732 break;
732 } 733 }
733 unlock_kernel(); 734 mutex_unlock(&lp_mutex);
734 735
735 return ret; 736 return ret;
736} 737}
@@ -748,6 +749,7 @@ static const struct file_operations lp_fops = {
748#ifdef CONFIG_PARPORT_1284 749#ifdef CONFIG_PARPORT_1284
749 .read = lp_read, 750 .read = lp_read,
750#endif 751#endif
752 .llseek = noop_llseek,
751}; 753};
752 754
753/* --- support for console on the line printer ----------------- */ 755/* --- support for console on the line printer ----------------- */
diff --git a/drivers/char/mbcs.c b/drivers/char/mbcs.c
index 83bef4efe376..1aeaaba680d2 100644
--- a/drivers/char/mbcs.c
+++ b/drivers/char/mbcs.c
@@ -25,7 +25,6 @@
25#include <linux/mm.h> 25#include <linux/mm.h>
26#include <linux/uio.h> 26#include <linux/uio.h>
27#include <linux/mutex.h> 27#include <linux/mutex.h>
28#include <linux/smp_lock.h>
29#include <linux/slab.h> 28#include <linux/slab.h>
30#include <asm/io.h> 29#include <asm/io.h>
31#include <asm/uaccess.h> 30#include <asm/uaccess.h>
@@ -42,6 +41,7 @@
42#else 41#else
43#define DBG(fmt...) 42#define DBG(fmt...)
44#endif 43#endif
44static DEFINE_MUTEX(mbcs_mutex);
45static int mbcs_major; 45static int mbcs_major;
46 46
47static LIST_HEAD(soft_list); 47static LIST_HEAD(soft_list);
@@ -385,19 +385,19 @@ static int mbcs_open(struct inode *ip, struct file *fp)
385 struct mbcs_soft *soft; 385 struct mbcs_soft *soft;
386 int minor; 386 int minor;
387 387
388 lock_kernel(); 388 mutex_lock(&mbcs_mutex);
389 minor = iminor(ip); 389 minor = iminor(ip);
390 390
391 /* Nothing protects access to this list... */ 391 /* Nothing protects access to this list... */
392 list_for_each_entry(soft, &soft_list, list) { 392 list_for_each_entry(soft, &soft_list, list) {
393 if (soft->nasid == minor) { 393 if (soft->nasid == minor) {
394 fp->private_data = soft->cxdev; 394 fp->private_data = soft->cxdev;
395 unlock_kernel(); 395 mutex_unlock(&mbcs_mutex);
396 return 0; 396 return 0;
397 } 397 }
398 } 398 }
399 399
400 unlock_kernel(); 400 mutex_unlock(&mbcs_mutex);
401 return -ENODEV; 401 return -ENODEV;
402} 402}
403 403
diff --git a/drivers/char/mbcs.h b/drivers/char/mbcs.h
index ba671589f4cb..1a36884c48b5 100644
--- a/drivers/char/mbcs.h
+++ b/drivers/char/mbcs.h
@@ -36,13 +36,13 @@
36#define MBCS_RD_DMA_CTRL 0x0110 /* Read DMA Control */ 36#define MBCS_RD_DMA_CTRL 0x0110 /* Read DMA Control */
37#define MBCS_RD_DMA_AMO_DEST 0x0118 /* Read DMA AMO Destination */ 37#define MBCS_RD_DMA_AMO_DEST 0x0118 /* Read DMA AMO Destination */
38#define MBCS_RD_DMA_INT_DEST 0x0120 /* Read DMA Interrupt Destination */ 38#define MBCS_RD_DMA_INT_DEST 0x0120 /* Read DMA Interrupt Destination */
39#define MBCS_RD_DMA_AUX_STAT 0x0130 /* Read DMA Auxillary Status */ 39#define MBCS_RD_DMA_AUX_STAT 0x0130 /* Read DMA Auxiliary Status */
40#define MBCS_WR_DMA_SYS_ADDR 0x0200 /* Write DMA System Address */ 40#define MBCS_WR_DMA_SYS_ADDR 0x0200 /* Write DMA System Address */
41#define MBCS_WR_DMA_LOC_ADDR 0x0208 /* Write DMA Local Address */ 41#define MBCS_WR_DMA_LOC_ADDR 0x0208 /* Write DMA Local Address */
42#define MBCS_WR_DMA_CTRL 0x0210 /* Write DMA Control */ 42#define MBCS_WR_DMA_CTRL 0x0210 /* Write DMA Control */
43#define MBCS_WR_DMA_AMO_DEST 0x0218 /* Write DMA AMO Destination */ 43#define MBCS_WR_DMA_AMO_DEST 0x0218 /* Write DMA AMO Destination */
44#define MBCS_WR_DMA_INT_DEST 0x0220 /* Write DMA Interrupt Destination */ 44#define MBCS_WR_DMA_INT_DEST 0x0220 /* Write DMA Interrupt Destination */
45#define MBCS_WR_DMA_AUX_STAT 0x0230 /* Write DMA Auxillary Status */ 45#define MBCS_WR_DMA_AUX_STAT 0x0230 /* Write DMA Auxiliary Status */
46#define MBCS_ALG_AMO_DEST 0x0300 /* Algorithm AMO Destination */ 46#define MBCS_ALG_AMO_DEST 0x0300 /* Algorithm AMO Destination */
47#define MBCS_ALG_INT_DEST 0x0308 /* Algorithm Interrupt Destination */ 47#define MBCS_ALG_INT_DEST 0x0308 /* Algorithm Interrupt Destination */
48#define MBCS_ALG_OFFSETS 0x0310 48#define MBCS_ALG_OFFSETS 0x0310
diff --git a/drivers/char/mem.c b/drivers/char/mem.c
index 1f528fad3516..8fc04b4f311f 100644
--- a/drivers/char/mem.c
+++ b/drivers/char/mem.c
@@ -47,10 +47,7 @@ static inline unsigned long size_inside_page(unsigned long start,
47#ifndef ARCH_HAS_VALID_PHYS_ADDR_RANGE 47#ifndef ARCH_HAS_VALID_PHYS_ADDR_RANGE
48static inline int valid_phys_addr_range(unsigned long addr, size_t count) 48static inline int valid_phys_addr_range(unsigned long addr, size_t count)
49{ 49{
50 if (addr + count > __pa(high_memory)) 50 return addr + count <= __pa(high_memory);
51 return 0;
52
53 return 1;
54} 51}
55 52
56static inline int valid_mmap_phys_addr_range(unsigned long pfn, size_t size) 53static inline int valid_mmap_phys_addr_range(unsigned long pfn, size_t size)
@@ -805,32 +802,46 @@ static const struct file_operations full_fops = {
805static const struct file_operations oldmem_fops = { 802static const struct file_operations oldmem_fops = {
806 .read = read_oldmem, 803 .read = read_oldmem,
807 .open = open_oldmem, 804 .open = open_oldmem,
805 .llseek = default_llseek,
808}; 806};
809#endif 807#endif
810 808
811static ssize_t kmsg_write(struct file *file, const char __user *buf, 809static ssize_t kmsg_writev(struct kiocb *iocb, const struct iovec *iv,
812 size_t count, loff_t *ppos) 810 unsigned long count, loff_t pos)
813{ 811{
814 char *tmp; 812 char *line, *p;
815 ssize_t ret; 813 int i;
814 ssize_t ret = -EFAULT;
815 size_t len = iov_length(iv, count);
816 816
817 tmp = kmalloc(count + 1, GFP_KERNEL); 817 line = kmalloc(len + 1, GFP_KERNEL);
818 if (tmp == NULL) 818 if (line == NULL)
819 return -ENOMEM; 819 return -ENOMEM;
820 ret = -EFAULT; 820
821 if (!copy_from_user(tmp, buf, count)) { 821 /*
822 tmp[count] = 0; 822 * copy all vectors into a single string, to ensure we do
823 ret = printk("%s", tmp); 823 * not interleave our log line with other printk calls
824 if (ret > count) 824 */
825 /* printk can add a prefix */ 825 p = line;
826 ret = count; 826 for (i = 0; i < count; i++) {
827 if (copy_from_user(p, iv[i].iov_base, iv[i].iov_len))
828 goto out;
829 p += iv[i].iov_len;
827 } 830 }
828 kfree(tmp); 831 p[0] = '\0';
832
833 ret = printk("%s", line);
834 /* printk can add a prefix */
835 if (ret > len)
836 ret = len;
837out:
838 kfree(line);
829 return ret; 839 return ret;
830} 840}
831 841
832static const struct file_operations kmsg_fops = { 842static const struct file_operations kmsg_fops = {
833 .write = kmsg_write, 843 .aio_write = kmsg_writev,
844 .llseek = noop_llseek,
834}; 845};
835 846
836static const struct memdev { 847static const struct memdev {
@@ -874,6 +885,10 @@ static int memory_open(struct inode *inode, struct file *filp)
874 if (dev->dev_info) 885 if (dev->dev_info)
875 filp->f_mapping->backing_dev_info = dev->dev_info; 886 filp->f_mapping->backing_dev_info = dev->dev_info;
876 887
888 /* Is /dev/mem or /dev/kmem ? */
889 if (dev->dev_info == &directly_mappable_cdev_bdi)
890 filp->f_mode |= FMODE_UNSIGNED_OFFSET;
891
877 if (dev->fops->open) 892 if (dev->fops->open)
878 return dev->fops->open(inode, filp); 893 return dev->fops->open(inode, filp);
879 894
@@ -882,6 +897,7 @@ static int memory_open(struct inode *inode, struct file *filp)
882 897
883static const struct file_operations memory_fops = { 898static const struct file_operations memory_fops = {
884 .open = memory_open, 899 .open = memory_open,
900 .llseek = noop_llseek,
885}; 901};
886 902
887static char *mem_devnode(struct device *dev, mode_t *mode) 903static char *mem_devnode(struct device *dev, mode_t *mode)
diff --git a/drivers/char/misc.c b/drivers/char/misc.c
index abdafd488980..778273c93242 100644
--- a/drivers/char/misc.c
+++ b/drivers/char/misc.c
@@ -162,6 +162,7 @@ static struct class *misc_class;
162static const struct file_operations misc_fops = { 162static const struct file_operations misc_fops = {
163 .owner = THIS_MODULE, 163 .owner = THIS_MODULE,
164 .open = misc_open, 164 .open = misc_open,
165 .llseek = noop_llseek,
165}; 166};
166 167
167/** 168/**
diff --git a/drivers/char/mmtimer.c b/drivers/char/mmtimer.c
index ea7c99fa978f..33dc2298af73 100644
--- a/drivers/char/mmtimer.c
+++ b/drivers/char/mmtimer.c
@@ -32,7 +32,7 @@
32#include <linux/interrupt.h> 32#include <linux/interrupt.h>
33#include <linux/time.h> 33#include <linux/time.h>
34#include <linux/math64.h> 34#include <linux/math64.h>
35#include <linux/smp_lock.h> 35#include <linux/mutex.h>
36#include <linux/slab.h> 36#include <linux/slab.h>
37 37
38#include <asm/uaccess.h> 38#include <asm/uaccess.h>
@@ -53,12 +53,15 @@ MODULE_LICENSE("GPL");
53 53
54#define RTC_BITS 55 /* 55 bits for this implementation */ 54#define RTC_BITS 55 /* 55 bits for this implementation */
55 55
56static struct k_clock sgi_clock;
57
56extern unsigned long sn_rtc_cycles_per_second; 58extern unsigned long sn_rtc_cycles_per_second;
57 59
58#define RTC_COUNTER_ADDR ((long *)LOCAL_MMR_ADDR(SH_RTC)) 60#define RTC_COUNTER_ADDR ((long *)LOCAL_MMR_ADDR(SH_RTC))
59 61
60#define rtc_time() (*RTC_COUNTER_ADDR) 62#define rtc_time() (*RTC_COUNTER_ADDR)
61 63
64static DEFINE_MUTEX(mmtimer_mutex);
62static long mmtimer_ioctl(struct file *file, unsigned int cmd, 65static long mmtimer_ioctl(struct file *file, unsigned int cmd,
63 unsigned long arg); 66 unsigned long arg);
64static int mmtimer_mmap(struct file *file, struct vm_area_struct *vma); 67static int mmtimer_mmap(struct file *file, struct vm_area_struct *vma);
@@ -72,6 +75,7 @@ static const struct file_operations mmtimer_fops = {
72 .owner = THIS_MODULE, 75 .owner = THIS_MODULE,
73 .mmap = mmtimer_mmap, 76 .mmap = mmtimer_mmap,
74 .unlocked_ioctl = mmtimer_ioctl, 77 .unlocked_ioctl = mmtimer_ioctl,
78 .llseek = noop_llseek,
75}; 79};
76 80
77/* 81/*
@@ -174,9 +178,9 @@ static void mmtimer_setup_int_2(int cpu, u64 expires)
174 * in order to insure that the setup succeeds in a deterministic time frame. 178 * in order to insure that the setup succeeds in a deterministic time frame.
175 * It will check if the interrupt setup succeeded. 179 * It will check if the interrupt setup succeeded.
176 */ 180 */
177static int mmtimer_setup(int cpu, int comparator, unsigned long expires) 181static int mmtimer_setup(int cpu, int comparator, unsigned long expires,
182 u64 *set_completion_time)
178{ 183{
179
180 switch (comparator) { 184 switch (comparator) {
181 case 0: 185 case 0:
182 mmtimer_setup_int_0(cpu, expires); 186 mmtimer_setup_int_0(cpu, expires);
@@ -189,7 +193,8 @@ static int mmtimer_setup(int cpu, int comparator, unsigned long expires)
189 break; 193 break;
190 } 194 }
191 /* We might've missed our expiration time */ 195 /* We might've missed our expiration time */
192 if (rtc_time() <= expires) 196 *set_completion_time = rtc_time();
197 if (*set_completion_time <= expires)
193 return 1; 198 return 1;
194 199
195 /* 200 /*
@@ -225,6 +230,8 @@ static int mmtimer_disable_int(long nasid, int comparator)
225#define TIMER_OFF 0xbadcabLL /* Timer is not setup */ 230#define TIMER_OFF 0xbadcabLL /* Timer is not setup */
226#define TIMER_SET 0 /* Comparator is set for this timer */ 231#define TIMER_SET 0 /* Comparator is set for this timer */
227 232
233#define MMTIMER_INTERVAL_RETRY_INCREMENT_DEFAULT 40
234
228/* There is one of these for each timer */ 235/* There is one of these for each timer */
229struct mmtimer { 236struct mmtimer {
230 struct rb_node list; 237 struct rb_node list;
@@ -240,6 +247,11 @@ struct mmtimer_node {
240}; 247};
241static struct mmtimer_node *timers; 248static struct mmtimer_node *timers;
242 249
250static unsigned mmtimer_interval_retry_increment =
251 MMTIMER_INTERVAL_RETRY_INCREMENT_DEFAULT;
252module_param(mmtimer_interval_retry_increment, uint, 0644);
253MODULE_PARM_DESC(mmtimer_interval_retry_increment,
254 "RTC ticks to add to expiration on interval retry (default 40)");
243 255
244/* 256/*
245 * Add a new mmtimer struct to the node's mmtimer list. 257 * Add a new mmtimer struct to the node's mmtimer list.
@@ -287,7 +299,8 @@ static void mmtimer_set_next_timer(int nodeid)
287 struct mmtimer_node *n = &timers[nodeid]; 299 struct mmtimer_node *n = &timers[nodeid];
288 struct mmtimer *x; 300 struct mmtimer *x;
289 struct k_itimer *t; 301 struct k_itimer *t;
290 int o; 302 u64 expires, exp, set_completion_time;
303 int i;
291 304
292restart: 305restart:
293 if (n->next == NULL) 306 if (n->next == NULL)
@@ -298,7 +311,8 @@ restart:
298 if (!t->it.mmtimer.incr) { 311 if (!t->it.mmtimer.incr) {
299 /* Not an interval timer */ 312 /* Not an interval timer */
300 if (!mmtimer_setup(x->cpu, COMPARATOR, 313 if (!mmtimer_setup(x->cpu, COMPARATOR,
301 t->it.mmtimer.expires)) { 314 t->it.mmtimer.expires,
315 &set_completion_time)) {
302 /* Late setup, fire now */ 316 /* Late setup, fire now */
303 tasklet_schedule(&n->tasklet); 317 tasklet_schedule(&n->tasklet);
304 } 318 }
@@ -306,14 +320,23 @@ restart:
306 } 320 }
307 321
308 /* Interval timer */ 322 /* Interval timer */
309 o = 0; 323 i = 0;
310 while (!mmtimer_setup(x->cpu, COMPARATOR, t->it.mmtimer.expires)) { 324 expires = exp = t->it.mmtimer.expires;
311 unsigned long e, e1; 325 while (!mmtimer_setup(x->cpu, COMPARATOR, expires,
312 struct rb_node *next; 326 &set_completion_time)) {
313 t->it.mmtimer.expires += t->it.mmtimer.incr << o; 327 int to;
314 t->it_overrun += 1 << o; 328
315 o++; 329 i++;
316 if (o > 20) { 330 expires = set_completion_time +
331 mmtimer_interval_retry_increment + (1 << i);
332 /* Calculate overruns as we go. */
333 to = ((u64)(expires - exp) / t->it.mmtimer.incr);
334 if (to) {
335 t->it_overrun += to;
336 t->it.mmtimer.expires += t->it.mmtimer.incr * to;
337 exp = t->it.mmtimer.expires;
338 }
339 if (i > 20) {
317 printk(KERN_ALERT "mmtimer: cannot reschedule timer\n"); 340 printk(KERN_ALERT "mmtimer: cannot reschedule timer\n");
318 t->it.mmtimer.clock = TIMER_OFF; 341 t->it.mmtimer.clock = TIMER_OFF;
319 n->next = rb_next(&x->list); 342 n->next = rb_next(&x->list);
@@ -321,21 +344,6 @@ restart:
321 kfree(x); 344 kfree(x);
322 goto restart; 345 goto restart;
323 } 346 }
324
325 e = t->it.mmtimer.expires;
326 next = rb_next(&x->list);
327
328 if (next == NULL)
329 continue;
330
331 e1 = rb_entry(next, struct mmtimer, list)->
332 timer->it.mmtimer.expires;
333 if (e > e1) {
334 n->next = next;
335 rb_erase(&x->list, &n->timer_head);
336 mmtimer_add_list(x);
337 goto restart;
338 }
339 } 347 }
340} 348}
341 349
@@ -371,7 +379,7 @@ static long mmtimer_ioctl(struct file *file, unsigned int cmd,
371{ 379{
372 int ret = 0; 380 int ret = 0;
373 381
374 lock_kernel(); 382 mutex_lock(&mmtimer_mutex);
375 383
376 switch (cmd) { 384 switch (cmd) {
377 case MMTIMER_GETOFFSET: /* offset of the counter */ 385 case MMTIMER_GETOFFSET: /* offset of the counter */
@@ -414,7 +422,7 @@ static long mmtimer_ioctl(struct file *file, unsigned int cmd,
414 ret = -ENOTTY; 422 ret = -ENOTTY;
415 break; 423 break;
416 } 424 }
417 unlock_kernel(); 425 mutex_unlock(&mmtimer_mutex);
418 return ret; 426 return ret;
419} 427}
420 428
@@ -481,7 +489,7 @@ static int sgi_clock_get(clockid_t clockid, struct timespec *tp)
481 return 0; 489 return 0;
482}; 490};
483 491
484static int sgi_clock_set(clockid_t clockid, struct timespec *tp) 492static int sgi_clock_set(const clockid_t clockid, const struct timespec *tp)
485{ 493{
486 494
487 u64 nsec; 495 u64 nsec;
@@ -757,15 +765,21 @@ static int sgi_timer_set(struct k_itimer *timr, int flags,
757 return err; 765 return err;
758} 766}
759 767
768static int sgi_clock_getres(const clockid_t which_clock, struct timespec *tp)
769{
770 tp->tv_sec = 0;
771 tp->tv_nsec = sgi_clock_period;
772 return 0;
773}
774
760static struct k_clock sgi_clock = { 775static struct k_clock sgi_clock = {
761 .res = 0, 776 .clock_set = sgi_clock_set,
762 .clock_set = sgi_clock_set, 777 .clock_get = sgi_clock_get,
763 .clock_get = sgi_clock_get, 778 .clock_getres = sgi_clock_getres,
764 .timer_create = sgi_timer_create, 779 .timer_create = sgi_timer_create,
765 .nsleep = do_posix_clock_nonanosleep, 780 .timer_set = sgi_timer_set,
766 .timer_set = sgi_timer_set, 781 .timer_del = sgi_timer_del,
767 .timer_del = sgi_timer_del, 782 .timer_get = sgi_timer_get
768 .timer_get = sgi_timer_get
769}; 783};
770 784
771/** 785/**
@@ -825,8 +839,8 @@ static int __init mmtimer_init(void)
825 (unsigned long) node); 839 (unsigned long) node);
826 } 840 }
827 841
828 sgi_clock_period = sgi_clock.res = NSEC_PER_SEC / sn_rtc_cycles_per_second; 842 sgi_clock_period = NSEC_PER_SEC / sn_rtc_cycles_per_second;
829 register_posix_clock(CLOCK_SGI_CYCLE, &sgi_clock); 843 posix_timers_register_clock(CLOCK_SGI_CYCLE, &sgi_clock);
830 844
831 printk(KERN_INFO "%s: v%s, %ld MHz\n", MMTIMER_DESC, MMTIMER_VERSION, 845 printk(KERN_INFO "%s: v%s, %ld MHz\n", MMTIMER_DESC, MMTIMER_VERSION,
832 sn_rtc_cycles_per_second/(unsigned long)1E6); 846 sn_rtc_cycles_per_second/(unsigned long)1E6);
diff --git a/drivers/char/moxa.c b/drivers/char/moxa.c
deleted file mode 100644
index 107b0bd58d19..000000000000
--- a/drivers/char/moxa.c
+++ /dev/null
@@ -1,2092 +0,0 @@
1/*****************************************************************************/
2/*
3 * moxa.c -- MOXA Intellio family multiport serial driver.
4 *
5 * Copyright (C) 1999-2000 Moxa Technologies (support@moxa.com).
6 * Copyright (c) 2007 Jiri Slaby <jirislaby@gmail.com>
7 *
8 * This code is loosely based on the Linux serial driver, written by
9 * Linus Torvalds, Theodore T'so and others.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 */
16
17/*
18 * MOXA Intellio Series Driver
19 * for : LINUX
20 * date : 1999/1/7
21 * version : 5.1
22 */
23
24#include <linux/module.h>
25#include <linux/types.h>
26#include <linux/mm.h>
27#include <linux/ioport.h>
28#include <linux/errno.h>
29#include <linux/firmware.h>
30#include <linux/signal.h>
31#include <linux/sched.h>
32#include <linux/timer.h>
33#include <linux/interrupt.h>
34#include <linux/tty.h>
35#include <linux/tty_flip.h>
36#include <linux/major.h>
37#include <linux/string.h>
38#include <linux/fcntl.h>
39#include <linux/ptrace.h>
40#include <linux/serial.h>
41#include <linux/tty_driver.h>
42#include <linux/delay.h>
43#include <linux/pci.h>
44#include <linux/init.h>
45#include <linux/bitops.h>
46#include <linux/slab.h>
47
48#include <asm/system.h>
49#include <asm/io.h>
50#include <asm/uaccess.h>
51
52#include "moxa.h"
53
54#define MOXA_VERSION "6.0k"
55
56#define MOXA_FW_HDRLEN 32
57
58#define MOXAMAJOR 172
59
60#define MAX_BOARDS 4 /* Don't change this value */
61#define MAX_PORTS_PER_BOARD 32 /* Don't change this value */
62#define MAX_PORTS (MAX_BOARDS * MAX_PORTS_PER_BOARD)
63
64#define MOXA_IS_320(brd) ((brd)->boardType == MOXA_BOARD_C320_ISA || \
65 (brd)->boardType == MOXA_BOARD_C320_PCI)
66
67/*
68 * Define the Moxa PCI vendor and device IDs.
69 */
70#define MOXA_BUS_TYPE_ISA 0
71#define MOXA_BUS_TYPE_PCI 1
72
73enum {
74 MOXA_BOARD_C218_PCI = 1,
75 MOXA_BOARD_C218_ISA,
76 MOXA_BOARD_C320_PCI,
77 MOXA_BOARD_C320_ISA,
78 MOXA_BOARD_CP204J,
79};
80
81static char *moxa_brdname[] =
82{
83 "C218 Turbo PCI series",
84 "C218 Turbo ISA series",
85 "C320 Turbo PCI series",
86 "C320 Turbo ISA series",
87 "CP-204J series",
88};
89
90#ifdef CONFIG_PCI
91static struct pci_device_id moxa_pcibrds[] = {
92 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C218),
93 .driver_data = MOXA_BOARD_C218_PCI },
94 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C320),
95 .driver_data = MOXA_BOARD_C320_PCI },
96 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP204J),
97 .driver_data = MOXA_BOARD_CP204J },
98 { 0 }
99};
100MODULE_DEVICE_TABLE(pci, moxa_pcibrds);
101#endif /* CONFIG_PCI */
102
103struct moxa_port;
104
105static struct moxa_board_conf {
106 int boardType;
107 int numPorts;
108 int busType;
109
110 unsigned int ready;
111
112 struct moxa_port *ports;
113
114 void __iomem *basemem;
115 void __iomem *intNdx;
116 void __iomem *intPend;
117 void __iomem *intTable;
118} moxa_boards[MAX_BOARDS];
119
120struct mxser_mstatus {
121 tcflag_t cflag;
122 int cts;
123 int dsr;
124 int ri;
125 int dcd;
126};
127
128struct moxaq_str {
129 int inq;
130 int outq;
131};
132
133struct moxa_port {
134 struct tty_port port;
135 struct moxa_board_conf *board;
136 void __iomem *tableAddr;
137
138 int type;
139 int cflag;
140 unsigned long statusflags;
141
142 u8 DCDState; /* Protected by the port lock */
143 u8 lineCtrl;
144 u8 lowChkFlag;
145};
146
147struct mon_str {
148 int tick;
149 int rxcnt[MAX_PORTS];
150 int txcnt[MAX_PORTS];
151};
152
153/* statusflags */
154#define TXSTOPPED 1
155#define LOWWAIT 2
156#define EMPTYWAIT 3
157
158#define SERIAL_DO_RESTART
159
160#define WAKEUP_CHARS 256
161
162static int ttymajor = MOXAMAJOR;
163static struct mon_str moxaLog;
164static unsigned int moxaFuncTout = HZ / 2;
165static unsigned int moxaLowWaterChk;
166static DEFINE_MUTEX(moxa_openlock);
167static DEFINE_SPINLOCK(moxa_lock);
168
169static unsigned long baseaddr[MAX_BOARDS];
170static unsigned int type[MAX_BOARDS];
171static unsigned int numports[MAX_BOARDS];
172
173MODULE_AUTHOR("William Chen");
174MODULE_DESCRIPTION("MOXA Intellio Family Multiport Board Device Driver");
175MODULE_LICENSE("GPL");
176MODULE_FIRMWARE("c218tunx.cod");
177MODULE_FIRMWARE("cp204unx.cod");
178MODULE_FIRMWARE("c320tunx.cod");
179
180module_param_array(type, uint, NULL, 0);
181MODULE_PARM_DESC(type, "card type: C218=2, C320=4");
182module_param_array(baseaddr, ulong, NULL, 0);
183MODULE_PARM_DESC(baseaddr, "base address");
184module_param_array(numports, uint, NULL, 0);
185MODULE_PARM_DESC(numports, "numports (ignored for C218)");
186
187module_param(ttymajor, int, 0);
188
189/*
190 * static functions:
191 */
192static int moxa_open(struct tty_struct *, struct file *);
193static void moxa_close(struct tty_struct *, struct file *);
194static int moxa_write(struct tty_struct *, const unsigned char *, int);
195static int moxa_write_room(struct tty_struct *);
196static void moxa_flush_buffer(struct tty_struct *);
197static int moxa_chars_in_buffer(struct tty_struct *);
198static void moxa_set_termios(struct tty_struct *, struct ktermios *);
199static void moxa_stop(struct tty_struct *);
200static void moxa_start(struct tty_struct *);
201static void moxa_hangup(struct tty_struct *);
202static int moxa_tiocmget(struct tty_struct *tty, struct file *file);
203static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
204 unsigned int set, unsigned int clear);
205static void moxa_poll(unsigned long);
206static void moxa_set_tty_param(struct tty_struct *, struct ktermios *);
207static void moxa_shutdown(struct tty_port *);
208static int moxa_carrier_raised(struct tty_port *);
209static void moxa_dtr_rts(struct tty_port *, int);
210/*
211 * moxa board interface functions:
212 */
213static void MoxaPortEnable(struct moxa_port *);
214static void MoxaPortDisable(struct moxa_port *);
215static int MoxaPortSetTermio(struct moxa_port *, struct ktermios *, speed_t);
216static int MoxaPortGetLineOut(struct moxa_port *, int *, int *);
217static void MoxaPortLineCtrl(struct moxa_port *, int, int);
218static void MoxaPortFlowCtrl(struct moxa_port *, int, int, int, int, int);
219static int MoxaPortLineStatus(struct moxa_port *);
220static void MoxaPortFlushData(struct moxa_port *, int);
221static int MoxaPortWriteData(struct tty_struct *, const unsigned char *, int);
222static int MoxaPortReadData(struct moxa_port *);
223static int MoxaPortTxQueue(struct moxa_port *);
224static int MoxaPortRxQueue(struct moxa_port *);
225static int MoxaPortTxFree(struct moxa_port *);
226static void MoxaPortTxDisable(struct moxa_port *);
227static void MoxaPortTxEnable(struct moxa_port *);
228static int moxa_get_serial_info(struct moxa_port *, struct serial_struct __user *);
229static int moxa_set_serial_info(struct moxa_port *, struct serial_struct __user *);
230static void MoxaSetFifo(struct moxa_port *port, int enable);
231
232/*
233 * I/O functions
234 */
235
236static DEFINE_SPINLOCK(moxafunc_lock);
237
238static void moxa_wait_finish(void __iomem *ofsAddr)
239{
240 unsigned long end = jiffies + moxaFuncTout;
241
242 while (readw(ofsAddr + FuncCode) != 0)
243 if (time_after(jiffies, end))
244 return;
245 if (readw(ofsAddr + FuncCode) != 0 && printk_ratelimit())
246 printk(KERN_WARNING "moxa function expired\n");
247}
248
249static void moxafunc(void __iomem *ofsAddr, u16 cmd, u16 arg)
250{
251 unsigned long flags;
252 spin_lock_irqsave(&moxafunc_lock, flags);
253 writew(arg, ofsAddr + FuncArg);
254 writew(cmd, ofsAddr + FuncCode);
255 moxa_wait_finish(ofsAddr);
256 spin_unlock_irqrestore(&moxafunc_lock, flags);
257}
258
259static int moxafuncret(void __iomem *ofsAddr, u16 cmd, u16 arg)
260{
261 unsigned long flags;
262 u16 ret;
263 spin_lock_irqsave(&moxafunc_lock, flags);
264 writew(arg, ofsAddr + FuncArg);
265 writew(cmd, ofsAddr + FuncCode);
266 moxa_wait_finish(ofsAddr);
267 ret = readw(ofsAddr + FuncArg);
268 spin_unlock_irqrestore(&moxafunc_lock, flags);
269 return ret;
270}
271
272static void moxa_low_water_check(void __iomem *ofsAddr)
273{
274 u16 rptr, wptr, mask, len;
275
276 if (readb(ofsAddr + FlagStat) & Xoff_state) {
277 rptr = readw(ofsAddr + RXrptr);
278 wptr = readw(ofsAddr + RXwptr);
279 mask = readw(ofsAddr + RX_mask);
280 len = (wptr - rptr) & mask;
281 if (len <= Low_water)
282 moxafunc(ofsAddr, FC_SendXon, 0);
283 }
284}
285
286/*
287 * TTY operations
288 */
289
290static int moxa_ioctl(struct tty_struct *tty, struct file *file,
291 unsigned int cmd, unsigned long arg)
292{
293 struct moxa_port *ch = tty->driver_data;
294 void __user *argp = (void __user *)arg;
295 int status, ret = 0;
296
297 if (tty->index == MAX_PORTS) {
298 if (cmd != MOXA_GETDATACOUNT && cmd != MOXA_GET_IOQUEUE &&
299 cmd != MOXA_GETMSTATUS)
300 return -EINVAL;
301 } else if (!ch)
302 return -ENODEV;
303
304 switch (cmd) {
305 case MOXA_GETDATACOUNT:
306 moxaLog.tick = jiffies;
307 if (copy_to_user(argp, &moxaLog, sizeof(moxaLog)))
308 ret = -EFAULT;
309 break;
310 case MOXA_FLUSH_QUEUE:
311 MoxaPortFlushData(ch, arg);
312 break;
313 case MOXA_GET_IOQUEUE: {
314 struct moxaq_str __user *argm = argp;
315 struct moxaq_str tmp;
316 struct moxa_port *p;
317 unsigned int i, j;
318
319 for (i = 0; i < MAX_BOARDS; i++) {
320 p = moxa_boards[i].ports;
321 for (j = 0; j < MAX_PORTS_PER_BOARD; j++, p++, argm++) {
322 memset(&tmp, 0, sizeof(tmp));
323 spin_lock_bh(&moxa_lock);
324 if (moxa_boards[i].ready) {
325 tmp.inq = MoxaPortRxQueue(p);
326 tmp.outq = MoxaPortTxQueue(p);
327 }
328 spin_unlock_bh(&moxa_lock);
329 if (copy_to_user(argm, &tmp, sizeof(tmp)))
330 return -EFAULT;
331 }
332 }
333 break;
334 } case MOXA_GET_OQUEUE:
335 status = MoxaPortTxQueue(ch);
336 ret = put_user(status, (unsigned long __user *)argp);
337 break;
338 case MOXA_GET_IQUEUE:
339 status = MoxaPortRxQueue(ch);
340 ret = put_user(status, (unsigned long __user *)argp);
341 break;
342 case MOXA_GETMSTATUS: {
343 struct mxser_mstatus __user *argm = argp;
344 struct mxser_mstatus tmp;
345 struct moxa_port *p;
346 unsigned int i, j;
347
348 for (i = 0; i < MAX_BOARDS; i++) {
349 p = moxa_boards[i].ports;
350 for (j = 0; j < MAX_PORTS_PER_BOARD; j++, p++, argm++) {
351 struct tty_struct *ttyp;
352 memset(&tmp, 0, sizeof(tmp));
353 spin_lock_bh(&moxa_lock);
354 if (!moxa_boards[i].ready) {
355 spin_unlock_bh(&moxa_lock);
356 goto copy;
357 }
358
359 status = MoxaPortLineStatus(p);
360 spin_unlock_bh(&moxa_lock);
361
362 if (status & 1)
363 tmp.cts = 1;
364 if (status & 2)
365 tmp.dsr = 1;
366 if (status & 4)
367 tmp.dcd = 1;
368
369 ttyp = tty_port_tty_get(&p->port);
370 if (!ttyp || !ttyp->termios)
371 tmp.cflag = p->cflag;
372 else
373 tmp.cflag = ttyp->termios->c_cflag;
374 tty_kref_put(tty);
375copy:
376 if (copy_to_user(argm, &tmp, sizeof(tmp)))
377 return -EFAULT;
378 }
379 }
380 break;
381 }
382 case TIOCGSERIAL:
383 mutex_lock(&ch->port.mutex);
384 ret = moxa_get_serial_info(ch, argp);
385 mutex_unlock(&ch->port.mutex);
386 break;
387 case TIOCSSERIAL:
388 mutex_lock(&ch->port.mutex);
389 ret = moxa_set_serial_info(ch, argp);
390 mutex_unlock(&ch->port.mutex);
391 break;
392 default:
393 ret = -ENOIOCTLCMD;
394 }
395 return ret;
396}
397
398static int moxa_break_ctl(struct tty_struct *tty, int state)
399{
400 struct moxa_port *port = tty->driver_data;
401
402 moxafunc(port->tableAddr, state ? FC_SendBreak : FC_StopBreak,
403 Magic_code);
404 return 0;
405}
406
407static const struct tty_operations moxa_ops = {
408 .open = moxa_open,
409 .close = moxa_close,
410 .write = moxa_write,
411 .write_room = moxa_write_room,
412 .flush_buffer = moxa_flush_buffer,
413 .chars_in_buffer = moxa_chars_in_buffer,
414 .ioctl = moxa_ioctl,
415 .set_termios = moxa_set_termios,
416 .stop = moxa_stop,
417 .start = moxa_start,
418 .hangup = moxa_hangup,
419 .break_ctl = moxa_break_ctl,
420 .tiocmget = moxa_tiocmget,
421 .tiocmset = moxa_tiocmset,
422};
423
424static const struct tty_port_operations moxa_port_ops = {
425 .carrier_raised = moxa_carrier_raised,
426 .dtr_rts = moxa_dtr_rts,
427 .shutdown = moxa_shutdown,
428};
429
430static struct tty_driver *moxaDriver;
431static DEFINE_TIMER(moxaTimer, moxa_poll, 0, 0);
432
433/*
434 * HW init
435 */
436
437static int moxa_check_fw_model(struct moxa_board_conf *brd, u8 model)
438{
439 switch (brd->boardType) {
440 case MOXA_BOARD_C218_ISA:
441 case MOXA_BOARD_C218_PCI:
442 if (model != 1)
443 goto err;
444 break;
445 case MOXA_BOARD_CP204J:
446 if (model != 3)
447 goto err;
448 break;
449 default:
450 if (model != 2)
451 goto err;
452 break;
453 }
454 return 0;
455err:
456 return -EINVAL;
457}
458
459static int moxa_check_fw(const void *ptr)
460{
461 const __le16 *lptr = ptr;
462
463 if (*lptr != cpu_to_le16(0x7980))
464 return -EINVAL;
465
466 return 0;
467}
468
469static int moxa_load_bios(struct moxa_board_conf *brd, const u8 *buf,
470 size_t len)
471{
472 void __iomem *baseAddr = brd->basemem;
473 u16 tmp;
474
475 writeb(HW_reset, baseAddr + Control_reg); /* reset */
476 msleep(10);
477 memset_io(baseAddr, 0, 4096);
478 memcpy_toio(baseAddr, buf, len); /* download BIOS */
479 writeb(0, baseAddr + Control_reg); /* restart */
480
481 msleep(2000);
482
483 switch (brd->boardType) {
484 case MOXA_BOARD_C218_ISA:
485 case MOXA_BOARD_C218_PCI:
486 tmp = readw(baseAddr + C218_key);
487 if (tmp != C218_KeyCode)
488 goto err;
489 break;
490 case MOXA_BOARD_CP204J:
491 tmp = readw(baseAddr + C218_key);
492 if (tmp != CP204J_KeyCode)
493 goto err;
494 break;
495 default:
496 tmp = readw(baseAddr + C320_key);
497 if (tmp != C320_KeyCode)
498 goto err;
499 tmp = readw(baseAddr + C320_status);
500 if (tmp != STS_init) {
501 printk(KERN_ERR "MOXA: bios upload failed -- CPU/Basic "
502 "module not found\n");
503 return -EIO;
504 }
505 break;
506 }
507
508 return 0;
509err:
510 printk(KERN_ERR "MOXA: bios upload failed -- board not found\n");
511 return -EIO;
512}
513
514static int moxa_load_320b(struct moxa_board_conf *brd, const u8 *ptr,
515 size_t len)
516{
517 void __iomem *baseAddr = brd->basemem;
518
519 if (len < 7168) {
520 printk(KERN_ERR "MOXA: invalid 320 bios -- too short\n");
521 return -EINVAL;
522 }
523
524 writew(len - 7168 - 2, baseAddr + C320bapi_len);
525 writeb(1, baseAddr + Control_reg); /* Select Page 1 */
526 memcpy_toio(baseAddr + DynPage_addr, ptr, 7168);
527 writeb(2, baseAddr + Control_reg); /* Select Page 2 */
528 memcpy_toio(baseAddr + DynPage_addr, ptr + 7168, len - 7168);
529
530 return 0;
531}
532
533static int moxa_real_load_code(struct moxa_board_conf *brd, const void *ptr,
534 size_t len)
535{
536 void __iomem *baseAddr = brd->basemem;
537 const __le16 *uptr = ptr;
538 size_t wlen, len2, j;
539 unsigned long key, loadbuf, loadlen, checksum, checksum_ok;
540 unsigned int i, retry;
541 u16 usum, keycode;
542
543 keycode = (brd->boardType == MOXA_BOARD_CP204J) ? CP204J_KeyCode :
544 C218_KeyCode;
545
546 switch (brd->boardType) {
547 case MOXA_BOARD_CP204J:
548 case MOXA_BOARD_C218_ISA:
549 case MOXA_BOARD_C218_PCI:
550 key = C218_key;
551 loadbuf = C218_LoadBuf;
552 loadlen = C218DLoad_len;
553 checksum = C218check_sum;
554 checksum_ok = C218chksum_ok;
555 break;
556 default:
557 key = C320_key;
558 keycode = C320_KeyCode;
559 loadbuf = C320_LoadBuf;
560 loadlen = C320DLoad_len;
561 checksum = C320check_sum;
562 checksum_ok = C320chksum_ok;
563 break;
564 }
565
566 usum = 0;
567 wlen = len >> 1;
568 for (i = 0; i < wlen; i++)
569 usum += le16_to_cpu(uptr[i]);
570 retry = 0;
571 do {
572 wlen = len >> 1;
573 j = 0;
574 while (wlen) {
575 len2 = (wlen > 2048) ? 2048 : wlen;
576 wlen -= len2;
577 memcpy_toio(baseAddr + loadbuf, ptr + j, len2 << 1);
578 j += len2 << 1;
579
580 writew(len2, baseAddr + loadlen);
581 writew(0, baseAddr + key);
582 for (i = 0; i < 100; i++) {
583 if (readw(baseAddr + key) == keycode)
584 break;
585 msleep(10);
586 }
587 if (readw(baseAddr + key) != keycode)
588 return -EIO;
589 }
590 writew(0, baseAddr + loadlen);
591 writew(usum, baseAddr + checksum);
592 writew(0, baseAddr + key);
593 for (i = 0; i < 100; i++) {
594 if (readw(baseAddr + key) == keycode)
595 break;
596 msleep(10);
597 }
598 retry++;
599 } while ((readb(baseAddr + checksum_ok) != 1) && (retry < 3));
600 if (readb(baseAddr + checksum_ok) != 1)
601 return -EIO;
602
603 writew(0, baseAddr + key);
604 for (i = 0; i < 600; i++) {
605 if (readw(baseAddr + Magic_no) == Magic_code)
606 break;
607 msleep(10);
608 }
609 if (readw(baseAddr + Magic_no) != Magic_code)
610 return -EIO;
611
612 if (MOXA_IS_320(brd)) {
613 if (brd->busType == MOXA_BUS_TYPE_PCI) { /* ASIC board */
614 writew(0x3800, baseAddr + TMS320_PORT1);
615 writew(0x3900, baseAddr + TMS320_PORT2);
616 writew(28499, baseAddr + TMS320_CLOCK);
617 } else {
618 writew(0x3200, baseAddr + TMS320_PORT1);
619 writew(0x3400, baseAddr + TMS320_PORT2);
620 writew(19999, baseAddr + TMS320_CLOCK);
621 }
622 }
623 writew(1, baseAddr + Disable_IRQ);
624 writew(0, baseAddr + Magic_no);
625 for (i = 0; i < 500; i++) {
626 if (readw(baseAddr + Magic_no) == Magic_code)
627 break;
628 msleep(10);
629 }
630 if (readw(baseAddr + Magic_no) != Magic_code)
631 return -EIO;
632
633 if (MOXA_IS_320(brd)) {
634 j = readw(baseAddr + Module_cnt);
635 if (j <= 0)
636 return -EIO;
637 brd->numPorts = j * 8;
638 writew(j, baseAddr + Module_no);
639 writew(0, baseAddr + Magic_no);
640 for (i = 0; i < 600; i++) {
641 if (readw(baseAddr + Magic_no) == Magic_code)
642 break;
643 msleep(10);
644 }
645 if (readw(baseAddr + Magic_no) != Magic_code)
646 return -EIO;
647 }
648 brd->intNdx = baseAddr + IRQindex;
649 brd->intPend = baseAddr + IRQpending;
650 brd->intTable = baseAddr + IRQtable;
651
652 return 0;
653}
654
655static int moxa_load_code(struct moxa_board_conf *brd, const void *ptr,
656 size_t len)
657{
658 void __iomem *ofsAddr, *baseAddr = brd->basemem;
659 struct moxa_port *port;
660 int retval, i;
661
662 if (len % 2) {
663 printk(KERN_ERR "MOXA: bios length is not even\n");
664 return -EINVAL;
665 }
666
667 retval = moxa_real_load_code(brd, ptr, len); /* may change numPorts */
668 if (retval)
669 return retval;
670
671 switch (brd->boardType) {
672 case MOXA_BOARD_C218_ISA:
673 case MOXA_BOARD_C218_PCI:
674 case MOXA_BOARD_CP204J:
675 port = brd->ports;
676 for (i = 0; i < brd->numPorts; i++, port++) {
677 port->board = brd;
678 port->DCDState = 0;
679 port->tableAddr = baseAddr + Extern_table +
680 Extern_size * i;
681 ofsAddr = port->tableAddr;
682 writew(C218rx_mask, ofsAddr + RX_mask);
683 writew(C218tx_mask, ofsAddr + TX_mask);
684 writew(C218rx_spage + i * C218buf_pageno, ofsAddr + Page_rxb);
685 writew(readw(ofsAddr + Page_rxb) + C218rx_pageno, ofsAddr + EndPage_rxb);
686
687 writew(C218tx_spage + i * C218buf_pageno, ofsAddr + Page_txb);
688 writew(readw(ofsAddr + Page_txb) + C218tx_pageno, ofsAddr + EndPage_txb);
689
690 }
691 break;
692 default:
693 port = brd->ports;
694 for (i = 0; i < brd->numPorts; i++, port++) {
695 port->board = brd;
696 port->DCDState = 0;
697 port->tableAddr = baseAddr + Extern_table +
698 Extern_size * i;
699 ofsAddr = port->tableAddr;
700 switch (brd->numPorts) {
701 case 8:
702 writew(C320p8rx_mask, ofsAddr + RX_mask);
703 writew(C320p8tx_mask, ofsAddr + TX_mask);
704 writew(C320p8rx_spage + i * C320p8buf_pgno, ofsAddr + Page_rxb);
705 writew(readw(ofsAddr + Page_rxb) + C320p8rx_pgno, ofsAddr + EndPage_rxb);
706 writew(C320p8tx_spage + i * C320p8buf_pgno, ofsAddr + Page_txb);
707 writew(readw(ofsAddr + Page_txb) + C320p8tx_pgno, ofsAddr + EndPage_txb);
708
709 break;
710 case 16:
711 writew(C320p16rx_mask, ofsAddr + RX_mask);
712 writew(C320p16tx_mask, ofsAddr + TX_mask);
713 writew(C320p16rx_spage + i * C320p16buf_pgno, ofsAddr + Page_rxb);
714 writew(readw(ofsAddr + Page_rxb) + C320p16rx_pgno, ofsAddr + EndPage_rxb);
715 writew(C320p16tx_spage + i * C320p16buf_pgno, ofsAddr + Page_txb);
716 writew(readw(ofsAddr + Page_txb) + C320p16tx_pgno, ofsAddr + EndPage_txb);
717 break;
718
719 case 24:
720 writew(C320p24rx_mask, ofsAddr + RX_mask);
721 writew(C320p24tx_mask, ofsAddr + TX_mask);
722 writew(C320p24rx_spage + i * C320p24buf_pgno, ofsAddr + Page_rxb);
723 writew(readw(ofsAddr + Page_rxb) + C320p24rx_pgno, ofsAddr + EndPage_rxb);
724 writew(C320p24tx_spage + i * C320p24buf_pgno, ofsAddr + Page_txb);
725 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
726 break;
727 case 32:
728 writew(C320p32rx_mask, ofsAddr + RX_mask);
729 writew(C320p32tx_mask, ofsAddr + TX_mask);
730 writew(C320p32tx_ofs, ofsAddr + Ofs_txb);
731 writew(C320p32rx_spage + i * C320p32buf_pgno, ofsAddr + Page_rxb);
732 writew(readb(ofsAddr + Page_rxb), ofsAddr + EndPage_rxb);
733 writew(C320p32tx_spage + i * C320p32buf_pgno, ofsAddr + Page_txb);
734 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
735 break;
736 }
737 }
738 break;
739 }
740 return 0;
741}
742
743static int moxa_load_fw(struct moxa_board_conf *brd, const struct firmware *fw)
744{
745 const void *ptr = fw->data;
746 char rsn[64];
747 u16 lens[5];
748 size_t len;
749 unsigned int a, lenp, lencnt;
750 int ret = -EINVAL;
751 struct {
752 __le32 magic; /* 0x34303430 */
753 u8 reserved1[2];
754 u8 type; /* UNIX = 3 */
755 u8 model; /* C218T=1, C320T=2, CP204=3 */
756 u8 reserved2[8];
757 __le16 len[5];
758 } const *hdr = ptr;
759
760 BUILD_BUG_ON(ARRAY_SIZE(hdr->len) != ARRAY_SIZE(lens));
761
762 if (fw->size < MOXA_FW_HDRLEN) {
763 strcpy(rsn, "too short (even header won't fit)");
764 goto err;
765 }
766 if (hdr->magic != cpu_to_le32(0x30343034)) {
767 sprintf(rsn, "bad magic: %.8x", le32_to_cpu(hdr->magic));
768 goto err;
769 }
770 if (hdr->type != 3) {
771 sprintf(rsn, "not for linux, type is %u", hdr->type);
772 goto err;
773 }
774 if (moxa_check_fw_model(brd, hdr->model)) {
775 sprintf(rsn, "not for this card, model is %u", hdr->model);
776 goto err;
777 }
778
779 len = MOXA_FW_HDRLEN;
780 lencnt = hdr->model == 2 ? 5 : 3;
781 for (a = 0; a < ARRAY_SIZE(lens); a++) {
782 lens[a] = le16_to_cpu(hdr->len[a]);
783 if (lens[a] && len + lens[a] <= fw->size &&
784 moxa_check_fw(&fw->data[len]))
785 printk(KERN_WARNING "MOXA firmware: unexpected input "
786 "at offset %u, but going on\n", (u32)len);
787 if (!lens[a] && a < lencnt) {
788 sprintf(rsn, "too few entries in fw file");
789 goto err;
790 }
791 len += lens[a];
792 }
793
794 if (len != fw->size) {
795 sprintf(rsn, "bad length: %u (should be %u)", (u32)fw->size,
796 (u32)len);
797 goto err;
798 }
799
800 ptr += MOXA_FW_HDRLEN;
801 lenp = 0; /* bios */
802
803 strcpy(rsn, "read above");
804
805 ret = moxa_load_bios(brd, ptr, lens[lenp]);
806 if (ret)
807 goto err;
808
809 /* we skip the tty section (lens[1]), since we don't need it */
810 ptr += lens[lenp] + lens[lenp + 1];
811 lenp += 2; /* comm */
812
813 if (hdr->model == 2) {
814 ret = moxa_load_320b(brd, ptr, lens[lenp]);
815 if (ret)
816 goto err;
817 /* skip another tty */
818 ptr += lens[lenp] + lens[lenp + 1];
819 lenp += 2;
820 }
821
822 ret = moxa_load_code(brd, ptr, lens[lenp]);
823 if (ret)
824 goto err;
825
826 return 0;
827err:
828 printk(KERN_ERR "firmware failed to load, reason: %s\n", rsn);
829 return ret;
830}
831
832static int moxa_init_board(struct moxa_board_conf *brd, struct device *dev)
833{
834 const struct firmware *fw;
835 const char *file;
836 struct moxa_port *p;
837 unsigned int i;
838 int ret;
839
840 brd->ports = kcalloc(MAX_PORTS_PER_BOARD, sizeof(*brd->ports),
841 GFP_KERNEL);
842 if (brd->ports == NULL) {
843 printk(KERN_ERR "cannot allocate memory for ports\n");
844 ret = -ENOMEM;
845 goto err;
846 }
847
848 for (i = 0, p = brd->ports; i < MAX_PORTS_PER_BOARD; i++, p++) {
849 tty_port_init(&p->port);
850 p->port.ops = &moxa_port_ops;
851 p->type = PORT_16550A;
852 p->cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
853 }
854
855 switch (brd->boardType) {
856 case MOXA_BOARD_C218_ISA:
857 case MOXA_BOARD_C218_PCI:
858 file = "c218tunx.cod";
859 break;
860 case MOXA_BOARD_CP204J:
861 file = "cp204unx.cod";
862 break;
863 default:
864 file = "c320tunx.cod";
865 break;
866 }
867
868 ret = request_firmware(&fw, file, dev);
869 if (ret) {
870 printk(KERN_ERR "MOXA: request_firmware failed. Make sure "
871 "you've placed '%s' file into your firmware "
872 "loader directory (e.g. /lib/firmware)\n",
873 file);
874 goto err_free;
875 }
876
877 ret = moxa_load_fw(brd, fw);
878
879 release_firmware(fw);
880
881 if (ret)
882 goto err_free;
883
884 spin_lock_bh(&moxa_lock);
885 brd->ready = 1;
886 if (!timer_pending(&moxaTimer))
887 mod_timer(&moxaTimer, jiffies + HZ / 50);
888 spin_unlock_bh(&moxa_lock);
889
890 return 0;
891err_free:
892 kfree(brd->ports);
893err:
894 return ret;
895}
896
897static void moxa_board_deinit(struct moxa_board_conf *brd)
898{
899 unsigned int a, opened;
900
901 mutex_lock(&moxa_openlock);
902 spin_lock_bh(&moxa_lock);
903 brd->ready = 0;
904 spin_unlock_bh(&moxa_lock);
905
906 /* pci hot-un-plug support */
907 for (a = 0; a < brd->numPorts; a++)
908 if (brd->ports[a].port.flags & ASYNC_INITIALIZED) {
909 struct tty_struct *tty = tty_port_tty_get(
910 &brd->ports[a].port);
911 if (tty) {
912 tty_hangup(tty);
913 tty_kref_put(tty);
914 }
915 }
916 while (1) {
917 opened = 0;
918 for (a = 0; a < brd->numPorts; a++)
919 if (brd->ports[a].port.flags & ASYNC_INITIALIZED)
920 opened++;
921 mutex_unlock(&moxa_openlock);
922 if (!opened)
923 break;
924 msleep(50);
925 mutex_lock(&moxa_openlock);
926 }
927
928 iounmap(brd->basemem);
929 brd->basemem = NULL;
930 kfree(brd->ports);
931}
932
933#ifdef CONFIG_PCI
934static int __devinit moxa_pci_probe(struct pci_dev *pdev,
935 const struct pci_device_id *ent)
936{
937 struct moxa_board_conf *board;
938 unsigned int i;
939 int board_type = ent->driver_data;
940 int retval;
941
942 retval = pci_enable_device(pdev);
943 if (retval) {
944 dev_err(&pdev->dev, "can't enable pci device\n");
945 goto err;
946 }
947
948 for (i = 0; i < MAX_BOARDS; i++)
949 if (moxa_boards[i].basemem == NULL)
950 break;
951
952 retval = -ENODEV;
953 if (i >= MAX_BOARDS) {
954 dev_warn(&pdev->dev, "more than %u MOXA Intellio family boards "
955 "found. Board is ignored.\n", MAX_BOARDS);
956 goto err;
957 }
958
959 board = &moxa_boards[i];
960
961 retval = pci_request_region(pdev, 2, "moxa-base");
962 if (retval) {
963 dev_err(&pdev->dev, "can't request pci region 2\n");
964 goto err;
965 }
966
967 board->basemem = ioremap_nocache(pci_resource_start(pdev, 2), 0x4000);
968 if (board->basemem == NULL) {
969 dev_err(&pdev->dev, "can't remap io space 2\n");
970 goto err_reg;
971 }
972
973 board->boardType = board_type;
974 switch (board_type) {
975 case MOXA_BOARD_C218_ISA:
976 case MOXA_BOARD_C218_PCI:
977 board->numPorts = 8;
978 break;
979
980 case MOXA_BOARD_CP204J:
981 board->numPorts = 4;
982 break;
983 default:
984 board->numPorts = 0;
985 break;
986 }
987 board->busType = MOXA_BUS_TYPE_PCI;
988
989 retval = moxa_init_board(board, &pdev->dev);
990 if (retval)
991 goto err_base;
992
993 pci_set_drvdata(pdev, board);
994
995 dev_info(&pdev->dev, "board '%s' ready (%u ports, firmware loaded)\n",
996 moxa_brdname[board_type - 1], board->numPorts);
997
998 return 0;
999err_base:
1000 iounmap(board->basemem);
1001 board->basemem = NULL;
1002err_reg:
1003 pci_release_region(pdev, 2);
1004err:
1005 return retval;
1006}
1007
1008static void __devexit moxa_pci_remove(struct pci_dev *pdev)
1009{
1010 struct moxa_board_conf *brd = pci_get_drvdata(pdev);
1011
1012 moxa_board_deinit(brd);
1013
1014 pci_release_region(pdev, 2);
1015}
1016
1017static struct pci_driver moxa_pci_driver = {
1018 .name = "moxa",
1019 .id_table = moxa_pcibrds,
1020 .probe = moxa_pci_probe,
1021 .remove = __devexit_p(moxa_pci_remove)
1022};
1023#endif /* CONFIG_PCI */
1024
1025static int __init moxa_init(void)
1026{
1027 unsigned int isabrds = 0;
1028 int retval = 0;
1029 struct moxa_board_conf *brd = moxa_boards;
1030 unsigned int i;
1031
1032 printk(KERN_INFO "MOXA Intellio family driver version %s\n",
1033 MOXA_VERSION);
1034 moxaDriver = alloc_tty_driver(MAX_PORTS + 1);
1035 if (!moxaDriver)
1036 return -ENOMEM;
1037
1038 moxaDriver->owner = THIS_MODULE;
1039 moxaDriver->name = "ttyMX";
1040 moxaDriver->major = ttymajor;
1041 moxaDriver->minor_start = 0;
1042 moxaDriver->type = TTY_DRIVER_TYPE_SERIAL;
1043 moxaDriver->subtype = SERIAL_TYPE_NORMAL;
1044 moxaDriver->init_termios = tty_std_termios;
1045 moxaDriver->init_termios.c_cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
1046 moxaDriver->init_termios.c_ispeed = 9600;
1047 moxaDriver->init_termios.c_ospeed = 9600;
1048 moxaDriver->flags = TTY_DRIVER_REAL_RAW;
1049 tty_set_operations(moxaDriver, &moxa_ops);
1050
1051 if (tty_register_driver(moxaDriver)) {
1052 printk(KERN_ERR "can't register MOXA Smartio tty driver!\n");
1053 put_tty_driver(moxaDriver);
1054 return -1;
1055 }
1056
1057 /* Find the boards defined from module args. */
1058
1059 for (i = 0; i < MAX_BOARDS; i++) {
1060 if (!baseaddr[i])
1061 break;
1062 if (type[i] == MOXA_BOARD_C218_ISA ||
1063 type[i] == MOXA_BOARD_C320_ISA) {
1064 pr_debug("Moxa board %2d: %s board(baseAddr=%lx)\n",
1065 isabrds + 1, moxa_brdname[type[i] - 1],
1066 baseaddr[i]);
1067 brd->boardType = type[i];
1068 brd->numPorts = type[i] == MOXA_BOARD_C218_ISA ? 8 :
1069 numports[i];
1070 brd->busType = MOXA_BUS_TYPE_ISA;
1071 brd->basemem = ioremap_nocache(baseaddr[i], 0x4000);
1072 if (!brd->basemem) {
1073 printk(KERN_ERR "MOXA: can't remap %lx\n",
1074 baseaddr[i]);
1075 continue;
1076 }
1077 if (moxa_init_board(brd, NULL)) {
1078 iounmap(brd->basemem);
1079 brd->basemem = NULL;
1080 continue;
1081 }
1082
1083 printk(KERN_INFO "MOXA isa board found at 0x%.8lu and "
1084 "ready (%u ports, firmware loaded)\n",
1085 baseaddr[i], brd->numPorts);
1086
1087 brd++;
1088 isabrds++;
1089 }
1090 }
1091
1092#ifdef CONFIG_PCI
1093 retval = pci_register_driver(&moxa_pci_driver);
1094 if (retval) {
1095 printk(KERN_ERR "Can't register MOXA pci driver!\n");
1096 if (isabrds)
1097 retval = 0;
1098 }
1099#endif
1100
1101 return retval;
1102}
1103
1104static void __exit moxa_exit(void)
1105{
1106 unsigned int i;
1107
1108#ifdef CONFIG_PCI
1109 pci_unregister_driver(&moxa_pci_driver);
1110#endif
1111
1112 for (i = 0; i < MAX_BOARDS; i++) /* ISA boards */
1113 if (moxa_boards[i].ready)
1114 moxa_board_deinit(&moxa_boards[i]);
1115
1116 del_timer_sync(&moxaTimer);
1117
1118 if (tty_unregister_driver(moxaDriver))
1119 printk(KERN_ERR "Couldn't unregister MOXA Intellio family "
1120 "serial driver\n");
1121 put_tty_driver(moxaDriver);
1122}
1123
1124module_init(moxa_init);
1125module_exit(moxa_exit);
1126
1127static void moxa_shutdown(struct tty_port *port)
1128{
1129 struct moxa_port *ch = container_of(port, struct moxa_port, port);
1130 MoxaPortDisable(ch);
1131 MoxaPortFlushData(ch, 2);
1132 clear_bit(ASYNCB_NORMAL_ACTIVE, &port->flags);
1133}
1134
1135static int moxa_carrier_raised(struct tty_port *port)
1136{
1137 struct moxa_port *ch = container_of(port, struct moxa_port, port);
1138 int dcd;
1139
1140 spin_lock_irq(&port->lock);
1141 dcd = ch->DCDState;
1142 spin_unlock_irq(&port->lock);
1143 return dcd;
1144}
1145
1146static void moxa_dtr_rts(struct tty_port *port, int onoff)
1147{
1148 struct moxa_port *ch = container_of(port, struct moxa_port, port);
1149 MoxaPortLineCtrl(ch, onoff, onoff);
1150}
1151
1152
1153static int moxa_open(struct tty_struct *tty, struct file *filp)
1154{
1155 struct moxa_board_conf *brd;
1156 struct moxa_port *ch;
1157 int port;
1158 int retval;
1159
1160 port = tty->index;
1161 if (port == MAX_PORTS) {
1162 return capable(CAP_SYS_ADMIN) ? 0 : -EPERM;
1163 }
1164 if (mutex_lock_interruptible(&moxa_openlock))
1165 return -ERESTARTSYS;
1166 brd = &moxa_boards[port / MAX_PORTS_PER_BOARD];
1167 if (!brd->ready) {
1168 mutex_unlock(&moxa_openlock);
1169 return -ENODEV;
1170 }
1171
1172 if (port % MAX_PORTS_PER_BOARD >= brd->numPorts) {
1173 mutex_unlock(&moxa_openlock);
1174 return -ENODEV;
1175 }
1176
1177 ch = &brd->ports[port % MAX_PORTS_PER_BOARD];
1178 ch->port.count++;
1179 tty->driver_data = ch;
1180 tty_port_tty_set(&ch->port, tty);
1181 mutex_lock(&ch->port.mutex);
1182 if (!(ch->port.flags & ASYNC_INITIALIZED)) {
1183 ch->statusflags = 0;
1184 moxa_set_tty_param(tty, tty->termios);
1185 MoxaPortLineCtrl(ch, 1, 1);
1186 MoxaPortEnable(ch);
1187 MoxaSetFifo(ch, ch->type == PORT_16550A);
1188 ch->port.flags |= ASYNC_INITIALIZED;
1189 }
1190 mutex_unlock(&ch->port.mutex);
1191 mutex_unlock(&moxa_openlock);
1192
1193 retval = tty_port_block_til_ready(&ch->port, tty, filp);
1194 if (retval == 0)
1195 set_bit(ASYNCB_NORMAL_ACTIVE, &ch->port.flags);
1196 return retval;
1197}
1198
1199static void moxa_close(struct tty_struct *tty, struct file *filp)
1200{
1201 struct moxa_port *ch = tty->driver_data;
1202 ch->cflag = tty->termios->c_cflag;
1203 tty_port_close(&ch->port, tty, filp);
1204}
1205
1206static int moxa_write(struct tty_struct *tty,
1207 const unsigned char *buf, int count)
1208{
1209 struct moxa_port *ch = tty->driver_data;
1210 int len;
1211
1212 if (ch == NULL)
1213 return 0;
1214
1215 spin_lock_bh(&moxa_lock);
1216 len = MoxaPortWriteData(tty, buf, count);
1217 spin_unlock_bh(&moxa_lock);
1218
1219 set_bit(LOWWAIT, &ch->statusflags);
1220 return len;
1221}
1222
1223static int moxa_write_room(struct tty_struct *tty)
1224{
1225 struct moxa_port *ch;
1226
1227 if (tty->stopped)
1228 return 0;
1229 ch = tty->driver_data;
1230 if (ch == NULL)
1231 return 0;
1232 return MoxaPortTxFree(ch);
1233}
1234
1235static void moxa_flush_buffer(struct tty_struct *tty)
1236{
1237 struct moxa_port *ch = tty->driver_data;
1238
1239 if (ch == NULL)
1240 return;
1241 MoxaPortFlushData(ch, 1);
1242 tty_wakeup(tty);
1243}
1244
1245static int moxa_chars_in_buffer(struct tty_struct *tty)
1246{
1247 struct moxa_port *ch = tty->driver_data;
1248 int chars;
1249
1250 chars = MoxaPortTxQueue(ch);
1251 if (chars)
1252 /*
1253 * Make it possible to wakeup anything waiting for output
1254 * in tty_ioctl.c, etc.
1255 */
1256 set_bit(EMPTYWAIT, &ch->statusflags);
1257 return chars;
1258}
1259
1260static int moxa_tiocmget(struct tty_struct *tty, struct file *file)
1261{
1262 struct moxa_port *ch = tty->driver_data;
1263 int flag = 0, dtr, rts;
1264
1265 MoxaPortGetLineOut(ch, &dtr, &rts);
1266 if (dtr)
1267 flag |= TIOCM_DTR;
1268 if (rts)
1269 flag |= TIOCM_RTS;
1270 dtr = MoxaPortLineStatus(ch);
1271 if (dtr & 1)
1272 flag |= TIOCM_CTS;
1273 if (dtr & 2)
1274 flag |= TIOCM_DSR;
1275 if (dtr & 4)
1276 flag |= TIOCM_CD;
1277 return flag;
1278}
1279
1280static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
1281 unsigned int set, unsigned int clear)
1282{
1283 struct moxa_port *ch;
1284 int port;
1285 int dtr, rts;
1286
1287 port = tty->index;
1288 mutex_lock(&moxa_openlock);
1289 ch = tty->driver_data;
1290 if (!ch) {
1291 mutex_unlock(&moxa_openlock);
1292 return -EINVAL;
1293 }
1294
1295 MoxaPortGetLineOut(ch, &dtr, &rts);
1296 if (set & TIOCM_RTS)
1297 rts = 1;
1298 if (set & TIOCM_DTR)
1299 dtr = 1;
1300 if (clear & TIOCM_RTS)
1301 rts = 0;
1302 if (clear & TIOCM_DTR)
1303 dtr = 0;
1304 MoxaPortLineCtrl(ch, dtr, rts);
1305 mutex_unlock(&moxa_openlock);
1306 return 0;
1307}
1308
1309static void moxa_set_termios(struct tty_struct *tty,
1310 struct ktermios *old_termios)
1311{
1312 struct moxa_port *ch = tty->driver_data;
1313
1314 if (ch == NULL)
1315 return;
1316 moxa_set_tty_param(tty, old_termios);
1317 if (!(old_termios->c_cflag & CLOCAL) && C_CLOCAL(tty))
1318 wake_up_interruptible(&ch->port.open_wait);
1319}
1320
1321static void moxa_stop(struct tty_struct *tty)
1322{
1323 struct moxa_port *ch = tty->driver_data;
1324
1325 if (ch == NULL)
1326 return;
1327 MoxaPortTxDisable(ch);
1328 set_bit(TXSTOPPED, &ch->statusflags);
1329}
1330
1331
1332static void moxa_start(struct tty_struct *tty)
1333{
1334 struct moxa_port *ch = tty->driver_data;
1335
1336 if (ch == NULL)
1337 return;
1338
1339 if (!(ch->statusflags & TXSTOPPED))
1340 return;
1341
1342 MoxaPortTxEnable(ch);
1343 clear_bit(TXSTOPPED, &ch->statusflags);
1344}
1345
1346static void moxa_hangup(struct tty_struct *tty)
1347{
1348 struct moxa_port *ch = tty->driver_data;
1349 tty_port_hangup(&ch->port);
1350}
1351
1352static void moxa_new_dcdstate(struct moxa_port *p, u8 dcd)
1353{
1354 struct tty_struct *tty;
1355 unsigned long flags;
1356 dcd = !!dcd;
1357
1358 spin_lock_irqsave(&p->port.lock, flags);
1359 if (dcd != p->DCDState) {
1360 p->DCDState = dcd;
1361 spin_unlock_irqrestore(&p->port.lock, flags);
1362 tty = tty_port_tty_get(&p->port);
1363 if (tty && C_CLOCAL(tty) && !dcd)
1364 tty_hangup(tty);
1365 tty_kref_put(tty);
1366 }
1367 else
1368 spin_unlock_irqrestore(&p->port.lock, flags);
1369}
1370
1371static int moxa_poll_port(struct moxa_port *p, unsigned int handle,
1372 u16 __iomem *ip)
1373{
1374 struct tty_struct *tty = tty_port_tty_get(&p->port);
1375 void __iomem *ofsAddr;
1376 unsigned int inited = p->port.flags & ASYNC_INITIALIZED;
1377 u16 intr;
1378
1379 if (tty) {
1380 if (test_bit(EMPTYWAIT, &p->statusflags) &&
1381 MoxaPortTxQueue(p) == 0) {
1382 clear_bit(EMPTYWAIT, &p->statusflags);
1383 tty_wakeup(tty);
1384 }
1385 if (test_bit(LOWWAIT, &p->statusflags) && !tty->stopped &&
1386 MoxaPortTxQueue(p) <= WAKEUP_CHARS) {
1387 clear_bit(LOWWAIT, &p->statusflags);
1388 tty_wakeup(tty);
1389 }
1390
1391 if (inited && !test_bit(TTY_THROTTLED, &tty->flags) &&
1392 MoxaPortRxQueue(p) > 0) { /* RX */
1393 MoxaPortReadData(p);
1394 tty_schedule_flip(tty);
1395 }
1396 } else {
1397 clear_bit(EMPTYWAIT, &p->statusflags);
1398 MoxaPortFlushData(p, 0); /* flush RX */
1399 }
1400
1401 if (!handle) /* nothing else to do */
1402 goto put;
1403
1404 intr = readw(ip); /* port irq status */
1405 if (intr == 0)
1406 goto put;
1407
1408 writew(0, ip); /* ACK port */
1409 ofsAddr = p->tableAddr;
1410 if (intr & IntrTx) /* disable tx intr */
1411 writew(readw(ofsAddr + HostStat) & ~WakeupTx,
1412 ofsAddr + HostStat);
1413
1414 if (!inited)
1415 goto put;
1416
1417 if (tty && (intr & IntrBreak) && !I_IGNBRK(tty)) { /* BREAK */
1418 tty_insert_flip_char(tty, 0, TTY_BREAK);
1419 tty_schedule_flip(tty);
1420 }
1421
1422 if (intr & IntrLine)
1423 moxa_new_dcdstate(p, readb(ofsAddr + FlagStat) & DCD_state);
1424put:
1425 tty_kref_put(tty);
1426
1427 return 0;
1428}
1429
1430static void moxa_poll(unsigned long ignored)
1431{
1432 struct moxa_board_conf *brd;
1433 u16 __iomem *ip;
1434 unsigned int card, port, served = 0;
1435
1436 spin_lock(&moxa_lock);
1437 for (card = 0; card < MAX_BOARDS; card++) {
1438 brd = &moxa_boards[card];
1439 if (!brd->ready)
1440 continue;
1441
1442 served++;
1443
1444 ip = NULL;
1445 if (readb(brd->intPend) == 0xff)
1446 ip = brd->intTable + readb(brd->intNdx);
1447
1448 for (port = 0; port < brd->numPorts; port++)
1449 moxa_poll_port(&brd->ports[port], !!ip, ip + port);
1450
1451 if (ip)
1452 writeb(0, brd->intPend); /* ACK */
1453
1454 if (moxaLowWaterChk) {
1455 struct moxa_port *p = brd->ports;
1456 for (port = 0; port < brd->numPorts; port++, p++)
1457 if (p->lowChkFlag) {
1458 p->lowChkFlag = 0;
1459 moxa_low_water_check(p->tableAddr);
1460 }
1461 }
1462 }
1463 moxaLowWaterChk = 0;
1464
1465 if (served)
1466 mod_timer(&moxaTimer, jiffies + HZ / 50);
1467 spin_unlock(&moxa_lock);
1468}
1469
1470/******************************************************************************/
1471
1472static void moxa_set_tty_param(struct tty_struct *tty, struct ktermios *old_termios)
1473{
1474 register struct ktermios *ts = tty->termios;
1475 struct moxa_port *ch = tty->driver_data;
1476 int rts, cts, txflow, rxflow, xany, baud;
1477
1478 rts = cts = txflow = rxflow = xany = 0;
1479 if (ts->c_cflag & CRTSCTS)
1480 rts = cts = 1;
1481 if (ts->c_iflag & IXON)
1482 txflow = 1;
1483 if (ts->c_iflag & IXOFF)
1484 rxflow = 1;
1485 if (ts->c_iflag & IXANY)
1486 xany = 1;
1487
1488 /* Clear the features we don't support */
1489 ts->c_cflag &= ~CMSPAR;
1490 MoxaPortFlowCtrl(ch, rts, cts, txflow, rxflow, xany);
1491 baud = MoxaPortSetTermio(ch, ts, tty_get_baud_rate(tty));
1492 if (baud == -1)
1493 baud = tty_termios_baud_rate(old_termios);
1494 /* Not put the baud rate into the termios data */
1495 tty_encode_baud_rate(tty, baud, baud);
1496}
1497
1498/*****************************************************************************
1499 * Driver level functions: *
1500 *****************************************************************************/
1501
1502static void MoxaPortFlushData(struct moxa_port *port, int mode)
1503{
1504 void __iomem *ofsAddr;
1505 if (mode < 0 || mode > 2)
1506 return;
1507 ofsAddr = port->tableAddr;
1508 moxafunc(ofsAddr, FC_FlushQueue, mode);
1509 if (mode != 1) {
1510 port->lowChkFlag = 0;
1511 moxa_low_water_check(ofsAddr);
1512 }
1513}
1514
1515/*
1516 * Moxa Port Number Description:
1517 *
1518 * MOXA serial driver supports up to 4 MOXA-C218/C320 boards. And,
1519 * the port number using in MOXA driver functions will be 0 to 31 for
1520 * first MOXA board, 32 to 63 for second, 64 to 95 for third and 96
1521 * to 127 for fourth. For example, if you setup three MOXA boards,
1522 * first board is C218, second board is C320-16 and third board is
1523 * C320-32. The port number of first board (C218 - 8 ports) is from
1524 * 0 to 7. The port number of second board (C320 - 16 ports) is form
1525 * 32 to 47. The port number of third board (C320 - 32 ports) is from
1526 * 64 to 95. And those port numbers form 8 to 31, 48 to 63 and 96 to
1527 * 127 will be invalid.
1528 *
1529 *
1530 * Moxa Functions Description:
1531 *
1532 * Function 1: Driver initialization routine, this routine must be
1533 * called when initialized driver.
1534 * Syntax:
1535 * void MoxaDriverInit();
1536 *
1537 *
1538 * Function 2: Moxa driver private IOCTL command processing.
1539 * Syntax:
1540 * int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port);
1541 *
1542 * unsigned int cmd : IOCTL command
1543 * unsigned long arg : IOCTL argument
1544 * int port : port number (0 - 127)
1545 *
1546 * return: 0 (OK)
1547 * -EINVAL
1548 * -ENOIOCTLCMD
1549 *
1550 *
1551 * Function 6: Enable this port to start Tx/Rx data.
1552 * Syntax:
1553 * void MoxaPortEnable(int port);
1554 * int port : port number (0 - 127)
1555 *
1556 *
1557 * Function 7: Disable this port
1558 * Syntax:
1559 * void MoxaPortDisable(int port);
1560 * int port : port number (0 - 127)
1561 *
1562 *
1563 * Function 10: Setting baud rate of this port.
1564 * Syntax:
1565 * speed_t MoxaPortSetBaud(int port, speed_t baud);
1566 * int port : port number (0 - 127)
1567 * long baud : baud rate (50 - 115200)
1568 *
1569 * return: 0 : this port is invalid or baud < 50
1570 * 50 - 115200 : the real baud rate set to the port, if
1571 * the argument baud is large than maximun
1572 * available baud rate, the real setting
1573 * baud rate will be the maximun baud rate.
1574 *
1575 *
1576 * Function 12: Configure the port.
1577 * Syntax:
1578 * int MoxaPortSetTermio(int port, struct ktermios *termio, speed_t baud);
1579 * int port : port number (0 - 127)
1580 * struct ktermios * termio : termio structure pointer
1581 * speed_t baud : baud rate
1582 *
1583 * return: -1 : this port is invalid or termio == NULL
1584 * 0 : setting O.K.
1585 *
1586 *
1587 * Function 13: Get the DTR/RTS state of this port.
1588 * Syntax:
1589 * int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState);
1590 * int port : port number (0 - 127)
1591 * int * dtrState : pointer to INT to receive the current DTR
1592 * state. (if NULL, this function will not
1593 * write to this address)
1594 * int * rtsState : pointer to INT to receive the current RTS
1595 * state. (if NULL, this function will not
1596 * write to this address)
1597 *
1598 * return: -1 : this port is invalid
1599 * 0 : O.K.
1600 *
1601 *
1602 * Function 14: Setting the DTR/RTS output state of this port.
1603 * Syntax:
1604 * void MoxaPortLineCtrl(int port, int dtrState, int rtsState);
1605 * int port : port number (0 - 127)
1606 * int dtrState : DTR output state (0: off, 1: on)
1607 * int rtsState : RTS output state (0: off, 1: on)
1608 *
1609 *
1610 * Function 15: Setting the flow control of this port.
1611 * Syntax:
1612 * void MoxaPortFlowCtrl(int port, int rtsFlow, int ctsFlow, int rxFlow,
1613 * int txFlow,int xany);
1614 * int port : port number (0 - 127)
1615 * int rtsFlow : H/W RTS flow control (0: no, 1: yes)
1616 * int ctsFlow : H/W CTS flow control (0: no, 1: yes)
1617 * int rxFlow : S/W Rx XON/XOFF flow control (0: no, 1: yes)
1618 * int txFlow : S/W Tx XON/XOFF flow control (0: no, 1: yes)
1619 * int xany : S/W XANY flow control (0: no, 1: yes)
1620 *
1621 *
1622 * Function 16: Get ths line status of this port
1623 * Syntax:
1624 * int MoxaPortLineStatus(int port);
1625 * int port : port number (0 - 127)
1626 *
1627 * return: Bit 0 - CTS state (0: off, 1: on)
1628 * Bit 1 - DSR state (0: off, 1: on)
1629 * Bit 2 - DCD state (0: off, 1: on)
1630 *
1631 *
1632 * Function 19: Flush the Rx/Tx buffer data of this port.
1633 * Syntax:
1634 * void MoxaPortFlushData(int port, int mode);
1635 * int port : port number (0 - 127)
1636 * int mode
1637 * 0 : flush the Rx buffer
1638 * 1 : flush the Tx buffer
1639 * 2 : flush the Rx and Tx buffer
1640 *
1641 *
1642 * Function 20: Write data.
1643 * Syntax:
1644 * int MoxaPortWriteData(int port, unsigned char * buffer, int length);
1645 * int port : port number (0 - 127)
1646 * unsigned char * buffer : pointer to write data buffer.
1647 * int length : write data length
1648 *
1649 * return: 0 - length : real write data length
1650 *
1651 *
1652 * Function 21: Read data.
1653 * Syntax:
1654 * int MoxaPortReadData(int port, struct tty_struct *tty);
1655 * int port : port number (0 - 127)
1656 * struct tty_struct *tty : tty for data
1657 *
1658 * return: 0 - length : real read data length
1659 *
1660 *
1661 * Function 24: Get the Tx buffer current queued data bytes
1662 * Syntax:
1663 * int MoxaPortTxQueue(int port);
1664 * int port : port number (0 - 127)
1665 *
1666 * return: .. : Tx buffer current queued data bytes
1667 *
1668 *
1669 * Function 25: Get the Tx buffer current free space
1670 * Syntax:
1671 * int MoxaPortTxFree(int port);
1672 * int port : port number (0 - 127)
1673 *
1674 * return: .. : Tx buffer current free space
1675 *
1676 *
1677 * Function 26: Get the Rx buffer current queued data bytes
1678 * Syntax:
1679 * int MoxaPortRxQueue(int port);
1680 * int port : port number (0 - 127)
1681 *
1682 * return: .. : Rx buffer current queued data bytes
1683 *
1684 *
1685 * Function 28: Disable port data transmission.
1686 * Syntax:
1687 * void MoxaPortTxDisable(int port);
1688 * int port : port number (0 - 127)
1689 *
1690 *
1691 * Function 29: Enable port data transmission.
1692 * Syntax:
1693 * void MoxaPortTxEnable(int port);
1694 * int port : port number (0 - 127)
1695 *
1696 *
1697 * Function 31: Get the received BREAK signal count and reset it.
1698 * Syntax:
1699 * int MoxaPortResetBrkCnt(int port);
1700 * int port : port number (0 - 127)
1701 *
1702 * return: 0 - .. : BREAK signal count
1703 *
1704 *
1705 */
1706
1707static void MoxaPortEnable(struct moxa_port *port)
1708{
1709 void __iomem *ofsAddr;
1710 u16 lowwater = 512;
1711
1712 ofsAddr = port->tableAddr;
1713 writew(lowwater, ofsAddr + Low_water);
1714 if (MOXA_IS_320(port->board))
1715 moxafunc(ofsAddr, FC_SetBreakIrq, 0);
1716 else
1717 writew(readw(ofsAddr + HostStat) | WakeupBreak,
1718 ofsAddr + HostStat);
1719
1720 moxafunc(ofsAddr, FC_SetLineIrq, Magic_code);
1721 moxafunc(ofsAddr, FC_FlushQueue, 2);
1722
1723 moxafunc(ofsAddr, FC_EnableCH, Magic_code);
1724 MoxaPortLineStatus(port);
1725}
1726
1727static void MoxaPortDisable(struct moxa_port *port)
1728{
1729 void __iomem *ofsAddr = port->tableAddr;
1730
1731 moxafunc(ofsAddr, FC_SetFlowCtl, 0); /* disable flow control */
1732 moxafunc(ofsAddr, FC_ClrLineIrq, Magic_code);
1733 writew(0, ofsAddr + HostStat);
1734 moxafunc(ofsAddr, FC_DisableCH, Magic_code);
1735}
1736
1737static speed_t MoxaPortSetBaud(struct moxa_port *port, speed_t baud)
1738{
1739 void __iomem *ofsAddr = port->tableAddr;
1740 unsigned int clock, val;
1741 speed_t max;
1742
1743 max = MOXA_IS_320(port->board) ? 460800 : 921600;
1744 if (baud < 50)
1745 return 0;
1746 if (baud > max)
1747 baud = max;
1748 clock = 921600;
1749 val = clock / baud;
1750 moxafunc(ofsAddr, FC_SetBaud, val);
1751 baud = clock / val;
1752 return baud;
1753}
1754
1755static int MoxaPortSetTermio(struct moxa_port *port, struct ktermios *termio,
1756 speed_t baud)
1757{
1758 void __iomem *ofsAddr;
1759 tcflag_t cflag;
1760 tcflag_t mode = 0;
1761
1762 ofsAddr = port->tableAddr;
1763 cflag = termio->c_cflag; /* termio->c_cflag */
1764
1765 mode = termio->c_cflag & CSIZE;
1766 if (mode == CS5)
1767 mode = MX_CS5;
1768 else if (mode == CS6)
1769 mode = MX_CS6;
1770 else if (mode == CS7)
1771 mode = MX_CS7;
1772 else if (mode == CS8)
1773 mode = MX_CS8;
1774
1775 if (termio->c_cflag & CSTOPB) {
1776 if (mode == MX_CS5)
1777 mode |= MX_STOP15;
1778 else
1779 mode |= MX_STOP2;
1780 } else
1781 mode |= MX_STOP1;
1782
1783 if (termio->c_cflag & PARENB) {
1784 if (termio->c_cflag & PARODD)
1785 mode |= MX_PARODD;
1786 else
1787 mode |= MX_PAREVEN;
1788 } else
1789 mode |= MX_PARNONE;
1790
1791 moxafunc(ofsAddr, FC_SetDataMode, (u16)mode);
1792
1793 if (MOXA_IS_320(port->board) && baud >= 921600)
1794 return -1;
1795
1796 baud = MoxaPortSetBaud(port, baud);
1797
1798 if (termio->c_iflag & (IXON | IXOFF | IXANY)) {
1799 spin_lock_irq(&moxafunc_lock);
1800 writeb(termio->c_cc[VSTART], ofsAddr + FuncArg);
1801 writeb(termio->c_cc[VSTOP], ofsAddr + FuncArg1);
1802 writeb(FC_SetXonXoff, ofsAddr + FuncCode);
1803 moxa_wait_finish(ofsAddr);
1804 spin_unlock_irq(&moxafunc_lock);
1805
1806 }
1807 return baud;
1808}
1809
1810static int MoxaPortGetLineOut(struct moxa_port *port, int *dtrState,
1811 int *rtsState)
1812{
1813 if (dtrState)
1814 *dtrState = !!(port->lineCtrl & DTR_ON);
1815 if (rtsState)
1816 *rtsState = !!(port->lineCtrl & RTS_ON);
1817
1818 return 0;
1819}
1820
1821static void MoxaPortLineCtrl(struct moxa_port *port, int dtr, int rts)
1822{
1823 u8 mode = 0;
1824
1825 if (dtr)
1826 mode |= DTR_ON;
1827 if (rts)
1828 mode |= RTS_ON;
1829 port->lineCtrl = mode;
1830 moxafunc(port->tableAddr, FC_LineControl, mode);
1831}
1832
1833static void MoxaPortFlowCtrl(struct moxa_port *port, int rts, int cts,
1834 int txflow, int rxflow, int txany)
1835{
1836 int mode = 0;
1837
1838 if (rts)
1839 mode |= RTS_FlowCtl;
1840 if (cts)
1841 mode |= CTS_FlowCtl;
1842 if (txflow)
1843 mode |= Tx_FlowCtl;
1844 if (rxflow)
1845 mode |= Rx_FlowCtl;
1846 if (txany)
1847 mode |= IXM_IXANY;
1848 moxafunc(port->tableAddr, FC_SetFlowCtl, mode);
1849}
1850
1851static int MoxaPortLineStatus(struct moxa_port *port)
1852{
1853 void __iomem *ofsAddr;
1854 int val;
1855
1856 ofsAddr = port->tableAddr;
1857 if (MOXA_IS_320(port->board))
1858 val = moxafuncret(ofsAddr, FC_LineStatus, 0);
1859 else
1860 val = readw(ofsAddr + FlagStat) >> 4;
1861 val &= 0x0B;
1862 if (val & 8)
1863 val |= 4;
1864 moxa_new_dcdstate(port, val & 8);
1865 val &= 7;
1866 return val;
1867}
1868
1869static int MoxaPortWriteData(struct tty_struct *tty,
1870 const unsigned char *buffer, int len)
1871{
1872 struct moxa_port *port = tty->driver_data;
1873 void __iomem *baseAddr, *ofsAddr, *ofs;
1874 unsigned int c, total;
1875 u16 head, tail, tx_mask, spage, epage;
1876 u16 pageno, pageofs, bufhead;
1877
1878 ofsAddr = port->tableAddr;
1879 baseAddr = port->board->basemem;
1880 tx_mask = readw(ofsAddr + TX_mask);
1881 spage = readw(ofsAddr + Page_txb);
1882 epage = readw(ofsAddr + EndPage_txb);
1883 tail = readw(ofsAddr + TXwptr);
1884 head = readw(ofsAddr + TXrptr);
1885 c = (head > tail) ? (head - tail - 1) : (head - tail + tx_mask);
1886 if (c > len)
1887 c = len;
1888 moxaLog.txcnt[port->port.tty->index] += c;
1889 total = c;
1890 if (spage == epage) {
1891 bufhead = readw(ofsAddr + Ofs_txb);
1892 writew(spage, baseAddr + Control_reg);
1893 while (c > 0) {
1894 if (head > tail)
1895 len = head - tail - 1;
1896 else
1897 len = tx_mask + 1 - tail;
1898 len = (c > len) ? len : c;
1899 ofs = baseAddr + DynPage_addr + bufhead + tail;
1900 memcpy_toio(ofs, buffer, len);
1901 buffer += len;
1902 tail = (tail + len) & tx_mask;
1903 c -= len;
1904 }
1905 } else {
1906 pageno = spage + (tail >> 13);
1907 pageofs = tail & Page_mask;
1908 while (c > 0) {
1909 len = Page_size - pageofs;
1910 if (len > c)
1911 len = c;
1912 writeb(pageno, baseAddr + Control_reg);
1913 ofs = baseAddr + DynPage_addr + pageofs;
1914 memcpy_toio(ofs, buffer, len);
1915 buffer += len;
1916 if (++pageno == epage)
1917 pageno = spage;
1918 pageofs = 0;
1919 c -= len;
1920 }
1921 tail = (tail + total) & tx_mask;
1922 }
1923 writew(tail, ofsAddr + TXwptr);
1924 writeb(1, ofsAddr + CD180TXirq); /* start to send */
1925 return total;
1926}
1927
1928static int MoxaPortReadData(struct moxa_port *port)
1929{
1930 struct tty_struct *tty = port->port.tty;
1931 unsigned char *dst;
1932 void __iomem *baseAddr, *ofsAddr, *ofs;
1933 unsigned int count, len, total;
1934 u16 tail, rx_mask, spage, epage;
1935 u16 pageno, pageofs, bufhead, head;
1936
1937 ofsAddr = port->tableAddr;
1938 baseAddr = port->board->basemem;
1939 head = readw(ofsAddr + RXrptr);
1940 tail = readw(ofsAddr + RXwptr);
1941 rx_mask = readw(ofsAddr + RX_mask);
1942 spage = readw(ofsAddr + Page_rxb);
1943 epage = readw(ofsAddr + EndPage_rxb);
1944 count = (tail >= head) ? (tail - head) : (tail - head + rx_mask + 1);
1945 if (count == 0)
1946 return 0;
1947
1948 total = count;
1949 moxaLog.rxcnt[tty->index] += total;
1950 if (spage == epage) {
1951 bufhead = readw(ofsAddr + Ofs_rxb);
1952 writew(spage, baseAddr + Control_reg);
1953 while (count > 0) {
1954 ofs = baseAddr + DynPage_addr + bufhead + head;
1955 len = (tail >= head) ? (tail - head) :
1956 (rx_mask + 1 - head);
1957 len = tty_prepare_flip_string(tty, &dst,
1958 min(len, count));
1959 memcpy_fromio(dst, ofs, len);
1960 head = (head + len) & rx_mask;
1961 count -= len;
1962 }
1963 } else {
1964 pageno = spage + (head >> 13);
1965 pageofs = head & Page_mask;
1966 while (count > 0) {
1967 writew(pageno, baseAddr + Control_reg);
1968 ofs = baseAddr + DynPage_addr + pageofs;
1969 len = tty_prepare_flip_string(tty, &dst,
1970 min(Page_size - pageofs, count));
1971 memcpy_fromio(dst, ofs, len);
1972
1973 count -= len;
1974 pageofs = (pageofs + len) & Page_mask;
1975 if (pageofs == 0 && ++pageno == epage)
1976 pageno = spage;
1977 }
1978 head = (head + total) & rx_mask;
1979 }
1980 writew(head, ofsAddr + RXrptr);
1981 if (readb(ofsAddr + FlagStat) & Xoff_state) {
1982 moxaLowWaterChk = 1;
1983 port->lowChkFlag = 1;
1984 }
1985 return total;
1986}
1987
1988
1989static int MoxaPortTxQueue(struct moxa_port *port)
1990{
1991 void __iomem *ofsAddr = port->tableAddr;
1992 u16 rptr, wptr, mask;
1993
1994 rptr = readw(ofsAddr + TXrptr);
1995 wptr = readw(ofsAddr + TXwptr);
1996 mask = readw(ofsAddr + TX_mask);
1997 return (wptr - rptr) & mask;
1998}
1999
2000static int MoxaPortTxFree(struct moxa_port *port)
2001{
2002 void __iomem *ofsAddr = port->tableAddr;
2003 u16 rptr, wptr, mask;
2004
2005 rptr = readw(ofsAddr + TXrptr);
2006 wptr = readw(ofsAddr + TXwptr);
2007 mask = readw(ofsAddr + TX_mask);
2008 return mask - ((wptr - rptr) & mask);
2009}
2010
2011static int MoxaPortRxQueue(struct moxa_port *port)
2012{
2013 void __iomem *ofsAddr = port->tableAddr;
2014 u16 rptr, wptr, mask;
2015
2016 rptr = readw(ofsAddr + RXrptr);
2017 wptr = readw(ofsAddr + RXwptr);
2018 mask = readw(ofsAddr + RX_mask);
2019 return (wptr - rptr) & mask;
2020}
2021
2022static void MoxaPortTxDisable(struct moxa_port *port)
2023{
2024 moxafunc(port->tableAddr, FC_SetXoffState, Magic_code);
2025}
2026
2027static void MoxaPortTxEnable(struct moxa_port *port)
2028{
2029 moxafunc(port->tableAddr, FC_SetXonState, Magic_code);
2030}
2031
2032static int moxa_get_serial_info(struct moxa_port *info,
2033 struct serial_struct __user *retinfo)
2034{
2035 struct serial_struct tmp = {
2036 .type = info->type,
2037 .line = info->port.tty->index,
2038 .flags = info->port.flags,
2039 .baud_base = 921600,
2040 .close_delay = info->port.close_delay
2041 };
2042 return copy_to_user(retinfo, &tmp, sizeof(*retinfo)) ? -EFAULT : 0;
2043}
2044
2045
2046static int moxa_set_serial_info(struct moxa_port *info,
2047 struct serial_struct __user *new_info)
2048{
2049 struct serial_struct new_serial;
2050
2051 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
2052 return -EFAULT;
2053
2054 if (new_serial.irq != 0 || new_serial.port != 0 ||
2055 new_serial.custom_divisor != 0 ||
2056 new_serial.baud_base != 921600)
2057 return -EPERM;
2058
2059 if (!capable(CAP_SYS_ADMIN)) {
2060 if (((new_serial.flags & ~ASYNC_USR_MASK) !=
2061 (info->port.flags & ~ASYNC_USR_MASK)))
2062 return -EPERM;
2063 } else
2064 info->port.close_delay = new_serial.close_delay * HZ / 100;
2065
2066 new_serial.flags = (new_serial.flags & ~ASYNC_FLAGS);
2067 new_serial.flags |= (info->port.flags & ASYNC_FLAGS);
2068
2069 MoxaSetFifo(info, new_serial.type == PORT_16550A);
2070
2071 info->type = new_serial.type;
2072 return 0;
2073}
2074
2075
2076
2077/*****************************************************************************
2078 * Static local functions: *
2079 *****************************************************************************/
2080
2081static void MoxaSetFifo(struct moxa_port *port, int enable)
2082{
2083 void __iomem *ofsAddr = port->tableAddr;
2084
2085 if (!enable) {
2086 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 0);
2087 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 1);
2088 } else {
2089 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 3);
2090 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 16);
2091 }
2092}
diff --git a/drivers/char/moxa.h b/drivers/char/moxa.h
deleted file mode 100644
index 87d16ce57be7..000000000000
--- a/drivers/char/moxa.h
+++ /dev/null
@@ -1,304 +0,0 @@
1#ifndef MOXA_H_FILE
2#define MOXA_H_FILE
3
4#define MOXA 0x400
5#define MOXA_GET_IQUEUE (MOXA + 1) /* get input buffered count */
6#define MOXA_GET_OQUEUE (MOXA + 2) /* get output buffered count */
7#define MOXA_GETDATACOUNT (MOXA + 23)
8#define MOXA_GET_IOQUEUE (MOXA + 27)
9#define MOXA_FLUSH_QUEUE (MOXA + 28)
10#define MOXA_GETMSTATUS (MOXA + 65)
11
12/*
13 * System Configuration
14 */
15
16#define Magic_code 0x404
17
18/*
19 * for C218 BIOS initialization
20 */
21#define C218_ConfBase 0x800
22#define C218_status (C218_ConfBase + 0) /* BIOS running status */
23#define C218_diag (C218_ConfBase + 2) /* diagnostic status */
24#define C218_key (C218_ConfBase + 4) /* WORD (0x218 for C218) */
25#define C218DLoad_len (C218_ConfBase + 6) /* WORD */
26#define C218check_sum (C218_ConfBase + 8) /* BYTE */
27#define C218chksum_ok (C218_ConfBase + 0x0a) /* BYTE (1:ok) */
28#define C218_TestRx (C218_ConfBase + 0x10) /* 8 bytes for 8 ports */
29#define C218_TestTx (C218_ConfBase + 0x18) /* 8 bytes for 8 ports */
30#define C218_RXerr (C218_ConfBase + 0x20) /* 8 bytes for 8 ports */
31#define C218_ErrFlag (C218_ConfBase + 0x28) /* 8 bytes for 8 ports */
32
33#define C218_LoadBuf 0x0F00
34#define C218_KeyCode 0x218
35#define CP204J_KeyCode 0x204
36
37/*
38 * for C320 BIOS initialization
39 */
40#define C320_ConfBase 0x800
41#define C320_LoadBuf 0x0f00
42#define STS_init 0x05 /* for C320_status */
43
44#define C320_status C320_ConfBase + 0 /* BIOS running status */
45#define C320_diag C320_ConfBase + 2 /* diagnostic status */
46#define C320_key C320_ConfBase + 4 /* WORD (0320H for C320) */
47#define C320DLoad_len C320_ConfBase + 6 /* WORD */
48#define C320check_sum C320_ConfBase + 8 /* WORD */
49#define C320chksum_ok C320_ConfBase + 0x0a /* WORD (1:ok) */
50#define C320bapi_len C320_ConfBase + 0x0c /* WORD */
51#define C320UART_no C320_ConfBase + 0x0e /* WORD */
52
53#define C320_KeyCode 0x320
54
55#define FixPage_addr 0x0000 /* starting addr of static page */
56#define DynPage_addr 0x2000 /* starting addr of dynamic page */
57#define C218_start 0x3000 /* starting addr of C218 BIOS prg */
58#define Control_reg 0x1ff0 /* select page and reset control */
59#define HW_reset 0x80
60
61/*
62 * Function Codes
63 */
64#define FC_CardReset 0x80
65#define FC_ChannelReset 1 /* C320 firmware not supported */
66#define FC_EnableCH 2
67#define FC_DisableCH 3
68#define FC_SetParam 4
69#define FC_SetMode 5
70#define FC_SetRate 6
71#define FC_LineControl 7
72#define FC_LineStatus 8
73#define FC_XmitControl 9
74#define FC_FlushQueue 10
75#define FC_SendBreak 11
76#define FC_StopBreak 12
77#define FC_LoopbackON 13
78#define FC_LoopbackOFF 14
79#define FC_ClrIrqTable 15
80#define FC_SendXon 16
81#define FC_SetTermIrq 17 /* C320 firmware not supported */
82#define FC_SetCntIrq 18 /* C320 firmware not supported */
83#define FC_SetBreakIrq 19
84#define FC_SetLineIrq 20
85#define FC_SetFlowCtl 21
86#define FC_GenIrq 22
87#define FC_InCD180 23
88#define FC_OutCD180 24
89#define FC_InUARTreg 23
90#define FC_OutUARTreg 24
91#define FC_SetXonXoff 25
92#define FC_OutCD180CCR 26
93#define FC_ExtIQueue 27
94#define FC_ExtOQueue 28
95#define FC_ClrLineIrq 29
96#define FC_HWFlowCtl 30
97#define FC_GetClockRate 35
98#define FC_SetBaud 36
99#define FC_SetDataMode 41
100#define FC_GetCCSR 43
101#define FC_GetDataError 45
102#define FC_RxControl 50
103#define FC_ImmSend 51
104#define FC_SetXonState 52
105#define FC_SetXoffState 53
106#define FC_SetRxFIFOTrig 54
107#define FC_SetTxFIFOCnt 55
108#define FC_UnixRate 56
109#define FC_UnixResetTimer 57
110
111#define RxFIFOTrig1 0
112#define RxFIFOTrig4 1
113#define RxFIFOTrig8 2
114#define RxFIFOTrig14 3
115
116/*
117 * Dual-Ported RAM
118 */
119#define DRAM_global 0
120#define INT_data (DRAM_global + 0)
121#define Config_base (DRAM_global + 0x108)
122
123#define IRQindex (INT_data + 0)
124#define IRQpending (INT_data + 4)
125#define IRQtable (INT_data + 8)
126
127/*
128 * Interrupt Status
129 */
130#define IntrRx 0x01 /* receiver data O.K. */
131#define IntrTx 0x02 /* transmit buffer empty */
132#define IntrFunc 0x04 /* function complete */
133#define IntrBreak 0x08 /* received break */
134#define IntrLine 0x10 /* line status change
135 for transmitter */
136#define IntrIntr 0x20 /* received INTR code */
137#define IntrQuit 0x40 /* received QUIT code */
138#define IntrEOF 0x80 /* received EOF code */
139
140#define IntrRxTrigger 0x100 /* rx data count reach tigger value */
141#define IntrTxTrigger 0x200 /* tx data count below trigger value */
142
143#define Magic_no (Config_base + 0)
144#define Card_model_no (Config_base + 2)
145#define Total_ports (Config_base + 4)
146#define Module_cnt (Config_base + 8)
147#define Module_no (Config_base + 10)
148#define Timer_10ms (Config_base + 14)
149#define Disable_IRQ (Config_base + 20)
150#define TMS320_PORT1 (Config_base + 22)
151#define TMS320_PORT2 (Config_base + 24)
152#define TMS320_CLOCK (Config_base + 26)
153
154/*
155 * DATA BUFFER in DRAM
156 */
157#define Extern_table 0x400 /* Base address of the external table
158 (24 words * 64) total 3K bytes
159 (24 words * 128) total 6K bytes */
160#define Extern_size 0x60 /* 96 bytes */
161#define RXrptr 0x00 /* read pointer for RX buffer */
162#define RXwptr 0x02 /* write pointer for RX buffer */
163#define TXrptr 0x04 /* read pointer for TX buffer */
164#define TXwptr 0x06 /* write pointer for TX buffer */
165#define HostStat 0x08 /* IRQ flag and general flag */
166#define FlagStat 0x0A
167#define FlowControl 0x0C /* B7 B6 B5 B4 B3 B2 B1 B0 */
168 /* x x x x | | | | */
169 /* | | | + CTS flow */
170 /* | | +--- RTS flow */
171 /* | +------ TX Xon/Xoff */
172 /* +--------- RX Xon/Xoff */
173#define Break_cnt 0x0E /* received break count */
174#define CD180TXirq 0x10 /* if non-0: enable TX irq */
175#define RX_mask 0x12
176#define TX_mask 0x14
177#define Ofs_rxb 0x16
178#define Ofs_txb 0x18
179#define Page_rxb 0x1A
180#define Page_txb 0x1C
181#define EndPage_rxb 0x1E
182#define EndPage_txb 0x20
183#define Data_error 0x22
184#define RxTrigger 0x28
185#define TxTrigger 0x2a
186
187#define rRXwptr 0x34
188#define Low_water 0x36
189
190#define FuncCode 0x40
191#define FuncArg 0x42
192#define FuncArg1 0x44
193
194#define C218rx_size 0x2000 /* 8K bytes */
195#define C218tx_size 0x8000 /* 32K bytes */
196
197#define C218rx_mask (C218rx_size - 1)
198#define C218tx_mask (C218tx_size - 1)
199
200#define C320p8rx_size 0x2000
201#define C320p8tx_size 0x8000
202#define C320p8rx_mask (C320p8rx_size - 1)
203#define C320p8tx_mask (C320p8tx_size - 1)
204
205#define C320p16rx_size 0x2000
206#define C320p16tx_size 0x4000
207#define C320p16rx_mask (C320p16rx_size - 1)
208#define C320p16tx_mask (C320p16tx_size - 1)
209
210#define C320p24rx_size 0x2000
211#define C320p24tx_size 0x2000
212#define C320p24rx_mask (C320p24rx_size - 1)
213#define C320p24tx_mask (C320p24tx_size - 1)
214
215#define C320p32rx_size 0x1000
216#define C320p32tx_size 0x1000
217#define C320p32rx_mask (C320p32rx_size - 1)
218#define C320p32tx_mask (C320p32tx_size - 1)
219
220#define Page_size 0x2000U
221#define Page_mask (Page_size - 1)
222#define C218rx_spage 3
223#define C218tx_spage 4
224#define C218rx_pageno 1
225#define C218tx_pageno 4
226#define C218buf_pageno 5
227
228#define C320p8rx_spage 3
229#define C320p8tx_spage 4
230#define C320p8rx_pgno 1
231#define C320p8tx_pgno 4
232#define C320p8buf_pgno 5
233
234#define C320p16rx_spage 3
235#define C320p16tx_spage 4
236#define C320p16rx_pgno 1
237#define C320p16tx_pgno 2
238#define C320p16buf_pgno 3
239
240#define C320p24rx_spage 3
241#define C320p24tx_spage 4
242#define C320p24rx_pgno 1
243#define C320p24tx_pgno 1
244#define C320p24buf_pgno 2
245
246#define C320p32rx_spage 3
247#define C320p32tx_ofs C320p32rx_size
248#define C320p32tx_spage 3
249#define C320p32buf_pgno 1
250
251/*
252 * Host Status
253 */
254#define WakeupRx 0x01
255#define WakeupTx 0x02
256#define WakeupBreak 0x08
257#define WakeupLine 0x10
258#define WakeupIntr 0x20
259#define WakeupQuit 0x40
260#define WakeupEOF 0x80 /* used in VTIME control */
261#define WakeupRxTrigger 0x100
262#define WakeupTxTrigger 0x200
263/*
264 * Flag status
265 */
266#define Rx_over 0x01
267#define Xoff_state 0x02
268#define Tx_flowOff 0x04
269#define Tx_enable 0x08
270#define CTS_state 0x10
271#define DSR_state 0x20
272#define DCD_state 0x80
273/*
274 * FlowControl
275 */
276#define CTS_FlowCtl 1
277#define RTS_FlowCtl 2
278#define Tx_FlowCtl 4
279#define Rx_FlowCtl 8
280#define IXM_IXANY 0x10
281
282#define LowWater 128
283
284#define DTR_ON 1
285#define RTS_ON 2
286#define CTS_ON 1
287#define DSR_ON 2
288#define DCD_ON 8
289
290/* mode definition */
291#define MX_CS8 0x03
292#define MX_CS7 0x02
293#define MX_CS6 0x01
294#define MX_CS5 0x00
295
296#define MX_STOP1 0x00
297#define MX_STOP15 0x04
298#define MX_STOP2 0x08
299
300#define MX_PARNONE 0x00
301#define MX_PAREVEN 0x40
302#define MX_PARODD 0xC0
303
304#endif
diff --git a/drivers/char/msm_smd_pkt.c b/drivers/char/msm_smd_pkt.c
new file mode 100644
index 000000000000..b6f8a65c9960
--- /dev/null
+++ b/drivers/char/msm_smd_pkt.c
@@ -0,0 +1,466 @@
1/* Copyright (c) 2008-2010, Code Aurora Forum. All rights reserved.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15 * 02110-1301, USA.
16 *
17 */
18/*
19 * SMD Packet Driver -- Provides userspace interface to SMD packet ports.
20 */
21
22#include <linux/slab.h>
23#include <linux/cdev.h>
24#include <linux/module.h>
25#include <linux/fs.h>
26#include <linux/device.h>
27#include <linux/sched.h>
28#include <linux/mutex.h>
29#include <linux/delay.h>
30#include <linux/uaccess.h>
31#include <linux/workqueue.h>
32#include <linux/poll.h>
33
34#include <mach/msm_smd.h>
35
36#define NUM_SMD_PKT_PORTS 9
37#define DEVICE_NAME "smdpkt"
38#define MAX_BUF_SIZE 2048
39
40struct smd_pkt_dev {
41 struct cdev cdev;
42 struct device *devicep;
43
44 struct smd_channel *ch;
45 int open_count;
46 struct mutex ch_lock;
47 struct mutex rx_lock;
48 struct mutex tx_lock;
49 wait_queue_head_t ch_read_wait_queue;
50 wait_queue_head_t ch_opened_wait_queue;
51
52 int i;
53
54 unsigned char tx_buf[MAX_BUF_SIZE];
55 unsigned char rx_buf[MAX_BUF_SIZE];
56 int remote_open;
57
58} *smd_pkt_devp[NUM_SMD_PKT_PORTS];
59
60struct class *smd_pkt_classp;
61static dev_t smd_pkt_number;
62
63static int msm_smd_pkt_debug_enable;
64module_param_named(debug_enable, msm_smd_pkt_debug_enable,
65 int, S_IRUGO | S_IWUSR | S_IWGRP);
66
67#ifdef DEBUG
68#define D_DUMP_BUFFER(prestr, cnt, buf) do { \
69 int i; \
70 if (msm_smd_pkt_debug_enable) { \
71 pr_debug("%s", prestr); \
72 for (i = 0; i < cnt; i++) \
73 pr_debug("%.2x", buf[i]); \
74 pr_debug("\n"); \
75 } \
76 } while (0)
77#else
78#define D_DUMP_BUFFER(prestr, cnt, buf) do {} while (0)
79#endif
80
81#ifdef DEBUG
82#define DBG(x...) do { \
83 if (msm_smd_pkt_debug_enable) \
84 pr_debug(x); \
85 } while (0)
86#else
87#define DBG(x...) do {} while (0)
88#endif
89
90static void check_and_wakeup_reader(struct smd_pkt_dev *smd_pkt_devp)
91{
92 int sz;
93
94 if (!smd_pkt_devp || !smd_pkt_devp->ch)
95 return;
96
97 sz = smd_cur_packet_size(smd_pkt_devp->ch);
98 if (sz == 0) {
99 DBG("no packet\n");
100 return;
101 }
102 if (sz > smd_read_avail(smd_pkt_devp->ch)) {
103 DBG("incomplete packet\n");
104 return;
105 }
106
107 DBG("waking up reader\n");
108 wake_up_interruptible(&smd_pkt_devp->ch_read_wait_queue);
109}
110
111static int smd_pkt_read(struct file *file, char __user *buf,
112 size_t count, loff_t *ppos)
113{
114 int r, bytes_read;
115 struct smd_pkt_dev *smd_pkt_devp;
116 struct smd_channel *chl;
117
118 DBG("read %d bytes\n", count);
119 if (count > MAX_BUF_SIZE)
120 return -EINVAL;
121
122 smd_pkt_devp = file->private_data;
123 if (!smd_pkt_devp || !smd_pkt_devp->ch)
124 return -EINVAL;
125
126 chl = smd_pkt_devp->ch;
127wait_for_packet:
128 r = wait_event_interruptible(smd_pkt_devp->ch_read_wait_queue,
129 (smd_cur_packet_size(chl) > 0 &&
130 smd_read_avail(chl) >=
131 smd_cur_packet_size(chl)));
132
133 if (r < 0) {
134 if (r != -ERESTARTSYS)
135 pr_err("wait returned %d\n", r);
136 return r;
137 }
138
139 mutex_lock(&smd_pkt_devp->rx_lock);
140
141 bytes_read = smd_cur_packet_size(smd_pkt_devp->ch);
142 if (bytes_read == 0 ||
143 bytes_read < smd_read_avail(smd_pkt_devp->ch)) {
144 mutex_unlock(&smd_pkt_devp->rx_lock);
145 DBG("Nothing to read\n");
146 goto wait_for_packet;
147 }
148
149 if (bytes_read > count) {
150 mutex_unlock(&smd_pkt_devp->rx_lock);
151 pr_info("packet size %d > buffer size %d", bytes_read, count);
152 return -EINVAL;
153 }
154
155 r = smd_read(smd_pkt_devp->ch, smd_pkt_devp->rx_buf, bytes_read);
156 if (r != bytes_read) {
157 mutex_unlock(&smd_pkt_devp->rx_lock);
158 pr_err("smd_read failed to read %d bytes: %d\n", bytes_read, r);
159 return -EIO;
160 }
161
162 D_DUMP_BUFFER("read: ", bytes_read, smd_pkt_devp->rx_buf);
163 r = copy_to_user(buf, smd_pkt_devp->rx_buf, bytes_read);
164 mutex_unlock(&smd_pkt_devp->rx_lock);
165 if (r) {
166 pr_err("copy_to_user failed %d\n", r);
167 return -EFAULT;
168 }
169
170 DBG("read complete %d bytes\n", bytes_read);
171 check_and_wakeup_reader(smd_pkt_devp);
172
173 return bytes_read;
174}
175
176static int smd_pkt_write(struct file *file, const char __user *buf,
177 size_t count, loff_t *ppos)
178{
179 int r;
180 struct smd_pkt_dev *smd_pkt_devp;
181
182 if (count > MAX_BUF_SIZE)
183 return -EINVAL;
184
185 DBG("writting %d bytes\n", count);
186
187 smd_pkt_devp = file->private_data;
188 if (!smd_pkt_devp || !smd_pkt_devp->ch)
189 return -EINVAL;
190
191 mutex_lock(&smd_pkt_devp->tx_lock);
192 if (smd_write_avail(smd_pkt_devp->ch) < count) {
193 mutex_unlock(&smd_pkt_devp->tx_lock);
194 DBG("Not enough space to write\n");
195 return -ENOMEM;
196 }
197
198 D_DUMP_BUFFER("write: ", count, buf);
199 r = copy_from_user(smd_pkt_devp->tx_buf, buf, count);
200 if (r) {
201 mutex_unlock(&smd_pkt_devp->tx_lock);
202 pr_err("copy_from_user failed %d\n", r);
203 return -EFAULT;
204 }
205
206 r = smd_write(smd_pkt_devp->ch, smd_pkt_devp->tx_buf, count);
207 if (r != count) {
208 mutex_unlock(&smd_pkt_devp->tx_lock);
209 pr_err("smd_write failed to write %d bytes: %d.\n", count, r);
210 return -EIO;
211 }
212 mutex_unlock(&smd_pkt_devp->tx_lock);
213
214 DBG("wrote %d bytes\n", count);
215 return count;
216}
217
218static unsigned int smd_pkt_poll(struct file *file, poll_table *wait)
219{
220 struct smd_pkt_dev *smd_pkt_devp;
221 unsigned int mask = 0;
222
223 smd_pkt_devp = file->private_data;
224 if (!smd_pkt_devp)
225 return POLLERR;
226
227 DBG("poll waiting\n");
228 poll_wait(file, &smd_pkt_devp->ch_read_wait_queue, wait);
229 if (smd_read_avail(smd_pkt_devp->ch))
230 mask |= POLLIN | POLLRDNORM;
231
232 DBG("poll return\n");
233 return mask;
234}
235
236static void smd_pkt_ch_notify(void *priv, unsigned event)
237{
238 struct smd_pkt_dev *smd_pkt_devp = priv;
239
240 if (smd_pkt_devp->ch == 0)
241 return;
242
243 switch (event) {
244 case SMD_EVENT_DATA:
245 DBG("data\n");
246 check_and_wakeup_reader(smd_pkt_devp);
247 break;
248
249 case SMD_EVENT_OPEN:
250 DBG("remote open\n");
251 smd_pkt_devp->remote_open = 1;
252 wake_up_interruptible(&smd_pkt_devp->ch_opened_wait_queue);
253 break;
254
255 case SMD_EVENT_CLOSE:
256 smd_pkt_devp->remote_open = 0;
257 pr_info("remote closed\n");
258 break;
259
260 default:
261 pr_err("unknown event %d\n", event);
262 break;
263 }
264}
265
266static char *smd_pkt_dev_name[] = {
267 "smdcntl0",
268 "smdcntl1",
269 "smdcntl2",
270 "smdcntl3",
271 "smdcntl4",
272 "smdcntl5",
273 "smdcntl6",
274 "smdcntl7",
275 "smd22",
276};
277
278static char *smd_ch_name[] = {
279 "DATA5_CNTL",
280 "DATA6_CNTL",
281 "DATA7_CNTL",
282 "DATA8_CNTL",
283 "DATA9_CNTL",
284 "DATA12_CNTL",
285 "DATA13_CNTL",
286 "DATA14_CNTL",
287 "DATA22",
288};
289
290static int smd_pkt_open(struct inode *inode, struct file *file)
291{
292 int r = 0;
293 struct smd_pkt_dev *smd_pkt_devp;
294
295 smd_pkt_devp = container_of(inode->i_cdev, struct smd_pkt_dev, cdev);
296 if (!smd_pkt_devp)
297 return -EINVAL;
298
299 file->private_data = smd_pkt_devp;
300
301 mutex_lock(&smd_pkt_devp->ch_lock);
302 if (smd_pkt_devp->open_count == 0) {
303 r = smd_open(smd_ch_name[smd_pkt_devp->i],
304 &smd_pkt_devp->ch, smd_pkt_devp,
305 smd_pkt_ch_notify);
306 if (r < 0) {
307 pr_err("smd_open failed for %s, %d\n",
308 smd_ch_name[smd_pkt_devp->i], r);
309 goto out;
310 }
311
312 r = wait_event_interruptible_timeout(
313 smd_pkt_devp->ch_opened_wait_queue,
314 smd_pkt_devp->remote_open,
315 msecs_to_jiffies(2 * HZ));
316 if (r == 0)
317 r = -ETIMEDOUT;
318
319 if (r < 0) {
320 pr_err("wait returned %d\n", r);
321 smd_close(smd_pkt_devp->ch);
322 smd_pkt_devp->ch = 0;
323 } else {
324 smd_pkt_devp->open_count++;
325 r = 0;
326 }
327 }
328out:
329 mutex_unlock(&smd_pkt_devp->ch_lock);
330 return r;
331}
332
333static int smd_pkt_release(struct inode *inode, struct file *file)
334{
335 int r = 0;
336 struct smd_pkt_dev *smd_pkt_devp = file->private_data;
337
338 if (!smd_pkt_devp)
339 return -EINVAL;
340
341 mutex_lock(&smd_pkt_devp->ch_lock);
342 if (--smd_pkt_devp->open_count == 0) {
343 r = smd_close(smd_pkt_devp->ch);
344 smd_pkt_devp->ch = 0;
345 }
346 mutex_unlock(&smd_pkt_devp->ch_lock);
347
348 return r;
349}
350
351static const struct file_operations smd_pkt_fops = {
352 .owner = THIS_MODULE,
353 .open = smd_pkt_open,
354 .release = smd_pkt_release,
355 .read = smd_pkt_read,
356 .write = smd_pkt_write,
357 .poll = smd_pkt_poll,
358};
359
360static int __init smd_pkt_init(void)
361{
362 int i;
363 int r;
364
365 r = alloc_chrdev_region(&smd_pkt_number, 0,
366 NUM_SMD_PKT_PORTS, DEVICE_NAME);
367 if (r) {
368 pr_err("alloc_chrdev_region() failed %d\n", r);
369 return r;
370 }
371
372 smd_pkt_classp = class_create(THIS_MODULE, DEVICE_NAME);
373 if (IS_ERR(smd_pkt_classp)) {
374 r = PTR_ERR(smd_pkt_classp);
375 pr_err("class_create() failed %d\n", r);
376 goto unreg_chardev;
377 }
378
379 for (i = 0; i < NUM_SMD_PKT_PORTS; ++i) {
380 smd_pkt_devp[i] = kzalloc(sizeof(struct smd_pkt_dev),
381 GFP_KERNEL);
382 if (IS_ERR(smd_pkt_devp[i])) {
383 r = PTR_ERR(smd_pkt_devp[i]);
384 pr_err("kmalloc() failed %d\n", r);
385 goto clean_cdevs;
386 }
387
388 smd_pkt_devp[i]->i = i;
389
390 init_waitqueue_head(&smd_pkt_devp[i]->ch_read_wait_queue);
391 smd_pkt_devp[i]->remote_open = 0;
392 init_waitqueue_head(&smd_pkt_devp[i]->ch_opened_wait_queue);
393
394 mutex_init(&smd_pkt_devp[i]->ch_lock);
395 mutex_init(&smd_pkt_devp[i]->rx_lock);
396 mutex_init(&smd_pkt_devp[i]->tx_lock);
397
398 cdev_init(&smd_pkt_devp[i]->cdev, &smd_pkt_fops);
399 smd_pkt_devp[i]->cdev.owner = THIS_MODULE;
400
401 r = cdev_add(&smd_pkt_devp[i]->cdev,
402 (smd_pkt_number + i), 1);
403 if (r) {
404 pr_err("cdev_add() failed %d\n", r);
405 kfree(smd_pkt_devp[i]);
406 goto clean_cdevs;
407 }
408
409 smd_pkt_devp[i]->devicep =
410 device_create(smd_pkt_classp, NULL,
411 (smd_pkt_number + i), NULL,
412 smd_pkt_dev_name[i]);
413 if (IS_ERR(smd_pkt_devp[i]->devicep)) {
414 r = PTR_ERR(smd_pkt_devp[i]->devicep);
415 pr_err("device_create() failed %d\n", r);
416 cdev_del(&smd_pkt_devp[i]->cdev);
417 kfree(smd_pkt_devp[i]);
418 goto clean_cdevs;
419 }
420
421 }
422
423 pr_info("SMD Packet Port Driver Initialized.\n");
424 return 0;
425
426clean_cdevs:
427 if (i > 0) {
428 while (--i >= 0) {
429 mutex_destroy(&smd_pkt_devp[i]->ch_lock);
430 mutex_destroy(&smd_pkt_devp[i]->rx_lock);
431 mutex_destroy(&smd_pkt_devp[i]->tx_lock);
432 cdev_del(&smd_pkt_devp[i]->cdev);
433 kfree(smd_pkt_devp[i]);
434 device_destroy(smd_pkt_classp,
435 MKDEV(MAJOR(smd_pkt_number), i));
436 }
437 }
438
439 class_destroy(smd_pkt_classp);
440unreg_chardev:
441 unregister_chrdev_region(MAJOR(smd_pkt_number), NUM_SMD_PKT_PORTS);
442 return r;
443}
444module_init(smd_pkt_init);
445
446static void __exit smd_pkt_cleanup(void)
447{
448 int i;
449
450 for (i = 0; i < NUM_SMD_PKT_PORTS; ++i) {
451 mutex_destroy(&smd_pkt_devp[i]->ch_lock);
452 mutex_destroy(&smd_pkt_devp[i]->rx_lock);
453 mutex_destroy(&smd_pkt_devp[i]->tx_lock);
454 cdev_del(&smd_pkt_devp[i]->cdev);
455 kfree(smd_pkt_devp[i]);
456 device_destroy(smd_pkt_classp,
457 MKDEV(MAJOR(smd_pkt_number), i));
458 }
459
460 class_destroy(smd_pkt_classp);
461 unregister_chrdev_region(MAJOR(smd_pkt_number), NUM_SMD_PKT_PORTS);
462}
463module_exit(smd_pkt_cleanup);
464
465MODULE_DESCRIPTION("MSM Shared Memory Packet Port");
466MODULE_LICENSE("GPL v2");
diff --git a/drivers/char/mspec.c b/drivers/char/mspec.c
index ecb89d798e35..25d139c9dbed 100644
--- a/drivers/char/mspec.c
+++ b/drivers/char/mspec.c
@@ -271,14 +271,13 @@ mspec_mmap(struct file *file, struct vm_area_struct *vma,
271 pages = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT; 271 pages = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
272 vdata_size = sizeof(struct vma_data) + pages * sizeof(long); 272 vdata_size = sizeof(struct vma_data) + pages * sizeof(long);
273 if (vdata_size <= PAGE_SIZE) 273 if (vdata_size <= PAGE_SIZE)
274 vdata = kmalloc(vdata_size, GFP_KERNEL); 274 vdata = kzalloc(vdata_size, GFP_KERNEL);
275 else { 275 else {
276 vdata = vmalloc(vdata_size); 276 vdata = vzalloc(vdata_size);
277 flags = VMD_VMALLOCED; 277 flags = VMD_VMALLOCED;
278 } 278 }
279 if (!vdata) 279 if (!vdata)
280 return -ENOMEM; 280 return -ENOMEM;
281 memset(vdata, 0, vdata_size);
282 281
283 vdata->vm_start = vma->vm_start; 282 vdata->vm_start = vma->vm_start;
284 vdata->vm_end = vma->vm_end; 283 vdata->vm_end = vma->vm_end;
@@ -316,7 +315,8 @@ uncached_mmap(struct file *file, struct vm_area_struct *vma)
316 315
317static const struct file_operations fetchop_fops = { 316static const struct file_operations fetchop_fops = {
318 .owner = THIS_MODULE, 317 .owner = THIS_MODULE,
319 .mmap = fetchop_mmap 318 .mmap = fetchop_mmap,
319 .llseek = noop_llseek,
320}; 320};
321 321
322static struct miscdevice fetchop_miscdev = { 322static struct miscdevice fetchop_miscdev = {
@@ -327,7 +327,8 @@ static struct miscdevice fetchop_miscdev = {
327 327
328static const struct file_operations cached_fops = { 328static const struct file_operations cached_fops = {
329 .owner = THIS_MODULE, 329 .owner = THIS_MODULE,
330 .mmap = cached_mmap 330 .mmap = cached_mmap,
331 .llseek = noop_llseek,
331}; 332};
332 333
333static struct miscdevice cached_miscdev = { 334static struct miscdevice cached_miscdev = {
@@ -338,7 +339,8 @@ static struct miscdevice cached_miscdev = {
338 339
339static const struct file_operations uncached_fops = { 340static const struct file_operations uncached_fops = {
340 .owner = THIS_MODULE, 341 .owner = THIS_MODULE,
341 .mmap = uncached_mmap 342 .mmap = uncached_mmap,
343 .llseek = noop_llseek,
342}; 344};
343 345
344static struct miscdevice uncached_miscdev = { 346static struct miscdevice uncached_miscdev = {
diff --git a/drivers/char/mwave/3780i.h b/drivers/char/mwave/3780i.h
index 270431ca7dae..fba6ab1160ce 100644
--- a/drivers/char/mwave/3780i.h
+++ b/drivers/char/mwave/3780i.h
@@ -122,7 +122,7 @@ typedef struct {
122typedef struct { 122typedef struct {
123 unsigned char Dma:3; /* RW: DMA channel selection */ 123 unsigned char Dma:3; /* RW: DMA channel selection */
124 unsigned char NumTransfers:2; /* RW: Maximum # of transfers once being granted the ISA bus */ 124 unsigned char NumTransfers:2; /* RW: Maximum # of transfers once being granted the ISA bus */
125 unsigned char ReRequest:2; /* RW: Minumum delay between releasing the ISA bus and requesting it again */ 125 unsigned char ReRequest:2; /* RW: Minimum delay between releasing the ISA bus and requesting it again */
126 unsigned char MEMCS16:1; /* RW: ISA signal MEMCS16: 0=disabled, 1=enabled */ 126 unsigned char MEMCS16:1; /* RW: ISA signal MEMCS16: 0=disabled, 1=enabled */
127} DSP_BUSMASTER_CFG_1; 127} DSP_BUSMASTER_CFG_1;
128 128
diff --git a/drivers/char/mwave/Makefile b/drivers/char/mwave/Makefile
index 754c9e2058ed..efa6a82e543d 100644
--- a/drivers/char/mwave/Makefile
+++ b/drivers/char/mwave/Makefile
@@ -6,10 +6,10 @@
6 6
7obj-$(CONFIG_MWAVE) += mwave.o 7obj-$(CONFIG_MWAVE) += mwave.o
8 8
9mwave-objs := mwavedd.o smapi.o tp3780i.o 3780i.o 9mwave-y := mwavedd.o smapi.o tp3780i.o 3780i.o
10 10
11# To have the mwave driver disable other uarts if necessary 11# To have the mwave driver disable other uarts if necessary
12# EXTRA_CFLAGS += -DMWAVE_FUTZ_WITH_OTHER_DEVICES 12# ccflags-y := -DMWAVE_FUTZ_WITH_OTHER_DEVICES
13 13
14# To compile in lots (~20 KiB) of run-time enablable printk()s for debugging: 14# To compile in lots (~20 KiB) of run-time enablable printk()s for debugging:
15EXTRA_CFLAGS += -DMW_TRACE 15ccflags-y += -DMW_TRACE
diff --git a/drivers/char/mwave/README b/drivers/char/mwave/README
index 480251fc78e2..c2a58f428bc8 100644
--- a/drivers/char/mwave/README
+++ b/drivers/char/mwave/README
@@ -11,7 +11,7 @@ are not saved by the BIOS and so do not persist after unload and reload.
11 0x0008 tp3780i tracing 11 0x0008 tp3780i tracing
12 12
13 Tracing only occurs if the driver has been compiled with the 13 Tracing only occurs if the driver has been compiled with the
14 MW_TRACE macro #defined (i.e. let EXTRA_CFLAGS += -DMW_TRACE 14 MW_TRACE macro #defined (i.e. let ccflags-y := -DMW_TRACE
15 in the Makefile). 15 in the Makefile).
16 16
17 mwave_3780i_irq=5/7/10/11/15 17 mwave_3780i_irq=5/7/10/11/15
diff --git a/drivers/char/mwave/mwavedd.c b/drivers/char/mwave/mwavedd.c
index a4ec50c95072..1d82d5838f0c 100644
--- a/drivers/char/mwave/mwavedd.c
+++ b/drivers/char/mwave/mwavedd.c
@@ -56,7 +56,7 @@
56#include <linux/serial.h> 56#include <linux/serial.h>
57#include <linux/sched.h> 57#include <linux/sched.h>
58#include <linux/spinlock.h> 58#include <linux/spinlock.h>
59#include <linux/smp_lock.h> 59#include <linux/mutex.h>
60#include <linux/delay.h> 60#include <linux/delay.h>
61#include <linux/serial_8250.h> 61#include <linux/serial_8250.h>
62#include "smapi.h" 62#include "smapi.h"
@@ -73,6 +73,7 @@ MODULE_LICENSE("GPL");
73* checks are made against other devices (ie. superio) for conflicts. 73* checks are made against other devices (ie. superio) for conflicts.
74* We'll depend on users using the tpctl utility to do that for now 74* We'll depend on users using the tpctl utility to do that for now
75*/ 75*/
76static DEFINE_MUTEX(mwave_mutex);
76int mwave_debug = 0; 77int mwave_debug = 0;
77int mwave_3780i_irq = 0; 78int mwave_3780i_irq = 0;
78int mwave_3780i_io = 0; 79int mwave_3780i_io = 0;
@@ -101,7 +102,6 @@ static int mwave_open(struct inode *inode, struct file *file)
101 PRINTK_2(TRACE_MWAVE, 102 PRINTK_2(TRACE_MWAVE,
102 "mwavedd::mwave_open, exit return retval %x\n", retval); 103 "mwavedd::mwave_open, exit return retval %x\n", retval);
103 104
104 cycle_kernel_lock();
105 return retval; 105 return retval;
106} 106}
107 107
@@ -136,9 +136,9 @@ static long mwave_ioctl(struct file *file, unsigned int iocmd,
136 PRINTK_1(TRACE_MWAVE, 136 PRINTK_1(TRACE_MWAVE,
137 "mwavedd::mwave_ioctl, IOCTL_MW_RESET" 137 "mwavedd::mwave_ioctl, IOCTL_MW_RESET"
138 " calling tp3780I_ResetDSP\n"); 138 " calling tp3780I_ResetDSP\n");
139 lock_kernel(); 139 mutex_lock(&mwave_mutex);
140 retval = tp3780I_ResetDSP(&pDrvData->rBDData); 140 retval = tp3780I_ResetDSP(&pDrvData->rBDData);
141 unlock_kernel(); 141 mutex_unlock(&mwave_mutex);
142 PRINTK_2(TRACE_MWAVE, 142 PRINTK_2(TRACE_MWAVE,
143 "mwavedd::mwave_ioctl, IOCTL_MW_RESET" 143 "mwavedd::mwave_ioctl, IOCTL_MW_RESET"
144 " retval %x from tp3780I_ResetDSP\n", 144 " retval %x from tp3780I_ResetDSP\n",
@@ -149,9 +149,9 @@ static long mwave_ioctl(struct file *file, unsigned int iocmd,
149 PRINTK_1(TRACE_MWAVE, 149 PRINTK_1(TRACE_MWAVE,
150 "mwavedd::mwave_ioctl, IOCTL_MW_RUN" 150 "mwavedd::mwave_ioctl, IOCTL_MW_RUN"
151 " calling tp3780I_StartDSP\n"); 151 " calling tp3780I_StartDSP\n");
152 lock_kernel(); 152 mutex_lock(&mwave_mutex);
153 retval = tp3780I_StartDSP(&pDrvData->rBDData); 153 retval = tp3780I_StartDSP(&pDrvData->rBDData);
154 unlock_kernel(); 154 mutex_unlock(&mwave_mutex);
155 PRINTK_2(TRACE_MWAVE, 155 PRINTK_2(TRACE_MWAVE,
156 "mwavedd::mwave_ioctl, IOCTL_MW_RUN" 156 "mwavedd::mwave_ioctl, IOCTL_MW_RUN"
157 " retval %x from tp3780I_StartDSP\n", 157 " retval %x from tp3780I_StartDSP\n",
@@ -165,10 +165,10 @@ static long mwave_ioctl(struct file *file, unsigned int iocmd,
165 "mwavedd::mwave_ioctl," 165 "mwavedd::mwave_ioctl,"
166 " IOCTL_MW_DSP_ABILITIES calling" 166 " IOCTL_MW_DSP_ABILITIES calling"
167 " tp3780I_QueryAbilities\n"); 167 " tp3780I_QueryAbilities\n");
168 lock_kernel(); 168 mutex_lock(&mwave_mutex);
169 retval = tp3780I_QueryAbilities(&pDrvData->rBDData, 169 retval = tp3780I_QueryAbilities(&pDrvData->rBDData,
170 &rAbilities); 170 &rAbilities);
171 unlock_kernel(); 171 mutex_unlock(&mwave_mutex);
172 PRINTK_2(TRACE_MWAVE, 172 PRINTK_2(TRACE_MWAVE,
173 "mwavedd::mwave_ioctl, IOCTL_MW_DSP_ABILITIES" 173 "mwavedd::mwave_ioctl, IOCTL_MW_DSP_ABILITIES"
174 " retval %x from tp3780I_QueryAbilities\n", 174 " retval %x from tp3780I_QueryAbilities\n",
@@ -199,13 +199,13 @@ static long mwave_ioctl(struct file *file, unsigned int iocmd,
199 "mwavedd::mwave_ioctl IOCTL_MW_READ_DATA," 199 "mwavedd::mwave_ioctl IOCTL_MW_READ_DATA,"
200 " size %lx, ioarg %lx pusBuffer %p\n", 200 " size %lx, ioarg %lx pusBuffer %p\n",
201 rReadData.ulDataLength, ioarg, pusBuffer); 201 rReadData.ulDataLength, ioarg, pusBuffer);
202 lock_kernel(); 202 mutex_lock(&mwave_mutex);
203 retval = tp3780I_ReadWriteDspDStore(&pDrvData->rBDData, 203 retval = tp3780I_ReadWriteDspDStore(&pDrvData->rBDData,
204 iocmd, 204 iocmd,
205 pusBuffer, 205 pusBuffer,
206 rReadData.ulDataLength, 206 rReadData.ulDataLength,
207 rReadData.usDspAddress); 207 rReadData.usDspAddress);
208 unlock_kernel(); 208 mutex_unlock(&mwave_mutex);
209 } 209 }
210 break; 210 break;
211 211
@@ -223,12 +223,12 @@ static long mwave_ioctl(struct file *file, unsigned int iocmd,
223 " size %lx, ioarg %lx pusBuffer %p\n", 223 " size %lx, ioarg %lx pusBuffer %p\n",
224 rReadData.ulDataLength / 2, ioarg, 224 rReadData.ulDataLength / 2, ioarg,
225 pusBuffer); 225 pusBuffer);
226 lock_kernel(); 226 mutex_lock(&mwave_mutex);
227 retval = tp3780I_ReadWriteDspDStore(&pDrvData->rBDData, 227 retval = tp3780I_ReadWriteDspDStore(&pDrvData->rBDData,
228 iocmd, pusBuffer, 228 iocmd, pusBuffer,
229 rReadData.ulDataLength / 2, 229 rReadData.ulDataLength / 2,
230 rReadData.usDspAddress); 230 rReadData.usDspAddress);
231 unlock_kernel(); 231 mutex_unlock(&mwave_mutex);
232 } 232 }
233 break; 233 break;
234 234
@@ -246,12 +246,12 @@ static long mwave_ioctl(struct file *file, unsigned int iocmd,
246 " size %lx, ioarg %lx pusBuffer %p\n", 246 " size %lx, ioarg %lx pusBuffer %p\n",
247 rWriteData.ulDataLength, ioarg, 247 rWriteData.ulDataLength, ioarg,
248 pusBuffer); 248 pusBuffer);
249 lock_kernel(); 249 mutex_lock(&mwave_mutex);
250 retval = tp3780I_ReadWriteDspDStore(&pDrvData->rBDData, 250 retval = tp3780I_ReadWriteDspDStore(&pDrvData->rBDData,
251 iocmd, pusBuffer, 251 iocmd, pusBuffer,
252 rWriteData.ulDataLength, 252 rWriteData.ulDataLength,
253 rWriteData.usDspAddress); 253 rWriteData.usDspAddress);
254 unlock_kernel(); 254 mutex_unlock(&mwave_mutex);
255 } 255 }
256 break; 256 break;
257 257
@@ -269,12 +269,12 @@ static long mwave_ioctl(struct file *file, unsigned int iocmd,
269 " size %lx, ioarg %lx pusBuffer %p\n", 269 " size %lx, ioarg %lx pusBuffer %p\n",
270 rWriteData.ulDataLength, ioarg, 270 rWriteData.ulDataLength, ioarg,
271 pusBuffer); 271 pusBuffer);
272 lock_kernel(); 272 mutex_lock(&mwave_mutex);
273 retval = tp3780I_ReadWriteDspIStore(&pDrvData->rBDData, 273 retval = tp3780I_ReadWriteDspIStore(&pDrvData->rBDData,
274 iocmd, pusBuffer, 274 iocmd, pusBuffer,
275 rWriteData.ulDataLength, 275 rWriteData.ulDataLength,
276 rWriteData.usDspAddress); 276 rWriteData.usDspAddress);
277 unlock_kernel(); 277 mutex_unlock(&mwave_mutex);
278 } 278 }
279 break; 279 break;
280 280
@@ -295,10 +295,10 @@ static long mwave_ioctl(struct file *file, unsigned int iocmd,
295 ipcnum, 295 ipcnum,
296 pDrvData->IPCs[ipcnum].usIntCount); 296 pDrvData->IPCs[ipcnum].usIntCount);
297 297
298 lock_kernel(); 298 mutex_lock(&mwave_mutex);
299 pDrvData->IPCs[ipcnum].bIsHere = FALSE; 299 pDrvData->IPCs[ipcnum].bIsHere = FALSE;
300 pDrvData->IPCs[ipcnum].bIsEnabled = TRUE; 300 pDrvData->IPCs[ipcnum].bIsEnabled = TRUE;
301 unlock_kernel(); 301 mutex_unlock(&mwave_mutex);
302 302
303 PRINTK_2(TRACE_MWAVE, 303 PRINTK_2(TRACE_MWAVE,
304 "mwavedd::mwave_ioctl IOCTL_MW_REGISTER_IPC" 304 "mwavedd::mwave_ioctl IOCTL_MW_REGISTER_IPC"
@@ -323,7 +323,7 @@ static long mwave_ioctl(struct file *file, unsigned int iocmd,
323 ipcnum, 323 ipcnum,
324 pDrvData->IPCs[ipcnum].usIntCount); 324 pDrvData->IPCs[ipcnum].usIntCount);
325 325
326 lock_kernel(); 326 mutex_lock(&mwave_mutex);
327 if (pDrvData->IPCs[ipcnum].bIsEnabled == TRUE) { 327 if (pDrvData->IPCs[ipcnum].bIsEnabled == TRUE) {
328 DECLARE_WAITQUEUE(wait, current); 328 DECLARE_WAITQUEUE(wait, current);
329 329
@@ -364,7 +364,7 @@ static long mwave_ioctl(struct file *file, unsigned int iocmd,
364 " processing\n", 364 " processing\n",
365 ipcnum); 365 ipcnum);
366 } 366 }
367 unlock_kernel(); 367 mutex_unlock(&mwave_mutex);
368 } 368 }
369 break; 369 break;
370 370
@@ -383,14 +383,14 @@ static long mwave_ioctl(struct file *file, unsigned int iocmd,
383 ipcnum); 383 ipcnum);
384 return -EINVAL; 384 return -EINVAL;
385 } 385 }
386 lock_kernel(); 386 mutex_lock(&mwave_mutex);
387 if (pDrvData->IPCs[ipcnum].bIsEnabled == TRUE) { 387 if (pDrvData->IPCs[ipcnum].bIsEnabled == TRUE) {
388 pDrvData->IPCs[ipcnum].bIsEnabled = FALSE; 388 pDrvData->IPCs[ipcnum].bIsEnabled = FALSE;
389 if (pDrvData->IPCs[ipcnum].bIsHere == TRUE) { 389 if (pDrvData->IPCs[ipcnum].bIsHere == TRUE) {
390 wake_up_interruptible(&pDrvData->IPCs[ipcnum].ipc_wait_queue); 390 wake_up_interruptible(&pDrvData->IPCs[ipcnum].ipc_wait_queue);
391 } 391 }
392 } 392 }
393 unlock_kernel(); 393 mutex_unlock(&mwave_mutex);
394 } 394 }
395 break; 395 break;
396 396
@@ -479,7 +479,8 @@ static const struct file_operations mwave_fops = {
479 .write = mwave_write, 479 .write = mwave_write,
480 .unlocked_ioctl = mwave_ioctl, 480 .unlocked_ioctl = mwave_ioctl,
481 .open = mwave_open, 481 .open = mwave_open,
482 .release = mwave_close 482 .release = mwave_close,
483 .llseek = default_llseek,
483}; 484};
484 485
485 486
diff --git a/drivers/char/mxser.c b/drivers/char/mxser.c
deleted file mode 100644
index 3fc89da856ae..000000000000
--- a/drivers/char/mxser.c
+++ /dev/null
@@ -1,2748 +0,0 @@
1/*
2 * mxser.c -- MOXA Smartio/Industio family multiport serial driver.
3 *
4 * Copyright (C) 1999-2006 Moxa Technologies (support@moxa.com).
5 * Copyright (C) 2006-2008 Jiri Slaby <jirislaby@gmail.com>
6 *
7 * This code is loosely based on the 1.8 moxa driver which is based on
8 * Linux serial driver, written by Linus Torvalds, Theodore T'so and
9 * others.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * Fed through a cleanup, indent and remove of non 2.6 code by Alan Cox
17 * <alan@lxorguk.ukuu.org.uk>. The original 1.8 code is available on
18 * www.moxa.com.
19 * - Fixed x86_64 cleanness
20 */
21
22#include <linux/module.h>
23#include <linux/errno.h>
24#include <linux/signal.h>
25#include <linux/sched.h>
26#include <linux/timer.h>
27#include <linux/interrupt.h>
28#include <linux/tty.h>
29#include <linux/tty_flip.h>
30#include <linux/serial.h>
31#include <linux/serial_reg.h>
32#include <linux/major.h>
33#include <linux/string.h>
34#include <linux/fcntl.h>
35#include <linux/ptrace.h>
36#include <linux/ioport.h>
37#include <linux/mm.h>
38#include <linux/delay.h>
39#include <linux/pci.h>
40#include <linux/bitops.h>
41#include <linux/slab.h>
42
43#include <asm/system.h>
44#include <asm/io.h>
45#include <asm/irq.h>
46#include <asm/uaccess.h>
47
48#include "mxser.h"
49
50#define MXSER_VERSION "2.0.5" /* 1.14 */
51#define MXSERMAJOR 174
52
53#define MXSER_BOARDS 4 /* Max. boards */
54#define MXSER_PORTS_PER_BOARD 8 /* Max. ports per board */
55#define MXSER_PORTS (MXSER_BOARDS * MXSER_PORTS_PER_BOARD)
56#define MXSER_ISR_PASS_LIMIT 100
57
58/*CheckIsMoxaMust return value*/
59#define MOXA_OTHER_UART 0x00
60#define MOXA_MUST_MU150_HWID 0x01
61#define MOXA_MUST_MU860_HWID 0x02
62
63#define WAKEUP_CHARS 256
64
65#define UART_MCR_AFE 0x20
66#define UART_LSR_SPECIAL 0x1E
67
68#define PCI_DEVICE_ID_POS104UL 0x1044
69#define PCI_DEVICE_ID_CB108 0x1080
70#define PCI_DEVICE_ID_CP102UF 0x1023
71#define PCI_DEVICE_ID_CP112UL 0x1120
72#define PCI_DEVICE_ID_CB114 0x1142
73#define PCI_DEVICE_ID_CP114UL 0x1143
74#define PCI_DEVICE_ID_CB134I 0x1341
75#define PCI_DEVICE_ID_CP138U 0x1380
76
77
78#define C168_ASIC_ID 1
79#define C104_ASIC_ID 2
80#define C102_ASIC_ID 0xB
81#define CI132_ASIC_ID 4
82#define CI134_ASIC_ID 3
83#define CI104J_ASIC_ID 5
84
85#define MXSER_HIGHBAUD 1
86#define MXSER_HAS2 2
87
88/* This is only for PCI */
89static const struct {
90 int type;
91 int tx_fifo;
92 int rx_fifo;
93 int xmit_fifo_size;
94 int rx_high_water;
95 int rx_trigger;
96 int rx_low_water;
97 long max_baud;
98} Gpci_uart_info[] = {
99 {MOXA_OTHER_UART, 16, 16, 16, 14, 14, 1, 921600L},
100 {MOXA_MUST_MU150_HWID, 64, 64, 64, 48, 48, 16, 230400L},
101 {MOXA_MUST_MU860_HWID, 128, 128, 128, 96, 96, 32, 921600L}
102};
103#define UART_INFO_NUM ARRAY_SIZE(Gpci_uart_info)
104
105struct mxser_cardinfo {
106 char *name;
107 unsigned int nports;
108 unsigned int flags;
109};
110
111static const struct mxser_cardinfo mxser_cards[] = {
112/* 0*/ { "C168 series", 8, },
113 { "C104 series", 4, },
114 { "CI-104J series", 4, },
115 { "C168H/PCI series", 8, },
116 { "C104H/PCI series", 4, },
117/* 5*/ { "C102 series", 4, MXSER_HAS2 }, /* C102-ISA */
118 { "CI-132 series", 4, MXSER_HAS2 },
119 { "CI-134 series", 4, },
120 { "CP-132 series", 2, },
121 { "CP-114 series", 4, },
122/*10*/ { "CT-114 series", 4, },
123 { "CP-102 series", 2, MXSER_HIGHBAUD },
124 { "CP-104U series", 4, },
125 { "CP-168U series", 8, },
126 { "CP-132U series", 2, },
127/*15*/ { "CP-134U series", 4, },
128 { "CP-104JU series", 4, },
129 { "Moxa UC7000 Serial", 8, }, /* RC7000 */
130 { "CP-118U series", 8, },
131 { "CP-102UL series", 2, },
132/*20*/ { "CP-102U series", 2, },
133 { "CP-118EL series", 8, },
134 { "CP-168EL series", 8, },
135 { "CP-104EL series", 4, },
136 { "CB-108 series", 8, },
137/*25*/ { "CB-114 series", 4, },
138 { "CB-134I series", 4, },
139 { "CP-138U series", 8, },
140 { "POS-104UL series", 4, },
141 { "CP-114UL series", 4, },
142/*30*/ { "CP-102UF series", 2, },
143 { "CP-112UL series", 2, },
144};
145
146/* driver_data correspond to the lines in the structure above
147 see also ISA probe function before you change something */
148static struct pci_device_id mxser_pcibrds[] = {
149 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_C168), .driver_data = 3 },
150 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_C104), .driver_data = 4 },
151 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP132), .driver_data = 8 },
152 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP114), .driver_data = 9 },
153 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CT114), .driver_data = 10 },
154 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP102), .driver_data = 11 },
155 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP104U), .driver_data = 12 },
156 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP168U), .driver_data = 13 },
157 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP132U), .driver_data = 14 },
158 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP134U), .driver_data = 15 },
159 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP104JU),.driver_data = 16 },
160 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_RC7000), .driver_data = 17 },
161 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP118U), .driver_data = 18 },
162 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP102UL),.driver_data = 19 },
163 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP102U), .driver_data = 20 },
164 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP118EL),.driver_data = 21 },
165 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP168EL),.driver_data = 22 },
166 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP104EL),.driver_data = 23 },
167 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CB108), .driver_data = 24 },
168 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CB114), .driver_data = 25 },
169 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CB134I), .driver_data = 26 },
170 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CP138U), .driver_data = 27 },
171 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_POS104UL), .driver_data = 28 },
172 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CP114UL), .driver_data = 29 },
173 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CP102UF), .driver_data = 30 },
174 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CP112UL), .driver_data = 31 },
175 { }
176};
177MODULE_DEVICE_TABLE(pci, mxser_pcibrds);
178
179static unsigned long ioaddr[MXSER_BOARDS];
180static int ttymajor = MXSERMAJOR;
181
182/* Variables for insmod */
183
184MODULE_AUTHOR("Casper Yang");
185MODULE_DESCRIPTION("MOXA Smartio/Industio Family Multiport Board Device Driver");
186module_param_array(ioaddr, ulong, NULL, 0);
187MODULE_PARM_DESC(ioaddr, "ISA io addresses to look for a moxa board");
188module_param(ttymajor, int, 0);
189MODULE_LICENSE("GPL");
190
191struct mxser_log {
192 int tick;
193 unsigned long rxcnt[MXSER_PORTS];
194 unsigned long txcnt[MXSER_PORTS];
195};
196
197struct mxser_mon {
198 unsigned long rxcnt;
199 unsigned long txcnt;
200 unsigned long up_rxcnt;
201 unsigned long up_txcnt;
202 int modem_status;
203 unsigned char hold_reason;
204};
205
206struct mxser_mon_ext {
207 unsigned long rx_cnt[32];
208 unsigned long tx_cnt[32];
209 unsigned long up_rxcnt[32];
210 unsigned long up_txcnt[32];
211 int modem_status[32];
212
213 long baudrate[32];
214 int databits[32];
215 int stopbits[32];
216 int parity[32];
217 int flowctrl[32];
218 int fifo[32];
219 int iftype[32];
220};
221
222struct mxser_board;
223
224struct mxser_port {
225 struct tty_port port;
226 struct mxser_board *board;
227
228 unsigned long ioaddr;
229 unsigned long opmode_ioaddr;
230 int max_baud;
231
232 int rx_high_water;
233 int rx_trigger; /* Rx fifo trigger level */
234 int rx_low_water;
235 int baud_base; /* max. speed */
236 int type; /* UART type */
237
238 int x_char; /* xon/xoff character */
239 int IER; /* Interrupt Enable Register */
240 int MCR; /* Modem control register */
241
242 unsigned char stop_rx;
243 unsigned char ldisc_stop_rx;
244
245 int custom_divisor;
246 unsigned char err_shadow;
247
248 struct async_icount icount; /* kernel counters for 4 input interrupts */
249 int timeout;
250
251 int read_status_mask;
252 int ignore_status_mask;
253 int xmit_fifo_size;
254 int xmit_head;
255 int xmit_tail;
256 int xmit_cnt;
257
258 struct ktermios normal_termios;
259
260 struct mxser_mon mon_data;
261
262 spinlock_t slock;
263};
264
265struct mxser_board {
266 unsigned int idx;
267 int irq;
268 const struct mxser_cardinfo *info;
269 unsigned long vector;
270 unsigned long vector_mask;
271
272 int chip_flag;
273 int uart_type;
274
275 struct mxser_port ports[MXSER_PORTS_PER_BOARD];
276};
277
278struct mxser_mstatus {
279 tcflag_t cflag;
280 int cts;
281 int dsr;
282 int ri;
283 int dcd;
284};
285
286static struct mxser_board mxser_boards[MXSER_BOARDS];
287static struct tty_driver *mxvar_sdriver;
288static struct mxser_log mxvar_log;
289static int mxser_set_baud_method[MXSER_PORTS + 1];
290
291static void mxser_enable_must_enchance_mode(unsigned long baseio)
292{
293 u8 oldlcr;
294 u8 efr;
295
296 oldlcr = inb(baseio + UART_LCR);
297 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
298
299 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
300 efr |= MOXA_MUST_EFR_EFRB_ENABLE;
301
302 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
303 outb(oldlcr, baseio + UART_LCR);
304}
305
306static void mxser_disable_must_enchance_mode(unsigned long baseio)
307{
308 u8 oldlcr;
309 u8 efr;
310
311 oldlcr = inb(baseio + UART_LCR);
312 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
313
314 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
315 efr &= ~MOXA_MUST_EFR_EFRB_ENABLE;
316
317 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
318 outb(oldlcr, baseio + UART_LCR);
319}
320
321static void mxser_set_must_xon1_value(unsigned long baseio, u8 value)
322{
323 u8 oldlcr;
324 u8 efr;
325
326 oldlcr = inb(baseio + UART_LCR);
327 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
328
329 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
330 efr &= ~MOXA_MUST_EFR_BANK_MASK;
331 efr |= MOXA_MUST_EFR_BANK0;
332
333 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
334 outb(value, baseio + MOXA_MUST_XON1_REGISTER);
335 outb(oldlcr, baseio + UART_LCR);
336}
337
338static void mxser_set_must_xoff1_value(unsigned long baseio, u8 value)
339{
340 u8 oldlcr;
341 u8 efr;
342
343 oldlcr = inb(baseio + UART_LCR);
344 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
345
346 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
347 efr &= ~MOXA_MUST_EFR_BANK_MASK;
348 efr |= MOXA_MUST_EFR_BANK0;
349
350 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
351 outb(value, baseio + MOXA_MUST_XOFF1_REGISTER);
352 outb(oldlcr, baseio + UART_LCR);
353}
354
355static void mxser_set_must_fifo_value(struct mxser_port *info)
356{
357 u8 oldlcr;
358 u8 efr;
359
360 oldlcr = inb(info->ioaddr + UART_LCR);
361 outb(MOXA_MUST_ENTER_ENCHANCE, info->ioaddr + UART_LCR);
362
363 efr = inb(info->ioaddr + MOXA_MUST_EFR_REGISTER);
364 efr &= ~MOXA_MUST_EFR_BANK_MASK;
365 efr |= MOXA_MUST_EFR_BANK1;
366
367 outb(efr, info->ioaddr + MOXA_MUST_EFR_REGISTER);
368 outb((u8)info->rx_high_water, info->ioaddr + MOXA_MUST_RBRTH_REGISTER);
369 outb((u8)info->rx_trigger, info->ioaddr + MOXA_MUST_RBRTI_REGISTER);
370 outb((u8)info->rx_low_water, info->ioaddr + MOXA_MUST_RBRTL_REGISTER);
371 outb(oldlcr, info->ioaddr + UART_LCR);
372}
373
374static void mxser_set_must_enum_value(unsigned long baseio, u8 value)
375{
376 u8 oldlcr;
377 u8 efr;
378
379 oldlcr = inb(baseio + UART_LCR);
380 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
381
382 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
383 efr &= ~MOXA_MUST_EFR_BANK_MASK;
384 efr |= MOXA_MUST_EFR_BANK2;
385
386 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
387 outb(value, baseio + MOXA_MUST_ENUM_REGISTER);
388 outb(oldlcr, baseio + UART_LCR);
389}
390
391static void mxser_get_must_hardware_id(unsigned long baseio, u8 *pId)
392{
393 u8 oldlcr;
394 u8 efr;
395
396 oldlcr = inb(baseio + UART_LCR);
397 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
398
399 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
400 efr &= ~MOXA_MUST_EFR_BANK_MASK;
401 efr |= MOXA_MUST_EFR_BANK2;
402
403 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
404 *pId = inb(baseio + MOXA_MUST_HWID_REGISTER);
405 outb(oldlcr, baseio + UART_LCR);
406}
407
408static void SET_MOXA_MUST_NO_SOFTWARE_FLOW_CONTROL(unsigned long baseio)
409{
410 u8 oldlcr;
411 u8 efr;
412
413 oldlcr = inb(baseio + UART_LCR);
414 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
415
416 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
417 efr &= ~MOXA_MUST_EFR_SF_MASK;
418
419 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
420 outb(oldlcr, baseio + UART_LCR);
421}
422
423static void mxser_enable_must_tx_software_flow_control(unsigned long baseio)
424{
425 u8 oldlcr;
426 u8 efr;
427
428 oldlcr = inb(baseio + UART_LCR);
429 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
430
431 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
432 efr &= ~MOXA_MUST_EFR_SF_TX_MASK;
433 efr |= MOXA_MUST_EFR_SF_TX1;
434
435 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
436 outb(oldlcr, baseio + UART_LCR);
437}
438
439static void mxser_disable_must_tx_software_flow_control(unsigned long baseio)
440{
441 u8 oldlcr;
442 u8 efr;
443
444 oldlcr = inb(baseio + UART_LCR);
445 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
446
447 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
448 efr &= ~MOXA_MUST_EFR_SF_TX_MASK;
449
450 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
451 outb(oldlcr, baseio + UART_LCR);
452}
453
454static void mxser_enable_must_rx_software_flow_control(unsigned long baseio)
455{
456 u8 oldlcr;
457 u8 efr;
458
459 oldlcr = inb(baseio + UART_LCR);
460 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
461
462 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
463 efr &= ~MOXA_MUST_EFR_SF_RX_MASK;
464 efr |= MOXA_MUST_EFR_SF_RX1;
465
466 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
467 outb(oldlcr, baseio + UART_LCR);
468}
469
470static void mxser_disable_must_rx_software_flow_control(unsigned long baseio)
471{
472 u8 oldlcr;
473 u8 efr;
474
475 oldlcr = inb(baseio + UART_LCR);
476 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
477
478 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
479 efr &= ~MOXA_MUST_EFR_SF_RX_MASK;
480
481 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
482 outb(oldlcr, baseio + UART_LCR);
483}
484
485#ifdef CONFIG_PCI
486static int __devinit CheckIsMoxaMust(unsigned long io)
487{
488 u8 oldmcr, hwid;
489 int i;
490
491 outb(0, io + UART_LCR);
492 mxser_disable_must_enchance_mode(io);
493 oldmcr = inb(io + UART_MCR);
494 outb(0, io + UART_MCR);
495 mxser_set_must_xon1_value(io, 0x11);
496 if ((hwid = inb(io + UART_MCR)) != 0) {
497 outb(oldmcr, io + UART_MCR);
498 return MOXA_OTHER_UART;
499 }
500
501 mxser_get_must_hardware_id(io, &hwid);
502 for (i = 1; i < UART_INFO_NUM; i++) { /* 0 = OTHER_UART */
503 if (hwid == Gpci_uart_info[i].type)
504 return (int)hwid;
505 }
506 return MOXA_OTHER_UART;
507}
508#endif
509
510static void process_txrx_fifo(struct mxser_port *info)
511{
512 int i;
513
514 if ((info->type == PORT_16450) || (info->type == PORT_8250)) {
515 info->rx_trigger = 1;
516 info->rx_high_water = 1;
517 info->rx_low_water = 1;
518 info->xmit_fifo_size = 1;
519 } else
520 for (i = 0; i < UART_INFO_NUM; i++)
521 if (info->board->chip_flag == Gpci_uart_info[i].type) {
522 info->rx_trigger = Gpci_uart_info[i].rx_trigger;
523 info->rx_low_water = Gpci_uart_info[i].rx_low_water;
524 info->rx_high_water = Gpci_uart_info[i].rx_high_water;
525 info->xmit_fifo_size = Gpci_uart_info[i].xmit_fifo_size;
526 break;
527 }
528}
529
530static unsigned char mxser_get_msr(int baseaddr, int mode, int port)
531{
532 static unsigned char mxser_msr[MXSER_PORTS + 1];
533 unsigned char status = 0;
534
535 status = inb(baseaddr + UART_MSR);
536
537 mxser_msr[port] &= 0x0F;
538 mxser_msr[port] |= status;
539 status = mxser_msr[port];
540 if (mode)
541 mxser_msr[port] = 0;
542
543 return status;
544}
545
546static int mxser_carrier_raised(struct tty_port *port)
547{
548 struct mxser_port *mp = container_of(port, struct mxser_port, port);
549 return (inb(mp->ioaddr + UART_MSR) & UART_MSR_DCD)?1:0;
550}
551
552static void mxser_dtr_rts(struct tty_port *port, int on)
553{
554 struct mxser_port *mp = container_of(port, struct mxser_port, port);
555 unsigned long flags;
556
557 spin_lock_irqsave(&mp->slock, flags);
558 if (on)
559 outb(inb(mp->ioaddr + UART_MCR) |
560 UART_MCR_DTR | UART_MCR_RTS, mp->ioaddr + UART_MCR);
561 else
562 outb(inb(mp->ioaddr + UART_MCR)&~(UART_MCR_DTR | UART_MCR_RTS),
563 mp->ioaddr + UART_MCR);
564 spin_unlock_irqrestore(&mp->slock, flags);
565}
566
567static int mxser_set_baud(struct tty_struct *tty, long newspd)
568{
569 struct mxser_port *info = tty->driver_data;
570 int quot = 0, baud;
571 unsigned char cval;
572
573 if (!info->ioaddr)
574 return -1;
575
576 if (newspd > info->max_baud)
577 return -1;
578
579 if (newspd == 134) {
580 quot = 2 * info->baud_base / 269;
581 tty_encode_baud_rate(tty, 134, 134);
582 } else if (newspd) {
583 quot = info->baud_base / newspd;
584 if (quot == 0)
585 quot = 1;
586 baud = info->baud_base/quot;
587 tty_encode_baud_rate(tty, baud, baud);
588 } else {
589 quot = 0;
590 }
591
592 info->timeout = ((info->xmit_fifo_size * HZ * 10 * quot) / info->baud_base);
593 info->timeout += HZ / 50; /* Add .02 seconds of slop */
594
595 if (quot) {
596 info->MCR |= UART_MCR_DTR;
597 outb(info->MCR, info->ioaddr + UART_MCR);
598 } else {
599 info->MCR &= ~UART_MCR_DTR;
600 outb(info->MCR, info->ioaddr + UART_MCR);
601 return 0;
602 }
603
604 cval = inb(info->ioaddr + UART_LCR);
605
606 outb(cval | UART_LCR_DLAB, info->ioaddr + UART_LCR); /* set DLAB */
607
608 outb(quot & 0xff, info->ioaddr + UART_DLL); /* LS of divisor */
609 outb(quot >> 8, info->ioaddr + UART_DLM); /* MS of divisor */
610 outb(cval, info->ioaddr + UART_LCR); /* reset DLAB */
611
612#ifdef BOTHER
613 if (C_BAUD(tty) == BOTHER) {
614 quot = info->baud_base % newspd;
615 quot *= 8;
616 if (quot % newspd > newspd / 2) {
617 quot /= newspd;
618 quot++;
619 } else
620 quot /= newspd;
621
622 mxser_set_must_enum_value(info->ioaddr, quot);
623 } else
624#endif
625 mxser_set_must_enum_value(info->ioaddr, 0);
626
627 return 0;
628}
629
630/*
631 * This routine is called to set the UART divisor registers to match
632 * the specified baud rate for a serial port.
633 */
634static int mxser_change_speed(struct tty_struct *tty,
635 struct ktermios *old_termios)
636{
637 struct mxser_port *info = tty->driver_data;
638 unsigned cflag, cval, fcr;
639 int ret = 0;
640 unsigned char status;
641
642 cflag = tty->termios->c_cflag;
643 if (!info->ioaddr)
644 return ret;
645
646 if (mxser_set_baud_method[tty->index] == 0)
647 mxser_set_baud(tty, tty_get_baud_rate(tty));
648
649 /* byte size and parity */
650 switch (cflag & CSIZE) {
651 case CS5:
652 cval = 0x00;
653 break;
654 case CS6:
655 cval = 0x01;
656 break;
657 case CS7:
658 cval = 0x02;
659 break;
660 case CS8:
661 cval = 0x03;
662 break;
663 default:
664 cval = 0x00;
665 break; /* too keep GCC shut... */
666 }
667 if (cflag & CSTOPB)
668 cval |= 0x04;
669 if (cflag & PARENB)
670 cval |= UART_LCR_PARITY;
671 if (!(cflag & PARODD))
672 cval |= UART_LCR_EPAR;
673 if (cflag & CMSPAR)
674 cval |= UART_LCR_SPAR;
675
676 if ((info->type == PORT_8250) || (info->type == PORT_16450)) {
677 if (info->board->chip_flag) {
678 fcr = UART_FCR_ENABLE_FIFO;
679 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
680 mxser_set_must_fifo_value(info);
681 } else
682 fcr = 0;
683 } else {
684 fcr = UART_FCR_ENABLE_FIFO;
685 if (info->board->chip_flag) {
686 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
687 mxser_set_must_fifo_value(info);
688 } else {
689 switch (info->rx_trigger) {
690 case 1:
691 fcr |= UART_FCR_TRIGGER_1;
692 break;
693 case 4:
694 fcr |= UART_FCR_TRIGGER_4;
695 break;
696 case 8:
697 fcr |= UART_FCR_TRIGGER_8;
698 break;
699 default:
700 fcr |= UART_FCR_TRIGGER_14;
701 break;
702 }
703 }
704 }
705
706 /* CTS flow control flag and modem status interrupts */
707 info->IER &= ~UART_IER_MSI;
708 info->MCR &= ~UART_MCR_AFE;
709 if (cflag & CRTSCTS) {
710 info->port.flags |= ASYNC_CTS_FLOW;
711 info->IER |= UART_IER_MSI;
712 if ((info->type == PORT_16550A) || (info->board->chip_flag)) {
713 info->MCR |= UART_MCR_AFE;
714 } else {
715 status = inb(info->ioaddr + UART_MSR);
716 if (tty->hw_stopped) {
717 if (status & UART_MSR_CTS) {
718 tty->hw_stopped = 0;
719 if (info->type != PORT_16550A &&
720 !info->board->chip_flag) {
721 outb(info->IER & ~UART_IER_THRI,
722 info->ioaddr +
723 UART_IER);
724 info->IER |= UART_IER_THRI;
725 outb(info->IER, info->ioaddr +
726 UART_IER);
727 }
728 tty_wakeup(tty);
729 }
730 } else {
731 if (!(status & UART_MSR_CTS)) {
732 tty->hw_stopped = 1;
733 if ((info->type != PORT_16550A) &&
734 (!info->board->chip_flag)) {
735 info->IER &= ~UART_IER_THRI;
736 outb(info->IER, info->ioaddr +
737 UART_IER);
738 }
739 }
740 }
741 }
742 } else {
743 info->port.flags &= ~ASYNC_CTS_FLOW;
744 }
745 outb(info->MCR, info->ioaddr + UART_MCR);
746 if (cflag & CLOCAL) {
747 info->port.flags &= ~ASYNC_CHECK_CD;
748 } else {
749 info->port.flags |= ASYNC_CHECK_CD;
750 info->IER |= UART_IER_MSI;
751 }
752 outb(info->IER, info->ioaddr + UART_IER);
753
754 /*
755 * Set up parity check flag
756 */
757 info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
758 if (I_INPCK(tty))
759 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
760 if (I_BRKINT(tty) || I_PARMRK(tty))
761 info->read_status_mask |= UART_LSR_BI;
762
763 info->ignore_status_mask = 0;
764
765 if (I_IGNBRK(tty)) {
766 info->ignore_status_mask |= UART_LSR_BI;
767 info->read_status_mask |= UART_LSR_BI;
768 /*
769 * If we're ignore parity and break indicators, ignore
770 * overruns too. (For real raw support).
771 */
772 if (I_IGNPAR(tty)) {
773 info->ignore_status_mask |=
774 UART_LSR_OE |
775 UART_LSR_PE |
776 UART_LSR_FE;
777 info->read_status_mask |=
778 UART_LSR_OE |
779 UART_LSR_PE |
780 UART_LSR_FE;
781 }
782 }
783 if (info->board->chip_flag) {
784 mxser_set_must_xon1_value(info->ioaddr, START_CHAR(tty));
785 mxser_set_must_xoff1_value(info->ioaddr, STOP_CHAR(tty));
786 if (I_IXON(tty)) {
787 mxser_enable_must_rx_software_flow_control(
788 info->ioaddr);
789 } else {
790 mxser_disable_must_rx_software_flow_control(
791 info->ioaddr);
792 }
793 if (I_IXOFF(tty)) {
794 mxser_enable_must_tx_software_flow_control(
795 info->ioaddr);
796 } else {
797 mxser_disable_must_tx_software_flow_control(
798 info->ioaddr);
799 }
800 }
801
802
803 outb(fcr, info->ioaddr + UART_FCR); /* set fcr */
804 outb(cval, info->ioaddr + UART_LCR);
805
806 return ret;
807}
808
809static void mxser_check_modem_status(struct tty_struct *tty,
810 struct mxser_port *port, int status)
811{
812 /* update input line counters */
813 if (status & UART_MSR_TERI)
814 port->icount.rng++;
815 if (status & UART_MSR_DDSR)
816 port->icount.dsr++;
817 if (status & UART_MSR_DDCD)
818 port->icount.dcd++;
819 if (status & UART_MSR_DCTS)
820 port->icount.cts++;
821 port->mon_data.modem_status = status;
822 wake_up_interruptible(&port->port.delta_msr_wait);
823
824 if ((port->port.flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
825 if (status & UART_MSR_DCD)
826 wake_up_interruptible(&port->port.open_wait);
827 }
828
829 if (port->port.flags & ASYNC_CTS_FLOW) {
830 if (tty->hw_stopped) {
831 if (status & UART_MSR_CTS) {
832 tty->hw_stopped = 0;
833
834 if ((port->type != PORT_16550A) &&
835 (!port->board->chip_flag)) {
836 outb(port->IER & ~UART_IER_THRI,
837 port->ioaddr + UART_IER);
838 port->IER |= UART_IER_THRI;
839 outb(port->IER, port->ioaddr +
840 UART_IER);
841 }
842 tty_wakeup(tty);
843 }
844 } else {
845 if (!(status & UART_MSR_CTS)) {
846 tty->hw_stopped = 1;
847 if (port->type != PORT_16550A &&
848 !port->board->chip_flag) {
849 port->IER &= ~UART_IER_THRI;
850 outb(port->IER, port->ioaddr +
851 UART_IER);
852 }
853 }
854 }
855 }
856}
857
858static int mxser_activate(struct tty_port *port, struct tty_struct *tty)
859{
860 struct mxser_port *info = container_of(port, struct mxser_port, port);
861 unsigned long page;
862 unsigned long flags;
863
864 page = __get_free_page(GFP_KERNEL);
865 if (!page)
866 return -ENOMEM;
867
868 spin_lock_irqsave(&info->slock, flags);
869
870 if (!info->ioaddr || !info->type) {
871 set_bit(TTY_IO_ERROR, &tty->flags);
872 free_page(page);
873 spin_unlock_irqrestore(&info->slock, flags);
874 return 0;
875 }
876 info->port.xmit_buf = (unsigned char *) page;
877
878 /*
879 * Clear the FIFO buffers and disable them
880 * (they will be reenabled in mxser_change_speed())
881 */
882 if (info->board->chip_flag)
883 outb((UART_FCR_CLEAR_RCVR |
884 UART_FCR_CLEAR_XMIT |
885 MOXA_MUST_FCR_GDA_MODE_ENABLE), info->ioaddr + UART_FCR);
886 else
887 outb((UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
888 info->ioaddr + UART_FCR);
889
890 /*
891 * At this point there's no way the LSR could still be 0xFF;
892 * if it is, then bail out, because there's likely no UART
893 * here.
894 */
895 if (inb(info->ioaddr + UART_LSR) == 0xff) {
896 spin_unlock_irqrestore(&info->slock, flags);
897 if (capable(CAP_SYS_ADMIN)) {
898 set_bit(TTY_IO_ERROR, &tty->flags);
899 return 0;
900 } else
901 return -ENODEV;
902 }
903
904 /*
905 * Clear the interrupt registers.
906 */
907 (void) inb(info->ioaddr + UART_LSR);
908 (void) inb(info->ioaddr + UART_RX);
909 (void) inb(info->ioaddr + UART_IIR);
910 (void) inb(info->ioaddr + UART_MSR);
911
912 /*
913 * Now, initialize the UART
914 */
915 outb(UART_LCR_WLEN8, info->ioaddr + UART_LCR); /* reset DLAB */
916 info->MCR = UART_MCR_DTR | UART_MCR_RTS;
917 outb(info->MCR, info->ioaddr + UART_MCR);
918
919 /*
920 * Finally, enable interrupts
921 */
922 info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
923
924 if (info->board->chip_flag)
925 info->IER |= MOXA_MUST_IER_EGDAI;
926 outb(info->IER, info->ioaddr + UART_IER); /* enable interrupts */
927
928 /*
929 * And clear the interrupt registers again for luck.
930 */
931 (void) inb(info->ioaddr + UART_LSR);
932 (void) inb(info->ioaddr + UART_RX);
933 (void) inb(info->ioaddr + UART_IIR);
934 (void) inb(info->ioaddr + UART_MSR);
935
936 clear_bit(TTY_IO_ERROR, &tty->flags);
937 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
938
939 /*
940 * and set the speed of the serial port
941 */
942 mxser_change_speed(tty, NULL);
943 spin_unlock_irqrestore(&info->slock, flags);
944
945 return 0;
946}
947
948/*
949 * This routine will shutdown a serial port
950 */
951static void mxser_shutdown_port(struct tty_port *port)
952{
953 struct mxser_port *info = container_of(port, struct mxser_port, port);
954 unsigned long flags;
955
956 spin_lock_irqsave(&info->slock, flags);
957
958 /*
959 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
960 * here so the queue might never be waken up
961 */
962 wake_up_interruptible(&info->port.delta_msr_wait);
963
964 /*
965 * Free the xmit buffer, if necessary
966 */
967 if (info->port.xmit_buf) {
968 free_page((unsigned long) info->port.xmit_buf);
969 info->port.xmit_buf = NULL;
970 }
971
972 info->IER = 0;
973 outb(0x00, info->ioaddr + UART_IER);
974
975 /* clear Rx/Tx FIFO's */
976 if (info->board->chip_flag)
977 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT |
978 MOXA_MUST_FCR_GDA_MODE_ENABLE,
979 info->ioaddr + UART_FCR);
980 else
981 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT,
982 info->ioaddr + UART_FCR);
983
984 /* read data port to reset things */
985 (void) inb(info->ioaddr + UART_RX);
986
987
988 if (info->board->chip_flag)
989 SET_MOXA_MUST_NO_SOFTWARE_FLOW_CONTROL(info->ioaddr);
990
991 spin_unlock_irqrestore(&info->slock, flags);
992}
993
994/*
995 * This routine is called whenever a serial port is opened. It
996 * enables interrupts for a serial port, linking in its async structure into
997 * the IRQ chain. It also performs the serial-specific
998 * initialization for the tty structure.
999 */
1000static int mxser_open(struct tty_struct *tty, struct file *filp)
1001{
1002 struct mxser_port *info;
1003 int line;
1004
1005 line = tty->index;
1006 if (line == MXSER_PORTS)
1007 return 0;
1008 if (line < 0 || line > MXSER_PORTS)
1009 return -ENODEV;
1010 info = &mxser_boards[line / MXSER_PORTS_PER_BOARD].ports[line % MXSER_PORTS_PER_BOARD];
1011 if (!info->ioaddr)
1012 return -ENODEV;
1013
1014 tty->driver_data = info;
1015 return tty_port_open(&info->port, tty, filp);
1016}
1017
1018static void mxser_flush_buffer(struct tty_struct *tty)
1019{
1020 struct mxser_port *info = tty->driver_data;
1021 char fcr;
1022 unsigned long flags;
1023
1024
1025 spin_lock_irqsave(&info->slock, flags);
1026 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1027
1028 fcr = inb(info->ioaddr + UART_FCR);
1029 outb((fcr | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
1030 info->ioaddr + UART_FCR);
1031 outb(fcr, info->ioaddr + UART_FCR);
1032
1033 spin_unlock_irqrestore(&info->slock, flags);
1034
1035 tty_wakeup(tty);
1036}
1037
1038
1039static void mxser_close_port(struct tty_port *port)
1040{
1041 struct mxser_port *info = container_of(port, struct mxser_port, port);
1042 unsigned long timeout;
1043 /*
1044 * At this point we stop accepting input. To do this, we
1045 * disable the receive line status interrupts, and tell the
1046 * interrupt driver to stop checking the data ready bit in the
1047 * line status register.
1048 */
1049 info->IER &= ~UART_IER_RLSI;
1050 if (info->board->chip_flag)
1051 info->IER &= ~MOXA_MUST_RECV_ISR;
1052
1053 outb(info->IER, info->ioaddr + UART_IER);
1054 /*
1055 * Before we drop DTR, make sure the UART transmitter
1056 * has completely drained; this is especially
1057 * important if there is a transmit FIFO!
1058 */
1059 timeout = jiffies + HZ;
1060 while (!(inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT)) {
1061 schedule_timeout_interruptible(5);
1062 if (time_after(jiffies, timeout))
1063 break;
1064 }
1065}
1066
1067/*
1068 * This routine is called when the serial port gets closed. First, we
1069 * wait for the last remaining data to be sent. Then, we unlink its
1070 * async structure from the interrupt chain if necessary, and we free
1071 * that IRQ if nothing is left in the chain.
1072 */
1073static void mxser_close(struct tty_struct *tty, struct file *filp)
1074{
1075 struct mxser_port *info = tty->driver_data;
1076 struct tty_port *port = &info->port;
1077
1078 if (tty->index == MXSER_PORTS || info == NULL)
1079 return;
1080 if (tty_port_close_start(port, tty, filp) == 0)
1081 return;
1082 mutex_lock(&port->mutex);
1083 mxser_close_port(port);
1084 mxser_flush_buffer(tty);
1085 mxser_shutdown_port(port);
1086 clear_bit(ASYNCB_INITIALIZED, &port->flags);
1087 mutex_unlock(&port->mutex);
1088 /* Right now the tty_port set is done outside of the close_end helper
1089 as we don't yet have everyone using refcounts */
1090 tty_port_close_end(port, tty);
1091 tty_port_tty_set(port, NULL);
1092}
1093
1094static int mxser_write(struct tty_struct *tty, const unsigned char *buf, int count)
1095{
1096 int c, total = 0;
1097 struct mxser_port *info = tty->driver_data;
1098 unsigned long flags;
1099
1100 if (!info->port.xmit_buf)
1101 return 0;
1102
1103 while (1) {
1104 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1105 SERIAL_XMIT_SIZE - info->xmit_head));
1106 if (c <= 0)
1107 break;
1108
1109 memcpy(info->port.xmit_buf + info->xmit_head, buf, c);
1110 spin_lock_irqsave(&info->slock, flags);
1111 info->xmit_head = (info->xmit_head + c) &
1112 (SERIAL_XMIT_SIZE - 1);
1113 info->xmit_cnt += c;
1114 spin_unlock_irqrestore(&info->slock, flags);
1115
1116 buf += c;
1117 count -= c;
1118 total += c;
1119 }
1120
1121 if (info->xmit_cnt && !tty->stopped) {
1122 if (!tty->hw_stopped ||
1123 (info->type == PORT_16550A) ||
1124 (info->board->chip_flag)) {
1125 spin_lock_irqsave(&info->slock, flags);
1126 outb(info->IER & ~UART_IER_THRI, info->ioaddr +
1127 UART_IER);
1128 info->IER |= UART_IER_THRI;
1129 outb(info->IER, info->ioaddr + UART_IER);
1130 spin_unlock_irqrestore(&info->slock, flags);
1131 }
1132 }
1133 return total;
1134}
1135
1136static int mxser_put_char(struct tty_struct *tty, unsigned char ch)
1137{
1138 struct mxser_port *info = tty->driver_data;
1139 unsigned long flags;
1140
1141 if (!info->port.xmit_buf)
1142 return 0;
1143
1144 if (info->xmit_cnt >= SERIAL_XMIT_SIZE - 1)
1145 return 0;
1146
1147 spin_lock_irqsave(&info->slock, flags);
1148 info->port.xmit_buf[info->xmit_head++] = ch;
1149 info->xmit_head &= SERIAL_XMIT_SIZE - 1;
1150 info->xmit_cnt++;
1151 spin_unlock_irqrestore(&info->slock, flags);
1152 if (!tty->stopped) {
1153 if (!tty->hw_stopped ||
1154 (info->type == PORT_16550A) ||
1155 info->board->chip_flag) {
1156 spin_lock_irqsave(&info->slock, flags);
1157 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
1158 info->IER |= UART_IER_THRI;
1159 outb(info->IER, info->ioaddr + UART_IER);
1160 spin_unlock_irqrestore(&info->slock, flags);
1161 }
1162 }
1163 return 1;
1164}
1165
1166
1167static void mxser_flush_chars(struct tty_struct *tty)
1168{
1169 struct mxser_port *info = tty->driver_data;
1170 unsigned long flags;
1171
1172 if (info->xmit_cnt <= 0 || tty->stopped || !info->port.xmit_buf ||
1173 (tty->hw_stopped && info->type != PORT_16550A &&
1174 !info->board->chip_flag))
1175 return;
1176
1177 spin_lock_irqsave(&info->slock, flags);
1178
1179 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
1180 info->IER |= UART_IER_THRI;
1181 outb(info->IER, info->ioaddr + UART_IER);
1182
1183 spin_unlock_irqrestore(&info->slock, flags);
1184}
1185
1186static int mxser_write_room(struct tty_struct *tty)
1187{
1188 struct mxser_port *info = tty->driver_data;
1189 int ret;
1190
1191 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
1192 return ret < 0 ? 0 : ret;
1193}
1194
1195static int mxser_chars_in_buffer(struct tty_struct *tty)
1196{
1197 struct mxser_port *info = tty->driver_data;
1198 return info->xmit_cnt;
1199}
1200
1201/*
1202 * ------------------------------------------------------------
1203 * friends of mxser_ioctl()
1204 * ------------------------------------------------------------
1205 */
1206static int mxser_get_serial_info(struct tty_struct *tty,
1207 struct serial_struct __user *retinfo)
1208{
1209 struct mxser_port *info = tty->driver_data;
1210 struct serial_struct tmp = {
1211 .type = info->type,
1212 .line = tty->index,
1213 .port = info->ioaddr,
1214 .irq = info->board->irq,
1215 .flags = info->port.flags,
1216 .baud_base = info->baud_base,
1217 .close_delay = info->port.close_delay,
1218 .closing_wait = info->port.closing_wait,
1219 .custom_divisor = info->custom_divisor,
1220 .hub6 = 0
1221 };
1222 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1223 return -EFAULT;
1224 return 0;
1225}
1226
1227static int mxser_set_serial_info(struct tty_struct *tty,
1228 struct serial_struct __user *new_info)
1229{
1230 struct mxser_port *info = tty->driver_data;
1231 struct tty_port *port = &info->port;
1232 struct serial_struct new_serial;
1233 speed_t baud;
1234 unsigned long sl_flags;
1235 unsigned int flags;
1236 int retval = 0;
1237
1238 if (!new_info || !info->ioaddr)
1239 return -ENODEV;
1240 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
1241 return -EFAULT;
1242
1243 if (new_serial.irq != info->board->irq ||
1244 new_serial.port != info->ioaddr)
1245 return -EINVAL;
1246
1247 flags = port->flags & ASYNC_SPD_MASK;
1248
1249 if (!capable(CAP_SYS_ADMIN)) {
1250 if ((new_serial.baud_base != info->baud_base) ||
1251 (new_serial.close_delay != info->port.close_delay) ||
1252 ((new_serial.flags & ~ASYNC_USR_MASK) != (info->port.flags & ~ASYNC_USR_MASK)))
1253 return -EPERM;
1254 info->port.flags = ((info->port.flags & ~ASYNC_USR_MASK) |
1255 (new_serial.flags & ASYNC_USR_MASK));
1256 } else {
1257 /*
1258 * OK, past this point, all the error checking has been done.
1259 * At this point, we start making changes.....
1260 */
1261 port->flags = ((port->flags & ~ASYNC_FLAGS) |
1262 (new_serial.flags & ASYNC_FLAGS));
1263 port->close_delay = new_serial.close_delay * HZ / 100;
1264 port->closing_wait = new_serial.closing_wait * HZ / 100;
1265 tty->low_latency = (port->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
1266 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST &&
1267 (new_serial.baud_base != info->baud_base ||
1268 new_serial.custom_divisor !=
1269 info->custom_divisor)) {
1270 if (new_serial.custom_divisor == 0)
1271 return -EINVAL;
1272 baud = new_serial.baud_base / new_serial.custom_divisor;
1273 tty_encode_baud_rate(tty, baud, baud);
1274 }
1275 }
1276
1277 info->type = new_serial.type;
1278
1279 process_txrx_fifo(info);
1280
1281 if (test_bit(ASYNCB_INITIALIZED, &port->flags)) {
1282 if (flags != (port->flags & ASYNC_SPD_MASK)) {
1283 spin_lock_irqsave(&info->slock, sl_flags);
1284 mxser_change_speed(tty, NULL);
1285 spin_unlock_irqrestore(&info->slock, sl_flags);
1286 }
1287 } else {
1288 retval = mxser_activate(port, tty);
1289 if (retval == 0)
1290 set_bit(ASYNCB_INITIALIZED, &port->flags);
1291 }
1292 return retval;
1293}
1294
1295/*
1296 * mxser_get_lsr_info - get line status register info
1297 *
1298 * Purpose: Let user call ioctl() to get info when the UART physically
1299 * is emptied. On bus types like RS485, the transmitter must
1300 * release the bus after transmitting. This must be done when
1301 * the transmit shift register is empty, not be done when the
1302 * transmit holding register is empty. This functionality
1303 * allows an RS485 driver to be written in user space.
1304 */
1305static int mxser_get_lsr_info(struct mxser_port *info,
1306 unsigned int __user *value)
1307{
1308 unsigned char status;
1309 unsigned int result;
1310 unsigned long flags;
1311
1312 spin_lock_irqsave(&info->slock, flags);
1313 status = inb(info->ioaddr + UART_LSR);
1314 spin_unlock_irqrestore(&info->slock, flags);
1315 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
1316 return put_user(result, value);
1317}
1318
1319static int mxser_tiocmget(struct tty_struct *tty, struct file *file)
1320{
1321 struct mxser_port *info = tty->driver_data;
1322 unsigned char control, status;
1323 unsigned long flags;
1324
1325
1326 if (tty->index == MXSER_PORTS)
1327 return -ENOIOCTLCMD;
1328 if (test_bit(TTY_IO_ERROR, &tty->flags))
1329 return -EIO;
1330
1331 control = info->MCR;
1332
1333 spin_lock_irqsave(&info->slock, flags);
1334 status = inb(info->ioaddr + UART_MSR);
1335 if (status & UART_MSR_ANY_DELTA)
1336 mxser_check_modem_status(tty, info, status);
1337 spin_unlock_irqrestore(&info->slock, flags);
1338 return ((control & UART_MCR_RTS) ? TIOCM_RTS : 0) |
1339 ((control & UART_MCR_DTR) ? TIOCM_DTR : 0) |
1340 ((status & UART_MSR_DCD) ? TIOCM_CAR : 0) |
1341 ((status & UART_MSR_RI) ? TIOCM_RNG : 0) |
1342 ((status & UART_MSR_DSR) ? TIOCM_DSR : 0) |
1343 ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
1344}
1345
1346static int mxser_tiocmset(struct tty_struct *tty, struct file *file,
1347 unsigned int set, unsigned int clear)
1348{
1349 struct mxser_port *info = tty->driver_data;
1350 unsigned long flags;
1351
1352
1353 if (tty->index == MXSER_PORTS)
1354 return -ENOIOCTLCMD;
1355 if (test_bit(TTY_IO_ERROR, &tty->flags))
1356 return -EIO;
1357
1358 spin_lock_irqsave(&info->slock, flags);
1359
1360 if (set & TIOCM_RTS)
1361 info->MCR |= UART_MCR_RTS;
1362 if (set & TIOCM_DTR)
1363 info->MCR |= UART_MCR_DTR;
1364
1365 if (clear & TIOCM_RTS)
1366 info->MCR &= ~UART_MCR_RTS;
1367 if (clear & TIOCM_DTR)
1368 info->MCR &= ~UART_MCR_DTR;
1369
1370 outb(info->MCR, info->ioaddr + UART_MCR);
1371 spin_unlock_irqrestore(&info->slock, flags);
1372 return 0;
1373}
1374
1375static int __init mxser_program_mode(int port)
1376{
1377 int id, i, j, n;
1378
1379 outb(0, port);
1380 outb(0, port);
1381 outb(0, port);
1382 (void)inb(port);
1383 (void)inb(port);
1384 outb(0, port);
1385 (void)inb(port);
1386
1387 id = inb(port + 1) & 0x1F;
1388 if ((id != C168_ASIC_ID) &&
1389 (id != C104_ASIC_ID) &&
1390 (id != C102_ASIC_ID) &&
1391 (id != CI132_ASIC_ID) &&
1392 (id != CI134_ASIC_ID) &&
1393 (id != CI104J_ASIC_ID))
1394 return -1;
1395 for (i = 0, j = 0; i < 4; i++) {
1396 n = inb(port + 2);
1397 if (n == 'M') {
1398 j = 1;
1399 } else if ((j == 1) && (n == 1)) {
1400 j = 2;
1401 break;
1402 } else
1403 j = 0;
1404 }
1405 if (j != 2)
1406 id = -2;
1407 return id;
1408}
1409
1410static void __init mxser_normal_mode(int port)
1411{
1412 int i, n;
1413
1414 outb(0xA5, port + 1);
1415 outb(0x80, port + 3);
1416 outb(12, port + 0); /* 9600 bps */
1417 outb(0, port + 1);
1418 outb(0x03, port + 3); /* 8 data bits */
1419 outb(0x13, port + 4); /* loop back mode */
1420 for (i = 0; i < 16; i++) {
1421 n = inb(port + 5);
1422 if ((n & 0x61) == 0x60)
1423 break;
1424 if ((n & 1) == 1)
1425 (void)inb(port);
1426 }
1427 outb(0x00, port + 4);
1428}
1429
1430#define CHIP_SK 0x01 /* Serial Data Clock in Eprom */
1431#define CHIP_DO 0x02 /* Serial Data Output in Eprom */
1432#define CHIP_CS 0x04 /* Serial Chip Select in Eprom */
1433#define CHIP_DI 0x08 /* Serial Data Input in Eprom */
1434#define EN_CCMD 0x000 /* Chip's command register */
1435#define EN0_RSARLO 0x008 /* Remote start address reg 0 */
1436#define EN0_RSARHI 0x009 /* Remote start address reg 1 */
1437#define EN0_RCNTLO 0x00A /* Remote byte count reg WR */
1438#define EN0_RCNTHI 0x00B /* Remote byte count reg WR */
1439#define EN0_DCFG 0x00E /* Data configuration reg WR */
1440#define EN0_PORT 0x010 /* Rcv missed frame error counter RD */
1441#define ENC_PAGE0 0x000 /* Select page 0 of chip registers */
1442#define ENC_PAGE3 0x0C0 /* Select page 3 of chip registers */
1443static int __init mxser_read_register(int port, unsigned short *regs)
1444{
1445 int i, k, value, id;
1446 unsigned int j;
1447
1448 id = mxser_program_mode(port);
1449 if (id < 0)
1450 return id;
1451 for (i = 0; i < 14; i++) {
1452 k = (i & 0x3F) | 0x180;
1453 for (j = 0x100; j > 0; j >>= 1) {
1454 outb(CHIP_CS, port);
1455 if (k & j) {
1456 outb(CHIP_CS | CHIP_DO, port);
1457 outb(CHIP_CS | CHIP_DO | CHIP_SK, port); /* A? bit of read */
1458 } else {
1459 outb(CHIP_CS, port);
1460 outb(CHIP_CS | CHIP_SK, port); /* A? bit of read */
1461 }
1462 }
1463 (void)inb(port);
1464 value = 0;
1465 for (k = 0, j = 0x8000; k < 16; k++, j >>= 1) {
1466 outb(CHIP_CS, port);
1467 outb(CHIP_CS | CHIP_SK, port);
1468 if (inb(port) & CHIP_DI)
1469 value |= j;
1470 }
1471 regs[i] = value;
1472 outb(0, port);
1473 }
1474 mxser_normal_mode(port);
1475 return id;
1476}
1477
1478static int mxser_ioctl_special(unsigned int cmd, void __user *argp)
1479{
1480 struct mxser_port *ip;
1481 struct tty_port *port;
1482 struct tty_struct *tty;
1483 int result, status;
1484 unsigned int i, j;
1485 int ret = 0;
1486
1487 switch (cmd) {
1488 case MOXA_GET_MAJOR:
1489 if (printk_ratelimit())
1490 printk(KERN_WARNING "mxser: '%s' uses deprecated ioctl "
1491 "%x (GET_MAJOR), fix your userspace\n",
1492 current->comm, cmd);
1493 return put_user(ttymajor, (int __user *)argp);
1494
1495 case MOXA_CHKPORTENABLE:
1496 result = 0;
1497 for (i = 0; i < MXSER_BOARDS; i++)
1498 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++)
1499 if (mxser_boards[i].ports[j].ioaddr)
1500 result |= (1 << i);
1501 return put_user(result, (unsigned long __user *)argp);
1502 case MOXA_GETDATACOUNT:
1503 /* The receive side is locked by port->slock but it isn't
1504 clear that an exact snapshot is worth copying here */
1505 if (copy_to_user(argp, &mxvar_log, sizeof(mxvar_log)))
1506 ret = -EFAULT;
1507 return ret;
1508 case MOXA_GETMSTATUS: {
1509 struct mxser_mstatus ms, __user *msu = argp;
1510 for (i = 0; i < MXSER_BOARDS; i++)
1511 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++) {
1512 ip = &mxser_boards[i].ports[j];
1513 port = &ip->port;
1514 memset(&ms, 0, sizeof(ms));
1515
1516 mutex_lock(&port->mutex);
1517 if (!ip->ioaddr)
1518 goto copy;
1519
1520 tty = tty_port_tty_get(port);
1521
1522 if (!tty || !tty->termios)
1523 ms.cflag = ip->normal_termios.c_cflag;
1524 else
1525 ms.cflag = tty->termios->c_cflag;
1526 tty_kref_put(tty);
1527 spin_lock_irq(&ip->slock);
1528 status = inb(ip->ioaddr + UART_MSR);
1529 spin_unlock_irq(&ip->slock);
1530 if (status & UART_MSR_DCD)
1531 ms.dcd = 1;
1532 if (status & UART_MSR_DSR)
1533 ms.dsr = 1;
1534 if (status & UART_MSR_CTS)
1535 ms.cts = 1;
1536 copy:
1537 mutex_unlock(&port->mutex);
1538 if (copy_to_user(msu, &ms, sizeof(ms)))
1539 return -EFAULT;
1540 msu++;
1541 }
1542 return 0;
1543 }
1544 case MOXA_ASPP_MON_EXT: {
1545 struct mxser_mon_ext *me; /* it's 2k, stack unfriendly */
1546 unsigned int cflag, iflag, p;
1547 u8 opmode;
1548
1549 me = kzalloc(sizeof(*me), GFP_KERNEL);
1550 if (!me)
1551 return -ENOMEM;
1552
1553 for (i = 0, p = 0; i < MXSER_BOARDS; i++) {
1554 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++, p++) {
1555 if (p >= ARRAY_SIZE(me->rx_cnt)) {
1556 i = MXSER_BOARDS;
1557 break;
1558 }
1559 ip = &mxser_boards[i].ports[j];
1560 port = &ip->port;
1561
1562 mutex_lock(&port->mutex);
1563 if (!ip->ioaddr) {
1564 mutex_unlock(&port->mutex);
1565 continue;
1566 }
1567
1568 spin_lock_irq(&ip->slock);
1569 status = mxser_get_msr(ip->ioaddr, 0, p);
1570
1571 if (status & UART_MSR_TERI)
1572 ip->icount.rng++;
1573 if (status & UART_MSR_DDSR)
1574 ip->icount.dsr++;
1575 if (status & UART_MSR_DDCD)
1576 ip->icount.dcd++;
1577 if (status & UART_MSR_DCTS)
1578 ip->icount.cts++;
1579
1580 ip->mon_data.modem_status = status;
1581 me->rx_cnt[p] = ip->mon_data.rxcnt;
1582 me->tx_cnt[p] = ip->mon_data.txcnt;
1583 me->up_rxcnt[p] = ip->mon_data.up_rxcnt;
1584 me->up_txcnt[p] = ip->mon_data.up_txcnt;
1585 me->modem_status[p] =
1586 ip->mon_data.modem_status;
1587 spin_unlock_irq(&ip->slock);
1588
1589 tty = tty_port_tty_get(&ip->port);
1590
1591 if (!tty || !tty->termios) {
1592 cflag = ip->normal_termios.c_cflag;
1593 iflag = ip->normal_termios.c_iflag;
1594 me->baudrate[p] = tty_termios_baud_rate(&ip->normal_termios);
1595 } else {
1596 cflag = tty->termios->c_cflag;
1597 iflag = tty->termios->c_iflag;
1598 me->baudrate[p] = tty_get_baud_rate(tty);
1599 }
1600 tty_kref_put(tty);
1601
1602 me->databits[p] = cflag & CSIZE;
1603 me->stopbits[p] = cflag & CSTOPB;
1604 me->parity[p] = cflag & (PARENB | PARODD |
1605 CMSPAR);
1606
1607 if (cflag & CRTSCTS)
1608 me->flowctrl[p] |= 0x03;
1609
1610 if (iflag & (IXON | IXOFF))
1611 me->flowctrl[p] |= 0x0C;
1612
1613 if (ip->type == PORT_16550A)
1614 me->fifo[p] = 1;
1615
1616 opmode = inb(ip->opmode_ioaddr)>>((p % 4) * 2);
1617 opmode &= OP_MODE_MASK;
1618 me->iftype[p] = opmode;
1619 mutex_unlock(&port->mutex);
1620 }
1621 }
1622 if (copy_to_user(argp, me, sizeof(*me)))
1623 ret = -EFAULT;
1624 kfree(me);
1625 return ret;
1626 }
1627 default:
1628 return -ENOIOCTLCMD;
1629 }
1630 return 0;
1631}
1632
1633static int mxser_cflags_changed(struct mxser_port *info, unsigned long arg,
1634 struct async_icount *cprev)
1635{
1636 struct async_icount cnow;
1637 unsigned long flags;
1638 int ret;
1639
1640 spin_lock_irqsave(&info->slock, flags);
1641 cnow = info->icount; /* atomic copy */
1642 spin_unlock_irqrestore(&info->slock, flags);
1643
1644 ret = ((arg & TIOCM_RNG) && (cnow.rng != cprev->rng)) ||
1645 ((arg & TIOCM_DSR) && (cnow.dsr != cprev->dsr)) ||
1646 ((arg & TIOCM_CD) && (cnow.dcd != cprev->dcd)) ||
1647 ((arg & TIOCM_CTS) && (cnow.cts != cprev->cts));
1648
1649 *cprev = cnow;
1650
1651 return ret;
1652}
1653
1654static int mxser_ioctl(struct tty_struct *tty, struct file *file,
1655 unsigned int cmd, unsigned long arg)
1656{
1657 struct mxser_port *info = tty->driver_data;
1658 struct tty_port *port = &info->port;
1659 struct async_icount cnow;
1660 unsigned long flags;
1661 void __user *argp = (void __user *)arg;
1662 int retval;
1663
1664 if (tty->index == MXSER_PORTS)
1665 return mxser_ioctl_special(cmd, argp);
1666
1667 if (cmd == MOXA_SET_OP_MODE || cmd == MOXA_GET_OP_MODE) {
1668 int p;
1669 unsigned long opmode;
1670 static unsigned char ModeMask[] = { 0xfc, 0xf3, 0xcf, 0x3f };
1671 int shiftbit;
1672 unsigned char val, mask;
1673
1674 p = tty->index % 4;
1675 if (cmd == MOXA_SET_OP_MODE) {
1676 if (get_user(opmode, (int __user *) argp))
1677 return -EFAULT;
1678 if (opmode != RS232_MODE &&
1679 opmode != RS485_2WIRE_MODE &&
1680 opmode != RS422_MODE &&
1681 opmode != RS485_4WIRE_MODE)
1682 return -EFAULT;
1683 mask = ModeMask[p];
1684 shiftbit = p * 2;
1685 spin_lock_irq(&info->slock);
1686 val = inb(info->opmode_ioaddr);
1687 val &= mask;
1688 val |= (opmode << shiftbit);
1689 outb(val, info->opmode_ioaddr);
1690 spin_unlock_irq(&info->slock);
1691 } else {
1692 shiftbit = p * 2;
1693 spin_lock_irq(&info->slock);
1694 opmode = inb(info->opmode_ioaddr) >> shiftbit;
1695 spin_unlock_irq(&info->slock);
1696 opmode &= OP_MODE_MASK;
1697 if (put_user(opmode, (int __user *)argp))
1698 return -EFAULT;
1699 }
1700 return 0;
1701 }
1702
1703 if (cmd != TIOCGSERIAL && cmd != TIOCMIWAIT && cmd != TIOCGICOUNT &&
1704 test_bit(TTY_IO_ERROR, &tty->flags))
1705 return -EIO;
1706
1707 switch (cmd) {
1708 case TIOCGSERIAL:
1709 mutex_lock(&port->mutex);
1710 retval = mxser_get_serial_info(tty, argp);
1711 mutex_unlock(&port->mutex);
1712 return retval;
1713 case TIOCSSERIAL:
1714 mutex_lock(&port->mutex);
1715 retval = mxser_set_serial_info(tty, argp);
1716 mutex_unlock(&port->mutex);
1717 return retval;
1718 case TIOCSERGETLSR: /* Get line status register */
1719 return mxser_get_lsr_info(info, argp);
1720 /*
1721 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1722 * - mask passed in arg for lines of interest
1723 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1724 * Caller should use TIOCGICOUNT to see which one it was
1725 */
1726 case TIOCMIWAIT:
1727 spin_lock_irqsave(&info->slock, flags);
1728 cnow = info->icount; /* note the counters on entry */
1729 spin_unlock_irqrestore(&info->slock, flags);
1730
1731 return wait_event_interruptible(info->port.delta_msr_wait,
1732 mxser_cflags_changed(info, arg, &cnow));
1733 /*
1734 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1735 * Return: write counters to the user passed counter struct
1736 * NB: both 1->0 and 0->1 transitions are counted except for
1737 * RI where only 0->1 is counted.
1738 */
1739 case TIOCGICOUNT: {
1740 struct serial_icounter_struct icnt = { 0 };
1741 spin_lock_irqsave(&info->slock, flags);
1742 cnow = info->icount;
1743 spin_unlock_irqrestore(&info->slock, flags);
1744
1745 icnt.frame = cnow.frame;
1746 icnt.brk = cnow.brk;
1747 icnt.overrun = cnow.overrun;
1748 icnt.buf_overrun = cnow.buf_overrun;
1749 icnt.parity = cnow.parity;
1750 icnt.rx = cnow.rx;
1751 icnt.tx = cnow.tx;
1752 icnt.cts = cnow.cts;
1753 icnt.dsr = cnow.dsr;
1754 icnt.rng = cnow.rng;
1755 icnt.dcd = cnow.dcd;
1756
1757 return copy_to_user(argp, &icnt, sizeof(icnt)) ? -EFAULT : 0;
1758 }
1759 case MOXA_HighSpeedOn:
1760 return put_user(info->baud_base != 115200 ? 1 : 0, (int __user *)argp);
1761 case MOXA_SDS_RSTICOUNTER:
1762 spin_lock_irq(&info->slock);
1763 info->mon_data.rxcnt = 0;
1764 info->mon_data.txcnt = 0;
1765 spin_unlock_irq(&info->slock);
1766 return 0;
1767
1768 case MOXA_ASPP_OQUEUE:{
1769 int len, lsr;
1770
1771 len = mxser_chars_in_buffer(tty);
1772 spin_lock_irq(&info->slock);
1773 lsr = inb(info->ioaddr + UART_LSR) & UART_LSR_THRE;
1774 spin_unlock_irq(&info->slock);
1775 len += (lsr ? 0 : 1);
1776
1777 return put_user(len, (int __user *)argp);
1778 }
1779 case MOXA_ASPP_MON: {
1780 int mcr, status;
1781
1782 spin_lock_irq(&info->slock);
1783 status = mxser_get_msr(info->ioaddr, 1, tty->index);
1784 mxser_check_modem_status(tty, info, status);
1785
1786 mcr = inb(info->ioaddr + UART_MCR);
1787 spin_unlock_irq(&info->slock);
1788
1789 if (mcr & MOXA_MUST_MCR_XON_FLAG)
1790 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFHOLD;
1791 else
1792 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFHOLD;
1793
1794 if (mcr & MOXA_MUST_MCR_TX_XON)
1795 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFXENT;
1796 else
1797 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFXENT;
1798
1799 if (tty->hw_stopped)
1800 info->mon_data.hold_reason |= NPPI_NOTIFY_CTSHOLD;
1801 else
1802 info->mon_data.hold_reason &= ~NPPI_NOTIFY_CTSHOLD;
1803
1804 if (copy_to_user(argp, &info->mon_data,
1805 sizeof(struct mxser_mon)))
1806 return -EFAULT;
1807
1808 return 0;
1809 }
1810 case MOXA_ASPP_LSTATUS: {
1811 if (put_user(info->err_shadow, (unsigned char __user *)argp))
1812 return -EFAULT;
1813
1814 info->err_shadow = 0;
1815 return 0;
1816 }
1817 case MOXA_SET_BAUD_METHOD: {
1818 int method;
1819
1820 if (get_user(method, (int __user *)argp))
1821 return -EFAULT;
1822 mxser_set_baud_method[tty->index] = method;
1823 return put_user(method, (int __user *)argp);
1824 }
1825 default:
1826 return -ENOIOCTLCMD;
1827 }
1828 return 0;
1829}
1830
1831static void mxser_stoprx(struct tty_struct *tty)
1832{
1833 struct mxser_port *info = tty->driver_data;
1834
1835 info->ldisc_stop_rx = 1;
1836 if (I_IXOFF(tty)) {
1837 if (info->board->chip_flag) {
1838 info->IER &= ~MOXA_MUST_RECV_ISR;
1839 outb(info->IER, info->ioaddr + UART_IER);
1840 } else {
1841 info->x_char = STOP_CHAR(tty);
1842 outb(0, info->ioaddr + UART_IER);
1843 info->IER |= UART_IER_THRI;
1844 outb(info->IER, info->ioaddr + UART_IER);
1845 }
1846 }
1847
1848 if (tty->termios->c_cflag & CRTSCTS) {
1849 info->MCR &= ~UART_MCR_RTS;
1850 outb(info->MCR, info->ioaddr + UART_MCR);
1851 }
1852}
1853
1854/*
1855 * This routine is called by the upper-layer tty layer to signal that
1856 * incoming characters should be throttled.
1857 */
1858static void mxser_throttle(struct tty_struct *tty)
1859{
1860 mxser_stoprx(tty);
1861}
1862
1863static void mxser_unthrottle(struct tty_struct *tty)
1864{
1865 struct mxser_port *info = tty->driver_data;
1866
1867 /* startrx */
1868 info->ldisc_stop_rx = 0;
1869 if (I_IXOFF(tty)) {
1870 if (info->x_char)
1871 info->x_char = 0;
1872 else {
1873 if (info->board->chip_flag) {
1874 info->IER |= MOXA_MUST_RECV_ISR;
1875 outb(info->IER, info->ioaddr + UART_IER);
1876 } else {
1877 info->x_char = START_CHAR(tty);
1878 outb(0, info->ioaddr + UART_IER);
1879 info->IER |= UART_IER_THRI;
1880 outb(info->IER, info->ioaddr + UART_IER);
1881 }
1882 }
1883 }
1884
1885 if (tty->termios->c_cflag & CRTSCTS) {
1886 info->MCR |= UART_MCR_RTS;
1887 outb(info->MCR, info->ioaddr + UART_MCR);
1888 }
1889}
1890
1891/*
1892 * mxser_stop() and mxser_start()
1893 *
1894 * This routines are called before setting or resetting tty->stopped.
1895 * They enable or disable transmitter interrupts, as necessary.
1896 */
1897static void mxser_stop(struct tty_struct *tty)
1898{
1899 struct mxser_port *info = tty->driver_data;
1900 unsigned long flags;
1901
1902 spin_lock_irqsave(&info->slock, flags);
1903 if (info->IER & UART_IER_THRI) {
1904 info->IER &= ~UART_IER_THRI;
1905 outb(info->IER, info->ioaddr + UART_IER);
1906 }
1907 spin_unlock_irqrestore(&info->slock, flags);
1908}
1909
1910static void mxser_start(struct tty_struct *tty)
1911{
1912 struct mxser_port *info = tty->driver_data;
1913 unsigned long flags;
1914
1915 spin_lock_irqsave(&info->slock, flags);
1916 if (info->xmit_cnt && info->port.xmit_buf) {
1917 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
1918 info->IER |= UART_IER_THRI;
1919 outb(info->IER, info->ioaddr + UART_IER);
1920 }
1921 spin_unlock_irqrestore(&info->slock, flags);
1922}
1923
1924static void mxser_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
1925{
1926 struct mxser_port *info = tty->driver_data;
1927 unsigned long flags;
1928
1929 spin_lock_irqsave(&info->slock, flags);
1930 mxser_change_speed(tty, old_termios);
1931 spin_unlock_irqrestore(&info->slock, flags);
1932
1933 if ((old_termios->c_cflag & CRTSCTS) &&
1934 !(tty->termios->c_cflag & CRTSCTS)) {
1935 tty->hw_stopped = 0;
1936 mxser_start(tty);
1937 }
1938
1939 /* Handle sw stopped */
1940 if ((old_termios->c_iflag & IXON) &&
1941 !(tty->termios->c_iflag & IXON)) {
1942 tty->stopped = 0;
1943
1944 if (info->board->chip_flag) {
1945 spin_lock_irqsave(&info->slock, flags);
1946 mxser_disable_must_rx_software_flow_control(
1947 info->ioaddr);
1948 spin_unlock_irqrestore(&info->slock, flags);
1949 }
1950
1951 mxser_start(tty);
1952 }
1953}
1954
1955/*
1956 * mxser_wait_until_sent() --- wait until the transmitter is empty
1957 */
1958static void mxser_wait_until_sent(struct tty_struct *tty, int timeout)
1959{
1960 struct mxser_port *info = tty->driver_data;
1961 unsigned long orig_jiffies, char_time;
1962 unsigned long flags;
1963 int lsr;
1964
1965 if (info->type == PORT_UNKNOWN)
1966 return;
1967
1968 if (info->xmit_fifo_size == 0)
1969 return; /* Just in case.... */
1970
1971 orig_jiffies = jiffies;
1972 /*
1973 * Set the check interval to be 1/5 of the estimated time to
1974 * send a single character, and make it at least 1. The check
1975 * interval should also be less than the timeout.
1976 *
1977 * Note: we have to use pretty tight timings here to satisfy
1978 * the NIST-PCTS.
1979 */
1980 char_time = (info->timeout - HZ / 50) / info->xmit_fifo_size;
1981 char_time = char_time / 5;
1982 if (char_time == 0)
1983 char_time = 1;
1984 if (timeout && timeout < char_time)
1985 char_time = timeout;
1986 /*
1987 * If the transmitter hasn't cleared in twice the approximate
1988 * amount of time to send the entire FIFO, it probably won't
1989 * ever clear. This assumes the UART isn't doing flow
1990 * control, which is currently the case. Hence, if it ever
1991 * takes longer than info->timeout, this is probably due to a
1992 * UART bug of some kind. So, we clamp the timeout parameter at
1993 * 2*info->timeout.
1994 */
1995 if (!timeout || timeout > 2 * info->timeout)
1996 timeout = 2 * info->timeout;
1997#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1998 printk(KERN_DEBUG "In rs_wait_until_sent(%d) check=%lu...",
1999 timeout, char_time);
2000 printk("jiff=%lu...", jiffies);
2001#endif
2002 spin_lock_irqsave(&info->slock, flags);
2003 while (!((lsr = inb(info->ioaddr + UART_LSR)) & UART_LSR_TEMT)) {
2004#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2005 printk("lsr = %d (jiff=%lu)...", lsr, jiffies);
2006#endif
2007 spin_unlock_irqrestore(&info->slock, flags);
2008 schedule_timeout_interruptible(char_time);
2009 spin_lock_irqsave(&info->slock, flags);
2010 if (signal_pending(current))
2011 break;
2012 if (timeout && time_after(jiffies, orig_jiffies + timeout))
2013 break;
2014 }
2015 spin_unlock_irqrestore(&info->slock, flags);
2016 set_current_state(TASK_RUNNING);
2017
2018#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2019 printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
2020#endif
2021}
2022
2023/*
2024 * This routine is called by tty_hangup() when a hangup is signaled.
2025 */
2026static void mxser_hangup(struct tty_struct *tty)
2027{
2028 struct mxser_port *info = tty->driver_data;
2029
2030 mxser_flush_buffer(tty);
2031 tty_port_hangup(&info->port);
2032}
2033
2034/*
2035 * mxser_rs_break() --- routine which turns the break handling on or off
2036 */
2037static int mxser_rs_break(struct tty_struct *tty, int break_state)
2038{
2039 struct mxser_port *info = tty->driver_data;
2040 unsigned long flags;
2041
2042 spin_lock_irqsave(&info->slock, flags);
2043 if (break_state == -1)
2044 outb(inb(info->ioaddr + UART_LCR) | UART_LCR_SBC,
2045 info->ioaddr + UART_LCR);
2046 else
2047 outb(inb(info->ioaddr + UART_LCR) & ~UART_LCR_SBC,
2048 info->ioaddr + UART_LCR);
2049 spin_unlock_irqrestore(&info->slock, flags);
2050 return 0;
2051}
2052
2053static void mxser_receive_chars(struct tty_struct *tty,
2054 struct mxser_port *port, int *status)
2055{
2056 unsigned char ch, gdl;
2057 int ignored = 0;
2058 int cnt = 0;
2059 int recv_room;
2060 int max = 256;
2061
2062 recv_room = tty->receive_room;
2063 if (recv_room == 0 && !port->ldisc_stop_rx)
2064 mxser_stoprx(tty);
2065 if (port->board->chip_flag != MOXA_OTHER_UART) {
2066
2067 if (*status & UART_LSR_SPECIAL)
2068 goto intr_old;
2069 if (port->board->chip_flag == MOXA_MUST_MU860_HWID &&
2070 (*status & MOXA_MUST_LSR_RERR))
2071 goto intr_old;
2072 if (*status & MOXA_MUST_LSR_RERR)
2073 goto intr_old;
2074
2075 gdl = inb(port->ioaddr + MOXA_MUST_GDL_REGISTER);
2076
2077 if (port->board->chip_flag == MOXA_MUST_MU150_HWID)
2078 gdl &= MOXA_MUST_GDL_MASK;
2079 if (gdl >= recv_room) {
2080 if (!port->ldisc_stop_rx)
2081 mxser_stoprx(tty);
2082 }
2083 while (gdl--) {
2084 ch = inb(port->ioaddr + UART_RX);
2085 tty_insert_flip_char(tty, ch, 0);
2086 cnt++;
2087 }
2088 goto end_intr;
2089 }
2090intr_old:
2091
2092 do {
2093 if (max-- < 0)
2094 break;
2095
2096 ch = inb(port->ioaddr + UART_RX);
2097 if (port->board->chip_flag && (*status & UART_LSR_OE))
2098 outb(0x23, port->ioaddr + UART_FCR);
2099 *status &= port->read_status_mask;
2100 if (*status & port->ignore_status_mask) {
2101 if (++ignored > 100)
2102 break;
2103 } else {
2104 char flag = 0;
2105 if (*status & UART_LSR_SPECIAL) {
2106 if (*status & UART_LSR_BI) {
2107 flag = TTY_BREAK;
2108 port->icount.brk++;
2109
2110 if (port->port.flags & ASYNC_SAK)
2111 do_SAK(tty);
2112 } else if (*status & UART_LSR_PE) {
2113 flag = TTY_PARITY;
2114 port->icount.parity++;
2115 } else if (*status & UART_LSR_FE) {
2116 flag = TTY_FRAME;
2117 port->icount.frame++;
2118 } else if (*status & UART_LSR_OE) {
2119 flag = TTY_OVERRUN;
2120 port->icount.overrun++;
2121 } else
2122 flag = TTY_BREAK;
2123 }
2124 tty_insert_flip_char(tty, ch, flag);
2125 cnt++;
2126 if (cnt >= recv_room) {
2127 if (!port->ldisc_stop_rx)
2128 mxser_stoprx(tty);
2129 break;
2130 }
2131
2132 }
2133
2134 if (port->board->chip_flag)
2135 break;
2136
2137 *status = inb(port->ioaddr + UART_LSR);
2138 } while (*status & UART_LSR_DR);
2139
2140end_intr:
2141 mxvar_log.rxcnt[tty->index] += cnt;
2142 port->mon_data.rxcnt += cnt;
2143 port->mon_data.up_rxcnt += cnt;
2144
2145 /*
2146 * We are called from an interrupt context with &port->slock
2147 * being held. Drop it temporarily in order to prevent
2148 * recursive locking.
2149 */
2150 spin_unlock(&port->slock);
2151 tty_flip_buffer_push(tty);
2152 spin_lock(&port->slock);
2153}
2154
2155static void mxser_transmit_chars(struct tty_struct *tty, struct mxser_port *port)
2156{
2157 int count, cnt;
2158
2159 if (port->x_char) {
2160 outb(port->x_char, port->ioaddr + UART_TX);
2161 port->x_char = 0;
2162 mxvar_log.txcnt[tty->index]++;
2163 port->mon_data.txcnt++;
2164 port->mon_data.up_txcnt++;
2165 port->icount.tx++;
2166 return;
2167 }
2168
2169 if (port->port.xmit_buf == NULL)
2170 return;
2171
2172 if (port->xmit_cnt <= 0 || tty->stopped ||
2173 (tty->hw_stopped &&
2174 (port->type != PORT_16550A) &&
2175 (!port->board->chip_flag))) {
2176 port->IER &= ~UART_IER_THRI;
2177 outb(port->IER, port->ioaddr + UART_IER);
2178 return;
2179 }
2180
2181 cnt = port->xmit_cnt;
2182 count = port->xmit_fifo_size;
2183 do {
2184 outb(port->port.xmit_buf[port->xmit_tail++],
2185 port->ioaddr + UART_TX);
2186 port->xmit_tail = port->xmit_tail & (SERIAL_XMIT_SIZE - 1);
2187 if (--port->xmit_cnt <= 0)
2188 break;
2189 } while (--count > 0);
2190 mxvar_log.txcnt[tty->index] += (cnt - port->xmit_cnt);
2191
2192 port->mon_data.txcnt += (cnt - port->xmit_cnt);
2193 port->mon_data.up_txcnt += (cnt - port->xmit_cnt);
2194 port->icount.tx += (cnt - port->xmit_cnt);
2195
2196 if (port->xmit_cnt < WAKEUP_CHARS)
2197 tty_wakeup(tty);
2198
2199 if (port->xmit_cnt <= 0) {
2200 port->IER &= ~UART_IER_THRI;
2201 outb(port->IER, port->ioaddr + UART_IER);
2202 }
2203}
2204
2205/*
2206 * This is the serial driver's generic interrupt routine
2207 */
2208static irqreturn_t mxser_interrupt(int irq, void *dev_id)
2209{
2210 int status, iir, i;
2211 struct mxser_board *brd = NULL;
2212 struct mxser_port *port;
2213 int max, irqbits, bits, msr;
2214 unsigned int int_cnt, pass_counter = 0;
2215 int handled = IRQ_NONE;
2216 struct tty_struct *tty;
2217
2218 for (i = 0; i < MXSER_BOARDS; i++)
2219 if (dev_id == &mxser_boards[i]) {
2220 brd = dev_id;
2221 break;
2222 }
2223
2224 if (i == MXSER_BOARDS)
2225 goto irq_stop;
2226 if (brd == NULL)
2227 goto irq_stop;
2228 max = brd->info->nports;
2229 while (pass_counter++ < MXSER_ISR_PASS_LIMIT) {
2230 irqbits = inb(brd->vector) & brd->vector_mask;
2231 if (irqbits == brd->vector_mask)
2232 break;
2233
2234 handled = IRQ_HANDLED;
2235 for (i = 0, bits = 1; i < max; i++, irqbits |= bits, bits <<= 1) {
2236 if (irqbits == brd->vector_mask)
2237 break;
2238 if (bits & irqbits)
2239 continue;
2240 port = &brd->ports[i];
2241
2242 int_cnt = 0;
2243 spin_lock(&port->slock);
2244 do {
2245 iir = inb(port->ioaddr + UART_IIR);
2246 if (iir & UART_IIR_NO_INT)
2247 break;
2248 iir &= MOXA_MUST_IIR_MASK;
2249 tty = tty_port_tty_get(&port->port);
2250 if (!tty ||
2251 (port->port.flags & ASYNC_CLOSING) ||
2252 !(port->port.flags &
2253 ASYNC_INITIALIZED)) {
2254 status = inb(port->ioaddr + UART_LSR);
2255 outb(0x27, port->ioaddr + UART_FCR);
2256 inb(port->ioaddr + UART_MSR);
2257 tty_kref_put(tty);
2258 break;
2259 }
2260
2261 status = inb(port->ioaddr + UART_LSR);
2262
2263 if (status & UART_LSR_PE)
2264 port->err_shadow |= NPPI_NOTIFY_PARITY;
2265 if (status & UART_LSR_FE)
2266 port->err_shadow |= NPPI_NOTIFY_FRAMING;
2267 if (status & UART_LSR_OE)
2268 port->err_shadow |=
2269 NPPI_NOTIFY_HW_OVERRUN;
2270 if (status & UART_LSR_BI)
2271 port->err_shadow |= NPPI_NOTIFY_BREAK;
2272
2273 if (port->board->chip_flag) {
2274 if (iir == MOXA_MUST_IIR_GDA ||
2275 iir == MOXA_MUST_IIR_RDA ||
2276 iir == MOXA_MUST_IIR_RTO ||
2277 iir == MOXA_MUST_IIR_LSR)
2278 mxser_receive_chars(tty, port,
2279 &status);
2280
2281 } else {
2282 status &= port->read_status_mask;
2283 if (status & UART_LSR_DR)
2284 mxser_receive_chars(tty, port,
2285 &status);
2286 }
2287 msr = inb(port->ioaddr + UART_MSR);
2288 if (msr & UART_MSR_ANY_DELTA)
2289 mxser_check_modem_status(tty, port, msr);
2290
2291 if (port->board->chip_flag) {
2292 if (iir == 0x02 && (status &
2293 UART_LSR_THRE))
2294 mxser_transmit_chars(tty, port);
2295 } else {
2296 if (status & UART_LSR_THRE)
2297 mxser_transmit_chars(tty, port);
2298 }
2299 tty_kref_put(tty);
2300 } while (int_cnt++ < MXSER_ISR_PASS_LIMIT);
2301 spin_unlock(&port->slock);
2302 }
2303 }
2304
2305irq_stop:
2306 return handled;
2307}
2308
2309static const struct tty_operations mxser_ops = {
2310 .open = mxser_open,
2311 .close = mxser_close,
2312 .write = mxser_write,
2313 .put_char = mxser_put_char,
2314 .flush_chars = mxser_flush_chars,
2315 .write_room = mxser_write_room,
2316 .chars_in_buffer = mxser_chars_in_buffer,
2317 .flush_buffer = mxser_flush_buffer,
2318 .ioctl = mxser_ioctl,
2319 .throttle = mxser_throttle,
2320 .unthrottle = mxser_unthrottle,
2321 .set_termios = mxser_set_termios,
2322 .stop = mxser_stop,
2323 .start = mxser_start,
2324 .hangup = mxser_hangup,
2325 .break_ctl = mxser_rs_break,
2326 .wait_until_sent = mxser_wait_until_sent,
2327 .tiocmget = mxser_tiocmget,
2328 .tiocmset = mxser_tiocmset,
2329};
2330
2331struct tty_port_operations mxser_port_ops = {
2332 .carrier_raised = mxser_carrier_raised,
2333 .dtr_rts = mxser_dtr_rts,
2334 .activate = mxser_activate,
2335 .shutdown = mxser_shutdown_port,
2336};
2337
2338/*
2339 * The MOXA Smartio/Industio serial driver boot-time initialization code!
2340 */
2341
2342static void mxser_release_res(struct mxser_board *brd, struct pci_dev *pdev,
2343 unsigned int irq)
2344{
2345 if (irq)
2346 free_irq(brd->irq, brd);
2347 if (pdev != NULL) { /* PCI */
2348#ifdef CONFIG_PCI
2349 pci_release_region(pdev, 2);
2350 pci_release_region(pdev, 3);
2351#endif
2352 } else {
2353 release_region(brd->ports[0].ioaddr, 8 * brd->info->nports);
2354 release_region(brd->vector, 1);
2355 }
2356}
2357
2358static int __devinit mxser_initbrd(struct mxser_board *brd,
2359 struct pci_dev *pdev)
2360{
2361 struct mxser_port *info;
2362 unsigned int i;
2363 int retval;
2364
2365 printk(KERN_INFO "mxser: max. baud rate = %d bps\n",
2366 brd->ports[0].max_baud);
2367
2368 for (i = 0; i < brd->info->nports; i++) {
2369 info = &brd->ports[i];
2370 tty_port_init(&info->port);
2371 info->port.ops = &mxser_port_ops;
2372 info->board = brd;
2373 info->stop_rx = 0;
2374 info->ldisc_stop_rx = 0;
2375
2376 /* Enhance mode enabled here */
2377 if (brd->chip_flag != MOXA_OTHER_UART)
2378 mxser_enable_must_enchance_mode(info->ioaddr);
2379
2380 info->port.flags = ASYNC_SHARE_IRQ;
2381 info->type = brd->uart_type;
2382
2383 process_txrx_fifo(info);
2384
2385 info->custom_divisor = info->baud_base * 16;
2386 info->port.close_delay = 5 * HZ / 10;
2387 info->port.closing_wait = 30 * HZ;
2388 info->normal_termios = mxvar_sdriver->init_termios;
2389 memset(&info->mon_data, 0, sizeof(struct mxser_mon));
2390 info->err_shadow = 0;
2391 spin_lock_init(&info->slock);
2392
2393 /* before set INT ISR, disable all int */
2394 outb(inb(info->ioaddr + UART_IER) & 0xf0,
2395 info->ioaddr + UART_IER);
2396 }
2397
2398 retval = request_irq(brd->irq, mxser_interrupt, IRQF_SHARED, "mxser",
2399 brd);
2400 if (retval) {
2401 printk(KERN_ERR "Board %s: Request irq failed, IRQ (%d) may "
2402 "conflict with another device.\n",
2403 brd->info->name, brd->irq);
2404 /* We hold resources, we need to release them. */
2405 mxser_release_res(brd, pdev, 0);
2406 }
2407 return retval;
2408}
2409
2410static int __init mxser_get_ISA_conf(int cap, struct mxser_board *brd)
2411{
2412 int id, i, bits;
2413 unsigned short regs[16], irq;
2414 unsigned char scratch, scratch2;
2415
2416 brd->chip_flag = MOXA_OTHER_UART;
2417
2418 id = mxser_read_register(cap, regs);
2419 switch (id) {
2420 case C168_ASIC_ID:
2421 brd->info = &mxser_cards[0];
2422 break;
2423 case C104_ASIC_ID:
2424 brd->info = &mxser_cards[1];
2425 break;
2426 case CI104J_ASIC_ID:
2427 brd->info = &mxser_cards[2];
2428 break;
2429 case C102_ASIC_ID:
2430 brd->info = &mxser_cards[5];
2431 break;
2432 case CI132_ASIC_ID:
2433 brd->info = &mxser_cards[6];
2434 break;
2435 case CI134_ASIC_ID:
2436 brd->info = &mxser_cards[7];
2437 break;
2438 default:
2439 return 0;
2440 }
2441
2442 irq = 0;
2443 /* some ISA cards have 2 ports, but we want to see them as 4-port (why?)
2444 Flag-hack checks if configuration should be read as 2-port here. */
2445 if (brd->info->nports == 2 || (brd->info->flags & MXSER_HAS2)) {
2446 irq = regs[9] & 0xF000;
2447 irq = irq | (irq >> 4);
2448 if (irq != (regs[9] & 0xFF00))
2449 goto err_irqconflict;
2450 } else if (brd->info->nports == 4) {
2451 irq = regs[9] & 0xF000;
2452 irq = irq | (irq >> 4);
2453 irq = irq | (irq >> 8);
2454 if (irq != regs[9])
2455 goto err_irqconflict;
2456 } else if (brd->info->nports == 8) {
2457 irq = regs[9] & 0xF000;
2458 irq = irq | (irq >> 4);
2459 irq = irq | (irq >> 8);
2460 if ((irq != regs[9]) || (irq != regs[10]))
2461 goto err_irqconflict;
2462 }
2463
2464 if (!irq) {
2465 printk(KERN_ERR "mxser: interrupt number unset\n");
2466 return -EIO;
2467 }
2468 brd->irq = ((int)(irq & 0xF000) >> 12);
2469 for (i = 0; i < 8; i++)
2470 brd->ports[i].ioaddr = (int) regs[i + 1] & 0xFFF8;
2471 if ((regs[12] & 0x80) == 0) {
2472 printk(KERN_ERR "mxser: invalid interrupt vector\n");
2473 return -EIO;
2474 }
2475 brd->vector = (int)regs[11]; /* interrupt vector */
2476 if (id == 1)
2477 brd->vector_mask = 0x00FF;
2478 else
2479 brd->vector_mask = 0x000F;
2480 for (i = 7, bits = 0x0100; i >= 0; i--, bits <<= 1) {
2481 if (regs[12] & bits) {
2482 brd->ports[i].baud_base = 921600;
2483 brd->ports[i].max_baud = 921600;
2484 } else {
2485 brd->ports[i].baud_base = 115200;
2486 brd->ports[i].max_baud = 115200;
2487 }
2488 }
2489 scratch2 = inb(cap + UART_LCR) & (~UART_LCR_DLAB);
2490 outb(scratch2 | UART_LCR_DLAB, cap + UART_LCR);
2491 outb(0, cap + UART_EFR); /* EFR is the same as FCR */
2492 outb(scratch2, cap + UART_LCR);
2493 outb(UART_FCR_ENABLE_FIFO, cap + UART_FCR);
2494 scratch = inb(cap + UART_IIR);
2495
2496 if (scratch & 0xC0)
2497 brd->uart_type = PORT_16550A;
2498 else
2499 brd->uart_type = PORT_16450;
2500 if (!request_region(brd->ports[0].ioaddr, 8 * brd->info->nports,
2501 "mxser(IO)")) {
2502 printk(KERN_ERR "mxser: can't request ports I/O region: "
2503 "0x%.8lx-0x%.8lx\n",
2504 brd->ports[0].ioaddr, brd->ports[0].ioaddr +
2505 8 * brd->info->nports - 1);
2506 return -EIO;
2507 }
2508 if (!request_region(brd->vector, 1, "mxser(vector)")) {
2509 release_region(brd->ports[0].ioaddr, 8 * brd->info->nports);
2510 printk(KERN_ERR "mxser: can't request interrupt vector region: "
2511 "0x%.8lx-0x%.8lx\n",
2512 brd->ports[0].ioaddr, brd->ports[0].ioaddr +
2513 8 * brd->info->nports - 1);
2514 return -EIO;
2515 }
2516 return brd->info->nports;
2517
2518err_irqconflict:
2519 printk(KERN_ERR "mxser: invalid interrupt number\n");
2520 return -EIO;
2521}
2522
2523static int __devinit mxser_probe(struct pci_dev *pdev,
2524 const struct pci_device_id *ent)
2525{
2526#ifdef CONFIG_PCI
2527 struct mxser_board *brd;
2528 unsigned int i, j;
2529 unsigned long ioaddress;
2530 int retval = -EINVAL;
2531
2532 for (i = 0; i < MXSER_BOARDS; i++)
2533 if (mxser_boards[i].info == NULL)
2534 break;
2535
2536 if (i >= MXSER_BOARDS) {
2537 dev_err(&pdev->dev, "too many boards found (maximum %d), board "
2538 "not configured\n", MXSER_BOARDS);
2539 goto err;
2540 }
2541
2542 brd = &mxser_boards[i];
2543 brd->idx = i * MXSER_PORTS_PER_BOARD;
2544 dev_info(&pdev->dev, "found MOXA %s board (BusNo=%d, DevNo=%d)\n",
2545 mxser_cards[ent->driver_data].name,
2546 pdev->bus->number, PCI_SLOT(pdev->devfn));
2547
2548 retval = pci_enable_device(pdev);
2549 if (retval) {
2550 dev_err(&pdev->dev, "PCI enable failed\n");
2551 goto err;
2552 }
2553
2554 /* io address */
2555 ioaddress = pci_resource_start(pdev, 2);
2556 retval = pci_request_region(pdev, 2, "mxser(IO)");
2557 if (retval)
2558 goto err;
2559
2560 brd->info = &mxser_cards[ent->driver_data];
2561 for (i = 0; i < brd->info->nports; i++)
2562 brd->ports[i].ioaddr = ioaddress + 8 * i;
2563
2564 /* vector */
2565 ioaddress = pci_resource_start(pdev, 3);
2566 retval = pci_request_region(pdev, 3, "mxser(vector)");
2567 if (retval)
2568 goto err_relio;
2569 brd->vector = ioaddress;
2570
2571 /* irq */
2572 brd->irq = pdev->irq;
2573
2574 brd->chip_flag = CheckIsMoxaMust(brd->ports[0].ioaddr);
2575 brd->uart_type = PORT_16550A;
2576 brd->vector_mask = 0;
2577
2578 for (i = 0; i < brd->info->nports; i++) {
2579 for (j = 0; j < UART_INFO_NUM; j++) {
2580 if (Gpci_uart_info[j].type == brd->chip_flag) {
2581 brd->ports[i].max_baud =
2582 Gpci_uart_info[j].max_baud;
2583
2584 /* exception....CP-102 */
2585 if (brd->info->flags & MXSER_HIGHBAUD)
2586 brd->ports[i].max_baud = 921600;
2587 break;
2588 }
2589 }
2590 }
2591
2592 if (brd->chip_flag == MOXA_MUST_MU860_HWID) {
2593 for (i = 0; i < brd->info->nports; i++) {
2594 if (i < 4)
2595 brd->ports[i].opmode_ioaddr = ioaddress + 4;
2596 else
2597 brd->ports[i].opmode_ioaddr = ioaddress + 0x0c;
2598 }
2599 outb(0, ioaddress + 4); /* default set to RS232 mode */
2600 outb(0, ioaddress + 0x0c); /* default set to RS232 mode */
2601 }
2602
2603 for (i = 0; i < brd->info->nports; i++) {
2604 brd->vector_mask |= (1 << i);
2605 brd->ports[i].baud_base = 921600;
2606 }
2607
2608 /* mxser_initbrd will hook ISR. */
2609 retval = mxser_initbrd(brd, pdev);
2610 if (retval)
2611 goto err_null;
2612
2613 for (i = 0; i < brd->info->nports; i++)
2614 tty_register_device(mxvar_sdriver, brd->idx + i, &pdev->dev);
2615
2616 pci_set_drvdata(pdev, brd);
2617
2618 return 0;
2619err_relio:
2620 pci_release_region(pdev, 2);
2621err_null:
2622 brd->info = NULL;
2623err:
2624 return retval;
2625#else
2626 return -ENODEV;
2627#endif
2628}
2629
2630static void __devexit mxser_remove(struct pci_dev *pdev)
2631{
2632 struct mxser_board *brd = pci_get_drvdata(pdev);
2633 unsigned int i;
2634
2635 for (i = 0; i < brd->info->nports; i++)
2636 tty_unregister_device(mxvar_sdriver, brd->idx + i);
2637
2638 mxser_release_res(brd, pdev, 1);
2639 brd->info = NULL;
2640}
2641
2642static struct pci_driver mxser_driver = {
2643 .name = "mxser",
2644 .id_table = mxser_pcibrds,
2645 .probe = mxser_probe,
2646 .remove = __devexit_p(mxser_remove)
2647};
2648
2649static int __init mxser_module_init(void)
2650{
2651 struct mxser_board *brd;
2652 unsigned int b, i, m;
2653 int retval;
2654
2655 mxvar_sdriver = alloc_tty_driver(MXSER_PORTS + 1);
2656 if (!mxvar_sdriver)
2657 return -ENOMEM;
2658
2659 printk(KERN_INFO "MOXA Smartio/Industio family driver version %s\n",
2660 MXSER_VERSION);
2661
2662 /* Initialize the tty_driver structure */
2663 mxvar_sdriver->owner = THIS_MODULE;
2664 mxvar_sdriver->magic = TTY_DRIVER_MAGIC;
2665 mxvar_sdriver->name = "ttyMI";
2666 mxvar_sdriver->major = ttymajor;
2667 mxvar_sdriver->minor_start = 0;
2668 mxvar_sdriver->num = MXSER_PORTS + 1;
2669 mxvar_sdriver->type = TTY_DRIVER_TYPE_SERIAL;
2670 mxvar_sdriver->subtype = SERIAL_TYPE_NORMAL;
2671 mxvar_sdriver->init_termios = tty_std_termios;
2672 mxvar_sdriver->init_termios.c_cflag = B9600|CS8|CREAD|HUPCL|CLOCAL;
2673 mxvar_sdriver->flags = TTY_DRIVER_REAL_RAW|TTY_DRIVER_DYNAMIC_DEV;
2674 tty_set_operations(mxvar_sdriver, &mxser_ops);
2675
2676 retval = tty_register_driver(mxvar_sdriver);
2677 if (retval) {
2678 printk(KERN_ERR "Couldn't install MOXA Smartio/Industio family "
2679 "tty driver !\n");
2680 goto err_put;
2681 }
2682
2683 /* Start finding ISA boards here */
2684 for (m = 0, b = 0; b < MXSER_BOARDS; b++) {
2685 if (!ioaddr[b])
2686 continue;
2687
2688 brd = &mxser_boards[m];
2689 retval = mxser_get_ISA_conf(ioaddr[b], brd);
2690 if (retval <= 0) {
2691 brd->info = NULL;
2692 continue;
2693 }
2694
2695 printk(KERN_INFO "mxser: found MOXA %s board (CAP=0x%lx)\n",
2696 brd->info->name, ioaddr[b]);
2697
2698 /* mxser_initbrd will hook ISR. */
2699 if (mxser_initbrd(brd, NULL) < 0) {
2700 brd->info = NULL;
2701 continue;
2702 }
2703
2704 brd->idx = m * MXSER_PORTS_PER_BOARD;
2705 for (i = 0; i < brd->info->nports; i++)
2706 tty_register_device(mxvar_sdriver, brd->idx + i, NULL);
2707
2708 m++;
2709 }
2710
2711 retval = pci_register_driver(&mxser_driver);
2712 if (retval) {
2713 printk(KERN_ERR "mxser: can't register pci driver\n");
2714 if (!m) {
2715 retval = -ENODEV;
2716 goto err_unr;
2717 } /* else: we have some ISA cards under control */
2718 }
2719
2720 return 0;
2721err_unr:
2722 tty_unregister_driver(mxvar_sdriver);
2723err_put:
2724 put_tty_driver(mxvar_sdriver);
2725 return retval;
2726}
2727
2728static void __exit mxser_module_exit(void)
2729{
2730 unsigned int i, j;
2731
2732 pci_unregister_driver(&mxser_driver);
2733
2734 for (i = 0; i < MXSER_BOARDS; i++) /* ISA remains */
2735 if (mxser_boards[i].info != NULL)
2736 for (j = 0; j < mxser_boards[i].info->nports; j++)
2737 tty_unregister_device(mxvar_sdriver,
2738 mxser_boards[i].idx + j);
2739 tty_unregister_driver(mxvar_sdriver);
2740 put_tty_driver(mxvar_sdriver);
2741
2742 for (i = 0; i < MXSER_BOARDS; i++)
2743 if (mxser_boards[i].info != NULL)
2744 mxser_release_res(&mxser_boards[i], NULL, 1);
2745}
2746
2747module_init(mxser_module_init);
2748module_exit(mxser_module_exit);
diff --git a/drivers/char/mxser.h b/drivers/char/mxser.h
deleted file mode 100644
index 41878a69203d..000000000000
--- a/drivers/char/mxser.h
+++ /dev/null
@@ -1,150 +0,0 @@
1#ifndef _MXSER_H
2#define _MXSER_H
3
4/*
5 * Semi-public control interfaces
6 */
7
8/*
9 * MOXA ioctls
10 */
11
12#define MOXA 0x400
13#define MOXA_GETDATACOUNT (MOXA + 23)
14#define MOXA_DIAGNOSE (MOXA + 50)
15#define MOXA_CHKPORTENABLE (MOXA + 60)
16#define MOXA_HighSpeedOn (MOXA + 61)
17#define MOXA_GET_MAJOR (MOXA + 63)
18#define MOXA_GETMSTATUS (MOXA + 65)
19#define MOXA_SET_OP_MODE (MOXA + 66)
20#define MOXA_GET_OP_MODE (MOXA + 67)
21
22#define RS232_MODE 0
23#define RS485_2WIRE_MODE 1
24#define RS422_MODE 2
25#define RS485_4WIRE_MODE 3
26#define OP_MODE_MASK 3
27
28#define MOXA_SDS_RSTICOUNTER (MOXA + 69)
29#define MOXA_ASPP_OQUEUE (MOXA + 70)
30#define MOXA_ASPP_MON (MOXA + 73)
31#define MOXA_ASPP_LSTATUS (MOXA + 74)
32#define MOXA_ASPP_MON_EXT (MOXA + 75)
33#define MOXA_SET_BAUD_METHOD (MOXA + 76)
34
35/* --------------------------------------------------- */
36
37#define NPPI_NOTIFY_PARITY 0x01
38#define NPPI_NOTIFY_FRAMING 0x02
39#define NPPI_NOTIFY_HW_OVERRUN 0x04
40#define NPPI_NOTIFY_SW_OVERRUN 0x08
41#define NPPI_NOTIFY_BREAK 0x10
42
43#define NPPI_NOTIFY_CTSHOLD 0x01 /* Tx hold by CTS low */
44#define NPPI_NOTIFY_DSRHOLD 0x02 /* Tx hold by DSR low */
45#define NPPI_NOTIFY_XOFFHOLD 0x08 /* Tx hold by Xoff received */
46#define NPPI_NOTIFY_XOFFXENT 0x10 /* Xoff Sent */
47
48/* follow just for Moxa Must chip define. */
49/* */
50/* when LCR register (offset 0x03) write following value, */
51/* the Must chip will enter enchance mode. And write value */
52/* on EFR (offset 0x02) bit 6,7 to change bank. */
53#define MOXA_MUST_ENTER_ENCHANCE 0xBF
54
55/* when enhance mode enable, access on general bank register */
56#define MOXA_MUST_GDL_REGISTER 0x07
57#define MOXA_MUST_GDL_MASK 0x7F
58#define MOXA_MUST_GDL_HAS_BAD_DATA 0x80
59
60#define MOXA_MUST_LSR_RERR 0x80 /* error in receive FIFO */
61/* enchance register bank select and enchance mode setting register */
62/* when LCR register equal to 0xBF */
63#define MOXA_MUST_EFR_REGISTER 0x02
64/* enchance mode enable */
65#define MOXA_MUST_EFR_EFRB_ENABLE 0x10
66/* enchance reister bank set 0, 1, 2 */
67#define MOXA_MUST_EFR_BANK0 0x00
68#define MOXA_MUST_EFR_BANK1 0x40
69#define MOXA_MUST_EFR_BANK2 0x80
70#define MOXA_MUST_EFR_BANK3 0xC0
71#define MOXA_MUST_EFR_BANK_MASK 0xC0
72
73/* set XON1 value register, when LCR=0xBF and change to bank0 */
74#define MOXA_MUST_XON1_REGISTER 0x04
75
76/* set XON2 value register, when LCR=0xBF and change to bank0 */
77#define MOXA_MUST_XON2_REGISTER 0x05
78
79/* set XOFF1 value register, when LCR=0xBF and change to bank0 */
80#define MOXA_MUST_XOFF1_REGISTER 0x06
81
82/* set XOFF2 value register, when LCR=0xBF and change to bank0 */
83#define MOXA_MUST_XOFF2_REGISTER 0x07
84
85#define MOXA_MUST_RBRTL_REGISTER 0x04
86#define MOXA_MUST_RBRTH_REGISTER 0x05
87#define MOXA_MUST_RBRTI_REGISTER 0x06
88#define MOXA_MUST_THRTL_REGISTER 0x07
89#define MOXA_MUST_ENUM_REGISTER 0x04
90#define MOXA_MUST_HWID_REGISTER 0x05
91#define MOXA_MUST_ECR_REGISTER 0x06
92#define MOXA_MUST_CSR_REGISTER 0x07
93
94/* good data mode enable */
95#define MOXA_MUST_FCR_GDA_MODE_ENABLE 0x20
96/* only good data put into RxFIFO */
97#define MOXA_MUST_FCR_GDA_ONLY_ENABLE 0x10
98
99/* enable CTS interrupt */
100#define MOXA_MUST_IER_ECTSI 0x80
101/* enable RTS interrupt */
102#define MOXA_MUST_IER_ERTSI 0x40
103/* enable Xon/Xoff interrupt */
104#define MOXA_MUST_IER_XINT 0x20
105/* enable GDA interrupt */
106#define MOXA_MUST_IER_EGDAI 0x10
107
108#define MOXA_MUST_RECV_ISR (UART_IER_RDI | MOXA_MUST_IER_EGDAI)
109
110/* GDA interrupt pending */
111#define MOXA_MUST_IIR_GDA 0x1C
112#define MOXA_MUST_IIR_RDA 0x04
113#define MOXA_MUST_IIR_RTO 0x0C
114#define MOXA_MUST_IIR_LSR 0x06
115
116/* recieved Xon/Xoff or specical interrupt pending */
117#define MOXA_MUST_IIR_XSC 0x10
118
119/* RTS/CTS change state interrupt pending */
120#define MOXA_MUST_IIR_RTSCTS 0x20
121#define MOXA_MUST_IIR_MASK 0x3E
122
123#define MOXA_MUST_MCR_XON_FLAG 0x40
124#define MOXA_MUST_MCR_XON_ANY 0x80
125#define MOXA_MUST_MCR_TX_XON 0x08
126
127/* software flow control on chip mask value */
128#define MOXA_MUST_EFR_SF_MASK 0x0F
129/* send Xon1/Xoff1 */
130#define MOXA_MUST_EFR_SF_TX1 0x08
131/* send Xon2/Xoff2 */
132#define MOXA_MUST_EFR_SF_TX2 0x04
133/* send Xon1,Xon2/Xoff1,Xoff2 */
134#define MOXA_MUST_EFR_SF_TX12 0x0C
135/* don't send Xon/Xoff */
136#define MOXA_MUST_EFR_SF_TX_NO 0x00
137/* Tx software flow control mask */
138#define MOXA_MUST_EFR_SF_TX_MASK 0x0C
139/* don't receive Xon/Xoff */
140#define MOXA_MUST_EFR_SF_RX_NO 0x00
141/* receive Xon1/Xoff1 */
142#define MOXA_MUST_EFR_SF_RX1 0x02
143/* receive Xon2/Xoff2 */
144#define MOXA_MUST_EFR_SF_RX2 0x01
145/* receive Xon1,Xon2/Xoff1,Xoff2 */
146#define MOXA_MUST_EFR_SF_RX12 0x03
147/* Rx software flow control mask */
148#define MOXA_MUST_EFR_SF_RX_MASK 0x03
149
150#endif
diff --git a/drivers/char/n_gsm.c b/drivers/char/n_gsm.c
deleted file mode 100644
index 04ef3ef0a422..000000000000
--- a/drivers/char/n_gsm.c
+++ /dev/null
@@ -1,2763 +0,0 @@
1/*
2 * n_gsm.c GSM 0710 tty multiplexor
3 * Copyright (c) 2009/10 Intel Corporation
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 *
18 * * THIS IS A DEVELOPMENT SNAPSHOT IT IS NOT A FINAL RELEASE *
19 *
20 * TO DO:
21 * Mostly done: ioctls for setting modes/timing
22 * Partly done: hooks so you can pull off frames to non tty devs
23 * Restart DLCI 0 when it closes ?
24 * Test basic encoding
25 * Improve the tx engine
26 * Resolve tx side locking by adding a queue_head and routing
27 * all control traffic via it
28 * General tidy/document
29 * Review the locking/move to refcounts more (mux now moved to an
30 * alloc/free model ready)
31 * Use newest tty open/close port helpers and install hooks
32 * What to do about power functions ?
33 * Termios setting and negotiation
34 * Do we need a 'which mux are you' ioctl to correlate mux and tty sets
35 *
36 */
37
38#include <linux/types.h>
39#include <linux/major.h>
40#include <linux/errno.h>
41#include <linux/signal.h>
42#include <linux/fcntl.h>
43#include <linux/sched.h>
44#include <linux/interrupt.h>
45#include <linux/tty.h>
46#include <linux/ctype.h>
47#include <linux/mm.h>
48#include <linux/string.h>
49#include <linux/slab.h>
50#include <linux/poll.h>
51#include <linux/bitops.h>
52#include <linux/file.h>
53#include <linux/uaccess.h>
54#include <linux/module.h>
55#include <linux/timer.h>
56#include <linux/tty_flip.h>
57#include <linux/tty_driver.h>
58#include <linux/serial.h>
59#include <linux/kfifo.h>
60#include <linux/skbuff.h>
61#include <linux/gsmmux.h>
62
63static int debug;
64module_param(debug, int, 0600);
65
66#define T1 (HZ/10)
67#define T2 (HZ/3)
68#define N2 3
69
70/* Use long timers for testing at low speed with debug on */
71#ifdef DEBUG_TIMING
72#define T1 HZ
73#define T2 (2 * HZ)
74#endif
75
76/* Semi-arbitary buffer size limits. 0710 is normally run with 32-64 byte
77 limits so this is plenty */
78#define MAX_MRU 512
79#define MAX_MTU 512
80
81/*
82 * Each block of data we have queued to go out is in the form of
83 * a gsm_msg which holds everything we need in a link layer independant
84 * format
85 */
86
87struct gsm_msg {
88 struct gsm_msg *next;
89 u8 addr; /* DLCI address + flags */
90 u8 ctrl; /* Control byte + flags */
91 unsigned int len; /* Length of data block (can be zero) */
92 unsigned char *data; /* Points into buffer but not at the start */
93 unsigned char buffer[0];
94};
95
96/*
97 * Each active data link has a gsm_dlci structure associated which ties
98 * the link layer to an optional tty (if the tty side is open). To avoid
99 * complexity right now these are only ever freed up when the mux is
100 * shut down.
101 *
102 * At the moment we don't free DLCI objects until the mux is torn down
103 * this avoid object life time issues but might be worth review later.
104 */
105
106struct gsm_dlci {
107 struct gsm_mux *gsm;
108 int addr;
109 int state;
110#define DLCI_CLOSED 0
111#define DLCI_OPENING 1 /* Sending SABM not seen UA */
112#define DLCI_OPEN 2 /* SABM/UA complete */
113#define DLCI_CLOSING 3 /* Sending DISC not seen UA/DM */
114
115 /* Link layer */
116 spinlock_t lock; /* Protects the internal state */
117 struct timer_list t1; /* Retransmit timer for SABM and UA */
118 int retries;
119 /* Uplink tty if active */
120 struct tty_port port; /* The tty bound to this DLCI if there is one */
121 struct kfifo *fifo; /* Queue fifo for the DLCI */
122 struct kfifo _fifo; /* For new fifo API porting only */
123 int adaption; /* Adaption layer in use */
124 u32 modem_rx; /* Our incoming virtual modem lines */
125 u32 modem_tx; /* Our outgoing modem lines */
126 int dead; /* Refuse re-open */
127 /* Flow control */
128 int throttled; /* Private copy of throttle state */
129 int constipated; /* Throttle status for outgoing */
130 /* Packetised I/O */
131 struct sk_buff *skb; /* Frame being sent */
132 struct sk_buff_head skb_list; /* Queued frames */
133 /* Data handling callback */
134 void (*data)(struct gsm_dlci *dlci, u8 *data, int len);
135};
136
137/* DLCI 0, 62/63 are special or reseved see gsmtty_open */
138
139#define NUM_DLCI 64
140
141/*
142 * DLCI 0 is used to pass control blocks out of band of the data
143 * flow (and with a higher link priority). One command can be outstanding
144 * at a time and we use this structure to manage them. They are created
145 * and destroyed by the user context, and updated by the receive paths
146 * and timers
147 */
148
149struct gsm_control {
150 u8 cmd; /* Command we are issuing */
151 u8 *data; /* Data for the command in case we retransmit */
152 int len; /* Length of block for retransmission */
153 int done; /* Done flag */
154 int error; /* Error if any */
155};
156
157/*
158 * Each GSM mux we have is represented by this structure. If we are
159 * operating as an ldisc then we use this structure as our ldisc
160 * state. We need to sort out lifetimes and locking with respect
161 * to the gsm mux array. For now we don't free DLCI objects that
162 * have been instantiated until the mux itself is terminated.
163 *
164 * To consider further: tty open versus mux shutdown.
165 */
166
167struct gsm_mux {
168 struct tty_struct *tty; /* The tty our ldisc is bound to */
169 spinlock_t lock;
170
171 /* Events on the GSM channel */
172 wait_queue_head_t event;
173
174 /* Bits for GSM mode decoding */
175
176 /* Framing Layer */
177 unsigned char *buf;
178 int state;
179#define GSM_SEARCH 0
180#define GSM_START 1
181#define GSM_ADDRESS 2
182#define GSM_CONTROL 3
183#define GSM_LEN 4
184#define GSM_DATA 5
185#define GSM_FCS 6
186#define GSM_OVERRUN 7
187 unsigned int len;
188 unsigned int address;
189 unsigned int count;
190 int escape;
191 int encoding;
192 u8 control;
193 u8 fcs;
194 u8 *txframe; /* TX framing buffer */
195
196 /* Methods for the receiver side */
197 void (*receive)(struct gsm_mux *gsm, u8 ch);
198 void (*error)(struct gsm_mux *gsm, u8 ch, u8 flag);
199 /* And transmit side */
200 int (*output)(struct gsm_mux *mux, u8 *data, int len);
201
202 /* Link Layer */
203 unsigned int mru;
204 unsigned int mtu;
205 int initiator; /* Did we initiate connection */
206 int dead; /* Has the mux been shut down */
207 struct gsm_dlci *dlci[NUM_DLCI];
208 int constipated; /* Asked by remote to shut up */
209
210 spinlock_t tx_lock;
211 unsigned int tx_bytes; /* TX data outstanding */
212#define TX_THRESH_HI 8192
213#define TX_THRESH_LO 2048
214 struct gsm_msg *tx_head; /* Pending data packets */
215 struct gsm_msg *tx_tail;
216
217 /* Control messages */
218 struct timer_list t2_timer; /* Retransmit timer for commands */
219 int cretries; /* Command retry counter */
220 struct gsm_control *pending_cmd;/* Our current pending command */
221 spinlock_t control_lock; /* Protects the pending command */
222
223 /* Configuration */
224 int adaption; /* 1 or 2 supported */
225 u8 ftype; /* UI or UIH */
226 int t1, t2; /* Timers in 1/100th of a sec */
227 int n2; /* Retry count */
228
229 /* Statistics (not currently exposed) */
230 unsigned long bad_fcs;
231 unsigned long malformed;
232 unsigned long io_error;
233 unsigned long bad_size;
234 unsigned long unsupported;
235};
236
237
238/*
239 * Mux objects - needed so that we can translate a tty index into the
240 * relevant mux and DLCI.
241 */
242
243#define MAX_MUX 4 /* 256 minors */
244static struct gsm_mux *gsm_mux[MAX_MUX]; /* GSM muxes */
245static spinlock_t gsm_mux_lock;
246
247/*
248 * This section of the driver logic implements the GSM encodings
249 * both the basic and the 'advanced'. Reliable transport is not
250 * supported.
251 */
252
253#define CR 0x02
254#define EA 0x01
255#define PF 0x10
256
257/* I is special: the rest are ..*/
258#define RR 0x01
259#define UI 0x03
260#define RNR 0x05
261#define REJ 0x09
262#define DM 0x0F
263#define SABM 0x2F
264#define DISC 0x43
265#define UA 0x63
266#define UIH 0xEF
267
268/* Channel commands */
269#define CMD_NSC 0x09
270#define CMD_TEST 0x11
271#define CMD_PSC 0x21
272#define CMD_RLS 0x29
273#define CMD_FCOFF 0x31
274#define CMD_PN 0x41
275#define CMD_RPN 0x49
276#define CMD_FCON 0x51
277#define CMD_CLD 0x61
278#define CMD_SNC 0x69
279#define CMD_MSC 0x71
280
281/* Virtual modem bits */
282#define MDM_FC 0x01
283#define MDM_RTC 0x02
284#define MDM_RTR 0x04
285#define MDM_IC 0x20
286#define MDM_DV 0x40
287
288#define GSM0_SOF 0xF9
289#define GSM1_SOF 0x7E
290#define GSM1_ESCAPE 0x7D
291#define GSM1_ESCAPE_BITS 0x20
292#define XON 0x11
293#define XOFF 0x13
294
295static const struct tty_port_operations gsm_port_ops;
296
297/*
298 * CRC table for GSM 0710
299 */
300
301static const u8 gsm_fcs8[256] = {
302 0x00, 0x91, 0xE3, 0x72, 0x07, 0x96, 0xE4, 0x75,
303 0x0E, 0x9F, 0xED, 0x7C, 0x09, 0x98, 0xEA, 0x7B,
304 0x1C, 0x8D, 0xFF, 0x6E, 0x1B, 0x8A, 0xF8, 0x69,
305 0x12, 0x83, 0xF1, 0x60, 0x15, 0x84, 0xF6, 0x67,
306 0x38, 0xA9, 0xDB, 0x4A, 0x3F, 0xAE, 0xDC, 0x4D,
307 0x36, 0xA7, 0xD5, 0x44, 0x31, 0xA0, 0xD2, 0x43,
308 0x24, 0xB5, 0xC7, 0x56, 0x23, 0xB2, 0xC0, 0x51,
309 0x2A, 0xBB, 0xC9, 0x58, 0x2D, 0xBC, 0xCE, 0x5F,
310 0x70, 0xE1, 0x93, 0x02, 0x77, 0xE6, 0x94, 0x05,
311 0x7E, 0xEF, 0x9D, 0x0C, 0x79, 0xE8, 0x9A, 0x0B,
312 0x6C, 0xFD, 0x8F, 0x1E, 0x6B, 0xFA, 0x88, 0x19,
313 0x62, 0xF3, 0x81, 0x10, 0x65, 0xF4, 0x86, 0x17,
314 0x48, 0xD9, 0xAB, 0x3A, 0x4F, 0xDE, 0xAC, 0x3D,
315 0x46, 0xD7, 0xA5, 0x34, 0x41, 0xD0, 0xA2, 0x33,
316 0x54, 0xC5, 0xB7, 0x26, 0x53, 0xC2, 0xB0, 0x21,
317 0x5A, 0xCB, 0xB9, 0x28, 0x5D, 0xCC, 0xBE, 0x2F,
318 0xE0, 0x71, 0x03, 0x92, 0xE7, 0x76, 0x04, 0x95,
319 0xEE, 0x7F, 0x0D, 0x9C, 0xE9, 0x78, 0x0A, 0x9B,
320 0xFC, 0x6D, 0x1F, 0x8E, 0xFB, 0x6A, 0x18, 0x89,
321 0xF2, 0x63, 0x11, 0x80, 0xF5, 0x64, 0x16, 0x87,
322 0xD8, 0x49, 0x3B, 0xAA, 0xDF, 0x4E, 0x3C, 0xAD,
323 0xD6, 0x47, 0x35, 0xA4, 0xD1, 0x40, 0x32, 0xA3,
324 0xC4, 0x55, 0x27, 0xB6, 0xC3, 0x52, 0x20, 0xB1,
325 0xCA, 0x5B, 0x29, 0xB8, 0xCD, 0x5C, 0x2E, 0xBF,
326 0x90, 0x01, 0x73, 0xE2, 0x97, 0x06, 0x74, 0xE5,
327 0x9E, 0x0F, 0x7D, 0xEC, 0x99, 0x08, 0x7A, 0xEB,
328 0x8C, 0x1D, 0x6F, 0xFE, 0x8B, 0x1A, 0x68, 0xF9,
329 0x82, 0x13, 0x61, 0xF0, 0x85, 0x14, 0x66, 0xF7,
330 0xA8, 0x39, 0x4B, 0xDA, 0xAF, 0x3E, 0x4C, 0xDD,
331 0xA6, 0x37, 0x45, 0xD4, 0xA1, 0x30, 0x42, 0xD3,
332 0xB4, 0x25, 0x57, 0xC6, 0xB3, 0x22, 0x50, 0xC1,
333 0xBA, 0x2B, 0x59, 0xC8, 0xBD, 0x2C, 0x5E, 0xCF
334};
335
336#define INIT_FCS 0xFF
337#define GOOD_FCS 0xCF
338
339/**
340 * gsm_fcs_add - update FCS
341 * @fcs: Current FCS
342 * @c: Next data
343 *
344 * Update the FCS to include c. Uses the algorithm in the specification
345 * notes.
346 */
347
348static inline u8 gsm_fcs_add(u8 fcs, u8 c)
349{
350 return gsm_fcs8[fcs ^ c];
351}
352
353/**
354 * gsm_fcs_add_block - update FCS for a block
355 * @fcs: Current FCS
356 * @c: buffer of data
357 * @len: length of buffer
358 *
359 * Update the FCS to include c. Uses the algorithm in the specification
360 * notes.
361 */
362
363static inline u8 gsm_fcs_add_block(u8 fcs, u8 *c, int len)
364{
365 while (len--)
366 fcs = gsm_fcs8[fcs ^ *c++];
367 return fcs;
368}
369
370/**
371 * gsm_read_ea - read a byte into an EA
372 * @val: variable holding value
373 * c: byte going into the EA
374 *
375 * Processes one byte of an EA. Updates the passed variable
376 * and returns 1 if the EA is now completely read
377 */
378
379static int gsm_read_ea(unsigned int *val, u8 c)
380{
381 /* Add the next 7 bits into the value */
382 *val <<= 7;
383 *val |= c >> 1;
384 /* Was this the last byte of the EA 1 = yes*/
385 return c & EA;
386}
387
388/**
389 * gsm_encode_modem - encode modem data bits
390 * @dlci: DLCI to encode from
391 *
392 * Returns the correct GSM encoded modem status bits (6 bit field) for
393 * the current status of the DLCI and attached tty object
394 */
395
396static u8 gsm_encode_modem(const struct gsm_dlci *dlci)
397{
398 u8 modembits = 0;
399 /* FC is true flow control not modem bits */
400 if (dlci->throttled)
401 modembits |= MDM_FC;
402 if (dlci->modem_tx & TIOCM_DTR)
403 modembits |= MDM_RTC;
404 if (dlci->modem_tx & TIOCM_RTS)
405 modembits |= MDM_RTR;
406 if (dlci->modem_tx & TIOCM_RI)
407 modembits |= MDM_IC;
408 if (dlci->modem_tx & TIOCM_CD)
409 modembits |= MDM_DV;
410 return modembits;
411}
412
413/**
414 * gsm_print_packet - display a frame for debug
415 * @hdr: header to print before decode
416 * @addr: address EA from the frame
417 * @cr: C/R bit from the frame
418 * @control: control including PF bit
419 * @data: following data bytes
420 * @dlen: length of data
421 *
422 * Displays a packet in human readable format for debugging purposes. The
423 * style is based on amateur radio LAP-B dump display.
424 */
425
426static void gsm_print_packet(const char *hdr, int addr, int cr,
427 u8 control, const u8 *data, int dlen)
428{
429 if (!(debug & 1))
430 return;
431
432 printk(KERN_INFO "%s %d) %c: ", hdr, addr, "RC"[cr]);
433
434 switch (control & ~PF) {
435 case SABM:
436 printk(KERN_CONT "SABM");
437 break;
438 case UA:
439 printk(KERN_CONT "UA");
440 break;
441 case DISC:
442 printk(KERN_CONT "DISC");
443 break;
444 case DM:
445 printk(KERN_CONT "DM");
446 break;
447 case UI:
448 printk(KERN_CONT "UI");
449 break;
450 case UIH:
451 printk(KERN_CONT "UIH");
452 break;
453 default:
454 if (!(control & 0x01)) {
455 printk(KERN_CONT "I N(S)%d N(R)%d",
456 (control & 0x0E) >> 1, (control & 0xE)>> 5);
457 } else switch (control & 0x0F) {
458 case RR:
459 printk("RR(%d)", (control & 0xE0) >> 5);
460 break;
461 case RNR:
462 printk("RNR(%d)", (control & 0xE0) >> 5);
463 break;
464 case REJ:
465 printk("REJ(%d)", (control & 0xE0) >> 5);
466 break;
467 default:
468 printk(KERN_CONT "[%02X]", control);
469 }
470 }
471
472 if (control & PF)
473 printk(KERN_CONT "(P)");
474 else
475 printk(KERN_CONT "(F)");
476
477 if (dlen) {
478 int ct = 0;
479 while (dlen--) {
480 if (ct % 8 == 0)
481 printk(KERN_CONT "\n ");
482 printk(KERN_CONT "%02X ", *data++);
483 ct++;
484 }
485 }
486 printk(KERN_CONT "\n");
487}
488
489
490/*
491 * Link level transmission side
492 */
493
494/**
495 * gsm_stuff_packet - bytestuff a packet
496 * @ibuf: input
497 * @obuf: output
498 * @len: length of input
499 *
500 * Expand a buffer by bytestuffing it. The worst case size change
501 * is doubling and the caller is responsible for handing out
502 * suitable sized buffers.
503 */
504
505static int gsm_stuff_frame(const u8 *input, u8 *output, int len)
506{
507 int olen = 0;
508 while (len--) {
509 if (*input == GSM1_SOF || *input == GSM1_ESCAPE
510 || *input == XON || *input == XOFF) {
511 *output++ = GSM1_ESCAPE;
512 *output++ = *input++ ^ GSM1_ESCAPE_BITS;
513 olen++;
514 } else
515 *output++ = *input++;
516 olen++;
517 }
518 return olen;
519}
520
521static void hex_packet(const unsigned char *p, int len)
522{
523 int i;
524 for (i = 0; i < len; i++) {
525 if (i && (i % 16) == 0)
526 printk("\n");
527 printk("%02X ", *p++);
528 }
529 printk("\n");
530}
531
532/**
533 * gsm_send - send a control frame
534 * @gsm: our GSM mux
535 * @addr: address for control frame
536 * @cr: command/response bit
537 * @control: control byte including PF bit
538 *
539 * Format up and transmit a control frame. These do not go via the
540 * queueing logic as they should be transmitted ahead of data when
541 * they are needed.
542 *
543 * FIXME: Lock versus data TX path
544 */
545
546static void gsm_send(struct gsm_mux *gsm, int addr, int cr, int control)
547{
548 int len;
549 u8 cbuf[10];
550 u8 ibuf[3];
551
552 switch (gsm->encoding) {
553 case 0:
554 cbuf[0] = GSM0_SOF;
555 cbuf[1] = (addr << 2) | (cr << 1) | EA;
556 cbuf[2] = control;
557 cbuf[3] = EA; /* Length of data = 0 */
558 cbuf[4] = 0xFF - gsm_fcs_add_block(INIT_FCS, cbuf + 1, 3);
559 cbuf[5] = GSM0_SOF;
560 len = 6;
561 break;
562 case 1:
563 case 2:
564 /* Control frame + packing (but not frame stuffing) in mode 1 */
565 ibuf[0] = (addr << 2) | (cr << 1) | EA;
566 ibuf[1] = control;
567 ibuf[2] = 0xFF - gsm_fcs_add_block(INIT_FCS, ibuf, 2);
568 /* Stuffing may double the size worst case */
569 len = gsm_stuff_frame(ibuf, cbuf + 1, 3);
570 /* Now add the SOF markers */
571 cbuf[0] = GSM1_SOF;
572 cbuf[len + 1] = GSM1_SOF;
573 /* FIXME: we can omit the lead one in many cases */
574 len += 2;
575 break;
576 default:
577 WARN_ON(1);
578 return;
579 }
580 gsm->output(gsm, cbuf, len);
581 gsm_print_packet("-->", addr, cr, control, NULL, 0);
582}
583
584/**
585 * gsm_response - send a control response
586 * @gsm: our GSM mux
587 * @addr: address for control frame
588 * @control: control byte including PF bit
589 *
590 * Format up and transmit a link level response frame.
591 */
592
593static inline void gsm_response(struct gsm_mux *gsm, int addr, int control)
594{
595 gsm_send(gsm, addr, 0, control);
596}
597
598/**
599 * gsm_command - send a control command
600 * @gsm: our GSM mux
601 * @addr: address for control frame
602 * @control: control byte including PF bit
603 *
604 * Format up and transmit a link level command frame.
605 */
606
607static inline void gsm_command(struct gsm_mux *gsm, int addr, int control)
608{
609 gsm_send(gsm, addr, 1, control);
610}
611
612/* Data transmission */
613
614#define HDR_LEN 6 /* ADDR CTRL [LEN.2] DATA FCS */
615
616/**
617 * gsm_data_alloc - allocate data frame
618 * @gsm: GSM mux
619 * @addr: DLCI address
620 * @len: length excluding header and FCS
621 * @ctrl: control byte
622 *
623 * Allocate a new data buffer for sending frames with data. Space is left
624 * at the front for header bytes but that is treated as an implementation
625 * detail and not for the high level code to use
626 */
627
628static struct gsm_msg *gsm_data_alloc(struct gsm_mux *gsm, u8 addr, int len,
629 u8 ctrl)
630{
631 struct gsm_msg *m = kmalloc(sizeof(struct gsm_msg) + len + HDR_LEN,
632 GFP_ATOMIC);
633 if (m == NULL)
634 return NULL;
635 m->data = m->buffer + HDR_LEN - 1; /* Allow for FCS */
636 m->len = len;
637 m->addr = addr;
638 m->ctrl = ctrl;
639 m->next = NULL;
640 return m;
641}
642
643/**
644 * gsm_data_kick - poke the queue
645 * @gsm: GSM Mux
646 *
647 * The tty device has called us to indicate that room has appeared in
648 * the transmit queue. Ram more data into the pipe if we have any
649 *
650 * FIXME: lock against link layer control transmissions
651 */
652
653static void gsm_data_kick(struct gsm_mux *gsm)
654{
655 struct gsm_msg *msg = gsm->tx_head;
656 int len;
657 int skip_sof = 0;
658
659 /* FIXME: We need to apply this solely to data messages */
660 if (gsm->constipated)
661 return;
662
663 while (gsm->tx_head != NULL) {
664 msg = gsm->tx_head;
665 if (gsm->encoding != 0) {
666 gsm->txframe[0] = GSM1_SOF;
667 len = gsm_stuff_frame(msg->data,
668 gsm->txframe + 1, msg->len);
669 gsm->txframe[len + 1] = GSM1_SOF;
670 len += 2;
671 } else {
672 gsm->txframe[0] = GSM0_SOF;
673 memcpy(gsm->txframe + 1 , msg->data, msg->len);
674 gsm->txframe[msg->len + 1] = GSM0_SOF;
675 len = msg->len + 2;
676 }
677
678 if (debug & 4) {
679 printk("gsm_data_kick: \n");
680 hex_packet(gsm->txframe, len);
681 }
682
683 if (gsm->output(gsm, gsm->txframe + skip_sof,
684 len - skip_sof) < 0)
685 break;
686 /* FIXME: Can eliminate one SOF in many more cases */
687 gsm->tx_head = msg->next;
688 if (gsm->tx_head == NULL)
689 gsm->tx_tail = NULL;
690 gsm->tx_bytes -= msg->len;
691 kfree(msg);
692 /* For a burst of frames skip the extra SOF within the
693 burst */
694 skip_sof = 1;
695 }
696}
697
698/**
699 * __gsm_data_queue - queue a UI or UIH frame
700 * @dlci: DLCI sending the data
701 * @msg: message queued
702 *
703 * Add data to the transmit queue and try and get stuff moving
704 * out of the mux tty if not already doing so. The Caller must hold
705 * the gsm tx lock.
706 */
707
708static void __gsm_data_queue(struct gsm_dlci *dlci, struct gsm_msg *msg)
709{
710 struct gsm_mux *gsm = dlci->gsm;
711 u8 *dp = msg->data;
712 u8 *fcs = dp + msg->len;
713
714 /* Fill in the header */
715 if (gsm->encoding == 0) {
716 if (msg->len < 128)
717 *--dp = (msg->len << 1) | EA;
718 else {
719 *--dp = (msg->len >> 6) | EA;
720 *--dp = (msg->len & 127) << 1;
721 }
722 }
723
724 *--dp = msg->ctrl;
725 if (gsm->initiator)
726 *--dp = (msg->addr << 2) | 2 | EA;
727 else
728 *--dp = (msg->addr << 2) | EA;
729 *fcs = gsm_fcs_add_block(INIT_FCS, dp , msg->data - dp);
730 /* Ugly protocol layering violation */
731 if (msg->ctrl == UI || msg->ctrl == (UI|PF))
732 *fcs = gsm_fcs_add_block(*fcs, msg->data, msg->len);
733 *fcs = 0xFF - *fcs;
734
735 gsm_print_packet("Q> ", msg->addr, gsm->initiator, msg->ctrl,
736 msg->data, msg->len);
737
738 /* Move the header back and adjust the length, also allow for the FCS
739 now tacked on the end */
740 msg->len += (msg->data - dp) + 1;
741 msg->data = dp;
742
743 /* Add to the actual output queue */
744 if (gsm->tx_tail)
745 gsm->tx_tail->next = msg;
746 else
747 gsm->tx_head = msg;
748 gsm->tx_tail = msg;
749 gsm->tx_bytes += msg->len;
750 gsm_data_kick(gsm);
751}
752
753/**
754 * gsm_data_queue - queue a UI or UIH frame
755 * @dlci: DLCI sending the data
756 * @msg: message queued
757 *
758 * Add data to the transmit queue and try and get stuff moving
759 * out of the mux tty if not already doing so. Take the
760 * the gsm tx lock and dlci lock.
761 */
762
763static void gsm_data_queue(struct gsm_dlci *dlci, struct gsm_msg *msg)
764{
765 unsigned long flags;
766 spin_lock_irqsave(&dlci->gsm->tx_lock, flags);
767 __gsm_data_queue(dlci, msg);
768 spin_unlock_irqrestore(&dlci->gsm->tx_lock, flags);
769}
770
771/**
772 * gsm_dlci_data_output - try and push data out of a DLCI
773 * @gsm: mux
774 * @dlci: the DLCI to pull data from
775 *
776 * Pull data from a DLCI and send it into the transmit queue if there
777 * is data. Keep to the MRU of the mux. This path handles the usual tty
778 * interface which is a byte stream with optional modem data.
779 *
780 * Caller must hold the tx_lock of the mux.
781 */
782
783static int gsm_dlci_data_output(struct gsm_mux *gsm, struct gsm_dlci *dlci)
784{
785 struct gsm_msg *msg;
786 u8 *dp;
787 int len, size;
788 int h = dlci->adaption - 1;
789
790 len = kfifo_len(dlci->fifo);
791 if (len == 0)
792 return 0;
793
794 /* MTU/MRU count only the data bits */
795 if (len > gsm->mtu)
796 len = gsm->mtu;
797
798 size = len + h;
799
800 msg = gsm_data_alloc(gsm, dlci->addr, size, gsm->ftype);
801 /* FIXME: need a timer or something to kick this so it can't
802 get stuck with no work outstanding and no buffer free */
803 if (msg == NULL)
804 return -ENOMEM;
805 dp = msg->data;
806 switch (dlci->adaption) {
807 case 1: /* Unstructured */
808 break;
809 case 2: /* Unstructed with modem bits. Always one byte as we never
810 send inline break data */
811 *dp += gsm_encode_modem(dlci);
812 len--;
813 break;
814 }
815 WARN_ON(kfifo_out_locked(dlci->fifo, dp , len, &dlci->lock) != len);
816 __gsm_data_queue(dlci, msg);
817 /* Bytes of data we used up */
818 return size;
819}
820
821/**
822 * gsm_dlci_data_output_framed - try and push data out of a DLCI
823 * @gsm: mux
824 * @dlci: the DLCI to pull data from
825 *
826 * Pull data from a DLCI and send it into the transmit queue if there
827 * is data. Keep to the MRU of the mux. This path handles framed data
828 * queued as skbuffs to the DLCI.
829 *
830 * Caller must hold the tx_lock of the mux.
831 */
832
833static int gsm_dlci_data_output_framed(struct gsm_mux *gsm,
834 struct gsm_dlci *dlci)
835{
836 struct gsm_msg *msg;
837 u8 *dp;
838 int len, size;
839 int last = 0, first = 0;
840 int overhead = 0;
841
842 /* One byte per frame is used for B/F flags */
843 if (dlci->adaption == 4)
844 overhead = 1;
845
846 /* dlci->skb is locked by tx_lock */
847 if (dlci->skb == NULL) {
848 dlci->skb = skb_dequeue(&dlci->skb_list);
849 if (dlci->skb == NULL)
850 return 0;
851 first = 1;
852 }
853 len = dlci->skb->len + overhead;
854
855 /* MTU/MRU count only the data bits */
856 if (len > gsm->mtu) {
857 if (dlci->adaption == 3) {
858 /* Over long frame, bin it */
859 kfree_skb(dlci->skb);
860 dlci->skb = NULL;
861 return 0;
862 }
863 len = gsm->mtu;
864 } else
865 last = 1;
866
867 size = len + overhead;
868 msg = gsm_data_alloc(gsm, dlci->addr, size, gsm->ftype);
869
870 /* FIXME: need a timer or something to kick this so it can't
871 get stuck with no work outstanding and no buffer free */
872 if (msg == NULL)
873 return -ENOMEM;
874 dp = msg->data;
875
876 if (dlci->adaption == 4) { /* Interruptible framed (Packetised Data) */
877 /* Flag byte to carry the start/end info */
878 *dp++ = last << 7 | first << 6 | 1; /* EA */
879 len--;
880 }
881 memcpy(dp, skb_pull(dlci->skb, len), len);
882 __gsm_data_queue(dlci, msg);
883 if (last)
884 dlci->skb = NULL;
885 return size;
886}
887
888/**
889 * gsm_dlci_data_sweep - look for data to send
890 * @gsm: the GSM mux
891 *
892 * Sweep the GSM mux channels in priority order looking for ones with
893 * data to send. We could do with optimising this scan a bit. We aim
894 * to fill the queue totally or up to TX_THRESH_HI bytes. Once we hit
895 * TX_THRESH_LO we get called again
896 *
897 * FIXME: We should round robin between groups and in theory you can
898 * renegotiate DLCI priorities with optional stuff. Needs optimising.
899 */
900
901static void gsm_dlci_data_sweep(struct gsm_mux *gsm)
902{
903 int len;
904 /* Priority ordering: We should do priority with RR of the groups */
905 int i = 1;
906
907 while (i < NUM_DLCI) {
908 struct gsm_dlci *dlci;
909
910 if (gsm->tx_bytes > TX_THRESH_HI)
911 break;
912 dlci = gsm->dlci[i];
913 if (dlci == NULL || dlci->constipated) {
914 i++;
915 continue;
916 }
917 if (dlci->adaption < 3)
918 len = gsm_dlci_data_output(gsm, dlci);
919 else
920 len = gsm_dlci_data_output_framed(gsm, dlci);
921 if (len < 0)
922 break;
923 /* DLCI empty - try the next */
924 if (len == 0)
925 i++;
926 }
927}
928
929/**
930 * gsm_dlci_data_kick - transmit if possible
931 * @dlci: DLCI to kick
932 *
933 * Transmit data from this DLCI if the queue is empty. We can't rely on
934 * a tty wakeup except when we filled the pipe so we need to fire off
935 * new data ourselves in other cases.
936 */
937
938static void gsm_dlci_data_kick(struct gsm_dlci *dlci)
939{
940 unsigned long flags;
941
942 spin_lock_irqsave(&dlci->gsm->tx_lock, flags);
943 /* If we have nothing running then we need to fire up */
944 if (dlci->gsm->tx_bytes == 0)
945 gsm_dlci_data_output(dlci->gsm, dlci);
946 else if (dlci->gsm->tx_bytes < TX_THRESH_LO)
947 gsm_dlci_data_sweep(dlci->gsm);
948 spin_unlock_irqrestore(&dlci->gsm->tx_lock, flags);
949}
950
951/*
952 * Control message processing
953 */
954
955
956/**
957 * gsm_control_reply - send a response frame to a control
958 * @gsm: gsm channel
959 * @cmd: the command to use
960 * @data: data to follow encoded info
961 * @dlen: length of data
962 *
963 * Encode up and queue a UI/UIH frame containing our response.
964 */
965
966static void gsm_control_reply(struct gsm_mux *gsm, int cmd, u8 *data,
967 int dlen)
968{
969 struct gsm_msg *msg;
970 msg = gsm_data_alloc(gsm, 0, dlen + 2, gsm->ftype);
971 msg->data[0] = (cmd & 0xFE) << 1 | EA; /* Clear C/R */
972 msg->data[1] = (dlen << 1) | EA;
973 memcpy(msg->data + 2, data, dlen);
974 gsm_data_queue(gsm->dlci[0], msg);
975}
976
977/**
978 * gsm_process_modem - process received modem status
979 * @tty: virtual tty bound to the DLCI
980 * @dlci: DLCI to affect
981 * @modem: modem bits (full EA)
982 *
983 * Used when a modem control message or line state inline in adaption
984 * layer 2 is processed. Sort out the local modem state and throttles
985 */
986
987static void gsm_process_modem(struct tty_struct *tty, struct gsm_dlci *dlci,
988 u32 modem)
989{
990 int mlines = 0;
991 u8 brk = modem >> 6;
992
993 /* Flow control/ready to communicate */
994 if (modem & MDM_FC) {
995 /* Need to throttle our output on this device */
996 dlci->constipated = 1;
997 }
998 if (modem & MDM_RTC) {
999 mlines |= TIOCM_DSR | TIOCM_DTR;
1000 dlci->constipated = 0;
1001 gsm_dlci_data_kick(dlci);
1002 }
1003 /* Map modem bits */
1004 if (modem & MDM_RTR)
1005 mlines |= TIOCM_RTS | TIOCM_CTS;
1006 if (modem & MDM_IC)
1007 mlines |= TIOCM_RI;
1008 if (modem & MDM_DV)
1009 mlines |= TIOCM_CD;
1010
1011 /* Carrier drop -> hangup */
1012 if (tty) {
1013 if ((mlines & TIOCM_CD) == 0 && (dlci->modem_rx & TIOCM_CD))
1014 if (!(tty->termios->c_cflag & CLOCAL))
1015 tty_hangup(tty);
1016 if (brk & 0x01)
1017 tty_insert_flip_char(tty, 0, TTY_BREAK);
1018 }
1019 dlci->modem_rx = mlines;
1020}
1021
1022/**
1023 * gsm_control_modem - modem status received
1024 * @gsm: GSM channel
1025 * @data: data following command
1026 * @clen: command length
1027 *
1028 * We have received a modem status control message. This is used by
1029 * the GSM mux protocol to pass virtual modem line status and optionally
1030 * to indicate break signals. Unpack it, convert to Linux representation
1031 * and if need be stuff a break message down the tty.
1032 */
1033
1034static void gsm_control_modem(struct gsm_mux *gsm, u8 *data, int clen)
1035{
1036 unsigned int addr = 0;
1037 unsigned int modem = 0;
1038 struct gsm_dlci *dlci;
1039 int len = clen;
1040 u8 *dp = data;
1041 struct tty_struct *tty;
1042
1043 while (gsm_read_ea(&addr, *dp++) == 0) {
1044 len--;
1045 if (len == 0)
1046 return;
1047 }
1048 /* Must be at least one byte following the EA */
1049 len--;
1050 if (len <= 0)
1051 return;
1052
1053 addr >>= 1;
1054 /* Closed port, or invalid ? */
1055 if (addr == 0 || addr >= NUM_DLCI || gsm->dlci[addr] == NULL)
1056 return;
1057 dlci = gsm->dlci[addr];
1058
1059 while (gsm_read_ea(&modem, *dp++) == 0) {
1060 len--;
1061 if (len == 0)
1062 return;
1063 }
1064 tty = tty_port_tty_get(&dlci->port);
1065 gsm_process_modem(tty, dlci, modem);
1066 if (tty) {
1067 tty_wakeup(tty);
1068 tty_kref_put(tty);
1069 }
1070 gsm_control_reply(gsm, CMD_MSC, data, clen);
1071}
1072
1073/**
1074 * gsm_control_rls - remote line status
1075 * @gsm: GSM channel
1076 * @data: data bytes
1077 * @clen: data length
1078 *
1079 * The modem sends us a two byte message on the control channel whenever
1080 * it wishes to send us an error state from the virtual link. Stuff
1081 * this into the uplink tty if present
1082 */
1083
1084static void gsm_control_rls(struct gsm_mux *gsm, u8 *data, int clen)
1085{
1086 struct tty_struct *tty;
1087 unsigned int addr = 0 ;
1088 u8 bits;
1089 int len = clen;
1090 u8 *dp = data;
1091
1092 while (gsm_read_ea(&addr, *dp++) == 0) {
1093 len--;
1094 if (len == 0)
1095 return;
1096 }
1097 /* Must be at least one byte following ea */
1098 len--;
1099 if (len <= 0)
1100 return;
1101 addr >>= 1;
1102 /* Closed port, or invalid ? */
1103 if (addr == 0 || addr >= NUM_DLCI || gsm->dlci[addr] == NULL)
1104 return;
1105 /* No error ? */
1106 bits = *dp;
1107 if ((bits & 1) == 0)
1108 return;
1109 /* See if we have an uplink tty */
1110 tty = tty_port_tty_get(&gsm->dlci[addr]->port);
1111
1112 if (tty) {
1113 if (bits & 2)
1114 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
1115 if (bits & 4)
1116 tty_insert_flip_char(tty, 0, TTY_PARITY);
1117 if (bits & 8)
1118 tty_insert_flip_char(tty, 0, TTY_FRAME);
1119 tty_flip_buffer_push(tty);
1120 tty_kref_put(tty);
1121 }
1122 gsm_control_reply(gsm, CMD_RLS, data, clen);
1123}
1124
1125static void gsm_dlci_begin_close(struct gsm_dlci *dlci);
1126
1127/**
1128 * gsm_control_message - DLCI 0 control processing
1129 * @gsm: our GSM mux
1130 * @command: the command EA
1131 * @data: data beyond the command/length EAs
1132 * @clen: length
1133 *
1134 * Input processor for control messages from the other end of the link.
1135 * Processes the incoming request and queues a response frame or an
1136 * NSC response if not supported
1137 */
1138
1139static void gsm_control_message(struct gsm_mux *gsm, unsigned int command,
1140 u8 *data, int clen)
1141{
1142 u8 buf[1];
1143 switch (command) {
1144 case CMD_CLD: {
1145 struct gsm_dlci *dlci = gsm->dlci[0];
1146 /* Modem wishes to close down */
1147 if (dlci) {
1148 dlci->dead = 1;
1149 gsm->dead = 1;
1150 gsm_dlci_begin_close(dlci);
1151 }
1152 }
1153 break;
1154 case CMD_TEST:
1155 /* Modem wishes to test, reply with the data */
1156 gsm_control_reply(gsm, CMD_TEST, data, clen);
1157 break;
1158 case CMD_FCON:
1159 /* Modem wants us to STFU */
1160 gsm->constipated = 1;
1161 gsm_control_reply(gsm, CMD_FCON, NULL, 0);
1162 break;
1163 case CMD_FCOFF:
1164 /* Modem can accept data again */
1165 gsm->constipated = 0;
1166 gsm_control_reply(gsm, CMD_FCOFF, NULL, 0);
1167 /* Kick the link in case it is idling */
1168 gsm_data_kick(gsm);
1169 break;
1170 case CMD_MSC:
1171 /* Out of band modem line change indicator for a DLCI */
1172 gsm_control_modem(gsm, data, clen);
1173 break;
1174 case CMD_RLS:
1175 /* Out of band error reception for a DLCI */
1176 gsm_control_rls(gsm, data, clen);
1177 break;
1178 case CMD_PSC:
1179 /* Modem wishes to enter power saving state */
1180 gsm_control_reply(gsm, CMD_PSC, NULL, 0);
1181 break;
1182 /* Optional unsupported commands */
1183 case CMD_PN: /* Parameter negotiation */
1184 case CMD_RPN: /* Remote port negotation */
1185 case CMD_SNC: /* Service negotation command */
1186 default:
1187 /* Reply to bad commands with an NSC */
1188 buf[0] = command;
1189 gsm_control_reply(gsm, CMD_NSC, buf, 1);
1190 break;
1191 }
1192}
1193
1194/**
1195 * gsm_control_response - process a response to our control
1196 * @gsm: our GSM mux
1197 * @command: the command (response) EA
1198 * @data: data beyond the command/length EA
1199 * @clen: length
1200 *
1201 * Process a response to an outstanding command. We only allow a single
1202 * control message in flight so this is fairly easy. All the clean up
1203 * is done by the caller, we just update the fields, flag it as done
1204 * and return
1205 */
1206
1207static void gsm_control_response(struct gsm_mux *gsm, unsigned int command,
1208 u8 *data, int clen)
1209{
1210 struct gsm_control *ctrl;
1211 unsigned long flags;
1212
1213 spin_lock_irqsave(&gsm->control_lock, flags);
1214
1215 ctrl = gsm->pending_cmd;
1216 /* Does the reply match our command */
1217 command |= 1;
1218 if (ctrl != NULL && (command == ctrl->cmd || command == CMD_NSC)) {
1219 /* Our command was replied to, kill the retry timer */
1220 del_timer(&gsm->t2_timer);
1221 gsm->pending_cmd = NULL;
1222 /* Rejected by the other end */
1223 if (command == CMD_NSC)
1224 ctrl->error = -EOPNOTSUPP;
1225 ctrl->done = 1;
1226 wake_up(&gsm->event);
1227 }
1228 spin_unlock_irqrestore(&gsm->control_lock, flags);
1229}
1230
1231/**
1232 * gsm_control_transmit - send control packet
1233 * @gsm: gsm mux
1234 * @ctrl: frame to send
1235 *
1236 * Send out a pending control command (called under control lock)
1237 */
1238
1239static void gsm_control_transmit(struct gsm_mux *gsm, struct gsm_control *ctrl)
1240{
1241 struct gsm_msg *msg = gsm_data_alloc(gsm, 0, ctrl->len + 1,
1242 gsm->ftype|PF);
1243 if (msg == NULL)
1244 return;
1245 msg->data[0] = (ctrl->cmd << 1) | 2 | EA; /* command */
1246 memcpy(msg->data + 1, ctrl->data, ctrl->len);
1247 gsm_data_queue(gsm->dlci[0], msg);
1248}
1249
1250/**
1251 * gsm_control_retransmit - retransmit a control frame
1252 * @data: pointer to our gsm object
1253 *
1254 * Called off the T2 timer expiry in order to retransmit control frames
1255 * that have been lost in the system somewhere. The control_lock protects
1256 * us from colliding with another sender or a receive completion event.
1257 * In that situation the timer may still occur in a small window but
1258 * gsm->pending_cmd will be NULL and we just let the timer expire.
1259 */
1260
1261static void gsm_control_retransmit(unsigned long data)
1262{
1263 struct gsm_mux *gsm = (struct gsm_mux *)data;
1264 struct gsm_control *ctrl;
1265 unsigned long flags;
1266 spin_lock_irqsave(&gsm->control_lock, flags);
1267 ctrl = gsm->pending_cmd;
1268 if (ctrl) {
1269 gsm->cretries--;
1270 if (gsm->cretries == 0) {
1271 gsm->pending_cmd = NULL;
1272 ctrl->error = -ETIMEDOUT;
1273 ctrl->done = 1;
1274 spin_unlock_irqrestore(&gsm->control_lock, flags);
1275 wake_up(&gsm->event);
1276 return;
1277 }
1278 gsm_control_transmit(gsm, ctrl);
1279 mod_timer(&gsm->t2_timer, jiffies + gsm->t2 * HZ / 100);
1280 }
1281 spin_unlock_irqrestore(&gsm->control_lock, flags);
1282}
1283
1284/**
1285 * gsm_control_send - send a control frame on DLCI 0
1286 * @gsm: the GSM channel
1287 * @command: command to send including CR bit
1288 * @data: bytes of data (must be kmalloced)
1289 * @len: length of the block to send
1290 *
1291 * Queue and dispatch a control command. Only one command can be
1292 * active at a time. In theory more can be outstanding but the matching
1293 * gets really complicated so for now stick to one outstanding.
1294 */
1295
1296static struct gsm_control *gsm_control_send(struct gsm_mux *gsm,
1297 unsigned int command, u8 *data, int clen)
1298{
1299 struct gsm_control *ctrl = kzalloc(sizeof(struct gsm_control),
1300 GFP_KERNEL);
1301 unsigned long flags;
1302 if (ctrl == NULL)
1303 return NULL;
1304retry:
1305 wait_event(gsm->event, gsm->pending_cmd == NULL);
1306 spin_lock_irqsave(&gsm->control_lock, flags);
1307 if (gsm->pending_cmd != NULL) {
1308 spin_unlock_irqrestore(&gsm->control_lock, flags);
1309 goto retry;
1310 }
1311 ctrl->cmd = command;
1312 ctrl->data = data;
1313 ctrl->len = clen;
1314 gsm->pending_cmd = ctrl;
1315 gsm->cretries = gsm->n2;
1316 mod_timer(&gsm->t2_timer, jiffies + gsm->t2 * HZ / 100);
1317 gsm_control_transmit(gsm, ctrl);
1318 spin_unlock_irqrestore(&gsm->control_lock, flags);
1319 return ctrl;
1320}
1321
1322/**
1323 * gsm_control_wait - wait for a control to finish
1324 * @gsm: GSM mux
1325 * @control: control we are waiting on
1326 *
1327 * Waits for the control to complete or time out. Frees any used
1328 * resources and returns 0 for success, or an error if the remote
1329 * rejected or ignored the request.
1330 */
1331
1332static int gsm_control_wait(struct gsm_mux *gsm, struct gsm_control *control)
1333{
1334 int err;
1335 wait_event(gsm->event, control->done == 1);
1336 err = control->error;
1337 kfree(control);
1338 return err;
1339}
1340
1341
1342/*
1343 * DLCI level handling: Needs krefs
1344 */
1345
1346/*
1347 * State transitions and timers
1348 */
1349
1350/**
1351 * gsm_dlci_close - a DLCI has closed
1352 * @dlci: DLCI that closed
1353 *
1354 * Perform processing when moving a DLCI into closed state. If there
1355 * is an attached tty this is hung up
1356 */
1357
1358static void gsm_dlci_close(struct gsm_dlci *dlci)
1359{
1360 del_timer(&dlci->t1);
1361 if (debug & 8)
1362 printk("DLCI %d goes closed.\n", dlci->addr);
1363 dlci->state = DLCI_CLOSED;
1364 if (dlci->addr != 0) {
1365 struct tty_struct *tty = tty_port_tty_get(&dlci->port);
1366 if (tty) {
1367 tty_hangup(tty);
1368 tty_kref_put(tty);
1369 }
1370 kfifo_reset(dlci->fifo);
1371 } else
1372 dlci->gsm->dead = 1;
1373 wake_up(&dlci->gsm->event);
1374 /* A DLCI 0 close is a MUX termination so we need to kick that
1375 back to userspace somehow */
1376}
1377
1378/**
1379 * gsm_dlci_open - a DLCI has opened
1380 * @dlci: DLCI that opened
1381 *
1382 * Perform processing when moving a DLCI into open state.
1383 */
1384
1385static void gsm_dlci_open(struct gsm_dlci *dlci)
1386{
1387 /* Note that SABM UA .. SABM UA first UA lost can mean that we go
1388 open -> open */
1389 del_timer(&dlci->t1);
1390 /* This will let a tty open continue */
1391 dlci->state = DLCI_OPEN;
1392 if (debug & 8)
1393 printk("DLCI %d goes open.\n", dlci->addr);
1394 wake_up(&dlci->gsm->event);
1395}
1396
1397/**
1398 * gsm_dlci_t1 - T1 timer expiry
1399 * @dlci: DLCI that opened
1400 *
1401 * The T1 timer handles retransmits of control frames (essentially of
1402 * SABM and DISC). We resend the command until the retry count runs out
1403 * in which case an opening port goes back to closed and a closing port
1404 * is simply put into closed state (any further frames from the other
1405 * end will get a DM response)
1406 */
1407
1408static void gsm_dlci_t1(unsigned long data)
1409{
1410 struct gsm_dlci *dlci = (struct gsm_dlci *)data;
1411 struct gsm_mux *gsm = dlci->gsm;
1412
1413 switch (dlci->state) {
1414 case DLCI_OPENING:
1415 dlci->retries--;
1416 if (dlci->retries) {
1417 gsm_command(dlci->gsm, dlci->addr, SABM|PF);
1418 mod_timer(&dlci->t1, jiffies + gsm->t1 * HZ / 100);
1419 } else
1420 gsm_dlci_close(dlci);
1421 break;
1422 case DLCI_CLOSING:
1423 dlci->retries--;
1424 if (dlci->retries) {
1425 gsm_command(dlci->gsm, dlci->addr, DISC|PF);
1426 mod_timer(&dlci->t1, jiffies + gsm->t1 * HZ / 100);
1427 } else
1428 gsm_dlci_close(dlci);
1429 break;
1430 }
1431}
1432
1433/**
1434 * gsm_dlci_begin_open - start channel open procedure
1435 * @dlci: DLCI to open
1436 *
1437 * Commence opening a DLCI from the Linux side. We issue SABM messages
1438 * to the modem which should then reply with a UA, at which point we
1439 * will move into open state. Opening is done asynchronously with retry
1440 * running off timers and the responses.
1441 */
1442
1443static void gsm_dlci_begin_open(struct gsm_dlci *dlci)
1444{
1445 struct gsm_mux *gsm = dlci->gsm;
1446 if (dlci->state == DLCI_OPEN || dlci->state == DLCI_OPENING)
1447 return;
1448 dlci->retries = gsm->n2;
1449 dlci->state = DLCI_OPENING;
1450 gsm_command(dlci->gsm, dlci->addr, SABM|PF);
1451 mod_timer(&dlci->t1, jiffies + gsm->t1 * HZ / 100);
1452}
1453
1454/**
1455 * gsm_dlci_begin_close - start channel open procedure
1456 * @dlci: DLCI to open
1457 *
1458 * Commence closing a DLCI from the Linux side. We issue DISC messages
1459 * to the modem which should then reply with a UA, at which point we
1460 * will move into closed state. Closing is done asynchronously with retry
1461 * off timers. We may also receive a DM reply from the other end which
1462 * indicates the channel was already closed.
1463 */
1464
1465static void gsm_dlci_begin_close(struct gsm_dlci *dlci)
1466{
1467 struct gsm_mux *gsm = dlci->gsm;
1468 if (dlci->state == DLCI_CLOSED || dlci->state == DLCI_CLOSING)
1469 return;
1470 dlci->retries = gsm->n2;
1471 dlci->state = DLCI_CLOSING;
1472 gsm_command(dlci->gsm, dlci->addr, DISC|PF);
1473 mod_timer(&dlci->t1, jiffies + gsm->t1 * HZ / 100);
1474}
1475
1476/**
1477 * gsm_dlci_data - data arrived
1478 * @dlci: channel
1479 * @data: block of bytes received
1480 * @len: length of received block
1481 *
1482 * A UI or UIH frame has arrived which contains data for a channel
1483 * other than the control channel. If the relevant virtual tty is
1484 * open we shovel the bits down it, if not we drop them.
1485 */
1486
1487static void gsm_dlci_data(struct gsm_dlci *dlci, u8 *data, int len)
1488{
1489 /* krefs .. */
1490 struct tty_port *port = &dlci->port;
1491 struct tty_struct *tty = tty_port_tty_get(port);
1492 unsigned int modem = 0;
1493
1494 if (debug & 16)
1495 printk("%d bytes for tty %p\n", len, tty);
1496 if (tty) {
1497 switch (dlci->adaption) {
1498 /* Unsupported types */
1499 /* Packetised interruptible data */
1500 case 4:
1501 break;
1502 /* Packetised uininterruptible voice/data */
1503 case 3:
1504 break;
1505 /* Asynchronous serial with line state in each frame */
1506 case 2:
1507 while (gsm_read_ea(&modem, *data++) == 0) {
1508 len--;
1509 if (len == 0)
1510 return;
1511 }
1512 gsm_process_modem(tty, dlci, modem);
1513 /* Line state will go via DLCI 0 controls only */
1514 case 1:
1515 default:
1516 tty_insert_flip_string(tty, data, len);
1517 tty_flip_buffer_push(tty);
1518 }
1519 tty_kref_put(tty);
1520 }
1521}
1522
1523/**
1524 * gsm_dlci_control - data arrived on control channel
1525 * @dlci: channel
1526 * @data: block of bytes received
1527 * @len: length of received block
1528 *
1529 * A UI or UIH frame has arrived which contains data for DLCI 0 the
1530 * control channel. This should contain a command EA followed by
1531 * control data bytes. The command EA contains a command/response bit
1532 * and we divide up the work accordingly.
1533 */
1534
1535static void gsm_dlci_command(struct gsm_dlci *dlci, u8 *data, int len)
1536{
1537 /* See what command is involved */
1538 unsigned int command = 0;
1539 while (len-- > 0) {
1540 if (gsm_read_ea(&command, *data++) == 1) {
1541 int clen = *data++;
1542 len--;
1543 /* FIXME: this is properly an EA */
1544 clen >>= 1;
1545 /* Malformed command ? */
1546 if (clen > len)
1547 return;
1548 if (command & 1)
1549 gsm_control_message(dlci->gsm, command,
1550 data, clen);
1551 else
1552 gsm_control_response(dlci->gsm, command,
1553 data, clen);
1554 return;
1555 }
1556 }
1557}
1558
1559/*
1560 * Allocate/Free DLCI channels
1561 */
1562
1563/**
1564 * gsm_dlci_alloc - allocate a DLCI
1565 * @gsm: GSM mux
1566 * @addr: address of the DLCI
1567 *
1568 * Allocate and install a new DLCI object into the GSM mux.
1569 *
1570 * FIXME: review locking races
1571 */
1572
1573static struct gsm_dlci *gsm_dlci_alloc(struct gsm_mux *gsm, int addr)
1574{
1575 struct gsm_dlci *dlci = kzalloc(sizeof(struct gsm_dlci), GFP_ATOMIC);
1576 if (dlci == NULL)
1577 return NULL;
1578 spin_lock_init(&dlci->lock);
1579 dlci->fifo = &dlci->_fifo;
1580 if (kfifo_alloc(&dlci->_fifo, 4096, GFP_KERNEL) < 0) {
1581 kfree(dlci);
1582 return NULL;
1583 }
1584
1585 skb_queue_head_init(&dlci->skb_list);
1586 init_timer(&dlci->t1);
1587 dlci->t1.function = gsm_dlci_t1;
1588 dlci->t1.data = (unsigned long)dlci;
1589 tty_port_init(&dlci->port);
1590 dlci->port.ops = &gsm_port_ops;
1591 dlci->gsm = gsm;
1592 dlci->addr = addr;
1593 dlci->adaption = gsm->adaption;
1594 dlci->state = DLCI_CLOSED;
1595 if (addr)
1596 dlci->data = gsm_dlci_data;
1597 else
1598 dlci->data = gsm_dlci_command;
1599 gsm->dlci[addr] = dlci;
1600 return dlci;
1601}
1602
1603/**
1604 * gsm_dlci_free - release DLCI
1605 * @dlci: DLCI to destroy
1606 *
1607 * Free up a DLCI. Currently to keep the lifetime rules sane we only
1608 * clean up DLCI objects when the MUX closes rather than as the port
1609 * is closed down on both the tty and mux levels.
1610 *
1611 * Can sleep.
1612 */
1613static void gsm_dlci_free(struct gsm_dlci *dlci)
1614{
1615 struct tty_struct *tty = tty_port_tty_get(&dlci->port);
1616 if (tty) {
1617 tty_vhangup(tty);
1618 tty_kref_put(tty);
1619 }
1620 del_timer_sync(&dlci->t1);
1621 dlci->gsm->dlci[dlci->addr] = NULL;
1622 kfifo_free(dlci->fifo);
1623 kfree(dlci);
1624}
1625
1626
1627/*
1628 * LAPBish link layer logic
1629 */
1630
1631/**
1632 * gsm_queue - a GSM frame is ready to process
1633 * @gsm: pointer to our gsm mux
1634 *
1635 * At this point in time a frame has arrived and been demangled from
1636 * the line encoding. All the differences between the encodings have
1637 * been handled below us and the frame is unpacked into the structures.
1638 * The fcs holds the header FCS but any data FCS must be added here.
1639 */
1640
1641static void gsm_queue(struct gsm_mux *gsm)
1642{
1643 struct gsm_dlci *dlci;
1644 u8 cr;
1645 int address;
1646 /* We have to sneak a look at the packet body to do the FCS.
1647 A somewhat layering violation in the spec */
1648
1649 if ((gsm->control & ~PF) == UI)
1650 gsm->fcs = gsm_fcs_add_block(gsm->fcs, gsm->buf, gsm->len);
1651 if (gsm->fcs != GOOD_FCS) {
1652 gsm->bad_fcs++;
1653 if (debug & 4)
1654 printk("BAD FCS %02x\n", gsm->fcs);
1655 return;
1656 }
1657 address = gsm->address >> 1;
1658 if (address >= NUM_DLCI)
1659 goto invalid;
1660
1661 cr = gsm->address & 1; /* C/R bit */
1662
1663 gsm_print_packet("<--", address, cr, gsm->control, gsm->buf, gsm->len);
1664
1665 cr ^= 1 - gsm->initiator; /* Flip so 1 always means command */
1666 dlci = gsm->dlci[address];
1667
1668 switch (gsm->control) {
1669 case SABM|PF:
1670 if (cr == 0)
1671 goto invalid;
1672 if (dlci == NULL)
1673 dlci = gsm_dlci_alloc(gsm, address);
1674 if (dlci == NULL)
1675 return;
1676 if (dlci->dead)
1677 gsm_response(gsm, address, DM);
1678 else {
1679 gsm_response(gsm, address, UA);
1680 gsm_dlci_open(dlci);
1681 }
1682 break;
1683 case DISC|PF:
1684 if (cr == 0)
1685 goto invalid;
1686 if (dlci == NULL || dlci->state == DLCI_CLOSED) {
1687 gsm_response(gsm, address, DM);
1688 return;
1689 }
1690 /* Real close complete */
1691 gsm_response(gsm, address, UA);
1692 gsm_dlci_close(dlci);
1693 break;
1694 case UA:
1695 case UA|PF:
1696 if (cr == 0 || dlci == NULL)
1697 break;
1698 switch (dlci->state) {
1699 case DLCI_CLOSING:
1700 gsm_dlci_close(dlci);
1701 break;
1702 case DLCI_OPENING:
1703 gsm_dlci_open(dlci);
1704 break;
1705 }
1706 break;
1707 case DM: /* DM can be valid unsolicited */
1708 case DM|PF:
1709 if (cr)
1710 goto invalid;
1711 if (dlci == NULL)
1712 return;
1713 gsm_dlci_close(dlci);
1714 break;
1715 case UI:
1716 case UI|PF:
1717 case UIH:
1718 case UIH|PF:
1719#if 0
1720 if (cr)
1721 goto invalid;
1722#endif
1723 if (dlci == NULL || dlci->state != DLCI_OPEN) {
1724 gsm_command(gsm, address, DM|PF);
1725 return;
1726 }
1727 dlci->data(dlci, gsm->buf, gsm->len);
1728 break;
1729 default:
1730 goto invalid;
1731 }
1732 return;
1733invalid:
1734 gsm->malformed++;
1735 return;
1736}
1737
1738
1739/**
1740 * gsm0_receive - perform processing for non-transparency
1741 * @gsm: gsm data for this ldisc instance
1742 * @c: character
1743 *
1744 * Receive bytes in gsm mode 0
1745 */
1746
1747static void gsm0_receive(struct gsm_mux *gsm, unsigned char c)
1748{
1749 switch (gsm->state) {
1750 case GSM_SEARCH: /* SOF marker */
1751 if (c == GSM0_SOF) {
1752 gsm->state = GSM_ADDRESS;
1753 gsm->address = 0;
1754 gsm->len = 0;
1755 gsm->fcs = INIT_FCS;
1756 }
1757 break; /* Address EA */
1758 case GSM_ADDRESS:
1759 gsm->fcs = gsm_fcs_add(gsm->fcs, c);
1760 if (gsm_read_ea(&gsm->address, c))
1761 gsm->state = GSM_CONTROL;
1762 break;
1763 case GSM_CONTROL: /* Control Byte */
1764 gsm->fcs = gsm_fcs_add(gsm->fcs, c);
1765 gsm->control = c;
1766 gsm->state = GSM_LEN;
1767 break;
1768 case GSM_LEN: /* Length EA */
1769 gsm->fcs = gsm_fcs_add(gsm->fcs, c);
1770 if (gsm_read_ea(&gsm->len, c)) {
1771 if (gsm->len > gsm->mru) {
1772 gsm->bad_size++;
1773 gsm->state = GSM_SEARCH;
1774 break;
1775 }
1776 gsm->count = 0;
1777 gsm->state = GSM_DATA;
1778 }
1779 break;
1780 case GSM_DATA: /* Data */
1781 gsm->buf[gsm->count++] = c;
1782 if (gsm->count == gsm->len)
1783 gsm->state = GSM_FCS;
1784 break;
1785 case GSM_FCS: /* FCS follows the packet */
1786 gsm->fcs = c;
1787 gsm_queue(gsm);
1788 /* And then back for the next frame */
1789 gsm->state = GSM_SEARCH;
1790 break;
1791 }
1792}
1793
1794/**
1795 * gsm0_receive - perform processing for non-transparency
1796 * @gsm: gsm data for this ldisc instance
1797 * @c: character
1798 *
1799 * Receive bytes in mode 1 (Advanced option)
1800 */
1801
1802static void gsm1_receive(struct gsm_mux *gsm, unsigned char c)
1803{
1804 if (c == GSM1_SOF) {
1805 /* EOF is only valid in frame if we have got to the data state
1806 and received at least one byte (the FCS) */
1807 if (gsm->state == GSM_DATA && gsm->count) {
1808 /* Extract the FCS */
1809 gsm->count--;
1810 gsm->fcs = gsm_fcs_add(gsm->fcs, gsm->buf[gsm->count]);
1811 gsm->len = gsm->count;
1812 gsm_queue(gsm);
1813 gsm->state = GSM_START;
1814 return;
1815 }
1816 /* Any partial frame was a runt so go back to start */
1817 if (gsm->state != GSM_START) {
1818 gsm->malformed++;
1819 gsm->state = GSM_START;
1820 }
1821 /* A SOF in GSM_START means we are still reading idling or
1822 framing bytes */
1823 return;
1824 }
1825
1826 if (c == GSM1_ESCAPE) {
1827 gsm->escape = 1;
1828 return;
1829 }
1830
1831 /* Only an unescaped SOF gets us out of GSM search */
1832 if (gsm->state == GSM_SEARCH)
1833 return;
1834
1835 if (gsm->escape) {
1836 c ^= GSM1_ESCAPE_BITS;
1837 gsm->escape = 0;
1838 }
1839 switch (gsm->state) {
1840 case GSM_START: /* First byte after SOF */
1841 gsm->address = 0;
1842 gsm->state = GSM_ADDRESS;
1843 gsm->fcs = INIT_FCS;
1844 /* Drop through */
1845 case GSM_ADDRESS: /* Address continuation */
1846 gsm->fcs = gsm_fcs_add(gsm->fcs, c);
1847 if (gsm_read_ea(&gsm->address, c))
1848 gsm->state = GSM_CONTROL;
1849 break;
1850 case GSM_CONTROL: /* Control Byte */
1851 gsm->fcs = gsm_fcs_add(gsm->fcs, c);
1852 gsm->control = c;
1853 gsm->count = 0;
1854 gsm->state = GSM_DATA;
1855 break;
1856 case GSM_DATA: /* Data */
1857 if (gsm->count > gsm->mru ) { /* Allow one for the FCS */
1858 gsm->state = GSM_OVERRUN;
1859 gsm->bad_size++;
1860 } else
1861 gsm->buf[gsm->count++] = c;
1862 break;
1863 case GSM_OVERRUN: /* Over-long - eg a dropped SOF */
1864 break;
1865 }
1866}
1867
1868/**
1869 * gsm_error - handle tty error
1870 * @gsm: ldisc data
1871 * @data: byte received (may be invalid)
1872 * @flag: error received
1873 *
1874 * Handle an error in the receipt of data for a frame. Currently we just
1875 * go back to hunting for a SOF.
1876 *
1877 * FIXME: better diagnostics ?
1878 */
1879
1880static void gsm_error(struct gsm_mux *gsm,
1881 unsigned char data, unsigned char flag)
1882{
1883 gsm->state = GSM_SEARCH;
1884 gsm->io_error++;
1885}
1886
1887/**
1888 * gsm_cleanup_mux - generic GSM protocol cleanup
1889 * @gsm: our mux
1890 *
1891 * Clean up the bits of the mux which are the same for all framing
1892 * protocols. Remove the mux from the mux table, stop all the timers
1893 * and then shut down each device hanging up the channels as we go.
1894 */
1895
1896void gsm_cleanup_mux(struct gsm_mux *gsm)
1897{
1898 int i;
1899 struct gsm_dlci *dlci = gsm->dlci[0];
1900 struct gsm_msg *txq;
1901
1902 gsm->dead = 1;
1903
1904 spin_lock(&gsm_mux_lock);
1905 for (i = 0; i < MAX_MUX; i++) {
1906 if (gsm_mux[i] == gsm) {
1907 gsm_mux[i] = NULL;
1908 break;
1909 }
1910 }
1911 spin_unlock(&gsm_mux_lock);
1912 WARN_ON(i == MAX_MUX);
1913
1914 del_timer_sync(&gsm->t2_timer);
1915 /* Now we are sure T2 has stopped */
1916 if (dlci) {
1917 dlci->dead = 1;
1918 gsm_dlci_begin_close(dlci);
1919 wait_event_interruptible(gsm->event,
1920 dlci->state == DLCI_CLOSED);
1921 }
1922 /* Free up any link layer users */
1923 for (i = 0; i < NUM_DLCI; i++)
1924 if (gsm->dlci[i])
1925 gsm_dlci_free(gsm->dlci[i]);
1926 /* Now wipe the queues */
1927 for (txq = gsm->tx_head; txq != NULL; txq = gsm->tx_head) {
1928 gsm->tx_head = txq->next;
1929 kfree(txq);
1930 }
1931 gsm->tx_tail = NULL;
1932}
1933EXPORT_SYMBOL_GPL(gsm_cleanup_mux);
1934
1935/**
1936 * gsm_activate_mux - generic GSM setup
1937 * @gsm: our mux
1938 *
1939 * Set up the bits of the mux which are the same for all framing
1940 * protocols. Add the mux to the mux table so it can be opened and
1941 * finally kick off connecting to DLCI 0 on the modem.
1942 */
1943
1944int gsm_activate_mux(struct gsm_mux *gsm)
1945{
1946 struct gsm_dlci *dlci;
1947 int i = 0;
1948
1949 init_timer(&gsm->t2_timer);
1950 gsm->t2_timer.function = gsm_control_retransmit;
1951 gsm->t2_timer.data = (unsigned long)gsm;
1952 init_waitqueue_head(&gsm->event);
1953 spin_lock_init(&gsm->control_lock);
1954 spin_lock_init(&gsm->tx_lock);
1955
1956 if (gsm->encoding == 0)
1957 gsm->receive = gsm0_receive;
1958 else
1959 gsm->receive = gsm1_receive;
1960 gsm->error = gsm_error;
1961
1962 spin_lock(&gsm_mux_lock);
1963 for (i = 0; i < MAX_MUX; i++) {
1964 if (gsm_mux[i] == NULL) {
1965 gsm_mux[i] = gsm;
1966 break;
1967 }
1968 }
1969 spin_unlock(&gsm_mux_lock);
1970 if (i == MAX_MUX)
1971 return -EBUSY;
1972
1973 dlci = gsm_dlci_alloc(gsm, 0);
1974 if (dlci == NULL)
1975 return -ENOMEM;
1976 gsm->dead = 0; /* Tty opens are now permissible */
1977 return 0;
1978}
1979EXPORT_SYMBOL_GPL(gsm_activate_mux);
1980
1981/**
1982 * gsm_free_mux - free up a mux
1983 * @mux: mux to free
1984 *
1985 * Dispose of allocated resources for a dead mux. No refcounting
1986 * at present so the mux must be truely dead.
1987 */
1988void gsm_free_mux(struct gsm_mux *gsm)
1989{
1990 kfree(gsm->txframe);
1991 kfree(gsm->buf);
1992 kfree(gsm);
1993}
1994EXPORT_SYMBOL_GPL(gsm_free_mux);
1995
1996/**
1997 * gsm_alloc_mux - allocate a mux
1998 *
1999 * Creates a new mux ready for activation.
2000 */
2001
2002struct gsm_mux *gsm_alloc_mux(void)
2003{
2004 struct gsm_mux *gsm = kzalloc(sizeof(struct gsm_mux), GFP_KERNEL);
2005 if (gsm == NULL)
2006 return NULL;
2007 gsm->buf = kmalloc(MAX_MRU + 1, GFP_KERNEL);
2008 if (gsm->buf == NULL) {
2009 kfree(gsm);
2010 return NULL;
2011 }
2012 gsm->txframe = kmalloc(2 * MAX_MRU + 2, GFP_KERNEL);
2013 if (gsm->txframe == NULL) {
2014 kfree(gsm->buf);
2015 kfree(gsm);
2016 return NULL;
2017 }
2018 spin_lock_init(&gsm->lock);
2019
2020 gsm->t1 = T1;
2021 gsm->t2 = T2;
2022 gsm->n2 = N2;
2023 gsm->ftype = UIH;
2024 gsm->initiator = 0;
2025 gsm->adaption = 1;
2026 gsm->encoding = 1;
2027 gsm->mru = 64; /* Default to encoding 1 so these should be 64 */
2028 gsm->mtu = 64;
2029 gsm->dead = 1; /* Avoid early tty opens */
2030
2031 return gsm;
2032}
2033EXPORT_SYMBOL_GPL(gsm_alloc_mux);
2034
2035
2036
2037
2038/**
2039 * gsmld_output - write to link
2040 * @gsm: our mux
2041 * @data: bytes to output
2042 * @len: size
2043 *
2044 * Write a block of data from the GSM mux to the data channel. This
2045 * will eventually be serialized from above but at the moment isn't.
2046 */
2047
2048static int gsmld_output(struct gsm_mux *gsm, u8 *data, int len)
2049{
2050 if (tty_write_room(gsm->tty) < len) {
2051 set_bit(TTY_DO_WRITE_WAKEUP, &gsm->tty->flags);
2052 return -ENOSPC;
2053 }
2054 if (debug & 4) {
2055 printk("-->%d bytes out\n", len);
2056 hex_packet(data, len);
2057 }
2058 gsm->tty->ops->write(gsm->tty, data, len);
2059 return len;
2060}
2061
2062/**
2063 * gsmld_attach_gsm - mode set up
2064 * @tty: our tty structure
2065 * @gsm: our mux
2066 *
2067 * Set up the MUX for basic mode and commence connecting to the
2068 * modem. Currently called from the line discipline set up but
2069 * will need moving to an ioctl path.
2070 */
2071
2072static int gsmld_attach_gsm(struct tty_struct *tty, struct gsm_mux *gsm)
2073{
2074 int ret;
2075
2076 gsm->tty = tty_kref_get(tty);
2077 gsm->output = gsmld_output;
2078 ret = gsm_activate_mux(gsm);
2079 if (ret != 0)
2080 tty_kref_put(gsm->tty);
2081 return ret;
2082}
2083
2084
2085/**
2086 * gsmld_detach_gsm - stop doing 0710 mux
2087 * @tty: tty atttached to the mux
2088 * @gsm: mux
2089 *
2090 * Shutdown and then clean up the resources used by the line discipline
2091 */
2092
2093static void gsmld_detach_gsm(struct tty_struct *tty, struct gsm_mux *gsm)
2094{
2095 WARN_ON(tty != gsm->tty);
2096 gsm_cleanup_mux(gsm);
2097 tty_kref_put(gsm->tty);
2098 gsm->tty = NULL;
2099}
2100
2101static void gsmld_receive_buf(struct tty_struct *tty, const unsigned char *cp,
2102 char *fp, int count)
2103{
2104 struct gsm_mux *gsm = tty->disc_data;
2105 const unsigned char *dp;
2106 char *f;
2107 int i;
2108 char buf[64];
2109 char flags;
2110
2111 if (debug & 4) {
2112 printk("Inbytes %dd\n", count);
2113 hex_packet(cp, count);
2114 }
2115
2116 for (i = count, dp = cp, f = fp; i; i--, dp++) {
2117 flags = *f++;
2118 switch (flags) {
2119 case TTY_NORMAL:
2120 gsm->receive(gsm, *dp);
2121 break;
2122 case TTY_OVERRUN:
2123 case TTY_BREAK:
2124 case TTY_PARITY:
2125 case TTY_FRAME:
2126 gsm->error(gsm, *dp, flags);
2127 break;
2128 default:
2129 printk(KERN_ERR "%s: unknown flag %d\n",
2130 tty_name(tty, buf), flags);
2131 break;
2132 }
2133 }
2134 /* FASYNC if needed ? */
2135 /* If clogged call tty_throttle(tty); */
2136}
2137
2138/**
2139 * gsmld_chars_in_buffer - report available bytes
2140 * @tty: tty device
2141 *
2142 * Report the number of characters buffered to be delivered to user
2143 * at this instant in time.
2144 *
2145 * Locking: gsm lock
2146 */
2147
2148static ssize_t gsmld_chars_in_buffer(struct tty_struct *tty)
2149{
2150 return 0;
2151}
2152
2153/**
2154 * gsmld_flush_buffer - clean input queue
2155 * @tty: terminal device
2156 *
2157 * Flush the input buffer. Called when the line discipline is
2158 * being closed, when the tty layer wants the buffer flushed (eg
2159 * at hangup).
2160 */
2161
2162static void gsmld_flush_buffer(struct tty_struct *tty)
2163{
2164}
2165
2166/**
2167 * gsmld_close - close the ldisc for this tty
2168 * @tty: device
2169 *
2170 * Called from the terminal layer when this line discipline is
2171 * being shut down, either because of a close or becsuse of a
2172 * discipline change. The function will not be called while other
2173 * ldisc methods are in progress.
2174 */
2175
2176static void gsmld_close(struct tty_struct *tty)
2177{
2178 struct gsm_mux *gsm = tty->disc_data;
2179
2180 gsmld_detach_gsm(tty, gsm);
2181
2182 gsmld_flush_buffer(tty);
2183 /* Do other clean up here */
2184 gsm_free_mux(gsm);
2185}
2186
2187/**
2188 * gsmld_open - open an ldisc
2189 * @tty: terminal to open
2190 *
2191 * Called when this line discipline is being attached to the
2192 * terminal device. Can sleep. Called serialized so that no
2193 * other events will occur in parallel. No further open will occur
2194 * until a close.
2195 */
2196
2197static int gsmld_open(struct tty_struct *tty)
2198{
2199 struct gsm_mux *gsm;
2200
2201 if (tty->ops->write == NULL)
2202 return -EINVAL;
2203
2204 /* Attach our ldisc data */
2205 gsm = gsm_alloc_mux();
2206 if (gsm == NULL)
2207 return -ENOMEM;
2208
2209 tty->disc_data = gsm;
2210 tty->receive_room = 65536;
2211
2212 /* Attach the initial passive connection */
2213 gsm->encoding = 1;
2214 return gsmld_attach_gsm(tty, gsm);
2215}
2216
2217/**
2218 * gsmld_write_wakeup - asynchronous I/O notifier
2219 * @tty: tty device
2220 *
2221 * Required for the ptys, serial driver etc. since processes
2222 * that attach themselves to the master and rely on ASYNC
2223 * IO must be woken up
2224 */
2225
2226static void gsmld_write_wakeup(struct tty_struct *tty)
2227{
2228 struct gsm_mux *gsm = tty->disc_data;
2229 unsigned long flags;
2230
2231 /* Queue poll */
2232 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
2233 gsm_data_kick(gsm);
2234 if (gsm->tx_bytes < TX_THRESH_LO) {
2235 spin_lock_irqsave(&gsm->tx_lock, flags);
2236 gsm_dlci_data_sweep(gsm);
2237 spin_unlock_irqrestore(&gsm->tx_lock, flags);
2238 }
2239}
2240
2241/**
2242 * gsmld_read - read function for tty
2243 * @tty: tty device
2244 * @file: file object
2245 * @buf: userspace buffer pointer
2246 * @nr: size of I/O
2247 *
2248 * Perform reads for the line discipline. We are guaranteed that the
2249 * line discipline will not be closed under us but we may get multiple
2250 * parallel readers and must handle this ourselves. We may also get
2251 * a hangup. Always called in user context, may sleep.
2252 *
2253 * This code must be sure never to sleep through a hangup.
2254 */
2255
2256static ssize_t gsmld_read(struct tty_struct *tty, struct file *file,
2257 unsigned char __user *buf, size_t nr)
2258{
2259 return -EOPNOTSUPP;
2260}
2261
2262/**
2263 * gsmld_write - write function for tty
2264 * @tty: tty device
2265 * @file: file object
2266 * @buf: userspace buffer pointer
2267 * @nr: size of I/O
2268 *
2269 * Called when the owner of the device wants to send a frame
2270 * itself (or some other control data). The data is transferred
2271 * as-is and must be properly framed and checksummed as appropriate
2272 * by userspace. Frames are either sent whole or not at all as this
2273 * avoids pain user side.
2274 */
2275
2276static ssize_t gsmld_write(struct tty_struct *tty, struct file *file,
2277 const unsigned char *buf, size_t nr)
2278{
2279 int space = tty_write_room(tty);
2280 if (space >= nr)
2281 return tty->ops->write(tty, buf, nr);
2282 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
2283 return -ENOBUFS;
2284}
2285
2286/**
2287 * gsmld_poll - poll method for N_GSM0710
2288 * @tty: terminal device
2289 * @file: file accessing it
2290 * @wait: poll table
2291 *
2292 * Called when the line discipline is asked to poll() for data or
2293 * for special events. This code is not serialized with respect to
2294 * other events save open/close.
2295 *
2296 * This code must be sure never to sleep through a hangup.
2297 * Called without the kernel lock held - fine
2298 */
2299
2300static unsigned int gsmld_poll(struct tty_struct *tty, struct file *file,
2301 poll_table *wait)
2302{
2303 unsigned int mask = 0;
2304 struct gsm_mux *gsm = tty->disc_data;
2305
2306 poll_wait(file, &tty->read_wait, wait);
2307 poll_wait(file, &tty->write_wait, wait);
2308 if (tty_hung_up_p(file))
2309 mask |= POLLHUP;
2310 if (!tty_is_writelocked(tty) && tty_write_room(tty) > 0)
2311 mask |= POLLOUT | POLLWRNORM;
2312 if (gsm->dead)
2313 mask |= POLLHUP;
2314 return mask;
2315}
2316
2317static int gsmld_config(struct tty_struct *tty, struct gsm_mux *gsm,
2318 struct gsm_config *c)
2319{
2320 int need_close = 0;
2321 int need_restart = 0;
2322
2323 /* Stuff we don't support yet - UI or I frame transport, windowing */
2324 if ((c->adaption !=1 && c->adaption != 2) || c->k)
2325 return -EOPNOTSUPP;
2326 /* Check the MRU/MTU range looks sane */
2327 if (c->mru > MAX_MRU || c->mtu > MAX_MTU || c->mru < 8 || c->mtu < 8)
2328 return -EINVAL;
2329 if (c->n2 < 3)
2330 return -EINVAL;
2331 if (c->encapsulation > 1) /* Basic, advanced, no I */
2332 return -EINVAL;
2333 if (c->initiator > 1)
2334 return -EINVAL;
2335 if (c->i == 0 || c->i > 2) /* UIH and UI only */
2336 return -EINVAL;
2337 /*
2338 * See what is needed for reconfiguration
2339 */
2340
2341 /* Timing fields */
2342 if (c->t1 != 0 && c->t1 != gsm->t1)
2343 need_restart = 1;
2344 if (c->t2 != 0 && c->t2 != gsm->t2)
2345 need_restart = 1;
2346 if (c->encapsulation != gsm->encoding)
2347 need_restart = 1;
2348 if (c->adaption != gsm->adaption)
2349 need_restart = 1;
2350 /* Requires care */
2351 if (c->initiator != gsm->initiator)
2352 need_close = 1;
2353 if (c->mru != gsm->mru)
2354 need_restart = 1;
2355 if (c->mtu != gsm->mtu)
2356 need_restart = 1;
2357
2358 /*
2359 * Close down what is needed, restart and initiate the new
2360 * configuration
2361 */
2362
2363 if (need_close || need_restart) {
2364 gsm_dlci_begin_close(gsm->dlci[0]);
2365 /* This will timeout if the link is down due to N2 expiring */
2366 wait_event_interruptible(gsm->event,
2367 gsm->dlci[0]->state == DLCI_CLOSED);
2368 if (signal_pending(current))
2369 return -EINTR;
2370 }
2371 if (need_restart)
2372 gsm_cleanup_mux(gsm);
2373
2374 gsm->initiator = c->initiator;
2375 gsm->mru = c->mru;
2376 gsm->encoding = c->encapsulation;
2377 gsm->adaption = c->adaption;
2378
2379 if (c->i == 1)
2380 gsm->ftype = UIH;
2381 else if (c->i == 2)
2382 gsm->ftype = UI;
2383
2384 if (c->t1)
2385 gsm->t1 = c->t1;
2386 if (c->t2)
2387 gsm->t2 = c->t2;
2388
2389 /* FIXME: We need to separate activation/deactivation from adding
2390 and removing from the mux array */
2391 if (need_restart)
2392 gsm_activate_mux(gsm);
2393 if (gsm->initiator && need_close)
2394 gsm_dlci_begin_open(gsm->dlci[0]);
2395 return 0;
2396}
2397
2398static int gsmld_ioctl(struct tty_struct *tty, struct file *file,
2399 unsigned int cmd, unsigned long arg)
2400{
2401 struct gsm_config c;
2402 struct gsm_mux *gsm = tty->disc_data;
2403
2404 switch (cmd) {
2405 case GSMIOC_GETCONF:
2406 memset(&c, 0, sizeof(c));
2407 c.adaption = gsm->adaption;
2408 c.encapsulation = gsm->encoding;
2409 c.initiator = gsm->initiator;
2410 c.t1 = gsm->t1;
2411 c.t2 = gsm->t2;
2412 c.t3 = 0; /* Not supported */
2413 c.n2 = gsm->n2;
2414 if (gsm->ftype == UIH)
2415 c.i = 1;
2416 else
2417 c.i = 2;
2418 printk("Ftype %d i %d\n", gsm->ftype, c.i);
2419 c.mru = gsm->mru;
2420 c.mtu = gsm->mtu;
2421 c.k = 0;
2422 if (copy_to_user((void *)arg, &c, sizeof(c)))
2423 return -EFAULT;
2424 return 0;
2425 case GSMIOC_SETCONF:
2426 if (copy_from_user(&c, (void *)arg, sizeof(c)))
2427 return -EFAULT;
2428 return gsmld_config(tty, gsm, &c);
2429 default:
2430 return n_tty_ioctl_helper(tty, file, cmd, arg);
2431 }
2432}
2433
2434
2435/* Line discipline for real tty */
2436struct tty_ldisc_ops tty_ldisc_packet = {
2437 .owner = THIS_MODULE,
2438 .magic = TTY_LDISC_MAGIC,
2439 .name = "n_gsm",
2440 .open = gsmld_open,
2441 .close = gsmld_close,
2442 .flush_buffer = gsmld_flush_buffer,
2443 .chars_in_buffer = gsmld_chars_in_buffer,
2444 .read = gsmld_read,
2445 .write = gsmld_write,
2446 .ioctl = gsmld_ioctl,
2447 .poll = gsmld_poll,
2448 .receive_buf = gsmld_receive_buf,
2449 .write_wakeup = gsmld_write_wakeup
2450};
2451
2452/*
2453 * Virtual tty side
2454 */
2455
2456#define TX_SIZE 512
2457
2458static int gsmtty_modem_update(struct gsm_dlci *dlci, u8 brk)
2459{
2460 u8 modembits[5];
2461 struct gsm_control *ctrl;
2462 int len = 2;
2463
2464 if (brk)
2465 len++;
2466
2467 modembits[0] = len << 1 | EA; /* Data bytes */
2468 modembits[1] = dlci->addr << 2 | 3; /* DLCI, EA, 1 */
2469 modembits[2] = gsm_encode_modem(dlci) << 1 | EA;
2470 if (brk)
2471 modembits[3] = brk << 4 | 2 | EA; /* Valid, EA */
2472 ctrl = gsm_control_send(dlci->gsm, CMD_MSC, modembits, len + 1);
2473 if (ctrl == NULL)
2474 return -ENOMEM;
2475 return gsm_control_wait(dlci->gsm, ctrl);
2476}
2477
2478static int gsm_carrier_raised(struct tty_port *port)
2479{
2480 struct gsm_dlci *dlci = container_of(port, struct gsm_dlci, port);
2481 /* Not yet open so no carrier info */
2482 if (dlci->state != DLCI_OPEN)
2483 return 0;
2484 if (debug & 2)
2485 return 1;
2486 return dlci->modem_rx & TIOCM_CD;
2487}
2488
2489static void gsm_dtr_rts(struct tty_port *port, int onoff)
2490{
2491 struct gsm_dlci *dlci = container_of(port, struct gsm_dlci, port);
2492 unsigned int modem_tx = dlci->modem_tx;
2493 if (onoff)
2494 modem_tx |= TIOCM_DTR | TIOCM_RTS;
2495 else
2496 modem_tx &= ~(TIOCM_DTR | TIOCM_RTS);
2497 if (modem_tx != dlci->modem_tx) {
2498 dlci->modem_tx = modem_tx;
2499 gsmtty_modem_update(dlci, 0);
2500 }
2501}
2502
2503static const struct tty_port_operations gsm_port_ops = {
2504 .carrier_raised = gsm_carrier_raised,
2505 .dtr_rts = gsm_dtr_rts,
2506};
2507
2508
2509static int gsmtty_open(struct tty_struct *tty, struct file *filp)
2510{
2511 struct gsm_mux *gsm;
2512 struct gsm_dlci *dlci;
2513 struct tty_port *port;
2514 unsigned int line = tty->index;
2515 unsigned int mux = line >> 6;
2516
2517 line = line & 0x3F;
2518
2519 if (mux >= MAX_MUX)
2520 return -ENXIO;
2521 /* FIXME: we need to lock gsm_mux for lifetimes of ttys eventually */
2522 if (gsm_mux[mux] == NULL)
2523 return -EUNATCH;
2524 if (line == 0 || line > 61) /* 62/63 reserved */
2525 return -ECHRNG;
2526 gsm = gsm_mux[mux];
2527 if (gsm->dead)
2528 return -EL2HLT;
2529 dlci = gsm->dlci[line];
2530 if (dlci == NULL)
2531 dlci = gsm_dlci_alloc(gsm, line);
2532 if (dlci == NULL)
2533 return -ENOMEM;
2534 port = &dlci->port;
2535 port->count++;
2536 tty->driver_data = dlci;
2537 tty_port_tty_set(port, tty);
2538
2539 dlci->modem_rx = 0;
2540 /* We could in theory open and close before we wait - eg if we get
2541 a DM straight back. This is ok as that will have caused a hangup */
2542 set_bit(ASYNCB_INITIALIZED, &port->flags);
2543 /* Start sending off SABM messages */
2544 gsm_dlci_begin_open(dlci);
2545 /* And wait for virtual carrier */
2546 return tty_port_block_til_ready(port, tty, filp);
2547}
2548
2549static void gsmtty_close(struct tty_struct *tty, struct file *filp)
2550{
2551 struct gsm_dlci *dlci = tty->driver_data;
2552 if (dlci == NULL)
2553 return;
2554 if (tty_port_close_start(&dlci->port, tty, filp) == 0)
2555 return;
2556 gsm_dlci_begin_close(dlci);
2557 tty_port_close_end(&dlci->port, tty);
2558 tty_port_tty_set(&dlci->port, NULL);
2559}
2560
2561static void gsmtty_hangup(struct tty_struct *tty)
2562{
2563 struct gsm_dlci *dlci = tty->driver_data;
2564 tty_port_hangup(&dlci->port);
2565 gsm_dlci_begin_close(dlci);
2566}
2567
2568static int gsmtty_write(struct tty_struct *tty, const unsigned char *buf,
2569 int len)
2570{
2571 struct gsm_dlci *dlci = tty->driver_data;
2572 /* Stuff the bytes into the fifo queue */
2573 int sent = kfifo_in_locked(dlci->fifo, buf, len, &dlci->lock);
2574 /* Need to kick the channel */
2575 gsm_dlci_data_kick(dlci);
2576 return sent;
2577}
2578
2579static int gsmtty_write_room(struct tty_struct *tty)
2580{
2581 struct gsm_dlci *dlci = tty->driver_data;
2582 return TX_SIZE - kfifo_len(dlci->fifo);
2583}
2584
2585static int gsmtty_chars_in_buffer(struct tty_struct *tty)
2586{
2587 struct gsm_dlci *dlci = tty->driver_data;
2588 return kfifo_len(dlci->fifo);
2589}
2590
2591static void gsmtty_flush_buffer(struct tty_struct *tty)
2592{
2593 struct gsm_dlci *dlci = tty->driver_data;
2594 /* Caution needed: If we implement reliable transport classes
2595 then the data being transmitted can't simply be junked once
2596 it has first hit the stack. Until then we can just blow it
2597 away */
2598 kfifo_reset(dlci->fifo);
2599 /* Need to unhook this DLCI from the transmit queue logic */
2600}
2601
2602static void gsmtty_wait_until_sent(struct tty_struct *tty, int timeout)
2603{
2604 /* The FIFO handles the queue so the kernel will do the right
2605 thing waiting on chars_in_buffer before calling us. No work
2606 to do here */
2607}
2608
2609static int gsmtty_tiocmget(struct tty_struct *tty, struct file *filp)
2610{
2611 struct gsm_dlci *dlci = tty->driver_data;
2612 return dlci->modem_rx;
2613}
2614
2615static int gsmtty_tiocmset(struct tty_struct *tty, struct file *filp,
2616 unsigned int set, unsigned int clear)
2617{
2618 struct gsm_dlci *dlci = tty->driver_data;
2619 unsigned int modem_tx = dlci->modem_tx;
2620
2621 modem_tx &= clear;
2622 modem_tx |= set;
2623
2624 if (modem_tx != dlci->modem_tx) {
2625 dlci->modem_tx = modem_tx;
2626 return gsmtty_modem_update(dlci, 0);
2627 }
2628 return 0;
2629}
2630
2631
2632static int gsmtty_ioctl(struct tty_struct *tty, struct file *filp,
2633 unsigned int cmd, unsigned long arg)
2634{
2635 return -ENOIOCTLCMD;
2636}
2637
2638static void gsmtty_set_termios(struct tty_struct *tty, struct ktermios *old)
2639{
2640 /* For the moment its fixed. In actual fact the speed information
2641 for the virtual channel can be propogated in both directions by
2642 the RPN control message. This however rapidly gets nasty as we
2643 then have to remap modem signals each way according to whether
2644 our virtual cable is null modem etc .. */
2645 tty_termios_copy_hw(tty->termios, old);
2646}
2647
2648static void gsmtty_throttle(struct tty_struct *tty)
2649{
2650 struct gsm_dlci *dlci = tty->driver_data;
2651 if (tty->termios->c_cflag & CRTSCTS)
2652 dlci->modem_tx &= ~TIOCM_DTR;
2653 dlci->throttled = 1;
2654 /* Send an MSC with DTR cleared */
2655 gsmtty_modem_update(dlci, 0);
2656}
2657
2658static void gsmtty_unthrottle(struct tty_struct *tty)
2659{
2660 struct gsm_dlci *dlci = tty->driver_data;
2661 if (tty->termios->c_cflag & CRTSCTS)
2662 dlci->modem_tx |= TIOCM_DTR;
2663 dlci->throttled = 0;
2664 /* Send an MSC with DTR set */
2665 gsmtty_modem_update(dlci, 0);
2666}
2667
2668static int gsmtty_break_ctl(struct tty_struct *tty, int state)
2669{
2670 struct gsm_dlci *dlci = tty->driver_data;
2671 int encode = 0; /* Off */
2672
2673 if (state == -1) /* "On indefinitely" - we can't encode this
2674 properly */
2675 encode = 0x0F;
2676 else if (state > 0) {
2677 encode = state / 200; /* mS to encoding */
2678 if (encode > 0x0F)
2679 encode = 0x0F; /* Best effort */
2680 }
2681 return gsmtty_modem_update(dlci, encode);
2682}
2683
2684static struct tty_driver *gsm_tty_driver;
2685
2686/* Virtual ttys for the demux */
2687static const struct tty_operations gsmtty_ops = {
2688 .open = gsmtty_open,
2689 .close = gsmtty_close,
2690 .write = gsmtty_write,
2691 .write_room = gsmtty_write_room,
2692 .chars_in_buffer = gsmtty_chars_in_buffer,
2693 .flush_buffer = gsmtty_flush_buffer,
2694 .ioctl = gsmtty_ioctl,
2695 .throttle = gsmtty_throttle,
2696 .unthrottle = gsmtty_unthrottle,
2697 .set_termios = gsmtty_set_termios,
2698 .hangup = gsmtty_hangup,
2699 .wait_until_sent = gsmtty_wait_until_sent,
2700 .tiocmget = gsmtty_tiocmget,
2701 .tiocmset = gsmtty_tiocmset,
2702 .break_ctl = gsmtty_break_ctl,
2703};
2704
2705
2706
2707static int __init gsm_init(void)
2708{
2709 /* Fill in our line protocol discipline, and register it */
2710 int status = tty_register_ldisc(N_GSM0710, &tty_ldisc_packet);
2711 if (status != 0) {
2712 printk(KERN_ERR "n_gsm: can't register line discipline (err = %d)\n", status);
2713 return status;
2714 }
2715
2716 gsm_tty_driver = alloc_tty_driver(256);
2717 if (!gsm_tty_driver) {
2718 tty_unregister_ldisc(N_GSM0710);
2719 printk(KERN_ERR "gsm_init: tty allocation failed.\n");
2720 return -EINVAL;
2721 }
2722 gsm_tty_driver->owner = THIS_MODULE;
2723 gsm_tty_driver->driver_name = "gsmtty";
2724 gsm_tty_driver->name = "gsmtty";
2725 gsm_tty_driver->major = 0; /* Dynamic */
2726 gsm_tty_driver->minor_start = 0;
2727 gsm_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
2728 gsm_tty_driver->subtype = SERIAL_TYPE_NORMAL;
2729 gsm_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV
2730 | TTY_DRIVER_HARDWARE_BREAK;
2731 gsm_tty_driver->init_termios = tty_std_termios;
2732 /* Fixme */
2733 gsm_tty_driver->init_termios.c_lflag &= ~ECHO;
2734 tty_set_operations(gsm_tty_driver, &gsmtty_ops);
2735
2736 spin_lock_init(&gsm_mux_lock);
2737
2738 if (tty_register_driver(gsm_tty_driver)) {
2739 put_tty_driver(gsm_tty_driver);
2740 tty_unregister_ldisc(N_GSM0710);
2741 printk(KERN_ERR "gsm_init: tty registration failed.\n");
2742 return -EBUSY;
2743 }
2744 printk(KERN_INFO "gsm_init: loaded as %d,%d.\n", gsm_tty_driver->major, gsm_tty_driver->minor_start);
2745 return 0;
2746}
2747
2748static void __exit gsm_exit(void)
2749{
2750 int status = tty_unregister_ldisc(N_GSM0710);
2751 if (status != 0)
2752 printk(KERN_ERR "n_gsm: can't unregister line discipline (err = %d)\n", status);
2753 tty_unregister_driver(gsm_tty_driver);
2754 put_tty_driver(gsm_tty_driver);
2755 printk(KERN_INFO "gsm_init: unloaded.\n");
2756}
2757
2758module_init(gsm_init);
2759module_exit(gsm_exit);
2760
2761
2762MODULE_LICENSE("GPL");
2763MODULE_ALIAS_LDISC(N_GSM0710);
diff --git a/drivers/char/n_hdlc.c b/drivers/char/n_hdlc.c
deleted file mode 100644
index 47d32281032c..000000000000
--- a/drivers/char/n_hdlc.c
+++ /dev/null
@@ -1,1007 +0,0 @@
1/* generic HDLC line discipline for Linux
2 *
3 * Written by Paul Fulghum paulkf@microgate.com
4 * for Microgate Corporation
5 *
6 * Microgate and SyncLink are registered trademarks of Microgate Corporation
7 *
8 * Adapted from ppp.c, written by Michael Callahan <callahan@maths.ox.ac.uk>,
9 * Al Longyear <longyear@netcom.com>,
10 * Paul Mackerras <Paul.Mackerras@cs.anu.edu.au>
11 *
12 * Original release 01/11/99
13 *
14 * This code is released under the GNU General Public License (GPL)
15 *
16 * This module implements the tty line discipline N_HDLC for use with
17 * tty device drivers that support bit-synchronous HDLC communications.
18 *
19 * All HDLC data is frame oriented which means:
20 *
21 * 1. tty write calls represent one complete transmit frame of data
22 * The device driver should accept the complete frame or none of
23 * the frame (busy) in the write method. Each write call should have
24 * a byte count in the range of 2-65535 bytes (2 is min HDLC frame
25 * with 1 addr byte and 1 ctrl byte). The max byte count of 65535
26 * should include any crc bytes required. For example, when using
27 * CCITT CRC32, 4 crc bytes are required, so the maximum size frame
28 * the application may transmit is limited to 65531 bytes. For CCITT
29 * CRC16, the maximum application frame size would be 65533.
30 *
31 *
32 * 2. receive callbacks from the device driver represents
33 * one received frame. The device driver should bypass
34 * the tty flip buffer and call the line discipline receive
35 * callback directly to avoid fragmenting or concatenating
36 * multiple frames into a single receive callback.
37 *
38 * The HDLC line discipline queues the receive frames in separate
39 * buffers so complete receive frames can be returned by the
40 * tty read calls.
41 *
42 * 3. tty read calls returns an entire frame of data or nothing.
43 *
44 * 4. all send and receive data is considered raw. No processing
45 * or translation is performed by the line discipline, regardless
46 * of the tty flags
47 *
48 * 5. When line discipline is queried for the amount of receive
49 * data available (FIOC), 0 is returned if no data available,
50 * otherwise the count of the next available frame is returned.
51 * (instead of the sum of all received frame counts).
52 *
53 * These conventions allow the standard tty programming interface
54 * to be used for synchronous HDLC applications when used with
55 * this line discipline (or another line discipline that is frame
56 * oriented such as N_PPP).
57 *
58 * The SyncLink driver (synclink.c) implements both asynchronous
59 * (using standard line discipline N_TTY) and synchronous HDLC
60 * (using N_HDLC) communications, with the latter using the above
61 * conventions.
62 *
63 * This implementation is very basic and does not maintain
64 * any statistics. The main point is to enforce the raw data
65 * and frame orientation of HDLC communications.
66 *
67 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
68 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
69 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
70 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
71 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
72 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
73 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
74 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
75 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
76 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
77 * OF THE POSSIBILITY OF SUCH DAMAGE.
78 */
79
80#define HDLC_MAGIC 0x239e
81
82#include <linux/module.h>
83#include <linux/init.h>
84#include <linux/kernel.h>
85#include <linux/sched.h>
86#include <linux/types.h>
87#include <linux/fcntl.h>
88#include <linux/interrupt.h>
89#include <linux/ptrace.h>
90
91#undef VERSION
92#define VERSION(major,minor,patch) (((((major)<<8)+(minor))<<8)+(patch))
93
94#include <linux/poll.h>
95#include <linux/in.h>
96#include <linux/ioctl.h>
97#include <linux/slab.h>
98#include <linux/tty.h>
99#include <linux/errno.h>
100#include <linux/smp_lock.h>
101#include <linux/string.h> /* used in new tty drivers */
102#include <linux/signal.h> /* used in new tty drivers */
103#include <linux/if.h>
104#include <linux/bitops.h>
105
106#include <asm/system.h>
107#include <asm/termios.h>
108#include <asm/uaccess.h>
109
110/*
111 * Buffers for individual HDLC frames
112 */
113#define MAX_HDLC_FRAME_SIZE 65535
114#define DEFAULT_RX_BUF_COUNT 10
115#define MAX_RX_BUF_COUNT 60
116#define DEFAULT_TX_BUF_COUNT 3
117
118struct n_hdlc_buf {
119 struct n_hdlc_buf *link;
120 int count;
121 char buf[1];
122};
123
124#define N_HDLC_BUF_SIZE (sizeof(struct n_hdlc_buf) + maxframe)
125
126struct n_hdlc_buf_list {
127 struct n_hdlc_buf *head;
128 struct n_hdlc_buf *tail;
129 int count;
130 spinlock_t spinlock;
131};
132
133/**
134 * struct n_hdlc - per device instance data structure
135 * @magic - magic value for structure
136 * @flags - miscellaneous control flags
137 * @tty - ptr to TTY structure
138 * @backup_tty - TTY to use if tty gets closed
139 * @tbusy - reentrancy flag for tx wakeup code
140 * @woke_up - FIXME: describe this field
141 * @tbuf - currently transmitting tx buffer
142 * @tx_buf_list - list of pending transmit frame buffers
143 * @rx_buf_list - list of received frame buffers
144 * @tx_free_buf_list - list unused transmit frame buffers
145 * @rx_free_buf_list - list unused received frame buffers
146 */
147struct n_hdlc {
148 int magic;
149 __u32 flags;
150 struct tty_struct *tty;
151 struct tty_struct *backup_tty;
152 int tbusy;
153 int woke_up;
154 struct n_hdlc_buf *tbuf;
155 struct n_hdlc_buf_list tx_buf_list;
156 struct n_hdlc_buf_list rx_buf_list;
157 struct n_hdlc_buf_list tx_free_buf_list;
158 struct n_hdlc_buf_list rx_free_buf_list;
159};
160
161/*
162 * HDLC buffer list manipulation functions
163 */
164static void n_hdlc_buf_list_init(struct n_hdlc_buf_list *list);
165static void n_hdlc_buf_put(struct n_hdlc_buf_list *list,
166 struct n_hdlc_buf *buf);
167static struct n_hdlc_buf *n_hdlc_buf_get(struct n_hdlc_buf_list *list);
168
169/* Local functions */
170
171static struct n_hdlc *n_hdlc_alloc (void);
172
173/* debug level can be set by insmod for debugging purposes */
174#define DEBUG_LEVEL_INFO 1
175static int debuglevel;
176
177/* max frame size for memory allocations */
178static int maxframe = 4096;
179
180/* TTY callbacks */
181
182static ssize_t n_hdlc_tty_read(struct tty_struct *tty, struct file *file,
183 __u8 __user *buf, size_t nr);
184static ssize_t n_hdlc_tty_write(struct tty_struct *tty, struct file *file,
185 const unsigned char *buf, size_t nr);
186static int n_hdlc_tty_ioctl(struct tty_struct *tty, struct file *file,
187 unsigned int cmd, unsigned long arg);
188static unsigned int n_hdlc_tty_poll(struct tty_struct *tty, struct file *filp,
189 poll_table *wait);
190static int n_hdlc_tty_open(struct tty_struct *tty);
191static void n_hdlc_tty_close(struct tty_struct *tty);
192static void n_hdlc_tty_receive(struct tty_struct *tty, const __u8 *cp,
193 char *fp, int count);
194static void n_hdlc_tty_wakeup(struct tty_struct *tty);
195
196#define bset(p,b) ((p)[(b) >> 5] |= (1 << ((b) & 0x1f)))
197
198#define tty2n_hdlc(tty) ((struct n_hdlc *) ((tty)->disc_data))
199#define n_hdlc2tty(n_hdlc) ((n_hdlc)->tty)
200
201static void flush_rx_queue(struct tty_struct *tty)
202{
203 struct n_hdlc *n_hdlc = tty2n_hdlc(tty);
204 struct n_hdlc_buf *buf;
205
206 while ((buf = n_hdlc_buf_get(&n_hdlc->rx_buf_list)))
207 n_hdlc_buf_put(&n_hdlc->rx_free_buf_list, buf);
208}
209
210static void flush_tx_queue(struct tty_struct *tty)
211{
212 struct n_hdlc *n_hdlc = tty2n_hdlc(tty);
213 struct n_hdlc_buf *buf;
214 unsigned long flags;
215
216 while ((buf = n_hdlc_buf_get(&n_hdlc->tx_buf_list)))
217 n_hdlc_buf_put(&n_hdlc->tx_free_buf_list, buf);
218 spin_lock_irqsave(&n_hdlc->tx_buf_list.spinlock, flags);
219 if (n_hdlc->tbuf) {
220 n_hdlc_buf_put(&n_hdlc->tx_free_buf_list, n_hdlc->tbuf);
221 n_hdlc->tbuf = NULL;
222 }
223 spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock, flags);
224}
225
226static struct tty_ldisc_ops n_hdlc_ldisc = {
227 .owner = THIS_MODULE,
228 .magic = TTY_LDISC_MAGIC,
229 .name = "hdlc",
230 .open = n_hdlc_tty_open,
231 .close = n_hdlc_tty_close,
232 .read = n_hdlc_tty_read,
233 .write = n_hdlc_tty_write,
234 .ioctl = n_hdlc_tty_ioctl,
235 .poll = n_hdlc_tty_poll,
236 .receive_buf = n_hdlc_tty_receive,
237 .write_wakeup = n_hdlc_tty_wakeup,
238 .flush_buffer = flush_rx_queue,
239};
240
241/**
242 * n_hdlc_release - release an n_hdlc per device line discipline info structure
243 * @n_hdlc - per device line discipline info structure
244 */
245static void n_hdlc_release(struct n_hdlc *n_hdlc)
246{
247 struct tty_struct *tty = n_hdlc2tty (n_hdlc);
248 struct n_hdlc_buf *buf;
249
250 if (debuglevel >= DEBUG_LEVEL_INFO)
251 printk("%s(%d)n_hdlc_release() called\n",__FILE__,__LINE__);
252
253 /* Ensure that the n_hdlcd process is not hanging on select()/poll() */
254 wake_up_interruptible (&tty->read_wait);
255 wake_up_interruptible (&tty->write_wait);
256
257 if (tty->disc_data == n_hdlc)
258 tty->disc_data = NULL; /* Break the tty->n_hdlc link */
259
260 /* Release transmit and receive buffers */
261 for(;;) {
262 buf = n_hdlc_buf_get(&n_hdlc->rx_free_buf_list);
263 if (buf) {
264 kfree(buf);
265 } else
266 break;
267 }
268 for(;;) {
269 buf = n_hdlc_buf_get(&n_hdlc->tx_free_buf_list);
270 if (buf) {
271 kfree(buf);
272 } else
273 break;
274 }
275 for(;;) {
276 buf = n_hdlc_buf_get(&n_hdlc->rx_buf_list);
277 if (buf) {
278 kfree(buf);
279 } else
280 break;
281 }
282 for(;;) {
283 buf = n_hdlc_buf_get(&n_hdlc->tx_buf_list);
284 if (buf) {
285 kfree(buf);
286 } else
287 break;
288 }
289 kfree(n_hdlc->tbuf);
290 kfree(n_hdlc);
291
292} /* end of n_hdlc_release() */
293
294/**
295 * n_hdlc_tty_close - line discipline close
296 * @tty - pointer to tty info structure
297 *
298 * Called when the line discipline is changed to something
299 * else, the tty is closed, or the tty detects a hangup.
300 */
301static void n_hdlc_tty_close(struct tty_struct *tty)
302{
303 struct n_hdlc *n_hdlc = tty2n_hdlc (tty);
304
305 if (debuglevel >= DEBUG_LEVEL_INFO)
306 printk("%s(%d)n_hdlc_tty_close() called\n",__FILE__,__LINE__);
307
308 if (n_hdlc != NULL) {
309 if (n_hdlc->magic != HDLC_MAGIC) {
310 printk (KERN_WARNING"n_hdlc: trying to close unopened tty!\n");
311 return;
312 }
313#if defined(TTY_NO_WRITE_SPLIT)
314 clear_bit(TTY_NO_WRITE_SPLIT,&tty->flags);
315#endif
316 tty->disc_data = NULL;
317 if (tty == n_hdlc->backup_tty)
318 n_hdlc->backup_tty = NULL;
319 if (tty != n_hdlc->tty)
320 return;
321 if (n_hdlc->backup_tty) {
322 n_hdlc->tty = n_hdlc->backup_tty;
323 } else {
324 n_hdlc_release (n_hdlc);
325 }
326 }
327
328 if (debuglevel >= DEBUG_LEVEL_INFO)
329 printk("%s(%d)n_hdlc_tty_close() success\n",__FILE__,__LINE__);
330
331} /* end of n_hdlc_tty_close() */
332
333/**
334 * n_hdlc_tty_open - called when line discipline changed to n_hdlc
335 * @tty - pointer to tty info structure
336 *
337 * Returns 0 if success, otherwise error code
338 */
339static int n_hdlc_tty_open (struct tty_struct *tty)
340{
341 struct n_hdlc *n_hdlc = tty2n_hdlc (tty);
342
343 if (debuglevel >= DEBUG_LEVEL_INFO)
344 printk("%s(%d)n_hdlc_tty_open() called (device=%s)\n",
345 __FILE__,__LINE__,
346 tty->name);
347
348 /* There should not be an existing table for this slot. */
349 if (n_hdlc) {
350 printk (KERN_ERR"n_hdlc_tty_open:tty already associated!\n" );
351 return -EEXIST;
352 }
353
354 n_hdlc = n_hdlc_alloc();
355 if (!n_hdlc) {
356 printk (KERN_ERR "n_hdlc_alloc failed\n");
357 return -ENFILE;
358 }
359
360 tty->disc_data = n_hdlc;
361 n_hdlc->tty = tty;
362 tty->receive_room = 65536;
363
364#if defined(TTY_NO_WRITE_SPLIT)
365 /* change tty_io write() to not split large writes into 8K chunks */
366 set_bit(TTY_NO_WRITE_SPLIT,&tty->flags);
367#endif
368
369 /* flush receive data from driver */
370 tty_driver_flush_buffer(tty);
371
372 if (debuglevel >= DEBUG_LEVEL_INFO)
373 printk("%s(%d)n_hdlc_tty_open() success\n",__FILE__,__LINE__);
374
375 return 0;
376
377} /* end of n_tty_hdlc_open() */
378
379/**
380 * n_hdlc_send_frames - send frames on pending send buffer list
381 * @n_hdlc - pointer to ldisc instance data
382 * @tty - pointer to tty instance data
383 *
384 * Send frames on pending send buffer list until the driver does not accept a
385 * frame (busy) this function is called after adding a frame to the send buffer
386 * list and by the tty wakeup callback.
387 */
388static void n_hdlc_send_frames(struct n_hdlc *n_hdlc, struct tty_struct *tty)
389{
390 register int actual;
391 unsigned long flags;
392 struct n_hdlc_buf *tbuf;
393
394 if (debuglevel >= DEBUG_LEVEL_INFO)
395 printk("%s(%d)n_hdlc_send_frames() called\n",__FILE__,__LINE__);
396 check_again:
397
398 spin_lock_irqsave(&n_hdlc->tx_buf_list.spinlock, flags);
399 if (n_hdlc->tbusy) {
400 n_hdlc->woke_up = 1;
401 spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock, flags);
402 return;
403 }
404 n_hdlc->tbusy = 1;
405 n_hdlc->woke_up = 0;
406 spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock, flags);
407
408 /* get current transmit buffer or get new transmit */
409 /* buffer from list of pending transmit buffers */
410
411 tbuf = n_hdlc->tbuf;
412 if (!tbuf)
413 tbuf = n_hdlc_buf_get(&n_hdlc->tx_buf_list);
414
415 while (tbuf) {
416 if (debuglevel >= DEBUG_LEVEL_INFO)
417 printk("%s(%d)sending frame %p, count=%d\n",
418 __FILE__,__LINE__,tbuf,tbuf->count);
419
420 /* Send the next block of data to device */
421 tty->flags |= (1 << TTY_DO_WRITE_WAKEUP);
422 actual = tty->ops->write(tty, tbuf->buf, tbuf->count);
423
424 /* rollback was possible and has been done */
425 if (actual == -ERESTARTSYS) {
426 n_hdlc->tbuf = tbuf;
427 break;
428 }
429 /* if transmit error, throw frame away by */
430 /* pretending it was accepted by driver */
431 if (actual < 0)
432 actual = tbuf->count;
433
434 if (actual == tbuf->count) {
435 if (debuglevel >= DEBUG_LEVEL_INFO)
436 printk("%s(%d)frame %p completed\n",
437 __FILE__,__LINE__,tbuf);
438
439 /* free current transmit buffer */
440 n_hdlc_buf_put(&n_hdlc->tx_free_buf_list, tbuf);
441
442 /* this tx buffer is done */
443 n_hdlc->tbuf = NULL;
444
445 /* wait up sleeping writers */
446 wake_up_interruptible(&tty->write_wait);
447
448 /* get next pending transmit buffer */
449 tbuf = n_hdlc_buf_get(&n_hdlc->tx_buf_list);
450 } else {
451 if (debuglevel >= DEBUG_LEVEL_INFO)
452 printk("%s(%d)frame %p pending\n",
453 __FILE__,__LINE__,tbuf);
454
455 /* buffer not accepted by driver */
456 /* set this buffer as pending buffer */
457 n_hdlc->tbuf = tbuf;
458 break;
459 }
460 }
461
462 if (!tbuf)
463 tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
464
465 /* Clear the re-entry flag */
466 spin_lock_irqsave(&n_hdlc->tx_buf_list.spinlock, flags);
467 n_hdlc->tbusy = 0;
468 spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock, flags);
469
470 if (n_hdlc->woke_up)
471 goto check_again;
472
473 if (debuglevel >= DEBUG_LEVEL_INFO)
474 printk("%s(%d)n_hdlc_send_frames() exit\n",__FILE__,__LINE__);
475
476} /* end of n_hdlc_send_frames() */
477
478/**
479 * n_hdlc_tty_wakeup - Callback for transmit wakeup
480 * @tty - pointer to associated tty instance data
481 *
482 * Called when low level device driver can accept more send data.
483 */
484static void n_hdlc_tty_wakeup(struct tty_struct *tty)
485{
486 struct n_hdlc *n_hdlc = tty2n_hdlc(tty);
487
488 if (debuglevel >= DEBUG_LEVEL_INFO)
489 printk("%s(%d)n_hdlc_tty_wakeup() called\n",__FILE__,__LINE__);
490
491 if (!n_hdlc)
492 return;
493
494 if (tty != n_hdlc->tty) {
495 tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
496 return;
497 }
498
499 n_hdlc_send_frames (n_hdlc, tty);
500
501} /* end of n_hdlc_tty_wakeup() */
502
503/**
504 * n_hdlc_tty_receive - Called by tty driver when receive data is available
505 * @tty - pointer to tty instance data
506 * @data - pointer to received data
507 * @flags - pointer to flags for data
508 * @count - count of received data in bytes
509 *
510 * Called by tty low level driver when receive data is available. Data is
511 * interpreted as one HDLC frame.
512 */
513static void n_hdlc_tty_receive(struct tty_struct *tty, const __u8 *data,
514 char *flags, int count)
515{
516 register struct n_hdlc *n_hdlc = tty2n_hdlc (tty);
517 register struct n_hdlc_buf *buf;
518
519 if (debuglevel >= DEBUG_LEVEL_INFO)
520 printk("%s(%d)n_hdlc_tty_receive() called count=%d\n",
521 __FILE__,__LINE__, count);
522
523 /* This can happen if stuff comes in on the backup tty */
524 if (!n_hdlc || tty != n_hdlc->tty)
525 return;
526
527 /* verify line is using HDLC discipline */
528 if (n_hdlc->magic != HDLC_MAGIC) {
529 printk("%s(%d) line not using HDLC discipline\n",
530 __FILE__,__LINE__);
531 return;
532 }
533
534 if ( count>maxframe ) {
535 if (debuglevel >= DEBUG_LEVEL_INFO)
536 printk("%s(%d) rx count>maxframesize, data discarded\n",
537 __FILE__,__LINE__);
538 return;
539 }
540
541 /* get a free HDLC buffer */
542 buf = n_hdlc_buf_get(&n_hdlc->rx_free_buf_list);
543 if (!buf) {
544 /* no buffers in free list, attempt to allocate another rx buffer */
545 /* unless the maximum count has been reached */
546 if (n_hdlc->rx_buf_list.count < MAX_RX_BUF_COUNT)
547 buf = kmalloc(N_HDLC_BUF_SIZE, GFP_ATOMIC);
548 }
549
550 if (!buf) {
551 if (debuglevel >= DEBUG_LEVEL_INFO)
552 printk("%s(%d) no more rx buffers, data discarded\n",
553 __FILE__,__LINE__);
554 return;
555 }
556
557 /* copy received data to HDLC buffer */
558 memcpy(buf->buf,data,count);
559 buf->count=count;
560
561 /* add HDLC buffer to list of received frames */
562 n_hdlc_buf_put(&n_hdlc->rx_buf_list, buf);
563
564 /* wake up any blocked reads and perform async signalling */
565 wake_up_interruptible (&tty->read_wait);
566 if (n_hdlc->tty->fasync != NULL)
567 kill_fasync (&n_hdlc->tty->fasync, SIGIO, POLL_IN);
568
569} /* end of n_hdlc_tty_receive() */
570
571/**
572 * n_hdlc_tty_read - Called to retrieve one frame of data (if available)
573 * @tty - pointer to tty instance data
574 * @file - pointer to open file object
575 * @buf - pointer to returned data buffer
576 * @nr - size of returned data buffer
577 *
578 * Returns the number of bytes returned or error code.
579 */
580static ssize_t n_hdlc_tty_read(struct tty_struct *tty, struct file *file,
581 __u8 __user *buf, size_t nr)
582{
583 struct n_hdlc *n_hdlc = tty2n_hdlc(tty);
584 int ret;
585 struct n_hdlc_buf *rbuf;
586
587 if (debuglevel >= DEBUG_LEVEL_INFO)
588 printk("%s(%d)n_hdlc_tty_read() called\n",__FILE__,__LINE__);
589
590 /* Validate the pointers */
591 if (!n_hdlc)
592 return -EIO;
593
594 /* verify user access to buffer */
595 if (!access_ok(VERIFY_WRITE, buf, nr)) {
596 printk(KERN_WARNING "%s(%d) n_hdlc_tty_read() can't verify user "
597 "buffer\n", __FILE__, __LINE__);
598 return -EFAULT;
599 }
600
601 tty_lock();
602
603 for (;;) {
604 if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) {
605 tty_unlock();
606 return -EIO;
607 }
608
609 n_hdlc = tty2n_hdlc (tty);
610 if (!n_hdlc || n_hdlc->magic != HDLC_MAGIC ||
611 tty != n_hdlc->tty) {
612 tty_unlock();
613 return 0;
614 }
615
616 rbuf = n_hdlc_buf_get(&n_hdlc->rx_buf_list);
617 if (rbuf)
618 break;
619
620 /* no data */
621 if (file->f_flags & O_NONBLOCK) {
622 tty_unlock();
623 return -EAGAIN;
624 }
625
626 interruptible_sleep_on (&tty->read_wait);
627 if (signal_pending(current)) {
628 tty_unlock();
629 return -EINTR;
630 }
631 }
632
633 if (rbuf->count > nr)
634 /* frame too large for caller's buffer (discard frame) */
635 ret = -EOVERFLOW;
636 else {
637 /* Copy the data to the caller's buffer */
638 if (copy_to_user(buf, rbuf->buf, rbuf->count))
639 ret = -EFAULT;
640 else
641 ret = rbuf->count;
642 }
643
644 /* return HDLC buffer to free list unless the free list */
645 /* count has exceeded the default value, in which case the */
646 /* buffer is freed back to the OS to conserve memory */
647 if (n_hdlc->rx_free_buf_list.count > DEFAULT_RX_BUF_COUNT)
648 kfree(rbuf);
649 else
650 n_hdlc_buf_put(&n_hdlc->rx_free_buf_list,rbuf);
651 tty_unlock();
652 return ret;
653
654} /* end of n_hdlc_tty_read() */
655
656/**
657 * n_hdlc_tty_write - write a single frame of data to device
658 * @tty - pointer to associated tty device instance data
659 * @file - pointer to file object data
660 * @data - pointer to transmit data (one frame)
661 * @count - size of transmit frame in bytes
662 *
663 * Returns the number of bytes written (or error code).
664 */
665static ssize_t n_hdlc_tty_write(struct tty_struct *tty, struct file *file,
666 const unsigned char *data, size_t count)
667{
668 struct n_hdlc *n_hdlc = tty2n_hdlc (tty);
669 int error = 0;
670 DECLARE_WAITQUEUE(wait, current);
671 struct n_hdlc_buf *tbuf;
672
673 if (debuglevel >= DEBUG_LEVEL_INFO)
674 printk("%s(%d)n_hdlc_tty_write() called count=%Zd\n",
675 __FILE__,__LINE__,count);
676
677 /* Verify pointers */
678 if (!n_hdlc)
679 return -EIO;
680
681 if (n_hdlc->magic != HDLC_MAGIC)
682 return -EIO;
683
684 /* verify frame size */
685 if (count > maxframe ) {
686 if (debuglevel & DEBUG_LEVEL_INFO)
687 printk (KERN_WARNING
688 "n_hdlc_tty_write: truncating user packet "
689 "from %lu to %d\n", (unsigned long) count,
690 maxframe );
691 count = maxframe;
692 }
693
694 tty_lock();
695
696 add_wait_queue(&tty->write_wait, &wait);
697 set_current_state(TASK_INTERRUPTIBLE);
698
699 /* Allocate transmit buffer */
700 /* sleep until transmit buffer available */
701 while (!(tbuf = n_hdlc_buf_get(&n_hdlc->tx_free_buf_list))) {
702 if (file->f_flags & O_NONBLOCK) {
703 error = -EAGAIN;
704 break;
705 }
706 schedule();
707
708 n_hdlc = tty2n_hdlc (tty);
709 if (!n_hdlc || n_hdlc->magic != HDLC_MAGIC ||
710 tty != n_hdlc->tty) {
711 printk("n_hdlc_tty_write: %p invalid after wait!\n", n_hdlc);
712 error = -EIO;
713 break;
714 }
715
716 if (signal_pending(current)) {
717 error = -EINTR;
718 break;
719 }
720 }
721
722 set_current_state(TASK_RUNNING);
723 remove_wait_queue(&tty->write_wait, &wait);
724
725 if (!error) {
726 /* Retrieve the user's buffer */
727 memcpy(tbuf->buf, data, count);
728
729 /* Send the data */
730 tbuf->count = error = count;
731 n_hdlc_buf_put(&n_hdlc->tx_buf_list,tbuf);
732 n_hdlc_send_frames(n_hdlc,tty);
733 }
734 tty_unlock();
735 return error;
736
737} /* end of n_hdlc_tty_write() */
738
739/**
740 * n_hdlc_tty_ioctl - process IOCTL system call for the tty device.
741 * @tty - pointer to tty instance data
742 * @file - pointer to open file object for device
743 * @cmd - IOCTL command code
744 * @arg - argument for IOCTL call (cmd dependent)
745 *
746 * Returns command dependent result.
747 */
748static int n_hdlc_tty_ioctl(struct tty_struct *tty, struct file *file,
749 unsigned int cmd, unsigned long arg)
750{
751 struct n_hdlc *n_hdlc = tty2n_hdlc (tty);
752 int error = 0;
753 int count;
754 unsigned long flags;
755
756 if (debuglevel >= DEBUG_LEVEL_INFO)
757 printk("%s(%d)n_hdlc_tty_ioctl() called %d\n",
758 __FILE__,__LINE__,cmd);
759
760 /* Verify the status of the device */
761 if (!n_hdlc || n_hdlc->magic != HDLC_MAGIC)
762 return -EBADF;
763
764 switch (cmd) {
765 case FIONREAD:
766 /* report count of read data available */
767 /* in next available frame (if any) */
768 spin_lock_irqsave(&n_hdlc->rx_buf_list.spinlock,flags);
769 if (n_hdlc->rx_buf_list.head)
770 count = n_hdlc->rx_buf_list.head->count;
771 else
772 count = 0;
773 spin_unlock_irqrestore(&n_hdlc->rx_buf_list.spinlock,flags);
774 error = put_user(count, (int __user *)arg);
775 break;
776
777 case TIOCOUTQ:
778 /* get the pending tx byte count in the driver */
779 count = tty_chars_in_buffer(tty);
780 /* add size of next output frame in queue */
781 spin_lock_irqsave(&n_hdlc->tx_buf_list.spinlock,flags);
782 if (n_hdlc->tx_buf_list.head)
783 count += n_hdlc->tx_buf_list.head->count;
784 spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock,flags);
785 error = put_user(count, (int __user *)arg);
786 break;
787
788 case TCFLSH:
789 switch (arg) {
790 case TCIOFLUSH:
791 case TCOFLUSH:
792 flush_tx_queue(tty);
793 }
794 /* fall through to default */
795
796 default:
797 error = n_tty_ioctl_helper(tty, file, cmd, arg);
798 break;
799 }
800 return error;
801
802} /* end of n_hdlc_tty_ioctl() */
803
804/**
805 * n_hdlc_tty_poll - TTY callback for poll system call
806 * @tty - pointer to tty instance data
807 * @filp - pointer to open file object for device
808 * @poll_table - wait queue for operations
809 *
810 * Determine which operations (read/write) will not block and return info
811 * to caller.
812 * Returns a bit mask containing info on which ops will not block.
813 */
814static unsigned int n_hdlc_tty_poll(struct tty_struct *tty, struct file *filp,
815 poll_table *wait)
816{
817 struct n_hdlc *n_hdlc = tty2n_hdlc (tty);
818 unsigned int mask = 0;
819
820 if (debuglevel >= DEBUG_LEVEL_INFO)
821 printk("%s(%d)n_hdlc_tty_poll() called\n",__FILE__,__LINE__);
822
823 if (n_hdlc && n_hdlc->magic == HDLC_MAGIC && tty == n_hdlc->tty) {
824 /* queue current process into any wait queue that */
825 /* may awaken in the future (read and write) */
826
827 poll_wait(filp, &tty->read_wait, wait);
828 poll_wait(filp, &tty->write_wait, wait);
829
830 /* set bits for operations that won't block */
831 if (n_hdlc->rx_buf_list.head)
832 mask |= POLLIN | POLLRDNORM; /* readable */
833 if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
834 mask |= POLLHUP;
835 if (tty_hung_up_p(filp))
836 mask |= POLLHUP;
837 if (!tty_is_writelocked(tty) &&
838 n_hdlc->tx_free_buf_list.head)
839 mask |= POLLOUT | POLLWRNORM; /* writable */
840 }
841 return mask;
842} /* end of n_hdlc_tty_poll() */
843
844/**
845 * n_hdlc_alloc - allocate an n_hdlc instance data structure
846 *
847 * Returns a pointer to newly created structure if success, otherwise %NULL
848 */
849static struct n_hdlc *n_hdlc_alloc(void)
850{
851 struct n_hdlc_buf *buf;
852 int i;
853 struct n_hdlc *n_hdlc = kmalloc(sizeof(*n_hdlc), GFP_KERNEL);
854
855 if (!n_hdlc)
856 return NULL;
857
858 memset(n_hdlc, 0, sizeof(*n_hdlc));
859
860 n_hdlc_buf_list_init(&n_hdlc->rx_free_buf_list);
861 n_hdlc_buf_list_init(&n_hdlc->tx_free_buf_list);
862 n_hdlc_buf_list_init(&n_hdlc->rx_buf_list);
863 n_hdlc_buf_list_init(&n_hdlc->tx_buf_list);
864
865 /* allocate free rx buffer list */
866 for(i=0;i<DEFAULT_RX_BUF_COUNT;i++) {
867 buf = kmalloc(N_HDLC_BUF_SIZE, GFP_KERNEL);
868 if (buf)
869 n_hdlc_buf_put(&n_hdlc->rx_free_buf_list,buf);
870 else if (debuglevel >= DEBUG_LEVEL_INFO)
871 printk("%s(%d)n_hdlc_alloc(), kalloc() failed for rx buffer %d\n",__FILE__,__LINE__, i);
872 }
873
874 /* allocate free tx buffer list */
875 for(i=0;i<DEFAULT_TX_BUF_COUNT;i++) {
876 buf = kmalloc(N_HDLC_BUF_SIZE, GFP_KERNEL);
877 if (buf)
878 n_hdlc_buf_put(&n_hdlc->tx_free_buf_list,buf);
879 else if (debuglevel >= DEBUG_LEVEL_INFO)
880 printk("%s(%d)n_hdlc_alloc(), kalloc() failed for tx buffer %d\n",__FILE__,__LINE__, i);
881 }
882
883 /* Initialize the control block */
884 n_hdlc->magic = HDLC_MAGIC;
885 n_hdlc->flags = 0;
886
887 return n_hdlc;
888
889} /* end of n_hdlc_alloc() */
890
891/**
892 * n_hdlc_buf_list_init - initialize specified HDLC buffer list
893 * @list - pointer to buffer list
894 */
895static void n_hdlc_buf_list_init(struct n_hdlc_buf_list *list)
896{
897 memset(list, 0, sizeof(*list));
898 spin_lock_init(&list->spinlock);
899} /* end of n_hdlc_buf_list_init() */
900
901/**
902 * n_hdlc_buf_put - add specified HDLC buffer to tail of specified list
903 * @list - pointer to buffer list
904 * @buf - pointer to buffer
905 */
906static void n_hdlc_buf_put(struct n_hdlc_buf_list *list,
907 struct n_hdlc_buf *buf)
908{
909 unsigned long flags;
910 spin_lock_irqsave(&list->spinlock,flags);
911
912 buf->link=NULL;
913 if (list->tail)
914 list->tail->link = buf;
915 else
916 list->head = buf;
917 list->tail = buf;
918 (list->count)++;
919
920 spin_unlock_irqrestore(&list->spinlock,flags);
921
922} /* end of n_hdlc_buf_put() */
923
924/**
925 * n_hdlc_buf_get - remove and return an HDLC buffer from list
926 * @list - pointer to HDLC buffer list
927 *
928 * Remove and return an HDLC buffer from the head of the specified HDLC buffer
929 * list.
930 * Returns a pointer to HDLC buffer if available, otherwise %NULL.
931 */
932static struct n_hdlc_buf* n_hdlc_buf_get(struct n_hdlc_buf_list *list)
933{
934 unsigned long flags;
935 struct n_hdlc_buf *buf;
936 spin_lock_irqsave(&list->spinlock,flags);
937
938 buf = list->head;
939 if (buf) {
940 list->head = buf->link;
941 (list->count)--;
942 }
943 if (!list->head)
944 list->tail = NULL;
945
946 spin_unlock_irqrestore(&list->spinlock,flags);
947 return buf;
948
949} /* end of n_hdlc_buf_get() */
950
951static char hdlc_banner[] __initdata =
952 KERN_INFO "HDLC line discipline maxframe=%u\n";
953static char hdlc_register_ok[] __initdata =
954 KERN_INFO "N_HDLC line discipline registered.\n";
955static char hdlc_register_fail[] __initdata =
956 KERN_ERR "error registering line discipline: %d\n";
957static char hdlc_init_fail[] __initdata =
958 KERN_INFO "N_HDLC: init failure %d\n";
959
960static int __init n_hdlc_init(void)
961{
962 int status;
963
964 /* range check maxframe arg */
965 if (maxframe < 4096)
966 maxframe = 4096;
967 else if (maxframe > 65535)
968 maxframe = 65535;
969
970 printk(hdlc_banner, maxframe);
971
972 status = tty_register_ldisc(N_HDLC, &n_hdlc_ldisc);
973 if (!status)
974 printk(hdlc_register_ok);
975 else
976 printk(hdlc_register_fail, status);
977
978 if (status)
979 printk(hdlc_init_fail, status);
980 return status;
981
982} /* end of init_module() */
983
984static char hdlc_unregister_ok[] __exitdata =
985 KERN_INFO "N_HDLC: line discipline unregistered\n";
986static char hdlc_unregister_fail[] __exitdata =
987 KERN_ERR "N_HDLC: can't unregister line discipline (err = %d)\n";
988
989static void __exit n_hdlc_exit(void)
990{
991 /* Release tty registration of line discipline */
992 int status = tty_unregister_ldisc(N_HDLC);
993
994 if (status)
995 printk(hdlc_unregister_fail, status);
996 else
997 printk(hdlc_unregister_ok);
998}
999
1000module_init(n_hdlc_init);
1001module_exit(n_hdlc_exit);
1002
1003MODULE_LICENSE("GPL");
1004MODULE_AUTHOR("Paul Fulghum paulkf@microgate.com");
1005module_param(debuglevel, int, 0);
1006module_param(maxframe, int, 0);
1007MODULE_ALIAS_LDISC(N_HDLC);
diff --git a/drivers/char/n_r3964.c b/drivers/char/n_r3964.c
deleted file mode 100644
index a98290d7a2c5..000000000000
--- a/drivers/char/n_r3964.c
+++ /dev/null
@@ -1,1265 +0,0 @@
1/* r3964 linediscipline for linux
2 *
3 * -----------------------------------------------------------
4 * Copyright by
5 * Philips Automation Projects
6 * Kassel (Germany)
7 * http://www.pap-philips.de
8 * -----------------------------------------------------------
9 * This software may be used and distributed according to the terms of
10 * the GNU General Public License, incorporated herein by reference.
11 *
12 * Author:
13 * L. Haag
14 *
15 * $Log: n_r3964.c,v $
16 * Revision 1.10 2001/03/18 13:02:24 dwmw2
17 * Fix timer usage, use spinlocks properly.
18 *
19 * Revision 1.9 2001/03/18 12:52:14 dwmw2
20 * Merge changes in 2.4.2
21 *
22 * Revision 1.8 2000/03/23 14:14:54 dwmw2
23 * Fix race in sleeping in r3964_read()
24 *
25 * Revision 1.7 1999/28/08 11:41:50 dwmw2
26 * Port to 2.3 kernel
27 *
28 * Revision 1.6 1998/09/30 00:40:40 dwmw2
29 * Fixed compilation on 2.0.x kernels
30 * Updated to newly registered tty-ldisc number 9
31 *
32 * Revision 1.5 1998/09/04 21:57:36 dwmw2
33 * Signal handling bug fixes, port to 2.1.x.
34 *
35 * Revision 1.4 1998/04/02 20:26:59 lhaag
36 * select, blocking, ...
37 *
38 * Revision 1.3 1998/02/12 18:58:43 root
39 * fixed some memory leaks
40 * calculation of checksum characters
41 *
42 * Revision 1.2 1998/02/07 13:03:34 root
43 * ioctl read_telegram
44 *
45 * Revision 1.1 1998/02/06 19:21:03 root
46 * Initial revision
47 *
48 *
49 */
50
51#include <linux/module.h>
52#include <linux/kernel.h>
53#include <linux/sched.h>
54#include <linux/types.h>
55#include <linux/fcntl.h>
56#include <linux/interrupt.h>
57#include <linux/ptrace.h>
58#include <linux/ioport.h>
59#include <linux/in.h>
60#include <linux/slab.h>
61#include <linux/smp_lock.h>
62#include <linux/tty.h>
63#include <linux/errno.h>
64#include <linux/string.h> /* used in new tty drivers */
65#include <linux/signal.h> /* used in new tty drivers */
66#include <linux/ioctl.h>
67#include <linux/n_r3964.h>
68#include <linux/poll.h>
69#include <linux/init.h>
70#include <asm/uaccess.h>
71
72/*#define DEBUG_QUEUE*/
73
74/* Log successful handshake and protocol operations */
75/*#define DEBUG_PROTO_S*/
76
77/* Log handshake and protocol errors: */
78/*#define DEBUG_PROTO_E*/
79
80/* Log Linediscipline operations (open, close, read, write...): */
81/*#define DEBUG_LDISC*/
82
83/* Log module and memory operations (init, cleanup; kmalloc, kfree): */
84/*#define DEBUG_MODUL*/
85
86/* Macro helpers for debug output: */
87#define TRACE(format, args...) printk("r3964: " format "\n" , ## args)
88
89#ifdef DEBUG_MODUL
90#define TRACE_M(format, args...) printk("r3964: " format "\n" , ## args)
91#else
92#define TRACE_M(fmt, arg...) do {} while (0)
93#endif
94#ifdef DEBUG_PROTO_S
95#define TRACE_PS(format, args...) printk("r3964: " format "\n" , ## args)
96#else
97#define TRACE_PS(fmt, arg...) do {} while (0)
98#endif
99#ifdef DEBUG_PROTO_E
100#define TRACE_PE(format, args...) printk("r3964: " format "\n" , ## args)
101#else
102#define TRACE_PE(fmt, arg...) do {} while (0)
103#endif
104#ifdef DEBUG_LDISC
105#define TRACE_L(format, args...) printk("r3964: " format "\n" , ## args)
106#else
107#define TRACE_L(fmt, arg...) do {} while (0)
108#endif
109#ifdef DEBUG_QUEUE
110#define TRACE_Q(format, args...) printk("r3964: " format "\n" , ## args)
111#else
112#define TRACE_Q(fmt, arg...) do {} while (0)
113#endif
114static void add_tx_queue(struct r3964_info *, struct r3964_block_header *);
115static void remove_from_tx_queue(struct r3964_info *pInfo, int error_code);
116static void put_char(struct r3964_info *pInfo, unsigned char ch);
117static void trigger_transmit(struct r3964_info *pInfo);
118static void retry_transmit(struct r3964_info *pInfo);
119static void transmit_block(struct r3964_info *pInfo);
120static void receive_char(struct r3964_info *pInfo, const unsigned char c);
121static void receive_error(struct r3964_info *pInfo, const char flag);
122static void on_timeout(unsigned long priv);
123static int enable_signals(struct r3964_info *pInfo, struct pid *pid, int arg);
124static int read_telegram(struct r3964_info *pInfo, struct pid *pid,
125 unsigned char __user * buf);
126static void add_msg(struct r3964_client_info *pClient, int msg_id, int arg,
127 int error_code, struct r3964_block_header *pBlock);
128static struct r3964_message *remove_msg(struct r3964_info *pInfo,
129 struct r3964_client_info *pClient);
130static void remove_client_block(struct r3964_info *pInfo,
131 struct r3964_client_info *pClient);
132
133static int r3964_open(struct tty_struct *tty);
134static void r3964_close(struct tty_struct *tty);
135static ssize_t r3964_read(struct tty_struct *tty, struct file *file,
136 unsigned char __user * buf, size_t nr);
137static ssize_t r3964_write(struct tty_struct *tty, struct file *file,
138 const unsigned char *buf, size_t nr);
139static int r3964_ioctl(struct tty_struct *tty, struct file *file,
140 unsigned int cmd, unsigned long arg);
141static void r3964_set_termios(struct tty_struct *tty, struct ktermios *old);
142static unsigned int r3964_poll(struct tty_struct *tty, struct file *file,
143 struct poll_table_struct *wait);
144static void r3964_receive_buf(struct tty_struct *tty, const unsigned char *cp,
145 char *fp, int count);
146
147static struct tty_ldisc_ops tty_ldisc_N_R3964 = {
148 .owner = THIS_MODULE,
149 .magic = TTY_LDISC_MAGIC,
150 .name = "R3964",
151 .open = r3964_open,
152 .close = r3964_close,
153 .read = r3964_read,
154 .write = r3964_write,
155 .ioctl = r3964_ioctl,
156 .set_termios = r3964_set_termios,
157 .poll = r3964_poll,
158 .receive_buf = r3964_receive_buf,
159};
160
161static void dump_block(const unsigned char *block, unsigned int length)
162{
163 unsigned int i, j;
164 char linebuf[16 * 3 + 1];
165
166 for (i = 0; i < length; i += 16) {
167 for (j = 0; (j < 16) && (j + i < length); j++) {
168 sprintf(linebuf + 3 * j, "%02x ", block[i + j]);
169 }
170 linebuf[3 * j] = '\0';
171 TRACE_PS("%s", linebuf);
172 }
173}
174
175/*************************************************************
176 * Driver initialisation
177 *************************************************************/
178
179/*************************************************************
180 * Module support routines
181 *************************************************************/
182
183static void __exit r3964_exit(void)
184{
185 int status;
186
187 TRACE_M("cleanup_module()");
188
189 status = tty_unregister_ldisc(N_R3964);
190
191 if (status != 0) {
192 printk(KERN_ERR "r3964: error unregistering linediscipline: "
193 "%d\n", status);
194 } else {
195 TRACE_L("linediscipline successfully unregistered");
196 }
197}
198
199static int __init r3964_init(void)
200{
201 int status;
202
203 printk("r3964: Philips r3964 Driver $Revision: 1.10 $\n");
204
205 /*
206 * Register the tty line discipline
207 */
208
209 status = tty_register_ldisc(N_R3964, &tty_ldisc_N_R3964);
210 if (status == 0) {
211 TRACE_L("line discipline %d registered", N_R3964);
212 TRACE_L("flags=%x num=%x", tty_ldisc_N_R3964.flags,
213 tty_ldisc_N_R3964.num);
214 TRACE_L("open=%p", tty_ldisc_N_R3964.open);
215 TRACE_L("tty_ldisc_N_R3964 = %p", &tty_ldisc_N_R3964);
216 } else {
217 printk(KERN_ERR "r3964: error registering line discipline: "
218 "%d\n", status);
219 }
220 return status;
221}
222
223module_init(r3964_init);
224module_exit(r3964_exit);
225
226/*************************************************************
227 * Protocol implementation routines
228 *************************************************************/
229
230static void add_tx_queue(struct r3964_info *pInfo,
231 struct r3964_block_header *pHeader)
232{
233 unsigned long flags;
234
235 spin_lock_irqsave(&pInfo->lock, flags);
236
237 pHeader->next = NULL;
238
239 if (pInfo->tx_last == NULL) {
240 pInfo->tx_first = pInfo->tx_last = pHeader;
241 } else {
242 pInfo->tx_last->next = pHeader;
243 pInfo->tx_last = pHeader;
244 }
245
246 spin_unlock_irqrestore(&pInfo->lock, flags);
247
248 TRACE_Q("add_tx_queue %p, length %d, tx_first = %p",
249 pHeader, pHeader->length, pInfo->tx_first);
250}
251
252static void remove_from_tx_queue(struct r3964_info *pInfo, int error_code)
253{
254 struct r3964_block_header *pHeader;
255 unsigned long flags;
256#ifdef DEBUG_QUEUE
257 struct r3964_block_header *pDump;
258#endif
259
260 pHeader = pInfo->tx_first;
261
262 if (pHeader == NULL)
263 return;
264
265#ifdef DEBUG_QUEUE
266 printk("r3964: remove_from_tx_queue: %p, length %u - ",
267 pHeader, pHeader->length);
268 for (pDump = pHeader; pDump; pDump = pDump->next)
269 printk("%p ", pDump);
270 printk("\n");
271#endif
272
273 if (pHeader->owner) {
274 if (error_code) {
275 add_msg(pHeader->owner, R3964_MSG_ACK, 0,
276 error_code, NULL);
277 } else {
278 add_msg(pHeader->owner, R3964_MSG_ACK, pHeader->length,
279 error_code, NULL);
280 }
281 wake_up_interruptible(&pInfo->read_wait);
282 }
283
284 spin_lock_irqsave(&pInfo->lock, flags);
285
286 pInfo->tx_first = pHeader->next;
287 if (pInfo->tx_first == NULL) {
288 pInfo->tx_last = NULL;
289 }
290
291 spin_unlock_irqrestore(&pInfo->lock, flags);
292
293 kfree(pHeader);
294 TRACE_M("remove_from_tx_queue - kfree %p", pHeader);
295
296 TRACE_Q("remove_from_tx_queue: tx_first = %p, tx_last = %p",
297 pInfo->tx_first, pInfo->tx_last);
298}
299
300static void add_rx_queue(struct r3964_info *pInfo,
301 struct r3964_block_header *pHeader)
302{
303 unsigned long flags;
304
305 spin_lock_irqsave(&pInfo->lock, flags);
306
307 pHeader->next = NULL;
308
309 if (pInfo->rx_last == NULL) {
310 pInfo->rx_first = pInfo->rx_last = pHeader;
311 } else {
312 pInfo->rx_last->next = pHeader;
313 pInfo->rx_last = pHeader;
314 }
315 pInfo->blocks_in_rx_queue++;
316
317 spin_unlock_irqrestore(&pInfo->lock, flags);
318
319 TRACE_Q("add_rx_queue: %p, length = %d, rx_first = %p, count = %d",
320 pHeader, pHeader->length,
321 pInfo->rx_first, pInfo->blocks_in_rx_queue);
322}
323
324static void remove_from_rx_queue(struct r3964_info *pInfo,
325 struct r3964_block_header *pHeader)
326{
327 unsigned long flags;
328 struct r3964_block_header *pFind;
329
330 if (pHeader == NULL)
331 return;
332
333 TRACE_Q("remove_from_rx_queue: rx_first = %p, rx_last = %p, count = %d",
334 pInfo->rx_first, pInfo->rx_last, pInfo->blocks_in_rx_queue);
335 TRACE_Q("remove_from_rx_queue: %p, length %u",
336 pHeader, pHeader->length);
337
338 spin_lock_irqsave(&pInfo->lock, flags);
339
340 if (pInfo->rx_first == pHeader) {
341 /* Remove the first block in the linked list: */
342 pInfo->rx_first = pHeader->next;
343
344 if (pInfo->rx_first == NULL) {
345 pInfo->rx_last = NULL;
346 }
347 pInfo->blocks_in_rx_queue--;
348 } else {
349 /* Find block to remove: */
350 for (pFind = pInfo->rx_first; pFind; pFind = pFind->next) {
351 if (pFind->next == pHeader) {
352 /* Got it. */
353 pFind->next = pHeader->next;
354 pInfo->blocks_in_rx_queue--;
355 if (pFind->next == NULL) {
356 /* Oh, removed the last one! */
357 pInfo->rx_last = pFind;
358 }
359 break;
360 }
361 }
362 }
363
364 spin_unlock_irqrestore(&pInfo->lock, flags);
365
366 kfree(pHeader);
367 TRACE_M("remove_from_rx_queue - kfree %p", pHeader);
368
369 TRACE_Q("remove_from_rx_queue: rx_first = %p, rx_last = %p, count = %d",
370 pInfo->rx_first, pInfo->rx_last, pInfo->blocks_in_rx_queue);
371}
372
373static void put_char(struct r3964_info *pInfo, unsigned char ch)
374{
375 struct tty_struct *tty = pInfo->tty;
376 /* FIXME: put_char should not be called from an IRQ */
377 tty_put_char(tty, ch);
378 pInfo->bcc ^= ch;
379}
380
381static void flush(struct r3964_info *pInfo)
382{
383 struct tty_struct *tty = pInfo->tty;
384
385 if (tty == NULL || tty->ops->flush_chars == NULL)
386 return;
387 tty->ops->flush_chars(tty);
388}
389
390static void trigger_transmit(struct r3964_info *pInfo)
391{
392 unsigned long flags;
393
394 spin_lock_irqsave(&pInfo->lock, flags);
395
396 if ((pInfo->state == R3964_IDLE) && (pInfo->tx_first != NULL)) {
397 pInfo->state = R3964_TX_REQUEST;
398 pInfo->nRetry = 0;
399 pInfo->flags &= ~R3964_ERROR;
400 mod_timer(&pInfo->tmr, jiffies + R3964_TO_QVZ);
401
402 spin_unlock_irqrestore(&pInfo->lock, flags);
403
404 TRACE_PS("trigger_transmit - sent STX");
405
406 put_char(pInfo, STX);
407 flush(pInfo);
408
409 pInfo->bcc = 0;
410 } else {
411 spin_unlock_irqrestore(&pInfo->lock, flags);
412 }
413}
414
415static void retry_transmit(struct r3964_info *pInfo)
416{
417 if (pInfo->nRetry < R3964_MAX_RETRIES) {
418 TRACE_PE("transmission failed. Retry #%d", pInfo->nRetry);
419 pInfo->bcc = 0;
420 put_char(pInfo, STX);
421 flush(pInfo);
422 pInfo->state = R3964_TX_REQUEST;
423 pInfo->nRetry++;
424 mod_timer(&pInfo->tmr, jiffies + R3964_TO_QVZ);
425 } else {
426 TRACE_PE("transmission failed after %d retries",
427 R3964_MAX_RETRIES);
428
429 remove_from_tx_queue(pInfo, R3964_TX_FAIL);
430
431 put_char(pInfo, NAK);
432 flush(pInfo);
433 pInfo->state = R3964_IDLE;
434
435 trigger_transmit(pInfo);
436 }
437}
438
439static void transmit_block(struct r3964_info *pInfo)
440{
441 struct tty_struct *tty = pInfo->tty;
442 struct r3964_block_header *pBlock = pInfo->tx_first;
443 int room = 0;
444
445 if (tty == NULL || pBlock == NULL) {
446 return;
447 }
448
449 room = tty_write_room(tty);
450
451 TRACE_PS("transmit_block %p, room %d, length %d",
452 pBlock, room, pBlock->length);
453
454 while (pInfo->tx_position < pBlock->length) {
455 if (room < 2)
456 break;
457
458 if (pBlock->data[pInfo->tx_position] == DLE) {
459 /* send additional DLE char: */
460 put_char(pInfo, DLE);
461 }
462 put_char(pInfo, pBlock->data[pInfo->tx_position++]);
463
464 room--;
465 }
466
467 if ((pInfo->tx_position == pBlock->length) && (room >= 3)) {
468 put_char(pInfo, DLE);
469 put_char(pInfo, ETX);
470 if (pInfo->flags & R3964_BCC) {
471 put_char(pInfo, pInfo->bcc);
472 }
473 pInfo->state = R3964_WAIT_FOR_TX_ACK;
474 mod_timer(&pInfo->tmr, jiffies + R3964_TO_QVZ);
475 }
476 flush(pInfo);
477}
478
479static void on_receive_block(struct r3964_info *pInfo)
480{
481 unsigned int length;
482 struct r3964_client_info *pClient;
483 struct r3964_block_header *pBlock;
484
485 length = pInfo->rx_position;
486
487 /* compare byte checksum characters: */
488 if (pInfo->flags & R3964_BCC) {
489 if (pInfo->bcc != pInfo->last_rx) {
490 TRACE_PE("checksum error - got %x but expected %x",
491 pInfo->last_rx, pInfo->bcc);
492 pInfo->flags |= R3964_CHECKSUM;
493 }
494 }
495
496 /* check for errors (parity, overrun,...): */
497 if (pInfo->flags & R3964_ERROR) {
498 TRACE_PE("on_receive_block - transmission failed error %x",
499 pInfo->flags & R3964_ERROR);
500
501 put_char(pInfo, NAK);
502 flush(pInfo);
503 if (pInfo->nRetry < R3964_MAX_RETRIES) {
504 pInfo->state = R3964_WAIT_FOR_RX_REPEAT;
505 pInfo->nRetry++;
506 mod_timer(&pInfo->tmr, jiffies + R3964_TO_RX_PANIC);
507 } else {
508 TRACE_PE("on_receive_block - failed after max retries");
509 pInfo->state = R3964_IDLE;
510 }
511 return;
512 }
513
514 /* received block; submit DLE: */
515 put_char(pInfo, DLE);
516 flush(pInfo);
517 del_timer_sync(&pInfo->tmr);
518 TRACE_PS(" rx success: got %d chars", length);
519
520 /* prepare struct r3964_block_header: */
521 pBlock = kmalloc(length + sizeof(struct r3964_block_header),
522 GFP_KERNEL);
523 TRACE_M("on_receive_block - kmalloc %p", pBlock);
524
525 if (pBlock == NULL)
526 return;
527
528 pBlock->length = length;
529 pBlock->data = ((unsigned char *)pBlock) +
530 sizeof(struct r3964_block_header);
531 pBlock->locks = 0;
532 pBlock->next = NULL;
533 pBlock->owner = NULL;
534
535 memcpy(pBlock->data, pInfo->rx_buf, length);
536
537 /* queue block into rx_queue: */
538 add_rx_queue(pInfo, pBlock);
539
540 /* notify attached client processes: */
541 for (pClient = pInfo->firstClient; pClient; pClient = pClient->next) {
542 if (pClient->sig_flags & R3964_SIG_DATA) {
543 add_msg(pClient, R3964_MSG_DATA, length, R3964_OK,
544 pBlock);
545 }
546 }
547 wake_up_interruptible(&pInfo->read_wait);
548
549 pInfo->state = R3964_IDLE;
550
551 trigger_transmit(pInfo);
552}
553
554static void receive_char(struct r3964_info *pInfo, const unsigned char c)
555{
556 switch (pInfo->state) {
557 case R3964_TX_REQUEST:
558 if (c == DLE) {
559 TRACE_PS("TX_REQUEST - got DLE");
560
561 pInfo->state = R3964_TRANSMITTING;
562 pInfo->tx_position = 0;
563
564 transmit_block(pInfo);
565 } else if (c == STX) {
566 if (pInfo->nRetry == 0) {
567 TRACE_PE("TX_REQUEST - init conflict");
568 if (pInfo->priority == R3964_SLAVE) {
569 goto start_receiving;
570 }
571 } else {
572 TRACE_PE("TX_REQUEST - secondary init "
573 "conflict!? Switching to SLAVE mode "
574 "for next rx.");
575 goto start_receiving;
576 }
577 } else {
578 TRACE_PE("TX_REQUEST - char != DLE: %x", c);
579 retry_transmit(pInfo);
580 }
581 break;
582 case R3964_TRANSMITTING:
583 if (c == NAK) {
584 TRACE_PE("TRANSMITTING - got NAK");
585 retry_transmit(pInfo);
586 } else {
587 TRACE_PE("TRANSMITTING - got invalid char");
588
589 pInfo->state = R3964_WAIT_ZVZ_BEFORE_TX_RETRY;
590 mod_timer(&pInfo->tmr, jiffies + R3964_TO_ZVZ);
591 }
592 break;
593 case R3964_WAIT_FOR_TX_ACK:
594 if (c == DLE) {
595 TRACE_PS("WAIT_FOR_TX_ACK - got DLE");
596 remove_from_tx_queue(pInfo, R3964_OK);
597
598 pInfo->state = R3964_IDLE;
599 trigger_transmit(pInfo);
600 } else {
601 retry_transmit(pInfo);
602 }
603 break;
604 case R3964_WAIT_FOR_RX_REPEAT:
605 /* FALLTHROUGH */
606 case R3964_IDLE:
607 if (c == STX) {
608 /* Prevent rx_queue from overflow: */
609 if (pInfo->blocks_in_rx_queue >=
610 R3964_MAX_BLOCKS_IN_RX_QUEUE) {
611 TRACE_PE("IDLE - got STX but no space in "
612 "rx_queue!");
613 pInfo->state = R3964_WAIT_FOR_RX_BUF;
614 mod_timer(&pInfo->tmr,
615 jiffies + R3964_TO_NO_BUF);
616 break;
617 }
618start_receiving:
619 /* Ok, start receiving: */
620 TRACE_PS("IDLE - got STX");
621 pInfo->rx_position = 0;
622 pInfo->last_rx = 0;
623 pInfo->flags &= ~R3964_ERROR;
624 pInfo->state = R3964_RECEIVING;
625 mod_timer(&pInfo->tmr, jiffies + R3964_TO_ZVZ);
626 pInfo->nRetry = 0;
627 put_char(pInfo, DLE);
628 flush(pInfo);
629 pInfo->bcc = 0;
630 }
631 break;
632 case R3964_RECEIVING:
633 if (pInfo->rx_position < RX_BUF_SIZE) {
634 pInfo->bcc ^= c;
635
636 if (c == DLE) {
637 if (pInfo->last_rx == DLE) {
638 pInfo->last_rx = 0;
639 goto char_to_buf;
640 }
641 pInfo->last_rx = DLE;
642 break;
643 } else if ((c == ETX) && (pInfo->last_rx == DLE)) {
644 if (pInfo->flags & R3964_BCC) {
645 pInfo->state = R3964_WAIT_FOR_BCC;
646 mod_timer(&pInfo->tmr,
647 jiffies + R3964_TO_ZVZ);
648 } else {
649 on_receive_block(pInfo);
650 }
651 } else {
652 pInfo->last_rx = c;
653char_to_buf:
654 pInfo->rx_buf[pInfo->rx_position++] = c;
655 mod_timer(&pInfo->tmr, jiffies + R3964_TO_ZVZ);
656 }
657 }
658 /* else: overflow-msg? BUF_SIZE>MTU; should not happen? */
659 break;
660 case R3964_WAIT_FOR_BCC:
661 pInfo->last_rx = c;
662 on_receive_block(pInfo);
663 break;
664 }
665}
666
667static void receive_error(struct r3964_info *pInfo, const char flag)
668{
669 switch (flag) {
670 case TTY_NORMAL:
671 break;
672 case TTY_BREAK:
673 TRACE_PE("received break");
674 pInfo->flags |= R3964_BREAK;
675 break;
676 case TTY_PARITY:
677 TRACE_PE("parity error");
678 pInfo->flags |= R3964_PARITY;
679 break;
680 case TTY_FRAME:
681 TRACE_PE("frame error");
682 pInfo->flags |= R3964_FRAME;
683 break;
684 case TTY_OVERRUN:
685 TRACE_PE("frame overrun");
686 pInfo->flags |= R3964_OVERRUN;
687 break;
688 default:
689 TRACE_PE("receive_error - unknown flag %d", flag);
690 pInfo->flags |= R3964_UNKNOWN;
691 break;
692 }
693}
694
695static void on_timeout(unsigned long priv)
696{
697 struct r3964_info *pInfo = (void *)priv;
698
699 switch (pInfo->state) {
700 case R3964_TX_REQUEST:
701 TRACE_PE("TX_REQUEST - timeout");
702 retry_transmit(pInfo);
703 break;
704 case R3964_WAIT_ZVZ_BEFORE_TX_RETRY:
705 put_char(pInfo, NAK);
706 flush(pInfo);
707 retry_transmit(pInfo);
708 break;
709 case R3964_WAIT_FOR_TX_ACK:
710 TRACE_PE("WAIT_FOR_TX_ACK - timeout");
711 retry_transmit(pInfo);
712 break;
713 case R3964_WAIT_FOR_RX_BUF:
714 TRACE_PE("WAIT_FOR_RX_BUF - timeout");
715 put_char(pInfo, NAK);
716 flush(pInfo);
717 pInfo->state = R3964_IDLE;
718 break;
719 case R3964_RECEIVING:
720 TRACE_PE("RECEIVING - timeout after %d chars",
721 pInfo->rx_position);
722 put_char(pInfo, NAK);
723 flush(pInfo);
724 pInfo->state = R3964_IDLE;
725 break;
726 case R3964_WAIT_FOR_RX_REPEAT:
727 TRACE_PE("WAIT_FOR_RX_REPEAT - timeout");
728 pInfo->state = R3964_IDLE;
729 break;
730 case R3964_WAIT_FOR_BCC:
731 TRACE_PE("WAIT_FOR_BCC - timeout");
732 put_char(pInfo, NAK);
733 flush(pInfo);
734 pInfo->state = R3964_IDLE;
735 break;
736 }
737}
738
739static struct r3964_client_info *findClient(struct r3964_info *pInfo,
740 struct pid *pid)
741{
742 struct r3964_client_info *pClient;
743
744 for (pClient = pInfo->firstClient; pClient; pClient = pClient->next) {
745 if (pClient->pid == pid) {
746 return pClient;
747 }
748 }
749 return NULL;
750}
751
752static int enable_signals(struct r3964_info *pInfo, struct pid *pid, int arg)
753{
754 struct r3964_client_info *pClient;
755 struct r3964_client_info **ppClient;
756 struct r3964_message *pMsg;
757
758 if ((arg & R3964_SIG_ALL) == 0) {
759 /* Remove client from client list */
760 for (ppClient = &pInfo->firstClient; *ppClient;
761 ppClient = &(*ppClient)->next) {
762 pClient = *ppClient;
763
764 if (pClient->pid == pid) {
765 TRACE_PS("removing client %d from client list",
766 pid_nr(pid));
767 *ppClient = pClient->next;
768 while (pClient->msg_count) {
769 pMsg = remove_msg(pInfo, pClient);
770 if (pMsg) {
771 kfree(pMsg);
772 TRACE_M("enable_signals - msg "
773 "kfree %p", pMsg);
774 }
775 }
776 put_pid(pClient->pid);
777 kfree(pClient);
778 TRACE_M("enable_signals - kfree %p", pClient);
779 return 0;
780 }
781 }
782 return -EINVAL;
783 } else {
784 pClient = findClient(pInfo, pid);
785 if (pClient) {
786 /* update signal options */
787 pClient->sig_flags = arg;
788 } else {
789 /* add client to client list */
790 pClient = kmalloc(sizeof(struct r3964_client_info),
791 GFP_KERNEL);
792 TRACE_M("enable_signals - kmalloc %p", pClient);
793 if (pClient == NULL)
794 return -ENOMEM;
795
796 TRACE_PS("add client %d to client list", pid_nr(pid));
797 spin_lock_init(&pClient->lock);
798 pClient->sig_flags = arg;
799 pClient->pid = get_pid(pid);
800 pClient->next = pInfo->firstClient;
801 pClient->first_msg = NULL;
802 pClient->last_msg = NULL;
803 pClient->next_block_to_read = NULL;
804 pClient->msg_count = 0;
805 pInfo->firstClient = pClient;
806 }
807 }
808
809 return 0;
810}
811
812static int read_telegram(struct r3964_info *pInfo, struct pid *pid,
813 unsigned char __user * buf)
814{
815 struct r3964_client_info *pClient;
816 struct r3964_block_header *block;
817
818 if (!buf) {
819 return -EINVAL;
820 }
821
822 pClient = findClient(pInfo, pid);
823 if (pClient == NULL) {
824 return -EINVAL;
825 }
826
827 block = pClient->next_block_to_read;
828 if (!block) {
829 return 0;
830 } else {
831 if (copy_to_user(buf, block->data, block->length))
832 return -EFAULT;
833
834 remove_client_block(pInfo, pClient);
835 return block->length;
836 }
837
838 return -EINVAL;
839}
840
841static void add_msg(struct r3964_client_info *pClient, int msg_id, int arg,
842 int error_code, struct r3964_block_header *pBlock)
843{
844 struct r3964_message *pMsg;
845 unsigned long flags;
846
847 if (pClient->msg_count < R3964_MAX_MSG_COUNT - 1) {
848queue_the_message:
849
850 pMsg = kmalloc(sizeof(struct r3964_message),
851 error_code ? GFP_ATOMIC : GFP_KERNEL);
852 TRACE_M("add_msg - kmalloc %p", pMsg);
853 if (pMsg == NULL) {
854 return;
855 }
856
857 spin_lock_irqsave(&pClient->lock, flags);
858
859 pMsg->msg_id = msg_id;
860 pMsg->arg = arg;
861 pMsg->error_code = error_code;
862 pMsg->block = pBlock;
863 pMsg->next = NULL;
864
865 if (pClient->last_msg == NULL) {
866 pClient->first_msg = pClient->last_msg = pMsg;
867 } else {
868 pClient->last_msg->next = pMsg;
869 pClient->last_msg = pMsg;
870 }
871
872 pClient->msg_count++;
873
874 if (pBlock != NULL) {
875 pBlock->locks++;
876 }
877 spin_unlock_irqrestore(&pClient->lock, flags);
878 } else {
879 if ((pClient->last_msg->msg_id == R3964_MSG_ACK)
880 && (pClient->last_msg->error_code == R3964_OVERFLOW)) {
881 pClient->last_msg->arg++;
882 TRACE_PE("add_msg - inc prev OVERFLOW-msg");
883 } else {
884 msg_id = R3964_MSG_ACK;
885 arg = 0;
886 error_code = R3964_OVERFLOW;
887 pBlock = NULL;
888 TRACE_PE("add_msg - queue OVERFLOW-msg");
889 goto queue_the_message;
890 }
891 }
892 /* Send SIGIO signal to client process: */
893 if (pClient->sig_flags & R3964_USE_SIGIO) {
894 kill_pid(pClient->pid, SIGIO, 1);
895 }
896}
897
898static struct r3964_message *remove_msg(struct r3964_info *pInfo,
899 struct r3964_client_info *pClient)
900{
901 struct r3964_message *pMsg = NULL;
902 unsigned long flags;
903
904 if (pClient->first_msg) {
905 spin_lock_irqsave(&pClient->lock, flags);
906
907 pMsg = pClient->first_msg;
908 pClient->first_msg = pMsg->next;
909 if (pClient->first_msg == NULL) {
910 pClient->last_msg = NULL;
911 }
912
913 pClient->msg_count--;
914 if (pMsg->block) {
915 remove_client_block(pInfo, pClient);
916 pClient->next_block_to_read = pMsg->block;
917 }
918 spin_unlock_irqrestore(&pClient->lock, flags);
919 }
920 return pMsg;
921}
922
923static void remove_client_block(struct r3964_info *pInfo,
924 struct r3964_client_info *pClient)
925{
926 struct r3964_block_header *block;
927
928 TRACE_PS("remove_client_block PID %d", pid_nr(pClient->pid));
929
930 block = pClient->next_block_to_read;
931 if (block) {
932 block->locks--;
933 if (block->locks == 0) {
934 remove_from_rx_queue(pInfo, block);
935 }
936 }
937 pClient->next_block_to_read = NULL;
938}
939
940/*************************************************************
941 * Line discipline routines
942 *************************************************************/
943
944static int r3964_open(struct tty_struct *tty)
945{
946 struct r3964_info *pInfo;
947
948 TRACE_L("open");
949 TRACE_L("tty=%p, PID=%d, disc_data=%p",
950 tty, current->pid, tty->disc_data);
951
952 pInfo = kmalloc(sizeof(struct r3964_info), GFP_KERNEL);
953 TRACE_M("r3964_open - info kmalloc %p", pInfo);
954
955 if (!pInfo) {
956 printk(KERN_ERR "r3964: failed to alloc info structure\n");
957 return -ENOMEM;
958 }
959
960 pInfo->rx_buf = kmalloc(RX_BUF_SIZE, GFP_KERNEL);
961 TRACE_M("r3964_open - rx_buf kmalloc %p", pInfo->rx_buf);
962
963 if (!pInfo->rx_buf) {
964 printk(KERN_ERR "r3964: failed to alloc receive buffer\n");
965 kfree(pInfo);
966 TRACE_M("r3964_open - info kfree %p", pInfo);
967 return -ENOMEM;
968 }
969
970 pInfo->tx_buf = kmalloc(TX_BUF_SIZE, GFP_KERNEL);
971 TRACE_M("r3964_open - tx_buf kmalloc %p", pInfo->tx_buf);
972
973 if (!pInfo->tx_buf) {
974 printk(KERN_ERR "r3964: failed to alloc transmit buffer\n");
975 kfree(pInfo->rx_buf);
976 TRACE_M("r3964_open - rx_buf kfree %p", pInfo->rx_buf);
977 kfree(pInfo);
978 TRACE_M("r3964_open - info kfree %p", pInfo);
979 return -ENOMEM;
980 }
981
982 spin_lock_init(&pInfo->lock);
983 pInfo->tty = tty;
984 init_waitqueue_head(&pInfo->read_wait);
985 pInfo->priority = R3964_MASTER;
986 pInfo->rx_first = pInfo->rx_last = NULL;
987 pInfo->tx_first = pInfo->tx_last = NULL;
988 pInfo->rx_position = 0;
989 pInfo->tx_position = 0;
990 pInfo->last_rx = 0;
991 pInfo->blocks_in_rx_queue = 0;
992 pInfo->firstClient = NULL;
993 pInfo->state = R3964_IDLE;
994 pInfo->flags = R3964_DEBUG;
995 pInfo->nRetry = 0;
996
997 tty->disc_data = pInfo;
998 tty->receive_room = 65536;
999
1000 setup_timer(&pInfo->tmr, on_timeout, (unsigned long)pInfo);
1001
1002 return 0;
1003}
1004
1005static void r3964_close(struct tty_struct *tty)
1006{
1007 struct r3964_info *pInfo = tty->disc_data;
1008 struct r3964_client_info *pClient, *pNext;
1009 struct r3964_message *pMsg;
1010 struct r3964_block_header *pHeader, *pNextHeader;
1011 unsigned long flags;
1012
1013 TRACE_L("close");
1014
1015 /*
1016 * Make sure that our task queue isn't activated. If it
1017 * is, take it out of the linked list.
1018 */
1019 del_timer_sync(&pInfo->tmr);
1020
1021 /* Remove client-structs and message queues: */
1022 pClient = pInfo->firstClient;
1023 while (pClient) {
1024 pNext = pClient->next;
1025 while (pClient->msg_count) {
1026 pMsg = remove_msg(pInfo, pClient);
1027 if (pMsg) {
1028 kfree(pMsg);
1029 TRACE_M("r3964_close - msg kfree %p", pMsg);
1030 }
1031 }
1032 put_pid(pClient->pid);
1033 kfree(pClient);
1034 TRACE_M("r3964_close - client kfree %p", pClient);
1035 pClient = pNext;
1036 }
1037 /* Remove jobs from tx_queue: */
1038 spin_lock_irqsave(&pInfo->lock, flags);
1039 pHeader = pInfo->tx_first;
1040 pInfo->tx_first = pInfo->tx_last = NULL;
1041 spin_unlock_irqrestore(&pInfo->lock, flags);
1042
1043 while (pHeader) {
1044 pNextHeader = pHeader->next;
1045 kfree(pHeader);
1046 pHeader = pNextHeader;
1047 }
1048
1049 /* Free buffers: */
1050 wake_up_interruptible(&pInfo->read_wait);
1051 kfree(pInfo->rx_buf);
1052 TRACE_M("r3964_close - rx_buf kfree %p", pInfo->rx_buf);
1053 kfree(pInfo->tx_buf);
1054 TRACE_M("r3964_close - tx_buf kfree %p", pInfo->tx_buf);
1055 kfree(pInfo);
1056 TRACE_M("r3964_close - info kfree %p", pInfo);
1057}
1058
1059static ssize_t r3964_read(struct tty_struct *tty, struct file *file,
1060 unsigned char __user * buf, size_t nr)
1061{
1062 struct r3964_info *pInfo = tty->disc_data;
1063 struct r3964_client_info *pClient;
1064 struct r3964_message *pMsg;
1065 struct r3964_client_message theMsg;
1066 int ret;
1067
1068 TRACE_L("read()");
1069
1070 tty_lock();
1071
1072 pClient = findClient(pInfo, task_pid(current));
1073 if (pClient) {
1074 pMsg = remove_msg(pInfo, pClient);
1075 if (pMsg == NULL) {
1076 /* no messages available. */
1077 if (file->f_flags & O_NONBLOCK) {
1078 ret = -EAGAIN;
1079 goto unlock;
1080 }
1081 /* block until there is a message: */
1082 wait_event_interruptible_tty(pInfo->read_wait,
1083 (pMsg = remove_msg(pInfo, pClient)));
1084 }
1085
1086 /* If we still haven't got a message, we must have been signalled */
1087
1088 if (!pMsg) {
1089 ret = -EINTR;
1090 goto unlock;
1091 }
1092
1093 /* deliver msg to client process: */
1094 theMsg.msg_id = pMsg->msg_id;
1095 theMsg.arg = pMsg->arg;
1096 theMsg.error_code = pMsg->error_code;
1097 ret = sizeof(struct r3964_client_message);
1098
1099 kfree(pMsg);
1100 TRACE_M("r3964_read - msg kfree %p", pMsg);
1101
1102 if (copy_to_user(buf, &theMsg, ret)) {
1103 ret = -EFAULT;
1104 goto unlock;
1105 }
1106
1107 TRACE_PS("read - return %d", ret);
1108 goto unlock;
1109 }
1110 ret = -EPERM;
1111unlock:
1112 tty_unlock();
1113 return ret;
1114}
1115
1116static ssize_t r3964_write(struct tty_struct *tty, struct file *file,
1117 const unsigned char *data, size_t count)
1118{
1119 struct r3964_info *pInfo = tty->disc_data;
1120 struct r3964_block_header *pHeader;
1121 struct r3964_client_info *pClient;
1122 unsigned char *new_data;
1123
1124 TRACE_L("write request, %d characters", count);
1125/*
1126 * Verify the pointers
1127 */
1128
1129 if (!pInfo)
1130 return -EIO;
1131
1132/*
1133 * Ensure that the caller does not wish to send too much.
1134 */
1135 if (count > R3964_MTU) {
1136 if (pInfo->flags & R3964_DEBUG) {
1137 TRACE_L(KERN_WARNING "r3964_write: truncating user "
1138 "packet from %u to mtu %d", count, R3964_MTU);
1139 }
1140 count = R3964_MTU;
1141 }
1142/*
1143 * Allocate a buffer for the data and copy it from the buffer with header prepended
1144 */
1145 new_data = kmalloc(count + sizeof(struct r3964_block_header),
1146 GFP_KERNEL);
1147 TRACE_M("r3964_write - kmalloc %p", new_data);
1148 if (new_data == NULL) {
1149 if (pInfo->flags & R3964_DEBUG) {
1150 printk(KERN_ERR "r3964_write: no memory\n");
1151 }
1152 return -ENOSPC;
1153 }
1154
1155 pHeader = (struct r3964_block_header *)new_data;
1156 pHeader->data = new_data + sizeof(struct r3964_block_header);
1157 pHeader->length = count;
1158 pHeader->locks = 0;
1159 pHeader->owner = NULL;
1160
1161 tty_lock();
1162
1163 pClient = findClient(pInfo, task_pid(current));
1164 if (pClient) {
1165 pHeader->owner = pClient;
1166 }
1167
1168 memcpy(pHeader->data, data, count); /* We already verified this */
1169
1170 if (pInfo->flags & R3964_DEBUG) {
1171 dump_block(pHeader->data, count);
1172 }
1173
1174/*
1175 * Add buffer to transmit-queue:
1176 */
1177 add_tx_queue(pInfo, pHeader);
1178 trigger_transmit(pInfo);
1179
1180 tty_unlock();
1181
1182 return 0;
1183}
1184
1185static int r3964_ioctl(struct tty_struct *tty, struct file *file,
1186 unsigned int cmd, unsigned long arg)
1187{
1188 struct r3964_info *pInfo = tty->disc_data;
1189 if (pInfo == NULL)
1190 return -EINVAL;
1191 switch (cmd) {
1192 case R3964_ENABLE_SIGNALS:
1193 return enable_signals(pInfo, task_pid(current), arg);
1194 case R3964_SETPRIORITY:
1195 if (arg < R3964_MASTER || arg > R3964_SLAVE)
1196 return -EINVAL;
1197 pInfo->priority = arg & 0xff;
1198 return 0;
1199 case R3964_USE_BCC:
1200 if (arg)
1201 pInfo->flags |= R3964_BCC;
1202 else
1203 pInfo->flags &= ~R3964_BCC;
1204 return 0;
1205 case R3964_READ_TELEGRAM:
1206 return read_telegram(pInfo, task_pid(current),
1207 (unsigned char __user *)arg);
1208 default:
1209 return -ENOIOCTLCMD;
1210 }
1211}
1212
1213static void r3964_set_termios(struct tty_struct *tty, struct ktermios *old)
1214{
1215 TRACE_L("set_termios");
1216}
1217
1218/* Called without the kernel lock held - fine */
1219static unsigned int r3964_poll(struct tty_struct *tty, struct file *file,
1220 struct poll_table_struct *wait)
1221{
1222 struct r3964_info *pInfo = tty->disc_data;
1223 struct r3964_client_info *pClient;
1224 struct r3964_message *pMsg = NULL;
1225 unsigned long flags;
1226 int result = POLLOUT;
1227
1228 TRACE_L("POLL");
1229
1230 pClient = findClient(pInfo, task_pid(current));
1231 if (pClient) {
1232 poll_wait(file, &pInfo->read_wait, wait);
1233 spin_lock_irqsave(&pInfo->lock, flags);
1234 pMsg = pClient->first_msg;
1235 spin_unlock_irqrestore(&pInfo->lock, flags);
1236 if (pMsg)
1237 result |= POLLIN | POLLRDNORM;
1238 } else {
1239 result = -EINVAL;
1240 }
1241 return result;
1242}
1243
1244static void r3964_receive_buf(struct tty_struct *tty, const unsigned char *cp,
1245 char *fp, int count)
1246{
1247 struct r3964_info *pInfo = tty->disc_data;
1248 const unsigned char *p;
1249 char *f, flags = 0;
1250 int i;
1251
1252 for (i = count, p = cp, f = fp; i; i--, p++) {
1253 if (f)
1254 flags = *f++;
1255 if (flags == TTY_NORMAL) {
1256 receive_char(pInfo, *p);
1257 } else {
1258 receive_error(pInfo, flags);
1259 }
1260
1261 }
1262}
1263
1264MODULE_LICENSE("GPL");
1265MODULE_ALIAS_LDISC(N_R3964);
diff --git a/drivers/char/n_tty.c b/drivers/char/n_tty.c
deleted file mode 100644
index 428f4fe0b5f7..000000000000
--- a/drivers/char/n_tty.c
+++ /dev/null
@@ -1,2121 +0,0 @@
1/*
2 * n_tty.c --- implements the N_TTY line discipline.
3 *
4 * This code used to be in tty_io.c, but things are getting hairy
5 * enough that it made sense to split things off. (The N_TTY
6 * processing has changed so much that it's hardly recognizable,
7 * anyway...)
8 *
9 * Note that the open routine for N_TTY is guaranteed never to return
10 * an error. This is because Linux will fall back to setting a line
11 * to N_TTY if it can not switch to any other line discipline.
12 *
13 * Written by Theodore Ts'o, Copyright 1994.
14 *
15 * This file also contains code originally written by Linus Torvalds,
16 * Copyright 1991, 1992, 1993, and by Julian Cowley, Copyright 1994.
17 *
18 * This file may be redistributed under the terms of the GNU General Public
19 * License.
20 *
21 * Reduced memory usage for older ARM systems - Russell King.
22 *
23 * 2000/01/20 Fixed SMP locking on put_tty_queue using bits of
24 * the patch by Andrew J. Kroll <ag784@freenet.buffalo.edu>
25 * who actually finally proved there really was a race.
26 *
27 * 2002/03/18 Implemented n_tty_wakeup to send SIGIO POLL_OUTs to
28 * waiting writing processes-Sapan Bhatia <sapan@corewars.org>.
29 * Also fixed a bug in BLOCKING mode where n_tty_write returns
30 * EAGAIN
31 */
32
33#include <linux/types.h>
34#include <linux/major.h>
35#include <linux/errno.h>
36#include <linux/signal.h>
37#include <linux/fcntl.h>
38#include <linux/sched.h>
39#include <linux/interrupt.h>
40#include <linux/tty.h>
41#include <linux/timer.h>
42#include <linux/ctype.h>
43#include <linux/mm.h>
44#include <linux/string.h>
45#include <linux/slab.h>
46#include <linux/poll.h>
47#include <linux/bitops.h>
48#include <linux/audit.h>
49#include <linux/file.h>
50#include <linux/uaccess.h>
51#include <linux/module.h>
52
53#include <asm/system.h>
54
55/* number of characters left in xmit buffer before select has we have room */
56#define WAKEUP_CHARS 256
57
58/*
59 * This defines the low- and high-watermarks for throttling and
60 * unthrottling the TTY driver. These watermarks are used for
61 * controlling the space in the read buffer.
62 */
63#define TTY_THRESHOLD_THROTTLE 128 /* now based on remaining room */
64#define TTY_THRESHOLD_UNTHROTTLE 128
65
66/*
67 * Special byte codes used in the echo buffer to represent operations
68 * or special handling of characters. Bytes in the echo buffer that
69 * are not part of such special blocks are treated as normal character
70 * codes.
71 */
72#define ECHO_OP_START 0xff
73#define ECHO_OP_MOVE_BACK_COL 0x80
74#define ECHO_OP_SET_CANON_COL 0x81
75#define ECHO_OP_ERASE_TAB 0x82
76
77static inline int tty_put_user(struct tty_struct *tty, unsigned char x,
78 unsigned char __user *ptr)
79{
80 tty_audit_add_data(tty, &x, 1);
81 return put_user(x, ptr);
82}
83
84/**
85 * n_tty_set__room - receive space
86 * @tty: terminal
87 *
88 * Called by the driver to find out how much data it is
89 * permitted to feed to the line discipline without any being lost
90 * and thus to manage flow control. Not serialized. Answers for the
91 * "instant".
92 */
93
94static void n_tty_set_room(struct tty_struct *tty)
95{
96 /* tty->read_cnt is not read locked ? */
97 int left = N_TTY_BUF_SIZE - tty->read_cnt - 1;
98
99 /*
100 * If we are doing input canonicalization, and there are no
101 * pending newlines, let characters through without limit, so
102 * that erase characters will be handled. Other excess
103 * characters will be beeped.
104 */
105 if (left <= 0)
106 left = tty->icanon && !tty->canon_data;
107 tty->receive_room = left;
108}
109
110static void put_tty_queue_nolock(unsigned char c, struct tty_struct *tty)
111{
112 if (tty->read_cnt < N_TTY_BUF_SIZE) {
113 tty->read_buf[tty->read_head] = c;
114 tty->read_head = (tty->read_head + 1) & (N_TTY_BUF_SIZE-1);
115 tty->read_cnt++;
116 }
117}
118
119/**
120 * put_tty_queue - add character to tty
121 * @c: character
122 * @tty: tty device
123 *
124 * Add a character to the tty read_buf queue. This is done under the
125 * read_lock to serialize character addition and also to protect us
126 * against parallel reads or flushes
127 */
128
129static void put_tty_queue(unsigned char c, struct tty_struct *tty)
130{
131 unsigned long flags;
132 /*
133 * The problem of stomping on the buffers ends here.
134 * Why didn't anyone see this one coming? --AJK
135 */
136 spin_lock_irqsave(&tty->read_lock, flags);
137 put_tty_queue_nolock(c, tty);
138 spin_unlock_irqrestore(&tty->read_lock, flags);
139}
140
141/**
142 * check_unthrottle - allow new receive data
143 * @tty; tty device
144 *
145 * Check whether to call the driver unthrottle functions
146 *
147 * Can sleep, may be called under the atomic_read_lock mutex but
148 * this is not guaranteed.
149 */
150static void check_unthrottle(struct tty_struct *tty)
151{
152 if (tty->count)
153 tty_unthrottle(tty);
154}
155
156/**
157 * reset_buffer_flags - reset buffer state
158 * @tty: terminal to reset
159 *
160 * Reset the read buffer counters, clear the flags,
161 * and make sure the driver is unthrottled. Called
162 * from n_tty_open() and n_tty_flush_buffer().
163 *
164 * Locking: tty_read_lock for read fields.
165 */
166
167static void reset_buffer_flags(struct tty_struct *tty)
168{
169 unsigned long flags;
170
171 spin_lock_irqsave(&tty->read_lock, flags);
172 tty->read_head = tty->read_tail = tty->read_cnt = 0;
173 spin_unlock_irqrestore(&tty->read_lock, flags);
174
175 mutex_lock(&tty->echo_lock);
176 tty->echo_pos = tty->echo_cnt = tty->echo_overrun = 0;
177 mutex_unlock(&tty->echo_lock);
178
179 tty->canon_head = tty->canon_data = tty->erasing = 0;
180 memset(&tty->read_flags, 0, sizeof tty->read_flags);
181 n_tty_set_room(tty);
182 check_unthrottle(tty);
183}
184
185/**
186 * n_tty_flush_buffer - clean input queue
187 * @tty: terminal device
188 *
189 * Flush the input buffer. Called when the line discipline is
190 * being closed, when the tty layer wants the buffer flushed (eg
191 * at hangup) or when the N_TTY line discipline internally has to
192 * clean the pending queue (for example some signals).
193 *
194 * Locking: ctrl_lock, read_lock.
195 */
196
197static void n_tty_flush_buffer(struct tty_struct *tty)
198{
199 unsigned long flags;
200 /* clear everything and unthrottle the driver */
201 reset_buffer_flags(tty);
202
203 if (!tty->link)
204 return;
205
206 spin_lock_irqsave(&tty->ctrl_lock, flags);
207 if (tty->link->packet) {
208 tty->ctrl_status |= TIOCPKT_FLUSHREAD;
209 wake_up_interruptible(&tty->link->read_wait);
210 }
211 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
212}
213
214/**
215 * n_tty_chars_in_buffer - report available bytes
216 * @tty: tty device
217 *
218 * Report the number of characters buffered to be delivered to user
219 * at this instant in time.
220 *
221 * Locking: read_lock
222 */
223
224static ssize_t n_tty_chars_in_buffer(struct tty_struct *tty)
225{
226 unsigned long flags;
227 ssize_t n = 0;
228
229 spin_lock_irqsave(&tty->read_lock, flags);
230 if (!tty->icanon) {
231 n = tty->read_cnt;
232 } else if (tty->canon_data) {
233 n = (tty->canon_head > tty->read_tail) ?
234 tty->canon_head - tty->read_tail :
235 tty->canon_head + (N_TTY_BUF_SIZE - tty->read_tail);
236 }
237 spin_unlock_irqrestore(&tty->read_lock, flags);
238 return n;
239}
240
241/**
242 * is_utf8_continuation - utf8 multibyte check
243 * @c: byte to check
244 *
245 * Returns true if the utf8 character 'c' is a multibyte continuation
246 * character. We use this to correctly compute the on screen size
247 * of the character when printing
248 */
249
250static inline int is_utf8_continuation(unsigned char c)
251{
252 return (c & 0xc0) == 0x80;
253}
254
255/**
256 * is_continuation - multibyte check
257 * @c: byte to check
258 *
259 * Returns true if the utf8 character 'c' is a multibyte continuation
260 * character and the terminal is in unicode mode.
261 */
262
263static inline int is_continuation(unsigned char c, struct tty_struct *tty)
264{
265 return I_IUTF8(tty) && is_utf8_continuation(c);
266}
267
268/**
269 * do_output_char - output one character
270 * @c: character (or partial unicode symbol)
271 * @tty: terminal device
272 * @space: space available in tty driver write buffer
273 *
274 * This is a helper function that handles one output character
275 * (including special characters like TAB, CR, LF, etc.),
276 * doing OPOST processing and putting the results in the
277 * tty driver's write buffer.
278 *
279 * Note that Linux currently ignores TABDLY, CRDLY, VTDLY, FFDLY
280 * and NLDLY. They simply aren't relevant in the world today.
281 * If you ever need them, add them here.
282 *
283 * Returns the number of bytes of buffer space used or -1 if
284 * no space left.
285 *
286 * Locking: should be called under the output_lock to protect
287 * the column state and space left in the buffer
288 */
289
290static int do_output_char(unsigned char c, struct tty_struct *tty, int space)
291{
292 int spaces;
293
294 if (!space)
295 return -1;
296
297 switch (c) {
298 case '\n':
299 if (O_ONLRET(tty))
300 tty->column = 0;
301 if (O_ONLCR(tty)) {
302 if (space < 2)
303 return -1;
304 tty->canon_column = tty->column = 0;
305 tty->ops->write(tty, "\r\n", 2);
306 return 2;
307 }
308 tty->canon_column = tty->column;
309 break;
310 case '\r':
311 if (O_ONOCR(tty) && tty->column == 0)
312 return 0;
313 if (O_OCRNL(tty)) {
314 c = '\n';
315 if (O_ONLRET(tty))
316 tty->canon_column = tty->column = 0;
317 break;
318 }
319 tty->canon_column = tty->column = 0;
320 break;
321 case '\t':
322 spaces = 8 - (tty->column & 7);
323 if (O_TABDLY(tty) == XTABS) {
324 if (space < spaces)
325 return -1;
326 tty->column += spaces;
327 tty->ops->write(tty, " ", spaces);
328 return spaces;
329 }
330 tty->column += spaces;
331 break;
332 case '\b':
333 if (tty->column > 0)
334 tty->column--;
335 break;
336 default:
337 if (!iscntrl(c)) {
338 if (O_OLCUC(tty))
339 c = toupper(c);
340 if (!is_continuation(c, tty))
341 tty->column++;
342 }
343 break;
344 }
345
346 tty_put_char(tty, c);
347 return 1;
348}
349
350/**
351 * process_output - output post processor
352 * @c: character (or partial unicode symbol)
353 * @tty: terminal device
354 *
355 * Output one character with OPOST processing.
356 * Returns -1 when the output device is full and the character
357 * must be retried.
358 *
359 * Locking: output_lock to protect column state and space left
360 * (also, this is called from n_tty_write under the
361 * tty layer write lock)
362 */
363
364static int process_output(unsigned char c, struct tty_struct *tty)
365{
366 int space, retval;
367
368 mutex_lock(&tty->output_lock);
369
370 space = tty_write_room(tty);
371 retval = do_output_char(c, tty, space);
372
373 mutex_unlock(&tty->output_lock);
374 if (retval < 0)
375 return -1;
376 else
377 return 0;
378}
379
380/**
381 * process_output_block - block post processor
382 * @tty: terminal device
383 * @buf: character buffer
384 * @nr: number of bytes to output
385 *
386 * Output a block of characters with OPOST processing.
387 * Returns the number of characters output.
388 *
389 * This path is used to speed up block console writes, among other
390 * things when processing blocks of output data. It handles only
391 * the simple cases normally found and helps to generate blocks of
392 * symbols for the console driver and thus improve performance.
393 *
394 * Locking: output_lock to protect column state and space left
395 * (also, this is called from n_tty_write under the
396 * tty layer write lock)
397 */
398
399static ssize_t process_output_block(struct tty_struct *tty,
400 const unsigned char *buf, unsigned int nr)
401{
402 int space;
403 int i;
404 const unsigned char *cp;
405
406 mutex_lock(&tty->output_lock);
407
408 space = tty_write_room(tty);
409 if (!space) {
410 mutex_unlock(&tty->output_lock);
411 return 0;
412 }
413 if (nr > space)
414 nr = space;
415
416 for (i = 0, cp = buf; i < nr; i++, cp++) {
417 unsigned char c = *cp;
418
419 switch (c) {
420 case '\n':
421 if (O_ONLRET(tty))
422 tty->column = 0;
423 if (O_ONLCR(tty))
424 goto break_out;
425 tty->canon_column = tty->column;
426 break;
427 case '\r':
428 if (O_ONOCR(tty) && tty->column == 0)
429 goto break_out;
430 if (O_OCRNL(tty))
431 goto break_out;
432 tty->canon_column = tty->column = 0;
433 break;
434 case '\t':
435 goto break_out;
436 case '\b':
437 if (tty->column > 0)
438 tty->column--;
439 break;
440 default:
441 if (!iscntrl(c)) {
442 if (O_OLCUC(tty))
443 goto break_out;
444 if (!is_continuation(c, tty))
445 tty->column++;
446 }
447 break;
448 }
449 }
450break_out:
451 i = tty->ops->write(tty, buf, i);
452
453 mutex_unlock(&tty->output_lock);
454 return i;
455}
456
457/**
458 * process_echoes - write pending echo characters
459 * @tty: terminal device
460 *
461 * Write previously buffered echo (and other ldisc-generated)
462 * characters to the tty.
463 *
464 * Characters generated by the ldisc (including echoes) need to
465 * be buffered because the driver's write buffer can fill during
466 * heavy program output. Echoing straight to the driver will
467 * often fail under these conditions, causing lost characters and
468 * resulting mismatches of ldisc state information.
469 *
470 * Since the ldisc state must represent the characters actually sent
471 * to the driver at the time of the write, operations like certain
472 * changes in column state are also saved in the buffer and executed
473 * here.
474 *
475 * A circular fifo buffer is used so that the most recent characters
476 * are prioritized. Also, when control characters are echoed with a
477 * prefixed "^", the pair is treated atomically and thus not separated.
478 *
479 * Locking: output_lock to protect column state and space left,
480 * echo_lock to protect the echo buffer
481 */
482
483static void process_echoes(struct tty_struct *tty)
484{
485 int space, nr;
486 unsigned char c;
487 unsigned char *cp, *buf_end;
488
489 if (!tty->echo_cnt)
490 return;
491
492 mutex_lock(&tty->output_lock);
493 mutex_lock(&tty->echo_lock);
494
495 space = tty_write_room(tty);
496
497 buf_end = tty->echo_buf + N_TTY_BUF_SIZE;
498 cp = tty->echo_buf + tty->echo_pos;
499 nr = tty->echo_cnt;
500 while (nr > 0) {
501 c = *cp;
502 if (c == ECHO_OP_START) {
503 unsigned char op;
504 unsigned char *opp;
505 int no_space_left = 0;
506
507 /*
508 * If the buffer byte is the start of a multi-byte
509 * operation, get the next byte, which is either the
510 * op code or a control character value.
511 */
512 opp = cp + 1;
513 if (opp == buf_end)
514 opp -= N_TTY_BUF_SIZE;
515 op = *opp;
516
517 switch (op) {
518 unsigned int num_chars, num_bs;
519
520 case ECHO_OP_ERASE_TAB:
521 if (++opp == buf_end)
522 opp -= N_TTY_BUF_SIZE;
523 num_chars = *opp;
524
525 /*
526 * Determine how many columns to go back
527 * in order to erase the tab.
528 * This depends on the number of columns
529 * used by other characters within the tab
530 * area. If this (modulo 8) count is from
531 * the start of input rather than from a
532 * previous tab, we offset by canon column.
533 * Otherwise, tab spacing is normal.
534 */
535 if (!(num_chars & 0x80))
536 num_chars += tty->canon_column;
537 num_bs = 8 - (num_chars & 7);
538
539 if (num_bs > space) {
540 no_space_left = 1;
541 break;
542 }
543 space -= num_bs;
544 while (num_bs--) {
545 tty_put_char(tty, '\b');
546 if (tty->column > 0)
547 tty->column--;
548 }
549 cp += 3;
550 nr -= 3;
551 break;
552
553 case ECHO_OP_SET_CANON_COL:
554 tty->canon_column = tty->column;
555 cp += 2;
556 nr -= 2;
557 break;
558
559 case ECHO_OP_MOVE_BACK_COL:
560 if (tty->column > 0)
561 tty->column--;
562 cp += 2;
563 nr -= 2;
564 break;
565
566 case ECHO_OP_START:
567 /* This is an escaped echo op start code */
568 if (!space) {
569 no_space_left = 1;
570 break;
571 }
572 tty_put_char(tty, ECHO_OP_START);
573 tty->column++;
574 space--;
575 cp += 2;
576 nr -= 2;
577 break;
578
579 default:
580 /*
581 * If the op is not a special byte code,
582 * it is a ctrl char tagged to be echoed
583 * as "^X" (where X is the letter
584 * representing the control char).
585 * Note that we must ensure there is
586 * enough space for the whole ctrl pair.
587 *
588 */
589 if (space < 2) {
590 no_space_left = 1;
591 break;
592 }
593 tty_put_char(tty, '^');
594 tty_put_char(tty, op ^ 0100);
595 tty->column += 2;
596 space -= 2;
597 cp += 2;
598 nr -= 2;
599 }
600
601 if (no_space_left)
602 break;
603 } else {
604 if (O_OPOST(tty) &&
605 !(test_bit(TTY_HW_COOK_OUT, &tty->flags))) {
606 int retval = do_output_char(c, tty, space);
607 if (retval < 0)
608 break;
609 space -= retval;
610 } else {
611 if (!space)
612 break;
613 tty_put_char(tty, c);
614 space -= 1;
615 }
616 cp += 1;
617 nr -= 1;
618 }
619
620 /* When end of circular buffer reached, wrap around */
621 if (cp >= buf_end)
622 cp -= N_TTY_BUF_SIZE;
623 }
624
625 if (nr == 0) {
626 tty->echo_pos = 0;
627 tty->echo_cnt = 0;
628 tty->echo_overrun = 0;
629 } else {
630 int num_processed = tty->echo_cnt - nr;
631 tty->echo_pos += num_processed;
632 tty->echo_pos &= N_TTY_BUF_SIZE - 1;
633 tty->echo_cnt = nr;
634 if (num_processed > 0)
635 tty->echo_overrun = 0;
636 }
637
638 mutex_unlock(&tty->echo_lock);
639 mutex_unlock(&tty->output_lock);
640
641 if (tty->ops->flush_chars)
642 tty->ops->flush_chars(tty);
643}
644
645/**
646 * add_echo_byte - add a byte to the echo buffer
647 * @c: unicode byte to echo
648 * @tty: terminal device
649 *
650 * Add a character or operation byte to the echo buffer.
651 *
652 * Should be called under the echo lock to protect the echo buffer.
653 */
654
655static void add_echo_byte(unsigned char c, struct tty_struct *tty)
656{
657 int new_byte_pos;
658
659 if (tty->echo_cnt == N_TTY_BUF_SIZE) {
660 /* Circular buffer is already at capacity */
661 new_byte_pos = tty->echo_pos;
662
663 /*
664 * Since the buffer start position needs to be advanced,
665 * be sure to step by a whole operation byte group.
666 */
667 if (tty->echo_buf[tty->echo_pos] == ECHO_OP_START) {
668 if (tty->echo_buf[(tty->echo_pos + 1) &
669 (N_TTY_BUF_SIZE - 1)] ==
670 ECHO_OP_ERASE_TAB) {
671 tty->echo_pos += 3;
672 tty->echo_cnt -= 2;
673 } else {
674 tty->echo_pos += 2;
675 tty->echo_cnt -= 1;
676 }
677 } else {
678 tty->echo_pos++;
679 }
680 tty->echo_pos &= N_TTY_BUF_SIZE - 1;
681
682 tty->echo_overrun = 1;
683 } else {
684 new_byte_pos = tty->echo_pos + tty->echo_cnt;
685 new_byte_pos &= N_TTY_BUF_SIZE - 1;
686 tty->echo_cnt++;
687 }
688
689 tty->echo_buf[new_byte_pos] = c;
690}
691
692/**
693 * echo_move_back_col - add operation to move back a column
694 * @tty: terminal device
695 *
696 * Add an operation to the echo buffer to move back one column.
697 *
698 * Locking: echo_lock to protect the echo buffer
699 */
700
701static void echo_move_back_col(struct tty_struct *tty)
702{
703 mutex_lock(&tty->echo_lock);
704
705 add_echo_byte(ECHO_OP_START, tty);
706 add_echo_byte(ECHO_OP_MOVE_BACK_COL, tty);
707
708 mutex_unlock(&tty->echo_lock);
709}
710
711/**
712 * echo_set_canon_col - add operation to set the canon column
713 * @tty: terminal device
714 *
715 * Add an operation to the echo buffer to set the canon column
716 * to the current column.
717 *
718 * Locking: echo_lock to protect the echo buffer
719 */
720
721static void echo_set_canon_col(struct tty_struct *tty)
722{
723 mutex_lock(&tty->echo_lock);
724
725 add_echo_byte(ECHO_OP_START, tty);
726 add_echo_byte(ECHO_OP_SET_CANON_COL, tty);
727
728 mutex_unlock(&tty->echo_lock);
729}
730
731/**
732 * echo_erase_tab - add operation to erase a tab
733 * @num_chars: number of character columns already used
734 * @after_tab: true if num_chars starts after a previous tab
735 * @tty: terminal device
736 *
737 * Add an operation to the echo buffer to erase a tab.
738 *
739 * Called by the eraser function, which knows how many character
740 * columns have been used since either a previous tab or the start
741 * of input. This information will be used later, along with
742 * canon column (if applicable), to go back the correct number
743 * of columns.
744 *
745 * Locking: echo_lock to protect the echo buffer
746 */
747
748static void echo_erase_tab(unsigned int num_chars, int after_tab,
749 struct tty_struct *tty)
750{
751 mutex_lock(&tty->echo_lock);
752
753 add_echo_byte(ECHO_OP_START, tty);
754 add_echo_byte(ECHO_OP_ERASE_TAB, tty);
755
756 /* We only need to know this modulo 8 (tab spacing) */
757 num_chars &= 7;
758
759 /* Set the high bit as a flag if num_chars is after a previous tab */
760 if (after_tab)
761 num_chars |= 0x80;
762
763 add_echo_byte(num_chars, tty);
764
765 mutex_unlock(&tty->echo_lock);
766}
767
768/**
769 * echo_char_raw - echo a character raw
770 * @c: unicode byte to echo
771 * @tty: terminal device
772 *
773 * Echo user input back onto the screen. This must be called only when
774 * L_ECHO(tty) is true. Called from the driver receive_buf path.
775 *
776 * This variant does not treat control characters specially.
777 *
778 * Locking: echo_lock to protect the echo buffer
779 */
780
781static void echo_char_raw(unsigned char c, struct tty_struct *tty)
782{
783 mutex_lock(&tty->echo_lock);
784
785 if (c == ECHO_OP_START) {
786 add_echo_byte(ECHO_OP_START, tty);
787 add_echo_byte(ECHO_OP_START, tty);
788 } else {
789 add_echo_byte(c, tty);
790 }
791
792 mutex_unlock(&tty->echo_lock);
793}
794
795/**
796 * echo_char - echo a character
797 * @c: unicode byte to echo
798 * @tty: terminal device
799 *
800 * Echo user input back onto the screen. This must be called only when
801 * L_ECHO(tty) is true. Called from the driver receive_buf path.
802 *
803 * This variant tags control characters to be echoed as "^X"
804 * (where X is the letter representing the control char).
805 *
806 * Locking: echo_lock to protect the echo buffer
807 */
808
809static void echo_char(unsigned char c, struct tty_struct *tty)
810{
811 mutex_lock(&tty->echo_lock);
812
813 if (c == ECHO_OP_START) {
814 add_echo_byte(ECHO_OP_START, tty);
815 add_echo_byte(ECHO_OP_START, tty);
816 } else {
817 if (L_ECHOCTL(tty) && iscntrl(c) && c != '\t')
818 add_echo_byte(ECHO_OP_START, tty);
819 add_echo_byte(c, tty);
820 }
821
822 mutex_unlock(&tty->echo_lock);
823}
824
825/**
826 * finish_erasing - complete erase
827 * @tty: tty doing the erase
828 */
829
830static inline void finish_erasing(struct tty_struct *tty)
831{
832 if (tty->erasing) {
833 echo_char_raw('/', tty);
834 tty->erasing = 0;
835 }
836}
837
838/**
839 * eraser - handle erase function
840 * @c: character input
841 * @tty: terminal device
842 *
843 * Perform erase and necessary output when an erase character is
844 * present in the stream from the driver layer. Handles the complexities
845 * of UTF-8 multibyte symbols.
846 *
847 * Locking: read_lock for tty buffers
848 */
849
850static void eraser(unsigned char c, struct tty_struct *tty)
851{
852 enum { ERASE, WERASE, KILL } kill_type;
853 int head, seen_alnums, cnt;
854 unsigned long flags;
855
856 /* FIXME: locking needed ? */
857 if (tty->read_head == tty->canon_head) {
858 /* process_output('\a', tty); */ /* what do you think? */
859 return;
860 }
861 if (c == ERASE_CHAR(tty))
862 kill_type = ERASE;
863 else if (c == WERASE_CHAR(tty))
864 kill_type = WERASE;
865 else {
866 if (!L_ECHO(tty)) {
867 spin_lock_irqsave(&tty->read_lock, flags);
868 tty->read_cnt -= ((tty->read_head - tty->canon_head) &
869 (N_TTY_BUF_SIZE - 1));
870 tty->read_head = tty->canon_head;
871 spin_unlock_irqrestore(&tty->read_lock, flags);
872 return;
873 }
874 if (!L_ECHOK(tty) || !L_ECHOKE(tty) || !L_ECHOE(tty)) {
875 spin_lock_irqsave(&tty->read_lock, flags);
876 tty->read_cnt -= ((tty->read_head - tty->canon_head) &
877 (N_TTY_BUF_SIZE - 1));
878 tty->read_head = tty->canon_head;
879 spin_unlock_irqrestore(&tty->read_lock, flags);
880 finish_erasing(tty);
881 echo_char(KILL_CHAR(tty), tty);
882 /* Add a newline if ECHOK is on and ECHOKE is off. */
883 if (L_ECHOK(tty))
884 echo_char_raw('\n', tty);
885 return;
886 }
887 kill_type = KILL;
888 }
889
890 seen_alnums = 0;
891 /* FIXME: Locking ?? */
892 while (tty->read_head != tty->canon_head) {
893 head = tty->read_head;
894
895 /* erase a single possibly multibyte character */
896 do {
897 head = (head - 1) & (N_TTY_BUF_SIZE-1);
898 c = tty->read_buf[head];
899 } while (is_continuation(c, tty) && head != tty->canon_head);
900
901 /* do not partially erase */
902 if (is_continuation(c, tty))
903 break;
904
905 if (kill_type == WERASE) {
906 /* Equivalent to BSD's ALTWERASE. */
907 if (isalnum(c) || c == '_')
908 seen_alnums++;
909 else if (seen_alnums)
910 break;
911 }
912 cnt = (tty->read_head - head) & (N_TTY_BUF_SIZE-1);
913 spin_lock_irqsave(&tty->read_lock, flags);
914 tty->read_head = head;
915 tty->read_cnt -= cnt;
916 spin_unlock_irqrestore(&tty->read_lock, flags);
917 if (L_ECHO(tty)) {
918 if (L_ECHOPRT(tty)) {
919 if (!tty->erasing) {
920 echo_char_raw('\\', tty);
921 tty->erasing = 1;
922 }
923 /* if cnt > 1, output a multi-byte character */
924 echo_char(c, tty);
925 while (--cnt > 0) {
926 head = (head+1) & (N_TTY_BUF_SIZE-1);
927 echo_char_raw(tty->read_buf[head], tty);
928 echo_move_back_col(tty);
929 }
930 } else if (kill_type == ERASE && !L_ECHOE(tty)) {
931 echo_char(ERASE_CHAR(tty), tty);
932 } else if (c == '\t') {
933 unsigned int num_chars = 0;
934 int after_tab = 0;
935 unsigned long tail = tty->read_head;
936
937 /*
938 * Count the columns used for characters
939 * since the start of input or after a
940 * previous tab.
941 * This info is used to go back the correct
942 * number of columns.
943 */
944 while (tail != tty->canon_head) {
945 tail = (tail-1) & (N_TTY_BUF_SIZE-1);
946 c = tty->read_buf[tail];
947 if (c == '\t') {
948 after_tab = 1;
949 break;
950 } else if (iscntrl(c)) {
951 if (L_ECHOCTL(tty))
952 num_chars += 2;
953 } else if (!is_continuation(c, tty)) {
954 num_chars++;
955 }
956 }
957 echo_erase_tab(num_chars, after_tab, tty);
958 } else {
959 if (iscntrl(c) && L_ECHOCTL(tty)) {
960 echo_char_raw('\b', tty);
961 echo_char_raw(' ', tty);
962 echo_char_raw('\b', tty);
963 }
964 if (!iscntrl(c) || L_ECHOCTL(tty)) {
965 echo_char_raw('\b', tty);
966 echo_char_raw(' ', tty);
967 echo_char_raw('\b', tty);
968 }
969 }
970 }
971 if (kill_type == ERASE)
972 break;
973 }
974 if (tty->read_head == tty->canon_head && L_ECHO(tty))
975 finish_erasing(tty);
976}
977
978/**
979 * isig - handle the ISIG optio
980 * @sig: signal
981 * @tty: terminal
982 * @flush: force flush
983 *
984 * Called when a signal is being sent due to terminal input. This
985 * may caus terminal flushing to take place according to the termios
986 * settings and character used. Called from the driver receive_buf
987 * path so serialized.
988 *
989 * Locking: ctrl_lock, read_lock (both via flush buffer)
990 */
991
992static inline void isig(int sig, struct tty_struct *tty, int flush)
993{
994 if (tty->pgrp)
995 kill_pgrp(tty->pgrp, sig, 1);
996 if (flush || !L_NOFLSH(tty)) {
997 n_tty_flush_buffer(tty);
998 tty_driver_flush_buffer(tty);
999 }
1000}
1001
1002/**
1003 * n_tty_receive_break - handle break
1004 * @tty: terminal
1005 *
1006 * An RS232 break event has been hit in the incoming bitstream. This
1007 * can cause a variety of events depending upon the termios settings.
1008 *
1009 * Called from the receive_buf path so single threaded.
1010 */
1011
1012static inline void n_tty_receive_break(struct tty_struct *tty)
1013{
1014 if (I_IGNBRK(tty))
1015 return;
1016 if (I_BRKINT(tty)) {
1017 isig(SIGINT, tty, 1);
1018 return;
1019 }
1020 if (I_PARMRK(tty)) {
1021 put_tty_queue('\377', tty);
1022 put_tty_queue('\0', tty);
1023 }
1024 put_tty_queue('\0', tty);
1025 wake_up_interruptible(&tty->read_wait);
1026}
1027
1028/**
1029 * n_tty_receive_overrun - handle overrun reporting
1030 * @tty: terminal
1031 *
1032 * Data arrived faster than we could process it. While the tty
1033 * driver has flagged this the bits that were missed are gone
1034 * forever.
1035 *
1036 * Called from the receive_buf path so single threaded. Does not
1037 * need locking as num_overrun and overrun_time are function
1038 * private.
1039 */
1040
1041static inline void n_tty_receive_overrun(struct tty_struct *tty)
1042{
1043 char buf[64];
1044
1045 tty->num_overrun++;
1046 if (time_before(tty->overrun_time, jiffies - HZ) ||
1047 time_after(tty->overrun_time, jiffies)) {
1048 printk(KERN_WARNING "%s: %d input overrun(s)\n",
1049 tty_name(tty, buf),
1050 tty->num_overrun);
1051 tty->overrun_time = jiffies;
1052 tty->num_overrun = 0;
1053 }
1054}
1055
1056/**
1057 * n_tty_receive_parity_error - error notifier
1058 * @tty: terminal device
1059 * @c: character
1060 *
1061 * Process a parity error and queue the right data to indicate
1062 * the error case if necessary. Locking as per n_tty_receive_buf.
1063 */
1064static inline void n_tty_receive_parity_error(struct tty_struct *tty,
1065 unsigned char c)
1066{
1067 if (I_IGNPAR(tty))
1068 return;
1069 if (I_PARMRK(tty)) {
1070 put_tty_queue('\377', tty);
1071 put_tty_queue('\0', tty);
1072 put_tty_queue(c, tty);
1073 } else if (I_INPCK(tty))
1074 put_tty_queue('\0', tty);
1075 else
1076 put_tty_queue(c, tty);
1077 wake_up_interruptible(&tty->read_wait);
1078}
1079
1080/**
1081 * n_tty_receive_char - perform processing
1082 * @tty: terminal device
1083 * @c: character
1084 *
1085 * Process an individual character of input received from the driver.
1086 * This is serialized with respect to itself by the rules for the
1087 * driver above.
1088 */
1089
1090static inline void n_tty_receive_char(struct tty_struct *tty, unsigned char c)
1091{
1092 unsigned long flags;
1093 int parmrk;
1094
1095 if (tty->raw) {
1096 put_tty_queue(c, tty);
1097 return;
1098 }
1099
1100 if (I_ISTRIP(tty))
1101 c &= 0x7f;
1102 if (I_IUCLC(tty) && L_IEXTEN(tty))
1103 c = tolower(c);
1104
1105 if (L_EXTPROC(tty)) {
1106 put_tty_queue(c, tty);
1107 return;
1108 }
1109
1110 if (tty->stopped && !tty->flow_stopped && I_IXON(tty) &&
1111 I_IXANY(tty) && c != START_CHAR(tty) && c != STOP_CHAR(tty) &&
1112 c != INTR_CHAR(tty) && c != QUIT_CHAR(tty) && c != SUSP_CHAR(tty)) {
1113 start_tty(tty);
1114 process_echoes(tty);
1115 }
1116
1117 if (tty->closing) {
1118 if (I_IXON(tty)) {
1119 if (c == START_CHAR(tty)) {
1120 start_tty(tty);
1121 process_echoes(tty);
1122 } else if (c == STOP_CHAR(tty))
1123 stop_tty(tty);
1124 }
1125 return;
1126 }
1127
1128 /*
1129 * If the previous character was LNEXT, or we know that this
1130 * character is not one of the characters that we'll have to
1131 * handle specially, do shortcut processing to speed things
1132 * up.
1133 */
1134 if (!test_bit(c, tty->process_char_map) || tty->lnext) {
1135 tty->lnext = 0;
1136 parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty)) ? 1 : 0;
1137 if (tty->read_cnt >= (N_TTY_BUF_SIZE - parmrk - 1)) {
1138 /* beep if no space */
1139 if (L_ECHO(tty))
1140 process_output('\a', tty);
1141 return;
1142 }
1143 if (L_ECHO(tty)) {
1144 finish_erasing(tty);
1145 /* Record the column of first canon char. */
1146 if (tty->canon_head == tty->read_head)
1147 echo_set_canon_col(tty);
1148 echo_char(c, tty);
1149 process_echoes(tty);
1150 }
1151 if (parmrk)
1152 put_tty_queue(c, tty);
1153 put_tty_queue(c, tty);
1154 return;
1155 }
1156
1157 if (I_IXON(tty)) {
1158 if (c == START_CHAR(tty)) {
1159 start_tty(tty);
1160 process_echoes(tty);
1161 return;
1162 }
1163 if (c == STOP_CHAR(tty)) {
1164 stop_tty(tty);
1165 return;
1166 }
1167 }
1168
1169 if (L_ISIG(tty)) {
1170 int signal;
1171 signal = SIGINT;
1172 if (c == INTR_CHAR(tty))
1173 goto send_signal;
1174 signal = SIGQUIT;
1175 if (c == QUIT_CHAR(tty))
1176 goto send_signal;
1177 signal = SIGTSTP;
1178 if (c == SUSP_CHAR(tty)) {
1179send_signal:
1180 /*
1181 * Note that we do not use isig() here because we want
1182 * the order to be:
1183 * 1) flush, 2) echo, 3) signal
1184 */
1185 if (!L_NOFLSH(tty)) {
1186 n_tty_flush_buffer(tty);
1187 tty_driver_flush_buffer(tty);
1188 }
1189 if (I_IXON(tty))
1190 start_tty(tty);
1191 if (L_ECHO(tty)) {
1192 echo_char(c, tty);
1193 process_echoes(tty);
1194 }
1195 if (tty->pgrp)
1196 kill_pgrp(tty->pgrp, signal, 1);
1197 return;
1198 }
1199 }
1200
1201 if (c == '\r') {
1202 if (I_IGNCR(tty))
1203 return;
1204 if (I_ICRNL(tty))
1205 c = '\n';
1206 } else if (c == '\n' && I_INLCR(tty))
1207 c = '\r';
1208
1209 if (tty->icanon) {
1210 if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) ||
1211 (c == WERASE_CHAR(tty) && L_IEXTEN(tty))) {
1212 eraser(c, tty);
1213 process_echoes(tty);
1214 return;
1215 }
1216 if (c == LNEXT_CHAR(tty) && L_IEXTEN(tty)) {
1217 tty->lnext = 1;
1218 if (L_ECHO(tty)) {
1219 finish_erasing(tty);
1220 if (L_ECHOCTL(tty)) {
1221 echo_char_raw('^', tty);
1222 echo_char_raw('\b', tty);
1223 process_echoes(tty);
1224 }
1225 }
1226 return;
1227 }
1228 if (c == REPRINT_CHAR(tty) && L_ECHO(tty) &&
1229 L_IEXTEN(tty)) {
1230 unsigned long tail = tty->canon_head;
1231
1232 finish_erasing(tty);
1233 echo_char(c, tty);
1234 echo_char_raw('\n', tty);
1235 while (tail != tty->read_head) {
1236 echo_char(tty->read_buf[tail], tty);
1237 tail = (tail+1) & (N_TTY_BUF_SIZE-1);
1238 }
1239 process_echoes(tty);
1240 return;
1241 }
1242 if (c == '\n') {
1243 if (tty->read_cnt >= N_TTY_BUF_SIZE) {
1244 if (L_ECHO(tty))
1245 process_output('\a', tty);
1246 return;
1247 }
1248 if (L_ECHO(tty) || L_ECHONL(tty)) {
1249 echo_char_raw('\n', tty);
1250 process_echoes(tty);
1251 }
1252 goto handle_newline;
1253 }
1254 if (c == EOF_CHAR(tty)) {
1255 if (tty->read_cnt >= N_TTY_BUF_SIZE)
1256 return;
1257 if (tty->canon_head != tty->read_head)
1258 set_bit(TTY_PUSH, &tty->flags);
1259 c = __DISABLED_CHAR;
1260 goto handle_newline;
1261 }
1262 if ((c == EOL_CHAR(tty)) ||
1263 (c == EOL2_CHAR(tty) && L_IEXTEN(tty))) {
1264 parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty))
1265 ? 1 : 0;
1266 if (tty->read_cnt >= (N_TTY_BUF_SIZE - parmrk)) {
1267 if (L_ECHO(tty))
1268 process_output('\a', tty);
1269 return;
1270 }
1271 /*
1272 * XXX are EOL_CHAR and EOL2_CHAR echoed?!?
1273 */
1274 if (L_ECHO(tty)) {
1275 /* Record the column of first canon char. */
1276 if (tty->canon_head == tty->read_head)
1277 echo_set_canon_col(tty);
1278 echo_char(c, tty);
1279 process_echoes(tty);
1280 }
1281 /*
1282 * XXX does PARMRK doubling happen for
1283 * EOL_CHAR and EOL2_CHAR?
1284 */
1285 if (parmrk)
1286 put_tty_queue(c, tty);
1287
1288handle_newline:
1289 spin_lock_irqsave(&tty->read_lock, flags);
1290 set_bit(tty->read_head, tty->read_flags);
1291 put_tty_queue_nolock(c, tty);
1292 tty->canon_head = tty->read_head;
1293 tty->canon_data++;
1294 spin_unlock_irqrestore(&tty->read_lock, flags);
1295 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
1296 if (waitqueue_active(&tty->read_wait))
1297 wake_up_interruptible(&tty->read_wait);
1298 return;
1299 }
1300 }
1301
1302 parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty)) ? 1 : 0;
1303 if (tty->read_cnt >= (N_TTY_BUF_SIZE - parmrk - 1)) {
1304 /* beep if no space */
1305 if (L_ECHO(tty))
1306 process_output('\a', tty);
1307 return;
1308 }
1309 if (L_ECHO(tty)) {
1310 finish_erasing(tty);
1311 if (c == '\n')
1312 echo_char_raw('\n', tty);
1313 else {
1314 /* Record the column of first canon char. */
1315 if (tty->canon_head == tty->read_head)
1316 echo_set_canon_col(tty);
1317 echo_char(c, tty);
1318 }
1319 process_echoes(tty);
1320 }
1321
1322 if (parmrk)
1323 put_tty_queue(c, tty);
1324
1325 put_tty_queue(c, tty);
1326}
1327
1328
1329/**
1330 * n_tty_write_wakeup - asynchronous I/O notifier
1331 * @tty: tty device
1332 *
1333 * Required for the ptys, serial driver etc. since processes
1334 * that attach themselves to the master and rely on ASYNC
1335 * IO must be woken up
1336 */
1337
1338static void n_tty_write_wakeup(struct tty_struct *tty)
1339{
1340 if (tty->fasync && test_and_clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags))
1341 kill_fasync(&tty->fasync, SIGIO, POLL_OUT);
1342}
1343
1344/**
1345 * n_tty_receive_buf - data receive
1346 * @tty: terminal device
1347 * @cp: buffer
1348 * @fp: flag buffer
1349 * @count: characters
1350 *
1351 * Called by the terminal driver when a block of characters has
1352 * been received. This function must be called from soft contexts
1353 * not from interrupt context. The driver is responsible for making
1354 * calls one at a time and in order (or using flush_to_ldisc)
1355 */
1356
1357static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp,
1358 char *fp, int count)
1359{
1360 const unsigned char *p;
1361 char *f, flags = TTY_NORMAL;
1362 int i;
1363 char buf[64];
1364 unsigned long cpuflags;
1365
1366 if (!tty->read_buf)
1367 return;
1368
1369 if (tty->real_raw) {
1370 spin_lock_irqsave(&tty->read_lock, cpuflags);
1371 i = min(N_TTY_BUF_SIZE - tty->read_cnt,
1372 N_TTY_BUF_SIZE - tty->read_head);
1373 i = min(count, i);
1374 memcpy(tty->read_buf + tty->read_head, cp, i);
1375 tty->read_head = (tty->read_head + i) & (N_TTY_BUF_SIZE-1);
1376 tty->read_cnt += i;
1377 cp += i;
1378 count -= i;
1379
1380 i = min(N_TTY_BUF_SIZE - tty->read_cnt,
1381 N_TTY_BUF_SIZE - tty->read_head);
1382 i = min(count, i);
1383 memcpy(tty->read_buf + tty->read_head, cp, i);
1384 tty->read_head = (tty->read_head + i) & (N_TTY_BUF_SIZE-1);
1385 tty->read_cnt += i;
1386 spin_unlock_irqrestore(&tty->read_lock, cpuflags);
1387 } else {
1388 for (i = count, p = cp, f = fp; i; i--, p++) {
1389 if (f)
1390 flags = *f++;
1391 switch (flags) {
1392 case TTY_NORMAL:
1393 n_tty_receive_char(tty, *p);
1394 break;
1395 case TTY_BREAK:
1396 n_tty_receive_break(tty);
1397 break;
1398 case TTY_PARITY:
1399 case TTY_FRAME:
1400 n_tty_receive_parity_error(tty, *p);
1401 break;
1402 case TTY_OVERRUN:
1403 n_tty_receive_overrun(tty);
1404 break;
1405 default:
1406 printk(KERN_ERR "%s: unknown flag %d\n",
1407 tty_name(tty, buf), flags);
1408 break;
1409 }
1410 }
1411 if (tty->ops->flush_chars)
1412 tty->ops->flush_chars(tty);
1413 }
1414
1415 n_tty_set_room(tty);
1416
1417 if ((!tty->icanon && (tty->read_cnt >= tty->minimum_to_wake)) ||
1418 L_EXTPROC(tty)) {
1419 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
1420 if (waitqueue_active(&tty->read_wait))
1421 wake_up_interruptible(&tty->read_wait);
1422 }
1423
1424 /*
1425 * Check the remaining room for the input canonicalization
1426 * mode. We don't want to throttle the driver if we're in
1427 * canonical mode and don't have a newline yet!
1428 */
1429 if (tty->receive_room < TTY_THRESHOLD_THROTTLE)
1430 tty_throttle(tty);
1431}
1432
1433int is_ignored(int sig)
1434{
1435 return (sigismember(&current->blocked, sig) ||
1436 current->sighand->action[sig-1].sa.sa_handler == SIG_IGN);
1437}
1438
1439/**
1440 * n_tty_set_termios - termios data changed
1441 * @tty: terminal
1442 * @old: previous data
1443 *
1444 * Called by the tty layer when the user changes termios flags so
1445 * that the line discipline can plan ahead. This function cannot sleep
1446 * and is protected from re-entry by the tty layer. The user is
1447 * guaranteed that this function will not be re-entered or in progress
1448 * when the ldisc is closed.
1449 *
1450 * Locking: Caller holds tty->termios_mutex
1451 */
1452
1453static void n_tty_set_termios(struct tty_struct *tty, struct ktermios *old)
1454{
1455 int canon_change = 1;
1456 BUG_ON(!tty);
1457
1458 if (old)
1459 canon_change = (old->c_lflag ^ tty->termios->c_lflag) & ICANON;
1460 if (canon_change) {
1461 memset(&tty->read_flags, 0, sizeof tty->read_flags);
1462 tty->canon_head = tty->read_tail;
1463 tty->canon_data = 0;
1464 tty->erasing = 0;
1465 }
1466
1467 if (canon_change && !L_ICANON(tty) && tty->read_cnt)
1468 wake_up_interruptible(&tty->read_wait);
1469
1470 tty->icanon = (L_ICANON(tty) != 0);
1471 if (test_bit(TTY_HW_COOK_IN, &tty->flags)) {
1472 tty->raw = 1;
1473 tty->real_raw = 1;
1474 n_tty_set_room(tty);
1475 return;
1476 }
1477 if (I_ISTRIP(tty) || I_IUCLC(tty) || I_IGNCR(tty) ||
1478 I_ICRNL(tty) || I_INLCR(tty) || L_ICANON(tty) ||
1479 I_IXON(tty) || L_ISIG(tty) || L_ECHO(tty) ||
1480 I_PARMRK(tty)) {
1481 memset(tty->process_char_map, 0, 256/8);
1482
1483 if (I_IGNCR(tty) || I_ICRNL(tty))
1484 set_bit('\r', tty->process_char_map);
1485 if (I_INLCR(tty))
1486 set_bit('\n', tty->process_char_map);
1487
1488 if (L_ICANON(tty)) {
1489 set_bit(ERASE_CHAR(tty), tty->process_char_map);
1490 set_bit(KILL_CHAR(tty), tty->process_char_map);
1491 set_bit(EOF_CHAR(tty), tty->process_char_map);
1492 set_bit('\n', tty->process_char_map);
1493 set_bit(EOL_CHAR(tty), tty->process_char_map);
1494 if (L_IEXTEN(tty)) {
1495 set_bit(WERASE_CHAR(tty),
1496 tty->process_char_map);
1497 set_bit(LNEXT_CHAR(tty),
1498 tty->process_char_map);
1499 set_bit(EOL2_CHAR(tty),
1500 tty->process_char_map);
1501 if (L_ECHO(tty))
1502 set_bit(REPRINT_CHAR(tty),
1503 tty->process_char_map);
1504 }
1505 }
1506 if (I_IXON(tty)) {
1507 set_bit(START_CHAR(tty), tty->process_char_map);
1508 set_bit(STOP_CHAR(tty), tty->process_char_map);
1509 }
1510 if (L_ISIG(tty)) {
1511 set_bit(INTR_CHAR(tty), tty->process_char_map);
1512 set_bit(QUIT_CHAR(tty), tty->process_char_map);
1513 set_bit(SUSP_CHAR(tty), tty->process_char_map);
1514 }
1515 clear_bit(__DISABLED_CHAR, tty->process_char_map);
1516 tty->raw = 0;
1517 tty->real_raw = 0;
1518 } else {
1519 tty->raw = 1;
1520 if ((I_IGNBRK(tty) || (!I_BRKINT(tty) && !I_PARMRK(tty))) &&
1521 (I_IGNPAR(tty) || !I_INPCK(tty)) &&
1522 (tty->driver->flags & TTY_DRIVER_REAL_RAW))
1523 tty->real_raw = 1;
1524 else
1525 tty->real_raw = 0;
1526 }
1527 n_tty_set_room(tty);
1528 /* The termios change make the tty ready for I/O */
1529 wake_up_interruptible(&tty->write_wait);
1530 wake_up_interruptible(&tty->read_wait);
1531}
1532
1533/**
1534 * n_tty_close - close the ldisc for this tty
1535 * @tty: device
1536 *
1537 * Called from the terminal layer when this line discipline is
1538 * being shut down, either because of a close or becsuse of a
1539 * discipline change. The function will not be called while other
1540 * ldisc methods are in progress.
1541 */
1542
1543static void n_tty_close(struct tty_struct *tty)
1544{
1545 n_tty_flush_buffer(tty);
1546 if (tty->read_buf) {
1547 kfree(tty->read_buf);
1548 tty->read_buf = NULL;
1549 }
1550 if (tty->echo_buf) {
1551 kfree(tty->echo_buf);
1552 tty->echo_buf = NULL;
1553 }
1554}
1555
1556/**
1557 * n_tty_open - open an ldisc
1558 * @tty: terminal to open
1559 *
1560 * Called when this line discipline is being attached to the
1561 * terminal device. Can sleep. Called serialized so that no
1562 * other events will occur in parallel. No further open will occur
1563 * until a close.
1564 */
1565
1566static int n_tty_open(struct tty_struct *tty)
1567{
1568 if (!tty)
1569 return -EINVAL;
1570
1571 /* These are ugly. Currently a malloc failure here can panic */
1572 if (!tty->read_buf) {
1573 tty->read_buf = kzalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
1574 if (!tty->read_buf)
1575 return -ENOMEM;
1576 }
1577 if (!tty->echo_buf) {
1578 tty->echo_buf = kzalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
1579
1580 if (!tty->echo_buf)
1581 return -ENOMEM;
1582 }
1583 reset_buffer_flags(tty);
1584 tty->column = 0;
1585 n_tty_set_termios(tty, NULL);
1586 tty->minimum_to_wake = 1;
1587 tty->closing = 0;
1588 return 0;
1589}
1590
1591static inline int input_available_p(struct tty_struct *tty, int amt)
1592{
1593 tty_flush_to_ldisc(tty);
1594 if (tty->icanon && !L_EXTPROC(tty)) {
1595 if (tty->canon_data)
1596 return 1;
1597 } else if (tty->read_cnt >= (amt ? amt : 1))
1598 return 1;
1599
1600 return 0;
1601}
1602
1603/**
1604 * copy_from_read_buf - copy read data directly
1605 * @tty: terminal device
1606 * @b: user data
1607 * @nr: size of data
1608 *
1609 * Helper function to speed up n_tty_read. It is only called when
1610 * ICANON is off; it copies characters straight from the tty queue to
1611 * user space directly. It can be profitably called twice; once to
1612 * drain the space from the tail pointer to the (physical) end of the
1613 * buffer, and once to drain the space from the (physical) beginning of
1614 * the buffer to head pointer.
1615 *
1616 * Called under the tty->atomic_read_lock sem
1617 *
1618 */
1619
1620static int copy_from_read_buf(struct tty_struct *tty,
1621 unsigned char __user **b,
1622 size_t *nr)
1623
1624{
1625 int retval;
1626 size_t n;
1627 unsigned long flags;
1628
1629 retval = 0;
1630 spin_lock_irqsave(&tty->read_lock, flags);
1631 n = min(tty->read_cnt, N_TTY_BUF_SIZE - tty->read_tail);
1632 n = min(*nr, n);
1633 spin_unlock_irqrestore(&tty->read_lock, flags);
1634 if (n) {
1635 retval = copy_to_user(*b, &tty->read_buf[tty->read_tail], n);
1636 n -= retval;
1637 tty_audit_add_data(tty, &tty->read_buf[tty->read_tail], n);
1638 spin_lock_irqsave(&tty->read_lock, flags);
1639 tty->read_tail = (tty->read_tail + n) & (N_TTY_BUF_SIZE-1);
1640 tty->read_cnt -= n;
1641 /* Turn single EOF into zero-length read */
1642 if (L_EXTPROC(tty) && tty->icanon && n == 1) {
1643 if (!tty->read_cnt && (*b)[n-1] == EOF_CHAR(tty))
1644 n--;
1645 }
1646 spin_unlock_irqrestore(&tty->read_lock, flags);
1647 *b += n;
1648 *nr -= n;
1649 }
1650 return retval;
1651}
1652
1653extern ssize_t redirected_tty_write(struct file *, const char __user *,
1654 size_t, loff_t *);
1655
1656/**
1657 * job_control - check job control
1658 * @tty: tty
1659 * @file: file handle
1660 *
1661 * Perform job control management checks on this file/tty descriptor
1662 * and if appropriate send any needed signals and return a negative
1663 * error code if action should be taken.
1664 *
1665 * FIXME:
1666 * Locking: None - redirected write test is safe, testing
1667 * current->signal should possibly lock current->sighand
1668 * pgrp locking ?
1669 */
1670
1671static int job_control(struct tty_struct *tty, struct file *file)
1672{
1673 /* Job control check -- must be done at start and after
1674 every sleep (POSIX.1 7.1.1.4). */
1675 /* NOTE: not yet done after every sleep pending a thorough
1676 check of the logic of this change. -- jlc */
1677 /* don't stop on /dev/console */
1678 if (file->f_op->write != redirected_tty_write &&
1679 current->signal->tty == tty) {
1680 if (!tty->pgrp)
1681 printk(KERN_ERR "n_tty_read: no tty->pgrp!\n");
1682 else if (task_pgrp(current) != tty->pgrp) {
1683 if (is_ignored(SIGTTIN) ||
1684 is_current_pgrp_orphaned())
1685 return -EIO;
1686 kill_pgrp(task_pgrp(current), SIGTTIN, 1);
1687 set_thread_flag(TIF_SIGPENDING);
1688 return -ERESTARTSYS;
1689 }
1690 }
1691 return 0;
1692}
1693
1694
1695/**
1696 * n_tty_read - read function for tty
1697 * @tty: tty device
1698 * @file: file object
1699 * @buf: userspace buffer pointer
1700 * @nr: size of I/O
1701 *
1702 * Perform reads for the line discipline. We are guaranteed that the
1703 * line discipline will not be closed under us but we may get multiple
1704 * parallel readers and must handle this ourselves. We may also get
1705 * a hangup. Always called in user context, may sleep.
1706 *
1707 * This code must be sure never to sleep through a hangup.
1708 */
1709
1710static ssize_t n_tty_read(struct tty_struct *tty, struct file *file,
1711 unsigned char __user *buf, size_t nr)
1712{
1713 unsigned char __user *b = buf;
1714 DECLARE_WAITQUEUE(wait, current);
1715 int c;
1716 int minimum, time;
1717 ssize_t retval = 0;
1718 ssize_t size;
1719 long timeout;
1720 unsigned long flags;
1721 int packet;
1722
1723do_it_again:
1724
1725 BUG_ON(!tty->read_buf);
1726
1727 c = job_control(tty, file);
1728 if (c < 0)
1729 return c;
1730
1731 minimum = time = 0;
1732 timeout = MAX_SCHEDULE_TIMEOUT;
1733 if (!tty->icanon) {
1734 time = (HZ / 10) * TIME_CHAR(tty);
1735 minimum = MIN_CHAR(tty);
1736 if (minimum) {
1737 if (time)
1738 tty->minimum_to_wake = 1;
1739 else if (!waitqueue_active(&tty->read_wait) ||
1740 (tty->minimum_to_wake > minimum))
1741 tty->minimum_to_wake = minimum;
1742 } else {
1743 timeout = 0;
1744 if (time) {
1745 timeout = time;
1746 time = 0;
1747 }
1748 tty->minimum_to_wake = minimum = 1;
1749 }
1750 }
1751
1752 /*
1753 * Internal serialization of reads.
1754 */
1755 if (file->f_flags & O_NONBLOCK) {
1756 if (!mutex_trylock(&tty->atomic_read_lock))
1757 return -EAGAIN;
1758 } else {
1759 if (mutex_lock_interruptible(&tty->atomic_read_lock))
1760 return -ERESTARTSYS;
1761 }
1762 packet = tty->packet;
1763
1764 add_wait_queue(&tty->read_wait, &wait);
1765 while (nr) {
1766 /* First test for status change. */
1767 if (packet && tty->link->ctrl_status) {
1768 unsigned char cs;
1769 if (b != buf)
1770 break;
1771 spin_lock_irqsave(&tty->link->ctrl_lock, flags);
1772 cs = tty->link->ctrl_status;
1773 tty->link->ctrl_status = 0;
1774 spin_unlock_irqrestore(&tty->link->ctrl_lock, flags);
1775 if (tty_put_user(tty, cs, b++)) {
1776 retval = -EFAULT;
1777 b--;
1778 break;
1779 }
1780 nr--;
1781 break;
1782 }
1783 /* This statement must be first before checking for input
1784 so that any interrupt will set the state back to
1785 TASK_RUNNING. */
1786 set_current_state(TASK_INTERRUPTIBLE);
1787
1788 if (((minimum - (b - buf)) < tty->minimum_to_wake) &&
1789 ((minimum - (b - buf)) >= 1))
1790 tty->minimum_to_wake = (minimum - (b - buf));
1791
1792 if (!input_available_p(tty, 0)) {
1793 if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) {
1794 retval = -EIO;
1795 break;
1796 }
1797 if (tty_hung_up_p(file))
1798 break;
1799 if (!timeout)
1800 break;
1801 if (file->f_flags & O_NONBLOCK) {
1802 retval = -EAGAIN;
1803 break;
1804 }
1805 if (signal_pending(current)) {
1806 retval = -ERESTARTSYS;
1807 break;
1808 }
1809 /* FIXME: does n_tty_set_room need locking ? */
1810 n_tty_set_room(tty);
1811 timeout = schedule_timeout(timeout);
1812 continue;
1813 }
1814 __set_current_state(TASK_RUNNING);
1815
1816 /* Deal with packet mode. */
1817 if (packet && b == buf) {
1818 if (tty_put_user(tty, TIOCPKT_DATA, b++)) {
1819 retval = -EFAULT;
1820 b--;
1821 break;
1822 }
1823 nr--;
1824 }
1825
1826 if (tty->icanon && !L_EXTPROC(tty)) {
1827 /* N.B. avoid overrun if nr == 0 */
1828 while (nr && tty->read_cnt) {
1829 int eol;
1830
1831 eol = test_and_clear_bit(tty->read_tail,
1832 tty->read_flags);
1833 c = tty->read_buf[tty->read_tail];
1834 spin_lock_irqsave(&tty->read_lock, flags);
1835 tty->read_tail = ((tty->read_tail+1) &
1836 (N_TTY_BUF_SIZE-1));
1837 tty->read_cnt--;
1838 if (eol) {
1839 /* this test should be redundant:
1840 * we shouldn't be reading data if
1841 * canon_data is 0
1842 */
1843 if (--tty->canon_data < 0)
1844 tty->canon_data = 0;
1845 }
1846 spin_unlock_irqrestore(&tty->read_lock, flags);
1847
1848 if (!eol || (c != __DISABLED_CHAR)) {
1849 if (tty_put_user(tty, c, b++)) {
1850 retval = -EFAULT;
1851 b--;
1852 break;
1853 }
1854 nr--;
1855 }
1856 if (eol) {
1857 tty_audit_push(tty);
1858 break;
1859 }
1860 }
1861 if (retval)
1862 break;
1863 } else {
1864 int uncopied;
1865 /* The copy function takes the read lock and handles
1866 locking internally for this case */
1867 uncopied = copy_from_read_buf(tty, &b, &nr);
1868 uncopied += copy_from_read_buf(tty, &b, &nr);
1869 if (uncopied) {
1870 retval = -EFAULT;
1871 break;
1872 }
1873 }
1874
1875 /* If there is enough space in the read buffer now, let the
1876 * low-level driver know. We use n_tty_chars_in_buffer() to
1877 * check the buffer, as it now knows about canonical mode.
1878 * Otherwise, if the driver is throttled and the line is
1879 * longer than TTY_THRESHOLD_UNTHROTTLE in canonical mode,
1880 * we won't get any more characters.
1881 */
1882 if (n_tty_chars_in_buffer(tty) <= TTY_THRESHOLD_UNTHROTTLE) {
1883 n_tty_set_room(tty);
1884 check_unthrottle(tty);
1885 }
1886
1887 if (b - buf >= minimum)
1888 break;
1889 if (time)
1890 timeout = time;
1891 }
1892 mutex_unlock(&tty->atomic_read_lock);
1893 remove_wait_queue(&tty->read_wait, &wait);
1894
1895 if (!waitqueue_active(&tty->read_wait))
1896 tty->minimum_to_wake = minimum;
1897
1898 __set_current_state(TASK_RUNNING);
1899 size = b - buf;
1900 if (size) {
1901 retval = size;
1902 if (nr)
1903 clear_bit(TTY_PUSH, &tty->flags);
1904 } else if (test_and_clear_bit(TTY_PUSH, &tty->flags))
1905 goto do_it_again;
1906
1907 n_tty_set_room(tty);
1908 return retval;
1909}
1910
1911/**
1912 * n_tty_write - write function for tty
1913 * @tty: tty device
1914 * @file: file object
1915 * @buf: userspace buffer pointer
1916 * @nr: size of I/O
1917 *
1918 * Write function of the terminal device. This is serialized with
1919 * respect to other write callers but not to termios changes, reads
1920 * and other such events. Since the receive code will echo characters,
1921 * thus calling driver write methods, the output_lock is used in
1922 * the output processing functions called here as well as in the
1923 * echo processing function to protect the column state and space
1924 * left in the buffer.
1925 *
1926 * This code must be sure never to sleep through a hangup.
1927 *
1928 * Locking: output_lock to protect column state and space left
1929 * (note that the process_output*() functions take this
1930 * lock themselves)
1931 */
1932
1933static ssize_t n_tty_write(struct tty_struct *tty, struct file *file,
1934 const unsigned char *buf, size_t nr)
1935{
1936 const unsigned char *b = buf;
1937 DECLARE_WAITQUEUE(wait, current);
1938 int c;
1939 ssize_t retval = 0;
1940
1941 /* Job control check -- must be done at start (POSIX.1 7.1.1.4). */
1942 if (L_TOSTOP(tty) && file->f_op->write != redirected_tty_write) {
1943 retval = tty_check_change(tty);
1944 if (retval)
1945 return retval;
1946 }
1947
1948 /* Write out any echoed characters that are still pending */
1949 process_echoes(tty);
1950
1951 add_wait_queue(&tty->write_wait, &wait);
1952 while (1) {
1953 set_current_state(TASK_INTERRUPTIBLE);
1954 if (signal_pending(current)) {
1955 retval = -ERESTARTSYS;
1956 break;
1957 }
1958 if (tty_hung_up_p(file) || (tty->link && !tty->link->count)) {
1959 retval = -EIO;
1960 break;
1961 }
1962 if (O_OPOST(tty) && !(test_bit(TTY_HW_COOK_OUT, &tty->flags))) {
1963 while (nr > 0) {
1964 ssize_t num = process_output_block(tty, b, nr);
1965 if (num < 0) {
1966 if (num == -EAGAIN)
1967 break;
1968 retval = num;
1969 goto break_out;
1970 }
1971 b += num;
1972 nr -= num;
1973 if (nr == 0)
1974 break;
1975 c = *b;
1976 if (process_output(c, tty) < 0)
1977 break;
1978 b++; nr--;
1979 }
1980 if (tty->ops->flush_chars)
1981 tty->ops->flush_chars(tty);
1982 } else {
1983 while (nr > 0) {
1984 c = tty->ops->write(tty, b, nr);
1985 if (c < 0) {
1986 retval = c;
1987 goto break_out;
1988 }
1989 if (!c)
1990 break;
1991 b += c;
1992 nr -= c;
1993 }
1994 }
1995 if (!nr)
1996 break;
1997 if (file->f_flags & O_NONBLOCK) {
1998 retval = -EAGAIN;
1999 break;
2000 }
2001 schedule();
2002 }
2003break_out:
2004 __set_current_state(TASK_RUNNING);
2005 remove_wait_queue(&tty->write_wait, &wait);
2006 if (b - buf != nr && tty->fasync)
2007 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
2008 return (b - buf) ? b - buf : retval;
2009}
2010
2011/**
2012 * n_tty_poll - poll method for N_TTY
2013 * @tty: terminal device
2014 * @file: file accessing it
2015 * @wait: poll table
2016 *
2017 * Called when the line discipline is asked to poll() for data or
2018 * for special events. This code is not serialized with respect to
2019 * other events save open/close.
2020 *
2021 * This code must be sure never to sleep through a hangup.
2022 * Called without the kernel lock held - fine
2023 */
2024
2025static unsigned int n_tty_poll(struct tty_struct *tty, struct file *file,
2026 poll_table *wait)
2027{
2028 unsigned int mask = 0;
2029
2030 poll_wait(file, &tty->read_wait, wait);
2031 poll_wait(file, &tty->write_wait, wait);
2032 if (input_available_p(tty, TIME_CHAR(tty) ? 0 : MIN_CHAR(tty)))
2033 mask |= POLLIN | POLLRDNORM;
2034 if (tty->packet && tty->link->ctrl_status)
2035 mask |= POLLPRI | POLLIN | POLLRDNORM;
2036 if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
2037 mask |= POLLHUP;
2038 if (tty_hung_up_p(file))
2039 mask |= POLLHUP;
2040 if (!(mask & (POLLHUP | POLLIN | POLLRDNORM))) {
2041 if (MIN_CHAR(tty) && !TIME_CHAR(tty))
2042 tty->minimum_to_wake = MIN_CHAR(tty);
2043 else
2044 tty->minimum_to_wake = 1;
2045 }
2046 if (tty->ops->write && !tty_is_writelocked(tty) &&
2047 tty_chars_in_buffer(tty) < WAKEUP_CHARS &&
2048 tty_write_room(tty) > 0)
2049 mask |= POLLOUT | POLLWRNORM;
2050 return mask;
2051}
2052
2053static unsigned long inq_canon(struct tty_struct *tty)
2054{
2055 int nr, head, tail;
2056
2057 if (!tty->canon_data)
2058 return 0;
2059 head = tty->canon_head;
2060 tail = tty->read_tail;
2061 nr = (head - tail) & (N_TTY_BUF_SIZE-1);
2062 /* Skip EOF-chars.. */
2063 while (head != tail) {
2064 if (test_bit(tail, tty->read_flags) &&
2065 tty->read_buf[tail] == __DISABLED_CHAR)
2066 nr--;
2067 tail = (tail+1) & (N_TTY_BUF_SIZE-1);
2068 }
2069 return nr;
2070}
2071
2072static int n_tty_ioctl(struct tty_struct *tty, struct file *file,
2073 unsigned int cmd, unsigned long arg)
2074{
2075 int retval;
2076
2077 switch (cmd) {
2078 case TIOCOUTQ:
2079 return put_user(tty_chars_in_buffer(tty), (int __user *) arg);
2080 case TIOCINQ:
2081 /* FIXME: Locking */
2082 retval = tty->read_cnt;
2083 if (L_ICANON(tty))
2084 retval = inq_canon(tty);
2085 return put_user(retval, (unsigned int __user *) arg);
2086 default:
2087 return n_tty_ioctl_helper(tty, file, cmd, arg);
2088 }
2089}
2090
2091struct tty_ldisc_ops tty_ldisc_N_TTY = {
2092 .magic = TTY_LDISC_MAGIC,
2093 .name = "n_tty",
2094 .open = n_tty_open,
2095 .close = n_tty_close,
2096 .flush_buffer = n_tty_flush_buffer,
2097 .chars_in_buffer = n_tty_chars_in_buffer,
2098 .read = n_tty_read,
2099 .write = n_tty_write,
2100 .ioctl = n_tty_ioctl,
2101 .set_termios = n_tty_set_termios,
2102 .poll = n_tty_poll,
2103 .receive_buf = n_tty_receive_buf,
2104 .write_wakeup = n_tty_write_wakeup
2105};
2106
2107/**
2108 * n_tty_inherit_ops - inherit N_TTY methods
2109 * @ops: struct tty_ldisc_ops where to save N_TTY methods
2110 *
2111 * Used by a generic struct tty_ldisc_ops to easily inherit N_TTY
2112 * methods.
2113 */
2114
2115void n_tty_inherit_ops(struct tty_ldisc_ops *ops)
2116{
2117 *ops = tty_ldisc_N_TTY;
2118 ops->owner = NULL;
2119 ops->refcount = ops->flags = 0;
2120}
2121EXPORT_SYMBOL_GPL(n_tty_inherit_ops);
diff --git a/drivers/char/nozomi.c b/drivers/char/nozomi.c
deleted file mode 100644
index 817169cbb245..000000000000
--- a/drivers/char/nozomi.c
+++ /dev/null
@@ -1,1995 +0,0 @@
1/*
2 * nozomi.c -- HSDPA driver Broadband Wireless Data Card - Globe Trotter
3 *
4 * Written by: Ulf Jakobsson,
5 * Jan Ã…kerfeldt,
6 * Stefan Thomasson,
7 *
8 * Maintained by: Paul Hardwick (p.hardwick@option.com)
9 *
10 * Patches:
11 * Locking code changes for Vodafone by Sphere Systems Ltd,
12 * Andrew Bird (ajb@spheresystems.co.uk )
13 * & Phil Sanderson
14 *
15 * Source has been ported from an implementation made by Filip Aben @ Option
16 *
17 * --------------------------------------------------------------------------
18 *
19 * Copyright (c) 2005,2006 Option Wireless Sweden AB
20 * Copyright (c) 2006 Sphere Systems Ltd
21 * Copyright (c) 2006 Option Wireless n/v
22 * All rights Reserved.
23 *
24 * This program is free software; you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License as published by
26 * the Free Software Foundation; either version 2 of the License, or
27 * (at your option) any later version.
28 *
29 * This program is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 * GNU General Public License for more details.
33 *
34 * You should have received a copy of the GNU General Public License
35 * along with this program; if not, write to the Free Software
36 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
37 *
38 * --------------------------------------------------------------------------
39 */
40
41/* Enable this to have a lot of debug printouts */
42#define DEBUG
43
44#include <linux/kernel.h>
45#include <linux/module.h>
46#include <linux/pci.h>
47#include <linux/ioport.h>
48#include <linux/tty.h>
49#include <linux/tty_driver.h>
50#include <linux/tty_flip.h>
51#include <linux/sched.h>
52#include <linux/serial.h>
53#include <linux/interrupt.h>
54#include <linux/kmod.h>
55#include <linux/init.h>
56#include <linux/kfifo.h>
57#include <linux/uaccess.h>
58#include <linux/slab.h>
59#include <asm/byteorder.h>
60
61#include <linux/delay.h>
62
63
64#define VERSION_STRING DRIVER_DESC " 2.1d (build date: " \
65 __DATE__ " " __TIME__ ")"
66
67/* Macros definitions */
68
69/* Default debug printout level */
70#define NOZOMI_DEBUG_LEVEL 0x00
71
72#define P_BUF_SIZE 128
73#define NFO(_err_flag_, args...) \
74do { \
75 char tmp[P_BUF_SIZE]; \
76 snprintf(tmp, sizeof(tmp), ##args); \
77 printk(_err_flag_ "[%d] %s(): %s\n", __LINE__, \
78 __func__, tmp); \
79} while (0)
80
81#define DBG1(args...) D_(0x01, ##args)
82#define DBG2(args...) D_(0x02, ##args)
83#define DBG3(args...) D_(0x04, ##args)
84#define DBG4(args...) D_(0x08, ##args)
85#define DBG5(args...) D_(0x10, ##args)
86#define DBG6(args...) D_(0x20, ##args)
87#define DBG7(args...) D_(0x40, ##args)
88#define DBG8(args...) D_(0x80, ##args)
89
90#ifdef DEBUG
91/* Do we need this settable at runtime? */
92static int debug = NOZOMI_DEBUG_LEVEL;
93
94#define D(lvl, args...) do \
95 {if (lvl & debug) NFO(KERN_DEBUG, ##args); } \
96 while (0)
97#define D_(lvl, args...) D(lvl, ##args)
98
99/* These printouts are always printed */
100
101#else
102static int debug;
103#define D_(lvl, args...)
104#endif
105
106/* TODO: rewrite to optimize macros... */
107
108#define TMP_BUF_MAX 256
109
110#define DUMP(buf__,len__) \
111 do { \
112 char tbuf[TMP_BUF_MAX] = {0};\
113 if (len__ > 1) {\
114 snprintf(tbuf, len__ > TMP_BUF_MAX ? TMP_BUF_MAX : len__, "%s", buf__);\
115 if (tbuf[len__-2] == '\r') {\
116 tbuf[len__-2] = 'r';\
117 } \
118 DBG1("SENDING: '%s' (%d+n)", tbuf, len__);\
119 } else {\
120 DBG1("SENDING: '%s' (%d)", tbuf, len__);\
121 } \
122} while (0)
123
124/* Defines */
125#define NOZOMI_NAME "nozomi"
126#define NOZOMI_NAME_TTY "nozomi_tty"
127#define DRIVER_DESC "Nozomi driver"
128
129#define NTTY_TTY_MAXMINORS 256
130#define NTTY_FIFO_BUFFER_SIZE 8192
131
132/* Must be power of 2 */
133#define FIFO_BUFFER_SIZE_UL 8192
134
135/* Size of tmp send buffer to card */
136#define SEND_BUF_MAX 1024
137#define RECEIVE_BUF_MAX 4
138
139
140#define R_IIR 0x0000 /* Interrupt Identity Register */
141#define R_FCR 0x0000 /* Flow Control Register */
142#define R_IER 0x0004 /* Interrupt Enable Register */
143
144#define CONFIG_MAGIC 0xEFEFFEFE
145#define TOGGLE_VALID 0x0000
146
147/* Definition of interrupt tokens */
148#define MDM_DL1 0x0001
149#define MDM_UL1 0x0002
150#define MDM_DL2 0x0004
151#define MDM_UL2 0x0008
152#define DIAG_DL1 0x0010
153#define DIAG_DL2 0x0020
154#define DIAG_UL 0x0040
155#define APP1_DL 0x0080
156#define APP1_UL 0x0100
157#define APP2_DL 0x0200
158#define APP2_UL 0x0400
159#define CTRL_DL 0x0800
160#define CTRL_UL 0x1000
161#define RESET 0x8000
162
163#define MDM_DL (MDM_DL1 | MDM_DL2)
164#define MDM_UL (MDM_UL1 | MDM_UL2)
165#define DIAG_DL (DIAG_DL1 | DIAG_DL2)
166
167/* modem signal definition */
168#define CTRL_DSR 0x0001
169#define CTRL_DCD 0x0002
170#define CTRL_RI 0x0004
171#define CTRL_CTS 0x0008
172
173#define CTRL_DTR 0x0001
174#define CTRL_RTS 0x0002
175
176#define MAX_PORT 4
177#define NOZOMI_MAX_PORTS 5
178#define NOZOMI_MAX_CARDS (NTTY_TTY_MAXMINORS / MAX_PORT)
179
180/* Type definitions */
181
182/*
183 * There are two types of nozomi cards,
184 * one with 2048 memory and with 8192 memory
185 */
186enum card_type {
187 F32_2 = 2048, /* 512 bytes downlink + uplink * 2 -> 2048 */
188 F32_8 = 8192, /* 3072 bytes downl. + 1024 bytes uplink * 2 -> 8192 */
189};
190
191/* Initialization states a card can be in */
192enum card_state {
193 NOZOMI_STATE_UKNOWN = 0,
194 NOZOMI_STATE_ENABLED = 1, /* pci device enabled */
195 NOZOMI_STATE_ALLOCATED = 2, /* config setup done */
196 NOZOMI_STATE_READY = 3, /* flowcontrols received */
197};
198
199/* Two different toggle channels exist */
200enum channel_type {
201 CH_A = 0,
202 CH_B = 1,
203};
204
205/* Port definition for the card regarding flow control */
206enum ctrl_port_type {
207 CTRL_CMD = 0,
208 CTRL_MDM = 1,
209 CTRL_DIAG = 2,
210 CTRL_APP1 = 3,
211 CTRL_APP2 = 4,
212 CTRL_ERROR = -1,
213};
214
215/* Ports that the nozomi has */
216enum port_type {
217 PORT_MDM = 0,
218 PORT_DIAG = 1,
219 PORT_APP1 = 2,
220 PORT_APP2 = 3,
221 PORT_CTRL = 4,
222 PORT_ERROR = -1,
223};
224
225#ifdef __BIG_ENDIAN
226/* Big endian */
227
228struct toggles {
229 unsigned int enabled:5; /*
230 * Toggle fields are valid if enabled is 0,
231 * else A-channels must always be used.
232 */
233 unsigned int diag_dl:1;
234 unsigned int mdm_dl:1;
235 unsigned int mdm_ul:1;
236} __attribute__ ((packed));
237
238/* Configuration table to read at startup of card */
239/* Is for now only needed during initialization phase */
240struct config_table {
241 u32 signature;
242 u16 product_information;
243 u16 version;
244 u8 pad3[3];
245 struct toggles toggle;
246 u8 pad1[4];
247 u16 dl_mdm_len1; /*
248 * If this is 64, it can hold
249 * 60 bytes + 4 that is length field
250 */
251 u16 dl_start;
252
253 u16 dl_diag_len1;
254 u16 dl_mdm_len2; /*
255 * If this is 64, it can hold
256 * 60 bytes + 4 that is length field
257 */
258 u16 dl_app1_len;
259
260 u16 dl_diag_len2;
261 u16 dl_ctrl_len;
262 u16 dl_app2_len;
263 u8 pad2[16];
264 u16 ul_mdm_len1;
265 u16 ul_start;
266 u16 ul_diag_len;
267 u16 ul_mdm_len2;
268 u16 ul_app1_len;
269 u16 ul_app2_len;
270 u16 ul_ctrl_len;
271} __attribute__ ((packed));
272
273/* This stores all control downlink flags */
274struct ctrl_dl {
275 u8 port;
276 unsigned int reserved:4;
277 unsigned int CTS:1;
278 unsigned int RI:1;
279 unsigned int DCD:1;
280 unsigned int DSR:1;
281} __attribute__ ((packed));
282
283/* This stores all control uplink flags */
284struct ctrl_ul {
285 u8 port;
286 unsigned int reserved:6;
287 unsigned int RTS:1;
288 unsigned int DTR:1;
289} __attribute__ ((packed));
290
291#else
292/* Little endian */
293
294/* This represents the toggle information */
295struct toggles {
296 unsigned int mdm_ul:1;
297 unsigned int mdm_dl:1;
298 unsigned int diag_dl:1;
299 unsigned int enabled:5; /*
300 * Toggle fields are valid if enabled is 0,
301 * else A-channels must always be used.
302 */
303} __attribute__ ((packed));
304
305/* Configuration table to read at startup of card */
306struct config_table {
307 u32 signature;
308 u16 version;
309 u16 product_information;
310 struct toggles toggle;
311 u8 pad1[7];
312 u16 dl_start;
313 u16 dl_mdm_len1; /*
314 * If this is 64, it can hold
315 * 60 bytes + 4 that is length field
316 */
317 u16 dl_mdm_len2;
318 u16 dl_diag_len1;
319 u16 dl_diag_len2;
320 u16 dl_app1_len;
321 u16 dl_app2_len;
322 u16 dl_ctrl_len;
323 u8 pad2[16];
324 u16 ul_start;
325 u16 ul_mdm_len2;
326 u16 ul_mdm_len1;
327 u16 ul_diag_len;
328 u16 ul_app1_len;
329 u16 ul_app2_len;
330 u16 ul_ctrl_len;
331} __attribute__ ((packed));
332
333/* This stores all control downlink flags */
334struct ctrl_dl {
335 unsigned int DSR:1;
336 unsigned int DCD:1;
337 unsigned int RI:1;
338 unsigned int CTS:1;
339 unsigned int reserverd:4;
340 u8 port;
341} __attribute__ ((packed));
342
343/* This stores all control uplink flags */
344struct ctrl_ul {
345 unsigned int DTR:1;
346 unsigned int RTS:1;
347 unsigned int reserved:6;
348 u8 port;
349} __attribute__ ((packed));
350#endif
351
352/* This holds all information that is needed regarding a port */
353struct port {
354 struct tty_port port;
355 u8 update_flow_control;
356 struct ctrl_ul ctrl_ul;
357 struct ctrl_dl ctrl_dl;
358 struct kfifo fifo_ul;
359 void __iomem *dl_addr[2];
360 u32 dl_size[2];
361 u8 toggle_dl;
362 void __iomem *ul_addr[2];
363 u32 ul_size[2];
364 u8 toggle_ul;
365 u16 token_dl;
366
367 /* mutex to ensure one access patch to this port */
368 struct mutex tty_sem;
369 wait_queue_head_t tty_wait;
370 struct async_icount tty_icount;
371
372 struct nozomi *dc;
373};
374
375/* Private data one for each card in the system */
376struct nozomi {
377 void __iomem *base_addr;
378 unsigned long flip;
379
380 /* Pointers to registers */
381 void __iomem *reg_iir;
382 void __iomem *reg_fcr;
383 void __iomem *reg_ier;
384
385 u16 last_ier;
386 enum card_type card_type;
387 struct config_table config_table; /* Configuration table */
388 struct pci_dev *pdev;
389 struct port port[NOZOMI_MAX_PORTS];
390 u8 *send_buf;
391
392 spinlock_t spin_mutex; /* secures access to registers and tty */
393
394 unsigned int index_start;
395 enum card_state state;
396 u32 open_ttys;
397};
398
399/* This is a data packet that is read or written to/from card */
400struct buffer {
401 u32 size; /* size is the length of the data buffer */
402 u8 *data;
403} __attribute__ ((packed));
404
405/* Global variables */
406static const struct pci_device_id nozomi_pci_tbl[] __devinitconst = {
407 {PCI_DEVICE(0x1931, 0x000c)}, /* Nozomi HSDPA */
408 {},
409};
410
411MODULE_DEVICE_TABLE(pci, nozomi_pci_tbl);
412
413static struct nozomi *ndevs[NOZOMI_MAX_CARDS];
414static struct tty_driver *ntty_driver;
415
416static const struct tty_port_operations noz_tty_port_ops;
417
418/*
419 * find card by tty_index
420 */
421static inline struct nozomi *get_dc_by_tty(const struct tty_struct *tty)
422{
423 return tty ? ndevs[tty->index / MAX_PORT] : NULL;
424}
425
426static inline struct port *get_port_by_tty(const struct tty_struct *tty)
427{
428 struct nozomi *ndev = get_dc_by_tty(tty);
429 return ndev ? &ndev->port[tty->index % MAX_PORT] : NULL;
430}
431
432/*
433 * TODO:
434 * -Optimize
435 * -Rewrite cleaner
436 */
437
438static void read_mem32(u32 *buf, const void __iomem *mem_addr_start,
439 u32 size_bytes)
440{
441 u32 i = 0;
442 const u32 __iomem *ptr = mem_addr_start;
443 u16 *buf16;
444
445 if (unlikely(!ptr || !buf))
446 goto out;
447
448 /* shortcut for extremely often used cases */
449 switch (size_bytes) {
450 case 2: /* 2 bytes */
451 buf16 = (u16 *) buf;
452 *buf16 = __le16_to_cpu(readw(ptr));
453 goto out;
454 break;
455 case 4: /* 4 bytes */
456 *(buf) = __le32_to_cpu(readl(ptr));
457 goto out;
458 break;
459 }
460
461 while (i < size_bytes) {
462 if (size_bytes - i == 2) {
463 /* Handle 2 bytes in the end */
464 buf16 = (u16 *) buf;
465 *(buf16) = __le16_to_cpu(readw(ptr));
466 i += 2;
467 } else {
468 /* Read 4 bytes */
469 *(buf) = __le32_to_cpu(readl(ptr));
470 i += 4;
471 }
472 buf++;
473 ptr++;
474 }
475out:
476 return;
477}
478
479/*
480 * TODO:
481 * -Optimize
482 * -Rewrite cleaner
483 */
484static u32 write_mem32(void __iomem *mem_addr_start, const u32 *buf,
485 u32 size_bytes)
486{
487 u32 i = 0;
488 u32 __iomem *ptr = mem_addr_start;
489 const u16 *buf16;
490
491 if (unlikely(!ptr || !buf))
492 return 0;
493
494 /* shortcut for extremely often used cases */
495 switch (size_bytes) {
496 case 2: /* 2 bytes */
497 buf16 = (const u16 *)buf;
498 writew(__cpu_to_le16(*buf16), ptr);
499 return 2;
500 break;
501 case 1: /*
502 * also needs to write 4 bytes in this case
503 * so falling through..
504 */
505 case 4: /* 4 bytes */
506 writel(__cpu_to_le32(*buf), ptr);
507 return 4;
508 break;
509 }
510
511 while (i < size_bytes) {
512 if (size_bytes - i == 2) {
513 /* 2 bytes */
514 buf16 = (const u16 *)buf;
515 writew(__cpu_to_le16(*buf16), ptr);
516 i += 2;
517 } else {
518 /* 4 bytes */
519 writel(__cpu_to_le32(*buf), ptr);
520 i += 4;
521 }
522 buf++;
523 ptr++;
524 }
525 return i;
526}
527
528/* Setup pointers to different channels and also setup buffer sizes. */
529static void setup_memory(struct nozomi *dc)
530{
531 void __iomem *offset = dc->base_addr + dc->config_table.dl_start;
532 /* The length reported is including the length field of 4 bytes,
533 * hence subtract with 4.
534 */
535 const u16 buff_offset = 4;
536
537 /* Modem port dl configuration */
538 dc->port[PORT_MDM].dl_addr[CH_A] = offset;
539 dc->port[PORT_MDM].dl_addr[CH_B] =
540 (offset += dc->config_table.dl_mdm_len1);
541 dc->port[PORT_MDM].dl_size[CH_A] =
542 dc->config_table.dl_mdm_len1 - buff_offset;
543 dc->port[PORT_MDM].dl_size[CH_B] =
544 dc->config_table.dl_mdm_len2 - buff_offset;
545
546 /* Diag port dl configuration */
547 dc->port[PORT_DIAG].dl_addr[CH_A] =
548 (offset += dc->config_table.dl_mdm_len2);
549 dc->port[PORT_DIAG].dl_size[CH_A] =
550 dc->config_table.dl_diag_len1 - buff_offset;
551 dc->port[PORT_DIAG].dl_addr[CH_B] =
552 (offset += dc->config_table.dl_diag_len1);
553 dc->port[PORT_DIAG].dl_size[CH_B] =
554 dc->config_table.dl_diag_len2 - buff_offset;
555
556 /* App1 port dl configuration */
557 dc->port[PORT_APP1].dl_addr[CH_A] =
558 (offset += dc->config_table.dl_diag_len2);
559 dc->port[PORT_APP1].dl_size[CH_A] =
560 dc->config_table.dl_app1_len - buff_offset;
561
562 /* App2 port dl configuration */
563 dc->port[PORT_APP2].dl_addr[CH_A] =
564 (offset += dc->config_table.dl_app1_len);
565 dc->port[PORT_APP2].dl_size[CH_A] =
566 dc->config_table.dl_app2_len - buff_offset;
567
568 /* Ctrl dl configuration */
569 dc->port[PORT_CTRL].dl_addr[CH_A] =
570 (offset += dc->config_table.dl_app2_len);
571 dc->port[PORT_CTRL].dl_size[CH_A] =
572 dc->config_table.dl_ctrl_len - buff_offset;
573
574 offset = dc->base_addr + dc->config_table.ul_start;
575
576 /* Modem Port ul configuration */
577 dc->port[PORT_MDM].ul_addr[CH_A] = offset;
578 dc->port[PORT_MDM].ul_size[CH_A] =
579 dc->config_table.ul_mdm_len1 - buff_offset;
580 dc->port[PORT_MDM].ul_addr[CH_B] =
581 (offset += dc->config_table.ul_mdm_len1);
582 dc->port[PORT_MDM].ul_size[CH_B] =
583 dc->config_table.ul_mdm_len2 - buff_offset;
584
585 /* Diag port ul configuration */
586 dc->port[PORT_DIAG].ul_addr[CH_A] =
587 (offset += dc->config_table.ul_mdm_len2);
588 dc->port[PORT_DIAG].ul_size[CH_A] =
589 dc->config_table.ul_diag_len - buff_offset;
590
591 /* App1 port ul configuration */
592 dc->port[PORT_APP1].ul_addr[CH_A] =
593 (offset += dc->config_table.ul_diag_len);
594 dc->port[PORT_APP1].ul_size[CH_A] =
595 dc->config_table.ul_app1_len - buff_offset;
596
597 /* App2 port ul configuration */
598 dc->port[PORT_APP2].ul_addr[CH_A] =
599 (offset += dc->config_table.ul_app1_len);
600 dc->port[PORT_APP2].ul_size[CH_A] =
601 dc->config_table.ul_app2_len - buff_offset;
602
603 /* Ctrl ul configuration */
604 dc->port[PORT_CTRL].ul_addr[CH_A] =
605 (offset += dc->config_table.ul_app2_len);
606 dc->port[PORT_CTRL].ul_size[CH_A] =
607 dc->config_table.ul_ctrl_len - buff_offset;
608}
609
610/* Dump config table under initalization phase */
611#ifdef DEBUG
612static void dump_table(const struct nozomi *dc)
613{
614 DBG3("signature: 0x%08X", dc->config_table.signature);
615 DBG3("version: 0x%04X", dc->config_table.version);
616 DBG3("product_information: 0x%04X", \
617 dc->config_table.product_information);
618 DBG3("toggle enabled: %d", dc->config_table.toggle.enabled);
619 DBG3("toggle up_mdm: %d", dc->config_table.toggle.mdm_ul);
620 DBG3("toggle dl_mdm: %d", dc->config_table.toggle.mdm_dl);
621 DBG3("toggle dl_dbg: %d", dc->config_table.toggle.diag_dl);
622
623 DBG3("dl_start: 0x%04X", dc->config_table.dl_start);
624 DBG3("dl_mdm_len0: 0x%04X, %d", dc->config_table.dl_mdm_len1,
625 dc->config_table.dl_mdm_len1);
626 DBG3("dl_mdm_len1: 0x%04X, %d", dc->config_table.dl_mdm_len2,
627 dc->config_table.dl_mdm_len2);
628 DBG3("dl_diag_len0: 0x%04X, %d", dc->config_table.dl_diag_len1,
629 dc->config_table.dl_diag_len1);
630 DBG3("dl_diag_len1: 0x%04X, %d", dc->config_table.dl_diag_len2,
631 dc->config_table.dl_diag_len2);
632 DBG3("dl_app1_len: 0x%04X, %d", dc->config_table.dl_app1_len,
633 dc->config_table.dl_app1_len);
634 DBG3("dl_app2_len: 0x%04X, %d", dc->config_table.dl_app2_len,
635 dc->config_table.dl_app2_len);
636 DBG3("dl_ctrl_len: 0x%04X, %d", dc->config_table.dl_ctrl_len,
637 dc->config_table.dl_ctrl_len);
638 DBG3("ul_start: 0x%04X, %d", dc->config_table.ul_start,
639 dc->config_table.ul_start);
640 DBG3("ul_mdm_len[0]: 0x%04X, %d", dc->config_table.ul_mdm_len1,
641 dc->config_table.ul_mdm_len1);
642 DBG3("ul_mdm_len[1]: 0x%04X, %d", dc->config_table.ul_mdm_len2,
643 dc->config_table.ul_mdm_len2);
644 DBG3("ul_diag_len: 0x%04X, %d", dc->config_table.ul_diag_len,
645 dc->config_table.ul_diag_len);
646 DBG3("ul_app1_len: 0x%04X, %d", dc->config_table.ul_app1_len,
647 dc->config_table.ul_app1_len);
648 DBG3("ul_app2_len: 0x%04X, %d", dc->config_table.ul_app2_len,
649 dc->config_table.ul_app2_len);
650 DBG3("ul_ctrl_len: 0x%04X, %d", dc->config_table.ul_ctrl_len,
651 dc->config_table.ul_ctrl_len);
652}
653#else
654static inline void dump_table(const struct nozomi *dc) { }
655#endif
656
657/*
658 * Read configuration table from card under intalization phase
659 * Returns 1 if ok, else 0
660 */
661static int nozomi_read_config_table(struct nozomi *dc)
662{
663 read_mem32((u32 *) &dc->config_table, dc->base_addr + 0,
664 sizeof(struct config_table));
665
666 if (dc->config_table.signature != CONFIG_MAGIC) {
667 dev_err(&dc->pdev->dev, "ConfigTable Bad! 0x%08X != 0x%08X\n",
668 dc->config_table.signature, CONFIG_MAGIC);
669 return 0;
670 }
671
672 if ((dc->config_table.version == 0)
673 || (dc->config_table.toggle.enabled == TOGGLE_VALID)) {
674 int i;
675 DBG1("Second phase, configuring card");
676
677 setup_memory(dc);
678
679 dc->port[PORT_MDM].toggle_ul = dc->config_table.toggle.mdm_ul;
680 dc->port[PORT_MDM].toggle_dl = dc->config_table.toggle.mdm_dl;
681 dc->port[PORT_DIAG].toggle_dl = dc->config_table.toggle.diag_dl;
682 DBG1("toggle ports: MDM UL:%d MDM DL:%d, DIAG DL:%d",
683 dc->port[PORT_MDM].toggle_ul,
684 dc->port[PORT_MDM].toggle_dl, dc->port[PORT_DIAG].toggle_dl);
685
686 dump_table(dc);
687
688 for (i = PORT_MDM; i < MAX_PORT; i++) {
689 memset(&dc->port[i].ctrl_dl, 0, sizeof(struct ctrl_dl));
690 memset(&dc->port[i].ctrl_ul, 0, sizeof(struct ctrl_ul));
691 }
692
693 /* Enable control channel */
694 dc->last_ier = dc->last_ier | CTRL_DL;
695 writew(dc->last_ier, dc->reg_ier);
696
697 dc->state = NOZOMI_STATE_ALLOCATED;
698 dev_info(&dc->pdev->dev, "Initialization OK!\n");
699 return 1;
700 }
701
702 if ((dc->config_table.version > 0)
703 && (dc->config_table.toggle.enabled != TOGGLE_VALID)) {
704 u32 offset = 0;
705 DBG1("First phase: pushing upload buffers, clearing download");
706
707 dev_info(&dc->pdev->dev, "Version of card: %d\n",
708 dc->config_table.version);
709
710 /* Here we should disable all I/O over F32. */
711 setup_memory(dc);
712
713 /*
714 * We should send ALL channel pair tokens back along
715 * with reset token
716 */
717
718 /* push upload modem buffers */
719 write_mem32(dc->port[PORT_MDM].ul_addr[CH_A],
720 (u32 *) &offset, 4);
721 write_mem32(dc->port[PORT_MDM].ul_addr[CH_B],
722 (u32 *) &offset, 4);
723
724 writew(MDM_UL | DIAG_DL | MDM_DL, dc->reg_fcr);
725
726 DBG1("First phase done");
727 }
728
729 return 1;
730}
731
732/* Enable uplink interrupts */
733static void enable_transmit_ul(enum port_type port, struct nozomi *dc)
734{
735 static const u16 mask[] = {MDM_UL, DIAG_UL, APP1_UL, APP2_UL, CTRL_UL};
736
737 if (port < NOZOMI_MAX_PORTS) {
738 dc->last_ier |= mask[port];
739 writew(dc->last_ier, dc->reg_ier);
740 } else {
741 dev_err(&dc->pdev->dev, "Called with wrong port?\n");
742 }
743}
744
745/* Disable uplink interrupts */
746static void disable_transmit_ul(enum port_type port, struct nozomi *dc)
747{
748 static const u16 mask[] =
749 {~MDM_UL, ~DIAG_UL, ~APP1_UL, ~APP2_UL, ~CTRL_UL};
750
751 if (port < NOZOMI_MAX_PORTS) {
752 dc->last_ier &= mask[port];
753 writew(dc->last_ier, dc->reg_ier);
754 } else {
755 dev_err(&dc->pdev->dev, "Called with wrong port?\n");
756 }
757}
758
759/* Enable downlink interrupts */
760static void enable_transmit_dl(enum port_type port, struct nozomi *dc)
761{
762 static const u16 mask[] = {MDM_DL, DIAG_DL, APP1_DL, APP2_DL, CTRL_DL};
763
764 if (port < NOZOMI_MAX_PORTS) {
765 dc->last_ier |= mask[port];
766 writew(dc->last_ier, dc->reg_ier);
767 } else {
768 dev_err(&dc->pdev->dev, "Called with wrong port?\n");
769 }
770}
771
772/* Disable downlink interrupts */
773static void disable_transmit_dl(enum port_type port, struct nozomi *dc)
774{
775 static const u16 mask[] =
776 {~MDM_DL, ~DIAG_DL, ~APP1_DL, ~APP2_DL, ~CTRL_DL};
777
778 if (port < NOZOMI_MAX_PORTS) {
779 dc->last_ier &= mask[port];
780 writew(dc->last_ier, dc->reg_ier);
781 } else {
782 dev_err(&dc->pdev->dev, "Called with wrong port?\n");
783 }
784}
785
786/*
787 * Return 1 - send buffer to card and ack.
788 * Return 0 - don't ack, don't send buffer to card.
789 */
790static int send_data(enum port_type index, struct nozomi *dc)
791{
792 u32 size = 0;
793 struct port *port = &dc->port[index];
794 const u8 toggle = port->toggle_ul;
795 void __iomem *addr = port->ul_addr[toggle];
796 const u32 ul_size = port->ul_size[toggle];
797 struct tty_struct *tty = tty_port_tty_get(&port->port);
798
799 /* Get data from tty and place in buf for now */
800 size = kfifo_out(&port->fifo_ul, dc->send_buf,
801 ul_size < SEND_BUF_MAX ? ul_size : SEND_BUF_MAX);
802
803 if (size == 0) {
804 DBG4("No more data to send, disable link:");
805 tty_kref_put(tty);
806 return 0;
807 }
808
809 /* DUMP(buf, size); */
810
811 /* Write length + data */
812 write_mem32(addr, (u32 *) &size, 4);
813 write_mem32(addr + 4, (u32 *) dc->send_buf, size);
814
815 if (tty)
816 tty_wakeup(tty);
817
818 tty_kref_put(tty);
819 return 1;
820}
821
822/* If all data has been read, return 1, else 0 */
823static int receive_data(enum port_type index, struct nozomi *dc)
824{
825 u8 buf[RECEIVE_BUF_MAX] = { 0 };
826 int size;
827 u32 offset = 4;
828 struct port *port = &dc->port[index];
829 void __iomem *addr = port->dl_addr[port->toggle_dl];
830 struct tty_struct *tty = tty_port_tty_get(&port->port);
831 int i, ret;
832
833 if (unlikely(!tty)) {
834 DBG1("tty not open for port: %d?", index);
835 return 1;
836 }
837
838 read_mem32((u32 *) &size, addr, 4);
839 /* DBG1( "%d bytes port: %d", size, index); */
840
841 if (test_bit(TTY_THROTTLED, &tty->flags)) {
842 DBG1("No room in tty, don't read data, don't ack interrupt, "
843 "disable interrupt");
844
845 /* disable interrupt in downlink... */
846 disable_transmit_dl(index, dc);
847 ret = 0;
848 goto put;
849 }
850
851 if (unlikely(size == 0)) {
852 dev_err(&dc->pdev->dev, "size == 0?\n");
853 ret = 1;
854 goto put;
855 }
856
857 while (size > 0) {
858 read_mem32((u32 *) buf, addr + offset, RECEIVE_BUF_MAX);
859
860 if (size == 1) {
861 tty_insert_flip_char(tty, buf[0], TTY_NORMAL);
862 size = 0;
863 } else if (size < RECEIVE_BUF_MAX) {
864 size -= tty_insert_flip_string(tty, (char *) buf, size);
865 } else {
866 i = tty_insert_flip_string(tty, \
867 (char *) buf, RECEIVE_BUF_MAX);
868 size -= i;
869 offset += i;
870 }
871 }
872
873 set_bit(index, &dc->flip);
874 ret = 1;
875put:
876 tty_kref_put(tty);
877 return ret;
878}
879
880/* Debug for interrupts */
881#ifdef DEBUG
882static char *interrupt2str(u16 interrupt)
883{
884 static char buf[TMP_BUF_MAX];
885 char *p = buf;
886
887 interrupt & MDM_DL1 ? p += snprintf(p, TMP_BUF_MAX, "MDM_DL1 ") : NULL;
888 interrupt & MDM_DL2 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
889 "MDM_DL2 ") : NULL;
890
891 interrupt & MDM_UL1 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
892 "MDM_UL1 ") : NULL;
893 interrupt & MDM_UL2 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
894 "MDM_UL2 ") : NULL;
895
896 interrupt & DIAG_DL1 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
897 "DIAG_DL1 ") : NULL;
898 interrupt & DIAG_DL2 ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
899 "DIAG_DL2 ") : NULL;
900
901 interrupt & DIAG_UL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
902 "DIAG_UL ") : NULL;
903
904 interrupt & APP1_DL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
905 "APP1_DL ") : NULL;
906 interrupt & APP2_DL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
907 "APP2_DL ") : NULL;
908
909 interrupt & APP1_UL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
910 "APP1_UL ") : NULL;
911 interrupt & APP2_UL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
912 "APP2_UL ") : NULL;
913
914 interrupt & CTRL_DL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
915 "CTRL_DL ") : NULL;
916 interrupt & CTRL_UL ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
917 "CTRL_UL ") : NULL;
918
919 interrupt & RESET ? p += snprintf(p, TMP_BUF_MAX - (p - buf),
920 "RESET ") : NULL;
921
922 return buf;
923}
924#endif
925
926/*
927 * Receive flow control
928 * Return 1 - If ok, else 0
929 */
930static int receive_flow_control(struct nozomi *dc)
931{
932 enum port_type port = PORT_MDM;
933 struct ctrl_dl ctrl_dl;
934 struct ctrl_dl old_ctrl;
935 u16 enable_ier = 0;
936
937 read_mem32((u32 *) &ctrl_dl, dc->port[PORT_CTRL].dl_addr[CH_A], 2);
938
939 switch (ctrl_dl.port) {
940 case CTRL_CMD:
941 DBG1("The Base Band sends this value as a response to a "
942 "request for IMSI detach sent over the control "
943 "channel uplink (see section 7.6.1).");
944 break;
945 case CTRL_MDM:
946 port = PORT_MDM;
947 enable_ier = MDM_DL;
948 break;
949 case CTRL_DIAG:
950 port = PORT_DIAG;
951 enable_ier = DIAG_DL;
952 break;
953 case CTRL_APP1:
954 port = PORT_APP1;
955 enable_ier = APP1_DL;
956 break;
957 case CTRL_APP2:
958 port = PORT_APP2;
959 enable_ier = APP2_DL;
960 if (dc->state == NOZOMI_STATE_ALLOCATED) {
961 /*
962 * After card initialization the flow control
963 * received for APP2 is always the last
964 */
965 dc->state = NOZOMI_STATE_READY;
966 dev_info(&dc->pdev->dev, "Device READY!\n");
967 }
968 break;
969 default:
970 dev_err(&dc->pdev->dev,
971 "ERROR: flow control received for non-existing port\n");
972 return 0;
973 };
974
975 DBG1("0x%04X->0x%04X", *((u16 *)&dc->port[port].ctrl_dl),
976 *((u16 *)&ctrl_dl));
977
978 old_ctrl = dc->port[port].ctrl_dl;
979 dc->port[port].ctrl_dl = ctrl_dl;
980
981 if (old_ctrl.CTS == 1 && ctrl_dl.CTS == 0) {
982 DBG1("Disable interrupt (0x%04X) on port: %d",
983 enable_ier, port);
984 disable_transmit_ul(port, dc);
985
986 } else if (old_ctrl.CTS == 0 && ctrl_dl.CTS == 1) {
987
988 if (kfifo_len(&dc->port[port].fifo_ul)) {
989 DBG1("Enable interrupt (0x%04X) on port: %d",
990 enable_ier, port);
991 DBG1("Data in buffer [%d], enable transmit! ",
992 kfifo_len(&dc->port[port].fifo_ul));
993 enable_transmit_ul(port, dc);
994 } else {
995 DBG1("No data in buffer...");
996 }
997 }
998
999 if (*(u16 *)&old_ctrl == *(u16 *)&ctrl_dl) {
1000 DBG1(" No change in mctrl");
1001 return 1;
1002 }
1003 /* Update statistics */
1004 if (old_ctrl.CTS != ctrl_dl.CTS)
1005 dc->port[port].tty_icount.cts++;
1006 if (old_ctrl.DSR != ctrl_dl.DSR)
1007 dc->port[port].tty_icount.dsr++;
1008 if (old_ctrl.RI != ctrl_dl.RI)
1009 dc->port[port].tty_icount.rng++;
1010 if (old_ctrl.DCD != ctrl_dl.DCD)
1011 dc->port[port].tty_icount.dcd++;
1012
1013 wake_up_interruptible(&dc->port[port].tty_wait);
1014
1015 DBG1("port: %d DCD(%d), CTS(%d), RI(%d), DSR(%d)",
1016 port,
1017 dc->port[port].tty_icount.dcd, dc->port[port].tty_icount.cts,
1018 dc->port[port].tty_icount.rng, dc->port[port].tty_icount.dsr);
1019
1020 return 1;
1021}
1022
1023static enum ctrl_port_type port2ctrl(enum port_type port,
1024 const struct nozomi *dc)
1025{
1026 switch (port) {
1027 case PORT_MDM:
1028 return CTRL_MDM;
1029 case PORT_DIAG:
1030 return CTRL_DIAG;
1031 case PORT_APP1:
1032 return CTRL_APP1;
1033 case PORT_APP2:
1034 return CTRL_APP2;
1035 default:
1036 dev_err(&dc->pdev->dev,
1037 "ERROR: send flow control " \
1038 "received for non-existing port\n");
1039 };
1040 return CTRL_ERROR;
1041}
1042
1043/*
1044 * Send flow control, can only update one channel at a time
1045 * Return 0 - If we have updated all flow control
1046 * Return 1 - If we need to update more flow control, ack current enable more
1047 */
1048static int send_flow_control(struct nozomi *dc)
1049{
1050 u32 i, more_flow_control_to_be_updated = 0;
1051 u16 *ctrl;
1052
1053 for (i = PORT_MDM; i < MAX_PORT; i++) {
1054 if (dc->port[i].update_flow_control) {
1055 if (more_flow_control_to_be_updated) {
1056 /* We have more flow control to be updated */
1057 return 1;
1058 }
1059 dc->port[i].ctrl_ul.port = port2ctrl(i, dc);
1060 ctrl = (u16 *)&dc->port[i].ctrl_ul;
1061 write_mem32(dc->port[PORT_CTRL].ul_addr[0], \
1062 (u32 *) ctrl, 2);
1063 dc->port[i].update_flow_control = 0;
1064 more_flow_control_to_be_updated = 1;
1065 }
1066 }
1067 return 0;
1068}
1069
1070/*
1071 * Handle downlink data, ports that are handled are modem and diagnostics
1072 * Return 1 - ok
1073 * Return 0 - toggle fields are out of sync
1074 */
1075static int handle_data_dl(struct nozomi *dc, enum port_type port, u8 *toggle,
1076 u16 read_iir, u16 mask1, u16 mask2)
1077{
1078 if (*toggle == 0 && read_iir & mask1) {
1079 if (receive_data(port, dc)) {
1080 writew(mask1, dc->reg_fcr);
1081 *toggle = !(*toggle);
1082 }
1083
1084 if (read_iir & mask2) {
1085 if (receive_data(port, dc)) {
1086 writew(mask2, dc->reg_fcr);
1087 *toggle = !(*toggle);
1088 }
1089 }
1090 } else if (*toggle == 1 && read_iir & mask2) {
1091 if (receive_data(port, dc)) {
1092 writew(mask2, dc->reg_fcr);
1093 *toggle = !(*toggle);
1094 }
1095
1096 if (read_iir & mask1) {
1097 if (receive_data(port, dc)) {
1098 writew(mask1, dc->reg_fcr);
1099 *toggle = !(*toggle);
1100 }
1101 }
1102 } else {
1103 dev_err(&dc->pdev->dev, "port out of sync!, toggle:%d\n",
1104 *toggle);
1105 return 0;
1106 }
1107 return 1;
1108}
1109
1110/*
1111 * Handle uplink data, this is currently for the modem port
1112 * Return 1 - ok
1113 * Return 0 - toggle field are out of sync
1114 */
1115static int handle_data_ul(struct nozomi *dc, enum port_type port, u16 read_iir)
1116{
1117 u8 *toggle = &(dc->port[port].toggle_ul);
1118
1119 if (*toggle == 0 && read_iir & MDM_UL1) {
1120 dc->last_ier &= ~MDM_UL;
1121 writew(dc->last_ier, dc->reg_ier);
1122 if (send_data(port, dc)) {
1123 writew(MDM_UL1, dc->reg_fcr);
1124 dc->last_ier = dc->last_ier | MDM_UL;
1125 writew(dc->last_ier, dc->reg_ier);
1126 *toggle = !*toggle;
1127 }
1128
1129 if (read_iir & MDM_UL2) {
1130 dc->last_ier &= ~MDM_UL;
1131 writew(dc->last_ier, dc->reg_ier);
1132 if (send_data(port, dc)) {
1133 writew(MDM_UL2, dc->reg_fcr);
1134 dc->last_ier = dc->last_ier | MDM_UL;
1135 writew(dc->last_ier, dc->reg_ier);
1136 *toggle = !*toggle;
1137 }
1138 }
1139
1140 } else if (*toggle == 1 && read_iir & MDM_UL2) {
1141 dc->last_ier &= ~MDM_UL;
1142 writew(dc->last_ier, dc->reg_ier);
1143 if (send_data(port, dc)) {
1144 writew(MDM_UL2, dc->reg_fcr);
1145 dc->last_ier = dc->last_ier | MDM_UL;
1146 writew(dc->last_ier, dc->reg_ier);
1147 *toggle = !*toggle;
1148 }
1149
1150 if (read_iir & MDM_UL1) {
1151 dc->last_ier &= ~MDM_UL;
1152 writew(dc->last_ier, dc->reg_ier);
1153 if (send_data(port, dc)) {
1154 writew(MDM_UL1, dc->reg_fcr);
1155 dc->last_ier = dc->last_ier | MDM_UL;
1156 writew(dc->last_ier, dc->reg_ier);
1157 *toggle = !*toggle;
1158 }
1159 }
1160 } else {
1161 writew(read_iir & MDM_UL, dc->reg_fcr);
1162 dev_err(&dc->pdev->dev, "port out of sync!\n");
1163 return 0;
1164 }
1165 return 1;
1166}
1167
1168static irqreturn_t interrupt_handler(int irq, void *dev_id)
1169{
1170 struct nozomi *dc = dev_id;
1171 unsigned int a;
1172 u16 read_iir;
1173
1174 if (!dc)
1175 return IRQ_NONE;
1176
1177 spin_lock(&dc->spin_mutex);
1178 read_iir = readw(dc->reg_iir);
1179
1180 /* Card removed */
1181 if (read_iir == (u16)-1)
1182 goto none;
1183 /*
1184 * Just handle interrupt enabled in IER
1185 * (by masking with dc->last_ier)
1186 */
1187 read_iir &= dc->last_ier;
1188
1189 if (read_iir == 0)
1190 goto none;
1191
1192
1193 DBG4("%s irq:0x%04X, prev:0x%04X", interrupt2str(read_iir), read_iir,
1194 dc->last_ier);
1195
1196 if (read_iir & RESET) {
1197 if (unlikely(!nozomi_read_config_table(dc))) {
1198 dc->last_ier = 0x0;
1199 writew(dc->last_ier, dc->reg_ier);
1200 dev_err(&dc->pdev->dev, "Could not read status from "
1201 "card, we should disable interface\n");
1202 } else {
1203 writew(RESET, dc->reg_fcr);
1204 }
1205 /* No more useful info if this was the reset interrupt. */
1206 goto exit_handler;
1207 }
1208 if (read_iir & CTRL_UL) {
1209 DBG1("CTRL_UL");
1210 dc->last_ier &= ~CTRL_UL;
1211 writew(dc->last_ier, dc->reg_ier);
1212 if (send_flow_control(dc)) {
1213 writew(CTRL_UL, dc->reg_fcr);
1214 dc->last_ier = dc->last_ier | CTRL_UL;
1215 writew(dc->last_ier, dc->reg_ier);
1216 }
1217 }
1218 if (read_iir & CTRL_DL) {
1219 receive_flow_control(dc);
1220 writew(CTRL_DL, dc->reg_fcr);
1221 }
1222 if (read_iir & MDM_DL) {
1223 if (!handle_data_dl(dc, PORT_MDM,
1224 &(dc->port[PORT_MDM].toggle_dl), read_iir,
1225 MDM_DL1, MDM_DL2)) {
1226 dev_err(&dc->pdev->dev, "MDM_DL out of sync!\n");
1227 goto exit_handler;
1228 }
1229 }
1230 if (read_iir & MDM_UL) {
1231 if (!handle_data_ul(dc, PORT_MDM, read_iir)) {
1232 dev_err(&dc->pdev->dev, "MDM_UL out of sync!\n");
1233 goto exit_handler;
1234 }
1235 }
1236 if (read_iir & DIAG_DL) {
1237 if (!handle_data_dl(dc, PORT_DIAG,
1238 &(dc->port[PORT_DIAG].toggle_dl), read_iir,
1239 DIAG_DL1, DIAG_DL2)) {
1240 dev_err(&dc->pdev->dev, "DIAG_DL out of sync!\n");
1241 goto exit_handler;
1242 }
1243 }
1244 if (read_iir & DIAG_UL) {
1245 dc->last_ier &= ~DIAG_UL;
1246 writew(dc->last_ier, dc->reg_ier);
1247 if (send_data(PORT_DIAG, dc)) {
1248 writew(DIAG_UL, dc->reg_fcr);
1249 dc->last_ier = dc->last_ier | DIAG_UL;
1250 writew(dc->last_ier, dc->reg_ier);
1251 }
1252 }
1253 if (read_iir & APP1_DL) {
1254 if (receive_data(PORT_APP1, dc))
1255 writew(APP1_DL, dc->reg_fcr);
1256 }
1257 if (read_iir & APP1_UL) {
1258 dc->last_ier &= ~APP1_UL;
1259 writew(dc->last_ier, dc->reg_ier);
1260 if (send_data(PORT_APP1, dc)) {
1261 writew(APP1_UL, dc->reg_fcr);
1262 dc->last_ier = dc->last_ier | APP1_UL;
1263 writew(dc->last_ier, dc->reg_ier);
1264 }
1265 }
1266 if (read_iir & APP2_DL) {
1267 if (receive_data(PORT_APP2, dc))
1268 writew(APP2_DL, dc->reg_fcr);
1269 }
1270 if (read_iir & APP2_UL) {
1271 dc->last_ier &= ~APP2_UL;
1272 writew(dc->last_ier, dc->reg_ier);
1273 if (send_data(PORT_APP2, dc)) {
1274 writew(APP2_UL, dc->reg_fcr);
1275 dc->last_ier = dc->last_ier | APP2_UL;
1276 writew(dc->last_ier, dc->reg_ier);
1277 }
1278 }
1279
1280exit_handler:
1281 spin_unlock(&dc->spin_mutex);
1282 for (a = 0; a < NOZOMI_MAX_PORTS; a++) {
1283 struct tty_struct *tty;
1284 if (test_and_clear_bit(a, &dc->flip)) {
1285 tty = tty_port_tty_get(&dc->port[a].port);
1286 if (tty)
1287 tty_flip_buffer_push(tty);
1288 tty_kref_put(tty);
1289 }
1290 }
1291 return IRQ_HANDLED;
1292none:
1293 spin_unlock(&dc->spin_mutex);
1294 return IRQ_NONE;
1295}
1296
1297static void nozomi_get_card_type(struct nozomi *dc)
1298{
1299 int i;
1300 u32 size = 0;
1301
1302 for (i = 0; i < 6; i++)
1303 size += pci_resource_len(dc->pdev, i);
1304
1305 /* Assume card type F32_8 if no match */
1306 dc->card_type = size == 2048 ? F32_2 : F32_8;
1307
1308 dev_info(&dc->pdev->dev, "Card type is: %d\n", dc->card_type);
1309}
1310
1311static void nozomi_setup_private_data(struct nozomi *dc)
1312{
1313 void __iomem *offset = dc->base_addr + dc->card_type / 2;
1314 unsigned int i;
1315
1316 dc->reg_fcr = (void __iomem *)(offset + R_FCR);
1317 dc->reg_iir = (void __iomem *)(offset + R_IIR);
1318 dc->reg_ier = (void __iomem *)(offset + R_IER);
1319 dc->last_ier = 0;
1320 dc->flip = 0;
1321
1322 dc->port[PORT_MDM].token_dl = MDM_DL;
1323 dc->port[PORT_DIAG].token_dl = DIAG_DL;
1324 dc->port[PORT_APP1].token_dl = APP1_DL;
1325 dc->port[PORT_APP2].token_dl = APP2_DL;
1326
1327 for (i = 0; i < MAX_PORT; i++)
1328 init_waitqueue_head(&dc->port[i].tty_wait);
1329}
1330
1331static ssize_t card_type_show(struct device *dev, struct device_attribute *attr,
1332 char *buf)
1333{
1334 const struct nozomi *dc = pci_get_drvdata(to_pci_dev(dev));
1335
1336 return sprintf(buf, "%d\n", dc->card_type);
1337}
1338static DEVICE_ATTR(card_type, S_IRUGO, card_type_show, NULL);
1339
1340static ssize_t open_ttys_show(struct device *dev, struct device_attribute *attr,
1341 char *buf)
1342{
1343 const struct nozomi *dc = pci_get_drvdata(to_pci_dev(dev));
1344
1345 return sprintf(buf, "%u\n", dc->open_ttys);
1346}
1347static DEVICE_ATTR(open_ttys, S_IRUGO, open_ttys_show, NULL);
1348
1349static void make_sysfs_files(struct nozomi *dc)
1350{
1351 if (device_create_file(&dc->pdev->dev, &dev_attr_card_type))
1352 dev_err(&dc->pdev->dev,
1353 "Could not create sysfs file for card_type\n");
1354 if (device_create_file(&dc->pdev->dev, &dev_attr_open_ttys))
1355 dev_err(&dc->pdev->dev,
1356 "Could not create sysfs file for open_ttys\n");
1357}
1358
1359static void remove_sysfs_files(struct nozomi *dc)
1360{
1361 device_remove_file(&dc->pdev->dev, &dev_attr_card_type);
1362 device_remove_file(&dc->pdev->dev, &dev_attr_open_ttys);
1363}
1364
1365/* Allocate memory for one device */
1366static int __devinit nozomi_card_init(struct pci_dev *pdev,
1367 const struct pci_device_id *ent)
1368{
1369 resource_size_t start;
1370 int ret;
1371 struct nozomi *dc = NULL;
1372 int ndev_idx;
1373 int i;
1374
1375 dev_dbg(&pdev->dev, "Init, new card found\n");
1376
1377 for (ndev_idx = 0; ndev_idx < ARRAY_SIZE(ndevs); ndev_idx++)
1378 if (!ndevs[ndev_idx])
1379 break;
1380
1381 if (ndev_idx >= ARRAY_SIZE(ndevs)) {
1382 dev_err(&pdev->dev, "no free tty range for this card left\n");
1383 ret = -EIO;
1384 goto err;
1385 }
1386
1387 dc = kzalloc(sizeof(struct nozomi), GFP_KERNEL);
1388 if (unlikely(!dc)) {
1389 dev_err(&pdev->dev, "Could not allocate memory\n");
1390 ret = -ENOMEM;
1391 goto err_free;
1392 }
1393
1394 dc->pdev = pdev;
1395
1396 ret = pci_enable_device(dc->pdev);
1397 if (ret) {
1398 dev_err(&pdev->dev, "Failed to enable PCI Device\n");
1399 goto err_free;
1400 }
1401
1402 ret = pci_request_regions(dc->pdev, NOZOMI_NAME);
1403 if (ret) {
1404 dev_err(&pdev->dev, "I/O address 0x%04x already in use\n",
1405 (int) /* nozomi_private.io_addr */ 0);
1406 goto err_disable_device;
1407 }
1408
1409 start = pci_resource_start(dc->pdev, 0);
1410 if (start == 0) {
1411 dev_err(&pdev->dev, "No I/O address for card detected\n");
1412 ret = -ENODEV;
1413 goto err_rel_regs;
1414 }
1415
1416 /* Find out what card type it is */
1417 nozomi_get_card_type(dc);
1418
1419 dc->base_addr = ioremap_nocache(start, dc->card_type);
1420 if (!dc->base_addr) {
1421 dev_err(&pdev->dev, "Unable to map card MMIO\n");
1422 ret = -ENODEV;
1423 goto err_rel_regs;
1424 }
1425
1426 dc->send_buf = kmalloc(SEND_BUF_MAX, GFP_KERNEL);
1427 if (!dc->send_buf) {
1428 dev_err(&pdev->dev, "Could not allocate send buffer?\n");
1429 ret = -ENOMEM;
1430 goto err_free_sbuf;
1431 }
1432
1433 for (i = PORT_MDM; i < MAX_PORT; i++) {
1434 if (kfifo_alloc(&dc->port[i].fifo_ul,
1435 FIFO_BUFFER_SIZE_UL, GFP_ATOMIC)) {
1436 dev_err(&pdev->dev,
1437 "Could not allocate kfifo buffer\n");
1438 ret = -ENOMEM;
1439 goto err_free_kfifo;
1440 }
1441 }
1442
1443 spin_lock_init(&dc->spin_mutex);
1444
1445 nozomi_setup_private_data(dc);
1446
1447 /* Disable all interrupts */
1448 dc->last_ier = 0;
1449 writew(dc->last_ier, dc->reg_ier);
1450
1451 ret = request_irq(pdev->irq, &interrupt_handler, IRQF_SHARED,
1452 NOZOMI_NAME, dc);
1453 if (unlikely(ret)) {
1454 dev_err(&pdev->dev, "can't request irq %d\n", pdev->irq);
1455 goto err_free_kfifo;
1456 }
1457
1458 DBG1("base_addr: %p", dc->base_addr);
1459
1460 make_sysfs_files(dc);
1461
1462 dc->index_start = ndev_idx * MAX_PORT;
1463 ndevs[ndev_idx] = dc;
1464
1465 pci_set_drvdata(pdev, dc);
1466
1467 /* Enable RESET interrupt */
1468 dc->last_ier = RESET;
1469 iowrite16(dc->last_ier, dc->reg_ier);
1470
1471 dc->state = NOZOMI_STATE_ENABLED;
1472
1473 for (i = 0; i < MAX_PORT; i++) {
1474 struct device *tty_dev;
1475 struct port *port = &dc->port[i];
1476 port->dc = dc;
1477 mutex_init(&port->tty_sem);
1478 tty_port_init(&port->port);
1479 port->port.ops = &noz_tty_port_ops;
1480 tty_dev = tty_register_device(ntty_driver, dc->index_start + i,
1481 &pdev->dev);
1482
1483 if (IS_ERR(tty_dev)) {
1484 ret = PTR_ERR(tty_dev);
1485 dev_err(&pdev->dev, "Could not allocate tty?\n");
1486 goto err_free_tty;
1487 }
1488 }
1489
1490 return 0;
1491
1492err_free_tty:
1493 for (i = dc->index_start; i < dc->index_start + MAX_PORT; ++i)
1494 tty_unregister_device(ntty_driver, i);
1495err_free_kfifo:
1496 for (i = 0; i < MAX_PORT; i++)
1497 kfifo_free(&dc->port[i].fifo_ul);
1498err_free_sbuf:
1499 kfree(dc->send_buf);
1500 iounmap(dc->base_addr);
1501err_rel_regs:
1502 pci_release_regions(pdev);
1503err_disable_device:
1504 pci_disable_device(pdev);
1505err_free:
1506 kfree(dc);
1507err:
1508 return ret;
1509}
1510
1511static void __devexit tty_exit(struct nozomi *dc)
1512{
1513 unsigned int i;
1514
1515 DBG1(" ");
1516
1517 flush_scheduled_work();
1518
1519 for (i = 0; i < MAX_PORT; ++i) {
1520 struct tty_struct *tty = tty_port_tty_get(&dc->port[i].port);
1521 if (tty && list_empty(&tty->hangup_work.entry))
1522 tty_hangup(tty);
1523 tty_kref_put(tty);
1524 }
1525 /* Racy below - surely should wait for scheduled work to be done or
1526 complete off a hangup method ? */
1527 while (dc->open_ttys)
1528 msleep(1);
1529 for (i = dc->index_start; i < dc->index_start + MAX_PORT; ++i)
1530 tty_unregister_device(ntty_driver, i);
1531}
1532
1533/* Deallocate memory for one device */
1534static void __devexit nozomi_card_exit(struct pci_dev *pdev)
1535{
1536 int i;
1537 struct ctrl_ul ctrl;
1538 struct nozomi *dc = pci_get_drvdata(pdev);
1539
1540 /* Disable all interrupts */
1541 dc->last_ier = 0;
1542 writew(dc->last_ier, dc->reg_ier);
1543
1544 tty_exit(dc);
1545
1546 /* Send 0x0001, command card to resend the reset token. */
1547 /* This is to get the reset when the module is reloaded. */
1548 ctrl.port = 0x00;
1549 ctrl.reserved = 0;
1550 ctrl.RTS = 0;
1551 ctrl.DTR = 1;
1552 DBG1("sending flow control 0x%04X", *((u16 *)&ctrl));
1553
1554 /* Setup dc->reg addresses to we can use defines here */
1555 write_mem32(dc->port[PORT_CTRL].ul_addr[0], (u32 *)&ctrl, 2);
1556 writew(CTRL_UL, dc->reg_fcr); /* push the token to the card. */
1557
1558 remove_sysfs_files(dc);
1559
1560 free_irq(pdev->irq, dc);
1561
1562 for (i = 0; i < MAX_PORT; i++)
1563 kfifo_free(&dc->port[i].fifo_ul);
1564
1565 kfree(dc->send_buf);
1566
1567 iounmap(dc->base_addr);
1568
1569 pci_release_regions(pdev);
1570
1571 pci_disable_device(pdev);
1572
1573 ndevs[dc->index_start / MAX_PORT] = NULL;
1574
1575 kfree(dc);
1576}
1577
1578static void set_rts(const struct tty_struct *tty, int rts)
1579{
1580 struct port *port = get_port_by_tty(tty);
1581
1582 port->ctrl_ul.RTS = rts;
1583 port->update_flow_control = 1;
1584 enable_transmit_ul(PORT_CTRL, get_dc_by_tty(tty));
1585}
1586
1587static void set_dtr(const struct tty_struct *tty, int dtr)
1588{
1589 struct port *port = get_port_by_tty(tty);
1590
1591 DBG1("SETTING DTR index: %d, dtr: %d", tty->index, dtr);
1592
1593 port->ctrl_ul.DTR = dtr;
1594 port->update_flow_control = 1;
1595 enable_transmit_ul(PORT_CTRL, get_dc_by_tty(tty));
1596}
1597
1598/*
1599 * ----------------------------------------------------------------------------
1600 * TTY code
1601 * ----------------------------------------------------------------------------
1602 */
1603
1604static int ntty_install(struct tty_driver *driver, struct tty_struct *tty)
1605{
1606 struct port *port = get_port_by_tty(tty);
1607 struct nozomi *dc = get_dc_by_tty(tty);
1608 int ret;
1609 if (!port || !dc || dc->state != NOZOMI_STATE_READY)
1610 return -ENODEV;
1611 ret = tty_init_termios(tty);
1612 if (ret == 0) {
1613 tty_driver_kref_get(driver);
1614 tty->count++;
1615 tty->driver_data = port;
1616 driver->ttys[tty->index] = tty;
1617 }
1618 return ret;
1619}
1620
1621static void ntty_cleanup(struct tty_struct *tty)
1622{
1623 tty->driver_data = NULL;
1624}
1625
1626static int ntty_activate(struct tty_port *tport, struct tty_struct *tty)
1627{
1628 struct port *port = container_of(tport, struct port, port);
1629 struct nozomi *dc = port->dc;
1630 unsigned long flags;
1631
1632 DBG1("open: %d", port->token_dl);
1633 spin_lock_irqsave(&dc->spin_mutex, flags);
1634 dc->last_ier = dc->last_ier | port->token_dl;
1635 writew(dc->last_ier, dc->reg_ier);
1636 dc->open_ttys++;
1637 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1638 printk("noz: activated %d: %p\n", tty->index, tport);
1639 return 0;
1640}
1641
1642static int ntty_open(struct tty_struct *tty, struct file *filp)
1643{
1644 struct port *port = tty->driver_data;
1645 return tty_port_open(&port->port, tty, filp);
1646}
1647
1648static void ntty_shutdown(struct tty_port *tport)
1649{
1650 struct port *port = container_of(tport, struct port, port);
1651 struct nozomi *dc = port->dc;
1652 unsigned long flags;
1653
1654 DBG1("close: %d", port->token_dl);
1655 spin_lock_irqsave(&dc->spin_mutex, flags);
1656 dc->last_ier &= ~(port->token_dl);
1657 writew(dc->last_ier, dc->reg_ier);
1658 dc->open_ttys--;
1659 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1660 printk("noz: shutdown %p\n", tport);
1661}
1662
1663static void ntty_close(struct tty_struct *tty, struct file *filp)
1664{
1665 struct port *port = tty->driver_data;
1666 if (port)
1667 tty_port_close(&port->port, tty, filp);
1668}
1669
1670static void ntty_hangup(struct tty_struct *tty)
1671{
1672 struct port *port = tty->driver_data;
1673 tty_port_hangup(&port->port);
1674}
1675
1676/*
1677 * called when the userspace process writes to the tty (/dev/noz*).
1678 * Data is inserted into a fifo, which is then read and transfered to the modem.
1679 */
1680static int ntty_write(struct tty_struct *tty, const unsigned char *buffer,
1681 int count)
1682{
1683 int rval = -EINVAL;
1684 struct nozomi *dc = get_dc_by_tty(tty);
1685 struct port *port = tty->driver_data;
1686 unsigned long flags;
1687
1688 /* DBG1( "WRITEx: %d, index = %d", count, index); */
1689
1690 if (!dc || !port)
1691 return -ENODEV;
1692
1693 mutex_lock(&port->tty_sem);
1694
1695 if (unlikely(!port->port.count)) {
1696 DBG1(" ");
1697 goto exit;
1698 }
1699
1700 rval = kfifo_in(&port->fifo_ul, (unsigned char *)buffer, count);
1701
1702 /* notify card */
1703 if (unlikely(dc == NULL)) {
1704 DBG1("No device context?");
1705 goto exit;
1706 }
1707
1708 spin_lock_irqsave(&dc->spin_mutex, flags);
1709 /* CTS is only valid on the modem channel */
1710 if (port == &(dc->port[PORT_MDM])) {
1711 if (port->ctrl_dl.CTS) {
1712 DBG4("Enable interrupt");
1713 enable_transmit_ul(tty->index % MAX_PORT, dc);
1714 } else {
1715 dev_err(&dc->pdev->dev,
1716 "CTS not active on modem port?\n");
1717 }
1718 } else {
1719 enable_transmit_ul(tty->index % MAX_PORT, dc);
1720 }
1721 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1722
1723exit:
1724 mutex_unlock(&port->tty_sem);
1725 return rval;
1726}
1727
1728/*
1729 * Calculate how much is left in device
1730 * This method is called by the upper tty layer.
1731 * #according to sources N_TTY.c it expects a value >= 0 and
1732 * does not check for negative values.
1733 *
1734 * If the port is unplugged report lots of room and let the bits
1735 * dribble away so we don't block anything.
1736 */
1737static int ntty_write_room(struct tty_struct *tty)
1738{
1739 struct port *port = tty->driver_data;
1740 int room = 4096;
1741 const struct nozomi *dc = get_dc_by_tty(tty);
1742
1743 if (dc) {
1744 mutex_lock(&port->tty_sem);
1745 if (port->port.count)
1746 room = kfifo_avail(&port->fifo_ul);
1747 mutex_unlock(&port->tty_sem);
1748 }
1749 return room;
1750}
1751
1752/* Gets io control parameters */
1753static int ntty_tiocmget(struct tty_struct *tty, struct file *file)
1754{
1755 const struct port *port = tty->driver_data;
1756 const struct ctrl_dl *ctrl_dl = &port->ctrl_dl;
1757 const struct ctrl_ul *ctrl_ul = &port->ctrl_ul;
1758
1759 /* Note: these could change under us but it is not clear this
1760 matters if so */
1761 return (ctrl_ul->RTS ? TIOCM_RTS : 0) |
1762 (ctrl_ul->DTR ? TIOCM_DTR : 0) |
1763 (ctrl_dl->DCD ? TIOCM_CAR : 0) |
1764 (ctrl_dl->RI ? TIOCM_RNG : 0) |
1765 (ctrl_dl->DSR ? TIOCM_DSR : 0) |
1766 (ctrl_dl->CTS ? TIOCM_CTS : 0);
1767}
1768
1769/* Sets io controls parameters */
1770static int ntty_tiocmset(struct tty_struct *tty, struct file *file,
1771 unsigned int set, unsigned int clear)
1772{
1773 struct nozomi *dc = get_dc_by_tty(tty);
1774 unsigned long flags;
1775
1776 spin_lock_irqsave(&dc->spin_mutex, flags);
1777 if (set & TIOCM_RTS)
1778 set_rts(tty, 1);
1779 else if (clear & TIOCM_RTS)
1780 set_rts(tty, 0);
1781
1782 if (set & TIOCM_DTR)
1783 set_dtr(tty, 1);
1784 else if (clear & TIOCM_DTR)
1785 set_dtr(tty, 0);
1786 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1787
1788 return 0;
1789}
1790
1791static int ntty_cflags_changed(struct port *port, unsigned long flags,
1792 struct async_icount *cprev)
1793{
1794 const struct async_icount cnow = port->tty_icount;
1795 int ret;
1796
1797 ret = ((flags & TIOCM_RNG) && (cnow.rng != cprev->rng)) ||
1798 ((flags & TIOCM_DSR) && (cnow.dsr != cprev->dsr)) ||
1799 ((flags & TIOCM_CD) && (cnow.dcd != cprev->dcd)) ||
1800 ((flags & TIOCM_CTS) && (cnow.cts != cprev->cts));
1801
1802 *cprev = cnow;
1803
1804 return ret;
1805}
1806
1807static int ntty_ioctl_tiocgicount(struct port *port, void __user *argp)
1808{
1809 const struct async_icount cnow = port->tty_icount;
1810 struct serial_icounter_struct icount;
1811
1812 icount.cts = cnow.cts;
1813 icount.dsr = cnow.dsr;
1814 icount.rng = cnow.rng;
1815 icount.dcd = cnow.dcd;
1816 icount.rx = cnow.rx;
1817 icount.tx = cnow.tx;
1818 icount.frame = cnow.frame;
1819 icount.overrun = cnow.overrun;
1820 icount.parity = cnow.parity;
1821 icount.brk = cnow.brk;
1822 icount.buf_overrun = cnow.buf_overrun;
1823
1824 return copy_to_user(argp, &icount, sizeof(icount)) ? -EFAULT : 0;
1825}
1826
1827static int ntty_ioctl(struct tty_struct *tty, struct file *file,
1828 unsigned int cmd, unsigned long arg)
1829{
1830 struct port *port = tty->driver_data;
1831 void __user *argp = (void __user *)arg;
1832 int rval = -ENOIOCTLCMD;
1833
1834 DBG1("******** IOCTL, cmd: %d", cmd);
1835
1836 switch (cmd) {
1837 case TIOCMIWAIT: {
1838 struct async_icount cprev = port->tty_icount;
1839
1840 rval = wait_event_interruptible(port->tty_wait,
1841 ntty_cflags_changed(port, arg, &cprev));
1842 break;
1843 } case TIOCGICOUNT:
1844 rval = ntty_ioctl_tiocgicount(port, argp);
1845 break;
1846 default:
1847 DBG1("ERR: 0x%08X, %d", cmd, cmd);
1848 break;
1849 };
1850
1851 return rval;
1852}
1853
1854/*
1855 * Called by the upper tty layer when tty buffers are ready
1856 * to receive data again after a call to throttle.
1857 */
1858static void ntty_unthrottle(struct tty_struct *tty)
1859{
1860 struct nozomi *dc = get_dc_by_tty(tty);
1861 unsigned long flags;
1862
1863 DBG1("UNTHROTTLE");
1864 spin_lock_irqsave(&dc->spin_mutex, flags);
1865 enable_transmit_dl(tty->index % MAX_PORT, dc);
1866 set_rts(tty, 1);
1867
1868 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1869}
1870
1871/*
1872 * Called by the upper tty layer when the tty buffers are almost full.
1873 * The driver should stop send more data.
1874 */
1875static void ntty_throttle(struct tty_struct *tty)
1876{
1877 struct nozomi *dc = get_dc_by_tty(tty);
1878 unsigned long flags;
1879
1880 DBG1("THROTTLE");
1881 spin_lock_irqsave(&dc->spin_mutex, flags);
1882 set_rts(tty, 0);
1883 spin_unlock_irqrestore(&dc->spin_mutex, flags);
1884}
1885
1886/* Returns number of chars in buffer, called by tty layer */
1887static s32 ntty_chars_in_buffer(struct tty_struct *tty)
1888{
1889 struct port *port = tty->driver_data;
1890 struct nozomi *dc = get_dc_by_tty(tty);
1891 s32 rval = 0;
1892
1893 if (unlikely(!dc || !port)) {
1894 goto exit_in_buffer;
1895 }
1896
1897 if (unlikely(!port->port.count)) {
1898 dev_err(&dc->pdev->dev, "No tty open?\n");
1899 goto exit_in_buffer;
1900 }
1901
1902 rval = kfifo_len(&port->fifo_ul);
1903
1904exit_in_buffer:
1905 return rval;
1906}
1907
1908static const struct tty_port_operations noz_tty_port_ops = {
1909 .activate = ntty_activate,
1910 .shutdown = ntty_shutdown,
1911};
1912
1913static const struct tty_operations tty_ops = {
1914 .ioctl = ntty_ioctl,
1915 .open = ntty_open,
1916 .close = ntty_close,
1917 .hangup = ntty_hangup,
1918 .write = ntty_write,
1919 .write_room = ntty_write_room,
1920 .unthrottle = ntty_unthrottle,
1921 .throttle = ntty_throttle,
1922 .chars_in_buffer = ntty_chars_in_buffer,
1923 .tiocmget = ntty_tiocmget,
1924 .tiocmset = ntty_tiocmset,
1925 .install = ntty_install,
1926 .cleanup = ntty_cleanup,
1927};
1928
1929/* Module initialization */
1930static struct pci_driver nozomi_driver = {
1931 .name = NOZOMI_NAME,
1932 .id_table = nozomi_pci_tbl,
1933 .probe = nozomi_card_init,
1934 .remove = __devexit_p(nozomi_card_exit),
1935};
1936
1937static __init int nozomi_init(void)
1938{
1939 int ret;
1940
1941 printk(KERN_INFO "Initializing %s\n", VERSION_STRING);
1942
1943 ntty_driver = alloc_tty_driver(NTTY_TTY_MAXMINORS);
1944 if (!ntty_driver)
1945 return -ENOMEM;
1946
1947 ntty_driver->owner = THIS_MODULE;
1948 ntty_driver->driver_name = NOZOMI_NAME_TTY;
1949 ntty_driver->name = "noz";
1950 ntty_driver->major = 0;
1951 ntty_driver->type = TTY_DRIVER_TYPE_SERIAL;
1952 ntty_driver->subtype = SERIAL_TYPE_NORMAL;
1953 ntty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
1954 ntty_driver->init_termios = tty_std_termios;
1955 ntty_driver->init_termios.c_cflag = B115200 | CS8 | CREAD | \
1956 HUPCL | CLOCAL;
1957 ntty_driver->init_termios.c_ispeed = 115200;
1958 ntty_driver->init_termios.c_ospeed = 115200;
1959 tty_set_operations(ntty_driver, &tty_ops);
1960
1961 ret = tty_register_driver(ntty_driver);
1962 if (ret) {
1963 printk(KERN_ERR "Nozomi: failed to register ntty driver\n");
1964 goto free_tty;
1965 }
1966
1967 ret = pci_register_driver(&nozomi_driver);
1968 if (ret) {
1969 printk(KERN_ERR "Nozomi: can't register pci driver\n");
1970 goto unr_tty;
1971 }
1972
1973 return 0;
1974unr_tty:
1975 tty_unregister_driver(ntty_driver);
1976free_tty:
1977 put_tty_driver(ntty_driver);
1978 return ret;
1979}
1980
1981static __exit void nozomi_exit(void)
1982{
1983 printk(KERN_INFO "Unloading %s\n", DRIVER_DESC);
1984 pci_unregister_driver(&nozomi_driver);
1985 tty_unregister_driver(ntty_driver);
1986 put_tty_driver(ntty_driver);
1987}
1988
1989module_init(nozomi_init);
1990module_exit(nozomi_exit);
1991
1992module_param(debug, int, S_IRUGO | S_IWUSR);
1993
1994MODULE_LICENSE("Dual BSD/GPL");
1995MODULE_DESCRIPTION(DRIVER_DESC);
diff --git a/drivers/char/nvram.c b/drivers/char/nvram.c
index 66d2917b003f..166f1e7aaa7e 100644
--- a/drivers/char/nvram.c
+++ b/drivers/char/nvram.c
@@ -109,10 +109,11 @@
109#include <linux/spinlock.h> 109#include <linux/spinlock.h>
110#include <linux/io.h> 110#include <linux/io.h>
111#include <linux/uaccess.h> 111#include <linux/uaccess.h>
112#include <linux/smp_lock.h> 112#include <linux/mutex.h>
113 113
114#include <asm/system.h> 114#include <asm/system.h>
115 115
116static DEFINE_MUTEX(nvram_mutex);
116static DEFINE_SPINLOCK(nvram_state_lock); 117static DEFINE_SPINLOCK(nvram_state_lock);
117static int nvram_open_cnt; /* #times opened */ 118static int nvram_open_cnt; /* #times opened */
118static int nvram_open_mode; /* special open modes */ 119static int nvram_open_mode; /* special open modes */
@@ -308,7 +309,7 @@ static long nvram_ioctl(struct file *file, unsigned int cmd,
308 if (!capable(CAP_SYS_ADMIN)) 309 if (!capable(CAP_SYS_ADMIN))
309 return -EACCES; 310 return -EACCES;
310 311
311 lock_kernel(); 312 mutex_lock(&nvram_mutex);
312 spin_lock_irq(&rtc_lock); 313 spin_lock_irq(&rtc_lock);
313 314
314 for (i = 0; i < NVRAM_BYTES; ++i) 315 for (i = 0; i < NVRAM_BYTES; ++i)
@@ -316,7 +317,7 @@ static long nvram_ioctl(struct file *file, unsigned int cmd,
316 __nvram_set_checksum(); 317 __nvram_set_checksum();
317 318
318 spin_unlock_irq(&rtc_lock); 319 spin_unlock_irq(&rtc_lock);
319 unlock_kernel(); 320 mutex_unlock(&nvram_mutex);
320 return 0; 321 return 0;
321 322
322 case NVRAM_SETCKS: 323 case NVRAM_SETCKS:
@@ -325,11 +326,11 @@ static long nvram_ioctl(struct file *file, unsigned int cmd,
325 if (!capable(CAP_SYS_ADMIN)) 326 if (!capable(CAP_SYS_ADMIN))
326 return -EACCES; 327 return -EACCES;
327 328
328 lock_kernel(); 329 mutex_lock(&nvram_mutex);
329 spin_lock_irq(&rtc_lock); 330 spin_lock_irq(&rtc_lock);
330 __nvram_set_checksum(); 331 __nvram_set_checksum();
331 spin_unlock_irq(&rtc_lock); 332 spin_unlock_irq(&rtc_lock);
332 unlock_kernel(); 333 mutex_unlock(&nvram_mutex);
333 return 0; 334 return 0;
334 335
335 default: 336 default:
diff --git a/drivers/char/nwbutton.c b/drivers/char/nwbutton.c
index 2604246501e4..04a480f86c6c 100644
--- a/drivers/char/nwbutton.c
+++ b/drivers/char/nwbutton.c
@@ -75,7 +75,7 @@ int button_add_callback (void (*callback) (void), int count)
75 * with -EINVAL. If there is more than one entry with the same address, 75 * with -EINVAL. If there is more than one entry with the same address,
76 * because it searches the list from end to beginning, it will unregister the 76 * because it searches the list from end to beginning, it will unregister the
77 * last one to be registered first (FILO- First In Last Out). 77 * last one to be registered first (FILO- First In Last Out).
78 * Note that this is not neccessarily true if the entries are not submitted 78 * Note that this is not necessarily true if the entries are not submitted
79 * at the same time, because another driver could have unregistered a callback 79 * at the same time, because another driver could have unregistered a callback
80 * between the submissions creating a gap earlier in the list, which would 80 * between the submissions creating a gap earlier in the list, which would
81 * be filled first at submission time. 81 * be filled first at submission time.
@@ -182,6 +182,7 @@ static int button_read (struct file *filp, char __user *buffer,
182static const struct file_operations button_fops = { 182static const struct file_operations button_fops = {
183 .owner = THIS_MODULE, 183 .owner = THIS_MODULE,
184 .read = button_read, 184 .read = button_read,
185 .llseek = noop_llseek,
185}; 186};
186 187
187/* 188/*
diff --git a/drivers/char/nwflash.c b/drivers/char/nwflash.c
index 043a1c7b86be..a12f52400dbc 100644
--- a/drivers/char/nwflash.c
+++ b/drivers/char/nwflash.c
@@ -25,7 +25,6 @@
25#include <linux/spinlock.h> 25#include <linux/spinlock.h>
26#include <linux/rwsem.h> 26#include <linux/rwsem.h>
27#include <linux/init.h> 27#include <linux/init.h>
28#include <linux/smp_lock.h>
29#include <linux/mutex.h> 28#include <linux/mutex.h>
30#include <linux/jiffies.h> 29#include <linux/jiffies.h>
31 30
@@ -41,6 +40,7 @@
41 40
42#define NWFLASH_VERSION "6.4" 41#define NWFLASH_VERSION "6.4"
43 42
43static DEFINE_MUTEX(flash_mutex);
44static void kick_open(void); 44static void kick_open(void);
45static int get_flash_id(void); 45static int get_flash_id(void);
46static int erase_block(int nBlock); 46static int erase_block(int nBlock);
@@ -96,7 +96,7 @@ static int get_flash_id(void)
96 96
97static long flash_ioctl(struct file *filep, unsigned int cmd, unsigned long arg) 97static long flash_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
98{ 98{
99 lock_kernel(); 99 mutex_lock(&flash_mutex);
100 switch (cmd) { 100 switch (cmd) {
101 case CMD_WRITE_DISABLE: 101 case CMD_WRITE_DISABLE:
102 gbWriteBase64Enable = 0; 102 gbWriteBase64Enable = 0;
@@ -114,10 +114,10 @@ static long flash_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
114 default: 114 default:
115 gbWriteBase64Enable = 0; 115 gbWriteBase64Enable = 0;
116 gbWriteEnable = 0; 116 gbWriteEnable = 0;
117 unlock_kernel(); 117 mutex_unlock(&flash_mutex);
118 return -EINVAL; 118 return -EINVAL;
119 } 119 }
120 unlock_kernel(); 120 mutex_unlock(&flash_mutex);
121 return 0; 121 return 0;
122} 122}
123 123
@@ -282,7 +282,7 @@ static loff_t flash_llseek(struct file *file, loff_t offset, int orig)
282{ 282{
283 loff_t ret; 283 loff_t ret;
284 284
285 lock_kernel(); 285 mutex_lock(&flash_mutex);
286 if (flashdebug) 286 if (flashdebug)
287 printk(KERN_DEBUG "flash_llseek: offset=0x%X, orig=0x%X.\n", 287 printk(KERN_DEBUG "flash_llseek: offset=0x%X, orig=0x%X.\n",
288 (unsigned int) offset, orig); 288 (unsigned int) offset, orig);
@@ -317,7 +317,7 @@ static loff_t flash_llseek(struct file *file, loff_t offset, int orig)
317 default: 317 default:
318 ret = -EINVAL; 318 ret = -EINVAL;
319 } 319 }
320 unlock_kernel(); 320 mutex_unlock(&flash_mutex);
321 return ret; 321 return ret;
322} 322}
323 323
diff --git a/drivers/char/pc8736x_gpio.c b/drivers/char/pc8736x_gpio.c
index 8ecbcc174c15..b304ec052501 100644
--- a/drivers/char/pc8736x_gpio.c
+++ b/drivers/char/pc8736x_gpio.c
@@ -234,6 +234,7 @@ static const struct file_operations pc8736x_gpio_fileops = {
234 .open = pc8736x_gpio_open, 234 .open = pc8736x_gpio_open,
235 .write = nsc_gpio_write, 235 .write = nsc_gpio_write,
236 .read = nsc_gpio_read, 236 .read = nsc_gpio_read,
237 .llseek = no_llseek,
237}; 238};
238 239
239static void __init pc8736x_init_shadow(void) 240static void __init pc8736x_init_shadow(void)
diff --git a/drivers/char/pcmcia/Kconfig b/drivers/char/pcmcia/Kconfig
index ffa0efce0aed..6614416a8623 100644
--- a/drivers/char/pcmcia/Kconfig
+++ b/drivers/char/pcmcia/Kconfig
@@ -28,7 +28,7 @@ config CARDMAN_4000
28 28
29 This kernel driver requires additional userspace support, either 29 This kernel driver requires additional userspace support, either
30 by the vendor-provided PC/SC ifd_handler (http://www.omnikey.com/), 30 by the vendor-provided PC/SC ifd_handler (http://www.omnikey.com/),
31 or via the cm4000 backend of OpenCT (http://www.opensc.com/). 31 or via the cm4000 backend of OpenCT (http://www.opensc-project.org/opensc).
32 32
33config CARDMAN_4040 33config CARDMAN_4040
34 tristate "Omnikey CardMan 4040 support" 34 tristate "Omnikey CardMan 4040 support"
@@ -41,7 +41,7 @@ config CARDMAN_4040
41 in I/O space. To use the kernel driver, you will need either the 41 in I/O space. To use the kernel driver, you will need either the
42 PC/SC ifdhandler provided from the Omnikey homepage 42 PC/SC ifdhandler provided from the Omnikey homepage
43 (http://www.omnikey.com/), or a current development version of OpenCT 43 (http://www.omnikey.com/), or a current development version of OpenCT
44 (http://www.opensc.org/). 44 (http://www.opensc-project.org/opensc).
45 45
46config IPWIRELESS 46config IPWIRELESS
47 tristate "IPWireless 3G UMTS PCMCIA card support" 47 tristate "IPWireless 3G UMTS PCMCIA card support"
diff --git a/drivers/char/pcmcia/Makefile b/drivers/char/pcmcia/Makefile
index be8f287aa398..0aae20985d57 100644
--- a/drivers/char/pcmcia/Makefile
+++ b/drivers/char/pcmcia/Makefile
@@ -4,8 +4,6 @@
4# Makefile for the Linux PCMCIA char device drivers. 4# Makefile for the Linux PCMCIA char device drivers.
5# 5#
6 6
7obj-y += ipwireless/
8
9obj-$(CONFIG_SYNCLINK_CS) += synclink_cs.o 7obj-$(CONFIG_SYNCLINK_CS) += synclink_cs.o
10obj-$(CONFIG_CARDMAN_4000) += cm4000_cs.o 8obj-$(CONFIG_CARDMAN_4000) += cm4000_cs.o
11obj-$(CONFIG_CARDMAN_4040) += cm4040_cs.o 9obj-$(CONFIG_CARDMAN_4040) += cm4040_cs.o
diff --git a/drivers/char/pcmcia/cm4000_cs.c b/drivers/char/pcmcia/cm4000_cs.c
index ec73d9f6d9ed..a7584860e9a7 100644
--- a/drivers/char/pcmcia/cm4000_cs.c
+++ b/drivers/char/pcmcia/cm4000_cs.c
@@ -30,11 +30,10 @@
30#include <linux/fs.h> 30#include <linux/fs.h>
31#include <linux/delay.h> 31#include <linux/delay.h>
32#include <linux/bitrev.h> 32#include <linux/bitrev.h>
33#include <linux/smp_lock.h> 33#include <linux/mutex.h>
34#include <linux/uaccess.h> 34#include <linux/uaccess.h>
35#include <linux/io.h> 35#include <linux/io.h>
36 36
37#include <pcmcia/cs.h>
38#include <pcmcia/cistpl.h> 37#include <pcmcia/cistpl.h>
39#include <pcmcia/cisreg.h> 38#include <pcmcia/cisreg.h>
40#include <pcmcia/ciscode.h> 39#include <pcmcia/ciscode.h>
@@ -55,7 +54,7 @@
55 __func__ , ## args); \ 54 __func__ , ## args); \
56 } while (0) 55 } while (0)
57 56
58static char *version = "cm4000_cs.c v2.4.0gm6 - All bugs added by Harald Welte"; 57static DEFINE_MUTEX(cmm_mutex);
59 58
60#define T_1SEC (HZ) 59#define T_1SEC (HZ)
61#define T_10MSEC msecs_to_jiffies(10) 60#define T_10MSEC msecs_to_jiffies(10)
@@ -807,7 +806,7 @@ static void monitor_card(unsigned long p)
807 dev->flags1 = 0x01; 806 dev->flags1 = 0x01;
808 xoutb(dev->flags1, REG_FLAGS1(iobase)); 807 xoutb(dev->flags1, REG_FLAGS1(iobase));
809 808
810 /* atr is present (which doesnt mean it's valid) */ 809 /* atr is present (which doesn't mean it's valid) */
811 set_bit(IS_ATR_PRESENT, &dev->flags); 810 set_bit(IS_ATR_PRESENT, &dev->flags);
812 if (dev->atr[0] == 0x03) 811 if (dev->atr[0] == 0x03)
813 str_invert_revert(dev->atr, dev->atr_len); 812 str_invert_revert(dev->atr, dev->atr_len);
@@ -831,8 +830,7 @@ static void monitor_card(unsigned long p)
831 test_bit(IS_ANY_T1, &dev->flags))) { 830 test_bit(IS_ANY_T1, &dev->flags))) {
832 DEBUGP(4, dev, "Perform AUTOPPS\n"); 831 DEBUGP(4, dev, "Perform AUTOPPS\n");
833 set_bit(IS_AUTOPPS_ACT, &dev->flags); 832 set_bit(IS_AUTOPPS_ACT, &dev->flags);
834 ptsreq.protocol = ptsreq.protocol = 833 ptsreq.protocol = (0x01 << dev->proto);
835 (0x01 << dev->proto);
836 ptsreq.flags = 0x01; 834 ptsreq.flags = 0x01;
837 ptsreq.pts1 = 0x00; 835 ptsreq.pts1 = 0x00;
838 ptsreq.pts2 = 0x00; 836 ptsreq.pts2 = 0x00;
@@ -980,8 +978,9 @@ static ssize_t cmm_read(struct file *filp, __user char *buf, size_t count,
980 if (dev->flags0 & 1) { 978 if (dev->flags0 & 1) {
981 set_bit(IS_CMM_ABSENT, &dev->flags); 979 set_bit(IS_CMM_ABSENT, &dev->flags);
982 rc = -ENODEV; 980 rc = -ENODEV;
981 } else {
982 rc = -EIO;
983 } 983 }
984 rc = -EIO;
985 goto release_io; 984 goto release_io;
986 } 985 }
987 986
@@ -1418,7 +1417,7 @@ static long cmm_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1418 iminor(inode), ioctl_names[_IOC_NR(cmd)]); 1417 iminor(inode), ioctl_names[_IOC_NR(cmd)]);
1419#endif 1418#endif
1420 1419
1421 lock_kernel(); 1420 mutex_lock(&cmm_mutex);
1422 rc = -ENODEV; 1421 rc = -ENODEV;
1423 link = dev_table[iminor(inode)]; 1422 link = dev_table[iminor(inode)];
1424 if (!pcmcia_dev_present(link)) { 1423 if (!pcmcia_dev_present(link)) {
@@ -1626,7 +1625,7 @@ static long cmm_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1626 rc = -ENOTTY; 1625 rc = -ENOTTY;
1627 } 1626 }
1628out: 1627out:
1629 unlock_kernel(); 1628 mutex_unlock(&cmm_mutex);
1630 return rc; 1629 return rc;
1631} 1630}
1632 1631
@@ -1640,7 +1639,7 @@ static int cmm_open(struct inode *inode, struct file *filp)
1640 if (minor >= CM4000_MAX_DEV) 1639 if (minor >= CM4000_MAX_DEV)
1641 return -ENODEV; 1640 return -ENODEV;
1642 1641
1643 lock_kernel(); 1642 mutex_lock(&cmm_mutex);
1644 link = dev_table[minor]; 1643 link = dev_table[minor];
1645 if (link == NULL || !pcmcia_dev_present(link)) { 1644 if (link == NULL || !pcmcia_dev_present(link)) {
1646 ret = -ENODEV; 1645 ret = -ENODEV;
@@ -1667,7 +1666,7 @@ static int cmm_open(struct inode *inode, struct file *filp)
1667 /* opening will always block since the 1666 /* opening will always block since the
1668 * monitor will be started by open, which 1667 * monitor will be started by open, which
1669 * means we have to wait for ATR becoming 1668 * means we have to wait for ATR becoming
1670 * vaild = block until valid (or card 1669 * valid = block until valid (or card
1671 * inserted) 1670 * inserted)
1672 */ 1671 */
1673 if (filp->f_flags & O_NONBLOCK) { 1672 if (filp->f_flags & O_NONBLOCK) {
@@ -1685,7 +1684,7 @@ static int cmm_open(struct inode *inode, struct file *filp)
1685 DEBUGP(2, dev, "<- cmm_open\n"); 1684 DEBUGP(2, dev, "<- cmm_open\n");
1686 ret = nonseekable_open(inode, filp); 1685 ret = nonseekable_open(inode, filp);
1687out: 1686out:
1688 unlock_kernel(); 1687 mutex_unlock(&cmm_mutex);
1689 return ret; 1688 return ret;
1690} 1689}
1691 1690
@@ -1742,20 +1741,8 @@ static void cmm_cm4000_release(struct pcmcia_device * link)
1742 1741
1743/*==== Interface to PCMCIA Layer =======================================*/ 1742/*==== Interface to PCMCIA Layer =======================================*/
1744 1743
1745static int cm4000_config_check(struct pcmcia_device *p_dev, 1744static int cm4000_config_check(struct pcmcia_device *p_dev, void *priv_data)
1746 cistpl_cftable_entry_t *cfg,
1747 cistpl_cftable_entry_t *dflt,
1748 unsigned int vcc,
1749 void *priv_data)
1750{ 1745{
1751 if (!cfg->io.nwin)
1752 return -ENODEV;
1753
1754 p_dev->resource[0]->start = cfg->io.win[0].base;
1755 p_dev->resource[0]->end = cfg->io.win[0].len;
1756 p_dev->resource[0]->flags |= pcmcia_io_cfg_data_width(cfg->io.flags);
1757 p_dev->io_lines = cfg->io.flags & CISTPL_IO_LINES_MASK;
1758
1759 return pcmcia_request_io(p_dev); 1746 return pcmcia_request_io(p_dev);
1760} 1747}
1761 1748
@@ -1763,13 +1750,13 @@ static int cm4000_config(struct pcmcia_device * link, int devno)
1763{ 1750{
1764 struct cm4000_dev *dev; 1751 struct cm4000_dev *dev;
1765 1752
1753 link->config_flags |= CONF_AUTO_SET_IO;
1754
1766 /* read the config-tuples */ 1755 /* read the config-tuples */
1767 if (pcmcia_loop_config(link, cm4000_config_check, NULL)) 1756 if (pcmcia_loop_config(link, cm4000_config_check, NULL))
1768 goto cs_release; 1757 goto cs_release;
1769 1758
1770 link->conf.IntType = 00000002; 1759 if (pcmcia_enable_device(link))
1771
1772 if (pcmcia_request_configuration(link, &link->conf))
1773 goto cs_release; 1760 goto cs_release;
1774 1761
1775 dev = link->priv; 1762 dev = link->priv;
@@ -1829,7 +1816,6 @@ static int cm4000_probe(struct pcmcia_device *link)
1829 1816
1830 dev->p_dev = link; 1817 dev->p_dev = link;
1831 link->priv = dev; 1818 link->priv = dev;
1832 link->conf.IntType = INT_MEMORY_AND_IO;
1833 dev_table[i] = link; 1819 dev_table[i] = link;
1834 1820
1835 init_waitqueue_head(&dev->devq); 1821 init_waitqueue_head(&dev->devq);
@@ -1880,9 +1866,10 @@ static const struct file_operations cm4000_fops = {
1880 .unlocked_ioctl = cmm_ioctl, 1866 .unlocked_ioctl = cmm_ioctl,
1881 .open = cmm_open, 1867 .open = cmm_open,
1882 .release= cmm_close, 1868 .release= cmm_close,
1869 .llseek = no_llseek,
1883}; 1870};
1884 1871
1885static struct pcmcia_device_id cm4000_ids[] = { 1872static const struct pcmcia_device_id cm4000_ids[] = {
1886 PCMCIA_DEVICE_MANF_CARD(0x0223, 0x0002), 1873 PCMCIA_DEVICE_MANF_CARD(0x0223, 0x0002),
1887 PCMCIA_DEVICE_PROD_ID12("CardMan", "4000", 0x2FB368CA, 0xA2BD8C39), 1874 PCMCIA_DEVICE_PROD_ID12("CardMan", "4000", 0x2FB368CA, 0xA2BD8C39),
1888 PCMCIA_DEVICE_NULL, 1875 PCMCIA_DEVICE_NULL,
@@ -1891,9 +1878,7 @@ MODULE_DEVICE_TABLE(pcmcia, cm4000_ids);
1891 1878
1892static struct pcmcia_driver cm4000_driver = { 1879static struct pcmcia_driver cm4000_driver = {
1893 .owner = THIS_MODULE, 1880 .owner = THIS_MODULE,
1894 .drv = { 1881 .name = "cm4000_cs",
1895 .name = "cm4000_cs",
1896 },
1897 .probe = cm4000_probe, 1882 .probe = cm4000_probe,
1898 .remove = cm4000_detach, 1883 .remove = cm4000_detach,
1899 .suspend = cm4000_suspend, 1884 .suspend = cm4000_suspend,
@@ -1905,8 +1890,6 @@ static int __init cmm_init(void)
1905{ 1890{
1906 int rc; 1891 int rc;
1907 1892
1908 printk(KERN_INFO "%s\n", version);
1909
1910 cmm_class = class_create(THIS_MODULE, "cardman_4000"); 1893 cmm_class = class_create(THIS_MODULE, "cardman_4000");
1911 if (IS_ERR(cmm_class)) 1894 if (IS_ERR(cmm_class))
1912 return PTR_ERR(cmm_class); 1895 return PTR_ERR(cmm_class);
@@ -1931,7 +1914,6 @@ static int __init cmm_init(void)
1931 1914
1932static void __exit cmm_exit(void) 1915static void __exit cmm_exit(void)
1933{ 1916{
1934 printk(KERN_INFO MODULE_NAME ": unloading\n");
1935 pcmcia_unregister_driver(&cm4000_driver); 1917 pcmcia_unregister_driver(&cm4000_driver);
1936 unregister_chrdev(major, DEVICE_NAME); 1918 unregister_chrdev(major, DEVICE_NAME);
1937 class_destroy(cmm_class); 1919 class_destroy(cmm_class);
diff --git a/drivers/char/pcmcia/cm4040_cs.c b/drivers/char/pcmcia/cm4040_cs.c
index 815cde1d0570..8dd48a2be911 100644
--- a/drivers/char/pcmcia/cm4040_cs.c
+++ b/drivers/char/pcmcia/cm4040_cs.c
@@ -24,12 +24,11 @@
24#include <linux/fs.h> 24#include <linux/fs.h>
25#include <linux/delay.h> 25#include <linux/delay.h>
26#include <linux/poll.h> 26#include <linux/poll.h>
27#include <linux/smp_lock.h> 27#include <linux/mutex.h>
28#include <linux/wait.h> 28#include <linux/wait.h>
29#include <asm/uaccess.h> 29#include <asm/uaccess.h>
30#include <asm/io.h> 30#include <asm/io.h>
31 31
32#include <pcmcia/cs.h>
33#include <pcmcia/cistpl.h> 32#include <pcmcia/cistpl.h>
34#include <pcmcia/cisreg.h> 33#include <pcmcia/cisreg.h>
35#include <pcmcia/ciscode.h> 34#include <pcmcia/ciscode.h>
@@ -49,8 +48,7 @@
49 __func__ , ## args); \ 48 __func__ , ## args); \
50 } while (0) 49 } while (0)
51 50
52static char *version = 51static DEFINE_MUTEX(cm4040_mutex);
53"OMNIKEY CardMan 4040 v1.1.0gm5 - All bugs added by Harald Welte";
54 52
55#define CCID_DRIVER_BULK_DEFAULT_TIMEOUT (150*HZ) 53#define CCID_DRIVER_BULK_DEFAULT_TIMEOUT (150*HZ)
56#define CCID_DRIVER_ASYNC_POWERUP_TIMEOUT (35*HZ) 54#define CCID_DRIVER_ASYNC_POWERUP_TIMEOUT (35*HZ)
@@ -444,7 +442,7 @@ static int cm4040_open(struct inode *inode, struct file *filp)
444 if (minor >= CM_MAX_DEV) 442 if (minor >= CM_MAX_DEV)
445 return -ENODEV; 443 return -ENODEV;
446 444
447 lock_kernel(); 445 mutex_lock(&cm4040_mutex);
448 link = dev_table[minor]; 446 link = dev_table[minor];
449 if (link == NULL || !pcmcia_dev_present(link)) { 447 if (link == NULL || !pcmcia_dev_present(link)) {
450 ret = -ENODEV; 448 ret = -ENODEV;
@@ -473,7 +471,7 @@ static int cm4040_open(struct inode *inode, struct file *filp)
473 DEBUGP(2, dev, "<- cm4040_open (successfully)\n"); 471 DEBUGP(2, dev, "<- cm4040_open (successfully)\n");
474 ret = nonseekable_open(inode, filp); 472 ret = nonseekable_open(inode, filp);
475out: 473out:
476 unlock_kernel(); 474 mutex_unlock(&cm4040_mutex);
477 return ret; 475 return ret;
478} 476}
479 477
@@ -516,26 +514,9 @@ static void cm4040_reader_release(struct pcmcia_device *link)
516 return; 514 return;
517} 515}
518 516
519static int cm4040_config_check(struct pcmcia_device *p_dev, 517static int cm4040_config_check(struct pcmcia_device *p_dev, void *priv_data)
520 cistpl_cftable_entry_t *cfg,
521 cistpl_cftable_entry_t *dflt,
522 unsigned int vcc,
523 void *priv_data)
524{ 518{
525 int rc; 519 return pcmcia_request_io(p_dev);
526 if (!cfg->io.nwin)
527 return -ENODEV;
528
529 /* Get the IOaddr */
530 p_dev->resource[0]->start = cfg->io.win[0].base;
531 p_dev->resource[0]->end = cfg->io.win[0].len;
532 p_dev->resource[0]->flags |= pcmcia_io_cfg_data_width(cfg->io.flags);
533 p_dev->io_lines = cfg->io.flags & CISTPL_IO_LINES_MASK;
534 rc = pcmcia_request_io(p_dev);
535
536 dev_printk(KERN_INFO, &p_dev->dev,
537 "pcmcia_request_io returned 0x%x\n", rc);
538 return rc;
539} 520}
540 521
541 522
@@ -544,15 +525,15 @@ static int reader_config(struct pcmcia_device *link, int devno)
544 struct reader_dev *dev; 525 struct reader_dev *dev;
545 int fail_rc; 526 int fail_rc;
546 527
528 link->config_flags |= CONF_AUTO_SET_IO;
529
547 if (pcmcia_loop_config(link, cm4040_config_check, NULL)) 530 if (pcmcia_loop_config(link, cm4040_config_check, NULL))
548 goto cs_release; 531 goto cs_release;
549 532
550 link->conf.IntType = 00000002; 533 fail_rc = pcmcia_enable_device(link);
551
552 fail_rc = pcmcia_request_configuration(link, &link->conf);
553 if (fail_rc != 0) { 534 if (fail_rc != 0) {
554 dev_printk(KERN_INFO, &link->dev, 535 dev_printk(KERN_INFO, &link->dev,
555 "pcmcia_request_configuration failed 0x%x\n", 536 "pcmcia_enable_device failed 0x%x\n",
556 fail_rc); 537 fail_rc);
557 goto cs_release; 538 goto cs_release;
558 } 539 }
@@ -599,7 +580,6 @@ static int reader_probe(struct pcmcia_device *link)
599 link->priv = dev; 580 link->priv = dev;
600 dev->p_dev = link; 581 dev->p_dev = link;
601 582
602 link->conf.IntType = INT_MEMORY_AND_IO;
603 dev_table[i] = link; 583 dev_table[i] = link;
604 584
605 init_waitqueue_head(&dev->devq); 585 init_waitqueue_head(&dev->devq);
@@ -650,9 +630,10 @@ static const struct file_operations reader_fops = {
650 .open = cm4040_open, 630 .open = cm4040_open,
651 .release = cm4040_close, 631 .release = cm4040_close,
652 .poll = cm4040_poll, 632 .poll = cm4040_poll,
633 .llseek = no_llseek,
653}; 634};
654 635
655static struct pcmcia_device_id cm4040_ids[] = { 636static const struct pcmcia_device_id cm4040_ids[] = {
656 PCMCIA_DEVICE_MANF_CARD(0x0223, 0x0200), 637 PCMCIA_DEVICE_MANF_CARD(0x0223, 0x0200),
657 PCMCIA_DEVICE_PROD_ID12("OMNIKEY", "CardMan 4040", 638 PCMCIA_DEVICE_PROD_ID12("OMNIKEY", "CardMan 4040",
658 0xE32CDD8C, 0x8F23318B), 639 0xE32CDD8C, 0x8F23318B),
@@ -662,9 +643,7 @@ MODULE_DEVICE_TABLE(pcmcia, cm4040_ids);
662 643
663static struct pcmcia_driver reader_driver = { 644static struct pcmcia_driver reader_driver = {
664 .owner = THIS_MODULE, 645 .owner = THIS_MODULE,
665 .drv = { 646 .name = "cm4040_cs",
666 .name = "cm4040_cs",
667 },
668 .probe = reader_probe, 647 .probe = reader_probe,
669 .remove = reader_detach, 648 .remove = reader_detach,
670 .id_table = cm4040_ids, 649 .id_table = cm4040_ids,
@@ -674,7 +653,6 @@ static int __init cm4040_init(void)
674{ 653{
675 int rc; 654 int rc;
676 655
677 printk(KERN_INFO "%s\n", version);
678 cmx_class = class_create(THIS_MODULE, "cardman_4040"); 656 cmx_class = class_create(THIS_MODULE, "cardman_4040");
679 if (IS_ERR(cmx_class)) 657 if (IS_ERR(cmx_class))
680 return PTR_ERR(cmx_class); 658 return PTR_ERR(cmx_class);
@@ -699,7 +677,6 @@ static int __init cm4040_init(void)
699 677
700static void __exit cm4040_exit(void) 678static void __exit cm4040_exit(void)
701{ 679{
702 printk(KERN_INFO MODULE_NAME ": unloading\n");
703 pcmcia_unregister_driver(&reader_driver); 680 pcmcia_unregister_driver(&reader_driver);
704 unregister_chrdev(major, DEVICE_NAME); 681 unregister_chrdev(major, DEVICE_NAME);
705 class_destroy(cmx_class); 682 class_destroy(cmx_class);
diff --git a/drivers/char/pcmcia/ipwireless/Makefile b/drivers/char/pcmcia/ipwireless/Makefile
deleted file mode 100644
index b71eb593643d..000000000000
--- a/drivers/char/pcmcia/ipwireless/Makefile
+++ /dev/null
@@ -1,10 +0,0 @@
1#
2# drivers/char/pcmcia/ipwireless/Makefile
3#
4# Makefile for the IPWireless driver
5#
6
7obj-$(CONFIG_IPWIRELESS) += ipwireless.o
8
9ipwireless-objs := hardware.o main.o network.o tty.o
10
diff --git a/drivers/char/pcmcia/ipwireless/hardware.c b/drivers/char/pcmcia/ipwireless/hardware.c
deleted file mode 100644
index 99cffdab1056..000000000000
--- a/drivers/char/pcmcia/ipwireless/hardware.c
+++ /dev/null
@@ -1,1764 +0,0 @@
1/*
2 * IPWireless 3G PCMCIA Network Driver
3 *
4 * Original code
5 * by Stephen Blackheath <stephen@blacksapphire.com>,
6 * Ben Martel <benm@symmetric.co.nz>
7 *
8 * Copyrighted as follows:
9 * Copyright (C) 2004 by Symmetric Systems Ltd (NZ)
10 *
11 * Various driver changes and rewrites, port to new kernels
12 * Copyright (C) 2006-2007 Jiri Kosina
13 *
14 * Misc code cleanups and updates
15 * Copyright (C) 2007 David Sterba
16 */
17
18#include <linux/interrupt.h>
19#include <linux/io.h>
20#include <linux/irq.h>
21#include <linux/kernel.h>
22#include <linux/list.h>
23#include <linux/slab.h>
24
25#include "hardware.h"
26#include "setup_protocol.h"
27#include "network.h"
28#include "main.h"
29
30static void ipw_send_setup_packet(struct ipw_hardware *hw);
31static void handle_received_SETUP_packet(struct ipw_hardware *ipw,
32 unsigned int address,
33 const unsigned char *data, int len,
34 int is_last);
35static void ipwireless_setup_timer(unsigned long data);
36static void handle_received_CTRL_packet(struct ipw_hardware *hw,
37 unsigned int channel_idx, const unsigned char *data, int len);
38
39/*#define TIMING_DIAGNOSTICS*/
40
41#ifdef TIMING_DIAGNOSTICS
42
43static struct timing_stats {
44 unsigned long last_report_time;
45 unsigned long read_time;
46 unsigned long write_time;
47 unsigned long read_bytes;
48 unsigned long write_bytes;
49 unsigned long start_time;
50};
51
52static void start_timing(void)
53{
54 timing_stats.start_time = jiffies;
55}
56
57static void end_read_timing(unsigned length)
58{
59 timing_stats.read_time += (jiffies - start_time);
60 timing_stats.read_bytes += length + 2;
61 report_timing();
62}
63
64static void end_write_timing(unsigned length)
65{
66 timing_stats.write_time += (jiffies - start_time);
67 timing_stats.write_bytes += length + 2;
68 report_timing();
69}
70
71static void report_timing(void)
72{
73 unsigned long since = jiffies - timing_stats.last_report_time;
74
75 /* If it's been more than one second... */
76 if (since >= HZ) {
77 int first = (timing_stats.last_report_time == 0);
78
79 timing_stats.last_report_time = jiffies;
80 if (!first)
81 printk(KERN_INFO IPWIRELESS_PCCARD_NAME
82 ": %u us elapsed - read %lu bytes in %u us, wrote %lu bytes in %u us\n",
83 jiffies_to_usecs(since),
84 timing_stats.read_bytes,
85 jiffies_to_usecs(timing_stats.read_time),
86 timing_stats.write_bytes,
87 jiffies_to_usecs(timing_stats.write_time));
88
89 timing_stats.read_time = 0;
90 timing_stats.write_time = 0;
91 timing_stats.read_bytes = 0;
92 timing_stats.write_bytes = 0;
93 }
94}
95#else
96static void start_timing(void) { }
97static void end_read_timing(unsigned length) { }
98static void end_write_timing(unsigned length) { }
99#endif
100
101/* Imported IPW definitions */
102
103#define LL_MTU_V1 318
104#define LL_MTU_V2 250
105#define LL_MTU_MAX (LL_MTU_V1 > LL_MTU_V2 ? LL_MTU_V1 : LL_MTU_V2)
106
107#define PRIO_DATA 2
108#define PRIO_CTRL 1
109#define PRIO_SETUP 0
110
111/* Addresses */
112#define ADDR_SETUP_PROT 0
113
114/* Protocol ids */
115enum {
116 /* Identifier for the Com Data protocol */
117 TL_PROTOCOLID_COM_DATA = 0,
118
119 /* Identifier for the Com Control protocol */
120 TL_PROTOCOLID_COM_CTRL = 1,
121
122 /* Identifier for the Setup protocol */
123 TL_PROTOCOLID_SETUP = 2
124};
125
126/* Number of bytes in NL packet header (cannot do
127 * sizeof(nl_packet_header) since it's a bitfield) */
128#define NL_FIRST_PACKET_HEADER_SIZE 3
129
130/* Number of bytes in NL packet header (cannot do
131 * sizeof(nl_packet_header) since it's a bitfield) */
132#define NL_FOLLOWING_PACKET_HEADER_SIZE 1
133
134struct nl_first_packet_header {
135 unsigned char protocol:3;
136 unsigned char address:3;
137 unsigned char packet_rank:2;
138 unsigned char length_lsb;
139 unsigned char length_msb;
140};
141
142struct nl_packet_header {
143 unsigned char protocol:3;
144 unsigned char address:3;
145 unsigned char packet_rank:2;
146};
147
148/* Value of 'packet_rank' above */
149#define NL_INTERMEDIATE_PACKET 0x0
150#define NL_LAST_PACKET 0x1
151#define NL_FIRST_PACKET 0x2
152
153union nl_packet {
154 /* Network packet header of the first packet (a special case) */
155 struct nl_first_packet_header hdr_first;
156 /* Network packet header of the following packets (if any) */
157 struct nl_packet_header hdr;
158 /* Complete network packet (header + data) */
159 unsigned char rawpkt[LL_MTU_MAX];
160} __attribute__ ((__packed__));
161
162#define HW_VERSION_UNKNOWN -1
163#define HW_VERSION_1 1
164#define HW_VERSION_2 2
165
166/* IPW I/O ports */
167#define IOIER 0x00 /* Interrupt Enable Register */
168#define IOIR 0x02 /* Interrupt Source/ACK register */
169#define IODCR 0x04 /* Data Control Register */
170#define IODRR 0x06 /* Data Read Register */
171#define IODWR 0x08 /* Data Write Register */
172#define IOESR 0x0A /* Embedded Driver Status Register */
173#define IORXR 0x0C /* Rx Fifo Register (Host to Embedded) */
174#define IOTXR 0x0E /* Tx Fifo Register (Embedded to Host) */
175
176/* I/O ports and bit definitions for version 1 of the hardware */
177
178/* IER bits*/
179#define IER_RXENABLED 0x1
180#define IER_TXENABLED 0x2
181
182/* ISR bits */
183#define IR_RXINTR 0x1
184#define IR_TXINTR 0x2
185
186/* DCR bits */
187#define DCR_RXDONE 0x1
188#define DCR_TXDONE 0x2
189#define DCR_RXRESET 0x4
190#define DCR_TXRESET 0x8
191
192/* I/O ports and bit definitions for version 2 of the hardware */
193
194struct MEMCCR {
195 unsigned short reg_config_option; /* PCCOR: Configuration Option Register */
196 unsigned short reg_config_and_status; /* PCCSR: Configuration and Status Register */
197 unsigned short reg_pin_replacement; /* PCPRR: Pin Replacemant Register */
198 unsigned short reg_socket_and_copy; /* PCSCR: Socket and Copy Register */
199 unsigned short reg_ext_status; /* PCESR: Extendend Status Register */
200 unsigned short reg_io_base; /* PCIOB: I/O Base Register */
201};
202
203struct MEMINFREG {
204 unsigned short memreg_tx_old; /* TX Register (R/W) */
205 unsigned short pad1;
206 unsigned short memreg_rx_done; /* RXDone Register (R/W) */
207 unsigned short pad2;
208 unsigned short memreg_rx; /* RX Register (R/W) */
209 unsigned short pad3;
210 unsigned short memreg_pc_interrupt_ack; /* PC intr Ack Register (W) */
211 unsigned short pad4;
212 unsigned long memreg_card_present;/* Mask for Host to check (R) for
213 * CARD_PRESENT_VALUE */
214 unsigned short memreg_tx_new; /* TX2 (new) Register (R/W) */
215};
216
217#define CARD_PRESENT_VALUE (0xBEEFCAFEUL)
218
219#define MEMTX_TX 0x0001
220#define MEMRX_RX 0x0001
221#define MEMRX_RX_DONE 0x0001
222#define MEMRX_PCINTACKK 0x0001
223
224#define NL_NUM_OF_PRIORITIES 3
225#define NL_NUM_OF_PROTOCOLS 3
226#define NL_NUM_OF_ADDRESSES NO_OF_IPW_CHANNELS
227
228struct ipw_hardware {
229 unsigned int base_port;
230 short hw_version;
231 unsigned short ll_mtu;
232 spinlock_t lock;
233
234 int initializing;
235 int init_loops;
236 struct timer_list setup_timer;
237
238 /* Flag if hw is ready to send next packet */
239 int tx_ready;
240 /* Count of pending packets to be sent */
241 int tx_queued;
242 struct list_head tx_queue[NL_NUM_OF_PRIORITIES];
243
244 int rx_bytes_queued;
245 struct list_head rx_queue;
246 /* Pool of rx_packet structures that are not currently used. */
247 struct list_head rx_pool;
248 int rx_pool_size;
249 /* True if reception of data is blocked while userspace processes it. */
250 int blocking_rx;
251 /* True if there is RX data ready on the hardware. */
252 int rx_ready;
253 unsigned short last_memtx_serial;
254 /*
255 * Newer versions of the V2 card firmware send serial numbers in the
256 * MemTX register. 'serial_number_detected' is set true when we detect
257 * a non-zero serial number (indicating the new firmware). Thereafter,
258 * the driver can safely ignore the Timer Recovery re-sends to avoid
259 * out-of-sync problems.
260 */
261 int serial_number_detected;
262 struct work_struct work_rx;
263
264 /* True if we are to send the set-up data to the hardware. */
265 int to_setup;
266
267 /* Card has been removed */
268 int removed;
269 /* Saved irq value when we disable the interrupt. */
270 int irq;
271 /* True if this driver is shutting down. */
272 int shutting_down;
273 /* Modem control lines */
274 unsigned int control_lines[NL_NUM_OF_ADDRESSES];
275 struct ipw_rx_packet *packet_assembler[NL_NUM_OF_ADDRESSES];
276
277 struct tasklet_struct tasklet;
278
279 /* The handle for the network layer, for the sending of events to it. */
280 struct ipw_network *network;
281 struct MEMINFREG __iomem *memory_info_regs;
282 struct MEMCCR __iomem *memregs_CCR;
283 void (*reboot_callback) (void *data);
284 void *reboot_callback_data;
285
286 unsigned short __iomem *memreg_tx;
287};
288
289/*
290 * Packet info structure for tx packets.
291 * Note: not all the fields defined here are required for all protocols
292 */
293struct ipw_tx_packet {
294 struct list_head queue;
295 /* channel idx + 1 */
296 unsigned char dest_addr;
297 /* SETUP, CTRL or DATA */
298 unsigned char protocol;
299 /* Length of data block, which starts at the end of this structure */
300 unsigned short length;
301 /* Sending state */
302 /* Offset of where we've sent up to so far */
303 unsigned long offset;
304 /* Count of packet fragments, starting at 0 */
305 int fragment_count;
306
307 /* Called after packet is sent and before is freed */
308 void (*packet_callback) (void *cb_data, unsigned int packet_length);
309 void *callback_data;
310};
311
312/* Signals from DTE */
313#define COMCTRL_RTS 0
314#define COMCTRL_DTR 1
315
316/* Signals from DCE */
317#define COMCTRL_CTS 2
318#define COMCTRL_DCD 3
319#define COMCTRL_DSR 4
320#define COMCTRL_RI 5
321
322struct ipw_control_packet_body {
323 /* DTE signal or DCE signal */
324 unsigned char sig_no;
325 /* 0: set signal, 1: clear signal */
326 unsigned char value;
327} __attribute__ ((__packed__));
328
329struct ipw_control_packet {
330 struct ipw_tx_packet header;
331 struct ipw_control_packet_body body;
332};
333
334struct ipw_rx_packet {
335 struct list_head queue;
336 unsigned int capacity;
337 unsigned int length;
338 unsigned int protocol;
339 unsigned int channel_idx;
340};
341
342static char *data_type(const unsigned char *buf, unsigned length)
343{
344 struct nl_packet_header *hdr = (struct nl_packet_header *) buf;
345
346 if (length == 0)
347 return " ";
348
349 if (hdr->packet_rank & NL_FIRST_PACKET) {
350 switch (hdr->protocol) {
351 case TL_PROTOCOLID_COM_DATA: return "DATA ";
352 case TL_PROTOCOLID_COM_CTRL: return "CTRL ";
353 case TL_PROTOCOLID_SETUP: return "SETUP";
354 default: return "???? ";
355 }
356 } else
357 return " ";
358}
359
360#define DUMP_MAX_BYTES 64
361
362static void dump_data_bytes(const char *type, const unsigned char *data,
363 unsigned length)
364{
365 char prefix[56];
366
367 sprintf(prefix, IPWIRELESS_PCCARD_NAME ": %s %s ",
368 type, data_type(data, length));
369 print_hex_dump_bytes(prefix, 0, (void *)data,
370 length < DUMP_MAX_BYTES ? length : DUMP_MAX_BYTES);
371}
372
373static void swap_packet_bitfield_to_le(unsigned char *data)
374{
375#ifdef __BIG_ENDIAN_BITFIELD
376 unsigned char tmp = *data, ret = 0;
377
378 /*
379 * transform bits from aa.bbb.ccc to ccc.bbb.aa
380 */
381 ret |= tmp & 0xc0 >> 6;
382 ret |= tmp & 0x38 >> 1;
383 ret |= tmp & 0x07 << 5;
384 *data = ret & 0xff;
385#endif
386}
387
388static void swap_packet_bitfield_from_le(unsigned char *data)
389{
390#ifdef __BIG_ENDIAN_BITFIELD
391 unsigned char tmp = *data, ret = 0;
392
393 /*
394 * transform bits from ccc.bbb.aa to aa.bbb.ccc
395 */
396 ret |= tmp & 0xe0 >> 5;
397 ret |= tmp & 0x1c << 1;
398 ret |= tmp & 0x03 << 6;
399 *data = ret & 0xff;
400#endif
401}
402
403static void do_send_fragment(struct ipw_hardware *hw, unsigned char *data,
404 unsigned length)
405{
406 unsigned i;
407 unsigned long flags;
408
409 start_timing();
410 BUG_ON(length > hw->ll_mtu);
411
412 if (ipwireless_debug)
413 dump_data_bytes("send", data, length);
414
415 spin_lock_irqsave(&hw->lock, flags);
416
417 hw->tx_ready = 0;
418 swap_packet_bitfield_to_le(data);
419
420 if (hw->hw_version == HW_VERSION_1) {
421 outw((unsigned short) length, hw->base_port + IODWR);
422
423 for (i = 0; i < length; i += 2) {
424 unsigned short d = data[i];
425 __le16 raw_data;
426
427 if (i + 1 < length)
428 d |= data[i + 1] << 8;
429 raw_data = cpu_to_le16(d);
430 outw(raw_data, hw->base_port + IODWR);
431 }
432
433 outw(DCR_TXDONE, hw->base_port + IODCR);
434 } else if (hw->hw_version == HW_VERSION_2) {
435 outw((unsigned short) length, hw->base_port);
436
437 for (i = 0; i < length; i += 2) {
438 unsigned short d = data[i];
439 __le16 raw_data;
440
441 if (i + 1 < length)
442 d |= data[i + 1] << 8;
443 raw_data = cpu_to_le16(d);
444 outw(raw_data, hw->base_port);
445 }
446 while ((i & 3) != 2) {
447 outw((unsigned short) 0xDEAD, hw->base_port);
448 i += 2;
449 }
450 writew(MEMRX_RX, &hw->memory_info_regs->memreg_rx);
451 }
452
453 spin_unlock_irqrestore(&hw->lock, flags);
454
455 end_write_timing(length);
456}
457
458static void do_send_packet(struct ipw_hardware *hw, struct ipw_tx_packet *packet)
459{
460 unsigned short fragment_data_len;
461 unsigned short data_left = packet->length - packet->offset;
462 unsigned short header_size;
463 union nl_packet pkt;
464
465 header_size =
466 (packet->fragment_count == 0)
467 ? NL_FIRST_PACKET_HEADER_SIZE
468 : NL_FOLLOWING_PACKET_HEADER_SIZE;
469 fragment_data_len = hw->ll_mtu - header_size;
470 if (data_left < fragment_data_len)
471 fragment_data_len = data_left;
472
473 /*
474 * hdr_first is now in machine bitfield order, which will be swapped
475 * to le just before it goes to hw
476 */
477 pkt.hdr_first.protocol = packet->protocol;
478 pkt.hdr_first.address = packet->dest_addr;
479 pkt.hdr_first.packet_rank = 0;
480
481 /* First packet? */
482 if (packet->fragment_count == 0) {
483 pkt.hdr_first.packet_rank |= NL_FIRST_PACKET;
484 pkt.hdr_first.length_lsb = (unsigned char) packet->length;
485 pkt.hdr_first.length_msb =
486 (unsigned char) (packet->length >> 8);
487 }
488
489 memcpy(pkt.rawpkt + header_size,
490 ((unsigned char *) packet) + sizeof(struct ipw_tx_packet) +
491 packet->offset, fragment_data_len);
492 packet->offset += fragment_data_len;
493 packet->fragment_count++;
494
495 /* Last packet? (May also be first packet.) */
496 if (packet->offset == packet->length)
497 pkt.hdr_first.packet_rank |= NL_LAST_PACKET;
498 do_send_fragment(hw, pkt.rawpkt, header_size + fragment_data_len);
499
500 /* If this packet has unsent data, then re-queue it. */
501 if (packet->offset < packet->length) {
502 /*
503 * Re-queue it at the head of the highest priority queue so
504 * it goes before all other packets
505 */
506 unsigned long flags;
507
508 spin_lock_irqsave(&hw->lock, flags);
509 list_add(&packet->queue, &hw->tx_queue[0]);
510 hw->tx_queued++;
511 spin_unlock_irqrestore(&hw->lock, flags);
512 } else {
513 if (packet->packet_callback)
514 packet->packet_callback(packet->callback_data,
515 packet->length);
516 kfree(packet);
517 }
518}
519
520static void ipw_setup_hardware(struct ipw_hardware *hw)
521{
522 unsigned long flags;
523
524 spin_lock_irqsave(&hw->lock, flags);
525 if (hw->hw_version == HW_VERSION_1) {
526 /* Reset RX FIFO */
527 outw(DCR_RXRESET, hw->base_port + IODCR);
528 /* SB: Reset TX FIFO */
529 outw(DCR_TXRESET, hw->base_port + IODCR);
530
531 /* Enable TX and RX interrupts. */
532 outw(IER_TXENABLED | IER_RXENABLED, hw->base_port + IOIER);
533 } else {
534 /*
535 * Set INTRACK bit (bit 0), which means we must explicitly
536 * acknowledge interrupts by clearing bit 2 of reg_config_and_status.
537 */
538 unsigned short csr = readw(&hw->memregs_CCR->reg_config_and_status);
539
540 csr |= 1;
541 writew(csr, &hw->memregs_CCR->reg_config_and_status);
542 }
543 spin_unlock_irqrestore(&hw->lock, flags);
544}
545
546/*
547 * If 'packet' is NULL, then this function allocates a new packet, setting its
548 * length to 0 and ensuring it has the specified minimum amount of free space.
549 *
550 * If 'packet' is not NULL, then this function enlarges it if it doesn't
551 * have the specified minimum amount of free space.
552 *
553 */
554static struct ipw_rx_packet *pool_allocate(struct ipw_hardware *hw,
555 struct ipw_rx_packet *packet,
556 int minimum_free_space)
557{
558
559 if (!packet) {
560 unsigned long flags;
561
562 spin_lock_irqsave(&hw->lock, flags);
563 if (!list_empty(&hw->rx_pool)) {
564 packet = list_first_entry(&hw->rx_pool,
565 struct ipw_rx_packet, queue);
566 hw->rx_pool_size--;
567 spin_unlock_irqrestore(&hw->lock, flags);
568 list_del(&packet->queue);
569 } else {
570 const int min_capacity =
571 ipwireless_ppp_mru(hw->network) + 2;
572 int new_capacity;
573
574 spin_unlock_irqrestore(&hw->lock, flags);
575 new_capacity =
576 (minimum_free_space > min_capacity
577 ? minimum_free_space
578 : min_capacity);
579 packet = kmalloc(sizeof(struct ipw_rx_packet)
580 + new_capacity, GFP_ATOMIC);
581 if (!packet)
582 return NULL;
583 packet->capacity = new_capacity;
584 }
585 packet->length = 0;
586 }
587
588 if (packet->length + minimum_free_space > packet->capacity) {
589 struct ipw_rx_packet *old_packet = packet;
590
591 packet = kmalloc(sizeof(struct ipw_rx_packet) +
592 old_packet->length + minimum_free_space,
593 GFP_ATOMIC);
594 if (!packet) {
595 kfree(old_packet);
596 return NULL;
597 }
598 memcpy(packet, old_packet,
599 sizeof(struct ipw_rx_packet)
600 + old_packet->length);
601 packet->capacity = old_packet->length + minimum_free_space;
602 kfree(old_packet);
603 }
604
605 return packet;
606}
607
608static void pool_free(struct ipw_hardware *hw, struct ipw_rx_packet *packet)
609{
610 if (hw->rx_pool_size > 6)
611 kfree(packet);
612 else {
613 hw->rx_pool_size++;
614 list_add(&packet->queue, &hw->rx_pool);
615 }
616}
617
618static void queue_received_packet(struct ipw_hardware *hw,
619 unsigned int protocol,
620 unsigned int address,
621 const unsigned char *data, int length,
622 int is_last)
623{
624 unsigned int channel_idx = address - 1;
625 struct ipw_rx_packet *packet = NULL;
626 unsigned long flags;
627
628 /* Discard packet if channel index is out of range. */
629 if (channel_idx >= NL_NUM_OF_ADDRESSES) {
630 printk(KERN_INFO IPWIRELESS_PCCARD_NAME
631 ": data packet has bad address %u\n", address);
632 return;
633 }
634
635 /*
636 * ->packet_assembler is safe to touch unlocked, this is the only place
637 */
638 if (protocol == TL_PROTOCOLID_COM_DATA) {
639 struct ipw_rx_packet **assem =
640 &hw->packet_assembler[channel_idx];
641
642 /*
643 * Create a new packet, or assembler already contains one
644 * enlarge it by 'length' bytes.
645 */
646 (*assem) = pool_allocate(hw, *assem, length);
647 if (!(*assem)) {
648 printk(KERN_ERR IPWIRELESS_PCCARD_NAME
649 ": no memory for incomming data packet, dropped!\n");
650 return;
651 }
652 (*assem)->protocol = protocol;
653 (*assem)->channel_idx = channel_idx;
654
655 /* Append this packet data onto existing data. */
656 memcpy((unsigned char *)(*assem) +
657 sizeof(struct ipw_rx_packet)
658 + (*assem)->length, data, length);
659 (*assem)->length += length;
660 if (is_last) {
661 packet = *assem;
662 *assem = NULL;
663 /* Count queued DATA bytes only */
664 spin_lock_irqsave(&hw->lock, flags);
665 hw->rx_bytes_queued += packet->length;
666 spin_unlock_irqrestore(&hw->lock, flags);
667 }
668 } else {
669 /* If it's a CTRL packet, don't assemble, just queue it. */
670 packet = pool_allocate(hw, NULL, length);
671 if (!packet) {
672 printk(KERN_ERR IPWIRELESS_PCCARD_NAME
673 ": no memory for incomming ctrl packet, dropped!\n");
674 return;
675 }
676 packet->protocol = protocol;
677 packet->channel_idx = channel_idx;
678 memcpy((unsigned char *)packet + sizeof(struct ipw_rx_packet),
679 data, length);
680 packet->length = length;
681 }
682
683 /*
684 * If this is the last packet, then send the assembled packet on to the
685 * network layer.
686 */
687 if (packet) {
688 spin_lock_irqsave(&hw->lock, flags);
689 list_add_tail(&packet->queue, &hw->rx_queue);
690 /* Block reception of incoming packets if queue is full. */
691 hw->blocking_rx =
692 (hw->rx_bytes_queued >= IPWIRELESS_RX_QUEUE_SIZE);
693
694 spin_unlock_irqrestore(&hw->lock, flags);
695 schedule_work(&hw->work_rx);
696 }
697}
698
699/*
700 * Workqueue callback
701 */
702static void ipw_receive_data_work(struct work_struct *work_rx)
703{
704 struct ipw_hardware *hw =
705 container_of(work_rx, struct ipw_hardware, work_rx);
706 unsigned long flags;
707
708 spin_lock_irqsave(&hw->lock, flags);
709 while (!list_empty(&hw->rx_queue)) {
710 struct ipw_rx_packet *packet =
711 list_first_entry(&hw->rx_queue,
712 struct ipw_rx_packet, queue);
713
714 if (hw->shutting_down)
715 break;
716 list_del(&packet->queue);
717
718 /*
719 * Note: ipwireless_network_packet_received must be called in a
720 * process context (i.e. via schedule_work) because the tty
721 * output code can sleep in the tty_flip_buffer_push call.
722 */
723 if (packet->protocol == TL_PROTOCOLID_COM_DATA) {
724 if (hw->network != NULL) {
725 /* If the network hasn't been disconnected. */
726 spin_unlock_irqrestore(&hw->lock, flags);
727 /*
728 * This must run unlocked due to tty processing
729 * and mutex locking
730 */
731 ipwireless_network_packet_received(
732 hw->network,
733 packet->channel_idx,
734 (unsigned char *)packet
735 + sizeof(struct ipw_rx_packet),
736 packet->length);
737 spin_lock_irqsave(&hw->lock, flags);
738 }
739 /* Count queued DATA bytes only */
740 hw->rx_bytes_queued -= packet->length;
741 } else {
742 /*
743 * This is safe to be called locked, callchain does
744 * not block
745 */
746 handle_received_CTRL_packet(hw, packet->channel_idx,
747 (unsigned char *)packet
748 + sizeof(struct ipw_rx_packet),
749 packet->length);
750 }
751 pool_free(hw, packet);
752 /*
753 * Unblock reception of incoming packets if queue is no longer
754 * full.
755 */
756 hw->blocking_rx =
757 hw->rx_bytes_queued >= IPWIRELESS_RX_QUEUE_SIZE;
758 if (hw->shutting_down)
759 break;
760 }
761 spin_unlock_irqrestore(&hw->lock, flags);
762}
763
764static void handle_received_CTRL_packet(struct ipw_hardware *hw,
765 unsigned int channel_idx,
766 const unsigned char *data, int len)
767{
768 const struct ipw_control_packet_body *body =
769 (const struct ipw_control_packet_body *) data;
770 unsigned int changed_mask;
771
772 if (len != sizeof(struct ipw_control_packet_body)) {
773 printk(KERN_INFO IPWIRELESS_PCCARD_NAME
774 ": control packet was %d bytes - wrong size!\n",
775 len);
776 return;
777 }
778
779 switch (body->sig_no) {
780 case COMCTRL_CTS:
781 changed_mask = IPW_CONTROL_LINE_CTS;
782 break;
783 case COMCTRL_DCD:
784 changed_mask = IPW_CONTROL_LINE_DCD;
785 break;
786 case COMCTRL_DSR:
787 changed_mask = IPW_CONTROL_LINE_DSR;
788 break;
789 case COMCTRL_RI:
790 changed_mask = IPW_CONTROL_LINE_RI;
791 break;
792 default:
793 changed_mask = 0;
794 }
795
796 if (changed_mask != 0) {
797 if (body->value)
798 hw->control_lines[channel_idx] |= changed_mask;
799 else
800 hw->control_lines[channel_idx] &= ~changed_mask;
801 if (hw->network)
802 ipwireless_network_notify_control_line_change(
803 hw->network,
804 channel_idx,
805 hw->control_lines[channel_idx],
806 changed_mask);
807 }
808}
809
810static void handle_received_packet(struct ipw_hardware *hw,
811 const union nl_packet *packet,
812 unsigned short len)
813{
814 unsigned int protocol = packet->hdr.protocol;
815 unsigned int address = packet->hdr.address;
816 unsigned int header_length;
817 const unsigned char *data;
818 unsigned int data_len;
819 int is_last = packet->hdr.packet_rank & NL_LAST_PACKET;
820
821 if (packet->hdr.packet_rank & NL_FIRST_PACKET)
822 header_length = NL_FIRST_PACKET_HEADER_SIZE;
823 else
824 header_length = NL_FOLLOWING_PACKET_HEADER_SIZE;
825
826 data = packet->rawpkt + header_length;
827 data_len = len - header_length;
828 switch (protocol) {
829 case TL_PROTOCOLID_COM_DATA:
830 case TL_PROTOCOLID_COM_CTRL:
831 queue_received_packet(hw, protocol, address, data, data_len,
832 is_last);
833 break;
834 case TL_PROTOCOLID_SETUP:
835 handle_received_SETUP_packet(hw, address, data, data_len,
836 is_last);
837 break;
838 }
839}
840
841static void acknowledge_data_read(struct ipw_hardware *hw)
842{
843 if (hw->hw_version == HW_VERSION_1)
844 outw(DCR_RXDONE, hw->base_port + IODCR);
845 else
846 writew(MEMRX_PCINTACKK,
847 &hw->memory_info_regs->memreg_pc_interrupt_ack);
848}
849
850/*
851 * Retrieve a packet from the IPW hardware.
852 */
853static void do_receive_packet(struct ipw_hardware *hw)
854{
855 unsigned len;
856 unsigned i;
857 unsigned char pkt[LL_MTU_MAX];
858
859 start_timing();
860
861 if (hw->hw_version == HW_VERSION_1) {
862 len = inw(hw->base_port + IODRR);
863 if (len > hw->ll_mtu) {
864 printk(KERN_INFO IPWIRELESS_PCCARD_NAME
865 ": received a packet of %u bytes - longer than the MTU!\n", len);
866 outw(DCR_RXDONE | DCR_RXRESET, hw->base_port + IODCR);
867 return;
868 }
869
870 for (i = 0; i < len; i += 2) {
871 __le16 raw_data = inw(hw->base_port + IODRR);
872 unsigned short data = le16_to_cpu(raw_data);
873
874 pkt[i] = (unsigned char) data;
875 pkt[i + 1] = (unsigned char) (data >> 8);
876 }
877 } else {
878 len = inw(hw->base_port);
879 if (len > hw->ll_mtu) {
880 printk(KERN_INFO IPWIRELESS_PCCARD_NAME
881 ": received a packet of %u bytes - longer than the MTU!\n", len);
882 writew(MEMRX_PCINTACKK,
883 &hw->memory_info_regs->memreg_pc_interrupt_ack);
884 return;
885 }
886
887 for (i = 0; i < len; i += 2) {
888 __le16 raw_data = inw(hw->base_port);
889 unsigned short data = le16_to_cpu(raw_data);
890
891 pkt[i] = (unsigned char) data;
892 pkt[i + 1] = (unsigned char) (data >> 8);
893 }
894
895 while ((i & 3) != 2) {
896 inw(hw->base_port);
897 i += 2;
898 }
899 }
900
901 acknowledge_data_read(hw);
902
903 swap_packet_bitfield_from_le(pkt);
904
905 if (ipwireless_debug)
906 dump_data_bytes("recv", pkt, len);
907
908 handle_received_packet(hw, (union nl_packet *) pkt, len);
909
910 end_read_timing(len);
911}
912
913static int get_current_packet_priority(struct ipw_hardware *hw)
914{
915 /*
916 * If we're initializing, don't send anything of higher priority than
917 * PRIO_SETUP. The network layer therefore need not care about
918 * hardware initialization - any of its stuff will simply be queued
919 * until setup is complete.
920 */
921 return (hw->to_setup || hw->initializing
922 ? PRIO_SETUP + 1 : NL_NUM_OF_PRIORITIES);
923}
924
925/*
926 * return 1 if something has been received from hw
927 */
928static int get_packets_from_hw(struct ipw_hardware *hw)
929{
930 int received = 0;
931 unsigned long flags;
932
933 spin_lock_irqsave(&hw->lock, flags);
934 while (hw->rx_ready && !hw->blocking_rx) {
935 received = 1;
936 hw->rx_ready--;
937 spin_unlock_irqrestore(&hw->lock, flags);
938
939 do_receive_packet(hw);
940
941 spin_lock_irqsave(&hw->lock, flags);
942 }
943 spin_unlock_irqrestore(&hw->lock, flags);
944
945 return received;
946}
947
948/*
949 * Send pending packet up to given priority, prioritize SETUP data until
950 * hardware is fully setup.
951 *
952 * return 1 if more packets can be sent
953 */
954static int send_pending_packet(struct ipw_hardware *hw, int priority_limit)
955{
956 int more_to_send = 0;
957 unsigned long flags;
958
959 spin_lock_irqsave(&hw->lock, flags);
960 if (hw->tx_queued && hw->tx_ready) {
961 int priority;
962 struct ipw_tx_packet *packet = NULL;
963
964 /* Pick a packet */
965 for (priority = 0; priority < priority_limit; priority++) {
966 if (!list_empty(&hw->tx_queue[priority])) {
967 packet = list_first_entry(
968 &hw->tx_queue[priority],
969 struct ipw_tx_packet,
970 queue);
971
972 hw->tx_queued--;
973 list_del(&packet->queue);
974
975 break;
976 }
977 }
978 if (!packet) {
979 hw->tx_queued = 0;
980 spin_unlock_irqrestore(&hw->lock, flags);
981 return 0;
982 }
983
984 spin_unlock_irqrestore(&hw->lock, flags);
985
986 /* Send */
987 do_send_packet(hw, packet);
988
989 /* Check if more to send */
990 spin_lock_irqsave(&hw->lock, flags);
991 for (priority = 0; priority < priority_limit; priority++)
992 if (!list_empty(&hw->tx_queue[priority])) {
993 more_to_send = 1;
994 break;
995 }
996
997 if (!more_to_send)
998 hw->tx_queued = 0;
999 }
1000 spin_unlock_irqrestore(&hw->lock, flags);
1001
1002 return more_to_send;
1003}
1004
1005/*
1006 * Send and receive all queued packets.
1007 */
1008static void ipwireless_do_tasklet(unsigned long hw_)
1009{
1010 struct ipw_hardware *hw = (struct ipw_hardware *) hw_;
1011 unsigned long flags;
1012
1013 spin_lock_irqsave(&hw->lock, flags);
1014 if (hw->shutting_down) {
1015 spin_unlock_irqrestore(&hw->lock, flags);
1016 return;
1017 }
1018
1019 if (hw->to_setup == 1) {
1020 /*
1021 * Initial setup data sent to hardware
1022 */
1023 hw->to_setup = 2;
1024 spin_unlock_irqrestore(&hw->lock, flags);
1025
1026 ipw_setup_hardware(hw);
1027 ipw_send_setup_packet(hw);
1028
1029 send_pending_packet(hw, PRIO_SETUP + 1);
1030 get_packets_from_hw(hw);
1031 } else {
1032 int priority_limit = get_current_packet_priority(hw);
1033 int again;
1034
1035 spin_unlock_irqrestore(&hw->lock, flags);
1036
1037 do {
1038 again = send_pending_packet(hw, priority_limit);
1039 again |= get_packets_from_hw(hw);
1040 } while (again);
1041 }
1042}
1043
1044/*
1045 * return true if the card is physically present.
1046 */
1047static int is_card_present(struct ipw_hardware *hw)
1048{
1049 if (hw->hw_version == HW_VERSION_1)
1050 return inw(hw->base_port + IOIR) != 0xFFFF;
1051 else
1052 return readl(&hw->memory_info_regs->memreg_card_present) ==
1053 CARD_PRESENT_VALUE;
1054}
1055
1056static irqreturn_t ipwireless_handle_v1_interrupt(int irq,
1057 struct ipw_hardware *hw)
1058{
1059 unsigned short irqn;
1060
1061 irqn = inw(hw->base_port + IOIR);
1062
1063 /* Check if card is present */
1064 if (irqn == 0xFFFF)
1065 return IRQ_NONE;
1066 else if (irqn != 0) {
1067 unsigned short ack = 0;
1068 unsigned long flags;
1069
1070 /* Transmit complete. */
1071 if (irqn & IR_TXINTR) {
1072 ack |= IR_TXINTR;
1073 spin_lock_irqsave(&hw->lock, flags);
1074 hw->tx_ready = 1;
1075 spin_unlock_irqrestore(&hw->lock, flags);
1076 }
1077 /* Received data */
1078 if (irqn & IR_RXINTR) {
1079 ack |= IR_RXINTR;
1080 spin_lock_irqsave(&hw->lock, flags);
1081 hw->rx_ready++;
1082 spin_unlock_irqrestore(&hw->lock, flags);
1083 }
1084 if (ack != 0) {
1085 outw(ack, hw->base_port + IOIR);
1086 tasklet_schedule(&hw->tasklet);
1087 }
1088 return IRQ_HANDLED;
1089 }
1090 return IRQ_NONE;
1091}
1092
1093static void acknowledge_pcmcia_interrupt(struct ipw_hardware *hw)
1094{
1095 unsigned short csr = readw(&hw->memregs_CCR->reg_config_and_status);
1096
1097 csr &= 0xfffd;
1098 writew(csr, &hw->memregs_CCR->reg_config_and_status);
1099}
1100
1101static irqreturn_t ipwireless_handle_v2_v3_interrupt(int irq,
1102 struct ipw_hardware *hw)
1103{
1104 int tx = 0;
1105 int rx = 0;
1106 int rx_repeat = 0;
1107 int try_mem_tx_old;
1108 unsigned long flags;
1109
1110 do {
1111
1112 unsigned short memtx = readw(hw->memreg_tx);
1113 unsigned short memtx_serial;
1114 unsigned short memrxdone =
1115 readw(&hw->memory_info_regs->memreg_rx_done);
1116
1117 try_mem_tx_old = 0;
1118
1119 /* check whether the interrupt was generated by ipwireless card */
1120 if (!(memtx & MEMTX_TX) && !(memrxdone & MEMRX_RX_DONE)) {
1121
1122 /* check if the card uses memreg_tx_old register */
1123 if (hw->memreg_tx == &hw->memory_info_regs->memreg_tx_new) {
1124 memtx = readw(&hw->memory_info_regs->memreg_tx_old);
1125 if (memtx & MEMTX_TX) {
1126 printk(KERN_INFO IPWIRELESS_PCCARD_NAME
1127 ": Using memreg_tx_old\n");
1128 hw->memreg_tx =
1129 &hw->memory_info_regs->memreg_tx_old;
1130 } else {
1131 return IRQ_NONE;
1132 }
1133 } else
1134 return IRQ_NONE;
1135 }
1136
1137 /*
1138 * See if the card is physically present. Note that while it is
1139 * powering up, it appears not to be present.
1140 */
1141 if (!is_card_present(hw)) {
1142 acknowledge_pcmcia_interrupt(hw);
1143 return IRQ_HANDLED;
1144 }
1145
1146 memtx_serial = memtx & (unsigned short) 0xff00;
1147 if (memtx & MEMTX_TX) {
1148 writew(memtx_serial, hw->memreg_tx);
1149
1150 if (hw->serial_number_detected) {
1151 if (memtx_serial != hw->last_memtx_serial) {
1152 hw->last_memtx_serial = memtx_serial;
1153 spin_lock_irqsave(&hw->lock, flags);
1154 hw->rx_ready++;
1155 spin_unlock_irqrestore(&hw->lock, flags);
1156 rx = 1;
1157 } else
1158 /* Ignore 'Timer Recovery' duplicates. */
1159 rx_repeat = 1;
1160 } else {
1161 /*
1162 * If a non-zero serial number is seen, then enable
1163 * serial number checking.
1164 */
1165 if (memtx_serial != 0) {
1166 hw->serial_number_detected = 1;
1167 printk(KERN_DEBUG IPWIRELESS_PCCARD_NAME
1168 ": memreg_tx serial num detected\n");
1169
1170 spin_lock_irqsave(&hw->lock, flags);
1171 hw->rx_ready++;
1172 spin_unlock_irqrestore(&hw->lock, flags);
1173 }
1174 rx = 1;
1175 }
1176 }
1177 if (memrxdone & MEMRX_RX_DONE) {
1178 writew(0, &hw->memory_info_regs->memreg_rx_done);
1179 spin_lock_irqsave(&hw->lock, flags);
1180 hw->tx_ready = 1;
1181 spin_unlock_irqrestore(&hw->lock, flags);
1182 tx = 1;
1183 }
1184 if (tx)
1185 writew(MEMRX_PCINTACKK,
1186 &hw->memory_info_regs->memreg_pc_interrupt_ack);
1187
1188 acknowledge_pcmcia_interrupt(hw);
1189
1190 if (tx || rx)
1191 tasklet_schedule(&hw->tasklet);
1192 else if (!rx_repeat) {
1193 if (hw->memreg_tx == &hw->memory_info_regs->memreg_tx_new) {
1194 if (hw->serial_number_detected)
1195 printk(KERN_WARNING IPWIRELESS_PCCARD_NAME
1196 ": spurious interrupt - new_tx mode\n");
1197 else {
1198 printk(KERN_WARNING IPWIRELESS_PCCARD_NAME
1199 ": no valid memreg_tx value - switching to the old memreg_tx\n");
1200 hw->memreg_tx =
1201 &hw->memory_info_regs->memreg_tx_old;
1202 try_mem_tx_old = 1;
1203 }
1204 } else
1205 printk(KERN_WARNING IPWIRELESS_PCCARD_NAME
1206 ": spurious interrupt - old_tx mode\n");
1207 }
1208
1209 } while (try_mem_tx_old == 1);
1210
1211 return IRQ_HANDLED;
1212}
1213
1214irqreturn_t ipwireless_interrupt(int irq, void *dev_id)
1215{
1216 struct ipw_dev *ipw = dev_id;
1217
1218 if (ipw->hardware->hw_version == HW_VERSION_1)
1219 return ipwireless_handle_v1_interrupt(irq, ipw->hardware);
1220 else
1221 return ipwireless_handle_v2_v3_interrupt(irq, ipw->hardware);
1222}
1223
1224static void flush_packets_to_hw(struct ipw_hardware *hw)
1225{
1226 int priority_limit;
1227 unsigned long flags;
1228
1229 spin_lock_irqsave(&hw->lock, flags);
1230 priority_limit = get_current_packet_priority(hw);
1231 spin_unlock_irqrestore(&hw->lock, flags);
1232
1233 while (send_pending_packet(hw, priority_limit));
1234}
1235
1236static void send_packet(struct ipw_hardware *hw, int priority,
1237 struct ipw_tx_packet *packet)
1238{
1239 unsigned long flags;
1240
1241 spin_lock_irqsave(&hw->lock, flags);
1242 list_add_tail(&packet->queue, &hw->tx_queue[priority]);
1243 hw->tx_queued++;
1244 spin_unlock_irqrestore(&hw->lock, flags);
1245
1246 flush_packets_to_hw(hw);
1247}
1248
1249/* Create data packet, non-atomic allocation */
1250static void *alloc_data_packet(int data_size,
1251 unsigned char dest_addr,
1252 unsigned char protocol)
1253{
1254 struct ipw_tx_packet *packet = kzalloc(
1255 sizeof(struct ipw_tx_packet) + data_size,
1256 GFP_ATOMIC);
1257
1258 if (!packet)
1259 return NULL;
1260
1261 INIT_LIST_HEAD(&packet->queue);
1262 packet->dest_addr = dest_addr;
1263 packet->protocol = protocol;
1264 packet->length = data_size;
1265
1266 return packet;
1267}
1268
1269static void *alloc_ctrl_packet(int header_size,
1270 unsigned char dest_addr,
1271 unsigned char protocol,
1272 unsigned char sig_no)
1273{
1274 /*
1275 * sig_no is located right after ipw_tx_packet struct in every
1276 * CTRL or SETUP packets, we can use ipw_control_packet as a
1277 * common struct
1278 */
1279 struct ipw_control_packet *packet = kzalloc(header_size, GFP_ATOMIC);
1280
1281 if (!packet)
1282 return NULL;
1283
1284 INIT_LIST_HEAD(&packet->header.queue);
1285 packet->header.dest_addr = dest_addr;
1286 packet->header.protocol = protocol;
1287 packet->header.length = header_size - sizeof(struct ipw_tx_packet);
1288 packet->body.sig_no = sig_no;
1289
1290 return packet;
1291}
1292
1293int ipwireless_send_packet(struct ipw_hardware *hw, unsigned int channel_idx,
1294 const unsigned char *data, unsigned int length,
1295 void (*callback) (void *cb, unsigned int length),
1296 void *callback_data)
1297{
1298 struct ipw_tx_packet *packet;
1299
1300 packet = alloc_data_packet(length, (channel_idx + 1),
1301 TL_PROTOCOLID_COM_DATA);
1302 if (!packet)
1303 return -ENOMEM;
1304 packet->packet_callback = callback;
1305 packet->callback_data = callback_data;
1306 memcpy((unsigned char *) packet + sizeof(struct ipw_tx_packet), data,
1307 length);
1308
1309 send_packet(hw, PRIO_DATA, packet);
1310 return 0;
1311}
1312
1313static int set_control_line(struct ipw_hardware *hw, int prio,
1314 unsigned int channel_idx, int line, int state)
1315{
1316 struct ipw_control_packet *packet;
1317 int protocolid = TL_PROTOCOLID_COM_CTRL;
1318
1319 if (prio == PRIO_SETUP)
1320 protocolid = TL_PROTOCOLID_SETUP;
1321
1322 packet = alloc_ctrl_packet(sizeof(struct ipw_control_packet),
1323 (channel_idx + 1), protocolid, line);
1324 if (!packet)
1325 return -ENOMEM;
1326 packet->header.length = sizeof(struct ipw_control_packet_body);
1327 packet->body.value = (state == 0 ? 0 : 1);
1328 send_packet(hw, prio, &packet->header);
1329 return 0;
1330}
1331
1332
1333static int set_DTR(struct ipw_hardware *hw, int priority,
1334 unsigned int channel_idx, int state)
1335{
1336 if (state != 0)
1337 hw->control_lines[channel_idx] |= IPW_CONTROL_LINE_DTR;
1338 else
1339 hw->control_lines[channel_idx] &= ~IPW_CONTROL_LINE_DTR;
1340
1341 return set_control_line(hw, priority, channel_idx, COMCTRL_DTR, state);
1342}
1343
1344static int set_RTS(struct ipw_hardware *hw, int priority,
1345 unsigned int channel_idx, int state)
1346{
1347 if (state != 0)
1348 hw->control_lines[channel_idx] |= IPW_CONTROL_LINE_RTS;
1349 else
1350 hw->control_lines[channel_idx] &= ~IPW_CONTROL_LINE_RTS;
1351
1352 return set_control_line(hw, priority, channel_idx, COMCTRL_RTS, state);
1353}
1354
1355int ipwireless_set_DTR(struct ipw_hardware *hw, unsigned int channel_idx,
1356 int state)
1357{
1358 return set_DTR(hw, PRIO_CTRL, channel_idx, state);
1359}
1360
1361int ipwireless_set_RTS(struct ipw_hardware *hw, unsigned int channel_idx,
1362 int state)
1363{
1364 return set_RTS(hw, PRIO_CTRL, channel_idx, state);
1365}
1366
1367struct ipw_setup_get_version_query_packet {
1368 struct ipw_tx_packet header;
1369 struct tl_setup_get_version_qry body;
1370};
1371
1372struct ipw_setup_config_packet {
1373 struct ipw_tx_packet header;
1374 struct tl_setup_config_msg body;
1375};
1376
1377struct ipw_setup_config_done_packet {
1378 struct ipw_tx_packet header;
1379 struct tl_setup_config_done_msg body;
1380};
1381
1382struct ipw_setup_open_packet {
1383 struct ipw_tx_packet header;
1384 struct tl_setup_open_msg body;
1385};
1386
1387struct ipw_setup_info_packet {
1388 struct ipw_tx_packet header;
1389 struct tl_setup_info_msg body;
1390};
1391
1392struct ipw_setup_reboot_msg_ack {
1393 struct ipw_tx_packet header;
1394 struct TlSetupRebootMsgAck body;
1395};
1396
1397/* This handles the actual initialization of the card */
1398static void __handle_setup_get_version_rsp(struct ipw_hardware *hw)
1399{
1400 struct ipw_setup_config_packet *config_packet;
1401 struct ipw_setup_config_done_packet *config_done_packet;
1402 struct ipw_setup_open_packet *open_packet;
1403 struct ipw_setup_info_packet *info_packet;
1404 int port;
1405 unsigned int channel_idx;
1406
1407 /* generate config packet */
1408 for (port = 1; port <= NL_NUM_OF_ADDRESSES; port++) {
1409 config_packet = alloc_ctrl_packet(
1410 sizeof(struct ipw_setup_config_packet),
1411 ADDR_SETUP_PROT,
1412 TL_PROTOCOLID_SETUP,
1413 TL_SETUP_SIGNO_CONFIG_MSG);
1414 if (!config_packet)
1415 goto exit_nomem;
1416 config_packet->header.length = sizeof(struct tl_setup_config_msg);
1417 config_packet->body.port_no = port;
1418 config_packet->body.prio_data = PRIO_DATA;
1419 config_packet->body.prio_ctrl = PRIO_CTRL;
1420 send_packet(hw, PRIO_SETUP, &config_packet->header);
1421 }
1422 config_done_packet = alloc_ctrl_packet(
1423 sizeof(struct ipw_setup_config_done_packet),
1424 ADDR_SETUP_PROT,
1425 TL_PROTOCOLID_SETUP,
1426 TL_SETUP_SIGNO_CONFIG_DONE_MSG);
1427 if (!config_done_packet)
1428 goto exit_nomem;
1429 config_done_packet->header.length = sizeof(struct tl_setup_config_done_msg);
1430 send_packet(hw, PRIO_SETUP, &config_done_packet->header);
1431
1432 /* generate open packet */
1433 for (port = 1; port <= NL_NUM_OF_ADDRESSES; port++) {
1434 open_packet = alloc_ctrl_packet(
1435 sizeof(struct ipw_setup_open_packet),
1436 ADDR_SETUP_PROT,
1437 TL_PROTOCOLID_SETUP,
1438 TL_SETUP_SIGNO_OPEN_MSG);
1439 if (!open_packet)
1440 goto exit_nomem;
1441 open_packet->header.length = sizeof(struct tl_setup_open_msg);
1442 open_packet->body.port_no = port;
1443 send_packet(hw, PRIO_SETUP, &open_packet->header);
1444 }
1445 for (channel_idx = 0;
1446 channel_idx < NL_NUM_OF_ADDRESSES; channel_idx++) {
1447 int ret;
1448
1449 ret = set_DTR(hw, PRIO_SETUP, channel_idx,
1450 (hw->control_lines[channel_idx] &
1451 IPW_CONTROL_LINE_DTR) != 0);
1452 if (ret) {
1453 printk(KERN_ERR IPWIRELESS_PCCARD_NAME
1454 ": error setting DTR (%d)\n", ret);
1455 return;
1456 }
1457
1458 set_RTS(hw, PRIO_SETUP, channel_idx,
1459 (hw->control_lines [channel_idx] &
1460 IPW_CONTROL_LINE_RTS) != 0);
1461 if (ret) {
1462 printk(KERN_ERR IPWIRELESS_PCCARD_NAME
1463 ": error setting RTS (%d)\n", ret);
1464 return;
1465 }
1466 }
1467 /*
1468 * For NDIS we assume that we are using sync PPP frames, for COM async.
1469 * This driver uses NDIS mode too. We don't bother with translation
1470 * from async -> sync PPP.
1471 */
1472 info_packet = alloc_ctrl_packet(sizeof(struct ipw_setup_info_packet),
1473 ADDR_SETUP_PROT,
1474 TL_PROTOCOLID_SETUP,
1475 TL_SETUP_SIGNO_INFO_MSG);
1476 if (!info_packet)
1477 goto exit_nomem;
1478 info_packet->header.length = sizeof(struct tl_setup_info_msg);
1479 info_packet->body.driver_type = NDISWAN_DRIVER;
1480 info_packet->body.major_version = NDISWAN_DRIVER_MAJOR_VERSION;
1481 info_packet->body.minor_version = NDISWAN_DRIVER_MINOR_VERSION;
1482 send_packet(hw, PRIO_SETUP, &info_packet->header);
1483
1484 /* Initialization is now complete, so we clear the 'to_setup' flag */
1485 hw->to_setup = 0;
1486
1487 return;
1488
1489exit_nomem:
1490 printk(KERN_ERR IPWIRELESS_PCCARD_NAME
1491 ": not enough memory to alloc control packet\n");
1492 hw->to_setup = -1;
1493}
1494
1495static void handle_setup_get_version_rsp(struct ipw_hardware *hw,
1496 unsigned char vers_no)
1497{
1498 del_timer(&hw->setup_timer);
1499 hw->initializing = 0;
1500 printk(KERN_INFO IPWIRELESS_PCCARD_NAME ": card is ready.\n");
1501
1502 if (vers_no == TL_SETUP_VERSION)
1503 __handle_setup_get_version_rsp(hw);
1504 else
1505 printk(KERN_ERR IPWIRELESS_PCCARD_NAME
1506 ": invalid hardware version no %u\n",
1507 (unsigned int) vers_no);
1508}
1509
1510static void ipw_send_setup_packet(struct ipw_hardware *hw)
1511{
1512 struct ipw_setup_get_version_query_packet *ver_packet;
1513
1514 ver_packet = alloc_ctrl_packet(
1515 sizeof(struct ipw_setup_get_version_query_packet),
1516 ADDR_SETUP_PROT, TL_PROTOCOLID_SETUP,
1517 TL_SETUP_SIGNO_GET_VERSION_QRY);
1518 ver_packet->header.length = sizeof(struct tl_setup_get_version_qry);
1519
1520 /*
1521 * Response is handled in handle_received_SETUP_packet
1522 */
1523 send_packet(hw, PRIO_SETUP, &ver_packet->header);
1524}
1525
1526static void handle_received_SETUP_packet(struct ipw_hardware *hw,
1527 unsigned int address,
1528 const unsigned char *data, int len,
1529 int is_last)
1530{
1531 const union ipw_setup_rx_msg *rx_msg = (const union ipw_setup_rx_msg *) data;
1532
1533 if (address != ADDR_SETUP_PROT) {
1534 printk(KERN_INFO IPWIRELESS_PCCARD_NAME
1535 ": setup packet has bad address %d\n", address);
1536 return;
1537 }
1538
1539 switch (rx_msg->sig_no) {
1540 case TL_SETUP_SIGNO_GET_VERSION_RSP:
1541 if (hw->to_setup)
1542 handle_setup_get_version_rsp(hw,
1543 rx_msg->version_rsp_msg.version);
1544 break;
1545
1546 case TL_SETUP_SIGNO_OPEN_MSG:
1547 if (ipwireless_debug) {
1548 unsigned int channel_idx = rx_msg->open_msg.port_no - 1;
1549
1550 printk(KERN_INFO IPWIRELESS_PCCARD_NAME
1551 ": OPEN_MSG [channel %u] reply received\n",
1552 channel_idx);
1553 }
1554 break;
1555
1556 case TL_SETUP_SIGNO_INFO_MSG_ACK:
1557 if (ipwireless_debug)
1558 printk(KERN_DEBUG IPWIRELESS_PCCARD_NAME
1559 ": card successfully configured as NDISWAN\n");
1560 break;
1561
1562 case TL_SETUP_SIGNO_REBOOT_MSG:
1563 if (hw->to_setup)
1564 printk(KERN_DEBUG IPWIRELESS_PCCARD_NAME
1565 ": Setup not completed - ignoring reboot msg\n");
1566 else {
1567 struct ipw_setup_reboot_msg_ack *packet;
1568
1569 printk(KERN_DEBUG IPWIRELESS_PCCARD_NAME
1570 ": Acknowledging REBOOT message\n");
1571 packet = alloc_ctrl_packet(
1572 sizeof(struct ipw_setup_reboot_msg_ack),
1573 ADDR_SETUP_PROT, TL_PROTOCOLID_SETUP,
1574 TL_SETUP_SIGNO_REBOOT_MSG_ACK);
1575 packet->header.length =
1576 sizeof(struct TlSetupRebootMsgAck);
1577 send_packet(hw, PRIO_SETUP, &packet->header);
1578 if (hw->reboot_callback)
1579 hw->reboot_callback(hw->reboot_callback_data);
1580 }
1581 break;
1582
1583 default:
1584 printk(KERN_INFO IPWIRELESS_PCCARD_NAME
1585 ": unknown setup message %u received\n",
1586 (unsigned int) rx_msg->sig_no);
1587 }
1588}
1589
1590static void do_close_hardware(struct ipw_hardware *hw)
1591{
1592 unsigned int irqn;
1593
1594 if (hw->hw_version == HW_VERSION_1) {
1595 /* Disable TX and RX interrupts. */
1596 outw(0, hw->base_port + IOIER);
1597
1598 /* Acknowledge any outstanding interrupt requests */
1599 irqn = inw(hw->base_port + IOIR);
1600 if (irqn & IR_TXINTR)
1601 outw(IR_TXINTR, hw->base_port + IOIR);
1602 if (irqn & IR_RXINTR)
1603 outw(IR_RXINTR, hw->base_port + IOIR);
1604
1605 synchronize_irq(hw->irq);
1606 }
1607}
1608
1609struct ipw_hardware *ipwireless_hardware_create(void)
1610{
1611 int i;
1612 struct ipw_hardware *hw =
1613 kzalloc(sizeof(struct ipw_hardware), GFP_KERNEL);
1614
1615 if (!hw)
1616 return NULL;
1617
1618 hw->irq = -1;
1619 hw->initializing = 1;
1620 hw->tx_ready = 1;
1621 hw->rx_bytes_queued = 0;
1622 hw->rx_pool_size = 0;
1623 hw->last_memtx_serial = (unsigned short) 0xffff;
1624 for (i = 0; i < NL_NUM_OF_PRIORITIES; i++)
1625 INIT_LIST_HEAD(&hw->tx_queue[i]);
1626
1627 INIT_LIST_HEAD(&hw->rx_queue);
1628 INIT_LIST_HEAD(&hw->rx_pool);
1629 spin_lock_init(&hw->lock);
1630 tasklet_init(&hw->tasklet, ipwireless_do_tasklet, (unsigned long) hw);
1631 INIT_WORK(&hw->work_rx, ipw_receive_data_work);
1632 setup_timer(&hw->setup_timer, ipwireless_setup_timer,
1633 (unsigned long) hw);
1634
1635 return hw;
1636}
1637
1638void ipwireless_init_hardware_v1(struct ipw_hardware *hw,
1639 unsigned int base_port,
1640 void __iomem *attr_memory,
1641 void __iomem *common_memory,
1642 int is_v2_card,
1643 void (*reboot_callback) (void *data),
1644 void *reboot_callback_data)
1645{
1646 if (hw->removed) {
1647 hw->removed = 0;
1648 enable_irq(hw->irq);
1649 }
1650 hw->base_port = base_port;
1651 hw->hw_version = (is_v2_card ? HW_VERSION_2 : HW_VERSION_1);
1652 hw->ll_mtu = (hw->hw_version == HW_VERSION_1 ? LL_MTU_V1 : LL_MTU_V2);
1653 hw->memregs_CCR = (struct MEMCCR __iomem *)
1654 ((unsigned short __iomem *) attr_memory + 0x200);
1655 hw->memory_info_regs = (struct MEMINFREG __iomem *) common_memory;
1656 hw->memreg_tx = &hw->memory_info_regs->memreg_tx_new;
1657 hw->reboot_callback = reboot_callback;
1658 hw->reboot_callback_data = reboot_callback_data;
1659}
1660
1661void ipwireless_init_hardware_v2_v3(struct ipw_hardware *hw)
1662{
1663 hw->initializing = 1;
1664 hw->init_loops = 0;
1665 printk(KERN_INFO IPWIRELESS_PCCARD_NAME
1666 ": waiting for card to start up...\n");
1667 ipwireless_setup_timer((unsigned long) hw);
1668}
1669
1670static void ipwireless_setup_timer(unsigned long data)
1671{
1672 struct ipw_hardware *hw = (struct ipw_hardware *) data;
1673
1674 hw->init_loops++;
1675
1676 if (hw->init_loops == TL_SETUP_MAX_VERSION_QRY &&
1677 hw->hw_version == HW_VERSION_2 &&
1678 hw->memreg_tx == &hw->memory_info_regs->memreg_tx_new) {
1679 printk(KERN_INFO IPWIRELESS_PCCARD_NAME
1680 ": failed to startup using TX2, trying TX\n");
1681
1682 hw->memreg_tx = &hw->memory_info_regs->memreg_tx_old;
1683 hw->init_loops = 0;
1684 }
1685 /* Give up after a certain number of retries */
1686 if (hw->init_loops == TL_SETUP_MAX_VERSION_QRY) {
1687 printk(KERN_INFO IPWIRELESS_PCCARD_NAME
1688 ": card failed to start up!\n");
1689 hw->initializing = 0;
1690 } else {
1691 /* Do not attempt to write to the board if it is not present. */
1692 if (is_card_present(hw)) {
1693 unsigned long flags;
1694
1695 spin_lock_irqsave(&hw->lock, flags);
1696 hw->to_setup = 1;
1697 hw->tx_ready = 1;
1698 spin_unlock_irqrestore(&hw->lock, flags);
1699 tasklet_schedule(&hw->tasklet);
1700 }
1701
1702 mod_timer(&hw->setup_timer,
1703 jiffies + msecs_to_jiffies(TL_SETUP_VERSION_QRY_TMO));
1704 }
1705}
1706
1707/*
1708 * Stop any interrupts from executing so that, once this function returns,
1709 * other layers of the driver can be sure they won't get any more callbacks.
1710 * Thus must be called on a proper process context.
1711 */
1712void ipwireless_stop_interrupts(struct ipw_hardware *hw)
1713{
1714 if (!hw->shutting_down) {
1715 /* Tell everyone we are going down. */
1716 hw->shutting_down = 1;
1717 del_timer(&hw->setup_timer);
1718
1719 /* Prevent the hardware from sending any more interrupts */
1720 do_close_hardware(hw);
1721 }
1722}
1723
1724void ipwireless_hardware_free(struct ipw_hardware *hw)
1725{
1726 int i;
1727 struct ipw_rx_packet *rp, *rq;
1728 struct ipw_tx_packet *tp, *tq;
1729
1730 ipwireless_stop_interrupts(hw);
1731
1732 flush_scheduled_work();
1733
1734 for (i = 0; i < NL_NUM_OF_ADDRESSES; i++)
1735 if (hw->packet_assembler[i] != NULL)
1736 kfree(hw->packet_assembler[i]);
1737
1738 for (i = 0; i < NL_NUM_OF_PRIORITIES; i++)
1739 list_for_each_entry_safe(tp, tq, &hw->tx_queue[i], queue) {
1740 list_del(&tp->queue);
1741 kfree(tp);
1742 }
1743
1744 list_for_each_entry_safe(rp, rq, &hw->rx_queue, queue) {
1745 list_del(&rp->queue);
1746 kfree(rp);
1747 }
1748
1749 list_for_each_entry_safe(rp, rq, &hw->rx_pool, queue) {
1750 list_del(&rp->queue);
1751 kfree(rp);
1752 }
1753 kfree(hw);
1754}
1755
1756/*
1757 * Associate the specified network with this hardware, so it will receive events
1758 * from it.
1759 */
1760void ipwireless_associate_network(struct ipw_hardware *hw,
1761 struct ipw_network *network)
1762{
1763 hw->network = network;
1764}
diff --git a/drivers/char/pcmcia/ipwireless/hardware.h b/drivers/char/pcmcia/ipwireless/hardware.h
deleted file mode 100644
index 90a8590e43b0..000000000000
--- a/drivers/char/pcmcia/ipwireless/hardware.h
+++ /dev/null
@@ -1,62 +0,0 @@
1/*
2 * IPWireless 3G PCMCIA Network Driver
3 *
4 * Original code
5 * by Stephen Blackheath <stephen@blacksapphire.com>,
6 * Ben Martel <benm@symmetric.co.nz>
7 *
8 * Copyrighted as follows:
9 * Copyright (C) 2004 by Symmetric Systems Ltd (NZ)
10 *
11 * Various driver changes and rewrites, port to new kernels
12 * Copyright (C) 2006-2007 Jiri Kosina
13 *
14 * Misc code cleanups and updates
15 * Copyright (C) 2007 David Sterba
16 */
17
18#ifndef _IPWIRELESS_CS_HARDWARE_H_
19#define _IPWIRELESS_CS_HARDWARE_H_
20
21#include <linux/types.h>
22#include <linux/sched.h>
23#include <linux/interrupt.h>
24
25#define IPW_CONTROL_LINE_CTS 0x0001
26#define IPW_CONTROL_LINE_DCD 0x0002
27#define IPW_CONTROL_LINE_DSR 0x0004
28#define IPW_CONTROL_LINE_RI 0x0008
29#define IPW_CONTROL_LINE_DTR 0x0010
30#define IPW_CONTROL_LINE_RTS 0x0020
31
32struct ipw_hardware;
33struct ipw_network;
34
35struct ipw_hardware *ipwireless_hardware_create(void);
36void ipwireless_hardware_free(struct ipw_hardware *hw);
37irqreturn_t ipwireless_interrupt(int irq, void *dev_id);
38int ipwireless_set_DTR(struct ipw_hardware *hw, unsigned int channel_idx,
39 int state);
40int ipwireless_set_RTS(struct ipw_hardware *hw, unsigned int channel_idx,
41 int state);
42int ipwireless_send_packet(struct ipw_hardware *hw,
43 unsigned int channel_idx,
44 const unsigned char *data,
45 unsigned int length,
46 void (*packet_sent_callback) (void *cb,
47 unsigned int length),
48 void *sent_cb_data);
49void ipwireless_associate_network(struct ipw_hardware *hw,
50 struct ipw_network *net);
51void ipwireless_stop_interrupts(struct ipw_hardware *hw);
52void ipwireless_init_hardware_v1(struct ipw_hardware *hw,
53 unsigned int base_port,
54 void __iomem *attr_memory,
55 void __iomem *common_memory,
56 int is_v2_card,
57 void (*reboot_cb) (void *data),
58 void *reboot_cb_data);
59void ipwireless_init_hardware_v2_v3(struct ipw_hardware *hw);
60void ipwireless_sleep(unsigned int tenths);
61
62#endif
diff --git a/drivers/char/pcmcia/ipwireless/main.c b/drivers/char/pcmcia/ipwireless/main.c
deleted file mode 100644
index 67bdb05798b1..000000000000
--- a/drivers/char/pcmcia/ipwireless/main.c
+++ /dev/null
@@ -1,370 +0,0 @@
1/*
2 * IPWireless 3G PCMCIA Network Driver
3 *
4 * Original code
5 * by Stephen Blackheath <stephen@blacksapphire.com>,
6 * Ben Martel <benm@symmetric.co.nz>
7 *
8 * Copyrighted as follows:
9 * Copyright (C) 2004 by Symmetric Systems Ltd (NZ)
10 *
11 * Various driver changes and rewrites, port to new kernels
12 * Copyright (C) 2006-2007 Jiri Kosina
13 *
14 * Misc code cleanups and updates
15 * Copyright (C) 2007 David Sterba
16 */
17
18#include "hardware.h"
19#include "network.h"
20#include "main.h"
21#include "tty.h"
22
23#include <linux/delay.h>
24#include <linux/init.h>
25#include <linux/io.h>
26#include <linux/kernel.h>
27#include <linux/module.h>
28#include <linux/sched.h>
29#include <linux/slab.h>
30
31#include <pcmcia/cisreg.h>
32#include <pcmcia/device_id.h>
33#include <pcmcia/ss.h>
34#include <pcmcia/ds.h>
35#include <pcmcia/cs.h>
36
37static struct pcmcia_device_id ipw_ids[] = {
38 PCMCIA_DEVICE_MANF_CARD(0x02f2, 0x0100),
39 PCMCIA_DEVICE_MANF_CARD(0x02f2, 0x0200),
40 PCMCIA_DEVICE_NULL
41};
42MODULE_DEVICE_TABLE(pcmcia, ipw_ids);
43
44static void ipwireless_detach(struct pcmcia_device *link);
45
46/*
47 * Module params
48 */
49/* Debug mode: more verbose, print sent/recv bytes */
50int ipwireless_debug;
51int ipwireless_loopback;
52int ipwireless_out_queue = 10;
53
54module_param_named(debug, ipwireless_debug, int, 0);
55module_param_named(loopback, ipwireless_loopback, int, 0);
56module_param_named(out_queue, ipwireless_out_queue, int, 0);
57MODULE_PARM_DESC(debug, "switch on debug messages [0]");
58MODULE_PARM_DESC(loopback,
59 "debug: enable ras_raw channel [0]");
60MODULE_PARM_DESC(out_queue, "debug: set size of outgoing PPP queue [10]");
61
62/* Executes in process context. */
63static void signalled_reboot_work(struct work_struct *work_reboot)
64{
65 struct ipw_dev *ipw = container_of(work_reboot, struct ipw_dev,
66 work_reboot);
67 struct pcmcia_device *link = ipw->link;
68 pcmcia_reset_card(link->socket);
69}
70
71static void signalled_reboot_callback(void *callback_data)
72{
73 struct ipw_dev *ipw = (struct ipw_dev *) callback_data;
74
75 /* Delegate to process context. */
76 schedule_work(&ipw->work_reboot);
77}
78
79static int ipwireless_probe(struct pcmcia_device *p_dev,
80 cistpl_cftable_entry_t *cfg,
81 cistpl_cftable_entry_t *dflt,
82 unsigned int vcc,
83 void *priv_data)
84{
85 struct ipw_dev *ipw = priv_data;
86 struct resource *io_resource;
87 int ret;
88
89 p_dev->resource[0]->flags |= IO_DATA_PATH_WIDTH_AUTO;
90 p_dev->resource[0]->start = cfg->io.win[0].base;
91 p_dev->resource[0]->end = cfg->io.win[0].len;
92
93 /* 0x40 causes it to generate level mode interrupts. */
94 /* 0x04 enables IREQ pin. */
95 p_dev->conf.ConfigIndex = cfg->index | 0x44;
96 p_dev->io_lines = 16;
97 ret = pcmcia_request_io(p_dev);
98 if (ret)
99 return ret;
100
101 io_resource = request_region(p_dev->resource[0]->start,
102 resource_size(p_dev->resource[0]),
103 IPWIRELESS_PCCARD_NAME);
104
105 if (cfg->mem.nwin == 0)
106 return 0;
107
108 ipw->request_common_memory.Attributes =
109 WIN_DATA_WIDTH_16 | WIN_MEMORY_TYPE_CM | WIN_ENABLE;
110 ipw->request_common_memory.Base = cfg->mem.win[0].host_addr;
111 ipw->request_common_memory.Size = cfg->mem.win[0].len;
112 if (ipw->request_common_memory.Size < 0x1000)
113 ipw->request_common_memory.Size = 0x1000;
114 ipw->request_common_memory.AccessSpeed = 0;
115
116 ret = pcmcia_request_window(p_dev, &ipw->request_common_memory,
117 &ipw->handle_common_memory);
118
119 if (ret != 0)
120 goto exit1;
121
122 ret = pcmcia_map_mem_page(p_dev, ipw->handle_common_memory,
123 cfg->mem.win[0].card_addr);
124
125 if (ret != 0)
126 goto exit2;
127
128 ipw->is_v2_card = cfg->mem.win[0].len == 0x100;
129
130 ipw->common_memory = ioremap(ipw->request_common_memory.Base,
131 ipw->request_common_memory.Size);
132 request_mem_region(ipw->request_common_memory.Base,
133 ipw->request_common_memory.Size,
134 IPWIRELESS_PCCARD_NAME);
135
136 ipw->request_attr_memory.Attributes =
137 WIN_DATA_WIDTH_16 | WIN_MEMORY_TYPE_AM | WIN_ENABLE;
138 ipw->request_attr_memory.Base = 0;
139 ipw->request_attr_memory.Size = 0; /* this used to be 0x1000 */
140 ipw->request_attr_memory.AccessSpeed = 0;
141
142 ret = pcmcia_request_window(p_dev, &ipw->request_attr_memory,
143 &ipw->handle_attr_memory);
144
145 if (ret != 0)
146 goto exit2;
147
148 ret = pcmcia_map_mem_page(p_dev, ipw->handle_attr_memory, 0);
149 if (ret != 0)
150 goto exit3;
151
152 ipw->attr_memory = ioremap(ipw->request_attr_memory.Base,
153 ipw->request_attr_memory.Size);
154 request_mem_region(ipw->request_attr_memory.Base,
155 ipw->request_attr_memory.Size, IPWIRELESS_PCCARD_NAME);
156
157 return 0;
158
159exit3:
160exit2:
161 if (ipw->common_memory) {
162 release_mem_region(ipw->request_common_memory.Base,
163 ipw->request_common_memory.Size);
164 iounmap(ipw->common_memory);
165 }
166exit1:
167 release_resource(io_resource);
168 pcmcia_disable_device(p_dev);
169 return -1;
170}
171
172static int config_ipwireless(struct ipw_dev *ipw)
173{
174 struct pcmcia_device *link = ipw->link;
175 int ret = 0;
176
177 ipw->is_v2_card = 0;
178
179 ret = pcmcia_loop_config(link, ipwireless_probe, ipw);
180 if (ret != 0)
181 return ret;
182
183 link->conf.Attributes = CONF_ENABLE_IRQ;
184 link->conf.IntType = INT_MEMORY_AND_IO;
185
186 INIT_WORK(&ipw->work_reboot, signalled_reboot_work);
187
188 ipwireless_init_hardware_v1(ipw->hardware, link->resource[0]->start,
189 ipw->attr_memory, ipw->common_memory,
190 ipw->is_v2_card, signalled_reboot_callback,
191 ipw);
192
193 ret = pcmcia_request_irq(link, ipwireless_interrupt);
194 if (ret != 0)
195 goto exit;
196
197 printk(KERN_INFO IPWIRELESS_PCCARD_NAME ": Card type %s\n",
198 ipw->is_v2_card ? "V2/V3" : "V1");
199 printk(KERN_INFO IPWIRELESS_PCCARD_NAME
200 ": I/O ports %pR, irq %d\n", link->resource[0],
201 (unsigned int) link->irq);
202 if (ipw->attr_memory && ipw->common_memory)
203 printk(KERN_INFO IPWIRELESS_PCCARD_NAME
204 ": attr memory 0x%08lx-0x%08lx, common memory 0x%08lx-0x%08lx\n",
205 ipw->request_attr_memory.Base,
206 ipw->request_attr_memory.Base
207 + ipw->request_attr_memory.Size - 1,
208 ipw->request_common_memory.Base,
209 ipw->request_common_memory.Base
210 + ipw->request_common_memory.Size - 1);
211
212 ipw->network = ipwireless_network_create(ipw->hardware);
213 if (!ipw->network)
214 goto exit;
215
216 ipw->tty = ipwireless_tty_create(ipw->hardware, ipw->network);
217 if (!ipw->tty)
218 goto exit;
219
220 ipwireless_init_hardware_v2_v3(ipw->hardware);
221
222 /*
223 * Do the RequestConfiguration last, because it enables interrupts.
224 * Then we don't get any interrupts before we're ready for them.
225 */
226 ret = pcmcia_request_configuration(link, &link->conf);
227
228 if (ret != 0)
229 goto exit;
230
231 return 0;
232
233exit:
234 if (ipw->attr_memory) {
235 release_mem_region(ipw->request_attr_memory.Base,
236 ipw->request_attr_memory.Size);
237 iounmap(ipw->attr_memory);
238
239 }
240 if (ipw->common_memory) {
241 release_mem_region(ipw->request_common_memory.Base,
242 ipw->request_common_memory.Size);
243 iounmap(ipw->common_memory);
244 }
245 pcmcia_disable_device(link);
246 return -1;
247}
248
249static void release_ipwireless(struct ipw_dev *ipw)
250{
251 if (ipw->common_memory) {
252 release_mem_region(ipw->request_common_memory.Base,
253 ipw->request_common_memory.Size);
254 iounmap(ipw->common_memory);
255 }
256 if (ipw->attr_memory) {
257 release_mem_region(ipw->request_attr_memory.Base,
258 ipw->request_attr_memory.Size);
259 iounmap(ipw->attr_memory);
260 }
261 pcmcia_disable_device(ipw->link);
262}
263
264/*
265 * ipwireless_attach() creates an "instance" of the driver, allocating
266 * local data structures for one device (one interface). The device
267 * is registered with Card Services.
268 *
269 * The pcmcia_device structure is initialized, but we don't actually
270 * configure the card at this point -- we wait until we receive a
271 * card insertion event.
272 */
273static int ipwireless_attach(struct pcmcia_device *link)
274{
275 struct ipw_dev *ipw;
276 int ret;
277
278 ipw = kzalloc(sizeof(struct ipw_dev), GFP_KERNEL);
279 if (!ipw)
280 return -ENOMEM;
281
282 ipw->link = link;
283 link->priv = ipw;
284
285 ipw->hardware = ipwireless_hardware_create();
286 if (!ipw->hardware) {
287 kfree(ipw);
288 return -ENOMEM;
289 }
290 /* RegisterClient will call config_ipwireless */
291
292 ret = config_ipwireless(ipw);
293
294 if (ret != 0) {
295 ipwireless_detach(link);
296 return ret;
297 }
298
299 return 0;
300}
301
302/*
303 * This deletes a driver "instance". The device is de-registered with
304 * Card Services. If it has been released, all local data structures
305 * are freed. Otherwise, the structures will be freed when the device
306 * is released.
307 */
308static void ipwireless_detach(struct pcmcia_device *link)
309{
310 struct ipw_dev *ipw = link->priv;
311
312 release_ipwireless(ipw);
313
314 if (ipw->tty != NULL)
315 ipwireless_tty_free(ipw->tty);
316 if (ipw->network != NULL)
317 ipwireless_network_free(ipw->network);
318 if (ipw->hardware != NULL)
319 ipwireless_hardware_free(ipw->hardware);
320 kfree(ipw);
321}
322
323static struct pcmcia_driver me = {
324 .owner = THIS_MODULE,
325 .probe = ipwireless_attach,
326 .remove = ipwireless_detach,
327 .drv = { .name = IPWIRELESS_PCCARD_NAME },
328 .id_table = ipw_ids
329};
330
331/*
332 * Module insertion : initialisation of the module.
333 * Register the card with cardmgr...
334 */
335static int __init init_ipwireless(void)
336{
337 int ret;
338
339 printk(KERN_INFO IPWIRELESS_PCCARD_NAME " "
340 IPWIRELESS_PCMCIA_VERSION " by " IPWIRELESS_PCMCIA_AUTHOR "\n");
341
342 ret = ipwireless_tty_init();
343 if (ret != 0)
344 return ret;
345
346 ret = pcmcia_register_driver(&me);
347 if (ret != 0)
348 ipwireless_tty_release();
349
350 return ret;
351}
352
353/*
354 * Module removal
355 */
356static void __exit exit_ipwireless(void)
357{
358 printk(KERN_INFO IPWIRELESS_PCCARD_NAME " "
359 IPWIRELESS_PCMCIA_VERSION " removed\n");
360
361 pcmcia_unregister_driver(&me);
362 ipwireless_tty_release();
363}
364
365module_init(init_ipwireless);
366module_exit(exit_ipwireless);
367
368MODULE_AUTHOR(IPWIRELESS_PCMCIA_AUTHOR);
369MODULE_DESCRIPTION(IPWIRELESS_PCCARD_NAME " " IPWIRELESS_PCMCIA_VERSION);
370MODULE_LICENSE("GPL");
diff --git a/drivers/char/pcmcia/ipwireless/main.h b/drivers/char/pcmcia/ipwireless/main.h
deleted file mode 100644
index c207be87b597..000000000000
--- a/drivers/char/pcmcia/ipwireless/main.h
+++ /dev/null
@@ -1,73 +0,0 @@
1/*
2 * IPWireless 3G PCMCIA Network Driver
3 *
4 * Original code
5 * by Stephen Blackheath <stephen@blacksapphire.com>,
6 * Ben Martel <benm@symmetric.co.nz>
7 *
8 * Copyrighted as follows:
9 * Copyright (C) 2004 by Symmetric Systems Ltd (NZ)
10 *
11 * Various driver changes and rewrites, port to new kernels
12 * Copyright (C) 2006-2007 Jiri Kosina
13 *
14 * Misc code cleanups and updates
15 * Copyright (C) 2007 David Sterba
16 */
17
18#ifndef _IPWIRELESS_CS_H_
19#define _IPWIRELESS_CS_H_
20
21#include <linux/sched.h>
22#include <linux/types.h>
23
24#include <pcmcia/cs.h>
25#include <pcmcia/cistpl.h>
26#include <pcmcia/ds.h>
27
28#include "hardware.h"
29
30#define IPWIRELESS_PCCARD_NAME "ipwireless"
31#define IPWIRELESS_PCMCIA_VERSION "1.1"
32#define IPWIRELESS_PCMCIA_AUTHOR \
33 "Stephen Blackheath, Ben Martel, Jiri Kosina and David Sterba"
34
35#define IPWIRELESS_TX_QUEUE_SIZE 262144
36#define IPWIRELESS_RX_QUEUE_SIZE 262144
37
38#define IPWIRELESS_STATE_DEBUG
39
40struct ipw_hardware;
41struct ipw_network;
42struct ipw_tty;
43
44struct ipw_dev {
45 struct pcmcia_device *link;
46 int is_v2_card;
47
48 window_handle_t handle_attr_memory;
49 void __iomem *attr_memory;
50 win_req_t request_attr_memory;
51
52 window_handle_t handle_common_memory;
53 void __iomem *common_memory;
54 win_req_t request_common_memory;
55
56 /* Reference to attribute memory, containing CIS data */
57 void *attribute_memory;
58
59 /* Hardware context */
60 struct ipw_hardware *hardware;
61 /* Network layer context */
62 struct ipw_network *network;
63 /* TTY device context */
64 struct ipw_tty *tty;
65 struct work_struct work_reboot;
66};
67
68/* Module parametres */
69extern int ipwireless_debug;
70extern int ipwireless_loopback;
71extern int ipwireless_out_queue;
72
73#endif
diff --git a/drivers/char/pcmcia/ipwireless/network.c b/drivers/char/pcmcia/ipwireless/network.c
deleted file mode 100644
index 9fe538347932..000000000000
--- a/drivers/char/pcmcia/ipwireless/network.c
+++ /dev/null
@@ -1,507 +0,0 @@
1/*
2 * IPWireless 3G PCMCIA Network Driver
3 *
4 * Original code
5 * by Stephen Blackheath <stephen@blacksapphire.com>,
6 * Ben Martel <benm@symmetric.co.nz>
7 *
8 * Copyrighted as follows:
9 * Copyright (C) 2004 by Symmetric Systems Ltd (NZ)
10 *
11 * Various driver changes and rewrites, port to new kernels
12 * Copyright (C) 2006-2007 Jiri Kosina
13 *
14 * Misc code cleanups and updates
15 * Copyright (C) 2007 David Sterba
16 */
17
18#include <linux/interrupt.h>
19#include <linux/kernel.h>
20#include <linux/mutex.h>
21#include <linux/netdevice.h>
22#include <linux/ppp_channel.h>
23#include <linux/ppp_defs.h>
24#include <linux/slab.h>
25#include <linux/if_ppp.h>
26#include <linux/skbuff.h>
27
28#include "network.h"
29#include "hardware.h"
30#include "main.h"
31#include "tty.h"
32
33#define MAX_ASSOCIATED_TTYS 2
34
35#define SC_RCV_BITS (SC_RCV_B7_1|SC_RCV_B7_0|SC_RCV_ODDP|SC_RCV_EVNP)
36
37struct ipw_network {
38 /* Hardware context, used for calls to hardware layer. */
39 struct ipw_hardware *hardware;
40 /* Context for kernel 'generic_ppp' functionality */
41 struct ppp_channel *ppp_channel;
42 /* tty context connected with IPW console */
43 struct ipw_tty *associated_ttys[NO_OF_IPW_CHANNELS][MAX_ASSOCIATED_TTYS];
44 /* True if ppp needs waking up once we're ready to xmit */
45 int ppp_blocked;
46 /* Number of packets queued up in hardware module. */
47 int outgoing_packets_queued;
48 /* Spinlock to avoid interrupts during shutdown */
49 spinlock_t lock;
50 struct mutex close_lock;
51
52 /* PPP ioctl data, not actually used anywere */
53 unsigned int flags;
54 unsigned int rbits;
55 u32 xaccm[8];
56 u32 raccm;
57 int mru;
58
59 int shutting_down;
60 unsigned int ras_control_lines;
61
62 struct work_struct work_go_online;
63 struct work_struct work_go_offline;
64};
65
66static void notify_packet_sent(void *callback_data, unsigned int packet_length)
67{
68 struct ipw_network *network = callback_data;
69 unsigned long flags;
70
71 spin_lock_irqsave(&network->lock, flags);
72 network->outgoing_packets_queued--;
73 if (network->ppp_channel != NULL) {
74 if (network->ppp_blocked) {
75 network->ppp_blocked = 0;
76 spin_unlock_irqrestore(&network->lock, flags);
77 ppp_output_wakeup(network->ppp_channel);
78 if (ipwireless_debug)
79 printk(KERN_DEBUG IPWIRELESS_PCCARD_NAME
80 ": ppp unblocked\n");
81 } else
82 spin_unlock_irqrestore(&network->lock, flags);
83 } else
84 spin_unlock_irqrestore(&network->lock, flags);
85}
86
87/*
88 * Called by the ppp system when it has a packet to send to the hardware.
89 */
90static int ipwireless_ppp_start_xmit(struct ppp_channel *ppp_channel,
91 struct sk_buff *skb)
92{
93 struct ipw_network *network = ppp_channel->private;
94 unsigned long flags;
95
96 spin_lock_irqsave(&network->lock, flags);
97 if (network->outgoing_packets_queued < ipwireless_out_queue) {
98 unsigned char *buf;
99 static unsigned char header[] = {
100 PPP_ALLSTATIONS, /* 0xff */
101 PPP_UI, /* 0x03 */
102 };
103 int ret;
104
105 network->outgoing_packets_queued++;
106 spin_unlock_irqrestore(&network->lock, flags);
107
108 /*
109 * If we have the requested amount of headroom in the skb we
110 * were handed, then we can add the header efficiently.
111 */
112 if (skb_headroom(skb) >= 2) {
113 memcpy(skb_push(skb, 2), header, 2);
114 ret = ipwireless_send_packet(network->hardware,
115 IPW_CHANNEL_RAS, skb->data,
116 skb->len,
117 notify_packet_sent,
118 network);
119 if (ret == -1) {
120 skb_pull(skb, 2);
121 return 0;
122 }
123 } else {
124 /* Otherwise (rarely) we do it inefficiently. */
125 buf = kmalloc(skb->len + 2, GFP_ATOMIC);
126 if (!buf)
127 return 0;
128 memcpy(buf + 2, skb->data, skb->len);
129 memcpy(buf, header, 2);
130 ret = ipwireless_send_packet(network->hardware,
131 IPW_CHANNEL_RAS, buf,
132 skb->len + 2,
133 notify_packet_sent,
134 network);
135 kfree(buf);
136 if (ret == -1)
137 return 0;
138 }
139 kfree_skb(skb);
140 return 1;
141 } else {
142 /*
143 * Otherwise reject the packet, and flag that the ppp system
144 * needs to be unblocked once we are ready to send.
145 */
146 network->ppp_blocked = 1;
147 spin_unlock_irqrestore(&network->lock, flags);
148 if (ipwireless_debug)
149 printk(KERN_DEBUG IPWIRELESS_PCCARD_NAME ": ppp blocked\n");
150 return 0;
151 }
152}
153
154/* Handle an ioctl call that has come in via ppp. (copy of ppp_async_ioctl() */
155static int ipwireless_ppp_ioctl(struct ppp_channel *ppp_channel,
156 unsigned int cmd, unsigned long arg)
157{
158 struct ipw_network *network = ppp_channel->private;
159 int err, val;
160 u32 accm[8];
161 int __user *user_arg = (int __user *) arg;
162
163 err = -EFAULT;
164 switch (cmd) {
165 case PPPIOCGFLAGS:
166 val = network->flags | network->rbits;
167 if (put_user(val, user_arg))
168 break;
169 err = 0;
170 break;
171
172 case PPPIOCSFLAGS:
173 if (get_user(val, user_arg))
174 break;
175 network->flags = val & ~SC_RCV_BITS;
176 network->rbits = val & SC_RCV_BITS;
177 err = 0;
178 break;
179
180 case PPPIOCGASYNCMAP:
181 if (put_user(network->xaccm[0], user_arg))
182 break;
183 err = 0;
184 break;
185
186 case PPPIOCSASYNCMAP:
187 if (get_user(network->xaccm[0], user_arg))
188 break;
189 err = 0;
190 break;
191
192 case PPPIOCGRASYNCMAP:
193 if (put_user(network->raccm, user_arg))
194 break;
195 err = 0;
196 break;
197
198 case PPPIOCSRASYNCMAP:
199 if (get_user(network->raccm, user_arg))
200 break;
201 err = 0;
202 break;
203
204 case PPPIOCGXASYNCMAP:
205 if (copy_to_user((void __user *) arg, network->xaccm,
206 sizeof(network->xaccm)))
207 break;
208 err = 0;
209 break;
210
211 case PPPIOCSXASYNCMAP:
212 if (copy_from_user(accm, (void __user *) arg, sizeof(accm)))
213 break;
214 accm[2] &= ~0x40000000U; /* can't escape 0x5e */
215 accm[3] |= 0x60000000U; /* must escape 0x7d, 0x7e */
216 memcpy(network->xaccm, accm, sizeof(network->xaccm));
217 err = 0;
218 break;
219
220 case PPPIOCGMRU:
221 if (put_user(network->mru, user_arg))
222 break;
223 err = 0;
224 break;
225
226 case PPPIOCSMRU:
227 if (get_user(val, user_arg))
228 break;
229 if (val < PPP_MRU)
230 val = PPP_MRU;
231 network->mru = val;
232 err = 0;
233 break;
234
235 default:
236 err = -ENOTTY;
237 }
238
239 return err;
240}
241
242static const struct ppp_channel_ops ipwireless_ppp_channel_ops = {
243 .start_xmit = ipwireless_ppp_start_xmit,
244 .ioctl = ipwireless_ppp_ioctl
245};
246
247static void do_go_online(struct work_struct *work_go_online)
248{
249 struct ipw_network *network =
250 container_of(work_go_online, struct ipw_network,
251 work_go_online);
252 unsigned long flags;
253
254 spin_lock_irqsave(&network->lock, flags);
255 if (!network->ppp_channel) {
256 struct ppp_channel *channel;
257
258 spin_unlock_irqrestore(&network->lock, flags);
259 channel = kzalloc(sizeof(struct ppp_channel), GFP_KERNEL);
260 if (!channel) {
261 printk(KERN_ERR IPWIRELESS_PCCARD_NAME
262 ": unable to allocate PPP channel\n");
263 return;
264 }
265 channel->private = network;
266 channel->mtu = 16384; /* Wild guess */
267 channel->hdrlen = 2;
268 channel->ops = &ipwireless_ppp_channel_ops;
269
270 network->flags = 0;
271 network->rbits = 0;
272 network->mru = PPP_MRU;
273 memset(network->xaccm, 0, sizeof(network->xaccm));
274 network->xaccm[0] = ~0U;
275 network->xaccm[3] = 0x60000000U;
276 network->raccm = ~0U;
277 ppp_register_channel(channel);
278 spin_lock_irqsave(&network->lock, flags);
279 network->ppp_channel = channel;
280 }
281 spin_unlock_irqrestore(&network->lock, flags);
282}
283
284static void do_go_offline(struct work_struct *work_go_offline)
285{
286 struct ipw_network *network =
287 container_of(work_go_offline, struct ipw_network,
288 work_go_offline);
289 unsigned long flags;
290
291 mutex_lock(&network->close_lock);
292 spin_lock_irqsave(&network->lock, flags);
293 if (network->ppp_channel != NULL) {
294 struct ppp_channel *channel = network->ppp_channel;
295
296 network->ppp_channel = NULL;
297 spin_unlock_irqrestore(&network->lock, flags);
298 mutex_unlock(&network->close_lock);
299 ppp_unregister_channel(channel);
300 } else {
301 spin_unlock_irqrestore(&network->lock, flags);
302 mutex_unlock(&network->close_lock);
303 }
304}
305
306void ipwireless_network_notify_control_line_change(struct ipw_network *network,
307 unsigned int channel_idx,
308 unsigned int control_lines,
309 unsigned int changed_mask)
310{
311 int i;
312
313 if (channel_idx == IPW_CHANNEL_RAS)
314 network->ras_control_lines = control_lines;
315
316 for (i = 0; i < MAX_ASSOCIATED_TTYS; i++) {
317 struct ipw_tty *tty =
318 network->associated_ttys[channel_idx][i];
319
320 /*
321 * If it's associated with a tty (other than the RAS channel
322 * when we're online), then send the data to that tty. The RAS
323 * channel's data is handled above - it always goes through
324 * ppp_generic.
325 */
326 if (tty)
327 ipwireless_tty_notify_control_line_change(tty,
328 channel_idx,
329 control_lines,
330 changed_mask);
331 }
332}
333
334/*
335 * Some versions of firmware stuff packets with 0xff 0x03 (PPP: ALLSTATIONS, UI)
336 * bytes, which are required on sent packet, but not always present on received
337 * packets
338 */
339static struct sk_buff *ipw_packet_received_skb(unsigned char *data,
340 unsigned int length)
341{
342 struct sk_buff *skb;
343
344 if (length > 2 && data[0] == PPP_ALLSTATIONS && data[1] == PPP_UI) {
345 length -= 2;
346 data += 2;
347 }
348
349 skb = dev_alloc_skb(length + 4);
350 skb_reserve(skb, 2);
351 memcpy(skb_put(skb, length), data, length);
352
353 return skb;
354}
355
356void ipwireless_network_packet_received(struct ipw_network *network,
357 unsigned int channel_idx,
358 unsigned char *data,
359 unsigned int length)
360{
361 int i;
362 unsigned long flags;
363
364 for (i = 0; i < MAX_ASSOCIATED_TTYS; i++) {
365 struct ipw_tty *tty = network->associated_ttys[channel_idx][i];
366
367 if (!tty)
368 continue;
369
370 /*
371 * If it's associated with a tty (other than the RAS channel
372 * when we're online), then send the data to that tty. The RAS
373 * channel's data is handled above - it always goes through
374 * ppp_generic.
375 */
376 if (channel_idx == IPW_CHANNEL_RAS
377 && (network->ras_control_lines &
378 IPW_CONTROL_LINE_DCD) != 0
379 && ipwireless_tty_is_modem(tty)) {
380 /*
381 * If data came in on the RAS channel and this tty is
382 * the modem tty, and we are online, then we send it to
383 * the PPP layer.
384 */
385 mutex_lock(&network->close_lock);
386 spin_lock_irqsave(&network->lock, flags);
387 if (network->ppp_channel != NULL) {
388 struct sk_buff *skb;
389
390 spin_unlock_irqrestore(&network->lock,
391 flags);
392
393 /* Send the data to the ppp_generic module. */
394 skb = ipw_packet_received_skb(data, length);
395 ppp_input(network->ppp_channel, skb);
396 } else
397 spin_unlock_irqrestore(&network->lock,
398 flags);
399 mutex_unlock(&network->close_lock);
400 }
401 /* Otherwise we send it out the tty. */
402 else
403 ipwireless_tty_received(tty, data, length);
404 }
405}
406
407struct ipw_network *ipwireless_network_create(struct ipw_hardware *hw)
408{
409 struct ipw_network *network =
410 kzalloc(sizeof(struct ipw_network), GFP_ATOMIC);
411
412 if (!network)
413 return NULL;
414
415 spin_lock_init(&network->lock);
416 mutex_init(&network->close_lock);
417
418 network->hardware = hw;
419
420 INIT_WORK(&network->work_go_online, do_go_online);
421 INIT_WORK(&network->work_go_offline, do_go_offline);
422
423 ipwireless_associate_network(hw, network);
424
425 return network;
426}
427
428void ipwireless_network_free(struct ipw_network *network)
429{
430 network->shutting_down = 1;
431
432 ipwireless_ppp_close(network);
433 flush_scheduled_work();
434
435 ipwireless_stop_interrupts(network->hardware);
436 ipwireless_associate_network(network->hardware, NULL);
437
438 kfree(network);
439}
440
441void ipwireless_associate_network_tty(struct ipw_network *network,
442 unsigned int channel_idx,
443 struct ipw_tty *tty)
444{
445 int i;
446
447 for (i = 0; i < MAX_ASSOCIATED_TTYS; i++)
448 if (network->associated_ttys[channel_idx][i] == NULL) {
449 network->associated_ttys[channel_idx][i] = tty;
450 break;
451 }
452}
453
454void ipwireless_disassociate_network_ttys(struct ipw_network *network,
455 unsigned int channel_idx)
456{
457 int i;
458
459 for (i = 0; i < MAX_ASSOCIATED_TTYS; i++)
460 network->associated_ttys[channel_idx][i] = NULL;
461}
462
463void ipwireless_ppp_open(struct ipw_network *network)
464{
465 if (ipwireless_debug)
466 printk(KERN_DEBUG IPWIRELESS_PCCARD_NAME ": online\n");
467 schedule_work(&network->work_go_online);
468}
469
470void ipwireless_ppp_close(struct ipw_network *network)
471{
472 /* Disconnect from the wireless network. */
473 if (ipwireless_debug)
474 printk(KERN_DEBUG IPWIRELESS_PCCARD_NAME ": offline\n");
475 schedule_work(&network->work_go_offline);
476}
477
478int ipwireless_ppp_channel_index(struct ipw_network *network)
479{
480 int ret = -1;
481 unsigned long flags;
482
483 spin_lock_irqsave(&network->lock, flags);
484 if (network->ppp_channel != NULL)
485 ret = ppp_channel_index(network->ppp_channel);
486 spin_unlock_irqrestore(&network->lock, flags);
487
488 return ret;
489}
490
491int ipwireless_ppp_unit_number(struct ipw_network *network)
492{
493 int ret = -1;
494 unsigned long flags;
495
496 spin_lock_irqsave(&network->lock, flags);
497 if (network->ppp_channel != NULL)
498 ret = ppp_unit_number(network->ppp_channel);
499 spin_unlock_irqrestore(&network->lock, flags);
500
501 return ret;
502}
503
504int ipwireless_ppp_mru(const struct ipw_network *network)
505{
506 return network->mru;
507}
diff --git a/drivers/char/pcmcia/ipwireless/network.h b/drivers/char/pcmcia/ipwireless/network.h
deleted file mode 100644
index 561f765b3334..000000000000
--- a/drivers/char/pcmcia/ipwireless/network.h
+++ /dev/null
@@ -1,53 +0,0 @@
1/*
2 * IPWireless 3G PCMCIA Network Driver
3 *
4 * Original code
5 * by Stephen Blackheath <stephen@blacksapphire.com>,
6 * Ben Martel <benm@symmetric.co.nz>
7 *
8 * Copyrighted as follows:
9 * Copyright (C) 2004 by Symmetric Systems Ltd (NZ)
10 *
11 * Various driver changes and rewrites, port to new kernels
12 * Copyright (C) 2006-2007 Jiri Kosina
13 *
14 * Misc code cleanups and updates
15 * Copyright (C) 2007 David Sterba
16 */
17
18#ifndef _IPWIRELESS_CS_NETWORK_H_
19#define _IPWIRELESS_CS_NETWORK_H_
20
21#include <linux/types.h>
22
23struct ipw_network;
24struct ipw_tty;
25struct ipw_hardware;
26
27/* Definitions of the different channels on the PCMCIA UE */
28#define IPW_CHANNEL_RAS 0
29#define IPW_CHANNEL_DIALLER 1
30#define IPW_CHANNEL_CONSOLE 2
31#define NO_OF_IPW_CHANNELS 5
32
33void ipwireless_network_notify_control_line_change(struct ipw_network *net,
34 unsigned int channel_idx, unsigned int control_lines,
35 unsigned int control_mask);
36void ipwireless_network_packet_received(struct ipw_network *net,
37 unsigned int channel_idx, unsigned char *data,
38 unsigned int length);
39struct ipw_network *ipwireless_network_create(struct ipw_hardware *hw);
40void ipwireless_network_free(struct ipw_network *net);
41void ipwireless_associate_network_tty(struct ipw_network *net,
42 unsigned int channel_idx, struct ipw_tty *tty);
43void ipwireless_disassociate_network_ttys(struct ipw_network *net,
44 unsigned int channel_idx);
45
46void ipwireless_ppp_open(struct ipw_network *net);
47
48void ipwireless_ppp_close(struct ipw_network *net);
49int ipwireless_ppp_channel_index(struct ipw_network *net);
50int ipwireless_ppp_unit_number(struct ipw_network *net);
51int ipwireless_ppp_mru(const struct ipw_network *net);
52
53#endif
diff --git a/drivers/char/pcmcia/ipwireless/setup_protocol.h b/drivers/char/pcmcia/ipwireless/setup_protocol.h
deleted file mode 100644
index 9d6bcc77c73c..000000000000
--- a/drivers/char/pcmcia/ipwireless/setup_protocol.h
+++ /dev/null
@@ -1,108 +0,0 @@
1/*
2 * IPWireless 3G PCMCIA Network Driver
3 *
4 * Original code
5 * by Stephen Blackheath <stephen@blacksapphire.com>,
6 * Ben Martel <benm@symmetric.co.nz>
7 *
8 * Copyrighted as follows:
9 * Copyright (C) 2004 by Symmetric Systems Ltd (NZ)
10 *
11 * Various driver changes and rewrites, port to new kernels
12 * Copyright (C) 2006-2007 Jiri Kosina
13 *
14 * Misc code cleanups and updates
15 * Copyright (C) 2007 David Sterba
16 */
17
18#ifndef _IPWIRELESS_CS_SETUP_PROTOCOL_H_
19#define _IPWIRELESS_CS_SETUP_PROTOCOL_H_
20
21/* Version of the setup protocol and transport protocols */
22#define TL_SETUP_VERSION 1
23
24#define TL_SETUP_VERSION_QRY_TMO 1000
25#define TL_SETUP_MAX_VERSION_QRY 30
26
27/* Message numbers 0-9 are obsoleted and must not be reused! */
28#define TL_SETUP_SIGNO_GET_VERSION_QRY 10
29#define TL_SETUP_SIGNO_GET_VERSION_RSP 11
30#define TL_SETUP_SIGNO_CONFIG_MSG 12
31#define TL_SETUP_SIGNO_CONFIG_DONE_MSG 13
32#define TL_SETUP_SIGNO_OPEN_MSG 14
33#define TL_SETUP_SIGNO_CLOSE_MSG 15
34
35#define TL_SETUP_SIGNO_INFO_MSG 20
36#define TL_SETUP_SIGNO_INFO_MSG_ACK 21
37
38#define TL_SETUP_SIGNO_REBOOT_MSG 22
39#define TL_SETUP_SIGNO_REBOOT_MSG_ACK 23
40
41/* Synchronous start-messages */
42struct tl_setup_get_version_qry {
43 unsigned char sig_no; /* TL_SETUP_SIGNO_GET_VERSION_QRY */
44} __attribute__ ((__packed__));
45
46struct tl_setup_get_version_rsp {
47 unsigned char sig_no; /* TL_SETUP_SIGNO_GET_VERSION_RSP */
48 unsigned char version; /* TL_SETUP_VERSION */
49} __attribute__ ((__packed__));
50
51struct tl_setup_config_msg {
52 unsigned char sig_no; /* TL_SETUP_SIGNO_CONFIG_MSG */
53 unsigned char port_no;
54 unsigned char prio_data;
55 unsigned char prio_ctrl;
56} __attribute__ ((__packed__));
57
58struct tl_setup_config_done_msg {
59 unsigned char sig_no; /* TL_SETUP_SIGNO_CONFIG_DONE_MSG */
60} __attribute__ ((__packed__));
61
62/* Asyncronous messages */
63struct tl_setup_open_msg {
64 unsigned char sig_no; /* TL_SETUP_SIGNO_OPEN_MSG */
65 unsigned char port_no;
66} __attribute__ ((__packed__));
67
68struct tl_setup_close_msg {
69 unsigned char sig_no; /* TL_SETUP_SIGNO_CLOSE_MSG */
70 unsigned char port_no;
71} __attribute__ ((__packed__));
72
73/* Driver type - for use in tl_setup_info_msg.driver_type */
74#define COMM_DRIVER 0
75#define NDISWAN_DRIVER 1
76#define NDISWAN_DRIVER_MAJOR_VERSION 2
77#define NDISWAN_DRIVER_MINOR_VERSION 0
78
79/*
80 * It should not matter when this message comes over as we just store the
81 * results and send the ACK.
82 */
83struct tl_setup_info_msg {
84 unsigned char sig_no; /* TL_SETUP_SIGNO_INFO_MSG */
85 unsigned char driver_type;
86 unsigned char major_version;
87 unsigned char minor_version;
88} __attribute__ ((__packed__));
89
90struct tl_setup_info_msgAck {
91 unsigned char sig_no; /* TL_SETUP_SIGNO_INFO_MSG_ACK */
92} __attribute__ ((__packed__));
93
94struct TlSetupRebootMsgAck {
95 unsigned char sig_no; /* TL_SETUP_SIGNO_REBOOT_MSG_ACK */
96} __attribute__ ((__packed__));
97
98/* Define a union of all the msgs that the driver can receive from the card.*/
99union ipw_setup_rx_msg {
100 unsigned char sig_no;
101 struct tl_setup_get_version_rsp version_rsp_msg;
102 struct tl_setup_open_msg open_msg;
103 struct tl_setup_close_msg close_msg;
104 struct tl_setup_info_msg InfoMsg;
105 struct tl_setup_info_msgAck info_msg_ack;
106} __attribute__ ((__packed__));
107
108#endif /* _IPWIRELESS_CS_SETUP_PROTOCOL_H_ */
diff --git a/drivers/char/pcmcia/ipwireless/tty.c b/drivers/char/pcmcia/ipwireless/tty.c
deleted file mode 100644
index 1a2c2c3b068f..000000000000
--- a/drivers/char/pcmcia/ipwireless/tty.c
+++ /dev/null
@@ -1,679 +0,0 @@
1/*
2 * IPWireless 3G PCMCIA Network Driver
3 *
4 * Original code
5 * by Stephen Blackheath <stephen@blacksapphire.com>,
6 * Ben Martel <benm@symmetric.co.nz>
7 *
8 * Copyrighted as follows:
9 * Copyright (C) 2004 by Symmetric Systems Ltd (NZ)
10 *
11 * Various driver changes and rewrites, port to new kernels
12 * Copyright (C) 2006-2007 Jiri Kosina
13 *
14 * Misc code cleanups and updates
15 * Copyright (C) 2007 David Sterba
16 */
17
18#include <linux/init.h>
19#include <linux/kernel.h>
20#include <linux/module.h>
21#include <linux/mutex.h>
22#include <linux/ppp_defs.h>
23#include <linux/if.h>
24#include <linux/if_ppp.h>
25#include <linux/sched.h>
26#include <linux/serial.h>
27#include <linux/slab.h>
28#include <linux/tty.h>
29#include <linux/tty_driver.h>
30#include <linux/tty_flip.h>
31#include <linux/uaccess.h>
32
33#include "tty.h"
34#include "network.h"
35#include "hardware.h"
36#include "main.h"
37
38#define IPWIRELESS_PCMCIA_START (0)
39#define IPWIRELESS_PCMCIA_MINORS (24)
40#define IPWIRELESS_PCMCIA_MINOR_RANGE (8)
41
42#define TTYTYPE_MODEM (0)
43#define TTYTYPE_MONITOR (1)
44#define TTYTYPE_RAS_RAW (2)
45
46struct ipw_tty {
47 int index;
48 struct ipw_hardware *hardware;
49 unsigned int channel_idx;
50 unsigned int secondary_channel_idx;
51 int tty_type;
52 struct ipw_network *network;
53 struct tty_struct *linux_tty;
54 int open_count;
55 unsigned int control_lines;
56 struct mutex ipw_tty_mutex;
57 int tx_bytes_queued;
58 int closing;
59};
60
61static struct ipw_tty *ttys[IPWIRELESS_PCMCIA_MINORS];
62
63static struct tty_driver *ipw_tty_driver;
64
65static char *tty_type_name(int tty_type)
66{
67 static char *channel_names[] = {
68 "modem",
69 "monitor",
70 "RAS-raw"
71 };
72
73 return channel_names[tty_type];
74}
75
76static void report_registering(struct ipw_tty *tty)
77{
78 char *iftype = tty_type_name(tty->tty_type);
79
80 printk(KERN_INFO IPWIRELESS_PCCARD_NAME
81 ": registering %s device ttyIPWp%d\n", iftype, tty->index);
82}
83
84static void report_deregistering(struct ipw_tty *tty)
85{
86 char *iftype = tty_type_name(tty->tty_type);
87
88 printk(KERN_INFO IPWIRELESS_PCCARD_NAME
89 ": deregistering %s device ttyIPWp%d\n", iftype,
90 tty->index);
91}
92
93static struct ipw_tty *get_tty(int minor)
94{
95 if (minor < ipw_tty_driver->minor_start
96 || minor >= ipw_tty_driver->minor_start +
97 IPWIRELESS_PCMCIA_MINORS)
98 return NULL;
99 else {
100 int minor_offset = minor - ipw_tty_driver->minor_start;
101
102 /*
103 * The 'ras_raw' channel is only available when 'loopback' mode
104 * is enabled.
105 * Number of minor starts with 16 (_RANGE * _RAS_RAW).
106 */
107 if (!ipwireless_loopback &&
108 minor_offset >=
109 IPWIRELESS_PCMCIA_MINOR_RANGE * TTYTYPE_RAS_RAW)
110 return NULL;
111
112 return ttys[minor_offset];
113 }
114}
115
116static int ipw_open(struct tty_struct *linux_tty, struct file *filp)
117{
118 int minor = linux_tty->index;
119 struct ipw_tty *tty = get_tty(minor);
120
121 if (!tty)
122 return -ENODEV;
123
124 mutex_lock(&tty->ipw_tty_mutex);
125
126 if (tty->closing) {
127 mutex_unlock(&tty->ipw_tty_mutex);
128 return -ENODEV;
129 }
130 if (tty->open_count == 0)
131 tty->tx_bytes_queued = 0;
132
133 tty->open_count++;
134
135 tty->linux_tty = linux_tty;
136 linux_tty->driver_data = tty;
137 linux_tty->low_latency = 1;
138
139 if (tty->tty_type == TTYTYPE_MODEM)
140 ipwireless_ppp_open(tty->network);
141
142 mutex_unlock(&tty->ipw_tty_mutex);
143
144 return 0;
145}
146
147static void do_ipw_close(struct ipw_tty *tty)
148{
149 tty->open_count--;
150
151 if (tty->open_count == 0) {
152 struct tty_struct *linux_tty = tty->linux_tty;
153
154 if (linux_tty != NULL) {
155 tty->linux_tty = NULL;
156 linux_tty->driver_data = NULL;
157
158 if (tty->tty_type == TTYTYPE_MODEM)
159 ipwireless_ppp_close(tty->network);
160 }
161 }
162}
163
164static void ipw_hangup(struct tty_struct *linux_tty)
165{
166 struct ipw_tty *tty = linux_tty->driver_data;
167
168 if (!tty)
169 return;
170
171 mutex_lock(&tty->ipw_tty_mutex);
172 if (tty->open_count == 0) {
173 mutex_unlock(&tty->ipw_tty_mutex);
174 return;
175 }
176
177 do_ipw_close(tty);
178
179 mutex_unlock(&tty->ipw_tty_mutex);
180}
181
182static void ipw_close(struct tty_struct *linux_tty, struct file *filp)
183{
184 ipw_hangup(linux_tty);
185}
186
187/* Take data received from hardware, and send it out the tty */
188void ipwireless_tty_received(struct ipw_tty *tty, unsigned char *data,
189 unsigned int length)
190{
191 struct tty_struct *linux_tty;
192 int work = 0;
193
194 mutex_lock(&tty->ipw_tty_mutex);
195 linux_tty = tty->linux_tty;
196 if (linux_tty == NULL) {
197 mutex_unlock(&tty->ipw_tty_mutex);
198 return;
199 }
200
201 if (!tty->open_count) {
202 mutex_unlock(&tty->ipw_tty_mutex);
203 return;
204 }
205 mutex_unlock(&tty->ipw_tty_mutex);
206
207 work = tty_insert_flip_string(linux_tty, data, length);
208
209 if (work != length)
210 printk(KERN_DEBUG IPWIRELESS_PCCARD_NAME
211 ": %d chars not inserted to flip buffer!\n",
212 length - work);
213
214 /*
215 * This may sleep if ->low_latency is set
216 */
217 if (work)
218 tty_flip_buffer_push(linux_tty);
219}
220
221static void ipw_write_packet_sent_callback(void *callback_data,
222 unsigned int packet_length)
223{
224 struct ipw_tty *tty = callback_data;
225
226 /*
227 * Packet has been sent, so we subtract the number of bytes from our
228 * tally of outstanding TX bytes.
229 */
230 tty->tx_bytes_queued -= packet_length;
231}
232
233static int ipw_write(struct tty_struct *linux_tty,
234 const unsigned char *buf, int count)
235{
236 struct ipw_tty *tty = linux_tty->driver_data;
237 int room, ret;
238
239 if (!tty)
240 return -ENODEV;
241
242 mutex_lock(&tty->ipw_tty_mutex);
243 if (!tty->open_count) {
244 mutex_unlock(&tty->ipw_tty_mutex);
245 return -EINVAL;
246 }
247
248 room = IPWIRELESS_TX_QUEUE_SIZE - tty->tx_bytes_queued;
249 if (room < 0)
250 room = 0;
251 /* Don't allow caller to write any more than we have room for */
252 if (count > room)
253 count = room;
254
255 if (count == 0) {
256 mutex_unlock(&tty->ipw_tty_mutex);
257 return 0;
258 }
259
260 ret = ipwireless_send_packet(tty->hardware, IPW_CHANNEL_RAS,
261 buf, count,
262 ipw_write_packet_sent_callback, tty);
263 if (ret == -1) {
264 mutex_unlock(&tty->ipw_tty_mutex);
265 return 0;
266 }
267
268 tty->tx_bytes_queued += count;
269 mutex_unlock(&tty->ipw_tty_mutex);
270
271 return count;
272}
273
274static int ipw_write_room(struct tty_struct *linux_tty)
275{
276 struct ipw_tty *tty = linux_tty->driver_data;
277 int room;
278
279 /* FIXME: Exactly how is the tty object locked here .. */
280 if (!tty)
281 return -ENODEV;
282
283 if (!tty->open_count)
284 return -EINVAL;
285
286 room = IPWIRELESS_TX_QUEUE_SIZE - tty->tx_bytes_queued;
287 if (room < 0)
288 room = 0;
289
290 return room;
291}
292
293static int ipwireless_get_serial_info(struct ipw_tty *tty,
294 struct serial_struct __user *retinfo)
295{
296 struct serial_struct tmp;
297
298 if (!retinfo)
299 return (-EFAULT);
300
301 memset(&tmp, 0, sizeof(tmp));
302 tmp.type = PORT_UNKNOWN;
303 tmp.line = tty->index;
304 tmp.port = 0;
305 tmp.irq = 0;
306 tmp.flags = 0;
307 tmp.baud_base = 115200;
308 tmp.close_delay = 0;
309 tmp.closing_wait = 0;
310 tmp.custom_divisor = 0;
311 tmp.hub6 = 0;
312 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
313 return -EFAULT;
314
315 return 0;
316}
317
318static int ipw_chars_in_buffer(struct tty_struct *linux_tty)
319{
320 struct ipw_tty *tty = linux_tty->driver_data;
321
322 if (!tty)
323 return 0;
324
325 if (!tty->open_count)
326 return 0;
327
328 return tty->tx_bytes_queued;
329}
330
331static int get_control_lines(struct ipw_tty *tty)
332{
333 unsigned int my = tty->control_lines;
334 unsigned int out = 0;
335
336 if (my & IPW_CONTROL_LINE_RTS)
337 out |= TIOCM_RTS;
338 if (my & IPW_CONTROL_LINE_DTR)
339 out |= TIOCM_DTR;
340 if (my & IPW_CONTROL_LINE_CTS)
341 out |= TIOCM_CTS;
342 if (my & IPW_CONTROL_LINE_DSR)
343 out |= TIOCM_DSR;
344 if (my & IPW_CONTROL_LINE_DCD)
345 out |= TIOCM_CD;
346
347 return out;
348}
349
350static int set_control_lines(struct ipw_tty *tty, unsigned int set,
351 unsigned int clear)
352{
353 int ret;
354
355 if (set & TIOCM_RTS) {
356 ret = ipwireless_set_RTS(tty->hardware, tty->channel_idx, 1);
357 if (ret)
358 return ret;
359 if (tty->secondary_channel_idx != -1) {
360 ret = ipwireless_set_RTS(tty->hardware,
361 tty->secondary_channel_idx, 1);
362 if (ret)
363 return ret;
364 }
365 }
366 if (set & TIOCM_DTR) {
367 ret = ipwireless_set_DTR(tty->hardware, tty->channel_idx, 1);
368 if (ret)
369 return ret;
370 if (tty->secondary_channel_idx != -1) {
371 ret = ipwireless_set_DTR(tty->hardware,
372 tty->secondary_channel_idx, 1);
373 if (ret)
374 return ret;
375 }
376 }
377 if (clear & TIOCM_RTS) {
378 ret = ipwireless_set_RTS(tty->hardware, tty->channel_idx, 0);
379 if (tty->secondary_channel_idx != -1) {
380 ret = ipwireless_set_RTS(tty->hardware,
381 tty->secondary_channel_idx, 0);
382 if (ret)
383 return ret;
384 }
385 }
386 if (clear & TIOCM_DTR) {
387 ret = ipwireless_set_DTR(tty->hardware, tty->channel_idx, 0);
388 if (tty->secondary_channel_idx != -1) {
389 ret = ipwireless_set_DTR(tty->hardware,
390 tty->secondary_channel_idx, 0);
391 if (ret)
392 return ret;
393 }
394 }
395 return 0;
396}
397
398static int ipw_tiocmget(struct tty_struct *linux_tty, struct file *file)
399{
400 struct ipw_tty *tty = linux_tty->driver_data;
401 /* FIXME: Exactly how is the tty object locked here .. */
402
403 if (!tty)
404 return -ENODEV;
405
406 if (!tty->open_count)
407 return -EINVAL;
408
409 return get_control_lines(tty);
410}
411
412static int
413ipw_tiocmset(struct tty_struct *linux_tty, struct file *file,
414 unsigned int set, unsigned int clear)
415{
416 struct ipw_tty *tty = linux_tty->driver_data;
417 /* FIXME: Exactly how is the tty object locked here .. */
418
419 if (!tty)
420 return -ENODEV;
421
422 if (!tty->open_count)
423 return -EINVAL;
424
425 return set_control_lines(tty, set, clear);
426}
427
428static int ipw_ioctl(struct tty_struct *linux_tty, struct file *file,
429 unsigned int cmd, unsigned long arg)
430{
431 struct ipw_tty *tty = linux_tty->driver_data;
432
433 if (!tty)
434 return -ENODEV;
435
436 if (!tty->open_count)
437 return -EINVAL;
438
439 /* FIXME: Exactly how is the tty object locked here .. */
440
441 switch (cmd) {
442 case TIOCGSERIAL:
443 return ipwireless_get_serial_info(tty, (void __user *) arg);
444
445 case TIOCSSERIAL:
446 return 0; /* Keeps the PCMCIA scripts happy. */
447 }
448
449 if (tty->tty_type == TTYTYPE_MODEM) {
450 switch (cmd) {
451 case PPPIOCGCHAN:
452 {
453 int chan = ipwireless_ppp_channel_index(
454 tty->network);
455
456 if (chan < 0)
457 return -ENODEV;
458 if (put_user(chan, (int __user *) arg))
459 return -EFAULT;
460 }
461 return 0;
462
463 case PPPIOCGUNIT:
464 {
465 int unit = ipwireless_ppp_unit_number(
466 tty->network);
467
468 if (unit < 0)
469 return -ENODEV;
470 if (put_user(unit, (int __user *) arg))
471 return -EFAULT;
472 }
473 return 0;
474
475 case FIONREAD:
476 {
477 int val = 0;
478
479 if (put_user(val, (int __user *) arg))
480 return -EFAULT;
481 }
482 return 0;
483 case TCFLSH:
484 return tty_perform_flush(linux_tty, arg);
485 }
486 }
487 return tty_mode_ioctl(linux_tty, file, cmd , arg);
488}
489
490static int add_tty(int j,
491 struct ipw_hardware *hardware,
492 struct ipw_network *network, int channel_idx,
493 int secondary_channel_idx, int tty_type)
494{
495 ttys[j] = kzalloc(sizeof(struct ipw_tty), GFP_KERNEL);
496 if (!ttys[j])
497 return -ENOMEM;
498 ttys[j]->index = j;
499 ttys[j]->hardware = hardware;
500 ttys[j]->channel_idx = channel_idx;
501 ttys[j]->secondary_channel_idx = secondary_channel_idx;
502 ttys[j]->network = network;
503 ttys[j]->tty_type = tty_type;
504 mutex_init(&ttys[j]->ipw_tty_mutex);
505
506 tty_register_device(ipw_tty_driver, j, NULL);
507 ipwireless_associate_network_tty(network, channel_idx, ttys[j]);
508
509 if (secondary_channel_idx != -1)
510 ipwireless_associate_network_tty(network,
511 secondary_channel_idx,
512 ttys[j]);
513 if (get_tty(j + ipw_tty_driver->minor_start) == ttys[j])
514 report_registering(ttys[j]);
515 return 0;
516}
517
518struct ipw_tty *ipwireless_tty_create(struct ipw_hardware *hardware,
519 struct ipw_network *network)
520{
521 int i, j;
522
523 for (i = 0; i < IPWIRELESS_PCMCIA_MINOR_RANGE; i++) {
524 int allfree = 1;
525
526 for (j = i; j < IPWIRELESS_PCMCIA_MINORS;
527 j += IPWIRELESS_PCMCIA_MINOR_RANGE)
528 if (ttys[j] != NULL) {
529 allfree = 0;
530 break;
531 }
532
533 if (allfree) {
534 j = i;
535
536 if (add_tty(j, hardware, network,
537 IPW_CHANNEL_DIALLER, IPW_CHANNEL_RAS,
538 TTYTYPE_MODEM))
539 return NULL;
540
541 j += IPWIRELESS_PCMCIA_MINOR_RANGE;
542 if (add_tty(j, hardware, network,
543 IPW_CHANNEL_DIALLER, -1,
544 TTYTYPE_MONITOR))
545 return NULL;
546
547 j += IPWIRELESS_PCMCIA_MINOR_RANGE;
548 if (add_tty(j, hardware, network,
549 IPW_CHANNEL_RAS, -1,
550 TTYTYPE_RAS_RAW))
551 return NULL;
552
553 return ttys[i];
554 }
555 }
556 return NULL;
557}
558
559/*
560 * Must be called before ipwireless_network_free().
561 */
562void ipwireless_tty_free(struct ipw_tty *tty)
563{
564 int j;
565 struct ipw_network *network = ttys[tty->index]->network;
566
567 for (j = tty->index; j < IPWIRELESS_PCMCIA_MINORS;
568 j += IPWIRELESS_PCMCIA_MINOR_RANGE) {
569 struct ipw_tty *ttyj = ttys[j];
570
571 if (ttyj) {
572 mutex_lock(&ttyj->ipw_tty_mutex);
573 if (get_tty(j + ipw_tty_driver->minor_start) == ttyj)
574 report_deregistering(ttyj);
575 ttyj->closing = 1;
576 if (ttyj->linux_tty != NULL) {
577 mutex_unlock(&ttyj->ipw_tty_mutex);
578 tty_hangup(ttyj->linux_tty);
579 /* Wait till the tty_hangup has completed */
580 flush_scheduled_work();
581 /* FIXME: Exactly how is the tty object locked here
582 against a parallel ioctl etc */
583 mutex_lock(&ttyj->ipw_tty_mutex);
584 }
585 while (ttyj->open_count)
586 do_ipw_close(ttyj);
587 ipwireless_disassociate_network_ttys(network,
588 ttyj->channel_idx);
589 tty_unregister_device(ipw_tty_driver, j);
590 ttys[j] = NULL;
591 mutex_unlock(&ttyj->ipw_tty_mutex);
592 kfree(ttyj);
593 }
594 }
595}
596
597static const struct tty_operations tty_ops = {
598 .open = ipw_open,
599 .close = ipw_close,
600 .hangup = ipw_hangup,
601 .write = ipw_write,
602 .write_room = ipw_write_room,
603 .ioctl = ipw_ioctl,
604 .chars_in_buffer = ipw_chars_in_buffer,
605 .tiocmget = ipw_tiocmget,
606 .tiocmset = ipw_tiocmset,
607};
608
609int ipwireless_tty_init(void)
610{
611 int result;
612
613 ipw_tty_driver = alloc_tty_driver(IPWIRELESS_PCMCIA_MINORS);
614 if (!ipw_tty_driver)
615 return -ENOMEM;
616
617 ipw_tty_driver->owner = THIS_MODULE;
618 ipw_tty_driver->driver_name = IPWIRELESS_PCCARD_NAME;
619 ipw_tty_driver->name = "ttyIPWp";
620 ipw_tty_driver->major = 0;
621 ipw_tty_driver->minor_start = IPWIRELESS_PCMCIA_START;
622 ipw_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
623 ipw_tty_driver->subtype = SERIAL_TYPE_NORMAL;
624 ipw_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
625 ipw_tty_driver->init_termios = tty_std_termios;
626 ipw_tty_driver->init_termios.c_cflag =
627 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
628 ipw_tty_driver->init_termios.c_ispeed = 9600;
629 ipw_tty_driver->init_termios.c_ospeed = 9600;
630 tty_set_operations(ipw_tty_driver, &tty_ops);
631 result = tty_register_driver(ipw_tty_driver);
632 if (result) {
633 printk(KERN_ERR IPWIRELESS_PCCARD_NAME
634 ": failed to register tty driver\n");
635 put_tty_driver(ipw_tty_driver);
636 return result;
637 }
638
639 return 0;
640}
641
642void ipwireless_tty_release(void)
643{
644 int ret;
645
646 ret = tty_unregister_driver(ipw_tty_driver);
647 put_tty_driver(ipw_tty_driver);
648 if (ret != 0)
649 printk(KERN_ERR IPWIRELESS_PCCARD_NAME
650 ": tty_unregister_driver failed with code %d\n", ret);
651}
652
653int ipwireless_tty_is_modem(struct ipw_tty *tty)
654{
655 return tty->tty_type == TTYTYPE_MODEM;
656}
657
658void
659ipwireless_tty_notify_control_line_change(struct ipw_tty *tty,
660 unsigned int channel_idx,
661 unsigned int control_lines,
662 unsigned int changed_mask)
663{
664 unsigned int old_control_lines = tty->control_lines;
665
666 tty->control_lines = (tty->control_lines & ~changed_mask)
667 | (control_lines & changed_mask);
668
669 /*
670 * If DCD is de-asserted, we close the tty so pppd can tell that we
671 * have gone offline.
672 */
673 if ((old_control_lines & IPW_CONTROL_LINE_DCD)
674 && !(tty->control_lines & IPW_CONTROL_LINE_DCD)
675 && tty->linux_tty) {
676 tty_hangup(tty->linux_tty);
677 }
678}
679
diff --git a/drivers/char/pcmcia/ipwireless/tty.h b/drivers/char/pcmcia/ipwireless/tty.h
deleted file mode 100644
index 3e163d4cab15..000000000000
--- a/drivers/char/pcmcia/ipwireless/tty.h
+++ /dev/null
@@ -1,46 +0,0 @@
1/*
2 * IPWireless 3G PCMCIA Network Driver
3 *
4 * Original code
5 * by Stephen Blackheath <stephen@blacksapphire.com>,
6 * Ben Martel <benm@symmetric.co.nz>
7 *
8 * Copyrighted as follows:
9 * Copyright (C) 2004 by Symmetric Systems Ltd (NZ)
10 *
11 * Various driver changes and rewrites, port to new kernels
12 * Copyright (C) 2006-2007 Jiri Kosina
13 *
14 * Misc code cleanups and updates
15 * Copyright (C) 2007 David Sterba
16 */
17
18#ifndef _IPWIRELESS_CS_TTY_H_
19#define _IPWIRELESS_CS_TTY_H_
20
21#include <linux/types.h>
22#include <linux/sched.h>
23
24#include <pcmcia/cs.h>
25#include <pcmcia/cistpl.h>
26#include <pcmcia/ds.h>
27
28struct ipw_tty;
29struct ipw_network;
30struct ipw_hardware;
31
32int ipwireless_tty_init(void);
33void ipwireless_tty_release(void);
34
35struct ipw_tty *ipwireless_tty_create(struct ipw_hardware *hw,
36 struct ipw_network *net);
37void ipwireless_tty_free(struct ipw_tty *tty);
38void ipwireless_tty_received(struct ipw_tty *tty, unsigned char *data,
39 unsigned int length);
40int ipwireless_tty_is_modem(struct ipw_tty *tty);
41void ipwireless_tty_notify_control_line_change(struct ipw_tty *tty,
42 unsigned int channel_idx,
43 unsigned int control_lines,
44 unsigned int changed_mask);
45
46#endif
diff --git a/drivers/char/pcmcia/synclink_cs.c b/drivers/char/pcmcia/synclink_cs.c
index 9ecd6bef5d3b..15781396af25 100644
--- a/drivers/char/pcmcia/synclink_cs.c
+++ b/drivers/char/pcmcia/synclink_cs.c
@@ -70,7 +70,6 @@
70#include <linux/workqueue.h> 70#include <linux/workqueue.h>
71#include <linux/hdlc.h> 71#include <linux/hdlc.h>
72 72
73#include <pcmcia/cs.h>
74#include <pcmcia/cistpl.h> 73#include <pcmcia/cistpl.h>
75#include <pcmcia/cisreg.h> 74#include <pcmcia/cisreg.h>
76#include <pcmcia/ds.h> 75#include <pcmcia/ds.h>
@@ -419,9 +418,9 @@ static void bh_status(MGSLPC_INFO *info);
419/* 418/*
420 * ioctl handlers 419 * ioctl handlers
421 */ 420 */
422static int tiocmget(struct tty_struct *tty, struct file *file); 421static int tiocmget(struct tty_struct *tty);
423static int tiocmset(struct tty_struct *tty, struct file *file, 422static int tiocmset(struct tty_struct *tty,
424 unsigned int set, unsigned int clear); 423 unsigned int set, unsigned int clear);
425static int get_stats(MGSLPC_INFO *info, struct mgsl_icount __user *user_icount); 424static int get_stats(MGSLPC_INFO *info, struct mgsl_icount __user *user_icount);
426static int get_params(MGSLPC_INFO *info, MGSL_PARAMS __user *user_params); 425static int get_params(MGSLPC_INFO *info, MGSL_PARAMS __user *user_params);
427static int set_params(MGSLPC_INFO *info, MGSL_PARAMS __user *new_params, struct tty_struct *tty); 426static int set_params(MGSLPC_INFO *info, MGSL_PARAMS __user *new_params, struct tty_struct *tty);
@@ -550,9 +549,6 @@ static int mgslpc_probe(struct pcmcia_device *link)
550 549
551 /* Initialize the struct pcmcia_device structure */ 550 /* Initialize the struct pcmcia_device structure */
552 551
553 link->conf.Attributes = 0;
554 link->conf.IntType = INT_MEMORY_AND_IO;
555
556 ret = mgslpc_config(link); 552 ret = mgslpc_config(link);
557 if (ret) 553 if (ret)
558 return ret; 554 return ret;
@@ -565,20 +561,8 @@ static int mgslpc_probe(struct pcmcia_device *link)
565/* Card has been inserted. 561/* Card has been inserted.
566 */ 562 */
567 563
568static int mgslpc_ioprobe(struct pcmcia_device *p_dev, 564static int mgslpc_ioprobe(struct pcmcia_device *p_dev, void *priv_data)
569 cistpl_cftable_entry_t *cfg,
570 cistpl_cftable_entry_t *dflt,
571 unsigned int vcc,
572 void *priv_data)
573{ 565{
574 if (!cfg->io.nwin)
575 return -ENODEV;
576
577 p_dev->resource[0]->start = cfg->io.win[0].base;
578 p_dev->resource[0]->end = cfg->io.win[0].len;
579 p_dev->resource[0]->flags |= pcmcia_io_cfg_data_width(cfg->io.flags);
580 p_dev->io_lines = cfg->io.flags & CISTPL_IO_LINES_MASK;
581
582 return pcmcia_request_io(p_dev); 566 return pcmcia_request_io(p_dev);
583} 567}
584 568
@@ -590,32 +574,24 @@ static int mgslpc_config(struct pcmcia_device *link)
590 if (debug_level >= DEBUG_LEVEL_INFO) 574 if (debug_level >= DEBUG_LEVEL_INFO)
591 printk("mgslpc_config(0x%p)\n", link); 575 printk("mgslpc_config(0x%p)\n", link);
592 576
577 link->config_flags |= CONF_ENABLE_IRQ | CONF_AUTO_SET_IO;
578
593 ret = pcmcia_loop_config(link, mgslpc_ioprobe, NULL); 579 ret = pcmcia_loop_config(link, mgslpc_ioprobe, NULL);
594 if (ret != 0) 580 if (ret != 0)
595 goto failed; 581 goto failed;
596 582
597 link->conf.Attributes = CONF_ENABLE_IRQ; 583 link->config_index = 8;
598 link->conf.IntType = INT_MEMORY_AND_IO; 584 link->config_regs = PRESENT_OPTION;
599 link->conf.ConfigIndex = 8;
600 link->conf.Present = PRESENT_OPTION;
601 585
602 ret = pcmcia_request_irq(link, mgslpc_isr); 586 ret = pcmcia_request_irq(link, mgslpc_isr);
603 if (ret) 587 if (ret)
604 goto failed; 588 goto failed;
605 ret = pcmcia_request_configuration(link, &link->conf); 589 ret = pcmcia_enable_device(link);
606 if (ret) 590 if (ret)
607 goto failed; 591 goto failed;
608 592
609 info->io_base = link->resource[0]->start; 593 info->io_base = link->resource[0]->start;
610 info->irq_level = link->irq; 594 info->irq_level = link->irq;
611
612 dev_info(&link->dev, "index 0x%02x:",
613 link->conf.ConfigIndex);
614 if (link->conf.Attributes & CONF_ENABLE_IRQ)
615 printk(", irq %d", link->irq);
616 if (link->resource[0])
617 printk(", io %pR", link->resource[0]);
618 printk("\n");
619 return 0; 595 return 0;
620 596
621failed: 597failed:
@@ -1314,7 +1290,7 @@ static int startup(MGSLPC_INFO * info, struct tty_struct *tty)
1314 /* Allocate and claim adapter resources */ 1290 /* Allocate and claim adapter resources */
1315 retval = claim_resources(info); 1291 retval = claim_resources(info);
1316 1292
1317 /* perform existance check and diagnostics */ 1293 /* perform existence check and diagnostics */
1318 if ( !retval ) 1294 if ( !retval )
1319 retval = adapter_test(info); 1295 retval = adapter_test(info);
1320 1296
@@ -2138,7 +2114,7 @@ static int modem_input_wait(MGSLPC_INFO *info,int arg)
2138 2114
2139/* return the state of the serial control and status signals 2115/* return the state of the serial control and status signals
2140 */ 2116 */
2141static int tiocmget(struct tty_struct *tty, struct file *file) 2117static int tiocmget(struct tty_struct *tty)
2142{ 2118{
2143 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data; 2119 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
2144 unsigned int result; 2120 unsigned int result;
@@ -2163,7 +2139,7 @@ static int tiocmget(struct tty_struct *tty, struct file *file)
2163 2139
2164/* set modem control signals (DTR/RTS) 2140/* set modem control signals (DTR/RTS)
2165 */ 2141 */
2166static int tiocmset(struct tty_struct *tty, struct file *file, 2142static int tiocmset(struct tty_struct *tty,
2167 unsigned int set, unsigned int clear) 2143 unsigned int set, unsigned int clear)
2168{ 2144{
2169 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data; 2145 MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
@@ -2215,26 +2191,47 @@ static int mgslpc_break(struct tty_struct *tty, int break_state)
2215 return 0; 2191 return 0;
2216} 2192}
2217 2193
2194static int mgslpc_get_icount(struct tty_struct *tty,
2195 struct serial_icounter_struct *icount)
2196{
2197 MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data;
2198 struct mgsl_icount cnow; /* kernel counter temps */
2199 unsigned long flags;
2200
2201 spin_lock_irqsave(&info->lock,flags);
2202 cnow = info->icount;
2203 spin_unlock_irqrestore(&info->lock,flags);
2204
2205 icount->cts = cnow.cts;
2206 icount->dsr = cnow.dsr;
2207 icount->rng = cnow.rng;
2208 icount->dcd = cnow.dcd;
2209 icount->rx = cnow.rx;
2210 icount->tx = cnow.tx;
2211 icount->frame = cnow.frame;
2212 icount->overrun = cnow.overrun;
2213 icount->parity = cnow.parity;
2214 icount->brk = cnow.brk;
2215 icount->buf_overrun = cnow.buf_overrun;
2216
2217 return 0;
2218}
2219
2218/* Service an IOCTL request 2220/* Service an IOCTL request
2219 * 2221 *
2220 * Arguments: 2222 * Arguments:
2221 * 2223 *
2222 * tty pointer to tty instance data 2224 * tty pointer to tty instance data
2223 * file pointer to associated file object for device
2224 * cmd IOCTL command code 2225 * cmd IOCTL command code
2225 * arg command argument/context 2226 * arg command argument/context
2226 * 2227 *
2227 * Return Value: 0 if success, otherwise error code 2228 * Return Value: 0 if success, otherwise error code
2228 */ 2229 */
2229static int mgslpc_ioctl(struct tty_struct *tty, struct file * file, 2230static int mgslpc_ioctl(struct tty_struct *tty,
2230 unsigned int cmd, unsigned long arg) 2231 unsigned int cmd, unsigned long arg)
2231{ 2232{
2232 MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data; 2233 MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data;
2233 int error;
2234 struct mgsl_icount cnow; /* kernel counter temps */
2235 struct serial_icounter_struct __user *p_cuser; /* user space */
2236 void __user *argp = (void __user *)arg; 2234 void __user *argp = (void __user *)arg;
2237 unsigned long flags;
2238 2235
2239 if (debug_level >= DEBUG_LEVEL_INFO) 2236 if (debug_level >= DEBUG_LEVEL_INFO)
2240 printk("%s(%d):mgslpc_ioctl %s cmd=%08X\n", __FILE__,__LINE__, 2237 printk("%s(%d):mgslpc_ioctl %s cmd=%08X\n", __FILE__,__LINE__,
@@ -2244,7 +2241,7 @@ static int mgslpc_ioctl(struct tty_struct *tty, struct file * file,
2244 return -ENODEV; 2241 return -ENODEV;
2245 2242
2246 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) && 2243 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
2247 (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) { 2244 (cmd != TIOCMIWAIT)) {
2248 if (tty->flags & (1 << TTY_IO_ERROR)) 2245 if (tty->flags & (1 << TTY_IO_ERROR))
2249 return -EIO; 2246 return -EIO;
2250 } 2247 }
@@ -2274,34 +2271,6 @@ static int mgslpc_ioctl(struct tty_struct *tty, struct file * file,
2274 return wait_events(info, argp); 2271 return wait_events(info, argp);
2275 case TIOCMIWAIT: 2272 case TIOCMIWAIT:
2276 return modem_input_wait(info,(int)arg); 2273 return modem_input_wait(info,(int)arg);
2277 case TIOCGICOUNT:
2278 spin_lock_irqsave(&info->lock,flags);
2279 cnow = info->icount;
2280 spin_unlock_irqrestore(&info->lock,flags);
2281 p_cuser = argp;
2282 PUT_USER(error,cnow.cts, &p_cuser->cts);
2283 if (error) return error;
2284 PUT_USER(error,cnow.dsr, &p_cuser->dsr);
2285 if (error) return error;
2286 PUT_USER(error,cnow.rng, &p_cuser->rng);
2287 if (error) return error;
2288 PUT_USER(error,cnow.dcd, &p_cuser->dcd);
2289 if (error) return error;
2290 PUT_USER(error,cnow.rx, &p_cuser->rx);
2291 if (error) return error;
2292 PUT_USER(error,cnow.tx, &p_cuser->tx);
2293 if (error) return error;
2294 PUT_USER(error,cnow.frame, &p_cuser->frame);
2295 if (error) return error;
2296 PUT_USER(error,cnow.overrun, &p_cuser->overrun);
2297 if (error) return error;
2298 PUT_USER(error,cnow.parity, &p_cuser->parity);
2299 if (error) return error;
2300 PUT_USER(error,cnow.brk, &p_cuser->brk);
2301 if (error) return error;
2302 PUT_USER(error,cnow.buf_overrun, &p_cuser->buf_overrun);
2303 if (error) return error;
2304 return 0;
2305 default: 2274 default:
2306 return -ENOIOCTLCMD; 2275 return -ENOIOCTLCMD;
2307 } 2276 }
@@ -2711,7 +2680,7 @@ static void rx_free_buffers(MGSLPC_INFO *info)
2711static int claim_resources(MGSLPC_INFO *info) 2680static int claim_resources(MGSLPC_INFO *info)
2712{ 2681{
2713 if (rx_alloc_buffers(info) < 0 ) { 2682 if (rx_alloc_buffers(info) < 0 ) {
2714 printk( "Cant allocate rx buffer %s\n", info->device_name); 2683 printk( "Can't allocate rx buffer %s\n", info->device_name);
2715 release_resources(info); 2684 release_resources(info);
2716 return -ENODEV; 2685 return -ENODEV;
2717 } 2686 }
@@ -2789,7 +2758,7 @@ static void mgslpc_remove_device(MGSLPC_INFO *remove_info)
2789 } 2758 }
2790} 2759}
2791 2760
2792static struct pcmcia_device_id mgslpc_ids[] = { 2761static const struct pcmcia_device_id mgslpc_ids[] = {
2793 PCMCIA_DEVICE_MANF_CARD(0x02c5, 0x0050), 2762 PCMCIA_DEVICE_MANF_CARD(0x02c5, 0x0050),
2794 PCMCIA_DEVICE_NULL 2763 PCMCIA_DEVICE_NULL
2795}; 2764};
@@ -2797,9 +2766,7 @@ MODULE_DEVICE_TABLE(pcmcia, mgslpc_ids);
2797 2766
2798static struct pcmcia_driver mgslpc_driver = { 2767static struct pcmcia_driver mgslpc_driver = {
2799 .owner = THIS_MODULE, 2768 .owner = THIS_MODULE,
2800 .drv = { 2769 .name = "synclink_cs",
2801 .name = "synclink_cs",
2802 },
2803 .probe = mgslpc_probe, 2770 .probe = mgslpc_probe,
2804 .remove = mgslpc_detach, 2771 .remove = mgslpc_detach,
2805 .id_table = mgslpc_ids, 2772 .id_table = mgslpc_ids,
@@ -2828,6 +2795,7 @@ static const struct tty_operations mgslpc_ops = {
2828 .hangup = mgslpc_hangup, 2795 .hangup = mgslpc_hangup,
2829 .tiocmget = tiocmget, 2796 .tiocmget = tiocmget,
2830 .tiocmset = tiocmset, 2797 .tiocmset = tiocmset,
2798 .get_icount = mgslpc_get_icount,
2831 .proc_fops = &mgslpc_proc_fops, 2799 .proc_fops = &mgslpc_proc_fops,
2832}; 2800};
2833 2801
@@ -2835,8 +2803,6 @@ static void synclink_cs_cleanup(void)
2835{ 2803{
2836 int rc; 2804 int rc;
2837 2805
2838 printk("Unloading %s: version %s\n", driver_name, driver_version);
2839
2840 while(mgslpc_device_list) 2806 while(mgslpc_device_list)
2841 mgslpc_remove_device(mgslpc_device_list); 2807 mgslpc_remove_device(mgslpc_device_list);
2842 2808
@@ -2859,8 +2825,6 @@ static int __init synclink_cs_init(void)
2859 BREAKPOINT(); 2825 BREAKPOINT();
2860 } 2826 }
2861 2827
2862 printk("%s %s\n", driver_name, driver_version);
2863
2864 if ((rc = pcmcia_register_driver(&mgslpc_driver)) < 0) 2828 if ((rc = pcmcia_register_driver(&mgslpc_driver)) < 0)
2865 return rc; 2829 return rc;
2866 2830
@@ -4127,6 +4091,8 @@ static int hdlcdev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
4127 if (cmd != SIOCWANDEV) 4091 if (cmd != SIOCWANDEV)
4128 return hdlc_ioctl(dev, ifr, cmd); 4092 return hdlc_ioctl(dev, ifr, cmd);
4129 4093
4094 memset(&new_line, 0, size);
4095
4130 switch(ifr->ifr_settings.type) { 4096 switch(ifr->ifr_settings.type) {
4131 case IF_GET_IFACE: /* return current sync_serial_settings */ 4097 case IF_GET_IFACE: /* return current sync_serial_settings */
4132 4098
diff --git a/drivers/char/ppdev.c b/drivers/char/ppdev.c
index 02abfddce45a..3fcf80ff12f2 100644
--- a/drivers/char/ppdev.c
+++ b/drivers/char/ppdev.c
@@ -67,7 +67,7 @@
67#include <linux/slab.h> 67#include <linux/slab.h>
68#include <linux/major.h> 68#include <linux/major.h>
69#include <linux/ppdev.h> 69#include <linux/ppdev.h>
70#include <linux/smp_lock.h> 70#include <linux/mutex.h>
71#include <linux/uaccess.h> 71#include <linux/uaccess.h>
72 72
73#define PP_VERSION "ppdev: user-space parallel port driver" 73#define PP_VERSION "ppdev: user-space parallel port driver"
@@ -97,6 +97,7 @@ struct pp_struct {
97/* ROUND_UP macro from fs/select.c */ 97/* ROUND_UP macro from fs/select.c */
98#define ROUND_UP(x,y) (((x)+(y)-1)/(y)) 98#define ROUND_UP(x,y) (((x)+(y)-1)/(y))
99 99
100static DEFINE_MUTEX(pp_do_mutex);
100static inline void pp_enable_irq (struct pp_struct *pp) 101static inline void pp_enable_irq (struct pp_struct *pp)
101{ 102{
102 struct parport *port = pp->pdev->port; 103 struct parport *port = pp->pdev->port;
@@ -456,6 +457,7 @@ static int pp_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
456 return -ENODEV; 457 return -ENODEV;
457 458
458 modes = port->modes; 459 modes = port->modes;
460 parport_put_port(port);
459 if (copy_to_user (argp, &modes, sizeof (modes))) { 461 if (copy_to_user (argp, &modes, sizeof (modes))) {
460 return -EFAULT; 462 return -EFAULT;
461 } 463 }
@@ -612,6 +614,7 @@ static int pp_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
612 614
613 case PPGETTIME: 615 case PPGETTIME:
614 to_jiffies = pp->pdev->timeout; 616 to_jiffies = pp->pdev->timeout;
617 memset(&par_timeout, 0, sizeof(par_timeout));
615 par_timeout.tv_sec = to_jiffies / HZ; 618 par_timeout.tv_sec = to_jiffies / HZ;
616 par_timeout.tv_usec = (to_jiffies % (long)HZ) * (1000000/HZ); 619 par_timeout.tv_usec = (to_jiffies % (long)HZ) * (1000000/HZ);
617 if (copy_to_user (argp, &par_timeout, sizeof(struct timeval))) 620 if (copy_to_user (argp, &par_timeout, sizeof(struct timeval)))
@@ -630,9 +633,9 @@ static int pp_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
630static long pp_ioctl(struct file *file, unsigned int cmd, unsigned long arg) 633static long pp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
631{ 634{
632 long ret; 635 long ret;
633 lock_kernel(); 636 mutex_lock(&pp_do_mutex);
634 ret = pp_do_ioctl(file, cmd, arg); 637 ret = pp_do_ioctl(file, cmd, arg);
635 unlock_kernel(); 638 mutex_unlock(&pp_do_mutex);
636 return ret; 639 return ret;
637} 640}
638 641
@@ -641,7 +644,6 @@ static int pp_open (struct inode * inode, struct file * file)
641 unsigned int minor = iminor(inode); 644 unsigned int minor = iminor(inode);
642 struct pp_struct *pp; 645 struct pp_struct *pp;
643 646
644 cycle_kernel_lock();
645 if (minor >= PARPORT_MAX) 647 if (minor >= PARPORT_MAX)
646 return -ENXIO; 648 return -ENXIO;
647 649
diff --git a/drivers/char/pty.c b/drivers/char/pty.c
deleted file mode 100644
index c350d01716bd..000000000000
--- a/drivers/char/pty.c
+++ /dev/null
@@ -1,775 +0,0 @@
1/*
2 * linux/drivers/char/pty.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 *
6 * Added support for a Unix98-style ptmx device.
7 * -- C. Scott Ananian <cananian@alumni.princeton.edu>, 14-Jan-1998
8 *
9 * When reading this code see also fs/devpts. In particular note that the
10 * driver_data field is used by the devpts side as a binding to the devpts
11 * inode.
12 */
13
14#include <linux/module.h>
15
16#include <linux/errno.h>
17#include <linux/interrupt.h>
18#include <linux/tty.h>
19#include <linux/tty_flip.h>
20#include <linux/fcntl.h>
21#include <linux/sched.h>
22#include <linux/string.h>
23#include <linux/major.h>
24#include <linux/mm.h>
25#include <linux/init.h>
26#include <linux/smp_lock.h>
27#include <linux/sysctl.h>
28#include <linux/device.h>
29#include <linux/uaccess.h>
30#include <linux/bitops.h>
31#include <linux/devpts_fs.h>
32#include <linux/slab.h>
33
34#include <asm/system.h>
35
36#ifdef CONFIG_UNIX98_PTYS
37static struct tty_driver *ptm_driver;
38static struct tty_driver *pts_driver;
39#endif
40
41static void pty_close(struct tty_struct *tty, struct file *filp)
42{
43 BUG_ON(!tty);
44 if (tty->driver->subtype == PTY_TYPE_MASTER)
45 WARN_ON(tty->count > 1);
46 else {
47 if (tty->count > 2)
48 return;
49 }
50 wake_up_interruptible(&tty->read_wait);
51 wake_up_interruptible(&tty->write_wait);
52 tty->packet = 0;
53 if (!tty->link)
54 return;
55 tty->link->packet = 0;
56 set_bit(TTY_OTHER_CLOSED, &tty->link->flags);
57 wake_up_interruptible(&tty->link->read_wait);
58 wake_up_interruptible(&tty->link->write_wait);
59 if (tty->driver->subtype == PTY_TYPE_MASTER) {
60 set_bit(TTY_OTHER_CLOSED, &tty->flags);
61#ifdef CONFIG_UNIX98_PTYS
62 if (tty->driver == ptm_driver)
63 devpts_pty_kill(tty->link);
64#endif
65 tty_unlock();
66 tty_vhangup(tty->link);
67 tty_lock();
68 }
69}
70
71/*
72 * The unthrottle routine is called by the line discipline to signal
73 * that it can receive more characters. For PTY's, the TTY_THROTTLED
74 * flag is always set, to force the line discipline to always call the
75 * unthrottle routine when there are fewer than TTY_THRESHOLD_UNTHROTTLE
76 * characters in the queue. This is necessary since each time this
77 * happens, we need to wake up any sleeping processes that could be
78 * (1) trying to send data to the pty, or (2) waiting in wait_until_sent()
79 * for the pty buffer to be drained.
80 */
81static void pty_unthrottle(struct tty_struct *tty)
82{
83 tty_wakeup(tty->link);
84 set_bit(TTY_THROTTLED, &tty->flags);
85}
86
87/**
88 * pty_space - report space left for writing
89 * @to: tty we are writing into
90 *
91 * The tty buffers allow 64K but we sneak a peak and clip at 8K this
92 * allows a lot of overspill room for echo and other fun messes to
93 * be handled properly
94 */
95
96static int pty_space(struct tty_struct *to)
97{
98 int n = 8192 - to->buf.memory_used;
99 if (n < 0)
100 return 0;
101 return n;
102}
103
104/**
105 * pty_write - write to a pty
106 * @tty: the tty we write from
107 * @buf: kernel buffer of data
108 * @count: bytes to write
109 *
110 * Our "hardware" write method. Data is coming from the ldisc which
111 * may be in a non sleeping state. We simply throw this at the other
112 * end of the link as if we were an IRQ handler receiving stuff for
113 * the other side of the pty/tty pair.
114 */
115
116static int pty_write(struct tty_struct *tty, const unsigned char *buf, int c)
117{
118 struct tty_struct *to = tty->link;
119
120 if (tty->stopped)
121 return 0;
122
123 if (c > 0) {
124 /* Stuff the data into the input queue of the other end */
125 c = tty_insert_flip_string(to, buf, c);
126 /* And shovel */
127 if (c) {
128 tty_flip_buffer_push(to);
129 tty_wakeup(tty);
130 }
131 }
132 return c;
133}
134
135/**
136 * pty_write_room - write space
137 * @tty: tty we are writing from
138 *
139 * Report how many bytes the ldisc can send into the queue for
140 * the other device.
141 */
142
143static int pty_write_room(struct tty_struct *tty)
144{
145 if (tty->stopped)
146 return 0;
147 return pty_space(tty->link);
148}
149
150/**
151 * pty_chars_in_buffer - characters currently in our tx queue
152 * @tty: our tty
153 *
154 * Report how much we have in the transmit queue. As everything is
155 * instantly at the other end this is easy to implement.
156 */
157
158static int pty_chars_in_buffer(struct tty_struct *tty)
159{
160 return 0;
161}
162
163/* Set the lock flag on a pty */
164static int pty_set_lock(struct tty_struct *tty, int __user *arg)
165{
166 int val;
167 if (get_user(val, arg))
168 return -EFAULT;
169 if (val)
170 set_bit(TTY_PTY_LOCK, &tty->flags);
171 else
172 clear_bit(TTY_PTY_LOCK, &tty->flags);
173 return 0;
174}
175
176/* Send a signal to the slave */
177static int pty_signal(struct tty_struct *tty, int sig)
178{
179 unsigned long flags;
180 struct pid *pgrp;
181
182 if (tty->link) {
183 spin_lock_irqsave(&tty->link->ctrl_lock, flags);
184 pgrp = get_pid(tty->link->pgrp);
185 spin_unlock_irqrestore(&tty->link->ctrl_lock, flags);
186
187 kill_pgrp(pgrp, sig, 1);
188 put_pid(pgrp);
189 }
190 return 0;
191}
192
193static void pty_flush_buffer(struct tty_struct *tty)
194{
195 struct tty_struct *to = tty->link;
196 unsigned long flags;
197
198 if (!to)
199 return;
200 /* tty_buffer_flush(to); FIXME */
201 if (to->packet) {
202 spin_lock_irqsave(&tty->ctrl_lock, flags);
203 tty->ctrl_status |= TIOCPKT_FLUSHWRITE;
204 wake_up_interruptible(&to->read_wait);
205 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
206 }
207}
208
209static int pty_open(struct tty_struct *tty, struct file *filp)
210{
211 int retval = -ENODEV;
212
213 if (!tty || !tty->link)
214 goto out;
215
216 retval = -EIO;
217 if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
218 goto out;
219 if (test_bit(TTY_PTY_LOCK, &tty->link->flags))
220 goto out;
221 if (tty->link->count != 1)
222 goto out;
223
224 clear_bit(TTY_OTHER_CLOSED, &tty->link->flags);
225 set_bit(TTY_THROTTLED, &tty->flags);
226 retval = 0;
227out:
228 return retval;
229}
230
231static void pty_set_termios(struct tty_struct *tty,
232 struct ktermios *old_termios)
233{
234 tty->termios->c_cflag &= ~(CSIZE | PARENB);
235 tty->termios->c_cflag |= (CS8 | CREAD);
236}
237
238/**
239 * pty_do_resize - resize event
240 * @tty: tty being resized
241 * @ws: window size being set.
242 *
243 * Update the termios variables and send the necessary signals to
244 * peform a terminal resize correctly
245 */
246
247int pty_resize(struct tty_struct *tty, struct winsize *ws)
248{
249 struct pid *pgrp, *rpgrp;
250 unsigned long flags;
251 struct tty_struct *pty = tty->link;
252
253 /* For a PTY we need to lock the tty side */
254 mutex_lock(&tty->termios_mutex);
255 if (!memcmp(ws, &tty->winsize, sizeof(*ws)))
256 goto done;
257
258 /* Get the PID values and reference them so we can
259 avoid holding the tty ctrl lock while sending signals.
260 We need to lock these individually however. */
261
262 spin_lock_irqsave(&tty->ctrl_lock, flags);
263 pgrp = get_pid(tty->pgrp);
264 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
265
266 spin_lock_irqsave(&pty->ctrl_lock, flags);
267 rpgrp = get_pid(pty->pgrp);
268 spin_unlock_irqrestore(&pty->ctrl_lock, flags);
269
270 if (pgrp)
271 kill_pgrp(pgrp, SIGWINCH, 1);
272 if (rpgrp != pgrp && rpgrp)
273 kill_pgrp(rpgrp, SIGWINCH, 1);
274
275 put_pid(pgrp);
276 put_pid(rpgrp);
277
278 tty->winsize = *ws;
279 pty->winsize = *ws; /* Never used so will go away soon */
280done:
281 mutex_unlock(&tty->termios_mutex);
282 return 0;
283}
284
285/* Traditional BSD devices */
286#ifdef CONFIG_LEGACY_PTYS
287
288static int pty_install(struct tty_driver *driver, struct tty_struct *tty)
289{
290 struct tty_struct *o_tty;
291 int idx = tty->index;
292 int retval;
293
294 o_tty = alloc_tty_struct();
295 if (!o_tty)
296 return -ENOMEM;
297 if (!try_module_get(driver->other->owner)) {
298 /* This cannot in fact currently happen */
299 free_tty_struct(o_tty);
300 return -ENOMEM;
301 }
302 initialize_tty_struct(o_tty, driver->other, idx);
303
304 /* We always use new tty termios data so we can do this
305 the easy way .. */
306 retval = tty_init_termios(tty);
307 if (retval)
308 goto free_mem_out;
309
310 retval = tty_init_termios(o_tty);
311 if (retval) {
312 tty_free_termios(tty);
313 goto free_mem_out;
314 }
315
316 /*
317 * Everything allocated ... set up the o_tty structure.
318 */
319 driver->other->ttys[idx] = o_tty;
320 tty_driver_kref_get(driver->other);
321 if (driver->subtype == PTY_TYPE_MASTER)
322 o_tty->count++;
323 /* Establish the links in both directions */
324 tty->link = o_tty;
325 o_tty->link = tty;
326
327 tty_driver_kref_get(driver);
328 tty->count++;
329 driver->ttys[idx] = tty;
330 return 0;
331free_mem_out:
332 module_put(o_tty->driver->owner);
333 free_tty_struct(o_tty);
334 return -ENOMEM;
335}
336
337static int pty_bsd_ioctl(struct tty_struct *tty, struct file *file,
338 unsigned int cmd, unsigned long arg)
339{
340 switch (cmd) {
341 case TIOCSPTLCK: /* Set PT Lock (disallow slave open) */
342 return pty_set_lock(tty, (int __user *) arg);
343 case TIOCSIG: /* Send signal to other side of pty */
344 return pty_signal(tty, (int) arg);
345 }
346 return -ENOIOCTLCMD;
347}
348
349static int legacy_count = CONFIG_LEGACY_PTY_COUNT;
350module_param(legacy_count, int, 0);
351
352/*
353 * The master side of a pty can do TIOCSPTLCK and thus
354 * has pty_bsd_ioctl.
355 */
356static const struct tty_operations master_pty_ops_bsd = {
357 .install = pty_install,
358 .open = pty_open,
359 .close = pty_close,
360 .write = pty_write,
361 .write_room = pty_write_room,
362 .flush_buffer = pty_flush_buffer,
363 .chars_in_buffer = pty_chars_in_buffer,
364 .unthrottle = pty_unthrottle,
365 .set_termios = pty_set_termios,
366 .ioctl = pty_bsd_ioctl,
367 .resize = pty_resize
368};
369
370static const struct tty_operations slave_pty_ops_bsd = {
371 .install = pty_install,
372 .open = pty_open,
373 .close = pty_close,
374 .write = pty_write,
375 .write_room = pty_write_room,
376 .flush_buffer = pty_flush_buffer,
377 .chars_in_buffer = pty_chars_in_buffer,
378 .unthrottle = pty_unthrottle,
379 .set_termios = pty_set_termios,
380 .resize = pty_resize
381};
382
383static void __init legacy_pty_init(void)
384{
385 struct tty_driver *pty_driver, *pty_slave_driver;
386
387 if (legacy_count <= 0)
388 return;
389
390 pty_driver = alloc_tty_driver(legacy_count);
391 if (!pty_driver)
392 panic("Couldn't allocate pty driver");
393
394 pty_slave_driver = alloc_tty_driver(legacy_count);
395 if (!pty_slave_driver)
396 panic("Couldn't allocate pty slave driver");
397
398 pty_driver->owner = THIS_MODULE;
399 pty_driver->driver_name = "pty_master";
400 pty_driver->name = "pty";
401 pty_driver->major = PTY_MASTER_MAJOR;
402 pty_driver->minor_start = 0;
403 pty_driver->type = TTY_DRIVER_TYPE_PTY;
404 pty_driver->subtype = PTY_TYPE_MASTER;
405 pty_driver->init_termios = tty_std_termios;
406 pty_driver->init_termios.c_iflag = 0;
407 pty_driver->init_termios.c_oflag = 0;
408 pty_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
409 pty_driver->init_termios.c_lflag = 0;
410 pty_driver->init_termios.c_ispeed = 38400;
411 pty_driver->init_termios.c_ospeed = 38400;
412 pty_driver->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW;
413 pty_driver->other = pty_slave_driver;
414 tty_set_operations(pty_driver, &master_pty_ops_bsd);
415
416 pty_slave_driver->owner = THIS_MODULE;
417 pty_slave_driver->driver_name = "pty_slave";
418 pty_slave_driver->name = "ttyp";
419 pty_slave_driver->major = PTY_SLAVE_MAJOR;
420 pty_slave_driver->minor_start = 0;
421 pty_slave_driver->type = TTY_DRIVER_TYPE_PTY;
422 pty_slave_driver->subtype = PTY_TYPE_SLAVE;
423 pty_slave_driver->init_termios = tty_std_termios;
424 pty_slave_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
425 pty_slave_driver->init_termios.c_ispeed = 38400;
426 pty_slave_driver->init_termios.c_ospeed = 38400;
427 pty_slave_driver->flags = TTY_DRIVER_RESET_TERMIOS |
428 TTY_DRIVER_REAL_RAW;
429 pty_slave_driver->other = pty_driver;
430 tty_set_operations(pty_slave_driver, &slave_pty_ops_bsd);
431
432 if (tty_register_driver(pty_driver))
433 panic("Couldn't register pty driver");
434 if (tty_register_driver(pty_slave_driver))
435 panic("Couldn't register pty slave driver");
436}
437#else
438static inline void legacy_pty_init(void) { }
439#endif
440
441/* Unix98 devices */
442#ifdef CONFIG_UNIX98_PTYS
443/*
444 * sysctl support for setting limits on the number of Unix98 ptys allocated.
445 * Otherwise one can eat up all kernel memory by opening /dev/ptmx repeatedly.
446 */
447int pty_limit = NR_UNIX98_PTY_DEFAULT;
448static int pty_limit_min;
449static int pty_limit_max = NR_UNIX98_PTY_MAX;
450static int pty_count;
451
452static struct cdev ptmx_cdev;
453
454static struct ctl_table pty_table[] = {
455 {
456 .procname = "max",
457 .maxlen = sizeof(int),
458 .mode = 0644,
459 .data = &pty_limit,
460 .proc_handler = proc_dointvec_minmax,
461 .extra1 = &pty_limit_min,
462 .extra2 = &pty_limit_max,
463 }, {
464 .procname = "nr",
465 .maxlen = sizeof(int),
466 .mode = 0444,
467 .data = &pty_count,
468 .proc_handler = proc_dointvec,
469 },
470 {}
471};
472
473static struct ctl_table pty_kern_table[] = {
474 {
475 .procname = "pty",
476 .mode = 0555,
477 .child = pty_table,
478 },
479 {}
480};
481
482static struct ctl_table pty_root_table[] = {
483 {
484 .procname = "kernel",
485 .mode = 0555,
486 .child = pty_kern_table,
487 },
488 {}
489};
490
491
492static int pty_unix98_ioctl(struct tty_struct *tty, struct file *file,
493 unsigned int cmd, unsigned long arg)
494{
495 switch (cmd) {
496 case TIOCSPTLCK: /* Set PT Lock (disallow slave open) */
497 return pty_set_lock(tty, (int __user *)arg);
498 case TIOCGPTN: /* Get PT Number */
499 return put_user(tty->index, (unsigned int __user *)arg);
500 case TIOCSIG: /* Send signal to other side of pty */
501 return pty_signal(tty, (int) arg);
502 }
503
504 return -ENOIOCTLCMD;
505}
506
507/**
508 * ptm_unix98_lookup - find a pty master
509 * @driver: ptm driver
510 * @idx: tty index
511 *
512 * Look up a pty master device. Called under the tty_mutex for now.
513 * This provides our locking.
514 */
515
516static struct tty_struct *ptm_unix98_lookup(struct tty_driver *driver,
517 struct inode *ptm_inode, int idx)
518{
519 struct tty_struct *tty = devpts_get_tty(ptm_inode, idx);
520 if (tty)
521 tty = tty->link;
522 return tty;
523}
524
525/**
526 * pts_unix98_lookup - find a pty slave
527 * @driver: pts driver
528 * @idx: tty index
529 *
530 * Look up a pty master device. Called under the tty_mutex for now.
531 * This provides our locking.
532 */
533
534static struct tty_struct *pts_unix98_lookup(struct tty_driver *driver,
535 struct inode *pts_inode, int idx)
536{
537 struct tty_struct *tty = devpts_get_tty(pts_inode, idx);
538 /* Master must be open before slave */
539 if (!tty)
540 return ERR_PTR(-EIO);
541 return tty;
542}
543
544static void pty_unix98_shutdown(struct tty_struct *tty)
545{
546 /* We have our own method as we don't use the tty index */
547 kfree(tty->termios);
548}
549
550/* We have no need to install and remove our tty objects as devpts does all
551 the work for us */
552
553static int pty_unix98_install(struct tty_driver *driver, struct tty_struct *tty)
554{
555 struct tty_struct *o_tty;
556 int idx = tty->index;
557
558 o_tty = alloc_tty_struct();
559 if (!o_tty)
560 return -ENOMEM;
561 if (!try_module_get(driver->other->owner)) {
562 /* This cannot in fact currently happen */
563 free_tty_struct(o_tty);
564 return -ENOMEM;
565 }
566 initialize_tty_struct(o_tty, driver->other, idx);
567
568 tty->termios = kzalloc(sizeof(struct ktermios[2]), GFP_KERNEL);
569 if (tty->termios == NULL)
570 goto free_mem_out;
571 *tty->termios = driver->init_termios;
572 tty->termios_locked = tty->termios + 1;
573
574 o_tty->termios = kzalloc(sizeof(struct ktermios[2]), GFP_KERNEL);
575 if (o_tty->termios == NULL)
576 goto free_mem_out;
577 *o_tty->termios = driver->other->init_termios;
578 o_tty->termios_locked = o_tty->termios + 1;
579
580 tty_driver_kref_get(driver->other);
581 if (driver->subtype == PTY_TYPE_MASTER)
582 o_tty->count++;
583 /* Establish the links in both directions */
584 tty->link = o_tty;
585 o_tty->link = tty;
586 /*
587 * All structures have been allocated, so now we install them.
588 * Failures after this point use release_tty to clean up, so
589 * there's no need to null out the local pointers.
590 */
591 tty_driver_kref_get(driver);
592 tty->count++;
593 pty_count++;
594 return 0;
595free_mem_out:
596 kfree(o_tty->termios);
597 module_put(o_tty->driver->owner);
598 free_tty_struct(o_tty);
599 kfree(tty->termios);
600 return -ENOMEM;
601}
602
603static void pty_unix98_remove(struct tty_driver *driver, struct tty_struct *tty)
604{
605 pty_count--;
606}
607
608static const struct tty_operations ptm_unix98_ops = {
609 .lookup = ptm_unix98_lookup,
610 .install = pty_unix98_install,
611 .remove = pty_unix98_remove,
612 .open = pty_open,
613 .close = pty_close,
614 .write = pty_write,
615 .write_room = pty_write_room,
616 .flush_buffer = pty_flush_buffer,
617 .chars_in_buffer = pty_chars_in_buffer,
618 .unthrottle = pty_unthrottle,
619 .set_termios = pty_set_termios,
620 .ioctl = pty_unix98_ioctl,
621 .shutdown = pty_unix98_shutdown,
622 .resize = pty_resize
623};
624
625static const struct tty_operations pty_unix98_ops = {
626 .lookup = pts_unix98_lookup,
627 .install = pty_unix98_install,
628 .remove = pty_unix98_remove,
629 .open = pty_open,
630 .close = pty_close,
631 .write = pty_write,
632 .write_room = pty_write_room,
633 .flush_buffer = pty_flush_buffer,
634 .chars_in_buffer = pty_chars_in_buffer,
635 .unthrottle = pty_unthrottle,
636 .set_termios = pty_set_termios,
637 .shutdown = pty_unix98_shutdown
638};
639
640/**
641 * ptmx_open - open a unix 98 pty master
642 * @inode: inode of device file
643 * @filp: file pointer to tty
644 *
645 * Allocate a unix98 pty master device from the ptmx driver.
646 *
647 * Locking: tty_mutex protects the init_dev work. tty->count should
648 * protect the rest.
649 * allocated_ptys_lock handles the list of free pty numbers
650 */
651
652static int ptmx_open(struct inode *inode, struct file *filp)
653{
654 struct tty_struct *tty;
655 int retval;
656 int index;
657
658 nonseekable_open(inode, filp);
659
660 /* find a device that is not in use. */
661 tty_lock();
662 index = devpts_new_index(inode);
663 tty_unlock();
664 if (index < 0)
665 return index;
666
667 mutex_lock(&tty_mutex);
668 tty_lock();
669 tty = tty_init_dev(ptm_driver, index, 1);
670 mutex_unlock(&tty_mutex);
671
672 if (IS_ERR(tty)) {
673 retval = PTR_ERR(tty);
674 goto out;
675 }
676
677 set_bit(TTY_PTY_LOCK, &tty->flags); /* LOCK THE SLAVE */
678
679 tty_add_file(tty, filp);
680
681 retval = devpts_pty_new(inode, tty->link);
682 if (retval)
683 goto out1;
684
685 retval = ptm_driver->ops->open(tty, filp);
686 if (retval)
687 goto out2;
688out1:
689 tty_unlock();
690 return retval;
691out2:
692 tty_unlock();
693 tty_release(inode, filp);
694 return retval;
695out:
696 devpts_kill_index(inode, index);
697 tty_unlock();
698 return retval;
699}
700
701static struct file_operations ptmx_fops;
702
703static void __init unix98_pty_init(void)
704{
705 ptm_driver = alloc_tty_driver(NR_UNIX98_PTY_MAX);
706 if (!ptm_driver)
707 panic("Couldn't allocate Unix98 ptm driver");
708 pts_driver = alloc_tty_driver(NR_UNIX98_PTY_MAX);
709 if (!pts_driver)
710 panic("Couldn't allocate Unix98 pts driver");
711
712 ptm_driver->owner = THIS_MODULE;
713 ptm_driver->driver_name = "pty_master";
714 ptm_driver->name = "ptm";
715 ptm_driver->major = UNIX98_PTY_MASTER_MAJOR;
716 ptm_driver->minor_start = 0;
717 ptm_driver->type = TTY_DRIVER_TYPE_PTY;
718 ptm_driver->subtype = PTY_TYPE_MASTER;
719 ptm_driver->init_termios = tty_std_termios;
720 ptm_driver->init_termios.c_iflag = 0;
721 ptm_driver->init_termios.c_oflag = 0;
722 ptm_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
723 ptm_driver->init_termios.c_lflag = 0;
724 ptm_driver->init_termios.c_ispeed = 38400;
725 ptm_driver->init_termios.c_ospeed = 38400;
726 ptm_driver->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW |
727 TTY_DRIVER_DYNAMIC_DEV | TTY_DRIVER_DEVPTS_MEM;
728 ptm_driver->other = pts_driver;
729 tty_set_operations(ptm_driver, &ptm_unix98_ops);
730
731 pts_driver->owner = THIS_MODULE;
732 pts_driver->driver_name = "pty_slave";
733 pts_driver->name = "pts";
734 pts_driver->major = UNIX98_PTY_SLAVE_MAJOR;
735 pts_driver->minor_start = 0;
736 pts_driver->type = TTY_DRIVER_TYPE_PTY;
737 pts_driver->subtype = PTY_TYPE_SLAVE;
738 pts_driver->init_termios = tty_std_termios;
739 pts_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
740 pts_driver->init_termios.c_ispeed = 38400;
741 pts_driver->init_termios.c_ospeed = 38400;
742 pts_driver->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW |
743 TTY_DRIVER_DYNAMIC_DEV | TTY_DRIVER_DEVPTS_MEM;
744 pts_driver->other = ptm_driver;
745 tty_set_operations(pts_driver, &pty_unix98_ops);
746
747 if (tty_register_driver(ptm_driver))
748 panic("Couldn't register Unix98 ptm driver");
749 if (tty_register_driver(pts_driver))
750 panic("Couldn't register Unix98 pts driver");
751
752 register_sysctl_table(pty_root_table);
753
754 /* Now create the /dev/ptmx special device */
755 tty_default_fops(&ptmx_fops);
756 ptmx_fops.open = ptmx_open;
757
758 cdev_init(&ptmx_cdev, &ptmx_fops);
759 if (cdev_add(&ptmx_cdev, MKDEV(TTYAUX_MAJOR, 2), 1) ||
760 register_chrdev_region(MKDEV(TTYAUX_MAJOR, 2), 1, "/dev/ptmx") < 0)
761 panic("Couldn't register /dev/ptmx driver\n");
762 device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 2), NULL, "ptmx");
763}
764
765#else
766static inline void unix98_pty_init(void) { }
767#endif
768
769static int __init pty_init(void)
770{
771 legacy_pty_init();
772 unix98_pty_init();
773 return 0;
774}
775module_init(pty_init);
diff --git a/drivers/char/ramoops.c b/drivers/char/ramoops.c
index 74f00b5ffa36..1a9f5f6d6ac5 100644
--- a/drivers/char/ramoops.c
+++ b/drivers/char/ramoops.c
@@ -25,11 +25,12 @@
25#include <linux/time.h> 25#include <linux/time.h>
26#include <linux/io.h> 26#include <linux/io.h>
27#include <linux/ioport.h> 27#include <linux/ioport.h>
28#include <linux/platform_device.h>
29#include <linux/ramoops.h>
28 30
29#define RAMOOPS_KERNMSG_HDR "====" 31#define RAMOOPS_KERNMSG_HDR "===="
30#define RAMOOPS_HEADER_SIZE (5 + sizeof(struct timeval))
31 32
32#define RECORD_SIZE 4096 33#define RECORD_SIZE 4096UL
33 34
34static ulong mem_address; 35static ulong mem_address;
35module_param(mem_address, ulong, 0400); 36module_param(mem_address, ulong, 0400);
@@ -63,15 +64,22 @@ static void ramoops_do_dump(struct kmsg_dumper *dumper,
63 struct ramoops_context, dump); 64 struct ramoops_context, dump);
64 unsigned long s1_start, s2_start; 65 unsigned long s1_start, s2_start;
65 unsigned long l1_cpy, l2_cpy; 66 unsigned long l1_cpy, l2_cpy;
66 int res; 67 int res, hdr_size;
67 char *buf; 68 char *buf, *buf_orig;
68 struct timeval timestamp; 69 struct timeval timestamp;
69 70
71 if (reason != KMSG_DUMP_OOPS &&
72 reason != KMSG_DUMP_PANIC &&
73 reason != KMSG_DUMP_KEXEC)
74 return;
75
70 /* Only dump oopses if dump_oops is set */ 76 /* Only dump oopses if dump_oops is set */
71 if (reason == KMSG_DUMP_OOPS && !dump_oops) 77 if (reason == KMSG_DUMP_OOPS && !dump_oops)
72 return; 78 return;
73 79
74 buf = (char *)(cxt->virt_addr + (cxt->count * RECORD_SIZE)); 80 buf = cxt->virt_addr + (cxt->count * RECORD_SIZE);
81 buf_orig = buf;
82
75 memset(buf, '\0', RECORD_SIZE); 83 memset(buf, '\0', RECORD_SIZE);
76 res = sprintf(buf, "%s", RAMOOPS_KERNMSG_HDR); 84 res = sprintf(buf, "%s", RAMOOPS_KERNMSG_HDR);
77 buf += res; 85 buf += res;
@@ -79,8 +87,9 @@ static void ramoops_do_dump(struct kmsg_dumper *dumper,
79 res = sprintf(buf, "%lu.%lu\n", (long)timestamp.tv_sec, (long)timestamp.tv_usec); 87 res = sprintf(buf, "%lu.%lu\n", (long)timestamp.tv_sec, (long)timestamp.tv_usec);
80 buf += res; 88 buf += res;
81 89
82 l2_cpy = min(l2, (unsigned long)(RECORD_SIZE - RAMOOPS_HEADER_SIZE)); 90 hdr_size = buf - buf_orig;
83 l1_cpy = min(l1, (unsigned long)(RECORD_SIZE - RAMOOPS_HEADER_SIZE) - l2_cpy); 91 l2_cpy = min(l2, RECORD_SIZE - hdr_size);
92 l1_cpy = min(l1, RECORD_SIZE - hdr_size - l2_cpy);
84 93
85 s2_start = l2 - l2_cpy; 94 s2_start = l2 - l2_cpy;
86 s1_start = l1 - l1_cpy; 95 s1_start = l1 - l1_cpy;
@@ -91,11 +100,17 @@ static void ramoops_do_dump(struct kmsg_dumper *dumper,
91 cxt->count = (cxt->count + 1) % cxt->max_count; 100 cxt->count = (cxt->count + 1) % cxt->max_count;
92} 101}
93 102
94static int __init ramoops_init(void) 103static int __init ramoops_probe(struct platform_device *pdev)
95{ 104{
105 struct ramoops_platform_data *pdata = pdev->dev.platform_data;
96 struct ramoops_context *cxt = &oops_cxt; 106 struct ramoops_context *cxt = &oops_cxt;
97 int err = -EINVAL; 107 int err = -EINVAL;
98 108
109 if (pdata) {
110 mem_size = pdata->mem_size;
111 mem_address = pdata->mem_address;
112 }
113
99 if (!mem_size) { 114 if (!mem_size) {
100 printk(KERN_ERR "ramoops: invalid size specification"); 115 printk(KERN_ERR "ramoops: invalid size specification");
101 goto fail3; 116 goto fail3;
@@ -142,7 +157,7 @@ fail3:
142 return err; 157 return err;
143} 158}
144 159
145static void __exit ramoops_exit(void) 160static int __exit ramoops_remove(struct platform_device *pdev)
146{ 161{
147 struct ramoops_context *cxt = &oops_cxt; 162 struct ramoops_context *cxt = &oops_cxt;
148 163
@@ -151,8 +166,26 @@ static void __exit ramoops_exit(void)
151 166
152 iounmap(cxt->virt_addr); 167 iounmap(cxt->virt_addr);
153 release_mem_region(cxt->phys_addr, cxt->size); 168 release_mem_region(cxt->phys_addr, cxt->size);
169 return 0;
154} 170}
155 171
172static struct platform_driver ramoops_driver = {
173 .remove = __exit_p(ramoops_remove),
174 .driver = {
175 .name = "ramoops",
176 .owner = THIS_MODULE,
177 },
178};
179
180static int __init ramoops_init(void)
181{
182 return platform_driver_probe(&ramoops_driver, ramoops_probe);
183}
184
185static void __exit ramoops_exit(void)
186{
187 platform_driver_unregister(&ramoops_driver);
188}
156 189
157module_init(ramoops_init); 190module_init(ramoops_init);
158module_exit(ramoops_exit); 191module_exit(ramoops_exit);
diff --git a/drivers/char/random.c b/drivers/char/random.c
index caef35a46890..d4ddeba56682 100644
--- a/drivers/char/random.c
+++ b/drivers/char/random.c
@@ -128,6 +128,7 @@
128 * void add_input_randomness(unsigned int type, unsigned int code, 128 * void add_input_randomness(unsigned int type, unsigned int code,
129 * unsigned int value); 129 * unsigned int value);
130 * void add_interrupt_randomness(int irq); 130 * void add_interrupt_randomness(int irq);
131 * void add_disk_randomness(struct gendisk *disk);
131 * 132 *
132 * add_input_randomness() uses the input layer interrupt timing, as well as 133 * add_input_randomness() uses the input layer interrupt timing, as well as
133 * the event type information from the hardware. 134 * the event type information from the hardware.
@@ -136,9 +137,15 @@
136 * inputs to the entropy pool. Note that not all interrupts are good 137 * inputs to the entropy pool. Note that not all interrupts are good
137 * sources of randomness! For example, the timer interrupts is not a 138 * sources of randomness! For example, the timer interrupts is not a
138 * good choice, because the periodicity of the interrupts is too 139 * good choice, because the periodicity of the interrupts is too
139 * regular, and hence predictable to an attacker. Disk interrupts are 140 * regular, and hence predictable to an attacker. Network Interface
140 * a better measure, since the timing of the disk interrupts are more 141 * Controller interrupts are a better measure, since the timing of the
141 * unpredictable. 142 * NIC interrupts are more unpredictable.
143 *
144 * add_disk_randomness() uses what amounts to the seek time of block
145 * layer request events, on a per-disk_devt basis, as input to the
146 * entropy pool. Note that high-speed solid state drives with very low
147 * seek times do not make for good sources of entropy, as their seek
148 * times are usually fairly consistent.
142 * 149 *
143 * All of these routines try to estimate how many bits of randomness a 150 * All of these routines try to estimate how many bits of randomness a
144 * particular randomness source. They do this by keeping track of the 151 * particular randomness source. They do this by keeping track of the
@@ -626,7 +633,7 @@ static void add_timer_randomness(struct timer_rand_state *state, unsigned num)
626 preempt_disable(); 633 preempt_disable();
627 /* if over the trickle threshold, use only 1 in 4096 samples */ 634 /* if over the trickle threshold, use only 1 in 4096 samples */
628 if (input_pool.entropy_count > trickle_thresh && 635 if (input_pool.entropy_count > trickle_thresh &&
629 (__get_cpu_var(trickle_count)++ & 0xfff)) 636 ((__this_cpu_inc_return(trickle_count) - 1) & 0xfff))
630 goto out; 637 goto out;
631 638
632 sample.jiffies = jiffies; 639 sample.jiffies = jiffies;
@@ -725,7 +732,7 @@ static ssize_t extract_entropy(struct entropy_store *r, void *buf,
725 size_t nbytes, int min, int rsvd); 732 size_t nbytes, int min, int rsvd);
726 733
727/* 734/*
728 * This utility inline function is responsible for transfering entropy 735 * This utility inline function is responsible for transferring entropy
729 * from the primary pool to the secondary extraction pool. We make 736 * from the primary pool to the secondary extraction pool. We make
730 * sure we pull enough for a 'catastrophic reseed'. 737 * sure we pull enough for a 'catastrophic reseed'.
731 */ 738 */
@@ -1165,6 +1172,7 @@ const struct file_operations random_fops = {
1165 .poll = random_poll, 1172 .poll = random_poll,
1166 .unlocked_ioctl = random_ioctl, 1173 .unlocked_ioctl = random_ioctl,
1167 .fasync = random_fasync, 1174 .fasync = random_fasync,
1175 .llseek = noop_llseek,
1168}; 1176};
1169 1177
1170const struct file_operations urandom_fops = { 1178const struct file_operations urandom_fops = {
@@ -1172,6 +1180,7 @@ const struct file_operations urandom_fops = {
1172 .write = random_write, 1180 .write = random_write,
1173 .unlocked_ioctl = random_ioctl, 1181 .unlocked_ioctl = random_ioctl,
1174 .fasync = random_fasync, 1182 .fasync = random_fasync,
1183 .llseek = noop_llseek,
1175}; 1184};
1176 1185
1177/*************************************************************** 1186/***************************************************************
diff --git a/drivers/char/raw.c b/drivers/char/raw.c
index b38942f6bf31..b33e8ea314ed 100644
--- a/drivers/char/raw.c
+++ b/drivers/char/raw.c
@@ -19,8 +19,9 @@
19#include <linux/cdev.h> 19#include <linux/cdev.h>
20#include <linux/device.h> 20#include <linux/device.h>
21#include <linux/mutex.h> 21#include <linux/mutex.h>
22#include <linux/smp_lock.h>
23#include <linux/gfp.h> 22#include <linux/gfp.h>
23#include <linux/compat.h>
24#include <linux/vmalloc.h>
24 25
25#include <asm/uaccess.h> 26#include <asm/uaccess.h>
26 27
@@ -30,10 +31,15 @@ struct raw_device_data {
30}; 31};
31 32
32static struct class *raw_class; 33static struct class *raw_class;
33static struct raw_device_data raw_devices[MAX_RAW_MINORS]; 34static struct raw_device_data *raw_devices;
34static DEFINE_MUTEX(raw_mutex); 35static DEFINE_MUTEX(raw_mutex);
35static const struct file_operations raw_ctl_fops; /* forward declaration */ 36static const struct file_operations raw_ctl_fops; /* forward declaration */
36 37
38static int max_raw_minors = MAX_RAW_MINORS;
39
40module_param(max_raw_minors, int, 0);
41MODULE_PARM_DESC(max_raw_minors, "Maximum number of raw devices (1-65536)");
42
37/* 43/*
38 * Open/close code for raw IO. 44 * Open/close code for raw IO.
39 * 45 *
@@ -55,7 +61,6 @@ static int raw_open(struct inode *inode, struct file *filp)
55 return 0; 61 return 0;
56 } 62 }
57 63
58 lock_kernel();
59 mutex_lock(&raw_mutex); 64 mutex_lock(&raw_mutex);
60 65
61 /* 66 /*
@@ -66,15 +71,12 @@ static int raw_open(struct inode *inode, struct file *filp)
66 if (!bdev) 71 if (!bdev)
67 goto out; 72 goto out;
68 igrab(bdev->bd_inode); 73 igrab(bdev->bd_inode);
69 err = blkdev_get(bdev, filp->f_mode); 74 err = blkdev_get(bdev, filp->f_mode | FMODE_EXCL, raw_open);
70 if (err) 75 if (err)
71 goto out; 76 goto out;
72 err = bd_claim(bdev, raw_open);
73 if (err)
74 goto out1;
75 err = set_blocksize(bdev, bdev_logical_block_size(bdev)); 77 err = set_blocksize(bdev, bdev_logical_block_size(bdev));
76 if (err) 78 if (err)
77 goto out2; 79 goto out1;
78 filp->f_flags |= O_DIRECT; 80 filp->f_flags |= O_DIRECT;
79 filp->f_mapping = bdev->bd_inode->i_mapping; 81 filp->f_mapping = bdev->bd_inode->i_mapping;
80 if (++raw_devices[minor].inuse == 1) 82 if (++raw_devices[minor].inuse == 1)
@@ -82,16 +84,12 @@ static int raw_open(struct inode *inode, struct file *filp)
82 bdev->bd_inode->i_mapping; 84 bdev->bd_inode->i_mapping;
83 filp->private_data = bdev; 85 filp->private_data = bdev;
84 mutex_unlock(&raw_mutex); 86 mutex_unlock(&raw_mutex);
85 unlock_kernel();
86 return 0; 87 return 0;
87 88
88out2:
89 bd_release(bdev);
90out1: 89out1:
91 blkdev_put(bdev, filp->f_mode); 90 blkdev_put(bdev, filp->f_mode | FMODE_EXCL);
92out: 91out:
93 mutex_unlock(&raw_mutex); 92 mutex_unlock(&raw_mutex);
94 unlock_kernel();
95 return err; 93 return err;
96} 94}
97 95
@@ -113,8 +111,7 @@ static int raw_release(struct inode *inode, struct file *filp)
113 } 111 }
114 mutex_unlock(&raw_mutex); 112 mutex_unlock(&raw_mutex);
115 113
116 bd_release(bdev); 114 blkdev_put(bdev, filp->f_mode | FMODE_EXCL);
117 blkdev_put(bdev, filp->f_mode);
118 return 0; 115 return 0;
119} 116}
120 117
@@ -125,20 +122,84 @@ static long
125raw_ioctl(struct file *filp, unsigned int command, unsigned long arg) 122raw_ioctl(struct file *filp, unsigned int command, unsigned long arg)
126{ 123{
127 struct block_device *bdev = filp->private_data; 124 struct block_device *bdev = filp->private_data;
128 int ret; 125 return blkdev_ioctl(bdev, 0, command, arg);
126}
129 127
130 lock_kernel(); 128static int bind_set(int number, u64 major, u64 minor)
131 ret = blkdev_ioctl(bdev, 0, command, arg); 129{
132 unlock_kernel(); 130 dev_t dev = MKDEV(major, minor);
131 struct raw_device_data *rawdev;
132 int err = 0;
133 133
134 return ret; 134 if (number <= 0 || number >= max_raw_minors)
135 return -EINVAL;
136
137 if (MAJOR(dev) != major || MINOR(dev) != minor)
138 return -EINVAL;
139
140 rawdev = &raw_devices[number];
141
142 /*
143 * This is like making block devices, so demand the
144 * same capability
145 */
146 if (!capable(CAP_SYS_ADMIN))
147 return -EPERM;
148
149 /*
150 * For now, we don't need to check that the underlying
151 * block device is present or not: we can do that when
152 * the raw device is opened. Just check that the
153 * major/minor numbers make sense.
154 */
155
156 if (MAJOR(dev) == 0 && dev != 0)
157 return -EINVAL;
158
159 mutex_lock(&raw_mutex);
160 if (rawdev->inuse) {
161 mutex_unlock(&raw_mutex);
162 return -EBUSY;
163 }
164 if (rawdev->binding) {
165 bdput(rawdev->binding);
166 module_put(THIS_MODULE);
167 }
168 if (!dev) {
169 /* unbind */
170 rawdev->binding = NULL;
171 device_destroy(raw_class, MKDEV(RAW_MAJOR, number));
172 } else {
173 rawdev->binding = bdget(dev);
174 if (rawdev->binding == NULL) {
175 err = -ENOMEM;
176 } else {
177 dev_t raw = MKDEV(RAW_MAJOR, number);
178 __module_get(THIS_MODULE);
179 device_destroy(raw_class, raw);
180 device_create(raw_class, NULL, raw, NULL,
181 "raw%d", number);
182 }
183 }
184 mutex_unlock(&raw_mutex);
185 return err;
135} 186}
136 187
137static void bind_device(struct raw_config_request *rq) 188static int bind_get(int number, dev_t *dev)
138{ 189{
139 device_destroy(raw_class, MKDEV(RAW_MAJOR, rq->raw_minor)); 190 struct raw_device_data *rawdev;
140 device_create(raw_class, NULL, MKDEV(RAW_MAJOR, rq->raw_minor), NULL, 191 struct block_device *bdev;
141 "raw%d", rq->raw_minor); 192
193 if (number <= 0 || number >= MAX_RAW_MINORS)
194 return -EINVAL;
195
196 rawdev = &raw_devices[number];
197
198 mutex_lock(&raw_mutex);
199 bdev = rawdev->binding;
200 *dev = bdev ? bdev->bd_dev : 0;
201 mutex_unlock(&raw_mutex);
202 return 0;
142} 203}
143 204
144/* 205/*
@@ -149,106 +210,79 @@ static long raw_ctl_ioctl(struct file *filp, unsigned int command,
149 unsigned long arg) 210 unsigned long arg)
150{ 211{
151 struct raw_config_request rq; 212 struct raw_config_request rq;
152 struct raw_device_data *rawdev; 213 dev_t dev;
153 int err = 0; 214 int err;
154 215
155 lock_kernel();
156 switch (command) { 216 switch (command) {
157 case RAW_SETBIND: 217 case RAW_SETBIND:
218 if (copy_from_user(&rq, (void __user *) arg, sizeof(rq)))
219 return -EFAULT;
220
221 return bind_set(rq.raw_minor, rq.block_major, rq.block_minor);
222
158 case RAW_GETBIND: 223 case RAW_GETBIND:
224 if (copy_from_user(&rq, (void __user *) arg, sizeof(rq)))
225 return -EFAULT;
159 226
160 /* First, find out which raw minor we want */ 227 err = bind_get(rq.raw_minor, &dev);
228 if (err)
229 return err;
161 230
162 if (copy_from_user(&rq, (void __user *) arg, sizeof(rq))) { 231 rq.block_major = MAJOR(dev);
163 err = -EFAULT; 232 rq.block_minor = MINOR(dev);
164 goto out;
165 }
166 233
167 if (rq.raw_minor <= 0 || rq.raw_minor >= MAX_RAW_MINORS) { 234 if (copy_to_user((void __user *)arg, &rq, sizeof(rq)))
168 err = -EINVAL; 235 return -EFAULT;
169 goto out; 236
170 } 237 return 0;
171 rawdev = &raw_devices[rq.raw_minor];
172
173 if (command == RAW_SETBIND) {
174 dev_t dev;
175
176 /*
177 * This is like making block devices, so demand the
178 * same capability
179 */
180 if (!capable(CAP_SYS_ADMIN)) {
181 err = -EPERM;
182 goto out;
183 }
184
185 /*
186 * For now, we don't need to check that the underlying
187 * block device is present or not: we can do that when
188 * the raw device is opened. Just check that the
189 * major/minor numbers make sense.
190 */
191
192 dev = MKDEV(rq.block_major, rq.block_minor);
193 if ((rq.block_major == 0 && rq.block_minor != 0) ||
194 MAJOR(dev) != rq.block_major ||
195 MINOR(dev) != rq.block_minor) {
196 err = -EINVAL;
197 goto out;
198 }
199
200 mutex_lock(&raw_mutex);
201 if (rawdev->inuse) {
202 mutex_unlock(&raw_mutex);
203 err = -EBUSY;
204 goto out;
205 }
206 if (rawdev->binding) {
207 bdput(rawdev->binding);
208 module_put(THIS_MODULE);
209 }
210 if (rq.block_major == 0 && rq.block_minor == 0) {
211 /* unbind */
212 rawdev->binding = NULL;
213 device_destroy(raw_class,
214 MKDEV(RAW_MAJOR, rq.raw_minor));
215 } else {
216 rawdev->binding = bdget(dev);
217 if (rawdev->binding == NULL)
218 err = -ENOMEM;
219 else {
220 __module_get(THIS_MODULE);
221 bind_device(&rq);
222 }
223 }
224 mutex_unlock(&raw_mutex);
225 } else {
226 struct block_device *bdev;
227
228 mutex_lock(&raw_mutex);
229 bdev = rawdev->binding;
230 if (bdev) {
231 rq.block_major = MAJOR(bdev->bd_dev);
232 rq.block_minor = MINOR(bdev->bd_dev);
233 } else {
234 rq.block_major = rq.block_minor = 0;
235 }
236 mutex_unlock(&raw_mutex);
237 if (copy_to_user((void __user *)arg, &rq, sizeof(rq))) {
238 err = -EFAULT;
239 goto out;
240 }
241 }
242 break;
243 default:
244 err = -EINVAL;
245 break;
246 } 238 }
247out: 239
248 unlock_kernel(); 240 return -EINVAL;
249 return err;
250} 241}
251 242
243#ifdef CONFIG_COMPAT
244struct raw32_config_request {
245 compat_int_t raw_minor;
246 compat_u64 block_major;
247 compat_u64 block_minor;
248};
249
250static long raw_ctl_compat_ioctl(struct file *file, unsigned int cmd,
251 unsigned long arg)
252{
253 struct raw32_config_request __user *user_req = compat_ptr(arg);
254 struct raw32_config_request rq;
255 dev_t dev;
256 int err = 0;
257
258 switch (cmd) {
259 case RAW_SETBIND:
260 if (copy_from_user(&rq, user_req, sizeof(rq)))
261 return -EFAULT;
262
263 return bind_set(rq.raw_minor, rq.block_major, rq.block_minor);
264
265 case RAW_GETBIND:
266 if (copy_from_user(&rq, user_req, sizeof(rq)))
267 return -EFAULT;
268
269 err = bind_get(rq.raw_minor, &dev);
270 if (err)
271 return err;
272
273 rq.block_major = MAJOR(dev);
274 rq.block_minor = MINOR(dev);
275
276 if (copy_to_user(user_req, &rq, sizeof(rq)))
277 return -EFAULT;
278
279 return 0;
280 }
281
282 return -EINVAL;
283}
284#endif
285
252static const struct file_operations raw_fops = { 286static const struct file_operations raw_fops = {
253 .read = do_sync_read, 287 .read = do_sync_read,
254 .aio_read = generic_file_aio_read, 288 .aio_read = generic_file_aio_read,
@@ -258,13 +292,18 @@ static const struct file_operations raw_fops = {
258 .open = raw_open, 292 .open = raw_open,
259 .release = raw_release, 293 .release = raw_release,
260 .unlocked_ioctl = raw_ioctl, 294 .unlocked_ioctl = raw_ioctl,
295 .llseek = default_llseek,
261 .owner = THIS_MODULE, 296 .owner = THIS_MODULE,
262}; 297};
263 298
264static const struct file_operations raw_ctl_fops = { 299static const struct file_operations raw_ctl_fops = {
265 .unlocked_ioctl = raw_ctl_ioctl, 300 .unlocked_ioctl = raw_ctl_ioctl,
301#ifdef CONFIG_COMPAT
302 .compat_ioctl = raw_ctl_compat_ioctl,
303#endif
266 .open = raw_open, 304 .open = raw_open,
267 .owner = THIS_MODULE, 305 .owner = THIS_MODULE,
306 .llseek = noop_llseek,
268}; 307};
269 308
270static struct cdev raw_cdev; 309static struct cdev raw_cdev;
@@ -279,14 +318,27 @@ static int __init raw_init(void)
279 dev_t dev = MKDEV(RAW_MAJOR, 0); 318 dev_t dev = MKDEV(RAW_MAJOR, 0);
280 int ret; 319 int ret;
281 320
282 ret = register_chrdev_region(dev, MAX_RAW_MINORS, "raw"); 321 if (max_raw_minors < 1 || max_raw_minors > 65536) {
322 printk(KERN_WARNING "raw: invalid max_raw_minors (must be"
323 " between 1 and 65536), using %d\n", MAX_RAW_MINORS);
324 max_raw_minors = MAX_RAW_MINORS;
325 }
326
327 raw_devices = vmalloc(sizeof(struct raw_device_data) * max_raw_minors);
328 if (!raw_devices) {
329 printk(KERN_ERR "Not enough memory for raw device structures\n");
330 ret = -ENOMEM;
331 goto error;
332 }
333 memset(raw_devices, 0, sizeof(struct raw_device_data) * max_raw_minors);
334
335 ret = register_chrdev_region(dev, max_raw_minors, "raw");
283 if (ret) 336 if (ret)
284 goto error; 337 goto error;
285 338
286 cdev_init(&raw_cdev, &raw_fops); 339 cdev_init(&raw_cdev, &raw_fops);
287 ret = cdev_add(&raw_cdev, dev, MAX_RAW_MINORS); 340 ret = cdev_add(&raw_cdev, dev, max_raw_minors);
288 if (ret) { 341 if (ret) {
289 kobject_put(&raw_cdev.kobj);
290 goto error_region; 342 goto error_region;
291 } 343 }
292 344
@@ -303,8 +355,9 @@ static int __init raw_init(void)
303 return 0; 355 return 0;
304 356
305error_region: 357error_region:
306 unregister_chrdev_region(dev, MAX_RAW_MINORS); 358 unregister_chrdev_region(dev, max_raw_minors);
307error: 359error:
360 vfree(raw_devices);
308 return ret; 361 return ret;
309} 362}
310 363
@@ -313,7 +366,7 @@ static void __exit raw_exit(void)
313 device_destroy(raw_class, MKDEV(RAW_MAJOR, 0)); 366 device_destroy(raw_class, MKDEV(RAW_MAJOR, 0));
314 class_destroy(raw_class); 367 class_destroy(raw_class);
315 cdev_del(&raw_cdev); 368 cdev_del(&raw_cdev);
316 unregister_chrdev_region(MKDEV(RAW_MAJOR, 0), MAX_RAW_MINORS); 369 unregister_chrdev_region(MKDEV(RAW_MAJOR, 0), max_raw_minors);
317} 370}
318 371
319module_init(raw_init); 372module_init(raw_init);
diff --git a/drivers/char/rio/Makefile b/drivers/char/rio/Makefile
deleted file mode 100644
index 2d1c5a7cba7d..000000000000
--- a/drivers/char/rio/Makefile
+++ /dev/null
@@ -1,12 +0,0 @@
1#
2# Makefile for the linux rio-subsystem.
3#
4# (C) R.E.Wolff@BitWizard.nl
5#
6# This file is GPL. See other files for the full Blurb. I'm lazy today.
7#
8
9obj-$(CONFIG_RIO) += rio.o
10
11rio-objs := rio_linux.o rioinit.o rioboot.o riocmd.o rioctrl.o riointr.o \
12 rioparam.o rioroute.o riotable.o riotty.o
diff --git a/drivers/char/rio/board.h b/drivers/char/rio/board.h
deleted file mode 100644
index bdea633a9076..000000000000
--- a/drivers/char/rio/board.h
+++ /dev/null
@@ -1,132 +0,0 @@
1/*
2** -----------------------------------------------------------------------------
3**
4** Perle Specialix driver for Linux
5** Ported from existing RIO Driver for SCO sources.
6 *
7 * (C) 1990 - 2000 Specialix International Ltd., Byfleet, Surrey, UK.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22**
23** Module : board.h
24** SID : 1.2
25** Last Modified : 11/6/98 11:34:07
26** Retrieved : 11/6/98 11:34:20
27**
28** ident @(#)board.h 1.2
29**
30** -----------------------------------------------------------------------------
31*/
32
33#ifndef __rio_board_h__
34#define __rio_board_h__
35
36/*
37** board.h contains the definitions for the *hardware* of the host cards.
38** It describes the memory overlay for the dual port RAM area.
39*/
40
41#define DP_SRAM1_SIZE 0x7C00
42#define DP_SRAM2_SIZE 0x0200
43#define DP_SRAM3_SIZE 0x7000
44#define DP_SCRATCH_SIZE 0x1000
45#define DP_PARMMAP_ADDR 0x01FE /* offset into SRAM2 */
46#define DP_STARTUP_ADDR 0x01F8 /* offset into SRAM2 */
47
48/*
49** The shape of the Host Control area, at offset 0x7C00, Write Only
50*/
51struct s_Ctrl {
52 u8 DpCtl; /* 7C00 */
53 u8 Dp_Unused2_[127];
54 u8 DpIntSet; /* 7C80 */
55 u8 Dp_Unused3_[127];
56 u8 DpTpuReset; /* 7D00 */
57 u8 Dp_Unused4_[127];
58 u8 DpIntReset; /* 7D80 */
59 u8 Dp_Unused5_[127];
60};
61
62/*
63** The PROM data area on the host (0x7C00), Read Only
64*/
65struct s_Prom {
66 u16 DpSlxCode[2];
67 u16 DpRev;
68 u16 Dp_Unused6_;
69 u16 DpUniq[4];
70 u16 DpJahre;
71 u16 DpWoche;
72 u16 DpHwFeature[5];
73 u16 DpOemId;
74 u16 DpSiggy[16];
75};
76
77/*
78** Union of the Ctrl and Prom areas
79*/
80union u_CtrlProm { /* This is the control/PROM area (0x7C00) */
81 struct s_Ctrl DpCtrl;
82 struct s_Prom DpProm;
83};
84
85/*
86** The top end of memory!
87*/
88struct s_ParmMapS { /* Area containing Parm Map Pointer */
89 u8 Dp_Unused8_[DP_PARMMAP_ADDR];
90 u16 DpParmMapAd;
91};
92
93struct s_StartUpS {
94 u8 Dp_Unused9_[DP_STARTUP_ADDR];
95 u8 Dp_LongJump[0x4];
96 u8 Dp_Unused10_[2];
97 u8 Dp_ShortJump[0x2];
98};
99
100union u_Sram2ParmMap { /* This is the top of memory (0x7E00-0x7FFF) */
101 u8 DpSramMem[DP_SRAM2_SIZE];
102 struct s_ParmMapS DpParmMapS;
103 struct s_StartUpS DpStartUpS;
104};
105
106/*
107** This is the DP RAM overlay.
108*/
109struct DpRam {
110 u8 DpSram1[DP_SRAM1_SIZE]; /* 0000 - 7BFF */
111 union u_CtrlProm DpCtrlProm; /* 7C00 - 7DFF */
112 union u_Sram2ParmMap DpSram2ParmMap; /* 7E00 - 7FFF */
113 u8 DpScratch[DP_SCRATCH_SIZE]; /* 8000 - 8FFF */
114 u8 DpSram3[DP_SRAM3_SIZE]; /* 9000 - FFFF */
115};
116
117#define DpControl DpCtrlProm.DpCtrl.DpCtl
118#define DpSetInt DpCtrlProm.DpCtrl.DpIntSet
119#define DpResetTpu DpCtrlProm.DpCtrl.DpTpuReset
120#define DpResetInt DpCtrlProm.DpCtrl.DpIntReset
121
122#define DpSlx DpCtrlProm.DpProm.DpSlxCode
123#define DpRevision DpCtrlProm.DpProm.DpRev
124#define DpUnique DpCtrlProm.DpProm.DpUniq
125#define DpYear DpCtrlProm.DpProm.DpJahre
126#define DpWeek DpCtrlProm.DpProm.DpWoche
127#define DpSignature DpCtrlProm.DpProm.DpSiggy
128
129#define DpParmMapR DpSram2ParmMap.DpParmMapS.DpParmMapAd
130#define DpSram2 DpSram2ParmMap.DpSramMem
131
132#endif
diff --git a/drivers/char/rio/cirrus.h b/drivers/char/rio/cirrus.h
deleted file mode 100644
index 5ab51679caa2..000000000000
--- a/drivers/char/rio/cirrus.h
+++ /dev/null
@@ -1,210 +0,0 @@
1/****************************************************************************
2 ******* *******
3 ******* CIRRUS.H *******
4 ******* *******
5 ****************************************************************************
6
7 Author : Jeremy Rolls
8 Date : 3 Aug 1990
9
10 *
11 * (C) 1990 - 2000 Specialix International Ltd., Byfleet, Surrey, UK.
12 *
13 * 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.
17 *
18 * 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.
22 *
23 * 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
27 Version : 0.01
28
29
30 Mods
31 ----------------------------------------------------------------------------
32 Date By Description
33 ----------------------------------------------------------------------------
34
35 ***************************************************************************/
36
37#ifndef _cirrus_h
38#define _cirrus_h 1
39
40/* Bit fields for particular registers shared with driver */
41
42/* COR1 - driver and RTA */
43#define RIOC_COR1_ODD 0x80 /* Odd parity */
44#define RIOC_COR1_EVEN 0x00 /* Even parity */
45#define RIOC_COR1_NOP 0x00 /* No parity */
46#define RIOC_COR1_FORCE 0x20 /* Force parity */
47#define RIOC_COR1_NORMAL 0x40 /* With parity */
48#define RIOC_COR1_1STOP 0x00 /* 1 stop bit */
49#define RIOC_COR1_15STOP 0x04 /* 1.5 stop bits */
50#define RIOC_COR1_2STOP 0x08 /* 2 stop bits */
51#define RIOC_COR1_5BITS 0x00 /* 5 data bits */
52#define RIOC_COR1_6BITS 0x01 /* 6 data bits */
53#define RIOC_COR1_7BITS 0x02 /* 7 data bits */
54#define RIOC_COR1_8BITS 0x03 /* 8 data bits */
55
56#define RIOC_COR1_HOST 0xef /* Safe host bits */
57
58/* RTA only */
59#define RIOC_COR1_CINPCK 0x00 /* Check parity of received characters */
60#define RIOC_COR1_CNINPCK 0x10 /* Don't check parity */
61
62/* COR2 bits for both RTA and driver use */
63#define RIOC_COR2_IXANY 0x80 /* IXANY - any character is XON */
64#define RIOC_COR2_IXON 0x40 /* IXON - enable tx soft flowcontrol */
65#define RIOC_COR2_RTSFLOW 0x02 /* Enable tx hardware flow control */
66
67/* Additional driver bits */
68#define RIOC_COR2_HUPCL 0x20 /* Hang up on close */
69#define RIOC_COR2_CTSFLOW 0x04 /* Enable rx hardware flow control */
70#define RIOC_COR2_IXOFF 0x01 /* Enable rx software flow control */
71#define RIOC_COR2_DTRFLOW 0x08 /* Enable tx hardware flow control */
72
73/* RTA use only */
74#define RIOC_COR2_ETC 0x20 /* Embedded transmit options */
75#define RIOC_COR2_LOCAL 0x10 /* Local loopback mode */
76#define RIOC_COR2_REMOTE 0x08 /* Remote loopback mode */
77#define RIOC_COR2_HOST 0xc2 /* Safe host bits */
78
79/* COR3 - RTA use only */
80#define RIOC_COR3_SCDRNG 0x80 /* Enable special char detect for range */
81#define RIOC_COR3_SCD34 0x40 /* Special character detect for SCHR's 3 + 4 */
82#define RIOC_COR3_FCT 0x20 /* Flow control transparency */
83#define RIOC_COR3_SCD12 0x10 /* Special character detect for SCHR's 1 + 2 */
84#define RIOC_COR3_FIFO12 0x0c /* 12 chars for receive FIFO threshold */
85#define RIOC_COR3_FIFO10 0x0a /* 10 chars for receive FIFO threshold */
86#define RIOC_COR3_FIFO8 0x08 /* 8 chars for receive FIFO threshold */
87#define RIOC_COR3_FIFO6 0x06 /* 6 chars for receive FIFO threshold */
88
89#define RIOC_COR3_THRESHOLD RIOC_COR3_FIFO8 /* MUST BE LESS THAN MCOR_THRESHOLD */
90
91#define RIOC_COR3_DEFAULT (RIOC_COR3_FCT | RIOC_COR3_THRESHOLD)
92 /* Default bits for COR3 */
93
94/* COR4 driver and RTA use */
95#define RIOC_COR4_IGNCR 0x80 /* Throw away CR's on input */
96#define RIOC_COR4_ICRNL 0x40 /* Map CR -> NL on input */
97#define RIOC_COR4_INLCR 0x20 /* Map NL -> CR on input */
98#define RIOC_COR4_IGNBRK 0x10 /* Ignore Break */
99#define RIOC_COR4_NBRKINT 0x08 /* No interrupt on break (-BRKINT) */
100#define RIOC_COR4_RAISEMOD 0x01 /* Raise modem output lines on non-zero baud */
101
102
103/* COR4 driver only */
104#define RIOC_COR4_IGNPAR 0x04 /* IGNPAR (ignore characters with errors) */
105#define RIOC_COR4_PARMRK 0x02 /* PARMRK */
106
107#define RIOC_COR4_HOST 0xf8 /* Safe host bits */
108
109/* COR4 RTA only */
110#define RIOC_COR4_CIGNPAR 0x02 /* Thrown away bad characters */
111#define RIOC_COR4_CPARMRK 0x04 /* PARMRK characters */
112#define RIOC_COR4_CNPARMRK 0x03 /* Don't PARMRK */
113
114/* COR5 driver and RTA use */
115#define RIOC_COR5_ISTRIP 0x80 /* Strip input chars to 7 bits */
116#define RIOC_COR5_LNE 0x40 /* Enable LNEXT processing */
117#define RIOC_COR5_CMOE 0x20 /* Match good and errored characters */
118#define RIOC_COR5_ONLCR 0x02 /* NL -> CR NL on output */
119#define RIOC_COR5_OCRNL 0x01 /* CR -> NL on output */
120
121/*
122** Spare bits - these are not used in the CIRRUS registers, so we use
123** them to set various other features.
124*/
125/*
126** tstop and tbusy indication
127*/
128#define RIOC_COR5_TSTATE_ON 0x08 /* Turn on monitoring of tbusy and tstop */
129#define RIOC_COR5_TSTATE_OFF 0x04 /* Turn off monitoring of tbusy and tstop */
130/*
131** TAB3
132*/
133#define RIOC_COR5_TAB3 0x10 /* TAB3 mode */
134
135#define RIOC_COR5_HOST 0xc3 /* Safe host bits */
136
137/* CCSR */
138#define RIOC_CCSR_TXFLOFF 0x04 /* Tx is xoffed */
139
140/* MSVR1 */
141/* NB. DTR / CD swapped from Cirrus spec as the pins are also reversed on the
142 RTA. This is because otherwise DCD would get lost on the 1 parallel / 3
143 serial option.
144*/
145#define RIOC_MSVR1_CD 0x80 /* CD (DSR on Cirrus) */
146#define RIOC_MSVR1_RTS 0x40 /* RTS (CTS on Cirrus) */
147#define RIOC_MSVR1_RI 0x20 /* RI */
148#define RIOC_MSVR1_DTR 0x10 /* DTR (CD on Cirrus) */
149#define RIOC_MSVR1_CTS 0x01 /* CTS output pin (RTS on Cirrus) */
150/* Next two used to indicate state of tbusy and tstop to driver */
151#define RIOC_MSVR1_TSTOP 0x08 /* Set if port flow controlled */
152#define RIOC_MSVR1_TEMPTY 0x04 /* Set if port tx buffer empty */
153
154#define RIOC_MSVR1_HOST 0xf3 /* The bits the host wants */
155
156/* Defines for the subscripts of a CONFIG packet */
157#define RIOC_CONFIG_COR1 1 /* Option register 1 */
158#define RIOC_CONFIG_COR2 2 /* Option register 2 */
159#define RIOC_CONFIG_COR4 3 /* Option register 4 */
160#define RIOC_CONFIG_COR5 4 /* Option register 5 */
161#define RIOC_CONFIG_TXXON 5 /* Tx XON character */
162#define RIOC_CONFIG_TXXOFF 6 /* Tx XOFF character */
163#define RIOC_CONFIG_RXXON 7 /* Rx XON character */
164#define RIOC_CONFIG_RXXOFF 8 /* Rx XOFF character */
165#define RIOC_CONFIG_LNEXT 9 /* LNEXT character */
166#define RIOC_CONFIG_TXBAUD 10 /* Tx baud rate */
167#define RIOC_CONFIG_RXBAUD 11 /* Rx baud rate */
168
169#define RIOC_PRE_EMPTIVE 0x80 /* Pre-emptive bit in command field */
170
171/* Packet types going from Host to remote - with the exception of OPEN, MOPEN,
172 CONFIG, SBREAK and MEMDUMP the remaining bytes of the data array will not
173 be used
174*/
175#define RIOC_OPEN 0x00 /* Open a port */
176#define RIOC_CONFIG 0x01 /* Configure a port */
177#define RIOC_MOPEN 0x02 /* Modem open (block for DCD) */
178#define RIOC_CLOSE 0x03 /* Close a port */
179#define RIOC_WFLUSH (0x04 | RIOC_PRE_EMPTIVE) /* Write flush */
180#define RIOC_RFLUSH (0x05 | RIOC_PRE_EMPTIVE) /* Read flush */
181#define RIOC_RESUME (0x06 | RIOC_PRE_EMPTIVE) /* Resume if xoffed */
182#define RIOC_SBREAK 0x07 /* Start break */
183#define RIOC_EBREAK 0x08 /* End break */
184#define RIOC_SUSPEND (0x09 | RIOC_PRE_EMPTIVE) /* Susp op (behave as tho xoffed) */
185#define RIOC_FCLOSE (0x0a | RIOC_PRE_EMPTIVE) /* Force close */
186#define RIOC_XPRINT 0x0b /* Xprint packet */
187#define RIOC_MBIS (0x0c | RIOC_PRE_EMPTIVE) /* Set modem lines */
188#define RIOC_MBIC (0x0d | RIOC_PRE_EMPTIVE) /* Clear modem lines */
189#define RIOC_MSET (0x0e | RIOC_PRE_EMPTIVE) /* Set modem lines */
190#define RIOC_PCLOSE 0x0f /* Pseudo close - Leaves rx/tx enabled */
191#define RIOC_MGET (0x10 | RIOC_PRE_EMPTIVE) /* Force update of modem status */
192#define RIOC_MEMDUMP (0x11 | RIOC_PRE_EMPTIVE) /* Send back mem from addr supplied */
193#define RIOC_READ_REGISTER (0x12 | RIOC_PRE_EMPTIVE) /* Read CD1400 register (debug) */
194
195/* "Command" packets going from remote to host COMPLETE and MODEM_STATUS
196 use data[4] / data[3] to indicate current state and modem status respectively
197*/
198
199#define RIOC_COMPLETE (0x20 | RIOC_PRE_EMPTIVE)
200 /* Command complete */
201#define RIOC_BREAK_RECEIVED (0x21 | RIOC_PRE_EMPTIVE)
202 /* Break received */
203#define RIOC_MODEM_STATUS (0x22 | RIOC_PRE_EMPTIVE)
204 /* Change in modem status */
205
206/* "Command" packet that could go either way - handshake wake-up */
207#define RIOC_HANDSHAKE (0x23 | RIOC_PRE_EMPTIVE)
208 /* Wake-up to HOST / RTA */
209
210#endif
diff --git a/drivers/char/rio/cmdblk.h b/drivers/char/rio/cmdblk.h
deleted file mode 100644
index 9ed4f861675a..000000000000
--- a/drivers/char/rio/cmdblk.h
+++ /dev/null
@@ -1,53 +0,0 @@
1/*
2** -----------------------------------------------------------------------------
3**
4** Perle Specialix driver for Linux
5** Ported from existing RIO Driver for SCO sources.
6 *
7 * (C) 1990 - 2000 Specialix International Ltd., Byfleet, Surrey, UK.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22**
23** Module : cmdblk.h
24** SID : 1.2
25** Last Modified : 11/6/98 11:34:09
26** Retrieved : 11/6/98 11:34:20
27**
28** ident @(#)cmdblk.h 1.2
29**
30** -----------------------------------------------------------------------------
31*/
32
33#ifndef __rio_cmdblk_h__
34#define __rio_cmdblk_h__
35
36/*
37** the structure of a command block, used to queue commands destined for
38** a rup.
39*/
40
41struct CmdBlk {
42 struct CmdBlk *NextP; /* Pointer to next command block */
43 struct PKT Packet; /* A packet, to copy to the rup */
44 /* The func to call to check if OK */
45 int (*PreFuncP) (unsigned long, struct CmdBlk *);
46 int PreArg; /* The arg for the func */
47 /* The func to call when completed */
48 int (*PostFuncP) (unsigned long, struct CmdBlk *);
49 int PostArg; /* The arg for the func */
50};
51
52#define NUM_RIO_CMD_BLKS (3 * (MAX_RUP * 4 + LINKS_PER_UNIT * 4))
53#endif
diff --git a/drivers/char/rio/cmdpkt.h b/drivers/char/rio/cmdpkt.h
deleted file mode 100644
index c1e7a2798070..000000000000
--- a/drivers/char/rio/cmdpkt.h
+++ /dev/null
@@ -1,177 +0,0 @@
1/*
2** -----------------------------------------------------------------------------
3**
4** Perle Specialix driver for Linux
5** Ported from existing RIO Driver for SCO sources.
6 *
7 * (C) 1990 - 2000 Specialix International Ltd., Byfleet, Surrey, UK.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22**
23** Module : cmdpkt.h
24** SID : 1.2
25** Last Modified : 11/6/98 11:34:09
26** Retrieved : 11/6/98 11:34:20
27**
28** ident @(#)cmdpkt.h 1.2
29**
30** -----------------------------------------------------------------------------
31*/
32#ifndef __rio_cmdpkt_h__
33#define __rio_cmdpkt_h__
34
35/*
36** overlays for the data area of a packet. Used in both directions
37** (to build a packet to send, and to interpret a packet that arrives)
38** and is very inconvenient for MIPS, so they appear as two separate
39** structures - those used for modifying/reading packets on the card
40** and those for modifying/reading packets in real memory, which have an _M
41** suffix.
42*/
43
44#define RTA_BOOT_DATA_SIZE (PKT_MAX_DATA_LEN-2)
45
46/*
47** The boot information packet looks like this:
48** This structure overlays a PktCmd->CmdData structure, and so starts
49** at Data[2] in the actual pkt!
50*/
51struct BootSequence {
52 u16 NumPackets;
53 u16 LoadBase;
54 u16 CodeSize;
55};
56
57#define BOOT_SEQUENCE_LEN 8
58
59struct SamTop {
60 u8 Unit;
61 u8 Link;
62};
63
64struct CmdHdr {
65 u8 PcCommand;
66 union {
67 u8 PcPhbNum;
68 u8 PcLinkNum;
69 u8 PcIDNum;
70 } U0;
71};
72
73
74struct PktCmd {
75 union {
76 struct {
77 struct CmdHdr CmdHdr;
78 struct BootSequence PcBootSequence;
79 } S1;
80 struct {
81 u16 PcSequence;
82 u8 PcBootData[RTA_BOOT_DATA_SIZE];
83 } S2;
84 struct {
85 u16 __crud__;
86 u8 PcUniqNum[4]; /* this is really a uint. */
87 u8 PcModuleTypes; /* what modules are fitted */
88 } S3;
89 struct {
90 struct CmdHdr CmdHdr;
91 u8 __undefined__;
92 u8 PcModemStatus;
93 u8 PcPortStatus;
94 u8 PcSubCommand; /* commands like mem or register dump */
95 u16 PcSubAddr; /* Address for command */
96 u8 PcSubData[64]; /* Date area for command */
97 } S4;
98 struct {
99 struct CmdHdr CmdHdr;
100 u8 PcCommandText[1];
101 u8 __crud__[20];
102 u8 PcIDNum2; /* It had to go somewhere! */
103 } S5;
104 struct {
105 struct CmdHdr CmdHdr;
106 struct SamTop Topology[LINKS_PER_UNIT];
107 } S6;
108 } U1;
109};
110
111struct PktCmd_M {
112 union {
113 struct {
114 struct {
115 u8 PcCommand;
116 union {
117 u8 PcPhbNum;
118 u8 PcLinkNum;
119 u8 PcIDNum;
120 } U0;
121 } CmdHdr;
122 struct {
123 u16 NumPackets;
124 u16 LoadBase;
125 u16 CodeSize;
126 } PcBootSequence;
127 } S1;
128 struct {
129 u16 PcSequence;
130 u8 PcBootData[RTA_BOOT_DATA_SIZE];
131 } S2;
132 struct {
133 u16 __crud__;
134 u8 PcUniqNum[4]; /* this is really a uint. */
135 u8 PcModuleTypes; /* what modules are fitted */
136 } S3;
137 struct {
138 u16 __cmd_hdr__;
139 u8 __undefined__;
140 u8 PcModemStatus;
141 u8 PcPortStatus;
142 u8 PcSubCommand;
143 u16 PcSubAddr;
144 u8 PcSubData[64];
145 } S4;
146 struct {
147 u16 __cmd_hdr__;
148 u8 PcCommandText[1];
149 u8 __crud__[20];
150 u8 PcIDNum2; /* Tacked on end */
151 } S5;
152 struct {
153 u16 __cmd_hdr__;
154 struct Top Topology[LINKS_PER_UNIT];
155 } S6;
156 } U1;
157};
158
159#define Command U1.S1.CmdHdr.PcCommand
160#define PhbNum U1.S1.CmdHdr.U0.PcPhbNum
161#define IDNum U1.S1.CmdHdr.U0.PcIDNum
162#define IDNum2 U1.S5.PcIDNum2
163#define LinkNum U1.S1.CmdHdr.U0.PcLinkNum
164#define Sequence U1.S2.PcSequence
165#define BootData U1.S2.PcBootData
166#define BootSequence U1.S1.PcBootSequence
167#define UniqNum U1.S3.PcUniqNum
168#define ModemStatus U1.S4.PcModemStatus
169#define PortStatus U1.S4.PcPortStatus
170#define SubCommand U1.S4.PcSubCommand
171#define SubAddr U1.S4.PcSubAddr
172#define SubData U1.S4.PcSubData
173#define CommandText U1.S5.PcCommandText
174#define RouteTopology U1.S6.Topology
175#define ModuleTypes U1.S3.PcModuleTypes
176
177#endif
diff --git a/drivers/char/rio/daemon.h b/drivers/char/rio/daemon.h
deleted file mode 100644
index 4af90323fd00..000000000000
--- a/drivers/char/rio/daemon.h
+++ /dev/null
@@ -1,307 +0,0 @@
1/*
2** -----------------------------------------------------------------------------
3**
4** Perle Specialix driver for Linux
5** Ported from existing RIO Driver for SCO sources.
6 *
7 * (C) 1990 - 2000 Specialix International Ltd., Byfleet, Surrey, UK.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22**
23** Module : daemon.h
24** SID : 1.3
25** Last Modified : 11/6/98 11:34:09
26** Retrieved : 11/6/98 11:34:21
27**
28** ident @(#)daemon.h 1.3
29**
30** -----------------------------------------------------------------------------
31*/
32
33#ifndef __rio_daemon_h__
34#define __rio_daemon_h__
35
36
37/*
38** structures used on /dev/rio
39*/
40
41struct Error {
42 unsigned int Error;
43 unsigned int Entry;
44 unsigned int Other;
45};
46
47struct DownLoad {
48 char __user *DataP;
49 unsigned int Count;
50 unsigned int ProductCode;
51};
52
53/*
54** A few constants....
55*/
56#ifndef MAX_VERSION_LEN
57#define MAX_VERSION_LEN 256
58#endif
59
60#ifndef MAX_XP_CTRL_LEN
61#define MAX_XP_CTRL_LEN 16 /* ALSO IN PORT.H */
62#endif
63
64struct PortSetup {
65 unsigned int From; /* Set/Clear XP & IXANY Control from this port.... */
66 unsigned int To; /* .... to this port */
67 unsigned int XpCps; /* at this speed */
68 char XpOn[MAX_XP_CTRL_LEN]; /* this is the start string */
69 char XpOff[MAX_XP_CTRL_LEN]; /* this is the stop string */
70 u8 IxAny; /* enable/disable IXANY */
71 u8 IxOn; /* enable/disable IXON */
72 u8 Lock; /* lock port params */
73 u8 Store; /* store params across closes */
74 u8 Drain; /* close only when drained */
75};
76
77struct LpbReq {
78 unsigned int Host;
79 unsigned int Link;
80 struct LPB __user *LpbP;
81};
82
83struct RupReq {
84 unsigned int HostNum;
85 unsigned int RupNum;
86 struct RUP __user *RupP;
87};
88
89struct PortReq {
90 unsigned int SysPort;
91 struct Port __user *PortP;
92};
93
94struct StreamInfo {
95 unsigned int SysPort;
96 int RQueue;
97 int WQueue;
98};
99
100struct HostReq {
101 unsigned int HostNum;
102 struct Host __user *HostP;
103};
104
105struct HostDpRam {
106 unsigned int HostNum;
107 struct DpRam __user *DpRamP;
108};
109
110struct DebugCtrl {
111 unsigned int SysPort;
112 unsigned int Debug;
113 unsigned int Wait;
114};
115
116struct MapInfo {
117 unsigned int FirstPort; /* 8 ports, starting from this (tty) number */
118 unsigned int RtaUnique; /* reside on this RTA (unique number) */
119};
120
121struct MapIn {
122 unsigned int NumEntries; /* How many port sets are we mapping? */
123 struct MapInfo *MapInfoP; /* Pointer to (user space) info */
124};
125
126struct SendPack {
127 unsigned int PortNum;
128 unsigned char Len;
129 unsigned char Data[PKT_MAX_DATA_LEN];
130};
131
132struct SpecialRupCmd {
133 struct PKT Packet;
134 unsigned short Host;
135 unsigned short RupNum;
136};
137
138struct IdentifyRta {
139 unsigned long RtaUnique;
140 u8 ID;
141};
142
143struct KillNeighbour {
144 unsigned long UniqueNum;
145 u8 Link;
146};
147
148struct rioVersion {
149 char version[MAX_VERSION_LEN];
150 char relid[MAX_VERSION_LEN];
151 int buildLevel;
152 char buildDate[MAX_VERSION_LEN];
153};
154
155
156/*
157** RIOC commands are for the daemon type operations
158**
159** 09.12.1998 ARG - ESIL 0776 part fix
160** Definition for 'RIOC' also appears in rioioctl.h, so we'd better do a
161** #ifndef here first.
162** rioioctl.h also now has #define 'RIO_QUICK_CHECK' as this ioctl is now
163** allowed to be used by customers.
164*/
165#ifndef RIOC
166#define RIOC ('R'<<8)|('i'<<16)|('o'<<24)
167#endif
168
169/*
170** Boot stuff
171*/
172#define RIO_GET_TABLE (RIOC | 100)
173#define RIO_PUT_TABLE (RIOC | 101)
174#define RIO_ASSIGN_RTA (RIOC | 102)
175#define RIO_DELETE_RTA (RIOC | 103)
176#define RIO_HOST_FOAD (RIOC | 104)
177#define RIO_QUICK_CHECK (RIOC | 105)
178#define RIO_SIGNALS_ON (RIOC | 106)
179#define RIO_SIGNALS_OFF (RIOC | 107)
180#define RIO_CHANGE_NAME (RIOC | 108)
181#define RIO_DOWNLOAD (RIOC | 109)
182#define RIO_GET_LOG (RIOC | 110)
183#define RIO_SETUP_PORTS (RIOC | 111)
184#define RIO_ALL_MODEM (RIOC | 112)
185
186/*
187** card state, debug stuff
188*/
189#define RIO_NUM_HOSTS (RIOC | 120)
190#define RIO_HOST_LPB (RIOC | 121)
191#define RIO_HOST_RUP (RIOC | 122)
192#define RIO_HOST_PORT (RIOC | 123)
193#define RIO_PARMS (RIOC | 124)
194#define RIO_HOST_REQ (RIOC | 125)
195#define RIO_READ_CONFIG (RIOC | 126)
196#define RIO_SET_CONFIG (RIOC | 127)
197#define RIO_VERSID (RIOC | 128)
198#define RIO_FLAGS (RIOC | 129)
199#define RIO_SETDEBUG (RIOC | 130)
200#define RIO_GETDEBUG (RIOC | 131)
201#define RIO_READ_LEVELS (RIOC | 132)
202#define RIO_SET_FAST_BUS (RIOC | 133)
203#define RIO_SET_SLOW_BUS (RIOC | 134)
204#define RIO_SET_BYTE_MODE (RIOC | 135)
205#define RIO_SET_WORD_MODE (RIOC | 136)
206#define RIO_STREAM_INFO (RIOC | 137)
207#define RIO_START_POLLER (RIOC | 138)
208#define RIO_STOP_POLLER (RIOC | 139)
209#define RIO_LAST_ERROR (RIOC | 140)
210#define RIO_TICK (RIOC | 141)
211#define RIO_TOCK (RIOC | 241) /* I did this on purpose, you know. */
212#define RIO_SEND_PACKET (RIOC | 142)
213#define RIO_SET_BUSY (RIOC | 143)
214#define SPECIAL_RUP_CMD (RIOC | 144)
215#define RIO_FOAD_RTA (RIOC | 145)
216#define RIO_ZOMBIE_RTA (RIOC | 146)
217#define RIO_IDENTIFY_RTA (RIOC | 147)
218#define RIO_KILL_NEIGHBOUR (RIOC | 148)
219#define RIO_DEBUG_MEM (RIOC | 149)
220/*
221** 150 - 167 used..... See below
222*/
223#define RIO_GET_PORT_SETUP (RIOC | 168)
224#define RIO_RESUME (RIOC | 169)
225#define RIO_MESG (RIOC | 170)
226#define RIO_NO_MESG (RIOC | 171)
227#define RIO_WHAT_MESG (RIOC | 172)
228#define RIO_HOST_DPRAM (RIOC | 173)
229#define RIO_MAP_B50_TO_50 (RIOC | 174)
230#define RIO_MAP_B50_TO_57600 (RIOC | 175)
231#define RIO_MAP_B110_TO_110 (RIOC | 176)
232#define RIO_MAP_B110_TO_115200 (RIOC | 177)
233#define RIO_GET_PORT_PARAMS (RIOC | 178)
234#define RIO_SET_PORT_PARAMS (RIOC | 179)
235#define RIO_GET_PORT_TTY (RIOC | 180)
236#define RIO_SET_PORT_TTY (RIOC | 181)
237#define RIO_SYSLOG_ONLY (RIOC | 182)
238#define RIO_SYSLOG_CONS (RIOC | 183)
239#define RIO_CONS_ONLY (RIOC | 184)
240#define RIO_BLOCK_OPENS (RIOC | 185)
241
242/*
243** 02.03.1999 ARG - ESIL 0820 fix :
244** RIOBootMode is no longer use by the driver, so these ioctls
245** are now obsolete :
246**
247#define RIO_GET_BOOT_MODE (RIOC | 186)
248#define RIO_SET_BOOT_MODE (RIOC | 187)
249**
250*/
251
252#define RIO_MEM_DUMP (RIOC | 189)
253#define RIO_READ_REGISTER (RIOC | 190)
254#define RIO_GET_MODTYPE (RIOC | 191)
255#define RIO_SET_TIMER (RIOC | 192)
256#define RIO_READ_CHECK (RIOC | 196)
257#define RIO_WAITING_FOR_RESTART (RIOC | 197)
258#define RIO_BIND_RTA (RIOC | 198)
259#define RIO_GET_BINDINGS (RIOC | 199)
260#define RIO_PUT_BINDINGS (RIOC | 200)
261
262#define RIO_MAKE_DEV (RIOC | 201)
263#define RIO_MINOR (RIOC | 202)
264
265#define RIO_IDENTIFY_DRIVER (RIOC | 203)
266#define RIO_DISPLAY_HOST_CFG (RIOC | 204)
267
268
269/*
270** MAKE_DEV / MINOR stuff
271*/
272#define RIO_DEV_DIRECT 0x0000
273#define RIO_DEV_MODEM 0x0200
274#define RIO_DEV_XPRINT 0x0400
275#define RIO_DEV_MASK 0x0600
276
277/*
278** port management, xprint stuff
279*/
280#define rIOCN(N) (RIOC|(N))
281#define rIOCR(N,T) (RIOC|(N))
282#define rIOCW(N,T) (RIOC|(N))
283
284#define RIO_GET_XP_ON rIOCR(150,char[16]) /* start xprint string */
285#define RIO_SET_XP_ON rIOCW(151,char[16])
286#define RIO_GET_XP_OFF rIOCR(152,char[16]) /* finish xprint string */
287#define RIO_SET_XP_OFF rIOCW(153,char[16])
288#define RIO_GET_XP_CPS rIOCR(154,int) /* xprint CPS */
289#define RIO_SET_XP_CPS rIOCW(155,int)
290#define RIO_GET_IXANY rIOCR(156,int) /* ixany allowed? */
291#define RIO_SET_IXANY rIOCW(157,int)
292#define RIO_SET_IXANY_ON rIOCN(158) /* allow ixany */
293#define RIO_SET_IXANY_OFF rIOCN(159) /* disallow ixany */
294#define RIO_GET_MODEM rIOCR(160,int) /* port is modem/direct line? */
295#define RIO_SET_MODEM rIOCW(161,int)
296#define RIO_SET_MODEM_ON rIOCN(162) /* port is a modem */
297#define RIO_SET_MODEM_OFF rIOCN(163) /* port is direct */
298#define RIO_GET_IXON rIOCR(164,int) /* ixon allowed? */
299#define RIO_SET_IXON rIOCW(165,int)
300#define RIO_SET_IXON_ON rIOCN(166) /* allow ixon */
301#define RIO_SET_IXON_OFF rIOCN(167) /* disallow ixon */
302
303#define RIO_GET_SIVIEW ((('s')<<8) | 106) /* backwards compatible with SI */
304
305#define RIO_IOCTL_UNKNOWN -2
306
307#endif
diff --git a/drivers/char/rio/errors.h b/drivers/char/rio/errors.h
deleted file mode 100644
index bdb05234090a..000000000000
--- a/drivers/char/rio/errors.h
+++ /dev/null
@@ -1,98 +0,0 @@
1/*
2** -----------------------------------------------------------------------------
3**
4** Perle Specialix driver for Linux
5** Ported from existing RIO Driver for SCO sources.
6 *
7 * (C) 1990 - 2000 Specialix International Ltd., Byfleet, Surrey, UK.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22**
23** Module : errors.h
24** SID : 1.2
25** Last Modified : 11/6/98 11:34:10
26** Retrieved : 11/6/98 11:34:21
27**
28** ident @(#)errors.h 1.2
29**
30** -----------------------------------------------------------------------------
31*/
32
33#ifndef __rio_errors_h__
34#define __rio_errors_h__
35
36/*
37** error codes
38*/
39
40#define NOTHING_WRONG_AT_ALL 0
41#define BAD_CHARACTER_IN_NAME 1
42#define TABLE_ENTRY_ISNT_PROPERLY_NULL 2
43#define UNKNOWN_HOST_NUMBER 3
44#define ZERO_RTA_ID 4
45#define BAD_RTA_ID 5
46#define DUPLICATED_RTA_ID 6
47#define DUPLICATE_UNIQUE_NUMBER 7
48#define BAD_TTY_NUMBER 8
49#define TTY_NUMBER_IN_USE 9
50#define NAME_USED_TWICE 10
51#define HOST_ID_NOT_ZERO 11
52#define BOOT_IN_PROGRESS 12
53#define COPYIN_FAILED 13
54#define HOST_FILE_TOO_LARGE 14
55#define COPYOUT_FAILED 15
56#define NOT_SUPER_USER 16
57#define RIO_ALREADY_POLLING 17
58
59#define ID_NUMBER_OUT_OF_RANGE 18
60#define PORT_NUMBER_OUT_OF_RANGE 19
61#define HOST_NUMBER_OUT_OF_RANGE 20
62#define RUP_NUMBER_OUT_OF_RANGE 21
63#define TTY_NUMBER_OUT_OF_RANGE 22
64#define LINK_NUMBER_OUT_OF_RANGE 23
65
66#define HOST_NOT_RUNNING 24
67#define IOCTL_COMMAND_UNKNOWN 25
68#define RIO_SYSTEM_HALTED 26
69#define WAIT_FOR_DRAIN_BROKEN 27
70#define PORT_NOT_MAPPED_INTO_SYSTEM 28
71#define EXCLUSIVE_USE_SET 29
72#define WAIT_FOR_NOT_CLOSING_BROKEN 30
73#define WAIT_FOR_PORT_TO_OPEN_BROKEN 31
74#define WAIT_FOR_CARRIER_BROKEN 32
75#define WAIT_FOR_NOT_IN_USE_BROKEN 33
76#define WAIT_FOR_CAN_ADD_COMMAND_BROKEN 34
77#define WAIT_FOR_ADD_COMMAND_BROKEN 35
78#define WAIT_FOR_NOT_PARAM_BROKEN 36
79#define WAIT_FOR_RETRY_BROKEN 37
80#define HOST_HAS_ALREADY_BEEN_BOOTED 38
81#define UNIT_IS_IN_USE 39
82#define COULDNT_FIND_ENTRY 40
83#define RTA_UNIQUE_NUMBER_ZERO 41
84#define CLOSE_COMMAND_FAILED 42
85#define WAIT_FOR_CLOSE_BROKEN 43
86#define CPS_VALUE_OUT_OF_RANGE 44
87#define ID_ALREADY_IN_USE 45
88#define SIGNALS_ALREADY_SET 46
89#define NOT_RECEIVING_PROCESS 47
90#define RTA_NUMBER_WRONG 48
91#define NO_SUCH_PRODUCT 49
92#define HOST_SYSPORT_BAD 50
93#define ID_NOT_TENTATIVE 51
94#define XPRINT_CPS_OUT_OF_RANGE 52
95#define NOT_ENOUGH_CORE_FOR_PCI_COPY 53
96
97
98#endif /* __rio_errors_h__ */
diff --git a/drivers/char/rio/func.h b/drivers/char/rio/func.h
deleted file mode 100644
index 078d44f85e45..000000000000
--- a/drivers/char/rio/func.h
+++ /dev/null
@@ -1,143 +0,0 @@
1/*
2** -----------------------------------------------------------------------------
3**
4** Perle Specialix driver for Linux
5** Ported from existing RIO Driver for SCO sources.
6 *
7 * (C) 1990 - 2000 Specialix International Ltd., Byfleet, Surrey, UK.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22**
23** Module : func.h
24** SID : 1.3
25** Last Modified : 11/6/98 11:34:10
26** Retrieved : 11/6/98 11:34:21
27**
28** ident @(#)func.h 1.3
29**
30** -----------------------------------------------------------------------------
31*/
32
33#ifndef __func_h_def
34#define __func_h_def
35
36#include <linux/kdev_t.h>
37
38/* rioboot.c */
39int RIOBootCodeRTA(struct rio_info *, struct DownLoad *);
40int RIOBootCodeHOST(struct rio_info *, struct DownLoad *);
41int RIOBootCodeUNKNOWN(struct rio_info *, struct DownLoad *);
42void msec_timeout(struct Host *);
43int RIOBootRup(struct rio_info *, unsigned int, struct Host *, struct PKT __iomem *);
44int RIOBootOk(struct rio_info *, struct Host *, unsigned long);
45int RIORtaBound(struct rio_info *, unsigned int);
46void rio_fill_host_slot(int, int, unsigned int, struct Host *);
47
48/* riocmd.c */
49int RIOFoadRta(struct Host *, struct Map *);
50int RIOZombieRta(struct Host *, struct Map *);
51int RIOCommandRta(struct rio_info *, unsigned long, int (*func) (struct Host *, struct Map *));
52int RIOIdentifyRta(struct rio_info *, void __user *);
53int RIOKillNeighbour(struct rio_info *, void __user *);
54int RIOSuspendBootRta(struct Host *, int, int);
55int RIOFoadWakeup(struct rio_info *);
56struct CmdBlk *RIOGetCmdBlk(void);
57void RIOFreeCmdBlk(struct CmdBlk *);
58int RIOQueueCmdBlk(struct Host *, unsigned int, struct CmdBlk *);
59void RIOPollHostCommands(struct rio_info *, struct Host *);
60int RIOWFlushMark(unsigned long, struct CmdBlk *);
61int RIORFlushEnable(unsigned long, struct CmdBlk *);
62int RIOUnUse(unsigned long, struct CmdBlk *);
63
64/* rioctrl.c */
65int riocontrol(struct rio_info *, dev_t, int, unsigned long, int);
66
67int RIOPreemptiveCmd(struct rio_info *, struct Port *, unsigned char);
68
69/* rioinit.c */
70void rioinit(struct rio_info *, struct RioHostInfo *);
71void RIOInitHosts(struct rio_info *, struct RioHostInfo *);
72void RIOISAinit(struct rio_info *, int);
73int RIODoAT(struct rio_info *, int, int);
74caddr_t RIOCheckForATCard(int);
75int RIOAssignAT(struct rio_info *, int, void __iomem *, int);
76int RIOBoardTest(unsigned long, void __iomem *, unsigned char, int);
77void RIOAllocDataStructs(struct rio_info *);
78void RIOSetupDataStructs(struct rio_info *);
79int RIODefaultName(struct rio_info *, struct Host *, unsigned int);
80struct rioVersion *RIOVersid(void);
81void RIOHostReset(unsigned int, struct DpRam __iomem *, unsigned int);
82
83/* riointr.c */
84void RIOTxEnable(char *);
85void RIOServiceHost(struct rio_info *, struct Host *);
86int riotproc(struct rio_info *, struct ttystatics *, int, int);
87
88/* rioparam.c */
89int RIOParam(struct Port *, int, int, int);
90int RIODelay(struct Port *PortP, int);
91int RIODelay_ni(struct Port *PortP, int);
92void ms_timeout(struct Port *);
93int can_add_transmit(struct PKT __iomem **, struct Port *);
94void add_transmit(struct Port *);
95void put_free_end(struct Host *, struct PKT __iomem *);
96int can_remove_receive(struct PKT __iomem **, struct Port *);
97void remove_receive(struct Port *);
98
99/* rioroute.c */
100int RIORouteRup(struct rio_info *, unsigned int, struct Host *, struct PKT __iomem *);
101void RIOFixPhbs(struct rio_info *, struct Host *, unsigned int);
102unsigned int GetUnitType(unsigned int);
103int RIOSetChange(struct rio_info *);
104int RIOFindFreeID(struct rio_info *, struct Host *, unsigned int *, unsigned int *);
105
106
107/* riotty.c */
108
109int riotopen(struct tty_struct *tty, struct file *filp);
110int riotclose(void *ptr);
111int riotioctl(struct rio_info *, struct tty_struct *, int, caddr_t);
112void ttyseth(struct Port *, struct ttystatics *, struct old_sgttyb *sg);
113
114/* riotable.c */
115int RIONewTable(struct rio_info *);
116int RIOApel(struct rio_info *);
117int RIODeleteRta(struct rio_info *, struct Map *);
118int RIOAssignRta(struct rio_info *, struct Map *);
119int RIOReMapPorts(struct rio_info *, struct Host *, struct Map *);
120int RIOChangeName(struct rio_info *, struct Map *);
121
122#if 0
123/* riodrvr.c */
124struct rio_info *rio_install(struct RioHostInfo *);
125int rio_uninstall(struct rio_info *);
126int rio_open(struct rio_info *, int, struct file *);
127int rio_close(struct rio_info *, struct file *);
128int rio_read(struct rio_info *, struct file *, char *, int);
129int rio_write(struct rio_info *, struct file *f, char *, int);
130int rio_ioctl(struct rio_info *, struct file *, int, char *);
131int rio_select(struct rio_info *, struct file *f, int, struct sel *);
132int rio_intr(char *);
133int rio_isr_thread(char *);
134struct rio_info *rio_info_store(int cmd, struct rio_info *p);
135#endif
136
137extern void rio_copy_to_card(void *from, void __iomem *to, int len);
138extern int rio_minor(struct tty_struct *tty);
139extern int rio_ismodem(struct tty_struct *tty);
140
141extern void rio_start_card_running(struct Host *HostP);
142
143#endif /* __func_h_def */
diff --git a/drivers/char/rio/host.h b/drivers/char/rio/host.h
deleted file mode 100644
index 78f24540c224..000000000000
--- a/drivers/char/rio/host.h
+++ /dev/null
@@ -1,123 +0,0 @@
1/*
2** -----------------------------------------------------------------------------
3**
4** Perle Specialix driver for Linux
5** Ported from existing RIO Driver for SCO sources.
6 *
7 * (C) 1990 - 2000 Specialix International Ltd., Byfleet, Surrey, UK.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22**
23** Module : host.h
24** SID : 1.2
25** Last Modified : 11/6/98 11:34:10
26** Retrieved : 11/6/98 11:34:21
27**
28** ident @(#)host.h 1.2
29**
30** -----------------------------------------------------------------------------
31*/
32
33#ifndef __rio_host_h__
34#define __rio_host_h__
35
36/*
37** the host structure - one per host card in the system.
38*/
39
40#define MAX_EXTRA_UNITS 64
41
42/*
43** Host data structure. This is used for the software equiv. of
44** the host.
45*/
46struct Host {
47 struct pci_dev *pdev;
48 unsigned char Type; /* RIO_EISA, RIO_MCA, ... */
49 unsigned char Ivec; /* POLLED or ivec number */
50 unsigned char Mode; /* Control stuff */
51 unsigned char Slot; /* Slot */
52 void __iomem *Caddr; /* KV address of DPRAM */
53 struct DpRam __iomem *CardP; /* KV address of DPRAM, with overlay */
54 unsigned long PaddrP; /* Phys. address of DPRAM */
55 char Name[MAX_NAME_LEN]; /* The name of the host */
56 unsigned int UniqueNum; /* host unique number */
57 spinlock_t HostLock; /* Lock structure for MPX */
58 unsigned int WorkToBeDone; /* set to true each interrupt */
59 unsigned int InIntr; /* Being serviced? */
60 unsigned int IntSrvDone; /* host's interrupt has been serviced */
61 void (*Copy) (void *, void __iomem *, int); /* copy func */
62 struct timer_list timer;
63 /*
64 ** I M P O R T A N T !
65 **
66 ** The rest of this data structure is cleared to zero after
67 ** a RIO_HOST_FOAD command.
68 */
69
70 unsigned long Flags; /* Whats going down */
71#define RC_WAITING 0
72#define RC_STARTUP 1
73#define RC_RUNNING 2
74#define RC_STUFFED 3
75#define RC_READY 7
76#define RUN_STATE 7
77/*
78** Boot mode applies to the way in which hosts in this system will
79** boot RTAs
80*/
81#define RC_BOOT_ALL 0x8 /* Boot all RTAs attached */
82#define RC_BOOT_OWN 0x10 /* Only boot RTAs bound to this system */
83#define RC_BOOT_NONE 0x20 /* Don't boot any RTAs (slave mode) */
84
85 struct Top Topology[LINKS_PER_UNIT]; /* one per link */
86 struct Map Mapping[MAX_RUP]; /* Mappings for host */
87 struct PHB __iomem *PhbP; /* Pointer to the PHB array */
88 unsigned short __iomem *PhbNumP; /* Ptr to Number of PHB's */
89 struct LPB __iomem *LinkStrP; /* Link Structure Array */
90 struct RUP __iomem *RupP; /* Sixteen real rups here */
91 struct PARM_MAP __iomem *ParmMapP; /* points to the parmmap */
92 unsigned int ExtraUnits[MAX_EXTRA_UNITS]; /* unknown things */
93 unsigned int NumExtraBooted; /* how many of the above */
94 /*
95 ** Twenty logical rups.
96 ** The first sixteen are the real Rup entries (above), the last four
97 ** are the link RUPs.
98 */
99 struct UnixRup UnixRups[MAX_RUP + LINKS_PER_UNIT];
100 int timeout_id; /* For calling 100 ms delays */
101 int timeout_sem; /* For calling 100 ms delays */
102 unsigned long locks; /* long req'd for set_bit --RR */
103 char ____end_marker____;
104};
105#define Control CardP->DpControl
106#define SetInt CardP->DpSetInt
107#define ResetTpu CardP->DpResetTpu
108#define ResetInt CardP->DpResetInt
109#define Signature CardP->DpSignature
110#define Sram1 CardP->DpSram1
111#define Sram2 CardP->DpSram2
112#define Sram3 CardP->DpSram3
113#define Scratch CardP->DpScratch
114#define __ParmMapR CardP->DpParmMapR
115#define SLX CardP->DpSlx
116#define Revision CardP->DpRevision
117#define Unique CardP->DpUnique
118#define Year CardP->DpYear
119#define Week CardP->DpWeek
120
121#define RIO_DUMBPARM 0x0860 /* what not to expect */
122
123#endif
diff --git a/drivers/char/rio/link.h b/drivers/char/rio/link.h
deleted file mode 100644
index f3bf11a04d41..000000000000
--- a/drivers/char/rio/link.h
+++ /dev/null
@@ -1,96 +0,0 @@
1/****************************************************************************
2 ******* *******
3 ******* L I N K
4 ******* *******
5 ****************************************************************************
6
7 Author : Ian Nandhra / Jeremy Rolls
8 Date :
9
10 *
11 * (C) 1990 - 2000 Specialix International Ltd., Byfleet, Surrey, UK.
12 *
13 * 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.
17 *
18 * 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.
22 *
23 * 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
27 Version : 0.01
28
29
30 Mods
31 ----------------------------------------------------------------------------
32 Date By Description
33 ----------------------------------------------------------------------------
34
35 ***************************************************************************/
36
37#ifndef _link_h
38#define _link_h 1
39
40/*************************************************
41 * Define the Link Status stuff
42 ************************************************/
43/* Boot request stuff */
44#define BOOT_REQUEST ((ushort) 0) /* Request for a boot */
45#define BOOT_ABORT ((ushort) 1) /* Abort a boot */
46#define BOOT_SEQUENCE ((ushort) 2) /* Packet with the number of packets
47 and load address */
48#define BOOT_COMPLETED ((ushort) 3) /* Boot completed */
49
50
51struct LPB {
52 u16 link_number; /* Link Number */
53 u16 in_ch; /* Link In Channel */
54 u16 out_ch; /* Link Out Channel */
55 u8 attached_serial[4]; /* Attached serial number */
56 u8 attached_host_serial[4];
57 /* Serial number of Host who
58 booted the other end */
59 u16 descheduled; /* Currently Descheduled */
60 u16 state; /* Current state */
61 u16 send_poll; /* Send a Poll Packet */
62 u16 ltt_p; /* Process Descriptor */
63 u16 lrt_p; /* Process Descriptor */
64 u16 lrt_status; /* Current lrt status */
65 u16 ltt_status; /* Current ltt status */
66 u16 timeout; /* Timeout value */
67 u16 topology; /* Topology bits */
68 u16 mon_ltt;
69 u16 mon_lrt;
70 u16 WaitNoBoot; /* Secs to hold off booting */
71 u16 add_packet_list; /* Add packets to here */
72 u16 remove_packet_list; /* Send packets from here */
73
74 u16 lrt_fail_chan; /* Lrt's failure channel */
75 u16 ltt_fail_chan; /* Ltt's failure channel */
76
77 /* RUP structure for HOST to driver communications */
78 struct RUP rup;
79 struct RUP link_rup; /* RUP for the link (POLL,
80 topology etc.) */
81 u16 attached_link; /* Number of attached link */
82 u16 csum_errors; /* csum errors */
83 u16 num_disconnects; /* number of disconnects */
84 u16 num_sync_rcvd; /* # sync's received */
85 u16 num_sync_rqst; /* # sync requests */
86 u16 num_tx; /* Num pkts sent */
87 u16 num_rx; /* Num pkts received */
88 u16 module_attached; /* Module tpyes of attached */
89 u16 led_timeout; /* LED timeout */
90 u16 first_port; /* First port to service */
91 u16 last_port; /* Last port to service */
92};
93
94#endif
95
96/*********** end of file ***********/
diff --git a/drivers/char/rio/linux_compat.h b/drivers/char/rio/linux_compat.h
deleted file mode 100644
index 34c0d2899ef1..000000000000
--- a/drivers/char/rio/linux_compat.h
+++ /dev/null
@@ -1,77 +0,0 @@
1/*
2 * (C) 2000 R.E.Wolff@BitWizard.nl
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18
19#include <linux/interrupt.h>
20
21
22#define DEBUG_ALL
23
24struct ttystatics {
25 struct termios tm;
26};
27
28extern int rio_debug;
29
30#define RIO_DEBUG_INIT 0x000001
31#define RIO_DEBUG_BOOT 0x000002
32#define RIO_DEBUG_CMD 0x000004
33#define RIO_DEBUG_CTRL 0x000008
34#define RIO_DEBUG_INTR 0x000010
35#define RIO_DEBUG_PARAM 0x000020
36#define RIO_DEBUG_ROUTE 0x000040
37#define RIO_DEBUG_TABLE 0x000080
38#define RIO_DEBUG_TTY 0x000100
39#define RIO_DEBUG_FLOW 0x000200
40#define RIO_DEBUG_MODEMSIGNALS 0x000400
41#define RIO_DEBUG_PROBE 0x000800
42#define RIO_DEBUG_CLEANUP 0x001000
43#define RIO_DEBUG_IFLOW 0x002000
44#define RIO_DEBUG_PFE 0x004000
45#define RIO_DEBUG_REC 0x008000
46#define RIO_DEBUG_SPINLOCK 0x010000
47#define RIO_DEBUG_DELAY 0x020000
48#define RIO_DEBUG_MOD_COUNT 0x040000
49
50
51/* Copied over from riowinif.h . This is ugly. The winif file declares
52also much other stuff which is incompatible with the headers from
53the older driver. The older driver includes "brates.h" which shadows
54the definitions from Linux, and is incompatible... */
55
56/* RxBaud and TxBaud definitions... */
57#define RIO_B0 0x00 /* RTS / DTR signals dropped */
58#define RIO_B50 0x01 /* 50 baud */
59#define RIO_B75 0x02 /* 75 baud */
60#define RIO_B110 0x03 /* 110 baud */
61#define RIO_B134 0x04 /* 134.5 baud */
62#define RIO_B150 0x05 /* 150 baud */
63#define RIO_B200 0x06 /* 200 baud */
64#define RIO_B300 0x07 /* 300 baud */
65#define RIO_B600 0x08 /* 600 baud */
66#define RIO_B1200 0x09 /* 1200 baud */
67#define RIO_B1800 0x0A /* 1800 baud */
68#define RIO_B2400 0x0B /* 2400 baud */
69#define RIO_B4800 0x0C /* 4800 baud */
70#define RIO_B9600 0x0D /* 9600 baud */
71#define RIO_B19200 0x0E /* 19200 baud */
72#define RIO_B38400 0x0F /* 38400 baud */
73#define RIO_B56000 0x10 /* 56000 baud */
74#define RIO_B57600 0x11 /* 57600 baud */
75#define RIO_B64000 0x12 /* 64000 baud */
76#define RIO_B115200 0x13 /* 115200 baud */
77#define RIO_B2000 0x14 /* 2000 baud */
diff --git a/drivers/char/rio/map.h b/drivers/char/rio/map.h
deleted file mode 100644
index 8366978578c1..000000000000
--- a/drivers/char/rio/map.h
+++ /dev/null
@@ -1,98 +0,0 @@
1/*
2** -----------------------------------------------------------------------------
3**
4** Perle Specialix driver for Linux
5** Ported from existing RIO Driver for SCO sources.
6 *
7 * (C) 1990 - 2000 Specialix International Ltd., Byfleet, Surrey, UK.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22**
23** Module : map.h
24** SID : 1.2
25** Last Modified : 11/6/98 11:34:11
26** Retrieved : 11/6/98 11:34:21
27**
28** ident @(#)map.h 1.2
29**
30** -----------------------------------------------------------------------------
31*/
32
33#ifndef __rio_map_h__
34#define __rio_map_h__
35
36/*
37** mapping structure passed to and from the config.rio program to
38** determine the current topology of the world
39*/
40
41#define MAX_MAP_ENTRY 17
42#define TOTAL_MAP_ENTRIES (MAX_MAP_ENTRY*RIO_SLOTS)
43#define MAX_NAME_LEN 32
44
45struct Map {
46 unsigned int HostUniqueNum; /* Supporting hosts unique number */
47 unsigned int RtaUniqueNum; /* Unique number */
48 /*
49 ** The next two IDs must be swapped on big-endian architectures
50 ** when using a v2.04 /etc/rio/config with a v3.00 driver (when
51 ** upgrading for example).
52 */
53 unsigned short ID; /* ID used in the subnet */
54 unsigned short ID2; /* ID of 2nd block of 8 for 16 port */
55 unsigned long Flags; /* Booted, ID Given, Disconnected */
56 unsigned long SysPort; /* First tty mapped to this port */
57 struct Top Topology[LINKS_PER_UNIT]; /* ID connected to each link */
58 char Name[MAX_NAME_LEN]; /* Cute name by which RTA is known */
59};
60
61/*
62** Flag values:
63*/
64#define RTA_BOOTED 0x00000001
65#define RTA_NEWBOOT 0x00000010
66#define MSG_DONE 0x00000020
67#define RTA_INTERCONNECT 0x00000040
68#define RTA16_SECOND_SLOT 0x00000080
69#define BEEN_HERE 0x00000100
70#define SLOT_TENTATIVE 0x40000000
71#define SLOT_IN_USE 0x80000000
72
73/*
74** HostUniqueNum is the unique number from the host card that this RTA
75** is to be connected to.
76** RtaUniqueNum is the unique number of the RTA concerned. It will be ZERO
77** if the slot in the table is unused. If it is the same as the HostUniqueNum
78** then this slot represents a host card.
79** Flags contains current boot/route state info
80** SysPort is a value in the range 0-504, being the number of the first tty
81** on this RTA. Each RTA supports 8 ports. The SysPort value must be modulo 8.
82** SysPort 0-127 correspond to /dev/ttyr001 to /dev/ttyr128, with minor
83** numbers 0-127. SysPort 128-255 correspond to /dev/ttyr129 to /dev/ttyr256,
84** again with minor numbers 0-127, and so on for SysPorts 256-383 and 384-511
85** ID will be in the range 0-16 for a `known' RTA. ID will be 0xFFFF for an
86** unused slot/unknown ID etc.
87** The Topology array contains the ID of the unit connected to each of the
88** four links on this unit. The entry will be 0xFFFF if NOTHING is connected
89** to the link, or will be 0xFF00 if an UNKNOWN unit is connected to the link.
90** The Name field is a null-terminated string, upto 31 characters, containing
91** the 'cute' name that the sysadmin/users know the RTA by. It is permissible
92** for this string to contain any character in the range \040 to \176 inclusive.
93** In particular, ctrl sequences and DEL (0x7F, \177) are not allowed. The
94** special character '%' IS allowable, and needs no special action.
95**
96*/
97
98#endif
diff --git a/drivers/char/rio/param.h b/drivers/char/rio/param.h
deleted file mode 100644
index 7e9b6283e8aa..000000000000
--- a/drivers/char/rio/param.h
+++ /dev/null
@@ -1,55 +0,0 @@
1/*
2** -----------------------------------------------------------------------------
3**
4** Perle Specialix driver for Linux
5** Ported from existing RIO Driver for SCO sources.
6 *
7 * (C) 1990 - 2000 Specialix International Ltd., Byfleet, Surrey, UK.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22**
23** Module : param.h
24** SID : 1.2
25** Last Modified : 11/6/98 11:34:12
26** Retrieved : 11/6/98 11:34:21
27**
28** ident @(#)param.h 1.2
29**
30** -----------------------------------------------------------------------------
31*/
32
33#ifndef __rio_param_h__
34#define __rio_param_h__
35
36/*
37** the param command block, as used in OPEN and PARAM calls.
38*/
39
40struct phb_param {
41 u8 Cmd; /* It is very important that these line up */
42 u8 Cor1; /* with what is expected at the other end. */
43 u8 Cor2; /* to confirm that you've got it right, */
44 u8 Cor4; /* check with cirrus/cirrus.h */
45 u8 Cor5;
46 u8 TxXon; /* Transmit X-On character */
47 u8 TxXoff; /* Transmit X-Off character */
48 u8 RxXon; /* Receive X-On character */
49 u8 RxXoff; /* Receive X-Off character */
50 u8 LNext; /* Literal-next character */
51 u8 TxBaud; /* Transmit baudrate */
52 u8 RxBaud; /* Receive baudrate */
53};
54
55#endif
diff --git a/drivers/char/rio/parmmap.h b/drivers/char/rio/parmmap.h
deleted file mode 100644
index acc8fa439df5..000000000000
--- a/drivers/char/rio/parmmap.h
+++ /dev/null
@@ -1,81 +0,0 @@
1/****************************************************************************
2 ******* *******
3 ******* H O S T M E M O R Y M A P
4 ******* *******
5 ****************************************************************************
6
7 Author : Ian Nandhra / Jeremy Rolls
8 Date :
9
10 *
11 * (C) 1990 - 2000 Specialix International Ltd., Byfleet, Surrey, UK.
12 *
13 * 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.
17 *
18 * 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.
22 *
23 * 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
27 Version : 0.01
28
29
30 Mods
31 ----------------------------------------------------------------------------
32 Date By Description
33 ----------------------------------------------------------------------------
346/4/1991 jonb Made changes to accommodate Mips R3230 bus
35 ***************************************************************************/
36
37#ifndef _parmap_h
38#define _parmap_h
39
40typedef struct PARM_MAP PARM_MAP;
41
42struct PARM_MAP {
43 u16 phb_ptr; /* Pointer to the PHB array */
44 u16 phb_num_ptr; /* Ptr to Number of PHB's */
45 u16 free_list; /* Free List pointer */
46 u16 free_list_end; /* Free List End pointer */
47 u16 q_free_list_ptr; /* Ptr to Q_BUF variable */
48 u16 unit_id_ptr; /* Unit Id */
49 u16 link_str_ptr; /* Link Structure Array */
50 u16 bootloader_1; /* 1st Stage Boot Loader */
51 u16 bootloader_2; /* 2nd Stage Boot Loader */
52 u16 port_route_map_ptr; /* Port Route Map */
53 u16 route_ptr; /* Unit Route Map */
54 u16 map_present; /* Route Map present */
55 s16 pkt_num; /* Total number of packets */
56 s16 q_num; /* Total number of Q packets */
57 u16 buffers_per_port; /* Number of buffers per port */
58 u16 heap_size; /* Initial size of heap */
59 u16 heap_left; /* Current Heap left */
60 u16 error; /* Error code */
61 u16 tx_max; /* Max number of tx pkts per phb */
62 u16 rx_max; /* Max number of rx pkts per phb */
63 u16 rx_limit; /* For high / low watermarks */
64 s16 links; /* Links to use */
65 s16 timer; /* Interrupts per second */
66 u16 rups; /* Pointer to the RUPs */
67 u16 max_phb; /* Mostly for debugging */
68 u16 living; /* Just increments!! */
69 u16 init_done; /* Initialisation over */
70 u16 booting_link;
71 u16 idle_count; /* Idle time counter */
72 u16 busy_count; /* Busy counter */
73 u16 idle_control; /* Control Idle Process */
74 u16 tx_intr; /* TX interrupt pending */
75 u16 rx_intr; /* RX interrupt pending */
76 u16 rup_intr; /* RUP interrupt pending */
77};
78
79#endif
80
81/*********** end of file ***********/
diff --git a/drivers/char/rio/pci.h b/drivers/char/rio/pci.h
deleted file mode 100644
index 6032f9135956..000000000000
--- a/drivers/char/rio/pci.h
+++ /dev/null
@@ -1,72 +0,0 @@
1/*
2** -----------------------------------------------------------------------------
3**
4** Perle Specialix driver for Linux
5** Ported from existing RIO Driver for SCO sources.
6 *
7 * (C) 1990 - 2000 Specialix International Ltd., Byfleet, Surrey, UK.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22**
23** Module : pci.h
24** SID : 1.2
25** Last Modified : 11/6/98 11:34:12
26** Retrieved : 11/6/98 11:34:21
27**
28** ident @(#)pci.h 1.2
29**
30** -----------------------------------------------------------------------------
31*/
32
33#ifndef __rio_pci_h__
34#define __rio_pci_h__
35
36/*
37** PCI stuff
38*/
39
40#define PCITpFastClock 0x80
41#define PCITpSlowClock 0x00
42#define PCITpFastLinks 0x40
43#define PCITpSlowLinks 0x00
44#define PCITpIntEnable 0x04
45#define PCITpIntDisable 0x00
46#define PCITpBusEnable 0x02
47#define PCITpBusDisable 0x00
48#define PCITpBootFromRam 0x01
49#define PCITpBootFromLink 0x00
50
51#define RIO_PCI_VENDOR 0x11CB
52#define RIO_PCI_DEVICE 0x8000
53#define RIO_PCI_BASE_CLASS 0x02
54#define RIO_PCI_SUB_CLASS 0x80
55#define RIO_PCI_PROG_IFACE 0x00
56
57#define RIO_PCI_RID 0x0008
58#define RIO_PCI_BADR0 0x0010
59#define RIO_PCI_INTLN 0x003C
60#define RIO_PCI_INTPIN 0x003D
61
62#define RIO_PCI_MEM_SIZE 65536
63
64#define RIO_PCI_TURBO_TP 0x80
65#define RIO_PCI_FAST_LINKS 0x40
66#define RIO_PCI_INT_ENABLE 0x04
67#define RIO_PCI_TP_BUS_ENABLE 0x02
68#define RIO_PCI_BOOT_FROM_RAM 0x01
69
70#define RIO_PCI_DEFAULT_MODE 0x05
71
72#endif /* __rio_pci_h__ */
diff --git a/drivers/char/rio/phb.h b/drivers/char/rio/phb.h
deleted file mode 100644
index a4c48ae4e365..000000000000
--- a/drivers/char/rio/phb.h
+++ /dev/null
@@ -1,142 +0,0 @@
1/****************************************************************************
2 ******* *******
3 ******* P H B H E A D E R *******
4 ******* *******
5 ****************************************************************************
6
7 Author : Ian Nandhra, Jeremy Rolls
8 Date :
9
10 *
11 * (C) 1990 - 2000 Specialix International Ltd., Byfleet, Surrey, UK.
12 *
13 * 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.
17 *
18 * 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.
22 *
23 * 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
27 Version : 0.01
28
29
30 Mods
31 ----------------------------------------------------------------------------
32 Date By Description
33 ----------------------------------------------------------------------------
34
35 ***************************************************************************/
36
37#ifndef _phb_h
38#define _phb_h 1
39
40/*************************************************
41 * Handshake asserted. Deasserted by the LTT(s)
42 ************************************************/
43#define PHB_HANDSHAKE_SET ((ushort) 0x001) /* Set by LRT */
44
45#define PHB_HANDSHAKE_RESET ((ushort) 0x002) /* Set by ISR / driver */
46
47#define PHB_HANDSHAKE_FLAGS (PHB_HANDSHAKE_RESET | PHB_HANDSHAKE_SET)
48 /* Reset by ltt */
49
50
51/*************************************************
52 * Maximum number of PHB's
53 ************************************************/
54#define MAX_PHB ((ushort) 128) /* range 0-127 */
55
56/*************************************************
57 * Defines for the mode fields
58 ************************************************/
59#define TXPKT_INCOMPLETE 0x0001 /* Previous tx packet not completed */
60#define TXINTR_ENABLED 0x0002 /* Tx interrupt is enabled */
61#define TX_TAB3 0x0004 /* TAB3 mode */
62#define TX_OCRNL 0x0008 /* OCRNL mode */
63#define TX_ONLCR 0x0010 /* ONLCR mode */
64#define TX_SENDSPACES 0x0020 /* Send n spaces command needs
65 completing */
66#define TX_SENDNULL 0x0040 /* Escaping NULL needs completing */
67#define TX_SENDLF 0x0080 /* LF -> CR LF needs completing */
68#define TX_PARALLELBUG 0x0100 /* CD1400 LF -> CR LF bug on parallel
69 port */
70#define TX_HANGOVER (TX_SENDSPACES | TX_SENDLF | TX_SENDNULL)
71#define TX_DTRFLOW 0x0200 /* DTR tx flow control */
72#define TX_DTRFLOWED 0x0400 /* DTR is low - don't allow more data
73 into the FIFO */
74#define TX_DATAINFIFO 0x0800 /* There is data in the FIFO */
75#define TX_BUSY 0x1000 /* Data in FIFO, shift or holding regs */
76
77#define RX_SPARE 0x0001 /* SPARE */
78#define RXINTR_ENABLED 0x0002 /* Rx interrupt enabled */
79#define RX_ICRNL 0x0008 /* ICRNL mode */
80#define RX_INLCR 0x0010 /* INLCR mode */
81#define RX_IGNCR 0x0020 /* IGNCR mode */
82#define RX_CTSFLOW 0x0040 /* CTSFLOW enabled */
83#define RX_IXOFF 0x0080 /* IXOFF enabled */
84#define RX_CTSFLOWED 0x0100 /* CTSFLOW and CTS dropped */
85#define RX_IXOFFED 0x0200 /* IXOFF and xoff sent */
86#define RX_BUFFERED 0x0400 /* Try and pass on complete packets */
87
88#define PORT_ISOPEN 0x0001 /* Port open? */
89#define PORT_HUPCL 0x0002 /* Hangup on close? */
90#define PORT_MOPENPEND 0x0004 /* Modem open pending */
91#define PORT_ISPARALLEL 0x0008 /* Parallel port */
92#define PORT_BREAK 0x0010 /* Port on break */
93#define PORT_STATUSPEND 0x0020 /* Status packet pending */
94#define PORT_BREAKPEND 0x0040 /* Break packet pending */
95#define PORT_MODEMPEND 0x0080 /* Modem status packet pending */
96#define PORT_PARALLELBUG 0x0100 /* CD1400 LF -> CR LF bug on parallel
97 port */
98#define PORT_FULLMODEM 0x0200 /* Full modem signals */
99#define PORT_RJ45 0x0400 /* RJ45 connector - no RI signal */
100#define PORT_RESTRICTED 0x0600 /* Restricted connector - no RI / DTR */
101
102#define PORT_MODEMBITS 0x0600 /* Mask for modem fields */
103
104#define PORT_WCLOSE 0x0800 /* Waiting for close */
105#define PORT_HANDSHAKEFIX 0x1000 /* Port has H/W flow control fix */
106#define PORT_WASPCLOSED 0x2000 /* Port closed with PCLOSE */
107#define DUMPMODE 0x4000 /* Dump RTA mem */
108#define READ_REG 0x8000 /* Read CD1400 register */
109
110
111
112/**************************************************************************
113 * PHB Structure
114 * A few words.
115 *
116 * Normally Packets are added to the end of the list and removed from
117 * the start. The pointer tx_add points to a SPACE to put a Packet.
118 * The pointer tx_remove points to the next Packet to remove
119 *************************************************************************/
120
121struct PHB {
122 u8 source;
123 u8 handshake;
124 u8 status;
125 u16 timeout; /* Maximum of 1.9 seconds */
126 u8 link; /* Send down this link */
127 u8 destination;
128 u16 tx_start;
129 u16 tx_end;
130 u16 tx_add;
131 u16 tx_remove;
132
133 u16 rx_start;
134 u16 rx_end;
135 u16 rx_add;
136 u16 rx_remove;
137
138};
139
140#endif
141
142/*********** end of file ***********/
diff --git a/drivers/char/rio/pkt.h b/drivers/char/rio/pkt.h
deleted file mode 100644
index a9458164f02f..000000000000
--- a/drivers/char/rio/pkt.h
+++ /dev/null
@@ -1,77 +0,0 @@
1/****************************************************************************
2 ******* *******
3 ******* P A C K E T H E A D E R F I L E
4 ******* *******
5 ****************************************************************************
6
7 Author : Ian Nandhra / Jeremy Rolls
8 Date :
9
10 *
11 * (C) 1990 - 2000 Specialix International Ltd., Byfleet, Surrey, UK.
12 *
13 * 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.
17 *
18 * 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.
22 *
23 * 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
27 Version : 0.01
28
29
30 Mods
31 ----------------------------------------------------------------------------
32 Date By Description
33 ----------------------------------------------------------------------------
34
35 ***************************************************************************/
36
37#ifndef _pkt_h
38#define _pkt_h 1
39
40#define PKT_CMD_BIT ((ushort) 0x080)
41#define PKT_CMD_DATA ((ushort) 0x080)
42
43#define PKT_ACK ((ushort) 0x040)
44
45#define PKT_TGL ((ushort) 0x020)
46
47#define PKT_LEN_MASK ((ushort) 0x07f)
48
49#define DATA_WNDW ((ushort) 0x10)
50#define PKT_TTL_MASK ((ushort) 0x0f)
51
52#define PKT_MAX_DATA_LEN 72
53
54#define PKT_LENGTH sizeof(struct PKT)
55#define SYNC_PKT_LENGTH (PKT_LENGTH + 4)
56
57#define CONTROL_PKT_LEN_MASK PKT_LEN_MASK
58#define CONTROL_PKT_CMD_BIT PKT_CMD_BIT
59#define CONTROL_PKT_ACK (PKT_ACK << 8)
60#define CONTROL_PKT_TGL (PKT_TGL << 8)
61#define CONTROL_PKT_TTL_MASK (PKT_TTL_MASK << 8)
62#define CONTROL_DATA_WNDW (DATA_WNDW << 8)
63
64struct PKT {
65 u8 dest_unit; /* Destination Unit Id */
66 u8 dest_port; /* Destination POrt */
67 u8 src_unit; /* Source Unit Id */
68 u8 src_port; /* Source POrt */
69 u8 len;
70 u8 control;
71 u8 data[PKT_MAX_DATA_LEN];
72 /* Actual data :-) */
73 u16 csum; /* C-SUM */
74};
75#endif
76
77/*********** end of file ***********/
diff --git a/drivers/char/rio/port.h b/drivers/char/rio/port.h
deleted file mode 100644
index 49cf6d15ee54..000000000000
--- a/drivers/char/rio/port.h
+++ /dev/null
@@ -1,179 +0,0 @@
1/*
2** -----------------------------------------------------------------------------
3**
4** Perle Specialix driver for Linux
5** Ported from existing RIO Driver for SCO sources.
6 *
7 * (C) 1990 - 2000 Specialix International Ltd., Byfleet, Surrey, UK.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22**
23** Module : port.h
24** SID : 1.3
25** Last Modified : 11/6/98 11:34:12
26** Retrieved : 11/6/98 11:34:21
27**
28** ident @(#)port.h 1.3
29**
30** -----------------------------------------------------------------------------
31*/
32
33#ifndef __rio_port_h__
34#define __rio_port_h__
35
36/*
37** Port data structure
38*/
39struct Port {
40 struct gs_port gs;
41 int PortNum; /* RIO port no., 0-511 */
42 struct Host *HostP;
43 void __iomem *Caddr;
44 unsigned short HostPort; /* Port number on host card */
45 unsigned char RupNum; /* Number of RUP for port */
46 unsigned char ID2; /* Second ID of RTA for port */
47 unsigned long State; /* FLAGS for open & xopen */
48#define RIO_LOPEN 0x00001 /* Local open */
49#define RIO_MOPEN 0x00002 /* Modem open */
50#define RIO_WOPEN 0x00004 /* Waiting for open */
51#define RIO_CLOSING 0x00008 /* The port is being close */
52#define RIO_XPBUSY 0x00010 /* Transparent printer busy */
53#define RIO_BREAKING 0x00020 /* Break in progress */
54#define RIO_DIRECT 0x00040 /* Doing Direct output */
55#define RIO_EXCLUSIVE 0x00080 /* Stream open for exclusive use */
56#define RIO_NDELAY 0x00100 /* Stream is open FNDELAY */
57#define RIO_CARR_ON 0x00200 /* Stream has carrier present */
58#define RIO_XPWANTR 0x00400 /* Stream wanted by Xprint */
59#define RIO_RBLK 0x00800 /* Stream is read-blocked */
60#define RIO_BUSY 0x01000 /* Stream is BUSY for write */
61#define RIO_TIMEOUT 0x02000 /* Stream timeout in progress */
62#define RIO_TXSTOP 0x04000 /* Stream output is stopped */
63#define RIO_WAITFLUSH 0x08000 /* Stream waiting for flush */
64#define RIO_DYNOROD 0x10000 /* Drain failed */
65#define RIO_DELETED 0x20000 /* RTA has been deleted */
66#define RIO_ISSCANCODE 0x40000 /* This line is in scancode mode */
67#define RIO_USING_EUC 0x100000 /* Using extended Unix chars */
68#define RIO_CAN_COOK 0x200000 /* This line can do cooking */
69#define RIO_TRIAD_MODE 0x400000 /* Enable TRIAD special ops. */
70#define RIO_TRIAD_BLOCK 0x800000 /* Next read will block */
71#define RIO_TRIAD_FUNC 0x1000000 /* Seen a function key coming in */
72#define RIO_THROTTLE_RX 0x2000000 /* RX needs to be throttled. */
73
74 unsigned long Config; /* FLAGS for NOREAD.... */
75#define RIO_NOREAD 0x0001 /* Are not allowed to read port */
76#define RIO_NOWRITE 0x0002 /* Are not allowed to write port */
77#define RIO_NOXPRINT 0x0004 /* Are not allowed to xprint port */
78#define RIO_NOMASK 0x0007 /* All not allowed things */
79#define RIO_IXANY 0x0008 /* Port is allowed ixany */
80#define RIO_MODEM 0x0010 /* Stream is a modem device */
81#define RIO_IXON 0x0020 /* Port is allowed ixon */
82#define RIO_WAITDRAIN 0x0040 /* Wait for port to completely drain */
83#define RIO_MAP_50_TO_50 0x0080 /* Map 50 baud to 50 baud */
84#define RIO_MAP_110_TO_110 0x0100 /* Map 110 baud to 110 baud */
85
86/*
87** 15.10.1998 ARG - ESIL 0761 prt fix
88** As LynxOS does not appear to support Hardware Flow Control .....
89** Define our own flow control flags in 'Config'.
90*/
91#define RIO_CTSFLOW 0x0200 /* RIO's own CTSFLOW flag */
92#define RIO_RTSFLOW 0x0400 /* RIO's own RTSFLOW flag */
93
94
95 struct PHB __iomem *PhbP; /* pointer to PHB for port */
96 u16 __iomem *TxAdd; /* Add packets here */
97 u16 __iomem *TxStart; /* Start of add array */
98 u16 __iomem *TxEnd; /* End of add array */
99 u16 __iomem *RxRemove; /* Remove packets here */
100 u16 __iomem *RxStart; /* Start of remove array */
101 u16 __iomem *RxEnd; /* End of remove array */
102 unsigned int RtaUniqueNum; /* Unique number of RTA */
103 unsigned short PortState; /* status of port */
104 unsigned short ModemState; /* status of modem lines */
105 unsigned long ModemLines; /* Modem bits sent to RTA */
106 unsigned char CookMode; /* who expands CR/LF? */
107 unsigned char ParamSem; /* Prevent write during param */
108 unsigned char Mapped; /* if port mapped onto host */
109 unsigned char SecondBlock; /* if port belongs to 2nd block
110 of 16 port RTA */
111 unsigned char InUse; /* how many pre-emptive cmds */
112 unsigned char Lock; /* if params locked */
113 unsigned char Store; /* if params stored across closes */
114 unsigned char FirstOpen; /* TRUE if first time port opened */
115 unsigned char FlushCmdBodge; /* if doing a (non)flush */
116 unsigned char MagicFlags; /* require intr processing */
117#define MAGIC_FLUSH 0x01 /* mirror of WflushFlag */
118#define MAGIC_REBOOT 0x02 /* RTA re-booted, re-open ports */
119#define MORE_OUTPUT_EYGOR 0x04 /* riotproc failed to empty clists */
120 unsigned char WflushFlag; /* 1 How many WFLUSHs active */
121/*
122** Transparent print stuff
123*/
124 struct Xprint {
125#ifndef MAX_XP_CTRL_LEN
126#define MAX_XP_CTRL_LEN 16 /* ALSO IN DAEMON.H */
127#endif
128 unsigned int XpCps;
129 char XpOn[MAX_XP_CTRL_LEN];
130 char XpOff[MAX_XP_CTRL_LEN];
131 unsigned short XpLen; /* strlen(XpOn)+strlen(XpOff) */
132 unsigned char XpActive;
133 unsigned char XpLastTickOk; /* TRUE if we can process */
134#define XP_OPEN 00001
135#define XP_RUNABLE 00002
136 struct ttystatics *XttyP;
137 } Xprint;
138 unsigned char RxDataStart;
139 unsigned char Cor2Copy; /* copy of COR2 */
140 char *Name; /* points to the Rta's name */
141 char *TxRingBuffer;
142 unsigned short TxBufferIn; /* New data arrives here */
143 unsigned short TxBufferOut; /* Intr removes data here */
144 unsigned short OldTxBufferOut; /* Indicates if draining */
145 int TimeoutId; /* Timeout ID */
146 unsigned int Debug;
147 unsigned char WaitUntilBooted; /* True if open should block */
148 unsigned int statsGather; /* True if gathering stats */
149 unsigned long txchars; /* Chars transmitted */
150 unsigned long rxchars; /* Chars received */
151 unsigned long opens; /* port open count */
152 unsigned long closes; /* port close count */
153 unsigned long ioctls; /* ioctl count */
154 unsigned char LastRxTgl; /* Last state of rx toggle bit */
155 spinlock_t portSem; /* Lock using this sem */
156 int MonitorTstate; /* Monitoring ? */
157 int timeout_id; /* For calling 100 ms delays */
158 int timeout_sem; /* For calling 100 ms delays */
159 int firstOpen; /* First time open ? */
160 char *p; /* save the global struc here .. */
161};
162
163struct ModuleInfo {
164 char *Name;
165 unsigned int Flags[4]; /* one per port on a module */
166};
167
168/*
169** This struct is required because trying to grab an entire Port structure
170** runs into problems with differing struct sizes between driver and config.
171*/
172struct PortParams {
173 unsigned int Port;
174 unsigned long Config;
175 unsigned long State;
176 struct ttystatics *TtyP;
177};
178
179#endif
diff --git a/drivers/char/rio/protsts.h b/drivers/char/rio/protsts.h
deleted file mode 100644
index 8ab79401d3ee..000000000000
--- a/drivers/char/rio/protsts.h
+++ /dev/null
@@ -1,110 +0,0 @@
1/****************************************************************************
2 ******* *******
3 ******* P R O T O C O L S T A T U S S T R U C T U R E *******
4 ******* *******
5 ****************************************************************************
6
7 Author : Ian Nandhra / Jeremy Rolls
8 Date :
9
10 *
11 * (C) 1990 - 2000 Specialix International Ltd., Byfleet, Surrey, UK.
12 *
13 * 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.
17 *
18 * 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.
22 *
23 * 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
27 Version : 0.01
28
29
30 Mods
31 ----------------------------------------------------------------------------
32 Date By Description
33 ----------------------------------------------------------------------------
34
35 ***************************************************************************/
36
37#ifndef _protsts_h
38#define _protsts_h 1
39
40/*************************************************
41 * ACK bit. Last Packet received OK. Set by
42 * rxpkt to indicate that the Packet has been
43 * received OK and that the LTT must set the ACK
44 * bit in the next outward bound Packet
45 * and re-set by LTT's after xmit.
46 *
47 * Gets shoved into rx_status
48 ************************************************/
49#define PHB_RX_LAST_PKT_ACKED ((ushort) 0x080)
50
51/*******************************************************
52 * The Rx TOGGLE bit.
53 * Stuffed into rx_status by RXPKT
54 ******************************************************/
55#define PHB_RX_DATA_WNDW ((ushort) 0x040)
56
57/*******************************************************
58 * The Rx TOGGLE bit. Matches the setting in PKT.H
59 * Stuffed into rx_status
60 ******************************************************/
61#define PHB_RX_TGL ((ushort) 0x2000)
62
63
64/*************************************************
65 * This bit is set by the LRT to indicate that
66 * an ACK (packet) must be returned.
67 *
68 * Gets shoved into tx_status
69 ************************************************/
70#define PHB_TX_SEND_PKT_ACK ((ushort) 0x08)
71
72/*************************************************
73 * Set by LTT to indicate that an ACK is required
74 *************************************************/
75#define PHB_TX_ACK_RQRD ((ushort) 0x01)
76
77
78/*******************************************************
79 * The Tx TOGGLE bit.
80 * Stuffed into tx_status by RXPKT from the PKT WndW
81 * field. Looked by the LTT when the NEXT Packet
82 * is going to be sent.
83 ******************************************************/
84#define PHB_TX_DATA_WNDW ((ushort) 0x04)
85
86
87/*******************************************************
88 * The Tx TOGGLE bit. Matches the setting in PKT.H
89 * Stuffed into tx_status
90 ******************************************************/
91#define PHB_TX_TGL ((ushort) 0x02)
92
93/*******************************************************
94 * Request intr bit. Set when the queue has gone quiet
95 * and the PHB has requested an interrupt.
96 ******************************************************/
97#define PHB_TX_INTR ((ushort) 0x100)
98
99/*******************************************************
100 * SET if the PHB cannot send any more data down the
101 * Link
102 ******************************************************/
103#define PHB_TX_HANDSHAKE ((ushort) 0x010)
104
105
106#define RUP_SEND_WNDW ((ushort) 0x08) ;
107
108#endif
109
110/*********** end of file ***********/
diff --git a/drivers/char/rio/rio.h b/drivers/char/rio/rio.h
deleted file mode 100644
index 1bf36223a4e8..000000000000
--- a/drivers/char/rio/rio.h
+++ /dev/null
@@ -1,208 +0,0 @@
1/*
2** -----------------------------------------------------------------------------
3**
4** Perle Specialix driver for Linux
5** Ported from existing RIO Driver for SCO sources.
6 *
7 * (C) 1990 - 1998 Specialix International Ltd., Byfleet, Surrey, UK.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22**
23** Module : rio.h
24** SID : 1.3
25** Last Modified : 11/6/98 11:34:13
26** Retrieved : 11/6/98 11:34:22
27**
28** ident @(#)rio.h 1.3
29**
30** -----------------------------------------------------------------------------
31*/
32
33#ifndef __rio_rio_h__
34#define __rio_rio_h__
35
36/*
37** Maximum numbers of things
38*/
39#define RIO_SLOTS 4 /* number of configuration slots */
40#define RIO_HOSTS 4 /* number of hosts that can be found */
41#define PORTS_PER_HOST 128 /* number of ports per host */
42#define LINKS_PER_UNIT 4 /* number of links from a host */
43#define RIO_PORTS (PORTS_PER_HOST * RIO_HOSTS) /* max. no. of ports */
44#define RTAS_PER_HOST (MAX_RUP) /* number of RTAs per host */
45#define PORTS_PER_RTA (PORTS_PER_HOST/RTAS_PER_HOST) /* ports on a rta */
46#define PORTS_PER_MODULE 4 /* number of ports on a plug-in module */
47 /* number of modules on an RTA */
48#define MODULES_PER_RTA (PORTS_PER_RTA/PORTS_PER_MODULE)
49#define MAX_PRODUCT 16 /* numbr of different product codes */
50#define MAX_MODULE_TYPES 16 /* number of different types of module */
51
52#define RIO_CONTROL_DEV 128 /* minor number of host/control device */
53#define RIO_INVALID_MAJOR 0 /* test first host card's major no for validity */
54
55/*
56** number of RTAs that can be bound to a master
57*/
58#define MAX_RTA_BINDINGS (MAX_RUP * RIO_HOSTS)
59
60/*
61** Unit types
62*/
63#define PC_RTA16 0x90000000
64#define PC_RTA8 0xe0000000
65#define TYPE_HOST 0
66#define TYPE_RTA8 1
67#define TYPE_RTA16 2
68
69/*
70** Flag values returned by functions
71*/
72
73#define RIO_FAIL -1
74
75/*
76** SysPort value for something that hasn't any ports
77*/
78#define NO_PORT 0xFFFFFFFF
79
80/*
81** Unit ID Of all hosts
82*/
83#define HOST_ID 0
84
85/*
86** Break bytes into nybles
87*/
88#define LONYBLE(X) ((X) & 0xF)
89#define HINYBLE(X) (((X)>>4) & 0xF)
90
91/*
92** Flag values passed into some functions
93*/
94#define DONT_SLEEP 0
95#define OK_TO_SLEEP 1
96
97#define DONT_PRINT 1
98#define DO_PRINT 0
99
100#define PRINT_TO_LOG_CONS 0
101#define PRINT_TO_CONS 1
102#define PRINT_TO_LOG 2
103
104/*
105** Timeout has trouble with times of less than 3 ticks...
106*/
107#define MIN_TIMEOUT 3
108
109/*
110** Generally useful constants
111*/
112
113#define HUNDRED_MS ((HZ/10)?(HZ/10):1)
114#define ONE_MEG 0x100000
115#define SIXTY_FOUR_K 0x10000
116
117#define RIO_AT_MEM_SIZE SIXTY_FOUR_K
118#define RIO_EISA_MEM_SIZE SIXTY_FOUR_K
119#define RIO_MCA_MEM_SIZE SIXTY_FOUR_K
120
121#define COOK_WELL 0
122#define COOK_MEDIUM 1
123#define COOK_RAW 2
124
125/*
126** Pointer manipulation stuff
127** RIO_PTR takes hostp->Caddr and the offset into the DP RAM area
128** and produces a UNIX caddr_t (pointer) to the object
129** RIO_OBJ takes hostp->Caddr and a UNIX pointer to an object and
130** returns the offset into the DP RAM area.
131*/
132#define RIO_PTR(C,O) (((unsigned char __iomem *)(C))+(0xFFFF&(O)))
133#define RIO_OFF(C,O) ((unsigned char __iomem *)(O)-(unsigned char __iomem *)(C))
134
135/*
136** How to convert from various different device number formats:
137** DEV is a dev number, as passed to open, close etc - NOT a minor
138** number!
139**/
140
141#define RIO_MODEM_MASK 0x1FF
142#define RIO_MODEM_BIT 0x200
143#define RIO_UNMODEM(DEV) (MINOR(DEV) & RIO_MODEM_MASK)
144#define RIO_ISMODEM(DEV) (MINOR(DEV) & RIO_MODEM_BIT)
145#define RIO_PORT(DEV,FIRST_MAJ) ( (MAJOR(DEV) - FIRST_MAJ) * PORTS_PER_HOST) \
146 + MINOR(DEV)
147#define CSUM(pkt_ptr) (((u16 *)(pkt_ptr))[0] + ((u16 *)(pkt_ptr))[1] + \
148 ((u16 *)(pkt_ptr))[2] + ((u16 *)(pkt_ptr))[3] + \
149 ((u16 *)(pkt_ptr))[4] + ((u16 *)(pkt_ptr))[5] + \
150 ((u16 *)(pkt_ptr))[6] + ((u16 *)(pkt_ptr))[7] + \
151 ((u16 *)(pkt_ptr))[8] + ((u16 *)(pkt_ptr))[9] )
152
153#define RIO_LINK_ENABLE 0x80FF /* FF is a hack, mainly for Mips, to */
154 /* prevent a really stupid race condition. */
155
156#define NOT_INITIALISED 0
157#define INITIALISED 1
158
159#define NOT_POLLING 0
160#define POLLING 1
161
162#define NOT_CHANGED 0
163#define CHANGED 1
164
165#define NOT_INUSE 0
166
167#define DISCONNECT 0
168#define CONNECT 1
169
170/* ------ Control Codes ------ */
171
172#define CONTROL '^'
173#define IFOAD ( CONTROL + 1 )
174#define IDENTIFY ( CONTROL + 2 )
175#define ZOMBIE ( CONTROL + 3 )
176#define UFOAD ( CONTROL + 4 )
177#define IWAIT ( CONTROL + 5 )
178
179#define IFOAD_MAGIC 0xF0AD /* of course */
180#define ZOMBIE_MAGIC (~0xDEAD) /* not dead -> zombie */
181#define UFOAD_MAGIC 0xD1E /* kill-your-neighbour */
182#define IWAIT_MAGIC 0xB1DE /* Bide your time */
183
184/* ------ Error Codes ------ */
185
186#define E_NO_ERROR ((ushort) 0)
187
188/* ------ Free Lists ------ */
189
190struct rio_free_list {
191 u16 next;
192 u16 prev;
193};
194
195/* NULL for card side linked lists */
196#define TPNULL ((ushort)(0x8000))
197/* We can add another packet to a transmit queue if the packet pointer pointed
198 * to by the TxAdd pointer has PKT_IN_USE clear in its address. */
199#define PKT_IN_USE 0x1
200
201/* ------ Topology ------ */
202
203struct Top {
204 u8 Unit;
205 u8 Link;
206};
207
208#endif /* __rio_h__ */
diff --git a/drivers/char/rio/rio_linux.c b/drivers/char/rio/rio_linux.c
deleted file mode 100644
index d58c2eb07f07..000000000000
--- a/drivers/char/rio/rio_linux.c
+++ /dev/null
@@ -1,1202 +0,0 @@
1
2/* rio_linux.c -- Linux driver for the Specialix RIO series cards.
3 *
4 *
5 * (C) 1999 R.E.Wolff@BitWizard.nl
6 *
7 * Specialix pays for the development and support of this driver.
8 * Please DO contact support@specialix.co.uk if you require
9 * support. But please read the documentation (rio.txt) first.
10 *
11 *
12 *
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License as
15 * published by the Free Software Foundation; either version 2 of
16 * the License, or (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be
19 * useful, but WITHOUT ANY WARRANTY; without even the implied
20 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
21 * PURPOSE. See the GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public
24 * License along with this program; if not, write to the Free
25 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,
26 * USA.
27 *
28 * */
29
30#include <linux/module.h>
31#include <linux/kdev_t.h>
32#include <asm/io.h>
33#include <linux/kernel.h>
34#include <linux/sched.h>
35#include <linux/ioport.h>
36#include <linux/interrupt.h>
37#include <linux/errno.h>
38#include <linux/tty.h>
39#include <linux/tty_flip.h>
40#include <linux/mm.h>
41#include <linux/serial.h>
42#include <linux/fcntl.h>
43#include <linux/major.h>
44#include <linux/delay.h>
45#include <linux/pci.h>
46#include <linux/slab.h>
47#include <linux/smp_lock.h>
48#include <linux/miscdevice.h>
49#include <linux/init.h>
50
51#include <linux/generic_serial.h>
52#include <asm/uaccess.h>
53
54#include "linux_compat.h"
55#include "pkt.h"
56#include "daemon.h"
57#include "rio.h"
58#include "riospace.h"
59#include "cmdpkt.h"
60#include "map.h"
61#include "rup.h"
62#include "port.h"
63#include "riodrvr.h"
64#include "rioinfo.h"
65#include "func.h"
66#include "errors.h"
67#include "pci.h"
68
69#include "parmmap.h"
70#include "unixrup.h"
71#include "board.h"
72#include "host.h"
73#include "phb.h"
74#include "link.h"
75#include "cmdblk.h"
76#include "route.h"
77#include "cirrus.h"
78#include "rioioctl.h"
79#include "param.h"
80#include "protsts.h"
81#include "rioboard.h"
82
83
84#include "rio_linux.h"
85
86/* I don't think that this driver can handle more than 512 ports on
87one machine. Specialix specifies max 4 boards in one machine. I don't
88know why. If you want to try anyway you'll have to increase the number
89of boards in rio.h. You'll have to allocate more majors if you need
90more than 512 ports.... */
91
92#ifndef RIO_NORMAL_MAJOR0
93/* This allows overriding on the compiler commandline, or in a "major.h"
94 include or something like that */
95#define RIO_NORMAL_MAJOR0 154
96#define RIO_NORMAL_MAJOR1 156
97#endif
98
99#ifndef PCI_DEVICE_ID_SPECIALIX_SX_XIO_IO8
100#define PCI_DEVICE_ID_SPECIALIX_SX_XIO_IO8 0x2000
101#endif
102
103#ifndef RIO_WINDOW_LEN
104#define RIO_WINDOW_LEN 0x10000
105#endif
106
107
108/* Configurable options:
109 (Don't be too sure that it'll work if you toggle them) */
110
111/* Am I paranoid or not ? ;-) */
112#undef RIO_PARANOIA_CHECK
113
114
115/* 20 -> 2000 per second. The card should rate-limit interrupts at 1000
116 Hz, but it is user configurable. I don't recommend going above 1000
117 Hz. The interrupt ratelimit might trigger if the interrupt is
118 shared with a very active other device.
119 undef this if you want to disable the check....
120*/
121#define IRQ_RATE_LIMIT 200
122
123
124/* These constants are derived from SCO Source */
125static struct Conf
126 RIOConf = {
127 /* locator */ "RIO Config here",
128 /* startuptime */ HZ * 2,
129 /* how long to wait for card to run */
130 /* slowcook */ 0,
131 /* TRUE -> always use line disc. */
132 /* intrpolltime */ 1,
133 /* The frequency of OUR polls */
134 /* breakinterval */ 25,
135 /* x10 mS XXX: units seem to be 1ms not 10! -- REW */
136 /* timer */ 10,
137 /* mS */
138 /* RtaLoadBase */ 0x7000,
139 /* HostLoadBase */ 0x7C00,
140 /* XpHz */ 5,
141 /* number of Xprint hits per second */
142 /* XpCps */ 120,
143 /* Xprint characters per second */
144 /* XpOn */ "\033d#",
145 /* start Xprint for a wyse 60 */
146 /* XpOff */ "\024",
147 /* end Xprint for a wyse 60 */
148 /* MaxXpCps */ 2000,
149 /* highest Xprint speed */
150 /* MinXpCps */ 10,
151 /* slowest Xprint speed */
152 /* SpinCmds */ 1,
153 /* non-zero for mega fast boots */
154 /* First Addr */ 0x0A0000,
155 /* First address to look at */
156 /* Last Addr */ 0xFF0000,
157 /* Last address looked at */
158 /* BufferSize */ 1024,
159 /* Bytes per port of buffering */
160 /* LowWater */ 256,
161 /* how much data left before wakeup */
162 /* LineLength */ 80,
163 /* how wide is the console? */
164 /* CmdTimeout */ HZ,
165 /* how long a close command may take */
166};
167
168
169
170
171/* Function prototypes */
172
173static void rio_disable_tx_interrupts(void *ptr);
174static void rio_enable_tx_interrupts(void *ptr);
175static void rio_disable_rx_interrupts(void *ptr);
176static void rio_enable_rx_interrupts(void *ptr);
177static int rio_carrier_raised(struct tty_port *port);
178static void rio_shutdown_port(void *ptr);
179static int rio_set_real_termios(void *ptr);
180static void rio_hungup(void *ptr);
181static void rio_close(void *ptr);
182static int rio_chars_in_buffer(void *ptr);
183static long rio_fw_ioctl(struct file *filp, unsigned int cmd, unsigned long arg);
184static int rio_init_drivers(void);
185
186static void my_hd(void *addr, int len);
187
188static struct tty_driver *rio_driver, *rio_driver2;
189
190/* The name "p" is a bit non-descript. But that's what the rio-lynxos
191sources use all over the place. */
192struct rio_info *p;
193
194int rio_debug;
195
196
197/* You can have the driver poll your card.
198 - Set rio_poll to 1 to poll every timer tick (10ms on Intel).
199 This is used when the card cannot use an interrupt for some reason.
200*/
201static int rio_poll = 1;
202
203
204/* These are the only open spaces in my computer. Yours may have more
205 or less.... */
206static int rio_probe_addrs[] = { 0xc0000, 0xd0000, 0xe0000 };
207
208#define NR_RIO_ADDRS ARRAY_SIZE(rio_probe_addrs)
209
210
211/* Set the mask to all-ones. This alas, only supports 32 interrupts.
212 Some architectures may need more. -- Changed to LONG to
213 support up to 64 bits on 64bit architectures. -- REW 20/06/99 */
214static long rio_irqmask = -1;
215
216MODULE_AUTHOR("Rogier Wolff <R.E.Wolff@bitwizard.nl>, Patrick van de Lageweg <patrick@bitwizard.nl>");
217MODULE_DESCRIPTION("RIO driver");
218MODULE_LICENSE("GPL");
219module_param(rio_poll, int, 0);
220module_param(rio_debug, int, 0644);
221module_param(rio_irqmask, long, 0);
222
223static struct real_driver rio_real_driver = {
224 rio_disable_tx_interrupts,
225 rio_enable_tx_interrupts,
226 rio_disable_rx_interrupts,
227 rio_enable_rx_interrupts,
228 rio_shutdown_port,
229 rio_set_real_termios,
230 rio_chars_in_buffer,
231 rio_close,
232 rio_hungup,
233 NULL
234};
235
236/*
237 * Firmware loader driver specific routines
238 *
239 */
240
241static const struct file_operations rio_fw_fops = {
242 .owner = THIS_MODULE,
243 .unlocked_ioctl = rio_fw_ioctl,
244};
245
246static struct miscdevice rio_fw_device = {
247 RIOCTL_MISC_MINOR, "rioctl", &rio_fw_fops
248};
249
250
251
252
253
254#ifdef RIO_PARANOIA_CHECK
255
256/* This doesn't work. Who's paranoid around here? Not me! */
257
258static inline int rio_paranoia_check(struct rio_port const *port, char *name, const char *routine)
259{
260
261 static const char *badmagic = KERN_ERR "rio: Warning: bad rio port magic number for device %s in %s\n";
262 static const char *badinfo = KERN_ERR "rio: Warning: null rio port for device %s in %s\n";
263
264 if (!port) {
265 printk(badinfo, name, routine);
266 return 1;
267 }
268 if (port->magic != RIO_MAGIC) {
269 printk(badmagic, name, routine);
270 return 1;
271 }
272
273 return 0;
274}
275#else
276#define rio_paranoia_check(a,b,c) 0
277#endif
278
279
280#ifdef DEBUG
281static void my_hd(void *ad, int len)
282{
283 int i, j, ch;
284 unsigned char *addr = ad;
285
286 for (i = 0; i < len; i += 16) {
287 rio_dprintk(RIO_DEBUG_PARAM, "%08lx ", (unsigned long) addr + i);
288 for (j = 0; j < 16; j++) {
289 rio_dprintk(RIO_DEBUG_PARAM, "%02x %s", addr[j + i], (j == 7) ? " " : "");
290 }
291 for (j = 0; j < 16; j++) {
292 ch = addr[j + i];
293 rio_dprintk(RIO_DEBUG_PARAM, "%c", (ch < 0x20) ? '.' : ((ch > 0x7f) ? '.' : ch));
294 }
295 rio_dprintk(RIO_DEBUG_PARAM, "\n");
296 }
297}
298#else
299#define my_hd(ad,len) do{/* nothing*/ } while (0)
300#endif
301
302
303/* Delay a number of jiffies, allowing a signal to interrupt */
304int RIODelay(struct Port *PortP, int njiffies)
305{
306 func_enter();
307
308 rio_dprintk(RIO_DEBUG_DELAY, "delaying %d jiffies\n", njiffies);
309 msleep_interruptible(jiffies_to_msecs(njiffies));
310 func_exit();
311
312 if (signal_pending(current))
313 return RIO_FAIL;
314 else
315 return !RIO_FAIL;
316}
317
318
319/* Delay a number of jiffies, disallowing a signal to interrupt */
320int RIODelay_ni(struct Port *PortP, int njiffies)
321{
322 func_enter();
323
324 rio_dprintk(RIO_DEBUG_DELAY, "delaying %d jiffies (ni)\n", njiffies);
325 msleep(jiffies_to_msecs(njiffies));
326 func_exit();
327 return !RIO_FAIL;
328}
329
330void rio_copy_to_card(void *from, void __iomem *to, int len)
331{
332 rio_copy_toio(to, from, len);
333}
334
335int rio_minor(struct tty_struct *tty)
336{
337 return tty->index + ((tty->driver == rio_driver) ? 0 : 256);
338}
339
340static int rio_set_real_termios(void *ptr)
341{
342 return RIOParam((struct Port *) ptr, RIOC_CONFIG, 1, 1);
343}
344
345
346static void rio_reset_interrupt(struct Host *HostP)
347{
348 func_enter();
349
350 switch (HostP->Type) {
351 case RIO_AT:
352 case RIO_MCA:
353 case RIO_PCI:
354 writeb(0xFF, &HostP->ResetInt);
355 }
356
357 func_exit();
358}
359
360
361static irqreturn_t rio_interrupt(int irq, void *ptr)
362{
363 struct Host *HostP;
364 func_enter();
365
366 HostP = ptr; /* &p->RIOHosts[(long)ptr]; */
367 rio_dprintk(RIO_DEBUG_IFLOW, "rio: enter rio_interrupt (%d/%d)\n", irq, HostP->Ivec);
368
369 /* AAargh! The order in which to do these things is essential and
370 not trivial.
371
372 - hardware twiddling goes before "recursive". Otherwise when we
373 poll the card, and a recursive interrupt happens, we won't
374 ack the card, so it might keep on interrupting us. (especially
375 level sensitive interrupt systems like PCI).
376
377 - Rate limit goes before hardware twiddling. Otherwise we won't
378 catch a card that has gone bonkers.
379
380 - The "initialized" test goes after the hardware twiddling. Otherwise
381 the card will stick us in the interrupt routine again.
382
383 - The initialized test goes before recursive.
384 */
385
386 rio_dprintk(RIO_DEBUG_IFLOW, "rio: We've have noticed the interrupt\n");
387 if (HostP->Ivec == irq) {
388 /* Tell the card we've noticed the interrupt. */
389 rio_reset_interrupt(HostP);
390 }
391
392 if ((HostP->Flags & RUN_STATE) != RC_RUNNING)
393 return IRQ_HANDLED;
394
395 if (test_and_set_bit(RIO_BOARD_INTR_LOCK, &HostP->locks)) {
396 printk(KERN_ERR "Recursive interrupt! (host %p/irq%d)\n", ptr, HostP->Ivec);
397 return IRQ_HANDLED;
398 }
399
400 RIOServiceHost(p, HostP);
401
402 rio_dprintk(RIO_DEBUG_IFLOW, "riointr() doing host %p type %d\n", ptr, HostP->Type);
403
404 clear_bit(RIO_BOARD_INTR_LOCK, &HostP->locks);
405 rio_dprintk(RIO_DEBUG_IFLOW, "rio: exit rio_interrupt (%d/%d)\n", irq, HostP->Ivec);
406 func_exit();
407 return IRQ_HANDLED;
408}
409
410
411static void rio_pollfunc(unsigned long data)
412{
413 func_enter();
414
415 rio_interrupt(0, &p->RIOHosts[data]);
416 mod_timer(&p->RIOHosts[data].timer, jiffies + rio_poll);
417
418 func_exit();
419}
420
421
422/* ********************************************************************** *
423 * Here are the routines that actually *
424 * interface with the generic_serial driver *
425 * ********************************************************************** */
426
427/* Ehhm. I don't know how to fiddle with interrupts on the Specialix
428 cards. .... Hmm. Ok I figured it out. You don't. -- REW */
429
430static void rio_disable_tx_interrupts(void *ptr)
431{
432 func_enter();
433
434 /* port->gs.port.flags &= ~GS_TX_INTEN; */
435
436 func_exit();
437}
438
439
440static void rio_enable_tx_interrupts(void *ptr)
441{
442 struct Port *PortP = ptr;
443 /* int hn; */
444
445 func_enter();
446
447 /* hn = PortP->HostP - p->RIOHosts;
448
449 rio_dprintk (RIO_DEBUG_TTY, "Pushing host %d\n", hn);
450 rio_interrupt (-1,(void *) hn, NULL); */
451
452 RIOTxEnable((char *) PortP);
453
454 /*
455 * In general we cannot count on "tx empty" interrupts, although
456 * the interrupt routine seems to be able to tell the difference.
457 */
458 PortP->gs.port.flags &= ~GS_TX_INTEN;
459
460 func_exit();
461}
462
463
464static void rio_disable_rx_interrupts(void *ptr)
465{
466 func_enter();
467 func_exit();
468}
469
470static void rio_enable_rx_interrupts(void *ptr)
471{
472 /* struct rio_port *port = ptr; */
473 func_enter();
474 func_exit();
475}
476
477
478/* Jeez. Isn't this simple? */
479static int rio_carrier_raised(struct tty_port *port)
480{
481 struct Port *PortP = container_of(port, struct Port, gs.port);
482 int rv;
483
484 func_enter();
485 rv = (PortP->ModemState & RIOC_MSVR1_CD) != 0;
486
487 rio_dprintk(RIO_DEBUG_INIT, "Getting CD status: %d\n", rv);
488
489 func_exit();
490 return rv;
491}
492
493
494/* Jeez. Isn't this simple? Actually, we can sync with the actual port
495 by just pushing stuff into the queue going to the port... */
496static int rio_chars_in_buffer(void *ptr)
497{
498 func_enter();
499
500 func_exit();
501 return 0;
502}
503
504
505/* Nothing special here... */
506static void rio_shutdown_port(void *ptr)
507{
508 struct Port *PortP;
509
510 func_enter();
511
512 PortP = (struct Port *) ptr;
513 PortP->gs.port.tty = NULL;
514 func_exit();
515}
516
517
518/* I haven't the foggiest why the decrement use count has to happen
519 here. The whole linux serial drivers stuff needs to be redesigned.
520 My guess is that this is a hack to minimize the impact of a bug
521 elsewhere. Thinking about it some more. (try it sometime) Try
522 running minicom on a serial port that is driven by a modularized
523 driver. Have the modem hangup. Then remove the driver module. Then
524 exit minicom. I expect an "oops". -- REW */
525static void rio_hungup(void *ptr)
526{
527 struct Port *PortP;
528
529 func_enter();
530
531 PortP = (struct Port *) ptr;
532 PortP->gs.port.tty = NULL;
533
534 func_exit();
535}
536
537
538/* The standard serial_close would become shorter if you'd wrap it like
539 this.
540 rs_close (...){save_flags;cli;real_close();dec_use_count;restore_flags;}
541 */
542static void rio_close(void *ptr)
543{
544 struct Port *PortP;
545
546 func_enter();
547
548 PortP = (struct Port *) ptr;
549
550 riotclose(ptr);
551
552 if (PortP->gs.port.count) {
553 printk(KERN_ERR "WARNING port count:%d\n", PortP->gs.port.count);
554 PortP->gs.port.count = 0;
555 }
556
557 PortP->gs.port.tty = NULL;
558 func_exit();
559}
560
561
562
563static long rio_fw_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
564{
565 int rc = 0;
566 func_enter();
567
568 /* The "dev" argument isn't used. */
569 lock_kernel();
570 rc = riocontrol(p, 0, cmd, arg, capable(CAP_SYS_ADMIN));
571 unlock_kernel();
572
573 func_exit();
574 return rc;
575}
576
577extern int RIOShortCommand(struct rio_info *p, struct Port *PortP, int command, int len, int arg);
578
579static int rio_ioctl(struct tty_struct *tty, struct file *filp, unsigned int cmd, unsigned long arg)
580{
581 void __user *argp = (void __user *)arg;
582 int rc;
583 struct Port *PortP;
584 int ival;
585
586 func_enter();
587
588 PortP = (struct Port *) tty->driver_data;
589
590 rc = 0;
591 switch (cmd) {
592 case TIOCSSOFTCAR:
593 if ((rc = get_user(ival, (unsigned __user *) argp)) == 0) {
594 tty->termios->c_cflag = (tty->termios->c_cflag & ~CLOCAL) | (ival ? CLOCAL : 0);
595 }
596 break;
597 case TIOCGSERIAL:
598 rc = -EFAULT;
599 if (access_ok(VERIFY_WRITE, argp, sizeof(struct serial_struct)))
600 rc = gs_getserial(&PortP->gs, argp);
601 break;
602 case TCSBRK:
603 if (PortP->State & RIO_DELETED) {
604 rio_dprintk(RIO_DEBUG_TTY, "BREAK on deleted RTA\n");
605 rc = -EIO;
606 } else {
607 if (RIOShortCommand(p, PortP, RIOC_SBREAK, 2, 250) ==
608 RIO_FAIL) {
609 rio_dprintk(RIO_DEBUG_INTR, "SBREAK RIOShortCommand failed\n");
610 rc = -EIO;
611 }
612 }
613 break;
614 case TCSBRKP:
615 if (PortP->State & RIO_DELETED) {
616 rio_dprintk(RIO_DEBUG_TTY, "BREAK on deleted RTA\n");
617 rc = -EIO;
618 } else {
619 int l;
620 l = arg ? arg * 100 : 250;
621 if (l > 255)
622 l = 255;
623 if (RIOShortCommand(p, PortP, RIOC_SBREAK, 2,
624 arg ? arg * 100 : 250) == RIO_FAIL) {
625 rio_dprintk(RIO_DEBUG_INTR, "SBREAK RIOShortCommand failed\n");
626 rc = -EIO;
627 }
628 }
629 break;
630 case TIOCSSERIAL:
631 rc = -EFAULT;
632 if (access_ok(VERIFY_READ, argp, sizeof(struct serial_struct)))
633 rc = gs_setserial(&PortP->gs, argp);
634 break;
635 default:
636 rc = -ENOIOCTLCMD;
637 break;
638 }
639 func_exit();
640 return rc;
641}
642
643
644/* The throttle/unthrottle scheme for the Specialix card is different
645 * from other drivers and deserves some explanation.
646 * The Specialix hardware takes care of XON/XOFF
647 * and CTS/RTS flow control itself. This means that all we have to
648 * do when signalled by the upper tty layer to throttle/unthrottle is
649 * to make a note of it here. When we come to read characters from the
650 * rx buffers on the card (rio_receive_chars()) we look to see if the
651 * upper layer can accept more (as noted here in rio_rx_throt[]).
652 * If it can't we simply don't remove chars from the cards buffer.
653 * When the tty layer can accept chars, we again note that here and when
654 * rio_receive_chars() is called it will remove them from the cards buffer.
655 * The card will notice that a ports buffer has drained below some low
656 * water mark and will unflow control the line itself, using whatever
657 * flow control scheme is in use for that port. -- Simon Allen
658 */
659
660static void rio_throttle(struct tty_struct *tty)
661{
662 struct Port *port = (struct Port *) tty->driver_data;
663
664 func_enter();
665 /* If the port is using any type of input flow
666 * control then throttle the port.
667 */
668
669 if ((tty->termios->c_cflag & CRTSCTS) || (I_IXOFF(tty))) {
670 port->State |= RIO_THROTTLE_RX;
671 }
672
673 func_exit();
674}
675
676
677static void rio_unthrottle(struct tty_struct *tty)
678{
679 struct Port *port = (struct Port *) tty->driver_data;
680
681 func_enter();
682 /* Always unthrottle even if flow control is not enabled on
683 * this port in case we disabled flow control while the port
684 * was throttled
685 */
686
687 port->State &= ~RIO_THROTTLE_RX;
688
689 func_exit();
690 return;
691}
692
693
694
695
696
697/* ********************************************************************** *
698 * Here are the initialization routines. *
699 * ********************************************************************** */
700
701
702static struct vpd_prom *get_VPD_PROM(struct Host *hp)
703{
704 static struct vpd_prom vpdp;
705 char *p;
706 int i;
707
708 func_enter();
709 rio_dprintk(RIO_DEBUG_PROBE, "Going to verify vpd prom at %p.\n", hp->Caddr + RIO_VPD_ROM);
710
711 p = (char *) &vpdp;
712 for (i = 0; i < sizeof(struct vpd_prom); i++)
713 *p++ = readb(hp->Caddr + RIO_VPD_ROM + i * 2);
714 /* read_rio_byte (hp, RIO_VPD_ROM + i*2); */
715
716 /* Terminate the identifier string.
717 *** requires one extra byte in struct vpd_prom *** */
718 *p++ = 0;
719
720 if (rio_debug & RIO_DEBUG_PROBE)
721 my_hd((char *) &vpdp, 0x20);
722
723 func_exit();
724
725 return &vpdp;
726}
727
728static const struct tty_operations rio_ops = {
729 .open = riotopen,
730 .close = gs_close,
731 .write = gs_write,
732 .put_char = gs_put_char,
733 .flush_chars = gs_flush_chars,
734 .write_room = gs_write_room,
735 .chars_in_buffer = gs_chars_in_buffer,
736 .flush_buffer = gs_flush_buffer,
737 .ioctl = rio_ioctl,
738 .throttle = rio_throttle,
739 .unthrottle = rio_unthrottle,
740 .set_termios = gs_set_termios,
741 .stop = gs_stop,
742 .start = gs_start,
743 .hangup = gs_hangup,
744};
745
746static int rio_init_drivers(void)
747{
748 int error = -ENOMEM;
749
750 rio_driver = alloc_tty_driver(256);
751 if (!rio_driver)
752 goto out;
753 rio_driver2 = alloc_tty_driver(256);
754 if (!rio_driver2)
755 goto out1;
756
757 func_enter();
758
759 rio_driver->owner = THIS_MODULE;
760 rio_driver->driver_name = "specialix_rio";
761 rio_driver->name = "ttySR";
762 rio_driver->major = RIO_NORMAL_MAJOR0;
763 rio_driver->type = TTY_DRIVER_TYPE_SERIAL;
764 rio_driver->subtype = SERIAL_TYPE_NORMAL;
765 rio_driver->init_termios = tty_std_termios;
766 rio_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
767 rio_driver->flags = TTY_DRIVER_REAL_RAW;
768 tty_set_operations(rio_driver, &rio_ops);
769
770 rio_driver2->owner = THIS_MODULE;
771 rio_driver2->driver_name = "specialix_rio";
772 rio_driver2->name = "ttySR";
773 rio_driver2->major = RIO_NORMAL_MAJOR1;
774 rio_driver2->type = TTY_DRIVER_TYPE_SERIAL;
775 rio_driver2->subtype = SERIAL_TYPE_NORMAL;
776 rio_driver2->init_termios = tty_std_termios;
777 rio_driver2->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
778 rio_driver2->flags = TTY_DRIVER_REAL_RAW;
779 tty_set_operations(rio_driver2, &rio_ops);
780
781 rio_dprintk(RIO_DEBUG_INIT, "set_termios = %p\n", gs_set_termios);
782
783 if ((error = tty_register_driver(rio_driver)))
784 goto out2;
785 if ((error = tty_register_driver(rio_driver2)))
786 goto out3;
787 func_exit();
788 return 0;
789 out3:
790 tty_unregister_driver(rio_driver);
791 out2:
792 put_tty_driver(rio_driver2);
793 out1:
794 put_tty_driver(rio_driver);
795 out:
796 printk(KERN_ERR "rio: Couldn't register a rio driver, error = %d\n", error);
797 return 1;
798}
799
800static const struct tty_port_operations rio_port_ops = {
801 .carrier_raised = rio_carrier_raised,
802};
803
804static int rio_init_datastructures(void)
805{
806 int i;
807 struct Port *port;
808 func_enter();
809
810 /* Many drivers statically allocate the maximum number of ports
811 There is no reason not to allocate them dynamically. Is there? -- REW */
812 /* However, the RIO driver allows users to configure their first
813 RTA as the ports numbered 504-511. We therefore need to allocate
814 the whole range. :-( -- REW */
815
816#define RI_SZ sizeof(struct rio_info)
817#define HOST_SZ sizeof(struct Host)
818#define PORT_SZ sizeof(struct Port *)
819#define TMIO_SZ sizeof(struct termios *)
820 rio_dprintk(RIO_DEBUG_INIT, "getting : %Zd %Zd %Zd %Zd %Zd bytes\n", RI_SZ, RIO_HOSTS * HOST_SZ, RIO_PORTS * PORT_SZ, RIO_PORTS * TMIO_SZ, RIO_PORTS * TMIO_SZ);
821
822 if (!(p = kzalloc(RI_SZ, GFP_KERNEL)))
823 goto free0;
824 if (!(p->RIOHosts = kzalloc(RIO_HOSTS * HOST_SZ, GFP_KERNEL)))
825 goto free1;
826 if (!(p->RIOPortp = kzalloc(RIO_PORTS * PORT_SZ, GFP_KERNEL)))
827 goto free2;
828 p->RIOConf = RIOConf;
829 rio_dprintk(RIO_DEBUG_INIT, "Got : %p %p %p\n", p, p->RIOHosts, p->RIOPortp);
830
831#if 1
832 for (i = 0; i < RIO_PORTS; i++) {
833 port = p->RIOPortp[i] = kzalloc(sizeof(struct Port), GFP_KERNEL);
834 if (!port) {
835 goto free6;
836 }
837 rio_dprintk(RIO_DEBUG_INIT, "initing port %d (%d)\n", i, port->Mapped);
838 tty_port_init(&port->gs.port);
839 port->gs.port.ops = &rio_port_ops;
840 port->PortNum = i;
841 port->gs.magic = RIO_MAGIC;
842 port->gs.close_delay = HZ / 2;
843 port->gs.closing_wait = 30 * HZ;
844 port->gs.rd = &rio_real_driver;
845 spin_lock_init(&port->portSem);
846 }
847#else
848 /* We could postpone initializing them to when they are configured. */
849#endif
850
851
852
853 if (rio_debug & RIO_DEBUG_INIT) {
854 my_hd(&rio_real_driver, sizeof(rio_real_driver));
855 }
856
857
858 func_exit();
859 return 0;
860
861 free6:for (i--; i >= 0; i--)
862 kfree(p->RIOPortp[i]);
863/*free5:
864 free4:
865 free3:*/ kfree(p->RIOPortp);
866 free2:kfree(p->RIOHosts);
867 free1:
868 rio_dprintk(RIO_DEBUG_INIT, "Not enough memory! %p %p %p\n", p, p->RIOHosts, p->RIOPortp);
869 kfree(p);
870 free0:
871 return -ENOMEM;
872}
873
874static void __exit rio_release_drivers(void)
875{
876 func_enter();
877 tty_unregister_driver(rio_driver2);
878 tty_unregister_driver(rio_driver);
879 put_tty_driver(rio_driver2);
880 put_tty_driver(rio_driver);
881 func_exit();
882}
883
884
885#ifdef CONFIG_PCI
886 /* This was written for SX, but applies to RIO too...
887 (including bugs....)
888
889 There is another bit besides Bit 17. Turning that bit off
890 (on boards shipped with the fix in the eeprom) results in a
891 hang on the next access to the card.
892 */
893
894 /********************************************************
895 * Setting bit 17 in the CNTRL register of the PLX 9050 *
896 * chip forces a retry on writes while a read is pending.*
897 * This is to prevent the card locking up on Intel Xeon *
898 * multiprocessor systems with the NX chipset. -- NV *
899 ********************************************************/
900
901/* Newer cards are produced with this bit set from the configuration
902 EEprom. As the bit is read/write for the CPU, we can fix it here,
903 if we detect that it isn't set correctly. -- REW */
904
905static void fix_rio_pci(struct pci_dev *pdev)
906{
907 unsigned long hwbase;
908 unsigned char __iomem *rebase;
909 unsigned int t;
910
911#define CNTRL_REG_OFFSET 0x50
912#define CNTRL_REG_GOODVALUE 0x18260000
913
914 hwbase = pci_resource_start(pdev, 0);
915 rebase = ioremap(hwbase, 0x80);
916 t = readl(rebase + CNTRL_REG_OFFSET);
917 if (t != CNTRL_REG_GOODVALUE) {
918 printk(KERN_DEBUG "rio: performing cntrl reg fix: %08x -> %08x\n", t, CNTRL_REG_GOODVALUE);
919 writel(CNTRL_REG_GOODVALUE, rebase + CNTRL_REG_OFFSET);
920 }
921 iounmap(rebase);
922}
923#endif
924
925
926static int __init rio_init(void)
927{
928 int found = 0;
929 int i;
930 struct Host *hp;
931 int retval;
932 struct vpd_prom *vpdp;
933 int okboard;
934
935#ifdef CONFIG_PCI
936 struct pci_dev *pdev = NULL;
937 unsigned short tshort;
938#endif
939
940 func_enter();
941 rio_dprintk(RIO_DEBUG_INIT, "Initing rio module... (rio_debug=%d)\n", rio_debug);
942
943 if (abs((long) (&rio_debug) - rio_debug) < 0x10000) {
944 printk(KERN_WARNING "rio: rio_debug is an address, instead of a value. " "Assuming -1. Was %x/%p.\n", rio_debug, &rio_debug);
945 rio_debug = -1;
946 }
947
948 if (misc_register(&rio_fw_device) < 0) {
949 printk(KERN_ERR "RIO: Unable to register firmware loader driver.\n");
950 return -EIO;
951 }
952
953 retval = rio_init_datastructures();
954 if (retval < 0) {
955 misc_deregister(&rio_fw_device);
956 return retval;
957 }
958#ifdef CONFIG_PCI
959 /* First look for the JET devices: */
960 while ((pdev = pci_get_device(PCI_VENDOR_ID_SPECIALIX, PCI_DEVICE_ID_SPECIALIX_SX_XIO_IO8, pdev))) {
961 u32 tint;
962
963 if (pci_enable_device(pdev))
964 continue;
965
966 /* Specialix has a whole bunch of cards with
967 0x2000 as the device ID. They say its because
968 the standard requires it. Stupid standard. */
969 /* It seems that reading a word doesn't work reliably on 2.0.
970 Also, reading a non-aligned dword doesn't work. So we read the
971 whole dword at 0x2c and extract the word at 0x2e (SUBSYSTEM_ID)
972 ourselves */
973 pci_read_config_dword(pdev, 0x2c, &tint);
974 tshort = (tint >> 16) & 0xffff;
975 rio_dprintk(RIO_DEBUG_PROBE, "Got a specialix card: %x.\n", tint);
976 if (tshort != 0x0100) {
977 rio_dprintk(RIO_DEBUG_PROBE, "But it's not a RIO card (%d)...\n", tshort);
978 continue;
979 }
980 rio_dprintk(RIO_DEBUG_PROBE, "cp1\n");
981
982 hp = &p->RIOHosts[p->RIONumHosts];
983 hp->PaddrP = pci_resource_start(pdev, 2);
984 hp->Ivec = pdev->irq;
985 if (((1 << hp->Ivec) & rio_irqmask) == 0)
986 hp->Ivec = 0;
987 hp->Caddr = ioremap(p->RIOHosts[p->RIONumHosts].PaddrP, RIO_WINDOW_LEN);
988 hp->CardP = (struct DpRam __iomem *) hp->Caddr;
989 hp->Type = RIO_PCI;
990 hp->Copy = rio_copy_to_card;
991 hp->Mode = RIO_PCI_BOOT_FROM_RAM;
992 spin_lock_init(&hp->HostLock);
993 rio_reset_interrupt(hp);
994 rio_start_card_running(hp);
995
996 rio_dprintk(RIO_DEBUG_PROBE, "Going to test it (%p/%p).\n", (void *) p->RIOHosts[p->RIONumHosts].PaddrP, p->RIOHosts[p->RIONumHosts].Caddr);
997 if (RIOBoardTest(p->RIOHosts[p->RIONumHosts].PaddrP, p->RIOHosts[p->RIONumHosts].Caddr, RIO_PCI, 0) == 0) {
998 rio_dprintk(RIO_DEBUG_INIT, "Done RIOBoardTest\n");
999 writeb(0xFF, &p->RIOHosts[p->RIONumHosts].ResetInt);
1000 p->RIOHosts[p->RIONumHosts].UniqueNum =
1001 ((readb(&p->RIOHosts[p->RIONumHosts].Unique[0]) & 0xFF) << 0) |
1002 ((readb(&p->RIOHosts[p->RIONumHosts].Unique[1]) & 0xFF) << 8) | ((readb(&p->RIOHosts[p->RIONumHosts].Unique[2]) & 0xFF) << 16) | ((readb(&p->RIOHosts[p->RIONumHosts].Unique[3]) & 0xFF) << 24);
1003 rio_dprintk(RIO_DEBUG_PROBE, "Hmm Tested ok, uniqid = %x.\n", p->RIOHosts[p->RIONumHosts].UniqueNum);
1004
1005 fix_rio_pci(pdev);
1006
1007 p->RIOHosts[p->RIONumHosts].pdev = pdev;
1008 pci_dev_get(pdev);
1009
1010 p->RIOLastPCISearch = 0;
1011 p->RIONumHosts++;
1012 found++;
1013 } else {
1014 iounmap(p->RIOHosts[p->RIONumHosts].Caddr);
1015 p->RIOHosts[p->RIONumHosts].Caddr = NULL;
1016 }
1017 }
1018
1019 /* Then look for the older PCI card.... : */
1020
1021 /* These older PCI cards have problems (only byte-mode access is
1022 supported), which makes them a bit awkward to support.
1023 They also have problems sharing interrupts. Be careful.
1024 (The driver now refuses to share interrupts for these
1025 cards. This should be sufficient).
1026 */
1027
1028 /* Then look for the older RIO/PCI devices: */
1029 while ((pdev = pci_get_device(PCI_VENDOR_ID_SPECIALIX, PCI_DEVICE_ID_SPECIALIX_RIO, pdev))) {
1030 if (pci_enable_device(pdev))
1031 continue;
1032
1033#ifdef CONFIG_RIO_OLDPCI
1034 hp = &p->RIOHosts[p->RIONumHosts];
1035 hp->PaddrP = pci_resource_start(pdev, 0);
1036 hp->Ivec = pdev->irq;
1037 if (((1 << hp->Ivec) & rio_irqmask) == 0)
1038 hp->Ivec = 0;
1039 hp->Ivec |= 0x8000; /* Mark as non-sharable */
1040 hp->Caddr = ioremap(p->RIOHosts[p->RIONumHosts].PaddrP, RIO_WINDOW_LEN);
1041 hp->CardP = (struct DpRam __iomem *) hp->Caddr;
1042 hp->Type = RIO_PCI;
1043 hp->Copy = rio_copy_to_card;
1044 hp->Mode = RIO_PCI_BOOT_FROM_RAM;
1045 spin_lock_init(&hp->HostLock);
1046
1047 rio_dprintk(RIO_DEBUG_PROBE, "Ivec: %x\n", hp->Ivec);
1048 rio_dprintk(RIO_DEBUG_PROBE, "Mode: %x\n", hp->Mode);
1049
1050 rio_reset_interrupt(hp);
1051 rio_start_card_running(hp);
1052 rio_dprintk(RIO_DEBUG_PROBE, "Going to test it (%p/%p).\n", (void *) p->RIOHosts[p->RIONumHosts].PaddrP, p->RIOHosts[p->RIONumHosts].Caddr);
1053 if (RIOBoardTest(p->RIOHosts[p->RIONumHosts].PaddrP, p->RIOHosts[p->RIONumHosts].Caddr, RIO_PCI, 0) == 0) {
1054 writeb(0xFF, &p->RIOHosts[p->RIONumHosts].ResetInt);
1055 p->RIOHosts[p->RIONumHosts].UniqueNum =
1056 ((readb(&p->RIOHosts[p->RIONumHosts].Unique[0]) & 0xFF) << 0) |
1057 ((readb(&p->RIOHosts[p->RIONumHosts].Unique[1]) & 0xFF) << 8) | ((readb(&p->RIOHosts[p->RIONumHosts].Unique[2]) & 0xFF) << 16) | ((readb(&p->RIOHosts[p->RIONumHosts].Unique[3]) & 0xFF) << 24);
1058 rio_dprintk(RIO_DEBUG_PROBE, "Hmm Tested ok, uniqid = %x.\n", p->RIOHosts[p->RIONumHosts].UniqueNum);
1059
1060 p->RIOHosts[p->RIONumHosts].pdev = pdev;
1061 pci_dev_get(pdev);
1062
1063 p->RIOLastPCISearch = 0;
1064 p->RIONumHosts++;
1065 found++;
1066 } else {
1067 iounmap(p->RIOHosts[p->RIONumHosts].Caddr);
1068 p->RIOHosts[p->RIONumHosts].Caddr = NULL;
1069 }
1070#else
1071 printk(KERN_ERR "Found an older RIO PCI card, but the driver is not " "compiled to support it.\n");
1072#endif
1073 }
1074#endif /* PCI */
1075
1076 /* Now probe for ISA cards... */
1077 for (i = 0; i < NR_RIO_ADDRS; i++) {
1078 hp = &p->RIOHosts[p->RIONumHosts];
1079 hp->PaddrP = rio_probe_addrs[i];
1080 /* There was something about the IRQs of these cards. 'Forget what.--REW */
1081 hp->Ivec = 0;
1082 hp->Caddr = ioremap(p->RIOHosts[p->RIONumHosts].PaddrP, RIO_WINDOW_LEN);
1083 hp->CardP = (struct DpRam __iomem *) hp->Caddr;
1084 hp->Type = RIO_AT;
1085 hp->Copy = rio_copy_to_card; /* AT card PCI???? - PVDL
1086 * -- YES! this is now a normal copy. Only the
1087 * old PCI card uses the special PCI copy.
1088 * Moreover, the ISA card will work with the
1089 * special PCI copy anyway. -- REW */
1090 hp->Mode = 0;
1091 spin_lock_init(&hp->HostLock);
1092
1093 vpdp = get_VPD_PROM(hp);
1094 rio_dprintk(RIO_DEBUG_PROBE, "Got VPD ROM\n");
1095 okboard = 0;
1096 if ((strncmp(vpdp->identifier, RIO_ISA_IDENT, 16) == 0) || (strncmp(vpdp->identifier, RIO_ISA2_IDENT, 16) == 0) || (strncmp(vpdp->identifier, RIO_ISA3_IDENT, 16) == 0)) {
1097 /* Board is present... */
1098 if (RIOBoardTest(hp->PaddrP, hp->Caddr, RIO_AT, 0) == 0) {
1099 /* ... and feeling fine!!!! */
1100 rio_dprintk(RIO_DEBUG_PROBE, "Hmm Tested ok, uniqid = %x.\n", p->RIOHosts[p->RIONumHosts].UniqueNum);
1101 if (RIOAssignAT(p, hp->PaddrP, hp->Caddr, 0)) {
1102 rio_dprintk(RIO_DEBUG_PROBE, "Hmm Tested ok, host%d uniqid = %x.\n", p->RIONumHosts, p->RIOHosts[p->RIONumHosts - 1].UniqueNum);
1103 okboard++;
1104 found++;
1105 }
1106 }
1107
1108 if (!okboard) {
1109 iounmap(hp->Caddr);
1110 hp->Caddr = NULL;
1111 }
1112 }
1113 }
1114
1115
1116 for (i = 0; i < p->RIONumHosts; i++) {
1117 hp = &p->RIOHosts[i];
1118 if (hp->Ivec) {
1119 int mode = IRQF_SHARED;
1120 if (hp->Ivec & 0x8000) {
1121 mode = 0;
1122 hp->Ivec &= 0x7fff;
1123 }
1124 rio_dprintk(RIO_DEBUG_INIT, "Requesting interrupt hp: %p rio_interrupt: %d Mode: %x\n", hp, hp->Ivec, hp->Mode);
1125 retval = request_irq(hp->Ivec, rio_interrupt, mode, "rio", hp);
1126 rio_dprintk(RIO_DEBUG_INIT, "Return value from request_irq: %d\n", retval);
1127 if (retval) {
1128 printk(KERN_ERR "rio: Cannot allocate irq %d.\n", hp->Ivec);
1129 hp->Ivec = 0;
1130 }
1131 rio_dprintk(RIO_DEBUG_INIT, "Got irq %d.\n", hp->Ivec);
1132 if (hp->Ivec != 0) {
1133 rio_dprintk(RIO_DEBUG_INIT, "Enabling interrupts on rio card.\n");
1134 hp->Mode |= RIO_PCI_INT_ENABLE;
1135 } else
1136 hp->Mode &= ~RIO_PCI_INT_ENABLE;
1137 rio_dprintk(RIO_DEBUG_INIT, "New Mode: %x\n", hp->Mode);
1138 rio_start_card_running(hp);
1139 }
1140 /* Init the timer "always" to make sure that it can safely be
1141 deleted when we unload... */
1142
1143 setup_timer(&hp->timer, rio_pollfunc, i);
1144 if (!hp->Ivec) {
1145 rio_dprintk(RIO_DEBUG_INIT, "Starting polling at %dj intervals.\n", rio_poll);
1146 mod_timer(&hp->timer, jiffies + rio_poll);
1147 }
1148 }
1149
1150 if (found) {
1151 rio_dprintk(RIO_DEBUG_INIT, "rio: total of %d boards detected.\n", found);
1152 rio_init_drivers();
1153 } else {
1154 /* deregister the misc device we created earlier */
1155 misc_deregister(&rio_fw_device);
1156 }
1157
1158 func_exit();
1159 return found ? 0 : -EIO;
1160}
1161
1162
1163static void __exit rio_exit(void)
1164{
1165 int i;
1166 struct Host *hp;
1167
1168 func_enter();
1169
1170 for (i = 0, hp = p->RIOHosts; i < p->RIONumHosts; i++, hp++) {
1171 RIOHostReset(hp->Type, hp->CardP, hp->Slot);
1172 if (hp->Ivec) {
1173 free_irq(hp->Ivec, hp);
1174 rio_dprintk(RIO_DEBUG_INIT, "freed irq %d.\n", hp->Ivec);
1175 }
1176 /* It is safe/allowed to del_timer a non-active timer */
1177 del_timer_sync(&hp->timer);
1178 if (hp->Caddr)
1179 iounmap(hp->Caddr);
1180 if (hp->Type == RIO_PCI)
1181 pci_dev_put(hp->pdev);
1182 }
1183
1184 if (misc_deregister(&rio_fw_device) < 0) {
1185 printk(KERN_INFO "rio: couldn't deregister control-device\n");
1186 }
1187
1188
1189 rio_dprintk(RIO_DEBUG_CLEANUP, "Cleaning up drivers\n");
1190
1191 rio_release_drivers();
1192
1193 /* Release dynamically allocated memory */
1194 kfree(p->RIOPortp);
1195 kfree(p->RIOHosts);
1196 kfree(p);
1197
1198 func_exit();
1199}
1200
1201module_init(rio_init);
1202module_exit(rio_exit);
diff --git a/drivers/char/rio/rio_linux.h b/drivers/char/rio/rio_linux.h
deleted file mode 100644
index 7f26cd7c815e..000000000000
--- a/drivers/char/rio/rio_linux.h
+++ /dev/null
@@ -1,197 +0,0 @@
1
2/*
3 * rio_linux.h
4 *
5 * Copyright (C) 1998,1999,2000 R.E.Wolff@BitWizard.nl
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 *
21 * RIO serial driver.
22 *
23 * Version 1.0 -- July, 1999.
24 *
25 */
26
27#define RIO_NBOARDS 4
28#define RIO_PORTSPERBOARD 128
29#define RIO_NPORTS (RIO_NBOARDS * RIO_PORTSPERBOARD)
30
31#define MODEM_SUPPORT
32
33#ifdef __KERNEL__
34
35#define RIO_MAGIC 0x12345678
36
37
38struct vpd_prom {
39 unsigned short id;
40 char hwrev;
41 char hwass;
42 int uniqid;
43 char myear;
44 char mweek;
45 char hw_feature[5];
46 char oem_id;
47 char identifier[16];
48};
49
50
51#define RIO_DEBUG_ALL 0xffffffff
52
53#define O_OTHER(tty) \
54 ((O_OLCUC(tty)) ||\
55 (O_ONLCR(tty)) ||\
56 (O_OCRNL(tty)) ||\
57 (O_ONOCR(tty)) ||\
58 (O_ONLRET(tty)) ||\
59 (O_OFILL(tty)) ||\
60 (O_OFDEL(tty)) ||\
61 (O_NLDLY(tty)) ||\
62 (O_CRDLY(tty)) ||\
63 (O_TABDLY(tty)) ||\
64 (O_BSDLY(tty)) ||\
65 (O_VTDLY(tty)) ||\
66 (O_FFDLY(tty)))
67
68/* Same for input. */
69#define I_OTHER(tty) \
70 ((I_INLCR(tty)) ||\
71 (I_IGNCR(tty)) ||\
72 (I_ICRNL(tty)) ||\
73 (I_IUCLC(tty)) ||\
74 (L_ISIG(tty)))
75
76
77#endif /* __KERNEL__ */
78
79
80#define RIO_BOARD_INTR_LOCK 1
81
82
83#ifndef RIOCTL_MISC_MINOR
84/* Allow others to gather this into "major.h" or something like that */
85#define RIOCTL_MISC_MINOR 169
86#endif
87
88
89/* Allow us to debug "in the field" without requiring clients to
90 recompile.... */
91#if 1
92#define rio_spin_lock_irqsave(sem, flags) do { \
93 rio_dprintk (RIO_DEBUG_SPINLOCK, "spinlockirqsave: %p %s:%d\n", \
94 sem, __FILE__, __LINE__);\
95 spin_lock_irqsave(sem, flags);\
96 } while (0)
97
98#define rio_spin_unlock_irqrestore(sem, flags) do { \
99 rio_dprintk (RIO_DEBUG_SPINLOCK, "spinunlockirqrestore: %p %s:%d\n",\
100 sem, __FILE__, __LINE__);\
101 spin_unlock_irqrestore(sem, flags);\
102 } while (0)
103
104#define rio_spin_lock(sem) do { \
105 rio_dprintk (RIO_DEBUG_SPINLOCK, "spinlock: %p %s:%d\n",\
106 sem, __FILE__, __LINE__);\
107 spin_lock(sem);\
108 } while (0)
109
110#define rio_spin_unlock(sem) do { \
111 rio_dprintk (RIO_DEBUG_SPINLOCK, "spinunlock: %p %s:%d\n",\
112 sem, __FILE__, __LINE__);\
113 spin_unlock(sem);\
114 } while (0)
115#else
116#define rio_spin_lock_irqsave(sem, flags) \
117 spin_lock_irqsave(sem, flags)
118
119#define rio_spin_unlock_irqrestore(sem, flags) \
120 spin_unlock_irqrestore(sem, flags)
121
122#define rio_spin_lock(sem) \
123 spin_lock(sem)
124
125#define rio_spin_unlock(sem) \
126 spin_unlock(sem)
127
128#endif
129
130
131
132#ifdef CONFIG_RIO_OLDPCI
133static inline void __iomem *rio_memcpy_toio(void __iomem *dummy, void __iomem *dest, void *source, int n)
134{
135 char __iomem *dst = dest;
136 char *src = source;
137
138 while (n--) {
139 writeb(*src++, dst++);
140 (void) readb(dummy);
141 }
142
143 return dest;
144}
145
146static inline void __iomem *rio_copy_toio(void __iomem *dest, void *source, int n)
147{
148 char __iomem *dst = dest;
149 char *src = source;
150
151 while (n--)
152 writeb(*src++, dst++);
153
154 return dest;
155}
156
157
158static inline void *rio_memcpy_fromio(void *dest, void __iomem *source, int n)
159{
160 char *dst = dest;
161 char __iomem *src = source;
162
163 while (n--)
164 *dst++ = readb(src++);
165
166 return dest;
167}
168
169#else
170#define rio_memcpy_toio(dummy,dest,source,n) memcpy_toio(dest, source, n)
171#define rio_copy_toio memcpy_toio
172#define rio_memcpy_fromio memcpy_fromio
173#endif
174
175#define DEBUG 1
176
177
178/*
179 This driver can spew a whole lot of debugging output at you. If you
180 need maximum performance, you should disable the DEBUG define. To
181 aid in debugging in the field, I'm leaving the compile-time debug
182 features enabled, and disable them "runtime". That allows me to
183 instruct people with problems to enable debugging without requiring
184 them to recompile...
185*/
186
187#ifdef DEBUG
188#define rio_dprintk(f, str...) do { if (rio_debug & f) printk (str);} while (0)
189#define func_enter() rio_dprintk (RIO_DEBUG_FLOW, "rio: enter %s\n", __func__)
190#define func_exit() rio_dprintk (RIO_DEBUG_FLOW, "rio: exit %s\n", __func__)
191#define func_enter2() rio_dprintk (RIO_DEBUG_FLOW, "rio: enter %s (port %d)\n",__func__, port->line)
192#else
193#define rio_dprintk(f, str...) /* nothing */
194#define func_enter()
195#define func_exit()
196#define func_enter2()
197#endif
diff --git a/drivers/char/rio/rioboard.h b/drivers/char/rio/rioboard.h
deleted file mode 100644
index 252230043c82..000000000000
--- a/drivers/char/rio/rioboard.h
+++ /dev/null
@@ -1,275 +0,0 @@
1/************************************************************************/
2/* */
3/* Title : RIO Host Card Hardware Definitions */
4/* */
5/* Author : N.P.Vassallo */
6/* */
7/* Creation : 26th April 1999 */
8/* */
9/* Version : 1.0.0 */
10/* */
11/* Copyright : (c) Specialix International Ltd. 1999 *
12 *
13 * 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.
17 *
18 * 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.
22 *
23 * 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 * */
27/* Description : Prototypes, structures and definitions */
28/* describing the RIO board hardware */
29/* */
30/************************************************************************/
31
32#ifndef _rioboard_h /* If RIOBOARD.H not already defined */
33#define _rioboard_h 1
34
35/*****************************************************************************
36*********************** ***********************
37*********************** Hardware Control Registers ***********************
38*********************** ***********************
39*****************************************************************************/
40
41/* Hardware Registers... */
42
43#define RIO_REG_BASE 0x7C00 /* Base of control registers */
44
45#define RIO_CONFIG RIO_REG_BASE + 0x0000 /* WRITE: Configuration Register */
46#define RIO_INTSET RIO_REG_BASE + 0x0080 /* WRITE: Interrupt Set */
47#define RIO_RESET RIO_REG_BASE + 0x0100 /* WRITE: Host Reset */
48#define RIO_INTRESET RIO_REG_BASE + 0x0180 /* WRITE: Interrupt Reset */
49
50#define RIO_VPD_ROM RIO_REG_BASE + 0x0000 /* READ: Vital Product Data ROM */
51#define RIO_INTSTAT RIO_REG_BASE + 0x0080 /* READ: Interrupt Status (Jet boards only) */
52#define RIO_RESETSTAT RIO_REG_BASE + 0x0100 /* READ: Reset Status (Jet boards only) */
53
54/* RIO_VPD_ROM definitions... */
55#define VPD_SLX_ID1 0x00 /* READ: Specialix Identifier #1 */
56#define VPD_SLX_ID2 0x01 /* READ: Specialix Identifier #2 */
57#define VPD_HW_REV 0x02 /* READ: Hardware Revision */
58#define VPD_HW_ASSEM 0x03 /* READ: Hardware Assembly Level */
59#define VPD_UNIQUEID4 0x04 /* READ: Unique Identifier #4 */
60#define VPD_UNIQUEID3 0x05 /* READ: Unique Identifier #3 */
61#define VPD_UNIQUEID2 0x06 /* READ: Unique Identifier #2 */
62#define VPD_UNIQUEID1 0x07 /* READ: Unique Identifier #1 */
63#define VPD_MANU_YEAR 0x08 /* READ: Year Of Manufacture (0 = 1970) */
64#define VPD_MANU_WEEK 0x09 /* READ: Week Of Manufacture (0 = week 1 Jan) */
65#define VPD_HWFEATURE1 0x0A /* READ: Hardware Feature Byte 1 */
66#define VPD_HWFEATURE2 0x0B /* READ: Hardware Feature Byte 2 */
67#define VPD_HWFEATURE3 0x0C /* READ: Hardware Feature Byte 3 */
68#define VPD_HWFEATURE4 0x0D /* READ: Hardware Feature Byte 4 */
69#define VPD_HWFEATURE5 0x0E /* READ: Hardware Feature Byte 5 */
70#define VPD_OEMID 0x0F /* READ: OEM Identifier */
71#define VPD_IDENT 0x10 /* READ: Identifier string (16 bytes) */
72#define VPD_IDENT_LEN 0x10
73
74/* VPD ROM Definitions... */
75#define SLX_ID1 0x4D
76#define SLX_ID2 0x98
77
78#define PRODUCT_ID(a) ((a>>4)&0xF) /* Use to obtain Product ID from VPD_UNIQUEID1 */
79
80#define ID_SX_ISA 0x2
81#define ID_RIO_EISA 0x3
82#define ID_SX_PCI 0x5
83#define ID_SX_EISA 0x7
84#define ID_RIO_RTA16 0x9
85#define ID_RIO_ISA 0xA
86#define ID_RIO_MCA 0xB
87#define ID_RIO_SBUS 0xC
88#define ID_RIO_PCI 0xD
89#define ID_RIO_RTA8 0xE
90
91/* Transputer bootstrap definitions... */
92
93#define BOOTLOADADDR (0x8000 - 6)
94#define BOOTINDICATE (0x8000 - 2)
95
96/* Firmware load position... */
97
98#define FIRMWARELOADADDR 0x7C00 /* Firmware is loaded _before_ this address */
99
100/*****************************************************************************
101***************************** *****************************
102***************************** RIO (Rev1) ISA *****************************
103***************************** *****************************
104*****************************************************************************/
105
106/* Control Register Definitions... */
107#define RIO_ISA_IDENT "JBJGPGGHINSMJPJR"
108
109#define RIO_ISA_CFG_BOOTRAM 0x01 /* Boot from RAM, else Link */
110#define RIO_ISA_CFG_BUSENABLE 0x02 /* Enable processor bus */
111#define RIO_ISA_CFG_IRQMASK 0x30 /* Interrupt mask */
112#define RIO_ISA_CFG_IRQ12 0x10 /* Interrupt Level 12 */
113#define RIO_ISA_CFG_IRQ11 0x20 /* Interrupt Level 11 */
114#define RIO_ISA_CFG_IRQ9 0x30 /* Interrupt Level 9 */
115#define RIO_ISA_CFG_LINK20 0x40 /* 20Mbps link, else 10Mbps */
116#define RIO_ISA_CFG_WAITSTATE0 0x80 /* 0 waitstates, else 1 */
117
118/*****************************************************************************
119***************************** *****************************
120***************************** RIO (Rev2) ISA *****************************
121***************************** *****************************
122*****************************************************************************/
123
124/* Control Register Definitions... */
125#define RIO_ISA2_IDENT "JBJGPGGHINSMJPJR"
126
127#define RIO_ISA2_CFG_BOOTRAM 0x01 /* Boot from RAM, else Link */
128#define RIO_ISA2_CFG_BUSENABLE 0x02 /* Enable processor bus */
129#define RIO_ISA2_CFG_INTENABLE 0x04 /* Interrupt enable, else disable */
130#define RIO_ISA2_CFG_16BIT 0x08 /* 16bit mode, else 8bit */
131#define RIO_ISA2_CFG_IRQMASK 0x30 /* Interrupt mask */
132#define RIO_ISA2_CFG_IRQ15 0x00 /* Interrupt Level 15 */
133#define RIO_ISA2_CFG_IRQ12 0x10 /* Interrupt Level 12 */
134#define RIO_ISA2_CFG_IRQ11 0x20 /* Interrupt Level 11 */
135#define RIO_ISA2_CFG_IRQ9 0x30 /* Interrupt Level 9 */
136#define RIO_ISA2_CFG_LINK20 0x40 /* 20Mbps link, else 10Mbps */
137#define RIO_ISA2_CFG_WAITSTATE0 0x80 /* 0 waitstates, else 1 */
138
139/*****************************************************************************
140***************************** ******************************
141***************************** RIO (Jet) ISA ******************************
142***************************** ******************************
143*****************************************************************************/
144
145/* Control Register Definitions... */
146#define RIO_ISA3_IDENT "JET HOST BY KEV#"
147
148#define RIO_ISA3_CFG_BUSENABLE 0x02 /* Enable processor bus */
149#define RIO_ISA3_CFG_INTENABLE 0x04 /* Interrupt enable, else disable */
150#define RIO_ISA32_CFG_IRQMASK 0xF30 /* Interrupt mask */
151#define RIO_ISA3_CFG_IRQ15 0xF0 /* Interrupt Level 15 */
152#define RIO_ISA3_CFG_IRQ12 0xC0 /* Interrupt Level 12 */
153#define RIO_ISA3_CFG_IRQ11 0xB0 /* Interrupt Level 11 */
154#define RIO_ISA3_CFG_IRQ10 0xA0 /* Interrupt Level 10 */
155#define RIO_ISA3_CFG_IRQ9 0x90 /* Interrupt Level 9 */
156
157/*****************************************************************************
158********************************* ********************************
159********************************* RIO MCA ********************************
160********************************* ********************************
161*****************************************************************************/
162
163/* Control Register Definitions... */
164#define RIO_MCA_IDENT "JBJGPGGHINSMJPJR"
165
166#define RIO_MCA_CFG_BOOTRAM 0x01 /* Boot from RAM, else Link */
167#define RIO_MCA_CFG_BUSENABLE 0x02 /* Enable processor bus */
168#define RIO_MCA_CFG_LINK20 0x40 /* 20Mbps link, else 10Mbps */
169
170/*****************************************************************************
171******************************** ********************************
172******************************** RIO EISA ********************************
173******************************** ********************************
174*****************************************************************************/
175
176/* EISA Configuration Space Definitions... */
177#define EISA_PRODUCT_ID1 0xC80
178#define EISA_PRODUCT_ID2 0xC81
179#define EISA_PRODUCT_NUMBER 0xC82
180#define EISA_REVISION_NUMBER 0xC83
181#define EISA_CARD_ENABLE 0xC84
182#define EISA_VPD_UNIQUEID4 0xC88 /* READ: Unique Identifier #4 */
183#define EISA_VPD_UNIQUEID3 0xC8A /* READ: Unique Identifier #3 */
184#define EISA_VPD_UNIQUEID2 0xC90 /* READ: Unique Identifier #2 */
185#define EISA_VPD_UNIQUEID1 0xC92 /* READ: Unique Identifier #1 */
186#define EISA_VPD_MANU_YEAR 0xC98 /* READ: Year Of Manufacture (0 = 1970) */
187#define EISA_VPD_MANU_WEEK 0xC9A /* READ: Week Of Manufacture (0 = week 1 Jan) */
188#define EISA_MEM_ADDR_23_16 0xC00
189#define EISA_MEM_ADDR_31_24 0xC01
190#define EISA_RIO_CONFIG 0xC02 /* WRITE: Configuration Register */
191#define EISA_RIO_INTSET 0xC03 /* WRITE: Interrupt Set */
192#define EISA_RIO_INTRESET 0xC03 /* READ: Interrupt Reset */
193
194/* Control Register Definitions... */
195#define RIO_EISA_CFG_BOOTRAM 0x01 /* Boot from RAM, else Link */
196#define RIO_EISA_CFG_LINK20 0x02 /* 20Mbps link, else 10Mbps */
197#define RIO_EISA_CFG_BUSENABLE 0x04 /* Enable processor bus */
198#define RIO_EISA_CFG_PROCRUN 0x08 /* Processor running, else reset */
199#define RIO_EISA_CFG_IRQMASK 0xF0 /* Interrupt mask */
200#define RIO_EISA_CFG_IRQ15 0xF0 /* Interrupt Level 15 */
201#define RIO_EISA_CFG_IRQ14 0xE0 /* Interrupt Level 14 */
202#define RIO_EISA_CFG_IRQ12 0xC0 /* Interrupt Level 12 */
203#define RIO_EISA_CFG_IRQ11 0xB0 /* Interrupt Level 11 */
204#define RIO_EISA_CFG_IRQ10 0xA0 /* Interrupt Level 10 */
205#define RIO_EISA_CFG_IRQ9 0x90 /* Interrupt Level 9 */
206#define RIO_EISA_CFG_IRQ7 0x70 /* Interrupt Level 7 */
207#define RIO_EISA_CFG_IRQ6 0x60 /* Interrupt Level 6 */
208#define RIO_EISA_CFG_IRQ5 0x50 /* Interrupt Level 5 */
209#define RIO_EISA_CFG_IRQ4 0x40 /* Interrupt Level 4 */
210#define RIO_EISA_CFG_IRQ3 0x30 /* Interrupt Level 3 */
211
212/*****************************************************************************
213******************************** ********************************
214******************************** RIO SBus ********************************
215******************************** ********************************
216*****************************************************************************/
217
218/* Control Register Definitions... */
219#define RIO_SBUS_IDENT "JBPGK#\0\0\0\0\0\0\0\0\0\0"
220
221#define RIO_SBUS_CFG_BOOTRAM 0x01 /* Boot from RAM, else Link */
222#define RIO_SBUS_CFG_BUSENABLE 0x02 /* Enable processor bus */
223#define RIO_SBUS_CFG_INTENABLE 0x04 /* Interrupt enable, else disable */
224#define RIO_SBUS_CFG_IRQMASK 0x38 /* Interrupt mask */
225#define RIO_SBUS_CFG_IRQNONE 0x00 /* No Interrupt */
226#define RIO_SBUS_CFG_IRQ7 0x38 /* Interrupt Level 7 */
227#define RIO_SBUS_CFG_IRQ6 0x30 /* Interrupt Level 6 */
228#define RIO_SBUS_CFG_IRQ5 0x28 /* Interrupt Level 5 */
229#define RIO_SBUS_CFG_IRQ4 0x20 /* Interrupt Level 4 */
230#define RIO_SBUS_CFG_IRQ3 0x18 /* Interrupt Level 3 */
231#define RIO_SBUS_CFG_IRQ2 0x10 /* Interrupt Level 2 */
232#define RIO_SBUS_CFG_IRQ1 0x08 /* Interrupt Level 1 */
233#define RIO_SBUS_CFG_LINK20 0x40 /* 20Mbps link, else 10Mbps */
234#define RIO_SBUS_CFG_PROC25 0x80 /* 25Mhz processor clock, else 20Mhz */
235
236/*****************************************************************************
237********************************* ********************************
238********************************* RIO PCI ********************************
239********************************* ********************************
240*****************************************************************************/
241
242/* Control Register Definitions... */
243#define RIO_PCI_IDENT "ECDDPGJGJHJRGSK#"
244
245#define RIO_PCI_CFG_BOOTRAM 0x01 /* Boot from RAM, else Link */
246#define RIO_PCI_CFG_BUSENABLE 0x02 /* Enable processor bus */
247#define RIO_PCI_CFG_INTENABLE 0x04 /* Interrupt enable, else disable */
248#define RIO_PCI_CFG_LINK20 0x40 /* 20Mbps link, else 10Mbps */
249#define RIO_PCI_CFG_PROC25 0x80 /* 25Mhz processor clock, else 20Mhz */
250
251/* PCI Definitions... */
252#define SPX_VENDOR_ID 0x11CB /* Assigned by the PCI SIG */
253#define SPX_DEVICE_ID 0x8000 /* RIO bridge boards */
254#define SPX_PLXDEVICE_ID 0x2000 /* PLX bridge boards */
255#define SPX_SUB_VENDOR_ID SPX_VENDOR_ID /* Same as vendor id */
256#define RIO_SUB_SYS_ID 0x0800 /* RIO PCI board */
257
258/*****************************************************************************
259***************************** ******************************
260***************************** RIO (Jet) PCI ******************************
261***************************** ******************************
262*****************************************************************************/
263
264/* Control Register Definitions... */
265#define RIO_PCI2_IDENT "JET HOST BY KEV#"
266
267#define RIO_PCI2_CFG_BUSENABLE 0x02 /* Enable processor bus */
268#define RIO_PCI2_CFG_INTENABLE 0x04 /* Interrupt enable, else disable */
269
270/* PCI Definitions... */
271#define RIO2_SUB_SYS_ID 0x0100 /* RIO (Jet) PCI board */
272
273#endif /*_rioboard_h */
274
275/* End of RIOBOARD.H */
diff --git a/drivers/char/rio/rioboot.c b/drivers/char/rio/rioboot.c
deleted file mode 100644
index d956dd316005..000000000000
--- a/drivers/char/rio/rioboot.c
+++ /dev/null
@@ -1,1113 +0,0 @@
1/*
2** -----------------------------------------------------------------------------
3**
4** Perle Specialix driver for Linux
5** Ported from existing RIO Driver for SCO sources.
6 *
7 * (C) 1990 - 2000 Specialix International Ltd., Byfleet, Surrey, UK.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22**
23** Module : rioboot.c
24** SID : 1.3
25** Last Modified : 11/6/98 10:33:36
26** Retrieved : 11/6/98 10:33:48
27**
28** ident @(#)rioboot.c 1.3
29**
30** -----------------------------------------------------------------------------
31*/
32
33#include <linux/module.h>
34#include <linux/slab.h>
35#include <linux/termios.h>
36#include <linux/serial.h>
37#include <linux/vmalloc.h>
38#include <linux/generic_serial.h>
39#include <linux/errno.h>
40#include <linux/interrupt.h>
41#include <linux/delay.h>
42#include <asm/io.h>
43#include <asm/system.h>
44#include <asm/string.h>
45#include <asm/uaccess.h>
46
47
48#include "linux_compat.h"
49#include "rio_linux.h"
50#include "pkt.h"
51#include "daemon.h"
52#include "rio.h"
53#include "riospace.h"
54#include "cmdpkt.h"
55#include "map.h"
56#include "rup.h"
57#include "port.h"
58#include "riodrvr.h"
59#include "rioinfo.h"
60#include "func.h"
61#include "errors.h"
62#include "pci.h"
63
64#include "parmmap.h"
65#include "unixrup.h"
66#include "board.h"
67#include "host.h"
68#include "phb.h"
69#include "link.h"
70#include "cmdblk.h"
71#include "route.h"
72
73static int RIOBootComplete(struct rio_info *p, struct Host *HostP, unsigned int Rup, struct PktCmd __iomem *PktCmdP);
74
75static const unsigned char RIOAtVec2Ctrl[] = {
76 /* 0 */ INTERRUPT_DISABLE,
77 /* 1 */ INTERRUPT_DISABLE,
78 /* 2 */ INTERRUPT_DISABLE,
79 /* 3 */ INTERRUPT_DISABLE,
80 /* 4 */ INTERRUPT_DISABLE,
81 /* 5 */ INTERRUPT_DISABLE,
82 /* 6 */ INTERRUPT_DISABLE,
83 /* 7 */ INTERRUPT_DISABLE,
84 /* 8 */ INTERRUPT_DISABLE,
85 /* 9 */ IRQ_9 | INTERRUPT_ENABLE,
86 /* 10 */ INTERRUPT_DISABLE,
87 /* 11 */ IRQ_11 | INTERRUPT_ENABLE,
88 /* 12 */ IRQ_12 | INTERRUPT_ENABLE,
89 /* 13 */ INTERRUPT_DISABLE,
90 /* 14 */ INTERRUPT_DISABLE,
91 /* 15 */ IRQ_15 | INTERRUPT_ENABLE
92};
93
94/**
95 * RIOBootCodeRTA - Load RTA boot code
96 * @p: RIO to load
97 * @rbp: Download descriptor
98 *
99 * Called when the user process initiates booting of the card firmware.
100 * Lads the firmware
101 */
102
103int RIOBootCodeRTA(struct rio_info *p, struct DownLoad * rbp)
104{
105 int offset;
106
107 func_enter();
108
109 rio_dprintk(RIO_DEBUG_BOOT, "Data at user address %p\n", rbp->DataP);
110
111 /*
112 ** Check that we have set asside enough memory for this
113 */
114 if (rbp->Count > SIXTY_FOUR_K) {
115 rio_dprintk(RIO_DEBUG_BOOT, "RTA Boot Code Too Large!\n");
116 p->RIOError.Error = HOST_FILE_TOO_LARGE;
117 func_exit();
118 return -ENOMEM;
119 }
120
121 if (p->RIOBooting) {
122 rio_dprintk(RIO_DEBUG_BOOT, "RTA Boot Code : BUSY BUSY BUSY!\n");
123 p->RIOError.Error = BOOT_IN_PROGRESS;
124 func_exit();
125 return -EBUSY;
126 }
127
128 /*
129 ** The data we load in must end on a (RTA_BOOT_DATA_SIZE) byte boundary,
130 ** so calculate how far we have to move the data up the buffer
131 ** to achieve this.
132 */
133 offset = (RTA_BOOT_DATA_SIZE - (rbp->Count % RTA_BOOT_DATA_SIZE)) % RTA_BOOT_DATA_SIZE;
134
135 /*
136 ** Be clean, and clear the 'unused' portion of the boot buffer,
137 ** because it will (eventually) be part of the Rta run time environment
138 ** and so should be zeroed.
139 */
140 memset(p->RIOBootPackets, 0, offset);
141
142 /*
143 ** Copy the data from user space into the array
144 */
145
146 if (copy_from_user(((u8 *)p->RIOBootPackets) + offset, rbp->DataP, rbp->Count)) {
147 rio_dprintk(RIO_DEBUG_BOOT, "Bad data copy from user space\n");
148 p->RIOError.Error = COPYIN_FAILED;
149 func_exit();
150 return -EFAULT;
151 }
152
153 /*
154 ** Make sure that our copy of the size includes that offset we discussed
155 ** earlier.
156 */
157 p->RIONumBootPkts = (rbp->Count + offset) / RTA_BOOT_DATA_SIZE;
158 p->RIOBootCount = rbp->Count;
159
160 func_exit();
161 return 0;
162}
163
164/**
165 * rio_start_card_running - host card start
166 * @HostP: The RIO to kick off
167 *
168 * Start a RIO processor unit running. Encapsulates the knowledge
169 * of the card type.
170 */
171
172void rio_start_card_running(struct Host *HostP)
173{
174 switch (HostP->Type) {
175 case RIO_AT:
176 rio_dprintk(RIO_DEBUG_BOOT, "Start ISA card running\n");
177 writeb(BOOT_FROM_RAM | EXTERNAL_BUS_ON | HostP->Mode | RIOAtVec2Ctrl[HostP->Ivec & 0xF], &HostP->Control);
178 break;
179 case RIO_PCI:
180 /*
181 ** PCI is much the same as MCA. Everything is once again memory
182 ** mapped, so we are writing to memory registers instead of io
183 ** ports.
184 */
185 rio_dprintk(RIO_DEBUG_BOOT, "Start PCI card running\n");
186 writeb(PCITpBootFromRam | PCITpBusEnable | HostP->Mode, &HostP->Control);
187 break;
188 default:
189 rio_dprintk(RIO_DEBUG_BOOT, "Unknown host type %d\n", HostP->Type);
190 break;
191 }
192 return;
193}
194
195/*
196** Load in the host boot code - load it directly onto all halted hosts
197** of the correct type.
198**
199** Put your rubber pants on before messing with this code - even the magic
200** numbers have trouble understanding what they are doing here.
201*/
202
203int RIOBootCodeHOST(struct rio_info *p, struct DownLoad *rbp)
204{
205 struct Host *HostP;
206 u8 __iomem *Cad;
207 PARM_MAP __iomem *ParmMapP;
208 int RupN;
209 int PortN;
210 unsigned int host;
211 u8 __iomem *StartP;
212 u8 __iomem *DestP;
213 int wait_count;
214 u16 OldParmMap;
215 u16 offset; /* It is very important that this is a u16 */
216 u8 *DownCode = NULL;
217 unsigned long flags;
218
219 HostP = NULL; /* Assure the compiler we've initialized it */
220
221
222 /* Walk the hosts */
223 for (host = 0; host < p->RIONumHosts; host++) {
224 rio_dprintk(RIO_DEBUG_BOOT, "Attempt to boot host %d\n", host);
225 HostP = &p->RIOHosts[host];
226
227 rio_dprintk(RIO_DEBUG_BOOT, "Host Type = 0x%x, Mode = 0x%x, IVec = 0x%x\n", HostP->Type, HostP->Mode, HostP->Ivec);
228
229 /* Don't boot hosts already running */
230 if ((HostP->Flags & RUN_STATE) != RC_WAITING) {
231 rio_dprintk(RIO_DEBUG_BOOT, "%s %d already running\n", "Host", host);
232 continue;
233 }
234
235 /*
236 ** Grab a pointer to the card (ioremapped)
237 */
238 Cad = HostP->Caddr;
239
240 /*
241 ** We are going to (try) and load in rbp->Count bytes.
242 ** The last byte will reside at p->RIOConf.HostLoadBase-1;
243 ** Therefore, we need to start copying at address
244 ** (caddr+p->RIOConf.HostLoadBase-rbp->Count)
245 */
246 StartP = &Cad[p->RIOConf.HostLoadBase - rbp->Count];
247
248 rio_dprintk(RIO_DEBUG_BOOT, "kernel virtual address for host is %p\n", Cad);
249 rio_dprintk(RIO_DEBUG_BOOT, "kernel virtual address for download is %p\n", StartP);
250 rio_dprintk(RIO_DEBUG_BOOT, "host loadbase is 0x%x\n", p->RIOConf.HostLoadBase);
251 rio_dprintk(RIO_DEBUG_BOOT, "size of download is 0x%x\n", rbp->Count);
252
253 /* Make sure it fits */
254 if (p->RIOConf.HostLoadBase < rbp->Count) {
255 rio_dprintk(RIO_DEBUG_BOOT, "Bin too large\n");
256 p->RIOError.Error = HOST_FILE_TOO_LARGE;
257 func_exit();
258 return -EFBIG;
259 }
260 /*
261 ** Ensure that the host really is stopped.
262 ** Disable it's external bus & twang its reset line.
263 */
264 RIOHostReset(HostP->Type, HostP->CardP, HostP->Slot);
265
266 /*
267 ** Copy the data directly from user space to the SRAM.
268 ** This ain't going to be none too clever if the download
269 ** code is bigger than this segment.
270 */
271 rio_dprintk(RIO_DEBUG_BOOT, "Copy in code\n");
272
273 /* Buffer to local memory as we want to use I/O space and
274 some cards only do 8 or 16 bit I/O */
275
276 DownCode = vmalloc(rbp->Count);
277 if (!DownCode) {
278 p->RIOError.Error = NOT_ENOUGH_CORE_FOR_PCI_COPY;
279 func_exit();
280 return -ENOMEM;
281 }
282 if (copy_from_user(DownCode, rbp->DataP, rbp->Count)) {
283 kfree(DownCode);
284 p->RIOError.Error = COPYIN_FAILED;
285 func_exit();
286 return -EFAULT;
287 }
288 HostP->Copy(DownCode, StartP, rbp->Count);
289 vfree(DownCode);
290
291 rio_dprintk(RIO_DEBUG_BOOT, "Copy completed\n");
292
293 /*
294 ** S T O P !
295 **
296 ** Upto this point the code has been fairly rational, and possibly
297 ** even straight forward. What follows is a pile of crud that will
298 ** magically turn into six bytes of transputer assembler. Normally
299 ** you would expect an array or something, but, being me, I have
300 ** chosen [been told] to use a technique whereby the startup code
301 ** will be correct if we change the loadbase for the code. Which
302 ** brings us onto another issue - the loadbase is the *end* of the
303 ** code, not the start.
304 **
305 ** If I were you I wouldn't start from here.
306 */
307
308 /*
309 ** We now need to insert a short boot section into
310 ** the memory at the end of Sram2. This is normally (de)composed
311 ** of the last eight bytes of the download code. The
312 ** download has been assembled/compiled to expect to be
313 ** loaded from 0x7FFF downwards. We have loaded it
314 ** at some other address. The startup code goes into the small
315 ** ram window at Sram2, in the last 8 bytes, which are really
316 ** at addresses 0x7FF8-0x7FFF.
317 **
318 ** If the loadbase is, say, 0x7C00, then we need to branch to
319 ** address 0x7BFE to run the host.bin startup code. We assemble
320 ** this jump manually.
321 **
322 ** The two byte sequence 60 08 is loaded into memory at address
323 ** 0x7FFE,F. This is a local branch to location 0x7FF8 (60 is nfix 0,
324 ** which adds '0' to the .O register, complements .O, and then shifts
325 ** it left by 4 bit positions, 08 is a jump .O+8 instruction. This will
326 ** add 8 to .O (which was 0xFFF0), and will branch RELATIVE to the new
327 ** location. Now, the branch starts from the value of .PC (or .IP or
328 ** whatever the bloody register is called on this chip), and the .PC
329 ** will be pointing to the location AFTER the branch, in this case
330 ** .PC == 0x8000, so the branch will be to 0x8000+0xFFF8 = 0x7FF8.
331 **
332 ** A long branch is coded at 0x7FF8. This consists of loading a four
333 ** byte offset into .O using nfix (as above) and pfix operators. The
334 ** pfix operates in exactly the same way as the nfix operator, but
335 ** without the complement operation. The offset, of course, must be
336 ** relative to the address of the byte AFTER the branch instruction,
337 ** which will be (urm) 0x7FFC, so, our final destination of the branch
338 ** (loadbase-2), has to be reached from here. Imagine that the loadbase
339 ** is 0x7C00 (which it is), then we will need to branch to 0x7BFE (which
340 ** is the first byte of the initial two byte short local branch of the
341 ** download code).
342 **
343 ** To code a jump from 0x7FFC (which is where the branch will start
344 ** from) to 0x7BFE, we will need to branch 0xFC02 bytes (0x7FFC+0xFC02)=
345 ** 0x7BFE.
346 ** This will be coded as four bytes:
347 ** 60 2C 20 02
348 ** being nfix .O+0
349 ** pfix .O+C
350 ** pfix .O+0
351 ** jump .O+2
352 **
353 ** The nfix operator is used, so that the startup code will be
354 ** compatible with the whole Tp family. (lies, damn lies, it'll never
355 ** work in a month of Sundays).
356 **
357 ** The nfix nyble is the 1s complement of the nyble value you
358 ** want to load - in this case we wanted 'F' so we nfix loaded '0'.
359 */
360
361
362 /*
363 ** Dest points to the top 8 bytes of Sram2. The Tp jumps
364 ** to 0x7FFE at reset time, and starts executing. This is
365 ** a short branch to 0x7FF8, where a long branch is coded.
366 */
367
368 DestP = &Cad[0x7FF8]; /* <<<---- READ THE ABOVE COMMENTS */
369
370#define NFIX(N) (0x60 | (N)) /* .O = (~(.O + N))<<4 */
371#define PFIX(N) (0x20 | (N)) /* .O = (.O + N)<<4 */
372#define JUMP(N) (0x00 | (N)) /* .PC = .PC + .O */
373
374 /*
375 ** 0x7FFC is the address of the location following the last byte of
376 ** the four byte jump instruction.
377 ** READ THE ABOVE COMMENTS
378 **
379 ** offset is (TO-FROM) % MEMSIZE, but with compound buggering about.
380 ** Memsize is 64K for this range of Tp, so offset is a short (unsigned,
381 ** cos I don't understand 2's complement).
382 */
383 offset = (p->RIOConf.HostLoadBase - 2) - 0x7FFC;
384
385 writeb(NFIX(((unsigned short) (~offset) >> (unsigned short) 12) & 0xF), DestP);
386 writeb(PFIX((offset >> 8) & 0xF), DestP + 1);
387 writeb(PFIX((offset >> 4) & 0xF), DestP + 2);
388 writeb(JUMP(offset & 0xF), DestP + 3);
389
390 writeb(NFIX(0), DestP + 6);
391 writeb(JUMP(8), DestP + 7);
392
393 rio_dprintk(RIO_DEBUG_BOOT, "host loadbase is 0x%x\n", p->RIOConf.HostLoadBase);
394 rio_dprintk(RIO_DEBUG_BOOT, "startup offset is 0x%x\n", offset);
395
396 /*
397 ** Flag what is going on
398 */
399 HostP->Flags &= ~RUN_STATE;
400 HostP->Flags |= RC_STARTUP;
401
402 /*
403 ** Grab a copy of the current ParmMap pointer, so we
404 ** can tell when it has changed.
405 */
406 OldParmMap = readw(&HostP->__ParmMapR);
407
408 rio_dprintk(RIO_DEBUG_BOOT, "Original parmmap is 0x%x\n", OldParmMap);
409
410 /*
411 ** And start it running (I hope).
412 ** As there is nothing dodgy or obscure about the
413 ** above code, this is guaranteed to work every time.
414 */
415 rio_dprintk(RIO_DEBUG_BOOT, "Host Type = 0x%x, Mode = 0x%x, IVec = 0x%x\n", HostP->Type, HostP->Mode, HostP->Ivec);
416
417 rio_start_card_running(HostP);
418
419 rio_dprintk(RIO_DEBUG_BOOT, "Set control port\n");
420
421 /*
422 ** Now, wait for upto five seconds for the Tp to setup the parmmap
423 ** pointer:
424 */
425 for (wait_count = 0; (wait_count < p->RIOConf.StartupTime) && (readw(&HostP->__ParmMapR) == OldParmMap); wait_count++) {
426 rio_dprintk(RIO_DEBUG_BOOT, "Checkout %d, 0x%x\n", wait_count, readw(&HostP->__ParmMapR));
427 mdelay(100);
428
429 }
430
431 /*
432 ** If the parmmap pointer is unchanged, then the host code
433 ** has crashed & burned in a really spectacular way
434 */
435 if (readw(&HostP->__ParmMapR) == OldParmMap) {
436 rio_dprintk(RIO_DEBUG_BOOT, "parmmap 0x%x\n", readw(&HostP->__ParmMapR));
437 rio_dprintk(RIO_DEBUG_BOOT, "RIO Mesg Run Fail\n");
438 HostP->Flags &= ~RUN_STATE;
439 HostP->Flags |= RC_STUFFED;
440 RIOHostReset( HostP->Type, HostP->CardP, HostP->Slot );
441 continue;
442 }
443
444 rio_dprintk(RIO_DEBUG_BOOT, "Running 0x%x\n", readw(&HostP->__ParmMapR));
445
446 /*
447 ** Well, the board thought it was OK, and setup its parmmap
448 ** pointer. For the time being, we will pretend that this
449 ** board is running, and check out what the error flag says.
450 */
451
452 /*
453 ** Grab a 32 bit pointer to the parmmap structure
454 */
455 ParmMapP = (PARM_MAP __iomem *) RIO_PTR(Cad, readw(&HostP->__ParmMapR));
456 rio_dprintk(RIO_DEBUG_BOOT, "ParmMapP : %p\n", ParmMapP);
457 ParmMapP = (PARM_MAP __iomem *)(Cad + readw(&HostP->__ParmMapR));
458 rio_dprintk(RIO_DEBUG_BOOT, "ParmMapP : %p\n", ParmMapP);
459
460 /*
461 ** The links entry should be 0xFFFF; we set it up
462 ** with a mask to say how many PHBs to use, and
463 ** which links to use.
464 */
465 if (readw(&ParmMapP->links) != 0xFFFF) {
466 rio_dprintk(RIO_DEBUG_BOOT, "RIO Mesg Run Fail %s\n", HostP->Name);
467 rio_dprintk(RIO_DEBUG_BOOT, "Links = 0x%x\n", readw(&ParmMapP->links));
468 HostP->Flags &= ~RUN_STATE;
469 HostP->Flags |= RC_STUFFED;
470 RIOHostReset( HostP->Type, HostP->CardP, HostP->Slot );
471 continue;
472 }
473
474 writew(RIO_LINK_ENABLE, &ParmMapP->links);
475
476 /*
477 ** now wait for the card to set all the parmmap->XXX stuff
478 ** this is a wait of upto two seconds....
479 */
480 rio_dprintk(RIO_DEBUG_BOOT, "Looking for init_done - %d ticks\n", p->RIOConf.StartupTime);
481 HostP->timeout_id = 0;
482 for (wait_count = 0; (wait_count < p->RIOConf.StartupTime) && !readw(&ParmMapP->init_done); wait_count++) {
483 rio_dprintk(RIO_DEBUG_BOOT, "Waiting for init_done\n");
484 mdelay(100);
485 }
486 rio_dprintk(RIO_DEBUG_BOOT, "OK! init_done!\n");
487
488 if (readw(&ParmMapP->error) != E_NO_ERROR || !readw(&ParmMapP->init_done)) {
489 rio_dprintk(RIO_DEBUG_BOOT, "RIO Mesg Run Fail %s\n", HostP->Name);
490 rio_dprintk(RIO_DEBUG_BOOT, "Timedout waiting for init_done\n");
491 HostP->Flags &= ~RUN_STATE;
492 HostP->Flags |= RC_STUFFED;
493 RIOHostReset( HostP->Type, HostP->CardP, HostP->Slot );
494 continue;
495 }
496
497 rio_dprintk(RIO_DEBUG_BOOT, "Got init_done\n");
498
499 /*
500 ** It runs! It runs!
501 */
502 rio_dprintk(RIO_DEBUG_BOOT, "Host ID %x Running\n", HostP->UniqueNum);
503
504 /*
505 ** set the time period between interrupts.
506 */
507 writew(p->RIOConf.Timer, &ParmMapP->timer);
508
509 /*
510 ** Translate all the 16 bit pointers in the __ParmMapR into
511 ** 32 bit pointers for the driver in ioremap space.
512 */
513 HostP->ParmMapP = ParmMapP;
514 HostP->PhbP = (struct PHB __iomem *) RIO_PTR(Cad, readw(&ParmMapP->phb_ptr));
515 HostP->RupP = (struct RUP __iomem *) RIO_PTR(Cad, readw(&ParmMapP->rups));
516 HostP->PhbNumP = (unsigned short __iomem *) RIO_PTR(Cad, readw(&ParmMapP->phb_num_ptr));
517 HostP->LinkStrP = (struct LPB __iomem *) RIO_PTR(Cad, readw(&ParmMapP->link_str_ptr));
518
519 /*
520 ** point the UnixRups at the real Rups
521 */
522 for (RupN = 0; RupN < MAX_RUP; RupN++) {
523 HostP->UnixRups[RupN].RupP = &HostP->RupP[RupN];
524 HostP->UnixRups[RupN].Id = RupN + 1;
525 HostP->UnixRups[RupN].BaseSysPort = NO_PORT;
526 spin_lock_init(&HostP->UnixRups[RupN].RupLock);
527 }
528
529 for (RupN = 0; RupN < LINKS_PER_UNIT; RupN++) {
530 HostP->UnixRups[RupN + MAX_RUP].RupP = &HostP->LinkStrP[RupN].rup;
531 HostP->UnixRups[RupN + MAX_RUP].Id = 0;
532 HostP->UnixRups[RupN + MAX_RUP].BaseSysPort = NO_PORT;
533 spin_lock_init(&HostP->UnixRups[RupN + MAX_RUP].RupLock);
534 }
535
536 /*
537 ** point the PortP->Phbs at the real Phbs
538 */
539 for (PortN = p->RIOFirstPortsMapped; PortN < p->RIOLastPortsMapped + PORTS_PER_RTA; PortN++) {
540 if (p->RIOPortp[PortN]->HostP == HostP) {
541 struct Port *PortP = p->RIOPortp[PortN];
542 struct PHB __iomem *PhbP;
543 /* int oldspl; */
544
545 if (!PortP->Mapped)
546 continue;
547
548 PhbP = &HostP->PhbP[PortP->HostPort];
549 rio_spin_lock_irqsave(&PortP->portSem, flags);
550
551 PortP->PhbP = PhbP;
552
553 PortP->TxAdd = (u16 __iomem *) RIO_PTR(Cad, readw(&PhbP->tx_add));
554 PortP->TxStart = (u16 __iomem *) RIO_PTR(Cad, readw(&PhbP->tx_start));
555 PortP->TxEnd = (u16 __iomem *) RIO_PTR(Cad, readw(&PhbP->tx_end));
556 PortP->RxRemove = (u16 __iomem *) RIO_PTR(Cad, readw(&PhbP->rx_remove));
557 PortP->RxStart = (u16 __iomem *) RIO_PTR(Cad, readw(&PhbP->rx_start));
558 PortP->RxEnd = (u16 __iomem *) RIO_PTR(Cad, readw(&PhbP->rx_end));
559
560 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
561 /*
562 ** point the UnixRup at the base SysPort
563 */
564 if (!(PortN % PORTS_PER_RTA))
565 HostP->UnixRups[PortP->RupNum].BaseSysPort = PortN;
566 }
567 }
568
569 rio_dprintk(RIO_DEBUG_BOOT, "Set the card running... \n");
570 /*
571 ** last thing - show the world that everything is in place
572 */
573 HostP->Flags &= ~RUN_STATE;
574 HostP->Flags |= RC_RUNNING;
575 }
576 /*
577 ** MPX always uses a poller. This is actually patched into the system
578 ** configuration and called directly from each clock tick.
579 **
580 */
581 p->RIOPolling = 1;
582
583 p->RIOSystemUp++;
584
585 rio_dprintk(RIO_DEBUG_BOOT, "Done everything %x\n", HostP->Ivec);
586 func_exit();
587 return 0;
588}
589
590
591
592/**
593 * RIOBootRup - Boot an RTA
594 * @p: rio we are working with
595 * @Rup: Rup number
596 * @HostP: host object
597 * @PacketP: packet to use
598 *
599 * If we have successfully processed this boot, then
600 * return 1. If we havent, then return 0.
601 */
602
603int RIOBootRup(struct rio_info *p, unsigned int Rup, struct Host *HostP, struct PKT __iomem *PacketP)
604{
605 struct PktCmd __iomem *PktCmdP = (struct PktCmd __iomem *) PacketP->data;
606 struct PktCmd_M *PktReplyP;
607 struct CmdBlk *CmdBlkP;
608 unsigned int sequence;
609
610 /*
611 ** If we haven't been told what to boot, we can't boot it.
612 */
613 if (p->RIONumBootPkts == 0) {
614 rio_dprintk(RIO_DEBUG_BOOT, "No RTA code to download yet\n");
615 return 0;
616 }
617
618 /*
619 ** Special case of boot completed - if we get one of these then we
620 ** don't need a command block. For all other cases we do, so handle
621 ** this first and then get a command block, then handle every other
622 ** case, relinquishing the command block if disaster strikes!
623 */
624 if ((readb(&PacketP->len) & PKT_CMD_BIT) && (readb(&PktCmdP->Command) == BOOT_COMPLETED))
625 return RIOBootComplete(p, HostP, Rup, PktCmdP);
626
627 /*
628 ** Try to allocate a command block. This is in kernel space
629 */
630 if (!(CmdBlkP = RIOGetCmdBlk())) {
631 rio_dprintk(RIO_DEBUG_BOOT, "No command blocks to boot RTA! come back later.\n");
632 return 0;
633 }
634
635 /*
636 ** Fill in the default info on the command block
637 */
638 CmdBlkP->Packet.dest_unit = Rup < (unsigned short) MAX_RUP ? Rup : 0;
639 CmdBlkP->Packet.dest_port = BOOT_RUP;
640 CmdBlkP->Packet.src_unit = 0;
641 CmdBlkP->Packet.src_port = BOOT_RUP;
642
643 CmdBlkP->PreFuncP = CmdBlkP->PostFuncP = NULL;
644 PktReplyP = (struct PktCmd_M *) CmdBlkP->Packet.data;
645
646 /*
647 ** process COMMANDS on the boot rup!
648 */
649 if (readb(&PacketP->len) & PKT_CMD_BIT) {
650 /*
651 ** We only expect one type of command - a BOOT_REQUEST!
652 */
653 if (readb(&PktCmdP->Command) != BOOT_REQUEST) {
654 rio_dprintk(RIO_DEBUG_BOOT, "Unexpected command %d on BOOT RUP %d of host %Zd\n", readb(&PktCmdP->Command), Rup, HostP - p->RIOHosts);
655 RIOFreeCmdBlk(CmdBlkP);
656 return 1;
657 }
658
659 /*
660 ** Build a Boot Sequence command block
661 **
662 ** We no longer need to use "Boot Mode", we'll always allow
663 ** boot requests - the boot will not complete if the device
664 ** appears in the bindings table.
665 **
666 ** We'll just (always) set the command field in packet reply
667 ** to allow an attempted boot sequence :
668 */
669 PktReplyP->Command = BOOT_SEQUENCE;
670
671 PktReplyP->BootSequence.NumPackets = p->RIONumBootPkts;
672 PktReplyP->BootSequence.LoadBase = p->RIOConf.RtaLoadBase;
673 PktReplyP->BootSequence.CodeSize = p->RIOBootCount;
674
675 CmdBlkP->Packet.len = BOOT_SEQUENCE_LEN | PKT_CMD_BIT;
676
677 memcpy((void *) &CmdBlkP->Packet.data[BOOT_SEQUENCE_LEN], "BOOT", 4);
678
679 rio_dprintk(RIO_DEBUG_BOOT, "Boot RTA on Host %Zd Rup %d - %d (0x%x) packets to 0x%x\n", HostP - p->RIOHosts, Rup, p->RIONumBootPkts, p->RIONumBootPkts, p->RIOConf.RtaLoadBase);
680
681 /*
682 ** If this host is in slave mode, send the RTA an invalid boot
683 ** sequence command block to force it to kill the boot. We wait
684 ** for half a second before sending this packet to prevent the RTA
685 ** attempting to boot too often. The master host should then grab
686 ** the RTA and make it its own.
687 */
688 p->RIOBooting++;
689 RIOQueueCmdBlk(HostP, Rup, CmdBlkP);
690 return 1;
691 }
692
693 /*
694 ** It is a request for boot data.
695 */
696 sequence = readw(&PktCmdP->Sequence);
697
698 rio_dprintk(RIO_DEBUG_BOOT, "Boot block %d on Host %Zd Rup%d\n", sequence, HostP - p->RIOHosts, Rup);
699
700 if (sequence >= p->RIONumBootPkts) {
701 rio_dprintk(RIO_DEBUG_BOOT, "Got a request for packet %d, max is %d\n", sequence, p->RIONumBootPkts);
702 }
703
704 PktReplyP->Sequence = sequence;
705 memcpy(PktReplyP->BootData, p->RIOBootPackets[p->RIONumBootPkts - sequence - 1], RTA_BOOT_DATA_SIZE);
706 CmdBlkP->Packet.len = PKT_MAX_DATA_LEN;
707 RIOQueueCmdBlk(HostP, Rup, CmdBlkP);
708 return 1;
709}
710
711/**
712 * RIOBootComplete - RTA boot is done
713 * @p: RIO we are working with
714 * @HostP: Host structure
715 * @Rup: RUP being used
716 * @PktCmdP: Packet command that was used
717 *
718 * This function is called when an RTA been booted.
719 * If booted by a host, HostP->HostUniqueNum is the booting host.
720 * If booted by an RTA, HostP->Mapping[Rup].RtaUniqueNum is the booting RTA.
721 * RtaUniq is the booted RTA.
722 */
723
724static int RIOBootComplete(struct rio_info *p, struct Host *HostP, unsigned int Rup, struct PktCmd __iomem *PktCmdP)
725{
726 struct Map *MapP = NULL;
727 struct Map *MapP2 = NULL;
728 int Flag;
729 int found;
730 int host, rta;
731 int EmptySlot = -1;
732 int entry, entry2;
733 char *MyType, *MyName;
734 unsigned int MyLink;
735 unsigned short RtaType;
736 u32 RtaUniq = (readb(&PktCmdP->UniqNum[0])) + (readb(&PktCmdP->UniqNum[1]) << 8) + (readb(&PktCmdP->UniqNum[2]) << 16) + (readb(&PktCmdP->UniqNum[3]) << 24);
737
738 p->RIOBooting = 0;
739
740 rio_dprintk(RIO_DEBUG_BOOT, "RTA Boot completed - BootInProgress now %d\n", p->RIOBooting);
741
742 /*
743 ** Determine type of unit (16/8 port RTA).
744 */
745
746 RtaType = GetUnitType(RtaUniq);
747 if (Rup >= (unsigned short) MAX_RUP)
748 rio_dprintk(RIO_DEBUG_BOOT, "RIO: Host %s has booted an RTA(%d) on link %c\n", HostP->Name, 8 * RtaType, readb(&PktCmdP->LinkNum) + 'A');
749 else
750 rio_dprintk(RIO_DEBUG_BOOT, "RIO: RTA %s has booted an RTA(%d) on link %c\n", HostP->Mapping[Rup].Name, 8 * RtaType, readb(&PktCmdP->LinkNum) + 'A');
751
752 rio_dprintk(RIO_DEBUG_BOOT, "UniqNum is 0x%x\n", RtaUniq);
753
754 if (RtaUniq == 0x00000000 || RtaUniq == 0xffffffff) {
755 rio_dprintk(RIO_DEBUG_BOOT, "Illegal RTA Uniq Number\n");
756 return 1;
757 }
758
759 /*
760 ** If this RTA has just booted an RTA which doesn't belong to this
761 ** system, or the system is in slave mode, do not attempt to create
762 ** a new table entry for it.
763 */
764
765 if (!RIOBootOk(p, HostP, RtaUniq)) {
766 MyLink = readb(&PktCmdP->LinkNum);
767 if (Rup < (unsigned short) MAX_RUP) {
768 /*
769 ** RtaUniq was clone booted (by this RTA). Instruct this RTA
770 ** to hold off further attempts to boot on this link for 30
771 ** seconds.
772 */
773 if (RIOSuspendBootRta(HostP, HostP->Mapping[Rup].ID, MyLink)) {
774 rio_dprintk(RIO_DEBUG_BOOT, "RTA failed to suspend booting on link %c\n", 'A' + MyLink);
775 }
776 } else
777 /*
778 ** RtaUniq was booted by this host. Set the booting link
779 ** to hold off for 30 seconds to give another unit a
780 ** chance to boot it.
781 */
782 writew(30, &HostP->LinkStrP[MyLink].WaitNoBoot);
783 rio_dprintk(RIO_DEBUG_BOOT, "RTA %x not owned - suspend booting down link %c on unit %x\n", RtaUniq, 'A' + MyLink, HostP->Mapping[Rup].RtaUniqueNum);
784 return 1;
785 }
786
787 /*
788 ** Check for a SLOT_IN_USE entry for this RTA attached to the
789 ** current host card in the driver table.
790 **
791 ** If it exists, make a note that we have booted it. Other parts of
792 ** the driver are interested in this information at a later date,
793 ** in particular when the booting RTA asks for an ID for this unit,
794 ** we must have set the BOOTED flag, and the NEWBOOT flag is used
795 ** to force an open on any ports that where previously open on this
796 ** unit.
797 */
798 for (entry = 0; entry < MAX_RUP; entry++) {
799 unsigned int sysport;
800
801 if ((HostP->Mapping[entry].Flags & SLOT_IN_USE) && (HostP->Mapping[entry].RtaUniqueNum == RtaUniq)) {
802 HostP->Mapping[entry].Flags |= RTA_BOOTED | RTA_NEWBOOT;
803 if ((sysport = HostP->Mapping[entry].SysPort) != NO_PORT) {
804 if (sysport < p->RIOFirstPortsBooted)
805 p->RIOFirstPortsBooted = sysport;
806 if (sysport > p->RIOLastPortsBooted)
807 p->RIOLastPortsBooted = sysport;
808 /*
809 ** For a 16 port RTA, check the second bank of 8 ports
810 */
811 if (RtaType == TYPE_RTA16) {
812 entry2 = HostP->Mapping[entry].ID2 - 1;
813 HostP->Mapping[entry2].Flags |= RTA_BOOTED | RTA_NEWBOOT;
814 sysport = HostP->Mapping[entry2].SysPort;
815 if (sysport < p->RIOFirstPortsBooted)
816 p->RIOFirstPortsBooted = sysport;
817 if (sysport > p->RIOLastPortsBooted)
818 p->RIOLastPortsBooted = sysport;
819 }
820 }
821 if (RtaType == TYPE_RTA16)
822 rio_dprintk(RIO_DEBUG_BOOT, "RTA will be given IDs %d+%d\n", entry + 1, entry2 + 1);
823 else
824 rio_dprintk(RIO_DEBUG_BOOT, "RTA will be given ID %d\n", entry + 1);
825 return 1;
826 }
827 }
828
829 rio_dprintk(RIO_DEBUG_BOOT, "RTA not configured for this host\n");
830
831 if (Rup >= (unsigned short) MAX_RUP) {
832 /*
833 ** It was a host that did the booting
834 */
835 MyType = "Host";
836 MyName = HostP->Name;
837 } else {
838 /*
839 ** It was an RTA that did the booting
840 */
841 MyType = "RTA";
842 MyName = HostP->Mapping[Rup].Name;
843 }
844 MyLink = readb(&PktCmdP->LinkNum);
845
846 /*
847 ** There is no SLOT_IN_USE entry for this RTA attached to the current
848 ** host card in the driver table.
849 **
850 ** Check for a SLOT_TENTATIVE entry for this RTA attached to the
851 ** current host card in the driver table.
852 **
853 ** If we find one, then we re-use that slot.
854 */
855 for (entry = 0; entry < MAX_RUP; entry++) {
856 if ((HostP->Mapping[entry].Flags & SLOT_TENTATIVE) && (HostP->Mapping[entry].RtaUniqueNum == RtaUniq)) {
857 if (RtaType == TYPE_RTA16) {
858 entry2 = HostP->Mapping[entry].ID2 - 1;
859 if ((HostP->Mapping[entry2].Flags & SLOT_TENTATIVE) && (HostP->Mapping[entry2].RtaUniqueNum == RtaUniq))
860 rio_dprintk(RIO_DEBUG_BOOT, "Found previous tentative slots (%d+%d)\n", entry, entry2);
861 else
862 continue;
863 } else
864 rio_dprintk(RIO_DEBUG_BOOT, "Found previous tentative slot (%d)\n", entry);
865 if (!p->RIONoMessage)
866 printk("RTA connected to %s '%s' (%c) not configured.\n", MyType, MyName, MyLink + 'A');
867 return 1;
868 }
869 }
870
871 /*
872 ** There is no SLOT_IN_USE or SLOT_TENTATIVE entry for this RTA
873 ** attached to the current host card in the driver table.
874 **
875 ** Check if there is a SLOT_IN_USE or SLOT_TENTATIVE entry on another
876 ** host for this RTA in the driver table.
877 **
878 ** For a SLOT_IN_USE entry on another host, we need to delete the RTA
879 ** entry from the other host and add it to this host (using some of
880 ** the functions from table.c which do this).
881 ** For a SLOT_TENTATIVE entry on another host, we must cope with the
882 ** following scenario:
883 **
884 ** + Plug 8 port RTA into host A. (This creates SLOT_TENTATIVE entry
885 ** in table)
886 ** + Unplug RTA and plug into host B. (We now have 2 SLOT_TENTATIVE
887 ** entries)
888 ** + Configure RTA on host B. (This slot now becomes SLOT_IN_USE)
889 ** + Unplug RTA and plug back into host A.
890 ** + Configure RTA on host A. We now have the same RTA configured
891 ** with different ports on two different hosts.
892 */
893 rio_dprintk(RIO_DEBUG_BOOT, "Have we seen RTA %x before?\n", RtaUniq);
894 found = 0;
895 Flag = 0; /* Convince the compiler this variable is initialized */
896 for (host = 0; !found && (host < p->RIONumHosts); host++) {
897 for (rta = 0; rta < MAX_RUP; rta++) {
898 if ((p->RIOHosts[host].Mapping[rta].Flags & (SLOT_IN_USE | SLOT_TENTATIVE)) && (p->RIOHosts[host].Mapping[rta].RtaUniqueNum == RtaUniq)) {
899 Flag = p->RIOHosts[host].Mapping[rta].Flags;
900 MapP = &p->RIOHosts[host].Mapping[rta];
901 if (RtaType == TYPE_RTA16) {
902 MapP2 = &p->RIOHosts[host].Mapping[MapP->ID2 - 1];
903 rio_dprintk(RIO_DEBUG_BOOT, "This RTA is units %d+%d from host %s\n", rta + 1, MapP->ID2, p->RIOHosts[host].Name);
904 } else
905 rio_dprintk(RIO_DEBUG_BOOT, "This RTA is unit %d from host %s\n", rta + 1, p->RIOHosts[host].Name);
906 found = 1;
907 break;
908 }
909 }
910 }
911
912 /*
913 ** There is no SLOT_IN_USE or SLOT_TENTATIVE entry for this RTA
914 ** attached to the current host card in the driver table.
915 **
916 ** If we have not found a SLOT_IN_USE or SLOT_TENTATIVE entry on
917 ** another host for this RTA in the driver table...
918 **
919 ** Check for a SLOT_IN_USE entry for this RTA in the config table.
920 */
921 if (!MapP) {
922 rio_dprintk(RIO_DEBUG_BOOT, "Look for RTA %x in RIOSavedTable\n", RtaUniq);
923 for (rta = 0; rta < TOTAL_MAP_ENTRIES; rta++) {
924 rio_dprintk(RIO_DEBUG_BOOT, "Check table entry %d (%x)", rta, p->RIOSavedTable[rta].RtaUniqueNum);
925
926 if ((p->RIOSavedTable[rta].Flags & SLOT_IN_USE) && (p->RIOSavedTable[rta].RtaUniqueNum == RtaUniq)) {
927 MapP = &p->RIOSavedTable[rta];
928 Flag = p->RIOSavedTable[rta].Flags;
929 if (RtaType == TYPE_RTA16) {
930 for (entry2 = rta + 1; entry2 < TOTAL_MAP_ENTRIES; entry2++) {
931 if (p->RIOSavedTable[entry2].RtaUniqueNum == RtaUniq)
932 break;
933 }
934 MapP2 = &p->RIOSavedTable[entry2];
935 rio_dprintk(RIO_DEBUG_BOOT, "This RTA is from table entries %d+%d\n", rta, entry2);
936 } else
937 rio_dprintk(RIO_DEBUG_BOOT, "This RTA is from table entry %d\n", rta);
938 break;
939 }
940 }
941 }
942
943 /*
944 ** There is no SLOT_IN_USE or SLOT_TENTATIVE entry for this RTA
945 ** attached to the current host card in the driver table.
946 **
947 ** We may have found a SLOT_IN_USE entry on another host for this
948 ** RTA in the config table, or a SLOT_IN_USE or SLOT_TENTATIVE entry
949 ** on another host for this RTA in the driver table.
950 **
951 ** Check the driver table for room to fit this newly discovered RTA.
952 ** RIOFindFreeID() first looks for free slots and if it does not
953 ** find any free slots it will then attempt to oust any
954 ** tentative entry in the table.
955 */
956 EmptySlot = 1;
957 if (RtaType == TYPE_RTA16) {
958 if (RIOFindFreeID(p, HostP, &entry, &entry2) == 0) {
959 RIODefaultName(p, HostP, entry);
960 rio_fill_host_slot(entry, entry2, RtaUniq, HostP);
961 EmptySlot = 0;
962 }
963 } else {
964 if (RIOFindFreeID(p, HostP, &entry, NULL) == 0) {
965 RIODefaultName(p, HostP, entry);
966 rio_fill_host_slot(entry, 0, RtaUniq, HostP);
967 EmptySlot = 0;
968 }
969 }
970
971 /*
972 ** There is no SLOT_IN_USE or SLOT_TENTATIVE entry for this RTA
973 ** attached to the current host card in the driver table.
974 **
975 ** If we found a SLOT_IN_USE entry on another host for this
976 ** RTA in the config or driver table, and there are enough free
977 ** slots in the driver table, then we need to move it over and
978 ** delete it from the other host.
979 ** If we found a SLOT_TENTATIVE entry on another host for this
980 ** RTA in the driver table, just delete the other host entry.
981 */
982 if (EmptySlot == 0) {
983 if (MapP) {
984 if (Flag & SLOT_IN_USE) {
985 rio_dprintk(RIO_DEBUG_BOOT, "This RTA configured on another host - move entry to current host (1)\n");
986 HostP->Mapping[entry].SysPort = MapP->SysPort;
987 memcpy(HostP->Mapping[entry].Name, MapP->Name, MAX_NAME_LEN);
988 HostP->Mapping[entry].Flags = SLOT_IN_USE | RTA_BOOTED | RTA_NEWBOOT;
989 RIOReMapPorts(p, HostP, &HostP->Mapping[entry]);
990 if (HostP->Mapping[entry].SysPort < p->RIOFirstPortsBooted)
991 p->RIOFirstPortsBooted = HostP->Mapping[entry].SysPort;
992 if (HostP->Mapping[entry].SysPort > p->RIOLastPortsBooted)
993 p->RIOLastPortsBooted = HostP->Mapping[entry].SysPort;
994 rio_dprintk(RIO_DEBUG_BOOT, "SysPort %d, Name %s\n", (int) MapP->SysPort, MapP->Name);
995 } else {
996 rio_dprintk(RIO_DEBUG_BOOT, "This RTA has a tentative entry on another host - delete that entry (1)\n");
997 HostP->Mapping[entry].Flags = SLOT_TENTATIVE | RTA_BOOTED | RTA_NEWBOOT;
998 }
999 if (RtaType == TYPE_RTA16) {
1000 if (Flag & SLOT_IN_USE) {
1001 HostP->Mapping[entry2].Flags = SLOT_IN_USE | RTA_BOOTED | RTA_NEWBOOT | RTA16_SECOND_SLOT;
1002 HostP->Mapping[entry2].SysPort = MapP2->SysPort;
1003 /*
1004 ** Map second block of ttys for 16 port RTA
1005 */
1006 RIOReMapPorts(p, HostP, &HostP->Mapping[entry2]);
1007 if (HostP->Mapping[entry2].SysPort < p->RIOFirstPortsBooted)
1008 p->RIOFirstPortsBooted = HostP->Mapping[entry2].SysPort;
1009 if (HostP->Mapping[entry2].SysPort > p->RIOLastPortsBooted)
1010 p->RIOLastPortsBooted = HostP->Mapping[entry2].SysPort;
1011 rio_dprintk(RIO_DEBUG_BOOT, "SysPort %d, Name %s\n", (int) HostP->Mapping[entry2].SysPort, HostP->Mapping[entry].Name);
1012 } else
1013 HostP->Mapping[entry2].Flags = SLOT_TENTATIVE | RTA_BOOTED | RTA_NEWBOOT | RTA16_SECOND_SLOT;
1014 memset(MapP2, 0, sizeof(struct Map));
1015 }
1016 memset(MapP, 0, sizeof(struct Map));
1017 if (!p->RIONoMessage)
1018 printk("An orphaned RTA has been adopted by %s '%s' (%c).\n", MyType, MyName, MyLink + 'A');
1019 } else if (!p->RIONoMessage)
1020 printk("RTA connected to %s '%s' (%c) not configured.\n", MyType, MyName, MyLink + 'A');
1021 RIOSetChange(p);
1022 return 1;
1023 }
1024
1025 /*
1026 ** There is no room in the driver table to make an entry for the
1027 ** booted RTA. Keep a note of its Uniq Num in the overflow table,
1028 ** so we can ignore it's ID requests.
1029 */
1030 if (!p->RIONoMessage)
1031 printk("The RTA connected to %s '%s' (%c) cannot be configured. You cannot configure more than 128 ports to one host card.\n", MyType, MyName, MyLink + 'A');
1032 for (entry = 0; entry < HostP->NumExtraBooted; entry++) {
1033 if (HostP->ExtraUnits[entry] == RtaUniq) {
1034 /*
1035 ** already got it!
1036 */
1037 return 1;
1038 }
1039 }
1040 /*
1041 ** If there is room, add the unit to the list of extras
1042 */
1043 if (HostP->NumExtraBooted < MAX_EXTRA_UNITS)
1044 HostP->ExtraUnits[HostP->NumExtraBooted++] = RtaUniq;
1045 return 1;
1046}
1047
1048
1049/*
1050** If the RTA or its host appears in the RIOBindTab[] structure then
1051** we mustn't boot the RTA and should return 0.
1052** This operation is slightly different from the other drivers for RIO
1053** in that this is designed to work with the new utilities
1054** not config.rio and is FAR SIMPLER.
1055** We no longer support the RIOBootMode variable. It is all done from the
1056** "boot/noboot" field in the rio.cf file.
1057*/
1058int RIOBootOk(struct rio_info *p, struct Host *HostP, unsigned long RtaUniq)
1059{
1060 int Entry;
1061 unsigned int HostUniq = HostP->UniqueNum;
1062
1063 /*
1064 ** Search bindings table for RTA or its parent.
1065 ** If it exists, return 0, else 1.
1066 */
1067 for (Entry = 0; (Entry < MAX_RTA_BINDINGS) && (p->RIOBindTab[Entry] != 0); Entry++) {
1068 if ((p->RIOBindTab[Entry] == HostUniq) || (p->RIOBindTab[Entry] == RtaUniq))
1069 return 0;
1070 }
1071 return 1;
1072}
1073
1074/*
1075** Make an empty slot tentative. If this is a 16 port RTA, make both
1076** slots tentative, and the second one RTA_SECOND_SLOT as well.
1077*/
1078
1079void rio_fill_host_slot(int entry, int entry2, unsigned int rta_uniq, struct Host *host)
1080{
1081 int link;
1082
1083 rio_dprintk(RIO_DEBUG_BOOT, "rio_fill_host_slot(%d, %d, 0x%x...)\n", entry, entry2, rta_uniq);
1084
1085 host->Mapping[entry].Flags = (RTA_BOOTED | RTA_NEWBOOT | SLOT_TENTATIVE);
1086 host->Mapping[entry].SysPort = NO_PORT;
1087 host->Mapping[entry].RtaUniqueNum = rta_uniq;
1088 host->Mapping[entry].HostUniqueNum = host->UniqueNum;
1089 host->Mapping[entry].ID = entry + 1;
1090 host->Mapping[entry].ID2 = 0;
1091 if (entry2) {
1092 host->Mapping[entry2].Flags = (RTA_BOOTED | RTA_NEWBOOT | SLOT_TENTATIVE | RTA16_SECOND_SLOT);
1093 host->Mapping[entry2].SysPort = NO_PORT;
1094 host->Mapping[entry2].RtaUniqueNum = rta_uniq;
1095 host->Mapping[entry2].HostUniqueNum = host->UniqueNum;
1096 host->Mapping[entry2].Name[0] = '\0';
1097 host->Mapping[entry2].ID = entry2 + 1;
1098 host->Mapping[entry2].ID2 = entry + 1;
1099 host->Mapping[entry].ID2 = entry2 + 1;
1100 }
1101 /*
1102 ** Must set these up, so that utilities show
1103 ** topology of 16 port RTAs correctly
1104 */
1105 for (link = 0; link < LINKS_PER_UNIT; link++) {
1106 host->Mapping[entry].Topology[link].Unit = ROUTE_DISCONNECT;
1107 host->Mapping[entry].Topology[link].Link = NO_LINK;
1108 if (entry2) {
1109 host->Mapping[entry2].Topology[link].Unit = ROUTE_DISCONNECT;
1110 host->Mapping[entry2].Topology[link].Link = NO_LINK;
1111 }
1112 }
1113}
diff --git a/drivers/char/rio/riocmd.c b/drivers/char/rio/riocmd.c
deleted file mode 100644
index f121357e5af0..000000000000
--- a/drivers/char/rio/riocmd.c
+++ /dev/null
@@ -1,939 +0,0 @@
1/*
2** -----------------------------------------------------------------------------
3**
4** Perle Specialix driver for Linux
5** ported from the existing SCO driver source
6**
7 *
8 * (C) 1990 - 2000 Specialix International Ltd., Byfleet, Surrey, UK.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23**
24** Module : riocmd.c
25** SID : 1.2
26** Last Modified : 11/6/98 10:33:41
27** Retrieved : 11/6/98 10:33:49
28**
29** ident @(#)riocmd.c 1.2
30**
31** -----------------------------------------------------------------------------
32*/
33
34#include <linux/module.h>
35#include <linux/sched.h>
36#include <linux/slab.h>
37#include <linux/errno.h>
38#include <linux/tty.h>
39#include <asm/io.h>
40#include <asm/system.h>
41#include <asm/string.h>
42#include <asm/uaccess.h>
43
44#include <linux/termios.h>
45#include <linux/serial.h>
46
47#include <linux/generic_serial.h>
48
49#include "linux_compat.h"
50#include "rio_linux.h"
51#include "pkt.h"
52#include "daemon.h"
53#include "rio.h"
54#include "riospace.h"
55#include "cmdpkt.h"
56#include "map.h"
57#include "rup.h"
58#include "port.h"
59#include "riodrvr.h"
60#include "rioinfo.h"
61#include "func.h"
62#include "errors.h"
63#include "pci.h"
64
65#include "parmmap.h"
66#include "unixrup.h"
67#include "board.h"
68#include "host.h"
69#include "phb.h"
70#include "link.h"
71#include "cmdblk.h"
72#include "route.h"
73#include "cirrus.h"
74
75
76static struct IdentifyRta IdRta;
77static struct KillNeighbour KillUnit;
78
79int RIOFoadRta(struct Host *HostP, struct Map *MapP)
80{
81 struct CmdBlk *CmdBlkP;
82
83 rio_dprintk(RIO_DEBUG_CMD, "FOAD RTA\n");
84
85 CmdBlkP = RIOGetCmdBlk();
86
87 if (!CmdBlkP) {
88 rio_dprintk(RIO_DEBUG_CMD, "FOAD RTA: GetCmdBlk failed\n");
89 return -ENXIO;
90 }
91
92 CmdBlkP->Packet.dest_unit = MapP->ID;
93 CmdBlkP->Packet.dest_port = BOOT_RUP;
94 CmdBlkP->Packet.src_unit = 0;
95 CmdBlkP->Packet.src_port = BOOT_RUP;
96 CmdBlkP->Packet.len = 0x84;
97 CmdBlkP->Packet.data[0] = IFOAD;
98 CmdBlkP->Packet.data[1] = 0;
99 CmdBlkP->Packet.data[2] = IFOAD_MAGIC & 0xFF;
100 CmdBlkP->Packet.data[3] = (IFOAD_MAGIC >> 8) & 0xFF;
101
102 if (RIOQueueCmdBlk(HostP, MapP->ID - 1, CmdBlkP) == RIO_FAIL) {
103 rio_dprintk(RIO_DEBUG_CMD, "FOAD RTA: Failed to queue foad command\n");
104 return -EIO;
105 }
106 return 0;
107}
108
109int RIOZombieRta(struct Host *HostP, struct Map *MapP)
110{
111 struct CmdBlk *CmdBlkP;
112
113 rio_dprintk(RIO_DEBUG_CMD, "ZOMBIE RTA\n");
114
115 CmdBlkP = RIOGetCmdBlk();
116
117 if (!CmdBlkP) {
118 rio_dprintk(RIO_DEBUG_CMD, "ZOMBIE RTA: GetCmdBlk failed\n");
119 return -ENXIO;
120 }
121
122 CmdBlkP->Packet.dest_unit = MapP->ID;
123 CmdBlkP->Packet.dest_port = BOOT_RUP;
124 CmdBlkP->Packet.src_unit = 0;
125 CmdBlkP->Packet.src_port = BOOT_RUP;
126 CmdBlkP->Packet.len = 0x84;
127 CmdBlkP->Packet.data[0] = ZOMBIE;
128 CmdBlkP->Packet.data[1] = 0;
129 CmdBlkP->Packet.data[2] = ZOMBIE_MAGIC & 0xFF;
130 CmdBlkP->Packet.data[3] = (ZOMBIE_MAGIC >> 8) & 0xFF;
131
132 if (RIOQueueCmdBlk(HostP, MapP->ID - 1, CmdBlkP) == RIO_FAIL) {
133 rio_dprintk(RIO_DEBUG_CMD, "ZOMBIE RTA: Failed to queue zombie command\n");
134 return -EIO;
135 }
136 return 0;
137}
138
139int RIOCommandRta(struct rio_info *p, unsigned long RtaUnique, int (*func) (struct Host * HostP, struct Map * MapP))
140{
141 unsigned int Host;
142
143 rio_dprintk(RIO_DEBUG_CMD, "Command RTA 0x%lx func %p\n", RtaUnique, func);
144
145 if (!RtaUnique)
146 return (0);
147
148 for (Host = 0; Host < p->RIONumHosts; Host++) {
149 unsigned int Rta;
150 struct Host *HostP = &p->RIOHosts[Host];
151
152 for (Rta = 0; Rta < RTAS_PER_HOST; Rta++) {
153 struct Map *MapP = &HostP->Mapping[Rta];
154
155 if (MapP->RtaUniqueNum == RtaUnique) {
156 uint Link;
157
158 /*
159 ** now, lets just check we have a route to it...
160 ** IF the routing stuff is working, then one of the
161 ** topology entries for this unit will have a legit
162 ** route *somewhere*. We care not where - if its got
163 ** any connections, we can get to it.
164 */
165 for (Link = 0; Link < LINKS_PER_UNIT; Link++) {
166 if (MapP->Topology[Link].Unit <= (u8) MAX_RUP) {
167 /*
168 ** Its worth trying the operation...
169 */
170 return (*func) (HostP, MapP);
171 }
172 }
173 }
174 }
175 }
176 return -ENXIO;
177}
178
179
180int RIOIdentifyRta(struct rio_info *p, void __user * arg)
181{
182 unsigned int Host;
183
184 if (copy_from_user(&IdRta, arg, sizeof(IdRta))) {
185 rio_dprintk(RIO_DEBUG_CMD, "RIO_IDENTIFY_RTA copy failed\n");
186 p->RIOError.Error = COPYIN_FAILED;
187 return -EFAULT;
188 }
189
190 for (Host = 0; Host < p->RIONumHosts; Host++) {
191 unsigned int Rta;
192 struct Host *HostP = &p->RIOHosts[Host];
193
194 for (Rta = 0; Rta < RTAS_PER_HOST; Rta++) {
195 struct Map *MapP = &HostP->Mapping[Rta];
196
197 if (MapP->RtaUniqueNum == IdRta.RtaUnique) {
198 uint Link;
199 /*
200 ** now, lets just check we have a route to it...
201 ** IF the routing stuff is working, then one of the
202 ** topology entries for this unit will have a legit
203 ** route *somewhere*. We care not where - if its got
204 ** any connections, we can get to it.
205 */
206 for (Link = 0; Link < LINKS_PER_UNIT; Link++) {
207 if (MapP->Topology[Link].Unit <= (u8) MAX_RUP) {
208 /*
209 ** Its worth trying the operation...
210 */
211 struct CmdBlk *CmdBlkP;
212
213 rio_dprintk(RIO_DEBUG_CMD, "IDENTIFY RTA\n");
214
215 CmdBlkP = RIOGetCmdBlk();
216
217 if (!CmdBlkP) {
218 rio_dprintk(RIO_DEBUG_CMD, "IDENTIFY RTA: GetCmdBlk failed\n");
219 return -ENXIO;
220 }
221
222 CmdBlkP->Packet.dest_unit = MapP->ID;
223 CmdBlkP->Packet.dest_port = BOOT_RUP;
224 CmdBlkP->Packet.src_unit = 0;
225 CmdBlkP->Packet.src_port = BOOT_RUP;
226 CmdBlkP->Packet.len = 0x84;
227 CmdBlkP->Packet.data[0] = IDENTIFY;
228 CmdBlkP->Packet.data[1] = 0;
229 CmdBlkP->Packet.data[2] = IdRta.ID;
230
231 if (RIOQueueCmdBlk(HostP, MapP->ID - 1, CmdBlkP) == RIO_FAIL) {
232 rio_dprintk(RIO_DEBUG_CMD, "IDENTIFY RTA: Failed to queue command\n");
233 return -EIO;
234 }
235 return 0;
236 }
237 }
238 }
239 }
240 }
241 return -ENOENT;
242}
243
244
245int RIOKillNeighbour(struct rio_info *p, void __user * arg)
246{
247 uint Host;
248 uint ID;
249 struct Host *HostP;
250 struct CmdBlk *CmdBlkP;
251
252 rio_dprintk(RIO_DEBUG_CMD, "KILL HOST NEIGHBOUR\n");
253
254 if (copy_from_user(&KillUnit, arg, sizeof(KillUnit))) {
255 rio_dprintk(RIO_DEBUG_CMD, "RIO_KILL_NEIGHBOUR copy failed\n");
256 p->RIOError.Error = COPYIN_FAILED;
257 return -EFAULT;
258 }
259
260 if (KillUnit.Link > 3)
261 return -ENXIO;
262
263 CmdBlkP = RIOGetCmdBlk();
264
265 if (!CmdBlkP) {
266 rio_dprintk(RIO_DEBUG_CMD, "UFOAD: GetCmdBlk failed\n");
267 return -ENXIO;
268 }
269
270 CmdBlkP->Packet.dest_unit = 0;
271 CmdBlkP->Packet.src_unit = 0;
272 CmdBlkP->Packet.dest_port = BOOT_RUP;
273 CmdBlkP->Packet.src_port = BOOT_RUP;
274 CmdBlkP->Packet.len = 0x84;
275 CmdBlkP->Packet.data[0] = UFOAD;
276 CmdBlkP->Packet.data[1] = KillUnit.Link;
277 CmdBlkP->Packet.data[2] = UFOAD_MAGIC & 0xFF;
278 CmdBlkP->Packet.data[3] = (UFOAD_MAGIC >> 8) & 0xFF;
279
280 for (Host = 0; Host < p->RIONumHosts; Host++) {
281 ID = 0;
282 HostP = &p->RIOHosts[Host];
283
284 if (HostP->UniqueNum == KillUnit.UniqueNum) {
285 if (RIOQueueCmdBlk(HostP, RTAS_PER_HOST + KillUnit.Link, CmdBlkP) == RIO_FAIL) {
286 rio_dprintk(RIO_DEBUG_CMD, "UFOAD: Failed queue command\n");
287 return -EIO;
288 }
289 return 0;
290 }
291
292 for (ID = 0; ID < RTAS_PER_HOST; ID++) {
293 if (HostP->Mapping[ID].RtaUniqueNum == KillUnit.UniqueNum) {
294 CmdBlkP->Packet.dest_unit = ID + 1;
295 if (RIOQueueCmdBlk(HostP, ID, CmdBlkP) == RIO_FAIL) {
296 rio_dprintk(RIO_DEBUG_CMD, "UFOAD: Failed queue command\n");
297 return -EIO;
298 }
299 return 0;
300 }
301 }
302 }
303 RIOFreeCmdBlk(CmdBlkP);
304 return -ENXIO;
305}
306
307int RIOSuspendBootRta(struct Host *HostP, int ID, int Link)
308{
309 struct CmdBlk *CmdBlkP;
310
311 rio_dprintk(RIO_DEBUG_CMD, "SUSPEND BOOT ON RTA ID %d, link %c\n", ID, 'A' + Link);
312
313 CmdBlkP = RIOGetCmdBlk();
314
315 if (!CmdBlkP) {
316 rio_dprintk(RIO_DEBUG_CMD, "SUSPEND BOOT ON RTA: GetCmdBlk failed\n");
317 return -ENXIO;
318 }
319
320 CmdBlkP->Packet.dest_unit = ID;
321 CmdBlkP->Packet.dest_port = BOOT_RUP;
322 CmdBlkP->Packet.src_unit = 0;
323 CmdBlkP->Packet.src_port = BOOT_RUP;
324 CmdBlkP->Packet.len = 0x84;
325 CmdBlkP->Packet.data[0] = IWAIT;
326 CmdBlkP->Packet.data[1] = Link;
327 CmdBlkP->Packet.data[2] = IWAIT_MAGIC & 0xFF;
328 CmdBlkP->Packet.data[3] = (IWAIT_MAGIC >> 8) & 0xFF;
329
330 if (RIOQueueCmdBlk(HostP, ID - 1, CmdBlkP) == RIO_FAIL) {
331 rio_dprintk(RIO_DEBUG_CMD, "SUSPEND BOOT ON RTA: Failed to queue iwait command\n");
332 return -EIO;
333 }
334 return 0;
335}
336
337int RIOFoadWakeup(struct rio_info *p)
338{
339 int port;
340 struct Port *PortP;
341 unsigned long flags;
342
343 for (port = 0; port < RIO_PORTS; port++) {
344 PortP = p->RIOPortp[port];
345
346 rio_spin_lock_irqsave(&PortP->portSem, flags);
347 PortP->Config = 0;
348 PortP->State = 0;
349 PortP->InUse = NOT_INUSE;
350 PortP->PortState = 0;
351 PortP->FlushCmdBodge = 0;
352 PortP->ModemLines = 0;
353 PortP->ModemState = 0;
354 PortP->CookMode = 0;
355 PortP->ParamSem = 0;
356 PortP->Mapped = 0;
357 PortP->WflushFlag = 0;
358 PortP->MagicFlags = 0;
359 PortP->RxDataStart = 0;
360 PortP->TxBufferIn = 0;
361 PortP->TxBufferOut = 0;
362 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
363 }
364 return (0);
365}
366
367/*
368** Incoming command on the COMMAND_RUP to be processed.
369*/
370static int RIOCommandRup(struct rio_info *p, uint Rup, struct Host *HostP, struct PKT __iomem *PacketP)
371{
372 struct PktCmd __iomem *PktCmdP = (struct PktCmd __iomem *)PacketP->data;
373 struct Port *PortP;
374 struct UnixRup *UnixRupP;
375 unsigned short SysPort;
376 unsigned short ReportedModemStatus;
377 unsigned short rup;
378 unsigned short subCommand;
379 unsigned long flags;
380
381 func_enter();
382
383 /*
384 ** 16 port RTA note:
385 ** Command rup packets coming from the RTA will have pkt->data[1] (which
386 ** translates to PktCmdP->PhbNum) set to the host port number for the
387 ** particular unit. To access the correct BaseSysPort for a 16 port RTA,
388 ** we can use PhbNum to get the rup number for the appropriate 8 port
389 ** block (for the first block, this should be equal to 'Rup').
390 */
391 rup = readb(&PktCmdP->PhbNum) / (unsigned short) PORTS_PER_RTA;
392 UnixRupP = &HostP->UnixRups[rup];
393 SysPort = UnixRupP->BaseSysPort + (readb(&PktCmdP->PhbNum) % (unsigned short) PORTS_PER_RTA);
394 rio_dprintk(RIO_DEBUG_CMD, "Command on rup %d, port %d\n", rup, SysPort);
395
396 if (UnixRupP->BaseSysPort == NO_PORT) {
397 rio_dprintk(RIO_DEBUG_CMD, "OBSCURE ERROR!\n");
398 rio_dprintk(RIO_DEBUG_CMD, "Diagnostics follow. Please WRITE THESE DOWN and report them to Specialix Technical Support\n");
399 rio_dprintk(RIO_DEBUG_CMD, "CONTROL information: Host number %Zd, name ``%s''\n", HostP - p->RIOHosts, HostP->Name);
400 rio_dprintk(RIO_DEBUG_CMD, "CONTROL information: Rup number 0x%x\n", rup);
401
402 if (Rup < (unsigned short) MAX_RUP) {
403 rio_dprintk(RIO_DEBUG_CMD, "CONTROL information: This is the RUP for RTA ``%s''\n", HostP->Mapping[Rup].Name);
404 } else
405 rio_dprintk(RIO_DEBUG_CMD, "CONTROL information: This is the RUP for link ``%c'' of host ``%s''\n", ('A' + Rup - MAX_RUP), HostP->Name);
406
407 rio_dprintk(RIO_DEBUG_CMD, "PACKET information: Destination 0x%x:0x%x\n", readb(&PacketP->dest_unit), readb(&PacketP->dest_port));
408 rio_dprintk(RIO_DEBUG_CMD, "PACKET information: Source 0x%x:0x%x\n", readb(&PacketP->src_unit), readb(&PacketP->src_port));
409 rio_dprintk(RIO_DEBUG_CMD, "PACKET information: Length 0x%x (%d)\n", readb(&PacketP->len), readb(&PacketP->len));
410 rio_dprintk(RIO_DEBUG_CMD, "PACKET information: Control 0x%x (%d)\n", readb(&PacketP->control), readb(&PacketP->control));
411 rio_dprintk(RIO_DEBUG_CMD, "PACKET information: Check 0x%x (%d)\n", readw(&PacketP->csum), readw(&PacketP->csum));
412 rio_dprintk(RIO_DEBUG_CMD, "COMMAND information: Host Port Number 0x%x, " "Command Code 0x%x\n", readb(&PktCmdP->PhbNum), readb(&PktCmdP->Command));
413 return 1;
414 }
415 PortP = p->RIOPortp[SysPort];
416 rio_spin_lock_irqsave(&PortP->portSem, flags);
417 switch (readb(&PktCmdP->Command)) {
418 case RIOC_BREAK_RECEIVED:
419 rio_dprintk(RIO_DEBUG_CMD, "Received a break!\n");
420 /* If the current line disc. is not multi-threading and
421 the current processor is not the default, reset rup_intr
422 and return 0 to ensure that the command packet is
423 not freed. */
424 /* Call tmgr HANGUP HERE */
425 /* Fix this later when every thing works !!!! RAMRAJ */
426 gs_got_break(&PortP->gs);
427 break;
428
429 case RIOC_COMPLETE:
430 rio_dprintk(RIO_DEBUG_CMD, "Command complete on phb %d host %Zd\n", readb(&PktCmdP->PhbNum), HostP - p->RIOHosts);
431 subCommand = 1;
432 switch (readb(&PktCmdP->SubCommand)) {
433 case RIOC_MEMDUMP:
434 rio_dprintk(RIO_DEBUG_CMD, "Memory dump cmd (0x%x) from addr 0x%x\n", readb(&PktCmdP->SubCommand), readw(&PktCmdP->SubAddr));
435 break;
436 case RIOC_READ_REGISTER:
437 rio_dprintk(RIO_DEBUG_CMD, "Read register (0x%x)\n", readw(&PktCmdP->SubAddr));
438 p->CdRegister = (readb(&PktCmdP->ModemStatus) & RIOC_MSVR1_HOST);
439 break;
440 default:
441 subCommand = 0;
442 break;
443 }
444 if (subCommand)
445 break;
446 rio_dprintk(RIO_DEBUG_CMD, "New status is 0x%x was 0x%x\n", readb(&PktCmdP->PortStatus), PortP->PortState);
447 if (PortP->PortState != readb(&PktCmdP->PortStatus)) {
448 rio_dprintk(RIO_DEBUG_CMD, "Mark status & wakeup\n");
449 PortP->PortState = readb(&PktCmdP->PortStatus);
450 /* What should we do here ...
451 wakeup( &PortP->PortState );
452 */
453 } else
454 rio_dprintk(RIO_DEBUG_CMD, "No change\n");
455
456 /* FALLTHROUGH */
457 case RIOC_MODEM_STATUS:
458 /*
459 ** Knock out the tbusy and tstop bits, as these are not relevant
460 ** to the check for modem status change (they're just there because
461 ** it's a convenient place to put them!).
462 */
463 ReportedModemStatus = readb(&PktCmdP->ModemStatus);
464 if ((PortP->ModemState & RIOC_MSVR1_HOST) ==
465 (ReportedModemStatus & RIOC_MSVR1_HOST)) {
466 rio_dprintk(RIO_DEBUG_CMD, "Modem status unchanged 0x%x\n", PortP->ModemState);
467 /*
468 ** Update ModemState just in case tbusy or tstop states have
469 ** changed.
470 */
471 PortP->ModemState = ReportedModemStatus;
472 } else {
473 rio_dprintk(RIO_DEBUG_CMD, "Modem status change from 0x%x to 0x%x\n", PortP->ModemState, ReportedModemStatus);
474 PortP->ModemState = ReportedModemStatus;
475#ifdef MODEM_SUPPORT
476 if (PortP->Mapped) {
477 /***********************************************************\
478 *************************************************************
479 *** ***
480 *** M O D E M S T A T E C H A N G E ***
481 *** ***
482 *************************************************************
483 \***********************************************************/
484 /*
485 ** If the device is a modem, then check the modem
486 ** carrier.
487 */
488 if (PortP->gs.port.tty == NULL)
489 break;
490 if (PortP->gs.port.tty->termios == NULL)
491 break;
492
493 if (!(PortP->gs.port.tty->termios->c_cflag & CLOCAL) && ((PortP->State & (RIO_MOPEN | RIO_WOPEN)))) {
494
495 rio_dprintk(RIO_DEBUG_CMD, "Is there a Carrier?\n");
496 /*
497 ** Is there a carrier?
498 */
499 if (PortP->ModemState & RIOC_MSVR1_CD) {
500 /*
501 ** Has carrier just appeared?
502 */
503 if (!(PortP->State & RIO_CARR_ON)) {
504 rio_dprintk(RIO_DEBUG_CMD, "Carrier just came up.\n");
505 PortP->State |= RIO_CARR_ON;
506 /*
507 ** wakeup anyone in WOPEN
508 */
509 if (PortP->State & (PORT_ISOPEN | RIO_WOPEN))
510 wake_up_interruptible(&PortP->gs.port.open_wait);
511 }
512 } else {
513 /*
514 ** Has carrier just dropped?
515 */
516 if (PortP->State & RIO_CARR_ON) {
517 if (PortP->State & (PORT_ISOPEN | RIO_WOPEN | RIO_MOPEN))
518 tty_hangup(PortP->gs.port.tty);
519 PortP->State &= ~RIO_CARR_ON;
520 rio_dprintk(RIO_DEBUG_CMD, "Carrirer just went down\n");
521 }
522 }
523 }
524 }
525#endif
526 }
527 break;
528
529 default:
530 rio_dprintk(RIO_DEBUG_CMD, "Unknown command %d on CMD_RUP of host %Zd\n", readb(&PktCmdP->Command), HostP - p->RIOHosts);
531 break;
532 }
533 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
534
535 func_exit();
536
537 return 1;
538}
539
540/*
541** The command mechanism:
542** Each rup has a chain of commands associated with it.
543** This chain is maintained by routines in this file.
544** Periodically we are called and we run a quick check of all the
545** active chains to determine if there is a command to be executed,
546** and if the rup is ready to accept it.
547**
548*/
549
550/*
551** Allocate an empty command block.
552*/
553struct CmdBlk *RIOGetCmdBlk(void)
554{
555 struct CmdBlk *CmdBlkP;
556
557 CmdBlkP = kzalloc(sizeof(struct CmdBlk), GFP_ATOMIC);
558 return CmdBlkP;
559}
560
561/*
562** Return a block to the head of the free list.
563*/
564void RIOFreeCmdBlk(struct CmdBlk *CmdBlkP)
565{
566 kfree(CmdBlkP);
567}
568
569/*
570** attach a command block to the list of commands to be performed for
571** a given rup.
572*/
573int RIOQueueCmdBlk(struct Host *HostP, uint Rup, struct CmdBlk *CmdBlkP)
574{
575 struct CmdBlk **Base;
576 struct UnixRup *UnixRupP;
577 unsigned long flags;
578
579 if (Rup >= (unsigned short) (MAX_RUP + LINKS_PER_UNIT)) {
580 rio_dprintk(RIO_DEBUG_CMD, "Illegal rup number %d in RIOQueueCmdBlk\n", Rup);
581 RIOFreeCmdBlk(CmdBlkP);
582 return RIO_FAIL;
583 }
584
585 UnixRupP = &HostP->UnixRups[Rup];
586
587 rio_spin_lock_irqsave(&UnixRupP->RupLock, flags);
588
589 /*
590 ** If the RUP is currently inactive, then put the request
591 ** straight on the RUP....
592 */
593 if ((UnixRupP->CmdsWaitingP == NULL) && (UnixRupP->CmdPendingP == NULL) && (readw(&UnixRupP->RupP->txcontrol) == TX_RUP_INACTIVE) && (CmdBlkP->PreFuncP ? (*CmdBlkP->PreFuncP) (CmdBlkP->PreArg, CmdBlkP)
594 : 1)) {
595 rio_dprintk(RIO_DEBUG_CMD, "RUP inactive-placing command straight on. Cmd byte is 0x%x\n", CmdBlkP->Packet.data[0]);
596
597 /*
598 ** Whammy! blat that pack!
599 */
600 HostP->Copy(&CmdBlkP->Packet, RIO_PTR(HostP->Caddr, readw(&UnixRupP->RupP->txpkt)), sizeof(struct PKT));
601
602 /*
603 ** place command packet on the pending position.
604 */
605 UnixRupP->CmdPendingP = CmdBlkP;
606
607 /*
608 ** set the command register
609 */
610 writew(TX_PACKET_READY, &UnixRupP->RupP->txcontrol);
611
612 rio_spin_unlock_irqrestore(&UnixRupP->RupLock, flags);
613
614 return 0;
615 }
616 rio_dprintk(RIO_DEBUG_CMD, "RUP active - en-queing\n");
617
618 if (UnixRupP->CmdsWaitingP != NULL)
619 rio_dprintk(RIO_DEBUG_CMD, "Rup active - command waiting\n");
620 if (UnixRupP->CmdPendingP != NULL)
621 rio_dprintk(RIO_DEBUG_CMD, "Rup active - command pending\n");
622 if (readw(&UnixRupP->RupP->txcontrol) != TX_RUP_INACTIVE)
623 rio_dprintk(RIO_DEBUG_CMD, "Rup active - command rup not ready\n");
624
625 Base = &UnixRupP->CmdsWaitingP;
626
627 rio_dprintk(RIO_DEBUG_CMD, "First try to queue cmdblk %p at %p\n", CmdBlkP, Base);
628
629 while (*Base) {
630 rio_dprintk(RIO_DEBUG_CMD, "Command cmdblk %p here\n", *Base);
631 Base = &((*Base)->NextP);
632 rio_dprintk(RIO_DEBUG_CMD, "Now try to queue cmd cmdblk %p at %p\n", CmdBlkP, Base);
633 }
634
635 rio_dprintk(RIO_DEBUG_CMD, "Will queue cmdblk %p at %p\n", CmdBlkP, Base);
636
637 *Base = CmdBlkP;
638
639 CmdBlkP->NextP = NULL;
640
641 rio_spin_unlock_irqrestore(&UnixRupP->RupLock, flags);
642
643 return 0;
644}
645
646/*
647** Here we go - if there is an empty rup, fill it!
648** must be called at splrio() or higher.
649*/
650void RIOPollHostCommands(struct rio_info *p, struct Host *HostP)
651{
652 struct CmdBlk *CmdBlkP;
653 struct UnixRup *UnixRupP;
654 struct PKT __iomem *PacketP;
655 unsigned short Rup;
656 unsigned long flags;
657
658
659 Rup = MAX_RUP + LINKS_PER_UNIT;
660
661 do { /* do this loop for each RUP */
662 /*
663 ** locate the rup we are processing & lock it
664 */
665 UnixRupP = &HostP->UnixRups[--Rup];
666
667 spin_lock_irqsave(&UnixRupP->RupLock, flags);
668
669 /*
670 ** First check for incoming commands:
671 */
672 if (readw(&UnixRupP->RupP->rxcontrol) != RX_RUP_INACTIVE) {
673 int FreeMe;
674
675 PacketP = (struct PKT __iomem *) RIO_PTR(HostP->Caddr, readw(&UnixRupP->RupP->rxpkt));
676
677 switch (readb(&PacketP->dest_port)) {
678 case BOOT_RUP:
679 rio_dprintk(RIO_DEBUG_CMD, "Incoming Boot %s packet '%x'\n", readb(&PacketP->len) & 0x80 ? "Command" : "Data", readb(&PacketP->data[0]));
680 rio_spin_unlock_irqrestore(&UnixRupP->RupLock, flags);
681 FreeMe = RIOBootRup(p, Rup, HostP, PacketP);
682 rio_spin_lock_irqsave(&UnixRupP->RupLock, flags);
683 break;
684
685 case COMMAND_RUP:
686 /*
687 ** Free the RUP lock as loss of carrier causes a
688 ** ttyflush which will (eventually) call another
689 ** routine that uses the RUP lock.
690 */
691 rio_spin_unlock_irqrestore(&UnixRupP->RupLock, flags);
692 FreeMe = RIOCommandRup(p, Rup, HostP, PacketP);
693 if (readb(&PacketP->data[5]) == RIOC_MEMDUMP) {
694 rio_dprintk(RIO_DEBUG_CMD, "Memdump from 0x%x complete\n", readw(&(PacketP->data[6])));
695 rio_memcpy_fromio(p->RIOMemDump, &(PacketP->data[8]), 32);
696 }
697 rio_spin_lock_irqsave(&UnixRupP->RupLock, flags);
698 break;
699
700 case ROUTE_RUP:
701 rio_spin_unlock_irqrestore(&UnixRupP->RupLock, flags);
702 FreeMe = RIORouteRup(p, Rup, HostP, PacketP);
703 rio_spin_lock_irqsave(&UnixRupP->RupLock, flags);
704 break;
705
706 default:
707 rio_dprintk(RIO_DEBUG_CMD, "Unknown RUP %d\n", readb(&PacketP->dest_port));
708 FreeMe = 1;
709 break;
710 }
711
712 if (FreeMe) {
713 rio_dprintk(RIO_DEBUG_CMD, "Free processed incoming command packet\n");
714 put_free_end(HostP, PacketP);
715
716 writew(RX_RUP_INACTIVE, &UnixRupP->RupP->rxcontrol);
717
718 if (readw(&UnixRupP->RupP->handshake) == PHB_HANDSHAKE_SET) {
719 rio_dprintk(RIO_DEBUG_CMD, "Handshake rup %d\n", Rup);
720 writew(PHB_HANDSHAKE_SET | PHB_HANDSHAKE_RESET, &UnixRupP->RupP->handshake);
721 }
722 }
723 }
724
725 /*
726 ** IF a command was running on the port,
727 ** and it has completed, then tidy it up.
728 */
729 if ((CmdBlkP = UnixRupP->CmdPendingP) && /* ASSIGN! */
730 (readw(&UnixRupP->RupP->txcontrol) == TX_RUP_INACTIVE)) {
731 /*
732 ** we are idle.
733 ** there is a command in pending.
734 ** Therefore, this command has finished.
735 ** So, wakeup whoever is waiting for it (and tell them
736 ** what happened).
737 */
738 if (CmdBlkP->Packet.dest_port == BOOT_RUP)
739 rio_dprintk(RIO_DEBUG_CMD, "Free Boot %s Command Block '%x'\n", CmdBlkP->Packet.len & 0x80 ? "Command" : "Data", CmdBlkP->Packet.data[0]);
740
741 rio_dprintk(RIO_DEBUG_CMD, "Command %p completed\n", CmdBlkP);
742
743 /*
744 ** Clear the Rup lock to prevent mutual exclusion.
745 */
746 if (CmdBlkP->PostFuncP) {
747 rio_spin_unlock_irqrestore(&UnixRupP->RupLock, flags);
748 (*CmdBlkP->PostFuncP) (CmdBlkP->PostArg, CmdBlkP);
749 rio_spin_lock_irqsave(&UnixRupP->RupLock, flags);
750 }
751
752 /*
753 ** ....clear the pending flag....
754 */
755 UnixRupP->CmdPendingP = NULL;
756
757 /*
758 ** ....and return the command block to the freelist.
759 */
760 RIOFreeCmdBlk(CmdBlkP);
761 }
762
763 /*
764 ** If there is a command for this rup, and the rup
765 ** is idle, then process the command
766 */
767 if ((CmdBlkP = UnixRupP->CmdsWaitingP) && /* ASSIGN! */
768 (UnixRupP->CmdPendingP == NULL) && (readw(&UnixRupP->RupP->txcontrol) == TX_RUP_INACTIVE)) {
769 /*
770 ** if the pre-function is non-zero, call it.
771 ** If it returns RIO_FAIL then don't
772 ** send this command yet!
773 */
774 if (!(CmdBlkP->PreFuncP ? (*CmdBlkP->PreFuncP) (CmdBlkP->PreArg, CmdBlkP) : 1)) {
775 rio_dprintk(RIO_DEBUG_CMD, "Not ready to start command %p\n", CmdBlkP);
776 } else {
777 rio_dprintk(RIO_DEBUG_CMD, "Start new command %p Cmd byte is 0x%x\n", CmdBlkP, CmdBlkP->Packet.data[0]);
778 /*
779 ** Whammy! blat that pack!
780 */
781 HostP->Copy(&CmdBlkP->Packet, RIO_PTR(HostP->Caddr, readw(&UnixRupP->RupP->txpkt)), sizeof(struct PKT));
782
783 /*
784 ** remove the command from the rup command queue...
785 */
786 UnixRupP->CmdsWaitingP = CmdBlkP->NextP;
787
788 /*
789 ** ...and place it on the pending position.
790 */
791 UnixRupP->CmdPendingP = CmdBlkP;
792
793 /*
794 ** set the command register
795 */
796 writew(TX_PACKET_READY, &UnixRupP->RupP->txcontrol);
797
798 /*
799 ** the command block will be freed
800 ** when the command has been processed.
801 */
802 }
803 }
804 spin_unlock_irqrestore(&UnixRupP->RupLock, flags);
805 } while (Rup);
806}
807
808int RIOWFlushMark(unsigned long iPortP, struct CmdBlk *CmdBlkP)
809{
810 struct Port *PortP = (struct Port *) iPortP;
811 unsigned long flags;
812
813 rio_spin_lock_irqsave(&PortP->portSem, flags);
814 PortP->WflushFlag++;
815 PortP->MagicFlags |= MAGIC_FLUSH;
816 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
817 return RIOUnUse(iPortP, CmdBlkP);
818}
819
820int RIORFlushEnable(unsigned long iPortP, struct CmdBlk *CmdBlkP)
821{
822 struct Port *PortP = (struct Port *) iPortP;
823 struct PKT __iomem *PacketP;
824 unsigned long flags;
825
826 rio_spin_lock_irqsave(&PortP->portSem, flags);
827
828 while (can_remove_receive(&PacketP, PortP)) {
829 remove_receive(PortP);
830 put_free_end(PortP->HostP, PacketP);
831 }
832
833 if (readw(&PortP->PhbP->handshake) == PHB_HANDSHAKE_SET) {
834 /*
835 ** MAGIC! (Basically, handshake the RX buffer, so that
836 ** the RTAs upstream can be re-enabled.)
837 */
838 rio_dprintk(RIO_DEBUG_CMD, "Util: Set RX handshake bit\n");
839 writew(PHB_HANDSHAKE_SET | PHB_HANDSHAKE_RESET, &PortP->PhbP->handshake);
840 }
841 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
842 return RIOUnUse(iPortP, CmdBlkP);
843}
844
845int RIOUnUse(unsigned long iPortP, struct CmdBlk *CmdBlkP)
846{
847 struct Port *PortP = (struct Port *) iPortP;
848 unsigned long flags;
849
850 rio_spin_lock_irqsave(&PortP->portSem, flags);
851
852 rio_dprintk(RIO_DEBUG_CMD, "Decrement in use count for port\n");
853
854 if (PortP->InUse) {
855 if (--PortP->InUse != NOT_INUSE) {
856 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
857 return 0;
858 }
859 }
860 /*
861 ** While PortP->InUse is set (i.e. a preemptive command has been sent to
862 ** the RTA and is awaiting completion), any transmit data is prevented from
863 ** being transferred from the write queue into the transmit packets
864 ** (add_transmit) and no furthur transmit interrupt will be sent for that
865 ** data. The next interrupt will occur up to 500ms later (RIOIntr is called
866 ** twice a second as a saftey measure). This was the case when kermit was
867 ** used to send data into a RIO port. After each packet was sent, TCFLSH
868 ** was called to flush the read queue preemptively. PortP->InUse was
869 ** incremented, thereby blocking the 6 byte acknowledgement packet
870 ** transmitted back. This acknowledgment hung around for 500ms before
871 ** being sent, thus reducing input performance substantially!.
872 ** When PortP->InUse becomes NOT_INUSE, we must ensure that any data
873 ** hanging around in the transmit buffer is sent immediately.
874 */
875 writew(1, &PortP->HostP->ParmMapP->tx_intr);
876 /* What to do here ..
877 wakeup( (caddr_t)&(PortP->InUse) );
878 */
879 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
880 return 0;
881}
882
883/*
884**
885** How to use this file:
886**
887** To send a command down a rup, you need to allocate a command block, fill
888** in the packet information, fill in the command number, fill in the pre-
889** and post- functions and arguments, and then add the command block to the
890** queue of command blocks for the port in question. When the port is idle,
891** then the pre-function will be called. If this returns RIO_FAIL then the
892** command will be re-queued and tried again at a later date (probably in one
893** clock tick). If the pre-function returns NOT RIO_FAIL, then the command
894** packet will be queued on the RUP, and the txcontrol field set to the
895** command number. When the txcontrol field has changed from being the
896** command number, then the post-function will be called, with the argument
897** specified earlier, a pointer to the command block, and the value of
898** txcontrol.
899**
900** To allocate a command block, call RIOGetCmdBlk(). This returns a pointer
901** to the command block structure allocated, or NULL if there aren't any.
902** The block will have been zeroed for you.
903**
904** The structure has the following fields:
905**
906** struct CmdBlk
907** {
908** struct CmdBlk *NextP; ** Pointer to next command block **
909** struct PKT Packet; ** A packet, to copy to the rup **
910** int (*PreFuncP)(); ** The func to call to check if OK **
911** int PreArg; ** The arg for the func **
912** int (*PostFuncP)(); ** The func to call when completed **
913** int PostArg; ** The arg for the func **
914** };
915**
916** You need to fill in ALL fields EXCEPT NextP, which is used to link the
917** blocks together either on the free list or on the Rup list.
918**
919** Packet is an actual packet structure to be filled in with the packet
920** information associated with the command. You need to fill in everything,
921** as the command processor doesn't process the command packet in any way.
922**
923** The PreFuncP is called before the packet is enqueued on the host rup.
924** PreFuncP is called as (*PreFuncP)(PreArg, CmdBlkP);. PreFuncP must
925** return !RIO_FAIL to have the packet queued on the rup, and RIO_FAIL
926** if the packet is NOT to be queued.
927**
928** The PostFuncP is called when the command has completed. It is called
929** as (*PostFuncP)(PostArg, CmdBlkP, txcontrol);. PostFuncP is not expected
930** to return a value. PostFuncP does NOT need to free the command block,
931** as this happens automatically after PostFuncP returns.
932**
933** Once the command block has been filled in, it is attached to the correct
934** queue by calling RIOQueueCmdBlk( HostP, Rup, CmdBlkP ) where HostP is
935** a pointer to the struct Host, Rup is the NUMBER of the rup (NOT a pointer
936** to it!), and CmdBlkP is the pointer to the command block allocated using
937** RIOGetCmdBlk().
938**
939*/
diff --git a/drivers/char/rio/rioctrl.c b/drivers/char/rio/rioctrl.c
deleted file mode 100644
index 780506326a73..000000000000
--- a/drivers/char/rio/rioctrl.c
+++ /dev/null
@@ -1,1504 +0,0 @@
1/*
2** -----------------------------------------------------------------------------
3**
4** Perle Specialix driver for Linux
5** Ported from existing RIO Driver for SCO sources.
6 *
7 * (C) 1990 - 2000 Specialix International Ltd., Byfleet, Surrey, UK.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22**
23** Module : rioctrl.c
24** SID : 1.3
25** Last Modified : 11/6/98 10:33:42
26** Retrieved : 11/6/98 10:33:49
27**
28** ident @(#)rioctrl.c 1.3
29**
30** -----------------------------------------------------------------------------
31*/
32
33#include <linux/module.h>
34#include <linux/sched.h>
35#include <linux/slab.h>
36#include <linux/errno.h>
37#include <asm/io.h>
38#include <asm/system.h>
39#include <asm/string.h>
40#include <asm/uaccess.h>
41
42#include <linux/termios.h>
43#include <linux/serial.h>
44
45#include <linux/generic_serial.h>
46
47
48#include "linux_compat.h"
49#include "rio_linux.h"
50#include "pkt.h"
51#include "daemon.h"
52#include "rio.h"
53#include "riospace.h"
54#include "cmdpkt.h"
55#include "map.h"
56#include "rup.h"
57#include "port.h"
58#include "riodrvr.h"
59#include "rioinfo.h"
60#include "func.h"
61#include "errors.h"
62#include "pci.h"
63
64#include "parmmap.h"
65#include "unixrup.h"
66#include "board.h"
67#include "host.h"
68#include "phb.h"
69#include "link.h"
70#include "cmdblk.h"
71#include "route.h"
72#include "cirrus.h"
73#include "rioioctl.h"
74
75
76static struct LpbReq LpbReq;
77static struct RupReq RupReq;
78static struct PortReq PortReq;
79static struct HostReq HostReq; /* oh really? global? and no locking? */
80static struct HostDpRam HostDpRam;
81static struct DebugCtrl DebugCtrl;
82static struct Map MapEnt;
83static struct PortSetup PortSetup;
84static struct DownLoad DownLoad;
85static struct SendPack SendPack;
86/* static struct StreamInfo StreamInfo; */
87/* static char modemtable[RIO_PORTS]; */
88static struct SpecialRupCmd SpecialRupCmd;
89static struct PortParams PortParams;
90static struct portStats portStats;
91
92static struct SubCmdStruct {
93 ushort Host;
94 ushort Rup;
95 ushort Port;
96 ushort Addr;
97} SubCmd;
98
99struct PortTty {
100 uint port;
101 struct ttystatics Tty;
102};
103
104static struct PortTty PortTty;
105typedef struct ttystatics TERMIO;
106
107/*
108** This table is used when the config.rio downloads bin code to the
109** driver. We index the table using the product code, 0-F, and call
110** the function pointed to by the entry, passing the information
111** about the boot.
112** The RIOBootCodeUNKNOWN entry is there to politely tell the calling
113** process to bog off.
114*/
115static int
116 (*RIOBootTable[MAX_PRODUCT]) (struct rio_info *, struct DownLoad *) = {
117 /* 0 */ RIOBootCodeHOST,
118 /* Host Card */
119 /* 1 */ RIOBootCodeRTA,
120 /* RTA */
121};
122
123#define drv_makedev(maj, min) ((((uint) maj & 0xff) << 8) | ((uint) min & 0xff))
124
125static int copy_from_io(void __user *to, void __iomem *from, size_t size)
126{
127 void *buf = kmalloc(size, GFP_KERNEL);
128 int res = -ENOMEM;
129 if (buf) {
130 rio_memcpy_fromio(buf, from, size);
131 res = copy_to_user(to, buf, size);
132 kfree(buf);
133 }
134 return res;
135}
136
137int riocontrol(struct rio_info *p, dev_t dev, int cmd, unsigned long arg, int su)
138{
139 uint Host; /* leave me unsigned! */
140 uint port; /* and me! */
141 struct Host *HostP;
142 ushort loop;
143 int Entry;
144 struct Port *PortP;
145 struct PKT __iomem *PacketP;
146 int retval = 0;
147 unsigned long flags;
148 void __user *argp = (void __user *)arg;
149
150 func_enter();
151
152 /* Confuse the compiler to think that we've initialized these */
153 Host = 0;
154 PortP = NULL;
155
156 rio_dprintk(RIO_DEBUG_CTRL, "control ioctl cmd: 0x%x arg: %p\n", cmd, argp);
157
158 switch (cmd) {
159 /*
160 ** RIO_SET_TIMER
161 **
162 ** Change the value of the host card interrupt timer.
163 ** If the host card number is -1 then all host cards are changed
164 ** otherwise just the specified host card will be changed.
165 */
166 case RIO_SET_TIMER:
167 rio_dprintk(RIO_DEBUG_CTRL, "RIO_SET_TIMER to %ldms\n", arg);
168 {
169 int host, value;
170 host = (arg >> 16) & 0x0000FFFF;
171 value = arg & 0x0000ffff;
172 if (host == -1) {
173 for (host = 0; host < p->RIONumHosts; host++) {
174 if (p->RIOHosts[host].Flags == RC_RUNNING) {
175 writew(value, &p->RIOHosts[host].ParmMapP->timer);
176 }
177 }
178 } else if (host >= p->RIONumHosts) {
179 return -EINVAL;
180 } else {
181 if (p->RIOHosts[host].Flags == RC_RUNNING) {
182 writew(value, &p->RIOHosts[host].ParmMapP->timer);
183 }
184 }
185 }
186 return 0;
187
188 case RIO_FOAD_RTA:
189 rio_dprintk(RIO_DEBUG_CTRL, "RIO_FOAD_RTA\n");
190 return RIOCommandRta(p, arg, RIOFoadRta);
191
192 case RIO_ZOMBIE_RTA:
193 rio_dprintk(RIO_DEBUG_CTRL, "RIO_ZOMBIE_RTA\n");
194 return RIOCommandRta(p, arg, RIOZombieRta);
195
196 case RIO_IDENTIFY_RTA:
197 rio_dprintk(RIO_DEBUG_CTRL, "RIO_IDENTIFY_RTA\n");
198 return RIOIdentifyRta(p, argp);
199
200 case RIO_KILL_NEIGHBOUR:
201 rio_dprintk(RIO_DEBUG_CTRL, "RIO_KILL_NEIGHBOUR\n");
202 return RIOKillNeighbour(p, argp);
203
204 case SPECIAL_RUP_CMD:
205 {
206 struct CmdBlk *CmdBlkP;
207
208 rio_dprintk(RIO_DEBUG_CTRL, "SPECIAL_RUP_CMD\n");
209 if (copy_from_user(&SpecialRupCmd, argp, sizeof(SpecialRupCmd))) {
210 rio_dprintk(RIO_DEBUG_CTRL, "SPECIAL_RUP_CMD copy failed\n");
211 p->RIOError.Error = COPYIN_FAILED;
212 return -EFAULT;
213 }
214 CmdBlkP = RIOGetCmdBlk();
215 if (!CmdBlkP) {
216 rio_dprintk(RIO_DEBUG_CTRL, "SPECIAL_RUP_CMD GetCmdBlk failed\n");
217 return -ENXIO;
218 }
219 CmdBlkP->Packet = SpecialRupCmd.Packet;
220 if (SpecialRupCmd.Host >= p->RIONumHosts)
221 SpecialRupCmd.Host = 0;
222 rio_dprintk(RIO_DEBUG_CTRL, "Queue special rup command for host %d rup %d\n", SpecialRupCmd.Host, SpecialRupCmd.RupNum);
223 if (RIOQueueCmdBlk(&p->RIOHosts[SpecialRupCmd.Host], SpecialRupCmd.RupNum, CmdBlkP) == RIO_FAIL) {
224 printk(KERN_WARNING "rio: FAILED TO QUEUE SPECIAL RUP COMMAND\n");
225 }
226 return 0;
227 }
228
229 case RIO_DEBUG_MEM:
230 return -EPERM;
231
232 case RIO_ALL_MODEM:
233 rio_dprintk(RIO_DEBUG_CTRL, "RIO_ALL_MODEM\n");
234 p->RIOError.Error = IOCTL_COMMAND_UNKNOWN;
235 return -EINVAL;
236
237 case RIO_GET_TABLE:
238 /*
239 ** Read the routing table from the device driver to user space
240 */
241 rio_dprintk(RIO_DEBUG_CTRL, "RIO_GET_TABLE\n");
242
243 if ((retval = RIOApel(p)) != 0)
244 return retval;
245
246 if (copy_to_user(argp, p->RIOConnectTable, TOTAL_MAP_ENTRIES * sizeof(struct Map))) {
247 rio_dprintk(RIO_DEBUG_CTRL, "RIO_GET_TABLE copy failed\n");
248 p->RIOError.Error = COPYOUT_FAILED;
249 return -EFAULT;
250 }
251
252 {
253 int entry;
254 rio_dprintk(RIO_DEBUG_CTRL, "*****\nMAP ENTRIES\n");
255 for (entry = 0; entry < TOTAL_MAP_ENTRIES; entry++) {
256 if ((p->RIOConnectTable[entry].ID == 0) && (p->RIOConnectTable[entry].HostUniqueNum == 0) && (p->RIOConnectTable[entry].RtaUniqueNum == 0))
257 continue;
258
259 rio_dprintk(RIO_DEBUG_CTRL, "Map entry %d.HostUniqueNum = 0x%x\n", entry, p->RIOConnectTable[entry].HostUniqueNum);
260 rio_dprintk(RIO_DEBUG_CTRL, "Map entry %d.RtaUniqueNum = 0x%x\n", entry, p->RIOConnectTable[entry].RtaUniqueNum);
261 rio_dprintk(RIO_DEBUG_CTRL, "Map entry %d.ID = 0x%x\n", entry, p->RIOConnectTable[entry].ID);
262 rio_dprintk(RIO_DEBUG_CTRL, "Map entry %d.ID2 = 0x%x\n", entry, p->RIOConnectTable[entry].ID2);
263 rio_dprintk(RIO_DEBUG_CTRL, "Map entry %d.Flags = 0x%x\n", entry, (int) p->RIOConnectTable[entry].Flags);
264 rio_dprintk(RIO_DEBUG_CTRL, "Map entry %d.SysPort = 0x%x\n", entry, (int) p->RIOConnectTable[entry].SysPort);
265 rio_dprintk(RIO_DEBUG_CTRL, "Map entry %d.Top[0].Unit = %x\n", entry, p->RIOConnectTable[entry].Topology[0].Unit);
266 rio_dprintk(RIO_DEBUG_CTRL, "Map entry %d.Top[0].Link = %x\n", entry, p->RIOConnectTable[entry].Topology[0].Link);
267 rio_dprintk(RIO_DEBUG_CTRL, "Map entry %d.Top[1].Unit = %x\n", entry, p->RIOConnectTable[entry].Topology[1].Unit);
268 rio_dprintk(RIO_DEBUG_CTRL, "Map entry %d.Top[1].Link = %x\n", entry, p->RIOConnectTable[entry].Topology[1].Link);
269 rio_dprintk(RIO_DEBUG_CTRL, "Map entry %d.Top[2].Unit = %x\n", entry, p->RIOConnectTable[entry].Topology[2].Unit);
270 rio_dprintk(RIO_DEBUG_CTRL, "Map entry %d.Top[2].Link = %x\n", entry, p->RIOConnectTable[entry].Topology[2].Link);
271 rio_dprintk(RIO_DEBUG_CTRL, "Map entry %d.Top[3].Unit = %x\n", entry, p->RIOConnectTable[entry].Topology[3].Unit);
272 rio_dprintk(RIO_DEBUG_CTRL, "Map entry %d.Top[4].Link = %x\n", entry, p->RIOConnectTable[entry].Topology[3].Link);
273 rio_dprintk(RIO_DEBUG_CTRL, "Map entry %d.Name = %s\n", entry, p->RIOConnectTable[entry].Name);
274 }
275 rio_dprintk(RIO_DEBUG_CTRL, "*****\nEND MAP ENTRIES\n");
276 }
277 p->RIOQuickCheck = NOT_CHANGED; /* a table has been gotten */
278 return 0;
279
280 case RIO_PUT_TABLE:
281 /*
282 ** Write the routing table to the device driver from user space
283 */
284 rio_dprintk(RIO_DEBUG_CTRL, "RIO_PUT_TABLE\n");
285
286 if (!su) {
287 rio_dprintk(RIO_DEBUG_CTRL, "RIO_PUT_TABLE !Root\n");
288 p->RIOError.Error = NOT_SUPER_USER;
289 return -EPERM;
290 }
291 if (copy_from_user(&p->RIOConnectTable[0], argp, TOTAL_MAP_ENTRIES * sizeof(struct Map))) {
292 rio_dprintk(RIO_DEBUG_CTRL, "RIO_PUT_TABLE copy failed\n");
293 p->RIOError.Error = COPYIN_FAILED;
294 return -EFAULT;
295 }
296/*
297***********************************
298 {
299 int entry;
300 rio_dprint(RIO_DEBUG_CTRL, ("*****\nMAP ENTRIES\n") );
301 for ( entry=0; entry<TOTAL_MAP_ENTRIES; entry++ )
302 {
303 rio_dprint(RIO_DEBUG_CTRL, ("Map entry %d.HostUniqueNum = 0x%x\n", entry, p->RIOConnectTable[entry].HostUniqueNum ) );
304 rio_dprint(RIO_DEBUG_CTRL, ("Map entry %d.RtaUniqueNum = 0x%x\n", entry, p->RIOConnectTable[entry].RtaUniqueNum ) );
305 rio_dprint(RIO_DEBUG_CTRL, ("Map entry %d.ID = 0x%x\n", entry, p->RIOConnectTable[entry].ID ) );
306 rio_dprint(RIO_DEBUG_CTRL, ("Map entry %d.ID2 = 0x%x\n", entry, p->RIOConnectTable[entry].ID2 ) );
307 rio_dprint(RIO_DEBUG_CTRL, ("Map entry %d.Flags = 0x%x\n", entry, p->RIOConnectTable[entry].Flags ) );
308 rio_dprint(RIO_DEBUG_CTRL, ("Map entry %d.SysPort = 0x%x\n", entry, p->RIOConnectTable[entry].SysPort ) );
309 rio_dprint(RIO_DEBUG_CTRL, ("Map entry %d.Top[0].Unit = %b\n", entry, p->RIOConnectTable[entry].Topology[0].Unit ) );
310 rio_dprint(RIO_DEBUG_CTRL, ("Map entry %d.Top[0].Link = %b\n", entry, p->RIOConnectTable[entry].Topology[0].Link ) );
311 rio_dprint(RIO_DEBUG_CTRL, ("Map entry %d.Top[1].Unit = %b\n", entry, p->RIOConnectTable[entry].Topology[1].Unit ) );
312 rio_dprint(RIO_DEBUG_CTRL, ("Map entry %d.Top[1].Link = %b\n", entry, p->RIOConnectTable[entry].Topology[1].Link ) );
313 rio_dprint(RIO_DEBUG_CTRL, ("Map entry %d.Top[2].Unit = %b\n", entry, p->RIOConnectTable[entry].Topology[2].Unit ) );
314 rio_dprint(RIO_DEBUG_CTRL, ("Map entry %d.Top[2].Link = %b\n", entry, p->RIOConnectTable[entry].Topology[2].Link ) );
315 rio_dprint(RIO_DEBUG_CTRL, ("Map entry %d.Top[3].Unit = %b\n", entry, p->RIOConnectTable[entry].Topology[3].Unit ) );
316 rio_dprint(RIO_DEBUG_CTRL, ("Map entry %d.Top[4].Link = %b\n", entry, p->RIOConnectTable[entry].Topology[3].Link ) );
317 rio_dprint(RIO_DEBUG_CTRL, ("Map entry %d.Name = %s\n", entry, p->RIOConnectTable[entry].Name ) );
318 }
319 rio_dprint(RIO_DEBUG_CTRL, ("*****\nEND MAP ENTRIES\n") );
320 }
321***********************************
322*/
323 return RIONewTable(p);
324
325 case RIO_GET_BINDINGS:
326 /*
327 ** Send bindings table, containing unique numbers of RTAs owned
328 ** by this system to user space
329 */
330 rio_dprintk(RIO_DEBUG_CTRL, "RIO_GET_BINDINGS\n");
331
332 if (!su) {
333 rio_dprintk(RIO_DEBUG_CTRL, "RIO_GET_BINDINGS !Root\n");
334 p->RIOError.Error = NOT_SUPER_USER;
335 return -EPERM;
336 }
337 if (copy_to_user(argp, p->RIOBindTab, (sizeof(ulong) * MAX_RTA_BINDINGS))) {
338 rio_dprintk(RIO_DEBUG_CTRL, "RIO_GET_BINDINGS copy failed\n");
339 p->RIOError.Error = COPYOUT_FAILED;
340 return -EFAULT;
341 }
342 return 0;
343
344 case RIO_PUT_BINDINGS:
345 /*
346 ** Receive a bindings table, containing unique numbers of RTAs owned
347 ** by this system
348 */
349 rio_dprintk(RIO_DEBUG_CTRL, "RIO_PUT_BINDINGS\n");
350
351 if (!su) {
352 rio_dprintk(RIO_DEBUG_CTRL, "RIO_PUT_BINDINGS !Root\n");
353 p->RIOError.Error = NOT_SUPER_USER;
354 return -EPERM;
355 }
356 if (copy_from_user(&p->RIOBindTab[0], argp, (sizeof(ulong) * MAX_RTA_BINDINGS))) {
357 rio_dprintk(RIO_DEBUG_CTRL, "RIO_PUT_BINDINGS copy failed\n");
358 p->RIOError.Error = COPYIN_FAILED;
359 return -EFAULT;
360 }
361 return 0;
362
363 case RIO_BIND_RTA:
364 {
365 int EmptySlot = -1;
366 /*
367 ** Bind this RTA to host, so that it will be booted by
368 ** host in 'boot owned RTAs' mode.
369 */
370 rio_dprintk(RIO_DEBUG_CTRL, "RIO_BIND_RTA\n");
371
372 if (!su) {
373 rio_dprintk(RIO_DEBUG_CTRL, "RIO_BIND_RTA !Root\n");
374 p->RIOError.Error = NOT_SUPER_USER;
375 return -EPERM;
376 }
377 for (Entry = 0; Entry < MAX_RTA_BINDINGS; Entry++) {
378 if ((EmptySlot == -1) && (p->RIOBindTab[Entry] == 0L))
379 EmptySlot = Entry;
380 else if (p->RIOBindTab[Entry] == arg) {
381 /*
382 ** Already exists - delete
383 */
384 p->RIOBindTab[Entry] = 0L;
385 rio_dprintk(RIO_DEBUG_CTRL, "Removing Rta %ld from p->RIOBindTab\n", arg);
386 return 0;
387 }
388 }
389 /*
390 ** Dosen't exist - add
391 */
392 if (EmptySlot != -1) {
393 p->RIOBindTab[EmptySlot] = arg;
394 rio_dprintk(RIO_DEBUG_CTRL, "Adding Rta %lx to p->RIOBindTab\n", arg);
395 } else {
396 rio_dprintk(RIO_DEBUG_CTRL, "p->RIOBindTab full! - Rta %lx not added\n", arg);
397 return -ENOMEM;
398 }
399 return 0;
400 }
401
402 case RIO_RESUME:
403 rio_dprintk(RIO_DEBUG_CTRL, "RIO_RESUME\n");
404 port = arg;
405 if ((port < 0) || (port > 511)) {
406 rio_dprintk(RIO_DEBUG_CTRL, "RIO_RESUME: Bad port number %d\n", port);
407 p->RIOError.Error = PORT_NUMBER_OUT_OF_RANGE;
408 return -EINVAL;
409 }
410 PortP = p->RIOPortp[port];
411 if (!PortP->Mapped) {
412 rio_dprintk(RIO_DEBUG_CTRL, "RIO_RESUME: Port %d not mapped\n", port);
413 p->RIOError.Error = PORT_NOT_MAPPED_INTO_SYSTEM;
414 return -EINVAL;
415 }
416 if (!(PortP->State & (RIO_LOPEN | RIO_MOPEN))) {
417 rio_dprintk(RIO_DEBUG_CTRL, "RIO_RESUME: Port %d not open\n", port);
418 return -EINVAL;
419 }
420
421 rio_spin_lock_irqsave(&PortP->portSem, flags);
422 if (RIOPreemptiveCmd(p, (p->RIOPortp[port]), RIOC_RESUME) ==
423 RIO_FAIL) {
424 rio_dprintk(RIO_DEBUG_CTRL, "RIO_RESUME failed\n");
425 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
426 return -EBUSY;
427 } else {
428 rio_dprintk(RIO_DEBUG_CTRL, "RIO_RESUME: Port %d resumed\n", port);
429 PortP->State |= RIO_BUSY;
430 }
431 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
432 return retval;
433
434 case RIO_ASSIGN_RTA:
435 rio_dprintk(RIO_DEBUG_CTRL, "RIO_ASSIGN_RTA\n");
436 if (!su) {
437 rio_dprintk(RIO_DEBUG_CTRL, "RIO_ASSIGN_RTA !Root\n");
438 p->RIOError.Error = NOT_SUPER_USER;
439 return -EPERM;
440 }
441 if (copy_from_user(&MapEnt, argp, sizeof(MapEnt))) {
442 rio_dprintk(RIO_DEBUG_CTRL, "Copy from user space failed\n");
443 p->RIOError.Error = COPYIN_FAILED;
444 return -EFAULT;
445 }
446 return RIOAssignRta(p, &MapEnt);
447
448 case RIO_CHANGE_NAME:
449 rio_dprintk(RIO_DEBUG_CTRL, "RIO_CHANGE_NAME\n");
450 if (!su) {
451 rio_dprintk(RIO_DEBUG_CTRL, "RIO_CHANGE_NAME !Root\n");
452 p->RIOError.Error = NOT_SUPER_USER;
453 return -EPERM;
454 }
455 if (copy_from_user(&MapEnt, argp, sizeof(MapEnt))) {
456 rio_dprintk(RIO_DEBUG_CTRL, "Copy from user space failed\n");
457 p->RIOError.Error = COPYIN_FAILED;
458 return -EFAULT;
459 }
460 return RIOChangeName(p, &MapEnt);
461
462 case RIO_DELETE_RTA:
463 rio_dprintk(RIO_DEBUG_CTRL, "RIO_DELETE_RTA\n");
464 if (!su) {
465 rio_dprintk(RIO_DEBUG_CTRL, "RIO_DELETE_RTA !Root\n");
466 p->RIOError.Error = NOT_SUPER_USER;
467 return -EPERM;
468 }
469 if (copy_from_user(&MapEnt, argp, sizeof(MapEnt))) {
470 rio_dprintk(RIO_DEBUG_CTRL, "Copy from data space failed\n");
471 p->RIOError.Error = COPYIN_FAILED;
472 return -EFAULT;
473 }
474 return RIODeleteRta(p, &MapEnt);
475
476 case RIO_QUICK_CHECK:
477 if (copy_to_user(argp, &p->RIORtaDisCons, sizeof(unsigned int))) {
478 p->RIOError.Error = COPYOUT_FAILED;
479 return -EFAULT;
480 }
481 return 0;
482
483 case RIO_LAST_ERROR:
484 if (copy_to_user(argp, &p->RIOError, sizeof(struct Error)))
485 return -EFAULT;
486 return 0;
487
488 case RIO_GET_LOG:
489 rio_dprintk(RIO_DEBUG_CTRL, "RIO_GET_LOG\n");
490 return -EINVAL;
491
492 case RIO_GET_MODTYPE:
493 if (copy_from_user(&port, argp, sizeof(unsigned int))) {
494 p->RIOError.Error = COPYIN_FAILED;
495 return -EFAULT;
496 }
497 rio_dprintk(RIO_DEBUG_CTRL, "Get module type for port %d\n", port);
498 if (port < 0 || port > 511) {
499 rio_dprintk(RIO_DEBUG_CTRL, "RIO_GET_MODTYPE: Bad port number %d\n", port);
500 p->RIOError.Error = PORT_NUMBER_OUT_OF_RANGE;
501 return -EINVAL;
502 }
503 PortP = (p->RIOPortp[port]);
504 if (!PortP->Mapped) {
505 rio_dprintk(RIO_DEBUG_CTRL, "RIO_GET_MODTYPE: Port %d not mapped\n", port);
506 p->RIOError.Error = PORT_NOT_MAPPED_INTO_SYSTEM;
507 return -EINVAL;
508 }
509 /*
510 ** Return module type of port
511 */
512 port = PortP->HostP->UnixRups[PortP->RupNum].ModTypes;
513 if (copy_to_user(argp, &port, sizeof(unsigned int))) {
514 p->RIOError.Error = COPYOUT_FAILED;
515 return -EFAULT;
516 }
517 return (0);
518 case RIO_BLOCK_OPENS:
519 rio_dprintk(RIO_DEBUG_CTRL, "Opens block until booted\n");
520 for (Entry = 0; Entry < RIO_PORTS; Entry++) {
521 rio_spin_lock_irqsave(&PortP->portSem, flags);
522 p->RIOPortp[Entry]->WaitUntilBooted = 1;
523 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
524 }
525 return 0;
526
527 case RIO_SETUP_PORTS:
528 rio_dprintk(RIO_DEBUG_CTRL, "Setup ports\n");
529 if (copy_from_user(&PortSetup, argp, sizeof(PortSetup))) {
530 p->RIOError.Error = COPYIN_FAILED;
531 rio_dprintk(RIO_DEBUG_CTRL, "EFAULT");
532 return -EFAULT;
533 }
534 if (PortSetup.From > PortSetup.To || PortSetup.To >= RIO_PORTS) {
535 p->RIOError.Error = PORT_NUMBER_OUT_OF_RANGE;
536 rio_dprintk(RIO_DEBUG_CTRL, "ENXIO");
537 return -ENXIO;
538 }
539 if (PortSetup.XpCps > p->RIOConf.MaxXpCps || PortSetup.XpCps < p->RIOConf.MinXpCps) {
540 p->RIOError.Error = XPRINT_CPS_OUT_OF_RANGE;
541 rio_dprintk(RIO_DEBUG_CTRL, "EINVAL");
542 return -EINVAL;
543 }
544 if (!p->RIOPortp) {
545 printk(KERN_ERR "rio: No p->RIOPortp array!\n");
546 rio_dprintk(RIO_DEBUG_CTRL, "No p->RIOPortp array!\n");
547 return -EIO;
548 }
549 rio_dprintk(RIO_DEBUG_CTRL, "entering loop (%d %d)!\n", PortSetup.From, PortSetup.To);
550 for (loop = PortSetup.From; loop <= PortSetup.To; loop++) {
551 rio_dprintk(RIO_DEBUG_CTRL, "in loop (%d)!\n", loop);
552 }
553 rio_dprintk(RIO_DEBUG_CTRL, "after loop (%d)!\n", loop);
554 rio_dprintk(RIO_DEBUG_CTRL, "Retval:%x\n", retval);
555 return retval;
556
557 case RIO_GET_PORT_SETUP:
558 rio_dprintk(RIO_DEBUG_CTRL, "Get port setup\n");
559 if (copy_from_user(&PortSetup, argp, sizeof(PortSetup))) {
560 p->RIOError.Error = COPYIN_FAILED;
561 return -EFAULT;
562 }
563 if (PortSetup.From >= RIO_PORTS) {
564 p->RIOError.Error = PORT_NUMBER_OUT_OF_RANGE;
565 return -ENXIO;
566 }
567
568 port = PortSetup.To = PortSetup.From;
569 PortSetup.IxAny = (p->RIOPortp[port]->Config & RIO_IXANY) ? 1 : 0;
570 PortSetup.IxOn = (p->RIOPortp[port]->Config & RIO_IXON) ? 1 : 0;
571 PortSetup.Drain = (p->RIOPortp[port]->Config & RIO_WAITDRAIN) ? 1 : 0;
572 PortSetup.Store = p->RIOPortp[port]->Store;
573 PortSetup.Lock = p->RIOPortp[port]->Lock;
574 PortSetup.XpCps = p->RIOPortp[port]->Xprint.XpCps;
575 memcpy(PortSetup.XpOn, p->RIOPortp[port]->Xprint.XpOn, MAX_XP_CTRL_LEN);
576 memcpy(PortSetup.XpOff, p->RIOPortp[port]->Xprint.XpOff, MAX_XP_CTRL_LEN);
577 PortSetup.XpOn[MAX_XP_CTRL_LEN - 1] = '\0';
578 PortSetup.XpOff[MAX_XP_CTRL_LEN - 1] = '\0';
579
580 if (copy_to_user(argp, &PortSetup, sizeof(PortSetup))) {
581 p->RIOError.Error = COPYOUT_FAILED;
582 return -EFAULT;
583 }
584 return retval;
585
586 case RIO_GET_PORT_PARAMS:
587 rio_dprintk(RIO_DEBUG_CTRL, "Get port params\n");
588 if (copy_from_user(&PortParams, argp, sizeof(struct PortParams))) {
589 p->RIOError.Error = COPYIN_FAILED;
590 return -EFAULT;
591 }
592 if (PortParams.Port >= RIO_PORTS) {
593 p->RIOError.Error = PORT_NUMBER_OUT_OF_RANGE;
594 return -ENXIO;
595 }
596 PortP = (p->RIOPortp[PortParams.Port]);
597 PortParams.Config = PortP->Config;
598 PortParams.State = PortP->State;
599 rio_dprintk(RIO_DEBUG_CTRL, "Port %d\n", PortParams.Port);
600
601 if (copy_to_user(argp, &PortParams, sizeof(struct PortParams))) {
602 p->RIOError.Error = COPYOUT_FAILED;
603 return -EFAULT;
604 }
605 return retval;
606
607 case RIO_GET_PORT_TTY:
608 rio_dprintk(RIO_DEBUG_CTRL, "Get port tty\n");
609 if (copy_from_user(&PortTty, argp, sizeof(struct PortTty))) {
610 p->RIOError.Error = COPYIN_FAILED;
611 return -EFAULT;
612 }
613 if (PortTty.port >= RIO_PORTS) {
614 p->RIOError.Error = PORT_NUMBER_OUT_OF_RANGE;
615 return -ENXIO;
616 }
617
618 rio_dprintk(RIO_DEBUG_CTRL, "Port %d\n", PortTty.port);
619 PortP = (p->RIOPortp[PortTty.port]);
620 if (copy_to_user(argp, &PortTty, sizeof(struct PortTty))) {
621 p->RIOError.Error = COPYOUT_FAILED;
622 return -EFAULT;
623 }
624 return retval;
625
626 case RIO_SET_PORT_TTY:
627 if (copy_from_user(&PortTty, argp, sizeof(struct PortTty))) {
628 p->RIOError.Error = COPYIN_FAILED;
629 return -EFAULT;
630 }
631 rio_dprintk(RIO_DEBUG_CTRL, "Set port %d tty\n", PortTty.port);
632 if (PortTty.port >= (ushort) RIO_PORTS) {
633 p->RIOError.Error = PORT_NUMBER_OUT_OF_RANGE;
634 return -ENXIO;
635 }
636 PortP = (p->RIOPortp[PortTty.port]);
637 RIOParam(PortP, RIOC_CONFIG, PortP->State & RIO_MODEM,
638 OK_TO_SLEEP);
639 return retval;
640
641 case RIO_SET_PORT_PARAMS:
642 rio_dprintk(RIO_DEBUG_CTRL, "Set port params\n");
643 if (copy_from_user(&PortParams, argp, sizeof(PortParams))) {
644 p->RIOError.Error = COPYIN_FAILED;
645 return -EFAULT;
646 }
647 if (PortParams.Port >= (ushort) RIO_PORTS) {
648 p->RIOError.Error = PORT_NUMBER_OUT_OF_RANGE;
649 return -ENXIO;
650 }
651 PortP = (p->RIOPortp[PortParams.Port]);
652 rio_spin_lock_irqsave(&PortP->portSem, flags);
653 PortP->Config = PortParams.Config;
654 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
655 return retval;
656
657 case RIO_GET_PORT_STATS:
658 rio_dprintk(RIO_DEBUG_CTRL, "RIO_GET_PORT_STATS\n");
659 if (copy_from_user(&portStats, argp, sizeof(struct portStats))) {
660 p->RIOError.Error = COPYIN_FAILED;
661 return -EFAULT;
662 }
663 if (portStats.port < 0 || portStats.port >= RIO_PORTS) {
664 p->RIOError.Error = PORT_NUMBER_OUT_OF_RANGE;
665 return -ENXIO;
666 }
667 PortP = (p->RIOPortp[portStats.port]);
668 portStats.gather = PortP->statsGather;
669 portStats.txchars = PortP->txchars;
670 portStats.rxchars = PortP->rxchars;
671 portStats.opens = PortP->opens;
672 portStats.closes = PortP->closes;
673 portStats.ioctls = PortP->ioctls;
674 if (copy_to_user(argp, &portStats, sizeof(struct portStats))) {
675 p->RIOError.Error = COPYOUT_FAILED;
676 return -EFAULT;
677 }
678 return retval;
679
680 case RIO_RESET_PORT_STATS:
681 port = arg;
682 rio_dprintk(RIO_DEBUG_CTRL, "RIO_RESET_PORT_STATS\n");
683 if (port >= RIO_PORTS) {
684 p->RIOError.Error = PORT_NUMBER_OUT_OF_RANGE;
685 return -ENXIO;
686 }
687 PortP = (p->RIOPortp[port]);
688 rio_spin_lock_irqsave(&PortP->portSem, flags);
689 PortP->txchars = 0;
690 PortP->rxchars = 0;
691 PortP->opens = 0;
692 PortP->closes = 0;
693 PortP->ioctls = 0;
694 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
695 return retval;
696
697 case RIO_GATHER_PORT_STATS:
698 rio_dprintk(RIO_DEBUG_CTRL, "RIO_GATHER_PORT_STATS\n");
699 if (copy_from_user(&portStats, argp, sizeof(struct portStats))) {
700 p->RIOError.Error = COPYIN_FAILED;
701 return -EFAULT;
702 }
703 if (portStats.port < 0 || portStats.port >= RIO_PORTS) {
704 p->RIOError.Error = PORT_NUMBER_OUT_OF_RANGE;
705 return -ENXIO;
706 }
707 PortP = (p->RIOPortp[portStats.port]);
708 rio_spin_lock_irqsave(&PortP->portSem, flags);
709 PortP->statsGather = portStats.gather;
710 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
711 return retval;
712
713 case RIO_READ_CONFIG:
714 rio_dprintk(RIO_DEBUG_CTRL, "RIO_READ_CONFIG\n");
715 if (copy_to_user(argp, &p->RIOConf, sizeof(struct Conf))) {
716 p->RIOError.Error = COPYOUT_FAILED;
717 return -EFAULT;
718 }
719 return retval;
720
721 case RIO_SET_CONFIG:
722 rio_dprintk(RIO_DEBUG_CTRL, "RIO_SET_CONFIG\n");
723 if (!su) {
724 p->RIOError.Error = NOT_SUPER_USER;
725 return -EPERM;
726 }
727 if (copy_from_user(&p->RIOConf, argp, sizeof(struct Conf))) {
728 p->RIOError.Error = COPYIN_FAILED;
729 return -EFAULT;
730 }
731 /*
732 ** move a few value around
733 */
734 for (Host = 0; Host < p->RIONumHosts; Host++)
735 if ((p->RIOHosts[Host].Flags & RUN_STATE) == RC_RUNNING)
736 writew(p->RIOConf.Timer, &p->RIOHosts[Host].ParmMapP->timer);
737 return retval;
738
739 case RIO_START_POLLER:
740 rio_dprintk(RIO_DEBUG_CTRL, "RIO_START_POLLER\n");
741 return -EINVAL;
742
743 case RIO_STOP_POLLER:
744 rio_dprintk(RIO_DEBUG_CTRL, "RIO_STOP_POLLER\n");
745 if (!su) {
746 p->RIOError.Error = NOT_SUPER_USER;
747 return -EPERM;
748 }
749 p->RIOPolling = NOT_POLLING;
750 return retval;
751
752 case RIO_SETDEBUG:
753 case RIO_GETDEBUG:
754 rio_dprintk(RIO_DEBUG_CTRL, "RIO_SETDEBUG/RIO_GETDEBUG\n");
755 if (copy_from_user(&DebugCtrl, argp, sizeof(DebugCtrl))) {
756 p->RIOError.Error = COPYIN_FAILED;
757 return -EFAULT;
758 }
759 if (DebugCtrl.SysPort == NO_PORT) {
760 if (cmd == RIO_SETDEBUG) {
761 if (!su) {
762 p->RIOError.Error = NOT_SUPER_USER;
763 return -EPERM;
764 }
765 p->rio_debug = DebugCtrl.Debug;
766 p->RIODebugWait = DebugCtrl.Wait;
767 rio_dprintk(RIO_DEBUG_CTRL, "Set global debug to 0x%x set wait to 0x%x\n", p->rio_debug, p->RIODebugWait);
768 } else {
769 rio_dprintk(RIO_DEBUG_CTRL, "Get global debug 0x%x wait 0x%x\n", p->rio_debug, p->RIODebugWait);
770 DebugCtrl.Debug = p->rio_debug;
771 DebugCtrl.Wait = p->RIODebugWait;
772 if (copy_to_user(argp, &DebugCtrl, sizeof(DebugCtrl))) {
773 rio_dprintk(RIO_DEBUG_CTRL, "RIO_SET/GET DEBUG: bad port number %d\n", DebugCtrl.SysPort);
774 p->RIOError.Error = COPYOUT_FAILED;
775 return -EFAULT;
776 }
777 }
778 } else if (DebugCtrl.SysPort >= RIO_PORTS && DebugCtrl.SysPort != NO_PORT) {
779 rio_dprintk(RIO_DEBUG_CTRL, "RIO_SET/GET DEBUG: bad port number %d\n", DebugCtrl.SysPort);
780 p->RIOError.Error = PORT_NUMBER_OUT_OF_RANGE;
781 return -ENXIO;
782 } else if (cmd == RIO_SETDEBUG) {
783 if (!su) {
784 p->RIOError.Error = NOT_SUPER_USER;
785 return -EPERM;
786 }
787 rio_spin_lock_irqsave(&PortP->portSem, flags);
788 p->RIOPortp[DebugCtrl.SysPort]->Debug = DebugCtrl.Debug;
789 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
790 rio_dprintk(RIO_DEBUG_CTRL, "RIO_SETDEBUG 0x%x\n", p->RIOPortp[DebugCtrl.SysPort]->Debug);
791 } else {
792 rio_dprintk(RIO_DEBUG_CTRL, "RIO_GETDEBUG 0x%x\n", p->RIOPortp[DebugCtrl.SysPort]->Debug);
793 DebugCtrl.Debug = p->RIOPortp[DebugCtrl.SysPort]->Debug;
794 if (copy_to_user(argp, &DebugCtrl, sizeof(DebugCtrl))) {
795 rio_dprintk(RIO_DEBUG_CTRL, "RIO_GETDEBUG: Bad copy to user space\n");
796 p->RIOError.Error = COPYOUT_FAILED;
797 return -EFAULT;
798 }
799 }
800 return retval;
801
802 case RIO_VERSID:
803 /*
804 ** Enquire about the release and version.
805 ** We return MAX_VERSION_LEN bytes, being a
806 ** textual null terminated string.
807 */
808 rio_dprintk(RIO_DEBUG_CTRL, "RIO_VERSID\n");
809 if (copy_to_user(argp, RIOVersid(), sizeof(struct rioVersion))) {
810 rio_dprintk(RIO_DEBUG_CTRL, "RIO_VERSID: Bad copy to user space (host=%d)\n", Host);
811 p->RIOError.Error = COPYOUT_FAILED;
812 return -EFAULT;
813 }
814 return retval;
815
816 case RIO_NUM_HOSTS:
817 /*
818 ** Enquire as to the number of hosts located
819 ** at init time.
820 */
821 rio_dprintk(RIO_DEBUG_CTRL, "RIO_NUM_HOSTS\n");
822 if (copy_to_user(argp, &p->RIONumHosts, sizeof(p->RIONumHosts))) {
823 rio_dprintk(RIO_DEBUG_CTRL, "RIO_NUM_HOSTS: Bad copy to user space\n");
824 p->RIOError.Error = COPYOUT_FAILED;
825 return -EFAULT;
826 }
827 return retval;
828
829 case RIO_HOST_FOAD:
830 /*
831 ** Kill host. This may not be in the final version...
832 */
833 rio_dprintk(RIO_DEBUG_CTRL, "RIO_HOST_FOAD %ld\n", arg);
834 if (!su) {
835 rio_dprintk(RIO_DEBUG_CTRL, "RIO_HOST_FOAD: Not super user\n");
836 p->RIOError.Error = NOT_SUPER_USER;
837 return -EPERM;
838 }
839 p->RIOHalted = 1;
840 p->RIOSystemUp = 0;
841
842 for (Host = 0; Host < p->RIONumHosts; Host++) {
843 (void) RIOBoardTest(p->RIOHosts[Host].PaddrP, p->RIOHosts[Host].Caddr, p->RIOHosts[Host].Type, p->RIOHosts[Host].Slot);
844 memset(&p->RIOHosts[Host].Flags, 0, ((char *) &p->RIOHosts[Host].____end_marker____) - ((char *) &p->RIOHosts[Host].Flags));
845 p->RIOHosts[Host].Flags = RC_WAITING;
846 }
847 RIOFoadWakeup(p);
848 p->RIONumBootPkts = 0;
849 p->RIOBooting = 0;
850 printk("HEEEEELP!\n");
851
852 for (loop = 0; loop < RIO_PORTS; loop++) {
853 spin_lock_init(&p->RIOPortp[loop]->portSem);
854 p->RIOPortp[loop]->InUse = NOT_INUSE;
855 }
856
857 p->RIOSystemUp = 0;
858 return retval;
859
860 case RIO_DOWNLOAD:
861 rio_dprintk(RIO_DEBUG_CTRL, "RIO_DOWNLOAD\n");
862 if (!su) {
863 rio_dprintk(RIO_DEBUG_CTRL, "RIO_DOWNLOAD: Not super user\n");
864 p->RIOError.Error = NOT_SUPER_USER;
865 return -EPERM;
866 }
867 if (copy_from_user(&DownLoad, argp, sizeof(DownLoad))) {
868 rio_dprintk(RIO_DEBUG_CTRL, "RIO_DOWNLOAD: Copy in from user space failed\n");
869 p->RIOError.Error = COPYIN_FAILED;
870 return -EFAULT;
871 }
872 rio_dprintk(RIO_DEBUG_CTRL, "Copied in download code for product code 0x%x\n", DownLoad.ProductCode);
873
874 /*
875 ** It is important that the product code is an unsigned object!
876 */
877 if (DownLoad.ProductCode >= MAX_PRODUCT) {
878 rio_dprintk(RIO_DEBUG_CTRL, "RIO_DOWNLOAD: Bad product code %d passed\n", DownLoad.ProductCode);
879 p->RIOError.Error = NO_SUCH_PRODUCT;
880 return -ENXIO;
881 }
882 /*
883 ** do something!
884 */
885 retval = (*(RIOBootTable[DownLoad.ProductCode])) (p, &DownLoad);
886 /* <-- Panic */
887 p->RIOHalted = 0;
888 /*
889 ** and go back, content with a job well completed.
890 */
891 return retval;
892
893 case RIO_PARMS:
894 {
895 unsigned int host;
896
897 if (copy_from_user(&host, argp, sizeof(host))) {
898 rio_dprintk(RIO_DEBUG_CTRL, "RIO_HOST_REQ: Copy in from user space failed\n");
899 p->RIOError.Error = COPYIN_FAILED;
900 return -EFAULT;
901 }
902 /*
903 ** Fetch the parmmap
904 */
905 rio_dprintk(RIO_DEBUG_CTRL, "RIO_PARMS\n");
906 if (copy_from_io(argp, p->RIOHosts[host].ParmMapP, sizeof(PARM_MAP))) {
907 p->RIOError.Error = COPYOUT_FAILED;
908 rio_dprintk(RIO_DEBUG_CTRL, "RIO_PARMS: Copy out to user space failed\n");
909 return -EFAULT;
910 }
911 }
912 return retval;
913
914 case RIO_HOST_REQ:
915 rio_dprintk(RIO_DEBUG_CTRL, "RIO_HOST_REQ\n");
916 if (copy_from_user(&HostReq, argp, sizeof(HostReq))) {
917 rio_dprintk(RIO_DEBUG_CTRL, "RIO_HOST_REQ: Copy in from user space failed\n");
918 p->RIOError.Error = COPYIN_FAILED;
919 return -EFAULT;
920 }
921 if (HostReq.HostNum >= p->RIONumHosts) {
922 p->RIOError.Error = HOST_NUMBER_OUT_OF_RANGE;
923 rio_dprintk(RIO_DEBUG_CTRL, "RIO_HOST_REQ: Illegal host number %d\n", HostReq.HostNum);
924 return -ENXIO;
925 }
926 rio_dprintk(RIO_DEBUG_CTRL, "Request for host %d\n", HostReq.HostNum);
927
928 if (copy_to_user(HostReq.HostP, &p->RIOHosts[HostReq.HostNum], sizeof(struct Host))) {
929 p->RIOError.Error = COPYOUT_FAILED;
930 rio_dprintk(RIO_DEBUG_CTRL, "RIO_HOST_REQ: Bad copy to user space\n");
931 return -EFAULT;
932 }
933 return retval;
934
935 case RIO_HOST_DPRAM:
936 rio_dprintk(RIO_DEBUG_CTRL, "Request for DPRAM\n");
937 if (copy_from_user(&HostDpRam, argp, sizeof(HostDpRam))) {
938 rio_dprintk(RIO_DEBUG_CTRL, "RIO_HOST_DPRAM: Copy in from user space failed\n");
939 p->RIOError.Error = COPYIN_FAILED;
940 return -EFAULT;
941 }
942 if (HostDpRam.HostNum >= p->RIONumHosts) {
943 p->RIOError.Error = HOST_NUMBER_OUT_OF_RANGE;
944 rio_dprintk(RIO_DEBUG_CTRL, "RIO_HOST_DPRAM: Illegal host number %d\n", HostDpRam.HostNum);
945 return -ENXIO;
946 }
947 rio_dprintk(RIO_DEBUG_CTRL, "Request for host %d\n", HostDpRam.HostNum);
948
949 if (p->RIOHosts[HostDpRam.HostNum].Type == RIO_PCI) {
950 int off;
951 /* It's hardware like this that really gets on my tits. */
952 static unsigned char copy[sizeof(struct DpRam)];
953 for (off = 0; off < sizeof(struct DpRam); off++)
954 copy[off] = readb(p->RIOHosts[HostDpRam.HostNum].Caddr + off);
955 if (copy_to_user(HostDpRam.DpRamP, copy, sizeof(struct DpRam))) {
956 p->RIOError.Error = COPYOUT_FAILED;
957 rio_dprintk(RIO_DEBUG_CTRL, "RIO_HOST_DPRAM: Bad copy to user space\n");
958 return -EFAULT;
959 }
960 } else if (copy_from_io(HostDpRam.DpRamP, p->RIOHosts[HostDpRam.HostNum].Caddr, sizeof(struct DpRam))) {
961 p->RIOError.Error = COPYOUT_FAILED;
962 rio_dprintk(RIO_DEBUG_CTRL, "RIO_HOST_DPRAM: Bad copy to user space\n");
963 return -EFAULT;
964 }
965 return retval;
966
967 case RIO_SET_BUSY:
968 rio_dprintk(RIO_DEBUG_CTRL, "RIO_SET_BUSY\n");
969 if (arg > 511) {
970 rio_dprintk(RIO_DEBUG_CTRL, "RIO_SET_BUSY: Bad port number %ld\n", arg);
971 p->RIOError.Error = PORT_NUMBER_OUT_OF_RANGE;
972 return -EINVAL;
973 }
974 rio_spin_lock_irqsave(&PortP->portSem, flags);
975 p->RIOPortp[arg]->State |= RIO_BUSY;
976 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
977 return retval;
978
979 case RIO_HOST_PORT:
980 /*
981 ** The daemon want port information
982 ** (probably for debug reasons)
983 */
984 rio_dprintk(RIO_DEBUG_CTRL, "RIO_HOST_PORT\n");
985 if (copy_from_user(&PortReq, argp, sizeof(PortReq))) {
986 rio_dprintk(RIO_DEBUG_CTRL, "RIO_HOST_PORT: Copy in from user space failed\n");
987 p->RIOError.Error = COPYIN_FAILED;
988 return -EFAULT;
989 }
990
991 if (PortReq.SysPort >= RIO_PORTS) { /* SysPort is unsigned */
992 rio_dprintk(RIO_DEBUG_CTRL, "RIO_HOST_PORT: Illegal port number %d\n", PortReq.SysPort);
993 p->RIOError.Error = PORT_NUMBER_OUT_OF_RANGE;
994 return -ENXIO;
995 }
996 rio_dprintk(RIO_DEBUG_CTRL, "Request for port %d\n", PortReq.SysPort);
997 if (copy_to_user(PortReq.PortP, p->RIOPortp[PortReq.SysPort], sizeof(struct Port))) {
998 p->RIOError.Error = COPYOUT_FAILED;
999 rio_dprintk(RIO_DEBUG_CTRL, "RIO_HOST_PORT: Bad copy to user space\n");
1000 return -EFAULT;
1001 }
1002 return retval;
1003
1004 case RIO_HOST_RUP:
1005 /*
1006 ** The daemon want rup information
1007 ** (probably for debug reasons)
1008 */
1009 rio_dprintk(RIO_DEBUG_CTRL, "RIO_HOST_RUP\n");
1010 if (copy_from_user(&RupReq, argp, sizeof(RupReq))) {
1011 rio_dprintk(RIO_DEBUG_CTRL, "RIO_HOST_RUP: Copy in from user space failed\n");
1012 p->RIOError.Error = COPYIN_FAILED;
1013 return -EFAULT;
1014 }
1015 if (RupReq.HostNum >= p->RIONumHosts) { /* host is unsigned */
1016 rio_dprintk(RIO_DEBUG_CTRL, "RIO_HOST_RUP: Illegal host number %d\n", RupReq.HostNum);
1017 p->RIOError.Error = HOST_NUMBER_OUT_OF_RANGE;
1018 return -ENXIO;
1019 }
1020 if (RupReq.RupNum >= MAX_RUP + LINKS_PER_UNIT) { /* eek! */
1021 rio_dprintk(RIO_DEBUG_CTRL, "RIO_HOST_RUP: Illegal rup number %d\n", RupReq.RupNum);
1022 p->RIOError.Error = RUP_NUMBER_OUT_OF_RANGE;
1023 return -EINVAL;
1024 }
1025 HostP = &p->RIOHosts[RupReq.HostNum];
1026
1027 if ((HostP->Flags & RUN_STATE) != RC_RUNNING) {
1028 rio_dprintk(RIO_DEBUG_CTRL, "RIO_HOST_RUP: Host %d not running\n", RupReq.HostNum);
1029 p->RIOError.Error = HOST_NOT_RUNNING;
1030 return -EIO;
1031 }
1032 rio_dprintk(RIO_DEBUG_CTRL, "Request for rup %d from host %d\n", RupReq.RupNum, RupReq.HostNum);
1033
1034 if (copy_from_io(RupReq.RupP, HostP->UnixRups[RupReq.RupNum].RupP, sizeof(struct RUP))) {
1035 p->RIOError.Error = COPYOUT_FAILED;
1036 rio_dprintk(RIO_DEBUG_CTRL, "RIO_HOST_RUP: Bad copy to user space\n");
1037 return -EFAULT;
1038 }
1039 return retval;
1040
1041 case RIO_HOST_LPB:
1042 /*
1043 ** The daemon want lpb information
1044 ** (probably for debug reasons)
1045 */
1046 rio_dprintk(RIO_DEBUG_CTRL, "RIO_HOST_LPB\n");
1047 if (copy_from_user(&LpbReq, argp, sizeof(LpbReq))) {
1048 rio_dprintk(RIO_DEBUG_CTRL, "RIO_HOST_LPB: Bad copy from user space\n");
1049 p->RIOError.Error = COPYIN_FAILED;
1050 return -EFAULT;
1051 }
1052 if (LpbReq.Host >= p->RIONumHosts) { /* host is unsigned */
1053 rio_dprintk(RIO_DEBUG_CTRL, "RIO_HOST_LPB: Illegal host number %d\n", LpbReq.Host);
1054 p->RIOError.Error = HOST_NUMBER_OUT_OF_RANGE;
1055 return -ENXIO;
1056 }
1057 if (LpbReq.Link >= LINKS_PER_UNIT) { /* eek! */
1058 rio_dprintk(RIO_DEBUG_CTRL, "RIO_HOST_LPB: Illegal link number %d\n", LpbReq.Link);
1059 p->RIOError.Error = LINK_NUMBER_OUT_OF_RANGE;
1060 return -EINVAL;
1061 }
1062 HostP = &p->RIOHosts[LpbReq.Host];
1063
1064 if ((HostP->Flags & RUN_STATE) != RC_RUNNING) {
1065 rio_dprintk(RIO_DEBUG_CTRL, "RIO_HOST_LPB: Host %d not running\n", LpbReq.Host);
1066 p->RIOError.Error = HOST_NOT_RUNNING;
1067 return -EIO;
1068 }
1069 rio_dprintk(RIO_DEBUG_CTRL, "Request for lpb %d from host %d\n", LpbReq.Link, LpbReq.Host);
1070
1071 if (copy_from_io(LpbReq.LpbP, &HostP->LinkStrP[LpbReq.Link], sizeof(struct LPB))) {
1072 rio_dprintk(RIO_DEBUG_CTRL, "RIO_HOST_LPB: Bad copy to user space\n");
1073 p->RIOError.Error = COPYOUT_FAILED;
1074 return -EFAULT;
1075 }
1076 return retval;
1077
1078 /*
1079 ** Here 3 IOCTL's that allow us to change the way in which
1080 ** rio logs errors. send them just to syslog or send them
1081 ** to both syslog and console or send them to just the console.
1082 **
1083 ** See RioStrBuf() in util.c for the other half.
1084 */
1085 case RIO_SYSLOG_ONLY:
1086 p->RIOPrintLogState = PRINT_TO_LOG; /* Just syslog */
1087 return 0;
1088
1089 case RIO_SYSLOG_CONS:
1090 p->RIOPrintLogState = PRINT_TO_LOG_CONS; /* syslog and console */
1091 return 0;
1092
1093 case RIO_CONS_ONLY:
1094 p->RIOPrintLogState = PRINT_TO_CONS; /* Just console */
1095 return 0;
1096
1097 case RIO_SIGNALS_ON:
1098 if (p->RIOSignalProcess) {
1099 p->RIOError.Error = SIGNALS_ALREADY_SET;
1100 return -EBUSY;
1101 }
1102 /* FIXME: PID tracking */
1103 p->RIOSignalProcess = current->pid;
1104 p->RIOPrintDisabled = DONT_PRINT;
1105 return retval;
1106
1107 case RIO_SIGNALS_OFF:
1108 if (p->RIOSignalProcess != current->pid) {
1109 p->RIOError.Error = NOT_RECEIVING_PROCESS;
1110 return -EPERM;
1111 }
1112 rio_dprintk(RIO_DEBUG_CTRL, "Clear signal process to zero\n");
1113 p->RIOSignalProcess = 0;
1114 return retval;
1115
1116 case RIO_SET_BYTE_MODE:
1117 for (Host = 0; Host < p->RIONumHosts; Host++)
1118 if (p->RIOHosts[Host].Type == RIO_AT)
1119 p->RIOHosts[Host].Mode &= ~WORD_OPERATION;
1120 return retval;
1121
1122 case RIO_SET_WORD_MODE:
1123 for (Host = 0; Host < p->RIONumHosts; Host++)
1124 if (p->RIOHosts[Host].Type == RIO_AT)
1125 p->RIOHosts[Host].Mode |= WORD_OPERATION;
1126 return retval;
1127
1128 case RIO_SET_FAST_BUS:
1129 for (Host = 0; Host < p->RIONumHosts; Host++)
1130 if (p->RIOHosts[Host].Type == RIO_AT)
1131 p->RIOHosts[Host].Mode |= FAST_AT_BUS;
1132 return retval;
1133
1134 case RIO_SET_SLOW_BUS:
1135 for (Host = 0; Host < p->RIONumHosts; Host++)
1136 if (p->RIOHosts[Host].Type == RIO_AT)
1137 p->RIOHosts[Host].Mode &= ~FAST_AT_BUS;
1138 return retval;
1139
1140 case RIO_MAP_B50_TO_50:
1141 case RIO_MAP_B50_TO_57600:
1142 case RIO_MAP_B110_TO_110:
1143 case RIO_MAP_B110_TO_115200:
1144 rio_dprintk(RIO_DEBUG_CTRL, "Baud rate mapping\n");
1145 port = arg;
1146 if (port < 0 || port > 511) {
1147 rio_dprintk(RIO_DEBUG_CTRL, "Baud rate mapping: Bad port number %d\n", port);
1148 p->RIOError.Error = PORT_NUMBER_OUT_OF_RANGE;
1149 return -EINVAL;
1150 }
1151 rio_spin_lock_irqsave(&PortP->portSem, flags);
1152 switch (cmd) {
1153 case RIO_MAP_B50_TO_50:
1154 p->RIOPortp[port]->Config |= RIO_MAP_50_TO_50;
1155 break;
1156 case RIO_MAP_B50_TO_57600:
1157 p->RIOPortp[port]->Config &= ~RIO_MAP_50_TO_50;
1158 break;
1159 case RIO_MAP_B110_TO_110:
1160 p->RIOPortp[port]->Config |= RIO_MAP_110_TO_110;
1161 break;
1162 case RIO_MAP_B110_TO_115200:
1163 p->RIOPortp[port]->Config &= ~RIO_MAP_110_TO_110;
1164 break;
1165 }
1166 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
1167 return retval;
1168
1169 case RIO_STREAM_INFO:
1170 rio_dprintk(RIO_DEBUG_CTRL, "RIO_STREAM_INFO\n");
1171 return -EINVAL;
1172
1173 case RIO_SEND_PACKET:
1174 rio_dprintk(RIO_DEBUG_CTRL, "RIO_SEND_PACKET\n");
1175 if (copy_from_user(&SendPack, argp, sizeof(SendPack))) {
1176 rio_dprintk(RIO_DEBUG_CTRL, "RIO_SEND_PACKET: Bad copy from user space\n");
1177 p->RIOError.Error = COPYIN_FAILED;
1178 return -EFAULT;
1179 }
1180 if (SendPack.PortNum >= 128) {
1181 p->RIOError.Error = PORT_NUMBER_OUT_OF_RANGE;
1182 return -ENXIO;
1183 }
1184
1185 PortP = p->RIOPortp[SendPack.PortNum];
1186 rio_spin_lock_irqsave(&PortP->portSem, flags);
1187
1188 if (!can_add_transmit(&PacketP, PortP)) {
1189 p->RIOError.Error = UNIT_IS_IN_USE;
1190 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
1191 return -ENOSPC;
1192 }
1193
1194 for (loop = 0; loop < (ushort) (SendPack.Len & 127); loop++)
1195 writeb(SendPack.Data[loop], &PacketP->data[loop]);
1196
1197 writeb(SendPack.Len, &PacketP->len);
1198
1199 add_transmit(PortP);
1200 /*
1201 ** Count characters transmitted for port statistics reporting
1202 */
1203 if (PortP->statsGather)
1204 PortP->txchars += (SendPack.Len & 127);
1205 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
1206 return retval;
1207
1208 case RIO_NO_MESG:
1209 if (su)
1210 p->RIONoMessage = 1;
1211 return su ? 0 : -EPERM;
1212
1213 case RIO_MESG:
1214 if (su)
1215 p->RIONoMessage = 0;
1216 return su ? 0 : -EPERM;
1217
1218 case RIO_WHAT_MESG:
1219 if (copy_to_user(argp, &p->RIONoMessage, sizeof(p->RIONoMessage))) {
1220 rio_dprintk(RIO_DEBUG_CTRL, "RIO_WHAT_MESG: Bad copy to user space\n");
1221 p->RIOError.Error = COPYOUT_FAILED;
1222 return -EFAULT;
1223 }
1224 return 0;
1225
1226 case RIO_MEM_DUMP:
1227 if (copy_from_user(&SubCmd, argp, sizeof(struct SubCmdStruct))) {
1228 p->RIOError.Error = COPYIN_FAILED;
1229 return -EFAULT;
1230 }
1231 rio_dprintk(RIO_DEBUG_CTRL, "RIO_MEM_DUMP host %d rup %d addr %x\n", SubCmd.Host, SubCmd.Rup, SubCmd.Addr);
1232
1233 if (SubCmd.Rup >= MAX_RUP + LINKS_PER_UNIT) {
1234 p->RIOError.Error = RUP_NUMBER_OUT_OF_RANGE;
1235 return -EINVAL;
1236 }
1237
1238 if (SubCmd.Host >= p->RIONumHosts) {
1239 p->RIOError.Error = HOST_NUMBER_OUT_OF_RANGE;
1240 return -EINVAL;
1241 }
1242
1243 port = p->RIOHosts[SubCmd.Host].UnixRups[SubCmd.Rup].BaseSysPort;
1244
1245 PortP = p->RIOPortp[port];
1246
1247 rio_spin_lock_irqsave(&PortP->portSem, flags);
1248
1249 if (RIOPreemptiveCmd(p, PortP, RIOC_MEMDUMP) == RIO_FAIL) {
1250 rio_dprintk(RIO_DEBUG_CTRL, "RIO_MEM_DUMP failed\n");
1251 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
1252 return -EBUSY;
1253 } else
1254 PortP->State |= RIO_BUSY;
1255
1256 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
1257 if (copy_to_user(argp, p->RIOMemDump, MEMDUMP_SIZE)) {
1258 rio_dprintk(RIO_DEBUG_CTRL, "RIO_MEM_DUMP copy failed\n");
1259 p->RIOError.Error = COPYOUT_FAILED;
1260 return -EFAULT;
1261 }
1262 return 0;
1263
1264 case RIO_TICK:
1265 if (arg >= p->RIONumHosts)
1266 return -EINVAL;
1267 rio_dprintk(RIO_DEBUG_CTRL, "Set interrupt for host %ld\n", arg);
1268 writeb(0xFF, &p->RIOHosts[arg].SetInt);
1269 return 0;
1270
1271 case RIO_TOCK:
1272 if (arg >= p->RIONumHosts)
1273 return -EINVAL;
1274 rio_dprintk(RIO_DEBUG_CTRL, "Clear interrupt for host %ld\n", arg);
1275 writeb(0xFF, &p->RIOHosts[arg].ResetInt);
1276 return 0;
1277
1278 case RIO_READ_CHECK:
1279 /* Check reads for pkts with data[0] the same */
1280 p->RIOReadCheck = !p->RIOReadCheck;
1281 if (copy_to_user(argp, &p->RIOReadCheck, sizeof(unsigned int))) {
1282 p->RIOError.Error = COPYOUT_FAILED;
1283 return -EFAULT;
1284 }
1285 return 0;
1286
1287 case RIO_READ_REGISTER:
1288 if (copy_from_user(&SubCmd, argp, sizeof(struct SubCmdStruct))) {
1289 p->RIOError.Error = COPYIN_FAILED;
1290 return -EFAULT;
1291 }
1292 rio_dprintk(RIO_DEBUG_CTRL, "RIO_READ_REGISTER host %d rup %d port %d reg %x\n", SubCmd.Host, SubCmd.Rup, SubCmd.Port, SubCmd.Addr);
1293
1294 if (SubCmd.Port > 511) {
1295 rio_dprintk(RIO_DEBUG_CTRL, "Baud rate mapping: Bad port number %d\n", SubCmd.Port);
1296 p->RIOError.Error = PORT_NUMBER_OUT_OF_RANGE;
1297 return -EINVAL;
1298 }
1299
1300 if (SubCmd.Rup >= MAX_RUP + LINKS_PER_UNIT) {
1301 p->RIOError.Error = RUP_NUMBER_OUT_OF_RANGE;
1302 return -EINVAL;
1303 }
1304
1305 if (SubCmd.Host >= p->RIONumHosts) {
1306 p->RIOError.Error = HOST_NUMBER_OUT_OF_RANGE;
1307 return -EINVAL;
1308 }
1309
1310 port = p->RIOHosts[SubCmd.Host].UnixRups[SubCmd.Rup].BaseSysPort + SubCmd.Port;
1311 PortP = p->RIOPortp[port];
1312
1313 rio_spin_lock_irqsave(&PortP->portSem, flags);
1314
1315 if (RIOPreemptiveCmd(p, PortP, RIOC_READ_REGISTER) ==
1316 RIO_FAIL) {
1317 rio_dprintk(RIO_DEBUG_CTRL, "RIO_READ_REGISTER failed\n");
1318 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
1319 return -EBUSY;
1320 } else
1321 PortP->State |= RIO_BUSY;
1322
1323 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
1324 if (copy_to_user(argp, &p->CdRegister, sizeof(unsigned int))) {
1325 rio_dprintk(RIO_DEBUG_CTRL, "RIO_READ_REGISTER copy failed\n");
1326 p->RIOError.Error = COPYOUT_FAILED;
1327 return -EFAULT;
1328 }
1329 return 0;
1330 /*
1331 ** rio_make_dev: given port number (0-511) ORed with port type
1332 ** (RIO_DEV_DIRECT, RIO_DEV_MODEM, RIO_DEV_XPRINT) return dev_t
1333 ** value to pass to mknod to create the correct device node.
1334 */
1335 case RIO_MAKE_DEV:
1336 {
1337 unsigned int port = arg & RIO_MODEM_MASK;
1338 unsigned int ret;
1339
1340 switch (arg & RIO_DEV_MASK) {
1341 case RIO_DEV_DIRECT:
1342 ret = drv_makedev(MAJOR(dev), port);
1343 rio_dprintk(RIO_DEBUG_CTRL, "Makedev direct 0x%x is 0x%x\n", port, ret);
1344 return ret;
1345 case RIO_DEV_MODEM:
1346 ret = drv_makedev(MAJOR(dev), (port | RIO_MODEM_BIT));
1347 rio_dprintk(RIO_DEBUG_CTRL, "Makedev modem 0x%x is 0x%x\n", port, ret);
1348 return ret;
1349 case RIO_DEV_XPRINT:
1350 ret = drv_makedev(MAJOR(dev), port);
1351 rio_dprintk(RIO_DEBUG_CTRL, "Makedev printer 0x%x is 0x%x\n", port, ret);
1352 return ret;
1353 }
1354 rio_dprintk(RIO_DEBUG_CTRL, "MAKE Device is called\n");
1355 return -EINVAL;
1356 }
1357 /*
1358 ** rio_minor: given a dev_t from a stat() call, return
1359 ** the port number (0-511) ORed with the port type
1360 ** ( RIO_DEV_DIRECT, RIO_DEV_MODEM, RIO_DEV_XPRINT )
1361 */
1362 case RIO_MINOR:
1363 {
1364 dev_t dv;
1365 int mino;
1366 unsigned long ret;
1367
1368 dv = (dev_t) (arg);
1369 mino = RIO_UNMODEM(dv);
1370
1371 if (RIO_ISMODEM(dv)) {
1372 rio_dprintk(RIO_DEBUG_CTRL, "Minor for device 0x%x: modem %d\n", dv, mino);
1373 ret = mino | RIO_DEV_MODEM;
1374 } else {
1375 rio_dprintk(RIO_DEBUG_CTRL, "Minor for device 0x%x: direct %d\n", dv, mino);
1376 ret = mino | RIO_DEV_DIRECT;
1377 }
1378 return ret;
1379 }
1380 }
1381 rio_dprintk(RIO_DEBUG_CTRL, "INVALID DAEMON IOCTL 0x%x\n", cmd);
1382 p->RIOError.Error = IOCTL_COMMAND_UNKNOWN;
1383
1384 func_exit();
1385 return -EINVAL;
1386}
1387
1388/*
1389** Pre-emptive commands go on RUPs and are only one byte long.
1390*/
1391int RIOPreemptiveCmd(struct rio_info *p, struct Port *PortP, u8 Cmd)
1392{
1393 struct CmdBlk *CmdBlkP;
1394 struct PktCmd_M *PktCmdP;
1395 int Ret;
1396 ushort rup;
1397 int port;
1398
1399 if (PortP->State & RIO_DELETED) {
1400 rio_dprintk(RIO_DEBUG_CTRL, "Preemptive command to deleted RTA ignored\n");
1401 return RIO_FAIL;
1402 }
1403
1404 if ((PortP->InUse == (typeof(PortP->InUse))-1) ||
1405 !(CmdBlkP = RIOGetCmdBlk())) {
1406 rio_dprintk(RIO_DEBUG_CTRL, "Cannot allocate command block "
1407 "for command %d on port %d\n", Cmd, PortP->PortNum);
1408 return RIO_FAIL;
1409 }
1410
1411 rio_dprintk(RIO_DEBUG_CTRL, "Command blk %p - InUse now %d\n",
1412 CmdBlkP, PortP->InUse);
1413
1414 PktCmdP = (struct PktCmd_M *)&CmdBlkP->Packet.data[0];
1415
1416 CmdBlkP->Packet.src_unit = 0;
1417 if (PortP->SecondBlock)
1418 rup = PortP->ID2;
1419 else
1420 rup = PortP->RupNum;
1421 CmdBlkP->Packet.dest_unit = rup;
1422 CmdBlkP->Packet.src_port = COMMAND_RUP;
1423 CmdBlkP->Packet.dest_port = COMMAND_RUP;
1424 CmdBlkP->Packet.len = PKT_CMD_BIT | 2;
1425 CmdBlkP->PostFuncP = RIOUnUse;
1426 CmdBlkP->PostArg = (unsigned long) PortP;
1427 PktCmdP->Command = Cmd;
1428 port = PortP->HostPort % (ushort) PORTS_PER_RTA;
1429 /*
1430 ** Index ports 8-15 for 2nd block of 16 port RTA.
1431 */
1432 if (PortP->SecondBlock)
1433 port += (ushort) PORTS_PER_RTA;
1434 PktCmdP->PhbNum = port;
1435
1436 switch (Cmd) {
1437 case RIOC_MEMDUMP:
1438 rio_dprintk(RIO_DEBUG_CTRL, "Queue MEMDUMP command blk %p "
1439 "(addr 0x%x)\n", CmdBlkP, (int) SubCmd.Addr);
1440 PktCmdP->SubCommand = RIOC_MEMDUMP;
1441 PktCmdP->SubAddr = SubCmd.Addr;
1442 break;
1443 case RIOC_FCLOSE:
1444 rio_dprintk(RIO_DEBUG_CTRL, "Queue FCLOSE command blk %p\n",
1445 CmdBlkP);
1446 break;
1447 case RIOC_READ_REGISTER:
1448 rio_dprintk(RIO_DEBUG_CTRL, "Queue READ_REGISTER (0x%x) "
1449 "command blk %p\n", (int) SubCmd.Addr, CmdBlkP);
1450 PktCmdP->SubCommand = RIOC_READ_REGISTER;
1451 PktCmdP->SubAddr = SubCmd.Addr;
1452 break;
1453 case RIOC_RESUME:
1454 rio_dprintk(RIO_DEBUG_CTRL, "Queue RESUME command blk %p\n",
1455 CmdBlkP);
1456 break;
1457 case RIOC_RFLUSH:
1458 rio_dprintk(RIO_DEBUG_CTRL, "Queue RFLUSH command blk %p\n",
1459 CmdBlkP);
1460 CmdBlkP->PostFuncP = RIORFlushEnable;
1461 break;
1462 case RIOC_SUSPEND:
1463 rio_dprintk(RIO_DEBUG_CTRL, "Queue SUSPEND command blk %p\n",
1464 CmdBlkP);
1465 break;
1466
1467 case RIOC_MGET:
1468 rio_dprintk(RIO_DEBUG_CTRL, "Queue MGET command blk %p\n",
1469 CmdBlkP);
1470 break;
1471
1472 case RIOC_MSET:
1473 case RIOC_MBIC:
1474 case RIOC_MBIS:
1475 CmdBlkP->Packet.data[4] = (char) PortP->ModemLines;
1476 rio_dprintk(RIO_DEBUG_CTRL, "Queue MSET/MBIC/MBIS command "
1477 "blk %p\n", CmdBlkP);
1478 break;
1479
1480 case RIOC_WFLUSH:
1481 /*
1482 ** If we have queued up the maximum number of Write flushes
1483 ** allowed then we should not bother sending any more to the
1484 ** RTA.
1485 */
1486 if (PortP->WflushFlag == (typeof(PortP->WflushFlag))-1) {
1487 rio_dprintk(RIO_DEBUG_CTRL, "Trashed WFLUSH, "
1488 "WflushFlag about to wrap!");
1489 RIOFreeCmdBlk(CmdBlkP);
1490 return (RIO_FAIL);
1491 } else {
1492 rio_dprintk(RIO_DEBUG_CTRL, "Queue WFLUSH command "
1493 "blk %p\n", CmdBlkP);
1494 CmdBlkP->PostFuncP = RIOWFlushMark;
1495 }
1496 break;
1497 }
1498
1499 PortP->InUse++;
1500
1501 Ret = RIOQueueCmdBlk(PortP->HostP, rup, CmdBlkP);
1502
1503 return Ret;
1504}
diff --git a/drivers/char/rio/riodrvr.h b/drivers/char/rio/riodrvr.h
deleted file mode 100644
index 0907e711b355..000000000000
--- a/drivers/char/rio/riodrvr.h
+++ /dev/null
@@ -1,138 +0,0 @@
1/*
2** -----------------------------------------------------------------------------
3**
4** Perle Specialix driver for Linux
5** Ported from existing RIO Driver for SCO sources.
6 *
7 * (C) 1990 - 2000 Specialix International Ltd., Byfleet, Surrey, UK.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22**
23** Module : riodrvr.h
24** SID : 1.3
25** Last Modified : 11/6/98 09:22:46
26** Retrieved : 11/6/98 09:22:46
27**
28** ident @(#)riodrvr.h 1.3
29**
30** -----------------------------------------------------------------------------
31*/
32
33#ifndef __riodrvr_h
34#define __riodrvr_h
35
36#include <asm/param.h> /* for HZ */
37
38#define MEMDUMP_SIZE 32
39#define MOD_DISABLE (RIO_NOREAD|RIO_NOWRITE|RIO_NOXPRINT)
40
41
42struct rio_info {
43 int mode; /* Intr or polled, word/byte */
44 spinlock_t RIOIntrSem; /* Interrupt thread sem */
45 int current_chan; /* current channel */
46 int RIOFailed; /* Not initialised ? */
47 int RIOInstallAttempts; /* no. of rio-install() calls */
48 int RIOLastPCISearch; /* status of last search */
49 int RIONumHosts; /* Number of RIO Hosts */
50 struct Host *RIOHosts; /* RIO Host values */
51 struct Port **RIOPortp; /* RIO port values */
52/*
53** 02.03.1999 ARG - ESIL 0820 fix
54** We no longer use RIOBootMode
55**
56 int RIOBootMode; * RIO boot mode *
57**
58*/
59 int RIOPrintDisabled; /* RIO printing disabled ? */
60 int RIOPrintLogState; /* RIO printing state ? */
61 int RIOPolling; /* Polling ? */
62/*
63** 09.12.1998 ARG - ESIL 0776 part fix
64** The 'RIO_QUICK_CHECK' ioctl was using RIOHalted.
65** The fix for this ESIL introduces another member (RIORtaDisCons) here to be
66** updated in RIOConCon() - to keep track of RTA connections/disconnections.
67** 'RIO_QUICK_CHECK' now returns the value of RIORtaDisCons.
68*/
69 int RIOHalted; /* halted ? */
70 int RIORtaDisCons; /* RTA connections/disconnections */
71 unsigned int RIOReadCheck; /* Rio read check */
72 unsigned int RIONoMessage; /* To display message or not */
73 unsigned int RIONumBootPkts; /* how many packets for an RTA */
74 unsigned int RIOBootCount; /* size of RTA code */
75 unsigned int RIOBooting; /* count of outstanding boots */
76 unsigned int RIOSystemUp; /* Booted ?? */
77 unsigned int RIOCounting; /* for counting interrupts */
78 unsigned int RIOIntCount; /* # of intr since last check */
79 unsigned int RIOTxCount; /* number of xmit intrs */
80 unsigned int RIORxCount; /* number of rx intrs */
81 unsigned int RIORupCount; /* number of rup intrs */
82 int RIXTimer;
83 int RIOBufferSize; /* Buffersize */
84 int RIOBufferMask; /* Buffersize */
85
86 int RIOFirstMajor; /* First host card's major no */
87
88 unsigned int RIOLastPortsMapped; /* highest port number known */
89 unsigned int RIOFirstPortsMapped; /* lowest port number known */
90
91 unsigned int RIOLastPortsBooted; /* highest port number running */
92 unsigned int RIOFirstPortsBooted; /* lowest port number running */
93
94 unsigned int RIOLastPortsOpened; /* highest port number running */
95 unsigned int RIOFirstPortsOpened; /* lowest port number running */
96
97 /* Flag to say that the topology information has been changed. */
98 unsigned int RIOQuickCheck;
99 unsigned int CdRegister; /* ??? */
100 int RIOSignalProcess; /* Signalling process */
101 int rio_debug; /* To debug ... */
102 int RIODebugWait; /* For what ??? */
103 int tpri; /* Thread prio */
104 int tid; /* Thread id */
105 unsigned int _RIO_Polled; /* Counter for polling */
106 unsigned int _RIO_Interrupted; /* Counter for interrupt */
107 int intr_tid; /* iointset return value */
108 int TxEnSem; /* TxEnable Semaphore */
109
110
111 struct Error RIOError; /* to Identify what went wrong */
112 struct Conf RIOConf; /* Configuration ??? */
113 struct ttystatics channel[RIO_PORTS]; /* channel information */
114 char RIOBootPackets[1 + (SIXTY_FOUR_K / RTA_BOOT_DATA_SIZE)]
115 [RTA_BOOT_DATA_SIZE];
116 struct Map RIOConnectTable[TOTAL_MAP_ENTRIES];
117 struct Map RIOSavedTable[TOTAL_MAP_ENTRIES];
118
119 /* RTA to host binding table for master/slave operation */
120 unsigned long RIOBindTab[MAX_RTA_BINDINGS];
121 /* RTA memory dump variable */
122 unsigned char RIOMemDump[MEMDUMP_SIZE];
123 struct ModuleInfo RIOModuleTypes[MAX_MODULE_TYPES];
124
125};
126
127
128#ifdef linux
129#define debug(x) printk x
130#else
131#define debug(x) kkprintf x
132#endif
133
134
135
136#define RIO_RESET_INT 0x7d80
137
138#endif /* __riodrvr.h */
diff --git a/drivers/char/rio/rioinfo.h b/drivers/char/rio/rioinfo.h
deleted file mode 100644
index 42ff1e79d96f..000000000000
--- a/drivers/char/rio/rioinfo.h
+++ /dev/null
@@ -1,92 +0,0 @@
1/*
2** -----------------------------------------------------------------------------
3**
4** Perle Specialix driver for Linux
5** Ported from existing RIO Driver for SCO sources.
6 *
7 * (C) 1990 - 2000 Specialix International Ltd., Byfleet, Surrey, UK.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22**
23** Module : rioinfo.h
24** SID : 1.2
25** Last Modified : 11/6/98 14:07:49
26** Retrieved : 11/6/98 14:07:50
27**
28** ident @(#)rioinfo.h 1.2
29**
30** -----------------------------------------------------------------------------
31*/
32
33#ifndef __rioinfo_h
34#define __rioinfo_h
35
36/*
37** Host card data structure
38*/
39struct RioHostInfo {
40 long location; /* RIO Card Base I/O address */
41 long vector; /* RIO Card IRQ vector */
42 int bus; /* ISA/EISA/MCA/PCI */
43 int mode; /* pointer to host mode - INTERRUPT / POLLED */
44 struct old_sgttyb
45 *Sg; /* pointer to default term characteristics */
46};
47
48
49/* Mode in rio device info */
50#define INTERRUPTED_MODE 0x01 /* Interrupt is generated */
51#define POLLED_MODE 0x02 /* No interrupt */
52#define AUTO_MODE 0x03 /* Auto mode */
53
54#define WORD_ACCESS_MODE 0x10 /* Word Access Mode */
55#define BYTE_ACCESS_MODE 0x20 /* Byte Access Mode */
56
57
58/* Bus type that RIO supports */
59#define ISA_BUS 0x01 /* The card is ISA */
60#define EISA_BUS 0x02 /* The card is EISA */
61#define MCA_BUS 0x04 /* The card is MCA */
62#define PCI_BUS 0x08 /* The card is PCI */
63
64/*
65** 11.11.1998 ARG - ESIL ???? part fix
66** Moved definition for 'CHAN' here from rioinfo.c (it is now
67** called 'DEF_TERM_CHARACTERISTICS').
68*/
69
70#define DEF_TERM_CHARACTERISTICS \
71{ \
72 B19200, B19200, /* input and output speed */ \
73 'H' - '@', /* erase char */ \
74 -1, /* 2nd erase char */ \
75 'U' - '@', /* kill char */ \
76 ECHO | CRMOD, /* mode */ \
77 'C' - '@', /* interrupt character */ \
78 '\\' - '@', /* quit char */ \
79 'Q' - '@', /* start char */ \
80 'S' - '@', /* stop char */ \
81 'D' - '@', /* EOF */ \
82 -1, /* brk */ \
83 (LCRTBS | LCRTERA | LCRTKIL | LCTLECH), /* local mode word */ \
84 'Z' - '@', /* process stop */ \
85 'Y' - '@', /* delayed stop */ \
86 'R' - '@', /* reprint line */ \
87 'O' - '@', /* flush output */ \
88 'W' - '@', /* word erase */ \
89 'V' - '@' /* literal next char */ \
90}
91
92#endif /* __rioinfo_h */
diff --git a/drivers/char/rio/rioinit.c b/drivers/char/rio/rioinit.c
deleted file mode 100644
index 24a282bb89d4..000000000000
--- a/drivers/char/rio/rioinit.c
+++ /dev/null
@@ -1,421 +0,0 @@
1/*
2** -----------------------------------------------------------------------------
3**
4** Perle Specialix driver for Linux
5** Ported from existing RIO Driver for SCO sources.
6 *
7 * (C) 1990 - 2000 Specialix International Ltd., Byfleet, Surrey, UK.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22**
23** Module : rioinit.c
24** SID : 1.3
25** Last Modified : 11/6/98 10:33:43
26** Retrieved : 11/6/98 10:33:49
27**
28** ident @(#)rioinit.c 1.3
29**
30** -----------------------------------------------------------------------------
31*/
32
33#include <linux/module.h>
34#include <linux/errno.h>
35#include <linux/delay.h>
36#include <asm/io.h>
37#include <asm/system.h>
38#include <asm/string.h>
39#include <asm/uaccess.h>
40
41#include <linux/termios.h>
42#include <linux/serial.h>
43
44#include <linux/generic_serial.h>
45
46
47#include "linux_compat.h"
48#include "pkt.h"
49#include "daemon.h"
50#include "rio.h"
51#include "riospace.h"
52#include "cmdpkt.h"
53#include "map.h"
54#include "rup.h"
55#include "port.h"
56#include "riodrvr.h"
57#include "rioinfo.h"
58#include "func.h"
59#include "errors.h"
60#include "pci.h"
61
62#include "parmmap.h"
63#include "unixrup.h"
64#include "board.h"
65#include "host.h"
66#include "phb.h"
67#include "link.h"
68#include "cmdblk.h"
69#include "route.h"
70#include "cirrus.h"
71#include "rioioctl.h"
72#include "rio_linux.h"
73
74int RIOPCIinit(struct rio_info *p, int Mode);
75
76static int RIOScrub(int, u8 __iomem *, int);
77
78
79/**
80** RIOAssignAT :
81**
82** Fill out the fields in the p->RIOHosts structure now we know we know
83** we have a board present.
84**
85** bits < 0 indicates 8 bit operation requested,
86** bits > 0 indicates 16 bit operation.
87*/
88
89int RIOAssignAT(struct rio_info *p, int Base, void __iomem *virtAddr, int mode)
90{
91 int bits;
92 struct DpRam __iomem *cardp = (struct DpRam __iomem *)virtAddr;
93
94 if ((Base < ONE_MEG) || (mode & BYTE_ACCESS_MODE))
95 bits = BYTE_OPERATION;
96 else
97 bits = WORD_OPERATION;
98
99 /*
100 ** Board has passed its scrub test. Fill in all the
101 ** transient stuff.
102 */
103 p->RIOHosts[p->RIONumHosts].Caddr = virtAddr;
104 p->RIOHosts[p->RIONumHosts].CardP = virtAddr;
105
106 /*
107 ** Revision 01 AT host cards don't support WORD operations,
108 */
109 if (readb(&cardp->DpRevision) == 01)
110 bits = BYTE_OPERATION;
111
112 p->RIOHosts[p->RIONumHosts].Type = RIO_AT;
113 p->RIOHosts[p->RIONumHosts].Copy = rio_copy_to_card;
114 /* set this later */
115 p->RIOHosts[p->RIONumHosts].Slot = -1;
116 p->RIOHosts[p->RIONumHosts].Mode = SLOW_LINKS | SLOW_AT_BUS | bits;
117 writeb(BOOT_FROM_RAM | EXTERNAL_BUS_OFF | p->RIOHosts[p->RIONumHosts].Mode | INTERRUPT_DISABLE ,
118 &p->RIOHosts[p->RIONumHosts].Control);
119 writeb(0xFF, &p->RIOHosts[p->RIONumHosts].ResetInt);
120 writeb(BOOT_FROM_RAM | EXTERNAL_BUS_OFF | p->RIOHosts[p->RIONumHosts].Mode | INTERRUPT_DISABLE,
121 &p->RIOHosts[p->RIONumHosts].Control);
122 writeb(0xFF, &p->RIOHosts[p->RIONumHosts].ResetInt);
123 p->RIOHosts[p->RIONumHosts].UniqueNum =
124 ((readb(&p->RIOHosts[p->RIONumHosts].Unique[0])&0xFF)<<0)|
125 ((readb(&p->RIOHosts[p->RIONumHosts].Unique[1])&0xFF)<<8)|
126 ((readb(&p->RIOHosts[p->RIONumHosts].Unique[2])&0xFF)<<16)|
127 ((readb(&p->RIOHosts[p->RIONumHosts].Unique[3])&0xFF)<<24);
128 rio_dprintk (RIO_DEBUG_INIT, "RIO-init: Uniquenum 0x%x\n",p->RIOHosts[p->RIONumHosts].UniqueNum);
129
130 p->RIONumHosts++;
131 rio_dprintk (RIO_DEBUG_INIT, "RIO-init: Tests Passed at 0x%x\n", Base);
132 return(1);
133}
134
135static u8 val[] = {
136#ifdef VERY_LONG_TEST
137 0x00, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,
138 0xa5, 0xff, 0x5a, 0x00, 0xff, 0xc9, 0x36,
139#endif
140 0xff, 0x00, 0x00 };
141
142#define TEST_END sizeof(val)
143
144/*
145** RAM test a board.
146** Nothing too complicated, just enough to check it out.
147*/
148int RIOBoardTest(unsigned long paddr, void __iomem *caddr, unsigned char type, int slot)
149{
150 struct DpRam __iomem *DpRam = caddr;
151 void __iomem *ram[4];
152 int size[4];
153 int op, bank;
154 int nbanks;
155
156 rio_dprintk (RIO_DEBUG_INIT, "RIO-init: Reset host type=%d, DpRam=%p, slot=%d\n",
157 type, DpRam, slot);
158
159 RIOHostReset(type, DpRam, slot);
160
161 /*
162 ** Scrub the memory. This comes in several banks:
163 ** DPsram1 - 7000h bytes
164 ** DPsram2 - 200h bytes
165 ** DPsram3 - 7000h bytes
166 ** scratch - 1000h bytes
167 */
168
169 rio_dprintk (RIO_DEBUG_INIT, "RIO-init: Setup ram/size arrays\n");
170
171 size[0] = DP_SRAM1_SIZE;
172 size[1] = DP_SRAM2_SIZE;
173 size[2] = DP_SRAM3_SIZE;
174 size[3] = DP_SCRATCH_SIZE;
175
176 ram[0] = DpRam->DpSram1;
177 ram[1] = DpRam->DpSram2;
178 ram[2] = DpRam->DpSram3;
179 nbanks = (type == RIO_PCI) ? 3 : 4;
180 if (nbanks == 4)
181 ram[3] = DpRam->DpScratch;
182
183
184 if (nbanks == 3) {
185 rio_dprintk (RIO_DEBUG_INIT, "RIO-init: Memory: %p(0x%x), %p(0x%x), %p(0x%x)\n",
186 ram[0], size[0], ram[1], size[1], ram[2], size[2]);
187 } else {
188 rio_dprintk (RIO_DEBUG_INIT, "RIO-init: %p(0x%x), %p(0x%x), %p(0x%x), %p(0x%x)\n",
189 ram[0], size[0], ram[1], size[1], ram[2], size[2], ram[3], size[3]);
190 }
191
192 /*
193 ** This scrub operation will test for crosstalk between
194 ** banks. TEST_END is a magic number, and relates to the offset
195 ** within the 'val' array used by Scrub.
196 */
197 for (op=0; op<TEST_END; op++) {
198 for (bank=0; bank<nbanks; bank++) {
199 if (RIOScrub(op, ram[bank], size[bank]) == RIO_FAIL) {
200 rio_dprintk (RIO_DEBUG_INIT, "RIO-init: RIOScrub band %d, op %d failed\n",
201 bank, op);
202 return RIO_FAIL;
203 }
204 }
205 }
206
207 rio_dprintk (RIO_DEBUG_INIT, "Test completed\n");
208 return 0;
209}
210
211
212/*
213** Scrub an area of RAM.
214** Define PRETEST and POSTTEST for a more thorough checking of the
215** state of the memory.
216** Call with op set to an index into the above 'val' array to determine
217** which value will be written into memory.
218** Call with op set to zero means that the RAM will not be read and checked
219** before it is written.
220** Call with op not zero and the RAM will be read and compared with val[op-1]
221** to check that the data from the previous phase was retained.
222*/
223
224static int RIOScrub(int op, u8 __iomem *ram, int size)
225{
226 int off;
227 unsigned char oldbyte;
228 unsigned char newbyte;
229 unsigned char invbyte;
230 unsigned short oldword;
231 unsigned short newword;
232 unsigned short invword;
233 unsigned short swapword;
234
235 if (op) {
236 oldbyte = val[op-1];
237 oldword = oldbyte | (oldbyte<<8);
238 } else
239 oldbyte = oldword = 0; /* Tell the compiler we've initilalized them. */
240 newbyte = val[op];
241 newword = newbyte | (newbyte<<8);
242 invbyte = ~newbyte;
243 invword = invbyte | (invbyte<<8);
244
245 /*
246 ** Check that the RAM contains the value that should have been left there
247 ** by the previous test (not applicable for pass zero)
248 */
249 if (op) {
250 for (off=0; off<size; off++) {
251 if (readb(ram + off) != oldbyte) {
252 rio_dprintk (RIO_DEBUG_INIT, "RIO-init: Byte Pre Check 1: BYTE at offset 0x%x should have been=%x, was=%x\n", off, oldbyte, readb(ram + off));
253 return RIO_FAIL;
254 }
255 }
256 for (off=0; off<size; off+=2) {
257 if (readw(ram + off) != oldword) {
258 rio_dprintk (RIO_DEBUG_INIT, "RIO-init: Word Pre Check: WORD at offset 0x%x should have been=%x, was=%x\n",off,oldword, readw(ram + off));
259 rio_dprintk (RIO_DEBUG_INIT, "RIO-init: Word Pre Check: BYTE at offset 0x%x is %x BYTE at offset 0x%x is %x\n", off, readb(ram + off), off+1, readb(ram+off+1));
260 return RIO_FAIL;
261 }
262 }
263 }
264
265 /*
266 ** Now write the INVERSE of the test data into every location, using
267 ** BYTE write operations, first checking before each byte is written
268 ** that the location contains the old value still, and checking after
269 ** the write that the location contains the data specified - this is
270 ** the BYTE read/write test.
271 */
272 for (off=0; off<size; off++) {
273 if (op && (readb(ram + off) != oldbyte)) {
274 rio_dprintk (RIO_DEBUG_INIT, "RIO-init: Byte Pre Check 2: BYTE at offset 0x%x should have been=%x, was=%x\n", off, oldbyte, readb(ram + off));
275 return RIO_FAIL;
276 }
277 writeb(invbyte, ram + off);
278 if (readb(ram + off) != invbyte) {
279 rio_dprintk (RIO_DEBUG_INIT, "RIO-init: Byte Inv Check: BYTE at offset 0x%x should have been=%x, was=%x\n", off, invbyte, readb(ram + off));
280 return RIO_FAIL;
281 }
282 }
283
284 /*
285 ** now, use WORD operations to write the test value into every location,
286 ** check as before that the location contains the previous test value
287 ** before overwriting, and that it contains the data value written
288 ** afterwards.
289 ** This is the WORD operation test.
290 */
291 for (off=0; off<size; off+=2) {
292 if (readw(ram + off) != invword) {
293 rio_dprintk (RIO_DEBUG_INIT, "RIO-init: Word Inv Check: WORD at offset 0x%x should have been=%x, was=%x\n", off, invword, readw(ram + off));
294 rio_dprintk (RIO_DEBUG_INIT, "RIO-init: Word Inv Check: BYTE at offset 0x%x is %x BYTE at offset 0x%x is %x\n", off, readb(ram + off), off+1, readb(ram+off+1));
295 return RIO_FAIL;
296 }
297
298 writew(newword, ram + off);
299 if ( readw(ram + off) != newword ) {
300 rio_dprintk (RIO_DEBUG_INIT, "RIO-init: Post Word Check 1: WORD at offset 0x%x should have been=%x, was=%x\n", off, newword, readw(ram + off));
301 rio_dprintk (RIO_DEBUG_INIT, "RIO-init: Post Word Check 1: BYTE at offset 0x%x is %x BYTE at offset 0x%x is %x\n", off, readb(ram + off), off+1, readb(ram + off + 1));
302 return RIO_FAIL;
303 }
304 }
305
306 /*
307 ** now run through the block of memory again, first in byte mode
308 ** then in word mode, and check that all the locations contain the
309 ** required test data.
310 */
311 for (off=0; off<size; off++) {
312 if (readb(ram + off) != newbyte) {
313 rio_dprintk (RIO_DEBUG_INIT, "RIO-init: Post Byte Check: BYTE at offset 0x%x should have been=%x, was=%x\n", off, newbyte, readb(ram + off));
314 return RIO_FAIL;
315 }
316 }
317
318 for (off=0; off<size; off+=2) {
319 if (readw(ram + off) != newword ) {
320 rio_dprintk (RIO_DEBUG_INIT, "RIO-init: Post Word Check 2: WORD at offset 0x%x should have been=%x, was=%x\n", off, newword, readw(ram + off));
321 rio_dprintk (RIO_DEBUG_INIT, "RIO-init: Post Word Check 2: BYTE at offset 0x%x is %x BYTE at offset 0x%x is %x\n", off, readb(ram + off), off+1, readb(ram + off + 1));
322 return RIO_FAIL;
323 }
324 }
325
326 /*
327 ** time to check out byte swapping errors
328 */
329 swapword = invbyte | (newbyte << 8);
330
331 for (off=0; off<size; off+=2) {
332 writeb(invbyte, &ram[off]);
333 writeb(newbyte, &ram[off+1]);
334 }
335
336 for ( off=0; off<size; off+=2 ) {
337 if (readw(ram + off) != swapword) {
338 rio_dprintk (RIO_DEBUG_INIT, "RIO-init: SwapWord Check 1: WORD at offset 0x%x should have been=%x, was=%x\n", off, swapword, readw(ram + off));
339 rio_dprintk (RIO_DEBUG_INIT, "RIO-init: SwapWord Check 1: BYTE at offset 0x%x is %x BYTE at offset 0x%x is %x\n", off, readb(ram + off), off+1, readb(ram + off + 1));
340 return RIO_FAIL;
341 }
342 writew(~swapword, ram + off);
343 }
344
345 for (off=0; off<size; off+=2) {
346 if (readb(ram + off) != newbyte) {
347 rio_dprintk (RIO_DEBUG_INIT, "RIO-init: SwapWord Check 2: BYTE at offset 0x%x should have been=%x, was=%x\n", off, newbyte, readb(ram + off));
348 return RIO_FAIL;
349 }
350 if (readb(ram + off + 1) != invbyte) {
351 rio_dprintk (RIO_DEBUG_INIT, "RIO-init: SwapWord Check 2: BYTE at offset 0x%x should have been=%x, was=%x\n", off+1, invbyte, readb(ram + off + 1));
352 return RIO_FAIL;
353 }
354 writew(newword, ram + off);
355 }
356 return 0;
357}
358
359
360int RIODefaultName(struct rio_info *p, struct Host *HostP, unsigned int UnitId)
361{
362 memcpy(HostP->Mapping[UnitId].Name, "UNKNOWN RTA X-XX", 17);
363 HostP->Mapping[UnitId].Name[12]='1'+(HostP-p->RIOHosts);
364 if ((UnitId+1) > 9) {
365 HostP->Mapping[UnitId].Name[14]='0'+((UnitId+1)/10);
366 HostP->Mapping[UnitId].Name[15]='0'+((UnitId+1)%10);
367 }
368 else {
369 HostP->Mapping[UnitId].Name[14]='1'+UnitId;
370 HostP->Mapping[UnitId].Name[15]=0;
371 }
372 return 0;
373}
374
375#define RIO_RELEASE "Linux"
376#define RELEASE_ID "1.0"
377
378static struct rioVersion stVersion;
379
380struct rioVersion *RIOVersid(void)
381{
382 strlcpy(stVersion.version, "RIO driver for linux V1.0",
383 sizeof(stVersion.version));
384 strlcpy(stVersion.buildDate, __DATE__,
385 sizeof(stVersion.buildDate));
386
387 return &stVersion;
388}
389
390void RIOHostReset(unsigned int Type, struct DpRam __iomem *DpRamP, unsigned int Slot)
391{
392 /*
393 ** Reset the Tpu
394 */
395 rio_dprintk (RIO_DEBUG_INIT, "RIOHostReset: type 0x%x", Type);
396 switch ( Type ) {
397 case RIO_AT:
398 rio_dprintk (RIO_DEBUG_INIT, " (RIO_AT)\n");
399 writeb(BOOT_FROM_RAM | EXTERNAL_BUS_OFF | INTERRUPT_DISABLE | BYTE_OPERATION |
400 SLOW_LINKS | SLOW_AT_BUS, &DpRamP->DpControl);
401 writeb(0xFF, &DpRamP->DpResetTpu);
402 udelay(3);
403 rio_dprintk (RIO_DEBUG_INIT, "RIOHostReset: Don't know if it worked. Try reset again\n");
404 writeb(BOOT_FROM_RAM | EXTERNAL_BUS_OFF | INTERRUPT_DISABLE |
405 BYTE_OPERATION | SLOW_LINKS | SLOW_AT_BUS, &DpRamP->DpControl);
406 writeb(0xFF, &DpRamP->DpResetTpu);
407 udelay(3);
408 break;
409 case RIO_PCI:
410 rio_dprintk (RIO_DEBUG_INIT, " (RIO_PCI)\n");
411 writeb(RIO_PCI_BOOT_FROM_RAM, &DpRamP->DpControl);
412 writeb(0xFF, &DpRamP->DpResetInt);
413 writeb(0xFF, &DpRamP->DpResetTpu);
414 udelay(100);
415 break;
416 default:
417 rio_dprintk (RIO_DEBUG_INIT, " (UNKNOWN)\n");
418 break;
419 }
420 return;
421}
diff --git a/drivers/char/rio/riointr.c b/drivers/char/rio/riointr.c
deleted file mode 100644
index 2e71aecae206..000000000000
--- a/drivers/char/rio/riointr.c
+++ /dev/null
@@ -1,645 +0,0 @@
1/*
2** -----------------------------------------------------------------------------
3**
4** Perle Specialix driver for Linux
5** Ported from existing RIO Driver for SCO sources.
6 *
7 * (C) 1990 - 2000 Specialix International Ltd., Byfleet, Surrey, UK.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22**
23** Module : riointr.c
24** SID : 1.2
25** Last Modified : 11/6/98 10:33:44
26** Retrieved : 11/6/98 10:33:49
27**
28** ident @(#)riointr.c 1.2
29**
30** -----------------------------------------------------------------------------
31*/
32
33#include <linux/module.h>
34#include <linux/errno.h>
35#include <linux/tty.h>
36#include <linux/tty_flip.h>
37#include <asm/io.h>
38#include <asm/system.h>
39#include <asm/string.h>
40#include <asm/uaccess.h>
41
42#include <linux/termios.h>
43#include <linux/serial.h>
44
45#include <linux/generic_serial.h>
46
47#include <linux/delay.h>
48
49#include "linux_compat.h"
50#include "rio_linux.h"
51#include "pkt.h"
52#include "daemon.h"
53#include "rio.h"
54#include "riospace.h"
55#include "cmdpkt.h"
56#include "map.h"
57#include "rup.h"
58#include "port.h"
59#include "riodrvr.h"
60#include "rioinfo.h"
61#include "func.h"
62#include "errors.h"
63#include "pci.h"
64
65#include "parmmap.h"
66#include "unixrup.h"
67#include "board.h"
68#include "host.h"
69#include "phb.h"
70#include "link.h"
71#include "cmdblk.h"
72#include "route.h"
73#include "cirrus.h"
74#include "rioioctl.h"
75
76
77static void RIOReceive(struct rio_info *, struct Port *);
78
79
80static char *firstchars(char *p, int nch)
81{
82 static char buf[2][128];
83 static int t = 0;
84 t = !t;
85 memcpy(buf[t], p, nch);
86 buf[t][nch] = 0;
87 return buf[t];
88}
89
90
91#define INCR( P, I ) ((P) = (((P)+(I)) & p->RIOBufferMask))
92/* Enable and start the transmission of packets */
93void RIOTxEnable(char *en)
94{
95 struct Port *PortP;
96 struct rio_info *p;
97 struct tty_struct *tty;
98 int c;
99 struct PKT __iomem *PacketP;
100 unsigned long flags;
101
102 PortP = (struct Port *) en;
103 p = (struct rio_info *) PortP->p;
104 tty = PortP->gs.port.tty;
105
106
107 rio_dprintk(RIO_DEBUG_INTR, "tx port %d: %d chars queued.\n", PortP->PortNum, PortP->gs.xmit_cnt);
108
109 if (!PortP->gs.xmit_cnt)
110 return;
111
112
113 /* This routine is an order of magnitude simpler than the specialix
114 version. One of the disadvantages is that this version will send
115 an incomplete packet (usually 64 bytes instead of 72) once for
116 every 4k worth of data. Let's just say that this won't influence
117 performance significantly..... */
118
119 rio_spin_lock_irqsave(&PortP->portSem, flags);
120
121 while (can_add_transmit(&PacketP, PortP)) {
122 c = PortP->gs.xmit_cnt;
123 if (c > PKT_MAX_DATA_LEN)
124 c = PKT_MAX_DATA_LEN;
125
126 /* Don't copy past the end of the source buffer */
127 if (c > SERIAL_XMIT_SIZE - PortP->gs.xmit_tail)
128 c = SERIAL_XMIT_SIZE - PortP->gs.xmit_tail;
129
130 {
131 int t;
132 t = (c > 10) ? 10 : c;
133
134 rio_dprintk(RIO_DEBUG_INTR, "rio: tx port %d: copying %d chars: %s - %s\n", PortP->PortNum, c, firstchars(PortP->gs.xmit_buf + PortP->gs.xmit_tail, t), firstchars(PortP->gs.xmit_buf + PortP->gs.xmit_tail + c - t, t));
135 }
136 /* If for one reason or another, we can't copy more data,
137 we're done! */
138 if (c == 0)
139 break;
140
141 rio_memcpy_toio(PortP->HostP->Caddr, PacketP->data, PortP->gs.xmit_buf + PortP->gs.xmit_tail, c);
142 /* udelay (1); */
143
144 writeb(c, &(PacketP->len));
145 if (!(PortP->State & RIO_DELETED)) {
146 add_transmit(PortP);
147 /*
148 ** Count chars tx'd for port statistics reporting
149 */
150 if (PortP->statsGather)
151 PortP->txchars += c;
152 }
153 PortP->gs.xmit_tail = (PortP->gs.xmit_tail + c) & (SERIAL_XMIT_SIZE - 1);
154 PortP->gs.xmit_cnt -= c;
155 }
156
157 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
158
159 if (PortP->gs.xmit_cnt <= (PortP->gs.wakeup_chars + 2 * PKT_MAX_DATA_LEN))
160 tty_wakeup(PortP->gs.port.tty);
161
162}
163
164
165/*
166** RIO Host Service routine. Does all the work traditionally associated with an
167** interrupt.
168*/
169static int RupIntr;
170static int RxIntr;
171static int TxIntr;
172
173void RIOServiceHost(struct rio_info *p, struct Host *HostP)
174{
175 rio_spin_lock(&HostP->HostLock);
176 if ((HostP->Flags & RUN_STATE) != RC_RUNNING) {
177 static int t = 0;
178 rio_spin_unlock(&HostP->HostLock);
179 if ((t++ % 200) == 0)
180 rio_dprintk(RIO_DEBUG_INTR, "Interrupt but host not running. flags=%x.\n", (int) HostP->Flags);
181 return;
182 }
183 rio_spin_unlock(&HostP->HostLock);
184
185 if (readw(&HostP->ParmMapP->rup_intr)) {
186 writew(0, &HostP->ParmMapP->rup_intr);
187 p->RIORupCount++;
188 RupIntr++;
189 rio_dprintk(RIO_DEBUG_INTR, "rio: RUP interrupt on host %Zd\n", HostP - p->RIOHosts);
190 RIOPollHostCommands(p, HostP);
191 }
192
193 if (readw(&HostP->ParmMapP->rx_intr)) {
194 int port;
195
196 writew(0, &HostP->ParmMapP->rx_intr);
197 p->RIORxCount++;
198 RxIntr++;
199
200 rio_dprintk(RIO_DEBUG_INTR, "rio: RX interrupt on host %Zd\n", HostP - p->RIOHosts);
201 /*
202 ** Loop through every port. If the port is mapped into
203 ** the system ( i.e. has /dev/ttyXXXX associated ) then it is
204 ** worth checking. If the port isn't open, grab any packets
205 ** hanging on its receive queue and stuff them on the free
206 ** list; check for commands on the way.
207 */
208 for (port = p->RIOFirstPortsBooted; port < p->RIOLastPortsBooted + PORTS_PER_RTA; port++) {
209 struct Port *PortP = p->RIOPortp[port];
210 struct tty_struct *ttyP;
211 struct PKT __iomem *PacketP;
212
213 /*
214 ** not mapped in - most of the RIOPortp[] information
215 ** has not been set up!
216 ** Optimise: ports come in bundles of eight.
217 */
218 if (!PortP->Mapped) {
219 port += 7;
220 continue; /* with the next port */
221 }
222
223 /*
224 ** If the host board isn't THIS host board, check the next one.
225 ** optimise: ports come in bundles of eight.
226 */
227 if (PortP->HostP != HostP) {
228 port += 7;
229 continue;
230 }
231
232 /*
233 ** Let us see - is the port open? If not, then don't service it.
234 */
235 if (!(PortP->PortState & PORT_ISOPEN)) {
236 continue;
237 }
238
239 /*
240 ** find corresponding tty structure. The process of mapping
241 ** the ports puts these here.
242 */
243 ttyP = PortP->gs.port.tty;
244
245 /*
246 ** Lock the port before we begin working on it.
247 */
248 rio_spin_lock(&PortP->portSem);
249
250 /*
251 ** Process received data if there is any.
252 */
253 if (can_remove_receive(&PacketP, PortP))
254 RIOReceive(p, PortP);
255
256 /*
257 ** If there is no data left to be read from the port, and
258 ** it's handshake bit is set, then we must clear the handshake,
259 ** so that that downstream RTA is re-enabled.
260 */
261 if (!can_remove_receive(&PacketP, PortP) && (readw(&PortP->PhbP->handshake) == PHB_HANDSHAKE_SET)) {
262 /*
263 ** MAGIC! ( Basically, handshake the RX buffer, so that
264 ** the RTAs upstream can be re-enabled. )
265 */
266 rio_dprintk(RIO_DEBUG_INTR, "Set RX handshake bit\n");
267 writew(PHB_HANDSHAKE_SET | PHB_HANDSHAKE_RESET, &PortP->PhbP->handshake);
268 }
269 rio_spin_unlock(&PortP->portSem);
270 }
271 }
272
273 if (readw(&HostP->ParmMapP->tx_intr)) {
274 int port;
275
276 writew(0, &HostP->ParmMapP->tx_intr);
277
278 p->RIOTxCount++;
279 TxIntr++;
280 rio_dprintk(RIO_DEBUG_INTR, "rio: TX interrupt on host %Zd\n", HostP - p->RIOHosts);
281
282 /*
283 ** Loop through every port.
284 ** If the port is mapped into the system ( i.e. has /dev/ttyXXXX
285 ** associated ) then it is worth checking.
286 */
287 for (port = p->RIOFirstPortsBooted; port < p->RIOLastPortsBooted + PORTS_PER_RTA; port++) {
288 struct Port *PortP = p->RIOPortp[port];
289 struct tty_struct *ttyP;
290 struct PKT __iomem *PacketP;
291
292 /*
293 ** not mapped in - most of the RIOPortp[] information
294 ** has not been set up!
295 */
296 if (!PortP->Mapped) {
297 port += 7;
298 continue; /* with the next port */
299 }
300
301 /*
302 ** If the host board isn't running, then its data structures
303 ** are no use to us - continue quietly.
304 */
305 if (PortP->HostP != HostP) {
306 port += 7;
307 continue; /* with the next port */
308 }
309
310 /*
311 ** Let us see - is the port open? If not, then don't service it.
312 */
313 if (!(PortP->PortState & PORT_ISOPEN)) {
314 continue;
315 }
316
317 rio_dprintk(RIO_DEBUG_INTR, "rio: Looking into port %d.\n", port);
318 /*
319 ** Lock the port before we begin working on it.
320 */
321 rio_spin_lock(&PortP->portSem);
322
323 /*
324 ** If we can't add anything to the transmit queue, then
325 ** we need do none of this processing.
326 */
327 if (!can_add_transmit(&PacketP, PortP)) {
328 rio_dprintk(RIO_DEBUG_INTR, "Can't add to port, so skipping.\n");
329 rio_spin_unlock(&PortP->portSem);
330 continue;
331 }
332
333 /*
334 ** find corresponding tty structure. The process of mapping
335 ** the ports puts these here.
336 */
337 ttyP = PortP->gs.port.tty;
338 /* If ttyP is NULL, the port is getting closed. Forget about it. */
339 if (!ttyP) {
340 rio_dprintk(RIO_DEBUG_INTR, "no tty, so skipping.\n");
341 rio_spin_unlock(&PortP->portSem);
342 continue;
343 }
344 /*
345 ** If there is more room available we start up the transmit
346 ** data process again. This can be direct I/O, if the cookmode
347 ** is set to COOK_RAW or COOK_MEDIUM, or will be a call to the
348 ** riotproc( T_OUTPUT ) if we are in COOK_WELL mode, to fetch
349 ** characters via the line discipline. We must always call
350 ** the line discipline,
351 ** so that user input characters can be echoed correctly.
352 **
353 ** ++++ Update +++++
354 ** With the advent of double buffering, we now see if
355 ** TxBufferOut-In is non-zero. If so, then we copy a packet
356 ** to the output place, and set it going. If this empties
357 ** the buffer, then we must issue a wakeup( ) on OUT.
358 ** If it frees space in the buffer then we must issue
359 ** a wakeup( ) on IN.
360 **
361 ** ++++ Extra! Extra! If PortP->WflushFlag is set, then we
362 ** have to send a WFLUSH command down the PHB, to mark the
363 ** end point of a WFLUSH. We also need to clear out any
364 ** data from the double buffer! ( note that WflushFlag is a
365 ** *count* of the number of WFLUSH commands outstanding! )
366 **
367 ** ++++ And there's more!
368 ** If an RTA is powered off, then on again, and rebooted,
369 ** whilst it has ports open, then we need to re-open the ports.
370 ** ( reasonable enough ). We can't do this when we spot the
371 ** re-boot, in interrupt time, because the queue is probably
372 ** full. So, when we come in here, we need to test if any
373 ** ports are in this condition, and re-open the port before
374 ** we try to send any more data to it. Now, the re-booted
375 ** RTA will be discarding packets from the PHB until it
376 ** receives this open packet, but don't worry tooo much
377 ** about that. The one thing that is interesting is the
378 ** combination of this effect and the WFLUSH effect!
379 */
380 /* For now don't handle RTA reboots. -- REW.
381 Reenabled. Otherwise RTA reboots didn't work. Duh. -- REW */
382 if (PortP->MagicFlags) {
383 if (PortP->MagicFlags & MAGIC_REBOOT) {
384 /*
385 ** well, the RTA has been rebooted, and there is room
386 ** on its queue to add the open packet that is required.
387 **
388 ** The messy part of this line is trying to decide if
389 ** we need to call the Param function as a tty or as
390 ** a modem.
391 ** DONT USE CLOCAL AS A TEST FOR THIS!
392 **
393 ** If we can't param the port, then move on to the
394 ** next port.
395 */
396 PortP->InUse = NOT_INUSE;
397
398 rio_spin_unlock(&PortP->portSem);
399 if (RIOParam(PortP, RIOC_OPEN, ((PortP->Cor2Copy & (RIOC_COR2_RTSFLOW | RIOC_COR2_CTSFLOW)) == (RIOC_COR2_RTSFLOW | RIOC_COR2_CTSFLOW)) ? 1 : 0, DONT_SLEEP) == RIO_FAIL)
400 continue; /* with next port */
401 rio_spin_lock(&PortP->portSem);
402 PortP->MagicFlags &= ~MAGIC_REBOOT;
403 }
404
405 /*
406 ** As mentioned above, this is a tacky hack to cope
407 ** with WFLUSH
408 */
409 if (PortP->WflushFlag) {
410 rio_dprintk(RIO_DEBUG_INTR, "Want to WFLUSH mark this port\n");
411
412 if (PortP->InUse)
413 rio_dprintk(RIO_DEBUG_INTR, "FAILS - PORT IS IN USE\n");
414 }
415
416 while (PortP->WflushFlag && can_add_transmit(&PacketP, PortP) && (PortP->InUse == NOT_INUSE)) {
417 int p;
418 struct PktCmd __iomem *PktCmdP;
419
420 rio_dprintk(RIO_DEBUG_INTR, "Add WFLUSH marker to data queue\n");
421 /*
422 ** make it look just like a WFLUSH command
423 */
424 PktCmdP = (struct PktCmd __iomem *) &PacketP->data[0];
425
426 writeb(RIOC_WFLUSH, &PktCmdP->Command);
427
428 p = PortP->HostPort % (u16) PORTS_PER_RTA;
429
430 /*
431 ** If second block of ports for 16 port RTA, add 8
432 ** to index 8-15.
433 */
434 if (PortP->SecondBlock)
435 p += PORTS_PER_RTA;
436
437 writeb(p, &PktCmdP->PhbNum);
438
439 /*
440 ** to make debuggery easier
441 */
442 writeb('W', &PacketP->data[2]);
443 writeb('F', &PacketP->data[3]);
444 writeb('L', &PacketP->data[4]);
445 writeb('U', &PacketP->data[5]);
446 writeb('S', &PacketP->data[6]);
447 writeb('H', &PacketP->data[7]);
448 writeb(' ', &PacketP->data[8]);
449 writeb('0' + PortP->WflushFlag, &PacketP->data[9]);
450 writeb(' ', &PacketP->data[10]);
451 writeb(' ', &PacketP->data[11]);
452 writeb('\0', &PacketP->data[12]);
453
454 /*
455 ** its two bytes long!
456 */
457 writeb(PKT_CMD_BIT | 2, &PacketP->len);
458
459 /*
460 ** queue it!
461 */
462 if (!(PortP->State & RIO_DELETED)) {
463 add_transmit(PortP);
464 /*
465 ** Count chars tx'd for port statistics reporting
466 */
467 if (PortP->statsGather)
468 PortP->txchars += 2;
469 }
470
471 if (--(PortP->WflushFlag) == 0) {
472 PortP->MagicFlags &= ~MAGIC_FLUSH;
473 }
474
475 rio_dprintk(RIO_DEBUG_INTR, "Wflush count now stands at %d\n", PortP->WflushFlag);
476 }
477 if (PortP->MagicFlags & MORE_OUTPUT_EYGOR) {
478 if (PortP->MagicFlags & MAGIC_FLUSH) {
479 PortP->MagicFlags |= MORE_OUTPUT_EYGOR;
480 } else {
481 if (!can_add_transmit(&PacketP, PortP)) {
482 rio_spin_unlock(&PortP->portSem);
483 continue;
484 }
485 rio_spin_unlock(&PortP->portSem);
486 RIOTxEnable((char *) PortP);
487 rio_spin_lock(&PortP->portSem);
488 PortP->MagicFlags &= ~MORE_OUTPUT_EYGOR;
489 }
490 }
491 }
492
493
494 /*
495 ** If we can't add anything to the transmit queue, then
496 ** we need do none of the remaining processing.
497 */
498 if (!can_add_transmit(&PacketP, PortP)) {
499 rio_spin_unlock(&PortP->portSem);
500 continue;
501 }
502
503 rio_spin_unlock(&PortP->portSem);
504 RIOTxEnable((char *) PortP);
505 }
506 }
507}
508
509/*
510** Routine for handling received data for tty drivers
511*/
512static void RIOReceive(struct rio_info *p, struct Port *PortP)
513{
514 struct tty_struct *TtyP;
515 unsigned short transCount;
516 struct PKT __iomem *PacketP;
517 register unsigned int DataCnt;
518 unsigned char __iomem *ptr;
519 unsigned char *buf;
520 int copied = 0;
521
522 static int intCount, RxIntCnt;
523
524 /*
525 ** The receive data process is to remove packets from the
526 ** PHB until there aren't any more or the current cblock
527 ** is full. When this occurs, there will be some left over
528 ** data in the packet, that we must do something with.
529 ** As we haven't unhooked the packet from the read list
530 ** yet, we can just leave the packet there, having first
531 ** made a note of how far we got. This means that we need
532 ** a pointer per port saying where we start taking the
533 ** data from - this will normally be zero, but when we
534 ** run out of space it will be set to the offset of the
535 ** next byte to copy from the packet data area. The packet
536 ** length field is decremented by the number of bytes that
537 ** we successfully removed from the packet. When this reaches
538 ** zero, we reset the offset pointer to be zero, and free
539 ** the packet from the front of the queue.
540 */
541
542 intCount++;
543
544 TtyP = PortP->gs.port.tty;
545 if (!TtyP) {
546 rio_dprintk(RIO_DEBUG_INTR, "RIOReceive: tty is null. \n");
547 return;
548 }
549
550 if (PortP->State & RIO_THROTTLE_RX) {
551 rio_dprintk(RIO_DEBUG_INTR, "RIOReceive: Throttled. Can't handle more input.\n");
552 return;
553 }
554
555 if (PortP->State & RIO_DELETED) {
556 while (can_remove_receive(&PacketP, PortP)) {
557 remove_receive(PortP);
558 put_free_end(PortP->HostP, PacketP);
559 }
560 } else {
561 /*
562 ** loop, just so long as:
563 ** i ) there's some data ( i.e. can_remove_receive )
564 ** ii ) we haven't been blocked
565 ** iii ) there's somewhere to put the data
566 ** iv ) we haven't outstayed our welcome
567 */
568 transCount = 1;
569 while (can_remove_receive(&PacketP, PortP)
570 && transCount) {
571 RxIntCnt++;
572
573 /*
574 ** check that it is not a command!
575 */
576 if (readb(&PacketP->len) & PKT_CMD_BIT) {
577 rio_dprintk(RIO_DEBUG_INTR, "RIO: unexpected command packet received on PHB\n");
578 /* rio_dprint(RIO_DEBUG_INTR, (" sysport = %d\n", p->RIOPortp->PortNum)); */
579 rio_dprintk(RIO_DEBUG_INTR, " dest_unit = %d\n", readb(&PacketP->dest_unit));
580 rio_dprintk(RIO_DEBUG_INTR, " dest_port = %d\n", readb(&PacketP->dest_port));
581 rio_dprintk(RIO_DEBUG_INTR, " src_unit = %d\n", readb(&PacketP->src_unit));
582 rio_dprintk(RIO_DEBUG_INTR, " src_port = %d\n", readb(&PacketP->src_port));
583 rio_dprintk(RIO_DEBUG_INTR, " len = %d\n", readb(&PacketP->len));
584 rio_dprintk(RIO_DEBUG_INTR, " control = %d\n", readb(&PacketP->control));
585 rio_dprintk(RIO_DEBUG_INTR, " csum = %d\n", readw(&PacketP->csum));
586 rio_dprintk(RIO_DEBUG_INTR, " data bytes: ");
587 for (DataCnt = 0; DataCnt < PKT_MAX_DATA_LEN; DataCnt++)
588 rio_dprintk(RIO_DEBUG_INTR, "%d\n", readb(&PacketP->data[DataCnt]));
589 remove_receive(PortP);
590 put_free_end(PortP->HostP, PacketP);
591 continue; /* with next packet */
592 }
593
594 /*
595 ** How many characters can we move 'upstream' ?
596 **
597 ** Determine the minimum of the amount of data
598 ** available and the amount of space in which to
599 ** put it.
600 **
601 ** 1. Get the packet length by masking 'len'
602 ** for only the length bits.
603 ** 2. Available space is [buffer size] - [space used]
604 **
605 ** Transfer count is the minimum of packet length
606 ** and available space.
607 */
608
609 transCount = tty_buffer_request_room(TtyP, readb(&PacketP->len) & PKT_LEN_MASK);
610 rio_dprintk(RIO_DEBUG_REC, "port %d: Copy %d bytes\n", PortP->PortNum, transCount);
611 /*
612 ** To use the following 'kkprintfs' for debugging - change the '#undef'
613 ** to '#define', (this is the only place ___DEBUG_IT___ occurs in the
614 ** driver).
615 */
616 ptr = (unsigned char __iomem *) PacketP->data + PortP->RxDataStart;
617
618 tty_prepare_flip_string(TtyP, &buf, transCount);
619 rio_memcpy_fromio(buf, ptr, transCount);
620 PortP->RxDataStart += transCount;
621 writeb(readb(&PacketP->len)-transCount, &PacketP->len);
622 copied += transCount;
623
624
625
626 if (readb(&PacketP->len) == 0) {
627 /*
628 ** If we have emptied the packet, then we can
629 ** free it, and reset the start pointer for
630 ** the next packet.
631 */
632 remove_receive(PortP);
633 put_free_end(PortP->HostP, PacketP);
634 PortP->RxDataStart = 0;
635 }
636 }
637 }
638 if (copied) {
639 rio_dprintk(RIO_DEBUG_REC, "port %d: pushing tty flip buffer: %d total bytes copied.\n", PortP->PortNum, copied);
640 tty_flip_buffer_push(TtyP);
641 }
642
643 return;
644}
645
diff --git a/drivers/char/rio/rioioctl.h b/drivers/char/rio/rioioctl.h
deleted file mode 100644
index e8af5b30519e..000000000000
--- a/drivers/char/rio/rioioctl.h
+++ /dev/null
@@ -1,57 +0,0 @@
1/*
2** -----------------------------------------------------------------------------
3**
4** Perle Specialix driver for Linux
5** Ported from existing RIO Driver for SCO sources.
6 *
7 * (C) 1990 - 2000 Specialix International Ltd., Byfleet, Surrey, UK.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22**
23** Module : rioioctl.h
24** SID : 1.2
25** Last Modified : 11/6/98 11:34:13
26** Retrieved : 11/6/98 11:34:22
27**
28** ident @(#)rioioctl.h 1.2
29**
30** -----------------------------------------------------------------------------
31*/
32
33#ifndef __rioioctl_h__
34#define __rioioctl_h__
35
36/*
37** RIO device driver - user ioctls and associated structures.
38*/
39
40struct portStats {
41 int port;
42 int gather;
43 unsigned long txchars;
44 unsigned long rxchars;
45 unsigned long opens;
46 unsigned long closes;
47 unsigned long ioctls;
48};
49
50#define RIOC ('R'<<8)|('i'<<16)|('o'<<24)
51
52#define RIO_QUICK_CHECK (RIOC | 105)
53#define RIO_GATHER_PORT_STATS (RIOC | 193)
54#define RIO_RESET_PORT_STATS (RIOC | 194)
55#define RIO_GET_PORT_STATS (RIOC | 195)
56
57#endif /* __rioioctl_h__ */
diff --git a/drivers/char/rio/rioparam.c b/drivers/char/rio/rioparam.c
deleted file mode 100644
index 6415f3f32a72..000000000000
--- a/drivers/char/rio/rioparam.c
+++ /dev/null
@@ -1,663 +0,0 @@
1/*
2** -----------------------------------------------------------------------------
3**
4** Perle Specialix driver for Linux
5** Ported from existing RIO Driver for SCO sources.
6 *
7 * (C) 1990 - 2000 Specialix International Ltd., Byfleet, Surrey, UK.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22**
23** Module : rioparam.c
24** SID : 1.3
25** Last Modified : 11/6/98 10:33:45
26** Retrieved : 11/6/98 10:33:50
27**
28** ident @(#)rioparam.c 1.3
29**
30** -----------------------------------------------------------------------------
31*/
32
33#include <linux/module.h>
34#include <linux/errno.h>
35#include <linux/tty.h>
36#include <asm/io.h>
37#include <asm/system.h>
38#include <asm/string.h>
39#include <asm/uaccess.h>
40
41#include <linux/termios.h>
42#include <linux/serial.h>
43
44#include <linux/generic_serial.h>
45
46
47#include "linux_compat.h"
48#include "rio_linux.h"
49#include "pkt.h"
50#include "daemon.h"
51#include "rio.h"
52#include "riospace.h"
53#include "cmdpkt.h"
54#include "map.h"
55#include "rup.h"
56#include "port.h"
57#include "riodrvr.h"
58#include "rioinfo.h"
59#include "func.h"
60#include "errors.h"
61#include "pci.h"
62
63#include "parmmap.h"
64#include "unixrup.h"
65#include "board.h"
66#include "host.h"
67#include "phb.h"
68#include "link.h"
69#include "cmdblk.h"
70#include "route.h"
71#include "cirrus.h"
72#include "rioioctl.h"
73#include "param.h"
74
75
76
77/*
78** The Scam, based on email from jeremyr@bugs.specialix.co.uk....
79**
80** To send a command on a particular port, you put a packet with the
81** command bit set onto the port. The command bit is in the len field,
82** and gets ORed in with the actual byte count.
83**
84** When you send a packet with the command bit set the first
85** data byte (data[0]) is interpreted as the command to execute.
86** It also governs what data structure overlay should accompany the packet.
87** Commands are defined in cirrus/cirrus.h
88**
89** If you want the command to pre-emt data already on the queue for the
90** port, set the pre-emptive bit in conjunction with the command bit.
91** It is not defined what will happen if you set the preemptive bit
92** on a packet that is NOT a command.
93**
94** Pre-emptive commands should be queued at the head of the queue using
95** add_start(), whereas normal commands and data are enqueued using
96** add_end().
97**
98** Most commands do not use the remaining bytes in the data array. The
99** exceptions are OPEN MOPEN and CONFIG. (NB. As with the SI CONFIG and
100** OPEN are currently analogous). With these three commands the following
101** 11 data bytes are all used to pass config information such as baud rate etc.
102** The fields are also defined in cirrus.h. Some contain straightforward
103** information such as the transmit XON character. Two contain the transmit and
104** receive baud rates respectively. For most baud rates there is a direct
105** mapping between the rates defined in <sys/termio.h> and the byte in the
106** packet. There are additional (non UNIX-standard) rates defined in
107** /u/dos/rio/cirrus/h/brates.h.
108**
109** The rest of the data fields contain approximations to the Cirrus registers
110** that are used to program number of bits etc. Each registers bit fields is
111** defined in cirrus.h.
112**
113** NB. Only use those bits that are defined as being driver specific
114** or common to the RTA and the driver.
115**
116** All commands going from RTA->Host will be dealt with by the Host code - you
117** will never see them. As with the SI there will be three fields to look out
118** for in each phb (not yet defined - needs defining a.s.a.p).
119**
120** modem_status - current state of handshake pins.
121**
122** port_status - current port status - equivalent to hi_stat for SI, indicates
123** if port is IDLE_OPEN, IDLE_CLOSED etc.
124**
125** break_status - bit X set if break has been received.
126**
127** Happy hacking.
128**
129*/
130
131/*
132** RIOParam is used to open or configure a port. You pass it a PortP,
133** which will have a tty struct attached to it. You also pass a command,
134** either OPEN or CONFIG. The port's setup is taken from the t_ fields
135** of the tty struct inside the PortP, and the port is either opened
136** or re-configured. You must also tell RIOParam if the device is a modem
137** device or not (i.e. top bit of minor number set or clear - take special
138** care when deciding on this!).
139** RIOParam neither flushes nor waits for drain, and is NOT preemptive.
140**
141** RIOParam assumes it will be called at splrio(), and also assumes
142** that CookMode is set correctly in the port structure.
143**
144** NB. for MPX
145** tty lock must NOT have been previously acquired.
146*/
147int RIOParam(struct Port *PortP, int cmd, int Modem, int SleepFlag)
148{
149 struct tty_struct *TtyP;
150 int retval;
151 struct phb_param __iomem *phb_param_ptr;
152 struct PKT __iomem *PacketP;
153 int res;
154 u8 Cor1 = 0, Cor2 = 0, Cor4 = 0, Cor5 = 0;
155 u8 TxXon = 0, TxXoff = 0, RxXon = 0, RxXoff = 0;
156 u8 LNext = 0, TxBaud = 0, RxBaud = 0;
157 int retries = 0xff;
158 unsigned long flags;
159
160 func_enter();
161
162 TtyP = PortP->gs.port.tty;
163
164 rio_dprintk(RIO_DEBUG_PARAM, "RIOParam: Port:%d cmd:%d Modem:%d SleepFlag:%d Mapped: %d, tty=%p\n", PortP->PortNum, cmd, Modem, SleepFlag, PortP->Mapped, TtyP);
165
166 if (!TtyP) {
167 rio_dprintk(RIO_DEBUG_PARAM, "Can't call rioparam with null tty.\n");
168
169 func_exit();
170
171 return RIO_FAIL;
172 }
173 rio_spin_lock_irqsave(&PortP->portSem, flags);
174
175 if (cmd == RIOC_OPEN) {
176 /*
177 ** If the port is set to store or lock the parameters, and it is
178 ** paramed with OPEN, we want to restore the saved port termio, but
179 ** only if StoredTermio has been saved, i.e. NOT 1st open after reboot.
180 */
181 }
182
183 /*
184 ** wait for space
185 */
186 while (!(res = can_add_transmit(&PacketP, PortP)) || (PortP->InUse != NOT_INUSE)) {
187 if (retries-- <= 0) {
188 break;
189 }
190 if (PortP->InUse != NOT_INUSE) {
191 rio_dprintk(RIO_DEBUG_PARAM, "Port IN_USE for pre-emptive command\n");
192 }
193
194 if (!res) {
195 rio_dprintk(RIO_DEBUG_PARAM, "Port has no space on transmit queue\n");
196 }
197
198 if (SleepFlag != OK_TO_SLEEP) {
199 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
200 func_exit();
201
202 return RIO_FAIL;
203 }
204
205 rio_dprintk(RIO_DEBUG_PARAM, "wait for can_add_transmit\n");
206 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
207 retval = RIODelay(PortP, HUNDRED_MS);
208 rio_spin_lock_irqsave(&PortP->portSem, flags);
209 if (retval == RIO_FAIL) {
210 rio_dprintk(RIO_DEBUG_PARAM, "wait for can_add_transmit broken by signal\n");
211 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
212 func_exit();
213 return -EINTR;
214 }
215 if (PortP->State & RIO_DELETED) {
216 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
217 func_exit();
218 return 0;
219 }
220 }
221
222 if (!res) {
223 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
224 func_exit();
225
226 return RIO_FAIL;
227 }
228
229 rio_dprintk(RIO_DEBUG_PARAM, "can_add_transmit() returns %x\n", res);
230 rio_dprintk(RIO_DEBUG_PARAM, "Packet is %p\n", PacketP);
231
232 phb_param_ptr = (struct phb_param __iomem *) PacketP->data;
233
234
235 switch (TtyP->termios->c_cflag & CSIZE) {
236 case CS5:
237 {
238 rio_dprintk(RIO_DEBUG_PARAM, "5 bit data\n");
239 Cor1 |= RIOC_COR1_5BITS;
240 break;
241 }
242 case CS6:
243 {
244 rio_dprintk(RIO_DEBUG_PARAM, "6 bit data\n");
245 Cor1 |= RIOC_COR1_6BITS;
246 break;
247 }
248 case CS7:
249 {
250 rio_dprintk(RIO_DEBUG_PARAM, "7 bit data\n");
251 Cor1 |= RIOC_COR1_7BITS;
252 break;
253 }
254 case CS8:
255 {
256 rio_dprintk(RIO_DEBUG_PARAM, "8 bit data\n");
257 Cor1 |= RIOC_COR1_8BITS;
258 break;
259 }
260 }
261
262 if (TtyP->termios->c_cflag & CSTOPB) {
263 rio_dprintk(RIO_DEBUG_PARAM, "2 stop bits\n");
264 Cor1 |= RIOC_COR1_2STOP;
265 } else {
266 rio_dprintk(RIO_DEBUG_PARAM, "1 stop bit\n");
267 Cor1 |= RIOC_COR1_1STOP;
268 }
269
270 if (TtyP->termios->c_cflag & PARENB) {
271 rio_dprintk(RIO_DEBUG_PARAM, "Enable parity\n");
272 Cor1 |= RIOC_COR1_NORMAL;
273 } else {
274 rio_dprintk(RIO_DEBUG_PARAM, "Disable parity\n");
275 Cor1 |= RIOC_COR1_NOP;
276 }
277 if (TtyP->termios->c_cflag & PARODD) {
278 rio_dprintk(RIO_DEBUG_PARAM, "Odd parity\n");
279 Cor1 |= RIOC_COR1_ODD;
280 } else {
281 rio_dprintk(RIO_DEBUG_PARAM, "Even parity\n");
282 Cor1 |= RIOC_COR1_EVEN;
283 }
284
285 /*
286 ** COR 2
287 */
288 if (TtyP->termios->c_iflag & IXON) {
289 rio_dprintk(RIO_DEBUG_PARAM, "Enable start/stop output control\n");
290 Cor2 |= RIOC_COR2_IXON;
291 } else {
292 if (PortP->Config & RIO_IXON) {
293 rio_dprintk(RIO_DEBUG_PARAM, "Force enable start/stop output control\n");
294 Cor2 |= RIOC_COR2_IXON;
295 } else
296 rio_dprintk(RIO_DEBUG_PARAM, "IXON has been disabled.\n");
297 }
298
299 if (TtyP->termios->c_iflag & IXANY) {
300 if (PortP->Config & RIO_IXANY) {
301 rio_dprintk(RIO_DEBUG_PARAM, "Enable any key to restart output\n");
302 Cor2 |= RIOC_COR2_IXANY;
303 } else
304 rio_dprintk(RIO_DEBUG_PARAM, "IXANY has been disabled due to sanity reasons.\n");
305 }
306
307 if (TtyP->termios->c_iflag & IXOFF) {
308 rio_dprintk(RIO_DEBUG_PARAM, "Enable start/stop input control 2\n");
309 Cor2 |= RIOC_COR2_IXOFF;
310 }
311
312 if (TtyP->termios->c_cflag & HUPCL) {
313 rio_dprintk(RIO_DEBUG_PARAM, "Hangup on last close\n");
314 Cor2 |= RIOC_COR2_HUPCL;
315 }
316
317 if (C_CRTSCTS(TtyP)) {
318 rio_dprintk(RIO_DEBUG_PARAM, "Rx hardware flow control enabled\n");
319 Cor2 |= RIOC_COR2_CTSFLOW;
320 Cor2 |= RIOC_COR2_RTSFLOW;
321 } else {
322 rio_dprintk(RIO_DEBUG_PARAM, "Rx hardware flow control disabled\n");
323 Cor2 &= ~RIOC_COR2_CTSFLOW;
324 Cor2 &= ~RIOC_COR2_RTSFLOW;
325 }
326
327
328 if (TtyP->termios->c_cflag & CLOCAL) {
329 rio_dprintk(RIO_DEBUG_PARAM, "Local line\n");
330 } else {
331 rio_dprintk(RIO_DEBUG_PARAM, "Possible Modem line\n");
332 }
333
334 /*
335 ** COR 4 (there is no COR 3)
336 */
337 if (TtyP->termios->c_iflag & IGNBRK) {
338 rio_dprintk(RIO_DEBUG_PARAM, "Ignore break condition\n");
339 Cor4 |= RIOC_COR4_IGNBRK;
340 }
341 if (!(TtyP->termios->c_iflag & BRKINT)) {
342 rio_dprintk(RIO_DEBUG_PARAM, "Break generates NULL condition\n");
343 Cor4 |= RIOC_COR4_NBRKINT;
344 } else {
345 rio_dprintk(RIO_DEBUG_PARAM, "Interrupt on break condition\n");
346 }
347
348 if (TtyP->termios->c_iflag & INLCR) {
349 rio_dprintk(RIO_DEBUG_PARAM, "Map newline to carriage return on input\n");
350 Cor4 |= RIOC_COR4_INLCR;
351 }
352
353 if (TtyP->termios->c_iflag & IGNCR) {
354 rio_dprintk(RIO_DEBUG_PARAM, "Ignore carriage return on input\n");
355 Cor4 |= RIOC_COR4_IGNCR;
356 }
357
358 if (TtyP->termios->c_iflag & ICRNL) {
359 rio_dprintk(RIO_DEBUG_PARAM, "Map carriage return to newline on input\n");
360 Cor4 |= RIOC_COR4_ICRNL;
361 }
362 if (TtyP->termios->c_iflag & IGNPAR) {
363 rio_dprintk(RIO_DEBUG_PARAM, "Ignore characters with parity errors\n");
364 Cor4 |= RIOC_COR4_IGNPAR;
365 }
366 if (TtyP->termios->c_iflag & PARMRK) {
367 rio_dprintk(RIO_DEBUG_PARAM, "Mark parity errors\n");
368 Cor4 |= RIOC_COR4_PARMRK;
369 }
370
371 /*
372 ** Set the RAISEMOD flag to ensure that the modem lines are raised
373 ** on reception of a config packet.
374 ** The download code handles the zero baud condition.
375 */
376 Cor4 |= RIOC_COR4_RAISEMOD;
377
378 /*
379 ** COR 5
380 */
381
382 Cor5 = RIOC_COR5_CMOE;
383
384 /*
385 ** Set to monitor tbusy/tstop (or not).
386 */
387
388 if (PortP->MonitorTstate)
389 Cor5 |= RIOC_COR5_TSTATE_ON;
390 else
391 Cor5 |= RIOC_COR5_TSTATE_OFF;
392
393 /*
394 ** Could set LNE here if you wanted LNext processing. SVR4 will use it.
395 */
396 if (TtyP->termios->c_iflag & ISTRIP) {
397 rio_dprintk(RIO_DEBUG_PARAM, "Strip input characters\n");
398 if (!(PortP->State & RIO_TRIAD_MODE)) {
399 Cor5 |= RIOC_COR5_ISTRIP;
400 }
401 }
402
403 if (TtyP->termios->c_oflag & ONLCR) {
404 rio_dprintk(RIO_DEBUG_PARAM, "Map newline to carriage-return, newline on output\n");
405 if (PortP->CookMode == COOK_MEDIUM)
406 Cor5 |= RIOC_COR5_ONLCR;
407 }
408 if (TtyP->termios->c_oflag & OCRNL) {
409 rio_dprintk(RIO_DEBUG_PARAM, "Map carriage return to newline on output\n");
410 if (PortP->CookMode == COOK_MEDIUM)
411 Cor5 |= RIOC_COR5_OCRNL;
412 }
413 if ((TtyP->termios->c_oflag & TABDLY) == TAB3) {
414 rio_dprintk(RIO_DEBUG_PARAM, "Tab delay 3 set\n");
415 if (PortP->CookMode == COOK_MEDIUM)
416 Cor5 |= RIOC_COR5_TAB3;
417 }
418
419 /*
420 ** Flow control bytes.
421 */
422 TxXon = TtyP->termios->c_cc[VSTART];
423 TxXoff = TtyP->termios->c_cc[VSTOP];
424 RxXon = TtyP->termios->c_cc[VSTART];
425 RxXoff = TtyP->termios->c_cc[VSTOP];
426 /*
427 ** LNEXT byte
428 */
429 LNext = 0;
430
431 /*
432 ** Baud rate bytes
433 */
434 rio_dprintk(RIO_DEBUG_PARAM, "Mapping of rx/tx baud %x (%x)\n", TtyP->termios->c_cflag, CBAUD);
435
436 switch (TtyP->termios->c_cflag & CBAUD) {
437#define e(b) case B ## b : RxBaud = TxBaud = RIO_B ## b ;break
438 e(50);
439 e(75);
440 e(110);
441 e(134);
442 e(150);
443 e(200);
444 e(300);
445 e(600);
446 e(1200);
447 e(1800);
448 e(2400);
449 e(4800);
450 e(9600);
451 e(19200);
452 e(38400);
453 e(57600);
454 e(115200); /* e(230400);e(460800); e(921600); */
455 }
456
457 rio_dprintk(RIO_DEBUG_PARAM, "tx baud 0x%x, rx baud 0x%x\n", TxBaud, RxBaud);
458
459
460 /*
461 ** Leftovers
462 */
463 if (TtyP->termios->c_cflag & CREAD)
464 rio_dprintk(RIO_DEBUG_PARAM, "Enable receiver\n");
465#ifdef RCV1EN
466 if (TtyP->termios->c_cflag & RCV1EN)
467 rio_dprintk(RIO_DEBUG_PARAM, "RCV1EN (?)\n");
468#endif
469#ifdef XMT1EN
470 if (TtyP->termios->c_cflag & XMT1EN)
471 rio_dprintk(RIO_DEBUG_PARAM, "XMT1EN (?)\n");
472#endif
473 if (TtyP->termios->c_lflag & ISIG)
474 rio_dprintk(RIO_DEBUG_PARAM, "Input character signal generating enabled\n");
475 if (TtyP->termios->c_lflag & ICANON)
476 rio_dprintk(RIO_DEBUG_PARAM, "Canonical input: erase and kill enabled\n");
477 if (TtyP->termios->c_lflag & XCASE)
478 rio_dprintk(RIO_DEBUG_PARAM, "Canonical upper/lower presentation\n");
479 if (TtyP->termios->c_lflag & ECHO)
480 rio_dprintk(RIO_DEBUG_PARAM, "Enable input echo\n");
481 if (TtyP->termios->c_lflag & ECHOE)
482 rio_dprintk(RIO_DEBUG_PARAM, "Enable echo erase\n");
483 if (TtyP->termios->c_lflag & ECHOK)
484 rio_dprintk(RIO_DEBUG_PARAM, "Enable echo kill\n");
485 if (TtyP->termios->c_lflag & ECHONL)
486 rio_dprintk(RIO_DEBUG_PARAM, "Enable echo newline\n");
487 if (TtyP->termios->c_lflag & NOFLSH)
488 rio_dprintk(RIO_DEBUG_PARAM, "Disable flush after interrupt or quit\n");
489#ifdef TOSTOP
490 if (TtyP->termios->c_lflag & TOSTOP)
491 rio_dprintk(RIO_DEBUG_PARAM, "Send SIGTTOU for background output\n");
492#endif
493#ifdef XCLUDE
494 if (TtyP->termios->c_lflag & XCLUDE)
495 rio_dprintk(RIO_DEBUG_PARAM, "Exclusive use of this line\n");
496#endif
497 if (TtyP->termios->c_iflag & IUCLC)
498 rio_dprintk(RIO_DEBUG_PARAM, "Map uppercase to lowercase on input\n");
499 if (TtyP->termios->c_oflag & OPOST)
500 rio_dprintk(RIO_DEBUG_PARAM, "Enable output post-processing\n");
501 if (TtyP->termios->c_oflag & OLCUC)
502 rio_dprintk(RIO_DEBUG_PARAM, "Map lowercase to uppercase on output\n");
503 if (TtyP->termios->c_oflag & ONOCR)
504 rio_dprintk(RIO_DEBUG_PARAM, "No carriage return output at column 0\n");
505 if (TtyP->termios->c_oflag & ONLRET)
506 rio_dprintk(RIO_DEBUG_PARAM, "Newline performs carriage return function\n");
507 if (TtyP->termios->c_oflag & OFILL)
508 rio_dprintk(RIO_DEBUG_PARAM, "Use fill characters for delay\n");
509 if (TtyP->termios->c_oflag & OFDEL)
510 rio_dprintk(RIO_DEBUG_PARAM, "Fill character is DEL\n");
511 if (TtyP->termios->c_oflag & NLDLY)
512 rio_dprintk(RIO_DEBUG_PARAM, "Newline delay set\n");
513 if (TtyP->termios->c_oflag & CRDLY)
514 rio_dprintk(RIO_DEBUG_PARAM, "Carriage return delay set\n");
515 if (TtyP->termios->c_oflag & TABDLY)
516 rio_dprintk(RIO_DEBUG_PARAM, "Tab delay set\n");
517 /*
518 ** These things are kind of useful in a later life!
519 */
520 PortP->Cor2Copy = Cor2;
521
522 if (PortP->State & RIO_DELETED) {
523 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
524 func_exit();
525
526 return RIO_FAIL;
527 }
528
529 /*
530 ** Actually write the info into the packet to be sent
531 */
532 writeb(cmd, &phb_param_ptr->Cmd);
533 writeb(Cor1, &phb_param_ptr->Cor1);
534 writeb(Cor2, &phb_param_ptr->Cor2);
535 writeb(Cor4, &phb_param_ptr->Cor4);
536 writeb(Cor5, &phb_param_ptr->Cor5);
537 writeb(TxXon, &phb_param_ptr->TxXon);
538 writeb(RxXon, &phb_param_ptr->RxXon);
539 writeb(TxXoff, &phb_param_ptr->TxXoff);
540 writeb(RxXoff, &phb_param_ptr->RxXoff);
541 writeb(LNext, &phb_param_ptr->LNext);
542 writeb(TxBaud, &phb_param_ptr->TxBaud);
543 writeb(RxBaud, &phb_param_ptr->RxBaud);
544
545 /*
546 ** Set the length/command field
547 */
548 writeb(12 | PKT_CMD_BIT, &PacketP->len);
549
550 /*
551 ** The packet is formed - now, whack it off
552 ** to its final destination:
553 */
554 add_transmit(PortP);
555 /*
556 ** Count characters transmitted for port statistics reporting
557 */
558 if (PortP->statsGather)
559 PortP->txchars += 12;
560
561 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
562
563 rio_dprintk(RIO_DEBUG_PARAM, "add_transmit returned.\n");
564 /*
565 ** job done.
566 */
567 func_exit();
568
569 return 0;
570}
571
572
573/*
574** We can add another packet to a transmit queue if the packet pointer pointed
575** to by the TxAdd pointer has PKT_IN_USE clear in its address.
576*/
577int can_add_transmit(struct PKT __iomem **PktP, struct Port *PortP)
578{
579 struct PKT __iomem *tp;
580
581 *PktP = tp = (struct PKT __iomem *) RIO_PTR(PortP->Caddr, readw(PortP->TxAdd));
582
583 return !((unsigned long) tp & PKT_IN_USE);
584}
585
586/*
587** To add a packet to the queue, you set the PKT_IN_USE bit in the address,
588** and then move the TxAdd pointer along one position to point to the next
589** packet pointer. You must wrap the pointer from the end back to the start.
590*/
591void add_transmit(struct Port *PortP)
592{
593 if (readw(PortP->TxAdd) & PKT_IN_USE) {
594 rio_dprintk(RIO_DEBUG_PARAM, "add_transmit: Packet has been stolen!");
595 }
596 writew(readw(PortP->TxAdd) | PKT_IN_USE, PortP->TxAdd);
597 PortP->TxAdd = (PortP->TxAdd == PortP->TxEnd) ? PortP->TxStart : PortP->TxAdd + 1;
598 writew(RIO_OFF(PortP->Caddr, PortP->TxAdd), &PortP->PhbP->tx_add);
599}
600
601/****************************************
602 * Put a packet onto the end of the
603 * free list
604 ****************************************/
605void put_free_end(struct Host *HostP, struct PKT __iomem *PktP)
606{
607 struct rio_free_list __iomem *tmp_pointer;
608 unsigned short old_end, new_end;
609 unsigned long flags;
610
611 rio_spin_lock_irqsave(&HostP->HostLock, flags);
612
613 /*************************************************
614 * Put a packet back onto the back of the free list
615 *
616 ************************************************/
617
618 rio_dprintk(RIO_DEBUG_PFE, "put_free_end(PktP=%p)\n", PktP);
619
620 if ((old_end = readw(&HostP->ParmMapP->free_list_end)) != TPNULL) {
621 new_end = RIO_OFF(HostP->Caddr, PktP);
622 tmp_pointer = (struct rio_free_list __iomem *) RIO_PTR(HostP->Caddr, old_end);
623 writew(new_end, &tmp_pointer->next);
624 writew(old_end, &((struct rio_free_list __iomem *) PktP)->prev);
625 writew(TPNULL, &((struct rio_free_list __iomem *) PktP)->next);
626 writew(new_end, &HostP->ParmMapP->free_list_end);
627 } else { /* First packet on the free list this should never happen! */
628 rio_dprintk(RIO_DEBUG_PFE, "put_free_end(): This should never happen\n");
629 writew(RIO_OFF(HostP->Caddr, PktP), &HostP->ParmMapP->free_list_end);
630 tmp_pointer = (struct rio_free_list __iomem *) PktP;
631 writew(TPNULL, &tmp_pointer->prev);
632 writew(TPNULL, &tmp_pointer->next);
633 }
634 rio_dprintk(RIO_DEBUG_CMD, "Before unlock: %p\n", &HostP->HostLock);
635 rio_spin_unlock_irqrestore(&HostP->HostLock, flags);
636}
637
638/*
639** can_remove_receive(PktP,P) returns non-zero if PKT_IN_USE is set
640** for the next packet on the queue. It will also set PktP to point to the
641** relevant packet, [having cleared the PKT_IN_USE bit]. If PKT_IN_USE is clear,
642** then can_remove_receive() returns 0.
643*/
644int can_remove_receive(struct PKT __iomem **PktP, struct Port *PortP)
645{
646 if (readw(PortP->RxRemove) & PKT_IN_USE) {
647 *PktP = (struct PKT __iomem *) RIO_PTR(PortP->Caddr, readw(PortP->RxRemove) & ~PKT_IN_USE);
648 return 1;
649 }
650 return 0;
651}
652
653/*
654** To remove a packet from the receive queue you clear its PKT_IN_USE bit,
655** and then bump the pointers. Once the pointers get to the end, they must
656** be wrapped back to the start.
657*/
658void remove_receive(struct Port *PortP)
659{
660 writew(readw(PortP->RxRemove) & ~PKT_IN_USE, PortP->RxRemove);
661 PortP->RxRemove = (PortP->RxRemove == PortP->RxEnd) ? PortP->RxStart : PortP->RxRemove + 1;
662 writew(RIO_OFF(PortP->Caddr, PortP->RxRemove), &PortP->PhbP->rx_remove);
663}
diff --git a/drivers/char/rio/rioroute.c b/drivers/char/rio/rioroute.c
deleted file mode 100644
index f9b936ac3394..000000000000
--- a/drivers/char/rio/rioroute.c
+++ /dev/null
@@ -1,1039 +0,0 @@
1/*
2** -----------------------------------------------------------------------------
3**
4** Perle Specialix driver for Linux
5** Ported from existing RIO Driver for SCO sources.
6 *
7 * (C) 1990 - 2000 Specialix International Ltd., Byfleet, Surrey, UK.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22**
23** Module : rioroute.c
24** SID : 1.3
25** Last Modified : 11/6/98 10:33:46
26** Retrieved : 11/6/98 10:33:50
27**
28** ident @(#)rioroute.c 1.3
29**
30** -----------------------------------------------------------------------------
31*/
32
33#include <linux/module.h>
34#include <linux/errno.h>
35#include <asm/io.h>
36#include <asm/system.h>
37#include <asm/string.h>
38#include <asm/uaccess.h>
39
40#include <linux/termios.h>
41#include <linux/serial.h>
42
43#include <linux/generic_serial.h>
44
45
46#include "linux_compat.h"
47#include "rio_linux.h"
48#include "pkt.h"
49#include "daemon.h"
50#include "rio.h"
51#include "riospace.h"
52#include "cmdpkt.h"
53#include "map.h"
54#include "rup.h"
55#include "port.h"
56#include "riodrvr.h"
57#include "rioinfo.h"
58#include "func.h"
59#include "errors.h"
60#include "pci.h"
61
62#include "parmmap.h"
63#include "unixrup.h"
64#include "board.h"
65#include "host.h"
66#include "phb.h"
67#include "link.h"
68#include "cmdblk.h"
69#include "route.h"
70#include "cirrus.h"
71#include "rioioctl.h"
72#include "param.h"
73
74static int RIOCheckIsolated(struct rio_info *, struct Host *, unsigned int);
75static int RIOIsolate(struct rio_info *, struct Host *, unsigned int);
76static int RIOCheck(struct Host *, unsigned int);
77static void RIOConCon(struct rio_info *, struct Host *, unsigned int, unsigned int, unsigned int, unsigned int, int);
78
79
80/*
81** Incoming on the ROUTE_RUP
82** I wrote this while I was tired. Forgive me.
83*/
84int RIORouteRup(struct rio_info *p, unsigned int Rup, struct Host *HostP, struct PKT __iomem * PacketP)
85{
86 struct PktCmd __iomem *PktCmdP = (struct PktCmd __iomem *) PacketP->data;
87 struct PktCmd_M *PktReplyP;
88 struct CmdBlk *CmdBlkP;
89 struct Port *PortP;
90 struct Map *MapP;
91 struct Top *TopP;
92 int ThisLink, ThisLinkMin, ThisLinkMax;
93 int port;
94 int Mod, Mod1, Mod2;
95 unsigned short RtaType;
96 unsigned int RtaUniq;
97 unsigned int ThisUnit, ThisUnit2; /* 2 ids to accommodate 16 port RTA */
98 unsigned int OldUnit, NewUnit, OldLink, NewLink;
99 char *MyType, *MyName;
100 int Lies;
101 unsigned long flags;
102
103 /*
104 ** Is this unit telling us it's current link topology?
105 */
106 if (readb(&PktCmdP->Command) == ROUTE_TOPOLOGY) {
107 MapP = HostP->Mapping;
108
109 /*
110 ** The packet can be sent either by the host or by an RTA.
111 ** If it comes from the host, then we need to fill in the
112 ** Topology array in the host structure. If it came in
113 ** from an RTA then we need to fill in the Mapping structure's
114 ** Topology array for the unit.
115 */
116 if (Rup >= (unsigned short) MAX_RUP) {
117 ThisUnit = HOST_ID;
118 TopP = HostP->Topology;
119 MyType = "Host";
120 MyName = HostP->Name;
121 ThisLinkMin = ThisLinkMax = Rup - MAX_RUP;
122 } else {
123 ThisUnit = Rup + 1;
124 TopP = HostP->Mapping[Rup].Topology;
125 MyType = "RTA";
126 MyName = HostP->Mapping[Rup].Name;
127 ThisLinkMin = 0;
128 ThisLinkMax = LINKS_PER_UNIT - 1;
129 }
130
131 /*
132 ** Lies will not be tolerated.
133 ** If any pair of links claim to be connected to the same
134 ** place, then ignore this packet completely.
135 */
136 Lies = 0;
137 for (ThisLink = ThisLinkMin + 1; ThisLink <= ThisLinkMax; ThisLink++) {
138 /*
139 ** it won't lie about network interconnect, total disconnects
140 ** and no-IDs. (or at least, it doesn't *matter* if it does)
141 */
142 if (readb(&PktCmdP->RouteTopology[ThisLink].Unit) > (unsigned short) MAX_RUP)
143 continue;
144
145 for (NewLink = ThisLinkMin; NewLink < ThisLink; NewLink++) {
146 if ((readb(&PktCmdP->RouteTopology[ThisLink].Unit) == readb(&PktCmdP->RouteTopology[NewLink].Unit)) && (readb(&PktCmdP->RouteTopology[ThisLink].Link) == readb(&PktCmdP->RouteTopology[NewLink].Link))) {
147 Lies++;
148 }
149 }
150 }
151
152 if (Lies) {
153 rio_dprintk(RIO_DEBUG_ROUTE, "LIES! DAMN LIES! %d LIES!\n", Lies);
154 rio_dprintk(RIO_DEBUG_ROUTE, "%d:%c %d:%c %d:%c %d:%c\n",
155 readb(&PktCmdP->RouteTopology[0].Unit),
156 'A' + readb(&PktCmdP->RouteTopology[0].Link),
157 readb(&PktCmdP->RouteTopology[1].Unit),
158 'A' + readb(&PktCmdP->RouteTopology[1].Link), readb(&PktCmdP->RouteTopology[2].Unit), 'A' + readb(&PktCmdP->RouteTopology[2].Link), readb(&PktCmdP->RouteTopology[3].Unit), 'A' + readb(&PktCmdP->RouteTopology[3].Link));
159 return 1;
160 }
161
162 /*
163 ** now, process each link.
164 */
165 for (ThisLink = ThisLinkMin; ThisLink <= ThisLinkMax; ThisLink++) {
166 /*
167 ** this is what it was connected to
168 */
169 OldUnit = TopP[ThisLink].Unit;
170 OldLink = TopP[ThisLink].Link;
171
172 /*
173 ** this is what it is now connected to
174 */
175 NewUnit = readb(&PktCmdP->RouteTopology[ThisLink].Unit);
176 NewLink = readb(&PktCmdP->RouteTopology[ThisLink].Link);
177
178 if (OldUnit != NewUnit || OldLink != NewLink) {
179 /*
180 ** something has changed!
181 */
182
183 if (NewUnit > MAX_RUP && NewUnit != ROUTE_DISCONNECT && NewUnit != ROUTE_NO_ID && NewUnit != ROUTE_INTERCONNECT) {
184 rio_dprintk(RIO_DEBUG_ROUTE, "I have a link from %s %s to unit %d:%d - I don't like it.\n", MyType, MyName, NewUnit, NewLink);
185 } else {
186 /*
187 ** put the new values in
188 */
189 TopP[ThisLink].Unit = NewUnit;
190 TopP[ThisLink].Link = NewLink;
191
192 RIOSetChange(p);
193
194 if (OldUnit <= MAX_RUP) {
195 /*
196 ** If something has become bust, then re-enable them messages
197 */
198 if (!p->RIONoMessage)
199 RIOConCon(p, HostP, ThisUnit, ThisLink, OldUnit, OldLink, DISCONNECT);
200 }
201
202 if ((NewUnit <= MAX_RUP) && !p->RIONoMessage)
203 RIOConCon(p, HostP, ThisUnit, ThisLink, NewUnit, NewLink, CONNECT);
204
205 if (NewUnit == ROUTE_NO_ID)
206 rio_dprintk(RIO_DEBUG_ROUTE, "%s %s (%c) is connected to an unconfigured unit.\n", MyType, MyName, 'A' + ThisLink);
207
208 if (NewUnit == ROUTE_INTERCONNECT) {
209 if (!p->RIONoMessage)
210 printk(KERN_DEBUG "rio: %s '%s' (%c) is connected to another network.\n", MyType, MyName, 'A' + ThisLink);
211 }
212
213 /*
214 ** perform an update for 'the other end', so that these messages
215 ** only appears once. Only disconnect the other end if it is pointing
216 ** at us!
217 */
218 if (OldUnit == HOST_ID) {
219 if (HostP->Topology[OldLink].Unit == ThisUnit && HostP->Topology[OldLink].Link == ThisLink) {
220 rio_dprintk(RIO_DEBUG_ROUTE, "SETTING HOST (%c) TO DISCONNECTED!\n", OldLink + 'A');
221 HostP->Topology[OldLink].Unit = ROUTE_DISCONNECT;
222 HostP->Topology[OldLink].Link = NO_LINK;
223 } else {
224 rio_dprintk(RIO_DEBUG_ROUTE, "HOST(%c) WAS NOT CONNECTED TO %s (%c)!\n", OldLink + 'A', HostP->Mapping[ThisUnit - 1].Name, ThisLink + 'A');
225 }
226 } else if (OldUnit <= MAX_RUP) {
227 if (HostP->Mapping[OldUnit - 1].Topology[OldLink].Unit == ThisUnit && HostP->Mapping[OldUnit - 1].Topology[OldLink].Link == ThisLink) {
228 rio_dprintk(RIO_DEBUG_ROUTE, "SETTING RTA %s (%c) TO DISCONNECTED!\n", HostP->Mapping[OldUnit - 1].Name, OldLink + 'A');
229 HostP->Mapping[OldUnit - 1].Topology[OldLink].Unit = ROUTE_DISCONNECT;
230 HostP->Mapping[OldUnit - 1].Topology[OldLink].Link = NO_LINK;
231 } else {
232 rio_dprintk(RIO_DEBUG_ROUTE, "RTA %s (%c) WAS NOT CONNECTED TO %s (%c)\n", HostP->Mapping[OldUnit - 1].Name, OldLink + 'A', HostP->Mapping[ThisUnit - 1].Name, ThisLink + 'A');
233 }
234 }
235 if (NewUnit == HOST_ID) {
236 rio_dprintk(RIO_DEBUG_ROUTE, "MARKING HOST (%c) CONNECTED TO %s (%c)\n", NewLink + 'A', MyName, ThisLink + 'A');
237 HostP->Topology[NewLink].Unit = ThisUnit;
238 HostP->Topology[NewLink].Link = ThisLink;
239 } else if (NewUnit <= MAX_RUP) {
240 rio_dprintk(RIO_DEBUG_ROUTE, "MARKING RTA %s (%c) CONNECTED TO %s (%c)\n", HostP->Mapping[NewUnit - 1].Name, NewLink + 'A', MyName, ThisLink + 'A');
241 HostP->Mapping[NewUnit - 1].Topology[NewLink].Unit = ThisUnit;
242 HostP->Mapping[NewUnit - 1].Topology[NewLink].Link = ThisLink;
243 }
244 }
245 RIOSetChange(p);
246 RIOCheckIsolated(p, HostP, OldUnit);
247 }
248 }
249 return 1;
250 }
251
252 /*
253 ** The only other command we recognise is a route_request command
254 */
255 if (readb(&PktCmdP->Command) != ROUTE_REQUEST) {
256 rio_dprintk(RIO_DEBUG_ROUTE, "Unknown command %d received on rup %d host %p ROUTE_RUP\n", readb(&PktCmdP->Command), Rup, HostP);
257 return 1;
258 }
259
260 RtaUniq = (readb(&PktCmdP->UniqNum[0])) + (readb(&PktCmdP->UniqNum[1]) << 8) + (readb(&PktCmdP->UniqNum[2]) << 16) + (readb(&PktCmdP->UniqNum[3]) << 24);
261
262 /*
263 ** Determine if 8 or 16 port RTA
264 */
265 RtaType = GetUnitType(RtaUniq);
266
267 rio_dprintk(RIO_DEBUG_ROUTE, "Received a request for an ID for serial number %x\n", RtaUniq);
268
269 Mod = readb(&PktCmdP->ModuleTypes);
270 Mod1 = LONYBLE(Mod);
271 if (RtaType == TYPE_RTA16) {
272 /*
273 ** Only one ident is set for a 16 port RTA. To make compatible
274 ** with 8 port, set 2nd ident in Mod2 to the same as Mod1.
275 */
276 Mod2 = Mod1;
277 rio_dprintk(RIO_DEBUG_ROUTE, "Backplane type is %s (all ports)\n", p->RIOModuleTypes[Mod1].Name);
278 } else {
279 Mod2 = HINYBLE(Mod);
280 rio_dprintk(RIO_DEBUG_ROUTE, "Module types are %s (ports 0-3) and %s (ports 4-7)\n", p->RIOModuleTypes[Mod1].Name, p->RIOModuleTypes[Mod2].Name);
281 }
282
283 /*
284 ** try to unhook a command block from the command free list.
285 */
286 if (!(CmdBlkP = RIOGetCmdBlk())) {
287 rio_dprintk(RIO_DEBUG_ROUTE, "No command blocks to route RTA! come back later.\n");
288 return 0;
289 }
290
291 /*
292 ** Fill in the default info on the command block
293 */
294 CmdBlkP->Packet.dest_unit = Rup;
295 CmdBlkP->Packet.dest_port = ROUTE_RUP;
296 CmdBlkP->Packet.src_unit = HOST_ID;
297 CmdBlkP->Packet.src_port = ROUTE_RUP;
298 CmdBlkP->Packet.len = PKT_CMD_BIT | 1;
299 CmdBlkP->PreFuncP = CmdBlkP->PostFuncP = NULL;
300 PktReplyP = (struct PktCmd_M *) CmdBlkP->Packet.data;
301
302 if (!RIOBootOk(p, HostP, RtaUniq)) {
303 rio_dprintk(RIO_DEBUG_ROUTE, "RTA %x tried to get an ID, but does not belong - FOAD it!\n", RtaUniq);
304 PktReplyP->Command = ROUTE_FOAD;
305 memcpy(PktReplyP->CommandText, "RT_FOAD", 7);
306 RIOQueueCmdBlk(HostP, Rup, CmdBlkP);
307 return 1;
308 }
309
310 /*
311 ** Check to see if the RTA is configured for this host
312 */
313 for (ThisUnit = 0; ThisUnit < MAX_RUP; ThisUnit++) {
314 rio_dprintk(RIO_DEBUG_ROUTE, "Entry %d Flags=%s %s UniqueNum=0x%x\n",
315 ThisUnit, HostP->Mapping[ThisUnit].Flags & SLOT_IN_USE ? "Slot-In-Use" : "Not In Use", HostP->Mapping[ThisUnit].Flags & SLOT_TENTATIVE ? "Slot-Tentative" : "Not Tentative", HostP->Mapping[ThisUnit].RtaUniqueNum);
316
317 /*
318 ** We have an entry for it.
319 */
320 if ((HostP->Mapping[ThisUnit].Flags & (SLOT_IN_USE | SLOT_TENTATIVE)) && (HostP->Mapping[ThisUnit].RtaUniqueNum == RtaUniq)) {
321 if (RtaType == TYPE_RTA16) {
322 ThisUnit2 = HostP->Mapping[ThisUnit].ID2 - 1;
323 rio_dprintk(RIO_DEBUG_ROUTE, "Found unit 0x%x at slots %d+%d\n", RtaUniq, ThisUnit, ThisUnit2);
324 } else
325 rio_dprintk(RIO_DEBUG_ROUTE, "Found unit 0x%x at slot %d\n", RtaUniq, ThisUnit);
326 /*
327 ** If we have no knowledge of booting it, then the host has
328 ** been re-booted, and so we must kill the RTA, so that it
329 ** will be booted again (potentially with new bins)
330 ** and it will then re-ask for an ID, which we will service.
331 */
332 if ((HostP->Mapping[ThisUnit].Flags & SLOT_IN_USE) && !(HostP->Mapping[ThisUnit].Flags & RTA_BOOTED)) {
333 if (!(HostP->Mapping[ThisUnit].Flags & MSG_DONE)) {
334 if (!p->RIONoMessage)
335 printk(KERN_DEBUG "rio: RTA '%s' is being updated.\n", HostP->Mapping[ThisUnit].Name);
336 HostP->Mapping[ThisUnit].Flags |= MSG_DONE;
337 }
338 PktReplyP->Command = ROUTE_FOAD;
339 memcpy(PktReplyP->CommandText, "RT_FOAD", 7);
340 RIOQueueCmdBlk(HostP, Rup, CmdBlkP);
341 return 1;
342 }
343
344 /*
345 ** Send the ID (entry) to this RTA. The ID number is implicit as
346 ** the offset into the table. It is worth noting at this stage
347 ** that offset zero in the table contains the entries for the
348 ** RTA with ID 1!!!!
349 */
350 PktReplyP->Command = ROUTE_ALLOCATE;
351 PktReplyP->IDNum = ThisUnit + 1;
352 if (RtaType == TYPE_RTA16) {
353 if (HostP->Mapping[ThisUnit].Flags & SLOT_IN_USE)
354 /*
355 ** Adjust the phb and tx pkt dest_units for 2nd block of 8
356 ** only if the RTA has ports associated (SLOT_IN_USE)
357 */
358 RIOFixPhbs(p, HostP, ThisUnit2);
359 PktReplyP->IDNum2 = ThisUnit2 + 1;
360 rio_dprintk(RIO_DEBUG_ROUTE, "RTA '%s' has been allocated IDs %d+%d\n", HostP->Mapping[ThisUnit].Name, PktReplyP->IDNum, PktReplyP->IDNum2);
361 } else {
362 PktReplyP->IDNum2 = ROUTE_NO_ID;
363 rio_dprintk(RIO_DEBUG_ROUTE, "RTA '%s' has been allocated ID %d\n", HostP->Mapping[ThisUnit].Name, PktReplyP->IDNum);
364 }
365 memcpy(PktReplyP->CommandText, "RT_ALLOCAT", 10);
366
367 RIOQueueCmdBlk(HostP, Rup, CmdBlkP);
368
369 /*
370 ** If this is a freshly booted RTA, then we need to re-open
371 ** the ports, if any where open, so that data may once more
372 ** flow around the system!
373 */
374 if ((HostP->Mapping[ThisUnit].Flags & RTA_NEWBOOT) && (HostP->Mapping[ThisUnit].SysPort != NO_PORT)) {
375 /*
376 ** look at the ports associated with this beast and
377 ** see if any where open. If they was, then re-open
378 ** them, using the info from the tty flags.
379 */
380 for (port = 0; port < PORTS_PER_RTA; port++) {
381 PortP = p->RIOPortp[port + HostP->Mapping[ThisUnit].SysPort];
382 if (PortP->State & (RIO_MOPEN | RIO_LOPEN)) {
383 rio_dprintk(RIO_DEBUG_ROUTE, "Re-opened this port\n");
384 rio_spin_lock_irqsave(&PortP->portSem, flags);
385 PortP->MagicFlags |= MAGIC_REBOOT;
386 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
387 }
388 }
389 if (RtaType == TYPE_RTA16) {
390 for (port = 0; port < PORTS_PER_RTA; port++) {
391 PortP = p->RIOPortp[port + HostP->Mapping[ThisUnit2].SysPort];
392 if (PortP->State & (RIO_MOPEN | RIO_LOPEN)) {
393 rio_dprintk(RIO_DEBUG_ROUTE, "Re-opened this port\n");
394 rio_spin_lock_irqsave(&PortP->portSem, flags);
395 PortP->MagicFlags |= MAGIC_REBOOT;
396 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
397 }
398 }
399 }
400 }
401
402 /*
403 ** keep a copy of the module types!
404 */
405 HostP->UnixRups[ThisUnit].ModTypes = Mod;
406 if (RtaType == TYPE_RTA16)
407 HostP->UnixRups[ThisUnit2].ModTypes = Mod;
408
409 /*
410 ** If either of the modules on this unit is read-only or write-only
411 ** or none-xprint, then we need to transfer that info over to the
412 ** relevant ports.
413 */
414 if (HostP->Mapping[ThisUnit].SysPort != NO_PORT) {
415 for (port = 0; port < PORTS_PER_MODULE; port++) {
416 p->RIOPortp[port + HostP->Mapping[ThisUnit].SysPort]->Config &= ~RIO_NOMASK;
417 p->RIOPortp[port + HostP->Mapping[ThisUnit].SysPort]->Config |= p->RIOModuleTypes[Mod1].Flags[port];
418 p->RIOPortp[port + PORTS_PER_MODULE + HostP->Mapping[ThisUnit].SysPort]->Config &= ~RIO_NOMASK;
419 p->RIOPortp[port + PORTS_PER_MODULE + HostP->Mapping[ThisUnit].SysPort]->Config |= p->RIOModuleTypes[Mod2].Flags[port];
420 }
421 if (RtaType == TYPE_RTA16) {
422 for (port = 0; port < PORTS_PER_MODULE; port++) {
423 p->RIOPortp[port + HostP->Mapping[ThisUnit2].SysPort]->Config &= ~RIO_NOMASK;
424 p->RIOPortp[port + HostP->Mapping[ThisUnit2].SysPort]->Config |= p->RIOModuleTypes[Mod1].Flags[port];
425 p->RIOPortp[port + PORTS_PER_MODULE + HostP->Mapping[ThisUnit2].SysPort]->Config &= ~RIO_NOMASK;
426 p->RIOPortp[port + PORTS_PER_MODULE + HostP->Mapping[ThisUnit2].SysPort]->Config |= p->RIOModuleTypes[Mod2].Flags[port];
427 }
428 }
429 }
430
431 /*
432 ** Job done, get on with the interrupts!
433 */
434 return 1;
435 }
436 }
437 /*
438 ** There is no table entry for this RTA at all.
439 **
440 ** Lets check to see if we actually booted this unit - if not,
441 ** then we reset it and it will go round the loop of being booted
442 ** we can then worry about trying to fit it into the table.
443 */
444 for (ThisUnit = 0; ThisUnit < HostP->NumExtraBooted; ThisUnit++)
445 if (HostP->ExtraUnits[ThisUnit] == RtaUniq)
446 break;
447 if (ThisUnit == HostP->NumExtraBooted && ThisUnit != MAX_EXTRA_UNITS) {
448 /*
449 ** if the unit wasn't in the table, and the table wasn't full, then
450 ** we reset the unit, because we didn't boot it.
451 ** However, if the table is full, it could be that we did boot
452 ** this unit, and so we won't reboot it, because it isn't really
453 ** all that disasterous to keep the old bins in most cases. This
454 ** is a rather tacky feature, but we are on the edge of reallity
455 ** here, because the implication is that someone has connected
456 ** 16+MAX_EXTRA_UNITS onto one host.
457 */
458 static int UnknownMesgDone = 0;
459
460 if (!UnknownMesgDone) {
461 if (!p->RIONoMessage)
462 printk(KERN_DEBUG "rio: One or more unknown RTAs are being updated.\n");
463 UnknownMesgDone = 1;
464 }
465
466 PktReplyP->Command = ROUTE_FOAD;
467 memcpy(PktReplyP->CommandText, "RT_FOAD", 7);
468 } else {
469 /*
470 ** we did boot it (as an extra), and there may now be a table
471 ** slot free (because of a delete), so we will try to make
472 ** a tentative entry for it, so that the configurator can see it
473 ** and fill in the details for us.
474 */
475 if (RtaType == TYPE_RTA16) {
476 if (RIOFindFreeID(p, HostP, &ThisUnit, &ThisUnit2) == 0) {
477 RIODefaultName(p, HostP, ThisUnit);
478 rio_fill_host_slot(ThisUnit, ThisUnit2, RtaUniq, HostP);
479 }
480 } else {
481 if (RIOFindFreeID(p, HostP, &ThisUnit, NULL) == 0) {
482 RIODefaultName(p, HostP, ThisUnit);
483 rio_fill_host_slot(ThisUnit, 0, RtaUniq, HostP);
484 }
485 }
486 PktReplyP->Command = ROUTE_USED;
487 memcpy(PktReplyP->CommandText, "RT_USED", 7);
488 }
489 RIOQueueCmdBlk(HostP, Rup, CmdBlkP);
490 return 1;
491}
492
493
494void RIOFixPhbs(struct rio_info *p, struct Host *HostP, unsigned int unit)
495{
496 unsigned short link, port;
497 struct Port *PortP;
498 unsigned long flags;
499 int PortN = HostP->Mapping[unit].SysPort;
500
501 rio_dprintk(RIO_DEBUG_ROUTE, "RIOFixPhbs unit %d sysport %d\n", unit, PortN);
502
503 if (PortN != -1) {
504 unsigned short dest_unit = HostP->Mapping[unit].ID2;
505
506 /*
507 ** Get the link number used for the 1st 8 phbs on this unit.
508 */
509 PortP = p->RIOPortp[HostP->Mapping[dest_unit - 1].SysPort];
510
511 link = readw(&PortP->PhbP->link);
512
513 for (port = 0; port < PORTS_PER_RTA; port++, PortN++) {
514 unsigned short dest_port = port + 8;
515 u16 __iomem *TxPktP;
516 struct PKT __iomem *Pkt;
517
518 PortP = p->RIOPortp[PortN];
519
520 rio_spin_lock_irqsave(&PortP->portSem, flags);
521 /*
522 ** If RTA is not powered on, the tx packets will be
523 ** unset, so go no further.
524 */
525 if (!PortP->TxStart) {
526 rio_dprintk(RIO_DEBUG_ROUTE, "Tx pkts not set up yet\n");
527 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
528 break;
529 }
530
531 /*
532 ** For the second slot of a 16 port RTA, the driver needs to
533 ** sort out the phb to port mappings. The dest_unit for this
534 ** group of 8 phbs is set to the dest_unit of the accompanying
535 ** 8 port block. The dest_port of the second unit is set to
536 ** be in the range 8-15 (i.e. 8 is added). Thus, for a 16 port
537 ** RTA with IDs 5 and 6, traffic bound for port 6 of unit 6
538 ** (being the second map ID) will be sent to dest_unit 5, port
539 ** 14. When this RTA is deleted, dest_unit for ID 6 will be
540 ** restored, and the dest_port will be reduced by 8.
541 ** Transmit packets also have a destination field which needs
542 ** adjusting in the same manner.
543 ** Note that the unit/port bytes in 'dest' are swapped.
544 ** We also need to adjust the phb and rup link numbers for the
545 ** second block of 8 ttys.
546 */
547 for (TxPktP = PortP->TxStart; TxPktP <= PortP->TxEnd; TxPktP++) {
548 /*
549 ** *TxPktP is the pointer to the transmit packet on the host
550 ** card. This needs to be translated into a 32 bit pointer
551 ** so it can be accessed from the driver.
552 */
553 Pkt = (struct PKT __iomem *) RIO_PTR(HostP->Caddr, readw(TxPktP));
554
555 /*
556 ** If the packet is used, reset it.
557 */
558 Pkt = (struct PKT __iomem *) ((unsigned long) Pkt & ~PKT_IN_USE);
559 writeb(dest_unit, &Pkt->dest_unit);
560 writeb(dest_port, &Pkt->dest_port);
561 }
562 rio_dprintk(RIO_DEBUG_ROUTE, "phb dest: Old %x:%x New %x:%x\n", readw(&PortP->PhbP->destination) & 0xff, (readw(&PortP->PhbP->destination) >> 8) & 0xff, dest_unit, dest_port);
563 writew(dest_unit + (dest_port << 8), &PortP->PhbP->destination);
564 writew(link, &PortP->PhbP->link);
565
566 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
567 }
568 /*
569 ** Now make sure the range of ports to be serviced includes
570 ** the 2nd 8 on this 16 port RTA.
571 */
572 if (link > 3)
573 return;
574 if (((unit * 8) + 7) > readw(&HostP->LinkStrP[link].last_port)) {
575 rio_dprintk(RIO_DEBUG_ROUTE, "last port on host link %d: %d\n", link, (unit * 8) + 7);
576 writew((unit * 8) + 7, &HostP->LinkStrP[link].last_port);
577 }
578 }
579}
580
581/*
582** Check to see if the new disconnection has isolated this unit.
583** If it has, then invalidate all its link information, and tell
584** the world about it. This is done to ensure that the configurator
585** only gets up-to-date information about what is going on.
586*/
587static int RIOCheckIsolated(struct rio_info *p, struct Host *HostP, unsigned int UnitId)
588{
589 unsigned long flags;
590 rio_spin_lock_irqsave(&HostP->HostLock, flags);
591
592 if (RIOCheck(HostP, UnitId)) {
593 rio_dprintk(RIO_DEBUG_ROUTE, "Unit %d is NOT isolated\n", UnitId);
594 rio_spin_unlock_irqrestore(&HostP->HostLock, flags);
595 return (0);
596 }
597
598 RIOIsolate(p, HostP, UnitId);
599 RIOSetChange(p);
600 rio_spin_unlock_irqrestore(&HostP->HostLock, flags);
601 return 1;
602}
603
604/*
605** Invalidate all the link interconnectivity of this unit, and of
606** all the units attached to it. This will mean that the entire
607** subnet will re-introduce itself.
608*/
609static int RIOIsolate(struct rio_info *p, struct Host *HostP, unsigned int UnitId)
610{
611 unsigned int link, unit;
612
613 UnitId--; /* this trick relies on the Unit Id being UNSIGNED! */
614
615 if (UnitId >= MAX_RUP) /* dontcha just lurv unsigned maths! */
616 return (0);
617
618 if (HostP->Mapping[UnitId].Flags & BEEN_HERE)
619 return (0);
620
621 HostP->Mapping[UnitId].Flags |= BEEN_HERE;
622
623 if (p->RIOPrintDisabled == DO_PRINT)
624 rio_dprintk(RIO_DEBUG_ROUTE, "RIOMesgIsolated %s", HostP->Mapping[UnitId].Name);
625
626 for (link = 0; link < LINKS_PER_UNIT; link++) {
627 unit = HostP->Mapping[UnitId].Topology[link].Unit;
628 HostP->Mapping[UnitId].Topology[link].Unit = ROUTE_DISCONNECT;
629 HostP->Mapping[UnitId].Topology[link].Link = NO_LINK;
630 RIOIsolate(p, HostP, unit);
631 }
632 HostP->Mapping[UnitId].Flags &= ~BEEN_HERE;
633 return 1;
634}
635
636static int RIOCheck(struct Host *HostP, unsigned int UnitId)
637{
638 unsigned char link;
639
640/* rio_dprint(RIO_DEBUG_ROUTE, ("Check to see if unit %d has a route to the host\n",UnitId)); */
641 rio_dprintk(RIO_DEBUG_ROUTE, "RIOCheck : UnitID = %d\n", UnitId);
642
643 if (UnitId == HOST_ID) {
644 /* rio_dprint(RIO_DEBUG_ROUTE, ("Unit %d is NOT isolated - it IS the host!\n", UnitId)); */
645 return 1;
646 }
647
648 UnitId--;
649
650 if (UnitId >= MAX_RUP) {
651 /* rio_dprint(RIO_DEBUG_ROUTE, ("Unit %d - ignored.\n", UnitId)); */
652 return 0;
653 }
654
655 for (link = 0; link < LINKS_PER_UNIT; link++) {
656 if (HostP->Mapping[UnitId].Topology[link].Unit == HOST_ID) {
657 /* rio_dprint(RIO_DEBUG_ROUTE, ("Unit %d is connected directly to host via link (%c).\n",
658 UnitId, 'A'+link)); */
659 return 1;
660 }
661 }
662
663 if (HostP->Mapping[UnitId].Flags & BEEN_HERE) {
664 /* rio_dprint(RIO_DEBUG_ROUTE, ("Been to Unit %d before - ignoring\n", UnitId)); */
665 return 0;
666 }
667
668 HostP->Mapping[UnitId].Flags |= BEEN_HERE;
669
670 for (link = 0; link < LINKS_PER_UNIT; link++) {
671 /* rio_dprint(RIO_DEBUG_ROUTE, ("Unit %d check link (%c)\n", UnitId,'A'+link)); */
672 if (RIOCheck(HostP, HostP->Mapping[UnitId].Topology[link].Unit)) {
673 /* rio_dprint(RIO_DEBUG_ROUTE, ("Unit %d is connected to something that knows the host via link (%c)\n", UnitId,link+'A')); */
674 HostP->Mapping[UnitId].Flags &= ~BEEN_HERE;
675 return 1;
676 }
677 }
678
679 HostP->Mapping[UnitId].Flags &= ~BEEN_HERE;
680
681 /* rio_dprint(RIO_DEBUG_ROUTE, ("Unit %d DOESNT KNOW THE HOST!\n", UnitId)); */
682
683 return 0;
684}
685
686/*
687** Returns the type of unit (host, 16/8 port RTA)
688*/
689
690unsigned int GetUnitType(unsigned int Uniq)
691{
692 switch ((Uniq >> 28) & 0xf) {
693 case RIO_AT:
694 case RIO_MCA:
695 case RIO_EISA:
696 case RIO_PCI:
697 rio_dprintk(RIO_DEBUG_ROUTE, "Unit type: Host\n");
698 return (TYPE_HOST);
699 case RIO_RTA_16:
700 rio_dprintk(RIO_DEBUG_ROUTE, "Unit type: 16 port RTA\n");
701 return (TYPE_RTA16);
702 case RIO_RTA:
703 rio_dprintk(RIO_DEBUG_ROUTE, "Unit type: 8 port RTA\n");
704 return (TYPE_RTA8);
705 default:
706 rio_dprintk(RIO_DEBUG_ROUTE, "Unit type: Unrecognised\n");
707 return (99);
708 }
709}
710
711int RIOSetChange(struct rio_info *p)
712{
713 if (p->RIOQuickCheck != NOT_CHANGED)
714 return (0);
715 p->RIOQuickCheck = CHANGED;
716 if (p->RIOSignalProcess) {
717 rio_dprintk(RIO_DEBUG_ROUTE, "Send SIG-HUP");
718 /*
719 psignal( RIOSignalProcess, SIGHUP );
720 */
721 }
722 return (0);
723}
724
725static void RIOConCon(struct rio_info *p,
726 struct Host *HostP,
727 unsigned int FromId,
728 unsigned int FromLink,
729 unsigned int ToId,
730 unsigned int ToLink,
731 int Change)
732{
733 char *FromName;
734 char *FromType;
735 char *ToName;
736 char *ToType;
737 unsigned int tp;
738
739/*
740** 15.10.1998 ARG - ESIL 0759
741** (Part) fix for port being trashed when opened whilst RTA "disconnected"
742**
743** What's this doing in here anyway ?
744** It was causing the port to be 'unmapped' if opened whilst RTA "disconnected"
745**
746** 09.12.1998 ARG - ESIL 0776 - part fix
747** Okay, We've found out what this was all about now !
748** Someone had botched this to use RIOHalted to indicated the number of RTAs
749** 'disconnected'. The value in RIOHalted was then being used in the
750** 'RIO_QUICK_CHECK' ioctl. A none zero value indicating that a least one RTA
751** is 'disconnected'. The change was put in to satisfy a customer's needs.
752** Having taken this bit of code out 'RIO_QUICK_CHECK' now no longer works for
753** the customer.
754**
755 if (Change == CONNECT) {
756 if (p->RIOHalted) p->RIOHalted --;
757 }
758 else {
759 p->RIOHalted ++;
760 }
761**
762** So - we need to implement it slightly differently - a new member of the
763** rio_info struct - RIORtaDisCons (RIO RTA connections) keeps track of RTA
764** connections and disconnections.
765*/
766 if (Change == CONNECT) {
767 if (p->RIORtaDisCons)
768 p->RIORtaDisCons--;
769 } else {
770 p->RIORtaDisCons++;
771 }
772
773 if (p->RIOPrintDisabled == DONT_PRINT)
774 return;
775
776 if (FromId > ToId) {
777 tp = FromId;
778 FromId = ToId;
779 ToId = tp;
780 tp = FromLink;
781 FromLink = ToLink;
782 ToLink = tp;
783 }
784
785 FromName = FromId ? HostP->Mapping[FromId - 1].Name : HostP->Name;
786 FromType = FromId ? "RTA" : "HOST";
787 ToName = ToId ? HostP->Mapping[ToId - 1].Name : HostP->Name;
788 ToType = ToId ? "RTA" : "HOST";
789
790 rio_dprintk(RIO_DEBUG_ROUTE, "Link between %s '%s' (%c) and %s '%s' (%c) %s.\n", FromType, FromName, 'A' + FromLink, ToType, ToName, 'A' + ToLink, (Change == CONNECT) ? "established" : "disconnected");
791 printk(KERN_DEBUG "rio: Link between %s '%s' (%c) and %s '%s' (%c) %s.\n", FromType, FromName, 'A' + FromLink, ToType, ToName, 'A' + ToLink, (Change == CONNECT) ? "established" : "disconnected");
792}
793
794/*
795** RIORemoveFromSavedTable :
796**
797** Delete and RTA entry from the saved table given to us
798** by the configuration program.
799*/
800static int RIORemoveFromSavedTable(struct rio_info *p, struct Map *pMap)
801{
802 int entry;
803
804 /*
805 ** We loop for all entries even after finding an entry and
806 ** zeroing it because we may have two entries to delete if
807 ** it's a 16 port RTA.
808 */
809 for (entry = 0; entry < TOTAL_MAP_ENTRIES; entry++) {
810 if (p->RIOSavedTable[entry].RtaUniqueNum == pMap->RtaUniqueNum) {
811 memset(&p->RIOSavedTable[entry], 0, sizeof(struct Map));
812 }
813 }
814 return 0;
815}
816
817
818/*
819** RIOCheckDisconnected :
820**
821** Scan the unit links to and return zero if the unit is completely
822** disconnected.
823*/
824static int RIOFreeDisconnected(struct rio_info *p, struct Host *HostP, int unit)
825{
826 int link;
827
828
829 rio_dprintk(RIO_DEBUG_ROUTE, "RIOFreeDisconnect unit %d\n", unit);
830 /*
831 ** If the slot is tentative and does not belong to the
832 ** second half of a 16 port RTA then scan to see if
833 ** is disconnected.
834 */
835 for (link = 0; link < LINKS_PER_UNIT; link++) {
836 if (HostP->Mapping[unit].Topology[link].Unit != ROUTE_DISCONNECT)
837 break;
838 }
839
840 /*
841 ** If not all links are disconnected then we can forget about it.
842 */
843 if (link < LINKS_PER_UNIT)
844 return 1;
845
846#ifdef NEED_TO_FIX_THIS
847 /* Ok so all the links are disconnected. But we may have only just
848 ** made this slot tentative and not yet received a topology update.
849 ** Lets check how long ago we made it tentative.
850 */
851 rio_dprintk(RIO_DEBUG_ROUTE, "Just about to check LBOLT on entry %d\n", unit);
852 if (drv_getparm(LBOLT, (ulong_t *) & current_time))
853 rio_dprintk(RIO_DEBUG_ROUTE, "drv_getparm(LBOLT,....) Failed.\n");
854
855 elapse_time = current_time - TentTime[unit];
856 rio_dprintk(RIO_DEBUG_ROUTE, "elapse %d = current %d - tent %d (%d usec)\n", elapse_time, current_time, TentTime[unit], drv_hztousec(elapse_time));
857 if (drv_hztousec(elapse_time) < WAIT_TO_FINISH) {
858 rio_dprintk(RIO_DEBUG_ROUTE, "Skipping slot %d, not timed out yet %d\n", unit, drv_hztousec(elapse_time));
859 return 1;
860 }
861#endif
862
863 /*
864 ** We have found an usable slot.
865 ** If it is half of a 16 port RTA then delete the other half.
866 */
867 if (HostP->Mapping[unit].ID2 != 0) {
868 int nOther = (HostP->Mapping[unit].ID2) - 1;
869
870 rio_dprintk(RIO_DEBUG_ROUTE, "RioFreedis second slot %d.\n", nOther);
871 memset(&HostP->Mapping[nOther], 0, sizeof(struct Map));
872 }
873 RIORemoveFromSavedTable(p, &HostP->Mapping[unit]);
874
875 return 0;
876}
877
878
879/*
880** RIOFindFreeID :
881**
882** This function scans the given host table for either one
883** or two free unit ID's.
884*/
885
886int RIOFindFreeID(struct rio_info *p, struct Host *HostP, unsigned int * pID1, unsigned int * pID2)
887{
888 int unit, tempID;
889
890 /*
891 ** Initialise the ID's to MAX_RUP.
892 ** We do this to make the loop for setting the ID's as simple as
893 ** possible.
894 */
895 *pID1 = MAX_RUP;
896 if (pID2 != NULL)
897 *pID2 = MAX_RUP;
898
899 /*
900 ** Scan all entries of the host mapping table for free slots.
901 ** We scan for free slots first and then if that is not successful
902 ** we start all over again looking for tentative slots we can re-use.
903 */
904 for (unit = 0; unit < MAX_RUP; unit++) {
905 rio_dprintk(RIO_DEBUG_ROUTE, "Scanning unit %d\n", unit);
906 /*
907 ** If the flags are zero then the slot is empty.
908 */
909 if (HostP->Mapping[unit].Flags == 0) {
910 rio_dprintk(RIO_DEBUG_ROUTE, " This slot is empty.\n");
911 /*
912 ** If we haven't allocated the first ID then do it now.
913 */
914 if (*pID1 == MAX_RUP) {
915 rio_dprintk(RIO_DEBUG_ROUTE, "Make tentative entry for first unit %d\n", unit);
916 *pID1 = unit;
917
918 /*
919 ** If the second ID is not needed then we can return
920 ** now.
921 */
922 if (pID2 == NULL)
923 return 0;
924 } else {
925 /*
926 ** Allocate the second slot and return.
927 */
928 rio_dprintk(RIO_DEBUG_ROUTE, "Make tentative entry for second unit %d\n", unit);
929 *pID2 = unit;
930 return 0;
931 }
932 }
933 }
934
935 /*
936 ** If we manage to come out of the free slot loop then we
937 ** need to start all over again looking for tentative slots
938 ** that we can re-use.
939 */
940 rio_dprintk(RIO_DEBUG_ROUTE, "Starting to scan for tentative slots\n");
941 for (unit = 0; unit < MAX_RUP; unit++) {
942 if (((HostP->Mapping[unit].Flags & SLOT_TENTATIVE) || (HostP->Mapping[unit].Flags == 0)) && !(HostP->Mapping[unit].Flags & RTA16_SECOND_SLOT)) {
943 rio_dprintk(RIO_DEBUG_ROUTE, " Slot %d looks promising.\n", unit);
944
945 if (unit == *pID1) {
946 rio_dprintk(RIO_DEBUG_ROUTE, " No it isn't, its the 1st half\n");
947 continue;
948 }
949
950 /*
951 ** Slot is Tentative or Empty, but not a tentative second
952 ** slot of a 16 porter.
953 ** Attempt to free up this slot (and its parnter if
954 ** it is a 16 port slot. The second slot will become
955 ** empty after a call to RIOFreeDisconnected so thats why
956 ** we look for empty slots above as well).
957 */
958 if (HostP->Mapping[unit].Flags != 0)
959 if (RIOFreeDisconnected(p, HostP, unit) != 0)
960 continue;
961 /*
962 ** If we haven't allocated the first ID then do it now.
963 */
964 if (*pID1 == MAX_RUP) {
965 rio_dprintk(RIO_DEBUG_ROUTE, "Grab tentative entry for first unit %d\n", unit);
966 *pID1 = unit;
967
968 /*
969 ** Clear out this slot now that we intend to use it.
970 */
971 memset(&HostP->Mapping[unit], 0, sizeof(struct Map));
972
973 /*
974 ** If the second ID is not needed then we can return
975 ** now.
976 */
977 if (pID2 == NULL)
978 return 0;
979 } else {
980 /*
981 ** Allocate the second slot and return.
982 */
983 rio_dprintk(RIO_DEBUG_ROUTE, "Grab tentative/empty entry for second unit %d\n", unit);
984 *pID2 = unit;
985
986 /*
987 ** Clear out this slot now that we intend to use it.
988 */
989 memset(&HostP->Mapping[unit], 0, sizeof(struct Map));
990
991 /* At this point under the right(wrong?) conditions
992 ** we may have a first unit ID being higher than the
993 ** second unit ID. This is a bad idea if we are about
994 ** to fill the slots with a 16 port RTA.
995 ** Better check and swap them over.
996 */
997
998 if (*pID1 > *pID2) {
999 rio_dprintk(RIO_DEBUG_ROUTE, "Swapping IDS %d %d\n", *pID1, *pID2);
1000 tempID = *pID1;
1001 *pID1 = *pID2;
1002 *pID2 = tempID;
1003 }
1004 return 0;
1005 }
1006 }
1007 }
1008
1009 /*
1010 ** If we manage to get to the end of the second loop then we
1011 ** can give up and return a failure.
1012 */
1013 return 1;
1014}
1015
1016
1017/*
1018** The link switch scenario.
1019**
1020** Rta Wun (A) is connected to Tuw (A).
1021** The tables are all up to date, and the system is OK.
1022**
1023** If Wun (A) is now moved to Wun (B) before Wun (A) can
1024** become disconnected, then the follow happens:
1025**
1026** Tuw (A) spots the change of unit:link at the other end
1027** of its link and Tuw sends a topology packet reflecting
1028** the change: Tuw (A) now disconnected from Wun (A), and
1029** this is closely followed by a packet indicating that
1030** Tuw (A) is now connected to Wun (B).
1031**
1032** Wun (B) will spot that it has now become connected, and
1033** Wun will send a topology packet, which indicates that
1034** both Wun (A) and Wun (B) is connected to Tuw (A).
1035**
1036** Eventually Wun (A) realises that it is now disconnected
1037** and Wun will send out a topology packet indicating that
1038** Wun (A) is now disconnected.
1039*/
diff --git a/drivers/char/rio/riospace.h b/drivers/char/rio/riospace.h
deleted file mode 100644
index ffb31d4332b9..000000000000
--- a/drivers/char/rio/riospace.h
+++ /dev/null
@@ -1,154 +0,0 @@
1/*
2** -----------------------------------------------------------------------------
3**
4** Perle Specialix driver for Linux
5** Ported from existing RIO Driver for SCO sources.
6 *
7 * (C) 1990 - 2000 Specialix International Ltd., Byfleet, Surrey, UK.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22**
23** Module : riospace.h
24** SID : 1.2
25** Last Modified : 11/6/98 11:34:13
26** Retrieved : 11/6/98 11:34:22
27**
28** ident @(#)riospace.h 1.2
29**
30** -----------------------------------------------------------------------------
31*/
32
33#ifndef __rio_riospace_h__
34#define __rio_riospace_h__
35
36#define RIO_LOCATOR_LEN 16
37#define MAX_RIO_BOARDS 4
38
39/*
40** DONT change this file. At all. Unless you can rebuild the entire
41** device driver, which you probably can't, then the rest of the
42** driver won't see any changes you make here. So don't make any.
43** In particular, it won't be able to see changes to RIO_SLOTS
44*/
45
46struct Conf {
47 char Locator[24];
48 unsigned int StartupTime;
49 unsigned int SlowCook;
50 unsigned int IntrPollTime;
51 unsigned int BreakInterval;
52 unsigned int Timer;
53 unsigned int RtaLoadBase;
54 unsigned int HostLoadBase;
55 unsigned int XpHz;
56 unsigned int XpCps;
57 char *XpOn;
58 char *XpOff;
59 unsigned int MaxXpCps;
60 unsigned int MinXpCps;
61 unsigned int SpinCmds;
62 unsigned int FirstAddr;
63 unsigned int LastAddr;
64 unsigned int BufferSize;
65 unsigned int LowWater;
66 unsigned int LineLength;
67 unsigned int CmdTime;
68};
69
70/*
71** Board types - these MUST correspond to product codes!
72*/
73#define RIO_EMPTY 0x0
74#define RIO_EISA 0x3
75#define RIO_RTA_16 0x9
76#define RIO_AT 0xA
77#define RIO_MCA 0xB
78#define RIO_PCI 0xD
79#define RIO_RTA 0xE
80
81/*
82** Board data structure. This is used for configuration info
83*/
84struct Brd {
85 unsigned char Type; /* RIO_EISA, RIO_MCA, RIO_AT, RIO_EMPTY... */
86 unsigned char Ivec; /* POLLED or ivec number */
87 unsigned char Mode; /* Control stuff, see below */
88};
89
90struct Board {
91 char Locator[RIO_LOCATOR_LEN];
92 int NumSlots;
93 struct Brd Boards[MAX_RIO_BOARDS];
94};
95
96#define BOOT_FROM_LINK 0x00
97#define BOOT_FROM_RAM 0x01
98#define EXTERNAL_BUS_OFF 0x00
99#define EXTERNAL_BUS_ON 0x02
100#define INTERRUPT_DISABLE 0x00
101#define INTERRUPT_ENABLE 0x04
102#define BYTE_OPERATION 0x00
103#define WORD_OPERATION 0x08
104#define POLLED INTERRUPT_DISABLE
105#define IRQ_15 (0x00 | INTERRUPT_ENABLE)
106#define IRQ_12 (0x10 | INTERRUPT_ENABLE)
107#define IRQ_11 (0x20 | INTERRUPT_ENABLE)
108#define IRQ_9 (0x30 | INTERRUPT_ENABLE)
109#define SLOW_LINKS 0x00
110#define FAST_LINKS 0x40
111#define SLOW_AT_BUS 0x00
112#define FAST_AT_BUS 0x80
113#define SLOW_PCI_TP 0x00
114#define FAST_PCI_TP 0x80
115/*
116** Debug levels
117*/
118#define DBG_NONE 0x00000000
119
120#define DBG_INIT 0x00000001
121#define DBG_OPEN 0x00000002
122#define DBG_CLOSE 0x00000004
123#define DBG_IOCTL 0x00000008
124
125#define DBG_READ 0x00000010
126#define DBG_WRITE 0x00000020
127#define DBG_INTR 0x00000040
128#define DBG_PROC 0x00000080
129
130#define DBG_PARAM 0x00000100
131#define DBG_CMD 0x00000200
132#define DBG_XPRINT 0x00000400
133#define DBG_POLL 0x00000800
134
135#define DBG_DAEMON 0x00001000
136#define DBG_FAIL 0x00002000
137#define DBG_MODEM 0x00004000
138#define DBG_LIST 0x00008000
139
140#define DBG_ROUTE 0x00010000
141#define DBG_UTIL 0x00020000
142#define DBG_BOOT 0x00040000
143#define DBG_BUFFER 0x00080000
144
145#define DBG_MON 0x00100000
146#define DBG_SPECIAL 0x00200000
147#define DBG_VPIX 0x00400000
148#define DBG_FLUSH 0x00800000
149
150#define DBG_QENABLE 0x01000000
151
152#define DBG_ALWAYS 0x80000000
153
154#endif /* __rio_riospace_h__ */
diff --git a/drivers/char/rio/riotable.c b/drivers/char/rio/riotable.c
deleted file mode 100644
index 3d15802dc0f3..000000000000
--- a/drivers/char/rio/riotable.c
+++ /dev/null
@@ -1,941 +0,0 @@
1/*
2** -----------------------------------------------------------------------------
3**
4** Perle Specialix driver for Linux
5** Ported from existing RIO Driver for SCO sources.
6 *
7 * (C) 1990 - 2000 Specialix International Ltd., Byfleet, Surrey, UK.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22**
23** Module : riotable.c
24** SID : 1.2
25** Last Modified : 11/6/98 10:33:47
26** Retrieved : 11/6/98 10:33:50
27**
28** ident @(#)riotable.c 1.2
29**
30** -----------------------------------------------------------------------------
31*/
32
33#include <linux/module.h>
34#include <linux/slab.h>
35#include <linux/errno.h>
36#include <linux/interrupt.h>
37#include <linux/string.h>
38
39#include <asm/io.h>
40#include <asm/system.h>
41#include <asm/string.h>
42#include <asm/uaccess.h>
43
44#include <linux/termios.h>
45#include <linux/serial.h>
46
47#include <linux/generic_serial.h>
48
49
50#include "linux_compat.h"
51#include "rio_linux.h"
52#include "pkt.h"
53#include "daemon.h"
54#include "rio.h"
55#include "riospace.h"
56#include "cmdpkt.h"
57#include "map.h"
58#include "rup.h"
59#include "port.h"
60#include "riodrvr.h"
61#include "rioinfo.h"
62#include "func.h"
63#include "errors.h"
64#include "pci.h"
65
66#include "parmmap.h"
67#include "unixrup.h"
68#include "board.h"
69#include "host.h"
70#include "phb.h"
71#include "link.h"
72#include "cmdblk.h"
73#include "route.h"
74#include "cirrus.h"
75#include "rioioctl.h"
76#include "param.h"
77#include "protsts.h"
78
79/*
80** A configuration table has been loaded. It is now up to us
81** to sort it out and use the information contained therein.
82*/
83int RIONewTable(struct rio_info *p)
84{
85 int Host, Host1, Host2, NameIsUnique, Entry, SubEnt;
86 struct Map *MapP;
87 struct Map *HostMapP;
88 struct Host *HostP;
89
90 char *cptr;
91
92 /*
93 ** We have been sent a new table to install. We need to break
94 ** it down into little bits and spread it around a bit to see
95 ** what we have got.
96 */
97 /*
98 ** Things to check:
99 ** (things marked 'xx' aren't checked any more!)
100 ** (1) That there are no booted Hosts/RTAs out there.
101 ** (2) That the names are properly formed
102 ** (3) That blank entries really are.
103 ** xx (4) That hosts mentioned in the table actually exist. xx
104 ** (5) That the IDs are unique (per host).
105 ** (6) That host IDs are zero
106 ** (7) That port numbers are valid
107 ** (8) That port numbers aren't duplicated
108 ** (9) That names aren't duplicated
109 ** xx (10) That hosts that actually exist are mentioned in the table. xx
110 */
111 rio_dprintk(RIO_DEBUG_TABLE, "RIONewTable: entering(1)\n");
112 if (p->RIOSystemUp) { /* (1) */
113 p->RIOError.Error = HOST_HAS_ALREADY_BEEN_BOOTED;
114 return -EBUSY;
115 }
116
117 p->RIOError.Error = NOTHING_WRONG_AT_ALL;
118 p->RIOError.Entry = -1;
119 p->RIOError.Other = -1;
120
121 for (Entry = 0; Entry < TOTAL_MAP_ENTRIES; Entry++) {
122 MapP = &p->RIOConnectTable[Entry];
123 if ((MapP->Flags & RTA16_SECOND_SLOT) == 0) {
124 rio_dprintk(RIO_DEBUG_TABLE, "RIONewTable: entering(2)\n");
125 cptr = MapP->Name; /* (2) */
126 cptr[MAX_NAME_LEN - 1] = '\0';
127 if (cptr[0] == '\0') {
128 memcpy(MapP->Name, MapP->RtaUniqueNum ? "RTA NN" : "HOST NN", 8);
129 MapP->Name[5] = '0' + Entry / 10;
130 MapP->Name[6] = '0' + Entry % 10;
131 }
132 while (*cptr) {
133 if (*cptr < ' ' || *cptr > '~') {
134 p->RIOError.Error = BAD_CHARACTER_IN_NAME;
135 p->RIOError.Entry = Entry;
136 return -ENXIO;
137 }
138 cptr++;
139 }
140 }
141
142 /*
143 ** If the entry saved was a tentative entry then just forget
144 ** about it.
145 */
146 if (MapP->Flags & SLOT_TENTATIVE) {
147 MapP->HostUniqueNum = 0;
148 MapP->RtaUniqueNum = 0;
149 continue;
150 }
151
152 rio_dprintk(RIO_DEBUG_TABLE, "RIONewTable: entering(3)\n");
153 if (!MapP->RtaUniqueNum && !MapP->HostUniqueNum) { /* (3) */
154 if (MapP->ID || MapP->SysPort || MapP->Flags) {
155 rio_dprintk(RIO_DEBUG_TABLE, "%s pretending to be empty but isn't\n", MapP->Name);
156 p->RIOError.Error = TABLE_ENTRY_ISNT_PROPERLY_NULL;
157 p->RIOError.Entry = Entry;
158 return -ENXIO;
159 }
160 rio_dprintk(RIO_DEBUG_TABLE, "!RIO: Daemon: test (3) passes\n");
161 continue;
162 }
163
164 rio_dprintk(RIO_DEBUG_TABLE, "RIONewTable: entering(4)\n");
165 for (Host = 0; Host < p->RIONumHosts; Host++) { /* (4) */
166 if (p->RIOHosts[Host].UniqueNum == MapP->HostUniqueNum) {
167 HostP = &p->RIOHosts[Host];
168 /*
169 ** having done the lookup, we don't really want to do
170 ** it again, so hang the host number in a safe place
171 */
172 MapP->Topology[0].Unit = Host;
173 break;
174 }
175 }
176
177 if (Host >= p->RIONumHosts) {
178 rio_dprintk(RIO_DEBUG_TABLE, "RTA %s has unknown host unique number 0x%x\n", MapP->Name, MapP->HostUniqueNum);
179 MapP->HostUniqueNum = 0;
180 /* MapP->RtaUniqueNum = 0; */
181 /* MapP->ID = 0; */
182 /* MapP->Flags = 0; */
183 /* MapP->SysPort = 0; */
184 /* MapP->Name[0] = 0; */
185 continue;
186 }
187
188 rio_dprintk(RIO_DEBUG_TABLE, "RIONewTable: entering(5)\n");
189 if (MapP->RtaUniqueNum) { /* (5) */
190 if (!MapP->ID) {
191 rio_dprintk(RIO_DEBUG_TABLE, "RIO: RTA %s has been allocated an ID of zero!\n", MapP->Name);
192 p->RIOError.Error = ZERO_RTA_ID;
193 p->RIOError.Entry = Entry;
194 return -ENXIO;
195 }
196 if (MapP->ID > MAX_RUP) {
197 rio_dprintk(RIO_DEBUG_TABLE, "RIO: RTA %s has been allocated an invalid ID %d\n", MapP->Name, MapP->ID);
198 p->RIOError.Error = ID_NUMBER_OUT_OF_RANGE;
199 p->RIOError.Entry = Entry;
200 return -ENXIO;
201 }
202 for (SubEnt = 0; SubEnt < Entry; SubEnt++) {
203 if (MapP->HostUniqueNum == p->RIOConnectTable[SubEnt].HostUniqueNum && MapP->ID == p->RIOConnectTable[SubEnt].ID) {
204 rio_dprintk(RIO_DEBUG_TABLE, "Dupl. ID number allocated to RTA %s and RTA %s\n", MapP->Name, p->RIOConnectTable[SubEnt].Name);
205 p->RIOError.Error = DUPLICATED_RTA_ID;
206 p->RIOError.Entry = Entry;
207 p->RIOError.Other = SubEnt;
208 return -ENXIO;
209 }
210 /*
211 ** If the RtaUniqueNum is the same, it may be looking at both
212 ** entries for a 16 port RTA, so check the ids
213 */
214 if ((MapP->RtaUniqueNum == p->RIOConnectTable[SubEnt].RtaUniqueNum)
215 && (MapP->ID2 != p->RIOConnectTable[SubEnt].ID)) {
216 rio_dprintk(RIO_DEBUG_TABLE, "RTA %s has duplicate unique number\n", MapP->Name);
217 rio_dprintk(RIO_DEBUG_TABLE, "RTA %s has duplicate unique number\n", p->RIOConnectTable[SubEnt].Name);
218 p->RIOError.Error = DUPLICATE_UNIQUE_NUMBER;
219 p->RIOError.Entry = Entry;
220 p->RIOError.Other = SubEnt;
221 return -ENXIO;
222 }
223 }
224 rio_dprintk(RIO_DEBUG_TABLE, "RIONewTable: entering(7a)\n");
225 /* (7a) */
226 if ((MapP->SysPort != NO_PORT) && (MapP->SysPort % PORTS_PER_RTA)) {
227 rio_dprintk(RIO_DEBUG_TABLE, "TTY Port number %d-RTA %s is not a multiple of %d!\n", (int) MapP->SysPort, MapP->Name, PORTS_PER_RTA);
228 p->RIOError.Error = TTY_NUMBER_OUT_OF_RANGE;
229 p->RIOError.Entry = Entry;
230 return -ENXIO;
231 }
232 rio_dprintk(RIO_DEBUG_TABLE, "RIONewTable: entering(7b)\n");
233 /* (7b) */
234 if ((MapP->SysPort != NO_PORT) && (MapP->SysPort >= RIO_PORTS)) {
235 rio_dprintk(RIO_DEBUG_TABLE, "TTY Port number %d for RTA %s is too big\n", (int) MapP->SysPort, MapP->Name);
236 p->RIOError.Error = TTY_NUMBER_OUT_OF_RANGE;
237 p->RIOError.Entry = Entry;
238 return -ENXIO;
239 }
240 for (SubEnt = 0; SubEnt < Entry; SubEnt++) {
241 if (p->RIOConnectTable[SubEnt].Flags & RTA16_SECOND_SLOT)
242 continue;
243 if (p->RIOConnectTable[SubEnt].RtaUniqueNum) {
244 rio_dprintk(RIO_DEBUG_TABLE, "RIONewTable: entering(8)\n");
245 /* (8) */
246 if ((MapP->SysPort != NO_PORT) && (MapP->SysPort == p->RIOConnectTable[SubEnt].SysPort)) {
247 rio_dprintk(RIO_DEBUG_TABLE, "RTA %s:same TTY port # as RTA %s (%d)\n", MapP->Name, p->RIOConnectTable[SubEnt].Name, (int) MapP->SysPort);
248 p->RIOError.Error = TTY_NUMBER_IN_USE;
249 p->RIOError.Entry = Entry;
250 p->RIOError.Other = SubEnt;
251 return -ENXIO;
252 }
253 rio_dprintk(RIO_DEBUG_TABLE, "RIONewTable: entering(9)\n");
254 if (strcmp(MapP->Name, p->RIOConnectTable[SubEnt].Name) == 0 && !(MapP->Flags & RTA16_SECOND_SLOT)) { /* (9) */
255 rio_dprintk(RIO_DEBUG_TABLE, "RTA name %s used twice\n", MapP->Name);
256 p->RIOError.Error = NAME_USED_TWICE;
257 p->RIOError.Entry = Entry;
258 p->RIOError.Other = SubEnt;
259 return -ENXIO;
260 }
261 }
262 }
263 } else { /* (6) */
264 rio_dprintk(RIO_DEBUG_TABLE, "RIONewTable: entering(6)\n");
265 if (MapP->ID) {
266 rio_dprintk(RIO_DEBUG_TABLE, "RIO:HOST %s has been allocated ID that isn't zero!\n", MapP->Name);
267 p->RIOError.Error = HOST_ID_NOT_ZERO;
268 p->RIOError.Entry = Entry;
269 return -ENXIO;
270 }
271 if (MapP->SysPort != NO_PORT) {
272 rio_dprintk(RIO_DEBUG_TABLE, "RIO: HOST %s has been allocated port numbers!\n", MapP->Name);
273 p->RIOError.Error = HOST_SYSPORT_BAD;
274 p->RIOError.Entry = Entry;
275 return -ENXIO;
276 }
277 }
278 }
279
280 /*
281 ** wow! if we get here then it's a goody!
282 */
283
284 /*
285 ** Zero the (old) entries for each host...
286 */
287 for (Host = 0; Host < RIO_HOSTS; Host++) {
288 for (Entry = 0; Entry < MAX_RUP; Entry++) {
289 memset(&p->RIOHosts[Host].Mapping[Entry], 0, sizeof(struct Map));
290 }
291 memset(&p->RIOHosts[Host].Name[0], 0, sizeof(p->RIOHosts[Host].Name));
292 }
293
294 /*
295 ** Copy in the new table entries
296 */
297 for (Entry = 0; Entry < TOTAL_MAP_ENTRIES; Entry++) {
298 rio_dprintk(RIO_DEBUG_TABLE, "RIONewTable: Copy table for Host entry %d\n", Entry);
299 MapP = &p->RIOConnectTable[Entry];
300
301 /*
302 ** Now, if it is an empty slot ignore it!
303 */
304 if (MapP->HostUniqueNum == 0)
305 continue;
306
307 /*
308 ** we saved the host number earlier, so grab it back
309 */
310 HostP = &p->RIOHosts[MapP->Topology[0].Unit];
311
312 /*
313 ** If it is a host, then we only need to fill in the name field.
314 */
315 if (MapP->ID == 0) {
316 rio_dprintk(RIO_DEBUG_TABLE, "Host entry found. Name %s\n", MapP->Name);
317 memcpy(HostP->Name, MapP->Name, MAX_NAME_LEN);
318 continue;
319 }
320
321 /*
322 ** Its an RTA entry, so fill in the host mapping entries for it
323 ** and the port mapping entries. Notice that entry zero is for
324 ** ID one.
325 */
326 HostMapP = &HostP->Mapping[MapP->ID - 1];
327
328 if (MapP->Flags & SLOT_IN_USE) {
329 rio_dprintk(RIO_DEBUG_TABLE, "Rta entry found. Name %s\n", MapP->Name);
330 /*
331 ** structure assign, then sort out the bits we shouldn't have done
332 */
333 *HostMapP = *MapP;
334
335 HostMapP->Flags = SLOT_IN_USE;
336 if (MapP->Flags & RTA16_SECOND_SLOT)
337 HostMapP->Flags |= RTA16_SECOND_SLOT;
338
339 RIOReMapPorts(p, HostP, HostMapP);
340 } else {
341 rio_dprintk(RIO_DEBUG_TABLE, "TENTATIVE Rta entry found. Name %s\n", MapP->Name);
342 }
343 }
344
345 for (Entry = 0; Entry < TOTAL_MAP_ENTRIES; Entry++) {
346 p->RIOSavedTable[Entry] = p->RIOConnectTable[Entry];
347 }
348
349 for (Host = 0; Host < p->RIONumHosts; Host++) {
350 for (SubEnt = 0; SubEnt < LINKS_PER_UNIT; SubEnt++) {
351 p->RIOHosts[Host].Topology[SubEnt].Unit = ROUTE_DISCONNECT;
352 p->RIOHosts[Host].Topology[SubEnt].Link = NO_LINK;
353 }
354 for (Entry = 0; Entry < MAX_RUP; Entry++) {
355 for (SubEnt = 0; SubEnt < LINKS_PER_UNIT; SubEnt++) {
356 p->RIOHosts[Host].Mapping[Entry].Topology[SubEnt].Unit = ROUTE_DISCONNECT;
357 p->RIOHosts[Host].Mapping[Entry].Topology[SubEnt].Link = NO_LINK;
358 }
359 }
360 if (!p->RIOHosts[Host].Name[0]) {
361 memcpy(p->RIOHosts[Host].Name, "HOST 1", 7);
362 p->RIOHosts[Host].Name[5] += Host;
363 }
364 /*
365 ** Check that default name assigned is unique.
366 */
367 Host1 = Host;
368 NameIsUnique = 0;
369 while (!NameIsUnique) {
370 NameIsUnique = 1;
371 for (Host2 = 0; Host2 < p->RIONumHosts; Host2++) {
372 if (Host2 == Host)
373 continue;
374 if (strcmp(p->RIOHosts[Host].Name, p->RIOHosts[Host2].Name)
375 == 0) {
376 NameIsUnique = 0;
377 Host1++;
378 if (Host1 >= p->RIONumHosts)
379 Host1 = 0;
380 p->RIOHosts[Host].Name[5] = '1' + Host1;
381 }
382 }
383 }
384 /*
385 ** Rename host if name already used.
386 */
387 if (Host1 != Host) {
388 rio_dprintk(RIO_DEBUG_TABLE, "Default name %s already used\n", p->RIOHosts[Host].Name);
389 memcpy(p->RIOHosts[Host].Name, "HOST 1", 7);
390 p->RIOHosts[Host].Name[5] += Host1;
391 }
392 rio_dprintk(RIO_DEBUG_TABLE, "Assigning default name %s\n", p->RIOHosts[Host].Name);
393 }
394 return 0;
395}
396
397/*
398** User process needs the config table - build it from first
399** principles.
400**
401* FIXME: SMP locking
402*/
403int RIOApel(struct rio_info *p)
404{
405 int Host;
406 int link;
407 int Rup;
408 int Next = 0;
409 struct Map *MapP;
410 struct Host *HostP;
411 unsigned long flags;
412
413 rio_dprintk(RIO_DEBUG_TABLE, "Generating a table to return to config.rio\n");
414
415 memset(&p->RIOConnectTable[0], 0, sizeof(struct Map) * TOTAL_MAP_ENTRIES);
416
417 for (Host = 0; Host < RIO_HOSTS; Host++) {
418 rio_dprintk(RIO_DEBUG_TABLE, "Processing host %d\n", Host);
419 HostP = &p->RIOHosts[Host];
420 rio_spin_lock_irqsave(&HostP->HostLock, flags);
421
422 MapP = &p->RIOConnectTable[Next++];
423 MapP->HostUniqueNum = HostP->UniqueNum;
424 if ((HostP->Flags & RUN_STATE) != RC_RUNNING) {
425 rio_spin_unlock_irqrestore(&HostP->HostLock, flags);
426 continue;
427 }
428 MapP->RtaUniqueNum = 0;
429 MapP->ID = 0;
430 MapP->Flags = SLOT_IN_USE;
431 MapP->SysPort = NO_PORT;
432 for (link = 0; link < LINKS_PER_UNIT; link++)
433 MapP->Topology[link] = HostP->Topology[link];
434 memcpy(MapP->Name, HostP->Name, MAX_NAME_LEN);
435 for (Rup = 0; Rup < MAX_RUP; Rup++) {
436 if (HostP->Mapping[Rup].Flags & (SLOT_IN_USE | SLOT_TENTATIVE)) {
437 p->RIOConnectTable[Next] = HostP->Mapping[Rup];
438 if (HostP->Mapping[Rup].Flags & SLOT_IN_USE)
439 p->RIOConnectTable[Next].Flags |= SLOT_IN_USE;
440 if (HostP->Mapping[Rup].Flags & SLOT_TENTATIVE)
441 p->RIOConnectTable[Next].Flags |= SLOT_TENTATIVE;
442 if (HostP->Mapping[Rup].Flags & RTA16_SECOND_SLOT)
443 p->RIOConnectTable[Next].Flags |= RTA16_SECOND_SLOT;
444 Next++;
445 }
446 }
447 rio_spin_unlock_irqrestore(&HostP->HostLock, flags);
448 }
449 return 0;
450}
451
452/*
453** config.rio has taken a dislike to one of the gross maps entries.
454** if the entry is suitably inactive, then we can gob on it and remove
455** it from the table.
456*/
457int RIODeleteRta(struct rio_info *p, struct Map *MapP)
458{
459 int host, entry, port, link;
460 int SysPort;
461 struct Host *HostP;
462 struct Map *HostMapP;
463 struct Port *PortP;
464 int work_done = 0;
465 unsigned long lock_flags, sem_flags;
466
467 rio_dprintk(RIO_DEBUG_TABLE, "Delete entry on host %x, rta %x\n", MapP->HostUniqueNum, MapP->RtaUniqueNum);
468
469 for (host = 0; host < p->RIONumHosts; host++) {
470 HostP = &p->RIOHosts[host];
471
472 rio_spin_lock_irqsave(&HostP->HostLock, lock_flags);
473
474 if ((HostP->Flags & RUN_STATE) != RC_RUNNING) {
475 rio_spin_unlock_irqrestore(&HostP->HostLock, lock_flags);
476 continue;
477 }
478
479 for (entry = 0; entry < MAX_RUP; entry++) {
480 if (MapP->RtaUniqueNum == HostP->Mapping[entry].RtaUniqueNum) {
481 HostMapP = &HostP->Mapping[entry];
482 rio_dprintk(RIO_DEBUG_TABLE, "Found entry offset %d on host %s\n", entry, HostP->Name);
483
484 /*
485 ** Check all four links of the unit are disconnected
486 */
487 for (link = 0; link < LINKS_PER_UNIT; link++) {
488 if (HostMapP->Topology[link].Unit != ROUTE_DISCONNECT) {
489 rio_dprintk(RIO_DEBUG_TABLE, "Entry is in use and cannot be deleted!\n");
490 p->RIOError.Error = UNIT_IS_IN_USE;
491 rio_spin_unlock_irqrestore(&HostP->HostLock, lock_flags);
492 return -EBUSY;
493 }
494 }
495 /*
496 ** Slot has been allocated, BUT not booted/routed/
497 ** connected/selected or anything else-ed
498 */
499 SysPort = HostMapP->SysPort;
500
501 if (SysPort != NO_PORT) {
502 for (port = SysPort; port < SysPort + PORTS_PER_RTA; port++) {
503 PortP = p->RIOPortp[port];
504 rio_dprintk(RIO_DEBUG_TABLE, "Unmap port\n");
505
506 rio_spin_lock_irqsave(&PortP->portSem, sem_flags);
507
508 PortP->Mapped = 0;
509
510 if (PortP->State & (RIO_MOPEN | RIO_LOPEN)) {
511
512 rio_dprintk(RIO_DEBUG_TABLE, "Gob on port\n");
513 PortP->TxBufferIn = PortP->TxBufferOut = 0;
514 /* What should I do
515 wakeup( &PortP->TxBufferIn );
516 wakeup( &PortP->TxBufferOut);
517 */
518 PortP->InUse = NOT_INUSE;
519 /* What should I do
520 wakeup( &PortP->InUse );
521 signal(PortP->TtyP->t_pgrp,SIGKILL);
522 ttyflush(PortP->TtyP,(FREAD|FWRITE));
523 */
524 PortP->State |= RIO_CLOSING | RIO_DELETED;
525 }
526
527 /*
528 ** For the second slot of a 16 port RTA, the
529 ** driver needs to reset the changes made to
530 ** the phb to port mappings in RIORouteRup.
531 */
532 if (PortP->SecondBlock) {
533 u16 dest_unit = HostMapP->ID;
534 u16 dest_port = port - SysPort;
535 u16 __iomem *TxPktP;
536 struct PKT __iomem *Pkt;
537
538 for (TxPktP = PortP->TxStart; TxPktP <= PortP->TxEnd; TxPktP++) {
539 /*
540 ** *TxPktP is the pointer to the
541 ** transmit packet on the host card.
542 ** This needs to be translated into
543 ** a 32 bit pointer so it can be
544 ** accessed from the driver.
545 */
546 Pkt = (struct PKT __iomem *) RIO_PTR(HostP->Caddr, readw(&*TxPktP));
547 rio_dprintk(RIO_DEBUG_TABLE, "Tx packet (%x) destination: Old %x:%x New %x:%x\n", readw(TxPktP), readb(&Pkt->dest_unit), readb(&Pkt->dest_port), dest_unit, dest_port);
548 writew(dest_unit, &Pkt->dest_unit);
549 writew(dest_port, &Pkt->dest_port);
550 }
551 rio_dprintk(RIO_DEBUG_TABLE, "Port %d phb destination: Old %x:%x New %x:%x\n", port, readb(&PortP->PhbP->destination) & 0xff, (readb(&PortP->PhbP->destination) >> 8) & 0xff, dest_unit, dest_port);
552 writew(dest_unit + (dest_port << 8), &PortP->PhbP->destination);
553 }
554 rio_spin_unlock_irqrestore(&PortP->portSem, sem_flags);
555 }
556 }
557 rio_dprintk(RIO_DEBUG_TABLE, "Entry nulled.\n");
558 memset(HostMapP, 0, sizeof(struct Map));
559 work_done++;
560 }
561 }
562 rio_spin_unlock_irqrestore(&HostP->HostLock, lock_flags);
563 }
564
565 /* XXXXX lock me up */
566 for (entry = 0; entry < TOTAL_MAP_ENTRIES; entry++) {
567 if (p->RIOSavedTable[entry].RtaUniqueNum == MapP->RtaUniqueNum) {
568 memset(&p->RIOSavedTable[entry], 0, sizeof(struct Map));
569 work_done++;
570 }
571 if (p->RIOConnectTable[entry].RtaUniqueNum == MapP->RtaUniqueNum) {
572 memset(&p->RIOConnectTable[entry], 0, sizeof(struct Map));
573 work_done++;
574 }
575 }
576 if (work_done)
577 return 0;
578
579 rio_dprintk(RIO_DEBUG_TABLE, "Couldn't find entry to be deleted\n");
580 p->RIOError.Error = COULDNT_FIND_ENTRY;
581 return -ENXIO;
582}
583
584int RIOAssignRta(struct rio_info *p, struct Map *MapP)
585{
586 int host;
587 struct Map *HostMapP;
588 char *sptr;
589 int link;
590
591
592 rio_dprintk(RIO_DEBUG_TABLE, "Assign entry on host %x, rta %x, ID %d, Sysport %d\n", MapP->HostUniqueNum, MapP->RtaUniqueNum, MapP->ID, (int) MapP->SysPort);
593
594 if ((MapP->ID != (u16) - 1) && ((int) MapP->ID < (int) 1 || (int) MapP->ID > MAX_RUP)) {
595 rio_dprintk(RIO_DEBUG_TABLE, "Bad ID in map entry!\n");
596 p->RIOError.Error = ID_NUMBER_OUT_OF_RANGE;
597 return -EINVAL;
598 }
599 if (MapP->RtaUniqueNum == 0) {
600 rio_dprintk(RIO_DEBUG_TABLE, "Rta Unique number zero!\n");
601 p->RIOError.Error = RTA_UNIQUE_NUMBER_ZERO;
602 return -EINVAL;
603 }
604 if ((MapP->SysPort != NO_PORT) && (MapP->SysPort % PORTS_PER_RTA)) {
605 rio_dprintk(RIO_DEBUG_TABLE, "Port %d not multiple of %d!\n", (int) MapP->SysPort, PORTS_PER_RTA);
606 p->RIOError.Error = TTY_NUMBER_OUT_OF_RANGE;
607 return -EINVAL;
608 }
609 if ((MapP->SysPort != NO_PORT) && (MapP->SysPort >= RIO_PORTS)) {
610 rio_dprintk(RIO_DEBUG_TABLE, "Port %d not valid!\n", (int) MapP->SysPort);
611 p->RIOError.Error = TTY_NUMBER_OUT_OF_RANGE;
612 return -EINVAL;
613 }
614
615 /*
616 ** Copy the name across to the map entry.
617 */
618 MapP->Name[MAX_NAME_LEN - 1] = '\0';
619 sptr = MapP->Name;
620 while (*sptr) {
621 if (*sptr < ' ' || *sptr > '~') {
622 rio_dprintk(RIO_DEBUG_TABLE, "Name entry contains non-printing characters!\n");
623 p->RIOError.Error = BAD_CHARACTER_IN_NAME;
624 return -EINVAL;
625 }
626 sptr++;
627 }
628
629 for (host = 0; host < p->RIONumHosts; host++) {
630 if (MapP->HostUniqueNum == p->RIOHosts[host].UniqueNum) {
631 if ((p->RIOHosts[host].Flags & RUN_STATE) != RC_RUNNING) {
632 p->RIOError.Error = HOST_NOT_RUNNING;
633 return -ENXIO;
634 }
635
636 /*
637 ** Now we have a host we need to allocate an ID
638 ** if the entry does not already have one.
639 */
640 if (MapP->ID == (u16) - 1) {
641 int nNewID;
642
643 rio_dprintk(RIO_DEBUG_TABLE, "Attempting to get a new ID for rta \"%s\"\n", MapP->Name);
644 /*
645 ** The idea here is to allow RTA's to be assigned
646 ** before they actually appear on the network.
647 ** This allows the addition of RTA's without having
648 ** to plug them in.
649 ** What we do is:
650 ** - Find a free ID and allocate it to the RTA.
651 ** - If this map entry is the second half of a
652 ** 16 port entry then find the other half and
653 ** make sure the 2 cross reference each other.
654 */
655 if (RIOFindFreeID(p, &p->RIOHosts[host], &nNewID, NULL) != 0) {
656 p->RIOError.Error = COULDNT_FIND_ENTRY;
657 return -EBUSY;
658 }
659 MapP->ID = (u16) nNewID + 1;
660 rio_dprintk(RIO_DEBUG_TABLE, "Allocated ID %d for this new RTA.\n", MapP->ID);
661 HostMapP = &p->RIOHosts[host].Mapping[nNewID];
662 HostMapP->RtaUniqueNum = MapP->RtaUniqueNum;
663 HostMapP->HostUniqueNum = MapP->HostUniqueNum;
664 HostMapP->ID = MapP->ID;
665 for (link = 0; link < LINKS_PER_UNIT; link++) {
666 HostMapP->Topology[link].Unit = ROUTE_DISCONNECT;
667 HostMapP->Topology[link].Link = NO_LINK;
668 }
669 if (MapP->Flags & RTA16_SECOND_SLOT) {
670 int unit;
671
672 for (unit = 0; unit < MAX_RUP; unit++)
673 if (p->RIOHosts[host].Mapping[unit].RtaUniqueNum == MapP->RtaUniqueNum)
674 break;
675 if (unit == MAX_RUP) {
676 p->RIOError.Error = COULDNT_FIND_ENTRY;
677 return -EBUSY;
678 }
679 HostMapP->Flags |= RTA16_SECOND_SLOT;
680 HostMapP->ID2 = MapP->ID2 = p->RIOHosts[host].Mapping[unit].ID;
681 p->RIOHosts[host].Mapping[unit].ID2 = MapP->ID;
682 rio_dprintk(RIO_DEBUG_TABLE, "Cross referenced id %d to ID %d.\n", MapP->ID, p->RIOHosts[host].Mapping[unit].ID);
683 }
684 }
685
686 HostMapP = &p->RIOHosts[host].Mapping[MapP->ID - 1];
687
688 if (HostMapP->Flags & SLOT_IN_USE) {
689 rio_dprintk(RIO_DEBUG_TABLE, "Map table slot for ID %d is already in use.\n", MapP->ID);
690 p->RIOError.Error = ID_ALREADY_IN_USE;
691 return -EBUSY;
692 }
693
694 /*
695 ** Assign the sys ports and the name, and mark the slot as
696 ** being in use.
697 */
698 HostMapP->SysPort = MapP->SysPort;
699 if ((MapP->Flags & RTA16_SECOND_SLOT) == 0)
700 memcpy(HostMapP->Name, MapP->Name, MAX_NAME_LEN);
701 HostMapP->Flags = SLOT_IN_USE | RTA_BOOTED;
702#ifdef NEED_TO_FIX
703 RIO_SV_BROADCAST(p->RIOHosts[host].svFlags[MapP->ID - 1]);
704#endif
705 if (MapP->Flags & RTA16_SECOND_SLOT)
706 HostMapP->Flags |= RTA16_SECOND_SLOT;
707
708 RIOReMapPorts(p, &p->RIOHosts[host], HostMapP);
709 /*
710 ** Adjust 2nd block of 8 phbs
711 */
712 if (MapP->Flags & RTA16_SECOND_SLOT)
713 RIOFixPhbs(p, &p->RIOHosts[host], HostMapP->ID - 1);
714
715 if (HostMapP->SysPort != NO_PORT) {
716 if (HostMapP->SysPort < p->RIOFirstPortsBooted)
717 p->RIOFirstPortsBooted = HostMapP->SysPort;
718 if (HostMapP->SysPort > p->RIOLastPortsBooted)
719 p->RIOLastPortsBooted = HostMapP->SysPort;
720 }
721 if (MapP->Flags & RTA16_SECOND_SLOT)
722 rio_dprintk(RIO_DEBUG_TABLE, "Second map of RTA %s added to configuration\n", p->RIOHosts[host].Mapping[MapP->ID2 - 1].Name);
723 else
724 rio_dprintk(RIO_DEBUG_TABLE, "RTA %s added to configuration\n", MapP->Name);
725 return 0;
726 }
727 }
728 p->RIOError.Error = UNKNOWN_HOST_NUMBER;
729 rio_dprintk(RIO_DEBUG_TABLE, "Unknown host %x\n", MapP->HostUniqueNum);
730 return -ENXIO;
731}
732
733
734int RIOReMapPorts(struct rio_info *p, struct Host *HostP, struct Map *HostMapP)
735{
736 struct Port *PortP;
737 unsigned int SubEnt;
738 unsigned int HostPort;
739 unsigned int SysPort;
740 u16 RtaType;
741 unsigned long flags;
742
743 rio_dprintk(RIO_DEBUG_TABLE, "Mapping sysport %d to id %d\n", (int) HostMapP->SysPort, HostMapP->ID);
744
745 /*
746 ** We need to tell the UnixRups which sysport the rup corresponds to
747 */
748 HostP->UnixRups[HostMapP->ID - 1].BaseSysPort = HostMapP->SysPort;
749
750 if (HostMapP->SysPort == NO_PORT)
751 return (0);
752
753 RtaType = GetUnitType(HostMapP->RtaUniqueNum);
754 rio_dprintk(RIO_DEBUG_TABLE, "Mapping sysport %d-%d\n", (int) HostMapP->SysPort, (int) HostMapP->SysPort + PORTS_PER_RTA - 1);
755
756 /*
757 ** now map each of its eight ports
758 */
759 for (SubEnt = 0; SubEnt < PORTS_PER_RTA; SubEnt++) {
760 rio_dprintk(RIO_DEBUG_TABLE, "subent = %d, HostMapP->SysPort = %d\n", SubEnt, (int) HostMapP->SysPort);
761 SysPort = HostMapP->SysPort + SubEnt; /* portnumber within system */
762 /* portnumber on host */
763
764 HostPort = (HostMapP->ID - 1) * PORTS_PER_RTA + SubEnt;
765
766 rio_dprintk(RIO_DEBUG_TABLE, "c1 p = %p, p->rioPortp = %p\n", p, p->RIOPortp);
767 PortP = p->RIOPortp[SysPort];
768 rio_dprintk(RIO_DEBUG_TABLE, "Map port\n");
769
770 /*
771 ** Point at all the real neat data structures
772 */
773 rio_spin_lock_irqsave(&PortP->portSem, flags);
774 PortP->HostP = HostP;
775 PortP->Caddr = HostP->Caddr;
776
777 /*
778 ** The PhbP cannot be filled in yet
779 ** unless the host has been booted
780 */
781 if ((HostP->Flags & RUN_STATE) == RC_RUNNING) {
782 struct PHB __iomem *PhbP = PortP->PhbP = &HostP->PhbP[HostPort];
783 PortP->TxAdd = (u16 __iomem *) RIO_PTR(HostP->Caddr, readw(&PhbP->tx_add));
784 PortP->TxStart = (u16 __iomem *) RIO_PTR(HostP->Caddr, readw(&PhbP->tx_start));
785 PortP->TxEnd = (u16 __iomem *) RIO_PTR(HostP->Caddr, readw(&PhbP->tx_end));
786 PortP->RxRemove = (u16 __iomem *) RIO_PTR(HostP->Caddr, readw(&PhbP->rx_remove));
787 PortP->RxStart = (u16 __iomem *) RIO_PTR(HostP->Caddr, readw(&PhbP->rx_start));
788 PortP->RxEnd = (u16 __iomem *) RIO_PTR(HostP->Caddr, readw(&PhbP->rx_end));
789 } else
790 PortP->PhbP = NULL;
791
792 /*
793 ** port related flags
794 */
795 PortP->HostPort = HostPort;
796 /*
797 ** For each part of a 16 port RTA, RupNum is ID - 1.
798 */
799 PortP->RupNum = HostMapP->ID - 1;
800 if (HostMapP->Flags & RTA16_SECOND_SLOT) {
801 PortP->ID2 = HostMapP->ID2 - 1;
802 PortP->SecondBlock = 1;
803 } else {
804 PortP->ID2 = 0;
805 PortP->SecondBlock = 0;
806 }
807 PortP->RtaUniqueNum = HostMapP->RtaUniqueNum;
808
809 /*
810 ** If the port was already mapped then thats all we need to do.
811 */
812 if (PortP->Mapped) {
813 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
814 continue;
815 } else
816 HostMapP->Flags &= ~RTA_NEWBOOT;
817
818 PortP->State = 0;
819 PortP->Config = 0;
820 /*
821 ** Check out the module type - if it is special (read only etc.)
822 ** then we need to set flags in the PortP->Config.
823 ** Note: For 16 port RTA, all ports are of the same type.
824 */
825 if (RtaType == TYPE_RTA16) {
826 PortP->Config |= p->RIOModuleTypes[HostP->UnixRups[HostMapP->ID - 1].ModTypes].Flags[SubEnt % PORTS_PER_MODULE];
827 } else {
828 if (SubEnt < PORTS_PER_MODULE)
829 PortP->Config |= p->RIOModuleTypes[LONYBLE(HostP->UnixRups[HostMapP->ID - 1].ModTypes)].Flags[SubEnt % PORTS_PER_MODULE];
830 else
831 PortP->Config |= p->RIOModuleTypes[HINYBLE(HostP->UnixRups[HostMapP->ID - 1].ModTypes)].Flags[SubEnt % PORTS_PER_MODULE];
832 }
833
834 /*
835 ** more port related flags
836 */
837 PortP->PortState = 0;
838 PortP->ModemLines = 0;
839 PortP->ModemState = 0;
840 PortP->CookMode = COOK_WELL;
841 PortP->ParamSem = 0;
842 PortP->FlushCmdBodge = 0;
843 PortP->WflushFlag = 0;
844 PortP->MagicFlags = 0;
845 PortP->Lock = 0;
846 PortP->Store = 0;
847 PortP->FirstOpen = 1;
848
849 /*
850 ** Buffers 'n things
851 */
852 PortP->RxDataStart = 0;
853 PortP->Cor2Copy = 0;
854 PortP->Name = &HostMapP->Name[0];
855 PortP->statsGather = 0;
856 PortP->txchars = 0;
857 PortP->rxchars = 0;
858 PortP->opens = 0;
859 PortP->closes = 0;
860 PortP->ioctls = 0;
861 if (PortP->TxRingBuffer)
862 memset(PortP->TxRingBuffer, 0, p->RIOBufferSize);
863 else if (p->RIOBufferSize) {
864 PortP->TxRingBuffer = kzalloc(p->RIOBufferSize, GFP_KERNEL);
865 }
866 PortP->TxBufferOut = 0;
867 PortP->TxBufferIn = 0;
868 PortP->Debug = 0;
869 /*
870 ** LastRxTgl stores the state of the rx toggle bit for this
871 ** port, to be compared with the state of the next pkt received.
872 ** If the same, we have received the same rx pkt from the RTA
873 ** twice. Initialise to a value not equal to PHB_RX_TGL or 0.
874 */
875 PortP->LastRxTgl = ~(u8) PHB_RX_TGL;
876
877 /*
878 ** and mark the port as usable
879 */
880 PortP->Mapped = 1;
881 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
882 }
883 if (HostMapP->SysPort < p->RIOFirstPortsMapped)
884 p->RIOFirstPortsMapped = HostMapP->SysPort;
885 if (HostMapP->SysPort > p->RIOLastPortsMapped)
886 p->RIOLastPortsMapped = HostMapP->SysPort;
887
888 return 0;
889}
890
891int RIOChangeName(struct rio_info *p, struct Map *MapP)
892{
893 int host;
894 struct Map *HostMapP;
895 char *sptr;
896
897 rio_dprintk(RIO_DEBUG_TABLE, "Change name entry on host %x, rta %x, ID %d, Sysport %d\n", MapP->HostUniqueNum, MapP->RtaUniqueNum, MapP->ID, (int) MapP->SysPort);
898
899 if (MapP->ID > MAX_RUP) {
900 rio_dprintk(RIO_DEBUG_TABLE, "Bad ID in map entry!\n");
901 p->RIOError.Error = ID_NUMBER_OUT_OF_RANGE;
902 return -EINVAL;
903 }
904
905 MapP->Name[MAX_NAME_LEN - 1] = '\0';
906 sptr = MapP->Name;
907
908 while (*sptr) {
909 if (*sptr < ' ' || *sptr > '~') {
910 rio_dprintk(RIO_DEBUG_TABLE, "Name entry contains non-printing characters!\n");
911 p->RIOError.Error = BAD_CHARACTER_IN_NAME;
912 return -EINVAL;
913 }
914 sptr++;
915 }
916
917 for (host = 0; host < p->RIONumHosts; host++) {
918 if (MapP->HostUniqueNum == p->RIOHosts[host].UniqueNum) {
919 if ((p->RIOHosts[host].Flags & RUN_STATE) != RC_RUNNING) {
920 p->RIOError.Error = HOST_NOT_RUNNING;
921 return -ENXIO;
922 }
923 if (MapP->ID == 0) {
924 memcpy(p->RIOHosts[host].Name, MapP->Name, MAX_NAME_LEN);
925 return 0;
926 }
927
928 HostMapP = &p->RIOHosts[host].Mapping[MapP->ID - 1];
929
930 if (HostMapP->RtaUniqueNum != MapP->RtaUniqueNum) {
931 p->RIOError.Error = RTA_NUMBER_WRONG;
932 return -ENXIO;
933 }
934 memcpy(HostMapP->Name, MapP->Name, MAX_NAME_LEN);
935 return 0;
936 }
937 }
938 p->RIOError.Error = UNKNOWN_HOST_NUMBER;
939 rio_dprintk(RIO_DEBUG_TABLE, "Unknown host %x\n", MapP->HostUniqueNum);
940 return -ENXIO;
941}
diff --git a/drivers/char/rio/riotty.c b/drivers/char/rio/riotty.c
deleted file mode 100644
index 8a90393faf3c..000000000000
--- a/drivers/char/rio/riotty.c
+++ /dev/null
@@ -1,654 +0,0 @@
1/*
2** -----------------------------------------------------------------------------
3**
4** Perle Specialix driver for Linux
5** Ported from existing RIO Driver for SCO sources.
6 *
7 * (C) 1990 - 2000 Specialix International Ltd., Byfleet, Surrey, UK.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22**
23** Module : riotty.c
24** SID : 1.3
25** Last Modified : 11/6/98 10:33:47
26** Retrieved : 11/6/98 10:33:50
27**
28** ident @(#)riotty.c 1.3
29**
30** -----------------------------------------------------------------------------
31*/
32
33#define __EXPLICIT_DEF_H__
34
35#include <linux/module.h>
36#include <linux/sched.h>
37#include <linux/errno.h>
38#include <linux/tty.h>
39#include <linux/string.h>
40#include <asm/io.h>
41#include <asm/system.h>
42#include <asm/string.h>
43#include <asm/uaccess.h>
44
45#include <linux/termios.h>
46
47#include <linux/serial.h>
48
49#include <linux/generic_serial.h>
50
51
52#include "linux_compat.h"
53#include "rio_linux.h"
54#include "pkt.h"
55#include "daemon.h"
56#include "rio.h"
57#include "riospace.h"
58#include "cmdpkt.h"
59#include "map.h"
60#include "rup.h"
61#include "port.h"
62#include "riodrvr.h"
63#include "rioinfo.h"
64#include "func.h"
65#include "errors.h"
66#include "pci.h"
67
68#include "parmmap.h"
69#include "unixrup.h"
70#include "board.h"
71#include "host.h"
72#include "phb.h"
73#include "link.h"
74#include "cmdblk.h"
75#include "route.h"
76#include "cirrus.h"
77#include "rioioctl.h"
78#include "param.h"
79
80static void RIOClearUp(struct Port *PortP);
81
82/* Below belongs in func.h */
83int RIOShortCommand(struct rio_info *p, struct Port *PortP, int command, int len, int arg);
84
85
86extern struct rio_info *p;
87
88
89int riotopen(struct tty_struct *tty, struct file *filp)
90{
91 unsigned int SysPort;
92 int repeat_this = 250;
93 struct Port *PortP; /* pointer to the port structure */
94 unsigned long flags;
95 int retval = 0;
96
97 func_enter();
98
99 /* Make sure driver_data is NULL in case the rio isn't booted jet. Else gs_close
100 is going to oops.
101 */
102 tty->driver_data = NULL;
103
104 SysPort = rio_minor(tty);
105
106 if (p->RIOFailed) {
107 rio_dprintk(RIO_DEBUG_TTY, "System initialisation failed\n");
108 func_exit();
109 return -ENXIO;
110 }
111
112 rio_dprintk(RIO_DEBUG_TTY, "port open SysPort %d (mapped:%d)\n", SysPort, p->RIOPortp[SysPort]->Mapped);
113
114 /*
115 ** Validate that we have received a legitimate request.
116 ** Currently, just check that we are opening a port on
117 ** a host card that actually exists, and that the port
118 ** has been mapped onto a host.
119 */
120 if (SysPort >= RIO_PORTS) { /* out of range ? */
121 rio_dprintk(RIO_DEBUG_TTY, "Illegal port number %d\n", SysPort);
122 func_exit();
123 return -ENXIO;
124 }
125
126 /*
127 ** Grab pointer to the port stucture
128 */
129 PortP = p->RIOPortp[SysPort]; /* Get control struc */
130 rio_dprintk(RIO_DEBUG_TTY, "PortP: %p\n", PortP);
131 if (!PortP->Mapped) { /* we aren't mapped yet! */
132 /*
133 ** The system doesn't know which RTA this port
134 ** corresponds to.
135 */
136 rio_dprintk(RIO_DEBUG_TTY, "port not mapped into system\n");
137 func_exit();
138 return -ENXIO;
139 }
140
141 tty->driver_data = PortP;
142
143 PortP->gs.port.tty = tty;
144 PortP->gs.port.count++;
145
146 rio_dprintk(RIO_DEBUG_TTY, "%d bytes in tx buffer\n", PortP->gs.xmit_cnt);
147
148 retval = gs_init_port(&PortP->gs);
149 if (retval) {
150 PortP->gs.port.count--;
151 return -ENXIO;
152 }
153 /*
154 ** If the host hasn't been booted yet, then
155 ** fail
156 */
157 if ((PortP->HostP->Flags & RUN_STATE) != RC_RUNNING) {
158 rio_dprintk(RIO_DEBUG_TTY, "Host not running\n");
159 func_exit();
160 return -ENXIO;
161 }
162
163 /*
164 ** If the RTA has not booted yet and the user has choosen to block
165 ** until the RTA is present then we must spin here waiting for
166 ** the RTA to boot.
167 */
168 /* I find the above code a bit hairy. I find the below code
169 easier to read and shorter. Now, if it works too that would
170 be great... -- REW
171 */
172 rio_dprintk(RIO_DEBUG_TTY, "Checking if RTA has booted... \n");
173 while (!(PortP->HostP->Mapping[PortP->RupNum].Flags & RTA_BOOTED)) {
174 if (!PortP->WaitUntilBooted) {
175 rio_dprintk(RIO_DEBUG_TTY, "RTA never booted\n");
176 func_exit();
177 return -ENXIO;
178 }
179
180 /* Under Linux you'd normally use a wait instead of this
181 busy-waiting. I'll stick with the old implementation for
182 now. --REW
183 */
184 if (RIODelay(PortP, HUNDRED_MS) == RIO_FAIL) {
185 rio_dprintk(RIO_DEBUG_TTY, "RTA_wait_for_boot: EINTR in delay \n");
186 func_exit();
187 return -EINTR;
188 }
189 if (repeat_this-- <= 0) {
190 rio_dprintk(RIO_DEBUG_TTY, "Waiting for RTA to boot timeout\n");
191 func_exit();
192 return -EIO;
193 }
194 }
195 rio_dprintk(RIO_DEBUG_TTY, "RTA has been booted\n");
196 rio_spin_lock_irqsave(&PortP->portSem, flags);
197 if (p->RIOHalted) {
198 goto bombout;
199 }
200
201 /*
202 ** If the port is in the final throws of being closed,
203 ** we should wait here (politely), waiting
204 ** for it to finish, so that it doesn't close us!
205 */
206 while ((PortP->State & RIO_CLOSING) && !p->RIOHalted) {
207 rio_dprintk(RIO_DEBUG_TTY, "Waiting for RIO_CLOSING to go away\n");
208 if (repeat_this-- <= 0) {
209 rio_dprintk(RIO_DEBUG_TTY, "Waiting for not idle closed broken by signal\n");
210 RIOPreemptiveCmd(p, PortP, RIOC_FCLOSE);
211 retval = -EINTR;
212 goto bombout;
213 }
214 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
215 if (RIODelay(PortP, HUNDRED_MS) == RIO_FAIL) {
216 rio_spin_lock_irqsave(&PortP->portSem, flags);
217 retval = -EINTR;
218 goto bombout;
219 }
220 rio_spin_lock_irqsave(&PortP->portSem, flags);
221 }
222
223 if (!PortP->Mapped) {
224 rio_dprintk(RIO_DEBUG_TTY, "Port unmapped while closing!\n");
225 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
226 retval = -ENXIO;
227 func_exit();
228 return retval;
229 }
230
231 if (p->RIOHalted) {
232 goto bombout;
233 }
234
235/*
236** 15.10.1998 ARG - ESIL 0761 part fix
237** RIO has it's own CTSFLOW and RTSFLOW flags in 'Config' in the port structure,
238** we need to make sure that the flags are clear when the port is opened.
239*/
240 /* Uh? Suppose I turn these on and then another process opens
241 the port again? The flags get cleared! Not good. -- REW */
242 if (!(PortP->State & (RIO_LOPEN | RIO_MOPEN))) {
243 PortP->Config &= ~(RIO_CTSFLOW | RIO_RTSFLOW);
244 }
245
246 if (!(PortP->firstOpen)) { /* First time ? */
247 rio_dprintk(RIO_DEBUG_TTY, "First open for this port\n");
248
249
250 PortP->firstOpen++;
251 PortP->CookMode = 0; /* XXX RIOCookMode(tp); */
252 PortP->InUse = NOT_INUSE;
253
254 /* Tentative fix for bug PR27. Didn't work. */
255 /* PortP->gs.xmit_cnt = 0; */
256
257 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
258
259 /* Someone explain to me why this delay/config is
260 here. If I read the docs correctly the "open"
261 command piggybacks the parameters immediately.
262 -- REW */
263 RIOParam(PortP, RIOC_OPEN, 1, OK_TO_SLEEP); /* Open the port */
264 rio_spin_lock_irqsave(&PortP->portSem, flags);
265
266 /*
267 ** wait for the port to be not closed.
268 */
269 while (!(PortP->PortState & PORT_ISOPEN) && !p->RIOHalted) {
270 rio_dprintk(RIO_DEBUG_TTY, "Waiting for PORT_ISOPEN-currently %x\n", PortP->PortState);
271 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
272 if (RIODelay(PortP, HUNDRED_MS) == RIO_FAIL) {
273 rio_dprintk(RIO_DEBUG_TTY, "Waiting for open to finish broken by signal\n");
274 RIOPreemptiveCmd(p, PortP, RIOC_FCLOSE);
275 func_exit();
276 return -EINTR;
277 }
278 rio_spin_lock_irqsave(&PortP->portSem, flags);
279 }
280
281 if (p->RIOHalted) {
282 retval = -EIO;
283 bombout:
284 /* RIOClearUp( PortP ); */
285 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
286 return retval;
287 }
288 rio_dprintk(RIO_DEBUG_TTY, "PORT_ISOPEN found\n");
289 }
290 rio_dprintk(RIO_DEBUG_TTY, "Modem - test for carrier\n");
291 /*
292 ** ACTION
293 ** insert test for carrier here. -- ???
294 ** I already see that test here. What's the deal? -- REW
295 */
296 if ((PortP->gs.port.tty->termios->c_cflag & CLOCAL) ||
297 (PortP->ModemState & RIOC_MSVR1_CD)) {
298 rio_dprintk(RIO_DEBUG_TTY, "open(%d) Modem carr on\n", SysPort);
299 /*
300 tp->tm.c_state |= CARR_ON;
301 wakeup((caddr_t) &tp->tm.c_canq);
302 */
303 PortP->State |= RIO_CARR_ON;
304 wake_up_interruptible(&PortP->gs.port.open_wait);
305 } else { /* no carrier - wait for DCD */
306 /*
307 while (!(PortP->gs.port.tty->termios->c_state & CARR_ON) &&
308 !(filp->f_flags & O_NONBLOCK) && !p->RIOHalted )
309 */
310 while (!(PortP->State & RIO_CARR_ON) && !(filp->f_flags & O_NONBLOCK) && !p->RIOHalted) {
311 rio_dprintk(RIO_DEBUG_TTY, "open(%d) sleeping for carr on\n", SysPort);
312 /*
313 PortP->gs.port.tty->termios->c_state |= WOPEN;
314 */
315 PortP->State |= RIO_WOPEN;
316 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
317 if (RIODelay(PortP, HUNDRED_MS) == RIO_FAIL) {
318 rio_spin_lock_irqsave(&PortP->portSem, flags);
319 /*
320 ** ACTION: verify that this is a good thing
321 ** to do here. -- ???
322 ** I think it's OK. -- REW
323 */
324 rio_dprintk(RIO_DEBUG_TTY, "open(%d) sleeping for carr broken by signal\n", SysPort);
325 RIOPreemptiveCmd(p, PortP, RIOC_FCLOSE);
326 /*
327 tp->tm.c_state &= ~WOPEN;
328 */
329 PortP->State &= ~RIO_WOPEN;
330 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
331 func_exit();
332 return -EINTR;
333 }
334 rio_spin_lock_irqsave(&PortP->portSem, flags);
335 }
336 PortP->State &= ~RIO_WOPEN;
337 }
338 if (p->RIOHalted)
339 goto bombout;
340 rio_dprintk(RIO_DEBUG_TTY, "Setting RIO_MOPEN\n");
341 PortP->State |= RIO_MOPEN;
342
343 if (p->RIOHalted)
344 goto bombout;
345
346 rio_dprintk(RIO_DEBUG_TTY, "high level open done\n");
347
348 /*
349 ** Count opens for port statistics reporting
350 */
351 if (PortP->statsGather)
352 PortP->opens++;
353
354 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
355 rio_dprintk(RIO_DEBUG_TTY, "Returning from open\n");
356 func_exit();
357 return 0;
358}
359
360/*
361** RIOClose the port.
362** The operating system thinks that this is last close for the device.
363** As there are two interfaces to the port (Modem and tty), we need to
364** check that both are closed before we close the device.
365*/
366int riotclose(void *ptr)
367{
368 struct Port *PortP = ptr; /* pointer to the port structure */
369 int deleted = 0;
370 int try = -1; /* Disable the timeouts by setting them to -1 */
371 int repeat_this = -1; /* Congrats to those having 15 years of
372 uptime! (You get to break the driver.) */
373 unsigned long end_time;
374 struct tty_struct *tty;
375 unsigned long flags;
376 int rv = 0;
377
378 rio_dprintk(RIO_DEBUG_TTY, "port close SysPort %d\n", PortP->PortNum);
379
380 /* PortP = p->RIOPortp[SysPort]; */
381 rio_dprintk(RIO_DEBUG_TTY, "Port is at address %p\n", PortP);
382 /* tp = PortP->TtyP; *//* Get tty */
383 tty = PortP->gs.port.tty;
384 rio_dprintk(RIO_DEBUG_TTY, "TTY is at address %p\n", tty);
385
386 if (PortP->gs.closing_wait)
387 end_time = jiffies + PortP->gs.closing_wait;
388 else
389 end_time = jiffies + MAX_SCHEDULE_TIMEOUT;
390
391 rio_spin_lock_irqsave(&PortP->portSem, flags);
392
393 /*
394 ** Setting this flag will make any process trying to open
395 ** this port block until we are complete closing it.
396 */
397 PortP->State |= RIO_CLOSING;
398
399 if ((PortP->State & RIO_DELETED)) {
400 rio_dprintk(RIO_DEBUG_TTY, "Close on deleted RTA\n");
401 deleted = 1;
402 }
403
404 if (p->RIOHalted) {
405 RIOClearUp(PortP);
406 rv = -EIO;
407 goto close_end;
408 }
409
410 rio_dprintk(RIO_DEBUG_TTY, "Clear bits\n");
411 /*
412 ** clear the open bits for this device
413 */
414 PortP->State &= ~RIO_MOPEN;
415 PortP->State &= ~RIO_CARR_ON;
416 PortP->ModemState &= ~RIOC_MSVR1_CD;
417 /*
418 ** If the device was open as both a Modem and a tty line
419 ** then we need to wimp out here, as the port has not really
420 ** been finally closed (gee, whizz!) The test here uses the
421 ** bit for the OTHER mode of operation, to see if THAT is
422 ** still active!
423 */
424 if ((PortP->State & (RIO_LOPEN | RIO_MOPEN))) {
425 /*
426 ** The port is still open for the other task -
427 ** return, pretending that we are still active.
428 */
429 rio_dprintk(RIO_DEBUG_TTY, "Channel %d still open !\n", PortP->PortNum);
430 PortP->State &= ~RIO_CLOSING;
431 if (PortP->firstOpen)
432 PortP->firstOpen--;
433 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
434 return -EIO;
435 }
436
437 rio_dprintk(RIO_DEBUG_TTY, "Closing down - everything must go!\n");
438
439 PortP->State &= ~RIO_DYNOROD;
440
441 /*
442 ** This is where we wait for the port
443 ** to drain down before closing. Bye-bye....
444 ** (We never meant to do this)
445 */
446 rio_dprintk(RIO_DEBUG_TTY, "Timeout 1 starts\n");
447
448 if (!deleted)
449 while ((PortP->InUse != NOT_INUSE) && !p->RIOHalted && (PortP->TxBufferIn != PortP->TxBufferOut)) {
450 if (repeat_this-- <= 0) {
451 rv = -EINTR;
452 rio_dprintk(RIO_DEBUG_TTY, "Waiting for not idle closed broken by signal\n");
453 RIOPreemptiveCmd(p, PortP, RIOC_FCLOSE);
454 goto close_end;
455 }
456 rio_dprintk(RIO_DEBUG_TTY, "Calling timeout to flush in closing\n");
457 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
458 if (RIODelay_ni(PortP, HUNDRED_MS * 10) == RIO_FAIL) {
459 rio_dprintk(RIO_DEBUG_TTY, "RTA EINTR in delay \n");
460 rv = -EINTR;
461 rio_spin_lock_irqsave(&PortP->portSem, flags);
462 goto close_end;
463 }
464 rio_spin_lock_irqsave(&PortP->portSem, flags);
465 }
466
467 PortP->TxBufferIn = PortP->TxBufferOut = 0;
468 repeat_this = 0xff;
469
470 PortP->InUse = 0;
471 if ((PortP->State & (RIO_LOPEN | RIO_MOPEN))) {
472 /*
473 ** The port has been re-opened for the other task -
474 ** return, pretending that we are still active.
475 */
476 rio_dprintk(RIO_DEBUG_TTY, "Channel %d re-open!\n", PortP->PortNum);
477 PortP->State &= ~RIO_CLOSING;
478 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
479 if (PortP->firstOpen)
480 PortP->firstOpen--;
481 return -EIO;
482 }
483
484 if (p->RIOHalted) {
485 RIOClearUp(PortP);
486 goto close_end;
487 }
488
489 /* Can't call RIOShortCommand with the port locked. */
490 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
491
492 if (RIOShortCommand(p, PortP, RIOC_CLOSE, 1, 0) == RIO_FAIL) {
493 RIOPreemptiveCmd(p, PortP, RIOC_FCLOSE);
494 rio_spin_lock_irqsave(&PortP->portSem, flags);
495 goto close_end;
496 }
497
498 if (!deleted)
499 while (try && (PortP->PortState & PORT_ISOPEN)) {
500 try--;
501 if (time_after(jiffies, end_time)) {
502 rio_dprintk(RIO_DEBUG_TTY, "Run out of tries - force the bugger shut!\n");
503 RIOPreemptiveCmd(p, PortP, RIOC_FCLOSE);
504 break;
505 }
506 rio_dprintk(RIO_DEBUG_TTY, "Close: PortState:ISOPEN is %d\n", PortP->PortState & PORT_ISOPEN);
507
508 if (p->RIOHalted) {
509 RIOClearUp(PortP);
510 rio_spin_lock_irqsave(&PortP->portSem, flags);
511 goto close_end;
512 }
513 if (RIODelay(PortP, HUNDRED_MS) == RIO_FAIL) {
514 rio_dprintk(RIO_DEBUG_TTY, "RTA EINTR in delay \n");
515 RIOPreemptiveCmd(p, PortP, RIOC_FCLOSE);
516 break;
517 }
518 }
519 rio_spin_lock_irqsave(&PortP->portSem, flags);
520 rio_dprintk(RIO_DEBUG_TTY, "Close: try was %d on completion\n", try);
521
522 /* RIOPreemptiveCmd(p, PortP, RIOC_FCLOSE); */
523
524/*
525** 15.10.1998 ARG - ESIL 0761 part fix
526** RIO has it's own CTSFLOW and RTSFLOW flags in 'Config' in the port structure,** we need to make sure that the flags are clear when the port is opened.
527*/
528 PortP->Config &= ~(RIO_CTSFLOW | RIO_RTSFLOW);
529
530 /*
531 ** Count opens for port statistics reporting
532 */
533 if (PortP->statsGather)
534 PortP->closes++;
535
536close_end:
537 /* XXX: Why would a "DELETED" flag be reset here? I'd have
538 thought that a "deleted" flag means that the port was
539 permanently gone, but here we can make it reappear by it
540 being in close during the "deletion".
541 */
542 PortP->State &= ~(RIO_CLOSING | RIO_DELETED);
543 if (PortP->firstOpen)
544 PortP->firstOpen--;
545 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
546 rio_dprintk(RIO_DEBUG_TTY, "Return from close\n");
547 return rv;
548}
549
550
551
552static void RIOClearUp(struct Port *PortP)
553{
554 rio_dprintk(RIO_DEBUG_TTY, "RIOHalted set\n");
555 PortP->Config = 0; /* Direct semaphore */
556 PortP->PortState = 0;
557 PortP->firstOpen = 0;
558 PortP->FlushCmdBodge = 0;
559 PortP->ModemState = PortP->CookMode = 0;
560 PortP->Mapped = 0;
561 PortP->WflushFlag = 0;
562 PortP->MagicFlags = 0;
563 PortP->RxDataStart = 0;
564 PortP->TxBufferIn = 0;
565 PortP->TxBufferOut = 0;
566}
567
568/*
569** Put a command onto a port.
570** The PortPointer, command, length and arg are passed.
571** The len is the length *inclusive* of the command byte,
572** and so for a command that takes no data, len==1.
573** The arg is a single byte, and is only used if len==2.
574** Other values of len aren't allowed, and will cause
575** a panic.
576*/
577int RIOShortCommand(struct rio_info *p, struct Port *PortP, int command, int len, int arg)
578{
579 struct PKT __iomem *PacketP;
580 int retries = 20; /* at 10 per second -> 2 seconds */
581 unsigned long flags;
582
583 rio_dprintk(RIO_DEBUG_TTY, "entering shortcommand.\n");
584
585 if (PortP->State & RIO_DELETED) {
586 rio_dprintk(RIO_DEBUG_TTY, "Short command to deleted RTA ignored\n");
587 return RIO_FAIL;
588 }
589 rio_spin_lock_irqsave(&PortP->portSem, flags);
590
591 /*
592 ** If the port is in use for pre-emptive command, then wait for it to
593 ** be free again.
594 */
595 while ((PortP->InUse != NOT_INUSE) && !p->RIOHalted) {
596 rio_dprintk(RIO_DEBUG_TTY, "Waiting for not in use (%d)\n", retries);
597 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
598 if (retries-- <= 0) {
599 return RIO_FAIL;
600 }
601 if (RIODelay_ni(PortP, HUNDRED_MS) == RIO_FAIL) {
602 return RIO_FAIL;
603 }
604 rio_spin_lock_irqsave(&PortP->portSem, flags);
605 }
606 if (PortP->State & RIO_DELETED) {
607 rio_dprintk(RIO_DEBUG_TTY, "Short command to deleted RTA ignored\n");
608 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
609 return RIO_FAIL;
610 }
611
612 while (!can_add_transmit(&PacketP, PortP) && !p->RIOHalted) {
613 rio_dprintk(RIO_DEBUG_TTY, "Waiting to add short command to queue (%d)\n", retries);
614 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
615 if (retries-- <= 0) {
616 rio_dprintk(RIO_DEBUG_TTY, "out of tries. Failing\n");
617 return RIO_FAIL;
618 }
619 if (RIODelay_ni(PortP, HUNDRED_MS) == RIO_FAIL) {
620 return RIO_FAIL;
621 }
622 rio_spin_lock_irqsave(&PortP->portSem, flags);
623 }
624
625 if (p->RIOHalted) {
626 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
627 return RIO_FAIL;
628 }
629
630 /*
631 ** set the command byte and the argument byte
632 */
633 writeb(command, &PacketP->data[0]);
634
635 if (len == 2)
636 writeb(arg, &PacketP->data[1]);
637
638 /*
639 ** set the length of the packet and set the command bit.
640 */
641 writeb(PKT_CMD_BIT | len, &PacketP->len);
642
643 add_transmit(PortP);
644 /*
645 ** Count characters transmitted for port statistics reporting
646 */
647 if (PortP->statsGather)
648 PortP->txchars += len;
649
650 rio_spin_unlock_irqrestore(&PortP->portSem, flags);
651 return p->RIOHalted ? RIO_FAIL : ~RIO_FAIL;
652}
653
654
diff --git a/drivers/char/rio/route.h b/drivers/char/rio/route.h
deleted file mode 100644
index 46e963771c30..000000000000
--- a/drivers/char/rio/route.h
+++ /dev/null
@@ -1,101 +0,0 @@
1/****************************************************************************
2 ******* *******
3 ******* R O U T E H E A D E R
4 ******* *******
5 ****************************************************************************
6
7 Author : Ian Nandhra / Jeremy Rolls
8 Date :
9
10 *
11 * (C) 1990 - 2000 Specialix International Ltd., Byfleet, Surrey, UK.
12 *
13 * 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.
17 *
18 * 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.
22 *
23 * 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
27 Version : 0.01
28
29
30 Mods
31 ----------------------------------------------------------------------------
32 Date By Description
33 ----------------------------------------------------------------------------
34
35 ***************************************************************************/
36
37#ifndef _route_h
38#define _route_h
39
40#define MAX_LINKS 4
41#define MAX_NODES 17 /* Maximum nodes in a subnet */
42#define NODE_BYTES ((MAX_NODES / 8) + 1) /* Number of bytes needed for
43 1 bit per node */
44#define ROUTE_DATA_SIZE (NODE_BYTES + 2) /* Number of bytes for complete
45 info about cost etc. */
46#define ROUTES_PER_PACKET ((PKT_MAX_DATA_LEN -2)/ ROUTE_DATA_SIZE)
47 /* Number of nodes we can squeeze
48 into one packet */
49#define MAX_TOPOLOGY_PACKETS (MAX_NODES / ROUTES_PER_PACKET + 1)
50/************************************************
51 * Define the types of command for the ROUTE RUP.
52 ************************************************/
53#define ROUTE_REQUEST 0 /* Request an ID */
54#define ROUTE_FOAD 1 /* Kill the RTA */
55#define ROUTE_ALREADY 2 /* ID given already */
56#define ROUTE_USED 3 /* All ID's used */
57#define ROUTE_ALLOCATE 4 /* Here it is */
58#define ROUTE_REQ_TOP 5 /* I bet you didn't expect....
59 the Topological Inquisition */
60#define ROUTE_TOPOLOGY 6 /* Topology request answered FD */
61/*******************************************************************
62 * Define the Route Map Structure
63 *
64 * The route map gives a pointer to a Link Structure to use.
65 * This allows Disconnected Links to be checked quickly
66 ******************************************************************/
67typedef struct COST_ROUTE COST_ROUTE;
68struct COST_ROUTE {
69 unsigned char cost; /* Cost down this link */
70 unsigned char route[NODE_BYTES]; /* Nodes through this route */
71};
72
73typedef struct ROUTE_STR ROUTE_STR;
74struct ROUTE_STR {
75 COST_ROUTE cost_route[MAX_LINKS];
76 /* cost / route for this link */
77 ushort favoured; /* favoured link */
78};
79
80
81#define NO_LINK (short) 5 /* Link unattached */
82#define ROUTE_NO_ID (short) 100 /* No Id */
83#define ROUTE_DISCONNECT (ushort) 0xff /* Not connected */
84#define ROUTE_INTERCONNECT (ushort) 0x40 /* Sub-net interconnect */
85
86
87#define SYNC_RUP (ushort) 255
88#define COMMAND_RUP (ushort) 254
89#define ERROR_RUP (ushort) 253
90#define POLL_RUP (ushort) 252
91#define BOOT_RUP (ushort) 251
92#define ROUTE_RUP (ushort) 250
93#define STATUS_RUP (ushort) 249
94#define POWER_RUP (ushort) 248
95
96#define HIGHEST_RUP (ushort) 255 /* Set to Top one */
97#define LOWEST_RUP (ushort) 248 /* Set to bottom one */
98
99#endif
100
101/*********** end of file ***********/
diff --git a/drivers/char/rio/rup.h b/drivers/char/rio/rup.h
deleted file mode 100644
index 4ae90cb207a9..000000000000
--- a/drivers/char/rio/rup.h
+++ /dev/null
@@ -1,69 +0,0 @@
1/****************************************************************************
2 ******* *******
3 ******* R U P S T R U C T U R E
4 ******* *******
5 ****************************************************************************
6
7 Author : Ian Nandhra
8 Date :
9
10 *
11 * (C) 1990 - 2000 Specialix International Ltd., Byfleet, Surrey, UK.
12 *
13 * 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.
17 *
18 * 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.
22 *
23 * 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
27 Version : 0.01
28
29
30 Mods
31 ----------------------------------------------------------------------------
32 Date By Description
33 ----------------------------------------------------------------------------
34
35 ***************************************************************************/
36
37#ifndef _rup_h
38#define _rup_h 1
39
40#define MAX_RUP ((short) 16)
41#define PKTS_PER_RUP ((short) 2) /* They are always used in pairs */
42
43/*************************************************
44 * Define all the packet request stuff
45 ************************************************/
46#define TX_RUP_INACTIVE 0 /* Nothing to transmit */
47#define TX_PACKET_READY 1 /* Transmit packet ready */
48#define TX_LOCK_RUP 2 /* Transmit side locked */
49
50#define RX_RUP_INACTIVE 0 /* Nothing received */
51#define RX_PACKET_READY 1 /* Packet received */
52
53#define RUP_NO_OWNER 0xff /* RUP not owned by any process */
54
55struct RUP {
56 u16 txpkt; /* Outgoing packet */
57 u16 rxpkt; /* Incoming packet */
58 u16 link; /* Which link to send down? */
59 u8 rup_dest_unit[2]; /* Destination unit */
60 u16 handshake; /* For handshaking */
61 u16 timeout; /* Timeout */
62 u16 status; /* Status */
63 u16 txcontrol; /* Transmit control */
64 u16 rxcontrol; /* Receive control */
65};
66
67#endif
68
69/*********** end of file ***********/
diff --git a/drivers/char/rio/unixrup.h b/drivers/char/rio/unixrup.h
deleted file mode 100644
index 7abf0cba0f2c..000000000000
--- a/drivers/char/rio/unixrup.h
+++ /dev/null
@@ -1,51 +0,0 @@
1/*
2** -----------------------------------------------------------------------------
3**
4** Perle Specialix driver for Linux
5** Ported from existing RIO Driver for SCO sources.
6 *
7 * (C) 1990 - 2000 Specialix International Ltd., Byfleet, Surrey, UK.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22**
23** Module : unixrup.h
24** SID : 1.2
25** Last Modified : 11/6/98 11:34:20
26** Retrieved : 11/6/98 11:34:22
27**
28** ident @(#)unixrup.h 1.2
29**
30** -----------------------------------------------------------------------------
31*/
32
33#ifndef __rio_unixrup_h__
34#define __rio_unixrup_h__
35
36/*
37** UnixRup data structure. This contains pointers to actual RUPs on the
38** host card, and all the command/boot control stuff.
39*/
40struct UnixRup {
41 struct CmdBlk *CmdsWaitingP; /* Commands waiting to be done */
42 struct CmdBlk *CmdPendingP; /* The command currently being sent */
43 struct RUP __iomem *RupP; /* the Rup to send it to */
44 unsigned int Id; /* Id number */
45 unsigned int BaseSysPort; /* SysPort of first tty on this RTA */
46 unsigned int ModTypes; /* Modules on this RTA */
47 spinlock_t RupLock; /* Lock structure for MPX */
48 /* struct lockb RupLock; *//* Lock structure for MPX */
49};
50
51#endif /* __rio_unixrup_h__ */
diff --git a/drivers/char/riscom8.c b/drivers/char/riscom8.c
deleted file mode 100644
index af4de1fe8445..000000000000
--- a/drivers/char/riscom8.c
+++ /dev/null
@@ -1,1560 +0,0 @@
1/*
2 * linux/drivers/char/riscom.c -- RISCom/8 multiport serial driver.
3 *
4 * Copyright (C) 1994-1996 Dmitry Gorodchanin (pgmdsg@ibi.com)
5 *
6 * This code is loosely based on the Linux serial driver, written by
7 * Linus Torvalds, Theodore T'so and others. The RISCom/8 card
8 * programming info was obtained from various drivers for other OSes
9 * (FreeBSD, ISC, etc), but no source code from those drivers were
10 * directly included in this driver.
11 *
12 *
13 * 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.
17 *
18 * 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.
22 *
23 * 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 *
27 * Revision 1.1
28 *
29 * ChangeLog:
30 * Arnaldo Carvalho de Melo <acme@conectiva.com.br> - 27-Jun-2001
31 * - get rid of check_region and several cleanups
32 */
33
34#include <linux/module.h>
35
36#include <linux/io.h>
37#include <linux/kernel.h>
38#include <linux/sched.h>
39#include <linux/ioport.h>
40#include <linux/interrupt.h>
41#include <linux/errno.h>
42#include <linux/tty.h>
43#include <linux/mm.h>
44#include <linux/serial.h>
45#include <linux/fcntl.h>
46#include <linux/major.h>
47#include <linux/init.h>
48#include <linux/delay.h>
49#include <linux/tty_flip.h>
50#include <linux/spinlock.h>
51#include <linux/device.h>
52
53#include <linux/uaccess.h>
54
55#include "riscom8.h"
56#include "riscom8_reg.h"
57
58/* Am I paranoid or not ? ;-) */
59#define RISCOM_PARANOIA_CHECK
60
61/*
62 * Crazy InteliCom/8 boards sometimes have swapped CTS & DSR signals.
63 * You can slightly speed up things by #undefing the following option,
64 * if you are REALLY sure that your board is correct one.
65 */
66
67#define RISCOM_BRAIN_DAMAGED_CTS
68
69/*
70 * The following defines are mostly for testing purposes. But if you need
71 * some nice reporting in your syslog, you can define them also.
72 */
73#undef RC_REPORT_FIFO
74#undef RC_REPORT_OVERRUN
75
76
77#define RISCOM_LEGAL_FLAGS \
78 (ASYNC_HUP_NOTIFY | ASYNC_SAK | ASYNC_SPLIT_TERMIOS | \
79 ASYNC_SPD_HI | ASYNC_SPEED_VHI | ASYNC_SESSION_LOCKOUT | \
80 ASYNC_PGRP_LOCKOUT | ASYNC_CALLOUT_NOHUP)
81
82static struct tty_driver *riscom_driver;
83
84static DEFINE_SPINLOCK(riscom_lock);
85
86static struct riscom_board rc_board[RC_NBOARD] = {
87 {
88 .base = RC_IOBASE1,
89 },
90 {
91 .base = RC_IOBASE2,
92 },
93 {
94 .base = RC_IOBASE3,
95 },
96 {
97 .base = RC_IOBASE4,
98 },
99};
100
101static struct riscom_port rc_port[RC_NBOARD * RC_NPORT];
102
103/* RISCom/8 I/O ports addresses (without address translation) */
104static unsigned short rc_ioport[] = {
105#if 1
106 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x09, 0x0a, 0x0b, 0x0c,
107#else
108 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x09, 0x0a, 0x0b, 0x0c, 0x10,
109 0x11, 0x12, 0x18, 0x28, 0x31, 0x32, 0x39, 0x3a, 0x40, 0x41, 0x61, 0x62,
110 0x63, 0x64, 0x6b, 0x70, 0x71, 0x78, 0x7a, 0x7b, 0x7f, 0x100, 0x101
111#endif
112};
113#define RC_NIOPORT ARRAY_SIZE(rc_ioport)
114
115
116static int rc_paranoia_check(struct riscom_port const *port,
117 char *name, const char *routine)
118{
119#ifdef RISCOM_PARANOIA_CHECK
120 static const char badmagic[] = KERN_INFO
121 "rc: Warning: bad riscom port magic number for device %s in %s\n";
122 static const char badinfo[] = KERN_INFO
123 "rc: Warning: null riscom port for device %s in %s\n";
124
125 if (!port) {
126 printk(badinfo, name, routine);
127 return 1;
128 }
129 if (port->magic != RISCOM8_MAGIC) {
130 printk(badmagic, name, routine);
131 return 1;
132 }
133#endif
134 return 0;
135}
136
137/*
138 *
139 * Service functions for RISCom/8 driver.
140 *
141 */
142
143/* Get board number from pointer */
144static inline int board_No(struct riscom_board const *bp)
145{
146 return bp - rc_board;
147}
148
149/* Get port number from pointer */
150static inline int port_No(struct riscom_port const *port)
151{
152 return RC_PORT(port - rc_port);
153}
154
155/* Get pointer to board from pointer to port */
156static inline struct riscom_board *port_Board(struct riscom_port const *port)
157{
158 return &rc_board[RC_BOARD(port - rc_port)];
159}
160
161/* Input Byte from CL CD180 register */
162static inline unsigned char rc_in(struct riscom_board const *bp,
163 unsigned short reg)
164{
165 return inb(bp->base + RC_TO_ISA(reg));
166}
167
168/* Output Byte to CL CD180 register */
169static inline void rc_out(struct riscom_board const *bp, unsigned short reg,
170 unsigned char val)
171{
172 outb(val, bp->base + RC_TO_ISA(reg));
173}
174
175/* Wait for Channel Command Register ready */
176static void rc_wait_CCR(struct riscom_board const *bp)
177{
178 unsigned long delay;
179
180 /* FIXME: need something more descriptive then 100000 :) */
181 for (delay = 100000; delay; delay--)
182 if (!rc_in(bp, CD180_CCR))
183 return;
184
185 printk(KERN_INFO "rc%d: Timeout waiting for CCR.\n", board_No(bp));
186}
187
188/*
189 * RISCom/8 probe functions.
190 */
191
192static int rc_request_io_range(struct riscom_board * const bp)
193{
194 int i;
195
196 for (i = 0; i < RC_NIOPORT; i++)
197 if (!request_region(RC_TO_ISA(rc_ioport[i]) + bp->base, 1,
198 "RISCom/8")) {
199 goto out_release;
200 }
201 return 0;
202out_release:
203 printk(KERN_INFO "rc%d: Skipping probe at 0x%03x. IO address in use.\n",
204 board_No(bp), bp->base);
205 while (--i >= 0)
206 release_region(RC_TO_ISA(rc_ioport[i]) + bp->base, 1);
207 return 1;
208}
209
210static void rc_release_io_range(struct riscom_board * const bp)
211{
212 int i;
213
214 for (i = 0; i < RC_NIOPORT; i++)
215 release_region(RC_TO_ISA(rc_ioport[i]) + bp->base, 1);
216}
217
218/* Reset and setup CD180 chip */
219static void __init rc_init_CD180(struct riscom_board const *bp)
220{
221 unsigned long flags;
222
223 spin_lock_irqsave(&riscom_lock, flags);
224
225 rc_out(bp, RC_CTOUT, 0); /* Clear timeout */
226 rc_wait_CCR(bp); /* Wait for CCR ready */
227 rc_out(bp, CD180_CCR, CCR_HARDRESET); /* Reset CD180 chip */
228 spin_unlock_irqrestore(&riscom_lock, flags);
229 msleep(50); /* Delay 0.05 sec */
230 spin_lock_irqsave(&riscom_lock, flags);
231 rc_out(bp, CD180_GIVR, RC_ID); /* Set ID for this chip */
232 rc_out(bp, CD180_GICR, 0); /* Clear all bits */
233 rc_out(bp, CD180_PILR1, RC_ACK_MINT); /* Prio for modem intr */
234 rc_out(bp, CD180_PILR2, RC_ACK_TINT); /* Prio for tx intr */
235 rc_out(bp, CD180_PILR3, RC_ACK_RINT); /* Prio for rx intr */
236
237 /* Setting up prescaler. We need 4 ticks per 1 ms */
238 rc_out(bp, CD180_PPRH, (RC_OSCFREQ/(1000000/RISCOM_TPS)) >> 8);
239 rc_out(bp, CD180_PPRL, (RC_OSCFREQ/(1000000/RISCOM_TPS)) & 0xff);
240
241 spin_unlock_irqrestore(&riscom_lock, flags);
242}
243
244/* Main probing routine, also sets irq. */
245static int __init rc_probe(struct riscom_board *bp)
246{
247 unsigned char val1, val2;
248 int irqs = 0;
249 int retries;
250
251 bp->irq = 0;
252
253 if (rc_request_io_range(bp))
254 return 1;
255
256 /* Are the I/O ports here ? */
257 rc_out(bp, CD180_PPRL, 0x5a);
258 outb(0xff, 0x80);
259 val1 = rc_in(bp, CD180_PPRL);
260 rc_out(bp, CD180_PPRL, 0xa5);
261 outb(0x00, 0x80);
262 val2 = rc_in(bp, CD180_PPRL);
263
264 if ((val1 != 0x5a) || (val2 != 0xa5)) {
265 printk(KERN_ERR "rc%d: RISCom/8 Board at 0x%03x not found.\n",
266 board_No(bp), bp->base);
267 goto out_release;
268 }
269
270 /* It's time to find IRQ for this board */
271 for (retries = 0; retries < 5 && irqs <= 0; retries++) {
272 irqs = probe_irq_on();
273 rc_init_CD180(bp); /* Reset CD180 chip */
274 rc_out(bp, CD180_CAR, 2); /* Select port 2 */
275 rc_wait_CCR(bp);
276 rc_out(bp, CD180_CCR, CCR_TXEN); /* Enable transmitter */
277 rc_out(bp, CD180_IER, IER_TXRDY);/* Enable tx empty intr */
278 msleep(50);
279 irqs = probe_irq_off(irqs);
280 val1 = rc_in(bp, RC_BSR); /* Get Board Status reg */
281 val2 = rc_in(bp, RC_ACK_TINT); /* ACK interrupt */
282 rc_init_CD180(bp); /* Reset CD180 again */
283
284 if ((val1 & RC_BSR_TINT) || (val2 != (RC_ID | GIVR_IT_TX))) {
285 printk(KERN_ERR "rc%d: RISCom/8 Board at 0x%03x not "
286 "found.\n", board_No(bp), bp->base);
287 goto out_release;
288 }
289 }
290
291 if (irqs <= 0) {
292 printk(KERN_ERR "rc%d: Can't find IRQ for RISCom/8 board "
293 "at 0x%03x.\n", board_No(bp), bp->base);
294 goto out_release;
295 }
296 bp->irq = irqs;
297 bp->flags |= RC_BOARD_PRESENT;
298
299 printk(KERN_INFO "rc%d: RISCom/8 Rev. %c board detected at "
300 "0x%03x, IRQ %d.\n",
301 board_No(bp),
302 (rc_in(bp, CD180_GFRCR) & 0x0f) + 'A', /* Board revision */
303 bp->base, bp->irq);
304
305 return 0;
306out_release:
307 rc_release_io_range(bp);
308 return 1;
309}
310
311/*
312 *
313 * Interrupt processing routines.
314 *
315 */
316
317static struct riscom_port *rc_get_port(struct riscom_board const *bp,
318 unsigned char const *what)
319{
320 unsigned char channel;
321 struct riscom_port *port;
322
323 channel = rc_in(bp, CD180_GICR) >> GICR_CHAN_OFF;
324 if (channel < CD180_NCH) {
325 port = &rc_port[board_No(bp) * RC_NPORT + channel];
326 if (port->port.flags & ASYNC_INITIALIZED)
327 return port;
328 }
329 printk(KERN_ERR "rc%d: %s interrupt from invalid port %d\n",
330 board_No(bp), what, channel);
331 return NULL;
332}
333
334static void rc_receive_exc(struct riscom_board const *bp)
335{
336 struct riscom_port *port;
337 struct tty_struct *tty;
338 unsigned char status;
339 unsigned char ch, flag;
340
341 port = rc_get_port(bp, "Receive");
342 if (port == NULL)
343 return;
344
345 tty = tty_port_tty_get(&port->port);
346
347#ifdef RC_REPORT_OVERRUN
348 status = rc_in(bp, CD180_RCSR);
349 if (status & RCSR_OE)
350 port->overrun++;
351 status &= port->mark_mask;
352#else
353 status = rc_in(bp, CD180_RCSR) & port->mark_mask;
354#endif
355 ch = rc_in(bp, CD180_RDR);
356 if (!status)
357 goto out;
358 if (status & RCSR_TOUT) {
359 printk(KERN_WARNING "rc%d: port %d: Receiver timeout. "
360 "Hardware problems ?\n",
361 board_No(bp), port_No(port));
362 goto out;
363
364 } else if (status & RCSR_BREAK) {
365 printk(KERN_INFO "rc%d: port %d: Handling break...\n",
366 board_No(bp), port_No(port));
367 flag = TTY_BREAK;
368 if (tty && (port->port.flags & ASYNC_SAK))
369 do_SAK(tty);
370
371 } else if (status & RCSR_PE)
372 flag = TTY_PARITY;
373
374 else if (status & RCSR_FE)
375 flag = TTY_FRAME;
376
377 else if (status & RCSR_OE)
378 flag = TTY_OVERRUN;
379 else
380 flag = TTY_NORMAL;
381
382 if (tty) {
383 tty_insert_flip_char(tty, ch, flag);
384 tty_flip_buffer_push(tty);
385 }
386out:
387 tty_kref_put(tty);
388}
389
390static void rc_receive(struct riscom_board const *bp)
391{
392 struct riscom_port *port;
393 struct tty_struct *tty;
394 unsigned char count;
395
396 port = rc_get_port(bp, "Receive");
397 if (port == NULL)
398 return;
399
400 tty = tty_port_tty_get(&port->port);
401
402 count = rc_in(bp, CD180_RDCR);
403
404#ifdef RC_REPORT_FIFO
405 port->hits[count > 8 ? 9 : count]++;
406#endif
407
408 while (count--) {
409 u8 ch = rc_in(bp, CD180_RDR);
410 if (tty)
411 tty_insert_flip_char(tty, ch, TTY_NORMAL);
412 }
413 if (tty) {
414 tty_flip_buffer_push(tty);
415 tty_kref_put(tty);
416 }
417}
418
419static void rc_transmit(struct riscom_board const *bp)
420{
421 struct riscom_port *port;
422 struct tty_struct *tty;
423 unsigned char count;
424
425 port = rc_get_port(bp, "Transmit");
426 if (port == NULL)
427 return;
428
429 tty = tty_port_tty_get(&port->port);
430
431 if (port->IER & IER_TXEMPTY) {
432 /* FIFO drained */
433 rc_out(bp, CD180_CAR, port_No(port));
434 port->IER &= ~IER_TXEMPTY;
435 rc_out(bp, CD180_IER, port->IER);
436 goto out;
437 }
438
439 if ((port->xmit_cnt <= 0 && !port->break_length)
440 || (tty && (tty->stopped || tty->hw_stopped))) {
441 rc_out(bp, CD180_CAR, port_No(port));
442 port->IER &= ~IER_TXRDY;
443 rc_out(bp, CD180_IER, port->IER);
444 goto out;
445 }
446
447 if (port->break_length) {
448 if (port->break_length > 0) {
449 if (port->COR2 & COR2_ETC) {
450 rc_out(bp, CD180_TDR, CD180_C_ESC);
451 rc_out(bp, CD180_TDR, CD180_C_SBRK);
452 port->COR2 &= ~COR2_ETC;
453 }
454 count = min_t(int, port->break_length, 0xff);
455 rc_out(bp, CD180_TDR, CD180_C_ESC);
456 rc_out(bp, CD180_TDR, CD180_C_DELAY);
457 rc_out(bp, CD180_TDR, count);
458 port->break_length -= count;
459 if (port->break_length == 0)
460 port->break_length--;
461 } else {
462 rc_out(bp, CD180_TDR, CD180_C_ESC);
463 rc_out(bp, CD180_TDR, CD180_C_EBRK);
464 rc_out(bp, CD180_COR2, port->COR2);
465 rc_wait_CCR(bp);
466 rc_out(bp, CD180_CCR, CCR_CORCHG2);
467 port->break_length = 0;
468 }
469 goto out;
470 }
471
472 count = CD180_NFIFO;
473 do {
474 rc_out(bp, CD180_TDR, port->port.xmit_buf[port->xmit_tail++]);
475 port->xmit_tail = port->xmit_tail & (SERIAL_XMIT_SIZE-1);
476 if (--port->xmit_cnt <= 0)
477 break;
478 } while (--count > 0);
479
480 if (port->xmit_cnt <= 0) {
481 rc_out(bp, CD180_CAR, port_No(port));
482 port->IER &= ~IER_TXRDY;
483 rc_out(bp, CD180_IER, port->IER);
484 }
485 if (tty && port->xmit_cnt <= port->wakeup_chars)
486 tty_wakeup(tty);
487out:
488 tty_kref_put(tty);
489}
490
491static void rc_check_modem(struct riscom_board const *bp)
492{
493 struct riscom_port *port;
494 struct tty_struct *tty;
495 unsigned char mcr;
496
497 port = rc_get_port(bp, "Modem");
498 if (port == NULL)
499 return;
500
501 tty = tty_port_tty_get(&port->port);
502
503 mcr = rc_in(bp, CD180_MCR);
504 if (mcr & MCR_CDCHG) {
505 if (rc_in(bp, CD180_MSVR) & MSVR_CD)
506 wake_up_interruptible(&port->port.open_wait);
507 else if (tty)
508 tty_hangup(tty);
509 }
510
511#ifdef RISCOM_BRAIN_DAMAGED_CTS
512 if (mcr & MCR_CTSCHG) {
513 if (rc_in(bp, CD180_MSVR) & MSVR_CTS) {
514 port->IER |= IER_TXRDY;
515 if (tty) {
516 tty->hw_stopped = 0;
517 if (port->xmit_cnt <= port->wakeup_chars)
518 tty_wakeup(tty);
519 }
520 } else {
521 if (tty)
522 tty->hw_stopped = 1;
523 port->IER &= ~IER_TXRDY;
524 }
525 rc_out(bp, CD180_IER, port->IER);
526 }
527 if (mcr & MCR_DSRCHG) {
528 if (rc_in(bp, CD180_MSVR) & MSVR_DSR) {
529 port->IER |= IER_TXRDY;
530 if (tty) {
531 tty->hw_stopped = 0;
532 if (port->xmit_cnt <= port->wakeup_chars)
533 tty_wakeup(tty);
534 }
535 } else {
536 if (tty)
537 tty->hw_stopped = 1;
538 port->IER &= ~IER_TXRDY;
539 }
540 rc_out(bp, CD180_IER, port->IER);
541 }
542#endif /* RISCOM_BRAIN_DAMAGED_CTS */
543
544 /* Clear change bits */
545 rc_out(bp, CD180_MCR, 0);
546 tty_kref_put(tty);
547}
548
549/* The main interrupt processing routine */
550static irqreturn_t rc_interrupt(int dummy, void *dev_id)
551{
552 unsigned char status;
553 unsigned char ack;
554 struct riscom_board *bp = dev_id;
555 unsigned long loop = 0;
556 int handled = 0;
557
558 if (!(bp->flags & RC_BOARD_ACTIVE))
559 return IRQ_NONE;
560
561 while ((++loop < 16) && ((status = ~(rc_in(bp, RC_BSR))) &
562 (RC_BSR_TOUT | RC_BSR_TINT |
563 RC_BSR_MINT | RC_BSR_RINT))) {
564 handled = 1;
565 if (status & RC_BSR_TOUT)
566 printk(KERN_WARNING "rc%d: Got timeout. Hardware "
567 "error?\n", board_No(bp));
568 else if (status & RC_BSR_RINT) {
569 ack = rc_in(bp, RC_ACK_RINT);
570 if (ack == (RC_ID | GIVR_IT_RCV))
571 rc_receive(bp);
572 else if (ack == (RC_ID | GIVR_IT_REXC))
573 rc_receive_exc(bp);
574 else
575 printk(KERN_WARNING "rc%d: Bad receive ack "
576 "0x%02x.\n",
577 board_No(bp), ack);
578 } else if (status & RC_BSR_TINT) {
579 ack = rc_in(bp, RC_ACK_TINT);
580 if (ack == (RC_ID | GIVR_IT_TX))
581 rc_transmit(bp);
582 else
583 printk(KERN_WARNING "rc%d: Bad transmit ack "
584 "0x%02x.\n",
585 board_No(bp), ack);
586 } else /* if (status & RC_BSR_MINT) */ {
587 ack = rc_in(bp, RC_ACK_MINT);
588 if (ack == (RC_ID | GIVR_IT_MODEM))
589 rc_check_modem(bp);
590 else
591 printk(KERN_WARNING "rc%d: Bad modem ack "
592 "0x%02x.\n",
593 board_No(bp), ack);
594 }
595 rc_out(bp, CD180_EOIR, 0); /* Mark end of interrupt */
596 rc_out(bp, RC_CTOUT, 0); /* Clear timeout flag */
597 }
598 return IRQ_RETVAL(handled);
599}
600
601/*
602 * Routines for open & close processing.
603 */
604
605/* Called with disabled interrupts */
606static int rc_setup_board(struct riscom_board *bp)
607{
608 int error;
609
610 if (bp->flags & RC_BOARD_ACTIVE)
611 return 0;
612
613 error = request_irq(bp->irq, rc_interrupt, IRQF_DISABLED,
614 "RISCom/8", bp);
615 if (error)
616 return error;
617
618 rc_out(bp, RC_CTOUT, 0); /* Just in case */
619 bp->DTR = ~0;
620 rc_out(bp, RC_DTR, bp->DTR); /* Drop DTR on all ports */
621
622 bp->flags |= RC_BOARD_ACTIVE;
623
624 return 0;
625}
626
627/* Called with disabled interrupts */
628static void rc_shutdown_board(struct riscom_board *bp)
629{
630 if (!(bp->flags & RC_BOARD_ACTIVE))
631 return;
632
633 bp->flags &= ~RC_BOARD_ACTIVE;
634
635 free_irq(bp->irq, NULL);
636
637 bp->DTR = ~0;
638 rc_out(bp, RC_DTR, bp->DTR); /* Drop DTR on all ports */
639
640}
641
642/*
643 * Setting up port characteristics.
644 * Must be called with disabled interrupts
645 */
646static void rc_change_speed(struct tty_struct *tty, struct riscom_board *bp,
647 struct riscom_port *port)
648{
649 unsigned long baud;
650 long tmp;
651 unsigned char cor1 = 0, cor3 = 0;
652 unsigned char mcor1 = 0, mcor2 = 0;
653
654 port->IER = 0;
655 port->COR2 = 0;
656 port->MSVR = MSVR_RTS;
657
658 baud = tty_get_baud_rate(tty);
659
660 /* Select port on the board */
661 rc_out(bp, CD180_CAR, port_No(port));
662
663 if (!baud) {
664 /* Drop DTR & exit */
665 bp->DTR |= (1u << port_No(port));
666 rc_out(bp, RC_DTR, bp->DTR);
667 return;
668 } else {
669 /* Set DTR on */
670 bp->DTR &= ~(1u << port_No(port));
671 rc_out(bp, RC_DTR, bp->DTR);
672 }
673
674 /*
675 * Now we must calculate some speed depended things
676 */
677
678 /* Set baud rate for port */
679 tmp = (((RC_OSCFREQ + baud/2) / baud +
680 CD180_TPC/2) / CD180_TPC);
681
682 rc_out(bp, CD180_RBPRH, (tmp >> 8) & 0xff);
683 rc_out(bp, CD180_TBPRH, (tmp >> 8) & 0xff);
684 rc_out(bp, CD180_RBPRL, tmp & 0xff);
685 rc_out(bp, CD180_TBPRL, tmp & 0xff);
686
687 baud = (baud + 5) / 10; /* Estimated CPS */
688
689 /* Two timer ticks seems enough to wakeup something like SLIP driver */
690 tmp = ((baud + HZ/2) / HZ) * 2 - CD180_NFIFO;
691 port->wakeup_chars = (tmp < 0) ? 0 : ((tmp >= SERIAL_XMIT_SIZE) ?
692 SERIAL_XMIT_SIZE - 1 : tmp);
693
694 /* Receiver timeout will be transmission time for 1.5 chars */
695 tmp = (RISCOM_TPS + RISCOM_TPS/2 + baud/2) / baud;
696 tmp = (tmp > 0xff) ? 0xff : tmp;
697 rc_out(bp, CD180_RTPR, tmp);
698
699 switch (C_CSIZE(tty)) {
700 case CS5:
701 cor1 |= COR1_5BITS;
702 break;
703 case CS6:
704 cor1 |= COR1_6BITS;
705 break;
706 case CS7:
707 cor1 |= COR1_7BITS;
708 break;
709 case CS8:
710 cor1 |= COR1_8BITS;
711 break;
712 }
713 if (C_CSTOPB(tty))
714 cor1 |= COR1_2SB;
715
716 cor1 |= COR1_IGNORE;
717 if (C_PARENB(tty)) {
718 cor1 |= COR1_NORMPAR;
719 if (C_PARODD(tty))
720 cor1 |= COR1_ODDP;
721 if (I_INPCK(tty))
722 cor1 &= ~COR1_IGNORE;
723 }
724 /* Set marking of some errors */
725 port->mark_mask = RCSR_OE | RCSR_TOUT;
726 if (I_INPCK(tty))
727 port->mark_mask |= RCSR_FE | RCSR_PE;
728 if (I_BRKINT(tty) || I_PARMRK(tty))
729 port->mark_mask |= RCSR_BREAK;
730 if (I_IGNPAR(tty))
731 port->mark_mask &= ~(RCSR_FE | RCSR_PE);
732 if (I_IGNBRK(tty)) {
733 port->mark_mask &= ~RCSR_BREAK;
734 if (I_IGNPAR(tty))
735 /* Real raw mode. Ignore all */
736 port->mark_mask &= ~RCSR_OE;
737 }
738 /* Enable Hardware Flow Control */
739 if (C_CRTSCTS(tty)) {
740#ifdef RISCOM_BRAIN_DAMAGED_CTS
741 port->IER |= IER_DSR | IER_CTS;
742 mcor1 |= MCOR1_DSRZD | MCOR1_CTSZD;
743 mcor2 |= MCOR2_DSROD | MCOR2_CTSOD;
744 tty->hw_stopped = !(rc_in(bp, CD180_MSVR) &
745 (MSVR_CTS|MSVR_DSR));
746#else
747 port->COR2 |= COR2_CTSAE;
748#endif
749 }
750 /* Enable Software Flow Control. FIXME: I'm not sure about this */
751 /* Some people reported that it works, but I still doubt */
752 if (I_IXON(tty)) {
753 port->COR2 |= COR2_TXIBE;
754 cor3 |= (COR3_FCT | COR3_SCDE);
755 if (I_IXANY(tty))
756 port->COR2 |= COR2_IXM;
757 rc_out(bp, CD180_SCHR1, START_CHAR(tty));
758 rc_out(bp, CD180_SCHR2, STOP_CHAR(tty));
759 rc_out(bp, CD180_SCHR3, START_CHAR(tty));
760 rc_out(bp, CD180_SCHR4, STOP_CHAR(tty));
761 }
762 if (!C_CLOCAL(tty)) {
763 /* Enable CD check */
764 port->IER |= IER_CD;
765 mcor1 |= MCOR1_CDZD;
766 mcor2 |= MCOR2_CDOD;
767 }
768
769 if (C_CREAD(tty))
770 /* Enable receiver */
771 port->IER |= IER_RXD;
772
773 /* Set input FIFO size (1-8 bytes) */
774 cor3 |= RISCOM_RXFIFO;
775 /* Setting up CD180 channel registers */
776 rc_out(bp, CD180_COR1, cor1);
777 rc_out(bp, CD180_COR2, port->COR2);
778 rc_out(bp, CD180_COR3, cor3);
779 /* Make CD180 know about registers change */
780 rc_wait_CCR(bp);
781 rc_out(bp, CD180_CCR, CCR_CORCHG1 | CCR_CORCHG2 | CCR_CORCHG3);
782 /* Setting up modem option registers */
783 rc_out(bp, CD180_MCOR1, mcor1);
784 rc_out(bp, CD180_MCOR2, mcor2);
785 /* Enable CD180 transmitter & receiver */
786 rc_wait_CCR(bp);
787 rc_out(bp, CD180_CCR, CCR_TXEN | CCR_RXEN);
788 /* Enable interrupts */
789 rc_out(bp, CD180_IER, port->IER);
790 /* And finally set RTS on */
791 rc_out(bp, CD180_MSVR, port->MSVR);
792}
793
794/* Must be called with interrupts enabled */
795static int rc_activate_port(struct tty_port *port, struct tty_struct *tty)
796{
797 struct riscom_port *rp = container_of(port, struct riscom_port, port);
798 struct riscom_board *bp = port_Board(rp);
799 unsigned long flags;
800
801 if (tty_port_alloc_xmit_buf(port) < 0)
802 return -ENOMEM;
803
804 spin_lock_irqsave(&riscom_lock, flags);
805
806 clear_bit(TTY_IO_ERROR, &tty->flags);
807 bp->count++;
808 rp->xmit_cnt = rp->xmit_head = rp->xmit_tail = 0;
809 rc_change_speed(tty, bp, rp);
810 spin_unlock_irqrestore(&riscom_lock, flags);
811 return 0;
812}
813
814/* Must be called with interrupts disabled */
815static void rc_shutdown_port(struct tty_struct *tty,
816 struct riscom_board *bp, struct riscom_port *port)
817{
818#ifdef RC_REPORT_OVERRUN
819 printk(KERN_INFO "rc%d: port %d: Total %ld overruns were detected.\n",
820 board_No(bp), port_No(port), port->overrun);
821#endif
822#ifdef RC_REPORT_FIFO
823 {
824 int i;
825
826 printk(KERN_INFO "rc%d: port %d: FIFO hits [ ",
827 board_No(bp), port_No(port));
828 for (i = 0; i < 10; i++)
829 printk("%ld ", port->hits[i]);
830 printk("].\n");
831 }
832#endif
833 tty_port_free_xmit_buf(&port->port);
834
835 /* Select port */
836 rc_out(bp, CD180_CAR, port_No(port));
837 /* Reset port */
838 rc_wait_CCR(bp);
839 rc_out(bp, CD180_CCR, CCR_SOFTRESET);
840 /* Disable all interrupts from this port */
841 port->IER = 0;
842 rc_out(bp, CD180_IER, port->IER);
843
844 set_bit(TTY_IO_ERROR, &tty->flags);
845
846 if (--bp->count < 0) {
847 printk(KERN_INFO "rc%d: rc_shutdown_port: "
848 "bad board count: %d\n",
849 board_No(bp), bp->count);
850 bp->count = 0;
851 }
852 /*
853 * If this is the last opened port on the board
854 * shutdown whole board
855 */
856 if (!bp->count)
857 rc_shutdown_board(bp);
858}
859
860static int carrier_raised(struct tty_port *port)
861{
862 struct riscom_port *p = container_of(port, struct riscom_port, port);
863 struct riscom_board *bp = port_Board(p);
864 unsigned long flags;
865 int CD;
866
867 spin_lock_irqsave(&riscom_lock, flags);
868 rc_out(bp, CD180_CAR, port_No(p));
869 CD = rc_in(bp, CD180_MSVR) & MSVR_CD;
870 rc_out(bp, CD180_MSVR, MSVR_RTS);
871 bp->DTR &= ~(1u << port_No(p));
872 rc_out(bp, RC_DTR, bp->DTR);
873 spin_unlock_irqrestore(&riscom_lock, flags);
874 return CD;
875}
876
877static void dtr_rts(struct tty_port *port, int onoff)
878{
879 struct riscom_port *p = container_of(port, struct riscom_port, port);
880 struct riscom_board *bp = port_Board(p);
881 unsigned long flags;
882
883 spin_lock_irqsave(&riscom_lock, flags);
884 bp->DTR &= ~(1u << port_No(p));
885 if (onoff == 0)
886 bp->DTR |= (1u << port_No(p));
887 rc_out(bp, RC_DTR, bp->DTR);
888 spin_unlock_irqrestore(&riscom_lock, flags);
889}
890
891static int rc_open(struct tty_struct *tty, struct file *filp)
892{
893 int board;
894 int error;
895 struct riscom_port *port;
896 struct riscom_board *bp;
897
898 board = RC_BOARD(tty->index);
899 if (board >= RC_NBOARD || !(rc_board[board].flags & RC_BOARD_PRESENT))
900 return -ENODEV;
901
902 bp = &rc_board[board];
903 port = rc_port + board * RC_NPORT + RC_PORT(tty->index);
904 if (rc_paranoia_check(port, tty->name, "rc_open"))
905 return -ENODEV;
906
907 error = rc_setup_board(bp);
908 if (error)
909 return error;
910
911 tty->driver_data = port;
912 return tty_port_open(&port->port, tty, filp);
913}
914
915static void rc_flush_buffer(struct tty_struct *tty)
916{
917 struct riscom_port *port = tty->driver_data;
918 unsigned long flags;
919
920 if (rc_paranoia_check(port, tty->name, "rc_flush_buffer"))
921 return;
922
923 spin_lock_irqsave(&riscom_lock, flags);
924 port->xmit_cnt = port->xmit_head = port->xmit_tail = 0;
925 spin_unlock_irqrestore(&riscom_lock, flags);
926
927 tty_wakeup(tty);
928}
929
930static void rc_close_port(struct tty_port *port)
931{
932 unsigned long flags;
933 struct riscom_port *rp = container_of(port, struct riscom_port, port);
934 struct riscom_board *bp = port_Board(rp);
935 unsigned long timeout;
936
937 /*
938 * At this point we stop accepting input. To do this, we
939 * disable the receive line status interrupts, and tell the
940 * interrupt driver to stop checking the data ready bit in the
941 * line status register.
942 */
943
944 spin_lock_irqsave(&riscom_lock, flags);
945 rp->IER &= ~IER_RXD;
946
947 rp->IER &= ~IER_TXRDY;
948 rp->IER |= IER_TXEMPTY;
949 rc_out(bp, CD180_CAR, port_No(rp));
950 rc_out(bp, CD180_IER, rp->IER);
951 /*
952 * Before we drop DTR, make sure the UART transmitter
953 * has completely drained; this is especially
954 * important if there is a transmit FIFO!
955 */
956 timeout = jiffies + HZ;
957 while (rp->IER & IER_TXEMPTY) {
958 spin_unlock_irqrestore(&riscom_lock, flags);
959 msleep_interruptible(jiffies_to_msecs(rp->timeout));
960 spin_lock_irqsave(&riscom_lock, flags);
961 if (time_after(jiffies, timeout))
962 break;
963 }
964 rc_shutdown_port(port->tty, bp, rp);
965 spin_unlock_irqrestore(&riscom_lock, flags);
966}
967
968static void rc_close(struct tty_struct *tty, struct file *filp)
969{
970 struct riscom_port *port = tty->driver_data;
971
972 if (!port || rc_paranoia_check(port, tty->name, "close"))
973 return;
974 tty_port_close(&port->port, tty, filp);
975}
976
977static int rc_write(struct tty_struct *tty,
978 const unsigned char *buf, int count)
979{
980 struct riscom_port *port = tty->driver_data;
981 struct riscom_board *bp;
982 int c, total = 0;
983 unsigned long flags;
984
985 if (rc_paranoia_check(port, tty->name, "rc_write"))
986 return 0;
987
988 bp = port_Board(port);
989
990 while (1) {
991 spin_lock_irqsave(&riscom_lock, flags);
992
993 c = min_t(int, count, min(SERIAL_XMIT_SIZE - port->xmit_cnt - 1,
994 SERIAL_XMIT_SIZE - port->xmit_head));
995 if (c <= 0)
996 break; /* lock continues to be held */
997
998 memcpy(port->port.xmit_buf + port->xmit_head, buf, c);
999 port->xmit_head = (port->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
1000 port->xmit_cnt += c;
1001
1002 spin_unlock_irqrestore(&riscom_lock, flags);
1003
1004 buf += c;
1005 count -= c;
1006 total += c;
1007 }
1008
1009 if (port->xmit_cnt && !tty->stopped && !tty->hw_stopped &&
1010 !(port->IER & IER_TXRDY)) {
1011 port->IER |= IER_TXRDY;
1012 rc_out(bp, CD180_CAR, port_No(port));
1013 rc_out(bp, CD180_IER, port->IER);
1014 }
1015
1016 spin_unlock_irqrestore(&riscom_lock, flags);
1017
1018 return total;
1019}
1020
1021static int rc_put_char(struct tty_struct *tty, unsigned char ch)
1022{
1023 struct riscom_port *port = tty->driver_data;
1024 unsigned long flags;
1025 int ret = 0;
1026
1027 if (rc_paranoia_check(port, tty->name, "rc_put_char"))
1028 return 0;
1029
1030 spin_lock_irqsave(&riscom_lock, flags);
1031
1032 if (port->xmit_cnt >= SERIAL_XMIT_SIZE - 1)
1033 goto out;
1034
1035 port->port.xmit_buf[port->xmit_head++] = ch;
1036 port->xmit_head &= SERIAL_XMIT_SIZE - 1;
1037 port->xmit_cnt++;
1038 ret = 1;
1039
1040out:
1041 spin_unlock_irqrestore(&riscom_lock, flags);
1042 return ret;
1043}
1044
1045static void rc_flush_chars(struct tty_struct *tty)
1046{
1047 struct riscom_port *port = tty->driver_data;
1048 unsigned long flags;
1049
1050 if (rc_paranoia_check(port, tty->name, "rc_flush_chars"))
1051 return;
1052
1053 if (port->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped)
1054 return;
1055
1056 spin_lock_irqsave(&riscom_lock, flags);
1057
1058 port->IER |= IER_TXRDY;
1059 rc_out(port_Board(port), CD180_CAR, port_No(port));
1060 rc_out(port_Board(port), CD180_IER, port->IER);
1061
1062 spin_unlock_irqrestore(&riscom_lock, flags);
1063}
1064
1065static int rc_write_room(struct tty_struct *tty)
1066{
1067 struct riscom_port *port = tty->driver_data;
1068 int ret;
1069
1070 if (rc_paranoia_check(port, tty->name, "rc_write_room"))
1071 return 0;
1072
1073 ret = SERIAL_XMIT_SIZE - port->xmit_cnt - 1;
1074 if (ret < 0)
1075 ret = 0;
1076 return ret;
1077}
1078
1079static int rc_chars_in_buffer(struct tty_struct *tty)
1080{
1081 struct riscom_port *port = tty->driver_data;
1082
1083 if (rc_paranoia_check(port, tty->name, "rc_chars_in_buffer"))
1084 return 0;
1085
1086 return port->xmit_cnt;
1087}
1088
1089static int rc_tiocmget(struct tty_struct *tty, struct file *file)
1090{
1091 struct riscom_port *port = tty->driver_data;
1092 struct riscom_board *bp;
1093 unsigned char status;
1094 unsigned int result;
1095 unsigned long flags;
1096
1097 if (rc_paranoia_check(port, tty->name, __func__))
1098 return -ENODEV;
1099
1100 bp = port_Board(port);
1101
1102 spin_lock_irqsave(&riscom_lock, flags);
1103
1104 rc_out(bp, CD180_CAR, port_No(port));
1105 status = rc_in(bp, CD180_MSVR);
1106 result = rc_in(bp, RC_RI) & (1u << port_No(port)) ? 0 : TIOCM_RNG;
1107
1108 spin_unlock_irqrestore(&riscom_lock, flags);
1109
1110 result |= ((status & MSVR_RTS) ? TIOCM_RTS : 0)
1111 | ((status & MSVR_DTR) ? TIOCM_DTR : 0)
1112 | ((status & MSVR_CD) ? TIOCM_CAR : 0)
1113 | ((status & MSVR_DSR) ? TIOCM_DSR : 0)
1114 | ((status & MSVR_CTS) ? TIOCM_CTS : 0);
1115 return result;
1116}
1117
1118static int rc_tiocmset(struct tty_struct *tty, struct file *file,
1119 unsigned int set, unsigned int clear)
1120{
1121 struct riscom_port *port = tty->driver_data;
1122 unsigned long flags;
1123 struct riscom_board *bp;
1124
1125 if (rc_paranoia_check(port, tty->name, __func__))
1126 return -ENODEV;
1127
1128 bp = port_Board(port);
1129
1130 spin_lock_irqsave(&riscom_lock, flags);
1131
1132 if (set & TIOCM_RTS)
1133 port->MSVR |= MSVR_RTS;
1134 if (set & TIOCM_DTR)
1135 bp->DTR &= ~(1u << port_No(port));
1136
1137 if (clear & TIOCM_RTS)
1138 port->MSVR &= ~MSVR_RTS;
1139 if (clear & TIOCM_DTR)
1140 bp->DTR |= (1u << port_No(port));
1141
1142 rc_out(bp, CD180_CAR, port_No(port));
1143 rc_out(bp, CD180_MSVR, port->MSVR);
1144 rc_out(bp, RC_DTR, bp->DTR);
1145
1146 spin_unlock_irqrestore(&riscom_lock, flags);
1147
1148 return 0;
1149}
1150
1151static int rc_send_break(struct tty_struct *tty, int length)
1152{
1153 struct riscom_port *port = tty->driver_data;
1154 struct riscom_board *bp = port_Board(port);
1155 unsigned long flags;
1156
1157 if (length == 0 || length == -1)
1158 return -EOPNOTSUPP;
1159
1160 spin_lock_irqsave(&riscom_lock, flags);
1161
1162 port->break_length = RISCOM_TPS / HZ * length;
1163 port->COR2 |= COR2_ETC;
1164 port->IER |= IER_TXRDY;
1165 rc_out(bp, CD180_CAR, port_No(port));
1166 rc_out(bp, CD180_COR2, port->COR2);
1167 rc_out(bp, CD180_IER, port->IER);
1168 rc_wait_CCR(bp);
1169 rc_out(bp, CD180_CCR, CCR_CORCHG2);
1170 rc_wait_CCR(bp);
1171
1172 spin_unlock_irqrestore(&riscom_lock, flags);
1173 return 0;
1174}
1175
1176static int rc_set_serial_info(struct tty_struct *tty, struct riscom_port *port,
1177 struct serial_struct __user *newinfo)
1178{
1179 struct serial_struct tmp;
1180 struct riscom_board *bp = port_Board(port);
1181 int change_speed;
1182
1183 if (copy_from_user(&tmp, newinfo, sizeof(tmp)))
1184 return -EFAULT;
1185
1186 mutex_lock(&port->port.mutex);
1187 change_speed = ((port->port.flags & ASYNC_SPD_MASK) !=
1188 (tmp.flags & ASYNC_SPD_MASK));
1189
1190 if (!capable(CAP_SYS_ADMIN)) {
1191 if ((tmp.close_delay != port->port.close_delay) ||
1192 (tmp.closing_wait != port->port.closing_wait) ||
1193 ((tmp.flags & ~ASYNC_USR_MASK) !=
1194 (port->port.flags & ~ASYNC_USR_MASK))) {
1195 mutex_unlock(&port->port.mutex);
1196 return -EPERM;
1197 }
1198 port->port.flags = ((port->port.flags & ~ASYNC_USR_MASK) |
1199 (tmp.flags & ASYNC_USR_MASK));
1200 } else {
1201 port->port.flags = ((port->port.flags & ~ASYNC_FLAGS) |
1202 (tmp.flags & ASYNC_FLAGS));
1203 port->port.close_delay = tmp.close_delay;
1204 port->port.closing_wait = tmp.closing_wait;
1205 }
1206 if (change_speed) {
1207 unsigned long flags;
1208
1209 spin_lock_irqsave(&riscom_lock, flags);
1210 rc_change_speed(tty, bp, port);
1211 spin_unlock_irqrestore(&riscom_lock, flags);
1212 }
1213 mutex_unlock(&port->port.mutex);
1214 return 0;
1215}
1216
1217static int rc_get_serial_info(struct riscom_port *port,
1218 struct serial_struct __user *retinfo)
1219{
1220 struct serial_struct tmp;
1221 struct riscom_board *bp = port_Board(port);
1222
1223 memset(&tmp, 0, sizeof(tmp));
1224 tmp.type = PORT_CIRRUS;
1225 tmp.line = port - rc_port;
1226
1227 mutex_lock(&port->port.mutex);
1228 tmp.port = bp->base;
1229 tmp.irq = bp->irq;
1230 tmp.flags = port->port.flags;
1231 tmp.baud_base = (RC_OSCFREQ + CD180_TPC/2) / CD180_TPC;
1232 tmp.close_delay = port->port.close_delay * HZ/100;
1233 tmp.closing_wait = port->port.closing_wait * HZ/100;
1234 mutex_unlock(&port->port.mutex);
1235 tmp.xmit_fifo_size = CD180_NFIFO;
1236 return copy_to_user(retinfo, &tmp, sizeof(tmp)) ? -EFAULT : 0;
1237}
1238
1239static int rc_ioctl(struct tty_struct *tty, struct file *filp,
1240 unsigned int cmd, unsigned long arg)
1241{
1242 struct riscom_port *port = tty->driver_data;
1243 void __user *argp = (void __user *)arg;
1244 int retval;
1245
1246 if (rc_paranoia_check(port, tty->name, "rc_ioctl"))
1247 return -ENODEV;
1248
1249 switch (cmd) {
1250 case TIOCGSERIAL:
1251 retval = rc_get_serial_info(port, argp);
1252 break;
1253 case TIOCSSERIAL:
1254 retval = rc_set_serial_info(tty, port, argp);
1255 break;
1256 default:
1257 retval = -ENOIOCTLCMD;
1258 }
1259 return retval;
1260}
1261
1262static void rc_throttle(struct tty_struct *tty)
1263{
1264 struct riscom_port *port = tty->driver_data;
1265 struct riscom_board *bp;
1266 unsigned long flags;
1267
1268 if (rc_paranoia_check(port, tty->name, "rc_throttle"))
1269 return;
1270 bp = port_Board(port);
1271
1272 spin_lock_irqsave(&riscom_lock, flags);
1273 port->MSVR &= ~MSVR_RTS;
1274 rc_out(bp, CD180_CAR, port_No(port));
1275 if (I_IXOFF(tty)) {
1276 rc_wait_CCR(bp);
1277 rc_out(bp, CD180_CCR, CCR_SSCH2);
1278 rc_wait_CCR(bp);
1279 }
1280 rc_out(bp, CD180_MSVR, port->MSVR);
1281 spin_unlock_irqrestore(&riscom_lock, flags);
1282}
1283
1284static void rc_unthrottle(struct tty_struct *tty)
1285{
1286 struct riscom_port *port = tty->driver_data;
1287 struct riscom_board *bp;
1288 unsigned long flags;
1289
1290 if (rc_paranoia_check(port, tty->name, "rc_unthrottle"))
1291 return;
1292 bp = port_Board(port);
1293
1294 spin_lock_irqsave(&riscom_lock, flags);
1295 port->MSVR |= MSVR_RTS;
1296 rc_out(bp, CD180_CAR, port_No(port));
1297 if (I_IXOFF(tty)) {
1298 rc_wait_CCR(bp);
1299 rc_out(bp, CD180_CCR, CCR_SSCH1);
1300 rc_wait_CCR(bp);
1301 }
1302 rc_out(bp, CD180_MSVR, port->MSVR);
1303 spin_unlock_irqrestore(&riscom_lock, flags);
1304}
1305
1306static void rc_stop(struct tty_struct *tty)
1307{
1308 struct riscom_port *port = tty->driver_data;
1309 struct riscom_board *bp;
1310 unsigned long flags;
1311
1312 if (rc_paranoia_check(port, tty->name, "rc_stop"))
1313 return;
1314
1315 bp = port_Board(port);
1316
1317 spin_lock_irqsave(&riscom_lock, flags);
1318 port->IER &= ~IER_TXRDY;
1319 rc_out(bp, CD180_CAR, port_No(port));
1320 rc_out(bp, CD180_IER, port->IER);
1321 spin_unlock_irqrestore(&riscom_lock, flags);
1322}
1323
1324static void rc_start(struct tty_struct *tty)
1325{
1326 struct riscom_port *port = tty->driver_data;
1327 struct riscom_board *bp;
1328 unsigned long flags;
1329
1330 if (rc_paranoia_check(port, tty->name, "rc_start"))
1331 return;
1332
1333 bp = port_Board(port);
1334
1335 spin_lock_irqsave(&riscom_lock, flags);
1336
1337 if (port->xmit_cnt && port->port.xmit_buf && !(port->IER & IER_TXRDY)) {
1338 port->IER |= IER_TXRDY;
1339 rc_out(bp, CD180_CAR, port_No(port));
1340 rc_out(bp, CD180_IER, port->IER);
1341 }
1342 spin_unlock_irqrestore(&riscom_lock, flags);
1343}
1344
1345static void rc_hangup(struct tty_struct *tty)
1346{
1347 struct riscom_port *port = tty->driver_data;
1348
1349 if (rc_paranoia_check(port, tty->name, "rc_hangup"))
1350 return;
1351
1352 tty_port_hangup(&port->port);
1353}
1354
1355static void rc_set_termios(struct tty_struct *tty,
1356 struct ktermios *old_termios)
1357{
1358 struct riscom_port *port = tty->driver_data;
1359 unsigned long flags;
1360
1361 if (rc_paranoia_check(port, tty->name, "rc_set_termios"))
1362 return;
1363
1364 spin_lock_irqsave(&riscom_lock, flags);
1365 rc_change_speed(tty, port_Board(port), port);
1366 spin_unlock_irqrestore(&riscom_lock, flags);
1367
1368 if ((old_termios->c_cflag & CRTSCTS) &&
1369 !(tty->termios->c_cflag & CRTSCTS)) {
1370 tty->hw_stopped = 0;
1371 rc_start(tty);
1372 }
1373}
1374
1375static const struct tty_operations riscom_ops = {
1376 .open = rc_open,
1377 .close = rc_close,
1378 .write = rc_write,
1379 .put_char = rc_put_char,
1380 .flush_chars = rc_flush_chars,
1381 .write_room = rc_write_room,
1382 .chars_in_buffer = rc_chars_in_buffer,
1383 .flush_buffer = rc_flush_buffer,
1384 .ioctl = rc_ioctl,
1385 .throttle = rc_throttle,
1386 .unthrottle = rc_unthrottle,
1387 .set_termios = rc_set_termios,
1388 .stop = rc_stop,
1389 .start = rc_start,
1390 .hangup = rc_hangup,
1391 .tiocmget = rc_tiocmget,
1392 .tiocmset = rc_tiocmset,
1393 .break_ctl = rc_send_break,
1394};
1395
1396static const struct tty_port_operations riscom_port_ops = {
1397 .carrier_raised = carrier_raised,
1398 .dtr_rts = dtr_rts,
1399 .shutdown = rc_close_port,
1400 .activate = rc_activate_port,
1401};
1402
1403
1404static int __init rc_init_drivers(void)
1405{
1406 int error;
1407 int i;
1408
1409 riscom_driver = alloc_tty_driver(RC_NBOARD * RC_NPORT);
1410 if (!riscom_driver)
1411 return -ENOMEM;
1412
1413 riscom_driver->owner = THIS_MODULE;
1414 riscom_driver->name = "ttyL";
1415 riscom_driver->major = RISCOM8_NORMAL_MAJOR;
1416 riscom_driver->type = TTY_DRIVER_TYPE_SERIAL;
1417 riscom_driver->subtype = SERIAL_TYPE_NORMAL;
1418 riscom_driver->init_termios = tty_std_termios;
1419 riscom_driver->init_termios.c_cflag =
1420 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1421 riscom_driver->init_termios.c_ispeed = 9600;
1422 riscom_driver->init_termios.c_ospeed = 9600;
1423 riscom_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_HARDWARE_BREAK;
1424 tty_set_operations(riscom_driver, &riscom_ops);
1425 error = tty_register_driver(riscom_driver);
1426 if (error != 0) {
1427 put_tty_driver(riscom_driver);
1428 printk(KERN_ERR "rc: Couldn't register RISCom/8 driver, "
1429 "error = %d\n", error);
1430 return 1;
1431 }
1432 memset(rc_port, 0, sizeof(rc_port));
1433 for (i = 0; i < RC_NPORT * RC_NBOARD; i++) {
1434 tty_port_init(&rc_port[i].port);
1435 rc_port[i].port.ops = &riscom_port_ops;
1436 rc_port[i].magic = RISCOM8_MAGIC;
1437 }
1438 return 0;
1439}
1440
1441static void rc_release_drivers(void)
1442{
1443 tty_unregister_driver(riscom_driver);
1444 put_tty_driver(riscom_driver);
1445}
1446
1447#ifndef MODULE
1448/*
1449 * Called at boot time.
1450 *
1451 * You can specify IO base for up to RC_NBOARD cards,
1452 * using line "riscom8=0xiobase1,0xiobase2,.." at LILO prompt.
1453 * Note that there will be no probing at default
1454 * addresses in this case.
1455 *
1456 */
1457static int __init riscom8_setup(char *str)
1458{
1459 int ints[RC_NBOARD];
1460 int i;
1461
1462 str = get_options(str, ARRAY_SIZE(ints), ints);
1463
1464 for (i = 0; i < RC_NBOARD; i++) {
1465 if (i < ints[0])
1466 rc_board[i].base = ints[i+1];
1467 else
1468 rc_board[i].base = 0;
1469 }
1470 return 1;
1471}
1472
1473__setup("riscom8=", riscom8_setup);
1474#endif
1475
1476static char banner[] __initdata =
1477 KERN_INFO "rc: SDL RISCom/8 card driver v1.1, (c) D.Gorodchanin "
1478 "1994-1996.\n";
1479static char no_boards_msg[] __initdata =
1480 KERN_INFO "rc: No RISCom/8 boards detected.\n";
1481
1482/*
1483 * This routine must be called by kernel at boot time
1484 */
1485static int __init riscom8_init(void)
1486{
1487 int i;
1488 int found = 0;
1489
1490 printk(banner);
1491
1492 if (rc_init_drivers())
1493 return -EIO;
1494
1495 for (i = 0; i < RC_NBOARD; i++)
1496 if (rc_board[i].base && !rc_probe(&rc_board[i]))
1497 found++;
1498 if (!found) {
1499 rc_release_drivers();
1500 printk(no_boards_msg);
1501 return -EIO;
1502 }
1503 return 0;
1504}
1505
1506#ifdef MODULE
1507static int iobase;
1508static int iobase1;
1509static int iobase2;
1510static int iobase3;
1511module_param(iobase, int, 0);
1512module_param(iobase1, int, 0);
1513module_param(iobase2, int, 0);
1514module_param(iobase3, int, 0);
1515
1516MODULE_LICENSE("GPL");
1517MODULE_ALIAS_CHARDEV_MAJOR(RISCOM8_NORMAL_MAJOR);
1518#endif /* MODULE */
1519
1520/*
1521 * You can setup up to 4 boards (current value of RC_NBOARD)
1522 * by specifying "iobase=0xXXX iobase1=0xXXX ..." as insmod parameter.
1523 *
1524 */
1525static int __init riscom8_init_module(void)
1526{
1527#ifdef MODULE
1528 int i;
1529
1530 if (iobase || iobase1 || iobase2 || iobase3) {
1531 for (i = 0; i < RC_NBOARD; i++)
1532 rc_board[i].base = 0;
1533 }
1534
1535 if (iobase)
1536 rc_board[0].base = iobase;
1537 if (iobase1)
1538 rc_board[1].base = iobase1;
1539 if (iobase2)
1540 rc_board[2].base = iobase2;
1541 if (iobase3)
1542 rc_board[3].base = iobase3;
1543#endif /* MODULE */
1544
1545 return riscom8_init();
1546}
1547
1548static void __exit riscom8_exit_module(void)
1549{
1550 int i;
1551
1552 rc_release_drivers();
1553 for (i = 0; i < RC_NBOARD; i++)
1554 if (rc_board[i].flags & RC_BOARD_PRESENT)
1555 rc_release_io_range(&rc_board[i]);
1556
1557}
1558
1559module_init(riscom8_init_module);
1560module_exit(riscom8_exit_module);
diff --git a/drivers/char/riscom8.h b/drivers/char/riscom8.h
deleted file mode 100644
index c9876b3f9714..000000000000
--- a/drivers/char/riscom8.h
+++ /dev/null
@@ -1,91 +0,0 @@
1/*
2 * linux/drivers/char/riscom8.h -- RISCom/8 multiport serial driver.
3 *
4 * Copyright (C) 1994-1996 Dmitry Gorodchanin (pgmdsg@ibi.com)
5 *
6 * This code is loosely based on the Linux serial driver, written by
7 * Linus Torvalds, Theodore T'so and others. The RISCom/8 card
8 * programming info was obtained from various drivers for other OSes
9 * (FreeBSD, ISC, etc), but no source code from those drivers were
10 * directly included in this driver.
11 *
12 *
13 * 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.
17 *
18 * 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.
22 *
23 * 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 */
27
28#ifndef __LINUX_RISCOM8_H
29#define __LINUX_RISCOM8_H
30
31#include <linux/serial.h>
32
33#ifdef __KERNEL__
34
35#define RC_NBOARD 4
36/* NOTE: RISCom decoder recognizes 16 addresses... */
37#define RC_NPORT 8
38#define RC_BOARD(line) (((line) >> 3) & 0x07)
39#define RC_PORT(line) ((line) & (RC_NPORT - 1))
40
41/* Ticks per sec. Used for setting receiver timeout and break length */
42#define RISCOM_TPS 4000
43
44/* Yeah, after heavy testing I decided it must be 6.
45 * Sure, You can change it if needed.
46 */
47#define RISCOM_RXFIFO 6 /* Max. receiver FIFO size (1-8) */
48
49#define RISCOM8_MAGIC 0x0907
50
51#define RC_IOBASE1 0x220
52#define RC_IOBASE2 0x240
53#define RC_IOBASE3 0x250
54#define RC_IOBASE4 0x260
55
56struct riscom_board {
57 unsigned long flags;
58 unsigned short base;
59 unsigned char irq;
60 signed char count;
61 unsigned char DTR;
62};
63
64#define RC_BOARD_PRESENT 0x00000001
65#define RC_BOARD_ACTIVE 0x00000002
66
67struct riscom_port {
68 int magic;
69 struct tty_port port;
70 int baud_base;
71 int timeout;
72 int custom_divisor;
73 int xmit_head;
74 int xmit_tail;
75 int xmit_cnt;
76 short wakeup_chars;
77 short break_length;
78 unsigned char mark_mask;
79 unsigned char IER;
80 unsigned char MSVR;
81 unsigned char COR2;
82#ifdef RC_REPORT_OVERRUN
83 unsigned long overrun;
84#endif
85#ifdef RC_REPORT_FIFO
86 unsigned long hits[10];
87#endif
88};
89
90#endif /* __KERNEL__ */
91#endif /* __LINUX_RISCOM8_H */
diff --git a/drivers/char/riscom8_reg.h b/drivers/char/riscom8_reg.h
deleted file mode 100644
index a32475ed0d18..000000000000
--- a/drivers/char/riscom8_reg.h
+++ /dev/null
@@ -1,254 +0,0 @@
1/*
2 * linux/drivers/char/riscom8_reg.h -- RISCom/8 multiport serial driver.
3 */
4
5/*
6 * Definitions for RISCom/8 Async Mux card by SDL Communications, Inc.
7 */
8
9/*
10 * Address mapping between Cirrus Logic CD180 chip internal registers
11 * and ISA port addresses:
12 *
13 * CL-CD180 A6 A5 A4 A3 A2 A1 A0
14 * ISA A15 A14 A13 A12 A11 A10 A9 A8 A7 A6 A5 A4 A3 A2 A1 A0
15 */
16#define RC_TO_ISA(r) ((((r)&0x07)<<1) | (((r)&~0x07)<<7))
17
18
19/* RISCom/8 On-Board Registers (assuming address translation) */
20
21#define RC_RI 0x100 /* Ring Indicator Register (R/O) */
22#define RC_DTR 0x100 /* DTR Register (W/O) */
23#define RC_BSR 0x101 /* Board Status Register (R/O) */
24#define RC_CTOUT 0x101 /* Clear Timeout (W/O) */
25
26
27/* Board Status Register */
28
29#define RC_BSR_TOUT 0x08 /* Hardware Timeout */
30#define RC_BSR_RINT 0x04 /* Receiver Interrupt */
31#define RC_BSR_TINT 0x02 /* Transmitter Interrupt */
32#define RC_BSR_MINT 0x01 /* Modem Ctl Interrupt */
33
34
35/* On-board oscillator frequency (in Hz) */
36#define RC_OSCFREQ 9830400
37
38/* Values of choice for Interrupt ACKs */
39#define RC_ACK_MINT 0x81 /* goes to PILR1 */
40#define RC_ACK_RINT 0x82 /* goes to PILR3 */
41#define RC_ACK_TINT 0x84 /* goes to PILR2 */
42
43/* Chip ID (sorry, only one chip now) */
44#define RC_ID 0x10
45
46/* Definitions for Cirrus Logic CL-CD180 8-port async mux chip */
47
48#define CD180_NCH 8 /* Total number of channels */
49#define CD180_TPC 16 /* Ticks per character */
50#define CD180_NFIFO 8 /* TX FIFO size */
51
52
53/* Global registers */
54
55#define CD180_GIVR 0x40 /* Global Interrupt Vector Register */
56#define CD180_GICR 0x41 /* Global Interrupting Channel Register */
57#define CD180_PILR1 0x61 /* Priority Interrupt Level Register 1 */
58#define CD180_PILR2 0x62 /* Priority Interrupt Level Register 2 */
59#define CD180_PILR3 0x63 /* Priority Interrupt Level Register 3 */
60#define CD180_CAR 0x64 /* Channel Access Register */
61#define CD180_GFRCR 0x6b /* Global Firmware Revision Code Register */
62#define CD180_PPRH 0x70 /* Prescaler Period Register High */
63#define CD180_PPRL 0x71 /* Prescaler Period Register Low */
64#define CD180_RDR 0x78 /* Receiver Data Register */
65#define CD180_RCSR 0x7a /* Receiver Character Status Register */
66#define CD180_TDR 0x7b /* Transmit Data Register */
67#define CD180_EOIR 0x7f /* End of Interrupt Register */
68
69
70/* Channel Registers */
71
72#define CD180_CCR 0x01 /* Channel Command Register */
73#define CD180_IER 0x02 /* Interrupt Enable Register */
74#define CD180_COR1 0x03 /* Channel Option Register 1 */
75#define CD180_COR2 0x04 /* Channel Option Register 2 */
76#define CD180_COR3 0x05 /* Channel Option Register 3 */
77#define CD180_CCSR 0x06 /* Channel Control Status Register */
78#define CD180_RDCR 0x07 /* Receive Data Count Register */
79#define CD180_SCHR1 0x09 /* Special Character Register 1 */
80#define CD180_SCHR2 0x0a /* Special Character Register 2 */
81#define CD180_SCHR3 0x0b /* Special Character Register 3 */
82#define CD180_SCHR4 0x0c /* Special Character Register 4 */
83#define CD180_MCOR1 0x10 /* Modem Change Option 1 Register */
84#define CD180_MCOR2 0x11 /* Modem Change Option 2 Register */
85#define CD180_MCR 0x12 /* Modem Change Register */
86#define CD180_RTPR 0x18 /* Receive Timeout Period Register */
87#define CD180_MSVR 0x28 /* Modem Signal Value Register */
88#define CD180_RBPRH 0x31 /* Receive Baud Rate Period Register High */
89#define CD180_RBPRL 0x32 /* Receive Baud Rate Period Register Low */
90#define CD180_TBPRH 0x39 /* Transmit Baud Rate Period Register High */
91#define CD180_TBPRL 0x3a /* Transmit Baud Rate Period Register Low */
92
93
94/* Global Interrupt Vector Register (R/W) */
95
96#define GIVR_ITMASK 0x07 /* Interrupt type mask */
97#define GIVR_IT_MODEM 0x01 /* Modem Signal Change Interrupt */
98#define GIVR_IT_TX 0x02 /* Transmit Data Interrupt */
99#define GIVR_IT_RCV 0x03 /* Receive Good Data Interrupt */
100#define GIVR_IT_REXC 0x07 /* Receive Exception Interrupt */
101
102
103/* Global Interrupt Channel Register (R/W) */
104
105#define GICR_CHAN 0x1c /* Channel Number Mask */
106#define GICR_CHAN_OFF 2 /* Channel Number Offset */
107
108
109/* Channel Address Register (R/W) */
110
111#define CAR_CHAN 0x07 /* Channel Number Mask */
112#define CAR_A7 0x08 /* A7 Address Extension (unused) */
113
114
115/* Receive Character Status Register (R/O) */
116
117#define RCSR_TOUT 0x80 /* Rx Timeout */
118#define RCSR_SCDET 0x70 /* Special Character Detected Mask */
119#define RCSR_NO_SC 0x00 /* No Special Characters Detected */
120#define RCSR_SC_1 0x10 /* Special Char 1 (or 1 & 3) Detected */
121#define RCSR_SC_2 0x20 /* Special Char 2 (or 2 & 4) Detected */
122#define RCSR_SC_3 0x30 /* Special Char 3 Detected */
123#define RCSR_SC_4 0x40 /* Special Char 4 Detected */
124#define RCSR_BREAK 0x08 /* Break has been detected */
125#define RCSR_PE 0x04 /* Parity Error */
126#define RCSR_FE 0x02 /* Frame Error */
127#define RCSR_OE 0x01 /* Overrun Error */
128
129
130/* Channel Command Register (R/W) (commands in groups can be OR-ed) */
131
132#define CCR_HARDRESET 0x81 /* Reset the chip */
133
134#define CCR_SOFTRESET 0x80 /* Soft Channel Reset */
135
136#define CCR_CORCHG1 0x42 /* Channel Option Register 1 Changed */
137#define CCR_CORCHG2 0x44 /* Channel Option Register 2 Changed */
138#define CCR_CORCHG3 0x48 /* Channel Option Register 3 Changed */
139
140#define CCR_SSCH1 0x21 /* Send Special Character 1 */
141
142#define CCR_SSCH2 0x22 /* Send Special Character 2 */
143
144#define CCR_SSCH3 0x23 /* Send Special Character 3 */
145
146#define CCR_SSCH4 0x24 /* Send Special Character 4 */
147
148#define CCR_TXEN 0x18 /* Enable Transmitter */
149#define CCR_RXEN 0x12 /* Enable Receiver */
150
151#define CCR_TXDIS 0x14 /* Disable Transmitter */
152#define CCR_RXDIS 0x11 /* Disable Receiver */
153
154
155/* Interrupt Enable Register (R/W) */
156
157#define IER_DSR 0x80 /* Enable interrupt on DSR change */
158#define IER_CD 0x40 /* Enable interrupt on CD change */
159#define IER_CTS 0x20 /* Enable interrupt on CTS change */
160#define IER_RXD 0x10 /* Enable interrupt on Receive Data */
161#define IER_RXSC 0x08 /* Enable interrupt on Receive Spec. Char */
162#define IER_TXRDY 0x04 /* Enable interrupt on TX FIFO empty */
163#define IER_TXEMPTY 0x02 /* Enable interrupt on TX completely empty */
164#define IER_RET 0x01 /* Enable interrupt on RX Exc. Timeout */
165
166
167/* Channel Option Register 1 (R/W) */
168
169#define COR1_ODDP 0x80 /* Odd Parity */
170#define COR1_PARMODE 0x60 /* Parity Mode mask */
171#define COR1_NOPAR 0x00 /* No Parity */
172#define COR1_FORCEPAR 0x20 /* Force Parity */
173#define COR1_NORMPAR 0x40 /* Normal Parity */
174#define COR1_IGNORE 0x10 /* Ignore Parity on RX */
175#define COR1_STOPBITS 0x0c /* Number of Stop Bits */
176#define COR1_1SB 0x00 /* 1 Stop Bit */
177#define COR1_15SB 0x04 /* 1.5 Stop Bits */
178#define COR1_2SB 0x08 /* 2 Stop Bits */
179#define COR1_CHARLEN 0x03 /* Character Length */
180#define COR1_5BITS 0x00 /* 5 bits */
181#define COR1_6BITS 0x01 /* 6 bits */
182#define COR1_7BITS 0x02 /* 7 bits */
183#define COR1_8BITS 0x03 /* 8 bits */
184
185
186/* Channel Option Register 2 (R/W) */
187
188#define COR2_IXM 0x80 /* Implied XON mode */
189#define COR2_TXIBE 0x40 /* Enable In-Band (XON/XOFF) Flow Control */
190#define COR2_ETC 0x20 /* Embedded Tx Commands Enable */
191#define COR2_LLM 0x10 /* Local Loopback Mode */
192#define COR2_RLM 0x08 /* Remote Loopback Mode */
193#define COR2_RTSAO 0x04 /* RTS Automatic Output Enable */
194#define COR2_CTSAE 0x02 /* CTS Automatic Enable */
195#define COR2_DSRAE 0x01 /* DSR Automatic Enable */
196
197
198/* Channel Option Register 3 (R/W) */
199
200#define COR3_XONCH 0x80 /* XON is a pair of characters (1 & 3) */
201#define COR3_XOFFCH 0x40 /* XOFF is a pair of characters (2 & 4) */
202#define COR3_FCT 0x20 /* Flow-Control Transparency Mode */
203#define COR3_SCDE 0x10 /* Special Character Detection Enable */
204#define COR3_RXTH 0x0f /* RX FIFO Threshold value (1-8) */
205
206
207/* Channel Control Status Register (R/O) */
208
209#define CCSR_RXEN 0x80 /* Receiver Enabled */
210#define CCSR_RXFLOFF 0x40 /* Receive Flow Off (XOFF was sent) */
211#define CCSR_RXFLON 0x20 /* Receive Flow On (XON was sent) */
212#define CCSR_TXEN 0x08 /* Transmitter Enabled */
213#define CCSR_TXFLOFF 0x04 /* Transmit Flow Off (got XOFF) */
214#define CCSR_TXFLON 0x02 /* Transmit Flow On (got XON) */
215
216
217/* Modem Change Option Register 1 (R/W) */
218
219#define MCOR1_DSRZD 0x80 /* Detect 0->1 transition of DSR */
220#define MCOR1_CDZD 0x40 /* Detect 0->1 transition of CD */
221#define MCOR1_CTSZD 0x20 /* Detect 0->1 transition of CTS */
222#define MCOR1_DTRTH 0x0f /* Auto DTR flow control Threshold (1-8) */
223#define MCOR1_NODTRFC 0x0 /* Automatic DTR flow control disabled */
224
225
226/* Modem Change Option Register 2 (R/W) */
227
228#define MCOR2_DSROD 0x80 /* Detect 1->0 transition of DSR */
229#define MCOR2_CDOD 0x40 /* Detect 1->0 transition of CD */
230#define MCOR2_CTSOD 0x20 /* Detect 1->0 transition of CTS */
231
232
233/* Modem Change Register (R/W) */
234
235#define MCR_DSRCHG 0x80 /* DSR Changed */
236#define MCR_CDCHG 0x40 /* CD Changed */
237#define MCR_CTSCHG 0x20 /* CTS Changed */
238
239
240/* Modem Signal Value Register (R/W) */
241
242#define MSVR_DSR 0x80 /* Current state of DSR input */
243#define MSVR_CD 0x40 /* Current state of CD input */
244#define MSVR_CTS 0x20 /* Current state of CTS input */
245#define MSVR_DTR 0x02 /* Current state of DTR output */
246#define MSVR_RTS 0x01 /* Current state of RTS output */
247
248
249/* Escape characters */
250
251#define CD180_C_ESC 0x00 /* Escape character */
252#define CD180_C_SBRK 0x81 /* Start sending BREAK */
253#define CD180_C_DELAY 0x82 /* Delay output */
254#define CD180_C_EBRK 0x83 /* Stop sending BREAK */
diff --git a/drivers/char/rocket.c b/drivers/char/rocket.c
deleted file mode 100644
index 7c79d243acc9..000000000000
--- a/drivers/char/rocket.c
+++ /dev/null
@@ -1,3196 +0,0 @@
1/*
2 * RocketPort device driver for Linux
3 *
4 * Written by Theodore Ts'o, 1995, 1996, 1997, 1998, 1999, 2000.
5 *
6 * Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2003 by Comtrol, Inc.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of the
11 * License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23/*
24 * Kernel Synchronization:
25 *
26 * This driver has 2 kernel control paths - exception handlers (calls into the driver
27 * from user mode) and the timer bottom half (tasklet). This is a polled driver, interrupts
28 * are not used.
29 *
30 * Critical data:
31 * - rp_table[], accessed through passed "info" pointers, is a global (static) array of
32 * serial port state information and the xmit_buf circular buffer. Protected by
33 * a per port spinlock.
34 * - xmit_flags[], an array of ints indexed by line (port) number, indicating that there
35 * is data to be transmitted. Protected by atomic bit operations.
36 * - rp_num_ports, int indicating number of open ports, protected by atomic operations.
37 *
38 * rp_write() and rp_write_char() functions use a per port semaphore to protect against
39 * simultaneous access to the same port by more than one process.
40 */
41
42/****** Defines ******/
43#define ROCKET_PARANOIA_CHECK
44#define ROCKET_DISABLE_SIMUSAGE
45
46#undef ROCKET_SOFT_FLOW
47#undef ROCKET_DEBUG_OPEN
48#undef ROCKET_DEBUG_INTR
49#undef ROCKET_DEBUG_WRITE
50#undef ROCKET_DEBUG_FLOW
51#undef ROCKET_DEBUG_THROTTLE
52#undef ROCKET_DEBUG_WAIT_UNTIL_SENT
53#undef ROCKET_DEBUG_RECEIVE
54#undef ROCKET_DEBUG_HANGUP
55#undef REV_PCI_ORDER
56#undef ROCKET_DEBUG_IO
57
58#define POLL_PERIOD HZ/100 /* Polling period .01 seconds (10ms) */
59
60/****** Kernel includes ******/
61
62#include <linux/module.h>
63#include <linux/errno.h>
64#include <linux/major.h>
65#include <linux/kernel.h>
66#include <linux/signal.h>
67#include <linux/slab.h>
68#include <linux/mm.h>
69#include <linux/sched.h>
70#include <linux/timer.h>
71#include <linux/interrupt.h>
72#include <linux/tty.h>
73#include <linux/tty_driver.h>
74#include <linux/tty_flip.h>
75#include <linux/serial.h>
76#include <linux/string.h>
77#include <linux/fcntl.h>
78#include <linux/ptrace.h>
79#include <linux/mutex.h>
80#include <linux/ioport.h>
81#include <linux/delay.h>
82#include <linux/completion.h>
83#include <linux/wait.h>
84#include <linux/pci.h>
85#include <linux/uaccess.h>
86#include <asm/atomic.h>
87#include <asm/unaligned.h>
88#include <linux/bitops.h>
89#include <linux/spinlock.h>
90#include <linux/init.h>
91
92/****** RocketPort includes ******/
93
94#include "rocket_int.h"
95#include "rocket.h"
96
97#define ROCKET_VERSION "2.09"
98#define ROCKET_DATE "12-June-2003"
99
100/****** RocketPort Local Variables ******/
101
102static void rp_do_poll(unsigned long dummy);
103
104static struct tty_driver *rocket_driver;
105
106static struct rocket_version driver_version = {
107 ROCKET_VERSION, ROCKET_DATE
108};
109
110static struct r_port *rp_table[MAX_RP_PORTS]; /* The main repository of serial port state information. */
111static unsigned int xmit_flags[NUM_BOARDS]; /* Bit significant, indicates port had data to transmit. */
112 /* eg. Bit 0 indicates port 0 has xmit data, ... */
113static atomic_t rp_num_ports_open; /* Number of serial ports open */
114static DEFINE_TIMER(rocket_timer, rp_do_poll, 0, 0);
115
116static unsigned long board1; /* ISA addresses, retrieved from rocketport.conf */
117static unsigned long board2;
118static unsigned long board3;
119static unsigned long board4;
120static unsigned long controller;
121static int support_low_speed;
122static unsigned long modem1;
123static unsigned long modem2;
124static unsigned long modem3;
125static unsigned long modem4;
126static unsigned long pc104_1[8];
127static unsigned long pc104_2[8];
128static unsigned long pc104_3[8];
129static unsigned long pc104_4[8];
130static unsigned long *pc104[4] = { pc104_1, pc104_2, pc104_3, pc104_4 };
131
132static int rp_baud_base[NUM_BOARDS]; /* Board config info (Someday make a per-board structure) */
133static unsigned long rcktpt_io_addr[NUM_BOARDS];
134static int rcktpt_type[NUM_BOARDS];
135static int is_PCI[NUM_BOARDS];
136static rocketModel_t rocketModel[NUM_BOARDS];
137static int max_board;
138static const struct tty_port_operations rocket_port_ops;
139
140/*
141 * The following arrays define the interrupt bits corresponding to each AIOP.
142 * These bits are different between the ISA and regular PCI boards and the
143 * Universal PCI boards.
144 */
145
146static Word_t aiop_intr_bits[AIOP_CTL_SIZE] = {
147 AIOP_INTR_BIT_0,
148 AIOP_INTR_BIT_1,
149 AIOP_INTR_BIT_2,
150 AIOP_INTR_BIT_3
151};
152
153static Word_t upci_aiop_intr_bits[AIOP_CTL_SIZE] = {
154 UPCI_AIOP_INTR_BIT_0,
155 UPCI_AIOP_INTR_BIT_1,
156 UPCI_AIOP_INTR_BIT_2,
157 UPCI_AIOP_INTR_BIT_3
158};
159
160static Byte_t RData[RDATASIZE] = {
161 0x00, 0x09, 0xf6, 0x82,
162 0x02, 0x09, 0x86, 0xfb,
163 0x04, 0x09, 0x00, 0x0a,
164 0x06, 0x09, 0x01, 0x0a,
165 0x08, 0x09, 0x8a, 0x13,
166 0x0a, 0x09, 0xc5, 0x11,
167 0x0c, 0x09, 0x86, 0x85,
168 0x0e, 0x09, 0x20, 0x0a,
169 0x10, 0x09, 0x21, 0x0a,
170 0x12, 0x09, 0x41, 0xff,
171 0x14, 0x09, 0x82, 0x00,
172 0x16, 0x09, 0x82, 0x7b,
173 0x18, 0x09, 0x8a, 0x7d,
174 0x1a, 0x09, 0x88, 0x81,
175 0x1c, 0x09, 0x86, 0x7a,
176 0x1e, 0x09, 0x84, 0x81,
177 0x20, 0x09, 0x82, 0x7c,
178 0x22, 0x09, 0x0a, 0x0a
179};
180
181static Byte_t RRegData[RREGDATASIZE] = {
182 0x00, 0x09, 0xf6, 0x82, /* 00: Stop Rx processor */
183 0x08, 0x09, 0x8a, 0x13, /* 04: Tx software flow control */
184 0x0a, 0x09, 0xc5, 0x11, /* 08: XON char */
185 0x0c, 0x09, 0x86, 0x85, /* 0c: XANY */
186 0x12, 0x09, 0x41, 0xff, /* 10: Rx mask char */
187 0x14, 0x09, 0x82, 0x00, /* 14: Compare/Ignore #0 */
188 0x16, 0x09, 0x82, 0x7b, /* 18: Compare #1 */
189 0x18, 0x09, 0x8a, 0x7d, /* 1c: Compare #2 */
190 0x1a, 0x09, 0x88, 0x81, /* 20: Interrupt #1 */
191 0x1c, 0x09, 0x86, 0x7a, /* 24: Ignore/Replace #1 */
192 0x1e, 0x09, 0x84, 0x81, /* 28: Interrupt #2 */
193 0x20, 0x09, 0x82, 0x7c, /* 2c: Ignore/Replace #2 */
194 0x22, 0x09, 0x0a, 0x0a /* 30: Rx FIFO Enable */
195};
196
197static CONTROLLER_T sController[CTL_SIZE] = {
198 {-1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, {0, 0, 0, 0},
199 {0, 0, 0, 0}, {-1, -1, -1, -1}, {0, 0, 0, 0}},
200 {-1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, {0, 0, 0, 0},
201 {0, 0, 0, 0}, {-1, -1, -1, -1}, {0, 0, 0, 0}},
202 {-1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, {0, 0, 0, 0},
203 {0, 0, 0, 0}, {-1, -1, -1, -1}, {0, 0, 0, 0}},
204 {-1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, {0, 0, 0, 0},
205 {0, 0, 0, 0}, {-1, -1, -1, -1}, {0, 0, 0, 0}}
206};
207
208static Byte_t sBitMapClrTbl[8] = {
209 0xfe, 0xfd, 0xfb, 0xf7, 0xef, 0xdf, 0xbf, 0x7f
210};
211
212static Byte_t sBitMapSetTbl[8] = {
213 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80
214};
215
216static int sClockPrescale = 0x14;
217
218/*
219 * Line number is the ttySIx number (x), the Minor number. We
220 * assign them sequentially, starting at zero. The following
221 * array keeps track of the line number assigned to a given board/aiop/channel.
222 */
223static unsigned char lineNumbers[MAX_RP_PORTS];
224static unsigned long nextLineNumber;
225
226/***** RocketPort Static Prototypes *********/
227static int __init init_ISA(int i);
228static void rp_wait_until_sent(struct tty_struct *tty, int timeout);
229static void rp_flush_buffer(struct tty_struct *tty);
230static void rmSpeakerReset(CONTROLLER_T * CtlP, unsigned long model);
231static unsigned char GetLineNumber(int ctrl, int aiop, int ch);
232static unsigned char SetLineNumber(int ctrl, int aiop, int ch);
233static void rp_start(struct tty_struct *tty);
234static int sInitChan(CONTROLLER_T * CtlP, CHANNEL_T * ChP, int AiopNum,
235 int ChanNum);
236static void sSetInterfaceMode(CHANNEL_T * ChP, Byte_t mode);
237static void sFlushRxFIFO(CHANNEL_T * ChP);
238static void sFlushTxFIFO(CHANNEL_T * ChP);
239static void sEnInterrupts(CHANNEL_T * ChP, Word_t Flags);
240static void sDisInterrupts(CHANNEL_T * ChP, Word_t Flags);
241static void sModemReset(CONTROLLER_T * CtlP, int chan, int on);
242static void sPCIModemReset(CONTROLLER_T * CtlP, int chan, int on);
243static int sWriteTxPrioByte(CHANNEL_T * ChP, Byte_t Data);
244static int sPCIInitController(CONTROLLER_T * CtlP, int CtlNum,
245 ByteIO_t * AiopIOList, int AiopIOListSize,
246 WordIO_t ConfigIO, int IRQNum, Byte_t Frequency,
247 int PeriodicOnly, int altChanRingIndicator,
248 int UPCIRingInd);
249static int sInitController(CONTROLLER_T * CtlP, int CtlNum, ByteIO_t MudbacIO,
250 ByteIO_t * AiopIOList, int AiopIOListSize,
251 int IRQNum, Byte_t Frequency, int PeriodicOnly);
252static int sReadAiopID(ByteIO_t io);
253static int sReadAiopNumChan(WordIO_t io);
254
255MODULE_AUTHOR("Theodore Ts'o");
256MODULE_DESCRIPTION("Comtrol RocketPort driver");
257module_param(board1, ulong, 0);
258MODULE_PARM_DESC(board1, "I/O port for (ISA) board #1");
259module_param(board2, ulong, 0);
260MODULE_PARM_DESC(board2, "I/O port for (ISA) board #2");
261module_param(board3, ulong, 0);
262MODULE_PARM_DESC(board3, "I/O port for (ISA) board #3");
263module_param(board4, ulong, 0);
264MODULE_PARM_DESC(board4, "I/O port for (ISA) board #4");
265module_param(controller, ulong, 0);
266MODULE_PARM_DESC(controller, "I/O port for (ISA) rocketport controller");
267module_param(support_low_speed, bool, 0);
268MODULE_PARM_DESC(support_low_speed, "1 means support 50 baud, 0 means support 460400 baud");
269module_param(modem1, ulong, 0);
270MODULE_PARM_DESC(modem1, "1 means (ISA) board #1 is a RocketModem");
271module_param(modem2, ulong, 0);
272MODULE_PARM_DESC(modem2, "1 means (ISA) board #2 is a RocketModem");
273module_param(modem3, ulong, 0);
274MODULE_PARM_DESC(modem3, "1 means (ISA) board #3 is a RocketModem");
275module_param(modem4, ulong, 0);
276MODULE_PARM_DESC(modem4, "1 means (ISA) board #4 is a RocketModem");
277module_param_array(pc104_1, ulong, NULL, 0);
278MODULE_PARM_DESC(pc104_1, "set interface types for ISA(PC104) board #1 (e.g. pc104_1=232,232,485,485,...");
279module_param_array(pc104_2, ulong, NULL, 0);
280MODULE_PARM_DESC(pc104_2, "set interface types for ISA(PC104) board #2 (e.g. pc104_2=232,232,485,485,...");
281module_param_array(pc104_3, ulong, NULL, 0);
282MODULE_PARM_DESC(pc104_3, "set interface types for ISA(PC104) board #3 (e.g. pc104_3=232,232,485,485,...");
283module_param_array(pc104_4, ulong, NULL, 0);
284MODULE_PARM_DESC(pc104_4, "set interface types for ISA(PC104) board #4 (e.g. pc104_4=232,232,485,485,...");
285
286static int rp_init(void);
287static void rp_cleanup_module(void);
288
289module_init(rp_init);
290module_exit(rp_cleanup_module);
291
292
293MODULE_LICENSE("Dual BSD/GPL");
294
295/*************************************************************************/
296/* Module code starts here */
297
298static inline int rocket_paranoia_check(struct r_port *info,
299 const char *routine)
300{
301#ifdef ROCKET_PARANOIA_CHECK
302 if (!info)
303 return 1;
304 if (info->magic != RPORT_MAGIC) {
305 printk(KERN_WARNING "Warning: bad magic number for rocketport "
306 "struct in %s\n", routine);
307 return 1;
308 }
309#endif
310 return 0;
311}
312
313
314/* Serial port receive data function. Called (from timer poll) when an AIOPIC signals
315 * that receive data is present on a serial port. Pulls data from FIFO, moves it into the
316 * tty layer.
317 */
318static void rp_do_receive(struct r_port *info,
319 struct tty_struct *tty,
320 CHANNEL_t * cp, unsigned int ChanStatus)
321{
322 unsigned int CharNStat;
323 int ToRecv, wRecv, space;
324 unsigned char *cbuf;
325
326 ToRecv = sGetRxCnt(cp);
327#ifdef ROCKET_DEBUG_INTR
328 printk(KERN_INFO "rp_do_receive(%d)...\n", ToRecv);
329#endif
330 if (ToRecv == 0)
331 return;
332
333 /*
334 * if status indicates there are errored characters in the
335 * FIFO, then enter status mode (a word in FIFO holds
336 * character and status).
337 */
338 if (ChanStatus & (RXFOVERFL | RXBREAK | RXFRAME | RXPARITY)) {
339 if (!(ChanStatus & STATMODE)) {
340#ifdef ROCKET_DEBUG_RECEIVE
341 printk(KERN_INFO "Entering STATMODE...\n");
342#endif
343 ChanStatus |= STATMODE;
344 sEnRxStatusMode(cp);
345 }
346 }
347
348 /*
349 * if we previously entered status mode, then read down the
350 * FIFO one word at a time, pulling apart the character and
351 * the status. Update error counters depending on status
352 */
353 if (ChanStatus & STATMODE) {
354#ifdef ROCKET_DEBUG_RECEIVE
355 printk(KERN_INFO "Ignore %x, read %x...\n",
356 info->ignore_status_mask, info->read_status_mask);
357#endif
358 while (ToRecv) {
359 char flag;
360
361 CharNStat = sInW(sGetTxRxDataIO(cp));
362#ifdef ROCKET_DEBUG_RECEIVE
363 printk(KERN_INFO "%x...\n", CharNStat);
364#endif
365 if (CharNStat & STMBREAKH)
366 CharNStat &= ~(STMFRAMEH | STMPARITYH);
367 if (CharNStat & info->ignore_status_mask) {
368 ToRecv--;
369 continue;
370 }
371 CharNStat &= info->read_status_mask;
372 if (CharNStat & STMBREAKH)
373 flag = TTY_BREAK;
374 else if (CharNStat & STMPARITYH)
375 flag = TTY_PARITY;
376 else if (CharNStat & STMFRAMEH)
377 flag = TTY_FRAME;
378 else if (CharNStat & STMRCVROVRH)
379 flag = TTY_OVERRUN;
380 else
381 flag = TTY_NORMAL;
382 tty_insert_flip_char(tty, CharNStat & 0xff, flag);
383 ToRecv--;
384 }
385
386 /*
387 * after we've emptied the FIFO in status mode, turn
388 * status mode back off
389 */
390 if (sGetRxCnt(cp) == 0) {
391#ifdef ROCKET_DEBUG_RECEIVE
392 printk(KERN_INFO "Status mode off.\n");
393#endif
394 sDisRxStatusMode(cp);
395 }
396 } else {
397 /*
398 * we aren't in status mode, so read down the FIFO two
399 * characters at time by doing repeated word IO
400 * transfer.
401 */
402 space = tty_prepare_flip_string(tty, &cbuf, ToRecv);
403 if (space < ToRecv) {
404#ifdef ROCKET_DEBUG_RECEIVE
405 printk(KERN_INFO "rp_do_receive:insufficient space ToRecv=%d space=%d\n", ToRecv, space);
406#endif
407 if (space <= 0)
408 return;
409 ToRecv = space;
410 }
411 wRecv = ToRecv >> 1;
412 if (wRecv)
413 sInStrW(sGetTxRxDataIO(cp), (unsigned short *) cbuf, wRecv);
414 if (ToRecv & 1)
415 cbuf[ToRecv - 1] = sInB(sGetTxRxDataIO(cp));
416 }
417 /* Push the data up to the tty layer */
418 tty_flip_buffer_push(tty);
419}
420
421/*
422 * Serial port transmit data function. Called from the timer polling loop as a
423 * result of a bit set in xmit_flags[], indicating data (from the tty layer) is ready
424 * to be sent out the serial port. Data is buffered in rp_table[line].xmit_buf, it is
425 * moved to the port's xmit FIFO. *info is critical data, protected by spinlocks.
426 */
427static void rp_do_transmit(struct r_port *info)
428{
429 int c;
430 CHANNEL_t *cp = &info->channel;
431 struct tty_struct *tty;
432 unsigned long flags;
433
434#ifdef ROCKET_DEBUG_INTR
435 printk(KERN_DEBUG "%s\n", __func__);
436#endif
437 if (!info)
438 return;
439 tty = tty_port_tty_get(&info->port);
440
441 if (tty == NULL) {
442 printk(KERN_WARNING "rp: WARNING %s called with tty==NULL\n", __func__);
443 clear_bit((info->aiop * 8) + info->chan, (void *) &xmit_flags[info->board]);
444 return;
445 }
446
447 spin_lock_irqsave(&info->slock, flags);
448 info->xmit_fifo_room = TXFIFO_SIZE - sGetTxCnt(cp);
449
450 /* Loop sending data to FIFO until done or FIFO full */
451 while (1) {
452 if (tty->stopped || tty->hw_stopped)
453 break;
454 c = min(info->xmit_fifo_room, info->xmit_cnt);
455 c = min(c, XMIT_BUF_SIZE - info->xmit_tail);
456 if (c <= 0 || info->xmit_fifo_room <= 0)
457 break;
458 sOutStrW(sGetTxRxDataIO(cp), (unsigned short *) (info->xmit_buf + info->xmit_tail), c / 2);
459 if (c & 1)
460 sOutB(sGetTxRxDataIO(cp), info->xmit_buf[info->xmit_tail + c - 1]);
461 info->xmit_tail += c;
462 info->xmit_tail &= XMIT_BUF_SIZE - 1;
463 info->xmit_cnt -= c;
464 info->xmit_fifo_room -= c;
465#ifdef ROCKET_DEBUG_INTR
466 printk(KERN_INFO "tx %d chars...\n", c);
467#endif
468 }
469
470 if (info->xmit_cnt == 0)
471 clear_bit((info->aiop * 8) + info->chan, (void *) &xmit_flags[info->board]);
472
473 if (info->xmit_cnt < WAKEUP_CHARS) {
474 tty_wakeup(tty);
475#ifdef ROCKETPORT_HAVE_POLL_WAIT
476 wake_up_interruptible(&tty->poll_wait);
477#endif
478 }
479
480 spin_unlock_irqrestore(&info->slock, flags);
481 tty_kref_put(tty);
482
483#ifdef ROCKET_DEBUG_INTR
484 printk(KERN_DEBUG "(%d,%d,%d,%d)...\n", info->xmit_cnt, info->xmit_head,
485 info->xmit_tail, info->xmit_fifo_room);
486#endif
487}
488
489/*
490 * Called when a serial port signals it has read data in it's RX FIFO.
491 * It checks what interrupts are pending and services them, including
492 * receiving serial data.
493 */
494static void rp_handle_port(struct r_port *info)
495{
496 CHANNEL_t *cp;
497 struct tty_struct *tty;
498 unsigned int IntMask, ChanStatus;
499
500 if (!info)
501 return;
502
503 if ((info->port.flags & ASYNC_INITIALIZED) == 0) {
504 printk(KERN_WARNING "rp: WARNING: rp_handle_port called with "
505 "info->flags & NOT_INIT\n");
506 return;
507 }
508 tty = tty_port_tty_get(&info->port);
509 if (!tty) {
510 printk(KERN_WARNING "rp: WARNING: rp_handle_port called with "
511 "tty==NULL\n");
512 return;
513 }
514 cp = &info->channel;
515
516 IntMask = sGetChanIntID(cp) & info->intmask;
517#ifdef ROCKET_DEBUG_INTR
518 printk(KERN_INFO "rp_interrupt %02x...\n", IntMask);
519#endif
520 ChanStatus = sGetChanStatus(cp);
521 if (IntMask & RXF_TRIG) { /* Rx FIFO trigger level */
522 rp_do_receive(info, tty, cp, ChanStatus);
523 }
524 if (IntMask & DELTA_CD) { /* CD change */
525#if (defined(ROCKET_DEBUG_OPEN) || defined(ROCKET_DEBUG_INTR) || defined(ROCKET_DEBUG_HANGUP))
526 printk(KERN_INFO "ttyR%d CD now %s...\n", info->line,
527 (ChanStatus & CD_ACT) ? "on" : "off");
528#endif
529 if (!(ChanStatus & CD_ACT) && info->cd_status) {
530#ifdef ROCKET_DEBUG_HANGUP
531 printk(KERN_INFO "CD drop, calling hangup.\n");
532#endif
533 tty_hangup(tty);
534 }
535 info->cd_status = (ChanStatus & CD_ACT) ? 1 : 0;
536 wake_up_interruptible(&info->port.open_wait);
537 }
538#ifdef ROCKET_DEBUG_INTR
539 if (IntMask & DELTA_CTS) { /* CTS change */
540 printk(KERN_INFO "CTS change...\n");
541 }
542 if (IntMask & DELTA_DSR) { /* DSR change */
543 printk(KERN_INFO "DSR change...\n");
544 }
545#endif
546 tty_kref_put(tty);
547}
548
549/*
550 * The top level polling routine. Repeats every 1/100 HZ (10ms).
551 */
552static void rp_do_poll(unsigned long dummy)
553{
554 CONTROLLER_t *ctlp;
555 int ctrl, aiop, ch, line;
556 unsigned int xmitmask, i;
557 unsigned int CtlMask;
558 unsigned char AiopMask;
559 Word_t bit;
560
561 /* Walk through all the boards (ctrl's) */
562 for (ctrl = 0; ctrl < max_board; ctrl++) {
563 if (rcktpt_io_addr[ctrl] <= 0)
564 continue;
565
566 /* Get a ptr to the board's control struct */
567 ctlp = sCtlNumToCtlPtr(ctrl);
568
569 /* Get the interrupt status from the board */
570#ifdef CONFIG_PCI
571 if (ctlp->BusType == isPCI)
572 CtlMask = sPCIGetControllerIntStatus(ctlp);
573 else
574#endif
575 CtlMask = sGetControllerIntStatus(ctlp);
576
577 /* Check if any AIOP read bits are set */
578 for (aiop = 0; CtlMask; aiop++) {
579 bit = ctlp->AiopIntrBits[aiop];
580 if (CtlMask & bit) {
581 CtlMask &= ~bit;
582 AiopMask = sGetAiopIntStatus(ctlp, aiop);
583
584 /* Check if any port read bits are set */
585 for (ch = 0; AiopMask; AiopMask >>= 1, ch++) {
586 if (AiopMask & 1) {
587
588 /* Get the line number (/dev/ttyRx number). */
589 /* Read the data from the port. */
590 line = GetLineNumber(ctrl, aiop, ch);
591 rp_handle_port(rp_table[line]);
592 }
593 }
594 }
595 }
596
597 xmitmask = xmit_flags[ctrl];
598
599 /*
600 * xmit_flags contains bit-significant flags, indicating there is data
601 * to xmit on the port. Bit 0 is port 0 on this board, bit 1 is port
602 * 1, ... (32 total possible). The variable i has the aiop and ch
603 * numbers encoded in it (port 0-7 are aiop0, 8-15 are aiop1, etc).
604 */
605 if (xmitmask) {
606 for (i = 0; i < rocketModel[ctrl].numPorts; i++) {
607 if (xmitmask & (1 << i)) {
608 aiop = (i & 0x18) >> 3;
609 ch = i & 0x07;
610 line = GetLineNumber(ctrl, aiop, ch);
611 rp_do_transmit(rp_table[line]);
612 }
613 }
614 }
615 }
616
617 /*
618 * Reset the timer so we get called at the next clock tick (10ms).
619 */
620 if (atomic_read(&rp_num_ports_open))
621 mod_timer(&rocket_timer, jiffies + POLL_PERIOD);
622}
623
624/*
625 * Initializes the r_port structure for a port, as well as enabling the port on
626 * the board.
627 * Inputs: board, aiop, chan numbers
628 */
629static void init_r_port(int board, int aiop, int chan, struct pci_dev *pci_dev)
630{
631 unsigned rocketMode;
632 struct r_port *info;
633 int line;
634 CONTROLLER_T *ctlp;
635
636 /* Get the next available line number */
637 line = SetLineNumber(board, aiop, chan);
638
639 ctlp = sCtlNumToCtlPtr(board);
640
641 /* Get a r_port struct for the port, fill it in and save it globally, indexed by line number */
642 info = kzalloc(sizeof (struct r_port), GFP_KERNEL);
643 if (!info) {
644 printk(KERN_ERR "Couldn't allocate info struct for line #%d\n",
645 line);
646 return;
647 }
648
649 info->magic = RPORT_MAGIC;
650 info->line = line;
651 info->ctlp = ctlp;
652 info->board = board;
653 info->aiop = aiop;
654 info->chan = chan;
655 tty_port_init(&info->port);
656 info->port.ops = &rocket_port_ops;
657 init_completion(&info->close_wait);
658 info->flags &= ~ROCKET_MODE_MASK;
659 switch (pc104[board][line]) {
660 case 422:
661 info->flags |= ROCKET_MODE_RS422;
662 break;
663 case 485:
664 info->flags |= ROCKET_MODE_RS485;
665 break;
666 case 232:
667 default:
668 info->flags |= ROCKET_MODE_RS232;
669 break;
670 }
671
672 info->intmask = RXF_TRIG | TXFIFO_MT | SRC_INT | DELTA_CD | DELTA_CTS | DELTA_DSR;
673 if (sInitChan(ctlp, &info->channel, aiop, chan) == 0) {
674 printk(KERN_ERR "RocketPort sInitChan(%d, %d, %d) failed!\n",
675 board, aiop, chan);
676 kfree(info);
677 return;
678 }
679
680 rocketMode = info->flags & ROCKET_MODE_MASK;
681
682 if ((info->flags & ROCKET_RTS_TOGGLE) || (rocketMode == ROCKET_MODE_RS485))
683 sEnRTSToggle(&info->channel);
684 else
685 sDisRTSToggle(&info->channel);
686
687 if (ctlp->boardType == ROCKET_TYPE_PC104) {
688 switch (rocketMode) {
689 case ROCKET_MODE_RS485:
690 sSetInterfaceMode(&info->channel, InterfaceModeRS485);
691 break;
692 case ROCKET_MODE_RS422:
693 sSetInterfaceMode(&info->channel, InterfaceModeRS422);
694 break;
695 case ROCKET_MODE_RS232:
696 default:
697 if (info->flags & ROCKET_RTS_TOGGLE)
698 sSetInterfaceMode(&info->channel, InterfaceModeRS232T);
699 else
700 sSetInterfaceMode(&info->channel, InterfaceModeRS232);
701 break;
702 }
703 }
704 spin_lock_init(&info->slock);
705 mutex_init(&info->write_mtx);
706 rp_table[line] = info;
707 tty_register_device(rocket_driver, line, pci_dev ? &pci_dev->dev :
708 NULL);
709}
710
711/*
712 * Configures a rocketport port according to its termio settings. Called from
713 * user mode into the driver (exception handler). *info CD manipulation is spinlock protected.
714 */
715static void configure_r_port(struct tty_struct *tty, struct r_port *info,
716 struct ktermios *old_termios)
717{
718 unsigned cflag;
719 unsigned long flags;
720 unsigned rocketMode;
721 int bits, baud, divisor;
722 CHANNEL_t *cp;
723 struct ktermios *t = tty->termios;
724
725 cp = &info->channel;
726 cflag = t->c_cflag;
727
728 /* Byte size and parity */
729 if ((cflag & CSIZE) == CS8) {
730 sSetData8(cp);
731 bits = 10;
732 } else {
733 sSetData7(cp);
734 bits = 9;
735 }
736 if (cflag & CSTOPB) {
737 sSetStop2(cp);
738 bits++;
739 } else {
740 sSetStop1(cp);
741 }
742
743 if (cflag & PARENB) {
744 sEnParity(cp);
745 bits++;
746 if (cflag & PARODD) {
747 sSetOddParity(cp);
748 } else {
749 sSetEvenParity(cp);
750 }
751 } else {
752 sDisParity(cp);
753 }
754
755 /* baud rate */
756 baud = tty_get_baud_rate(tty);
757 if (!baud)
758 baud = 9600;
759 divisor = ((rp_baud_base[info->board] + (baud >> 1)) / baud) - 1;
760 if ((divisor >= 8192 || divisor < 0) && old_termios) {
761 baud = tty_termios_baud_rate(old_termios);
762 if (!baud)
763 baud = 9600;
764 divisor = (rp_baud_base[info->board] / baud) - 1;
765 }
766 if (divisor >= 8192 || divisor < 0) {
767 baud = 9600;
768 divisor = (rp_baud_base[info->board] / baud) - 1;
769 }
770 info->cps = baud / bits;
771 sSetBaud(cp, divisor);
772
773 /* FIXME: Should really back compute a baud rate from the divisor */
774 tty_encode_baud_rate(tty, baud, baud);
775
776 if (cflag & CRTSCTS) {
777 info->intmask |= DELTA_CTS;
778 sEnCTSFlowCtl(cp);
779 } else {
780 info->intmask &= ~DELTA_CTS;
781 sDisCTSFlowCtl(cp);
782 }
783 if (cflag & CLOCAL) {
784 info->intmask &= ~DELTA_CD;
785 } else {
786 spin_lock_irqsave(&info->slock, flags);
787 if (sGetChanStatus(cp) & CD_ACT)
788 info->cd_status = 1;
789 else
790 info->cd_status = 0;
791 info->intmask |= DELTA_CD;
792 spin_unlock_irqrestore(&info->slock, flags);
793 }
794
795 /*
796 * Handle software flow control in the board
797 */
798#ifdef ROCKET_SOFT_FLOW
799 if (I_IXON(tty)) {
800 sEnTxSoftFlowCtl(cp);
801 if (I_IXANY(tty)) {
802 sEnIXANY(cp);
803 } else {
804 sDisIXANY(cp);
805 }
806 sSetTxXONChar(cp, START_CHAR(tty));
807 sSetTxXOFFChar(cp, STOP_CHAR(tty));
808 } else {
809 sDisTxSoftFlowCtl(cp);
810 sDisIXANY(cp);
811 sClrTxXOFF(cp);
812 }
813#endif
814
815 /*
816 * Set up ignore/read mask words
817 */
818 info->read_status_mask = STMRCVROVRH | 0xFF;
819 if (I_INPCK(tty))
820 info->read_status_mask |= STMFRAMEH | STMPARITYH;
821 if (I_BRKINT(tty) || I_PARMRK(tty))
822 info->read_status_mask |= STMBREAKH;
823
824 /*
825 * Characters to ignore
826 */
827 info->ignore_status_mask = 0;
828 if (I_IGNPAR(tty))
829 info->ignore_status_mask |= STMFRAMEH | STMPARITYH;
830 if (I_IGNBRK(tty)) {
831 info->ignore_status_mask |= STMBREAKH;
832 /*
833 * If we're ignoring parity and break indicators,
834 * ignore overruns too. (For real raw support).
835 */
836 if (I_IGNPAR(tty))
837 info->ignore_status_mask |= STMRCVROVRH;
838 }
839
840 rocketMode = info->flags & ROCKET_MODE_MASK;
841
842 if ((info->flags & ROCKET_RTS_TOGGLE)
843 || (rocketMode == ROCKET_MODE_RS485))
844 sEnRTSToggle(cp);
845 else
846 sDisRTSToggle(cp);
847
848 sSetRTS(&info->channel);
849
850 if (cp->CtlP->boardType == ROCKET_TYPE_PC104) {
851 switch (rocketMode) {
852 case ROCKET_MODE_RS485:
853 sSetInterfaceMode(cp, InterfaceModeRS485);
854 break;
855 case ROCKET_MODE_RS422:
856 sSetInterfaceMode(cp, InterfaceModeRS422);
857 break;
858 case ROCKET_MODE_RS232:
859 default:
860 if (info->flags & ROCKET_RTS_TOGGLE)
861 sSetInterfaceMode(cp, InterfaceModeRS232T);
862 else
863 sSetInterfaceMode(cp, InterfaceModeRS232);
864 break;
865 }
866 }
867}
868
869static int carrier_raised(struct tty_port *port)
870{
871 struct r_port *info = container_of(port, struct r_port, port);
872 return (sGetChanStatusLo(&info->channel) & CD_ACT) ? 1 : 0;
873}
874
875static void dtr_rts(struct tty_port *port, int on)
876{
877 struct r_port *info = container_of(port, struct r_port, port);
878 if (on) {
879 sSetDTR(&info->channel);
880 sSetRTS(&info->channel);
881 } else {
882 sClrDTR(&info->channel);
883 sClrRTS(&info->channel);
884 }
885}
886
887/*
888 * Exception handler that opens a serial port. Creates xmit_buf storage, fills in
889 * port's r_port struct. Initializes the port hardware.
890 */
891static int rp_open(struct tty_struct *tty, struct file *filp)
892{
893 struct r_port *info;
894 struct tty_port *port;
895 int line = 0, retval;
896 CHANNEL_t *cp;
897 unsigned long page;
898
899 line = tty->index;
900 if (line < 0 || line >= MAX_RP_PORTS || ((info = rp_table[line]) == NULL))
901 return -ENXIO;
902 port = &info->port;
903
904 page = __get_free_page(GFP_KERNEL);
905 if (!page)
906 return -ENOMEM;
907
908 if (port->flags & ASYNC_CLOSING) {
909 retval = wait_for_completion_interruptible(&info->close_wait);
910 free_page(page);
911 if (retval)
912 return retval;
913 return ((port->flags & ASYNC_HUP_NOTIFY) ? -EAGAIN : -ERESTARTSYS);
914 }
915
916 /*
917 * We must not sleep from here until the port is marked fully in use.
918 */
919 if (info->xmit_buf)
920 free_page(page);
921 else
922 info->xmit_buf = (unsigned char *) page;
923
924 tty->driver_data = info;
925 tty_port_tty_set(port, tty);
926
927 if (port->count++ == 0) {
928 atomic_inc(&rp_num_ports_open);
929
930#ifdef ROCKET_DEBUG_OPEN
931 printk(KERN_INFO "rocket mod++ = %d...\n",
932 atomic_read(&rp_num_ports_open));
933#endif
934 }
935#ifdef ROCKET_DEBUG_OPEN
936 printk(KERN_INFO "rp_open ttyR%d, count=%d\n", info->line, info->port.count);
937#endif
938
939 /*
940 * Info->count is now 1; so it's safe to sleep now.
941 */
942 if (!test_bit(ASYNCB_INITIALIZED, &port->flags)) {
943 cp = &info->channel;
944 sSetRxTrigger(cp, TRIG_1);
945 if (sGetChanStatus(cp) & CD_ACT)
946 info->cd_status = 1;
947 else
948 info->cd_status = 0;
949 sDisRxStatusMode(cp);
950 sFlushRxFIFO(cp);
951 sFlushTxFIFO(cp);
952
953 sEnInterrupts(cp, (TXINT_EN | MCINT_EN | RXINT_EN | SRCINT_EN | CHANINT_EN));
954 sSetRxTrigger(cp, TRIG_1);
955
956 sGetChanStatus(cp);
957 sDisRxStatusMode(cp);
958 sClrTxXOFF(cp);
959
960 sDisCTSFlowCtl(cp);
961 sDisTxSoftFlowCtl(cp);
962
963 sEnRxFIFO(cp);
964 sEnTransmit(cp);
965
966 set_bit(ASYNCB_INITIALIZED, &info->port.flags);
967
968 /*
969 * Set up the tty->alt_speed kludge
970 */
971 if ((info->flags & ROCKET_SPD_MASK) == ROCKET_SPD_HI)
972 tty->alt_speed = 57600;
973 if ((info->flags & ROCKET_SPD_MASK) == ROCKET_SPD_VHI)
974 tty->alt_speed = 115200;
975 if ((info->flags & ROCKET_SPD_MASK) == ROCKET_SPD_SHI)
976 tty->alt_speed = 230400;
977 if ((info->flags & ROCKET_SPD_MASK) == ROCKET_SPD_WARP)
978 tty->alt_speed = 460800;
979
980 configure_r_port(tty, info, NULL);
981 if (tty->termios->c_cflag & CBAUD) {
982 sSetDTR(cp);
983 sSetRTS(cp);
984 }
985 }
986 /* Starts (or resets) the maint polling loop */
987 mod_timer(&rocket_timer, jiffies + POLL_PERIOD);
988
989 retval = tty_port_block_til_ready(port, tty, filp);
990 if (retval) {
991#ifdef ROCKET_DEBUG_OPEN
992 printk(KERN_INFO "rp_open returning after block_til_ready with %d\n", retval);
993#endif
994 return retval;
995 }
996 return 0;
997}
998
999/*
1000 * Exception handler that closes a serial port. info->port.count is considered critical.
1001 */
1002static void rp_close(struct tty_struct *tty, struct file *filp)
1003{
1004 struct r_port *info = tty->driver_data;
1005 struct tty_port *port = &info->port;
1006 int timeout;
1007 CHANNEL_t *cp;
1008
1009 if (rocket_paranoia_check(info, "rp_close"))
1010 return;
1011
1012#ifdef ROCKET_DEBUG_OPEN
1013 printk(KERN_INFO "rp_close ttyR%d, count = %d\n", info->line, info->port.count);
1014#endif
1015
1016 if (tty_port_close_start(port, tty, filp) == 0)
1017 return;
1018
1019 mutex_lock(&port->mutex);
1020 cp = &info->channel;
1021 /*
1022 * Before we drop DTR, make sure the UART transmitter
1023 * has completely drained; this is especially
1024 * important if there is a transmit FIFO!
1025 */
1026 timeout = (sGetTxCnt(cp) + 1) * HZ / info->cps;
1027 if (timeout == 0)
1028 timeout = 1;
1029 rp_wait_until_sent(tty, timeout);
1030 clear_bit((info->aiop * 8) + info->chan, (void *) &xmit_flags[info->board]);
1031
1032 sDisTransmit(cp);
1033 sDisInterrupts(cp, (TXINT_EN | MCINT_EN | RXINT_EN | SRCINT_EN | CHANINT_EN));
1034 sDisCTSFlowCtl(cp);
1035 sDisTxSoftFlowCtl(cp);
1036 sClrTxXOFF(cp);
1037 sFlushRxFIFO(cp);
1038 sFlushTxFIFO(cp);
1039 sClrRTS(cp);
1040 if (C_HUPCL(tty))
1041 sClrDTR(cp);
1042
1043 rp_flush_buffer(tty);
1044
1045 tty_ldisc_flush(tty);
1046
1047 clear_bit((info->aiop * 8) + info->chan, (void *) &xmit_flags[info->board]);
1048
1049 /* We can't yet use tty_port_close_end as the buffer handling in this
1050 driver is a bit different to the usual */
1051
1052 if (port->blocked_open) {
1053 if (port->close_delay) {
1054 msleep_interruptible(jiffies_to_msecs(port->close_delay));
1055 }
1056 wake_up_interruptible(&port->open_wait);
1057 } else {
1058 if (info->xmit_buf) {
1059 free_page((unsigned long) info->xmit_buf);
1060 info->xmit_buf = NULL;
1061 }
1062 }
1063 spin_lock_irq(&port->lock);
1064 info->port.flags &= ~(ASYNC_INITIALIZED | ASYNC_CLOSING | ASYNC_NORMAL_ACTIVE);
1065 tty->closing = 0;
1066 spin_unlock_irq(&port->lock);
1067 mutex_unlock(&port->mutex);
1068 tty_port_tty_set(port, NULL);
1069
1070 wake_up_interruptible(&port->close_wait);
1071 complete_all(&info->close_wait);
1072 atomic_dec(&rp_num_ports_open);
1073
1074#ifdef ROCKET_DEBUG_OPEN
1075 printk(KERN_INFO "rocket mod-- = %d...\n",
1076 atomic_read(&rp_num_ports_open));
1077 printk(KERN_INFO "rp_close ttyR%d complete shutdown\n", info->line);
1078#endif
1079
1080}
1081
1082static void rp_set_termios(struct tty_struct *tty,
1083 struct ktermios *old_termios)
1084{
1085 struct r_port *info = tty->driver_data;
1086 CHANNEL_t *cp;
1087 unsigned cflag;
1088
1089 if (rocket_paranoia_check(info, "rp_set_termios"))
1090 return;
1091
1092 cflag = tty->termios->c_cflag;
1093
1094 /*
1095 * This driver doesn't support CS5 or CS6
1096 */
1097 if (((cflag & CSIZE) == CS5) || ((cflag & CSIZE) == CS6))
1098 tty->termios->c_cflag =
1099 ((cflag & ~CSIZE) | (old_termios->c_cflag & CSIZE));
1100 /* Or CMSPAR */
1101 tty->termios->c_cflag &= ~CMSPAR;
1102
1103 configure_r_port(tty, info, old_termios);
1104
1105 cp = &info->channel;
1106
1107 /* Handle transition to B0 status */
1108 if ((old_termios->c_cflag & CBAUD) && !(tty->termios->c_cflag & CBAUD)) {
1109 sClrDTR(cp);
1110 sClrRTS(cp);
1111 }
1112
1113 /* Handle transition away from B0 status */
1114 if (!(old_termios->c_cflag & CBAUD) && (tty->termios->c_cflag & CBAUD)) {
1115 if (!tty->hw_stopped || !(tty->termios->c_cflag & CRTSCTS))
1116 sSetRTS(cp);
1117 sSetDTR(cp);
1118 }
1119
1120 if ((old_termios->c_cflag & CRTSCTS) && !(tty->termios->c_cflag & CRTSCTS)) {
1121 tty->hw_stopped = 0;
1122 rp_start(tty);
1123 }
1124}
1125
1126static int rp_break(struct tty_struct *tty, int break_state)
1127{
1128 struct r_port *info = tty->driver_data;
1129 unsigned long flags;
1130
1131 if (rocket_paranoia_check(info, "rp_break"))
1132 return -EINVAL;
1133
1134 spin_lock_irqsave(&info->slock, flags);
1135 if (break_state == -1)
1136 sSendBreak(&info->channel);
1137 else
1138 sClrBreak(&info->channel);
1139 spin_unlock_irqrestore(&info->slock, flags);
1140 return 0;
1141}
1142
1143/*
1144 * sGetChanRI used to be a macro in rocket_int.h. When the functionality for
1145 * the UPCI boards was added, it was decided to make this a function because
1146 * the macro was getting too complicated. All cases except the first one
1147 * (UPCIRingInd) are taken directly from the original macro.
1148 */
1149static int sGetChanRI(CHANNEL_T * ChP)
1150{
1151 CONTROLLER_t *CtlP = ChP->CtlP;
1152 int ChanNum = ChP->ChanNum;
1153 int RingInd = 0;
1154
1155 if (CtlP->UPCIRingInd)
1156 RingInd = !(sInB(CtlP->UPCIRingInd) & sBitMapSetTbl[ChanNum]);
1157 else if (CtlP->AltChanRingIndicator)
1158 RingInd = sInB((ByteIO_t) (ChP->ChanStat + 8)) & DSR_ACT;
1159 else if (CtlP->boardType == ROCKET_TYPE_PC104)
1160 RingInd = !(sInB(CtlP->AiopIO[3]) & sBitMapSetTbl[ChanNum]);
1161
1162 return RingInd;
1163}
1164
1165/********************************************************************************************/
1166/* Here are the routines used by rp_ioctl. These are all called from exception handlers. */
1167
1168/*
1169 * Returns the state of the serial modem control lines. These next 2 functions
1170 * are the way kernel versions > 2.5 handle modem control lines rather than IOCTLs.
1171 */
1172static int rp_tiocmget(struct tty_struct *tty, struct file *file)
1173{
1174 struct r_port *info = tty->driver_data;
1175 unsigned int control, result, ChanStatus;
1176
1177 ChanStatus = sGetChanStatusLo(&info->channel);
1178 control = info->channel.TxControl[3];
1179 result = ((control & SET_RTS) ? TIOCM_RTS : 0) |
1180 ((control & SET_DTR) ? TIOCM_DTR : 0) |
1181 ((ChanStatus & CD_ACT) ? TIOCM_CAR : 0) |
1182 (sGetChanRI(&info->channel) ? TIOCM_RNG : 0) |
1183 ((ChanStatus & DSR_ACT) ? TIOCM_DSR : 0) |
1184 ((ChanStatus & CTS_ACT) ? TIOCM_CTS : 0);
1185
1186 return result;
1187}
1188
1189/*
1190 * Sets the modem control lines
1191 */
1192static int rp_tiocmset(struct tty_struct *tty, struct file *file,
1193 unsigned int set, unsigned int clear)
1194{
1195 struct r_port *info = tty->driver_data;
1196
1197 if (set & TIOCM_RTS)
1198 info->channel.TxControl[3] |= SET_RTS;
1199 if (set & TIOCM_DTR)
1200 info->channel.TxControl[3] |= SET_DTR;
1201 if (clear & TIOCM_RTS)
1202 info->channel.TxControl[3] &= ~SET_RTS;
1203 if (clear & TIOCM_DTR)
1204 info->channel.TxControl[3] &= ~SET_DTR;
1205
1206 out32(info->channel.IndexAddr, info->channel.TxControl);
1207 return 0;
1208}
1209
1210static int get_config(struct r_port *info, struct rocket_config __user *retinfo)
1211{
1212 struct rocket_config tmp;
1213
1214 if (!retinfo)
1215 return -EFAULT;
1216 memset(&tmp, 0, sizeof (tmp));
1217 mutex_lock(&info->port.mutex);
1218 tmp.line = info->line;
1219 tmp.flags = info->flags;
1220 tmp.close_delay = info->port.close_delay;
1221 tmp.closing_wait = info->port.closing_wait;
1222 tmp.port = rcktpt_io_addr[(info->line >> 5) & 3];
1223 mutex_unlock(&info->port.mutex);
1224
1225 if (copy_to_user(retinfo, &tmp, sizeof (*retinfo)))
1226 return -EFAULT;
1227 return 0;
1228}
1229
1230static int set_config(struct tty_struct *tty, struct r_port *info,
1231 struct rocket_config __user *new_info)
1232{
1233 struct rocket_config new_serial;
1234
1235 if (copy_from_user(&new_serial, new_info, sizeof (new_serial)))
1236 return -EFAULT;
1237
1238 mutex_lock(&info->port.mutex);
1239 if (!capable(CAP_SYS_ADMIN))
1240 {
1241 if ((new_serial.flags & ~ROCKET_USR_MASK) != (info->flags & ~ROCKET_USR_MASK)) {
1242 mutex_unlock(&info->port.mutex);
1243 return -EPERM;
1244 }
1245 info->flags = ((info->flags & ~ROCKET_USR_MASK) | (new_serial.flags & ROCKET_USR_MASK));
1246 configure_r_port(tty, info, NULL);
1247 mutex_unlock(&info->port.mutex);
1248 return 0;
1249 }
1250
1251 info->flags = ((info->flags & ~ROCKET_FLAGS) | (new_serial.flags & ROCKET_FLAGS));
1252 info->port.close_delay = new_serial.close_delay;
1253 info->port.closing_wait = new_serial.closing_wait;
1254
1255 if ((info->flags & ROCKET_SPD_MASK) == ROCKET_SPD_HI)
1256 tty->alt_speed = 57600;
1257 if ((info->flags & ROCKET_SPD_MASK) == ROCKET_SPD_VHI)
1258 tty->alt_speed = 115200;
1259 if ((info->flags & ROCKET_SPD_MASK) == ROCKET_SPD_SHI)
1260 tty->alt_speed = 230400;
1261 if ((info->flags & ROCKET_SPD_MASK) == ROCKET_SPD_WARP)
1262 tty->alt_speed = 460800;
1263 mutex_unlock(&info->port.mutex);
1264
1265 configure_r_port(tty, info, NULL);
1266 return 0;
1267}
1268
1269/*
1270 * This function fills in a rocket_ports struct with information
1271 * about what boards/ports are in the system. This info is passed
1272 * to user space. See setrocket.c where the info is used to create
1273 * the /dev/ttyRx ports.
1274 */
1275static int get_ports(struct r_port *info, struct rocket_ports __user *retports)
1276{
1277 struct rocket_ports tmp;
1278 int board;
1279
1280 if (!retports)
1281 return -EFAULT;
1282 memset(&tmp, 0, sizeof (tmp));
1283 tmp.tty_major = rocket_driver->major;
1284
1285 for (board = 0; board < 4; board++) {
1286 tmp.rocketModel[board].model = rocketModel[board].model;
1287 strcpy(tmp.rocketModel[board].modelString, rocketModel[board].modelString);
1288 tmp.rocketModel[board].numPorts = rocketModel[board].numPorts;
1289 tmp.rocketModel[board].loadrm2 = rocketModel[board].loadrm2;
1290 tmp.rocketModel[board].startingPortNumber = rocketModel[board].startingPortNumber;
1291 }
1292 if (copy_to_user(retports, &tmp, sizeof (*retports)))
1293 return -EFAULT;
1294 return 0;
1295}
1296
1297static int reset_rm2(struct r_port *info, void __user *arg)
1298{
1299 int reset;
1300
1301 if (!capable(CAP_SYS_ADMIN))
1302 return -EPERM;
1303
1304 if (copy_from_user(&reset, arg, sizeof (int)))
1305 return -EFAULT;
1306 if (reset)
1307 reset = 1;
1308
1309 if (rcktpt_type[info->board] != ROCKET_TYPE_MODEMII &&
1310 rcktpt_type[info->board] != ROCKET_TYPE_MODEMIII)
1311 return -EINVAL;
1312
1313 if (info->ctlp->BusType == isISA)
1314 sModemReset(info->ctlp, info->chan, reset);
1315 else
1316 sPCIModemReset(info->ctlp, info->chan, reset);
1317
1318 return 0;
1319}
1320
1321static int get_version(struct r_port *info, struct rocket_version __user *retvers)
1322{
1323 if (copy_to_user(retvers, &driver_version, sizeof (*retvers)))
1324 return -EFAULT;
1325 return 0;
1326}
1327
1328/* IOCTL call handler into the driver */
1329static int rp_ioctl(struct tty_struct *tty, struct file *file,
1330 unsigned int cmd, unsigned long arg)
1331{
1332 struct r_port *info = tty->driver_data;
1333 void __user *argp = (void __user *)arg;
1334 int ret = 0;
1335
1336 if (cmd != RCKP_GET_PORTS && rocket_paranoia_check(info, "rp_ioctl"))
1337 return -ENXIO;
1338
1339 switch (cmd) {
1340 case RCKP_GET_STRUCT:
1341 if (copy_to_user(argp, info, sizeof (struct r_port)))
1342 ret = -EFAULT;
1343 break;
1344 case RCKP_GET_CONFIG:
1345 ret = get_config(info, argp);
1346 break;
1347 case RCKP_SET_CONFIG:
1348 ret = set_config(tty, info, argp);
1349 break;
1350 case RCKP_GET_PORTS:
1351 ret = get_ports(info, argp);
1352 break;
1353 case RCKP_RESET_RM2:
1354 ret = reset_rm2(info, argp);
1355 break;
1356 case RCKP_GET_VERSION:
1357 ret = get_version(info, argp);
1358 break;
1359 default:
1360 ret = -ENOIOCTLCMD;
1361 }
1362 return ret;
1363}
1364
1365static void rp_send_xchar(struct tty_struct *tty, char ch)
1366{
1367 struct r_port *info = tty->driver_data;
1368 CHANNEL_t *cp;
1369
1370 if (rocket_paranoia_check(info, "rp_send_xchar"))
1371 return;
1372
1373 cp = &info->channel;
1374 if (sGetTxCnt(cp))
1375 sWriteTxPrioByte(cp, ch);
1376 else
1377 sWriteTxByte(sGetTxRxDataIO(cp), ch);
1378}
1379
1380static void rp_throttle(struct tty_struct *tty)
1381{
1382 struct r_port *info = tty->driver_data;
1383 CHANNEL_t *cp;
1384
1385#ifdef ROCKET_DEBUG_THROTTLE
1386 printk(KERN_INFO "throttle %s: %d....\n", tty->name,
1387 tty->ldisc.chars_in_buffer(tty));
1388#endif
1389
1390 if (rocket_paranoia_check(info, "rp_throttle"))
1391 return;
1392
1393 cp = &info->channel;
1394 if (I_IXOFF(tty))
1395 rp_send_xchar(tty, STOP_CHAR(tty));
1396
1397 sClrRTS(&info->channel);
1398}
1399
1400static void rp_unthrottle(struct tty_struct *tty)
1401{
1402 struct r_port *info = tty->driver_data;
1403 CHANNEL_t *cp;
1404#ifdef ROCKET_DEBUG_THROTTLE
1405 printk(KERN_INFO "unthrottle %s: %d....\n", tty->name,
1406 tty->ldisc.chars_in_buffer(tty));
1407#endif
1408
1409 if (rocket_paranoia_check(info, "rp_throttle"))
1410 return;
1411
1412 cp = &info->channel;
1413 if (I_IXOFF(tty))
1414 rp_send_xchar(tty, START_CHAR(tty));
1415
1416 sSetRTS(&info->channel);
1417}
1418
1419/*
1420 * ------------------------------------------------------------
1421 * rp_stop() and rp_start()
1422 *
1423 * This routines are called before setting or resetting tty->stopped.
1424 * They enable or disable transmitter interrupts, as necessary.
1425 * ------------------------------------------------------------
1426 */
1427static void rp_stop(struct tty_struct *tty)
1428{
1429 struct r_port *info = tty->driver_data;
1430
1431#ifdef ROCKET_DEBUG_FLOW
1432 printk(KERN_INFO "stop %s: %d %d....\n", tty->name,
1433 info->xmit_cnt, info->xmit_fifo_room);
1434#endif
1435
1436 if (rocket_paranoia_check(info, "rp_stop"))
1437 return;
1438
1439 if (sGetTxCnt(&info->channel))
1440 sDisTransmit(&info->channel);
1441}
1442
1443static void rp_start(struct tty_struct *tty)
1444{
1445 struct r_port *info = tty->driver_data;
1446
1447#ifdef ROCKET_DEBUG_FLOW
1448 printk(KERN_INFO "start %s: %d %d....\n", tty->name,
1449 info->xmit_cnt, info->xmit_fifo_room);
1450#endif
1451
1452 if (rocket_paranoia_check(info, "rp_stop"))
1453 return;
1454
1455 sEnTransmit(&info->channel);
1456 set_bit((info->aiop * 8) + info->chan,
1457 (void *) &xmit_flags[info->board]);
1458}
1459
1460/*
1461 * rp_wait_until_sent() --- wait until the transmitter is empty
1462 */
1463static void rp_wait_until_sent(struct tty_struct *tty, int timeout)
1464{
1465 struct r_port *info = tty->driver_data;
1466 CHANNEL_t *cp;
1467 unsigned long orig_jiffies;
1468 int check_time, exit_time;
1469 int txcnt;
1470
1471 if (rocket_paranoia_check(info, "rp_wait_until_sent"))
1472 return;
1473
1474 cp = &info->channel;
1475
1476 orig_jiffies = jiffies;
1477#ifdef ROCKET_DEBUG_WAIT_UNTIL_SENT
1478 printk(KERN_INFO "In RP_wait_until_sent(%d) (jiff=%lu)...\n", timeout,
1479 jiffies);
1480 printk(KERN_INFO "cps=%d...\n", info->cps);
1481#endif
1482 while (1) {
1483 txcnt = sGetTxCnt(cp);
1484 if (!txcnt) {
1485 if (sGetChanStatusLo(cp) & TXSHRMT)
1486 break;
1487 check_time = (HZ / info->cps) / 5;
1488 } else {
1489 check_time = HZ * txcnt / info->cps;
1490 }
1491 if (timeout) {
1492 exit_time = orig_jiffies + timeout - jiffies;
1493 if (exit_time <= 0)
1494 break;
1495 if (exit_time < check_time)
1496 check_time = exit_time;
1497 }
1498 if (check_time == 0)
1499 check_time = 1;
1500#ifdef ROCKET_DEBUG_WAIT_UNTIL_SENT
1501 printk(KERN_INFO "txcnt = %d (jiff=%lu,check=%d)...\n", txcnt,
1502 jiffies, check_time);
1503#endif
1504 msleep_interruptible(jiffies_to_msecs(check_time));
1505 if (signal_pending(current))
1506 break;
1507 }
1508 __set_current_state(TASK_RUNNING);
1509#ifdef ROCKET_DEBUG_WAIT_UNTIL_SENT
1510 printk(KERN_INFO "txcnt = %d (jiff=%lu)...done\n", txcnt, jiffies);
1511#endif
1512}
1513
1514/*
1515 * rp_hangup() --- called by tty_hangup() when a hangup is signaled.
1516 */
1517static void rp_hangup(struct tty_struct *tty)
1518{
1519 CHANNEL_t *cp;
1520 struct r_port *info = tty->driver_data;
1521 unsigned long flags;
1522
1523 if (rocket_paranoia_check(info, "rp_hangup"))
1524 return;
1525
1526#if (defined(ROCKET_DEBUG_OPEN) || defined(ROCKET_DEBUG_HANGUP))
1527 printk(KERN_INFO "rp_hangup of ttyR%d...\n", info->line);
1528#endif
1529 rp_flush_buffer(tty);
1530 spin_lock_irqsave(&info->port.lock, flags);
1531 if (info->port.flags & ASYNC_CLOSING) {
1532 spin_unlock_irqrestore(&info->port.lock, flags);
1533 return;
1534 }
1535 if (info->port.count)
1536 atomic_dec(&rp_num_ports_open);
1537 clear_bit((info->aiop * 8) + info->chan, (void *) &xmit_flags[info->board]);
1538 spin_unlock_irqrestore(&info->port.lock, flags);
1539
1540 tty_port_hangup(&info->port);
1541
1542 cp = &info->channel;
1543 sDisRxFIFO(cp);
1544 sDisTransmit(cp);
1545 sDisInterrupts(cp, (TXINT_EN | MCINT_EN | RXINT_EN | SRCINT_EN | CHANINT_EN));
1546 sDisCTSFlowCtl(cp);
1547 sDisTxSoftFlowCtl(cp);
1548 sClrTxXOFF(cp);
1549 clear_bit(ASYNCB_INITIALIZED, &info->port.flags);
1550
1551 wake_up_interruptible(&info->port.open_wait);
1552}
1553
1554/*
1555 * Exception handler - write char routine. The RocketPort driver uses a
1556 * double-buffering strategy, with the twist that if the in-memory CPU
1557 * buffer is empty, and there's space in the transmit FIFO, the
1558 * writing routines will write directly to transmit FIFO.
1559 * Write buffer and counters protected by spinlocks
1560 */
1561static int rp_put_char(struct tty_struct *tty, unsigned char ch)
1562{
1563 struct r_port *info = tty->driver_data;
1564 CHANNEL_t *cp;
1565 unsigned long flags;
1566
1567 if (rocket_paranoia_check(info, "rp_put_char"))
1568 return 0;
1569
1570 /*
1571 * Grab the port write mutex, locking out other processes that try to
1572 * write to this port
1573 */
1574 mutex_lock(&info->write_mtx);
1575
1576#ifdef ROCKET_DEBUG_WRITE
1577 printk(KERN_INFO "rp_put_char %c...\n", ch);
1578#endif
1579
1580 spin_lock_irqsave(&info->slock, flags);
1581 cp = &info->channel;
1582
1583 if (!tty->stopped && !tty->hw_stopped && info->xmit_fifo_room == 0)
1584 info->xmit_fifo_room = TXFIFO_SIZE - sGetTxCnt(cp);
1585
1586 if (tty->stopped || tty->hw_stopped || info->xmit_fifo_room == 0 || info->xmit_cnt != 0) {
1587 info->xmit_buf[info->xmit_head++] = ch;
1588 info->xmit_head &= XMIT_BUF_SIZE - 1;
1589 info->xmit_cnt++;
1590 set_bit((info->aiop * 8) + info->chan, (void *) &xmit_flags[info->board]);
1591 } else {
1592 sOutB(sGetTxRxDataIO(cp), ch);
1593 info->xmit_fifo_room--;
1594 }
1595 spin_unlock_irqrestore(&info->slock, flags);
1596 mutex_unlock(&info->write_mtx);
1597 return 1;
1598}
1599
1600/*
1601 * Exception handler - write routine, called when user app writes to the device.
1602 * A per port write mutex is used to protect from another process writing to
1603 * this port at the same time. This other process could be running on the other CPU
1604 * or get control of the CPU if the copy_from_user() blocks due to a page fault (swapped out).
1605 * Spinlocks protect the info xmit members.
1606 */
1607static int rp_write(struct tty_struct *tty,
1608 const unsigned char *buf, int count)
1609{
1610 struct r_port *info = tty->driver_data;
1611 CHANNEL_t *cp;
1612 const unsigned char *b;
1613 int c, retval = 0;
1614 unsigned long flags;
1615
1616 if (count <= 0 || rocket_paranoia_check(info, "rp_write"))
1617 return 0;
1618
1619 if (mutex_lock_interruptible(&info->write_mtx))
1620 return -ERESTARTSYS;
1621
1622#ifdef ROCKET_DEBUG_WRITE
1623 printk(KERN_INFO "rp_write %d chars...\n", count);
1624#endif
1625 cp = &info->channel;
1626
1627 if (!tty->stopped && !tty->hw_stopped && info->xmit_fifo_room < count)
1628 info->xmit_fifo_room = TXFIFO_SIZE - sGetTxCnt(cp);
1629
1630 /*
1631 * If the write queue for the port is empty, and there is FIFO space, stuff bytes
1632 * into FIFO. Use the write queue for temp storage.
1633 */
1634 if (!tty->stopped && !tty->hw_stopped && info->xmit_cnt == 0 && info->xmit_fifo_room > 0) {
1635 c = min(count, info->xmit_fifo_room);
1636 b = buf;
1637
1638 /* Push data into FIFO, 2 bytes at a time */
1639 sOutStrW(sGetTxRxDataIO(cp), (unsigned short *) b, c / 2);
1640
1641 /* If there is a byte remaining, write it */
1642 if (c & 1)
1643 sOutB(sGetTxRxDataIO(cp), b[c - 1]);
1644
1645 retval += c;
1646 buf += c;
1647 count -= c;
1648
1649 spin_lock_irqsave(&info->slock, flags);
1650 info->xmit_fifo_room -= c;
1651 spin_unlock_irqrestore(&info->slock, flags);
1652 }
1653
1654 /* If count is zero, we wrote it all and are done */
1655 if (!count)
1656 goto end;
1657
1658 /* Write remaining data into the port's xmit_buf */
1659 while (1) {
1660 /* Hung up ? */
1661 if (!test_bit(ASYNCB_NORMAL_ACTIVE, &info->port.flags))
1662 goto end;
1663 c = min(count, XMIT_BUF_SIZE - info->xmit_cnt - 1);
1664 c = min(c, XMIT_BUF_SIZE - info->xmit_head);
1665 if (c <= 0)
1666 break;
1667
1668 b = buf;
1669 memcpy(info->xmit_buf + info->xmit_head, b, c);
1670
1671 spin_lock_irqsave(&info->slock, flags);
1672 info->xmit_head =
1673 (info->xmit_head + c) & (XMIT_BUF_SIZE - 1);
1674 info->xmit_cnt += c;
1675 spin_unlock_irqrestore(&info->slock, flags);
1676
1677 buf += c;
1678 count -= c;
1679 retval += c;
1680 }
1681
1682 if ((retval > 0) && !tty->stopped && !tty->hw_stopped)
1683 set_bit((info->aiop * 8) + info->chan, (void *) &xmit_flags[info->board]);
1684
1685end:
1686 if (info->xmit_cnt < WAKEUP_CHARS) {
1687 tty_wakeup(tty);
1688#ifdef ROCKETPORT_HAVE_POLL_WAIT
1689 wake_up_interruptible(&tty->poll_wait);
1690#endif
1691 }
1692 mutex_unlock(&info->write_mtx);
1693 return retval;
1694}
1695
1696/*
1697 * Return the number of characters that can be sent. We estimate
1698 * only using the in-memory transmit buffer only, and ignore the
1699 * potential space in the transmit FIFO.
1700 */
1701static int rp_write_room(struct tty_struct *tty)
1702{
1703 struct r_port *info = tty->driver_data;
1704 int ret;
1705
1706 if (rocket_paranoia_check(info, "rp_write_room"))
1707 return 0;
1708
1709 ret = XMIT_BUF_SIZE - info->xmit_cnt - 1;
1710 if (ret < 0)
1711 ret = 0;
1712#ifdef ROCKET_DEBUG_WRITE
1713 printk(KERN_INFO "rp_write_room returns %d...\n", ret);
1714#endif
1715 return ret;
1716}
1717
1718/*
1719 * Return the number of characters in the buffer. Again, this only
1720 * counts those characters in the in-memory transmit buffer.
1721 */
1722static int rp_chars_in_buffer(struct tty_struct *tty)
1723{
1724 struct r_port *info = tty->driver_data;
1725 CHANNEL_t *cp;
1726
1727 if (rocket_paranoia_check(info, "rp_chars_in_buffer"))
1728 return 0;
1729
1730 cp = &info->channel;
1731
1732#ifdef ROCKET_DEBUG_WRITE
1733 printk(KERN_INFO "rp_chars_in_buffer returns %d...\n", info->xmit_cnt);
1734#endif
1735 return info->xmit_cnt;
1736}
1737
1738/*
1739 * Flushes the TX fifo for a port, deletes data in the xmit_buf stored in the
1740 * r_port struct for the port. Note that spinlock are used to protect info members,
1741 * do not call this function if the spinlock is already held.
1742 */
1743static void rp_flush_buffer(struct tty_struct *tty)
1744{
1745 struct r_port *info = tty->driver_data;
1746 CHANNEL_t *cp;
1747 unsigned long flags;
1748
1749 if (rocket_paranoia_check(info, "rp_flush_buffer"))
1750 return;
1751
1752 spin_lock_irqsave(&info->slock, flags);
1753 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1754 spin_unlock_irqrestore(&info->slock, flags);
1755
1756#ifdef ROCKETPORT_HAVE_POLL_WAIT
1757 wake_up_interruptible(&tty->poll_wait);
1758#endif
1759 tty_wakeup(tty);
1760
1761 cp = &info->channel;
1762 sFlushTxFIFO(cp);
1763}
1764
1765#ifdef CONFIG_PCI
1766
1767static struct pci_device_id __devinitdata rocket_pci_ids[] = {
1768 { PCI_DEVICE(PCI_VENDOR_ID_RP, PCI_ANY_ID) },
1769 { }
1770};
1771MODULE_DEVICE_TABLE(pci, rocket_pci_ids);
1772
1773/*
1774 * Called when a PCI card is found. Retrieves and stores model information,
1775 * init's aiopic and serial port hardware.
1776 * Inputs: i is the board number (0-n)
1777 */
1778static __init int register_PCI(int i, struct pci_dev *dev)
1779{
1780 int num_aiops, aiop, max_num_aiops, num_chan, chan;
1781 unsigned int aiopio[MAX_AIOPS_PER_BOARD];
1782 char *str, *board_type;
1783 CONTROLLER_t *ctlp;
1784
1785 int fast_clock = 0;
1786 int altChanRingIndicator = 0;
1787 int ports_per_aiop = 8;
1788 WordIO_t ConfigIO = 0;
1789 ByteIO_t UPCIRingInd = 0;
1790
1791 if (!dev || pci_enable_device(dev))
1792 return 0;
1793
1794 rcktpt_io_addr[i] = pci_resource_start(dev, 0);
1795
1796 rcktpt_type[i] = ROCKET_TYPE_NORMAL;
1797 rocketModel[i].loadrm2 = 0;
1798 rocketModel[i].startingPortNumber = nextLineNumber;
1799
1800 /* Depending on the model, set up some config variables */
1801 switch (dev->device) {
1802 case PCI_DEVICE_ID_RP4QUAD:
1803 str = "Quadcable";
1804 max_num_aiops = 1;
1805 ports_per_aiop = 4;
1806 rocketModel[i].model = MODEL_RP4QUAD;
1807 strcpy(rocketModel[i].modelString, "RocketPort 4 port w/quad cable");
1808 rocketModel[i].numPorts = 4;
1809 break;
1810 case PCI_DEVICE_ID_RP8OCTA:
1811 str = "Octacable";
1812 max_num_aiops = 1;
1813 rocketModel[i].model = MODEL_RP8OCTA;
1814 strcpy(rocketModel[i].modelString, "RocketPort 8 port w/octa cable");
1815 rocketModel[i].numPorts = 8;
1816 break;
1817 case PCI_DEVICE_ID_URP8OCTA:
1818 str = "Octacable";
1819 max_num_aiops = 1;
1820 rocketModel[i].model = MODEL_UPCI_RP8OCTA;
1821 strcpy(rocketModel[i].modelString, "RocketPort UPCI 8 port w/octa cable");
1822 rocketModel[i].numPorts = 8;
1823 break;
1824 case PCI_DEVICE_ID_RP8INTF:
1825 str = "8";
1826 max_num_aiops = 1;
1827 rocketModel[i].model = MODEL_RP8INTF;
1828 strcpy(rocketModel[i].modelString, "RocketPort 8 port w/external I/F");
1829 rocketModel[i].numPorts = 8;
1830 break;
1831 case PCI_DEVICE_ID_URP8INTF:
1832 str = "8";
1833 max_num_aiops = 1;
1834 rocketModel[i].model = MODEL_UPCI_RP8INTF;
1835 strcpy(rocketModel[i].modelString, "RocketPort UPCI 8 port w/external I/F");
1836 rocketModel[i].numPorts = 8;
1837 break;
1838 case PCI_DEVICE_ID_RP8J:
1839 str = "8J";
1840 max_num_aiops = 1;
1841 rocketModel[i].model = MODEL_RP8J;
1842 strcpy(rocketModel[i].modelString, "RocketPort 8 port w/RJ11 connectors");
1843 rocketModel[i].numPorts = 8;
1844 break;
1845 case PCI_DEVICE_ID_RP4J:
1846 str = "4J";
1847 max_num_aiops = 1;
1848 ports_per_aiop = 4;
1849 rocketModel[i].model = MODEL_RP4J;
1850 strcpy(rocketModel[i].modelString, "RocketPort 4 port w/RJ45 connectors");
1851 rocketModel[i].numPorts = 4;
1852 break;
1853 case PCI_DEVICE_ID_RP8SNI:
1854 str = "8 (DB78 Custom)";
1855 max_num_aiops = 1;
1856 rocketModel[i].model = MODEL_RP8SNI;
1857 strcpy(rocketModel[i].modelString, "RocketPort 8 port w/ custom DB78");
1858 rocketModel[i].numPorts = 8;
1859 break;
1860 case PCI_DEVICE_ID_RP16SNI:
1861 str = "16 (DB78 Custom)";
1862 max_num_aiops = 2;
1863 rocketModel[i].model = MODEL_RP16SNI;
1864 strcpy(rocketModel[i].modelString, "RocketPort 16 port w/ custom DB78");
1865 rocketModel[i].numPorts = 16;
1866 break;
1867 case PCI_DEVICE_ID_RP16INTF:
1868 str = "16";
1869 max_num_aiops = 2;
1870 rocketModel[i].model = MODEL_RP16INTF;
1871 strcpy(rocketModel[i].modelString, "RocketPort 16 port w/external I/F");
1872 rocketModel[i].numPorts = 16;
1873 break;
1874 case PCI_DEVICE_ID_URP16INTF:
1875 str = "16";
1876 max_num_aiops = 2;
1877 rocketModel[i].model = MODEL_UPCI_RP16INTF;
1878 strcpy(rocketModel[i].modelString, "RocketPort UPCI 16 port w/external I/F");
1879 rocketModel[i].numPorts = 16;
1880 break;
1881 case PCI_DEVICE_ID_CRP16INTF:
1882 str = "16";
1883 max_num_aiops = 2;
1884 rocketModel[i].model = MODEL_CPCI_RP16INTF;
1885 strcpy(rocketModel[i].modelString, "RocketPort Compact PCI 16 port w/external I/F");
1886 rocketModel[i].numPorts = 16;
1887 break;
1888 case PCI_DEVICE_ID_RP32INTF:
1889 str = "32";
1890 max_num_aiops = 4;
1891 rocketModel[i].model = MODEL_RP32INTF;
1892 strcpy(rocketModel[i].modelString, "RocketPort 32 port w/external I/F");
1893 rocketModel[i].numPorts = 32;
1894 break;
1895 case PCI_DEVICE_ID_URP32INTF:
1896 str = "32";
1897 max_num_aiops = 4;
1898 rocketModel[i].model = MODEL_UPCI_RP32INTF;
1899 strcpy(rocketModel[i].modelString, "RocketPort UPCI 32 port w/external I/F");
1900 rocketModel[i].numPorts = 32;
1901 break;
1902 case PCI_DEVICE_ID_RPP4:
1903 str = "Plus Quadcable";
1904 max_num_aiops = 1;
1905 ports_per_aiop = 4;
1906 altChanRingIndicator++;
1907 fast_clock++;
1908 rocketModel[i].model = MODEL_RPP4;
1909 strcpy(rocketModel[i].modelString, "RocketPort Plus 4 port");
1910 rocketModel[i].numPorts = 4;
1911 break;
1912 case PCI_DEVICE_ID_RPP8:
1913 str = "Plus Octacable";
1914 max_num_aiops = 2;
1915 ports_per_aiop = 4;
1916 altChanRingIndicator++;
1917 fast_clock++;
1918 rocketModel[i].model = MODEL_RPP8;
1919 strcpy(rocketModel[i].modelString, "RocketPort Plus 8 port");
1920 rocketModel[i].numPorts = 8;
1921 break;
1922 case PCI_DEVICE_ID_RP2_232:
1923 str = "Plus 2 (RS-232)";
1924 max_num_aiops = 1;
1925 ports_per_aiop = 2;
1926 altChanRingIndicator++;
1927 fast_clock++;
1928 rocketModel[i].model = MODEL_RP2_232;
1929 strcpy(rocketModel[i].modelString, "RocketPort Plus 2 port RS232");
1930 rocketModel[i].numPorts = 2;
1931 break;
1932 case PCI_DEVICE_ID_RP2_422:
1933 str = "Plus 2 (RS-422)";
1934 max_num_aiops = 1;
1935 ports_per_aiop = 2;
1936 altChanRingIndicator++;
1937 fast_clock++;
1938 rocketModel[i].model = MODEL_RP2_422;
1939 strcpy(rocketModel[i].modelString, "RocketPort Plus 2 port RS422");
1940 rocketModel[i].numPorts = 2;
1941 break;
1942 case PCI_DEVICE_ID_RP6M:
1943
1944 max_num_aiops = 1;
1945 ports_per_aiop = 6;
1946 str = "6-port";
1947
1948 /* If revision is 1, the rocketmodem flash must be loaded.
1949 * If it is 2 it is a "socketed" version. */
1950 if (dev->revision == 1) {
1951 rcktpt_type[i] = ROCKET_TYPE_MODEMII;
1952 rocketModel[i].loadrm2 = 1;
1953 } else {
1954 rcktpt_type[i] = ROCKET_TYPE_MODEM;
1955 }
1956
1957 rocketModel[i].model = MODEL_RP6M;
1958 strcpy(rocketModel[i].modelString, "RocketModem 6 port");
1959 rocketModel[i].numPorts = 6;
1960 break;
1961 case PCI_DEVICE_ID_RP4M:
1962 max_num_aiops = 1;
1963 ports_per_aiop = 4;
1964 str = "4-port";
1965 if (dev->revision == 1) {
1966 rcktpt_type[i] = ROCKET_TYPE_MODEMII;
1967 rocketModel[i].loadrm2 = 1;
1968 } else {
1969 rcktpt_type[i] = ROCKET_TYPE_MODEM;
1970 }
1971
1972 rocketModel[i].model = MODEL_RP4M;
1973 strcpy(rocketModel[i].modelString, "RocketModem 4 port");
1974 rocketModel[i].numPorts = 4;
1975 break;
1976 default:
1977 str = "(unknown/unsupported)";
1978 max_num_aiops = 0;
1979 break;
1980 }
1981
1982 /*
1983 * Check for UPCI boards.
1984 */
1985
1986 switch (dev->device) {
1987 case PCI_DEVICE_ID_URP32INTF:
1988 case PCI_DEVICE_ID_URP8INTF:
1989 case PCI_DEVICE_ID_URP16INTF:
1990 case PCI_DEVICE_ID_CRP16INTF:
1991 case PCI_DEVICE_ID_URP8OCTA:
1992 rcktpt_io_addr[i] = pci_resource_start(dev, 2);
1993 ConfigIO = pci_resource_start(dev, 1);
1994 if (dev->device == PCI_DEVICE_ID_URP8OCTA) {
1995 UPCIRingInd = rcktpt_io_addr[i] + _PCI_9030_RING_IND;
1996
1997 /*
1998 * Check for octa or quad cable.
1999 */
2000 if (!
2001 (sInW(ConfigIO + _PCI_9030_GPIO_CTRL) &
2002 PCI_GPIO_CTRL_8PORT)) {
2003 str = "Quadcable";
2004 ports_per_aiop = 4;
2005 rocketModel[i].numPorts = 4;
2006 }
2007 }
2008 break;
2009 case PCI_DEVICE_ID_UPCI_RM3_8PORT:
2010 str = "8 ports";
2011 max_num_aiops = 1;
2012 rocketModel[i].model = MODEL_UPCI_RM3_8PORT;
2013 strcpy(rocketModel[i].modelString, "RocketModem III 8 port");
2014 rocketModel[i].numPorts = 8;
2015 rcktpt_io_addr[i] = pci_resource_start(dev, 2);
2016 UPCIRingInd = rcktpt_io_addr[i] + _PCI_9030_RING_IND;
2017 ConfigIO = pci_resource_start(dev, 1);
2018 rcktpt_type[i] = ROCKET_TYPE_MODEMIII;
2019 break;
2020 case PCI_DEVICE_ID_UPCI_RM3_4PORT:
2021 str = "4 ports";
2022 max_num_aiops = 1;
2023 rocketModel[i].model = MODEL_UPCI_RM3_4PORT;
2024 strcpy(rocketModel[i].modelString, "RocketModem III 4 port");
2025 rocketModel[i].numPorts = 4;
2026 rcktpt_io_addr[i] = pci_resource_start(dev, 2);
2027 UPCIRingInd = rcktpt_io_addr[i] + _PCI_9030_RING_IND;
2028 ConfigIO = pci_resource_start(dev, 1);
2029 rcktpt_type[i] = ROCKET_TYPE_MODEMIII;
2030 break;
2031 default:
2032 break;
2033 }
2034
2035 switch (rcktpt_type[i]) {
2036 case ROCKET_TYPE_MODEM:
2037 board_type = "RocketModem";
2038 break;
2039 case ROCKET_TYPE_MODEMII:
2040 board_type = "RocketModem II";
2041 break;
2042 case ROCKET_TYPE_MODEMIII:
2043 board_type = "RocketModem III";
2044 break;
2045 default:
2046 board_type = "RocketPort";
2047 break;
2048 }
2049
2050 if (fast_clock) {
2051 sClockPrescale = 0x12; /* mod 2 (divide by 3) */
2052 rp_baud_base[i] = 921600;
2053 } else {
2054 /*
2055 * If support_low_speed is set, use the slow clock
2056 * prescale, which supports 50 bps
2057 */
2058 if (support_low_speed) {
2059 /* mod 9 (divide by 10) prescale */
2060 sClockPrescale = 0x19;
2061 rp_baud_base[i] = 230400;
2062 } else {
2063 /* mod 4 (devide by 5) prescale */
2064 sClockPrescale = 0x14;
2065 rp_baud_base[i] = 460800;
2066 }
2067 }
2068
2069 for (aiop = 0; aiop < max_num_aiops; aiop++)
2070 aiopio[aiop] = rcktpt_io_addr[i] + (aiop * 0x40);
2071 ctlp = sCtlNumToCtlPtr(i);
2072 num_aiops = sPCIInitController(ctlp, i, aiopio, max_num_aiops, ConfigIO, 0, FREQ_DIS, 0, altChanRingIndicator, UPCIRingInd);
2073 for (aiop = 0; aiop < max_num_aiops; aiop++)
2074 ctlp->AiopNumChan[aiop] = ports_per_aiop;
2075
2076 dev_info(&dev->dev, "comtrol PCI controller #%d found at "
2077 "address %04lx, %d AIOP(s) (%s), creating ttyR%d - %ld\n",
2078 i, rcktpt_io_addr[i], num_aiops, rocketModel[i].modelString,
2079 rocketModel[i].startingPortNumber,
2080 rocketModel[i].startingPortNumber + rocketModel[i].numPorts-1);
2081
2082 if (num_aiops <= 0) {
2083 rcktpt_io_addr[i] = 0;
2084 return (0);
2085 }
2086 is_PCI[i] = 1;
2087
2088 /* Reset the AIOPIC, init the serial ports */
2089 for (aiop = 0; aiop < num_aiops; aiop++) {
2090 sResetAiopByNum(ctlp, aiop);
2091 num_chan = ports_per_aiop;
2092 for (chan = 0; chan < num_chan; chan++)
2093 init_r_port(i, aiop, chan, dev);
2094 }
2095
2096 /* Rocket modems must be reset */
2097 if ((rcktpt_type[i] == ROCKET_TYPE_MODEM) ||
2098 (rcktpt_type[i] == ROCKET_TYPE_MODEMII) ||
2099 (rcktpt_type[i] == ROCKET_TYPE_MODEMIII)) {
2100 num_chan = ports_per_aiop;
2101 for (chan = 0; chan < num_chan; chan++)
2102 sPCIModemReset(ctlp, chan, 1);
2103 msleep(500);
2104 for (chan = 0; chan < num_chan; chan++)
2105 sPCIModemReset(ctlp, chan, 0);
2106 msleep(500);
2107 rmSpeakerReset(ctlp, rocketModel[i].model);
2108 }
2109 return (1);
2110}
2111
2112/*
2113 * Probes for PCI cards, inits them if found
2114 * Input: board_found = number of ISA boards already found, or the
2115 * starting board number
2116 * Returns: Number of PCI boards found
2117 */
2118static int __init init_PCI(int boards_found)
2119{
2120 struct pci_dev *dev = NULL;
2121 int count = 0;
2122
2123 /* Work through the PCI device list, pulling out ours */
2124 while ((dev = pci_get_device(PCI_VENDOR_ID_RP, PCI_ANY_ID, dev))) {
2125 if (register_PCI(count + boards_found, dev))
2126 count++;
2127 }
2128 return (count);
2129}
2130
2131#endif /* CONFIG_PCI */
2132
2133/*
2134 * Probes for ISA cards
2135 * Input: i = the board number to look for
2136 * Returns: 1 if board found, 0 else
2137 */
2138static int __init init_ISA(int i)
2139{
2140 int num_aiops, num_chan = 0, total_num_chan = 0;
2141 int aiop, chan;
2142 unsigned int aiopio[MAX_AIOPS_PER_BOARD];
2143 CONTROLLER_t *ctlp;
2144 char *type_string;
2145
2146 /* If io_addr is zero, no board configured */
2147 if (rcktpt_io_addr[i] == 0)
2148 return (0);
2149
2150 /* Reserve the IO region */
2151 if (!request_region(rcktpt_io_addr[i], 64, "Comtrol RocketPort")) {
2152 printk(KERN_ERR "Unable to reserve IO region for configured "
2153 "ISA RocketPort at address 0x%lx, board not "
2154 "installed...\n", rcktpt_io_addr[i]);
2155 rcktpt_io_addr[i] = 0;
2156 return (0);
2157 }
2158
2159 ctlp = sCtlNumToCtlPtr(i);
2160
2161 ctlp->boardType = rcktpt_type[i];
2162
2163 switch (rcktpt_type[i]) {
2164 case ROCKET_TYPE_PC104:
2165 type_string = "(PC104)";
2166 break;
2167 case ROCKET_TYPE_MODEM:
2168 type_string = "(RocketModem)";
2169 break;
2170 case ROCKET_TYPE_MODEMII:
2171 type_string = "(RocketModem II)";
2172 break;
2173 default:
2174 type_string = "";
2175 break;
2176 }
2177
2178 /*
2179 * If support_low_speed is set, use the slow clock prescale,
2180 * which supports 50 bps
2181 */
2182 if (support_low_speed) {
2183 sClockPrescale = 0x19; /* mod 9 (divide by 10) prescale */
2184 rp_baud_base[i] = 230400;
2185 } else {
2186 sClockPrescale = 0x14; /* mod 4 (devide by 5) prescale */
2187 rp_baud_base[i] = 460800;
2188 }
2189
2190 for (aiop = 0; aiop < MAX_AIOPS_PER_BOARD; aiop++)
2191 aiopio[aiop] = rcktpt_io_addr[i] + (aiop * 0x400);
2192
2193 num_aiops = sInitController(ctlp, i, controller + (i * 0x400), aiopio, MAX_AIOPS_PER_BOARD, 0, FREQ_DIS, 0);
2194
2195 if (ctlp->boardType == ROCKET_TYPE_PC104) {
2196 sEnAiop(ctlp, 2); /* only one AIOPIC, but these */
2197 sEnAiop(ctlp, 3); /* CSels used for other stuff */
2198 }
2199
2200 /* If something went wrong initing the AIOP's release the ISA IO memory */
2201 if (num_aiops <= 0) {
2202 release_region(rcktpt_io_addr[i], 64);
2203 rcktpt_io_addr[i] = 0;
2204 return (0);
2205 }
2206
2207 rocketModel[i].startingPortNumber = nextLineNumber;
2208
2209 for (aiop = 0; aiop < num_aiops; aiop++) {
2210 sResetAiopByNum(ctlp, aiop);
2211 sEnAiop(ctlp, aiop);
2212 num_chan = sGetAiopNumChan(ctlp, aiop);
2213 total_num_chan += num_chan;
2214 for (chan = 0; chan < num_chan; chan++)
2215 init_r_port(i, aiop, chan, NULL);
2216 }
2217 is_PCI[i] = 0;
2218 if ((rcktpt_type[i] == ROCKET_TYPE_MODEM) || (rcktpt_type[i] == ROCKET_TYPE_MODEMII)) {
2219 num_chan = sGetAiopNumChan(ctlp, 0);
2220 total_num_chan = num_chan;
2221 for (chan = 0; chan < num_chan; chan++)
2222 sModemReset(ctlp, chan, 1);
2223 msleep(500);
2224 for (chan = 0; chan < num_chan; chan++)
2225 sModemReset(ctlp, chan, 0);
2226 msleep(500);
2227 strcpy(rocketModel[i].modelString, "RocketModem ISA");
2228 } else {
2229 strcpy(rocketModel[i].modelString, "RocketPort ISA");
2230 }
2231 rocketModel[i].numPorts = total_num_chan;
2232 rocketModel[i].model = MODEL_ISA;
2233
2234 printk(KERN_INFO "RocketPort ISA card #%d found at 0x%lx - %d AIOPs %s\n",
2235 i, rcktpt_io_addr[i], num_aiops, type_string);
2236
2237 printk(KERN_INFO "Installing %s, creating /dev/ttyR%d - %ld\n",
2238 rocketModel[i].modelString,
2239 rocketModel[i].startingPortNumber,
2240 rocketModel[i].startingPortNumber +
2241 rocketModel[i].numPorts - 1);
2242
2243 return (1);
2244}
2245
2246static const struct tty_operations rocket_ops = {
2247 .open = rp_open,
2248 .close = rp_close,
2249 .write = rp_write,
2250 .put_char = rp_put_char,
2251 .write_room = rp_write_room,
2252 .chars_in_buffer = rp_chars_in_buffer,
2253 .flush_buffer = rp_flush_buffer,
2254 .ioctl = rp_ioctl,
2255 .throttle = rp_throttle,
2256 .unthrottle = rp_unthrottle,
2257 .set_termios = rp_set_termios,
2258 .stop = rp_stop,
2259 .start = rp_start,
2260 .hangup = rp_hangup,
2261 .break_ctl = rp_break,
2262 .send_xchar = rp_send_xchar,
2263 .wait_until_sent = rp_wait_until_sent,
2264 .tiocmget = rp_tiocmget,
2265 .tiocmset = rp_tiocmset,
2266};
2267
2268static const struct tty_port_operations rocket_port_ops = {
2269 .carrier_raised = carrier_raised,
2270 .dtr_rts = dtr_rts,
2271};
2272
2273/*
2274 * The module "startup" routine; it's run when the module is loaded.
2275 */
2276static int __init rp_init(void)
2277{
2278 int ret = -ENOMEM, pci_boards_found, isa_boards_found, i;
2279
2280 printk(KERN_INFO "RocketPort device driver module, version %s, %s\n",
2281 ROCKET_VERSION, ROCKET_DATE);
2282
2283 rocket_driver = alloc_tty_driver(MAX_RP_PORTS);
2284 if (!rocket_driver)
2285 goto err;
2286
2287 /*
2288 * If board 1 is non-zero, there is at least one ISA configured. If controller is
2289 * zero, use the default controller IO address of board1 + 0x40.
2290 */
2291 if (board1) {
2292 if (controller == 0)
2293 controller = board1 + 0x40;
2294 } else {
2295 controller = 0; /* Used as a flag, meaning no ISA boards */
2296 }
2297
2298 /* If an ISA card is configured, reserve the 4 byte IO space for the Mudbac controller */
2299 if (controller && (!request_region(controller, 4, "Comtrol RocketPort"))) {
2300 printk(KERN_ERR "Unable to reserve IO region for first "
2301 "configured ISA RocketPort controller 0x%lx. "
2302 "Driver exiting\n", controller);
2303 ret = -EBUSY;
2304 goto err_tty;
2305 }
2306
2307 /* Store ISA variable retrieved from command line or .conf file. */
2308 rcktpt_io_addr[0] = board1;
2309 rcktpt_io_addr[1] = board2;
2310 rcktpt_io_addr[2] = board3;
2311 rcktpt_io_addr[3] = board4;
2312
2313 rcktpt_type[0] = modem1 ? ROCKET_TYPE_MODEM : ROCKET_TYPE_NORMAL;
2314 rcktpt_type[0] = pc104_1[0] ? ROCKET_TYPE_PC104 : rcktpt_type[0];
2315 rcktpt_type[1] = modem2 ? ROCKET_TYPE_MODEM : ROCKET_TYPE_NORMAL;
2316 rcktpt_type[1] = pc104_2[0] ? ROCKET_TYPE_PC104 : rcktpt_type[1];
2317 rcktpt_type[2] = modem3 ? ROCKET_TYPE_MODEM : ROCKET_TYPE_NORMAL;
2318 rcktpt_type[2] = pc104_3[0] ? ROCKET_TYPE_PC104 : rcktpt_type[2];
2319 rcktpt_type[3] = modem4 ? ROCKET_TYPE_MODEM : ROCKET_TYPE_NORMAL;
2320 rcktpt_type[3] = pc104_4[0] ? ROCKET_TYPE_PC104 : rcktpt_type[3];
2321
2322 /*
2323 * Set up the tty driver structure and then register this
2324 * driver with the tty layer.
2325 */
2326
2327 rocket_driver->owner = THIS_MODULE;
2328 rocket_driver->flags = TTY_DRIVER_DYNAMIC_DEV;
2329 rocket_driver->name = "ttyR";
2330 rocket_driver->driver_name = "Comtrol RocketPort";
2331 rocket_driver->major = TTY_ROCKET_MAJOR;
2332 rocket_driver->minor_start = 0;
2333 rocket_driver->type = TTY_DRIVER_TYPE_SERIAL;
2334 rocket_driver->subtype = SERIAL_TYPE_NORMAL;
2335 rocket_driver->init_termios = tty_std_termios;
2336 rocket_driver->init_termios.c_cflag =
2337 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2338 rocket_driver->init_termios.c_ispeed = 9600;
2339 rocket_driver->init_termios.c_ospeed = 9600;
2340#ifdef ROCKET_SOFT_FLOW
2341 rocket_driver->flags |= TTY_DRIVER_REAL_RAW;
2342#endif
2343 tty_set_operations(rocket_driver, &rocket_ops);
2344
2345 ret = tty_register_driver(rocket_driver);
2346 if (ret < 0) {
2347 printk(KERN_ERR "Couldn't install tty RocketPort driver\n");
2348 goto err_tty;
2349 }
2350
2351#ifdef ROCKET_DEBUG_OPEN
2352 printk(KERN_INFO "RocketPort driver is major %d\n", rocket_driver.major);
2353#endif
2354
2355 /*
2356 * OK, let's probe each of the controllers looking for boards. Any boards found
2357 * will be initialized here.
2358 */
2359 isa_boards_found = 0;
2360 pci_boards_found = 0;
2361
2362 for (i = 0; i < NUM_BOARDS; i++) {
2363 if (init_ISA(i))
2364 isa_boards_found++;
2365 }
2366
2367#ifdef CONFIG_PCI
2368 if (isa_boards_found < NUM_BOARDS)
2369 pci_boards_found = init_PCI(isa_boards_found);
2370#endif
2371
2372 max_board = pci_boards_found + isa_boards_found;
2373
2374 if (max_board == 0) {
2375 printk(KERN_ERR "No rocketport ports found; unloading driver\n");
2376 ret = -ENXIO;
2377 goto err_ttyu;
2378 }
2379
2380 return 0;
2381err_ttyu:
2382 tty_unregister_driver(rocket_driver);
2383err_tty:
2384 put_tty_driver(rocket_driver);
2385err:
2386 return ret;
2387}
2388
2389
2390static void rp_cleanup_module(void)
2391{
2392 int retval;
2393 int i;
2394
2395 del_timer_sync(&rocket_timer);
2396
2397 retval = tty_unregister_driver(rocket_driver);
2398 if (retval)
2399 printk(KERN_ERR "Error %d while trying to unregister "
2400 "rocketport driver\n", -retval);
2401
2402 for (i = 0; i < MAX_RP_PORTS; i++)
2403 if (rp_table[i]) {
2404 tty_unregister_device(rocket_driver, i);
2405 kfree(rp_table[i]);
2406 }
2407
2408 put_tty_driver(rocket_driver);
2409
2410 for (i = 0; i < NUM_BOARDS; i++) {
2411 if (rcktpt_io_addr[i] <= 0 || is_PCI[i])
2412 continue;
2413 release_region(rcktpt_io_addr[i], 64);
2414 }
2415 if (controller)
2416 release_region(controller, 4);
2417}
2418
2419/***************************************************************************
2420Function: sInitController
2421Purpose: Initialization of controller global registers and controller
2422 structure.
2423Call: sInitController(CtlP,CtlNum,MudbacIO,AiopIOList,AiopIOListSize,
2424 IRQNum,Frequency,PeriodicOnly)
2425 CONTROLLER_T *CtlP; Ptr to controller structure
2426 int CtlNum; Controller number
2427 ByteIO_t MudbacIO; Mudbac base I/O address.
2428 ByteIO_t *AiopIOList; List of I/O addresses for each AIOP.
2429 This list must be in the order the AIOPs will be found on the
2430 controller. Once an AIOP in the list is not found, it is
2431 assumed that there are no more AIOPs on the controller.
2432 int AiopIOListSize; Number of addresses in AiopIOList
2433 int IRQNum; Interrupt Request number. Can be any of the following:
2434 0: Disable global interrupts
2435 3: IRQ 3
2436 4: IRQ 4
2437 5: IRQ 5
2438 9: IRQ 9
2439 10: IRQ 10
2440 11: IRQ 11
2441 12: IRQ 12
2442 15: IRQ 15
2443 Byte_t Frequency: A flag identifying the frequency
2444 of the periodic interrupt, can be any one of the following:
2445 FREQ_DIS - periodic interrupt disabled
2446 FREQ_137HZ - 137 Hertz
2447 FREQ_69HZ - 69 Hertz
2448 FREQ_34HZ - 34 Hertz
2449 FREQ_17HZ - 17 Hertz
2450 FREQ_9HZ - 9 Hertz
2451 FREQ_4HZ - 4 Hertz
2452 If IRQNum is set to 0 the Frequency parameter is
2453 overidden, it is forced to a value of FREQ_DIS.
2454 int PeriodicOnly: 1 if all interrupts except the periodic
2455 interrupt are to be blocked.
2456 0 is both the periodic interrupt and
2457 other channel interrupts are allowed.
2458 If IRQNum is set to 0 the PeriodicOnly parameter is
2459 overidden, it is forced to a value of 0.
2460Return: int: Number of AIOPs on the controller, or CTLID_NULL if controller
2461 initialization failed.
2462
2463Comments:
2464 If periodic interrupts are to be disabled but AIOP interrupts
2465 are allowed, set Frequency to FREQ_DIS and PeriodicOnly to 0.
2466
2467 If interrupts are to be completely disabled set IRQNum to 0.
2468
2469 Setting Frequency to FREQ_DIS and PeriodicOnly to 1 is an
2470 invalid combination.
2471
2472 This function performs initialization of global interrupt modes,
2473 but it does not actually enable global interrupts. To enable
2474 and disable global interrupts use functions sEnGlobalInt() and
2475 sDisGlobalInt(). Enabling of global interrupts is normally not
2476 done until all other initializations are complete.
2477
2478 Even if interrupts are globally enabled, they must also be
2479 individually enabled for each channel that is to generate
2480 interrupts.
2481
2482Warnings: No range checking on any of the parameters is done.
2483
2484 No context switches are allowed while executing this function.
2485
2486 After this function all AIOPs on the controller are disabled,
2487 they can be enabled with sEnAiop().
2488*/
2489static int sInitController(CONTROLLER_T * CtlP, int CtlNum, ByteIO_t MudbacIO,
2490 ByteIO_t * AiopIOList, int AiopIOListSize,
2491 int IRQNum, Byte_t Frequency, int PeriodicOnly)
2492{
2493 int i;
2494 ByteIO_t io;
2495 int done;
2496
2497 CtlP->AiopIntrBits = aiop_intr_bits;
2498 CtlP->AltChanRingIndicator = 0;
2499 CtlP->CtlNum = CtlNum;
2500 CtlP->CtlID = CTLID_0001; /* controller release 1 */
2501 CtlP->BusType = isISA;
2502 CtlP->MBaseIO = MudbacIO;
2503 CtlP->MReg1IO = MudbacIO + 1;
2504 CtlP->MReg2IO = MudbacIO + 2;
2505 CtlP->MReg3IO = MudbacIO + 3;
2506#if 1
2507 CtlP->MReg2 = 0; /* interrupt disable */
2508 CtlP->MReg3 = 0; /* no periodic interrupts */
2509#else
2510 if (sIRQMap[IRQNum] == 0) { /* interrupts globally disabled */
2511 CtlP->MReg2 = 0; /* interrupt disable */
2512 CtlP->MReg3 = 0; /* no periodic interrupts */
2513 } else {
2514 CtlP->MReg2 = sIRQMap[IRQNum]; /* set IRQ number */
2515 CtlP->MReg3 = Frequency; /* set frequency */
2516 if (PeriodicOnly) { /* periodic interrupt only */
2517 CtlP->MReg3 |= PERIODIC_ONLY;
2518 }
2519 }
2520#endif
2521 sOutB(CtlP->MReg2IO, CtlP->MReg2);
2522 sOutB(CtlP->MReg3IO, CtlP->MReg3);
2523 sControllerEOI(CtlP); /* clear EOI if warm init */
2524 /* Init AIOPs */
2525 CtlP->NumAiop = 0;
2526 for (i = done = 0; i < AiopIOListSize; i++) {
2527 io = AiopIOList[i];
2528 CtlP->AiopIO[i] = (WordIO_t) io;
2529 CtlP->AiopIntChanIO[i] = io + _INT_CHAN;
2530 sOutB(CtlP->MReg2IO, CtlP->MReg2 | (i & 0x03)); /* AIOP index */
2531 sOutB(MudbacIO, (Byte_t) (io >> 6)); /* set up AIOP I/O in MUDBAC */
2532 if (done)
2533 continue;
2534 sEnAiop(CtlP, i); /* enable the AIOP */
2535 CtlP->AiopID[i] = sReadAiopID(io); /* read AIOP ID */
2536 if (CtlP->AiopID[i] == AIOPID_NULL) /* if AIOP does not exist */
2537 done = 1; /* done looking for AIOPs */
2538 else {
2539 CtlP->AiopNumChan[i] = sReadAiopNumChan((WordIO_t) io); /* num channels in AIOP */
2540 sOutW((WordIO_t) io + _INDX_ADDR, _CLK_PRE); /* clock prescaler */
2541 sOutB(io + _INDX_DATA, sClockPrescale);
2542 CtlP->NumAiop++; /* bump count of AIOPs */
2543 }
2544 sDisAiop(CtlP, i); /* disable AIOP */
2545 }
2546
2547 if (CtlP->NumAiop == 0)
2548 return (-1);
2549 else
2550 return (CtlP->NumAiop);
2551}
2552
2553/***************************************************************************
2554Function: sPCIInitController
2555Purpose: Initialization of controller global registers and controller
2556 structure.
2557Call: sPCIInitController(CtlP,CtlNum,AiopIOList,AiopIOListSize,
2558 IRQNum,Frequency,PeriodicOnly)
2559 CONTROLLER_T *CtlP; Ptr to controller structure
2560 int CtlNum; Controller number
2561 ByteIO_t *AiopIOList; List of I/O addresses for each AIOP.
2562 This list must be in the order the AIOPs will be found on the
2563 controller. Once an AIOP in the list is not found, it is
2564 assumed that there are no more AIOPs on the controller.
2565 int AiopIOListSize; Number of addresses in AiopIOList
2566 int IRQNum; Interrupt Request number. Can be any of the following:
2567 0: Disable global interrupts
2568 3: IRQ 3
2569 4: IRQ 4
2570 5: IRQ 5
2571 9: IRQ 9
2572 10: IRQ 10
2573 11: IRQ 11
2574 12: IRQ 12
2575 15: IRQ 15
2576 Byte_t Frequency: A flag identifying the frequency
2577 of the periodic interrupt, can be any one of the following:
2578 FREQ_DIS - periodic interrupt disabled
2579 FREQ_137HZ - 137 Hertz
2580 FREQ_69HZ - 69 Hertz
2581 FREQ_34HZ - 34 Hertz
2582 FREQ_17HZ - 17 Hertz
2583 FREQ_9HZ - 9 Hertz
2584 FREQ_4HZ - 4 Hertz
2585 If IRQNum is set to 0 the Frequency parameter is
2586 overidden, it is forced to a value of FREQ_DIS.
2587 int PeriodicOnly: 1 if all interrupts except the periodic
2588 interrupt are to be blocked.
2589 0 is both the periodic interrupt and
2590 other channel interrupts are allowed.
2591 If IRQNum is set to 0 the PeriodicOnly parameter is
2592 overidden, it is forced to a value of 0.
2593Return: int: Number of AIOPs on the controller, or CTLID_NULL if controller
2594 initialization failed.
2595
2596Comments:
2597 If periodic interrupts are to be disabled but AIOP interrupts
2598 are allowed, set Frequency to FREQ_DIS and PeriodicOnly to 0.
2599
2600 If interrupts are to be completely disabled set IRQNum to 0.
2601
2602 Setting Frequency to FREQ_DIS and PeriodicOnly to 1 is an
2603 invalid combination.
2604
2605 This function performs initialization of global interrupt modes,
2606 but it does not actually enable global interrupts. To enable
2607 and disable global interrupts use functions sEnGlobalInt() and
2608 sDisGlobalInt(). Enabling of global interrupts is normally not
2609 done until all other initializations are complete.
2610
2611 Even if interrupts are globally enabled, they must also be
2612 individually enabled for each channel that is to generate
2613 interrupts.
2614
2615Warnings: No range checking on any of the parameters is done.
2616
2617 No context switches are allowed while executing this function.
2618
2619 After this function all AIOPs on the controller are disabled,
2620 they can be enabled with sEnAiop().
2621*/
2622static int sPCIInitController(CONTROLLER_T * CtlP, int CtlNum,
2623 ByteIO_t * AiopIOList, int AiopIOListSize,
2624 WordIO_t ConfigIO, int IRQNum, Byte_t Frequency,
2625 int PeriodicOnly, int altChanRingIndicator,
2626 int UPCIRingInd)
2627{
2628 int i;
2629 ByteIO_t io;
2630
2631 CtlP->AltChanRingIndicator = altChanRingIndicator;
2632 CtlP->UPCIRingInd = UPCIRingInd;
2633 CtlP->CtlNum = CtlNum;
2634 CtlP->CtlID = CTLID_0001; /* controller release 1 */
2635 CtlP->BusType = isPCI; /* controller release 1 */
2636
2637 if (ConfigIO) {
2638 CtlP->isUPCI = 1;
2639 CtlP->PCIIO = ConfigIO + _PCI_9030_INT_CTRL;
2640 CtlP->PCIIO2 = ConfigIO + _PCI_9030_GPIO_CTRL;
2641 CtlP->AiopIntrBits = upci_aiop_intr_bits;
2642 } else {
2643 CtlP->isUPCI = 0;
2644 CtlP->PCIIO =
2645 (WordIO_t) ((ByteIO_t) AiopIOList[0] + _PCI_INT_FUNC);
2646 CtlP->AiopIntrBits = aiop_intr_bits;
2647 }
2648
2649 sPCIControllerEOI(CtlP); /* clear EOI if warm init */
2650 /* Init AIOPs */
2651 CtlP->NumAiop = 0;
2652 for (i = 0; i < AiopIOListSize; i++) {
2653 io = AiopIOList[i];
2654 CtlP->AiopIO[i] = (WordIO_t) io;
2655 CtlP->AiopIntChanIO[i] = io + _INT_CHAN;
2656
2657 CtlP->AiopID[i] = sReadAiopID(io); /* read AIOP ID */
2658 if (CtlP->AiopID[i] == AIOPID_NULL) /* if AIOP does not exist */
2659 break; /* done looking for AIOPs */
2660
2661 CtlP->AiopNumChan[i] = sReadAiopNumChan((WordIO_t) io); /* num channels in AIOP */
2662 sOutW((WordIO_t) io + _INDX_ADDR, _CLK_PRE); /* clock prescaler */
2663 sOutB(io + _INDX_DATA, sClockPrescale);
2664 CtlP->NumAiop++; /* bump count of AIOPs */
2665 }
2666
2667 if (CtlP->NumAiop == 0)
2668 return (-1);
2669 else
2670 return (CtlP->NumAiop);
2671}
2672
2673/***************************************************************************
2674Function: sReadAiopID
2675Purpose: Read the AIOP idenfication number directly from an AIOP.
2676Call: sReadAiopID(io)
2677 ByteIO_t io: AIOP base I/O address
2678Return: int: Flag AIOPID_XXXX if a valid AIOP is found, where X
2679 is replace by an identifying number.
2680 Flag AIOPID_NULL if no valid AIOP is found
2681Warnings: No context switches are allowed while executing this function.
2682
2683*/
2684static int sReadAiopID(ByteIO_t io)
2685{
2686 Byte_t AiopID; /* ID byte from AIOP */
2687
2688 sOutB(io + _CMD_REG, RESET_ALL); /* reset AIOP */
2689 sOutB(io + _CMD_REG, 0x0);
2690 AiopID = sInW(io + _CHN_STAT0) & 0x07;
2691 if (AiopID == 0x06)
2692 return (1);
2693 else /* AIOP does not exist */
2694 return (-1);
2695}
2696
2697/***************************************************************************
2698Function: sReadAiopNumChan
2699Purpose: Read the number of channels available in an AIOP directly from
2700 an AIOP.
2701Call: sReadAiopNumChan(io)
2702 WordIO_t io: AIOP base I/O address
2703Return: int: The number of channels available
2704Comments: The number of channels is determined by write/reads from identical
2705 offsets within the SRAM address spaces for channels 0 and 4.
2706 If the channel 4 space is mirrored to channel 0 it is a 4 channel
2707 AIOP, otherwise it is an 8 channel.
2708Warnings: No context switches are allowed while executing this function.
2709*/
2710static int sReadAiopNumChan(WordIO_t io)
2711{
2712 Word_t x;
2713 static Byte_t R[4] = { 0x00, 0x00, 0x34, 0x12 };
2714
2715 /* write to chan 0 SRAM */
2716 out32((DWordIO_t) io + _INDX_ADDR, R);
2717 sOutW(io + _INDX_ADDR, 0); /* read from SRAM, chan 0 */
2718 x = sInW(io + _INDX_DATA);
2719 sOutW(io + _INDX_ADDR, 0x4000); /* read from SRAM, chan 4 */
2720 if (x != sInW(io + _INDX_DATA)) /* if different must be 8 chan */
2721 return (8);
2722 else
2723 return (4);
2724}
2725
2726/***************************************************************************
2727Function: sInitChan
2728Purpose: Initialization of a channel and channel structure
2729Call: sInitChan(CtlP,ChP,AiopNum,ChanNum)
2730 CONTROLLER_T *CtlP; Ptr to controller structure
2731 CHANNEL_T *ChP; Ptr to channel structure
2732 int AiopNum; AIOP number within controller
2733 int ChanNum; Channel number within AIOP
2734Return: int: 1 if initialization succeeded, 0 if it fails because channel
2735 number exceeds number of channels available in AIOP.
2736Comments: This function must be called before a channel can be used.
2737Warnings: No range checking on any of the parameters is done.
2738
2739 No context switches are allowed while executing this function.
2740*/
2741static int sInitChan(CONTROLLER_T * CtlP, CHANNEL_T * ChP, int AiopNum,
2742 int ChanNum)
2743{
2744 int i;
2745 WordIO_t AiopIO;
2746 WordIO_t ChIOOff;
2747 Byte_t *ChR;
2748 Word_t ChOff;
2749 static Byte_t R[4];
2750 int brd9600;
2751
2752 if (ChanNum >= CtlP->AiopNumChan[AiopNum])
2753 return 0; /* exceeds num chans in AIOP */
2754
2755 /* Channel, AIOP, and controller identifiers */
2756 ChP->CtlP = CtlP;
2757 ChP->ChanID = CtlP->AiopID[AiopNum];
2758 ChP->AiopNum = AiopNum;
2759 ChP->ChanNum = ChanNum;
2760
2761 /* Global direct addresses */
2762 AiopIO = CtlP->AiopIO[AiopNum];
2763 ChP->Cmd = (ByteIO_t) AiopIO + _CMD_REG;
2764 ChP->IntChan = (ByteIO_t) AiopIO + _INT_CHAN;
2765 ChP->IntMask = (ByteIO_t) AiopIO + _INT_MASK;
2766 ChP->IndexAddr = (DWordIO_t) AiopIO + _INDX_ADDR;
2767 ChP->IndexData = AiopIO + _INDX_DATA;
2768
2769 /* Channel direct addresses */
2770 ChIOOff = AiopIO + ChP->ChanNum * 2;
2771 ChP->TxRxData = ChIOOff + _TD0;
2772 ChP->ChanStat = ChIOOff + _CHN_STAT0;
2773 ChP->TxRxCount = ChIOOff + _FIFO_CNT0;
2774 ChP->IntID = (ByteIO_t) AiopIO + ChP->ChanNum + _INT_ID0;
2775
2776 /* Initialize the channel from the RData array */
2777 for (i = 0; i < RDATASIZE; i += 4) {
2778 R[0] = RData[i];
2779 R[1] = RData[i + 1] + 0x10 * ChanNum;
2780 R[2] = RData[i + 2];
2781 R[3] = RData[i + 3];
2782 out32(ChP->IndexAddr, R);
2783 }
2784
2785 ChR = ChP->R;
2786 for (i = 0; i < RREGDATASIZE; i += 4) {
2787 ChR[i] = RRegData[i];
2788 ChR[i + 1] = RRegData[i + 1] + 0x10 * ChanNum;
2789 ChR[i + 2] = RRegData[i + 2];
2790 ChR[i + 3] = RRegData[i + 3];
2791 }
2792
2793 /* Indexed registers */
2794 ChOff = (Word_t) ChanNum *0x1000;
2795
2796 if (sClockPrescale == 0x14)
2797 brd9600 = 47;
2798 else
2799 brd9600 = 23;
2800
2801 ChP->BaudDiv[0] = (Byte_t) (ChOff + _BAUD);
2802 ChP->BaudDiv[1] = (Byte_t) ((ChOff + _BAUD) >> 8);
2803 ChP->BaudDiv[2] = (Byte_t) brd9600;
2804 ChP->BaudDiv[3] = (Byte_t) (brd9600 >> 8);
2805 out32(ChP->IndexAddr, ChP->BaudDiv);
2806
2807 ChP->TxControl[0] = (Byte_t) (ChOff + _TX_CTRL);
2808 ChP->TxControl[1] = (Byte_t) ((ChOff + _TX_CTRL) >> 8);
2809 ChP->TxControl[2] = 0;
2810 ChP->TxControl[3] = 0;
2811 out32(ChP->IndexAddr, ChP->TxControl);
2812
2813 ChP->RxControl[0] = (Byte_t) (ChOff + _RX_CTRL);
2814 ChP->RxControl[1] = (Byte_t) ((ChOff + _RX_CTRL) >> 8);
2815 ChP->RxControl[2] = 0;
2816 ChP->RxControl[3] = 0;
2817 out32(ChP->IndexAddr, ChP->RxControl);
2818
2819 ChP->TxEnables[0] = (Byte_t) (ChOff + _TX_ENBLS);
2820 ChP->TxEnables[1] = (Byte_t) ((ChOff + _TX_ENBLS) >> 8);
2821 ChP->TxEnables[2] = 0;
2822 ChP->TxEnables[3] = 0;
2823 out32(ChP->IndexAddr, ChP->TxEnables);
2824
2825 ChP->TxCompare[0] = (Byte_t) (ChOff + _TXCMP1);
2826 ChP->TxCompare[1] = (Byte_t) ((ChOff + _TXCMP1) >> 8);
2827 ChP->TxCompare[2] = 0;
2828 ChP->TxCompare[3] = 0;
2829 out32(ChP->IndexAddr, ChP->TxCompare);
2830
2831 ChP->TxReplace1[0] = (Byte_t) (ChOff + _TXREP1B1);
2832 ChP->TxReplace1[1] = (Byte_t) ((ChOff + _TXREP1B1) >> 8);
2833 ChP->TxReplace1[2] = 0;
2834 ChP->TxReplace1[3] = 0;
2835 out32(ChP->IndexAddr, ChP->TxReplace1);
2836
2837 ChP->TxReplace2[0] = (Byte_t) (ChOff + _TXREP2);
2838 ChP->TxReplace2[1] = (Byte_t) ((ChOff + _TXREP2) >> 8);
2839 ChP->TxReplace2[2] = 0;
2840 ChP->TxReplace2[3] = 0;
2841 out32(ChP->IndexAddr, ChP->TxReplace2);
2842
2843 ChP->TxFIFOPtrs = ChOff + _TXF_OUTP;
2844 ChP->TxFIFO = ChOff + _TX_FIFO;
2845
2846 sOutB(ChP->Cmd, (Byte_t) ChanNum | RESTXFCNT); /* apply reset Tx FIFO count */
2847 sOutB(ChP->Cmd, (Byte_t) ChanNum); /* remove reset Tx FIFO count */
2848 sOutW((WordIO_t) ChP->IndexAddr, ChP->TxFIFOPtrs); /* clear Tx in/out ptrs */
2849 sOutW(ChP->IndexData, 0);
2850 ChP->RxFIFOPtrs = ChOff + _RXF_OUTP;
2851 ChP->RxFIFO = ChOff + _RX_FIFO;
2852
2853 sOutB(ChP->Cmd, (Byte_t) ChanNum | RESRXFCNT); /* apply reset Rx FIFO count */
2854 sOutB(ChP->Cmd, (Byte_t) ChanNum); /* remove reset Rx FIFO count */
2855 sOutW((WordIO_t) ChP->IndexAddr, ChP->RxFIFOPtrs); /* clear Rx out ptr */
2856 sOutW(ChP->IndexData, 0);
2857 sOutW((WordIO_t) ChP->IndexAddr, ChP->RxFIFOPtrs + 2); /* clear Rx in ptr */
2858 sOutW(ChP->IndexData, 0);
2859 ChP->TxPrioCnt = ChOff + _TXP_CNT;
2860 sOutW((WordIO_t) ChP->IndexAddr, ChP->TxPrioCnt);
2861 sOutB(ChP->IndexData, 0);
2862 ChP->TxPrioPtr = ChOff + _TXP_PNTR;
2863 sOutW((WordIO_t) ChP->IndexAddr, ChP->TxPrioPtr);
2864 sOutB(ChP->IndexData, 0);
2865 ChP->TxPrioBuf = ChOff + _TXP_BUF;
2866 sEnRxProcessor(ChP); /* start the Rx processor */
2867
2868 return 1;
2869}
2870
2871/***************************************************************************
2872Function: sStopRxProcessor
2873Purpose: Stop the receive processor from processing a channel.
2874Call: sStopRxProcessor(ChP)
2875 CHANNEL_T *ChP; Ptr to channel structure
2876
2877Comments: The receive processor can be started again with sStartRxProcessor().
2878 This function causes the receive processor to skip over the
2879 stopped channel. It does not stop it from processing other channels.
2880
2881Warnings: No context switches are allowed while executing this function.
2882
2883 Do not leave the receive processor stopped for more than one
2884 character time.
2885
2886 After calling this function a delay of 4 uS is required to ensure
2887 that the receive processor is no longer processing this channel.
2888*/
2889static void sStopRxProcessor(CHANNEL_T * ChP)
2890{
2891 Byte_t R[4];
2892
2893 R[0] = ChP->R[0];
2894 R[1] = ChP->R[1];
2895 R[2] = 0x0a;
2896 R[3] = ChP->R[3];
2897 out32(ChP->IndexAddr, R);
2898}
2899
2900/***************************************************************************
2901Function: sFlushRxFIFO
2902Purpose: Flush the Rx FIFO
2903Call: sFlushRxFIFO(ChP)
2904 CHANNEL_T *ChP; Ptr to channel structure
2905Return: void
2906Comments: To prevent data from being enqueued or dequeued in the Tx FIFO
2907 while it is being flushed the receive processor is stopped
2908 and the transmitter is disabled. After these operations a
2909 4 uS delay is done before clearing the pointers to allow
2910 the receive processor to stop. These items are handled inside
2911 this function.
2912Warnings: No context switches are allowed while executing this function.
2913*/
2914static void sFlushRxFIFO(CHANNEL_T * ChP)
2915{
2916 int i;
2917 Byte_t Ch; /* channel number within AIOP */
2918 int RxFIFOEnabled; /* 1 if Rx FIFO enabled */
2919
2920 if (sGetRxCnt(ChP) == 0) /* Rx FIFO empty */
2921 return; /* don't need to flush */
2922
2923 RxFIFOEnabled = 0;
2924 if (ChP->R[0x32] == 0x08) { /* Rx FIFO is enabled */
2925 RxFIFOEnabled = 1;
2926 sDisRxFIFO(ChP); /* disable it */
2927 for (i = 0; i < 2000 / 200; i++) /* delay 2 uS to allow proc to disable FIFO */
2928 sInB(ChP->IntChan); /* depends on bus i/o timing */
2929 }
2930 sGetChanStatus(ChP); /* clear any pending Rx errors in chan stat */
2931 Ch = (Byte_t) sGetChanNum(ChP);
2932 sOutB(ChP->Cmd, Ch | RESRXFCNT); /* apply reset Rx FIFO count */
2933 sOutB(ChP->Cmd, Ch); /* remove reset Rx FIFO count */
2934 sOutW((WordIO_t) ChP->IndexAddr, ChP->RxFIFOPtrs); /* clear Rx out ptr */
2935 sOutW(ChP->IndexData, 0);
2936 sOutW((WordIO_t) ChP->IndexAddr, ChP->RxFIFOPtrs + 2); /* clear Rx in ptr */
2937 sOutW(ChP->IndexData, 0);
2938 if (RxFIFOEnabled)
2939 sEnRxFIFO(ChP); /* enable Rx FIFO */
2940}
2941
2942/***************************************************************************
2943Function: sFlushTxFIFO
2944Purpose: Flush the Tx FIFO
2945Call: sFlushTxFIFO(ChP)
2946 CHANNEL_T *ChP; Ptr to channel structure
2947Return: void
2948Comments: To prevent data from being enqueued or dequeued in the Tx FIFO
2949 while it is being flushed the receive processor is stopped
2950 and the transmitter is disabled. After these operations a
2951 4 uS delay is done before clearing the pointers to allow
2952 the receive processor to stop. These items are handled inside
2953 this function.
2954Warnings: No context switches are allowed while executing this function.
2955*/
2956static void sFlushTxFIFO(CHANNEL_T * ChP)
2957{
2958 int i;
2959 Byte_t Ch; /* channel number within AIOP */
2960 int TxEnabled; /* 1 if transmitter enabled */
2961
2962 if (sGetTxCnt(ChP) == 0) /* Tx FIFO empty */
2963 return; /* don't need to flush */
2964
2965 TxEnabled = 0;
2966 if (ChP->TxControl[3] & TX_ENABLE) {
2967 TxEnabled = 1;
2968 sDisTransmit(ChP); /* disable transmitter */
2969 }
2970 sStopRxProcessor(ChP); /* stop Rx processor */
2971 for (i = 0; i < 4000 / 200; i++) /* delay 4 uS to allow proc to stop */
2972 sInB(ChP->IntChan); /* depends on bus i/o timing */
2973 Ch = (Byte_t) sGetChanNum(ChP);
2974 sOutB(ChP->Cmd, Ch | RESTXFCNT); /* apply reset Tx FIFO count */
2975 sOutB(ChP->Cmd, Ch); /* remove reset Tx FIFO count */
2976 sOutW((WordIO_t) ChP->IndexAddr, ChP->TxFIFOPtrs); /* clear Tx in/out ptrs */
2977 sOutW(ChP->IndexData, 0);
2978 if (TxEnabled)
2979 sEnTransmit(ChP); /* enable transmitter */
2980 sStartRxProcessor(ChP); /* restart Rx processor */
2981}
2982
2983/***************************************************************************
2984Function: sWriteTxPrioByte
2985Purpose: Write a byte of priority transmit data to a channel
2986Call: sWriteTxPrioByte(ChP,Data)
2987 CHANNEL_T *ChP; Ptr to channel structure
2988 Byte_t Data; The transmit data byte
2989
2990Return: int: 1 if the bytes is successfully written, otherwise 0.
2991
2992Comments: The priority byte is transmitted before any data in the Tx FIFO.
2993
2994Warnings: No context switches are allowed while executing this function.
2995*/
2996static int sWriteTxPrioByte(CHANNEL_T * ChP, Byte_t Data)
2997{
2998 Byte_t DWBuf[4]; /* buffer for double word writes */
2999 Word_t *WordPtr; /* must be far because Win SS != DS */
3000 register DWordIO_t IndexAddr;
3001
3002 if (sGetTxCnt(ChP) > 1) { /* write it to Tx priority buffer */
3003 IndexAddr = ChP->IndexAddr;
3004 sOutW((WordIO_t) IndexAddr, ChP->TxPrioCnt); /* get priority buffer status */
3005 if (sInB((ByteIO_t) ChP->IndexData) & PRI_PEND) /* priority buffer busy */
3006 return (0); /* nothing sent */
3007
3008 WordPtr = (Word_t *) (&DWBuf[0]);
3009 *WordPtr = ChP->TxPrioBuf; /* data byte address */
3010
3011 DWBuf[2] = Data; /* data byte value */
3012 out32(IndexAddr, DWBuf); /* write it out */
3013
3014 *WordPtr = ChP->TxPrioCnt; /* Tx priority count address */
3015
3016 DWBuf[2] = PRI_PEND + 1; /* indicate 1 byte pending */
3017 DWBuf[3] = 0; /* priority buffer pointer */
3018 out32(IndexAddr, DWBuf); /* write it out */
3019 } else { /* write it to Tx FIFO */
3020
3021 sWriteTxByte(sGetTxRxDataIO(ChP), Data);
3022 }
3023 return (1); /* 1 byte sent */
3024}
3025
3026/***************************************************************************
3027Function: sEnInterrupts
3028Purpose: Enable one or more interrupts for a channel
3029Call: sEnInterrupts(ChP,Flags)
3030 CHANNEL_T *ChP; Ptr to channel structure
3031 Word_t Flags: Interrupt enable flags, can be any combination
3032 of the following flags:
3033 TXINT_EN: Interrupt on Tx FIFO empty
3034 RXINT_EN: Interrupt on Rx FIFO at trigger level (see
3035 sSetRxTrigger())
3036 SRCINT_EN: Interrupt on SRC (Special Rx Condition)
3037 MCINT_EN: Interrupt on modem input change
3038 CHANINT_EN: Allow channel interrupt signal to the AIOP's
3039 Interrupt Channel Register.
3040Return: void
3041Comments: If an interrupt enable flag is set in Flags, that interrupt will be
3042 enabled. If an interrupt enable flag is not set in Flags, that
3043 interrupt will not be changed. Interrupts can be disabled with
3044 function sDisInterrupts().
3045
3046 This function sets the appropriate bit for the channel in the AIOP's
3047 Interrupt Mask Register if the CHANINT_EN flag is set. This allows
3048 this channel's bit to be set in the AIOP's Interrupt Channel Register.
3049
3050 Interrupts must also be globally enabled before channel interrupts
3051 will be passed on to the host. This is done with function
3052 sEnGlobalInt().
3053
3054 In some cases it may be desirable to disable interrupts globally but
3055 enable channel interrupts. This would allow the global interrupt
3056 status register to be used to determine which AIOPs need service.
3057*/
3058static void sEnInterrupts(CHANNEL_T * ChP, Word_t Flags)
3059{
3060 Byte_t Mask; /* Interrupt Mask Register */
3061
3062 ChP->RxControl[2] |=
3063 ((Byte_t) Flags & (RXINT_EN | SRCINT_EN | MCINT_EN));
3064
3065 out32(ChP->IndexAddr, ChP->RxControl);
3066
3067 ChP->TxControl[2] |= ((Byte_t) Flags & TXINT_EN);
3068
3069 out32(ChP->IndexAddr, ChP->TxControl);
3070
3071 if (Flags & CHANINT_EN) {
3072 Mask = sInB(ChP->IntMask) | sBitMapSetTbl[ChP->ChanNum];
3073 sOutB(ChP->IntMask, Mask);
3074 }
3075}
3076
3077/***************************************************************************
3078Function: sDisInterrupts
3079Purpose: Disable one or more interrupts for a channel
3080Call: sDisInterrupts(ChP,Flags)
3081 CHANNEL_T *ChP; Ptr to channel structure
3082 Word_t Flags: Interrupt flags, can be any combination
3083 of the following flags:
3084 TXINT_EN: Interrupt on Tx FIFO empty
3085 RXINT_EN: Interrupt on Rx FIFO at trigger level (see
3086 sSetRxTrigger())
3087 SRCINT_EN: Interrupt on SRC (Special Rx Condition)
3088 MCINT_EN: Interrupt on modem input change
3089 CHANINT_EN: Disable channel interrupt signal to the
3090 AIOP's Interrupt Channel Register.
3091Return: void
3092Comments: If an interrupt flag is set in Flags, that interrupt will be
3093 disabled. If an interrupt flag is not set in Flags, that
3094 interrupt will not be changed. Interrupts can be enabled with
3095 function sEnInterrupts().
3096
3097 This function clears the appropriate bit for the channel in the AIOP's
3098 Interrupt Mask Register if the CHANINT_EN flag is set. This blocks
3099 this channel's bit from being set in the AIOP's Interrupt Channel
3100 Register.
3101*/
3102static void sDisInterrupts(CHANNEL_T * ChP, Word_t Flags)
3103{
3104 Byte_t Mask; /* Interrupt Mask Register */
3105
3106 ChP->RxControl[2] &=
3107 ~((Byte_t) Flags & (RXINT_EN | SRCINT_EN | MCINT_EN));
3108 out32(ChP->IndexAddr, ChP->RxControl);
3109 ChP->TxControl[2] &= ~((Byte_t) Flags & TXINT_EN);
3110 out32(ChP->IndexAddr, ChP->TxControl);
3111
3112 if (Flags & CHANINT_EN) {
3113 Mask = sInB(ChP->IntMask) & sBitMapClrTbl[ChP->ChanNum];
3114 sOutB(ChP->IntMask, Mask);
3115 }
3116}
3117
3118static void sSetInterfaceMode(CHANNEL_T * ChP, Byte_t mode)
3119{
3120 sOutB(ChP->CtlP->AiopIO[2], (mode & 0x18) | ChP->ChanNum);
3121}
3122
3123/*
3124 * Not an official SSCI function, but how to reset RocketModems.
3125 * ISA bus version
3126 */
3127static void sModemReset(CONTROLLER_T * CtlP, int chan, int on)
3128{
3129 ByteIO_t addr;
3130 Byte_t val;
3131
3132 addr = CtlP->AiopIO[0] + 0x400;
3133 val = sInB(CtlP->MReg3IO);
3134 /* if AIOP[1] is not enabled, enable it */
3135 if ((val & 2) == 0) {
3136 val = sInB(CtlP->MReg2IO);
3137 sOutB(CtlP->MReg2IO, (val & 0xfc) | (1 & 0x03));
3138 sOutB(CtlP->MBaseIO, (unsigned char) (addr >> 6));
3139 }
3140
3141 sEnAiop(CtlP, 1);
3142 if (!on)
3143 addr += 8;
3144 sOutB(addr + chan, 0); /* apply or remove reset */
3145 sDisAiop(CtlP, 1);
3146}
3147
3148/*
3149 * Not an official SSCI function, but how to reset RocketModems.
3150 * PCI bus version
3151 */
3152static void sPCIModemReset(CONTROLLER_T * CtlP, int chan, int on)
3153{
3154 ByteIO_t addr;
3155
3156 addr = CtlP->AiopIO[0] + 0x40; /* 2nd AIOP */
3157 if (!on)
3158 addr += 8;
3159 sOutB(addr + chan, 0); /* apply or remove reset */
3160}
3161
3162/* Resets the speaker controller on RocketModem II and III devices */
3163static void rmSpeakerReset(CONTROLLER_T * CtlP, unsigned long model)
3164{
3165 ByteIO_t addr;
3166
3167 /* RocketModem II speaker control is at the 8th port location of offset 0x40 */
3168 if ((model == MODEL_RP4M) || (model == MODEL_RP6M)) {
3169 addr = CtlP->AiopIO[0] + 0x4F;
3170 sOutB(addr, 0);
3171 }
3172
3173 /* RocketModem III speaker control is at the 1st port location of offset 0x80 */
3174 if ((model == MODEL_UPCI_RM3_8PORT)
3175 || (model == MODEL_UPCI_RM3_4PORT)) {
3176 addr = CtlP->AiopIO[0] + 0x88;
3177 sOutB(addr, 0);
3178 }
3179}
3180
3181/* Returns the line number given the controller (board), aiop and channel number */
3182static unsigned char GetLineNumber(int ctrl, int aiop, int ch)
3183{
3184 return lineNumbers[(ctrl << 5) | (aiop << 3) | ch];
3185}
3186
3187/*
3188 * Stores the line number associated with a given controller (board), aiop
3189 * and channel number.
3190 * Returns: The line number assigned
3191 */
3192static unsigned char SetLineNumber(int ctrl, int aiop, int ch)
3193{
3194 lineNumbers[(ctrl << 5) | (aiop << 3) | ch] = nextLineNumber++;
3195 return (nextLineNumber - 1);
3196}
diff --git a/drivers/char/rocket.h b/drivers/char/rocket.h
deleted file mode 100644
index ec863f35f1a9..000000000000
--- a/drivers/char/rocket.h
+++ /dev/null
@@ -1,111 +0,0 @@
1/*
2 * rocket.h --- the exported interface of the rocket driver to its configuration program.
3 *
4 * Written by Theodore Ts'o, Copyright 1997.
5 * Copyright 1997 Comtrol Corporation.
6 *
7 */
8
9/* Model Information Struct */
10typedef struct {
11 unsigned long model;
12 char modelString[80];
13 unsigned long numPorts;
14 int loadrm2;
15 int startingPortNumber;
16} rocketModel_t;
17
18struct rocket_config {
19 int line;
20 int flags;
21 int closing_wait;
22 int close_delay;
23 int port;
24 int reserved[32];
25};
26
27struct rocket_ports {
28 int tty_major;
29 int callout_major;
30 rocketModel_t rocketModel[8];
31};
32
33struct rocket_version {
34 char rocket_version[32];
35 char rocket_date[32];
36 char reserved[64];
37};
38
39/*
40 * Rocketport flags
41 */
42/*#define ROCKET_CALLOUT_NOHUP 0x00000001 */
43#define ROCKET_FORCE_CD 0x00000002
44#define ROCKET_HUP_NOTIFY 0x00000004
45#define ROCKET_SPLIT_TERMIOS 0x00000008
46#define ROCKET_SPD_MASK 0x00000070
47#define ROCKET_SPD_HI 0x00000010 /* Use 56000 instead of 38400 bps */
48#define ROCKET_SPD_VHI 0x00000020 /* Use 115200 instead of 38400 bps */
49#define ROCKET_SPD_SHI 0x00000030 /* Use 230400 instead of 38400 bps */
50#define ROCKET_SPD_WARP 0x00000040 /* Use 460800 instead of 38400 bps */
51#define ROCKET_SAK 0x00000080
52#define ROCKET_SESSION_LOCKOUT 0x00000100
53#define ROCKET_PGRP_LOCKOUT 0x00000200
54#define ROCKET_RTS_TOGGLE 0x00000400
55#define ROCKET_MODE_MASK 0x00003000
56#define ROCKET_MODE_RS232 0x00000000
57#define ROCKET_MODE_RS485 0x00001000
58#define ROCKET_MODE_RS422 0x00002000
59#define ROCKET_FLAGS 0x00003FFF
60
61#define ROCKET_USR_MASK 0x0071 /* Legal flags that non-privileged
62 * users can set or reset */
63
64/*
65 * For closing_wait and closing_wait2
66 */
67#define ROCKET_CLOSING_WAIT_NONE ASYNC_CLOSING_WAIT_NONE
68#define ROCKET_CLOSING_WAIT_INF ASYNC_CLOSING_WAIT_INF
69
70/*
71 * Rocketport ioctls -- "RP"
72 */
73#define RCKP_GET_STRUCT 0x00525001
74#define RCKP_GET_CONFIG 0x00525002
75#define RCKP_SET_CONFIG 0x00525003
76#define RCKP_GET_PORTS 0x00525004
77#define RCKP_RESET_RM2 0x00525005
78#define RCKP_GET_VERSION 0x00525006
79
80/* Rocketport Models */
81#define MODEL_RP32INTF 0x0001 /* RP 32 port w/external I/F */
82#define MODEL_RP8INTF 0x0002 /* RP 8 port w/external I/F */
83#define MODEL_RP16INTF 0x0003 /* RP 16 port w/external I/F */
84#define MODEL_RP8OCTA 0x0005 /* RP 8 port w/octa cable */
85#define MODEL_RP4QUAD 0x0004 /* RP 4 port w/quad cable */
86#define MODEL_RP8J 0x0006 /* RP 8 port w/RJ11 connectors */
87#define MODEL_RP4J 0x0007 /* RP 4 port w/RJ45 connectors */
88#define MODEL_RP8SNI 0x0008 /* RP 8 port w/ DB78 SNI connector */
89#define MODEL_RP16SNI 0x0009 /* RP 16 port w/ DB78 SNI connector */
90#define MODEL_RPP4 0x000A /* RP Plus 4 port */
91#define MODEL_RPP8 0x000B /* RP Plus 8 port */
92#define MODEL_RP2_232 0x000E /* RP Plus 2 port RS232 */
93#define MODEL_RP2_422 0x000F /* RP Plus 2 port RS232 */
94
95/* Rocketmodem II Models */
96#define MODEL_RP6M 0x000C /* RM 6 port */
97#define MODEL_RP4M 0x000D /* RM 4 port */
98
99/* Universal PCI boards */
100#define MODEL_UPCI_RP32INTF 0x0801 /* RP UPCI 32 port w/external I/F */
101#define MODEL_UPCI_RP8INTF 0x0802 /* RP UPCI 8 port w/external I/F */
102#define MODEL_UPCI_RP16INTF 0x0803 /* RP UPCI 16 port w/external I/F */
103#define MODEL_UPCI_RP8OCTA 0x0805 /* RP UPCI 8 port w/octa cable */
104#define MODEL_UPCI_RM3_8PORT 0x080C /* RP UPCI Rocketmodem III 8 port */
105#define MODEL_UPCI_RM3_4PORT 0x080C /* RP UPCI Rocketmodem III 4 port */
106
107/* Compact PCI 16 port */
108#define MODEL_CPCI_RP16INTF 0x0903 /* RP Compact PCI 16 port w/external I/F */
109
110/* All ISA boards */
111#define MODEL_ISA 0x1000
diff --git a/drivers/char/rocket_int.h b/drivers/char/rocket_int.h
deleted file mode 100644
index 67e0f1e778a2..000000000000
--- a/drivers/char/rocket_int.h
+++ /dev/null
@@ -1,1214 +0,0 @@
1/*
2 * rocket_int.h --- internal header file for rocket.c
3 *
4 * Written by Theodore Ts'o, Copyright 1997.
5 * Copyright 1997 Comtrol Corporation.
6 *
7 */
8
9/*
10 * Definition of the types in rcktpt_type
11 */
12#define ROCKET_TYPE_NORMAL 0
13#define ROCKET_TYPE_MODEM 1
14#define ROCKET_TYPE_MODEMII 2
15#define ROCKET_TYPE_MODEMIII 3
16#define ROCKET_TYPE_PC104 4
17
18#include <linux/mutex.h>
19
20#include <asm/io.h>
21#include <asm/byteorder.h>
22
23typedef unsigned char Byte_t;
24typedef unsigned int ByteIO_t;
25
26typedef unsigned int Word_t;
27typedef unsigned int WordIO_t;
28
29typedef unsigned int DWordIO_t;
30
31/*
32 * Note! Normally the Linux I/O macros already take care of
33 * byte-swapping the I/O instructions. However, all accesses using
34 * sOutDW aren't really 32-bit accesses, but should be handled in byte
35 * order. Hence the use of the cpu_to_le32() macro to byte-swap
36 * things to no-op the byte swapping done by the big-endian outl()
37 * instruction.
38 */
39
40static inline void sOutB(unsigned short port, unsigned char value)
41{
42#ifdef ROCKET_DEBUG_IO
43 printk(KERN_DEBUG "sOutB(%x, %x)...\n", port, value);
44#endif
45 outb_p(value, port);
46}
47
48static inline void sOutW(unsigned short port, unsigned short value)
49{
50#ifdef ROCKET_DEBUG_IO
51 printk(KERN_DEBUG "sOutW(%x, %x)...\n", port, value);
52#endif
53 outw_p(value, port);
54}
55
56static inline void out32(unsigned short port, Byte_t *p)
57{
58 u32 value = get_unaligned_le32(p);
59#ifdef ROCKET_DEBUG_IO
60 printk(KERN_DEBUG "out32(%x, %lx)...\n", port, value);
61#endif
62 outl_p(value, port);
63}
64
65static inline unsigned char sInB(unsigned short port)
66{
67 return inb_p(port);
68}
69
70static inline unsigned short sInW(unsigned short port)
71{
72 return inw_p(port);
73}
74
75/* This is used to move arrays of bytes so byte swapping isn't appropriate. */
76#define sOutStrW(port, addr, count) if (count) outsw(port, addr, count)
77#define sInStrW(port, addr, count) if (count) insw(port, addr, count)
78
79#define CTL_SIZE 8
80#define AIOP_CTL_SIZE 4
81#define CHAN_AIOP_SIZE 8
82#define MAX_PORTS_PER_AIOP 8
83#define MAX_AIOPS_PER_BOARD 4
84#define MAX_PORTS_PER_BOARD 32
85
86/* Bus type ID */
87#define isISA 0
88#define isPCI 1
89#define isMC 2
90
91/* Controller ID numbers */
92#define CTLID_NULL -1 /* no controller exists */
93#define CTLID_0001 0x0001 /* controller release 1 */
94
95/* AIOP ID numbers, identifies AIOP type implementing channel */
96#define AIOPID_NULL -1 /* no AIOP or channel exists */
97#define AIOPID_0001 0x0001 /* AIOP release 1 */
98
99/************************************************************************
100 Global Register Offsets - Direct Access - Fixed values
101************************************************************************/
102
103#define _CMD_REG 0x38 /* Command Register 8 Write */
104#define _INT_CHAN 0x39 /* Interrupt Channel Register 8 Read */
105#define _INT_MASK 0x3A /* Interrupt Mask Register 8 Read / Write */
106#define _UNUSED 0x3B /* Unused 8 */
107#define _INDX_ADDR 0x3C /* Index Register Address 16 Write */
108#define _INDX_DATA 0x3E /* Index Register Data 8/16 Read / Write */
109
110/************************************************************************
111 Channel Register Offsets for 1st channel in AIOP - Direct Access
112************************************************************************/
113#define _TD0 0x00 /* Transmit Data 16 Write */
114#define _RD0 0x00 /* Receive Data 16 Read */
115#define _CHN_STAT0 0x20 /* Channel Status 8/16 Read / Write */
116#define _FIFO_CNT0 0x10 /* Transmit/Receive FIFO Count 16 Read */
117#define _INT_ID0 0x30 /* Interrupt Identification 8 Read */
118
119/************************************************************************
120 Tx Control Register Offsets - Indexed - External - Fixed
121************************************************************************/
122#define _TX_ENBLS 0x980 /* Tx Processor Enables Register 8 Read / Write */
123#define _TXCMP1 0x988 /* Transmit Compare Value #1 8 Read / Write */
124#define _TXCMP2 0x989 /* Transmit Compare Value #2 8 Read / Write */
125#define _TXREP1B1 0x98A /* Tx Replace Value #1 - Byte 1 8 Read / Write */
126#define _TXREP1B2 0x98B /* Tx Replace Value #1 - Byte 2 8 Read / Write */
127#define _TXREP2 0x98C /* Transmit Replace Value #2 8 Read / Write */
128
129/************************************************************************
130Memory Controller Register Offsets - Indexed - External - Fixed
131************************************************************************/
132#define _RX_FIFO 0x000 /* Rx FIFO */
133#define _TX_FIFO 0x800 /* Tx FIFO */
134#define _RXF_OUTP 0x990 /* Rx FIFO OUT pointer 16 Read / Write */
135#define _RXF_INP 0x992 /* Rx FIFO IN pointer 16 Read / Write */
136#define _TXF_OUTP 0x994 /* Tx FIFO OUT pointer 8 Read / Write */
137#define _TXF_INP 0x995 /* Tx FIFO IN pointer 8 Read / Write */
138#define _TXP_CNT 0x996 /* Tx Priority Count 8 Read / Write */
139#define _TXP_PNTR 0x997 /* Tx Priority Pointer 8 Read / Write */
140
141#define PRI_PEND 0x80 /* Priority data pending (bit7, Tx pri cnt) */
142#define TXFIFO_SIZE 255 /* size of Tx FIFO */
143#define RXFIFO_SIZE 1023 /* size of Rx FIFO */
144
145/************************************************************************
146Tx Priority Buffer - Indexed - External - Fixed
147************************************************************************/
148#define _TXP_BUF 0x9C0 /* Tx Priority Buffer 32 Bytes Read / Write */
149#define TXP_SIZE 0x20 /* 32 bytes */
150
151/************************************************************************
152Channel Register Offsets - Indexed - Internal - Fixed
153************************************************************************/
154
155#define _TX_CTRL 0xFF0 /* Transmit Control 16 Write */
156#define _RX_CTRL 0xFF2 /* Receive Control 8 Write */
157#define _BAUD 0xFF4 /* Baud Rate 16 Write */
158#define _CLK_PRE 0xFF6 /* Clock Prescaler 8 Write */
159
160#define STMBREAK 0x08 /* BREAK */
161#define STMFRAME 0x04 /* framing error */
162#define STMRCVROVR 0x02 /* receiver over run error */
163#define STMPARITY 0x01 /* parity error */
164#define STMERROR (STMBREAK | STMFRAME | STMPARITY)
165#define STMBREAKH 0x800 /* BREAK */
166#define STMFRAMEH 0x400 /* framing error */
167#define STMRCVROVRH 0x200 /* receiver over run error */
168#define STMPARITYH 0x100 /* parity error */
169#define STMERRORH (STMBREAKH | STMFRAMEH | STMPARITYH)
170
171#define CTS_ACT 0x20 /* CTS input asserted */
172#define DSR_ACT 0x10 /* DSR input asserted */
173#define CD_ACT 0x08 /* CD input asserted */
174#define TXFIFOMT 0x04 /* Tx FIFO is empty */
175#define TXSHRMT 0x02 /* Tx shift register is empty */
176#define RDA 0x01 /* Rx data available */
177#define DRAINED (TXFIFOMT | TXSHRMT) /* indicates Tx is drained */
178
179#define STATMODE 0x8000 /* status mode enable bit */
180#define RXFOVERFL 0x2000 /* receive FIFO overflow */
181#define RX2MATCH 0x1000 /* receive compare byte 2 match */
182#define RX1MATCH 0x0800 /* receive compare byte 1 match */
183#define RXBREAK 0x0400 /* received BREAK */
184#define RXFRAME 0x0200 /* received framing error */
185#define RXPARITY 0x0100 /* received parity error */
186#define STATERROR (RXBREAK | RXFRAME | RXPARITY)
187
188#define CTSFC_EN 0x80 /* CTS flow control enable bit */
189#define RTSTOG_EN 0x40 /* RTS toggle enable bit */
190#define TXINT_EN 0x10 /* transmit interrupt enable */
191#define STOP2 0x08 /* enable 2 stop bits (0 = 1 stop) */
192#define PARITY_EN 0x04 /* enable parity (0 = no parity) */
193#define EVEN_PAR 0x02 /* even parity (0 = odd parity) */
194#define DATA8BIT 0x01 /* 8 bit data (0 = 7 bit data) */
195
196#define SETBREAK 0x10 /* send break condition (must clear) */
197#define LOCALLOOP 0x08 /* local loopback set for test */
198#define SET_DTR 0x04 /* assert DTR */
199#define SET_RTS 0x02 /* assert RTS */
200#define TX_ENABLE 0x01 /* enable transmitter */
201
202#define RTSFC_EN 0x40 /* RTS flow control enable */
203#define RXPROC_EN 0x20 /* receive processor enable */
204#define TRIG_NO 0x00 /* Rx FIFO trigger level 0 (no trigger) */
205#define TRIG_1 0x08 /* trigger level 1 char */
206#define TRIG_1_2 0x10 /* trigger level 1/2 */
207#define TRIG_7_8 0x18 /* trigger level 7/8 */
208#define TRIG_MASK 0x18 /* trigger level mask */
209#define SRCINT_EN 0x04 /* special Rx condition interrupt enable */
210#define RXINT_EN 0x02 /* Rx interrupt enable */
211#define MCINT_EN 0x01 /* modem change interrupt enable */
212
213#define RXF_TRIG 0x20 /* Rx FIFO trigger level interrupt */
214#define TXFIFO_MT 0x10 /* Tx FIFO empty interrupt */
215#define SRC_INT 0x08 /* special receive condition interrupt */
216#define DELTA_CD 0x04 /* CD change interrupt */
217#define DELTA_CTS 0x02 /* CTS change interrupt */
218#define DELTA_DSR 0x01 /* DSR change interrupt */
219
220#define REP1W2_EN 0x10 /* replace byte 1 with 2 bytes enable */
221#define IGN2_EN 0x08 /* ignore byte 2 enable */
222#define IGN1_EN 0x04 /* ignore byte 1 enable */
223#define COMP2_EN 0x02 /* compare byte 2 enable */
224#define COMP1_EN 0x01 /* compare byte 1 enable */
225
226#define RESET_ALL 0x80 /* reset AIOP (all channels) */
227#define TXOVERIDE 0x40 /* Transmit software off override */
228#define RESETUART 0x20 /* reset channel's UART */
229#define RESTXFCNT 0x10 /* reset channel's Tx FIFO count register */
230#define RESRXFCNT 0x08 /* reset channel's Rx FIFO count register */
231
232#define INTSTAT0 0x01 /* AIOP 0 interrupt status */
233#define INTSTAT1 0x02 /* AIOP 1 interrupt status */
234#define INTSTAT2 0x04 /* AIOP 2 interrupt status */
235#define INTSTAT3 0x08 /* AIOP 3 interrupt status */
236
237#define INTR_EN 0x08 /* allow interrupts to host */
238#define INT_STROB 0x04 /* strobe and clear interrupt line (EOI) */
239
240/**************************************************************************
241 MUDBAC remapped for PCI
242**************************************************************************/
243
244#define _CFG_INT_PCI 0x40
245#define _PCI_INT_FUNC 0x3A
246
247#define PCI_STROB 0x2000 /* bit 13 of int aiop register */
248#define INTR_EN_PCI 0x0010 /* allow interrupts to host */
249
250/*
251 * Definitions for Universal PCI board registers
252 */
253#define _PCI_9030_INT_CTRL 0x4c /* Offsets from BAR1 */
254#define _PCI_9030_GPIO_CTRL 0x54
255#define PCI_INT_CTRL_AIOP 0x0001
256#define PCI_GPIO_CTRL_8PORT 0x4000
257#define _PCI_9030_RING_IND 0xc0 /* Offsets from BAR1 */
258
259#define CHAN3_EN 0x08 /* enable AIOP 3 */
260#define CHAN2_EN 0x04 /* enable AIOP 2 */
261#define CHAN1_EN 0x02 /* enable AIOP 1 */
262#define CHAN0_EN 0x01 /* enable AIOP 0 */
263#define FREQ_DIS 0x00
264#define FREQ_274HZ 0x60
265#define FREQ_137HZ 0x50
266#define FREQ_69HZ 0x40
267#define FREQ_34HZ 0x30
268#define FREQ_17HZ 0x20
269#define FREQ_9HZ 0x10
270#define PERIODIC_ONLY 0x80 /* only PERIODIC interrupt */
271
272#define CHANINT_EN 0x0100 /* flags to enable/disable channel ints */
273
274#define RDATASIZE 72
275#define RREGDATASIZE 52
276
277/*
278 * AIOP interrupt bits for ISA/PCI boards and UPCI boards.
279 */
280#define AIOP_INTR_BIT_0 0x0001
281#define AIOP_INTR_BIT_1 0x0002
282#define AIOP_INTR_BIT_2 0x0004
283#define AIOP_INTR_BIT_3 0x0008
284
285#define AIOP_INTR_BITS ( \
286 AIOP_INTR_BIT_0 \
287 | AIOP_INTR_BIT_1 \
288 | AIOP_INTR_BIT_2 \
289 | AIOP_INTR_BIT_3)
290
291#define UPCI_AIOP_INTR_BIT_0 0x0004
292#define UPCI_AIOP_INTR_BIT_1 0x0020
293#define UPCI_AIOP_INTR_BIT_2 0x0100
294#define UPCI_AIOP_INTR_BIT_3 0x0800
295
296#define UPCI_AIOP_INTR_BITS ( \
297 UPCI_AIOP_INTR_BIT_0 \
298 | UPCI_AIOP_INTR_BIT_1 \
299 | UPCI_AIOP_INTR_BIT_2 \
300 | UPCI_AIOP_INTR_BIT_3)
301
302/* Controller level information structure */
303typedef struct {
304 int CtlID;
305 int CtlNum;
306 int BusType;
307 int boardType;
308 int isUPCI;
309 WordIO_t PCIIO;
310 WordIO_t PCIIO2;
311 ByteIO_t MBaseIO;
312 ByteIO_t MReg1IO;
313 ByteIO_t MReg2IO;
314 ByteIO_t MReg3IO;
315 Byte_t MReg2;
316 Byte_t MReg3;
317 int NumAiop;
318 int AltChanRingIndicator;
319 ByteIO_t UPCIRingInd;
320 WordIO_t AiopIO[AIOP_CTL_SIZE];
321 ByteIO_t AiopIntChanIO[AIOP_CTL_SIZE];
322 int AiopID[AIOP_CTL_SIZE];
323 int AiopNumChan[AIOP_CTL_SIZE];
324 Word_t *AiopIntrBits;
325} CONTROLLER_T;
326
327typedef CONTROLLER_T CONTROLLER_t;
328
329/* Channel level information structure */
330typedef struct {
331 CONTROLLER_T *CtlP;
332 int AiopNum;
333 int ChanID;
334 int ChanNum;
335 int rtsToggle;
336
337 ByteIO_t Cmd;
338 ByteIO_t IntChan;
339 ByteIO_t IntMask;
340 DWordIO_t IndexAddr;
341 WordIO_t IndexData;
342
343 WordIO_t TxRxData;
344 WordIO_t ChanStat;
345 WordIO_t TxRxCount;
346 ByteIO_t IntID;
347
348 Word_t TxFIFO;
349 Word_t TxFIFOPtrs;
350 Word_t RxFIFO;
351 Word_t RxFIFOPtrs;
352 Word_t TxPrioCnt;
353 Word_t TxPrioPtr;
354 Word_t TxPrioBuf;
355
356 Byte_t R[RREGDATASIZE];
357
358 Byte_t BaudDiv[4];
359 Byte_t TxControl[4];
360 Byte_t RxControl[4];
361 Byte_t TxEnables[4];
362 Byte_t TxCompare[4];
363 Byte_t TxReplace1[4];
364 Byte_t TxReplace2[4];
365} CHANNEL_T;
366
367typedef CHANNEL_T CHANNEL_t;
368typedef CHANNEL_T *CHANPTR_T;
369
370#define InterfaceModeRS232 0x00
371#define InterfaceModeRS422 0x08
372#define InterfaceModeRS485 0x10
373#define InterfaceModeRS232T 0x18
374
375/***************************************************************************
376Function: sClrBreak
377Purpose: Stop sending a transmit BREAK signal
378Call: sClrBreak(ChP)
379 CHANNEL_T *ChP; Ptr to channel structure
380*/
381#define sClrBreak(ChP) \
382do { \
383 (ChP)->TxControl[3] &= ~SETBREAK; \
384 out32((ChP)->IndexAddr,(ChP)->TxControl); \
385} while (0)
386
387/***************************************************************************
388Function: sClrDTR
389Purpose: Clr the DTR output
390Call: sClrDTR(ChP)
391 CHANNEL_T *ChP; Ptr to channel structure
392*/
393#define sClrDTR(ChP) \
394do { \
395 (ChP)->TxControl[3] &= ~SET_DTR; \
396 out32((ChP)->IndexAddr,(ChP)->TxControl); \
397} while (0)
398
399/***************************************************************************
400Function: sClrRTS
401Purpose: Clr the RTS output
402Call: sClrRTS(ChP)
403 CHANNEL_T *ChP; Ptr to channel structure
404*/
405#define sClrRTS(ChP) \
406do { \
407 if ((ChP)->rtsToggle) break; \
408 (ChP)->TxControl[3] &= ~SET_RTS; \
409 out32((ChP)->IndexAddr,(ChP)->TxControl); \
410} while (0)
411
412/***************************************************************************
413Function: sClrTxXOFF
414Purpose: Clear any existing transmit software flow control off condition
415Call: sClrTxXOFF(ChP)
416 CHANNEL_T *ChP; Ptr to channel structure
417*/
418#define sClrTxXOFF(ChP) \
419do { \
420 sOutB((ChP)->Cmd,TXOVERIDE | (Byte_t)(ChP)->ChanNum); \
421 sOutB((ChP)->Cmd,(Byte_t)(ChP)->ChanNum); \
422} while (0)
423
424/***************************************************************************
425Function: sCtlNumToCtlPtr
426Purpose: Convert a controller number to controller structure pointer
427Call: sCtlNumToCtlPtr(CtlNum)
428 int CtlNum; Controller number
429Return: CONTROLLER_T *: Ptr to controller structure
430*/
431#define sCtlNumToCtlPtr(CTLNUM) &sController[CTLNUM]
432
433/***************************************************************************
434Function: sControllerEOI
435Purpose: Strobe the MUDBAC's End Of Interrupt bit.
436Call: sControllerEOI(CtlP)
437 CONTROLLER_T *CtlP; Ptr to controller structure
438*/
439#define sControllerEOI(CTLP) sOutB((CTLP)->MReg2IO,(CTLP)->MReg2 | INT_STROB)
440
441/***************************************************************************
442Function: sPCIControllerEOI
443Purpose: Strobe the PCI End Of Interrupt bit.
444 For the UPCI boards, toggle the AIOP interrupt enable bit
445 (this was taken from the Windows driver).
446Call: sPCIControllerEOI(CtlP)
447 CONTROLLER_T *CtlP; Ptr to controller structure
448*/
449#define sPCIControllerEOI(CTLP) \
450do { \
451 if ((CTLP)->isUPCI) { \
452 Word_t w = sInW((CTLP)->PCIIO); \
453 sOutW((CTLP)->PCIIO, (w ^ PCI_INT_CTRL_AIOP)); \
454 sOutW((CTLP)->PCIIO, w); \
455 } \
456 else { \
457 sOutW((CTLP)->PCIIO, PCI_STROB); \
458 } \
459} while (0)
460
461/***************************************************************************
462Function: sDisAiop
463Purpose: Disable I/O access to an AIOP
464Call: sDisAiop(CltP)
465 CONTROLLER_T *CtlP; Ptr to controller structure
466 int AiopNum; Number of AIOP on controller
467*/
468#define sDisAiop(CTLP,AIOPNUM) \
469do { \
470 (CTLP)->MReg3 &= sBitMapClrTbl[AIOPNUM]; \
471 sOutB((CTLP)->MReg3IO,(CTLP)->MReg3); \
472} while (0)
473
474/***************************************************************************
475Function: sDisCTSFlowCtl
476Purpose: Disable output flow control using CTS
477Call: sDisCTSFlowCtl(ChP)
478 CHANNEL_T *ChP; Ptr to channel structure
479*/
480#define sDisCTSFlowCtl(ChP) \
481do { \
482 (ChP)->TxControl[2] &= ~CTSFC_EN; \
483 out32((ChP)->IndexAddr,(ChP)->TxControl); \
484} while (0)
485
486/***************************************************************************
487Function: sDisIXANY
488Purpose: Disable IXANY Software Flow Control
489Call: sDisIXANY(ChP)
490 CHANNEL_T *ChP; Ptr to channel structure
491*/
492#define sDisIXANY(ChP) \
493do { \
494 (ChP)->R[0x0e] = 0x86; \
495 out32((ChP)->IndexAddr,&(ChP)->R[0x0c]); \
496} while (0)
497
498/***************************************************************************
499Function: DisParity
500Purpose: Disable parity
501Call: sDisParity(ChP)
502 CHANNEL_T *ChP; Ptr to channel structure
503Comments: Function sSetParity() can be used in place of functions sEnParity(),
504 sDisParity(), sSetOddParity(), and sSetEvenParity().
505*/
506#define sDisParity(ChP) \
507do { \
508 (ChP)->TxControl[2] &= ~PARITY_EN; \
509 out32((ChP)->IndexAddr,(ChP)->TxControl); \
510} while (0)
511
512/***************************************************************************
513Function: sDisRTSToggle
514Purpose: Disable RTS toggle
515Call: sDisRTSToggle(ChP)
516 CHANNEL_T *ChP; Ptr to channel structure
517*/
518#define sDisRTSToggle(ChP) \
519do { \
520 (ChP)->TxControl[2] &= ~RTSTOG_EN; \
521 out32((ChP)->IndexAddr,(ChP)->TxControl); \
522 (ChP)->rtsToggle = 0; \
523} while (0)
524
525/***************************************************************************
526Function: sDisRxFIFO
527Purpose: Disable Rx FIFO
528Call: sDisRxFIFO(ChP)
529 CHANNEL_T *ChP; Ptr to channel structure
530*/
531#define sDisRxFIFO(ChP) \
532do { \
533 (ChP)->R[0x32] = 0x0a; \
534 out32((ChP)->IndexAddr,&(ChP)->R[0x30]); \
535} while (0)
536
537/***************************************************************************
538Function: sDisRxStatusMode
539Purpose: Disable the Rx status mode
540Call: sDisRxStatusMode(ChP)
541 CHANNEL_T *ChP; Ptr to channel structure
542Comments: This takes the channel out of the receive status mode. All
543 subsequent reads of receive data using sReadRxWord() will return
544 two data bytes.
545*/
546#define sDisRxStatusMode(ChP) sOutW((ChP)->ChanStat,0)
547
548/***************************************************************************
549Function: sDisTransmit
550Purpose: Disable transmit
551Call: sDisTransmit(ChP)
552 CHANNEL_T *ChP; Ptr to channel structure
553 This disables movement of Tx data from the Tx FIFO into the 1 byte
554 Tx buffer. Therefore there could be up to a 2 byte latency
555 between the time sDisTransmit() is called and the transmit buffer
556 and transmit shift register going completely empty.
557*/
558#define sDisTransmit(ChP) \
559do { \
560 (ChP)->TxControl[3] &= ~TX_ENABLE; \
561 out32((ChP)->IndexAddr,(ChP)->TxControl); \
562} while (0)
563
564/***************************************************************************
565Function: sDisTxSoftFlowCtl
566Purpose: Disable Tx Software Flow Control
567Call: sDisTxSoftFlowCtl(ChP)
568 CHANNEL_T *ChP; Ptr to channel structure
569*/
570#define sDisTxSoftFlowCtl(ChP) \
571do { \
572 (ChP)->R[0x06] = 0x8a; \
573 out32((ChP)->IndexAddr,&(ChP)->R[0x04]); \
574} while (0)
575
576/***************************************************************************
577Function: sEnAiop
578Purpose: Enable I/O access to an AIOP
579Call: sEnAiop(CltP)
580 CONTROLLER_T *CtlP; Ptr to controller structure
581 int AiopNum; Number of AIOP on controller
582*/
583#define sEnAiop(CTLP,AIOPNUM) \
584do { \
585 (CTLP)->MReg3 |= sBitMapSetTbl[AIOPNUM]; \
586 sOutB((CTLP)->MReg3IO,(CTLP)->MReg3); \
587} while (0)
588
589/***************************************************************************
590Function: sEnCTSFlowCtl
591Purpose: Enable output flow control using CTS
592Call: sEnCTSFlowCtl(ChP)
593 CHANNEL_T *ChP; Ptr to channel structure
594*/
595#define sEnCTSFlowCtl(ChP) \
596do { \
597 (ChP)->TxControl[2] |= CTSFC_EN; \
598 out32((ChP)->IndexAddr,(ChP)->TxControl); \
599} while (0)
600
601/***************************************************************************
602Function: sEnIXANY
603Purpose: Enable IXANY Software Flow Control
604Call: sEnIXANY(ChP)
605 CHANNEL_T *ChP; Ptr to channel structure
606*/
607#define sEnIXANY(ChP) \
608do { \
609 (ChP)->R[0x0e] = 0x21; \
610 out32((ChP)->IndexAddr,&(ChP)->R[0x0c]); \
611} while (0)
612
613/***************************************************************************
614Function: EnParity
615Purpose: Enable parity
616Call: sEnParity(ChP)
617 CHANNEL_T *ChP; Ptr to channel structure
618Comments: Function sSetParity() can be used in place of functions sEnParity(),
619 sDisParity(), sSetOddParity(), and sSetEvenParity().
620
621Warnings: Before enabling parity odd or even parity should be chosen using
622 functions sSetOddParity() or sSetEvenParity().
623*/
624#define sEnParity(ChP) \
625do { \
626 (ChP)->TxControl[2] |= PARITY_EN; \
627 out32((ChP)->IndexAddr,(ChP)->TxControl); \
628} while (0)
629
630/***************************************************************************
631Function: sEnRTSToggle
632Purpose: Enable RTS toggle
633Call: sEnRTSToggle(ChP)
634 CHANNEL_T *ChP; Ptr to channel structure
635Comments: This function will disable RTS flow control and clear the RTS
636 line to allow operation of RTS toggle.
637*/
638#define sEnRTSToggle(ChP) \
639do { \
640 (ChP)->RxControl[2] &= ~RTSFC_EN; \
641 out32((ChP)->IndexAddr,(ChP)->RxControl); \
642 (ChP)->TxControl[2] |= RTSTOG_EN; \
643 (ChP)->TxControl[3] &= ~SET_RTS; \
644 out32((ChP)->IndexAddr,(ChP)->TxControl); \
645 (ChP)->rtsToggle = 1; \
646} while (0)
647
648/***************************************************************************
649Function: sEnRxFIFO
650Purpose: Enable Rx FIFO
651Call: sEnRxFIFO(ChP)
652 CHANNEL_T *ChP; Ptr to channel structure
653*/
654#define sEnRxFIFO(ChP) \
655do { \
656 (ChP)->R[0x32] = 0x08; \
657 out32((ChP)->IndexAddr,&(ChP)->R[0x30]); \
658} while (0)
659
660/***************************************************************************
661Function: sEnRxProcessor
662Purpose: Enable the receive processor
663Call: sEnRxProcessor(ChP)
664 CHANNEL_T *ChP; Ptr to channel structure
665Comments: This function is used to start the receive processor. When
666 the channel is in the reset state the receive processor is not
667 running. This is done to prevent the receive processor from
668 executing invalid microcode instructions prior to the
669 downloading of the microcode.
670
671Warnings: This function must be called after valid microcode has been
672 downloaded to the AIOP, and it must not be called before the
673 microcode has been downloaded.
674*/
675#define sEnRxProcessor(ChP) \
676do { \
677 (ChP)->RxControl[2] |= RXPROC_EN; \
678 out32((ChP)->IndexAddr,(ChP)->RxControl); \
679} while (0)
680
681/***************************************************************************
682Function: sEnRxStatusMode
683Purpose: Enable the Rx status mode
684Call: sEnRxStatusMode(ChP)
685 CHANNEL_T *ChP; Ptr to channel structure
686Comments: This places the channel in the receive status mode. All subsequent
687 reads of receive data using sReadRxWord() will return a data byte
688 in the low word and a status byte in the high word.
689
690*/
691#define sEnRxStatusMode(ChP) sOutW((ChP)->ChanStat,STATMODE)
692
693/***************************************************************************
694Function: sEnTransmit
695Purpose: Enable transmit
696Call: sEnTransmit(ChP)
697 CHANNEL_T *ChP; Ptr to channel structure
698*/
699#define sEnTransmit(ChP) \
700do { \
701 (ChP)->TxControl[3] |= TX_ENABLE; \
702 out32((ChP)->IndexAddr,(ChP)->TxControl); \
703} while (0)
704
705/***************************************************************************
706Function: sEnTxSoftFlowCtl
707Purpose: Enable Tx Software Flow Control
708Call: sEnTxSoftFlowCtl(ChP)
709 CHANNEL_T *ChP; Ptr to channel structure
710*/
711#define sEnTxSoftFlowCtl(ChP) \
712do { \
713 (ChP)->R[0x06] = 0xc5; \
714 out32((ChP)->IndexAddr,&(ChP)->R[0x04]); \
715} while (0)
716
717/***************************************************************************
718Function: sGetAiopIntStatus
719Purpose: Get the AIOP interrupt status
720Call: sGetAiopIntStatus(CtlP,AiopNum)
721 CONTROLLER_T *CtlP; Ptr to controller structure
722 int AiopNum; AIOP number
723Return: Byte_t: The AIOP interrupt status. Bits 0 through 7
724 represent channels 0 through 7 respectively. If a
725 bit is set that channel is interrupting.
726*/
727#define sGetAiopIntStatus(CTLP,AIOPNUM) sInB((CTLP)->AiopIntChanIO[AIOPNUM])
728
729/***************************************************************************
730Function: sGetAiopNumChan
731Purpose: Get the number of channels supported by an AIOP
732Call: sGetAiopNumChan(CtlP,AiopNum)
733 CONTROLLER_T *CtlP; Ptr to controller structure
734 int AiopNum; AIOP number
735Return: int: The number of channels supported by the AIOP
736*/
737#define sGetAiopNumChan(CTLP,AIOPNUM) (CTLP)->AiopNumChan[AIOPNUM]
738
739/***************************************************************************
740Function: sGetChanIntID
741Purpose: Get a channel's interrupt identification byte
742Call: sGetChanIntID(ChP)
743 CHANNEL_T *ChP; Ptr to channel structure
744Return: Byte_t: The channel interrupt ID. Can be any
745 combination of the following flags:
746 RXF_TRIG: Rx FIFO trigger level interrupt
747 TXFIFO_MT: Tx FIFO empty interrupt
748 SRC_INT: Special receive condition interrupt
749 DELTA_CD: CD change interrupt
750 DELTA_CTS: CTS change interrupt
751 DELTA_DSR: DSR change interrupt
752*/
753#define sGetChanIntID(ChP) (sInB((ChP)->IntID) & (RXF_TRIG | TXFIFO_MT | SRC_INT | DELTA_CD | DELTA_CTS | DELTA_DSR))
754
755/***************************************************************************
756Function: sGetChanNum
757Purpose: Get the number of a channel within an AIOP
758Call: sGetChanNum(ChP)
759 CHANNEL_T *ChP; Ptr to channel structure
760Return: int: Channel number within AIOP, or NULLCHAN if channel does
761 not exist.
762*/
763#define sGetChanNum(ChP) (ChP)->ChanNum
764
765/***************************************************************************
766Function: sGetChanStatus
767Purpose: Get the channel status
768Call: sGetChanStatus(ChP)
769 CHANNEL_T *ChP; Ptr to channel structure
770Return: Word_t: The channel status. Can be any combination of
771 the following flags:
772 LOW BYTE FLAGS
773 CTS_ACT: CTS input asserted
774 DSR_ACT: DSR input asserted
775 CD_ACT: CD input asserted
776 TXFIFOMT: Tx FIFO is empty
777 TXSHRMT: Tx shift register is empty
778 RDA: Rx data available
779
780 HIGH BYTE FLAGS
781 STATMODE: status mode enable bit
782 RXFOVERFL: receive FIFO overflow
783 RX2MATCH: receive compare byte 2 match
784 RX1MATCH: receive compare byte 1 match
785 RXBREAK: received BREAK
786 RXFRAME: received framing error
787 RXPARITY: received parity error
788Warnings: This function will clear the high byte flags in the Channel
789 Status Register.
790*/
791#define sGetChanStatus(ChP) sInW((ChP)->ChanStat)
792
793/***************************************************************************
794Function: sGetChanStatusLo
795Purpose: Get the low byte only of the channel status
796Call: sGetChanStatusLo(ChP)
797 CHANNEL_T *ChP; Ptr to channel structure
798Return: Byte_t: The channel status low byte. Can be any combination
799 of the following flags:
800 CTS_ACT: CTS input asserted
801 DSR_ACT: DSR input asserted
802 CD_ACT: CD input asserted
803 TXFIFOMT: Tx FIFO is empty
804 TXSHRMT: Tx shift register is empty
805 RDA: Rx data available
806*/
807#define sGetChanStatusLo(ChP) sInB((ByteIO_t)(ChP)->ChanStat)
808
809/**********************************************************************
810 * Get RI status of channel
811 * Defined as a function in rocket.c -aes
812 */
813#if 0
814#define sGetChanRI(ChP) ((ChP)->CtlP->AltChanRingIndicator ? \
815 (sInB((ByteIO_t)((ChP)->ChanStat+8)) & DSR_ACT) : \
816 (((ChP)->CtlP->boardType == ROCKET_TYPE_PC104) ? \
817 (!(sInB((ChP)->CtlP->AiopIO[3]) & sBitMapSetTbl[(ChP)->ChanNum])) : \
818 0))
819#endif
820
821/***************************************************************************
822Function: sGetControllerIntStatus
823Purpose: Get the controller interrupt status
824Call: sGetControllerIntStatus(CtlP)
825 CONTROLLER_T *CtlP; Ptr to controller structure
826Return: Byte_t: The controller interrupt status in the lower 4
827 bits. Bits 0 through 3 represent AIOP's 0
828 through 3 respectively. If a bit is set that
829 AIOP is interrupting. Bits 4 through 7 will
830 always be cleared.
831*/
832#define sGetControllerIntStatus(CTLP) (sInB((CTLP)->MReg1IO) & 0x0f)
833
834/***************************************************************************
835Function: sPCIGetControllerIntStatus
836Purpose: Get the controller interrupt status
837Call: sPCIGetControllerIntStatus(CtlP)
838 CONTROLLER_T *CtlP; Ptr to controller structure
839Return: unsigned char: The controller interrupt status in the lower 4
840 bits and bit 4. Bits 0 through 3 represent AIOP's 0
841 through 3 respectively. Bit 4 is set if the int
842 was generated from periodic. If a bit is set the
843 AIOP is interrupting.
844*/
845#define sPCIGetControllerIntStatus(CTLP) \
846 ((CTLP)->isUPCI ? \
847 (sInW((CTLP)->PCIIO2) & UPCI_AIOP_INTR_BITS) : \
848 ((sInW((CTLP)->PCIIO) >> 8) & AIOP_INTR_BITS))
849
850/***************************************************************************
851
852Function: sGetRxCnt
853Purpose: Get the number of data bytes in the Rx FIFO
854Call: sGetRxCnt(ChP)
855 CHANNEL_T *ChP; Ptr to channel structure
856Return: int: The number of data bytes in the Rx FIFO.
857Comments: Byte read of count register is required to obtain Rx count.
858
859*/
860#define sGetRxCnt(ChP) sInW((ChP)->TxRxCount)
861
862/***************************************************************************
863Function: sGetTxCnt
864Purpose: Get the number of data bytes in the Tx FIFO
865Call: sGetTxCnt(ChP)
866 CHANNEL_T *ChP; Ptr to channel structure
867Return: Byte_t: The number of data bytes in the Tx FIFO.
868Comments: Byte read of count register is required to obtain Tx count.
869
870*/
871#define sGetTxCnt(ChP) sInB((ByteIO_t)(ChP)->TxRxCount)
872
873/*****************************************************************************
874Function: sGetTxRxDataIO
875Purpose: Get the I/O address of a channel's TxRx Data register
876Call: sGetTxRxDataIO(ChP)
877 CHANNEL_T *ChP; Ptr to channel structure
878Return: WordIO_t: I/O address of a channel's TxRx Data register
879*/
880#define sGetTxRxDataIO(ChP) (ChP)->TxRxData
881
882/***************************************************************************
883Function: sInitChanDefaults
884Purpose: Initialize a channel structure to it's default state.
885Call: sInitChanDefaults(ChP)
886 CHANNEL_T *ChP; Ptr to the channel structure
887Comments: This function must be called once for every channel structure
888 that exists before any other SSCI calls can be made.
889
890*/
891#define sInitChanDefaults(ChP) \
892do { \
893 (ChP)->CtlP = NULLCTLPTR; \
894 (ChP)->AiopNum = NULLAIOP; \
895 (ChP)->ChanID = AIOPID_NULL; \
896 (ChP)->ChanNum = NULLCHAN; \
897} while (0)
898
899/***************************************************************************
900Function: sResetAiopByNum
901Purpose: Reset the AIOP by number
902Call: sResetAiopByNum(CTLP,AIOPNUM)
903 CONTROLLER_T CTLP; Ptr to controller structure
904 AIOPNUM; AIOP index
905*/
906#define sResetAiopByNum(CTLP,AIOPNUM) \
907do { \
908 sOutB((CTLP)->AiopIO[(AIOPNUM)]+_CMD_REG,RESET_ALL); \
909 sOutB((CTLP)->AiopIO[(AIOPNUM)]+_CMD_REG,0x0); \
910} while (0)
911
912/***************************************************************************
913Function: sSendBreak
914Purpose: Send a transmit BREAK signal
915Call: sSendBreak(ChP)
916 CHANNEL_T *ChP; Ptr to channel structure
917*/
918#define sSendBreak(ChP) \
919do { \
920 (ChP)->TxControl[3] |= SETBREAK; \
921 out32((ChP)->IndexAddr,(ChP)->TxControl); \
922} while (0)
923
924/***************************************************************************
925Function: sSetBaud
926Purpose: Set baud rate
927Call: sSetBaud(ChP,Divisor)
928 CHANNEL_T *ChP; Ptr to channel structure
929 Word_t Divisor; 16 bit baud rate divisor for channel
930*/
931#define sSetBaud(ChP,DIVISOR) \
932do { \
933 (ChP)->BaudDiv[2] = (Byte_t)(DIVISOR); \
934 (ChP)->BaudDiv[3] = (Byte_t)((DIVISOR) >> 8); \
935 out32((ChP)->IndexAddr,(ChP)->BaudDiv); \
936} while (0)
937
938/***************************************************************************
939Function: sSetData7
940Purpose: Set data bits to 7
941Call: sSetData7(ChP)
942 CHANNEL_T *ChP; Ptr to channel structure
943*/
944#define sSetData7(ChP) \
945do { \
946 (ChP)->TxControl[2] &= ~DATA8BIT; \
947 out32((ChP)->IndexAddr,(ChP)->TxControl); \
948} while (0)
949
950/***************************************************************************
951Function: sSetData8
952Purpose: Set data bits to 8
953Call: sSetData8(ChP)
954 CHANNEL_T *ChP; Ptr to channel structure
955*/
956#define sSetData8(ChP) \
957do { \
958 (ChP)->TxControl[2] |= DATA8BIT; \
959 out32((ChP)->IndexAddr,(ChP)->TxControl); \
960} while (0)
961
962/***************************************************************************
963Function: sSetDTR
964Purpose: Set the DTR output
965Call: sSetDTR(ChP)
966 CHANNEL_T *ChP; Ptr to channel structure
967*/
968#define sSetDTR(ChP) \
969do { \
970 (ChP)->TxControl[3] |= SET_DTR; \
971 out32((ChP)->IndexAddr,(ChP)->TxControl); \
972} while (0)
973
974/***************************************************************************
975Function: sSetEvenParity
976Purpose: Set even parity
977Call: sSetEvenParity(ChP)
978 CHANNEL_T *ChP; Ptr to channel structure
979Comments: Function sSetParity() can be used in place of functions sEnParity(),
980 sDisParity(), sSetOddParity(), and sSetEvenParity().
981
982Warnings: This function has no effect unless parity is enabled with function
983 sEnParity().
984*/
985#define sSetEvenParity(ChP) \
986do { \
987 (ChP)->TxControl[2] |= EVEN_PAR; \
988 out32((ChP)->IndexAddr,(ChP)->TxControl); \
989} while (0)
990
991/***************************************************************************
992Function: sSetOddParity
993Purpose: Set odd parity
994Call: sSetOddParity(ChP)
995 CHANNEL_T *ChP; Ptr to channel structure
996Comments: Function sSetParity() can be used in place of functions sEnParity(),
997 sDisParity(), sSetOddParity(), and sSetEvenParity().
998
999Warnings: This function has no effect unless parity is enabled with function
1000 sEnParity().
1001*/
1002#define sSetOddParity(ChP) \
1003do { \
1004 (ChP)->TxControl[2] &= ~EVEN_PAR; \
1005 out32((ChP)->IndexAddr,(ChP)->TxControl); \
1006} while (0)
1007
1008/***************************************************************************
1009Function: sSetRTS
1010Purpose: Set the RTS output
1011Call: sSetRTS(ChP)
1012 CHANNEL_T *ChP; Ptr to channel structure
1013*/
1014#define sSetRTS(ChP) \
1015do { \
1016 if ((ChP)->rtsToggle) break; \
1017 (ChP)->TxControl[3] |= SET_RTS; \
1018 out32((ChP)->IndexAddr,(ChP)->TxControl); \
1019} while (0)
1020
1021/***************************************************************************
1022Function: sSetRxTrigger
1023Purpose: Set the Rx FIFO trigger level
1024Call: sSetRxProcessor(ChP,Level)
1025 CHANNEL_T *ChP; Ptr to channel structure
1026 Byte_t Level; Number of characters in Rx FIFO at which the
1027 interrupt will be generated. Can be any of the following flags:
1028
1029 TRIG_NO: no trigger
1030 TRIG_1: 1 character in FIFO
1031 TRIG_1_2: FIFO 1/2 full
1032 TRIG_7_8: FIFO 7/8 full
1033Comments: An interrupt will be generated when the trigger level is reached
1034 only if function sEnInterrupt() has been called with flag
1035 RXINT_EN set. The RXF_TRIG flag in the Interrupt Idenfification
1036 register will be set whenever the trigger level is reached
1037 regardless of the setting of RXINT_EN.
1038
1039*/
1040#define sSetRxTrigger(ChP,LEVEL) \
1041do { \
1042 (ChP)->RxControl[2] &= ~TRIG_MASK; \
1043 (ChP)->RxControl[2] |= LEVEL; \
1044 out32((ChP)->IndexAddr,(ChP)->RxControl); \
1045} while (0)
1046
1047/***************************************************************************
1048Function: sSetStop1
1049Purpose: Set stop bits to 1
1050Call: sSetStop1(ChP)
1051 CHANNEL_T *ChP; Ptr to channel structure
1052*/
1053#define sSetStop1(ChP) \
1054do { \
1055 (ChP)->TxControl[2] &= ~STOP2; \
1056 out32((ChP)->IndexAddr,(ChP)->TxControl); \
1057} while (0)
1058
1059/***************************************************************************
1060Function: sSetStop2
1061Purpose: Set stop bits to 2
1062Call: sSetStop2(ChP)
1063 CHANNEL_T *ChP; Ptr to channel structure
1064*/
1065#define sSetStop2(ChP) \
1066do { \
1067 (ChP)->TxControl[2] |= STOP2; \
1068 out32((ChP)->IndexAddr,(ChP)->TxControl); \
1069} while (0)
1070
1071/***************************************************************************
1072Function: sSetTxXOFFChar
1073Purpose: Set the Tx XOFF flow control character
1074Call: sSetTxXOFFChar(ChP,Ch)
1075 CHANNEL_T *ChP; Ptr to channel structure
1076 Byte_t Ch; The value to set the Tx XOFF character to
1077*/
1078#define sSetTxXOFFChar(ChP,CH) \
1079do { \
1080 (ChP)->R[0x07] = (CH); \
1081 out32((ChP)->IndexAddr,&(ChP)->R[0x04]); \
1082} while (0)
1083
1084/***************************************************************************
1085Function: sSetTxXONChar
1086Purpose: Set the Tx XON flow control character
1087Call: sSetTxXONChar(ChP,Ch)
1088 CHANNEL_T *ChP; Ptr to channel structure
1089 Byte_t Ch; The value to set the Tx XON character to
1090*/
1091#define sSetTxXONChar(ChP,CH) \
1092do { \
1093 (ChP)->R[0x0b] = (CH); \
1094 out32((ChP)->IndexAddr,&(ChP)->R[0x08]); \
1095} while (0)
1096
1097/***************************************************************************
1098Function: sStartRxProcessor
1099Purpose: Start a channel's receive processor
1100Call: sStartRxProcessor(ChP)
1101 CHANNEL_T *ChP; Ptr to channel structure
1102Comments: This function is used to start a Rx processor after it was
1103 stopped with sStopRxProcessor() or sStopSWInFlowCtl(). It
1104 will restart both the Rx processor and software input flow control.
1105
1106*/
1107#define sStartRxProcessor(ChP) out32((ChP)->IndexAddr,&(ChP)->R[0])
1108
1109/***************************************************************************
1110Function: sWriteTxByte
1111Purpose: Write a transmit data byte to a channel.
1112 ByteIO_t io: Channel transmit register I/O address. This can
1113 be obtained with sGetTxRxDataIO().
1114 Byte_t Data; The transmit data byte.
1115Warnings: This function writes the data byte without checking to see if
1116 sMaxTxSize is exceeded in the Tx FIFO.
1117*/
1118#define sWriteTxByte(IO,DATA) sOutB(IO,DATA)
1119
1120/*
1121 * Begin Linux specific definitions for the Rocketport driver
1122 *
1123 * This code is Copyright Theodore Ts'o, 1995-1997
1124 */
1125
1126struct r_port {
1127 int magic;
1128 struct tty_port port;
1129 int line;
1130 int flags; /* Don't yet match the ASY_ flags!! */
1131 unsigned int board:3;
1132 unsigned int aiop:2;
1133 unsigned int chan:3;
1134 CONTROLLER_t *ctlp;
1135 CHANNEL_t channel;
1136 int intmask;
1137 int xmit_fifo_room; /* room in xmit fifo */
1138 unsigned char *xmit_buf;
1139 int xmit_head;
1140 int xmit_tail;
1141 int xmit_cnt;
1142 int cd_status;
1143 int ignore_status_mask;
1144 int read_status_mask;
1145 int cps;
1146
1147 struct completion close_wait; /* Not yet matching the core */
1148 spinlock_t slock;
1149 struct mutex write_mtx;
1150};
1151
1152#define RPORT_MAGIC 0x525001
1153
1154#define NUM_BOARDS 8
1155#define MAX_RP_PORTS (32*NUM_BOARDS)
1156
1157/*
1158 * The size of the xmit buffer is 1 page, or 4096 bytes
1159 */
1160#define XMIT_BUF_SIZE 4096
1161
1162/* number of characters left in xmit buffer before we ask for more */
1163#define WAKEUP_CHARS 256
1164
1165/*
1166 * Assigned major numbers for the Comtrol Rocketport
1167 */
1168#define TTY_ROCKET_MAJOR 46
1169#define CUA_ROCKET_MAJOR 47
1170
1171#ifdef PCI_VENDOR_ID_RP
1172#undef PCI_VENDOR_ID_RP
1173#undef PCI_DEVICE_ID_RP8OCTA
1174#undef PCI_DEVICE_ID_RP8INTF
1175#undef PCI_DEVICE_ID_RP16INTF
1176#undef PCI_DEVICE_ID_RP32INTF
1177#undef PCI_DEVICE_ID_URP8OCTA
1178#undef PCI_DEVICE_ID_URP8INTF
1179#undef PCI_DEVICE_ID_URP16INTF
1180#undef PCI_DEVICE_ID_CRP16INTF
1181#undef PCI_DEVICE_ID_URP32INTF
1182#endif
1183
1184/* Comtrol PCI Vendor ID */
1185#define PCI_VENDOR_ID_RP 0x11fe
1186
1187/* Comtrol Device ID's */
1188#define PCI_DEVICE_ID_RP32INTF 0x0001 /* Rocketport 32 port w/external I/F */
1189#define PCI_DEVICE_ID_RP8INTF 0x0002 /* Rocketport 8 port w/external I/F */
1190#define PCI_DEVICE_ID_RP16INTF 0x0003 /* Rocketport 16 port w/external I/F */
1191#define PCI_DEVICE_ID_RP4QUAD 0x0004 /* Rocketport 4 port w/quad cable */
1192#define PCI_DEVICE_ID_RP8OCTA 0x0005 /* Rocketport 8 port w/octa cable */
1193#define PCI_DEVICE_ID_RP8J 0x0006 /* Rocketport 8 port w/RJ11 connectors */
1194#define PCI_DEVICE_ID_RP4J 0x0007 /* Rocketport 4 port w/RJ11 connectors */
1195#define PCI_DEVICE_ID_RP8SNI 0x0008 /* Rocketport 8 port w/ DB78 SNI (Siemens) connector */
1196#define PCI_DEVICE_ID_RP16SNI 0x0009 /* Rocketport 16 port w/ DB78 SNI (Siemens) connector */
1197#define PCI_DEVICE_ID_RPP4 0x000A /* Rocketport Plus 4 port */
1198#define PCI_DEVICE_ID_RPP8 0x000B /* Rocketport Plus 8 port */
1199#define PCI_DEVICE_ID_RP6M 0x000C /* RocketModem 6 port */
1200#define PCI_DEVICE_ID_RP4M 0x000D /* RocketModem 4 port */
1201#define PCI_DEVICE_ID_RP2_232 0x000E /* Rocketport Plus 2 port RS232 */
1202#define PCI_DEVICE_ID_RP2_422 0x000F /* Rocketport Plus 2 port RS422 */
1203
1204/* Universal PCI boards */
1205#define PCI_DEVICE_ID_URP32INTF 0x0801 /* Rocketport UPCI 32 port w/external I/F */
1206#define PCI_DEVICE_ID_URP8INTF 0x0802 /* Rocketport UPCI 8 port w/external I/F */
1207#define PCI_DEVICE_ID_URP16INTF 0x0803 /* Rocketport UPCI 16 port w/external I/F */
1208#define PCI_DEVICE_ID_URP8OCTA 0x0805 /* Rocketport UPCI 8 port w/octa cable */
1209#define PCI_DEVICE_ID_UPCI_RM3_8PORT 0x080C /* Rocketmodem III 8 port */
1210#define PCI_DEVICE_ID_UPCI_RM3_4PORT 0x080D /* Rocketmodem III 4 port */
1211
1212/* Compact PCI device */
1213#define PCI_DEVICE_ID_CRP16INTF 0x0903 /* Rocketport Compact PCI 16 port w/external I/F */
1214
diff --git a/drivers/char/scx200_gpio.c b/drivers/char/scx200_gpio.c
index 99e5272e3c53..0bc135b9b16f 100644
--- a/drivers/char/scx200_gpio.c
+++ b/drivers/char/scx200_gpio.c
@@ -67,6 +67,7 @@ static const struct file_operations scx200_gpio_fileops = {
67 .read = nsc_gpio_read, 67 .read = nsc_gpio_read,
68 .open = scx200_gpio_open, 68 .open = scx200_gpio_open,
69 .release = scx200_gpio_release, 69 .release = scx200_gpio_release,
70 .llseek = no_llseek,
70}; 71};
71 72
72static struct cdev scx200_gpio_cdev; /* use 1 cdev for all pins */ 73static struct cdev scx200_gpio_cdev; /* use 1 cdev for all pins */
diff --git a/drivers/char/selection.c b/drivers/char/selection.c
deleted file mode 100644
index ebae344ce910..000000000000
--- a/drivers/char/selection.c
+++ /dev/null
@@ -1,348 +0,0 @@
1/*
2 * linux/drivers/char/selection.c
3 *
4 * This module exports the functions:
5 *
6 * 'int set_selection(struct tiocl_selection __user *, struct tty_struct *)'
7 * 'void clear_selection(void)'
8 * 'int paste_selection(struct tty_struct *)'
9 * 'int sel_loadlut(char __user *)'
10 *
11 * Now that /dev/vcs exists, most of this can disappear again.
12 */
13
14#include <linux/module.h>
15#include <linux/tty.h>
16#include <linux/sched.h>
17#include <linux/mm.h>
18#include <linux/slab.h>
19#include <linux/types.h>
20
21#include <asm/uaccess.h>
22
23#include <linux/kbd_kern.h>
24#include <linux/vt_kern.h>
25#include <linux/consolemap.h>
26#include <linux/selection.h>
27#include <linux/tiocl.h>
28#include <linux/console.h>
29#include <linux/smp_lock.h>
30
31/* Don't take this from <ctype.h>: 011-015 on the screen aren't spaces */
32#define isspace(c) ((c) == ' ')
33
34extern void poke_blanked_console(void);
35
36/* Variables for selection control. */
37/* Use a dynamic buffer, instead of static (Dec 1994) */
38struct vc_data *sel_cons; /* must not be deallocated */
39static int use_unicode;
40static volatile int sel_start = -1; /* cleared by clear_selection */
41static int sel_end;
42static int sel_buffer_lth;
43static char *sel_buffer;
44
45/* clear_selection, highlight and highlight_pointer can be called
46 from interrupt (via scrollback/front) */
47
48/* set reverse video on characters s-e of console with selection. */
49static inline void highlight(const int s, const int e)
50{
51 invert_screen(sel_cons, s, e-s+2, 1);
52}
53
54/* use complementary color to show the pointer */
55static inline void highlight_pointer(const int where)
56{
57 complement_pos(sel_cons, where);
58}
59
60static u16
61sel_pos(int n)
62{
63 return inverse_translate(sel_cons, screen_glyph(sel_cons, n),
64 use_unicode);
65}
66
67/* remove the current selection highlight, if any,
68 from the console holding the selection. */
69void
70clear_selection(void) {
71 highlight_pointer(-1); /* hide the pointer */
72 if (sel_start != -1) {
73 highlight(sel_start, sel_end);
74 sel_start = -1;
75 }
76}
77
78/*
79 * User settable table: what characters are to be considered alphabetic?
80 * 256 bits
81 */
82static u32 inwordLut[8]={
83 0x00000000, /* control chars */
84 0x03FF0000, /* digits */
85 0x87FFFFFE, /* uppercase and '_' */
86 0x07FFFFFE, /* lowercase */
87 0x00000000,
88 0x00000000,
89 0xFF7FFFFF, /* latin-1 accented letters, not multiplication sign */
90 0xFF7FFFFF /* latin-1 accented letters, not division sign */
91};
92
93static inline int inword(const u16 c) {
94 return c > 0xff || (( inwordLut[c>>5] >> (c & 0x1F) ) & 1);
95}
96
97/* set inwordLut contents. Invoked by ioctl(). */
98int sel_loadlut(char __user *p)
99{
100 return copy_from_user(inwordLut, (u32 __user *)(p+4), 32) ? -EFAULT : 0;
101}
102
103/* does screen address p correspond to character at LH/RH edge of screen? */
104static inline int atedge(const int p, int size_row)
105{
106 return (!(p % size_row) || !((p + 2) % size_row));
107}
108
109/* constrain v such that v <= u */
110static inline unsigned short limit(const unsigned short v, const unsigned short u)
111{
112 return (v > u) ? u : v;
113}
114
115/* stores the char in UTF8 and returns the number of bytes used (1-3) */
116static int store_utf8(u16 c, char *p)
117{
118 if (c < 0x80) {
119 /* 0******* */
120 p[0] = c;
121 return 1;
122 } else if (c < 0x800) {
123 /* 110***** 10****** */
124 p[0] = 0xc0 | (c >> 6);
125 p[1] = 0x80 | (c & 0x3f);
126 return 2;
127 } else {
128 /* 1110**** 10****** 10****** */
129 p[0] = 0xe0 | (c >> 12);
130 p[1] = 0x80 | ((c >> 6) & 0x3f);
131 p[2] = 0x80 | (c & 0x3f);
132 return 3;
133 }
134}
135
136/* set the current selection. Invoked by ioctl() or by kernel code. */
137int set_selection(const struct tiocl_selection __user *sel, struct tty_struct *tty)
138{
139 struct vc_data *vc = vc_cons[fg_console].d;
140 int sel_mode, new_sel_start, new_sel_end, spc;
141 char *bp, *obp;
142 int i, ps, pe, multiplier;
143 u16 c;
144 struct kbd_struct *kbd = kbd_table + fg_console;
145
146 poke_blanked_console();
147
148 { unsigned short xs, ys, xe, ye;
149
150 if (!access_ok(VERIFY_READ, sel, sizeof(*sel)))
151 return -EFAULT;
152 __get_user(xs, &sel->xs);
153 __get_user(ys, &sel->ys);
154 __get_user(xe, &sel->xe);
155 __get_user(ye, &sel->ye);
156 __get_user(sel_mode, &sel->sel_mode);
157 xs--; ys--; xe--; ye--;
158 xs = limit(xs, vc->vc_cols - 1);
159 ys = limit(ys, vc->vc_rows - 1);
160 xe = limit(xe, vc->vc_cols - 1);
161 ye = limit(ye, vc->vc_rows - 1);
162 ps = ys * vc->vc_size_row + (xs << 1);
163 pe = ye * vc->vc_size_row + (xe << 1);
164
165 if (sel_mode == TIOCL_SELCLEAR) {
166 /* useful for screendump without selection highlights */
167 clear_selection();
168 return 0;
169 }
170
171 if (mouse_reporting() && (sel_mode & TIOCL_SELMOUSEREPORT)) {
172 mouse_report(tty, sel_mode & TIOCL_SELBUTTONMASK, xs, ys);
173 return 0;
174 }
175 }
176
177 if (ps > pe) /* make sel_start <= sel_end */
178 {
179 int tmp = ps;
180 ps = pe;
181 pe = tmp;
182 }
183
184 if (sel_cons != vc_cons[fg_console].d) {
185 clear_selection();
186 sel_cons = vc_cons[fg_console].d;
187 }
188 use_unicode = kbd && kbd->kbdmode == VC_UNICODE;
189
190 switch (sel_mode)
191 {
192 case TIOCL_SELCHAR: /* character-by-character selection */
193 new_sel_start = ps;
194 new_sel_end = pe;
195 break;
196 case TIOCL_SELWORD: /* word-by-word selection */
197 spc = isspace(sel_pos(ps));
198 for (new_sel_start = ps; ; ps -= 2)
199 {
200 if ((spc && !isspace(sel_pos(ps))) ||
201 (!spc && !inword(sel_pos(ps))))
202 break;
203 new_sel_start = ps;
204 if (!(ps % vc->vc_size_row))
205 break;
206 }
207 spc = isspace(sel_pos(pe));
208 for (new_sel_end = pe; ; pe += 2)
209 {
210 if ((spc && !isspace(sel_pos(pe))) ||
211 (!spc && !inword(sel_pos(pe))))
212 break;
213 new_sel_end = pe;
214 if (!((pe + 2) % vc->vc_size_row))
215 break;
216 }
217 break;
218 case TIOCL_SELLINE: /* line-by-line selection */
219 new_sel_start = ps - ps % vc->vc_size_row;
220 new_sel_end = pe + vc->vc_size_row
221 - pe % vc->vc_size_row - 2;
222 break;
223 case TIOCL_SELPOINTER:
224 highlight_pointer(pe);
225 return 0;
226 default:
227 return -EINVAL;
228 }
229
230 /* remove the pointer */
231 highlight_pointer(-1);
232
233 /* select to end of line if on trailing space */
234 if (new_sel_end > new_sel_start &&
235 !atedge(new_sel_end, vc->vc_size_row) &&
236 isspace(sel_pos(new_sel_end))) {
237 for (pe = new_sel_end + 2; ; pe += 2)
238 if (!isspace(sel_pos(pe)) ||
239 atedge(pe, vc->vc_size_row))
240 break;
241 if (isspace(sel_pos(pe)))
242 new_sel_end = pe;
243 }
244 if (sel_start == -1) /* no current selection */
245 highlight(new_sel_start, new_sel_end);
246 else if (new_sel_start == sel_start)
247 {
248 if (new_sel_end == sel_end) /* no action required */
249 return 0;
250 else if (new_sel_end > sel_end) /* extend to right */
251 highlight(sel_end + 2, new_sel_end);
252 else /* contract from right */
253 highlight(new_sel_end + 2, sel_end);
254 }
255 else if (new_sel_end == sel_end)
256 {
257 if (new_sel_start < sel_start) /* extend to left */
258 highlight(new_sel_start, sel_start - 2);
259 else /* contract from left */
260 highlight(sel_start, new_sel_start - 2);
261 }
262 else /* some other case; start selection from scratch */
263 {
264 clear_selection();
265 highlight(new_sel_start, new_sel_end);
266 }
267 sel_start = new_sel_start;
268 sel_end = new_sel_end;
269
270 /* Allocate a new buffer before freeing the old one ... */
271 multiplier = use_unicode ? 3 : 1; /* chars can take up to 3 bytes */
272 bp = kmalloc(((sel_end-sel_start)/2+1)*multiplier, GFP_KERNEL);
273 if (!bp) {
274 printk(KERN_WARNING "selection: kmalloc() failed\n");
275 clear_selection();
276 return -ENOMEM;
277 }
278 kfree(sel_buffer);
279 sel_buffer = bp;
280
281 obp = bp;
282 for (i = sel_start; i <= sel_end; i += 2) {
283 c = sel_pos(i);
284 if (use_unicode)
285 bp += store_utf8(c, bp);
286 else
287 *bp++ = c;
288 if (!isspace(c))
289 obp = bp;
290 if (! ((i + 2) % vc->vc_size_row)) {
291 /* strip trailing blanks from line and add newline,
292 unless non-space at end of line. */
293 if (obp != bp) {
294 bp = obp;
295 *bp++ = '\r';
296 }
297 obp = bp;
298 }
299 }
300 sel_buffer_lth = bp - sel_buffer;
301 return 0;
302}
303
304/* Insert the contents of the selection buffer into the
305 * queue of the tty associated with the current console.
306 * Invoked by ioctl().
307 */
308int paste_selection(struct tty_struct *tty)
309{
310 struct vc_data *vc = tty->driver_data;
311 int pasted = 0;
312 unsigned int count;
313 struct tty_ldisc *ld;
314 DECLARE_WAITQUEUE(wait, current);
315
316 /* always called with BTM from vt_ioctl */
317 WARN_ON(!tty_locked());
318
319 acquire_console_sem();
320 poke_blanked_console();
321 release_console_sem();
322
323 ld = tty_ldisc_ref(tty);
324 if (!ld) {
325 tty_unlock();
326 ld = tty_ldisc_ref_wait(tty);
327 tty_lock();
328 }
329
330 add_wait_queue(&vc->paste_wait, &wait);
331 while (sel_buffer && sel_buffer_lth > pasted) {
332 set_current_state(TASK_INTERRUPTIBLE);
333 if (test_bit(TTY_THROTTLED, &tty->flags)) {
334 schedule();
335 continue;
336 }
337 count = sel_buffer_lth - pasted;
338 count = min(count, tty->receive_room);
339 tty->ldisc->ops->receive_buf(tty, sel_buffer + pasted,
340 NULL, count);
341 pasted += count;
342 }
343 remove_wait_queue(&vc->paste_wait, &wait);
344 __set_current_state(TASK_RUNNING);
345
346 tty_ldisc_deref(ld);
347 return 0;
348}
diff --git a/drivers/char/ser_a2232.c b/drivers/char/ser_a2232.c
deleted file mode 100644
index 9610861d1f5f..000000000000
--- a/drivers/char/ser_a2232.c
+++ /dev/null
@@ -1,831 +0,0 @@
1/* drivers/char/ser_a2232.c */
2
3/* $Id: ser_a2232.c,v 0.4 2000/01/25 12:00:00 ehaase Exp $ */
4
5/* Linux serial driver for the Amiga A2232 board */
6
7/* This driver is MAINTAINED. Before applying any changes, please contact
8 * the author.
9 */
10
11/* Copyright (c) 2000-2001 Enver Haase <ehaase@inf.fu-berlin.de>
12 * alias The A2232 driver project <A2232@gmx.net>
13 * All rights reserved.
14 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 *
29 */
30/***************************** Documentation ************************/
31/*
32 * This driver is in EXPERIMENTAL state. That means I could not find
33 * someone with five A2232 boards with 35 ports running at 19200 bps
34 * at the same time and test the machine's behaviour.
35 * However, I know that you can performance-tweak this driver (see
36 * the source code).
37 * One thing to consider is the time this driver consumes during the
38 * Amiga's vertical blank interrupt. Everything that is to be done
39 * _IS DONE_ when entering the vertical blank interrupt handler of
40 * this driver.
41 * However, it would be more sane to only do the job for only ONE card
42 * instead of ALL cards at a time; or, more generally, to handle only
43 * SOME ports instead of ALL ports at a time.
44 * However, as long as no-one runs into problems I guess I shouldn't
45 * change the driver as it runs fine for me :) .
46 *
47 * Version history of this file:
48 * 0.4 Resolved licensing issues.
49 * 0.3 Inclusion in the Linux/m68k tree, small fixes.
50 * 0.2 Added documentation, minor typo fixes.
51 * 0.1 Initial release.
52 *
53 * TO DO:
54 * - Handle incoming BREAK events. I guess "Stevens: Advanced
55 * Programming in the UNIX(R) Environment" is a good reference
56 * on what is to be done.
57 * - When installing as a module, don't simply 'printk' text, but
58 * send it to the TTY used by the user.
59 *
60 * THANKS TO:
61 * - Jukka Marin (65EC02 code).
62 * - The other NetBSD developers on whose A2232 driver I had a
63 * pretty close look. However, I didn't copy any code so it
64 * is okay to put my code under the GPL and include it into
65 * Linux.
66 */
67/***************************** End of Documentation *****************/
68
69/***************************** Defines ******************************/
70/*
71 * Enables experimental 115200 (normal) 230400 (turbo) baud rate.
72 * The A2232 specification states it can only operate at speeds up to
73 * 19200 bits per second, and I was not able to send a file via
74 * "sz"/"rz" and a null-modem cable from one A2232 port to another
75 * at 115200 bits per second.
76 * However, this might work for you.
77 */
78#undef A2232_SPEEDHACK
79/*
80 * Default is not to use RTS/CTS so you could be talked to death.
81 */
82#define A2232_SUPPRESS_RTSCTS_WARNING
83/************************* End of Defines ***************************/
84
85/***************************** Includes *****************************/
86#include <linux/module.h>
87
88#include <linux/types.h>
89#include <linux/interrupt.h>
90#include <linux/kernel.h>
91#include <linux/errno.h>
92#include <linux/sched.h>
93#include <linux/tty.h>
94
95#include <asm/setup.h>
96#include <asm/amigaints.h>
97#include <asm/amigahw.h>
98#include <linux/zorro.h>
99#include <asm/irq.h>
100#include <linux/mutex.h>
101
102#include <linux/delay.h>
103
104#include <linux/serial.h>
105#include <linux/generic_serial.h>
106#include <linux/tty_flip.h>
107
108#include "ser_a2232.h"
109#include "ser_a2232fw.h"
110/************************* End of Includes **************************/
111
112/***************************** Prototypes ***************************/
113/* The interrupt service routine */
114static irqreturn_t a2232_vbl_inter(int irq, void *data);
115/* Initialize the port structures */
116static void a2232_init_portstructs(void);
117/* Initialize and register TTY drivers. */
118/* returns 0 IFF successful */
119static int a2232_init_drivers(void);
120
121/* BEGIN GENERIC_SERIAL PROTOTYPES */
122static void a2232_disable_tx_interrupts(void *ptr);
123static void a2232_enable_tx_interrupts(void *ptr);
124static void a2232_disable_rx_interrupts(void *ptr);
125static void a2232_enable_rx_interrupts(void *ptr);
126static int a2232_carrier_raised(struct tty_port *port);
127static void a2232_shutdown_port(void *ptr);
128static int a2232_set_real_termios(void *ptr);
129static int a2232_chars_in_buffer(void *ptr);
130static void a2232_close(void *ptr);
131static void a2232_hungup(void *ptr);
132/* static void a2232_getserial (void *ptr, struct serial_struct *sp); */
133/* END GENERIC_SERIAL PROTOTYPES */
134
135/* Functions that the TTY driver struct expects */
136static int a2232_ioctl(struct tty_struct *tty, struct file *file,
137 unsigned int cmd, unsigned long arg);
138static void a2232_throttle(struct tty_struct *tty);
139static void a2232_unthrottle(struct tty_struct *tty);
140static int a2232_open(struct tty_struct * tty, struct file * filp);
141/************************* End of Prototypes ************************/
142
143/***************************** Global variables *********************/
144/*---------------------------------------------------------------------------
145 * Interface from generic_serial.c back here
146 *--------------------------------------------------------------------------*/
147static struct real_driver a2232_real_driver = {
148 a2232_disable_tx_interrupts,
149 a2232_enable_tx_interrupts,
150 a2232_disable_rx_interrupts,
151 a2232_enable_rx_interrupts,
152 a2232_shutdown_port,
153 a2232_set_real_termios,
154 a2232_chars_in_buffer,
155 a2232_close,
156 a2232_hungup,
157 NULL /* a2232_getserial */
158};
159
160static void *a2232_driver_ID = &a2232_driver_ID; // Some memory address WE own.
161
162/* Ports structs */
163static struct a2232_port a2232_ports[MAX_A2232_BOARDS*NUMLINES];
164
165/* TTY driver structs */
166static struct tty_driver *a2232_driver;
167
168/* nr of cards completely (all ports) and correctly configured */
169static int nr_a2232;
170
171/* zorro_dev structs for the A2232's */
172static struct zorro_dev *zd_a2232[MAX_A2232_BOARDS];
173/***************************** End of Global variables **************/
174
175/* Helper functions */
176
177static inline volatile struct a2232memory *a2232mem(unsigned int board)
178{
179 return (volatile struct a2232memory *)ZTWO_VADDR(zd_a2232[board]->resource.start);
180}
181
182static inline volatile struct a2232status *a2232stat(unsigned int board,
183 unsigned int portonboard)
184{
185 volatile struct a2232memory *mem = a2232mem(board);
186 return &(mem->Status[portonboard]);
187}
188
189static inline void a2232_receive_char(struct a2232_port *port, int ch, int err)
190{
191/* Mostly stolen from other drivers.
192 Maybe one could implement a more efficient version by not only
193 transferring one character at a time.
194*/
195 struct tty_struct *tty = port->gs.port.tty;
196
197#if 0
198 switch(err) {
199 case TTY_BREAK:
200 break;
201 case TTY_PARITY:
202 break;
203 case TTY_OVERRUN:
204 break;
205 case TTY_FRAME:
206 break;
207 }
208#endif
209
210 tty_insert_flip_char(tty, ch, err);
211 tty_flip_buffer_push(tty);
212}
213
214/***************************** Functions ****************************/
215/*** BEGIN OF REAL_DRIVER FUNCTIONS ***/
216
217static void a2232_disable_tx_interrupts(void *ptr)
218{
219 struct a2232_port *port;
220 volatile struct a2232status *stat;
221 unsigned long flags;
222
223 port = ptr;
224 stat = a2232stat(port->which_a2232, port->which_port_on_a2232);
225 stat->OutDisable = -1;
226
227 /* Does this here really have to be? */
228 local_irq_save(flags);
229 port->gs.port.flags &= ~GS_TX_INTEN;
230 local_irq_restore(flags);
231}
232
233static void a2232_enable_tx_interrupts(void *ptr)
234{
235 struct a2232_port *port;
236 volatile struct a2232status *stat;
237 unsigned long flags;
238
239 port = ptr;
240 stat = a2232stat(port->which_a2232, port->which_port_on_a2232);
241 stat->OutDisable = 0;
242
243 /* Does this here really have to be? */
244 local_irq_save(flags);
245 port->gs.port.flags |= GS_TX_INTEN;
246 local_irq_restore(flags);
247}
248
249static void a2232_disable_rx_interrupts(void *ptr)
250{
251 struct a2232_port *port;
252 port = ptr;
253 port->disable_rx = -1;
254}
255
256static void a2232_enable_rx_interrupts(void *ptr)
257{
258 struct a2232_port *port;
259 port = ptr;
260 port->disable_rx = 0;
261}
262
263static int a2232_carrier_raised(struct tty_port *port)
264{
265 struct a2232_port *ap = container_of(port, struct a2232_port, gs.port);
266 return ap->cd_status;
267}
268
269static void a2232_shutdown_port(void *ptr)
270{
271 struct a2232_port *port;
272 volatile struct a2232status *stat;
273 unsigned long flags;
274
275 port = ptr;
276 stat = a2232stat(port->which_a2232, port->which_port_on_a2232);
277
278 local_irq_save(flags);
279
280 port->gs.port.flags &= ~GS_ACTIVE;
281
282 if (port->gs.port.tty && port->gs.port.tty->termios->c_cflag & HUPCL) {
283 /* Set DTR and RTS to Low, flush output.
284 The NetBSD driver "msc.c" does it this way. */
285 stat->Command = ( (stat->Command & ~A2232CMD_CMask) |
286 A2232CMD_Close );
287 stat->OutFlush = -1;
288 stat->Setup = -1;
289 }
290
291 local_irq_restore(flags);
292
293 /* After analyzing control flow, I think a2232_shutdown_port
294 is actually the last call from the system when at application
295 level someone issues a "echo Hello >>/dev/ttyY0".
296 Therefore I think the MOD_DEC_USE_COUNT should be here and
297 not in "a2232_close()". See the comment in "sx.c", too.
298 If you run into problems, compile this driver into the
299 kernel instead of compiling it as a module. */
300}
301
302static int a2232_set_real_termios(void *ptr)
303{
304 unsigned int cflag, baud, chsize, stopb, parity, softflow;
305 int rate;
306 int a2232_param, a2232_cmd;
307 unsigned long flags;
308 unsigned int i;
309 struct a2232_port *port = ptr;
310 volatile struct a2232status *status;
311 volatile struct a2232memory *mem;
312
313 if (!port->gs.port.tty || !port->gs.port.tty->termios) return 0;
314
315 status = a2232stat(port->which_a2232, port->which_port_on_a2232);
316 mem = a2232mem(port->which_a2232);
317
318 a2232_param = a2232_cmd = 0;
319
320 // get baud rate
321 baud = port->gs.baud;
322 if (baud == 0) {
323 /* speed == 0 -> drop DTR, do nothing else */
324 local_irq_save(flags);
325 // Clear DTR (and RTS... mhhh).
326 status->Command = ( (status->Command & ~A2232CMD_CMask) |
327 A2232CMD_Close );
328 status->OutFlush = -1;
329 status->Setup = -1;
330
331 local_irq_restore(flags);
332 return 0;
333 }
334
335 rate = A2232_BAUD_TABLE_NOAVAIL;
336 for (i=0; i < A2232_BAUD_TABLE_NUM_RATES * 3; i += 3){
337 if (a2232_baud_table[i] == baud){
338 if (mem->Common.Crystal == A2232_TURBO) rate = a2232_baud_table[i+2];
339 else rate = a2232_baud_table[i+1];
340 }
341 }
342 if (rate == A2232_BAUD_TABLE_NOAVAIL){
343 printk("a2232: Board %d Port %d unsupported baud rate: %d baud. Using another.\n",port->which_a2232,port->which_port_on_a2232,baud);
344 // This is useful for both (turbo or normal) Crystal versions.
345 rate = A2232PARAM_B9600;
346 }
347 a2232_param |= rate;
348
349 cflag = port->gs.port.tty->termios->c_cflag;
350
351 // get character size
352 chsize = cflag & CSIZE;
353 switch (chsize){
354 case CS8: a2232_param |= A2232PARAM_8Bit; break;
355 case CS7: a2232_param |= A2232PARAM_7Bit; break;
356 case CS6: a2232_param |= A2232PARAM_6Bit; break;
357 case CS5: a2232_param |= A2232PARAM_5Bit; break;
358 default: printk("a2232: Board %d Port %d unsupported character size: %d. Using 8 data bits.\n",
359 port->which_a2232,port->which_port_on_a2232,chsize);
360 a2232_param |= A2232PARAM_8Bit; break;
361 }
362
363 // get number of stop bits
364 stopb = cflag & CSTOPB;
365 if (stopb){ // two stop bits instead of one
366 printk("a2232: Board %d Port %d 2 stop bits unsupported. Using 1 stop bit.\n",
367 port->which_a2232,port->which_port_on_a2232);
368 }
369
370 // Warn if RTS/CTS not wanted
371 if (!(cflag & CRTSCTS)){
372#ifndef A2232_SUPPRESS_RTSCTS_WARNING
373 printk("a2232: Board %d Port %d cannot switch off firmware-implemented RTS/CTS hardware flow control.\n",
374 port->which_a2232,port->which_port_on_a2232);
375#endif
376 }
377
378 /* I think this is correct.
379 However, IXOFF means _input_ flow control and I wonder
380 if one should care about IXON _output_ flow control,
381 too. If this makes problems, one should turn the A2232
382 firmware XON/XOFF "SoftFlow" flow control off and use
383 the conventional way of inserting START/STOP characters
384 by hand in throttle()/unthrottle().
385 */
386 softflow = !!( port->gs.port.tty->termios->c_iflag & IXOFF );
387
388 // get Parity (Enabled/Disabled? If Enabled, Odd or Even?)
389 parity = cflag & (PARENB | PARODD);
390 if (parity & PARENB){
391 if (parity & PARODD){
392 a2232_cmd |= A2232CMD_OddParity;
393 }
394 else{
395 a2232_cmd |= A2232CMD_EvenParity;
396 }
397 }
398 else a2232_cmd |= A2232CMD_NoParity;
399
400
401 /* Hmm. Maybe an own a2232_port structure
402 member would be cleaner? */
403 if (cflag & CLOCAL)
404 port->gs.port.flags &= ~ASYNC_CHECK_CD;
405 else
406 port->gs.port.flags |= ASYNC_CHECK_CD;
407
408
409 /* Now we have all parameters and can go to set them: */
410 local_irq_save(flags);
411
412 status->Param = a2232_param | A2232PARAM_RcvBaud;
413 status->Command = a2232_cmd | A2232CMD_Open | A2232CMD_Enable;
414 status->SoftFlow = softflow;
415 status->OutDisable = 0;
416 status->Setup = -1;
417
418 local_irq_restore(flags);
419 return 0;
420}
421
422static int a2232_chars_in_buffer(void *ptr)
423{
424 struct a2232_port *port;
425 volatile struct a2232status *status;
426 unsigned char ret; /* we need modulo-256 arithmetics */
427 port = ptr;
428 status = a2232stat(port->which_a2232, port->which_port_on_a2232);
429#if A2232_IOBUFLEN != 256
430#error "Re-Implement a2232_chars_in_buffer()!"
431#endif
432 ret = (status->OutHead - status->OutTail);
433 return ret;
434}
435
436static void a2232_close(void *ptr)
437{
438 a2232_disable_tx_interrupts(ptr);
439 a2232_disable_rx_interrupts(ptr);
440 /* see the comment in a2232_shutdown_port above. */
441}
442
443static void a2232_hungup(void *ptr)
444{
445 a2232_close(ptr);
446}
447/*** END OF REAL_DRIVER FUNCTIONS ***/
448
449/*** BEGIN FUNCTIONS EXPECTED BY TTY DRIVER STRUCTS ***/
450static int a2232_ioctl( struct tty_struct *tty, struct file *file,
451 unsigned int cmd, unsigned long arg)
452{
453 return -ENOIOCTLCMD;
454}
455
456static void a2232_throttle(struct tty_struct *tty)
457{
458/* Throttle: System cannot take another chars: Drop RTS or
459 send the STOP char or whatever.
460 The A2232 firmware does RTS/CTS anyway, and XON/XOFF
461 if switched on. So the only thing we can do at this
462 layer here is not taking any characters out of the
463 A2232 buffer any more. */
464 struct a2232_port *port = tty->driver_data;
465 port->throttle_input = -1;
466}
467
468static void a2232_unthrottle(struct tty_struct *tty)
469{
470/* Unthrottle: dual to "throttle()" above. */
471 struct a2232_port *port = tty->driver_data;
472 port->throttle_input = 0;
473}
474
475static int a2232_open(struct tty_struct * tty, struct file * filp)
476{
477/* More or less stolen from other drivers. */
478 int line;
479 int retval;
480 struct a2232_port *port;
481
482 line = tty->index;
483 port = &a2232_ports[line];
484
485 tty->driver_data = port;
486 port->gs.port.tty = tty;
487 port->gs.port.count++;
488 retval = gs_init_port(&port->gs);
489 if (retval) {
490 port->gs.port.count--;
491 return retval;
492 }
493 port->gs.port.flags |= GS_ACTIVE;
494 retval = gs_block_til_ready(port, filp);
495
496 if (retval) {
497 port->gs.port.count--;
498 return retval;
499 }
500
501 a2232_enable_rx_interrupts(port);
502
503 return 0;
504}
505/*** END OF FUNCTIONS EXPECTED BY TTY DRIVER STRUCTS ***/
506
507static irqreturn_t a2232_vbl_inter(int irq, void *data)
508{
509#if A2232_IOBUFLEN != 256
510#error "Re-Implement a2232_vbl_inter()!"
511#endif
512
513struct a2232_port *port;
514volatile struct a2232memory *mem;
515volatile struct a2232status *status;
516unsigned char newhead;
517unsigned char bufpos; /* Must be unsigned char. We need the modulo-256 arithmetics */
518unsigned char ncd, ocd, ccd; /* names consistent with the NetBSD driver */
519volatile u_char *ibuf, *cbuf, *obuf;
520int ch, err, n, p;
521 for (n = 0; n < nr_a2232; n++){ /* for every completely initialized A2232 board */
522 mem = a2232mem(n);
523 for (p = 0; p < NUMLINES; p++){ /* for every port on this board */
524 err = 0;
525 port = &a2232_ports[n*NUMLINES+p];
526 if ( port->gs.port.flags & GS_ACTIVE ){ /* if the port is used */
527
528 status = a2232stat(n,p);
529
530 if (!port->disable_rx && !port->throttle_input){ /* If input is not disabled */
531 newhead = status->InHead; /* 65EC02 write pointer */
532 bufpos = status->InTail;
533
534 /* check for input for this port */
535 if (newhead != bufpos) {
536 /* buffer for input chars/events */
537 ibuf = mem->InBuf[p];
538
539 /* data types of bytes in ibuf */
540 cbuf = mem->InCtl[p];
541
542 /* do for all chars */
543 while (bufpos != newhead) {
544 /* which type of input data? */
545 switch (cbuf[bufpos]) {
546 /* switch on input event (CD, BREAK, etc.) */
547 case A2232INCTL_EVENT:
548 switch (ibuf[bufpos++]) {
549 case A2232EVENT_Break:
550 /* TODO: Handle BREAK signal */
551 break;
552 /* A2232EVENT_CarrierOn and A2232EVENT_CarrierOff are
553 handled in a separate queue and should not occur here. */
554 case A2232EVENT_Sync:
555 printk("A2232: 65EC02 software sent SYNC event, don't know what to do. Ignoring.");
556 break;
557 default:
558 printk("A2232: 65EC02 software broken, unknown event type %d occurred.\n",ibuf[bufpos-1]);
559 } /* event type switch */
560 break;
561 case A2232INCTL_CHAR:
562 /* Receive incoming char */
563 a2232_receive_char(port, ibuf[bufpos], err);
564 bufpos++;
565 break;
566 default:
567 printk("A2232: 65EC02 software broken, unknown data type %d occurred.\n",cbuf[bufpos]);
568 bufpos++;
569 } /* switch on input data type */
570 } /* while there's something in the buffer */
571
572 status->InTail = bufpos; /* tell 65EC02 what we've read */
573
574 } /* if there was something in the buffer */
575 } /* If input is not disabled */
576
577 /* Now check if there's something to output */
578 obuf = mem->OutBuf[p];
579 bufpos = status->OutHead;
580 while ( (port->gs.xmit_cnt > 0) &&
581 (!port->gs.port.tty->stopped) &&
582 (!port->gs.port.tty->hw_stopped) ){ /* While there are chars to transmit */
583 if (((bufpos+1) & A2232_IOBUFLENMASK) != status->OutTail) { /* If the A2232 buffer is not full */
584 ch = port->gs.xmit_buf[port->gs.xmit_tail]; /* get the next char to transmit */
585 port->gs.xmit_tail = (port->gs.xmit_tail+1) & (SERIAL_XMIT_SIZE-1); /* modulo-addition for the gs.xmit_buf ring-buffer */
586 obuf[bufpos++] = ch; /* put it into the A2232 buffer */
587 port->gs.xmit_cnt--;
588 }
589 else{ /* If A2232 the buffer is full */
590 break; /* simply stop filling it. */
591 }
592 }
593 status->OutHead = bufpos;
594
595 /* WakeUp if output buffer runs low */
596 if ((port->gs.xmit_cnt <= port->gs.wakeup_chars) && port->gs.port.tty) {
597 tty_wakeup(port->gs.port.tty);
598 }
599 } // if the port is used
600 } // for every port on the board
601
602 /* Now check the CD message queue */
603 newhead = mem->Common.CDHead;
604 bufpos = mem->Common.CDTail;
605 if (newhead != bufpos){ /* There are CD events in queue */
606 ocd = mem->Common.CDStatus; /* get old status bits */
607 while (newhead != bufpos){ /* read all events */
608 ncd = mem->CDBuf[bufpos++]; /* get one event */
609 ccd = ncd ^ ocd; /* mask of changed lines */
610 ocd = ncd; /* save new status bits */
611 for(p=0; p < NUMLINES; p++){ /* for all ports */
612 if (ccd & 1){ /* this one changed */
613
614 struct a2232_port *port = &a2232_ports[n*7+p];
615 port->cd_status = !(ncd & 1); /* ncd&1 <=> CD is now off */
616
617 if (!(port->gs.port.flags & ASYNC_CHECK_CD))
618 ; /* Don't report DCD changes */
619 else if (port->cd_status) { // if DCD on: DCD went UP!
620
621 /* Are we blocking in open?*/
622 wake_up_interruptible(&port->gs.port.open_wait);
623 }
624 else { // if DCD off: DCD went DOWN!
625 if (port->gs.port.tty)
626 tty_hangup (port->gs.port.tty);
627 }
628
629 } // if CD changed for this port
630 ccd >>= 1;
631 ncd >>= 1; /* Shift bits for next line */
632 } // for every port
633 } // while CD events in queue
634 mem->Common.CDStatus = ocd; /* save new status */
635 mem->Common.CDTail = bufpos; /* remove events */
636 } // if events in CD queue
637
638 } // for every completely initialized A2232 board
639 return IRQ_HANDLED;
640}
641
642static const struct tty_port_operations a2232_port_ops = {
643 .carrier_raised = a2232_carrier_raised,
644};
645
646static void a2232_init_portstructs(void)
647{
648 struct a2232_port *port;
649 int i;
650
651 for (i = 0; i < MAX_A2232_BOARDS*NUMLINES; i++) {
652 port = a2232_ports + i;
653 tty_port_init(&port->gs.port);
654 port->gs.port.ops = &a2232_port_ops;
655 port->which_a2232 = i/NUMLINES;
656 port->which_port_on_a2232 = i%NUMLINES;
657 port->disable_rx = port->throttle_input = port->cd_status = 0;
658 port->gs.magic = A2232_MAGIC;
659 port->gs.close_delay = HZ/2;
660 port->gs.closing_wait = 30 * HZ;
661 port->gs.rd = &a2232_real_driver;
662 }
663}
664
665static const struct tty_operations a2232_ops = {
666 .open = a2232_open,
667 .close = gs_close,
668 .write = gs_write,
669 .put_char = gs_put_char,
670 .flush_chars = gs_flush_chars,
671 .write_room = gs_write_room,
672 .chars_in_buffer = gs_chars_in_buffer,
673 .flush_buffer = gs_flush_buffer,
674 .ioctl = a2232_ioctl,
675 .throttle = a2232_throttle,
676 .unthrottle = a2232_unthrottle,
677 .set_termios = gs_set_termios,
678 .stop = gs_stop,
679 .start = gs_start,
680 .hangup = gs_hangup,
681};
682
683static int a2232_init_drivers(void)
684{
685 int error;
686
687 a2232_driver = alloc_tty_driver(NUMLINES * nr_a2232);
688 if (!a2232_driver)
689 return -ENOMEM;
690 a2232_driver->owner = THIS_MODULE;
691 a2232_driver->driver_name = "commodore_a2232";
692 a2232_driver->name = "ttyY";
693 a2232_driver->major = A2232_NORMAL_MAJOR;
694 a2232_driver->type = TTY_DRIVER_TYPE_SERIAL;
695 a2232_driver->subtype = SERIAL_TYPE_NORMAL;
696 a2232_driver->init_termios = tty_std_termios;
697 a2232_driver->init_termios.c_cflag =
698 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
699 a2232_driver->init_termios.c_ispeed = 9600;
700 a2232_driver->init_termios.c_ospeed = 9600;
701 a2232_driver->flags = TTY_DRIVER_REAL_RAW;
702 tty_set_operations(a2232_driver, &a2232_ops);
703 if ((error = tty_register_driver(a2232_driver))) {
704 printk(KERN_ERR "A2232: Couldn't register A2232 driver, error = %d\n",
705 error);
706 put_tty_driver(a2232_driver);
707 return 1;
708 }
709 return 0;
710}
711
712static int __init a2232board_init(void)
713{
714 struct zorro_dev *z;
715
716 unsigned int boardaddr;
717 int bcount;
718 short start;
719 u_char *from;
720 volatile u_char *to;
721 volatile struct a2232memory *mem;
722 int error, i;
723
724#ifdef CONFIG_SMP
725 return -ENODEV; /* This driver is not SMP aware. Is there an SMP ZorroII-bus-machine? */
726#endif
727
728 if (!MACH_IS_AMIGA){
729 return -ENODEV;
730 }
731
732 printk("Commodore A2232 driver initializing.\n"); /* Say that we're alive. */
733
734 z = NULL;
735 nr_a2232 = 0;
736 while ( (z = zorro_find_device(ZORRO_WILDCARD, z)) ){
737 if ( (z->id != ZORRO_PROD_CBM_A2232_PROTOTYPE) &&
738 (z->id != ZORRO_PROD_CBM_A2232) ){
739 continue; // The board found was no A2232
740 }
741 if (!zorro_request_device(z,"A2232 driver"))
742 continue;
743
744 printk("Commodore A2232 found (#%d).\n",nr_a2232);
745
746 zd_a2232[nr_a2232] = z;
747
748 boardaddr = ZTWO_VADDR( z->resource.start );
749 printk("Board is located at address 0x%x, size is 0x%x.\n", boardaddr, (unsigned int) ((z->resource.end+1) - (z->resource.start)));
750
751 mem = (volatile struct a2232memory *) boardaddr;
752
753 (void) mem->Enable6502Reset; /* copy the code across to the board */
754 to = (u_char *)mem; from = a2232_65EC02code; bcount = sizeof(a2232_65EC02code) - 2;
755 start = *(short *)from;
756 from += sizeof(start);
757 to += start;
758 while(bcount--) *to++ = *from++;
759 printk("65EC02 software uploaded to the A2232 memory.\n");
760
761 mem->Common.Crystal = A2232_UNKNOWN; /* use automatic speed check */
762
763 /* start 6502 running */
764 (void) mem->ResetBoard;
765 printk("A2232's 65EC02 CPU up and running.\n");
766
767 /* wait until speed detector has finished */
768 for (bcount = 0; bcount < 2000; bcount++) {
769 udelay(1000);
770 if (mem->Common.Crystal)
771 break;
772 }
773 printk((mem->Common.Crystal?"A2232 oscillator crystal detected by 65EC02 software: ":"65EC02 software could not determine A2232 oscillator crystal: "));
774 switch (mem->Common.Crystal){
775 case A2232_UNKNOWN:
776 printk("Unknown crystal.\n");
777 break;
778 case A2232_NORMAL:
779 printk ("Normal crystal.\n");
780 break;
781 case A2232_TURBO:
782 printk ("Turbo crystal.\n");
783 break;
784 default:
785 printk ("0x%x. Huh?\n",mem->Common.Crystal);
786 }
787
788 nr_a2232++;
789
790 }
791
792 printk("Total: %d A2232 boards initialized.\n", nr_a2232); /* Some status report if no card was found */
793
794 a2232_init_portstructs();
795
796 /*
797 a2232_init_drivers also registers the drivers. Must be here because all boards
798 have to be detected first.
799 */
800 if (a2232_init_drivers()) return -ENODEV; // maybe we should use a different -Exxx?
801
802 error = request_irq(IRQ_AMIGA_VERTB, a2232_vbl_inter, 0,
803 "A2232 serial VBL", a2232_driver_ID);
804 if (error) {
805 for (i = 0; i < nr_a2232; i++)
806 zorro_release_device(zd_a2232[i]);
807 tty_unregister_driver(a2232_driver);
808 put_tty_driver(a2232_driver);
809 }
810 return error;
811}
812
813static void __exit a2232board_exit(void)
814{
815 int i;
816
817 for (i = 0; i < nr_a2232; i++) {
818 zorro_release_device(zd_a2232[i]);
819 }
820
821 tty_unregister_driver(a2232_driver);
822 put_tty_driver(a2232_driver);
823 free_irq(IRQ_AMIGA_VERTB, a2232_driver_ID);
824}
825
826module_init(a2232board_init);
827module_exit(a2232board_exit);
828
829MODULE_AUTHOR("Enver Haase");
830MODULE_DESCRIPTION("Amiga A2232 multi-serial board driver");
831MODULE_LICENSE("GPL");
diff --git a/drivers/char/ser_a2232.h b/drivers/char/ser_a2232.h
deleted file mode 100644
index bc09eb9e118b..000000000000
--- a/drivers/char/ser_a2232.h
+++ /dev/null
@@ -1,202 +0,0 @@
1/* drivers/char/ser_a2232.h */
2
3/* $Id: ser_a2232.h,v 0.4 2000/01/25 12:00:00 ehaase Exp $ */
4
5/* Linux serial driver for the Amiga A2232 board */
6
7/* This driver is MAINTAINED. Before applying any changes, please contact
8 * the author.
9 */
10
11/* Copyright (c) 2000-2001 Enver Haase <ehaase@inf.fu-berlin.de>
12 * alias The A2232 driver project <A2232@gmx.net>
13 * All rights reserved.
14 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 *
29 */
30
31#ifndef _SER_A2232_H_
32#define _SER_A2232_H_
33
34/*
35 How many boards are to be supported at maximum;
36 "up to five A2232 Multiport Serial Cards may be installed in a
37 single Amiga 2000" states the A2232 User's Guide. If you have
38 more slots available, you might want to change the value below.
39*/
40#define MAX_A2232_BOARDS 5
41
42#ifndef A2232_NORMAL_MAJOR
43/* This allows overriding on the compiler commandline, or in a "major.h"
44 include or something like that */
45#define A2232_NORMAL_MAJOR 224 /* /dev/ttyY* */
46#define A2232_CALLOUT_MAJOR 225 /* /dev/cuy* */
47#endif
48
49/* Some magic is always good - Who knows :) */
50#define A2232_MAGIC 0x000a2232
51
52/* A2232 port structure to keep track of the
53 status of every single line used */
54struct a2232_port{
55 struct gs_port gs;
56 unsigned int which_a2232;
57 unsigned int which_port_on_a2232;
58 short disable_rx;
59 short throttle_input;
60 short cd_status;
61};
62
63#define NUMLINES 7 /* number of lines per board */
64#define A2232_IOBUFLEN 256 /* number of bytes per buffer */
65#define A2232_IOBUFLENMASK 0xff /* mask for maximum number of bytes */
66
67
68#define A2232_UNKNOWN 0 /* crystal not known */
69#define A2232_NORMAL 1 /* normal A2232 (1.8432 MHz oscillator) */
70#define A2232_TURBO 2 /* turbo A2232 (3.6864 MHz oscillator) */
71
72
73struct a2232common {
74 char Crystal; /* normal (1) or turbo (2) board? */
75 u_char Pad_a;
76 u_char TimerH; /* timer value after speed check */
77 u_char TimerL;
78 u_char CDHead; /* head pointer for CD message queue */
79 u_char CDTail; /* tail pointer for CD message queue */
80 u_char CDStatus;
81 u_char Pad_b;
82};
83
84struct a2232status {
85 u_char InHead; /* input queue head */
86 u_char InTail; /* input queue tail */
87 u_char OutDisable; /* disables output */
88 u_char OutHead; /* output queue head */
89 u_char OutTail; /* output queue tail */
90 u_char OutCtrl; /* soft flow control character to send */
91 u_char OutFlush; /* flushes output buffer */
92 u_char Setup; /* causes reconfiguration */
93 u_char Param; /* parameter byte - see A2232PARAM */
94 u_char Command; /* command byte - see A2232CMD */
95 u_char SoftFlow; /* enables xon/xoff flow control */
96 /* private 65EC02 fields: */
97 u_char XonOff; /* stores XON/XOFF enable/disable */
98};
99
100#define A2232_MEMPAD1 \
101 (0x0200 - NUMLINES * sizeof(struct a2232status) - \
102 sizeof(struct a2232common))
103#define A2232_MEMPAD2 (0x2000 - NUMLINES * A2232_IOBUFLEN - A2232_IOBUFLEN)
104
105struct a2232memory {
106 struct a2232status Status[NUMLINES]; /* 0x0000-0x006f status areas */
107 struct a2232common Common; /* 0x0070-0x0077 common flags */
108 u_char Dummy1[A2232_MEMPAD1]; /* 0x00XX-0x01ff */
109 u_char OutBuf[NUMLINES][A2232_IOBUFLEN];/* 0x0200-0x08ff output bufs */
110 u_char InBuf[NUMLINES][A2232_IOBUFLEN]; /* 0x0900-0x0fff input bufs */
111 u_char InCtl[NUMLINES][A2232_IOBUFLEN]; /* 0x1000-0x16ff control data */
112 u_char CDBuf[A2232_IOBUFLEN]; /* 0x1700-0x17ff CD event buffer */
113 u_char Dummy2[A2232_MEMPAD2]; /* 0x1800-0x2fff */
114 u_char Code[0x1000]; /* 0x3000-0x3fff code area */
115 u_short InterruptAck; /* 0x4000 intr ack */
116 u_char Dummy3[0x3ffe]; /* 0x4002-0x7fff */
117 u_short Enable6502Reset; /* 0x8000 Stop board, */
118 /* 6502 RESET line held low */
119 u_char Dummy4[0x3ffe]; /* 0x8002-0xbfff */
120 u_short ResetBoard; /* 0xc000 reset board & run, */
121 /* 6502 RESET line held high */
122};
123
124#undef A2232_MEMPAD1
125#undef A2232_MEMPAD2
126
127#define A2232INCTL_CHAR 0 /* corresponding byte in InBuf is a character */
128#define A2232INCTL_EVENT 1 /* corresponding byte in InBuf is an event */
129
130#define A2232EVENT_Break 1 /* break set */
131#define A2232EVENT_CarrierOn 2 /* carrier raised */
132#define A2232EVENT_CarrierOff 3 /* carrier dropped */
133#define A2232EVENT_Sync 4 /* don't know, defined in 2232.ax */
134
135#define A2232CMD_Enable 0x1 /* enable/DTR bit */
136#define A2232CMD_Close 0x2 /* close the device */
137#define A2232CMD_Open 0xb /* open the device */
138#define A2232CMD_CMask 0xf /* command mask */
139#define A2232CMD_RTSOff 0x0 /* turn off RTS */
140#define A2232CMD_RTSOn 0x8 /* turn on RTS */
141#define A2232CMD_Break 0xd /* transmit a break */
142#define A2232CMD_RTSMask 0xc /* mask for RTS stuff */
143#define A2232CMD_NoParity 0x00 /* don't use parity */
144#define A2232CMD_OddParity 0x20 /* odd parity */
145#define A2232CMD_EvenParity 0x60 /* even parity */
146#define A2232CMD_ParityMask 0xe0 /* parity mask */
147
148#define A2232PARAM_B115200 0x0 /* baud rates */
149#define A2232PARAM_B50 0x1
150#define A2232PARAM_B75 0x2
151#define A2232PARAM_B110 0x3
152#define A2232PARAM_B134 0x4
153#define A2232PARAM_B150 0x5
154#define A2232PARAM_B300 0x6
155#define A2232PARAM_B600 0x7
156#define A2232PARAM_B1200 0x8
157#define A2232PARAM_B1800 0x9
158#define A2232PARAM_B2400 0xa
159#define A2232PARAM_B3600 0xb
160#define A2232PARAM_B4800 0xc
161#define A2232PARAM_B7200 0xd
162#define A2232PARAM_B9600 0xe
163#define A2232PARAM_B19200 0xf
164#define A2232PARAM_BaudMask 0xf /* baud rate mask */
165#define A2232PARAM_RcvBaud 0x10 /* enable receive baud rate */
166#define A2232PARAM_8Bit 0x00 /* numbers of bits */
167#define A2232PARAM_7Bit 0x20
168#define A2232PARAM_6Bit 0x40
169#define A2232PARAM_5Bit 0x60
170#define A2232PARAM_BitMask 0x60 /* numbers of bits mask */
171
172
173/* Standard speeds tables, -1 means unavailable, -2 means 0 baud: switch off line */
174#define A2232_BAUD_TABLE_NOAVAIL -1
175#define A2232_BAUD_TABLE_NUM_RATES (18)
176static int a2232_baud_table[A2232_BAUD_TABLE_NUM_RATES*3] = {
177 //Baud //Normal //Turbo
178 50, A2232PARAM_B50, A2232_BAUD_TABLE_NOAVAIL,
179 75, A2232PARAM_B75, A2232_BAUD_TABLE_NOAVAIL,
180 110, A2232PARAM_B110, A2232_BAUD_TABLE_NOAVAIL,
181 134, A2232PARAM_B134, A2232_BAUD_TABLE_NOAVAIL,
182 150, A2232PARAM_B150, A2232PARAM_B75,
183 200, A2232_BAUD_TABLE_NOAVAIL, A2232_BAUD_TABLE_NOAVAIL,
184 300, A2232PARAM_B300, A2232PARAM_B150,
185 600, A2232PARAM_B600, A2232PARAM_B300,
186 1200, A2232PARAM_B1200, A2232PARAM_B600,
187 1800, A2232PARAM_B1800, A2232_BAUD_TABLE_NOAVAIL,
188 2400, A2232PARAM_B2400, A2232PARAM_B1200,
189 4800, A2232PARAM_B4800, A2232PARAM_B2400,
190 9600, A2232PARAM_B9600, A2232PARAM_B4800,
191 19200, A2232PARAM_B19200, A2232PARAM_B9600,
192 38400, A2232_BAUD_TABLE_NOAVAIL, A2232PARAM_B19200,
193 57600, A2232_BAUD_TABLE_NOAVAIL, A2232_BAUD_TABLE_NOAVAIL,
194#ifdef A2232_SPEEDHACK
195 115200, A2232PARAM_B115200, A2232_BAUD_TABLE_NOAVAIL,
196 230400, A2232_BAUD_TABLE_NOAVAIL, A2232PARAM_B115200
197#else
198 115200, A2232_BAUD_TABLE_NOAVAIL, A2232_BAUD_TABLE_NOAVAIL,
199 230400, A2232_BAUD_TABLE_NOAVAIL, A2232_BAUD_TABLE_NOAVAIL
200#endif
201};
202#endif
diff --git a/drivers/char/ser_a2232fw.ax b/drivers/char/ser_a2232fw.ax
deleted file mode 100644
index 736438032768..000000000000
--- a/drivers/char/ser_a2232fw.ax
+++ /dev/null
@@ -1,529 +0,0 @@
1;.lib "axm"
2;
3;begin
4;title "A2232 serial board driver"
5;
6;set modules "2232"
7;set executable "2232.bin"
8;
9;;;;set nolink
10;
11;set temporary directory "t:"
12;
13;set assembly options "-m6502 -l60:t:list"
14;set link options "bin"; loadadr"
15;;;bin2c 2232.bin msc6502.h msc6502code
16;end
17;
18;
19; ### Commodore A2232 serial board driver for NetBSD by JM v1.3 ###
20;
21; - Created 950501 by JM -
22;
23;
24; Serial board driver software.
25;
26;
27% Copyright (c) 1995 Jukka Marin <jmarin@jmp.fi>.
28% All rights reserved.
29%
30% Redistribution and use in source and binary forms, with or without
31% modification, are permitted provided that the following conditions
32% are met:
33% 1. Redistributions of source code must retain the above copyright
34% notice, and the entire permission notice in its entirety,
35% including the disclaimer of warranties.
36% 2. Redistributions in binary form must reproduce the above copyright
37% notice, this list of conditions and the following disclaimer in the
38% documentation and/or other materials provided with the distribution.
39% 3. The name of the author may not be used to endorse or promote
40% products derived from this software without specific prior
41% written permission.
42%
43% ALTERNATIVELY, this product may be distributed under the terms of
44% the GNU General Public License, in which case the provisions of the
45% GPL are required INSTEAD OF the above restrictions. (This clause is
46% necessary due to a potential bad interaction between the GPL and
47% the restrictions contained in a BSD-style copyright.)
48%
49% THIS SOFTWARE IS PROVIDED `AS IS'' AND ANY EXPRESS OR IMPLIED
50% WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
51% OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
52% DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
53% INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
54% (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
55% SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56% HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
57% STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
58% ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
59% OF THE POSSIBILITY OF SUCH DAMAGE.
60;
61;
62; Bugs:
63;
64; - Can't send a break yet
65;
66;
67;
68; Edited:
69;
70; - 950501 by JM -> v0.1 - Created this file.
71; - 951029 by JM -> v1.3 - Carrier Detect events now queued in a separate
72; queue.
73;
74;
75
76
77CODE equ $3800 ; start address for program code
78
79
80CTL_CHAR equ $00 ; byte in ibuf is a character
81CTL_EVENT equ $01 ; byte in ibuf is an event
82
83EVENT_BREAK equ $01
84EVENT_CDON equ $02
85EVENT_CDOFF equ $03
86EVENT_SYNC equ $04
87
88XON equ $11
89XOFF equ $13
90
91
92VARBASE macro *starting_address ; was VARINIT
93_varbase set \1
94 endm
95
96VARDEF macro *name space_needs
97\1 equ _varbase
98_varbase set _varbase+\2
99 endm
100
101
102stz macro * address
103 db $64,\1
104 endm
105
106stzax macro * address
107 db $9e,<\1,>\1
108 endm
109
110
111biti macro * immediate value
112 db $89,\1
113 endm
114
115smb0 macro * address
116 db $87,\1
117 endm
118smb1 macro * address
119 db $97,\1
120 endm
121smb2 macro * address
122 db $a7,\1
123 endm
124smb3 macro * address
125 db $b7,\1
126 endm
127smb4 macro * address
128 db $c7,\1
129 endm
130smb5 macro * address
131 db $d7,\1
132 endm
133smb6 macro * address
134 db $e7,\1
135 endm
136smb7 macro * address
137 db $f7,\1
138 endm
139
140
141
142;-----------------------------------------------------------------------;
143; ;
144; stuff common for all ports, non-critical (run once / loop) ;
145; ;
146DO_SLOW macro * port_number ;
147 .local ; ;
148 lda CIA+C_PA ; check all CD inputs ;
149 cmp CommonCDo ; changed from previous accptd? ;
150 beq =over ; nope, do nothing else here ;
151 ; ;
152 cmp CommonCDb ; bouncing? ;
153 beq =nobounce ; nope -> ;
154 ; ;
155 sta CommonCDb ; save current state ;
156 lda #64 ; reinitialize counter ;
157 sta CommonCDc ; ;
158 jmp =over ; skip CD save ;
159 ; ;
160=nobounce dec CommonCDc ; no, decrement bounce counter ;
161 bpl =over ; not done yet, so skip CD save ;
162 ; ;
163=saveCD ldx CDHead ; get write index ;
164 sta cdbuf,x ; save status in buffer ;
165 inx ; ;
166 cpx CDTail ; buffer full? ;
167 .if ne ; no: preserve status: ;
168 stx CDHead ; update index in RAM ;
169 sta CommonCDo ; save state for the next check ;
170 .end ; ;
171=over .end local ;
172 endm ;
173 ;
174;-----------------------------------------------------------------------;
175
176
177; port specific stuff (no data transfer)
178
179DO_PORT macro * port_number
180 .local ; ;
181 lda SetUp\1 ; reconfiguration request? ;
182 .if ne ; yes: ;
183 lda SoftFlow\1 ; get XON/XOFF flag ;
184 sta XonOff\1 ; save it ;
185 lda Param\1 ; get parameter ;
186 ora #%00010000 ; use baud generator for Rx ;
187 sta ACIA\1+A_CTRL ; store in control register ;
188 stz OutDisable\1 ; enable transmit output ;
189 stz SetUp\1 ; no reconfiguration no more ;
190 .end ; ;
191 ; ;
192 lda InHead\1 ; get write index ;
193 sbc InTail\1 ; buffer full soon? ;
194 cmp #200 ; 200 chars or more in buffer? ;
195 lda Command\1 ; get Command reg value ;
196 and #%11110011 ; turn RTS OFF by default ;
197 .if cc ; still room in buffer: ;
198 ora #%00001000 ; turn RTS ON ;
199 .end ; ;
200 sta ACIA\1+A_CMD ; set/clear RTS ;
201 ; ;
202 lda OutFlush\1 ; request to flush output buffer;
203 .if ne ; yessh! ;
204 lda OutHead\1 ; get head ;
205 sta OutTail\1 ; save as tail ;
206 stz OutDisable\1 ; enable transmit output ;
207 stz OutFlush\1 ; clear request ;
208 .end
209 .end local
210 endm
211
212
213DO_DATA macro * port number
214 .local
215 lda ACIA\1+A_SR ; read ACIA status register ;
216 biti [1<<3] ; something received? ;
217 .if ne ; yes: ;
218 biti [1<<1] ; framing error? ;
219 .if ne ; yes: ;
220 lda ACIA\1+A_DATA ; read received character ;
221 bne =SEND ; not break -> ignore it ;
222 ldx InHead\1 ; get write pointer ;
223 lda #CTL_EVENT ; get type of byte ;
224 sta ictl\1,x ; save it in InCtl buffer ;
225 lda #EVENT_BREAK ; event code ;
226 sta ibuf\1,x ; save it as well ;
227 inx ; ;
228 cpx InTail\1 ; still room in buffer? ;
229 .if ne ; absolutely: ;
230 stx InHead\1 ; update index in memory ;
231 .end ; ;
232 jmp =SEND ; go check if anything to send ;
233 .end ; ;
234 ; normal char received: ;
235 ldx InHead\1 ; get write index ;
236 lda ACIA\1+A_DATA ; read received character ;
237 sta ibuf\1,x ; save char in buffer ;
238 stzax ictl\1 ; set type to CTL_CHAR ;
239 inx ; ;
240 cpx InTail\1 ; buffer full? ;
241 .if ne ; no: preserve character: ;
242 stx InHead\1 ; update index in RAM ;
243 .end ; ;
244 and #$7f ; mask off parity if any ;
245 cmp #XOFF ; XOFF from remote host? ;
246 .if eq ; yes: ;
247 lda XonOff\1 ; if XON/XOFF handshaking.. ;
248 sta OutDisable\1 ; ..disable transmitter ;
249 .end ; ;
250 .end ; ;
251 ; ;
252 ; BUFFER FULL CHECK WAS HERE ;
253 ; ;
254=SEND lda ACIA\1+A_SR ; transmit register empty? ;
255 and #[1<<4] ; ;
256 .if ne ; yes: ;
257 ldx OutCtrl\1 ; sending out XON/XOFF? ;
258 .if ne ; yes: ;
259 lda CIA+C_PB ; check CTS signal ;
260 and #[1<<\1] ; (for this port only) ;
261 bne =DONE ; not allowed to send -> done ;
262 stx ACIA\1+A_DATA ; transmit control char ;
263 stz OutCtrl\1 ; clear flag ;
264 jmp =DONE ; and we're done ;
265 .end ; ;
266 ; ;
267 ldx OutTail\1 ; anything to transmit? ;
268 cpx OutHead\1 ; ;
269 .if ne ; yes: ;
270 lda OutDisable\1 ; allowed to transmit? ;
271 .if eq ; yes: ;
272 lda CIA+C_PB ; check CTS signal ;
273 and #[1<<\1] ; (for this port only) ;
274 bne =DONE ; not allowed to send -> done ;
275 lda obuf\1,x ; get a char from buffer ;
276 sta ACIA\1+A_DATA ; send it away ;
277 inc OutTail\1 ; update read index ;
278 .end ; ;
279 .end ; ;
280 .end ; ;
281=DONE .end local
282 endm
283
284
285
286PORTVAR macro * port number
287 VARDEF InHead\1 1
288 VARDEF InTail\1 1
289 VARDEF OutDisable\1 1
290 VARDEF OutHead\1 1
291 VARDEF OutTail\1 1
292 VARDEF OutCtrl\1 1
293 VARDEF OutFlush\1 1
294 VARDEF SetUp\1 1
295 VARDEF Param\1 1
296 VARDEF Command\1 1
297 VARDEF SoftFlow\1 1
298 ; private:
299 VARDEF XonOff\1 1
300 endm
301
302
303 VARBASE 0 ; start variables at address $0000
304 PORTVAR 0 ; define variables for port 0
305 PORTVAR 1 ; define variables for port 1
306 PORTVAR 2 ; define variables for port 2
307 PORTVAR 3 ; define variables for port 3
308 PORTVAR 4 ; define variables for port 4
309 PORTVAR 5 ; define variables for port 5
310 PORTVAR 6 ; define variables for port 6
311
312
313
314 VARDEF Crystal 1 ; 0 = unknown, 1 = normal, 2 = turbo
315 VARDEF Pad_a 1
316 VARDEF TimerH 1
317 VARDEF TimerL 1
318 VARDEF CDHead 1
319 VARDEF CDTail 1
320 VARDEF CDStatus 1
321 VARDEF Pad_b 1
322
323 VARDEF CommonCDo 1 ; for carrier detect optimization
324 VARDEF CommonCDc 1 ; for carrier detect debouncing
325 VARDEF CommonCDb 1 ; for carrier detect debouncing
326
327
328 VARBASE $0200
329 VARDEF obuf0 256 ; output data (characters only)
330 VARDEF obuf1 256
331 VARDEF obuf2 256
332 VARDEF obuf3 256
333 VARDEF obuf4 256
334 VARDEF obuf5 256
335 VARDEF obuf6 256
336
337 VARDEF ibuf0 256 ; input data (characters, events etc - see ictl)
338 VARDEF ibuf1 256
339 VARDEF ibuf2 256
340 VARDEF ibuf3 256
341 VARDEF ibuf4 256
342 VARDEF ibuf5 256
343 VARDEF ibuf6 256
344
345 VARDEF ictl0 256 ; input control information (type of data in ibuf)
346 VARDEF ictl1 256
347 VARDEF ictl2 256
348 VARDEF ictl3 256
349 VARDEF ictl4 256
350 VARDEF ictl5 256
351 VARDEF ictl6 256
352
353 VARDEF cdbuf 256 ; CD event queue
354
355
356ACIA0 equ $4400
357ACIA1 equ $4c00
358ACIA2 equ $5400
359ACIA3 equ $5c00
360ACIA4 equ $6400
361ACIA5 equ $6c00
362ACIA6 equ $7400
363
364A_DATA equ $00
365A_SR equ $02
366A_CMD equ $04
367A_CTRL equ $06
368; 00 write transmit data read received data
369; 02 reset ACIA read status register
370; 04 write command register read command register
371; 06 write control register read control register
372
373CIA equ $7c00 ; 8520 CIA
374C_PA equ $00 ; port A data register
375C_PB equ $02 ; port B data register
376C_DDRA equ $04 ; data direction register for port A
377C_DDRB equ $06 ; data direction register for port B
378C_TAL equ $08 ; timer A
379C_TAH equ $0a
380C_TBL equ $0c ; timer B
381C_TBH equ $0e
382C_TODL equ $10 ; TOD LSB
383C_TODM equ $12 ; TOD middle byte
384C_TODH equ $14 ; TOD MSB
385C_DATA equ $18 ; serial data register
386C_INTCTRL equ $1a ; interrupt control register
387C_CTRLA equ $1c ; control register A
388C_CTRLB equ $1e ; control register B
389
390
391
392
393
394 section main,code,CODE-2
395
396 db >CODE,<CODE
397
398;-----------------------------------------------------------------------;
399; here's the initialization code: ;
400; ;
401R_RESET ldx #$ff ;
402 txs ; initialize stack pointer ;
403 cld ; in case a 6502 is used... ;
404 ldx #0 ; ;
405 lda #0 ; ;
406 ldy #Crystal ; this many bytes to clear ;
407clr_loop sta 0,x ; clear zero page variables ;
408 inx ; ;
409 dey ; ;
410 bne clr_loop ; ;
411 ; ;
412 stz CommonCDo ; force CD test at boot ;
413 stz CommonCDb ; ;
414 stz CDHead ; clear queue ;
415 stz CDTail ; ;
416 ; ;
417 lda #0 ; ;
418 sta Pad_a ; ;
419 lda #170 ; test cmp ;
420 cmp #100 ; ;
421 .if cs ; ;
422 inc Pad_a ; C was set ;
423 .end ; ;
424 ;
425;-----------------------------------------------------------------------;
426; Speed check ;
427;-----------------------------------------------------------------------;
428 ;
429 lda Crystal ; speed already set? ;
430 beq DoSpeedy ; ;
431 jmp LOOP ; yes, skip speed test ;
432 ; ;
433DoSpeedy lda #%10011000 ; 8N1, 1200/2400 bps ;
434 sta ACIA0+A_CTRL ; ;
435 lda #%00001011 ; enable DTR ;
436 sta ACIA0+A_CMD ; ;
437 lda ACIA0+A_SR ; read status register ;
438 ; ;
439 lda #%10000000 ; disable all ints (unnecessary);
440 sta CIA+C_INTCTRL ; ;
441 lda #255 ; program the timer ;
442 sta CIA+C_TAL ; ;
443 sta CIA+C_TAH ; ;
444 ; ;
445 ldx #0 ; ;
446 stx ACIA0+A_DATA ; transmit a zero ;
447 nop ; ;
448 nop ; ;
449 lda ACIA0+A_SR ; read status ;
450 nop ; ;
451 nop ; ;
452 stx ACIA0+A_DATA ; transmit a zero ;
453Speedy1 lda ACIA0+A_SR ; read status ;
454 and #[1<<4] ; transmit data reg empty? ;
455 beq Speedy1 ; not yet, wait more ;
456 ; ;
457 lda #%00010001 ; load & start the timer ;
458 stx ACIA0+A_DATA ; transmit one more zero ;
459 sta CIA+C_CTRLA ; ;
460Speedy2 lda ACIA0+A_SR ; read status ;
461 and #[1<<4] ; transmit data reg empty? ;
462 beq Speedy2 ; not yet, wait more ;
463 stx CIA+C_CTRLA ; stop the timer ;
464 ; ;
465 lda CIA+C_TAL ; copy timer value for 68k ;
466 sta TimerL ; ;
467 lda CIA+C_TAH ; ;
468 sta TimerH ; ;
469 cmp #$d0 ; turbo or normal? ;
470 .if cs ; ;
471 lda #2 ; turbo! :-) ;
472 .else ; ;
473 lda #1 ; normal :-( ;
474 .end ; ;
475 sta Crystal ; ;
476 lda #0 ; ;
477 sta ACIA0+A_SR ; ;
478 sta ACIA0+A_CTRL ; reset UART ;
479 sta ACIA0+A_CMD ; ;
480 ;
481 jmp LOOP ;
482 ;
483; ;
484;-----------------------------------------------------------------------;
485; ;
486; The Real Thing: ;
487; ;
488LOOP DO_SLOW ; do non-critical things ;
489 jsr do_input ; check for received data
490 DO_PORT 0
491 jsr do_input
492 DO_PORT 1
493 jsr do_input
494 DO_PORT 2
495 jsr do_input
496 DO_PORT 3
497 jsr do_input
498 DO_PORT 4
499 jsr do_input
500 DO_PORT 5
501 jsr do_input
502 DO_PORT 6
503 jsr do_input
504 jmp LOOP
505
506
507do_input DO_DATA 0
508 DO_DATA 1
509 DO_DATA 2
510 DO_DATA 3
511 DO_DATA 4
512 DO_DATA 5
513 DO_DATA 6
514 rts
515
516
517;-----------------------------------------------------------------------;
518 section vectors,data,$3ffa
519 dw $d0d0
520 dw R_RESET
521 dw $c0ce
522;-----------------------------------------------------------------------;
523
524
525
526 end
527
528
529
diff --git a/drivers/char/ser_a2232fw.h b/drivers/char/ser_a2232fw.h
deleted file mode 100644
index e09a30acfe5c..000000000000
--- a/drivers/char/ser_a2232fw.h
+++ /dev/null
@@ -1,306 +0,0 @@
1/* drivers/char/ser_a2232fw.h */
2
3/* $Id: ser_a2232fw.h,v 0.4 2000/01/25 12:00:00 ehaase Exp $ */
4
5/*
6 * Copyright (c) 1995 Jukka Marin <jmarin@jmp.fi>.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, and the entire permission notice in its entirety,
14 * including the disclaimer of warranties.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. The name of the author may not be used to endorse or promote
19 * products derived from this software without specific prior
20 * written permission.
21 *
22 * ALTERNATIVELY, this product may be distributed under the terms of
23 * the GNU Public License, in which case the provisions of the GPL are
24 * required INSTEAD OF the above restrictions. (This clause is
25 * necessary due to a potential bad interaction between the GPL and
26 * the restrictions contained in a BSD-style copyright.)
27 *
28 * THIS SOFTWARE IS PROVIDED `AS IS'' AND ANY EXPRESS OR IMPLIED
29 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
30 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
31 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
32 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
33 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
34 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
36 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
38 * OF THE POSSIBILITY OF SUCH DAMAGE.
39 *
40 */
41
42/* This is the 65EC02 code by Jukka Marin that is executed by
43 the A2232's 65EC02 processor (base address: 0x3800)
44 Source file: ser_a2232fw.ax
45 Version: 1.3 (951029)
46 Known Bugs: Cannot send a break yet
47*/
48static unsigned char a2232_65EC02code[] = {
49 0x38, 0x00, 0xA2, 0xFF, 0x9A, 0xD8, 0xA2, 0x00,
50 0xA9, 0x00, 0xA0, 0x54, 0x95, 0x00, 0xE8, 0x88,
51 0xD0, 0xFA, 0x64, 0x5C, 0x64, 0x5E, 0x64, 0x58,
52 0x64, 0x59, 0xA9, 0x00, 0x85, 0x55, 0xA9, 0xAA,
53 0xC9, 0x64, 0x90, 0x02, 0xE6, 0x55, 0xA5, 0x54,
54 0xF0, 0x03, 0x4C, 0x92, 0x38, 0xA9, 0x98, 0x8D,
55 0x06, 0x44, 0xA9, 0x0B, 0x8D, 0x04, 0x44, 0xAD,
56 0x02, 0x44, 0xA9, 0x80, 0x8D, 0x1A, 0x7C, 0xA9,
57 0xFF, 0x8D, 0x08, 0x7C, 0x8D, 0x0A, 0x7C, 0xA2,
58 0x00, 0x8E, 0x00, 0x44, 0xEA, 0xEA, 0xAD, 0x02,
59 0x44, 0xEA, 0xEA, 0x8E, 0x00, 0x44, 0xAD, 0x02,
60 0x44, 0x29, 0x10, 0xF0, 0xF9, 0xA9, 0x11, 0x8E,
61 0x00, 0x44, 0x8D, 0x1C, 0x7C, 0xAD, 0x02, 0x44,
62 0x29, 0x10, 0xF0, 0xF9, 0x8E, 0x1C, 0x7C, 0xAD,
63 0x08, 0x7C, 0x85, 0x57, 0xAD, 0x0A, 0x7C, 0x85,
64 0x56, 0xC9, 0xD0, 0x90, 0x05, 0xA9, 0x02, 0x4C,
65 0x82, 0x38, 0xA9, 0x01, 0x85, 0x54, 0xA9, 0x00,
66 0x8D, 0x02, 0x44, 0x8D, 0x06, 0x44, 0x8D, 0x04,
67 0x44, 0x4C, 0x92, 0x38, 0xAD, 0x00, 0x7C, 0xC5,
68 0x5C, 0xF0, 0x1F, 0xC5, 0x5E, 0xF0, 0x09, 0x85,
69 0x5E, 0xA9, 0x40, 0x85, 0x5D, 0x4C, 0xB8, 0x38,
70 0xC6, 0x5D, 0x10, 0x0E, 0xA6, 0x58, 0x9D, 0x00,
71 0x17, 0xE8, 0xE4, 0x59, 0xF0, 0x04, 0x86, 0x58,
72 0x85, 0x5C, 0x20, 0x23, 0x3A, 0xA5, 0x07, 0xF0,
73 0x0F, 0xA5, 0x0A, 0x85, 0x0B, 0xA5, 0x08, 0x09,
74 0x10, 0x8D, 0x06, 0x44, 0x64, 0x02, 0x64, 0x07,
75 0xA5, 0x00, 0xE5, 0x01, 0xC9, 0xC8, 0xA5, 0x09,
76 0x29, 0xF3, 0xB0, 0x02, 0x09, 0x08, 0x8D, 0x04,
77 0x44, 0xA5, 0x06, 0xF0, 0x08, 0xA5, 0x03, 0x85,
78 0x04, 0x64, 0x02, 0x64, 0x06, 0x20, 0x23, 0x3A,
79 0xA5, 0x13, 0xF0, 0x0F, 0xA5, 0x16, 0x85, 0x17,
80 0xA5, 0x14, 0x09, 0x10, 0x8D, 0x06, 0x4C, 0x64,
81 0x0E, 0x64, 0x13, 0xA5, 0x0C, 0xE5, 0x0D, 0xC9,
82 0xC8, 0xA5, 0x15, 0x29, 0xF3, 0xB0, 0x02, 0x09,
83 0x08, 0x8D, 0x04, 0x4C, 0xA5, 0x12, 0xF0, 0x08,
84 0xA5, 0x0F, 0x85, 0x10, 0x64, 0x0E, 0x64, 0x12,
85 0x20, 0x23, 0x3A, 0xA5, 0x1F, 0xF0, 0x0F, 0xA5,
86 0x22, 0x85, 0x23, 0xA5, 0x20, 0x09, 0x10, 0x8D,
87 0x06, 0x54, 0x64, 0x1A, 0x64, 0x1F, 0xA5, 0x18,
88 0xE5, 0x19, 0xC9, 0xC8, 0xA5, 0x21, 0x29, 0xF3,
89 0xB0, 0x02, 0x09, 0x08, 0x8D, 0x04, 0x54, 0xA5,
90 0x1E, 0xF0, 0x08, 0xA5, 0x1B, 0x85, 0x1C, 0x64,
91 0x1A, 0x64, 0x1E, 0x20, 0x23, 0x3A, 0xA5, 0x2B,
92 0xF0, 0x0F, 0xA5, 0x2E, 0x85, 0x2F, 0xA5, 0x2C,
93 0x09, 0x10, 0x8D, 0x06, 0x5C, 0x64, 0x26, 0x64,
94 0x2B, 0xA5, 0x24, 0xE5, 0x25, 0xC9, 0xC8, 0xA5,
95 0x2D, 0x29, 0xF3, 0xB0, 0x02, 0x09, 0x08, 0x8D,
96 0x04, 0x5C, 0xA5, 0x2A, 0xF0, 0x08, 0xA5, 0x27,
97 0x85, 0x28, 0x64, 0x26, 0x64, 0x2A, 0x20, 0x23,
98 0x3A, 0xA5, 0x37, 0xF0, 0x0F, 0xA5, 0x3A, 0x85,
99 0x3B, 0xA5, 0x38, 0x09, 0x10, 0x8D, 0x06, 0x64,
100 0x64, 0x32, 0x64, 0x37, 0xA5, 0x30, 0xE5, 0x31,
101 0xC9, 0xC8, 0xA5, 0x39, 0x29, 0xF3, 0xB0, 0x02,
102 0x09, 0x08, 0x8D, 0x04, 0x64, 0xA5, 0x36, 0xF0,
103 0x08, 0xA5, 0x33, 0x85, 0x34, 0x64, 0x32, 0x64,
104 0x36, 0x20, 0x23, 0x3A, 0xA5, 0x43, 0xF0, 0x0F,
105 0xA5, 0x46, 0x85, 0x47, 0xA5, 0x44, 0x09, 0x10,
106 0x8D, 0x06, 0x6C, 0x64, 0x3E, 0x64, 0x43, 0xA5,
107 0x3C, 0xE5, 0x3D, 0xC9, 0xC8, 0xA5, 0x45, 0x29,
108 0xF3, 0xB0, 0x02, 0x09, 0x08, 0x8D, 0x04, 0x6C,
109 0xA5, 0x42, 0xF0, 0x08, 0xA5, 0x3F, 0x85, 0x40,
110 0x64, 0x3E, 0x64, 0x42, 0x20, 0x23, 0x3A, 0xA5,
111 0x4F, 0xF0, 0x0F, 0xA5, 0x52, 0x85, 0x53, 0xA5,
112 0x50, 0x09, 0x10, 0x8D, 0x06, 0x74, 0x64, 0x4A,
113 0x64, 0x4F, 0xA5, 0x48, 0xE5, 0x49, 0xC9, 0xC8,
114 0xA5, 0x51, 0x29, 0xF3, 0xB0, 0x02, 0x09, 0x08,
115 0x8D, 0x04, 0x74, 0xA5, 0x4E, 0xF0, 0x08, 0xA5,
116 0x4B, 0x85, 0x4C, 0x64, 0x4A, 0x64, 0x4E, 0x20,
117 0x23, 0x3A, 0x4C, 0x92, 0x38, 0xAD, 0x02, 0x44,
118 0x89, 0x08, 0xF0, 0x3B, 0x89, 0x02, 0xF0, 0x1B,
119 0xAD, 0x00, 0x44, 0xD0, 0x32, 0xA6, 0x00, 0xA9,
120 0x01, 0x9D, 0x00, 0x10, 0xA9, 0x01, 0x9D, 0x00,
121 0x09, 0xE8, 0xE4, 0x01, 0xF0, 0x02, 0x86, 0x00,
122 0x4C, 0x65, 0x3A, 0xA6, 0x00, 0xAD, 0x00, 0x44,
123 0x9D, 0x00, 0x09, 0x9E, 0x00, 0x10, 0xE8, 0xE4,
124 0x01, 0xF0, 0x02, 0x86, 0x00, 0x29, 0x7F, 0xC9,
125 0x13, 0xD0, 0x04, 0xA5, 0x0B, 0x85, 0x02, 0xAD,
126 0x02, 0x44, 0x29, 0x10, 0xF0, 0x2C, 0xA6, 0x05,
127 0xF0, 0x0F, 0xAD, 0x02, 0x7C, 0x29, 0x01, 0xD0,
128 0x21, 0x8E, 0x00, 0x44, 0x64, 0x05, 0x4C, 0x98,
129 0x3A, 0xA6, 0x04, 0xE4, 0x03, 0xF0, 0x13, 0xA5,
130 0x02, 0xD0, 0x0F, 0xAD, 0x02, 0x7C, 0x29, 0x01,
131 0xD0, 0x08, 0xBD, 0x00, 0x02, 0x8D, 0x00, 0x44,
132 0xE6, 0x04, 0xAD, 0x02, 0x4C, 0x89, 0x08, 0xF0,
133 0x3B, 0x89, 0x02, 0xF0, 0x1B, 0xAD, 0x00, 0x4C,
134 0xD0, 0x32, 0xA6, 0x0C, 0xA9, 0x01, 0x9D, 0x00,
135 0x11, 0xA9, 0x01, 0x9D, 0x00, 0x0A, 0xE8, 0xE4,
136 0x0D, 0xF0, 0x02, 0x86, 0x0C, 0x4C, 0xDA, 0x3A,
137 0xA6, 0x0C, 0xAD, 0x00, 0x4C, 0x9D, 0x00, 0x0A,
138 0x9E, 0x00, 0x11, 0xE8, 0xE4, 0x0D, 0xF0, 0x02,
139 0x86, 0x0C, 0x29, 0x7F, 0xC9, 0x13, 0xD0, 0x04,
140 0xA5, 0x17, 0x85, 0x0E, 0xAD, 0x02, 0x4C, 0x29,
141 0x10, 0xF0, 0x2C, 0xA6, 0x11, 0xF0, 0x0F, 0xAD,
142 0x02, 0x7C, 0x29, 0x02, 0xD0, 0x21, 0x8E, 0x00,
143 0x4C, 0x64, 0x11, 0x4C, 0x0D, 0x3B, 0xA6, 0x10,
144 0xE4, 0x0F, 0xF0, 0x13, 0xA5, 0x0E, 0xD0, 0x0F,
145 0xAD, 0x02, 0x7C, 0x29, 0x02, 0xD0, 0x08, 0xBD,
146 0x00, 0x03, 0x8D, 0x00, 0x4C, 0xE6, 0x10, 0xAD,
147 0x02, 0x54, 0x89, 0x08, 0xF0, 0x3B, 0x89, 0x02,
148 0xF0, 0x1B, 0xAD, 0x00, 0x54, 0xD0, 0x32, 0xA6,
149 0x18, 0xA9, 0x01, 0x9D, 0x00, 0x12, 0xA9, 0x01,
150 0x9D, 0x00, 0x0B, 0xE8, 0xE4, 0x19, 0xF0, 0x02,
151 0x86, 0x18, 0x4C, 0x4F, 0x3B, 0xA6, 0x18, 0xAD,
152 0x00, 0x54, 0x9D, 0x00, 0x0B, 0x9E, 0x00, 0x12,
153 0xE8, 0xE4, 0x19, 0xF0, 0x02, 0x86, 0x18, 0x29,
154 0x7F, 0xC9, 0x13, 0xD0, 0x04, 0xA5, 0x23, 0x85,
155 0x1A, 0xAD, 0x02, 0x54, 0x29, 0x10, 0xF0, 0x2C,
156 0xA6, 0x1D, 0xF0, 0x0F, 0xAD, 0x02, 0x7C, 0x29,
157 0x04, 0xD0, 0x21, 0x8E, 0x00, 0x54, 0x64, 0x1D,
158 0x4C, 0x82, 0x3B, 0xA6, 0x1C, 0xE4, 0x1B, 0xF0,
159 0x13, 0xA5, 0x1A, 0xD0, 0x0F, 0xAD, 0x02, 0x7C,
160 0x29, 0x04, 0xD0, 0x08, 0xBD, 0x00, 0x04, 0x8D,
161 0x00, 0x54, 0xE6, 0x1C, 0xAD, 0x02, 0x5C, 0x89,
162 0x08, 0xF0, 0x3B, 0x89, 0x02, 0xF0, 0x1B, 0xAD,
163 0x00, 0x5C, 0xD0, 0x32, 0xA6, 0x24, 0xA9, 0x01,
164 0x9D, 0x00, 0x13, 0xA9, 0x01, 0x9D, 0x00, 0x0C,
165 0xE8, 0xE4, 0x25, 0xF0, 0x02, 0x86, 0x24, 0x4C,
166 0xC4, 0x3B, 0xA6, 0x24, 0xAD, 0x00, 0x5C, 0x9D,
167 0x00, 0x0C, 0x9E, 0x00, 0x13, 0xE8, 0xE4, 0x25,
168 0xF0, 0x02, 0x86, 0x24, 0x29, 0x7F, 0xC9, 0x13,
169 0xD0, 0x04, 0xA5, 0x2F, 0x85, 0x26, 0xAD, 0x02,
170 0x5C, 0x29, 0x10, 0xF0, 0x2C, 0xA6, 0x29, 0xF0,
171 0x0F, 0xAD, 0x02, 0x7C, 0x29, 0x08, 0xD0, 0x21,
172 0x8E, 0x00, 0x5C, 0x64, 0x29, 0x4C, 0xF7, 0x3B,
173 0xA6, 0x28, 0xE4, 0x27, 0xF0, 0x13, 0xA5, 0x26,
174 0xD0, 0x0F, 0xAD, 0x02, 0x7C, 0x29, 0x08, 0xD0,
175 0x08, 0xBD, 0x00, 0x05, 0x8D, 0x00, 0x5C, 0xE6,
176 0x28, 0xAD, 0x02, 0x64, 0x89, 0x08, 0xF0, 0x3B,
177 0x89, 0x02, 0xF0, 0x1B, 0xAD, 0x00, 0x64, 0xD0,
178 0x32, 0xA6, 0x30, 0xA9, 0x01, 0x9D, 0x00, 0x14,
179 0xA9, 0x01, 0x9D, 0x00, 0x0D, 0xE8, 0xE4, 0x31,
180 0xF0, 0x02, 0x86, 0x30, 0x4C, 0x39, 0x3C, 0xA6,
181 0x30, 0xAD, 0x00, 0x64, 0x9D, 0x00, 0x0D, 0x9E,
182 0x00, 0x14, 0xE8, 0xE4, 0x31, 0xF0, 0x02, 0x86,
183 0x30, 0x29, 0x7F, 0xC9, 0x13, 0xD0, 0x04, 0xA5,
184 0x3B, 0x85, 0x32, 0xAD, 0x02, 0x64, 0x29, 0x10,
185 0xF0, 0x2C, 0xA6, 0x35, 0xF0, 0x0F, 0xAD, 0x02,
186 0x7C, 0x29, 0x10, 0xD0, 0x21, 0x8E, 0x00, 0x64,
187 0x64, 0x35, 0x4C, 0x6C, 0x3C, 0xA6, 0x34, 0xE4,
188 0x33, 0xF0, 0x13, 0xA5, 0x32, 0xD0, 0x0F, 0xAD,
189 0x02, 0x7C, 0x29, 0x10, 0xD0, 0x08, 0xBD, 0x00,
190 0x06, 0x8D, 0x00, 0x64, 0xE6, 0x34, 0xAD, 0x02,
191 0x6C, 0x89, 0x08, 0xF0, 0x3B, 0x89, 0x02, 0xF0,
192 0x1B, 0xAD, 0x00, 0x6C, 0xD0, 0x32, 0xA6, 0x3C,
193 0xA9, 0x01, 0x9D, 0x00, 0x15, 0xA9, 0x01, 0x9D,
194 0x00, 0x0E, 0xE8, 0xE4, 0x3D, 0xF0, 0x02, 0x86,
195 0x3C, 0x4C, 0xAE, 0x3C, 0xA6, 0x3C, 0xAD, 0x00,
196 0x6C, 0x9D, 0x00, 0x0E, 0x9E, 0x00, 0x15, 0xE8,
197 0xE4, 0x3D, 0xF0, 0x02, 0x86, 0x3C, 0x29, 0x7F,
198 0xC9, 0x13, 0xD0, 0x04, 0xA5, 0x47, 0x85, 0x3E,
199 0xAD, 0x02, 0x6C, 0x29, 0x10, 0xF0, 0x2C, 0xA6,
200 0x41, 0xF0, 0x0F, 0xAD, 0x02, 0x7C, 0x29, 0x20,
201 0xD0, 0x21, 0x8E, 0x00, 0x6C, 0x64, 0x41, 0x4C,
202 0xE1, 0x3C, 0xA6, 0x40, 0xE4, 0x3F, 0xF0, 0x13,
203 0xA5, 0x3E, 0xD0, 0x0F, 0xAD, 0x02, 0x7C, 0x29,
204 0x20, 0xD0, 0x08, 0xBD, 0x00, 0x07, 0x8D, 0x00,
205 0x6C, 0xE6, 0x40, 0xAD, 0x02, 0x74, 0x89, 0x08,
206 0xF0, 0x3B, 0x89, 0x02, 0xF0, 0x1B, 0xAD, 0x00,
207 0x74, 0xD0, 0x32, 0xA6, 0x48, 0xA9, 0x01, 0x9D,
208 0x00, 0x16, 0xA9, 0x01, 0x9D, 0x00, 0x0F, 0xE8,
209 0xE4, 0x49, 0xF0, 0x02, 0x86, 0x48, 0x4C, 0x23,
210 0x3D, 0xA6, 0x48, 0xAD, 0x00, 0x74, 0x9D, 0x00,
211 0x0F, 0x9E, 0x00, 0x16, 0xE8, 0xE4, 0x49, 0xF0,
212 0x02, 0x86, 0x48, 0x29, 0x7F, 0xC9, 0x13, 0xD0,
213 0x04, 0xA5, 0x53, 0x85, 0x4A, 0xAD, 0x02, 0x74,
214 0x29, 0x10, 0xF0, 0x2C, 0xA6, 0x4D, 0xF0, 0x0F,
215 0xAD, 0x02, 0x7C, 0x29, 0x40, 0xD0, 0x21, 0x8E,
216 0x00, 0x74, 0x64, 0x4D, 0x4C, 0x56, 0x3D, 0xA6,
217 0x4C, 0xE4, 0x4B, 0xF0, 0x13, 0xA5, 0x4A, 0xD0,
218 0x0F, 0xAD, 0x02, 0x7C, 0x29, 0x40, 0xD0, 0x08,
219 0xBD, 0x00, 0x08, 0x8D, 0x00, 0x74, 0xE6, 0x4C,
220 0x60, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
221 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
222 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
223 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
224 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
225 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
226 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
227 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
228 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
229 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
230 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
231 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
232 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
233 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
234 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
235 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
236 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
237 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
238 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
239 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
240 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
241 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
242 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
243 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
244 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
245 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
246 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
247 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
248 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
249 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
250 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
251 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
252 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
253 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
254 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
255 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
256 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
257 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
258 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
259 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
260 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
261 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
262 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
263 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
264 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
265 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
266 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
267 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
268 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
269 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
270 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
271 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
272 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
273 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
274 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
275 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
276 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
277 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
278 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
279 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
280 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
281 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
282 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
283 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
284 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
285 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
286 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
287 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
288 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
289 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
290 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
291 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
292 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
293 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
294 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
295 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
296 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
297 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
298 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
299 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
300 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
301 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
302 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
303 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
304 0xFF, 0xFF, 0xFF, 0xFF, 0xD0, 0xD0, 0x00, 0x38,
305 0xCE, 0xC0,
306};
diff --git a/drivers/char/serial167.c b/drivers/char/serial167.c
deleted file mode 100644
index f646725bd567..000000000000
--- a/drivers/char/serial167.c
+++ /dev/null
@@ -1,2491 +0,0 @@
1/*
2 * linux/drivers/char/serial167.c
3 *
4 * Driver for MVME166/7 board serial ports, which are via a CD2401.
5 * Based very much on cyclades.c.
6 *
7 * MVME166/7 work by Richard Hirst [richard@sleepie.demon.co.uk]
8 *
9 * ==============================================================
10 *
11 * static char rcsid[] =
12 * "$Revision: 1.36.1.4 $$Date: 1995/03/29 06:14:14 $";
13 *
14 * linux/kernel/cyclades.c
15 *
16 * Maintained by Marcio Saito (cyclades@netcom.com) and
17 * Randolph Bentson (bentson@grieg.seaslug.org)
18 *
19 * Much of the design and some of the code came from serial.c
20 * which was copyright (C) 1991, 1992 Linus Torvalds. It was
21 * extensively rewritten by Theodore Ts'o, 8/16/92 -- 9/14/92,
22 * and then fixed as suggested by Michael K. Johnson 12/12/92.
23 *
24 * This version does not support shared irq's.
25 *
26 * $Log: cyclades.c,v $
27 * Revision 1.36.1.4 1995/03/29 06:14:14 bentson
28 * disambiguate between Cyclom-16Y and Cyclom-32Ye;
29 *
30 * Changes:
31 *
32 * 200 lines of changes record removed - RGH 11-10-95, starting work on
33 * converting this to drive serial ports on mvme166 (cd2401).
34 *
35 * Arnaldo Carvalho de Melo <acme@conectiva.com.br> - 2000/08/25
36 * - get rid of verify_area
37 * - use get_user to access memory from userspace in set_threshold,
38 * set_default_threshold and set_timeout
39 * - don't use the panic function in serial167_init
40 * - do resource release on failure on serial167_init
41 * - include missing restore_flags in mvme167_serial_console_setup
42 *
43 * Kars de Jong <jongk@linux-m68k.org> - 2004/09/06
44 * - replace bottom half handler with task queue handler
45 */
46
47#include <linux/errno.h>
48#include <linux/signal.h>
49#include <linux/sched.h>
50#include <linux/timer.h>
51#include <linux/tty.h>
52#include <linux/interrupt.h>
53#include <linux/serial.h>
54#include <linux/serialP.h>
55#include <linux/smp_lock.h>
56#include <linux/string.h>
57#include <linux/fcntl.h>
58#include <linux/ptrace.h>
59#include <linux/serial167.h>
60#include <linux/delay.h>
61#include <linux/major.h>
62#include <linux/mm.h>
63#include <linux/console.h>
64#include <linux/module.h>
65#include <linux/bitops.h>
66#include <linux/tty_flip.h>
67#include <linux/gfp.h>
68
69#include <asm/system.h>
70#include <asm/io.h>
71#include <asm/mvme16xhw.h>
72#include <asm/bootinfo.h>
73#include <asm/setup.h>
74
75#include <linux/types.h>
76#include <linux/kernel.h>
77
78#include <asm/uaccess.h>
79#include <linux/init.h>
80
81#define SERIAL_PARANOIA_CHECK
82#undef SERIAL_DEBUG_OPEN
83#undef SERIAL_DEBUG_THROTTLE
84#undef SERIAL_DEBUG_OTHER
85#undef SERIAL_DEBUG_IO
86#undef SERIAL_DEBUG_COUNT
87#undef SERIAL_DEBUG_DTR
88#undef CYCLOM_16Y_HACK
89#define CYCLOM_ENABLE_MONITORING
90
91#define WAKEUP_CHARS 256
92
93#define STD_COM_FLAGS (0)
94
95static struct tty_driver *cy_serial_driver;
96extern int serial_console;
97static struct cyclades_port *serial_console_info = NULL;
98static unsigned int serial_console_cflag = 0;
99u_char initial_console_speed;
100
101/* Base address of cd2401 chip on mvme166/7 */
102
103#define BASE_ADDR (0xfff45000)
104#define pcc2chip ((volatile u_char *)0xfff42000)
105#define PccSCCMICR 0x1d
106#define PccSCCTICR 0x1e
107#define PccSCCRICR 0x1f
108#define PccTPIACKR 0x25
109#define PccRPIACKR 0x27
110#define PccIMLR 0x3f
111
112/* This is the per-port data structure */
113struct cyclades_port cy_port[] = {
114 /* CARD# */
115 {-1}, /* ttyS0 */
116 {-1}, /* ttyS1 */
117 {-1}, /* ttyS2 */
118 {-1}, /* ttyS3 */
119};
120
121#define NR_PORTS ARRAY_SIZE(cy_port)
122
123/*
124 * This is used to look up the divisor speeds and the timeouts
125 * We're normally limited to 15 distinct baud rates. The extra
126 * are accessed via settings in info->flags.
127 * 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
128 * 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
129 * HI VHI
130 */
131static int baud_table[] = {
132 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200,
133 1800, 2400, 4800, 9600, 19200, 38400, 57600, 76800, 115200, 150000,
134 0
135};
136
137#if 0
138static char baud_co[] = { /* 25 MHz clock option table */
139 /* value => 00 01 02 03 04 */
140 /* divide by 8 32 128 512 2048 */
141 0x00, 0x04, 0x04, 0x04, 0x04, 0x04, 0x03, 0x03, 0x03, 0x02,
142 0x02, 0x02, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
143};
144
145static char baud_bpr[] = { /* 25 MHz baud rate period table */
146 0x00, 0xf5, 0xa3, 0x6f, 0x5c, 0x51, 0xf5, 0xa3, 0x51, 0xa3,
147 0x6d, 0x51, 0xa3, 0x51, 0xa3, 0x51, 0x36, 0x29, 0x1b, 0x15
148};
149#endif
150
151/* I think 166 brd clocks 2401 at 20MHz.... */
152
153/* These values are written directly to tcor, and >> 5 for writing to rcor */
154static u_char baud_co[] = { /* 20 MHz clock option table */
155 0x00, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x60, 0x60, 0x40,
156 0x40, 0x40, 0x20, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
157};
158
159/* These values written directly to tbpr/rbpr */
160static u_char baud_bpr[] = { /* 20 MHz baud rate period table */
161 0x00, 0xc0, 0x80, 0x58, 0x6c, 0x40, 0xc0, 0x81, 0x40, 0x81,
162 0x57, 0x40, 0x81, 0x40, 0x81, 0x40, 0x2b, 0x20, 0x15, 0x10
163};
164
165static u_char baud_cor4[] = { /* receive threshold */
166 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a,
167 0x0a, 0x0a, 0x0a, 0x09, 0x09, 0x08, 0x08, 0x08, 0x08, 0x07
168};
169
170static void shutdown(struct cyclades_port *);
171static int startup(struct cyclades_port *);
172static void cy_throttle(struct tty_struct *);
173static void cy_unthrottle(struct tty_struct *);
174static void config_setup(struct cyclades_port *);
175#ifdef CYCLOM_SHOW_STATUS
176static void show_status(int);
177#endif
178
179/*
180 * I have my own version of udelay(), as it is needed when initialising
181 * the chip, before the delay loop has been calibrated. Should probably
182 * reference one of the vmechip2 or pccchip2 counter for an accurate
183 * delay, but this wild guess will do for now.
184 */
185
186void my_udelay(long us)
187{
188 u_char x;
189 volatile u_char *p = &x;
190 int i;
191
192 while (us--)
193 for (i = 100; i; i--)
194 x |= *p;
195}
196
197static inline int serial_paranoia_check(struct cyclades_port *info, char *name,
198 const char *routine)
199{
200#ifdef SERIAL_PARANOIA_CHECK
201 if (!info) {
202 printk("Warning: null cyclades_port for (%s) in %s\n", name,
203 routine);
204 return 1;
205 }
206
207 if (info < &cy_port[0] || info >= &cy_port[NR_PORTS]) {
208 printk("Warning: cyclades_port out of range for (%s) in %s\n",
209 name, routine);
210 return 1;
211 }
212
213 if (info->magic != CYCLADES_MAGIC) {
214 printk("Warning: bad magic number for serial struct (%s) in "
215 "%s\n", name, routine);
216 return 1;
217 }
218#endif
219 return 0;
220} /* serial_paranoia_check */
221
222#if 0
223/* The following diagnostic routines allow the driver to spew
224 information on the screen, even (especially!) during interrupts.
225 */
226void SP(char *data)
227{
228 unsigned long flags;
229 local_irq_save(flags);
230 printk(KERN_EMERG "%s", data);
231 local_irq_restore(flags);
232}
233
234char scrn[2];
235void CP(char data)
236{
237 unsigned long flags;
238 local_irq_save(flags);
239 scrn[0] = data;
240 printk(KERN_EMERG "%c", scrn);
241 local_irq_restore(flags);
242} /* CP */
243
244void CP1(int data)
245{
246 (data < 10) ? CP(data + '0') : CP(data + 'A' - 10);
247} /* CP1 */
248void CP2(int data)
249{
250 CP1((data >> 4) & 0x0f);
251 CP1(data & 0x0f);
252} /* CP2 */
253void CP4(int data)
254{
255 CP2((data >> 8) & 0xff);
256 CP2(data & 0xff);
257} /* CP4 */
258void CP8(long data)
259{
260 CP4((data >> 16) & 0xffff);
261 CP4(data & 0xffff);
262} /* CP8 */
263#endif
264
265/* This routine waits up to 1000 micro-seconds for the previous
266 command to the Cirrus chip to complete and then issues the
267 new command. An error is returned if the previous command
268 didn't finish within the time limit.
269 */
270u_short write_cy_cmd(volatile u_char * base_addr, u_char cmd)
271{
272 unsigned long flags;
273 volatile int i;
274
275 local_irq_save(flags);
276 /* Check to see that the previous command has completed */
277 for (i = 0; i < 100; i++) {
278 if (base_addr[CyCCR] == 0) {
279 break;
280 }
281 my_udelay(10L);
282 }
283 /* if the CCR never cleared, the previous command
284 didn't finish within the "reasonable time" */
285 if (i == 10) {
286 local_irq_restore(flags);
287 return (-1);
288 }
289
290 /* Issue the new command */
291 base_addr[CyCCR] = cmd;
292 local_irq_restore(flags);
293 return (0);
294} /* write_cy_cmd */
295
296/* cy_start and cy_stop provide software output flow control as a
297 function of XON/XOFF, software CTS, and other such stuff. */
298
299static void cy_stop(struct tty_struct *tty)
300{
301 struct cyclades_port *info = tty->driver_data;
302 volatile unsigned char *base_addr = (unsigned char *)BASE_ADDR;
303 int channel;
304 unsigned long flags;
305
306#ifdef SERIAL_DEBUG_OTHER
307 printk("cy_stop %s\n", tty->name); /* */
308#endif
309
310 if (serial_paranoia_check(info, tty->name, "cy_stop"))
311 return;
312
313 channel = info->line;
314
315 local_irq_save(flags);
316 base_addr[CyCAR] = (u_char) (channel); /* index channel */
317 base_addr[CyIER] &= ~(CyTxMpty | CyTxRdy);
318 local_irq_restore(flags);
319} /* cy_stop */
320
321static void cy_start(struct tty_struct *tty)
322{
323 struct cyclades_port *info = tty->driver_data;
324 volatile unsigned char *base_addr = (unsigned char *)BASE_ADDR;
325 int channel;
326 unsigned long flags;
327
328#ifdef SERIAL_DEBUG_OTHER
329 printk("cy_start %s\n", tty->name); /* */
330#endif
331
332 if (serial_paranoia_check(info, tty->name, "cy_start"))
333 return;
334
335 channel = info->line;
336
337 local_irq_save(flags);
338 base_addr[CyCAR] = (u_char) (channel);
339 base_addr[CyIER] |= CyTxMpty;
340 local_irq_restore(flags);
341} /* cy_start */
342
343/* The real interrupt service routines are called
344 whenever the card wants its hand held--chars
345 received, out buffer empty, modem change, etc.
346 */
347static irqreturn_t cd2401_rxerr_interrupt(int irq, void *dev_id)
348{
349 struct tty_struct *tty;
350 struct cyclades_port *info;
351 volatile unsigned char *base_addr = (unsigned char *)BASE_ADDR;
352 unsigned char err, rfoc;
353 int channel;
354 char data;
355
356 /* determine the channel and change to that context */
357 channel = (u_short) (base_addr[CyLICR] >> 2);
358 info = &cy_port[channel];
359 info->last_active = jiffies;
360
361 if ((err = base_addr[CyRISR]) & CyTIMEOUT) {
362 /* This is a receive timeout interrupt, ignore it */
363 base_addr[CyREOIR] = CyNOTRANS;
364 return IRQ_HANDLED;
365 }
366
367 /* Read a byte of data if there is any - assume the error
368 * is associated with this character */
369
370 if ((rfoc = base_addr[CyRFOC]) != 0)
371 data = base_addr[CyRDR];
372 else
373 data = 0;
374
375 /* if there is nowhere to put the data, discard it */
376 if (info->tty == 0) {
377 base_addr[CyREOIR] = rfoc ? 0 : CyNOTRANS;
378 return IRQ_HANDLED;
379 } else { /* there is an open port for this data */
380 tty = info->tty;
381 if (err & info->ignore_status_mask) {
382 base_addr[CyREOIR] = rfoc ? 0 : CyNOTRANS;
383 return IRQ_HANDLED;
384 }
385 if (tty_buffer_request_room(tty, 1) != 0) {
386 if (err & info->read_status_mask) {
387 if (err & CyBREAK) {
388 tty_insert_flip_char(tty, data,
389 TTY_BREAK);
390 if (info->flags & ASYNC_SAK) {
391 do_SAK(tty);
392 }
393 } else if (err & CyFRAME) {
394 tty_insert_flip_char(tty, data,
395 TTY_FRAME);
396 } else if (err & CyPARITY) {
397 tty_insert_flip_char(tty, data,
398 TTY_PARITY);
399 } else if (err & CyOVERRUN) {
400 tty_insert_flip_char(tty, 0,
401 TTY_OVERRUN);
402 /*
403 If the flip buffer itself is
404 overflowing, we still lose
405 the next incoming character.
406 */
407 if (tty_buffer_request_room(tty, 1) !=
408 0) {
409 tty_insert_flip_char(tty, data,
410 TTY_FRAME);
411 }
412 /* These two conditions may imply */
413 /* a normal read should be done. */
414 /* else if(data & CyTIMEOUT) */
415 /* else if(data & CySPECHAR) */
416 } else {
417 tty_insert_flip_char(tty, 0,
418 TTY_NORMAL);
419 }
420 } else {
421 tty_insert_flip_char(tty, data, TTY_NORMAL);
422 }
423 } else {
424 /* there was a software buffer overrun
425 and nothing could be done about it!!! */
426 }
427 }
428 tty_schedule_flip(tty);
429 /* end of service */
430 base_addr[CyREOIR] = rfoc ? 0 : CyNOTRANS;
431 return IRQ_HANDLED;
432} /* cy_rxerr_interrupt */
433
434static irqreturn_t cd2401_modem_interrupt(int irq, void *dev_id)
435{
436 struct cyclades_port *info;
437 volatile unsigned char *base_addr = (unsigned char *)BASE_ADDR;
438 int channel;
439 int mdm_change;
440 int mdm_status;
441
442 /* determine the channel and change to that context */
443 channel = (u_short) (base_addr[CyLICR] >> 2);
444 info = &cy_port[channel];
445 info->last_active = jiffies;
446
447 mdm_change = base_addr[CyMISR];
448 mdm_status = base_addr[CyMSVR1];
449
450 if (info->tty == 0) { /* nowhere to put the data, ignore it */
451 ;
452 } else {
453 if ((mdm_change & CyDCD)
454 && (info->flags & ASYNC_CHECK_CD)) {
455 if (mdm_status & CyDCD) {
456/* CP('!'); */
457 wake_up_interruptible(&info->open_wait);
458 } else {
459/* CP('@'); */
460 tty_hangup(info->tty);
461 wake_up_interruptible(&info->open_wait);
462 info->flags &= ~ASYNC_NORMAL_ACTIVE;
463 }
464 }
465 if ((mdm_change & CyCTS)
466 && (info->flags & ASYNC_CTS_FLOW)) {
467 if (info->tty->stopped) {
468 if (mdm_status & CyCTS) {
469 /* !!! cy_start isn't used because... */
470 info->tty->stopped = 0;
471 base_addr[CyIER] |= CyTxMpty;
472 tty_wakeup(info->tty);
473 }
474 } else {
475 if (!(mdm_status & CyCTS)) {
476 /* !!! cy_stop isn't used because... */
477 info->tty->stopped = 1;
478 base_addr[CyIER] &=
479 ~(CyTxMpty | CyTxRdy);
480 }
481 }
482 }
483 if (mdm_status & CyDSR) {
484 }
485 }
486 base_addr[CyMEOIR] = 0;
487 return IRQ_HANDLED;
488} /* cy_modem_interrupt */
489
490static irqreturn_t cd2401_tx_interrupt(int irq, void *dev_id)
491{
492 struct cyclades_port *info;
493 volatile unsigned char *base_addr = (unsigned char *)BASE_ADDR;
494 int channel;
495 int char_count, saved_cnt;
496 int outch;
497
498 /* determine the channel and change to that context */
499 channel = (u_short) (base_addr[CyLICR] >> 2);
500
501 /* validate the port number (as configured and open) */
502 if ((channel < 0) || (NR_PORTS <= channel)) {
503 base_addr[CyIER] &= ~(CyTxMpty | CyTxRdy);
504 base_addr[CyTEOIR] = CyNOTRANS;
505 return IRQ_HANDLED;
506 }
507 info = &cy_port[channel];
508 info->last_active = jiffies;
509 if (info->tty == 0) {
510 base_addr[CyIER] &= ~(CyTxMpty | CyTxRdy);
511 base_addr[CyTEOIR] = CyNOTRANS;
512 return IRQ_HANDLED;
513 }
514
515 /* load the on-chip space available for outbound data */
516 saved_cnt = char_count = base_addr[CyTFTC];
517
518 if (info->x_char) { /* send special char */
519 outch = info->x_char;
520 base_addr[CyTDR] = outch;
521 char_count--;
522 info->x_char = 0;
523 }
524
525 if (info->x_break) {
526 /* The Cirrus chip requires the "Embedded Transmit
527 Commands" of start break, delay, and end break
528 sequences to be sent. The duration of the
529 break is given in TICs, which runs at HZ
530 (typically 100) and the PPR runs at 200 Hz,
531 so the delay is duration * 200/HZ, and thus a
532 break can run from 1/100 sec to about 5/4 sec.
533 Need to check these values - RGH 141095.
534 */
535 base_addr[CyTDR] = 0; /* start break */
536 base_addr[CyTDR] = 0x81;
537 base_addr[CyTDR] = 0; /* delay a bit */
538 base_addr[CyTDR] = 0x82;
539 base_addr[CyTDR] = info->x_break * 200 / HZ;
540 base_addr[CyTDR] = 0; /* terminate break */
541 base_addr[CyTDR] = 0x83;
542 char_count -= 7;
543 info->x_break = 0;
544 }
545
546 while (char_count > 0) {
547 if (!info->xmit_cnt) {
548 base_addr[CyIER] &= ~(CyTxMpty | CyTxRdy);
549 break;
550 }
551 if (info->xmit_buf == 0) {
552 base_addr[CyIER] &= ~(CyTxMpty | CyTxRdy);
553 break;
554 }
555 if (info->tty->stopped || info->tty->hw_stopped) {
556 base_addr[CyIER] &= ~(CyTxMpty | CyTxRdy);
557 break;
558 }
559 /* Because the Embedded Transmit Commands have been
560 enabled, we must check to see if the escape
561 character, NULL, is being sent. If it is, we
562 must ensure that there is room for it to be
563 doubled in the output stream. Therefore we
564 no longer advance the pointer when the character
565 is fetched, but rather wait until after the check
566 for a NULL output character. (This is necessary
567 because there may not be room for the two chars
568 needed to send a NULL.
569 */
570 outch = info->xmit_buf[info->xmit_tail];
571 if (outch) {
572 info->xmit_cnt--;
573 info->xmit_tail = (info->xmit_tail + 1)
574 & (PAGE_SIZE - 1);
575 base_addr[CyTDR] = outch;
576 char_count--;
577 } else {
578 if (char_count > 1) {
579 info->xmit_cnt--;
580 info->xmit_tail = (info->xmit_tail + 1)
581 & (PAGE_SIZE - 1);
582 base_addr[CyTDR] = outch;
583 base_addr[CyTDR] = 0;
584 char_count--;
585 char_count--;
586 } else {
587 break;
588 }
589 }
590 }
591
592 if (info->xmit_cnt < WAKEUP_CHARS)
593 tty_wakeup(info->tty);
594
595 base_addr[CyTEOIR] = (char_count != saved_cnt) ? 0 : CyNOTRANS;
596 return IRQ_HANDLED;
597} /* cy_tx_interrupt */
598
599static irqreturn_t cd2401_rx_interrupt(int irq, void *dev_id)
600{
601 struct tty_struct *tty;
602 struct cyclades_port *info;
603 volatile unsigned char *base_addr = (unsigned char *)BASE_ADDR;
604 int channel;
605 char data;
606 int char_count;
607 int save_cnt;
608
609 /* determine the channel and change to that context */
610 channel = (u_short) (base_addr[CyLICR] >> 2);
611 info = &cy_port[channel];
612 info->last_active = jiffies;
613 save_cnt = char_count = base_addr[CyRFOC];
614
615 /* if there is nowhere to put the data, discard it */
616 if (info->tty == 0) {
617 while (char_count--) {
618 data = base_addr[CyRDR];
619 }
620 } else { /* there is an open port for this data */
621 tty = info->tty;
622 /* load # characters available from the chip */
623
624#ifdef CYCLOM_ENABLE_MONITORING
625 ++info->mon.int_count;
626 info->mon.char_count += char_count;
627 if (char_count > info->mon.char_max)
628 info->mon.char_max = char_count;
629 info->mon.char_last = char_count;
630#endif
631 while (char_count--) {
632 data = base_addr[CyRDR];
633 tty_insert_flip_char(tty, data, TTY_NORMAL);
634#ifdef CYCLOM_16Y_HACK
635 udelay(10L);
636#endif
637 }
638 tty_schedule_flip(tty);
639 }
640 /* end of service */
641 base_addr[CyREOIR] = save_cnt ? 0 : CyNOTRANS;
642 return IRQ_HANDLED;
643} /* cy_rx_interrupt */
644
645/* This is called whenever a port becomes active;
646 interrupts are enabled and DTR & RTS are turned on.
647 */
648static int startup(struct cyclades_port *info)
649{
650 unsigned long flags;
651 volatile unsigned char *base_addr = (unsigned char *)BASE_ADDR;
652 int channel;
653
654 if (info->flags & ASYNC_INITIALIZED) {
655 return 0;
656 }
657
658 if (!info->type) {
659 if (info->tty) {
660 set_bit(TTY_IO_ERROR, &info->tty->flags);
661 }
662 return 0;
663 }
664 if (!info->xmit_buf) {
665 info->xmit_buf = (unsigned char *)get_zeroed_page(GFP_KERNEL);
666 if (!info->xmit_buf) {
667 return -ENOMEM;
668 }
669 }
670
671 config_setup(info);
672
673 channel = info->line;
674
675#ifdef SERIAL_DEBUG_OPEN
676 printk("startup channel %d\n", channel);
677#endif
678
679 local_irq_save(flags);
680 base_addr[CyCAR] = (u_char) channel;
681 write_cy_cmd(base_addr, CyENB_RCVR | CyENB_XMTR);
682
683 base_addr[CyCAR] = (u_char) channel; /* !!! Is this needed? */
684 base_addr[CyMSVR1] = CyRTS;
685/* CP('S');CP('1'); */
686 base_addr[CyMSVR2] = CyDTR;
687
688#ifdef SERIAL_DEBUG_DTR
689 printk("cyc: %d: raising DTR\n", __LINE__);
690 printk(" status: 0x%x, 0x%x\n", base_addr[CyMSVR1],
691 base_addr[CyMSVR2]);
692#endif
693
694 base_addr[CyIER] |= CyRxData;
695 info->flags |= ASYNC_INITIALIZED;
696
697 if (info->tty) {
698 clear_bit(TTY_IO_ERROR, &info->tty->flags);
699 }
700 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
701
702 local_irq_restore(flags);
703
704#ifdef SERIAL_DEBUG_OPEN
705 printk(" done\n");
706#endif
707 return 0;
708} /* startup */
709
710void start_xmit(struct cyclades_port *info)
711{
712 unsigned long flags;
713 volatile unsigned char *base_addr = (u_char *) BASE_ADDR;
714 int channel;
715
716 channel = info->line;
717 local_irq_save(flags);
718 base_addr[CyCAR] = channel;
719 base_addr[CyIER] |= CyTxMpty;
720 local_irq_restore(flags);
721} /* start_xmit */
722
723/*
724 * This routine shuts down a serial port; interrupts are disabled,
725 * and DTR is dropped if the hangup on close termio flag is on.
726 */
727static void shutdown(struct cyclades_port *info)
728{
729 unsigned long flags;
730 volatile unsigned char *base_addr = (u_char *) BASE_ADDR;
731 int channel;
732
733 if (!(info->flags & ASYNC_INITIALIZED)) {
734/* CP('$'); */
735 return;
736 }
737
738 channel = info->line;
739
740#ifdef SERIAL_DEBUG_OPEN
741 printk("shutdown channel %d\n", channel);
742#endif
743
744 /* !!! REALLY MUST WAIT FOR LAST CHARACTER TO BE
745 SENT BEFORE DROPPING THE LINE !!! (Perhaps
746 set some flag that is read when XMTY happens.)
747 Other choices are to delay some fixed interval
748 or schedule some later processing.
749 */
750 local_irq_save(flags);
751 if (info->xmit_buf) {
752 free_page((unsigned long)info->xmit_buf);
753 info->xmit_buf = NULL;
754 }
755
756 base_addr[CyCAR] = (u_char) channel;
757 if (!info->tty || (info->tty->termios->c_cflag & HUPCL)) {
758 base_addr[CyMSVR1] = 0;
759/* CP('C');CP('1'); */
760 base_addr[CyMSVR2] = 0;
761#ifdef SERIAL_DEBUG_DTR
762 printk("cyc: %d: dropping DTR\n", __LINE__);
763 printk(" status: 0x%x, 0x%x\n", base_addr[CyMSVR1],
764 base_addr[CyMSVR2]);
765#endif
766 }
767 write_cy_cmd(base_addr, CyDIS_RCVR);
768 /* it may be appropriate to clear _XMIT at
769 some later date (after testing)!!! */
770
771 if (info->tty) {
772 set_bit(TTY_IO_ERROR, &info->tty->flags);
773 }
774 info->flags &= ~ASYNC_INITIALIZED;
775 local_irq_restore(flags);
776
777#ifdef SERIAL_DEBUG_OPEN
778 printk(" done\n");
779#endif
780} /* shutdown */
781
782/*
783 * This routine finds or computes the various line characteristics.
784 */
785static void config_setup(struct cyclades_port *info)
786{
787 unsigned long flags;
788 volatile unsigned char *base_addr = (u_char *) BASE_ADDR;
789 int channel;
790 unsigned cflag;
791 int i;
792 unsigned char ti, need_init_chan = 0;
793
794 if (!info->tty || !info->tty->termios) {
795 return;
796 }
797 if (info->line == -1) {
798 return;
799 }
800 cflag = info->tty->termios->c_cflag;
801
802 /* baud rate */
803 i = cflag & CBAUD;
804#ifdef CBAUDEX
805/* Starting with kernel 1.1.65, there is direct support for
806 higher baud rates. The following code supports those
807 changes. The conditional aspect allows this driver to be
808 used for earlier as well as later kernel versions. (The
809 mapping is slightly different from serial.c because there
810 is still the possibility of supporting 75 kbit/sec with
811 the Cyclades board.)
812 */
813 if (i & CBAUDEX) {
814 if (i == B57600)
815 i = 16;
816 else if (i == B115200)
817 i = 18;
818#ifdef B78600
819 else if (i == B78600)
820 i = 17;
821#endif
822 else
823 info->tty->termios->c_cflag &= ~CBAUDEX;
824 }
825#endif
826 if (i == 15) {
827 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
828 i += 1;
829 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
830 i += 3;
831 }
832 /* Don't ever change the speed of the console port. It will
833 * run at the speed specified in bootinfo, or at 19.2K */
834 /* Actually, it should run at whatever speed 166Bug was using */
835 /* Note info->timeout isn't used at present */
836 if (info != serial_console_info) {
837 info->tbpr = baud_bpr[i]; /* Tx BPR */
838 info->tco = baud_co[i]; /* Tx CO */
839 info->rbpr = baud_bpr[i]; /* Rx BPR */
840 info->rco = baud_co[i] >> 5; /* Rx CO */
841 if (baud_table[i] == 134) {
842 info->timeout =
843 (info->xmit_fifo_size * HZ * 30 / 269) + 2;
844 /* get it right for 134.5 baud */
845 } else if (baud_table[i]) {
846 info->timeout =
847 (info->xmit_fifo_size * HZ * 15 / baud_table[i]) +
848 2;
849 /* this needs to be propagated into the card info */
850 } else {
851 info->timeout = 0;
852 }
853 }
854 /* By tradition (is it a standard?) a baud rate of zero
855 implies the line should be/has been closed. A bit
856 later in this routine such a test is performed. */
857
858 /* byte size and parity */
859 info->cor7 = 0;
860 info->cor6 = 0;
861 info->cor5 = 0;
862 info->cor4 = (info->default_threshold ? info->default_threshold : baud_cor4[i]); /* receive threshold */
863 /* Following two lines added 101295, RGH. */
864 /* It is obviously wrong to access CyCORx, and not info->corx here,
865 * try and remember to fix it later! */
866 channel = info->line;
867 base_addr[CyCAR] = (u_char) channel;
868 if (C_CLOCAL(info->tty)) {
869 if (base_addr[CyIER] & CyMdmCh)
870 base_addr[CyIER] &= ~CyMdmCh; /* without modem intr */
871 /* ignore 1->0 modem transitions */
872 if (base_addr[CyCOR4] & (CyDSR | CyCTS | CyDCD))
873 base_addr[CyCOR4] &= ~(CyDSR | CyCTS | CyDCD);
874 /* ignore 0->1 modem transitions */
875 if (base_addr[CyCOR5] & (CyDSR | CyCTS | CyDCD))
876 base_addr[CyCOR5] &= ~(CyDSR | CyCTS | CyDCD);
877 } else {
878 if ((base_addr[CyIER] & CyMdmCh) != CyMdmCh)
879 base_addr[CyIER] |= CyMdmCh; /* with modem intr */
880 /* act on 1->0 modem transitions */
881 if ((base_addr[CyCOR4] & (CyDSR | CyCTS | CyDCD)) !=
882 (CyDSR | CyCTS | CyDCD))
883 base_addr[CyCOR4] |= CyDSR | CyCTS | CyDCD;
884 /* act on 0->1 modem transitions */
885 if ((base_addr[CyCOR5] & (CyDSR | CyCTS | CyDCD)) !=
886 (CyDSR | CyCTS | CyDCD))
887 base_addr[CyCOR5] |= CyDSR | CyCTS | CyDCD;
888 }
889 info->cor3 = (cflag & CSTOPB) ? Cy_2_STOP : Cy_1_STOP;
890 info->cor2 = CyETC;
891 switch (cflag & CSIZE) {
892 case CS5:
893 info->cor1 = Cy_5_BITS;
894 break;
895 case CS6:
896 info->cor1 = Cy_6_BITS;
897 break;
898 case CS7:
899 info->cor1 = Cy_7_BITS;
900 break;
901 case CS8:
902 info->cor1 = Cy_8_BITS;
903 break;
904 }
905 if (cflag & PARENB) {
906 if (cflag & PARODD) {
907 info->cor1 |= CyPARITY_O;
908 } else {
909 info->cor1 |= CyPARITY_E;
910 }
911 } else {
912 info->cor1 |= CyPARITY_NONE;
913 }
914
915 /* CTS flow control flag */
916#if 0
917 /* Don't complcate matters for now! RGH 141095 */
918 if (cflag & CRTSCTS) {
919 info->flags |= ASYNC_CTS_FLOW;
920 info->cor2 |= CyCtsAE;
921 } else {
922 info->flags &= ~ASYNC_CTS_FLOW;
923 info->cor2 &= ~CyCtsAE;
924 }
925#endif
926 if (cflag & CLOCAL)
927 info->flags &= ~ASYNC_CHECK_CD;
928 else
929 info->flags |= ASYNC_CHECK_CD;
930
931 /***********************************************
932 The hardware option, CyRtsAO, presents RTS when
933 the chip has characters to send. Since most modems
934 use RTS as reverse (inbound) flow control, this
935 option is not used. If inbound flow control is
936 necessary, DTR can be programmed to provide the
937 appropriate signals for use with a non-standard
938 cable. Contact Marcio Saito for details.
939 ***********************************************/
940
941 channel = info->line;
942
943 local_irq_save(flags);
944 base_addr[CyCAR] = (u_char) channel;
945
946 /* CyCMR set once only in mvme167_init_serial() */
947 if (base_addr[CyLICR] != channel << 2)
948 base_addr[CyLICR] = channel << 2;
949 if (base_addr[CyLIVR] != 0x5c)
950 base_addr[CyLIVR] = 0x5c;
951
952 /* tx and rx baud rate */
953
954 if (base_addr[CyCOR1] != info->cor1)
955 need_init_chan = 1;
956 if (base_addr[CyTCOR] != info->tco)
957 base_addr[CyTCOR] = info->tco;
958 if (base_addr[CyTBPR] != info->tbpr)
959 base_addr[CyTBPR] = info->tbpr;
960 if (base_addr[CyRCOR] != info->rco)
961 base_addr[CyRCOR] = info->rco;
962 if (base_addr[CyRBPR] != info->rbpr)
963 base_addr[CyRBPR] = info->rbpr;
964
965 /* set line characteristics according configuration */
966
967 if (base_addr[CySCHR1] != START_CHAR(info->tty))
968 base_addr[CySCHR1] = START_CHAR(info->tty);
969 if (base_addr[CySCHR2] != STOP_CHAR(info->tty))
970 base_addr[CySCHR2] = STOP_CHAR(info->tty);
971 if (base_addr[CySCRL] != START_CHAR(info->tty))
972 base_addr[CySCRL] = START_CHAR(info->tty);
973 if (base_addr[CySCRH] != START_CHAR(info->tty))
974 base_addr[CySCRH] = START_CHAR(info->tty);
975 if (base_addr[CyCOR1] != info->cor1)
976 base_addr[CyCOR1] = info->cor1;
977 if (base_addr[CyCOR2] != info->cor2)
978 base_addr[CyCOR2] = info->cor2;
979 if (base_addr[CyCOR3] != info->cor3)
980 base_addr[CyCOR3] = info->cor3;
981 if (base_addr[CyCOR4] != info->cor4)
982 base_addr[CyCOR4] = info->cor4;
983 if (base_addr[CyCOR5] != info->cor5)
984 base_addr[CyCOR5] = info->cor5;
985 if (base_addr[CyCOR6] != info->cor6)
986 base_addr[CyCOR6] = info->cor6;
987 if (base_addr[CyCOR7] != info->cor7)
988 base_addr[CyCOR7] = info->cor7;
989
990 if (need_init_chan)
991 write_cy_cmd(base_addr, CyINIT_CHAN);
992
993 base_addr[CyCAR] = (u_char) channel; /* !!! Is this needed? */
994
995 /* 2ms default rx timeout */
996 ti = info->default_timeout ? info->default_timeout : 0x02;
997 if (base_addr[CyRTPRL] != ti)
998 base_addr[CyRTPRL] = ti;
999 if (base_addr[CyRTPRH] != 0)
1000 base_addr[CyRTPRH] = 0;
1001
1002 /* Set up RTS here also ????? RGH 141095 */
1003 if (i == 0) { /* baud rate is zero, turn off line */
1004 if ((base_addr[CyMSVR2] & CyDTR) == CyDTR)
1005 base_addr[CyMSVR2] = 0;
1006#ifdef SERIAL_DEBUG_DTR
1007 printk("cyc: %d: dropping DTR\n", __LINE__);
1008 printk(" status: 0x%x, 0x%x\n", base_addr[CyMSVR1],
1009 base_addr[CyMSVR2]);
1010#endif
1011 } else {
1012 if ((base_addr[CyMSVR2] & CyDTR) != CyDTR)
1013 base_addr[CyMSVR2] = CyDTR;
1014#ifdef SERIAL_DEBUG_DTR
1015 printk("cyc: %d: raising DTR\n", __LINE__);
1016 printk(" status: 0x%x, 0x%x\n", base_addr[CyMSVR1],
1017 base_addr[CyMSVR2]);
1018#endif
1019 }
1020
1021 if (info->tty) {
1022 clear_bit(TTY_IO_ERROR, &info->tty->flags);
1023 }
1024
1025 local_irq_restore(flags);
1026
1027} /* config_setup */
1028
1029static int cy_put_char(struct tty_struct *tty, unsigned char ch)
1030{
1031 struct cyclades_port *info = tty->driver_data;
1032 unsigned long flags;
1033
1034#ifdef SERIAL_DEBUG_IO
1035 printk("cy_put_char %s(0x%02x)\n", tty->name, ch);
1036#endif
1037
1038 if (serial_paranoia_check(info, tty->name, "cy_put_char"))
1039 return 0;
1040
1041 if (!info->xmit_buf)
1042 return 0;
1043
1044 local_irq_save(flags);
1045 if (info->xmit_cnt >= PAGE_SIZE - 1) {
1046 local_irq_restore(flags);
1047 return 0;
1048 }
1049
1050 info->xmit_buf[info->xmit_head++] = ch;
1051 info->xmit_head &= PAGE_SIZE - 1;
1052 info->xmit_cnt++;
1053 local_irq_restore(flags);
1054 return 1;
1055} /* cy_put_char */
1056
1057static void cy_flush_chars(struct tty_struct *tty)
1058{
1059 struct cyclades_port *info = tty->driver_data;
1060 unsigned long flags;
1061 volatile unsigned char *base_addr = (u_char *) BASE_ADDR;
1062 int channel;
1063
1064#ifdef SERIAL_DEBUG_IO
1065 printk("cy_flush_chars %s\n", tty->name); /* */
1066#endif
1067
1068 if (serial_paranoia_check(info, tty->name, "cy_flush_chars"))
1069 return;
1070
1071 if (info->xmit_cnt <= 0 || tty->stopped
1072 || tty->hw_stopped || !info->xmit_buf)
1073 return;
1074
1075 channel = info->line;
1076
1077 local_irq_save(flags);
1078 base_addr[CyCAR] = channel;
1079 base_addr[CyIER] |= CyTxMpty;
1080 local_irq_restore(flags);
1081} /* cy_flush_chars */
1082
1083/* This routine gets called when tty_write has put something into
1084 the write_queue. If the port is not already transmitting stuff,
1085 start it off by enabling interrupts. The interrupt service
1086 routine will then ensure that the characters are sent. If the
1087 port is already active, there is no need to kick it.
1088 */
1089static int cy_write(struct tty_struct *tty, const unsigned char *buf, int count)
1090{
1091 struct cyclades_port *info = tty->driver_data;
1092 unsigned long flags;
1093 int c, total = 0;
1094
1095#ifdef SERIAL_DEBUG_IO
1096 printk("cy_write %s\n", tty->name); /* */
1097#endif
1098
1099 if (serial_paranoia_check(info, tty->name, "cy_write")) {
1100 return 0;
1101 }
1102
1103 if (!info->xmit_buf) {
1104 return 0;
1105 }
1106
1107 while (1) {
1108 local_irq_save(flags);
1109 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1110 SERIAL_XMIT_SIZE - info->xmit_head));
1111 if (c <= 0) {
1112 local_irq_restore(flags);
1113 break;
1114 }
1115
1116 memcpy(info->xmit_buf + info->xmit_head, buf, c);
1117 info->xmit_head =
1118 (info->xmit_head + c) & (SERIAL_XMIT_SIZE - 1);
1119 info->xmit_cnt += c;
1120 local_irq_restore(flags);
1121
1122 buf += c;
1123 count -= c;
1124 total += c;
1125 }
1126
1127 if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped) {
1128 start_xmit(info);
1129 }
1130 return total;
1131} /* cy_write */
1132
1133static int cy_write_room(struct tty_struct *tty)
1134{
1135 struct cyclades_port *info = tty->driver_data;
1136 int ret;
1137
1138#ifdef SERIAL_DEBUG_IO
1139 printk("cy_write_room %s\n", tty->name); /* */
1140#endif
1141
1142 if (serial_paranoia_check(info, tty->name, "cy_write_room"))
1143 return 0;
1144 ret = PAGE_SIZE - info->xmit_cnt - 1;
1145 if (ret < 0)
1146 ret = 0;
1147 return ret;
1148} /* cy_write_room */
1149
1150static int cy_chars_in_buffer(struct tty_struct *tty)
1151{
1152 struct cyclades_port *info = tty->driver_data;
1153
1154#ifdef SERIAL_DEBUG_IO
1155 printk("cy_chars_in_buffer %s %d\n", tty->name, info->xmit_cnt); /* */
1156#endif
1157
1158 if (serial_paranoia_check(info, tty->name, "cy_chars_in_buffer"))
1159 return 0;
1160
1161 return info->xmit_cnt;
1162} /* cy_chars_in_buffer */
1163
1164static void cy_flush_buffer(struct tty_struct *tty)
1165{
1166 struct cyclades_port *info = tty->driver_data;
1167 unsigned long flags;
1168
1169#ifdef SERIAL_DEBUG_IO
1170 printk("cy_flush_buffer %s\n", tty->name); /* */
1171#endif
1172
1173 if (serial_paranoia_check(info, tty->name, "cy_flush_buffer"))
1174 return;
1175 local_irq_save(flags);
1176 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1177 local_irq_restore(flags);
1178 tty_wakeup(tty);
1179} /* cy_flush_buffer */
1180
1181/* This routine is called by the upper-layer tty layer to signal
1182 that incoming characters should be throttled or that the
1183 throttle should be released.
1184 */
1185static void cy_throttle(struct tty_struct *tty)
1186{
1187 struct cyclades_port *info = tty->driver_data;
1188 unsigned long flags;
1189 volatile unsigned char *base_addr = (u_char *) BASE_ADDR;
1190 int channel;
1191
1192#ifdef SERIAL_DEBUG_THROTTLE
1193 char buf[64];
1194
1195 printk("throttle %s: %d....\n", tty_name(tty, buf),
1196 tty->ldisc.chars_in_buffer(tty));
1197 printk("cy_throttle %s\n", tty->name);
1198#endif
1199
1200 if (serial_paranoia_check(info, tty->name, "cy_nthrottle")) {
1201 return;
1202 }
1203
1204 if (I_IXOFF(tty)) {
1205 info->x_char = STOP_CHAR(tty);
1206 /* Should use the "Send Special Character" feature!!! */
1207 }
1208
1209 channel = info->line;
1210
1211 local_irq_save(flags);
1212 base_addr[CyCAR] = (u_char) channel;
1213 base_addr[CyMSVR1] = 0;
1214 local_irq_restore(flags);
1215} /* cy_throttle */
1216
1217static void cy_unthrottle(struct tty_struct *tty)
1218{
1219 struct cyclades_port *info = tty->driver_data;
1220 unsigned long flags;
1221 volatile unsigned char *base_addr = (u_char *) BASE_ADDR;
1222 int channel;
1223
1224#ifdef SERIAL_DEBUG_THROTTLE
1225 char buf[64];
1226
1227 printk("throttle %s: %d....\n", tty_name(tty, buf),
1228 tty->ldisc.chars_in_buffer(tty));
1229 printk("cy_unthrottle %s\n", tty->name);
1230#endif
1231
1232 if (serial_paranoia_check(info, tty->name, "cy_nthrottle")) {
1233 return;
1234 }
1235
1236 if (I_IXOFF(tty)) {
1237 info->x_char = START_CHAR(tty);
1238 /* Should use the "Send Special Character" feature!!! */
1239 }
1240
1241 channel = info->line;
1242
1243 local_irq_save(flags);
1244 base_addr[CyCAR] = (u_char) channel;
1245 base_addr[CyMSVR1] = CyRTS;
1246 local_irq_restore(flags);
1247} /* cy_unthrottle */
1248
1249static int
1250get_serial_info(struct cyclades_port *info,
1251 struct serial_struct __user * retinfo)
1252{
1253 struct serial_struct tmp;
1254
1255/* CP('g'); */
1256 if (!retinfo)
1257 return -EFAULT;
1258 memset(&tmp, 0, sizeof(tmp));
1259 tmp.type = info->type;
1260 tmp.line = info->line;
1261 tmp.port = info->line;
1262 tmp.irq = 0;
1263 tmp.flags = info->flags;
1264 tmp.baud_base = 0; /*!!! */
1265 tmp.close_delay = info->close_delay;
1266 tmp.custom_divisor = 0; /*!!! */
1267 tmp.hub6 = 0; /*!!! */
1268 return copy_to_user(retinfo, &tmp, sizeof(*retinfo)) ? -EFAULT : 0;
1269} /* get_serial_info */
1270
1271static int
1272set_serial_info(struct cyclades_port *info,
1273 struct serial_struct __user * new_info)
1274{
1275 struct serial_struct new_serial;
1276 struct cyclades_port old_info;
1277
1278/* CP('s'); */
1279 if (!new_info)
1280 return -EFAULT;
1281 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
1282 return -EFAULT;
1283 old_info = *info;
1284
1285 if (!capable(CAP_SYS_ADMIN)) {
1286 if ((new_serial.close_delay != info->close_delay) ||
1287 ((new_serial.flags & ASYNC_FLAGS & ~ASYNC_USR_MASK) !=
1288 (info->flags & ASYNC_FLAGS & ~ASYNC_USR_MASK)))
1289 return -EPERM;
1290 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
1291 (new_serial.flags & ASYNC_USR_MASK));
1292 goto check_and_exit;
1293 }
1294
1295 /*
1296 * OK, past this point, all the error checking has been done.
1297 * At this point, we start making changes.....
1298 */
1299
1300 info->flags = ((info->flags & ~ASYNC_FLAGS) |
1301 (new_serial.flags & ASYNC_FLAGS));
1302 info->close_delay = new_serial.close_delay;
1303
1304check_and_exit:
1305 if (info->flags & ASYNC_INITIALIZED) {
1306 config_setup(info);
1307 return 0;
1308 }
1309 return startup(info);
1310} /* set_serial_info */
1311
1312static int cy_tiocmget(struct tty_struct *tty, struct file *file)
1313{
1314 struct cyclades_port *info = tty->driver_data;
1315 int channel;
1316 volatile unsigned char *base_addr = (u_char *) BASE_ADDR;
1317 unsigned long flags;
1318 unsigned char status;
1319
1320 channel = info->line;
1321
1322 local_irq_save(flags);
1323 base_addr[CyCAR] = (u_char) channel;
1324 status = base_addr[CyMSVR1] | base_addr[CyMSVR2];
1325 local_irq_restore(flags);
1326
1327 return ((status & CyRTS) ? TIOCM_RTS : 0)
1328 | ((status & CyDTR) ? TIOCM_DTR : 0)
1329 | ((status & CyDCD) ? TIOCM_CAR : 0)
1330 | ((status & CyDSR) ? TIOCM_DSR : 0)
1331 | ((status & CyCTS) ? TIOCM_CTS : 0);
1332} /* cy_tiocmget */
1333
1334static int
1335cy_tiocmset(struct tty_struct *tty, struct file *file,
1336 unsigned int set, unsigned int clear)
1337{
1338 struct cyclades_port *info = tty->driver_data;
1339 int channel;
1340 volatile unsigned char *base_addr = (u_char *) BASE_ADDR;
1341 unsigned long flags;
1342
1343 channel = info->line;
1344
1345 if (set & TIOCM_RTS) {
1346 local_irq_save(flags);
1347 base_addr[CyCAR] = (u_char) channel;
1348 base_addr[CyMSVR1] = CyRTS;
1349 local_irq_restore(flags);
1350 }
1351 if (set & TIOCM_DTR) {
1352 local_irq_save(flags);
1353 base_addr[CyCAR] = (u_char) channel;
1354/* CP('S');CP('2'); */
1355 base_addr[CyMSVR2] = CyDTR;
1356#ifdef SERIAL_DEBUG_DTR
1357 printk("cyc: %d: raising DTR\n", __LINE__);
1358 printk(" status: 0x%x, 0x%x\n", base_addr[CyMSVR1],
1359 base_addr[CyMSVR2]);
1360#endif
1361 local_irq_restore(flags);
1362 }
1363
1364 if (clear & TIOCM_RTS) {
1365 local_irq_save(flags);
1366 base_addr[CyCAR] = (u_char) channel;
1367 base_addr[CyMSVR1] = 0;
1368 local_irq_restore(flags);
1369 }
1370 if (clear & TIOCM_DTR) {
1371 local_irq_save(flags);
1372 base_addr[CyCAR] = (u_char) channel;
1373/* CP('C');CP('2'); */
1374 base_addr[CyMSVR2] = 0;
1375#ifdef SERIAL_DEBUG_DTR
1376 printk("cyc: %d: dropping DTR\n", __LINE__);
1377 printk(" status: 0x%x, 0x%x\n", base_addr[CyMSVR1],
1378 base_addr[CyMSVR2]);
1379#endif
1380 local_irq_restore(flags);
1381 }
1382
1383 return 0;
1384} /* set_modem_info */
1385
1386static void send_break(struct cyclades_port *info, int duration)
1387{ /* Let the transmit ISR take care of this (since it
1388 requires stuffing characters into the output stream).
1389 */
1390 info->x_break = duration;
1391 if (!info->xmit_cnt) {
1392 start_xmit(info);
1393 }
1394} /* send_break */
1395
1396static int
1397get_mon_info(struct cyclades_port *info, struct cyclades_monitor __user * mon)
1398{
1399
1400 if (copy_to_user(mon, &info->mon, sizeof(struct cyclades_monitor)))
1401 return -EFAULT;
1402 info->mon.int_count = 0;
1403 info->mon.char_count = 0;
1404 info->mon.char_max = 0;
1405 info->mon.char_last = 0;
1406 return 0;
1407}
1408
1409static int set_threshold(struct cyclades_port *info, unsigned long __user * arg)
1410{
1411 volatile unsigned char *base_addr = (u_char *) BASE_ADDR;
1412 unsigned long value;
1413 int channel;
1414
1415 if (get_user(value, arg))
1416 return -EFAULT;
1417
1418 channel = info->line;
1419 info->cor4 &= ~CyREC_FIFO;
1420 info->cor4 |= value & CyREC_FIFO;
1421 base_addr[CyCOR4] = info->cor4;
1422 return 0;
1423}
1424
1425static int
1426get_threshold(struct cyclades_port *info, unsigned long __user * value)
1427{
1428 volatile unsigned char *base_addr = (u_char *) BASE_ADDR;
1429 int channel;
1430 unsigned long tmp;
1431
1432 channel = info->line;
1433
1434 tmp = base_addr[CyCOR4] & CyREC_FIFO;
1435 return put_user(tmp, value);
1436}
1437
1438static int
1439set_default_threshold(struct cyclades_port *info, unsigned long __user * arg)
1440{
1441 unsigned long value;
1442
1443 if (get_user(value, arg))
1444 return -EFAULT;
1445
1446 info->default_threshold = value & 0x0f;
1447 return 0;
1448}
1449
1450static int
1451get_default_threshold(struct cyclades_port *info, unsigned long __user * value)
1452{
1453 return put_user(info->default_threshold, value);
1454}
1455
1456static int set_timeout(struct cyclades_port *info, unsigned long __user * arg)
1457{
1458 volatile unsigned char *base_addr = (u_char *) BASE_ADDR;
1459 int channel;
1460 unsigned long value;
1461
1462 if (get_user(value, arg))
1463 return -EFAULT;
1464
1465 channel = info->line;
1466
1467 base_addr[CyRTPRL] = value & 0xff;
1468 base_addr[CyRTPRH] = (value >> 8) & 0xff;
1469 return 0;
1470}
1471
1472static int get_timeout(struct cyclades_port *info, unsigned long __user * value)
1473{
1474 volatile unsigned char *base_addr = (u_char *) BASE_ADDR;
1475 int channel;
1476 unsigned long tmp;
1477
1478 channel = info->line;
1479
1480 tmp = base_addr[CyRTPRL];
1481 return put_user(tmp, value);
1482}
1483
1484static int set_default_timeout(struct cyclades_port *info, unsigned long value)
1485{
1486 info->default_timeout = value & 0xff;
1487 return 0;
1488}
1489
1490static int
1491get_default_timeout(struct cyclades_port *info, unsigned long __user * value)
1492{
1493 return put_user(info->default_timeout, value);
1494}
1495
1496static int
1497cy_ioctl(struct tty_struct *tty, struct file *file,
1498 unsigned int cmd, unsigned long arg)
1499{
1500 struct cyclades_port *info = tty->driver_data;
1501 int ret_val = 0;
1502 void __user *argp = (void __user *)arg;
1503
1504#ifdef SERIAL_DEBUG_OTHER
1505 printk("cy_ioctl %s, cmd = %x arg = %lx\n", tty->name, cmd, arg); /* */
1506#endif
1507
1508 tty_lock();
1509
1510 switch (cmd) {
1511 case CYGETMON:
1512 ret_val = get_mon_info(info, argp);
1513 break;
1514 case CYGETTHRESH:
1515 ret_val = get_threshold(info, argp);
1516 break;
1517 case CYSETTHRESH:
1518 ret_val = set_threshold(info, argp);
1519 break;
1520 case CYGETDEFTHRESH:
1521 ret_val = get_default_threshold(info, argp);
1522 break;
1523 case CYSETDEFTHRESH:
1524 ret_val = set_default_threshold(info, argp);
1525 break;
1526 case CYGETTIMEOUT:
1527 ret_val = get_timeout(info, argp);
1528 break;
1529 case CYSETTIMEOUT:
1530 ret_val = set_timeout(info, argp);
1531 break;
1532 case CYGETDEFTIMEOUT:
1533 ret_val = get_default_timeout(info, argp);
1534 break;
1535 case CYSETDEFTIMEOUT:
1536 ret_val = set_default_timeout(info, (unsigned long)arg);
1537 break;
1538 case TCSBRK: /* SVID version: non-zero arg --> no break */
1539 ret_val = tty_check_change(tty);
1540 if (ret_val)
1541 break;
1542 tty_wait_until_sent(tty, 0);
1543 if (!arg)
1544 send_break(info, HZ / 4); /* 1/4 second */
1545 break;
1546 case TCSBRKP: /* support for POSIX tcsendbreak() */
1547 ret_val = tty_check_change(tty);
1548 if (ret_val)
1549 break;
1550 tty_wait_until_sent(tty, 0);
1551 send_break(info, arg ? arg * (HZ / 10) : HZ / 4);
1552 break;
1553
1554/* The following commands are incompletely implemented!!! */
1555 case TIOCGSERIAL:
1556 ret_val = get_serial_info(info, argp);
1557 break;
1558 case TIOCSSERIAL:
1559 ret_val = set_serial_info(info, argp);
1560 break;
1561 default:
1562 ret_val = -ENOIOCTLCMD;
1563 }
1564 tty_unlock();
1565
1566#ifdef SERIAL_DEBUG_OTHER
1567 printk("cy_ioctl done\n");
1568#endif
1569
1570 return ret_val;
1571} /* cy_ioctl */
1572
1573static void cy_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
1574{
1575 struct cyclades_port *info = tty->driver_data;
1576
1577#ifdef SERIAL_DEBUG_OTHER
1578 printk("cy_set_termios %s\n", tty->name);
1579#endif
1580
1581 if (tty->termios->c_cflag == old_termios->c_cflag)
1582 return;
1583 config_setup(info);
1584
1585 if ((old_termios->c_cflag & CRTSCTS) &&
1586 !(tty->termios->c_cflag & CRTSCTS)) {
1587 tty->stopped = 0;
1588 cy_start(tty);
1589 }
1590#ifdef tytso_patch_94Nov25_1726
1591 if (!(old_termios->c_cflag & CLOCAL) &&
1592 (tty->termios->c_cflag & CLOCAL))
1593 wake_up_interruptible(&info->open_wait);
1594#endif
1595} /* cy_set_termios */
1596
1597static void cy_close(struct tty_struct *tty, struct file *filp)
1598{
1599 struct cyclades_port *info = tty->driver_data;
1600
1601/* CP('C'); */
1602#ifdef SERIAL_DEBUG_OTHER
1603 printk("cy_close %s\n", tty->name);
1604#endif
1605
1606 if (!info || serial_paranoia_check(info, tty->name, "cy_close")) {
1607 return;
1608 }
1609#ifdef SERIAL_DEBUG_OPEN
1610 printk("cy_close %s, count = %d\n", tty->name, info->count);
1611#endif
1612
1613 if ((tty->count == 1) && (info->count != 1)) {
1614 /*
1615 * Uh, oh. tty->count is 1, which means that the tty
1616 * structure will be freed. Info->count should always
1617 * be one in these conditions. If it's greater than
1618 * one, we've got real problems, since it means the
1619 * serial port won't be shutdown.
1620 */
1621 printk("cy_close: bad serial port count; tty->count is 1, "
1622 "info->count is %d\n", info->count);
1623 info->count = 1;
1624 }
1625#ifdef SERIAL_DEBUG_COUNT
1626 printk("cyc: %d: decrementing count to %d\n", __LINE__,
1627 info->count - 1);
1628#endif
1629 if (--info->count < 0) {
1630 printk("cy_close: bad serial port count for ttys%d: %d\n",
1631 info->line, info->count);
1632#ifdef SERIAL_DEBUG_COUNT
1633 printk("cyc: %d: setting count to 0\n", __LINE__);
1634#endif
1635 info->count = 0;
1636 }
1637 if (info->count)
1638 return;
1639 info->flags |= ASYNC_CLOSING;
1640 if (info->flags & ASYNC_INITIALIZED)
1641 tty_wait_until_sent(tty, 3000); /* 30 seconds timeout */
1642 shutdown(info);
1643 cy_flush_buffer(tty);
1644 tty_ldisc_flush(tty);
1645 info->tty = NULL;
1646 if (info->blocked_open) {
1647 if (info->close_delay) {
1648 msleep_interruptible(jiffies_to_msecs
1649 (info->close_delay));
1650 }
1651 wake_up_interruptible(&info->open_wait);
1652 }
1653 info->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
1654 wake_up_interruptible(&info->close_wait);
1655
1656#ifdef SERIAL_DEBUG_OTHER
1657 printk("cy_close done\n");
1658#endif
1659} /* cy_close */
1660
1661/*
1662 * cy_hangup() --- called by tty_hangup() when a hangup is signaled.
1663 */
1664void cy_hangup(struct tty_struct *tty)
1665{
1666 struct cyclades_port *info = tty->driver_data;
1667
1668#ifdef SERIAL_DEBUG_OTHER
1669 printk("cy_hangup %s\n", tty->name); /* */
1670#endif
1671
1672 if (serial_paranoia_check(info, tty->name, "cy_hangup"))
1673 return;
1674
1675 shutdown(info);
1676#if 0
1677 info->event = 0;
1678 info->count = 0;
1679#ifdef SERIAL_DEBUG_COUNT
1680 printk("cyc: %d: setting count to 0\n", __LINE__);
1681#endif
1682 info->tty = 0;
1683#endif
1684 info->flags &= ~ASYNC_NORMAL_ACTIVE;
1685 wake_up_interruptible(&info->open_wait);
1686} /* cy_hangup */
1687
1688/*
1689 * ------------------------------------------------------------
1690 * cy_open() and friends
1691 * ------------------------------------------------------------
1692 */
1693
1694static int
1695block_til_ready(struct tty_struct *tty, struct file *filp,
1696 struct cyclades_port *info)
1697{
1698 DECLARE_WAITQUEUE(wait, current);
1699 unsigned long flags;
1700 int channel;
1701 int retval;
1702 volatile u_char *base_addr = (u_char *) BASE_ADDR;
1703
1704 /*
1705 * If the device is in the middle of being closed, then block
1706 * until it's done, and then try again.
1707 */
1708 if (info->flags & ASYNC_CLOSING) {
1709 interruptible_sleep_on(&info->close_wait);
1710 if (info->flags & ASYNC_HUP_NOTIFY) {
1711 return -EAGAIN;
1712 } else {
1713 return -ERESTARTSYS;
1714 }
1715 }
1716
1717 /*
1718 * If non-blocking mode is set, then make the check up front
1719 * and then exit.
1720 */
1721 if (filp->f_flags & O_NONBLOCK) {
1722 info->flags |= ASYNC_NORMAL_ACTIVE;
1723 return 0;
1724 }
1725
1726 /*
1727 * Block waiting for the carrier detect and the line to become
1728 * free (i.e., not in use by the callout). While we are in
1729 * this loop, info->count is dropped by one, so that
1730 * cy_close() knows when to free things. We restore it upon
1731 * exit, either normal or abnormal.
1732 */
1733 retval = 0;
1734 add_wait_queue(&info->open_wait, &wait);
1735#ifdef SERIAL_DEBUG_OPEN
1736 printk("block_til_ready before block: %s, count = %d\n",
1737 tty->name, info->count);
1738 /**/
1739#endif
1740 info->count--;
1741#ifdef SERIAL_DEBUG_COUNT
1742 printk("cyc: %d: decrementing count to %d\n", __LINE__, info->count);
1743#endif
1744 info->blocked_open++;
1745
1746 channel = info->line;
1747
1748 while (1) {
1749 local_irq_save(flags);
1750 base_addr[CyCAR] = (u_char) channel;
1751 base_addr[CyMSVR1] = CyRTS;
1752/* CP('S');CP('4'); */
1753 base_addr[CyMSVR2] = CyDTR;
1754#ifdef SERIAL_DEBUG_DTR
1755 printk("cyc: %d: raising DTR\n", __LINE__);
1756 printk(" status: 0x%x, 0x%x\n", base_addr[CyMSVR1],
1757 base_addr[CyMSVR2]);
1758#endif
1759 local_irq_restore(flags);
1760 set_current_state(TASK_INTERRUPTIBLE);
1761 if (tty_hung_up_p(filp)
1762 || !(info->flags & ASYNC_INITIALIZED)) {
1763 if (info->flags & ASYNC_HUP_NOTIFY) {
1764 retval = -EAGAIN;
1765 } else {
1766 retval = -ERESTARTSYS;
1767 }
1768 break;
1769 }
1770 local_irq_save(flags);
1771 base_addr[CyCAR] = (u_char) channel;
1772/* CP('L');CP1(1 && C_CLOCAL(tty)); CP1(1 && (base_addr[CyMSVR1] & CyDCD) ); */
1773 if (!(info->flags & ASYNC_CLOSING)
1774 && (C_CLOCAL(tty)
1775 || (base_addr[CyMSVR1] & CyDCD))) {
1776 local_irq_restore(flags);
1777 break;
1778 }
1779 local_irq_restore(flags);
1780 if (signal_pending(current)) {
1781 retval = -ERESTARTSYS;
1782 break;
1783 }
1784#ifdef SERIAL_DEBUG_OPEN
1785 printk("block_til_ready blocking: %s, count = %d\n",
1786 tty->name, info->count);
1787 /**/
1788#endif
1789 tty_unlock();
1790 schedule();
1791 tty_lock();
1792 }
1793 __set_current_state(TASK_RUNNING);
1794 remove_wait_queue(&info->open_wait, &wait);
1795 if (!tty_hung_up_p(filp)) {
1796 info->count++;
1797#ifdef SERIAL_DEBUG_COUNT
1798 printk("cyc: %d: incrementing count to %d\n", __LINE__,
1799 info->count);
1800#endif
1801 }
1802 info->blocked_open--;
1803#ifdef SERIAL_DEBUG_OPEN
1804 printk("block_til_ready after blocking: %s, count = %d\n",
1805 tty->name, info->count);
1806 /**/
1807#endif
1808 if (retval)
1809 return retval;
1810 info->flags |= ASYNC_NORMAL_ACTIVE;
1811 return 0;
1812} /* block_til_ready */
1813
1814/*
1815 * This routine is called whenever a serial port is opened. It
1816 * performs the serial-specific initialization for the tty structure.
1817 */
1818int cy_open(struct tty_struct *tty, struct file *filp)
1819{
1820 struct cyclades_port *info;
1821 int retval, line;
1822
1823/* CP('O'); */
1824 line = tty->index;
1825 if ((line < 0) || (NR_PORTS <= line)) {
1826 return -ENODEV;
1827 }
1828 info = &cy_port[line];
1829 if (info->line < 0) {
1830 return -ENODEV;
1831 }
1832#ifdef SERIAL_DEBUG_OTHER
1833 printk("cy_open %s\n", tty->name); /* */
1834#endif
1835 if (serial_paranoia_check(info, tty->name, "cy_open")) {
1836 return -ENODEV;
1837 }
1838#ifdef SERIAL_DEBUG_OPEN
1839 printk("cy_open %s, count = %d\n", tty->name, info->count);
1840 /**/
1841#endif
1842 info->count++;
1843#ifdef SERIAL_DEBUG_COUNT
1844 printk("cyc: %d: incrementing count to %d\n", __LINE__, info->count);
1845#endif
1846 tty->driver_data = info;
1847 info->tty = tty;
1848
1849 /*
1850 * Start up serial port
1851 */
1852 retval = startup(info);
1853 if (retval) {
1854 return retval;
1855 }
1856
1857 retval = block_til_ready(tty, filp, info);
1858 if (retval) {
1859#ifdef SERIAL_DEBUG_OPEN
1860 printk("cy_open returning after block_til_ready with %d\n",
1861 retval);
1862#endif
1863 return retval;
1864 }
1865#ifdef SERIAL_DEBUG_OPEN
1866 printk("cy_open done\n");
1867 /**/
1868#endif
1869 return 0;
1870} /* cy_open */
1871
1872/*
1873 * ---------------------------------------------------------------------
1874 * serial167_init() and friends
1875 *
1876 * serial167_init() is called at boot-time to initialize the serial driver.
1877 * ---------------------------------------------------------------------
1878 */
1879
1880/*
1881 * This routine prints out the appropriate serial driver version
1882 * number, and identifies which options were configured into this
1883 * driver.
1884 */
1885static void show_version(void)
1886{
1887 printk("MVME166/167 cd2401 driver\n");
1888} /* show_version */
1889
1890/* initialize chips on card -- return number of valid
1891 chips (which is number of ports/4) */
1892
1893/*
1894 * This initialises the hardware to a reasonable state. It should
1895 * probe the chip first so as to copy 166-Bug setup as a default for
1896 * port 0. It initialises CMR to CyASYNC; that is never done again, so
1897 * as to limit the number of CyINIT_CHAN commands in normal running.
1898 *
1899 * ... I wonder what I should do if this fails ...
1900 */
1901
1902void mvme167_serial_console_setup(int cflag)
1903{
1904 volatile unsigned char *base_addr = (u_char *) BASE_ADDR;
1905 int ch;
1906 u_char spd;
1907 u_char rcor, rbpr, badspeed = 0;
1908 unsigned long flags;
1909
1910 local_irq_save(flags);
1911
1912 /*
1913 * First probe channel zero of the chip, to see what speed has
1914 * been selected.
1915 */
1916
1917 base_addr[CyCAR] = 0;
1918
1919 rcor = base_addr[CyRCOR] << 5;
1920 rbpr = base_addr[CyRBPR];
1921
1922 for (spd = 0; spd < sizeof(baud_bpr); spd++)
1923 if (rbpr == baud_bpr[spd] && rcor == baud_co[spd])
1924 break;
1925 if (spd >= sizeof(baud_bpr)) {
1926 spd = 14; /* 19200 */
1927 badspeed = 1; /* Failed to identify speed */
1928 }
1929 initial_console_speed = spd;
1930
1931 /* OK, we have chosen a speed, now reset and reinitialise */
1932
1933 my_udelay(20000L); /* Allow time for any active o/p to complete */
1934 if (base_addr[CyCCR] != 0x00) {
1935 local_irq_restore(flags);
1936 /* printk(" chip is never idle (CCR != 0)\n"); */
1937 return;
1938 }
1939
1940 base_addr[CyCCR] = CyCHIP_RESET; /* Reset the chip */
1941 my_udelay(1000L);
1942
1943 if (base_addr[CyGFRCR] == 0x00) {
1944 local_irq_restore(flags);
1945 /* printk(" chip is not responding (GFRCR stayed 0)\n"); */
1946 return;
1947 }
1948
1949 /*
1950 * System clock is 20Mhz, divided by 2048, so divide by 10 for a 1.0ms
1951 * tick
1952 */
1953
1954 base_addr[CyTPR] = 10;
1955
1956 base_addr[CyPILR1] = 0x01; /* Interrupt level for modem change */
1957 base_addr[CyPILR2] = 0x02; /* Interrupt level for tx ints */
1958 base_addr[CyPILR3] = 0x03; /* Interrupt level for rx ints */
1959
1960 /*
1961 * Attempt to set up all channels to something reasonable, and
1962 * bang out a INIT_CHAN command. We should then be able to limit
1963 * the amount of fiddling we have to do in normal running.
1964 */
1965
1966 for (ch = 3; ch >= 0; ch--) {
1967 base_addr[CyCAR] = (u_char) ch;
1968 base_addr[CyIER] = 0;
1969 base_addr[CyCMR] = CyASYNC;
1970 base_addr[CyLICR] = (u_char) ch << 2;
1971 base_addr[CyLIVR] = 0x5c;
1972 base_addr[CyTCOR] = baud_co[spd];
1973 base_addr[CyTBPR] = baud_bpr[spd];
1974 base_addr[CyRCOR] = baud_co[spd] >> 5;
1975 base_addr[CyRBPR] = baud_bpr[spd];
1976 base_addr[CySCHR1] = 'Q' & 0x1f;
1977 base_addr[CySCHR2] = 'X' & 0x1f;
1978 base_addr[CySCRL] = 0;
1979 base_addr[CySCRH] = 0;
1980 base_addr[CyCOR1] = Cy_8_BITS | CyPARITY_NONE;
1981 base_addr[CyCOR2] = 0;
1982 base_addr[CyCOR3] = Cy_1_STOP;
1983 base_addr[CyCOR4] = baud_cor4[spd];
1984 base_addr[CyCOR5] = 0;
1985 base_addr[CyCOR6] = 0;
1986 base_addr[CyCOR7] = 0;
1987 base_addr[CyRTPRL] = 2;
1988 base_addr[CyRTPRH] = 0;
1989 base_addr[CyMSVR1] = 0;
1990 base_addr[CyMSVR2] = 0;
1991 write_cy_cmd(base_addr, CyINIT_CHAN | CyDIS_RCVR | CyDIS_XMTR);
1992 }
1993
1994 /*
1995 * Now do specials for channel zero....
1996 */
1997
1998 base_addr[CyMSVR1] = CyRTS;
1999 base_addr[CyMSVR2] = CyDTR;
2000 base_addr[CyIER] = CyRxData;
2001 write_cy_cmd(base_addr, CyENB_RCVR | CyENB_XMTR);
2002
2003 local_irq_restore(flags);
2004
2005 my_udelay(20000L); /* Let it all settle down */
2006
2007 printk("CD2401 initialised, chip is rev 0x%02x\n", base_addr[CyGFRCR]);
2008 if (badspeed)
2009 printk
2010 (" WARNING: Failed to identify line speed, rcor=%02x,rbpr=%02x\n",
2011 rcor >> 5, rbpr);
2012} /* serial_console_init */
2013
2014static const struct tty_operations cy_ops = {
2015 .open = cy_open,
2016 .close = cy_close,
2017 .write = cy_write,
2018 .put_char = cy_put_char,
2019 .flush_chars = cy_flush_chars,
2020 .write_room = cy_write_room,
2021 .chars_in_buffer = cy_chars_in_buffer,
2022 .flush_buffer = cy_flush_buffer,
2023 .ioctl = cy_ioctl,
2024 .throttle = cy_throttle,
2025 .unthrottle = cy_unthrottle,
2026 .set_termios = cy_set_termios,
2027 .stop = cy_stop,
2028 .start = cy_start,
2029 .hangup = cy_hangup,
2030 .tiocmget = cy_tiocmget,
2031 .tiocmset = cy_tiocmset,
2032};
2033
2034/* The serial driver boot-time initialization code!
2035 Hardware I/O ports are mapped to character special devices on a
2036 first found, first allocated manner. That is, this code searches
2037 for Cyclom cards in the system. As each is found, it is probed
2038 to discover how many chips (and thus how many ports) are present.
2039 These ports are mapped to the tty ports 64 and upward in monotonic
2040 fashion. If an 8-port card is replaced with a 16-port card, the
2041 port mapping on a following card will shift.
2042
2043 This approach is different from what is used in the other serial
2044 device driver because the Cyclom is more properly a multiplexer,
2045 not just an aggregation of serial ports on one card.
2046
2047 If there are more cards with more ports than have been statically
2048 allocated above, a warning is printed and the extra ports are ignored.
2049 */
2050static int __init serial167_init(void)
2051{
2052 struct cyclades_port *info;
2053 int ret = 0;
2054 int good_ports = 0;
2055 int port_num = 0;
2056 int index;
2057 int DefSpeed;
2058#ifdef notyet
2059 struct sigaction sa;
2060#endif
2061
2062 if (!(mvme16x_config & MVME16x_CONFIG_GOT_CD2401))
2063 return 0;
2064
2065 cy_serial_driver = alloc_tty_driver(NR_PORTS);
2066 if (!cy_serial_driver)
2067 return -ENOMEM;
2068
2069#if 0
2070 scrn[1] = '\0';
2071#endif
2072
2073 show_version();
2074
2075 /* Has "console=0,9600n8" been used in bootinfo to change speed? */
2076 if (serial_console_cflag)
2077 DefSpeed = serial_console_cflag & 0017;
2078 else {
2079 DefSpeed = initial_console_speed;
2080 serial_console_info = &cy_port[0];
2081 serial_console_cflag = DefSpeed | CS8;
2082#if 0
2083 serial_console = 64; /*callout_driver.minor_start */
2084#endif
2085 }
2086
2087 /* Initialize the tty_driver structure */
2088
2089 cy_serial_driver->owner = THIS_MODULE;
2090 cy_serial_driver->name = "ttyS";
2091 cy_serial_driver->major = TTY_MAJOR;
2092 cy_serial_driver->minor_start = 64;
2093 cy_serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
2094 cy_serial_driver->subtype = SERIAL_TYPE_NORMAL;
2095 cy_serial_driver->init_termios = tty_std_termios;
2096 cy_serial_driver->init_termios.c_cflag =
2097 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2098 cy_serial_driver->flags = TTY_DRIVER_REAL_RAW;
2099 tty_set_operations(cy_serial_driver, &cy_ops);
2100
2101 ret = tty_register_driver(cy_serial_driver);
2102 if (ret) {
2103 printk(KERN_ERR "Couldn't register MVME166/7 serial driver\n");
2104 put_tty_driver(cy_serial_driver);
2105 return ret;
2106 }
2107
2108 port_num = 0;
2109 info = cy_port;
2110 for (index = 0; index < 1; index++) {
2111
2112 good_ports = 4;
2113
2114 if (port_num < NR_PORTS) {
2115 while (good_ports-- && port_num < NR_PORTS) {
2116 /*** initialize port ***/
2117 info->magic = CYCLADES_MAGIC;
2118 info->type = PORT_CIRRUS;
2119 info->card = index;
2120 info->line = port_num;
2121 info->flags = STD_COM_FLAGS;
2122 info->tty = NULL;
2123 info->xmit_fifo_size = 12;
2124 info->cor1 = CyPARITY_NONE | Cy_8_BITS;
2125 info->cor2 = CyETC;
2126 info->cor3 = Cy_1_STOP;
2127 info->cor4 = 0x08; /* _very_ small receive threshold */
2128 info->cor5 = 0;
2129 info->cor6 = 0;
2130 info->cor7 = 0;
2131 info->tbpr = baud_bpr[DefSpeed]; /* Tx BPR */
2132 info->tco = baud_co[DefSpeed]; /* Tx CO */
2133 info->rbpr = baud_bpr[DefSpeed]; /* Rx BPR */
2134 info->rco = baud_co[DefSpeed] >> 5; /* Rx CO */
2135 info->close_delay = 0;
2136 info->x_char = 0;
2137 info->count = 0;
2138#ifdef SERIAL_DEBUG_COUNT
2139 printk("cyc: %d: setting count to 0\n",
2140 __LINE__);
2141#endif
2142 info->blocked_open = 0;
2143 info->default_threshold = 0;
2144 info->default_timeout = 0;
2145 init_waitqueue_head(&info->open_wait);
2146 init_waitqueue_head(&info->close_wait);
2147 /* info->session */
2148 /* info->pgrp */
2149/*** !!!!!!!! this may expose new bugs !!!!!!!!! *********/
2150 info->read_status_mask =
2151 CyTIMEOUT | CySPECHAR | CyBREAK | CyPARITY |
2152 CyFRAME | CyOVERRUN;
2153 /* info->timeout */
2154
2155 printk("ttyS%d ", info->line);
2156 port_num++;
2157 info++;
2158 if (!(port_num & 7)) {
2159 printk("\n ");
2160 }
2161 }
2162 }
2163 printk("\n");
2164 }
2165 while (port_num < NR_PORTS) {
2166 info->line = -1;
2167 port_num++;
2168 info++;
2169 }
2170
2171 ret = request_irq(MVME167_IRQ_SER_ERR, cd2401_rxerr_interrupt, 0,
2172 "cd2401_errors", cd2401_rxerr_interrupt);
2173 if (ret) {
2174 printk(KERN_ERR "Could't get cd2401_errors IRQ");
2175 goto cleanup_serial_driver;
2176 }
2177
2178 ret = request_irq(MVME167_IRQ_SER_MODEM, cd2401_modem_interrupt, 0,
2179 "cd2401_modem", cd2401_modem_interrupt);
2180 if (ret) {
2181 printk(KERN_ERR "Could't get cd2401_modem IRQ");
2182 goto cleanup_irq_cd2401_errors;
2183 }
2184
2185 ret = request_irq(MVME167_IRQ_SER_TX, cd2401_tx_interrupt, 0,
2186 "cd2401_txints", cd2401_tx_interrupt);
2187 if (ret) {
2188 printk(KERN_ERR "Could't get cd2401_txints IRQ");
2189 goto cleanup_irq_cd2401_modem;
2190 }
2191
2192 ret = request_irq(MVME167_IRQ_SER_RX, cd2401_rx_interrupt, 0,
2193 "cd2401_rxints", cd2401_rx_interrupt);
2194 if (ret) {
2195 printk(KERN_ERR "Could't get cd2401_rxints IRQ");
2196 goto cleanup_irq_cd2401_txints;
2197 }
2198
2199 /* Now we have registered the interrupt handlers, allow the interrupts */
2200
2201 pcc2chip[PccSCCMICR] = 0x15; /* Serial ints are level 5 */
2202 pcc2chip[PccSCCTICR] = 0x15;
2203 pcc2chip[PccSCCRICR] = 0x15;
2204
2205 pcc2chip[PccIMLR] = 3; /* Allow PCC2 ints above 3!? */
2206
2207 return 0;
2208cleanup_irq_cd2401_txints:
2209 free_irq(MVME167_IRQ_SER_TX, cd2401_tx_interrupt);
2210cleanup_irq_cd2401_modem:
2211 free_irq(MVME167_IRQ_SER_MODEM, cd2401_modem_interrupt);
2212cleanup_irq_cd2401_errors:
2213 free_irq(MVME167_IRQ_SER_ERR, cd2401_rxerr_interrupt);
2214cleanup_serial_driver:
2215 if (tty_unregister_driver(cy_serial_driver))
2216 printk(KERN_ERR
2217 "Couldn't unregister MVME166/7 serial driver\n");
2218 put_tty_driver(cy_serial_driver);
2219 return ret;
2220} /* serial167_init */
2221
2222module_init(serial167_init);
2223
2224#ifdef CYCLOM_SHOW_STATUS
2225static void show_status(int line_num)
2226{
2227 volatile unsigned char *base_addr = (u_char *) BASE_ADDR;
2228 int channel;
2229 struct cyclades_port *info;
2230 unsigned long flags;
2231
2232 info = &cy_port[line_num];
2233 channel = info->line;
2234 printk(" channel %d\n", channel);
2235 /**/ printk(" cy_port\n");
2236 printk(" card line flags = %d %d %x\n",
2237 info->card, info->line, info->flags);
2238 printk
2239 (" *tty read_status_mask timeout xmit_fifo_size = %lx %x %x %x\n",
2240 (long)info->tty, info->read_status_mask, info->timeout,
2241 info->xmit_fifo_size);
2242 printk(" cor1,cor2,cor3,cor4,cor5,cor6,cor7 = %x %x %x %x %x %x %x\n",
2243 info->cor1, info->cor2, info->cor3, info->cor4, info->cor5,
2244 info->cor6, info->cor7);
2245 printk(" tbpr,tco,rbpr,rco = %d %d %d %d\n", info->tbpr, info->tco,
2246 info->rbpr, info->rco);
2247 printk(" close_delay event count = %d %d %d\n", info->close_delay,
2248 info->event, info->count);
2249 printk(" x_char blocked_open = %x %x\n", info->x_char,
2250 info->blocked_open);
2251 printk(" open_wait = %lx %lx %lx\n", (long)info->open_wait);
2252
2253 local_irq_save(flags);
2254
2255/* Global Registers */
2256
2257 printk(" CyGFRCR %x\n", base_addr[CyGFRCR]);
2258 printk(" CyCAR %x\n", base_addr[CyCAR]);
2259 printk(" CyRISR %x\n", base_addr[CyRISR]);
2260 printk(" CyTISR %x\n", base_addr[CyTISR]);
2261 printk(" CyMISR %x\n", base_addr[CyMISR]);
2262 printk(" CyRIR %x\n", base_addr[CyRIR]);
2263 printk(" CyTIR %x\n", base_addr[CyTIR]);
2264 printk(" CyMIR %x\n", base_addr[CyMIR]);
2265 printk(" CyTPR %x\n", base_addr[CyTPR]);
2266
2267 base_addr[CyCAR] = (u_char) channel;
2268
2269/* Virtual Registers */
2270
2271#if 0
2272 printk(" CyRIVR %x\n", base_addr[CyRIVR]);
2273 printk(" CyTIVR %x\n", base_addr[CyTIVR]);
2274 printk(" CyMIVR %x\n", base_addr[CyMIVR]);
2275 printk(" CyMISR %x\n", base_addr[CyMISR]);
2276#endif
2277
2278/* Channel Registers */
2279
2280 printk(" CyCCR %x\n", base_addr[CyCCR]);
2281 printk(" CyIER %x\n", base_addr[CyIER]);
2282 printk(" CyCOR1 %x\n", base_addr[CyCOR1]);
2283 printk(" CyCOR2 %x\n", base_addr[CyCOR2]);
2284 printk(" CyCOR3 %x\n", base_addr[CyCOR3]);
2285 printk(" CyCOR4 %x\n", base_addr[CyCOR4]);
2286 printk(" CyCOR5 %x\n", base_addr[CyCOR5]);
2287#if 0
2288 printk(" CyCCSR %x\n", base_addr[CyCCSR]);
2289 printk(" CyRDCR %x\n", base_addr[CyRDCR]);
2290#endif
2291 printk(" CySCHR1 %x\n", base_addr[CySCHR1]);
2292 printk(" CySCHR2 %x\n", base_addr[CySCHR2]);
2293#if 0
2294 printk(" CySCHR3 %x\n", base_addr[CySCHR3]);
2295 printk(" CySCHR4 %x\n", base_addr[CySCHR4]);
2296 printk(" CySCRL %x\n", base_addr[CySCRL]);
2297 printk(" CySCRH %x\n", base_addr[CySCRH]);
2298 printk(" CyLNC %x\n", base_addr[CyLNC]);
2299 printk(" CyMCOR1 %x\n", base_addr[CyMCOR1]);
2300 printk(" CyMCOR2 %x\n", base_addr[CyMCOR2]);
2301#endif
2302 printk(" CyRTPRL %x\n", base_addr[CyRTPRL]);
2303 printk(" CyRTPRH %x\n", base_addr[CyRTPRH]);
2304 printk(" CyMSVR1 %x\n", base_addr[CyMSVR1]);
2305 printk(" CyMSVR2 %x\n", base_addr[CyMSVR2]);
2306 printk(" CyRBPR %x\n", base_addr[CyRBPR]);
2307 printk(" CyRCOR %x\n", base_addr[CyRCOR]);
2308 printk(" CyTBPR %x\n", base_addr[CyTBPR]);
2309 printk(" CyTCOR %x\n", base_addr[CyTCOR]);
2310
2311 local_irq_restore(flags);
2312} /* show_status */
2313#endif
2314
2315#if 0
2316/* Dummy routine in mvme16x/config.c for now */
2317
2318/* Serial console setup. Called from linux/init/main.c */
2319
2320void console_setup(char *str, int *ints)
2321{
2322 char *s;
2323 int baud, bits, parity;
2324 int cflag = 0;
2325
2326 /* Sanity check. */
2327 if (ints[0] > 3 || ints[1] > 3)
2328 return;
2329
2330 /* Get baud, bits and parity */
2331 baud = 2400;
2332 bits = 8;
2333 parity = 'n';
2334 if (ints[2])
2335 baud = ints[2];
2336 if ((s = strchr(str, ','))) {
2337 do {
2338 s++;
2339 } while (*s >= '0' && *s <= '9');
2340 if (*s)
2341 parity = *s++;
2342 if (*s)
2343 bits = *s - '0';
2344 }
2345
2346 /* Now construct a cflag setting. */
2347 switch (baud) {
2348 case 1200:
2349 cflag |= B1200;
2350 break;
2351 case 9600:
2352 cflag |= B9600;
2353 break;
2354 case 19200:
2355 cflag |= B19200;
2356 break;
2357 case 38400:
2358 cflag |= B38400;
2359 break;
2360 case 2400:
2361 default:
2362 cflag |= B2400;
2363 break;
2364 }
2365 switch (bits) {
2366 case 7:
2367 cflag |= CS7;
2368 break;
2369 default:
2370 case 8:
2371 cflag |= CS8;
2372 break;
2373 }
2374 switch (parity) {
2375 case 'o':
2376 case 'O':
2377 cflag |= PARODD;
2378 break;
2379 case 'e':
2380 case 'E':
2381 cflag |= PARENB;
2382 break;
2383 }
2384
2385 serial_console_info = &cy_port[ints[1]];
2386 serial_console_cflag = cflag;
2387 serial_console = ints[1] + 64; /*callout_driver.minor_start */
2388}
2389#endif
2390
2391/*
2392 * The following is probably out of date for 2.1.x serial console stuff.
2393 *
2394 * The console is registered early on from arch/m68k/kernel/setup.c, and
2395 * it therefore relies on the chip being setup correctly by 166-Bug. This
2396 * seems reasonable, as the serial port has been used to invoke the system
2397 * boot. It also means that this function must not rely on any data
2398 * initialisation performed by serial167_init() etc.
2399 *
2400 * Of course, once the console has been registered, we had better ensure
2401 * that serial167_init() doesn't leave the chip non-functional.
2402 *
2403 * The console must be locked when we get here.
2404 */
2405
2406void serial167_console_write(struct console *co, const char *str,
2407 unsigned count)
2408{
2409 volatile unsigned char *base_addr = (u_char *) BASE_ADDR;
2410 unsigned long flags;
2411 volatile u_char sink;
2412 u_char ier;
2413 int port;
2414 u_char do_lf = 0;
2415 int i = 0;
2416
2417 local_irq_save(flags);
2418
2419 /* Ensure transmitter is enabled! */
2420
2421 port = 0;
2422 base_addr[CyCAR] = (u_char) port;
2423 while (base_addr[CyCCR])
2424 ;
2425 base_addr[CyCCR] = CyENB_XMTR;
2426
2427 ier = base_addr[CyIER];
2428 base_addr[CyIER] = CyTxMpty;
2429
2430 while (1) {
2431 if (pcc2chip[PccSCCTICR] & 0x20) {
2432 /* We have a Tx int. Acknowledge it */
2433 sink = pcc2chip[PccTPIACKR];
2434 if ((base_addr[CyLICR] >> 2) == port) {
2435 if (i == count) {
2436 /* Last char of string is now output */
2437 base_addr[CyTEOIR] = CyNOTRANS;
2438 break;
2439 }
2440 if (do_lf) {
2441 base_addr[CyTDR] = '\n';
2442 str++;
2443 i++;
2444 do_lf = 0;
2445 } else if (*str == '\n') {
2446 base_addr[CyTDR] = '\r';
2447 do_lf = 1;
2448 } else {
2449 base_addr[CyTDR] = *str++;
2450 i++;
2451 }
2452 base_addr[CyTEOIR] = 0;
2453 } else
2454 base_addr[CyTEOIR] = CyNOTRANS;
2455 }
2456 }
2457
2458 base_addr[CyIER] = ier;
2459
2460 local_irq_restore(flags);
2461}
2462
2463static struct tty_driver *serial167_console_device(struct console *c,
2464 int *index)
2465{
2466 *index = c->index;
2467 return cy_serial_driver;
2468}
2469
2470static struct console sercons = {
2471 .name = "ttyS",
2472 .write = serial167_console_write,
2473 .device = serial167_console_device,
2474 .flags = CON_PRINTBUFFER,
2475 .index = -1,
2476};
2477
2478static int __init serial167_console_init(void)
2479{
2480 if (vme_brdtype == VME_TYPE_MVME166 ||
2481 vme_brdtype == VME_TYPE_MVME167 ||
2482 vme_brdtype == VME_TYPE_MVME177) {
2483 mvme167_serial_console_setup(0);
2484 register_console(&sercons);
2485 }
2486 return 0;
2487}
2488
2489console_initcall(serial167_console_init);
2490
2491MODULE_LICENSE("GPL");
diff --git a/drivers/char/snsc.c b/drivers/char/snsc.c
index 32b74de18f5f..5816b39ff5a9 100644
--- a/drivers/char/snsc.c
+++ b/drivers/char/snsc.c
@@ -21,7 +21,7 @@
21#include <linux/poll.h> 21#include <linux/poll.h>
22#include <linux/module.h> 22#include <linux/module.h>
23#include <linux/slab.h> 23#include <linux/slab.h>
24#include <linux/smp_lock.h> 24#include <linux/mutex.h>
25#include <asm/sn/io.h> 25#include <asm/sn/io.h>
26#include <asm/sn/sn_sal.h> 26#include <asm/sn/sn_sal.h>
27#include <asm/sn/module.h> 27#include <asm/sn/module.h>
@@ -34,6 +34,7 @@
34#define SCDRV_BUFSZ 2048 34#define SCDRV_BUFSZ 2048
35#define SCDRV_TIMEOUT 1000 35#define SCDRV_TIMEOUT 1000
36 36
37static DEFINE_MUTEX(scdrv_mutex);
37static irqreturn_t 38static irqreturn_t
38scdrv_interrupt(int irq, void *subch_data) 39scdrv_interrupt(int irq, void *subch_data)
39{ 40{
@@ -105,7 +106,7 @@ scdrv_open(struct inode *inode, struct file *file)
105 file->private_data = sd; 106 file->private_data = sd;
106 107
107 /* hook this subchannel up to the system controller interrupt */ 108 /* hook this subchannel up to the system controller interrupt */
108 lock_kernel(); 109 mutex_lock(&scdrv_mutex);
109 rv = request_irq(SGI_UART_VECTOR, scdrv_interrupt, 110 rv = request_irq(SGI_UART_VECTOR, scdrv_interrupt,
110 IRQF_SHARED | IRQF_DISABLED, 111 IRQF_SHARED | IRQF_DISABLED,
111 SYSCTL_BASENAME, sd); 112 SYSCTL_BASENAME, sd);
@@ -113,10 +114,10 @@ scdrv_open(struct inode *inode, struct file *file)
113 ia64_sn_irtr_close(sd->sd_nasid, sd->sd_subch); 114 ia64_sn_irtr_close(sd->sd_nasid, sd->sd_subch);
114 kfree(sd); 115 kfree(sd);
115 printk("%s: irq request failed (%d)\n", __func__, rv); 116 printk("%s: irq request failed (%d)\n", __func__, rv);
116 unlock_kernel(); 117 mutex_unlock(&scdrv_mutex);
117 return -EBUSY; 118 return -EBUSY;
118 } 119 }
119 unlock_kernel(); 120 mutex_unlock(&scdrv_mutex);
120 return 0; 121 return 0;
121} 122}
122 123
@@ -357,6 +358,7 @@ static const struct file_operations scdrv_fops = {
357 .poll = scdrv_poll, 358 .poll = scdrv_poll,
358 .open = scdrv_open, 359 .open = scdrv_open,
359 .release = scdrv_release, 360 .release = scdrv_release,
361 .llseek = noop_llseek,
360}; 362};
361 363
362static struct class *snsc_class; 364static struct class *snsc_class;
diff --git a/drivers/char/snsc.h b/drivers/char/snsc.h
index 4be62eda9fbc..e8c52c882b21 100644
--- a/drivers/char/snsc.h
+++ b/drivers/char/snsc.h
@@ -19,7 +19,6 @@
19#include <linux/types.h> 19#include <linux/types.h>
20#include <linux/spinlock.h> 20#include <linux/spinlock.h>
21#include <linux/wait.h> 21#include <linux/wait.h>
22#include <linux/kobject.h>
23#include <linux/fs.h> 22#include <linux/fs.h>
24#include <linux/cdev.h> 23#include <linux/cdev.h>
25#include <linux/semaphore.h> 24#include <linux/semaphore.h>
diff --git a/drivers/char/sonypi.c b/drivers/char/sonypi.c
index 73f66d03624d..1ee8ce7d2762 100644
--- a/drivers/char/sonypi.c
+++ b/drivers/char/sonypi.c
@@ -1241,7 +1241,7 @@ static int __devinit sonypi_setup_ioports(struct sonypi_device *dev,
1241 while (check_ioport && check->port1) { 1241 while (check_ioport && check->port1) {
1242 if (!request_region(check->port1, 1242 if (!request_region(check->port1,
1243 sonypi_device.region_size, 1243 sonypi_device.region_size,
1244 "Sony Programable I/O Device Check")) { 1244 "Sony Programmable I/O Device Check")) {
1245 printk(KERN_ERR "sonypi: ioport 0x%.4x busy, using sony-laptop? " 1245 printk(KERN_ERR "sonypi: ioport 0x%.4x busy, using sony-laptop? "
1246 "if not use check_ioport=0\n", 1246 "if not use check_ioport=0\n",
1247 check->port1); 1247 check->port1);
@@ -1255,7 +1255,7 @@ static int __devinit sonypi_setup_ioports(struct sonypi_device *dev,
1255 1255
1256 if (request_region(ioport_list->port1, 1256 if (request_region(ioport_list->port1,
1257 sonypi_device.region_size, 1257 sonypi_device.region_size,
1258 "Sony Programable I/O Device")) { 1258 "Sony Programmable I/O Device")) {
1259 dev->ioport1 = ioport_list->port1; 1259 dev->ioport1 = ioport_list->port1;
1260 dev->ioport2 = ioport_list->port2; 1260 dev->ioport2 = ioport_list->port2;
1261 return 0; 1261 return 0;
@@ -1434,7 +1434,7 @@ static int __devexit sonypi_remove(struct platform_device *dev)
1434 sonypi_disable(); 1434 sonypi_disable();
1435 1435
1436 synchronize_irq(sonypi_device.irq); 1436 synchronize_irq(sonypi_device.irq);
1437 flush_scheduled_work(); 1437 flush_work_sync(&sonypi_device.input_work);
1438 1438
1439 if (useinput) { 1439 if (useinput) {
1440 input_unregister_device(sonypi_device.input_key_dev); 1440 input_unregister_device(sonypi_device.input_key_dev);
diff --git a/drivers/char/specialix.c b/drivers/char/specialix.c
deleted file mode 100644
index 9f8495b4fc8f..000000000000
--- a/drivers/char/specialix.c
+++ /dev/null
@@ -1,2369 +0,0 @@
1/*
2 * specialix.c -- specialix IO8+ multiport serial driver.
3 *
4 * Copyright (C) 1997 Roger Wolff (R.E.Wolff@BitWizard.nl)
5 * Copyright (C) 1994-1996 Dmitry Gorodchanin (pgmdsg@ibi.com)
6 *
7 * Specialix pays for the development and support of this driver.
8 * Please DO contact io8-linux@specialix.co.uk if you require
9 * support. But please read the documentation (specialix.txt)
10 * first.
11 *
12 * This driver was developped in the BitWizard linux device
13 * driver service. If you require a linux device driver for your
14 * product, please contact devices@BitWizard.nl for a quote.
15 *
16 * This code is firmly based on the riscom/8 serial driver,
17 * written by Dmitry Gorodchanin. The specialix IO8+ card
18 * programming information was obtained from the CL-CD1865 Data
19 * Book, and Specialix document number 6200059: IO8+ Hardware
20 * Functional Specification.
21 *
22 * This program is free software; you can redistribute it and/or
23 * modify it under the terms of the GNU General Public License as
24 * published by the Free Software Foundation; either version 2 of
25 * the License, or (at your option) any later version.
26 *
27 * This program is distributed in the hope that it will be
28 * useful, but WITHOUT ANY WARRANTY; without even the implied
29 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
30 * PURPOSE. See the GNU General Public License for more details.
31 *
32 * You should have received a copy of the GNU General Public
33 * License along with this program; if not, write to the Free
34 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,
35 * USA.
36 *
37 * Revision history:
38 *
39 * Revision 1.0: April 1st 1997.
40 * Initial release for alpha testing.
41 * Revision 1.1: April 14th 1997.
42 * Incorporated Richard Hudsons suggestions,
43 * removed some debugging printk's.
44 * Revision 1.2: April 15th 1997.
45 * Ported to 2.1.x kernels.
46 * Revision 1.3: April 17th 1997
47 * Backported to 2.0. (Compatibility macros).
48 * Revision 1.4: April 18th 1997
49 * Fixed DTR/RTS bug that caused the card to indicate
50 * "don't send data" to a modem after the password prompt.
51 * Fixed bug for premature (fake) interrupts.
52 * Revision 1.5: April 19th 1997
53 * fixed a minor typo in the header file, cleanup a little.
54 * performance warnings are now MAXed at once per minute.
55 * Revision 1.6: May 23 1997
56 * Changed the specialix=... format to include interrupt.
57 * Revision 1.7: May 27 1997
58 * Made many more debug printk's a compile time option.
59 * Revision 1.8: Jul 1 1997
60 * port to linux-2.1.43 kernel.
61 * Revision 1.9: Oct 9 1998
62 * Added stuff for the IO8+/PCI version.
63 * Revision 1.10: Oct 22 1999 / Jan 21 2000.
64 * Added stuff for setserial.
65 * Nicolas Mailhot (Nicolas.Mailhot@email.enst.fr)
66 *
67 */
68
69#define VERSION "1.11"
70
71
72/*
73 * There is a bunch of documentation about the card, jumpers, config
74 * settings, restrictions, cables, device names and numbers in
75 * Documentation/serial/specialix.txt
76 */
77
78#include <linux/module.h>
79
80#include <linux/io.h>
81#include <linux/kernel.h>
82#include <linux/sched.h>
83#include <linux/ioport.h>
84#include <linux/interrupt.h>
85#include <linux/errno.h>
86#include <linux/tty.h>
87#include <linux/tty_flip.h>
88#include <linux/mm.h>
89#include <linux/serial.h>
90#include <linux/smp_lock.h>
91#include <linux/fcntl.h>
92#include <linux/major.h>
93#include <linux/delay.h>
94#include <linux/pci.h>
95#include <linux/init.h>
96#include <linux/uaccess.h>
97#include <linux/gfp.h>
98
99#include "specialix_io8.h"
100#include "cd1865.h"
101
102
103/*
104 This driver can spew a whole lot of debugging output at you. If you
105 need maximum performance, you should disable the DEBUG define. To
106 aid in debugging in the field, I'm leaving the compile-time debug
107 features enabled, and disable them "runtime". That allows me to
108 instruct people with problems to enable debugging without requiring
109 them to recompile...
110*/
111#define DEBUG
112
113static int sx_debug;
114static int sx_rxfifo = SPECIALIX_RXFIFO;
115static int sx_rtscts;
116
117#ifdef DEBUG
118#define dprintk(f, str...) if (sx_debug & f) printk(str)
119#else
120#define dprintk(f, str...) /* nothing */
121#endif
122
123#define SX_DEBUG_FLOW 0x0001
124#define SX_DEBUG_DATA 0x0002
125#define SX_DEBUG_PROBE 0x0004
126#define SX_DEBUG_CHAN 0x0008
127#define SX_DEBUG_INIT 0x0010
128#define SX_DEBUG_RX 0x0020
129#define SX_DEBUG_TX 0x0040
130#define SX_DEBUG_IRQ 0x0080
131#define SX_DEBUG_OPEN 0x0100
132#define SX_DEBUG_TERMIOS 0x0200
133#define SX_DEBUG_SIGNALS 0x0400
134#define SX_DEBUG_FIFO 0x0800
135
136
137#define func_enter() dprintk(SX_DEBUG_FLOW, "io8: enter %s\n", __func__)
138#define func_exit() dprintk(SX_DEBUG_FLOW, "io8: exit %s\n", __func__)
139
140
141/* Configurable options: */
142
143/* Am I paranoid or not ? ;-) */
144#define SPECIALIX_PARANOIA_CHECK
145
146/*
147 * The following defines are mostly for testing purposes. But if you need
148 * some nice reporting in your syslog, you can define them also.
149 */
150#undef SX_REPORT_FIFO
151#undef SX_REPORT_OVERRUN
152
153
154
155
156#define SPECIALIX_LEGAL_FLAGS \
157 (ASYNC_HUP_NOTIFY | ASYNC_SAK | ASYNC_SPLIT_TERMIOS | \
158 ASYNC_SPD_HI | ASYNC_SPEED_VHI | ASYNC_SESSION_LOCKOUT | \
159 ASYNC_PGRP_LOCKOUT | ASYNC_CALLOUT_NOHUP)
160
161static struct tty_driver *specialix_driver;
162
163static struct specialix_board sx_board[SX_NBOARD] = {
164 { 0, SX_IOBASE1, 9, },
165 { 0, SX_IOBASE2, 11, },
166 { 0, SX_IOBASE3, 12, },
167 { 0, SX_IOBASE4, 15, },
168};
169
170static struct specialix_port sx_port[SX_NBOARD * SX_NPORT];
171
172
173static int sx_paranoia_check(struct specialix_port const *port,
174 char *name, const char *routine)
175{
176#ifdef SPECIALIX_PARANOIA_CHECK
177 static const char *badmagic = KERN_ERR
178 "sx: Warning: bad specialix port magic number for device %s in %s\n";
179 static const char *badinfo = KERN_ERR
180 "sx: Warning: null specialix port for device %s in %s\n";
181
182 if (!port) {
183 printk(badinfo, name, routine);
184 return 1;
185 }
186 if (port->magic != SPECIALIX_MAGIC) {
187 printk(badmagic, name, routine);
188 return 1;
189 }
190#endif
191 return 0;
192}
193
194
195/*
196 *
197 * Service functions for specialix IO8+ driver.
198 *
199 */
200
201/* Get board number from pointer */
202static inline int board_No(struct specialix_board *bp)
203{
204 return bp - sx_board;
205}
206
207
208/* Get port number from pointer */
209static inline int port_No(struct specialix_port const *port)
210{
211 return SX_PORT(port - sx_port);
212}
213
214
215/* Get pointer to board from pointer to port */
216static inline struct specialix_board *port_Board(
217 struct specialix_port const *port)
218{
219 return &sx_board[SX_BOARD(port - sx_port)];
220}
221
222
223/* Input Byte from CL CD186x register */
224static inline unsigned char sx_in(struct specialix_board *bp,
225 unsigned short reg)
226{
227 bp->reg = reg | 0x80;
228 outb(reg | 0x80, bp->base + SX_ADDR_REG);
229 return inb(bp->base + SX_DATA_REG);
230}
231
232
233/* Output Byte to CL CD186x register */
234static inline void sx_out(struct specialix_board *bp, unsigned short reg,
235 unsigned char val)
236{
237 bp->reg = reg | 0x80;
238 outb(reg | 0x80, bp->base + SX_ADDR_REG);
239 outb(val, bp->base + SX_DATA_REG);
240}
241
242
243/* Input Byte from CL CD186x register */
244static inline unsigned char sx_in_off(struct specialix_board *bp,
245 unsigned short reg)
246{
247 bp->reg = reg;
248 outb(reg, bp->base + SX_ADDR_REG);
249 return inb(bp->base + SX_DATA_REG);
250}
251
252
253/* Output Byte to CL CD186x register */
254static inline void sx_out_off(struct specialix_board *bp,
255 unsigned short reg, unsigned char val)
256{
257 bp->reg = reg;
258 outb(reg, bp->base + SX_ADDR_REG);
259 outb(val, bp->base + SX_DATA_REG);
260}
261
262
263/* Wait for Channel Command Register ready */
264static void sx_wait_CCR(struct specialix_board *bp)
265{
266 unsigned long delay, flags;
267 unsigned char ccr;
268
269 for (delay = SX_CCR_TIMEOUT; delay; delay--) {
270 spin_lock_irqsave(&bp->lock, flags);
271 ccr = sx_in(bp, CD186x_CCR);
272 spin_unlock_irqrestore(&bp->lock, flags);
273 if (!ccr)
274 return;
275 udelay(1);
276 }
277
278 printk(KERN_ERR "sx%d: Timeout waiting for CCR.\n", board_No(bp));
279}
280
281
282/* Wait for Channel Command Register ready */
283static void sx_wait_CCR_off(struct specialix_board *bp)
284{
285 unsigned long delay;
286 unsigned char crr;
287 unsigned long flags;
288
289 for (delay = SX_CCR_TIMEOUT; delay; delay--) {
290 spin_lock_irqsave(&bp->lock, flags);
291 crr = sx_in_off(bp, CD186x_CCR);
292 spin_unlock_irqrestore(&bp->lock, flags);
293 if (!crr)
294 return;
295 udelay(1);
296 }
297
298 printk(KERN_ERR "sx%d: Timeout waiting for CCR.\n", board_No(bp));
299}
300
301
302/*
303 * specialix IO8+ IO range functions.
304 */
305
306static int sx_request_io_range(struct specialix_board *bp)
307{
308 return request_region(bp->base,
309 bp->flags & SX_BOARD_IS_PCI ? SX_PCI_IO_SPACE : SX_IO_SPACE,
310 "specialix IO8+") == NULL;
311}
312
313
314static void sx_release_io_range(struct specialix_board *bp)
315{
316 release_region(bp->base, bp->flags & SX_BOARD_IS_PCI ?
317 SX_PCI_IO_SPACE : SX_IO_SPACE);
318}
319
320
321/* Set the IRQ using the RTS lines that run to the PAL on the board.... */
322static int sx_set_irq(struct specialix_board *bp)
323{
324 int virq;
325 int i;
326 unsigned long flags;
327
328 if (bp->flags & SX_BOARD_IS_PCI)
329 return 1;
330 switch (bp->irq) {
331 /* In the same order as in the docs... */
332 case 15:
333 virq = 0;
334 break;
335 case 12:
336 virq = 1;
337 break;
338 case 11:
339 virq = 2;
340 break;
341 case 9:
342 virq = 3;
343 break;
344 default:printk(KERN_ERR
345 "Speclialix: cannot set irq to %d.\n", bp->irq);
346 return 0;
347 }
348 spin_lock_irqsave(&bp->lock, flags);
349 for (i = 0; i < 2; i++) {
350 sx_out(bp, CD186x_CAR, i);
351 sx_out(bp, CD186x_MSVRTS, ((virq >> i) & 0x1)? MSVR_RTS:0);
352 }
353 spin_unlock_irqrestore(&bp->lock, flags);
354 return 1;
355}
356
357
358/* Reset and setup CD186x chip */
359static int sx_init_CD186x(struct specialix_board *bp)
360{
361 unsigned long flags;
362 int scaler;
363 int rv = 1;
364
365 func_enter();
366 sx_wait_CCR_off(bp); /* Wait for CCR ready */
367 spin_lock_irqsave(&bp->lock, flags);
368 sx_out_off(bp, CD186x_CCR, CCR_HARDRESET); /* Reset CD186x chip */
369 spin_unlock_irqrestore(&bp->lock, flags);
370 msleep(50); /* Delay 0.05 sec */
371 spin_lock_irqsave(&bp->lock, flags);
372 sx_out_off(bp, CD186x_GIVR, SX_ID); /* Set ID for this chip */
373 sx_out_off(bp, CD186x_GICR, 0); /* Clear all bits */
374 sx_out_off(bp, CD186x_PILR1, SX_ACK_MINT); /* Prio for modem intr */
375 sx_out_off(bp, CD186x_PILR2, SX_ACK_TINT); /* Prio for transmitter intr */
376 sx_out_off(bp, CD186x_PILR3, SX_ACK_RINT); /* Prio for receiver intr */
377 /* Set RegAckEn */
378 sx_out_off(bp, CD186x_SRCR, sx_in(bp, CD186x_SRCR) | SRCR_REGACKEN);
379
380 /* Setting up prescaler. We need 4 ticks per 1 ms */
381 scaler = SX_OSCFREQ/SPECIALIX_TPS;
382
383 sx_out_off(bp, CD186x_PPRH, scaler >> 8);
384 sx_out_off(bp, CD186x_PPRL, scaler & 0xff);
385 spin_unlock_irqrestore(&bp->lock, flags);
386
387 if (!sx_set_irq(bp)) {
388 /* Figure out how to pass this along... */
389 printk(KERN_ERR "Cannot set irq to %d.\n", bp->irq);
390 rv = 0;
391 }
392
393 func_exit();
394 return rv;
395}
396
397
398static int read_cross_byte(struct specialix_board *bp, int reg, int bit)
399{
400 int i;
401 int t;
402 unsigned long flags;
403
404 spin_lock_irqsave(&bp->lock, flags);
405 for (i = 0, t = 0; i < 8; i++) {
406 sx_out_off(bp, CD186x_CAR, i);
407 if (sx_in_off(bp, reg) & bit)
408 t |= 1 << i;
409 }
410 spin_unlock_irqrestore(&bp->lock, flags);
411
412 return t;
413}
414
415
416/* Main probing routine, also sets irq. */
417static int sx_probe(struct specialix_board *bp)
418{
419 unsigned char val1, val2;
420 int rev;
421 int chip;
422
423 func_enter();
424
425 if (sx_request_io_range(bp)) {
426 func_exit();
427 return 1;
428 }
429
430 /* Are the I/O ports here ? */
431 sx_out_off(bp, CD186x_PPRL, 0x5a);
432 udelay(1);
433 val1 = sx_in_off(bp, CD186x_PPRL);
434
435 sx_out_off(bp, CD186x_PPRL, 0xa5);
436 udelay(1);
437 val2 = sx_in_off(bp, CD186x_PPRL);
438
439
440 if (val1 != 0x5a || val2 != 0xa5) {
441 printk(KERN_INFO
442 "sx%d: specialix IO8+ Board at 0x%03x not found.\n",
443 board_No(bp), bp->base);
444 sx_release_io_range(bp);
445 func_exit();
446 return 1;
447 }
448
449 /* Check the DSR lines that Specialix uses as board
450 identification */
451 val1 = read_cross_byte(bp, CD186x_MSVR, MSVR_DSR);
452 val2 = read_cross_byte(bp, CD186x_MSVR, MSVR_RTS);
453 dprintk(SX_DEBUG_INIT,
454 "sx%d: DSR lines are: %02x, rts lines are: %02x\n",
455 board_No(bp), val1, val2);
456
457 /* They managed to switch the bit order between the docs and
458 the IO8+ card. The new PCI card now conforms to old docs.
459 They changed the PCI docs to reflect the situation on the
460 old card. */
461 val2 = (bp->flags & SX_BOARD_IS_PCI)?0x4d : 0xb2;
462 if (val1 != val2) {
463 printk(KERN_INFO
464 "sx%d: specialix IO8+ ID %02x at 0x%03x not found (%02x).\n",
465 board_No(bp), val2, bp->base, val1);
466 sx_release_io_range(bp);
467 func_exit();
468 return 1;
469 }
470
471
472 /* Reset CD186x again */
473 if (!sx_init_CD186x(bp)) {
474 sx_release_io_range(bp);
475 func_exit();
476 return 1;
477 }
478
479 sx_request_io_range(bp);
480 bp->flags |= SX_BOARD_PRESENT;
481
482 /* Chip revcode pkgtype
483 GFRCR SRCR bit 7
484 CD180 rev B 0x81 0
485 CD180 rev C 0x82 0
486 CD1864 rev A 0x82 1
487 CD1865 rev A 0x83 1 -- Do not use!!! Does not work.
488 CD1865 rev B 0x84 1
489 -- Thanks to Gwen Wang, Cirrus Logic.
490 */
491
492 switch (sx_in_off(bp, CD186x_GFRCR)) {
493 case 0x82:
494 chip = 1864;
495 rev = 'A';
496 break;
497 case 0x83:
498 chip = 1865;
499 rev = 'A';
500 break;
501 case 0x84:
502 chip = 1865;
503 rev = 'B';
504 break;
505 case 0x85:
506 chip = 1865;
507 rev = 'C';
508 break; /* Does not exist at this time */
509 default:
510 chip = -1;
511 rev = 'x';
512 }
513
514 dprintk(SX_DEBUG_INIT, " GFCR = 0x%02x\n", sx_in_off(bp, CD186x_GFRCR));
515
516 printk(KERN_INFO
517 "sx%d: specialix IO8+ board detected at 0x%03x, IRQ %d, CD%d Rev. %c.\n",
518 board_No(bp), bp->base, bp->irq, chip, rev);
519
520 func_exit();
521 return 0;
522}
523
524/*
525 *
526 * Interrupt processing routines.
527 * */
528
529static struct specialix_port *sx_get_port(struct specialix_board *bp,
530 unsigned char const *what)
531{
532 unsigned char channel;
533 struct specialix_port *port = NULL;
534
535 channel = sx_in(bp, CD186x_GICR) >> GICR_CHAN_OFF;
536 dprintk(SX_DEBUG_CHAN, "channel: %d\n", channel);
537 if (channel < CD186x_NCH) {
538 port = &sx_port[board_No(bp) * SX_NPORT + channel];
539 dprintk(SX_DEBUG_CHAN, "port: %d %p flags: 0x%lx\n",
540 board_No(bp) * SX_NPORT + channel, port,
541 port->port.flags & ASYNC_INITIALIZED);
542
543 if (port->port.flags & ASYNC_INITIALIZED) {
544 dprintk(SX_DEBUG_CHAN, "port: %d %p\n", channel, port);
545 func_exit();
546 return port;
547 }
548 }
549 printk(KERN_INFO "sx%d: %s interrupt from invalid port %d\n",
550 board_No(bp), what, channel);
551 return NULL;
552}
553
554
555static void sx_receive_exc(struct specialix_board *bp)
556{
557 struct specialix_port *port;
558 struct tty_struct *tty;
559 unsigned char status;
560 unsigned char ch, flag;
561
562 func_enter();
563
564 port = sx_get_port(bp, "Receive");
565 if (!port) {
566 dprintk(SX_DEBUG_RX, "Hmm, couldn't find port.\n");
567 func_exit();
568 return;
569 }
570 tty = port->port.tty;
571
572 status = sx_in(bp, CD186x_RCSR);
573
574 dprintk(SX_DEBUG_RX, "status: 0x%x\n", status);
575 if (status & RCSR_OE) {
576 port->overrun++;
577 dprintk(SX_DEBUG_FIFO,
578 "sx%d: port %d: Overrun. Total %ld overruns.\n",
579 board_No(bp), port_No(port), port->overrun);
580 }
581 status &= port->mark_mask;
582
583 /* This flip buffer check needs to be below the reading of the
584 status register to reset the chip's IRQ.... */
585 if (tty_buffer_request_room(tty, 1) == 0) {
586 dprintk(SX_DEBUG_FIFO,
587 "sx%d: port %d: Working around flip buffer overflow.\n",
588 board_No(bp), port_No(port));
589 func_exit();
590 return;
591 }
592
593 ch = sx_in(bp, CD186x_RDR);
594 if (!status) {
595 func_exit();
596 return;
597 }
598 if (status & RCSR_TOUT) {
599 printk(KERN_INFO
600 "sx%d: port %d: Receiver timeout. Hardware problems ?\n",
601 board_No(bp), port_No(port));
602 func_exit();
603 return;
604
605 } else if (status & RCSR_BREAK) {
606 dprintk(SX_DEBUG_RX, "sx%d: port %d: Handling break...\n",
607 board_No(bp), port_No(port));
608 flag = TTY_BREAK;
609 if (port->port.flags & ASYNC_SAK)
610 do_SAK(tty);
611
612 } else if (status & RCSR_PE)
613 flag = TTY_PARITY;
614
615 else if (status & RCSR_FE)
616 flag = TTY_FRAME;
617
618 else if (status & RCSR_OE)
619 flag = TTY_OVERRUN;
620
621 else
622 flag = TTY_NORMAL;
623
624 if (tty_insert_flip_char(tty, ch, flag))
625 tty_flip_buffer_push(tty);
626 func_exit();
627}
628
629
630static void sx_receive(struct specialix_board *bp)
631{
632 struct specialix_port *port;
633 struct tty_struct *tty;
634 unsigned char count;
635
636 func_enter();
637
638 port = sx_get_port(bp, "Receive");
639 if (port == NULL) {
640 dprintk(SX_DEBUG_RX, "Hmm, couldn't find port.\n");
641 func_exit();
642 return;
643 }
644 tty = port->port.tty;
645
646 count = sx_in(bp, CD186x_RDCR);
647 dprintk(SX_DEBUG_RX, "port: %p: count: %d\n", port, count);
648 port->hits[count > 8 ? 9 : count]++;
649
650 while (count--)
651 tty_insert_flip_char(tty, sx_in(bp, CD186x_RDR), TTY_NORMAL);
652 tty_flip_buffer_push(tty);
653 func_exit();
654}
655
656
657static void sx_transmit(struct specialix_board *bp)
658{
659 struct specialix_port *port;
660 struct tty_struct *tty;
661 unsigned char count;
662
663 func_enter();
664 port = sx_get_port(bp, "Transmit");
665 if (port == NULL) {
666 func_exit();
667 return;
668 }
669 dprintk(SX_DEBUG_TX, "port: %p\n", port);
670 tty = port->port.tty;
671
672 if (port->IER & IER_TXEMPTY) {
673 /* FIFO drained */
674 sx_out(bp, CD186x_CAR, port_No(port));
675 port->IER &= ~IER_TXEMPTY;
676 sx_out(bp, CD186x_IER, port->IER);
677 func_exit();
678 return;
679 }
680
681 if ((port->xmit_cnt <= 0 && !port->break_length)
682 || tty->stopped || tty->hw_stopped) {
683 sx_out(bp, CD186x_CAR, port_No(port));
684 port->IER &= ~IER_TXRDY;
685 sx_out(bp, CD186x_IER, port->IER);
686 func_exit();
687 return;
688 }
689
690 if (port->break_length) {
691 if (port->break_length > 0) {
692 if (port->COR2 & COR2_ETC) {
693 sx_out(bp, CD186x_TDR, CD186x_C_ESC);
694 sx_out(bp, CD186x_TDR, CD186x_C_SBRK);
695 port->COR2 &= ~COR2_ETC;
696 }
697 count = min_t(int, port->break_length, 0xff);
698 sx_out(bp, CD186x_TDR, CD186x_C_ESC);
699 sx_out(bp, CD186x_TDR, CD186x_C_DELAY);
700 sx_out(bp, CD186x_TDR, count);
701 port->break_length -= count;
702 if (port->break_length == 0)
703 port->break_length--;
704 } else {
705 sx_out(bp, CD186x_TDR, CD186x_C_ESC);
706 sx_out(bp, CD186x_TDR, CD186x_C_EBRK);
707 sx_out(bp, CD186x_COR2, port->COR2);
708 sx_wait_CCR(bp);
709 sx_out(bp, CD186x_CCR, CCR_CORCHG2);
710 port->break_length = 0;
711 }
712
713 func_exit();
714 return;
715 }
716
717 count = CD186x_NFIFO;
718 do {
719 sx_out(bp, CD186x_TDR, port->xmit_buf[port->xmit_tail++]);
720 port->xmit_tail = port->xmit_tail & (SERIAL_XMIT_SIZE-1);
721 if (--port->xmit_cnt <= 0)
722 break;
723 } while (--count > 0);
724
725 if (port->xmit_cnt <= 0) {
726 sx_out(bp, CD186x_CAR, port_No(port));
727 port->IER &= ~IER_TXRDY;
728 sx_out(bp, CD186x_IER, port->IER);
729 }
730 if (port->xmit_cnt <= port->wakeup_chars)
731 tty_wakeup(tty);
732
733 func_exit();
734}
735
736
737static void sx_check_modem(struct specialix_board *bp)
738{
739 struct specialix_port *port;
740 struct tty_struct *tty;
741 unsigned char mcr;
742 int msvr_cd;
743
744 dprintk(SX_DEBUG_SIGNALS, "Modem intr. ");
745 port = sx_get_port(bp, "Modem");
746 if (port == NULL)
747 return;
748
749 tty = port->port.tty;
750
751 mcr = sx_in(bp, CD186x_MCR);
752
753 if ((mcr & MCR_CDCHG)) {
754 dprintk(SX_DEBUG_SIGNALS, "CD just changed... ");
755 msvr_cd = sx_in(bp, CD186x_MSVR) & MSVR_CD;
756 if (msvr_cd) {
757 dprintk(SX_DEBUG_SIGNALS, "Waking up guys in open.\n");
758 wake_up_interruptible(&port->port.open_wait);
759 } else {
760 dprintk(SX_DEBUG_SIGNALS, "Sending HUP.\n");
761 tty_hangup(tty);
762 }
763 }
764
765#ifdef SPECIALIX_BRAIN_DAMAGED_CTS
766 if (mcr & MCR_CTSCHG) {
767 if (sx_in(bp, CD186x_MSVR) & MSVR_CTS) {
768 tty->hw_stopped = 0;
769 port->IER |= IER_TXRDY;
770 if (port->xmit_cnt <= port->wakeup_chars)
771 tty_wakeup(tty);
772 } else {
773 tty->hw_stopped = 1;
774 port->IER &= ~IER_TXRDY;
775 }
776 sx_out(bp, CD186x_IER, port->IER);
777 }
778 if (mcr & MCR_DSSXHG) {
779 if (sx_in(bp, CD186x_MSVR) & MSVR_DSR) {
780 tty->hw_stopped = 0;
781 port->IER |= IER_TXRDY;
782 if (port->xmit_cnt <= port->wakeup_chars)
783 tty_wakeup(tty);
784 } else {
785 tty->hw_stopped = 1;
786 port->IER &= ~IER_TXRDY;
787 }
788 sx_out(bp, CD186x_IER, port->IER);
789 }
790#endif /* SPECIALIX_BRAIN_DAMAGED_CTS */
791
792 /* Clear change bits */
793 sx_out(bp, CD186x_MCR, 0);
794}
795
796
797/* The main interrupt processing routine */
798static irqreturn_t sx_interrupt(int dummy, void *dev_id)
799{
800 unsigned char status;
801 unsigned char ack;
802 struct specialix_board *bp = dev_id;
803 unsigned long loop = 0;
804 int saved_reg;
805 unsigned long flags;
806
807 func_enter();
808
809 spin_lock_irqsave(&bp->lock, flags);
810
811 dprintk(SX_DEBUG_FLOW, "enter %s port %d room: %ld\n", __func__,
812 port_No(sx_get_port(bp, "INT")),
813 SERIAL_XMIT_SIZE - sx_get_port(bp, "ITN")->xmit_cnt - 1);
814 if (!(bp->flags & SX_BOARD_ACTIVE)) {
815 dprintk(SX_DEBUG_IRQ, "sx: False interrupt. irq %d.\n",
816 bp->irq);
817 spin_unlock_irqrestore(&bp->lock, flags);
818 func_exit();
819 return IRQ_NONE;
820 }
821
822 saved_reg = bp->reg;
823
824 while (++loop < 16) {
825 status = sx_in(bp, CD186x_SRSR) &
826 (SRSR_RREQint | SRSR_TREQint | SRSR_MREQint);
827 if (status == 0)
828 break;
829 if (status & SRSR_RREQint) {
830 ack = sx_in(bp, CD186x_RRAR);
831
832 if (ack == (SX_ID | GIVR_IT_RCV))
833 sx_receive(bp);
834 else if (ack == (SX_ID | GIVR_IT_REXC))
835 sx_receive_exc(bp);
836 else
837 printk(KERN_ERR
838 "sx%d: status: 0x%x Bad receive ack 0x%02x.\n",
839 board_No(bp), status, ack);
840
841 } else if (status & SRSR_TREQint) {
842 ack = sx_in(bp, CD186x_TRAR);
843
844 if (ack == (SX_ID | GIVR_IT_TX))
845 sx_transmit(bp);
846 else
847 printk(KERN_ERR "sx%d: status: 0x%x Bad transmit ack 0x%02x. port: %d\n",
848 board_No(bp), status, ack,
849 port_No(sx_get_port(bp, "Int")));
850 } else if (status & SRSR_MREQint) {
851 ack = sx_in(bp, CD186x_MRAR);
852
853 if (ack == (SX_ID | GIVR_IT_MODEM))
854 sx_check_modem(bp);
855 else
856 printk(KERN_ERR
857 "sx%d: status: 0x%x Bad modem ack 0x%02x.\n",
858 board_No(bp), status, ack);
859
860 }
861
862 sx_out(bp, CD186x_EOIR, 0); /* Mark end of interrupt */
863 }
864 bp->reg = saved_reg;
865 outb(bp->reg, bp->base + SX_ADDR_REG);
866 spin_unlock_irqrestore(&bp->lock, flags);
867 func_exit();
868 return IRQ_HANDLED;
869}
870
871
872/*
873 * Routines for open & close processing.
874 */
875
876static void turn_ints_off(struct specialix_board *bp)
877{
878 unsigned long flags;
879
880 func_enter();
881 spin_lock_irqsave(&bp->lock, flags);
882 (void) sx_in_off(bp, 0); /* Turn off interrupts. */
883 spin_unlock_irqrestore(&bp->lock, flags);
884
885 func_exit();
886}
887
888static void turn_ints_on(struct specialix_board *bp)
889{
890 unsigned long flags;
891
892 func_enter();
893
894 spin_lock_irqsave(&bp->lock, flags);
895 (void) sx_in(bp, 0); /* Turn ON interrupts. */
896 spin_unlock_irqrestore(&bp->lock, flags);
897
898 func_exit();
899}
900
901
902/* Called with disabled interrupts */
903static int sx_setup_board(struct specialix_board *bp)
904{
905 int error;
906
907 if (bp->flags & SX_BOARD_ACTIVE)
908 return 0;
909
910 if (bp->flags & SX_BOARD_IS_PCI)
911 error = request_irq(bp->irq, sx_interrupt,
912 IRQF_DISABLED | IRQF_SHARED, "specialix IO8+", bp);
913 else
914 error = request_irq(bp->irq, sx_interrupt,
915 IRQF_DISABLED, "specialix IO8+", bp);
916
917 if (error)
918 return error;
919
920 turn_ints_on(bp);
921 bp->flags |= SX_BOARD_ACTIVE;
922
923 return 0;
924}
925
926
927/* Called with disabled interrupts */
928static void sx_shutdown_board(struct specialix_board *bp)
929{
930 func_enter();
931
932 if (!(bp->flags & SX_BOARD_ACTIVE)) {
933 func_exit();
934 return;
935 }
936
937 bp->flags &= ~SX_BOARD_ACTIVE;
938
939 dprintk(SX_DEBUG_IRQ, "Freeing IRQ%d for board %d.\n",
940 bp->irq, board_No(bp));
941 free_irq(bp->irq, bp);
942 turn_ints_off(bp);
943 func_exit();
944}
945
946static unsigned int sx_crtscts(struct tty_struct *tty)
947{
948 if (sx_rtscts)
949 return C_CRTSCTS(tty);
950 return 1;
951}
952
953/*
954 * Setting up port characteristics.
955 * Must be called with disabled interrupts
956 */
957static void sx_change_speed(struct specialix_board *bp,
958 struct specialix_port *port)
959{
960 struct tty_struct *tty;
961 unsigned long baud;
962 long tmp;
963 unsigned char cor1 = 0, cor3 = 0;
964 unsigned char mcor1 = 0, mcor2 = 0;
965 static unsigned long again;
966 unsigned long flags;
967
968 func_enter();
969
970 tty = port->port.tty;
971 if (!tty || !tty->termios) {
972 func_exit();
973 return;
974 }
975
976 port->IER = 0;
977 port->COR2 = 0;
978 /* Select port on the board */
979 spin_lock_irqsave(&bp->lock, flags);
980 sx_out(bp, CD186x_CAR, port_No(port));
981
982 /* The Specialix board doens't implement the RTS lines.
983 They are used to set the IRQ level. Don't touch them. */
984 if (sx_crtscts(tty))
985 port->MSVR = MSVR_DTR | (sx_in(bp, CD186x_MSVR) & MSVR_RTS);
986 else
987 port->MSVR = (sx_in(bp, CD186x_MSVR) & MSVR_RTS);
988 spin_unlock_irqrestore(&bp->lock, flags);
989 dprintk(SX_DEBUG_TERMIOS, "sx: got MSVR=%02x.\n", port->MSVR);
990 baud = tty_get_baud_rate(tty);
991
992 if (baud == 38400) {
993 if ((port->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
994 baud = 57600;
995 if ((port->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
996 baud = 115200;
997 }
998
999 if (!baud) {
1000 /* Drop DTR & exit */
1001 dprintk(SX_DEBUG_TERMIOS, "Dropping DTR... Hmm....\n");
1002 if (!sx_crtscts(tty)) {
1003 port->MSVR &= ~MSVR_DTR;
1004 spin_lock_irqsave(&bp->lock, flags);
1005 sx_out(bp, CD186x_MSVR, port->MSVR);
1006 spin_unlock_irqrestore(&bp->lock, flags);
1007 } else
1008 dprintk(SX_DEBUG_TERMIOS, "Can't drop DTR: no DTR.\n");
1009 return;
1010 } else {
1011 /* Set DTR on */
1012 if (!sx_crtscts(tty))
1013 port->MSVR |= MSVR_DTR;
1014 }
1015
1016 /*
1017 * Now we must calculate some speed depended things
1018 */
1019
1020 /* Set baud rate for port */
1021 tmp = port->custom_divisor ;
1022 if (tmp)
1023 printk(KERN_INFO
1024 "sx%d: Using custom baud rate divisor %ld. \n"
1025 "This is an untested option, please be careful.\n",
1026 port_No(port), tmp);
1027 else
1028 tmp = (((SX_OSCFREQ + baud/2) / baud + CD186x_TPC/2) /
1029 CD186x_TPC);
1030
1031 if (tmp < 0x10 && time_before(again, jiffies)) {
1032 again = jiffies + HZ * 60;
1033 /* Page 48 of version 2.0 of the CL-CD1865 databook */
1034 if (tmp >= 12) {
1035 printk(KERN_INFO "sx%d: Baud rate divisor is %ld. \n"
1036 "Performance degradation is possible.\n"
1037 "Read specialix.txt for more info.\n",
1038 port_No(port), tmp);
1039 } else {
1040 printk(KERN_INFO "sx%d: Baud rate divisor is %ld. \n"
1041 "Warning: overstressing Cirrus chip. This might not work.\n"
1042 "Read specialix.txt for more info.\n", port_No(port), tmp);
1043 }
1044 }
1045 spin_lock_irqsave(&bp->lock, flags);
1046 sx_out(bp, CD186x_RBPRH, (tmp >> 8) & 0xff);
1047 sx_out(bp, CD186x_TBPRH, (tmp >> 8) & 0xff);
1048 sx_out(bp, CD186x_RBPRL, tmp & 0xff);
1049 sx_out(bp, CD186x_TBPRL, tmp & 0xff);
1050 spin_unlock_irqrestore(&bp->lock, flags);
1051 if (port->custom_divisor)
1052 baud = (SX_OSCFREQ + port->custom_divisor/2) /
1053 port->custom_divisor;
1054 baud = (baud + 5) / 10; /* Estimated CPS */
1055
1056 /* Two timer ticks seems enough to wakeup something like SLIP driver */
1057 tmp = ((baud + HZ/2) / HZ) * 2 - CD186x_NFIFO;
1058 port->wakeup_chars = (tmp < 0) ? 0 : ((tmp >= SERIAL_XMIT_SIZE) ?
1059 SERIAL_XMIT_SIZE - 1 : tmp);
1060
1061 /* Receiver timeout will be transmission time for 1.5 chars */
1062 tmp = (SPECIALIX_TPS + SPECIALIX_TPS/2 + baud/2) / baud;
1063 tmp = (tmp > 0xff) ? 0xff : tmp;
1064 spin_lock_irqsave(&bp->lock, flags);
1065 sx_out(bp, CD186x_RTPR, tmp);
1066 spin_unlock_irqrestore(&bp->lock, flags);
1067 switch (C_CSIZE(tty)) {
1068 case CS5:
1069 cor1 |= COR1_5BITS;
1070 break;
1071 case CS6:
1072 cor1 |= COR1_6BITS;
1073 break;
1074 case CS7:
1075 cor1 |= COR1_7BITS;
1076 break;
1077 case CS8:
1078 cor1 |= COR1_8BITS;
1079 break;
1080 }
1081
1082 if (C_CSTOPB(tty))
1083 cor1 |= COR1_2SB;
1084
1085 cor1 |= COR1_IGNORE;
1086 if (C_PARENB(tty)) {
1087 cor1 |= COR1_NORMPAR;
1088 if (C_PARODD(tty))
1089 cor1 |= COR1_ODDP;
1090 if (I_INPCK(tty))
1091 cor1 &= ~COR1_IGNORE;
1092 }
1093 /* Set marking of some errors */
1094 port->mark_mask = RCSR_OE | RCSR_TOUT;
1095 if (I_INPCK(tty))
1096 port->mark_mask |= RCSR_FE | RCSR_PE;
1097 if (I_BRKINT(tty) || I_PARMRK(tty))
1098 port->mark_mask |= RCSR_BREAK;
1099 if (I_IGNPAR(tty))
1100 port->mark_mask &= ~(RCSR_FE | RCSR_PE);
1101 if (I_IGNBRK(tty)) {
1102 port->mark_mask &= ~RCSR_BREAK;
1103 if (I_IGNPAR(tty))
1104 /* Real raw mode. Ignore all */
1105 port->mark_mask &= ~RCSR_OE;
1106 }
1107 /* Enable Hardware Flow Control */
1108 if (C_CRTSCTS(tty)) {
1109#ifdef SPECIALIX_BRAIN_DAMAGED_CTS
1110 port->IER |= IER_DSR | IER_CTS;
1111 mcor1 |= MCOR1_DSRZD | MCOR1_CTSZD;
1112 mcor2 |= MCOR2_DSROD | MCOR2_CTSOD;
1113 spin_lock_irqsave(&bp->lock, flags);
1114 tty->hw_stopped = !(sx_in(bp, CD186x_MSVR) &
1115 (MSVR_CTS|MSVR_DSR));
1116 spin_unlock_irqrestore(&bp->lock, flags);
1117#else
1118 port->COR2 |= COR2_CTSAE;
1119#endif
1120 }
1121 /* Enable Software Flow Control. FIXME: I'm not sure about this */
1122 /* Some people reported that it works, but I still doubt it */
1123 if (I_IXON(tty)) {
1124 port->COR2 |= COR2_TXIBE;
1125 cor3 |= (COR3_FCT | COR3_SCDE);
1126 if (I_IXANY(tty))
1127 port->COR2 |= COR2_IXM;
1128 spin_lock_irqsave(&bp->lock, flags);
1129 sx_out(bp, CD186x_SCHR1, START_CHAR(tty));
1130 sx_out(bp, CD186x_SCHR2, STOP_CHAR(tty));
1131 sx_out(bp, CD186x_SCHR3, START_CHAR(tty));
1132 sx_out(bp, CD186x_SCHR4, STOP_CHAR(tty));
1133 spin_unlock_irqrestore(&bp->lock, flags);
1134 }
1135 if (!C_CLOCAL(tty)) {
1136 /* Enable CD check */
1137 port->IER |= IER_CD;
1138 mcor1 |= MCOR1_CDZD;
1139 mcor2 |= MCOR2_CDOD;
1140 }
1141
1142 if (C_CREAD(tty))
1143 /* Enable receiver */
1144 port->IER |= IER_RXD;
1145
1146 /* Set input FIFO size (1-8 bytes) */
1147 cor3 |= sx_rxfifo;
1148 /* Setting up CD186x channel registers */
1149 spin_lock_irqsave(&bp->lock, flags);
1150 sx_out(bp, CD186x_COR1, cor1);
1151 sx_out(bp, CD186x_COR2, port->COR2);
1152 sx_out(bp, CD186x_COR3, cor3);
1153 spin_unlock_irqrestore(&bp->lock, flags);
1154 /* Make CD186x know about registers change */
1155 sx_wait_CCR(bp);
1156 spin_lock_irqsave(&bp->lock, flags);
1157 sx_out(bp, CD186x_CCR, CCR_CORCHG1 | CCR_CORCHG2 | CCR_CORCHG3);
1158 /* Setting up modem option registers */
1159 dprintk(SX_DEBUG_TERMIOS, "Mcor1 = %02x, mcor2 = %02x.\n",
1160 mcor1, mcor2);
1161 sx_out(bp, CD186x_MCOR1, mcor1);
1162 sx_out(bp, CD186x_MCOR2, mcor2);
1163 spin_unlock_irqrestore(&bp->lock, flags);
1164 /* Enable CD186x transmitter & receiver */
1165 sx_wait_CCR(bp);
1166 spin_lock_irqsave(&bp->lock, flags);
1167 sx_out(bp, CD186x_CCR, CCR_TXEN | CCR_RXEN);
1168 /* Enable interrupts */
1169 sx_out(bp, CD186x_IER, port->IER);
1170 /* And finally set the modem lines... */
1171 sx_out(bp, CD186x_MSVR, port->MSVR);
1172 spin_unlock_irqrestore(&bp->lock, flags);
1173
1174 func_exit();
1175}
1176
1177
1178/* Must be called with interrupts enabled */
1179static int sx_setup_port(struct specialix_board *bp,
1180 struct specialix_port *port)
1181{
1182 unsigned long flags;
1183
1184 func_enter();
1185
1186 if (port->port.flags & ASYNC_INITIALIZED) {
1187 func_exit();
1188 return 0;
1189 }
1190
1191 if (!port->xmit_buf) {
1192 /* We may sleep in get_zeroed_page() */
1193 unsigned long tmp;
1194
1195 tmp = get_zeroed_page(GFP_KERNEL);
1196 if (tmp == 0L) {
1197 func_exit();
1198 return -ENOMEM;
1199 }
1200
1201 if (port->xmit_buf) {
1202 free_page(tmp);
1203 func_exit();
1204 return -ERESTARTSYS;
1205 }
1206 port->xmit_buf = (unsigned char *) tmp;
1207 }
1208
1209 spin_lock_irqsave(&port->lock, flags);
1210
1211 if (port->port.tty)
1212 clear_bit(TTY_IO_ERROR, &port->port.tty->flags);
1213
1214 port->xmit_cnt = port->xmit_head = port->xmit_tail = 0;
1215 sx_change_speed(bp, port);
1216 port->port.flags |= ASYNC_INITIALIZED;
1217
1218 spin_unlock_irqrestore(&port->lock, flags);
1219
1220
1221 func_exit();
1222 return 0;
1223}
1224
1225
1226/* Must be called with interrupts disabled */
1227static void sx_shutdown_port(struct specialix_board *bp,
1228 struct specialix_port *port)
1229{
1230 struct tty_struct *tty;
1231 int i;
1232 unsigned long flags;
1233
1234 func_enter();
1235
1236 if (!(port->port.flags & ASYNC_INITIALIZED)) {
1237 func_exit();
1238 return;
1239 }
1240
1241 if (sx_debug & SX_DEBUG_FIFO) {
1242 dprintk(SX_DEBUG_FIFO,
1243 "sx%d: port %d: %ld overruns, FIFO hits [ ",
1244 board_No(bp), port_No(port), port->overrun);
1245 for (i = 0; i < 10; i++)
1246 dprintk(SX_DEBUG_FIFO, "%ld ", port->hits[i]);
1247 dprintk(SX_DEBUG_FIFO, "].\n");
1248 }
1249
1250 if (port->xmit_buf) {
1251 free_page((unsigned long) port->xmit_buf);
1252 port->xmit_buf = NULL;
1253 }
1254
1255 /* Select port */
1256 spin_lock_irqsave(&bp->lock, flags);
1257 sx_out(bp, CD186x_CAR, port_No(port));
1258
1259 tty = port->port.tty;
1260 if (tty == NULL || C_HUPCL(tty)) {
1261 /* Drop DTR */
1262 sx_out(bp, CD186x_MSVDTR, 0);
1263 }
1264 spin_unlock_irqrestore(&bp->lock, flags);
1265 /* Reset port */
1266 sx_wait_CCR(bp);
1267 spin_lock_irqsave(&bp->lock, flags);
1268 sx_out(bp, CD186x_CCR, CCR_SOFTRESET);
1269 /* Disable all interrupts from this port */
1270 port->IER = 0;
1271 sx_out(bp, CD186x_IER, port->IER);
1272 spin_unlock_irqrestore(&bp->lock, flags);
1273 if (tty)
1274 set_bit(TTY_IO_ERROR, &tty->flags);
1275 port->port.flags &= ~ASYNC_INITIALIZED;
1276
1277 if (!bp->count)
1278 sx_shutdown_board(bp);
1279 func_exit();
1280}
1281
1282
1283static int block_til_ready(struct tty_struct *tty, struct file *filp,
1284 struct specialix_port *port)
1285{
1286 DECLARE_WAITQUEUE(wait, current);
1287 struct specialix_board *bp = port_Board(port);
1288 int retval;
1289 int do_clocal = 0;
1290 int CD;
1291 unsigned long flags;
1292
1293 func_enter();
1294
1295 /*
1296 * If the device is in the middle of being closed, then block
1297 * until it's done, and then try again.
1298 */
1299 if (tty_hung_up_p(filp) || port->port.flags & ASYNC_CLOSING) {
1300 interruptible_sleep_on(&port->port.close_wait);
1301 if (port->port.flags & ASYNC_HUP_NOTIFY) {
1302 func_exit();
1303 return -EAGAIN;
1304 } else {
1305 func_exit();
1306 return -ERESTARTSYS;
1307 }
1308 }
1309
1310 /*
1311 * If non-blocking mode is set, or the port is not enabled,
1312 * then make the check up front and then exit.
1313 */
1314 if ((filp->f_flags & O_NONBLOCK) ||
1315 (tty->flags & (1 << TTY_IO_ERROR))) {
1316 port->port.flags |= ASYNC_NORMAL_ACTIVE;
1317 func_exit();
1318 return 0;
1319 }
1320
1321 if (C_CLOCAL(tty))
1322 do_clocal = 1;
1323
1324 /*
1325 * Block waiting for the carrier detect and the line to become
1326 * free (i.e., not in use by the callout). While we are in
1327 * this loop, info->count is dropped by one, so that
1328 * rs_close() knows when to free things. We restore it upon
1329 * exit, either normal or abnormal.
1330 */
1331 retval = 0;
1332 add_wait_queue(&port->port.open_wait, &wait);
1333 spin_lock_irqsave(&port->lock, flags);
1334 if (!tty_hung_up_p(filp))
1335 port->port.count--;
1336 spin_unlock_irqrestore(&port->lock, flags);
1337 port->port.blocked_open++;
1338 while (1) {
1339 spin_lock_irqsave(&bp->lock, flags);
1340 sx_out(bp, CD186x_CAR, port_No(port));
1341 CD = sx_in(bp, CD186x_MSVR) & MSVR_CD;
1342 if (sx_crtscts(tty)) {
1343 /* Activate RTS */
1344 port->MSVR |= MSVR_DTR; /* WTF? */
1345 sx_out(bp, CD186x_MSVR, port->MSVR);
1346 } else {
1347 /* Activate DTR */
1348 port->MSVR |= MSVR_DTR;
1349 sx_out(bp, CD186x_MSVR, port->MSVR);
1350 }
1351 spin_unlock_irqrestore(&bp->lock, flags);
1352 set_current_state(TASK_INTERRUPTIBLE);
1353 if (tty_hung_up_p(filp) ||
1354 !(port->port.flags & ASYNC_INITIALIZED)) {
1355 if (port->port.flags & ASYNC_HUP_NOTIFY)
1356 retval = -EAGAIN;
1357 else
1358 retval = -ERESTARTSYS;
1359 break;
1360 }
1361 if (!(port->port.flags & ASYNC_CLOSING) &&
1362 (do_clocal || CD))
1363 break;
1364 if (signal_pending(current)) {
1365 retval = -ERESTARTSYS;
1366 break;
1367 }
1368 tty_unlock();
1369 schedule();
1370 tty_lock();
1371 }
1372
1373 set_current_state(TASK_RUNNING);
1374 remove_wait_queue(&port->port.open_wait, &wait);
1375 spin_lock_irqsave(&port->lock, flags);
1376 if (!tty_hung_up_p(filp))
1377 port->port.count++;
1378 port->port.blocked_open--;
1379 spin_unlock_irqrestore(&port->lock, flags);
1380 if (retval) {
1381 func_exit();
1382 return retval;
1383 }
1384
1385 port->port.flags |= ASYNC_NORMAL_ACTIVE;
1386 func_exit();
1387 return 0;
1388}
1389
1390
1391static int sx_open(struct tty_struct *tty, struct file *filp)
1392{
1393 int board;
1394 int error;
1395 struct specialix_port *port;
1396 struct specialix_board *bp;
1397 int i;
1398 unsigned long flags;
1399
1400 func_enter();
1401
1402 board = SX_BOARD(tty->index);
1403
1404 if (board >= SX_NBOARD || !(sx_board[board].flags & SX_BOARD_PRESENT)) {
1405 func_exit();
1406 return -ENODEV;
1407 }
1408
1409 bp = &sx_board[board];
1410 port = sx_port + board * SX_NPORT + SX_PORT(tty->index);
1411 port->overrun = 0;
1412 for (i = 0; i < 10; i++)
1413 port->hits[i] = 0;
1414
1415 dprintk(SX_DEBUG_OPEN,
1416 "Board = %d, bp = %p, port = %p, portno = %d.\n",
1417 board, bp, port, SX_PORT(tty->index));
1418
1419 if (sx_paranoia_check(port, tty->name, "sx_open")) {
1420 func_enter();
1421 return -ENODEV;
1422 }
1423
1424 error = sx_setup_board(bp);
1425 if (error) {
1426 func_exit();
1427 return error;
1428 }
1429
1430 spin_lock_irqsave(&bp->lock, flags);
1431 port->port.count++;
1432 bp->count++;
1433 tty->driver_data = port;
1434 port->port.tty = tty;
1435 spin_unlock_irqrestore(&bp->lock, flags);
1436
1437 error = sx_setup_port(bp, port);
1438 if (error) {
1439 func_enter();
1440 return error;
1441 }
1442
1443 error = block_til_ready(tty, filp, port);
1444 if (error) {
1445 func_enter();
1446 return error;
1447 }
1448
1449 func_exit();
1450 return 0;
1451}
1452
1453static void sx_flush_buffer(struct tty_struct *tty)
1454{
1455 struct specialix_port *port = tty->driver_data;
1456 unsigned long flags;
1457 struct specialix_board *bp;
1458
1459 func_enter();
1460
1461 if (sx_paranoia_check(port, tty->name, "sx_flush_buffer")) {
1462 func_exit();
1463 return;
1464 }
1465
1466 bp = port_Board(port);
1467 spin_lock_irqsave(&port->lock, flags);
1468 port->xmit_cnt = port->xmit_head = port->xmit_tail = 0;
1469 spin_unlock_irqrestore(&port->lock, flags);
1470 tty_wakeup(tty);
1471
1472 func_exit();
1473}
1474
1475static void sx_close(struct tty_struct *tty, struct file *filp)
1476{
1477 struct specialix_port *port = tty->driver_data;
1478 struct specialix_board *bp;
1479 unsigned long flags;
1480 unsigned long timeout;
1481
1482 func_enter();
1483 if (!port || sx_paranoia_check(port, tty->name, "close")) {
1484 func_exit();
1485 return;
1486 }
1487 spin_lock_irqsave(&port->lock, flags);
1488
1489 if (tty_hung_up_p(filp)) {
1490 spin_unlock_irqrestore(&port->lock, flags);
1491 func_exit();
1492 return;
1493 }
1494
1495 bp = port_Board(port);
1496 if (tty->count == 1 && port->port.count != 1) {
1497 printk(KERN_ERR "sx%d: sx_close: bad port count;"
1498 " tty->count is 1, port count is %d\n",
1499 board_No(bp), port->port.count);
1500 port->port.count = 1;
1501 }
1502
1503 if (port->port.count > 1) {
1504 port->port.count--;
1505 bp->count--;
1506
1507 spin_unlock_irqrestore(&port->lock, flags);
1508
1509 func_exit();
1510 return;
1511 }
1512 port->port.flags |= ASYNC_CLOSING;
1513 /*
1514 * Now we wait for the transmit buffer to clear; and we notify
1515 * the line discipline to only process XON/XOFF characters.
1516 */
1517 tty->closing = 1;
1518 spin_unlock_irqrestore(&port->lock, flags);
1519 dprintk(SX_DEBUG_OPEN, "Closing\n");
1520 if (port->port.closing_wait != ASYNC_CLOSING_WAIT_NONE)
1521 tty_wait_until_sent(tty, port->port.closing_wait);
1522 /*
1523 * At this point we stop accepting input. To do this, we
1524 * disable the receive line status interrupts, and tell the
1525 * interrupt driver to stop checking the data ready bit in the
1526 * line status register.
1527 */
1528 dprintk(SX_DEBUG_OPEN, "Closed\n");
1529 port->IER &= ~IER_RXD;
1530 if (port->port.flags & ASYNC_INITIALIZED) {
1531 port->IER &= ~IER_TXRDY;
1532 port->IER |= IER_TXEMPTY;
1533 spin_lock_irqsave(&bp->lock, flags);
1534 sx_out(bp, CD186x_CAR, port_No(port));
1535 sx_out(bp, CD186x_IER, port->IER);
1536 spin_unlock_irqrestore(&bp->lock, flags);
1537 /*
1538 * Before we drop DTR, make sure the UART transmitter
1539 * has completely drained; this is especially
1540 * important if there is a transmit FIFO!
1541 */
1542 timeout = jiffies+HZ;
1543 while (port->IER & IER_TXEMPTY) {
1544 set_current_state(TASK_INTERRUPTIBLE);
1545 msleep_interruptible(jiffies_to_msecs(port->timeout));
1546 if (time_after(jiffies, timeout)) {
1547 printk(KERN_INFO "Timeout waiting for close\n");
1548 break;
1549 }
1550 }
1551
1552 }
1553
1554 if (--bp->count < 0) {
1555 printk(KERN_ERR
1556 "sx%d: sx_shutdown_port: bad board count: %d port: %d\n",
1557 board_No(bp), bp->count, tty->index);
1558 bp->count = 0;
1559 }
1560 if (--port->port.count < 0) {
1561 printk(KERN_ERR
1562 "sx%d: sx_close: bad port count for tty%d: %d\n",
1563 board_No(bp), port_No(port), port->port.count);
1564 port->port.count = 0;
1565 }
1566
1567 sx_shutdown_port(bp, port);
1568 sx_flush_buffer(tty);
1569 tty_ldisc_flush(tty);
1570 spin_lock_irqsave(&port->lock, flags);
1571 tty->closing = 0;
1572 port->port.tty = NULL;
1573 spin_unlock_irqrestore(&port->lock, flags);
1574 if (port->port.blocked_open) {
1575 if (port->port.close_delay)
1576 msleep_interruptible(
1577 jiffies_to_msecs(port->port.close_delay));
1578 wake_up_interruptible(&port->port.open_wait);
1579 }
1580 port->port.flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
1581 wake_up_interruptible(&port->port.close_wait);
1582
1583 func_exit();
1584}
1585
1586
1587static int sx_write(struct tty_struct *tty,
1588 const unsigned char *buf, int count)
1589{
1590 struct specialix_port *port = tty->driver_data;
1591 struct specialix_board *bp;
1592 int c, total = 0;
1593 unsigned long flags;
1594
1595 func_enter();
1596 if (sx_paranoia_check(port, tty->name, "sx_write")) {
1597 func_exit();
1598 return 0;
1599 }
1600
1601 bp = port_Board(port);
1602
1603 if (!port->xmit_buf) {
1604 func_exit();
1605 return 0;
1606 }
1607
1608 while (1) {
1609 spin_lock_irqsave(&port->lock, flags);
1610 c = min_t(int, count, min(SERIAL_XMIT_SIZE - port->xmit_cnt - 1,
1611 SERIAL_XMIT_SIZE - port->xmit_head));
1612 if (c <= 0) {
1613 spin_unlock_irqrestore(&port->lock, flags);
1614 break;
1615 }
1616 memcpy(port->xmit_buf + port->xmit_head, buf, c);
1617 port->xmit_head = (port->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
1618 port->xmit_cnt += c;
1619 spin_unlock_irqrestore(&port->lock, flags);
1620
1621 buf += c;
1622 count -= c;
1623 total += c;
1624 }
1625
1626 spin_lock_irqsave(&bp->lock, flags);
1627 if (port->xmit_cnt && !tty->stopped && !tty->hw_stopped &&
1628 !(port->IER & IER_TXRDY)) {
1629 port->IER |= IER_TXRDY;
1630 sx_out(bp, CD186x_CAR, port_No(port));
1631 sx_out(bp, CD186x_IER, port->IER);
1632 }
1633 spin_unlock_irqrestore(&bp->lock, flags);
1634 func_exit();
1635
1636 return total;
1637}
1638
1639
1640static int sx_put_char(struct tty_struct *tty, unsigned char ch)
1641{
1642 struct specialix_port *port = tty->driver_data;
1643 unsigned long flags;
1644 struct specialix_board *bp;
1645
1646 func_enter();
1647
1648 if (sx_paranoia_check(port, tty->name, "sx_put_char")) {
1649 func_exit();
1650 return 0;
1651 }
1652 dprintk(SX_DEBUG_TX, "check tty: %p %p\n", tty, port->xmit_buf);
1653 if (!port->xmit_buf) {
1654 func_exit();
1655 return 0;
1656 }
1657 bp = port_Board(port);
1658 spin_lock_irqsave(&port->lock, flags);
1659
1660 dprintk(SX_DEBUG_TX, "xmit_cnt: %d xmit_buf: %p\n",
1661 port->xmit_cnt, port->xmit_buf);
1662 if (port->xmit_cnt >= SERIAL_XMIT_SIZE - 1 || !port->xmit_buf) {
1663 spin_unlock_irqrestore(&port->lock, flags);
1664 dprintk(SX_DEBUG_TX, "Exit size\n");
1665 func_exit();
1666 return 0;
1667 }
1668 dprintk(SX_DEBUG_TX, "Handle xmit: %p %p\n", port, port->xmit_buf);
1669 port->xmit_buf[port->xmit_head++] = ch;
1670 port->xmit_head &= SERIAL_XMIT_SIZE - 1;
1671 port->xmit_cnt++;
1672 spin_unlock_irqrestore(&port->lock, flags);
1673
1674 func_exit();
1675 return 1;
1676}
1677
1678
1679static void sx_flush_chars(struct tty_struct *tty)
1680{
1681 struct specialix_port *port = tty->driver_data;
1682 unsigned long flags;
1683 struct specialix_board *bp = port_Board(port);
1684
1685 func_enter();
1686
1687 if (sx_paranoia_check(port, tty->name, "sx_flush_chars")) {
1688 func_exit();
1689 return;
1690 }
1691 if (port->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
1692 !port->xmit_buf) {
1693 func_exit();
1694 return;
1695 }
1696 spin_lock_irqsave(&bp->lock, flags);
1697 port->IER |= IER_TXRDY;
1698 sx_out(port_Board(port), CD186x_CAR, port_No(port));
1699 sx_out(port_Board(port), CD186x_IER, port->IER);
1700 spin_unlock_irqrestore(&bp->lock, flags);
1701
1702 func_exit();
1703}
1704
1705
1706static int sx_write_room(struct tty_struct *tty)
1707{
1708 struct specialix_port *port = tty->driver_data;
1709 int ret;
1710
1711 func_enter();
1712
1713 if (sx_paranoia_check(port, tty->name, "sx_write_room")) {
1714 func_exit();
1715 return 0;
1716 }
1717
1718 ret = SERIAL_XMIT_SIZE - port->xmit_cnt - 1;
1719 if (ret < 0)
1720 ret = 0;
1721
1722 func_exit();
1723 return ret;
1724}
1725
1726
1727static int sx_chars_in_buffer(struct tty_struct *tty)
1728{
1729 struct specialix_port *port = tty->driver_data;
1730
1731 func_enter();
1732
1733 if (sx_paranoia_check(port, tty->name, "sx_chars_in_buffer")) {
1734 func_exit();
1735 return 0;
1736 }
1737 func_exit();
1738 return port->xmit_cnt;
1739}
1740
1741static int sx_tiocmget(struct tty_struct *tty, struct file *file)
1742{
1743 struct specialix_port *port = tty->driver_data;
1744 struct specialix_board *bp;
1745 unsigned char status;
1746 unsigned int result;
1747 unsigned long flags;
1748
1749 func_enter();
1750
1751 if (sx_paranoia_check(port, tty->name, __func__)) {
1752 func_exit();
1753 return -ENODEV;
1754 }
1755
1756 bp = port_Board(port);
1757 spin_lock_irqsave(&bp->lock, flags);
1758 sx_out(bp, CD186x_CAR, port_No(port));
1759 status = sx_in(bp, CD186x_MSVR);
1760 spin_unlock_irqrestore(&bp->lock, flags);
1761 dprintk(SX_DEBUG_INIT, "Got msvr[%d] = %02x, car = %d.\n",
1762 port_No(port), status, sx_in(bp, CD186x_CAR));
1763 dprintk(SX_DEBUG_INIT, "sx_port = %p, port = %p\n", sx_port, port);
1764 if (sx_crtscts(port->port.tty)) {
1765 result = TIOCM_DTR | TIOCM_DSR
1766 | ((status & MSVR_DTR) ? TIOCM_RTS : 0)
1767 | ((status & MSVR_CD) ? TIOCM_CAR : 0)
1768 | ((status & MSVR_CTS) ? TIOCM_CTS : 0);
1769 } else {
1770 result = TIOCM_RTS | TIOCM_DSR
1771 | ((status & MSVR_DTR) ? TIOCM_DTR : 0)
1772 | ((status & MSVR_CD) ? TIOCM_CAR : 0)
1773 | ((status & MSVR_CTS) ? TIOCM_CTS : 0);
1774 }
1775
1776 func_exit();
1777
1778 return result;
1779}
1780
1781
1782static int sx_tiocmset(struct tty_struct *tty, struct file *file,
1783 unsigned int set, unsigned int clear)
1784{
1785 struct specialix_port *port = tty->driver_data;
1786 unsigned long flags;
1787 struct specialix_board *bp;
1788
1789 func_enter();
1790
1791 if (sx_paranoia_check(port, tty->name, __func__)) {
1792 func_exit();
1793 return -ENODEV;
1794 }
1795
1796 bp = port_Board(port);
1797
1798 spin_lock_irqsave(&port->lock, flags);
1799 if (sx_crtscts(port->port.tty)) {
1800 if (set & TIOCM_RTS)
1801 port->MSVR |= MSVR_DTR;
1802 } else {
1803 if (set & TIOCM_DTR)
1804 port->MSVR |= MSVR_DTR;
1805 }
1806 if (sx_crtscts(port->port.tty)) {
1807 if (clear & TIOCM_RTS)
1808 port->MSVR &= ~MSVR_DTR;
1809 } else {
1810 if (clear & TIOCM_DTR)
1811 port->MSVR &= ~MSVR_DTR;
1812 }
1813 spin_lock(&bp->lock);
1814 sx_out(bp, CD186x_CAR, port_No(port));
1815 sx_out(bp, CD186x_MSVR, port->MSVR);
1816 spin_unlock(&bp->lock);
1817 spin_unlock_irqrestore(&port->lock, flags);
1818 func_exit();
1819 return 0;
1820}
1821
1822
1823static int sx_send_break(struct tty_struct *tty, int length)
1824{
1825 struct specialix_port *port = tty->driver_data;
1826 struct specialix_board *bp = port_Board(port);
1827 unsigned long flags;
1828
1829 func_enter();
1830 if (length == 0 || length == -1)
1831 return -EOPNOTSUPP;
1832
1833 spin_lock_irqsave(&port->lock, flags);
1834 port->break_length = SPECIALIX_TPS / HZ * length;
1835 port->COR2 |= COR2_ETC;
1836 port->IER |= IER_TXRDY;
1837 spin_lock(&bp->lock);
1838 sx_out(bp, CD186x_CAR, port_No(port));
1839 sx_out(bp, CD186x_COR2, port->COR2);
1840 sx_out(bp, CD186x_IER, port->IER);
1841 spin_unlock(&bp->lock);
1842 spin_unlock_irqrestore(&port->lock, flags);
1843 sx_wait_CCR(bp);
1844 spin_lock_irqsave(&bp->lock, flags);
1845 sx_out(bp, CD186x_CCR, CCR_CORCHG2);
1846 spin_unlock_irqrestore(&bp->lock, flags);
1847 sx_wait_CCR(bp);
1848
1849 func_exit();
1850 return 0;
1851}
1852
1853
1854static int sx_set_serial_info(struct specialix_port *port,
1855 struct serial_struct __user *newinfo)
1856{
1857 struct serial_struct tmp;
1858 struct specialix_board *bp = port_Board(port);
1859 int change_speed;
1860
1861 func_enter();
1862
1863 if (copy_from_user(&tmp, newinfo, sizeof(tmp))) {
1864 func_enter();
1865 return -EFAULT;
1866 }
1867
1868 mutex_lock(&port->port.mutex);
1869 change_speed = ((port->port.flags & ASYNC_SPD_MASK) !=
1870 (tmp.flags & ASYNC_SPD_MASK));
1871 change_speed |= (tmp.custom_divisor != port->custom_divisor);
1872
1873 if (!capable(CAP_SYS_ADMIN)) {
1874 if ((tmp.close_delay != port->port.close_delay) ||
1875 (tmp.closing_wait != port->port.closing_wait) ||
1876 ((tmp.flags & ~ASYNC_USR_MASK) !=
1877 (port->port.flags & ~ASYNC_USR_MASK))) {
1878 func_exit();
1879 mutex_unlock(&port->port.mutex);
1880 return -EPERM;
1881 }
1882 port->port.flags = ((port->port.flags & ~ASYNC_USR_MASK) |
1883 (tmp.flags & ASYNC_USR_MASK));
1884 port->custom_divisor = tmp.custom_divisor;
1885 } else {
1886 port->port.flags = ((port->port.flags & ~ASYNC_FLAGS) |
1887 (tmp.flags & ASYNC_FLAGS));
1888 port->port.close_delay = tmp.close_delay;
1889 port->port.closing_wait = tmp.closing_wait;
1890 port->custom_divisor = tmp.custom_divisor;
1891 }
1892 if (change_speed)
1893 sx_change_speed(bp, port);
1894
1895 func_exit();
1896 mutex_unlock(&port->port.mutex);
1897 return 0;
1898}
1899
1900
1901static int sx_get_serial_info(struct specialix_port *port,
1902 struct serial_struct __user *retinfo)
1903{
1904 struct serial_struct tmp;
1905 struct specialix_board *bp = port_Board(port);
1906
1907 func_enter();
1908
1909 memset(&tmp, 0, sizeof(tmp));
1910 mutex_lock(&port->port.mutex);
1911 tmp.type = PORT_CIRRUS;
1912 tmp.line = port - sx_port;
1913 tmp.port = bp->base;
1914 tmp.irq = bp->irq;
1915 tmp.flags = port->port.flags;
1916 tmp.baud_base = (SX_OSCFREQ + CD186x_TPC/2) / CD186x_TPC;
1917 tmp.close_delay = port->port.close_delay * HZ/100;
1918 tmp.closing_wait = port->port.closing_wait * HZ/100;
1919 tmp.custom_divisor = port->custom_divisor;
1920 tmp.xmit_fifo_size = CD186x_NFIFO;
1921 mutex_unlock(&port->port.mutex);
1922 if (copy_to_user(retinfo, &tmp, sizeof(tmp))) {
1923 func_exit();
1924 return -EFAULT;
1925 }
1926
1927 func_exit();
1928 return 0;
1929}
1930
1931
1932static int sx_ioctl(struct tty_struct *tty, struct file *filp,
1933 unsigned int cmd, unsigned long arg)
1934{
1935 struct specialix_port *port = tty->driver_data;
1936 void __user *argp = (void __user *)arg;
1937
1938 func_enter();
1939
1940 if (sx_paranoia_check(port, tty->name, "sx_ioctl")) {
1941 func_exit();
1942 return -ENODEV;
1943 }
1944
1945 switch (cmd) {
1946 case TIOCGSERIAL:
1947 func_exit();
1948 return sx_get_serial_info(port, argp);
1949 case TIOCSSERIAL:
1950 func_exit();
1951 return sx_set_serial_info(port, argp);
1952 default:
1953 func_exit();
1954 return -ENOIOCTLCMD;
1955 }
1956 func_exit();
1957 return 0;
1958}
1959
1960
1961static void sx_throttle(struct tty_struct *tty)
1962{
1963 struct specialix_port *port = tty->driver_data;
1964 struct specialix_board *bp;
1965 unsigned long flags;
1966
1967 func_enter();
1968
1969 if (sx_paranoia_check(port, tty->name, "sx_throttle")) {
1970 func_exit();
1971 return;
1972 }
1973
1974 bp = port_Board(port);
1975
1976 /* Use DTR instead of RTS ! */
1977 if (sx_crtscts(tty))
1978 port->MSVR &= ~MSVR_DTR;
1979 else {
1980 /* Auch!!! I think the system shouldn't call this then. */
1981 /* Or maybe we're supposed (allowed?) to do our side of hw
1982 handshake anyway, even when hardware handshake is off.
1983 When you see this in your logs, please report.... */
1984 printk(KERN_ERR
1985 "sx%d: Need to throttle, but can't (hardware hs is off)\n",
1986 port_No(port));
1987 }
1988 spin_lock_irqsave(&bp->lock, flags);
1989 sx_out(bp, CD186x_CAR, port_No(port));
1990 spin_unlock_irqrestore(&bp->lock, flags);
1991 if (I_IXOFF(tty)) {
1992 sx_wait_CCR(bp);
1993 spin_lock_irqsave(&bp->lock, flags);
1994 sx_out(bp, CD186x_CCR, CCR_SSCH2);
1995 spin_unlock_irqrestore(&bp->lock, flags);
1996 sx_wait_CCR(bp);
1997 }
1998 spin_lock_irqsave(&bp->lock, flags);
1999 sx_out(bp, CD186x_MSVR, port->MSVR);
2000 spin_unlock_irqrestore(&bp->lock, flags);
2001
2002 func_exit();
2003}
2004
2005
2006static void sx_unthrottle(struct tty_struct *tty)
2007{
2008 struct specialix_port *port = tty->driver_data;
2009 struct specialix_board *bp;
2010 unsigned long flags;
2011
2012 func_enter();
2013
2014 if (sx_paranoia_check(port, tty->name, "sx_unthrottle")) {
2015 func_exit();
2016 return;
2017 }
2018
2019 bp = port_Board(port);
2020
2021 spin_lock_irqsave(&port->lock, flags);
2022 /* XXXX Use DTR INSTEAD???? */
2023 if (sx_crtscts(tty))
2024 port->MSVR |= MSVR_DTR;
2025 /* Else clause: see remark in "sx_throttle"... */
2026 spin_lock(&bp->lock);
2027 sx_out(bp, CD186x_CAR, port_No(port));
2028 spin_unlock(&bp->lock);
2029 if (I_IXOFF(tty)) {
2030 spin_unlock_irqrestore(&port->lock, flags);
2031 sx_wait_CCR(bp);
2032 spin_lock_irqsave(&bp->lock, flags);
2033 sx_out(bp, CD186x_CCR, CCR_SSCH1);
2034 spin_unlock_irqrestore(&bp->lock, flags);
2035 sx_wait_CCR(bp);
2036 spin_lock_irqsave(&port->lock, flags);
2037 }
2038 spin_lock(&bp->lock);
2039 sx_out(bp, CD186x_MSVR, port->MSVR);
2040 spin_unlock(&bp->lock);
2041 spin_unlock_irqrestore(&port->lock, flags);
2042
2043 func_exit();
2044}
2045
2046
2047static void sx_stop(struct tty_struct *tty)
2048{
2049 struct specialix_port *port = tty->driver_data;
2050 struct specialix_board *bp;
2051 unsigned long flags;
2052
2053 func_enter();
2054
2055 if (sx_paranoia_check(port, tty->name, "sx_stop")) {
2056 func_exit();
2057 return;
2058 }
2059
2060 bp = port_Board(port);
2061
2062 spin_lock_irqsave(&port->lock, flags);
2063 port->IER &= ~IER_TXRDY;
2064 spin_lock(&bp->lock);
2065 sx_out(bp, CD186x_CAR, port_No(port));
2066 sx_out(bp, CD186x_IER, port->IER);
2067 spin_unlock(&bp->lock);
2068 spin_unlock_irqrestore(&port->lock, flags);
2069
2070 func_exit();
2071}
2072
2073
2074static void sx_start(struct tty_struct *tty)
2075{
2076 struct specialix_port *port = tty->driver_data;
2077 struct specialix_board *bp;
2078 unsigned long flags;
2079
2080 func_enter();
2081
2082 if (sx_paranoia_check(port, tty->name, "sx_start")) {
2083 func_exit();
2084 return;
2085 }
2086
2087 bp = port_Board(port);
2088
2089 spin_lock_irqsave(&port->lock, flags);
2090 if (port->xmit_cnt && port->xmit_buf && !(port->IER & IER_TXRDY)) {
2091 port->IER |= IER_TXRDY;
2092 spin_lock(&bp->lock);
2093 sx_out(bp, CD186x_CAR, port_No(port));
2094 sx_out(bp, CD186x_IER, port->IER);
2095 spin_unlock(&bp->lock);
2096 }
2097 spin_unlock_irqrestore(&port->lock, flags);
2098
2099 func_exit();
2100}
2101
2102static void sx_hangup(struct tty_struct *tty)
2103{
2104 struct specialix_port *port = tty->driver_data;
2105 struct specialix_board *bp;
2106 unsigned long flags;
2107
2108 func_enter();
2109
2110 if (sx_paranoia_check(port, tty->name, "sx_hangup")) {
2111 func_exit();
2112 return;
2113 }
2114
2115 bp = port_Board(port);
2116
2117 sx_shutdown_port(bp, port);
2118 spin_lock_irqsave(&port->lock, flags);
2119 bp->count -= port->port.count;
2120 if (bp->count < 0) {
2121 printk(KERN_ERR
2122 "sx%d: sx_hangup: bad board count: %d port: %d\n",
2123 board_No(bp), bp->count, tty->index);
2124 bp->count = 0;
2125 }
2126 port->port.count = 0;
2127 port->port.flags &= ~ASYNC_NORMAL_ACTIVE;
2128 port->port.tty = NULL;
2129 spin_unlock_irqrestore(&port->lock, flags);
2130 wake_up_interruptible(&port->port.open_wait);
2131
2132 func_exit();
2133}
2134
2135
2136static void sx_set_termios(struct tty_struct *tty,
2137 struct ktermios *old_termios)
2138{
2139 struct specialix_port *port = tty->driver_data;
2140 unsigned long flags;
2141 struct specialix_board *bp;
2142
2143 if (sx_paranoia_check(port, tty->name, "sx_set_termios"))
2144 return;
2145
2146 bp = port_Board(port);
2147 spin_lock_irqsave(&port->lock, flags);
2148 sx_change_speed(port_Board(port), port);
2149 spin_unlock_irqrestore(&port->lock, flags);
2150
2151 if ((old_termios->c_cflag & CRTSCTS) &&
2152 !(tty->termios->c_cflag & CRTSCTS)) {
2153 tty->hw_stopped = 0;
2154 sx_start(tty);
2155 }
2156}
2157
2158static const struct tty_operations sx_ops = {
2159 .open = sx_open,
2160 .close = sx_close,
2161 .write = sx_write,
2162 .put_char = sx_put_char,
2163 .flush_chars = sx_flush_chars,
2164 .write_room = sx_write_room,
2165 .chars_in_buffer = sx_chars_in_buffer,
2166 .flush_buffer = sx_flush_buffer,
2167 .ioctl = sx_ioctl,
2168 .throttle = sx_throttle,
2169 .unthrottle = sx_unthrottle,
2170 .set_termios = sx_set_termios,
2171 .stop = sx_stop,
2172 .start = sx_start,
2173 .hangup = sx_hangup,
2174 .tiocmget = sx_tiocmget,
2175 .tiocmset = sx_tiocmset,
2176 .break_ctl = sx_send_break,
2177};
2178
2179static int sx_init_drivers(void)
2180{
2181 int error;
2182 int i;
2183
2184 func_enter();
2185
2186 specialix_driver = alloc_tty_driver(SX_NBOARD * SX_NPORT);
2187 if (!specialix_driver) {
2188 printk(KERN_ERR "sx: Couldn't allocate tty_driver.\n");
2189 func_exit();
2190 return 1;
2191 }
2192
2193 specialix_driver->owner = THIS_MODULE;
2194 specialix_driver->name = "ttyW";
2195 specialix_driver->major = SPECIALIX_NORMAL_MAJOR;
2196 specialix_driver->type = TTY_DRIVER_TYPE_SERIAL;
2197 specialix_driver->subtype = SERIAL_TYPE_NORMAL;
2198 specialix_driver->init_termios = tty_std_termios;
2199 specialix_driver->init_termios.c_cflag =
2200 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2201 specialix_driver->init_termios.c_ispeed = 9600;
2202 specialix_driver->init_termios.c_ospeed = 9600;
2203 specialix_driver->flags = TTY_DRIVER_REAL_RAW |
2204 TTY_DRIVER_HARDWARE_BREAK;
2205 tty_set_operations(specialix_driver, &sx_ops);
2206
2207 error = tty_register_driver(specialix_driver);
2208 if (error) {
2209 put_tty_driver(specialix_driver);
2210 printk(KERN_ERR
2211 "sx: Couldn't register specialix IO8+ driver, error = %d\n",
2212 error);
2213 func_exit();
2214 return 1;
2215 }
2216 memset(sx_port, 0, sizeof(sx_port));
2217 for (i = 0; i < SX_NPORT * SX_NBOARD; i++) {
2218 sx_port[i].magic = SPECIALIX_MAGIC;
2219 tty_port_init(&sx_port[i].port);
2220 spin_lock_init(&sx_port[i].lock);
2221 }
2222
2223 func_exit();
2224 return 0;
2225}
2226
2227static void sx_release_drivers(void)
2228{
2229 func_enter();
2230
2231 tty_unregister_driver(specialix_driver);
2232 put_tty_driver(specialix_driver);
2233 func_exit();
2234}
2235
2236/*
2237 * This routine must be called by kernel at boot time
2238 */
2239static int __init specialix_init(void)
2240{
2241 int i;
2242 int found = 0;
2243
2244 func_enter();
2245
2246 printk(KERN_INFO "sx: Specialix IO8+ driver v" VERSION ", (c) R.E.Wolff 1997/1998.\n");
2247 printk(KERN_INFO "sx: derived from work (c) D.Gorodchanin 1994-1996.\n");
2248 if (sx_rtscts)
2249 printk(KERN_INFO
2250 "sx: DTR/RTS pin is RTS when CRTSCTS is on.\n");
2251 else
2252 printk(KERN_INFO "sx: DTR/RTS pin is always RTS.\n");
2253
2254 for (i = 0; i < SX_NBOARD; i++)
2255 spin_lock_init(&sx_board[i].lock);
2256
2257 if (sx_init_drivers()) {
2258 func_exit();
2259 return -EIO;
2260 }
2261
2262 for (i = 0; i < SX_NBOARD; i++)
2263 if (sx_board[i].base && !sx_probe(&sx_board[i]))
2264 found++;
2265
2266#ifdef CONFIG_PCI
2267 {
2268 struct pci_dev *pdev = NULL;
2269
2270 i = 0;
2271 while (i < SX_NBOARD) {
2272 if (sx_board[i].flags & SX_BOARD_PRESENT) {
2273 i++;
2274 continue;
2275 }
2276 pdev = pci_get_device(PCI_VENDOR_ID_SPECIALIX,
2277 PCI_DEVICE_ID_SPECIALIX_IO8, pdev);
2278 if (!pdev)
2279 break;
2280
2281 if (pci_enable_device(pdev))
2282 continue;
2283
2284 sx_board[i].irq = pdev->irq;
2285
2286 sx_board[i].base = pci_resource_start(pdev, 2);
2287
2288 sx_board[i].flags |= SX_BOARD_IS_PCI;
2289 if (!sx_probe(&sx_board[i]))
2290 found++;
2291 }
2292 /* May exit pci_get sequence early with lots of boards */
2293 if (pdev != NULL)
2294 pci_dev_put(pdev);
2295 }
2296#endif
2297
2298 if (!found) {
2299 sx_release_drivers();
2300 printk(KERN_INFO "sx: No specialix IO8+ boards detected.\n");
2301 func_exit();
2302 return -EIO;
2303 }
2304
2305 func_exit();
2306 return 0;
2307}
2308
2309static int iobase[SX_NBOARD] = {0,};
2310static int irq[SX_NBOARD] = {0,};
2311
2312module_param_array(iobase, int, NULL, 0);
2313module_param_array(irq, int, NULL, 0);
2314module_param(sx_debug, int, 0);
2315module_param(sx_rtscts, int, 0);
2316module_param(sx_rxfifo, int, 0);
2317
2318/*
2319 * You can setup up to 4 boards.
2320 * by specifying "iobase=0xXXX,0xXXX ..." as insmod parameter.
2321 * You should specify the IRQs too in that case "irq=....,...".
2322 *
2323 * More than 4 boards in one computer is not possible, as the card can
2324 * only use 4 different interrupts.
2325 *
2326 */
2327static int __init specialix_init_module(void)
2328{
2329 int i;
2330
2331 func_enter();
2332
2333 if (iobase[0] || iobase[1] || iobase[2] || iobase[3]) {
2334 for (i = 0; i < SX_NBOARD; i++) {
2335 sx_board[i].base = iobase[i];
2336 sx_board[i].irq = irq[i];
2337 sx_board[i].count = 0;
2338 }
2339 }
2340
2341 func_exit();
2342
2343 return specialix_init();
2344}
2345
2346static void __exit specialix_exit_module(void)
2347{
2348 int i;
2349
2350 func_enter();
2351
2352 sx_release_drivers();
2353 for (i = 0; i < SX_NBOARD; i++)
2354 if (sx_board[i].flags & SX_BOARD_PRESENT)
2355 sx_release_io_range(&sx_board[i]);
2356 func_exit();
2357}
2358
2359static struct pci_device_id specialx_pci_tbl[] __devinitdata = {
2360 { PCI_DEVICE(PCI_VENDOR_ID_SPECIALIX, PCI_DEVICE_ID_SPECIALIX_IO8) },
2361 { }
2362};
2363MODULE_DEVICE_TABLE(pci, specialx_pci_tbl);
2364
2365module_init(specialix_init_module);
2366module_exit(specialix_exit_module);
2367
2368MODULE_LICENSE("GPL");
2369MODULE_ALIAS_CHARDEV_MAJOR(SPECIALIX_NORMAL_MAJOR);
diff --git a/drivers/char/specialix_io8.h b/drivers/char/specialix_io8.h
deleted file mode 100644
index c63005274d9b..000000000000
--- a/drivers/char/specialix_io8.h
+++ /dev/null
@@ -1,140 +0,0 @@
1/*
2 * linux/drivers/char/specialix_io8.h --
3 * Specialix IO8+ multiport serial driver.
4 *
5 * Copyright (C) 1997 Roger Wolff (R.E.Wolff@BitWizard.nl)
6 * Copyright (C) 1994-1996 Dmitry Gorodchanin (pgmdsg@ibi.com)
7 *
8 *
9 * Specialix pays for the development and support of this driver.
10 * Please DO contact io8-linux@specialix.co.uk if you require
11 * support.
12 *
13 * This driver was developped in the BitWizard linux device
14 * driver service. If you require a linux device driver for your
15 * product, please contact devices@BitWizard.nl for a quote.
16 *
17 * This code is firmly based on the riscom/8 serial driver,
18 * written by Dmitry Gorodchanin. The specialix IO8+ card
19 * programming information was obtained from the CL-CD1865 Data
20 * Book, and Specialix document number 6200059: IO8+ Hardware
21 * Functional Specification.
22 *
23 * This program is free software; you can redistribute it and/or
24 * modify it under the terms of the GNU General Public License as
25 * published by the Free Software Foundation; either version 2 of
26 * the License, or (at your option) any later version.
27 *
28 * This program is distributed in the hope that it will be
29 * useful, but WITHOUT ANY WARRANTY; without even the implied
30 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
31 * PURPOSE. See the GNU General Public License for more details.
32 *
33 * You should have received a copy of the GNU General Public
34 * License along with this program; if not, write to the Free
35 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,
36 * USA.
37 * */
38
39#ifndef __LINUX_SPECIALIX_H
40#define __LINUX_SPECIALIX_H
41
42#include <linux/serial.h>
43
44#ifdef __KERNEL__
45
46/* You can have max 4 ISA cards in one PC, and I recommend not much
47more than a few PCI versions of the card. */
48
49#define SX_NBOARD 8
50
51/* NOTE: Specialix decoder recognizes 4 addresses, but only two are used.... */
52#define SX_IO_SPACE 4
53/* The PCI version decodes 8 addresses, but still only 2 are used. */
54#define SX_PCI_IO_SPACE 8
55
56/* eight ports per board. */
57#define SX_NPORT 8
58#define SX_BOARD(line) ((line) / SX_NPORT)
59#define SX_PORT(line) ((line) & (SX_NPORT - 1))
60
61
62#define SX_DATA_REG 0 /* Base+0 : Data register */
63#define SX_ADDR_REG 1 /* base+1 : Address register. */
64
65#define MHz *1000000 /* I'm ashamed of myself. */
66
67/* On-board oscillator frequency */
68#define SX_OSCFREQ (25 MHz/2)
69/* There is a 25MHz crystal on the board, but the chip is in /2 mode */
70
71
72/* Ticks per sec. Used for setting receiver timeout and break length */
73#define SPECIALIX_TPS 4000
74
75/* Yeah, after heavy testing I decided it must be 6.
76 * Sure, You can change it if needed.
77 */
78#define SPECIALIX_RXFIFO 6 /* Max. receiver FIFO size (1-8) */
79
80#define SPECIALIX_MAGIC 0x0907
81
82#define SX_CCR_TIMEOUT 10000 /* CCR timeout. You may need to wait upto
83 10 milliseconds before the internal
84 processor is available again after
85 you give it a command */
86
87#define SX_IOBASE1 0x100
88#define SX_IOBASE2 0x180
89#define SX_IOBASE3 0x250
90#define SX_IOBASE4 0x260
91
92struct specialix_board {
93 unsigned long flags;
94 unsigned short base;
95 unsigned char irq;
96 //signed char count;
97 int count;
98 unsigned char DTR;
99 int reg;
100 spinlock_t lock;
101};
102
103#define SX_BOARD_PRESENT 0x00000001
104#define SX_BOARD_ACTIVE 0x00000002
105#define SX_BOARD_IS_PCI 0x00000004
106
107
108struct specialix_port {
109 int magic;
110 struct tty_port port;
111 int baud_base;
112 int flags;
113 int timeout;
114 unsigned char * xmit_buf;
115 int custom_divisor;
116 int xmit_head;
117 int xmit_tail;
118 int xmit_cnt;
119 short wakeup_chars;
120 short break_length;
121 unsigned char mark_mask;
122 unsigned char IER;
123 unsigned char MSVR;
124 unsigned char COR2;
125 unsigned long overrun;
126 unsigned long hits[10];
127 spinlock_t lock;
128};
129
130#endif /* __KERNEL__ */
131#endif /* __LINUX_SPECIALIX_H */
132
133
134
135
136
137
138
139
140
diff --git a/drivers/char/stallion.c b/drivers/char/stallion.c
deleted file mode 100644
index f2167f8e5aab..000000000000
--- a/drivers/char/stallion.c
+++ /dev/null
@@ -1,4652 +0,0 @@
1/*****************************************************************************/
2
3/*
4 * stallion.c -- stallion multiport serial driver.
5 *
6 * Copyright (C) 1996-1999 Stallion Technologies
7 * Copyright (C) 1994-1996 Greg Ungerer.
8 *
9 * This code is loosely based on the Linux serial driver, written by
10 * Linus Torvalds, Theodore T'so and others.
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 */
26
27/*****************************************************************************/
28
29#include <linux/module.h>
30#include <linux/sched.h>
31#include <linux/slab.h>
32#include <linux/interrupt.h>
33#include <linux/tty.h>
34#include <linux/tty_flip.h>
35#include <linux/serial.h>
36#include <linux/seq_file.h>
37#include <linux/cd1400.h>
38#include <linux/sc26198.h>
39#include <linux/comstats.h>
40#include <linux/stallion.h>
41#include <linux/ioport.h>
42#include <linux/init.h>
43#include <linux/smp_lock.h>
44#include <linux/device.h>
45#include <linux/delay.h>
46#include <linux/ctype.h>
47
48#include <asm/io.h>
49#include <asm/uaccess.h>
50
51#include <linux/pci.h>
52
53/*****************************************************************************/
54
55/*
56 * Define different board types. Use the standard Stallion "assigned"
57 * board numbers. Boards supported in this driver are abbreviated as
58 * EIO = EasyIO and ECH = EasyConnection 8/32.
59 */
60#define BRD_EASYIO 20
61#define BRD_ECH 21
62#define BRD_ECHMC 22
63#define BRD_ECHPCI 26
64#define BRD_ECH64PCI 27
65#define BRD_EASYIOPCI 28
66
67struct stlconf {
68 unsigned int brdtype;
69 int ioaddr1;
70 int ioaddr2;
71 unsigned long memaddr;
72 int irq;
73 int irqtype;
74};
75
76static unsigned int stl_nrbrds;
77
78/*****************************************************************************/
79
80/*
81 * Define some important driver characteristics. Device major numbers
82 * allocated as per Linux Device Registry.
83 */
84#ifndef STL_SIOMEMMAJOR
85#define STL_SIOMEMMAJOR 28
86#endif
87#ifndef STL_SERIALMAJOR
88#define STL_SERIALMAJOR 24
89#endif
90#ifndef STL_CALLOUTMAJOR
91#define STL_CALLOUTMAJOR 25
92#endif
93
94/*
95 * Set the TX buffer size. Bigger is better, but we don't want
96 * to chew too much memory with buffers!
97 */
98#define STL_TXBUFLOW 512
99#define STL_TXBUFSIZE 4096
100
101/*****************************************************************************/
102
103/*
104 * Define our local driver identity first. Set up stuff to deal with
105 * all the local structures required by a serial tty driver.
106 */
107static char *stl_drvtitle = "Stallion Multiport Serial Driver";
108static char *stl_drvname = "stallion";
109static char *stl_drvversion = "5.6.0";
110
111static struct tty_driver *stl_serial;
112
113/*
114 * Define a local default termios struct. All ports will be created
115 * with this termios initially. Basically all it defines is a raw port
116 * at 9600, 8 data bits, 1 stop bit.
117 */
118static struct ktermios stl_deftermios = {
119 .c_cflag = (B9600 | CS8 | CREAD | HUPCL | CLOCAL),
120 .c_cc = INIT_C_CC,
121 .c_ispeed = 9600,
122 .c_ospeed = 9600,
123};
124
125/*
126 * Define global place to put buffer overflow characters.
127 */
128static char stl_unwanted[SC26198_RXFIFOSIZE];
129
130/*****************************************************************************/
131
132static DEFINE_MUTEX(stl_brdslock);
133static struct stlbrd *stl_brds[STL_MAXBRDS];
134
135static const struct tty_port_operations stl_port_ops;
136
137/*
138 * Per board state flags. Used with the state field of the board struct.
139 * Not really much here!
140 */
141#define BRD_FOUND 0x1
142#define STL_PROBED 0x2
143
144
145/*
146 * Define the port structure istate flags. These set of flags are
147 * modified at interrupt time - so setting and reseting them needs
148 * to be atomic. Use the bit clear/setting routines for this.
149 */
150#define ASYI_TXBUSY 1
151#define ASYI_TXLOW 2
152#define ASYI_TXFLOWED 3
153
154/*
155 * Define an array of board names as printable strings. Handy for
156 * referencing boards when printing trace and stuff.
157 */
158static char *stl_brdnames[] = {
159 NULL,
160 NULL,
161 NULL,
162 NULL,
163 NULL,
164 NULL,
165 NULL,
166 NULL,
167 NULL,
168 NULL,
169 NULL,
170 NULL,
171 NULL,
172 NULL,
173 NULL,
174 NULL,
175 NULL,
176 NULL,
177 NULL,
178 NULL,
179 "EasyIO",
180 "EC8/32-AT",
181 "EC8/32-MC",
182 NULL,
183 NULL,
184 NULL,
185 "EC8/32-PCI",
186 "EC8/64-PCI",
187 "EasyIO-PCI",
188};
189
190/*****************************************************************************/
191
192/*
193 * Define some string labels for arguments passed from the module
194 * load line. These allow for easy board definitions, and easy
195 * modification of the io, memory and irq resoucres.
196 */
197static unsigned int stl_nargs;
198static char *board0[4];
199static char *board1[4];
200static char *board2[4];
201static char *board3[4];
202
203static char **stl_brdsp[] = {
204 (char **) &board0,
205 (char **) &board1,
206 (char **) &board2,
207 (char **) &board3
208};
209
210/*
211 * Define a set of common board names, and types. This is used to
212 * parse any module arguments.
213 */
214
215static struct {
216 char *name;
217 int type;
218} stl_brdstr[] = {
219 { "easyio", BRD_EASYIO },
220 { "eio", BRD_EASYIO },
221 { "20", BRD_EASYIO },
222 { "ec8/32", BRD_ECH },
223 { "ec8/32-at", BRD_ECH },
224 { "ec8/32-isa", BRD_ECH },
225 { "ech", BRD_ECH },
226 { "echat", BRD_ECH },
227 { "21", BRD_ECH },
228 { "ec8/32-mc", BRD_ECHMC },
229 { "ec8/32-mca", BRD_ECHMC },
230 { "echmc", BRD_ECHMC },
231 { "echmca", BRD_ECHMC },
232 { "22", BRD_ECHMC },
233 { "ec8/32-pc", BRD_ECHPCI },
234 { "ec8/32-pci", BRD_ECHPCI },
235 { "26", BRD_ECHPCI },
236 { "ec8/64-pc", BRD_ECH64PCI },
237 { "ec8/64-pci", BRD_ECH64PCI },
238 { "ech-pci", BRD_ECH64PCI },
239 { "echpci", BRD_ECH64PCI },
240 { "echpc", BRD_ECH64PCI },
241 { "27", BRD_ECH64PCI },
242 { "easyio-pc", BRD_EASYIOPCI },
243 { "easyio-pci", BRD_EASYIOPCI },
244 { "eio-pci", BRD_EASYIOPCI },
245 { "eiopci", BRD_EASYIOPCI },
246 { "28", BRD_EASYIOPCI },
247};
248
249/*
250 * Define the module agruments.
251 */
252
253module_param_array(board0, charp, &stl_nargs, 0);
254MODULE_PARM_DESC(board0, "Board 0 config -> name[,ioaddr[,ioaddr2][,irq]]");
255module_param_array(board1, charp, &stl_nargs, 0);
256MODULE_PARM_DESC(board1, "Board 1 config -> name[,ioaddr[,ioaddr2][,irq]]");
257module_param_array(board2, charp, &stl_nargs, 0);
258MODULE_PARM_DESC(board2, "Board 2 config -> name[,ioaddr[,ioaddr2][,irq]]");
259module_param_array(board3, charp, &stl_nargs, 0);
260MODULE_PARM_DESC(board3, "Board 3 config -> name[,ioaddr[,ioaddr2][,irq]]");
261
262/*****************************************************************************/
263
264/*
265 * Hardware ID bits for the EasyIO and ECH boards. These defines apply
266 * to the directly accessible io ports of these boards (not the uarts -
267 * they are in cd1400.h and sc26198.h).
268 */
269#define EIO_8PORTRS 0x04
270#define EIO_4PORTRS 0x05
271#define EIO_8PORTDI 0x00
272#define EIO_8PORTM 0x06
273#define EIO_MK3 0x03
274#define EIO_IDBITMASK 0x07
275
276#define EIO_BRDMASK 0xf0
277#define ID_BRD4 0x10
278#define ID_BRD8 0x20
279#define ID_BRD16 0x30
280
281#define EIO_INTRPEND 0x08
282#define EIO_INTEDGE 0x00
283#define EIO_INTLEVEL 0x08
284#define EIO_0WS 0x10
285
286#define ECH_ID 0xa0
287#define ECH_IDBITMASK 0xe0
288#define ECH_BRDENABLE 0x08
289#define ECH_BRDDISABLE 0x00
290#define ECH_INTENABLE 0x01
291#define ECH_INTDISABLE 0x00
292#define ECH_INTLEVEL 0x02
293#define ECH_INTEDGE 0x00
294#define ECH_INTRPEND 0x01
295#define ECH_BRDRESET 0x01
296
297#define ECHMC_INTENABLE 0x01
298#define ECHMC_BRDRESET 0x02
299
300#define ECH_PNLSTATUS 2
301#define ECH_PNL16PORT 0x20
302#define ECH_PNLIDMASK 0x07
303#define ECH_PNLXPID 0x40
304#define ECH_PNLINTRPEND 0x80
305
306#define ECH_ADDR2MASK 0x1e0
307
308/*
309 * Define the vector mapping bits for the programmable interrupt board
310 * hardware. These bits encode the interrupt for the board to use - it
311 * is software selectable (except the EIO-8M).
312 */
313static unsigned char stl_vecmap[] = {
314 0xff, 0xff, 0xff, 0x04, 0x06, 0x05, 0xff, 0x07,
315 0xff, 0xff, 0x00, 0x02, 0x01, 0xff, 0xff, 0x03
316};
317
318/*
319 * Lock ordering is that you may not take stallion_lock holding
320 * brd_lock.
321 */
322
323static spinlock_t brd_lock; /* Guard the board mapping */
324static spinlock_t stallion_lock; /* Guard the tty driver */
325
326/*
327 * Set up enable and disable macros for the ECH boards. They require
328 * the secondary io address space to be activated and deactivated.
329 * This way all ECH boards can share their secondary io region.
330 * If this is an ECH-PCI board then also need to set the page pointer
331 * to point to the correct page.
332 */
333#define BRDENABLE(brdnr,pagenr) \
334 if (stl_brds[(brdnr)]->brdtype == BRD_ECH) \
335 outb((stl_brds[(brdnr)]->ioctrlval | ECH_BRDENABLE), \
336 stl_brds[(brdnr)]->ioctrl); \
337 else if (stl_brds[(brdnr)]->brdtype == BRD_ECHPCI) \
338 outb((pagenr), stl_brds[(brdnr)]->ioctrl);
339
340#define BRDDISABLE(brdnr) \
341 if (stl_brds[(brdnr)]->brdtype == BRD_ECH) \
342 outb((stl_brds[(brdnr)]->ioctrlval | ECH_BRDDISABLE), \
343 stl_brds[(brdnr)]->ioctrl);
344
345#define STL_CD1400MAXBAUD 230400
346#define STL_SC26198MAXBAUD 460800
347
348#define STL_BAUDBASE 115200
349#define STL_CLOSEDELAY (5 * HZ / 10)
350
351/*****************************************************************************/
352
353/*
354 * Define the Stallion PCI vendor and device IDs.
355 */
356#ifndef PCI_VENDOR_ID_STALLION
357#define PCI_VENDOR_ID_STALLION 0x124d
358#endif
359#ifndef PCI_DEVICE_ID_ECHPCI832
360#define PCI_DEVICE_ID_ECHPCI832 0x0000
361#endif
362#ifndef PCI_DEVICE_ID_ECHPCI864
363#define PCI_DEVICE_ID_ECHPCI864 0x0002
364#endif
365#ifndef PCI_DEVICE_ID_EIOPCI
366#define PCI_DEVICE_ID_EIOPCI 0x0003
367#endif
368
369/*
370 * Define structure to hold all Stallion PCI boards.
371 */
372
373static struct pci_device_id stl_pcibrds[] = {
374 { PCI_DEVICE(PCI_VENDOR_ID_STALLION, PCI_DEVICE_ID_ECHPCI864),
375 .driver_data = BRD_ECH64PCI },
376 { PCI_DEVICE(PCI_VENDOR_ID_STALLION, PCI_DEVICE_ID_EIOPCI),
377 .driver_data = BRD_EASYIOPCI },
378 { PCI_DEVICE(PCI_VENDOR_ID_STALLION, PCI_DEVICE_ID_ECHPCI832),
379 .driver_data = BRD_ECHPCI },
380 { PCI_DEVICE(PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_87410),
381 .driver_data = BRD_ECHPCI },
382 { }
383};
384MODULE_DEVICE_TABLE(pci, stl_pcibrds);
385
386/*****************************************************************************/
387
388/*
389 * Define macros to extract a brd/port number from a minor number.
390 */
391#define MINOR2BRD(min) (((min) & 0xc0) >> 6)
392#define MINOR2PORT(min) ((min) & 0x3f)
393
394/*
395 * Define a baud rate table that converts termios baud rate selector
396 * into the actual baud rate value. All baud rate calculations are
397 * based on the actual baud rate required.
398 */
399static unsigned int stl_baudrates[] = {
400 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
401 9600, 19200, 38400, 57600, 115200, 230400, 460800, 921600
402};
403
404/*****************************************************************************/
405
406/*
407 * Declare all those functions in this driver!
408 */
409
410static long stl_memioctl(struct file *fp, unsigned int cmd, unsigned long arg);
411static int stl_brdinit(struct stlbrd *brdp);
412static int stl_getportstats(struct tty_struct *tty, struct stlport *portp, comstats_t __user *cp);
413static int stl_clrportstats(struct stlport *portp, comstats_t __user *cp);
414
415/*
416 * CD1400 uart specific handling functions.
417 */
418static void stl_cd1400setreg(struct stlport *portp, int regnr, int value);
419static int stl_cd1400getreg(struct stlport *portp, int regnr);
420static int stl_cd1400updatereg(struct stlport *portp, int regnr, int value);
421static int stl_cd1400panelinit(struct stlbrd *brdp, struct stlpanel *panelp);
422static void stl_cd1400portinit(struct stlbrd *brdp, struct stlpanel *panelp, struct stlport *portp);
423static void stl_cd1400setport(struct stlport *portp, struct ktermios *tiosp);
424static int stl_cd1400getsignals(struct stlport *portp);
425static void stl_cd1400setsignals(struct stlport *portp, int dtr, int rts);
426static void stl_cd1400ccrwait(struct stlport *portp);
427static void stl_cd1400enablerxtx(struct stlport *portp, int rx, int tx);
428static void stl_cd1400startrxtx(struct stlport *portp, int rx, int tx);
429static void stl_cd1400disableintrs(struct stlport *portp);
430static void stl_cd1400sendbreak(struct stlport *portp, int len);
431static void stl_cd1400flowctrl(struct stlport *portp, int state);
432static void stl_cd1400sendflow(struct stlport *portp, int state);
433static void stl_cd1400flush(struct stlport *portp);
434static int stl_cd1400datastate(struct stlport *portp);
435static void stl_cd1400eiointr(struct stlpanel *panelp, unsigned int iobase);
436static void stl_cd1400echintr(struct stlpanel *panelp, unsigned int iobase);
437static void stl_cd1400txisr(struct stlpanel *panelp, int ioaddr);
438static void stl_cd1400rxisr(struct stlpanel *panelp, int ioaddr);
439static void stl_cd1400mdmisr(struct stlpanel *panelp, int ioaddr);
440
441static inline int stl_cd1400breakisr(struct stlport *portp, int ioaddr);
442
443/*
444 * SC26198 uart specific handling functions.
445 */
446static void stl_sc26198setreg(struct stlport *portp, int regnr, int value);
447static int stl_sc26198getreg(struct stlport *portp, int regnr);
448static int stl_sc26198updatereg(struct stlport *portp, int regnr, int value);
449static int stl_sc26198getglobreg(struct stlport *portp, int regnr);
450static int stl_sc26198panelinit(struct stlbrd *brdp, struct stlpanel *panelp);
451static void stl_sc26198portinit(struct stlbrd *brdp, struct stlpanel *panelp, struct stlport *portp);
452static void stl_sc26198setport(struct stlport *portp, struct ktermios *tiosp);
453static int stl_sc26198getsignals(struct stlport *portp);
454static void stl_sc26198setsignals(struct stlport *portp, int dtr, int rts);
455static void stl_sc26198enablerxtx(struct stlport *portp, int rx, int tx);
456static void stl_sc26198startrxtx(struct stlport *portp, int rx, int tx);
457static void stl_sc26198disableintrs(struct stlport *portp);
458static void stl_sc26198sendbreak(struct stlport *portp, int len);
459static void stl_sc26198flowctrl(struct stlport *portp, int state);
460static void stl_sc26198sendflow(struct stlport *portp, int state);
461static void stl_sc26198flush(struct stlport *portp);
462static int stl_sc26198datastate(struct stlport *portp);
463static void stl_sc26198wait(struct stlport *portp);
464static void stl_sc26198txunflow(struct stlport *portp, struct tty_struct *tty);
465static void stl_sc26198intr(struct stlpanel *panelp, unsigned int iobase);
466static void stl_sc26198txisr(struct stlport *port);
467static void stl_sc26198rxisr(struct stlport *port, unsigned int iack);
468static void stl_sc26198rxbadch(struct stlport *portp, unsigned char status, char ch);
469static void stl_sc26198rxbadchars(struct stlport *portp);
470static void stl_sc26198otherisr(struct stlport *port, unsigned int iack);
471
472/*****************************************************************************/
473
474/*
475 * Generic UART support structure.
476 */
477typedef struct uart {
478 int (*panelinit)(struct stlbrd *brdp, struct stlpanel *panelp);
479 void (*portinit)(struct stlbrd *brdp, struct stlpanel *panelp, struct stlport *portp);
480 void (*setport)(struct stlport *portp, struct ktermios *tiosp);
481 int (*getsignals)(struct stlport *portp);
482 void (*setsignals)(struct stlport *portp, int dtr, int rts);
483 void (*enablerxtx)(struct stlport *portp, int rx, int tx);
484 void (*startrxtx)(struct stlport *portp, int rx, int tx);
485 void (*disableintrs)(struct stlport *portp);
486 void (*sendbreak)(struct stlport *portp, int len);
487 void (*flowctrl)(struct stlport *portp, int state);
488 void (*sendflow)(struct stlport *portp, int state);
489 void (*flush)(struct stlport *portp);
490 int (*datastate)(struct stlport *portp);
491 void (*intr)(struct stlpanel *panelp, unsigned int iobase);
492} uart_t;
493
494/*
495 * Define some macros to make calling these functions nice and clean.
496 */
497#define stl_panelinit (* ((uart_t *) panelp->uartp)->panelinit)
498#define stl_portinit (* ((uart_t *) portp->uartp)->portinit)
499#define stl_setport (* ((uart_t *) portp->uartp)->setport)
500#define stl_getsignals (* ((uart_t *) portp->uartp)->getsignals)
501#define stl_setsignals (* ((uart_t *) portp->uartp)->setsignals)
502#define stl_enablerxtx (* ((uart_t *) portp->uartp)->enablerxtx)
503#define stl_startrxtx (* ((uart_t *) portp->uartp)->startrxtx)
504#define stl_disableintrs (* ((uart_t *) portp->uartp)->disableintrs)
505#define stl_sendbreak (* ((uart_t *) portp->uartp)->sendbreak)
506#define stl_flowctrl (* ((uart_t *) portp->uartp)->flowctrl)
507#define stl_sendflow (* ((uart_t *) portp->uartp)->sendflow)
508#define stl_flush (* ((uart_t *) portp->uartp)->flush)
509#define stl_datastate (* ((uart_t *) portp->uartp)->datastate)
510
511/*****************************************************************************/
512
513/*
514 * CD1400 UART specific data initialization.
515 */
516static uart_t stl_cd1400uart = {
517 stl_cd1400panelinit,
518 stl_cd1400portinit,
519 stl_cd1400setport,
520 stl_cd1400getsignals,
521 stl_cd1400setsignals,
522 stl_cd1400enablerxtx,
523 stl_cd1400startrxtx,
524 stl_cd1400disableintrs,
525 stl_cd1400sendbreak,
526 stl_cd1400flowctrl,
527 stl_cd1400sendflow,
528 stl_cd1400flush,
529 stl_cd1400datastate,
530 stl_cd1400eiointr
531};
532
533/*
534 * Define the offsets within the register bank of a cd1400 based panel.
535 * These io address offsets are common to the EasyIO board as well.
536 */
537#define EREG_ADDR 0
538#define EREG_DATA 4
539#define EREG_RXACK 5
540#define EREG_TXACK 6
541#define EREG_MDACK 7
542
543#define EREG_BANKSIZE 8
544
545#define CD1400_CLK 25000000
546#define CD1400_CLK8M 20000000
547
548/*
549 * Define the cd1400 baud rate clocks. These are used when calculating
550 * what clock and divisor to use for the required baud rate. Also
551 * define the maximum baud rate allowed, and the default base baud.
552 */
553static int stl_cd1400clkdivs[] = {
554 CD1400_CLK0, CD1400_CLK1, CD1400_CLK2, CD1400_CLK3, CD1400_CLK4
555};
556
557/*****************************************************************************/
558
559/*
560 * SC26198 UART specific data initization.
561 */
562static uart_t stl_sc26198uart = {
563 stl_sc26198panelinit,
564 stl_sc26198portinit,
565 stl_sc26198setport,
566 stl_sc26198getsignals,
567 stl_sc26198setsignals,
568 stl_sc26198enablerxtx,
569 stl_sc26198startrxtx,
570 stl_sc26198disableintrs,
571 stl_sc26198sendbreak,
572 stl_sc26198flowctrl,
573 stl_sc26198sendflow,
574 stl_sc26198flush,
575 stl_sc26198datastate,
576 stl_sc26198intr
577};
578
579/*
580 * Define the offsets within the register bank of a sc26198 based panel.
581 */
582#define XP_DATA 0
583#define XP_ADDR 1
584#define XP_MODID 2
585#define XP_STATUS 2
586#define XP_IACK 3
587
588#define XP_BANKSIZE 4
589
590/*
591 * Define the sc26198 baud rate table. Offsets within the table
592 * represent the actual baud rate selector of sc26198 registers.
593 */
594static unsigned int sc26198_baudtable[] = {
595 50, 75, 150, 200, 300, 450, 600, 900, 1200, 1800, 2400, 3600,
596 4800, 7200, 9600, 14400, 19200, 28800, 38400, 57600, 115200,
597 230400, 460800, 921600
598};
599
600#define SC26198_NRBAUDS ARRAY_SIZE(sc26198_baudtable)
601
602/*****************************************************************************/
603
604/*
605 * Define the driver info for a user level control device. Used mainly
606 * to get at port stats - only not using the port device itself.
607 */
608static const struct file_operations stl_fsiomem = {
609 .owner = THIS_MODULE,
610 .unlocked_ioctl = stl_memioctl,
611};
612
613static struct class *stallion_class;
614
615static void stl_cd_change(struct stlport *portp)
616{
617 unsigned int oldsigs = portp->sigs;
618 struct tty_struct *tty = tty_port_tty_get(&portp->port);
619
620 if (!tty)
621 return;
622
623 portp->sigs = stl_getsignals(portp);
624
625 if ((portp->sigs & TIOCM_CD) && ((oldsigs & TIOCM_CD) == 0))
626 wake_up_interruptible(&portp->port.open_wait);
627
628 if ((oldsigs & TIOCM_CD) && ((portp->sigs & TIOCM_CD) == 0))
629 if (portp->port.flags & ASYNC_CHECK_CD)
630 tty_hangup(tty);
631 tty_kref_put(tty);
632}
633
634/*
635 * Check for any arguments passed in on the module load command line.
636 */
637
638/*****************************************************************************/
639
640/*
641 * Parse the supplied argument string, into the board conf struct.
642 */
643
644static int __init stl_parsebrd(struct stlconf *confp, char **argp)
645{
646 char *sp;
647 unsigned int i;
648
649 pr_debug("stl_parsebrd(confp=%p,argp=%p)\n", confp, argp);
650
651 if ((argp[0] == NULL) || (*argp[0] == 0))
652 return 0;
653
654 for (sp = argp[0], i = 0; (*sp != 0) && (i < 25); sp++, i++)
655 *sp = tolower(*sp);
656
657 for (i = 0; i < ARRAY_SIZE(stl_brdstr); i++)
658 if (strcmp(stl_brdstr[i].name, argp[0]) == 0)
659 break;
660
661 if (i == ARRAY_SIZE(stl_brdstr)) {
662 printk("STALLION: unknown board name, %s?\n", argp[0]);
663 return 0;
664 }
665
666 confp->brdtype = stl_brdstr[i].type;
667
668 i = 1;
669 if ((argp[i] != NULL) && (*argp[i] != 0))
670 confp->ioaddr1 = simple_strtoul(argp[i], NULL, 0);
671 i++;
672 if (confp->brdtype == BRD_ECH) {
673 if ((argp[i] != NULL) && (*argp[i] != 0))
674 confp->ioaddr2 = simple_strtoul(argp[i], NULL, 0);
675 i++;
676 }
677 if ((argp[i] != NULL) && (*argp[i] != 0))
678 confp->irq = simple_strtoul(argp[i], NULL, 0);
679 return 1;
680}
681
682/*****************************************************************************/
683
684/*
685 * Allocate a new board structure. Fill out the basic info in it.
686 */
687
688static struct stlbrd *stl_allocbrd(void)
689{
690 struct stlbrd *brdp;
691
692 brdp = kzalloc(sizeof(struct stlbrd), GFP_KERNEL);
693 if (!brdp) {
694 printk("STALLION: failed to allocate memory (size=%Zd)\n",
695 sizeof(struct stlbrd));
696 return NULL;
697 }
698
699 brdp->magic = STL_BOARDMAGIC;
700 return brdp;
701}
702
703/*****************************************************************************/
704
705static int stl_activate(struct tty_port *port, struct tty_struct *tty)
706{
707 struct stlport *portp = container_of(port, struct stlport, port);
708 if (!portp->tx.buf) {
709 portp->tx.buf = kmalloc(STL_TXBUFSIZE, GFP_KERNEL);
710 if (!portp->tx.buf)
711 return -ENOMEM;
712 portp->tx.head = portp->tx.buf;
713 portp->tx.tail = portp->tx.buf;
714 }
715 stl_setport(portp, tty->termios);
716 portp->sigs = stl_getsignals(portp);
717 stl_setsignals(portp, 1, 1);
718 stl_enablerxtx(portp, 1, 1);
719 stl_startrxtx(portp, 1, 0);
720 return 0;
721}
722
723static int stl_open(struct tty_struct *tty, struct file *filp)
724{
725 struct stlport *portp;
726 struct stlbrd *brdp;
727 unsigned int minordev, brdnr, panelnr;
728 int portnr;
729
730 pr_debug("stl_open(tty=%p,filp=%p): device=%s\n", tty, filp, tty->name);
731
732 minordev = tty->index;
733 brdnr = MINOR2BRD(minordev);
734 if (brdnr >= stl_nrbrds)
735 return -ENODEV;
736 brdp = stl_brds[brdnr];
737 if (brdp == NULL)
738 return -ENODEV;
739
740 minordev = MINOR2PORT(minordev);
741 for (portnr = -1, panelnr = 0; panelnr < STL_MAXPANELS; panelnr++) {
742 if (brdp->panels[panelnr] == NULL)
743 break;
744 if (minordev < brdp->panels[panelnr]->nrports) {
745 portnr = minordev;
746 break;
747 }
748 minordev -= brdp->panels[panelnr]->nrports;
749 }
750 if (portnr < 0)
751 return -ENODEV;
752
753 portp = brdp->panels[panelnr]->ports[portnr];
754 if (portp == NULL)
755 return -ENODEV;
756
757 tty->driver_data = portp;
758 return tty_port_open(&portp->port, tty, filp);
759
760}
761
762/*****************************************************************************/
763
764static int stl_carrier_raised(struct tty_port *port)
765{
766 struct stlport *portp = container_of(port, struct stlport, port);
767 return (portp->sigs & TIOCM_CD) ? 1 : 0;
768}
769
770static void stl_dtr_rts(struct tty_port *port, int on)
771{
772 struct stlport *portp = container_of(port, struct stlport, port);
773 /* Takes brd_lock internally */
774 stl_setsignals(portp, on, on);
775}
776
777/*****************************************************************************/
778
779static void stl_flushbuffer(struct tty_struct *tty)
780{
781 struct stlport *portp;
782
783 pr_debug("stl_flushbuffer(tty=%p)\n", tty);
784
785 portp = tty->driver_data;
786 if (portp == NULL)
787 return;
788
789 stl_flush(portp);
790 tty_wakeup(tty);
791}
792
793/*****************************************************************************/
794
795static void stl_waituntilsent(struct tty_struct *tty, int timeout)
796{
797 struct stlport *portp;
798 unsigned long tend;
799
800 pr_debug("stl_waituntilsent(tty=%p,timeout=%d)\n", tty, timeout);
801
802 portp = tty->driver_data;
803 if (portp == NULL)
804 return;
805
806 if (timeout == 0)
807 timeout = HZ;
808 tend = jiffies + timeout;
809
810 while (stl_datastate(portp)) {
811 if (signal_pending(current))
812 break;
813 msleep_interruptible(20);
814 if (time_after_eq(jiffies, tend))
815 break;
816 }
817}
818
819/*****************************************************************************/
820
821static void stl_shutdown(struct tty_port *port)
822{
823 struct stlport *portp = container_of(port, struct stlport, port);
824 stl_disableintrs(portp);
825 stl_enablerxtx(portp, 0, 0);
826 stl_flush(portp);
827 portp->istate = 0;
828 if (portp->tx.buf != NULL) {
829 kfree(portp->tx.buf);
830 portp->tx.buf = NULL;
831 portp->tx.head = NULL;
832 portp->tx.tail = NULL;
833 }
834}
835
836static void stl_close(struct tty_struct *tty, struct file *filp)
837{
838 struct stlport*portp;
839 pr_debug("stl_close(tty=%p,filp=%p)\n", tty, filp);
840
841 portp = tty->driver_data;
842 if(portp == NULL)
843 return;
844 tty_port_close(&portp->port, tty, filp);
845}
846
847/*****************************************************************************/
848
849/*
850 * Write routine. Take data and stuff it in to the TX ring queue.
851 * If transmit interrupts are not running then start them.
852 */
853
854static int stl_write(struct tty_struct *tty, const unsigned char *buf, int count)
855{
856 struct stlport *portp;
857 unsigned int len, stlen;
858 unsigned char *chbuf;
859 char *head, *tail;
860
861 pr_debug("stl_write(tty=%p,buf=%p,count=%d)\n", tty, buf, count);
862
863 portp = tty->driver_data;
864 if (portp == NULL)
865 return 0;
866 if (portp->tx.buf == NULL)
867 return 0;
868
869/*
870 * If copying direct from user space we must cater for page faults,
871 * causing us to "sleep" here for a while. To handle this copy in all
872 * the data we need now, into a local buffer. Then when we got it all
873 * copy it into the TX buffer.
874 */
875 chbuf = (unsigned char *) buf;
876
877 head = portp->tx.head;
878 tail = portp->tx.tail;
879 if (head >= tail) {
880 len = STL_TXBUFSIZE - (head - tail) - 1;
881 stlen = STL_TXBUFSIZE - (head - portp->tx.buf);
882 } else {
883 len = tail - head - 1;
884 stlen = len;
885 }
886
887 len = min(len, (unsigned int)count);
888 count = 0;
889 while (len > 0) {
890 stlen = min(len, stlen);
891 memcpy(head, chbuf, stlen);
892 len -= stlen;
893 chbuf += stlen;
894 count += stlen;
895 head += stlen;
896 if (head >= (portp->tx.buf + STL_TXBUFSIZE)) {
897 head = portp->tx.buf;
898 stlen = tail - head;
899 }
900 }
901 portp->tx.head = head;
902
903 clear_bit(ASYI_TXLOW, &portp->istate);
904 stl_startrxtx(portp, -1, 1);
905
906 return count;
907}
908
909/*****************************************************************************/
910
911static int stl_putchar(struct tty_struct *tty, unsigned char ch)
912{
913 struct stlport *portp;
914 unsigned int len;
915 char *head, *tail;
916
917 pr_debug("stl_putchar(tty=%p,ch=%x)\n", tty, ch);
918
919 portp = tty->driver_data;
920 if (portp == NULL)
921 return -EINVAL;
922 if (portp->tx.buf == NULL)
923 return -EINVAL;
924
925 head = portp->tx.head;
926 tail = portp->tx.tail;
927
928 len = (head >= tail) ? (STL_TXBUFSIZE - (head - tail)) : (tail - head);
929 len--;
930
931 if (len > 0) {
932 *head++ = ch;
933 if (head >= (portp->tx.buf + STL_TXBUFSIZE))
934 head = portp->tx.buf;
935 }
936 portp->tx.head = head;
937 return 0;
938}
939
940/*****************************************************************************/
941
942/*
943 * If there are any characters in the buffer then make sure that TX
944 * interrupts are on and get'em out. Normally used after the putchar
945 * routine has been called.
946 */
947
948static void stl_flushchars(struct tty_struct *tty)
949{
950 struct stlport *portp;
951
952 pr_debug("stl_flushchars(tty=%p)\n", tty);
953
954 portp = tty->driver_data;
955 if (portp == NULL)
956 return;
957 if (portp->tx.buf == NULL)
958 return;
959
960 stl_startrxtx(portp, -1, 1);
961}
962
963/*****************************************************************************/
964
965static int stl_writeroom(struct tty_struct *tty)
966{
967 struct stlport *portp;
968 char *head, *tail;
969
970 pr_debug("stl_writeroom(tty=%p)\n", tty);
971
972 portp = tty->driver_data;
973 if (portp == NULL)
974 return 0;
975 if (portp->tx.buf == NULL)
976 return 0;
977
978 head = portp->tx.head;
979 tail = portp->tx.tail;
980 return (head >= tail) ? (STL_TXBUFSIZE - (head - tail) - 1) : (tail - head - 1);
981}
982
983/*****************************************************************************/
984
985/*
986 * Return number of chars in the TX buffer. Normally we would just
987 * calculate the number of chars in the buffer and return that, but if
988 * the buffer is empty and TX interrupts are still on then we return
989 * that the buffer still has 1 char in it. This way whoever called us
990 * will not think that ALL chars have drained - since the UART still
991 * must have some chars in it (we are busy after all).
992 */
993
994static int stl_charsinbuffer(struct tty_struct *tty)
995{
996 struct stlport *portp;
997 unsigned int size;
998 char *head, *tail;
999
1000 pr_debug("stl_charsinbuffer(tty=%p)\n", tty);
1001
1002 portp = tty->driver_data;
1003 if (portp == NULL)
1004 return 0;
1005 if (portp->tx.buf == NULL)
1006 return 0;
1007
1008 head = portp->tx.head;
1009 tail = portp->tx.tail;
1010 size = (head >= tail) ? (head - tail) : (STL_TXBUFSIZE - (tail - head));
1011 if ((size == 0) && test_bit(ASYI_TXBUSY, &portp->istate))
1012 size = 1;
1013 return size;
1014}
1015
1016/*****************************************************************************/
1017
1018/*
1019 * Generate the serial struct info.
1020 */
1021
1022static int stl_getserial(struct stlport *portp, struct serial_struct __user *sp)
1023{
1024 struct serial_struct sio;
1025 struct stlbrd *brdp;
1026
1027 pr_debug("stl_getserial(portp=%p,sp=%p)\n", portp, sp);
1028
1029 memset(&sio, 0, sizeof(struct serial_struct));
1030
1031 mutex_lock(&portp->port.mutex);
1032 sio.line = portp->portnr;
1033 sio.port = portp->ioaddr;
1034 sio.flags = portp->port.flags;
1035 sio.baud_base = portp->baud_base;
1036 sio.close_delay = portp->close_delay;
1037 sio.closing_wait = portp->closing_wait;
1038 sio.custom_divisor = portp->custom_divisor;
1039 sio.hub6 = 0;
1040 if (portp->uartp == &stl_cd1400uart) {
1041 sio.type = PORT_CIRRUS;
1042 sio.xmit_fifo_size = CD1400_TXFIFOSIZE;
1043 } else {
1044 sio.type = PORT_UNKNOWN;
1045 sio.xmit_fifo_size = SC26198_TXFIFOSIZE;
1046 }
1047
1048 brdp = stl_brds[portp->brdnr];
1049 if (brdp != NULL)
1050 sio.irq = brdp->irq;
1051 mutex_unlock(&portp->port.mutex);
1052
1053 return copy_to_user(sp, &sio, sizeof(struct serial_struct)) ? -EFAULT : 0;
1054}
1055
1056/*****************************************************************************/
1057
1058/*
1059 * Set port according to the serial struct info.
1060 * At this point we do not do any auto-configure stuff, so we will
1061 * just quietly ignore any requests to change irq, etc.
1062 */
1063
1064static int stl_setserial(struct tty_struct *tty, struct serial_struct __user *sp)
1065{
1066 struct stlport * portp = tty->driver_data;
1067 struct serial_struct sio;
1068
1069 pr_debug("stl_setserial(portp=%p,sp=%p)\n", portp, sp);
1070
1071 if (copy_from_user(&sio, sp, sizeof(struct serial_struct)))
1072 return -EFAULT;
1073 mutex_lock(&portp->port.mutex);
1074 if (!capable(CAP_SYS_ADMIN)) {
1075 if ((sio.baud_base != portp->baud_base) ||
1076 (sio.close_delay != portp->close_delay) ||
1077 ((sio.flags & ~ASYNC_USR_MASK) !=
1078 (portp->port.flags & ~ASYNC_USR_MASK))) {
1079 mutex_unlock(&portp->port.mutex);
1080 return -EPERM;
1081 }
1082 }
1083
1084 portp->port.flags = (portp->port.flags & ~ASYNC_USR_MASK) |
1085 (sio.flags & ASYNC_USR_MASK);
1086 portp->baud_base = sio.baud_base;
1087 portp->close_delay = sio.close_delay;
1088 portp->closing_wait = sio.closing_wait;
1089 portp->custom_divisor = sio.custom_divisor;
1090 mutex_unlock(&portp->port.mutex);
1091 stl_setport(portp, tty->termios);
1092 return 0;
1093}
1094
1095/*****************************************************************************/
1096
1097static int stl_tiocmget(struct tty_struct *tty, struct file *file)
1098{
1099 struct stlport *portp;
1100
1101 portp = tty->driver_data;
1102 if (portp == NULL)
1103 return -ENODEV;
1104 if (tty->flags & (1 << TTY_IO_ERROR))
1105 return -EIO;
1106
1107 return stl_getsignals(portp);
1108}
1109
1110static int stl_tiocmset(struct tty_struct *tty, struct file *file,
1111 unsigned int set, unsigned int clear)
1112{
1113 struct stlport *portp;
1114 int rts = -1, dtr = -1;
1115
1116 portp = tty->driver_data;
1117 if (portp == NULL)
1118 return -ENODEV;
1119 if (tty->flags & (1 << TTY_IO_ERROR))
1120 return -EIO;
1121
1122 if (set & TIOCM_RTS)
1123 rts = 1;
1124 if (set & TIOCM_DTR)
1125 dtr = 1;
1126 if (clear & TIOCM_RTS)
1127 rts = 0;
1128 if (clear & TIOCM_DTR)
1129 dtr = 0;
1130
1131 stl_setsignals(portp, dtr, rts);
1132 return 0;
1133}
1134
1135static int stl_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
1136{
1137 struct stlport *portp;
1138 int rc;
1139 void __user *argp = (void __user *)arg;
1140
1141 pr_debug("stl_ioctl(tty=%p,file=%p,cmd=%x,arg=%lx)\n", tty, file, cmd,
1142 arg);
1143
1144 portp = tty->driver_data;
1145 if (portp == NULL)
1146 return -ENODEV;
1147
1148 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1149 (cmd != COM_GETPORTSTATS) && (cmd != COM_CLRPORTSTATS))
1150 if (tty->flags & (1 << TTY_IO_ERROR))
1151 return -EIO;
1152
1153 rc = 0;
1154
1155 switch (cmd) {
1156 case TIOCGSERIAL:
1157 rc = stl_getserial(portp, argp);
1158 break;
1159 case TIOCSSERIAL:
1160 rc = stl_setserial(tty, argp);
1161 break;
1162 case COM_GETPORTSTATS:
1163 rc = stl_getportstats(tty, portp, argp);
1164 break;
1165 case COM_CLRPORTSTATS:
1166 rc = stl_clrportstats(portp, argp);
1167 break;
1168 case TIOCSERCONFIG:
1169 case TIOCSERGWILD:
1170 case TIOCSERSWILD:
1171 case TIOCSERGETLSR:
1172 case TIOCSERGSTRUCT:
1173 case TIOCSERGETMULTI:
1174 case TIOCSERSETMULTI:
1175 default:
1176 rc = -ENOIOCTLCMD;
1177 break;
1178 }
1179 return rc;
1180}
1181
1182/*****************************************************************************/
1183
1184/*
1185 * Start the transmitter again. Just turn TX interrupts back on.
1186 */
1187
1188static void stl_start(struct tty_struct *tty)
1189{
1190 struct stlport *portp;
1191
1192 pr_debug("stl_start(tty=%p)\n", tty);
1193
1194 portp = tty->driver_data;
1195 if (portp == NULL)
1196 return;
1197 stl_startrxtx(portp, -1, 1);
1198}
1199
1200/*****************************************************************************/
1201
1202static void stl_settermios(struct tty_struct *tty, struct ktermios *old)
1203{
1204 struct stlport *portp;
1205 struct ktermios *tiosp;
1206
1207 pr_debug("stl_settermios(tty=%p,old=%p)\n", tty, old);
1208
1209 portp = tty->driver_data;
1210 if (portp == NULL)
1211 return;
1212
1213 tiosp = tty->termios;
1214 if ((tiosp->c_cflag == old->c_cflag) &&
1215 (tiosp->c_iflag == old->c_iflag))
1216 return;
1217
1218 stl_setport(portp, tiosp);
1219 stl_setsignals(portp, ((tiosp->c_cflag & (CBAUD & ~CBAUDEX)) ? 1 : 0),
1220 -1);
1221 if ((old->c_cflag & CRTSCTS) && ((tiosp->c_cflag & CRTSCTS) == 0)) {
1222 tty->hw_stopped = 0;
1223 stl_start(tty);
1224 }
1225 if (((old->c_cflag & CLOCAL) == 0) && (tiosp->c_cflag & CLOCAL))
1226 wake_up_interruptible(&portp->port.open_wait);
1227}
1228
1229/*****************************************************************************/
1230
1231/*
1232 * Attempt to flow control who ever is sending us data. Based on termios
1233 * settings use software or/and hardware flow control.
1234 */
1235
1236static void stl_throttle(struct tty_struct *tty)
1237{
1238 struct stlport *portp;
1239
1240 pr_debug("stl_throttle(tty=%p)\n", tty);
1241
1242 portp = tty->driver_data;
1243 if (portp == NULL)
1244 return;
1245 stl_flowctrl(portp, 0);
1246}
1247
1248/*****************************************************************************/
1249
1250/*
1251 * Unflow control the device sending us data...
1252 */
1253
1254static void stl_unthrottle(struct tty_struct *tty)
1255{
1256 struct stlport *portp;
1257
1258 pr_debug("stl_unthrottle(tty=%p)\n", tty);
1259
1260 portp = tty->driver_data;
1261 if (portp == NULL)
1262 return;
1263 stl_flowctrl(portp, 1);
1264}
1265
1266/*****************************************************************************/
1267
1268/*
1269 * Stop the transmitter. Basically to do this we will just turn TX
1270 * interrupts off.
1271 */
1272
1273static void stl_stop(struct tty_struct *tty)
1274{
1275 struct stlport *portp;
1276
1277 pr_debug("stl_stop(tty=%p)\n", tty);
1278
1279 portp = tty->driver_data;
1280 if (portp == NULL)
1281 return;
1282 stl_startrxtx(portp, -1, 0);
1283}
1284
1285/*****************************************************************************/
1286
1287/*
1288 * Hangup this port. This is pretty much like closing the port, only
1289 * a little more brutal. No waiting for data to drain. Shutdown the
1290 * port and maybe drop signals.
1291 */
1292
1293static void stl_hangup(struct tty_struct *tty)
1294{
1295 struct stlport *portp = tty->driver_data;
1296 pr_debug("stl_hangup(tty=%p)\n", tty);
1297
1298 if (portp == NULL)
1299 return;
1300 tty_port_hangup(&portp->port);
1301}
1302
1303/*****************************************************************************/
1304
1305static int stl_breakctl(struct tty_struct *tty, int state)
1306{
1307 struct stlport *portp;
1308
1309 pr_debug("stl_breakctl(tty=%p,state=%d)\n", tty, state);
1310
1311 portp = tty->driver_data;
1312 if (portp == NULL)
1313 return -EINVAL;
1314
1315 stl_sendbreak(portp, ((state == -1) ? 1 : 2));
1316 return 0;
1317}
1318
1319/*****************************************************************************/
1320
1321static void stl_sendxchar(struct tty_struct *tty, char ch)
1322{
1323 struct stlport *portp;
1324
1325 pr_debug("stl_sendxchar(tty=%p,ch=%x)\n", tty, ch);
1326
1327 portp = tty->driver_data;
1328 if (portp == NULL)
1329 return;
1330
1331 if (ch == STOP_CHAR(tty))
1332 stl_sendflow(portp, 0);
1333 else if (ch == START_CHAR(tty))
1334 stl_sendflow(portp, 1);
1335 else
1336 stl_putchar(tty, ch);
1337}
1338
1339static void stl_portinfo(struct seq_file *m, struct stlport *portp, int portnr)
1340{
1341 int sigs;
1342 char sep;
1343
1344 seq_printf(m, "%d: uart:%s tx:%d rx:%d",
1345 portnr, (portp->hwid == 1) ? "SC26198" : "CD1400",
1346 (int) portp->stats.txtotal, (int) portp->stats.rxtotal);
1347
1348 if (portp->stats.rxframing)
1349 seq_printf(m, " fe:%d", (int) portp->stats.rxframing);
1350 if (portp->stats.rxparity)
1351 seq_printf(m, " pe:%d", (int) portp->stats.rxparity);
1352 if (portp->stats.rxbreaks)
1353 seq_printf(m, " brk:%d", (int) portp->stats.rxbreaks);
1354 if (portp->stats.rxoverrun)
1355 seq_printf(m, " oe:%d", (int) portp->stats.rxoverrun);
1356
1357 sigs = stl_getsignals(portp);
1358 sep = ' ';
1359 if (sigs & TIOCM_RTS) {
1360 seq_printf(m, "%c%s", sep, "RTS");
1361 sep = '|';
1362 }
1363 if (sigs & TIOCM_CTS) {
1364 seq_printf(m, "%c%s", sep, "CTS");
1365 sep = '|';
1366 }
1367 if (sigs & TIOCM_DTR) {
1368 seq_printf(m, "%c%s", sep, "DTR");
1369 sep = '|';
1370 }
1371 if (sigs & TIOCM_CD) {
1372 seq_printf(m, "%c%s", sep, "DCD");
1373 sep = '|';
1374 }
1375 if (sigs & TIOCM_DSR) {
1376 seq_printf(m, "%c%s", sep, "DSR");
1377 sep = '|';
1378 }
1379 seq_putc(m, '\n');
1380}
1381
1382/*****************************************************************************/
1383
1384/*
1385 * Port info, read from the /proc file system.
1386 */
1387
1388static int stl_proc_show(struct seq_file *m, void *v)
1389{
1390 struct stlbrd *brdp;
1391 struct stlpanel *panelp;
1392 struct stlport *portp;
1393 unsigned int brdnr, panelnr, portnr;
1394 int totalport;
1395
1396 totalport = 0;
1397
1398 seq_printf(m, "%s: version %s\n", stl_drvtitle, stl_drvversion);
1399
1400/*
1401 * We scan through for each board, panel and port. The offset is
1402 * calculated on the fly, and irrelevant ports are skipped.
1403 */
1404 for (brdnr = 0; brdnr < stl_nrbrds; brdnr++) {
1405 brdp = stl_brds[brdnr];
1406 if (brdp == NULL)
1407 continue;
1408 if (brdp->state == 0)
1409 continue;
1410
1411 totalport = brdnr * STL_MAXPORTS;
1412 for (panelnr = 0; panelnr < brdp->nrpanels; panelnr++) {
1413 panelp = brdp->panels[panelnr];
1414 if (panelp == NULL)
1415 continue;
1416
1417 for (portnr = 0; portnr < panelp->nrports; portnr++,
1418 totalport++) {
1419 portp = panelp->ports[portnr];
1420 if (portp == NULL)
1421 continue;
1422 stl_portinfo(m, portp, totalport);
1423 }
1424 }
1425 }
1426 return 0;
1427}
1428
1429static int stl_proc_open(struct inode *inode, struct file *file)
1430{
1431 return single_open(file, stl_proc_show, NULL);
1432}
1433
1434static const struct file_operations stl_proc_fops = {
1435 .owner = THIS_MODULE,
1436 .open = stl_proc_open,
1437 .read = seq_read,
1438 .llseek = seq_lseek,
1439 .release = single_release,
1440};
1441
1442/*****************************************************************************/
1443
1444/*
1445 * All board interrupts are vectored through here first. This code then
1446 * calls off to the approrpriate board interrupt handlers.
1447 */
1448
1449static irqreturn_t stl_intr(int irq, void *dev_id)
1450{
1451 struct stlbrd *brdp = dev_id;
1452
1453 pr_debug("stl_intr(brdp=%p,irq=%d)\n", brdp, brdp->irq);
1454
1455 return IRQ_RETVAL((* brdp->isr)(brdp));
1456}
1457
1458/*****************************************************************************/
1459
1460/*
1461 * Interrupt service routine for EasyIO board types.
1462 */
1463
1464static int stl_eiointr(struct stlbrd *brdp)
1465{
1466 struct stlpanel *panelp;
1467 unsigned int iobase;
1468 int handled = 0;
1469
1470 spin_lock(&brd_lock);
1471 panelp = brdp->panels[0];
1472 iobase = panelp->iobase;
1473 while (inb(brdp->iostatus) & EIO_INTRPEND) {
1474 handled = 1;
1475 (* panelp->isr)(panelp, iobase);
1476 }
1477 spin_unlock(&brd_lock);
1478 return handled;
1479}
1480
1481/*****************************************************************************/
1482
1483/*
1484 * Interrupt service routine for ECH-AT board types.
1485 */
1486
1487static int stl_echatintr(struct stlbrd *brdp)
1488{
1489 struct stlpanel *panelp;
1490 unsigned int ioaddr, bnknr;
1491 int handled = 0;
1492
1493 outb((brdp->ioctrlval | ECH_BRDENABLE), brdp->ioctrl);
1494
1495 while (inb(brdp->iostatus) & ECH_INTRPEND) {
1496 handled = 1;
1497 for (bnknr = 0; bnknr < brdp->nrbnks; bnknr++) {
1498 ioaddr = brdp->bnkstataddr[bnknr];
1499 if (inb(ioaddr) & ECH_PNLINTRPEND) {
1500 panelp = brdp->bnk2panel[bnknr];
1501 (* panelp->isr)(panelp, (ioaddr & 0xfffc));
1502 }
1503 }
1504 }
1505
1506 outb((brdp->ioctrlval | ECH_BRDDISABLE), brdp->ioctrl);
1507
1508 return handled;
1509}
1510
1511/*****************************************************************************/
1512
1513/*
1514 * Interrupt service routine for ECH-MCA board types.
1515 */
1516
1517static int stl_echmcaintr(struct stlbrd *brdp)
1518{
1519 struct stlpanel *panelp;
1520 unsigned int ioaddr, bnknr;
1521 int handled = 0;
1522
1523 while (inb(brdp->iostatus) & ECH_INTRPEND) {
1524 handled = 1;
1525 for (bnknr = 0; bnknr < brdp->nrbnks; bnknr++) {
1526 ioaddr = brdp->bnkstataddr[bnknr];
1527 if (inb(ioaddr) & ECH_PNLINTRPEND) {
1528 panelp = brdp->bnk2panel[bnknr];
1529 (* panelp->isr)(panelp, (ioaddr & 0xfffc));
1530 }
1531 }
1532 }
1533 return handled;
1534}
1535
1536/*****************************************************************************/
1537
1538/*
1539 * Interrupt service routine for ECH-PCI board types.
1540 */
1541
1542static int stl_echpciintr(struct stlbrd *brdp)
1543{
1544 struct stlpanel *panelp;
1545 unsigned int ioaddr, bnknr, recheck;
1546 int handled = 0;
1547
1548 while (1) {
1549 recheck = 0;
1550 for (bnknr = 0; bnknr < brdp->nrbnks; bnknr++) {
1551 outb(brdp->bnkpageaddr[bnknr], brdp->ioctrl);
1552 ioaddr = brdp->bnkstataddr[bnknr];
1553 if (inb(ioaddr) & ECH_PNLINTRPEND) {
1554 panelp = brdp->bnk2panel[bnknr];
1555 (* panelp->isr)(panelp, (ioaddr & 0xfffc));
1556 recheck++;
1557 handled = 1;
1558 }
1559 }
1560 if (! recheck)
1561 break;
1562 }
1563 return handled;
1564}
1565
1566/*****************************************************************************/
1567
1568/*
1569 * Interrupt service routine for ECH-8/64-PCI board types.
1570 */
1571
1572static int stl_echpci64intr(struct stlbrd *brdp)
1573{
1574 struct stlpanel *panelp;
1575 unsigned int ioaddr, bnknr;
1576 int handled = 0;
1577
1578 while (inb(brdp->ioctrl) & 0x1) {
1579 handled = 1;
1580 for (bnknr = 0; bnknr < brdp->nrbnks; bnknr++) {
1581 ioaddr = brdp->bnkstataddr[bnknr];
1582 if (inb(ioaddr) & ECH_PNLINTRPEND) {
1583 panelp = brdp->bnk2panel[bnknr];
1584 (* panelp->isr)(panelp, (ioaddr & 0xfffc));
1585 }
1586 }
1587 }
1588
1589 return handled;
1590}
1591
1592/*****************************************************************************/
1593
1594/*
1595 * Initialize all the ports on a panel.
1596 */
1597
1598static int __devinit stl_initports(struct stlbrd *brdp, struct stlpanel *panelp)
1599{
1600 struct stlport *portp;
1601 unsigned int i;
1602 int chipmask;
1603
1604 pr_debug("stl_initports(brdp=%p,panelp=%p)\n", brdp, panelp);
1605
1606 chipmask = stl_panelinit(brdp, panelp);
1607
1608/*
1609 * All UART's are initialized (if found!). Now go through and setup
1610 * each ports data structures.
1611 */
1612 for (i = 0; i < panelp->nrports; i++) {
1613 portp = kzalloc(sizeof(struct stlport), GFP_KERNEL);
1614 if (!portp) {
1615 printk("STALLION: failed to allocate memory "
1616 "(size=%Zd)\n", sizeof(struct stlport));
1617 break;
1618 }
1619 tty_port_init(&portp->port);
1620 portp->port.ops = &stl_port_ops;
1621 portp->magic = STL_PORTMAGIC;
1622 portp->portnr = i;
1623 portp->brdnr = panelp->brdnr;
1624 portp->panelnr = panelp->panelnr;
1625 portp->uartp = panelp->uartp;
1626 portp->clk = brdp->clk;
1627 portp->baud_base = STL_BAUDBASE;
1628 portp->close_delay = STL_CLOSEDELAY;
1629 portp->closing_wait = 30 * HZ;
1630 init_waitqueue_head(&portp->port.open_wait);
1631 init_waitqueue_head(&portp->port.close_wait);
1632 portp->stats.brd = portp->brdnr;
1633 portp->stats.panel = portp->panelnr;
1634 portp->stats.port = portp->portnr;
1635 panelp->ports[i] = portp;
1636 stl_portinit(brdp, panelp, portp);
1637 }
1638
1639 return 0;
1640}
1641
1642static void stl_cleanup_panels(struct stlbrd *brdp)
1643{
1644 struct stlpanel *panelp;
1645 struct stlport *portp;
1646 unsigned int j, k;
1647 struct tty_struct *tty;
1648
1649 for (j = 0; j < STL_MAXPANELS; j++) {
1650 panelp = brdp->panels[j];
1651 if (panelp == NULL)
1652 continue;
1653 for (k = 0; k < STL_PORTSPERPANEL; k++) {
1654 portp = panelp->ports[k];
1655 if (portp == NULL)
1656 continue;
1657 tty = tty_port_tty_get(&portp->port);
1658 if (tty != NULL) {
1659 stl_hangup(tty);
1660 tty_kref_put(tty);
1661 }
1662 kfree(portp->tx.buf);
1663 kfree(portp);
1664 }
1665 kfree(panelp);
1666 }
1667}
1668
1669/*****************************************************************************/
1670
1671/*
1672 * Try to find and initialize an EasyIO board.
1673 */
1674
1675static int __devinit stl_initeio(struct stlbrd *brdp)
1676{
1677 struct stlpanel *panelp;
1678 unsigned int status;
1679 char *name;
1680 int retval;
1681
1682 pr_debug("stl_initeio(brdp=%p)\n", brdp);
1683
1684 brdp->ioctrl = brdp->ioaddr1 + 1;
1685 brdp->iostatus = brdp->ioaddr1 + 2;
1686
1687 status = inb(brdp->iostatus);
1688 if ((status & EIO_IDBITMASK) == EIO_MK3)
1689 brdp->ioctrl++;
1690
1691/*
1692 * Handle board specific stuff now. The real difference is PCI
1693 * or not PCI.
1694 */
1695 if (brdp->brdtype == BRD_EASYIOPCI) {
1696 brdp->iosize1 = 0x80;
1697 brdp->iosize2 = 0x80;
1698 name = "serial(EIO-PCI)";
1699 outb(0x41, (brdp->ioaddr2 + 0x4c));
1700 } else {
1701 brdp->iosize1 = 8;
1702 name = "serial(EIO)";
1703 if ((brdp->irq < 0) || (brdp->irq > 15) ||
1704 (stl_vecmap[brdp->irq] == (unsigned char) 0xff)) {
1705 printk("STALLION: invalid irq=%d for brd=%d\n",
1706 brdp->irq, brdp->brdnr);
1707 retval = -EINVAL;
1708 goto err;
1709 }
1710 outb((stl_vecmap[brdp->irq] | EIO_0WS |
1711 ((brdp->irqtype) ? EIO_INTLEVEL : EIO_INTEDGE)),
1712 brdp->ioctrl);
1713 }
1714
1715 retval = -EBUSY;
1716 if (!request_region(brdp->ioaddr1, brdp->iosize1, name)) {
1717 printk(KERN_WARNING "STALLION: Warning, board %d I/O address "
1718 "%x conflicts with another device\n", brdp->brdnr,
1719 brdp->ioaddr1);
1720 goto err;
1721 }
1722
1723 if (brdp->iosize2 > 0)
1724 if (!request_region(brdp->ioaddr2, brdp->iosize2, name)) {
1725 printk(KERN_WARNING "STALLION: Warning, board %d I/O "
1726 "address %x conflicts with another device\n",
1727 brdp->brdnr, brdp->ioaddr2);
1728 printk(KERN_WARNING "STALLION: Warning, also "
1729 "releasing board %d I/O address %x \n",
1730 brdp->brdnr, brdp->ioaddr1);
1731 goto err_rel1;
1732 }
1733
1734/*
1735 * Everything looks OK, so let's go ahead and probe for the hardware.
1736 */
1737 brdp->clk = CD1400_CLK;
1738 brdp->isr = stl_eiointr;
1739
1740 retval = -ENODEV;
1741 switch (status & EIO_IDBITMASK) {
1742 case EIO_8PORTM:
1743 brdp->clk = CD1400_CLK8M;
1744 /* fall thru */
1745 case EIO_8PORTRS:
1746 case EIO_8PORTDI:
1747 brdp->nrports = 8;
1748 break;
1749 case EIO_4PORTRS:
1750 brdp->nrports = 4;
1751 break;
1752 case EIO_MK3:
1753 switch (status & EIO_BRDMASK) {
1754 case ID_BRD4:
1755 brdp->nrports = 4;
1756 break;
1757 case ID_BRD8:
1758 brdp->nrports = 8;
1759 break;
1760 case ID_BRD16:
1761 brdp->nrports = 16;
1762 break;
1763 default:
1764 goto err_rel2;
1765 }
1766 break;
1767 default:
1768 goto err_rel2;
1769 }
1770
1771/*
1772 * We have verified that the board is actually present, so now we
1773 * can complete the setup.
1774 */
1775
1776 panelp = kzalloc(sizeof(struct stlpanel), GFP_KERNEL);
1777 if (!panelp) {
1778 printk(KERN_WARNING "STALLION: failed to allocate memory "
1779 "(size=%Zd)\n", sizeof(struct stlpanel));
1780 retval = -ENOMEM;
1781 goto err_rel2;
1782 }
1783
1784 panelp->magic = STL_PANELMAGIC;
1785 panelp->brdnr = brdp->brdnr;
1786 panelp->panelnr = 0;
1787 panelp->nrports = brdp->nrports;
1788 panelp->iobase = brdp->ioaddr1;
1789 panelp->hwid = status;
1790 if ((status & EIO_IDBITMASK) == EIO_MK3) {
1791 panelp->uartp = &stl_sc26198uart;
1792 panelp->isr = stl_sc26198intr;
1793 } else {
1794 panelp->uartp = &stl_cd1400uart;
1795 panelp->isr = stl_cd1400eiointr;
1796 }
1797
1798 brdp->panels[0] = panelp;
1799 brdp->nrpanels = 1;
1800 brdp->state |= BRD_FOUND;
1801 brdp->hwid = status;
1802 if (request_irq(brdp->irq, stl_intr, IRQF_SHARED, name, brdp) != 0) {
1803 printk("STALLION: failed to register interrupt "
1804 "routine for %s irq=%d\n", name, brdp->irq);
1805 retval = -ENODEV;
1806 goto err_fr;
1807 }
1808
1809 return 0;
1810err_fr:
1811 stl_cleanup_panels(brdp);
1812err_rel2:
1813 if (brdp->iosize2 > 0)
1814 release_region(brdp->ioaddr2, brdp->iosize2);
1815err_rel1:
1816 release_region(brdp->ioaddr1, brdp->iosize1);
1817err:
1818 return retval;
1819}
1820
1821/*****************************************************************************/
1822
1823/*
1824 * Try to find an ECH board and initialize it. This code is capable of
1825 * dealing with all types of ECH board.
1826 */
1827
1828static int __devinit stl_initech(struct stlbrd *brdp)
1829{
1830 struct stlpanel *panelp;
1831 unsigned int status, nxtid, ioaddr, conflict, panelnr, banknr, i;
1832 int retval;
1833 char *name;
1834
1835 pr_debug("stl_initech(brdp=%p)\n", brdp);
1836
1837 status = 0;
1838 conflict = 0;
1839
1840/*
1841 * Set up the initial board register contents for boards. This varies a
1842 * bit between the different board types. So we need to handle each
1843 * separately. Also do a check that the supplied IRQ is good.
1844 */
1845 switch (brdp->brdtype) {
1846
1847 case BRD_ECH:
1848 brdp->isr = stl_echatintr;
1849 brdp->ioctrl = brdp->ioaddr1 + 1;
1850 brdp->iostatus = brdp->ioaddr1 + 1;
1851 status = inb(brdp->iostatus);
1852 if ((status & ECH_IDBITMASK) != ECH_ID) {
1853 retval = -ENODEV;
1854 goto err;
1855 }
1856 if ((brdp->irq < 0) || (brdp->irq > 15) ||
1857 (stl_vecmap[brdp->irq] == (unsigned char) 0xff)) {
1858 printk("STALLION: invalid irq=%d for brd=%d\n",
1859 brdp->irq, brdp->brdnr);
1860 retval = -EINVAL;
1861 goto err;
1862 }
1863 status = ((brdp->ioaddr2 & ECH_ADDR2MASK) >> 1);
1864 status |= (stl_vecmap[brdp->irq] << 1);
1865 outb((status | ECH_BRDRESET), brdp->ioaddr1);
1866 brdp->ioctrlval = ECH_INTENABLE |
1867 ((brdp->irqtype) ? ECH_INTLEVEL : ECH_INTEDGE);
1868 for (i = 0; i < 10; i++)
1869 outb((brdp->ioctrlval | ECH_BRDENABLE), brdp->ioctrl);
1870 brdp->iosize1 = 2;
1871 brdp->iosize2 = 32;
1872 name = "serial(EC8/32)";
1873 outb(status, brdp->ioaddr1);
1874 break;
1875
1876 case BRD_ECHMC:
1877 brdp->isr = stl_echmcaintr;
1878 brdp->ioctrl = brdp->ioaddr1 + 0x20;
1879 brdp->iostatus = brdp->ioctrl;
1880 status = inb(brdp->iostatus);
1881 if ((status & ECH_IDBITMASK) != ECH_ID) {
1882 retval = -ENODEV;
1883 goto err;
1884 }
1885 if ((brdp->irq < 0) || (brdp->irq > 15) ||
1886 (stl_vecmap[brdp->irq] == (unsigned char) 0xff)) {
1887 printk("STALLION: invalid irq=%d for brd=%d\n",
1888 brdp->irq, brdp->brdnr);
1889 retval = -EINVAL;
1890 goto err;
1891 }
1892 outb(ECHMC_BRDRESET, brdp->ioctrl);
1893 outb(ECHMC_INTENABLE, brdp->ioctrl);
1894 brdp->iosize1 = 64;
1895 name = "serial(EC8/32-MC)";
1896 break;
1897
1898 case BRD_ECHPCI:
1899 brdp->isr = stl_echpciintr;
1900 brdp->ioctrl = brdp->ioaddr1 + 2;
1901 brdp->iosize1 = 4;
1902 brdp->iosize2 = 8;
1903 name = "serial(EC8/32-PCI)";
1904 break;
1905
1906 case BRD_ECH64PCI:
1907 brdp->isr = stl_echpci64intr;
1908 brdp->ioctrl = brdp->ioaddr2 + 0x40;
1909 outb(0x43, (brdp->ioaddr1 + 0x4c));
1910 brdp->iosize1 = 0x80;
1911 brdp->iosize2 = 0x80;
1912 name = "serial(EC8/64-PCI)";
1913 break;
1914
1915 default:
1916 printk("STALLION: unknown board type=%d\n", brdp->brdtype);
1917 retval = -EINVAL;
1918 goto err;
1919 }
1920
1921/*
1922 * Check boards for possible IO address conflicts and return fail status
1923 * if an IO conflict found.
1924 */
1925 retval = -EBUSY;
1926 if (!request_region(brdp->ioaddr1, brdp->iosize1, name)) {
1927 printk(KERN_WARNING "STALLION: Warning, board %d I/O address "
1928 "%x conflicts with another device\n", brdp->brdnr,
1929 brdp->ioaddr1);
1930 goto err;
1931 }
1932
1933 if (brdp->iosize2 > 0)
1934 if (!request_region(brdp->ioaddr2, brdp->iosize2, name)) {
1935 printk(KERN_WARNING "STALLION: Warning, board %d I/O "
1936 "address %x conflicts with another device\n",
1937 brdp->brdnr, brdp->ioaddr2);
1938 printk(KERN_WARNING "STALLION: Warning, also "
1939 "releasing board %d I/O address %x \n",
1940 brdp->brdnr, brdp->ioaddr1);
1941 goto err_rel1;
1942 }
1943
1944/*
1945 * Scan through the secondary io address space looking for panels.
1946 * As we find'em allocate and initialize panel structures for each.
1947 */
1948 brdp->clk = CD1400_CLK;
1949 brdp->hwid = status;
1950
1951 ioaddr = brdp->ioaddr2;
1952 banknr = 0;
1953 panelnr = 0;
1954 nxtid = 0;
1955
1956 for (i = 0; i < STL_MAXPANELS; i++) {
1957 if (brdp->brdtype == BRD_ECHPCI) {
1958 outb(nxtid, brdp->ioctrl);
1959 ioaddr = brdp->ioaddr2;
1960 }
1961 status = inb(ioaddr + ECH_PNLSTATUS);
1962 if ((status & ECH_PNLIDMASK) != nxtid)
1963 break;
1964 panelp = kzalloc(sizeof(struct stlpanel), GFP_KERNEL);
1965 if (!panelp) {
1966 printk("STALLION: failed to allocate memory "
1967 "(size=%Zd)\n", sizeof(struct stlpanel));
1968 retval = -ENOMEM;
1969 goto err_fr;
1970 }
1971 panelp->magic = STL_PANELMAGIC;
1972 panelp->brdnr = brdp->brdnr;
1973 panelp->panelnr = panelnr;
1974 panelp->iobase = ioaddr;
1975 panelp->pagenr = nxtid;
1976 panelp->hwid = status;
1977 brdp->bnk2panel[banknr] = panelp;
1978 brdp->bnkpageaddr[banknr] = nxtid;
1979 brdp->bnkstataddr[banknr++] = ioaddr + ECH_PNLSTATUS;
1980
1981 if (status & ECH_PNLXPID) {
1982 panelp->uartp = &stl_sc26198uart;
1983 panelp->isr = stl_sc26198intr;
1984 if (status & ECH_PNL16PORT) {
1985 panelp->nrports = 16;
1986 brdp->bnk2panel[banknr] = panelp;
1987 brdp->bnkpageaddr[banknr] = nxtid;
1988 brdp->bnkstataddr[banknr++] = ioaddr + 4 +
1989 ECH_PNLSTATUS;
1990 } else
1991 panelp->nrports = 8;
1992 } else {
1993 panelp->uartp = &stl_cd1400uart;
1994 panelp->isr = stl_cd1400echintr;
1995 if (status & ECH_PNL16PORT) {
1996 panelp->nrports = 16;
1997 panelp->ackmask = 0x80;
1998 if (brdp->brdtype != BRD_ECHPCI)
1999 ioaddr += EREG_BANKSIZE;
2000 brdp->bnk2panel[banknr] = panelp;
2001 brdp->bnkpageaddr[banknr] = ++nxtid;
2002 brdp->bnkstataddr[banknr++] = ioaddr +
2003 ECH_PNLSTATUS;
2004 } else {
2005 panelp->nrports = 8;
2006 panelp->ackmask = 0xc0;
2007 }
2008 }
2009
2010 nxtid++;
2011 ioaddr += EREG_BANKSIZE;
2012 brdp->nrports += panelp->nrports;
2013 brdp->panels[panelnr++] = panelp;
2014 if ((brdp->brdtype != BRD_ECHPCI) &&
2015 (ioaddr >= (brdp->ioaddr2 + brdp->iosize2))) {
2016 retval = -EINVAL;
2017 goto err_fr;
2018 }
2019 }
2020
2021 brdp->nrpanels = panelnr;
2022 brdp->nrbnks = banknr;
2023 if (brdp->brdtype == BRD_ECH)
2024 outb((brdp->ioctrlval | ECH_BRDDISABLE), brdp->ioctrl);
2025
2026 brdp->state |= BRD_FOUND;
2027 if (request_irq(brdp->irq, stl_intr, IRQF_SHARED, name, brdp) != 0) {
2028 printk("STALLION: failed to register interrupt "
2029 "routine for %s irq=%d\n", name, brdp->irq);
2030 retval = -ENODEV;
2031 goto err_fr;
2032 }
2033
2034 return 0;
2035err_fr:
2036 stl_cleanup_panels(brdp);
2037 if (brdp->iosize2 > 0)
2038 release_region(brdp->ioaddr2, brdp->iosize2);
2039err_rel1:
2040 release_region(brdp->ioaddr1, brdp->iosize1);
2041err:
2042 return retval;
2043}
2044
2045/*****************************************************************************/
2046
2047/*
2048 * Initialize and configure the specified board.
2049 * Scan through all the boards in the configuration and see what we
2050 * can find. Handle EIO and the ECH boards a little differently here
2051 * since the initial search and setup is very different.
2052 */
2053
2054static int __devinit stl_brdinit(struct stlbrd *brdp)
2055{
2056 int i, retval;
2057
2058 pr_debug("stl_brdinit(brdp=%p)\n", brdp);
2059
2060 switch (brdp->brdtype) {
2061 case BRD_EASYIO:
2062 case BRD_EASYIOPCI:
2063 retval = stl_initeio(brdp);
2064 if (retval)
2065 goto err;
2066 break;
2067 case BRD_ECH:
2068 case BRD_ECHMC:
2069 case BRD_ECHPCI:
2070 case BRD_ECH64PCI:
2071 retval = stl_initech(brdp);
2072 if (retval)
2073 goto err;
2074 break;
2075 default:
2076 printk("STALLION: board=%d is unknown board type=%d\n",
2077 brdp->brdnr, brdp->brdtype);
2078 retval = -ENODEV;
2079 goto err;
2080 }
2081
2082 if ((brdp->state & BRD_FOUND) == 0) {
2083 printk("STALLION: %s board not found, board=%d io=%x irq=%d\n",
2084 stl_brdnames[brdp->brdtype], brdp->brdnr,
2085 brdp->ioaddr1, brdp->irq);
2086 goto err_free;
2087 }
2088
2089 for (i = 0; i < STL_MAXPANELS; i++)
2090 if (brdp->panels[i] != NULL)
2091 stl_initports(brdp, brdp->panels[i]);
2092
2093 printk("STALLION: %s found, board=%d io=%x irq=%d "
2094 "nrpanels=%d nrports=%d\n", stl_brdnames[brdp->brdtype],
2095 brdp->brdnr, brdp->ioaddr1, brdp->irq, brdp->nrpanels,
2096 brdp->nrports);
2097
2098 return 0;
2099err_free:
2100 free_irq(brdp->irq, brdp);
2101
2102 stl_cleanup_panels(brdp);
2103
2104 release_region(brdp->ioaddr1, brdp->iosize1);
2105 if (brdp->iosize2 > 0)
2106 release_region(brdp->ioaddr2, brdp->iosize2);
2107err:
2108 return retval;
2109}
2110
2111/*****************************************************************************/
2112
2113/*
2114 * Find the next available board number that is free.
2115 */
2116
2117static int __devinit stl_getbrdnr(void)
2118{
2119 unsigned int i;
2120
2121 for (i = 0; i < STL_MAXBRDS; i++)
2122 if (stl_brds[i] == NULL) {
2123 if (i >= stl_nrbrds)
2124 stl_nrbrds = i + 1;
2125 return i;
2126 }
2127
2128 return -1;
2129}
2130
2131/*****************************************************************************/
2132/*
2133 * We have a Stallion board. Allocate a board structure and
2134 * initialize it. Read its IO and IRQ resources from PCI
2135 * configuration space.
2136 */
2137
2138static int __devinit stl_pciprobe(struct pci_dev *pdev,
2139 const struct pci_device_id *ent)
2140{
2141 struct stlbrd *brdp;
2142 unsigned int i, brdtype = ent->driver_data;
2143 int brdnr, retval = -ENODEV;
2144
2145 if ((pdev->class >> 8) == PCI_CLASS_STORAGE_IDE)
2146 goto err;
2147
2148 retval = pci_enable_device(pdev);
2149 if (retval)
2150 goto err;
2151 brdp = stl_allocbrd();
2152 if (brdp == NULL) {
2153 retval = -ENOMEM;
2154 goto err;
2155 }
2156 mutex_lock(&stl_brdslock);
2157 brdnr = stl_getbrdnr();
2158 if (brdnr < 0) {
2159 dev_err(&pdev->dev, "too many boards found, "
2160 "maximum supported %d\n", STL_MAXBRDS);
2161 mutex_unlock(&stl_brdslock);
2162 retval = -ENODEV;
2163 goto err_fr;
2164 }
2165 brdp->brdnr = (unsigned int)brdnr;
2166 stl_brds[brdp->brdnr] = brdp;
2167 mutex_unlock(&stl_brdslock);
2168
2169 brdp->brdtype = brdtype;
2170 brdp->state |= STL_PROBED;
2171
2172/*
2173 * We have all resources from the board, so let's setup the actual
2174 * board structure now.
2175 */
2176 switch (brdtype) {
2177 case BRD_ECHPCI:
2178 brdp->ioaddr2 = pci_resource_start(pdev, 0);
2179 brdp->ioaddr1 = pci_resource_start(pdev, 1);
2180 break;
2181 case BRD_ECH64PCI:
2182 brdp->ioaddr2 = pci_resource_start(pdev, 2);
2183 brdp->ioaddr1 = pci_resource_start(pdev, 1);
2184 break;
2185 case BRD_EASYIOPCI:
2186 brdp->ioaddr1 = pci_resource_start(pdev, 2);
2187 brdp->ioaddr2 = pci_resource_start(pdev, 1);
2188 break;
2189 default:
2190 dev_err(&pdev->dev, "unknown PCI board type=%u\n", brdtype);
2191 break;
2192 }
2193
2194 brdp->irq = pdev->irq;
2195 retval = stl_brdinit(brdp);
2196 if (retval)
2197 goto err_null;
2198
2199 pci_set_drvdata(pdev, brdp);
2200
2201 for (i = 0; i < brdp->nrports; i++)
2202 tty_register_device(stl_serial,
2203 brdp->brdnr * STL_MAXPORTS + i, &pdev->dev);
2204
2205 return 0;
2206err_null:
2207 stl_brds[brdp->brdnr] = NULL;
2208err_fr:
2209 kfree(brdp);
2210err:
2211 return retval;
2212}
2213
2214static void __devexit stl_pciremove(struct pci_dev *pdev)
2215{
2216 struct stlbrd *brdp = pci_get_drvdata(pdev);
2217 unsigned int i;
2218
2219 free_irq(brdp->irq, brdp);
2220
2221 stl_cleanup_panels(brdp);
2222
2223 release_region(brdp->ioaddr1, brdp->iosize1);
2224 if (brdp->iosize2 > 0)
2225 release_region(brdp->ioaddr2, brdp->iosize2);
2226
2227 for (i = 0; i < brdp->nrports; i++)
2228 tty_unregister_device(stl_serial,
2229 brdp->brdnr * STL_MAXPORTS + i);
2230
2231 stl_brds[brdp->brdnr] = NULL;
2232 kfree(brdp);
2233}
2234
2235static struct pci_driver stl_pcidriver = {
2236 .name = "stallion",
2237 .id_table = stl_pcibrds,
2238 .probe = stl_pciprobe,
2239 .remove = __devexit_p(stl_pciremove)
2240};
2241
2242/*****************************************************************************/
2243
2244/*
2245 * Return the board stats structure to user app.
2246 */
2247
2248static int stl_getbrdstats(combrd_t __user *bp)
2249{
2250 combrd_t stl_brdstats;
2251 struct stlbrd *brdp;
2252 struct stlpanel *panelp;
2253 unsigned int i;
2254
2255 if (copy_from_user(&stl_brdstats, bp, sizeof(combrd_t)))
2256 return -EFAULT;
2257 if (stl_brdstats.brd >= STL_MAXBRDS)
2258 return -ENODEV;
2259 brdp = stl_brds[stl_brdstats.brd];
2260 if (brdp == NULL)
2261 return -ENODEV;
2262
2263 memset(&stl_brdstats, 0, sizeof(combrd_t));
2264 stl_brdstats.brd = brdp->brdnr;
2265 stl_brdstats.type = brdp->brdtype;
2266 stl_brdstats.hwid = brdp->hwid;
2267 stl_brdstats.state = brdp->state;
2268 stl_brdstats.ioaddr = brdp->ioaddr1;
2269 stl_brdstats.ioaddr2 = brdp->ioaddr2;
2270 stl_brdstats.irq = brdp->irq;
2271 stl_brdstats.nrpanels = brdp->nrpanels;
2272 stl_brdstats.nrports = brdp->nrports;
2273 for (i = 0; i < brdp->nrpanels; i++) {
2274 panelp = brdp->panels[i];
2275 stl_brdstats.panels[i].panel = i;
2276 stl_brdstats.panels[i].hwid = panelp->hwid;
2277 stl_brdstats.panels[i].nrports = panelp->nrports;
2278 }
2279
2280 return copy_to_user(bp, &stl_brdstats, sizeof(combrd_t)) ? -EFAULT : 0;
2281}
2282
2283/*****************************************************************************/
2284
2285/*
2286 * Resolve the referenced port number into a port struct pointer.
2287 */
2288
2289static struct stlport *stl_getport(int brdnr, int panelnr, int portnr)
2290{
2291 struct stlbrd *brdp;
2292 struct stlpanel *panelp;
2293
2294 if (brdnr < 0 || brdnr >= STL_MAXBRDS)
2295 return NULL;
2296 brdp = stl_brds[brdnr];
2297 if (brdp == NULL)
2298 return NULL;
2299 if (panelnr < 0 || (unsigned int)panelnr >= brdp->nrpanels)
2300 return NULL;
2301 panelp = brdp->panels[panelnr];
2302 if (panelp == NULL)
2303 return NULL;
2304 if (portnr < 0 || (unsigned int)portnr >= panelp->nrports)
2305 return NULL;
2306 return panelp->ports[portnr];
2307}
2308
2309/*****************************************************************************/
2310
2311/*
2312 * Return the port stats structure to user app. A NULL port struct
2313 * pointer passed in means that we need to find out from the app
2314 * what port to get stats for (used through board control device).
2315 */
2316
2317static int stl_getportstats(struct tty_struct *tty, struct stlport *portp, comstats_t __user *cp)
2318{
2319 comstats_t stl_comstats;
2320 unsigned char *head, *tail;
2321 unsigned long flags;
2322
2323 if (!portp) {
2324 if (copy_from_user(&stl_comstats, cp, sizeof(comstats_t)))
2325 return -EFAULT;
2326 portp = stl_getport(stl_comstats.brd, stl_comstats.panel,
2327 stl_comstats.port);
2328 if (portp == NULL)
2329 return -ENODEV;
2330 }
2331
2332 mutex_lock(&portp->port.mutex);
2333 portp->stats.state = portp->istate;
2334 portp->stats.flags = portp->port.flags;
2335 portp->stats.hwid = portp->hwid;
2336
2337 portp->stats.ttystate = 0;
2338 portp->stats.cflags = 0;
2339 portp->stats.iflags = 0;
2340 portp->stats.oflags = 0;
2341 portp->stats.lflags = 0;
2342 portp->stats.rxbuffered = 0;
2343
2344 spin_lock_irqsave(&stallion_lock, flags);
2345 if (tty != NULL && portp->port.tty == tty) {
2346 portp->stats.ttystate = tty->flags;
2347 /* No longer available as a statistic */
2348 portp->stats.rxbuffered = 1; /*tty->flip.count; */
2349 if (tty->termios != NULL) {
2350 portp->stats.cflags = tty->termios->c_cflag;
2351 portp->stats.iflags = tty->termios->c_iflag;
2352 portp->stats.oflags = tty->termios->c_oflag;
2353 portp->stats.lflags = tty->termios->c_lflag;
2354 }
2355 }
2356 spin_unlock_irqrestore(&stallion_lock, flags);
2357
2358 head = portp->tx.head;
2359 tail = portp->tx.tail;
2360 portp->stats.txbuffered = (head >= tail) ? (head - tail) :
2361 (STL_TXBUFSIZE - (tail - head));
2362
2363 portp->stats.signals = (unsigned long) stl_getsignals(portp);
2364 mutex_unlock(&portp->port.mutex);
2365
2366 return copy_to_user(cp, &portp->stats,
2367 sizeof(comstats_t)) ? -EFAULT : 0;
2368}
2369
2370/*****************************************************************************/
2371
2372/*
2373 * Clear the port stats structure. We also return it zeroed out...
2374 */
2375
2376static int stl_clrportstats(struct stlport *portp, comstats_t __user *cp)
2377{
2378 comstats_t stl_comstats;
2379
2380 if (!portp) {
2381 if (copy_from_user(&stl_comstats, cp, sizeof(comstats_t)))
2382 return -EFAULT;
2383 portp = stl_getport(stl_comstats.brd, stl_comstats.panel,
2384 stl_comstats.port);
2385 if (portp == NULL)
2386 return -ENODEV;
2387 }
2388
2389 mutex_lock(&portp->port.mutex);
2390 memset(&portp->stats, 0, sizeof(comstats_t));
2391 portp->stats.brd = portp->brdnr;
2392 portp->stats.panel = portp->panelnr;
2393 portp->stats.port = portp->portnr;
2394 mutex_unlock(&portp->port.mutex);
2395 return copy_to_user(cp, &portp->stats,
2396 sizeof(comstats_t)) ? -EFAULT : 0;
2397}
2398
2399/*****************************************************************************/
2400
2401/*
2402 * Return the entire driver ports structure to a user app.
2403 */
2404
2405static int stl_getportstruct(struct stlport __user *arg)
2406{
2407 struct stlport stl_dummyport;
2408 struct stlport *portp;
2409
2410 if (copy_from_user(&stl_dummyport, arg, sizeof(struct stlport)))
2411 return -EFAULT;
2412 portp = stl_getport(stl_dummyport.brdnr, stl_dummyport.panelnr,
2413 stl_dummyport.portnr);
2414 if (!portp)
2415 return -ENODEV;
2416 return copy_to_user(arg, portp, sizeof(struct stlport)) ? -EFAULT : 0;
2417}
2418
2419/*****************************************************************************/
2420
2421/*
2422 * Return the entire driver board structure to a user app.
2423 */
2424
2425static int stl_getbrdstruct(struct stlbrd __user *arg)
2426{
2427 struct stlbrd stl_dummybrd;
2428 struct stlbrd *brdp;
2429
2430 if (copy_from_user(&stl_dummybrd, arg, sizeof(struct stlbrd)))
2431 return -EFAULT;
2432 if (stl_dummybrd.brdnr >= STL_MAXBRDS)
2433 return -ENODEV;
2434 brdp = stl_brds[stl_dummybrd.brdnr];
2435 if (!brdp)
2436 return -ENODEV;
2437 return copy_to_user(arg, brdp, sizeof(struct stlbrd)) ? -EFAULT : 0;
2438}
2439
2440/*****************************************************************************/
2441
2442/*
2443 * The "staliomem" device is also required to do some special operations
2444 * on the board and/or ports. In this driver it is mostly used for stats
2445 * collection.
2446 */
2447
2448static long stl_memioctl(struct file *fp, unsigned int cmd, unsigned long arg)
2449{
2450 int brdnr, rc;
2451 void __user *argp = (void __user *)arg;
2452
2453 pr_debug("stl_memioctl(fp=%p,cmd=%x,arg=%lx)\n", fp, cmd,arg);
2454
2455 brdnr = iminor(fp->f_dentry->d_inode);
2456 if (brdnr >= STL_MAXBRDS)
2457 return -ENODEV;
2458 rc = 0;
2459
2460 switch (cmd) {
2461 case COM_GETPORTSTATS:
2462 rc = stl_getportstats(NULL, NULL, argp);
2463 break;
2464 case COM_CLRPORTSTATS:
2465 rc = stl_clrportstats(NULL, argp);
2466 break;
2467 case COM_GETBRDSTATS:
2468 rc = stl_getbrdstats(argp);
2469 break;
2470 case COM_READPORT:
2471 rc = stl_getportstruct(argp);
2472 break;
2473 case COM_READBOARD:
2474 rc = stl_getbrdstruct(argp);
2475 break;
2476 default:
2477 rc = -ENOIOCTLCMD;
2478 break;
2479 }
2480 return rc;
2481}
2482
2483static const struct tty_operations stl_ops = {
2484 .open = stl_open,
2485 .close = stl_close,
2486 .write = stl_write,
2487 .put_char = stl_putchar,
2488 .flush_chars = stl_flushchars,
2489 .write_room = stl_writeroom,
2490 .chars_in_buffer = stl_charsinbuffer,
2491 .ioctl = stl_ioctl,
2492 .set_termios = stl_settermios,
2493 .throttle = stl_throttle,
2494 .unthrottle = stl_unthrottle,
2495 .stop = stl_stop,
2496 .start = stl_start,
2497 .hangup = stl_hangup,
2498 .flush_buffer = stl_flushbuffer,
2499 .break_ctl = stl_breakctl,
2500 .wait_until_sent = stl_waituntilsent,
2501 .send_xchar = stl_sendxchar,
2502 .tiocmget = stl_tiocmget,
2503 .tiocmset = stl_tiocmset,
2504 .proc_fops = &stl_proc_fops,
2505};
2506
2507static const struct tty_port_operations stl_port_ops = {
2508 .carrier_raised = stl_carrier_raised,
2509 .dtr_rts = stl_dtr_rts,
2510 .activate = stl_activate,
2511 .shutdown = stl_shutdown,
2512};
2513
2514/*****************************************************************************/
2515/* CD1400 HARDWARE FUNCTIONS */
2516/*****************************************************************************/
2517
2518/*
2519 * These functions get/set/update the registers of the cd1400 UARTs.
2520 * Access to the cd1400 registers is via an address/data io port pair.
2521 * (Maybe should make this inline...)
2522 */
2523
2524static int stl_cd1400getreg(struct stlport *portp, int regnr)
2525{
2526 outb((regnr + portp->uartaddr), portp->ioaddr);
2527 return inb(portp->ioaddr + EREG_DATA);
2528}
2529
2530static void stl_cd1400setreg(struct stlport *portp, int regnr, int value)
2531{
2532 outb(regnr + portp->uartaddr, portp->ioaddr);
2533 outb(value, portp->ioaddr + EREG_DATA);
2534}
2535
2536static int stl_cd1400updatereg(struct stlport *portp, int regnr, int value)
2537{
2538 outb(regnr + portp->uartaddr, portp->ioaddr);
2539 if (inb(portp->ioaddr + EREG_DATA) != value) {
2540 outb(value, portp->ioaddr + EREG_DATA);
2541 return 1;
2542 }
2543 return 0;
2544}
2545
2546/*****************************************************************************/
2547
2548/*
2549 * Inbitialize the UARTs in a panel. We don't care what sort of board
2550 * these ports are on - since the port io registers are almost
2551 * identical when dealing with ports.
2552 */
2553
2554static int stl_cd1400panelinit(struct stlbrd *brdp, struct stlpanel *panelp)
2555{
2556 unsigned int gfrcr;
2557 int chipmask, i, j;
2558 int nrchips, uartaddr, ioaddr;
2559 unsigned long flags;
2560
2561 pr_debug("stl_panelinit(brdp=%p,panelp=%p)\n", brdp, panelp);
2562
2563 spin_lock_irqsave(&brd_lock, flags);
2564 BRDENABLE(panelp->brdnr, panelp->pagenr);
2565
2566/*
2567 * Check that each chip is present and started up OK.
2568 */
2569 chipmask = 0;
2570 nrchips = panelp->nrports / CD1400_PORTS;
2571 for (i = 0; i < nrchips; i++) {
2572 if (brdp->brdtype == BRD_ECHPCI) {
2573 outb((panelp->pagenr + (i >> 1)), brdp->ioctrl);
2574 ioaddr = panelp->iobase;
2575 } else
2576 ioaddr = panelp->iobase + (EREG_BANKSIZE * (i >> 1));
2577 uartaddr = (i & 0x01) ? 0x080 : 0;
2578 outb((GFRCR + uartaddr), ioaddr);
2579 outb(0, (ioaddr + EREG_DATA));
2580 outb((CCR + uartaddr), ioaddr);
2581 outb(CCR_RESETFULL, (ioaddr + EREG_DATA));
2582 outb(CCR_RESETFULL, (ioaddr + EREG_DATA));
2583 outb((GFRCR + uartaddr), ioaddr);
2584 for (j = 0; j < CCR_MAXWAIT; j++)
2585 if ((gfrcr = inb(ioaddr + EREG_DATA)) != 0)
2586 break;
2587
2588 if ((j >= CCR_MAXWAIT) || (gfrcr < 0x40) || (gfrcr > 0x60)) {
2589 printk("STALLION: cd1400 not responding, "
2590 "brd=%d panel=%d chip=%d\n",
2591 panelp->brdnr, panelp->panelnr, i);
2592 continue;
2593 }
2594 chipmask |= (0x1 << i);
2595 outb((PPR + uartaddr), ioaddr);
2596 outb(PPR_SCALAR, (ioaddr + EREG_DATA));
2597 }
2598
2599 BRDDISABLE(panelp->brdnr);
2600 spin_unlock_irqrestore(&brd_lock, flags);
2601 return chipmask;
2602}
2603
2604/*****************************************************************************/
2605
2606/*
2607 * Initialize hardware specific port registers.
2608 */
2609
2610static void stl_cd1400portinit(struct stlbrd *brdp, struct stlpanel *panelp, struct stlport *portp)
2611{
2612 unsigned long flags;
2613 pr_debug("stl_cd1400portinit(brdp=%p,panelp=%p,portp=%p)\n", brdp,
2614 panelp, portp);
2615
2616 if ((brdp == NULL) || (panelp == NULL) ||
2617 (portp == NULL))
2618 return;
2619
2620 spin_lock_irqsave(&brd_lock, flags);
2621 portp->ioaddr = panelp->iobase + (((brdp->brdtype == BRD_ECHPCI) ||
2622 (portp->portnr < 8)) ? 0 : EREG_BANKSIZE);
2623 portp->uartaddr = (portp->portnr & 0x04) << 5;
2624 portp->pagenr = panelp->pagenr + (portp->portnr >> 3);
2625
2626 BRDENABLE(portp->brdnr, portp->pagenr);
2627 stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
2628 stl_cd1400setreg(portp, LIVR, (portp->portnr << 3));
2629 portp->hwid = stl_cd1400getreg(portp, GFRCR);
2630 BRDDISABLE(portp->brdnr);
2631 spin_unlock_irqrestore(&brd_lock, flags);
2632}
2633
2634/*****************************************************************************/
2635
2636/*
2637 * Wait for the command register to be ready. We will poll this,
2638 * since it won't usually take too long to be ready.
2639 */
2640
2641static void stl_cd1400ccrwait(struct stlport *portp)
2642{
2643 int i;
2644
2645 for (i = 0; i < CCR_MAXWAIT; i++)
2646 if (stl_cd1400getreg(portp, CCR) == 0)
2647 return;
2648
2649 printk("STALLION: cd1400 not responding, port=%d panel=%d brd=%d\n",
2650 portp->portnr, portp->panelnr, portp->brdnr);
2651}
2652
2653/*****************************************************************************/
2654
2655/*
2656 * Set up the cd1400 registers for a port based on the termios port
2657 * settings.
2658 */
2659
2660static void stl_cd1400setport(struct stlport *portp, struct ktermios *tiosp)
2661{
2662 struct stlbrd *brdp;
2663 unsigned long flags;
2664 unsigned int clkdiv, baudrate;
2665 unsigned char cor1, cor2, cor3;
2666 unsigned char cor4, cor5, ccr;
2667 unsigned char srer, sreron, sreroff;
2668 unsigned char mcor1, mcor2, rtpr;
2669 unsigned char clk, div;
2670
2671 cor1 = 0;
2672 cor2 = 0;
2673 cor3 = 0;
2674 cor4 = 0;
2675 cor5 = 0;
2676 ccr = 0;
2677 rtpr = 0;
2678 clk = 0;
2679 div = 0;
2680 mcor1 = 0;
2681 mcor2 = 0;
2682 sreron = 0;
2683 sreroff = 0;
2684
2685 brdp = stl_brds[portp->brdnr];
2686 if (brdp == NULL)
2687 return;
2688
2689/*
2690 * Set up the RX char ignore mask with those RX error types we
2691 * can ignore. We can get the cd1400 to help us out a little here,
2692 * it will ignore parity errors and breaks for us.
2693 */
2694 portp->rxignoremsk = 0;
2695 if (tiosp->c_iflag & IGNPAR) {
2696 portp->rxignoremsk |= (ST_PARITY | ST_FRAMING | ST_OVERRUN);
2697 cor1 |= COR1_PARIGNORE;
2698 }
2699 if (tiosp->c_iflag & IGNBRK) {
2700 portp->rxignoremsk |= ST_BREAK;
2701 cor4 |= COR4_IGNBRK;
2702 }
2703
2704 portp->rxmarkmsk = ST_OVERRUN;
2705 if (tiosp->c_iflag & (INPCK | PARMRK))
2706 portp->rxmarkmsk |= (ST_PARITY | ST_FRAMING);
2707 if (tiosp->c_iflag & BRKINT)
2708 portp->rxmarkmsk |= ST_BREAK;
2709
2710/*
2711 * Go through the char size, parity and stop bits and set all the
2712 * option register appropriately.
2713 */
2714 switch (tiosp->c_cflag & CSIZE) {
2715 case CS5:
2716 cor1 |= COR1_CHL5;
2717 break;
2718 case CS6:
2719 cor1 |= COR1_CHL6;
2720 break;
2721 case CS7:
2722 cor1 |= COR1_CHL7;
2723 break;
2724 default:
2725 cor1 |= COR1_CHL8;
2726 break;
2727 }
2728
2729 if (tiosp->c_cflag & CSTOPB)
2730 cor1 |= COR1_STOP2;
2731 else
2732 cor1 |= COR1_STOP1;
2733
2734 if (tiosp->c_cflag & PARENB) {
2735 if (tiosp->c_cflag & PARODD)
2736 cor1 |= (COR1_PARENB | COR1_PARODD);
2737 else
2738 cor1 |= (COR1_PARENB | COR1_PAREVEN);
2739 } else {
2740 cor1 |= COR1_PARNONE;
2741 }
2742
2743/*
2744 * Set the RX FIFO threshold at 6 chars. This gives a bit of breathing
2745 * space for hardware flow control and the like. This should be set to
2746 * VMIN. Also here we will set the RX data timeout to 10ms - this should
2747 * really be based on VTIME.
2748 */
2749 cor3 |= FIFO_RXTHRESHOLD;
2750 rtpr = 2;
2751
2752/*
2753 * Calculate the baud rate timers. For now we will just assume that
2754 * the input and output baud are the same. Could have used a baud
2755 * table here, but this way we can generate virtually any baud rate
2756 * we like!
2757 */
2758 baudrate = tiosp->c_cflag & CBAUD;
2759 if (baudrate & CBAUDEX) {
2760 baudrate &= ~CBAUDEX;
2761 if ((baudrate < 1) || (baudrate > 4))
2762 tiosp->c_cflag &= ~CBAUDEX;
2763 else
2764 baudrate += 15;
2765 }
2766 baudrate = stl_baudrates[baudrate];
2767 if ((tiosp->c_cflag & CBAUD) == B38400) {
2768 if ((portp->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
2769 baudrate = 57600;
2770 else if ((portp->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
2771 baudrate = 115200;
2772 else if ((portp->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
2773 baudrate = 230400;
2774 else if ((portp->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
2775 baudrate = 460800;
2776 else if ((portp->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST)
2777 baudrate = (portp->baud_base / portp->custom_divisor);
2778 }
2779 if (baudrate > STL_CD1400MAXBAUD)
2780 baudrate = STL_CD1400MAXBAUD;
2781
2782 if (baudrate > 0) {
2783 for (clk = 0; clk < CD1400_NUMCLKS; clk++) {
2784 clkdiv = (portp->clk / stl_cd1400clkdivs[clk]) / baudrate;
2785 if (clkdiv < 0x100)
2786 break;
2787 }
2788 div = (unsigned char) clkdiv;
2789 }
2790
2791/*
2792 * Check what form of modem signaling is required and set it up.
2793 */
2794 if ((tiosp->c_cflag & CLOCAL) == 0) {
2795 mcor1 |= MCOR1_DCD;
2796 mcor2 |= MCOR2_DCD;
2797 sreron |= SRER_MODEM;
2798 portp->port.flags |= ASYNC_CHECK_CD;
2799 } else
2800 portp->port.flags &= ~ASYNC_CHECK_CD;
2801
2802/*
2803 * Setup cd1400 enhanced modes if we can. In particular we want to
2804 * handle as much of the flow control as possible automatically. As
2805 * well as saving a few CPU cycles it will also greatly improve flow
2806 * control reliability.
2807 */
2808 if (tiosp->c_iflag & IXON) {
2809 cor2 |= COR2_TXIBE;
2810 cor3 |= COR3_SCD12;
2811 if (tiosp->c_iflag & IXANY)
2812 cor2 |= COR2_IXM;
2813 }
2814
2815 if (tiosp->c_cflag & CRTSCTS) {
2816 cor2 |= COR2_CTSAE;
2817 mcor1 |= FIFO_RTSTHRESHOLD;
2818 }
2819
2820/*
2821 * All cd1400 register values calculated so go through and set
2822 * them all up.
2823 */
2824
2825 pr_debug("SETPORT: portnr=%d panelnr=%d brdnr=%d\n",
2826 portp->portnr, portp->panelnr, portp->brdnr);
2827 pr_debug(" cor1=%x cor2=%x cor3=%x cor4=%x cor5=%x\n",
2828 cor1, cor2, cor3, cor4, cor5);
2829 pr_debug(" mcor1=%x mcor2=%x rtpr=%x sreron=%x sreroff=%x\n",
2830 mcor1, mcor2, rtpr, sreron, sreroff);
2831 pr_debug(" tcor=%x tbpr=%x rcor=%x rbpr=%x\n", clk, div, clk, div);
2832 pr_debug(" schr1=%x schr2=%x schr3=%x schr4=%x\n",
2833 tiosp->c_cc[VSTART], tiosp->c_cc[VSTOP],
2834 tiosp->c_cc[VSTART], tiosp->c_cc[VSTOP]);
2835
2836 spin_lock_irqsave(&brd_lock, flags);
2837 BRDENABLE(portp->brdnr, portp->pagenr);
2838 stl_cd1400setreg(portp, CAR, (portp->portnr & 0x3));
2839 srer = stl_cd1400getreg(portp, SRER);
2840 stl_cd1400setreg(portp, SRER, 0);
2841 if (stl_cd1400updatereg(portp, COR1, cor1))
2842 ccr = 1;
2843 if (stl_cd1400updatereg(portp, COR2, cor2))
2844 ccr = 1;
2845 if (stl_cd1400updatereg(portp, COR3, cor3))
2846 ccr = 1;
2847 if (ccr) {
2848 stl_cd1400ccrwait(portp);
2849 stl_cd1400setreg(portp, CCR, CCR_CORCHANGE);
2850 }
2851 stl_cd1400setreg(portp, COR4, cor4);
2852 stl_cd1400setreg(portp, COR5, cor5);
2853 stl_cd1400setreg(portp, MCOR1, mcor1);
2854 stl_cd1400setreg(portp, MCOR2, mcor2);
2855 if (baudrate > 0) {
2856 stl_cd1400setreg(portp, TCOR, clk);
2857 stl_cd1400setreg(portp, TBPR, div);
2858 stl_cd1400setreg(portp, RCOR, clk);
2859 stl_cd1400setreg(portp, RBPR, div);
2860 }
2861 stl_cd1400setreg(portp, SCHR1, tiosp->c_cc[VSTART]);
2862 stl_cd1400setreg(portp, SCHR2, tiosp->c_cc[VSTOP]);
2863 stl_cd1400setreg(portp, SCHR3, tiosp->c_cc[VSTART]);
2864 stl_cd1400setreg(portp, SCHR4, tiosp->c_cc[VSTOP]);
2865 stl_cd1400setreg(portp, RTPR, rtpr);
2866 mcor1 = stl_cd1400getreg(portp, MSVR1);
2867 if (mcor1 & MSVR1_DCD)
2868 portp->sigs |= TIOCM_CD;
2869 else
2870 portp->sigs &= ~TIOCM_CD;
2871 stl_cd1400setreg(portp, SRER, ((srer & ~sreroff) | sreron));
2872 BRDDISABLE(portp->brdnr);
2873 spin_unlock_irqrestore(&brd_lock, flags);
2874}
2875
2876/*****************************************************************************/
2877
2878/*
2879 * Set the state of the DTR and RTS signals.
2880 */
2881
2882static void stl_cd1400setsignals(struct stlport *portp, int dtr, int rts)
2883{
2884 unsigned char msvr1, msvr2;
2885 unsigned long flags;
2886
2887 pr_debug("stl_cd1400setsignals(portp=%p,dtr=%d,rts=%d)\n",
2888 portp, dtr, rts);
2889
2890 msvr1 = 0;
2891 msvr2 = 0;
2892 if (dtr > 0)
2893 msvr1 = MSVR1_DTR;
2894 if (rts > 0)
2895 msvr2 = MSVR2_RTS;
2896
2897 spin_lock_irqsave(&brd_lock, flags);
2898 BRDENABLE(portp->brdnr, portp->pagenr);
2899 stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
2900 if (rts >= 0)
2901 stl_cd1400setreg(portp, MSVR2, msvr2);
2902 if (dtr >= 0)
2903 stl_cd1400setreg(portp, MSVR1, msvr1);
2904 BRDDISABLE(portp->brdnr);
2905 spin_unlock_irqrestore(&brd_lock, flags);
2906}
2907
2908/*****************************************************************************/
2909
2910/*
2911 * Return the state of the signals.
2912 */
2913
2914static int stl_cd1400getsignals(struct stlport *portp)
2915{
2916 unsigned char msvr1, msvr2;
2917 unsigned long flags;
2918 int sigs;
2919
2920 pr_debug("stl_cd1400getsignals(portp=%p)\n", portp);
2921
2922 spin_lock_irqsave(&brd_lock, flags);
2923 BRDENABLE(portp->brdnr, portp->pagenr);
2924 stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
2925 msvr1 = stl_cd1400getreg(portp, MSVR1);
2926 msvr2 = stl_cd1400getreg(portp, MSVR2);
2927 BRDDISABLE(portp->brdnr);
2928 spin_unlock_irqrestore(&brd_lock, flags);
2929
2930 sigs = 0;
2931 sigs |= (msvr1 & MSVR1_DCD) ? TIOCM_CD : 0;
2932 sigs |= (msvr1 & MSVR1_CTS) ? TIOCM_CTS : 0;
2933 sigs |= (msvr1 & MSVR1_DTR) ? TIOCM_DTR : 0;
2934 sigs |= (msvr2 & MSVR2_RTS) ? TIOCM_RTS : 0;
2935#if 0
2936 sigs |= (msvr1 & MSVR1_RI) ? TIOCM_RI : 0;
2937 sigs |= (msvr1 & MSVR1_DSR) ? TIOCM_DSR : 0;
2938#else
2939 sigs |= TIOCM_DSR;
2940#endif
2941 return sigs;
2942}
2943
2944/*****************************************************************************/
2945
2946/*
2947 * Enable/Disable the Transmitter and/or Receiver.
2948 */
2949
2950static void stl_cd1400enablerxtx(struct stlport *portp, int rx, int tx)
2951{
2952 unsigned char ccr;
2953 unsigned long flags;
2954
2955 pr_debug("stl_cd1400enablerxtx(portp=%p,rx=%d,tx=%d)\n", portp, rx, tx);
2956
2957 ccr = 0;
2958
2959 if (tx == 0)
2960 ccr |= CCR_TXDISABLE;
2961 else if (tx > 0)
2962 ccr |= CCR_TXENABLE;
2963 if (rx == 0)
2964 ccr |= CCR_RXDISABLE;
2965 else if (rx > 0)
2966 ccr |= CCR_RXENABLE;
2967
2968 spin_lock_irqsave(&brd_lock, flags);
2969 BRDENABLE(portp->brdnr, portp->pagenr);
2970 stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
2971 stl_cd1400ccrwait(portp);
2972 stl_cd1400setreg(portp, CCR, ccr);
2973 stl_cd1400ccrwait(portp);
2974 BRDDISABLE(portp->brdnr);
2975 spin_unlock_irqrestore(&brd_lock, flags);
2976}
2977
2978/*****************************************************************************/
2979
2980/*
2981 * Start/stop the Transmitter and/or Receiver.
2982 */
2983
2984static void stl_cd1400startrxtx(struct stlport *portp, int rx, int tx)
2985{
2986 unsigned char sreron, sreroff;
2987 unsigned long flags;
2988
2989 pr_debug("stl_cd1400startrxtx(portp=%p,rx=%d,tx=%d)\n", portp, rx, tx);
2990
2991 sreron = 0;
2992 sreroff = 0;
2993 if (tx == 0)
2994 sreroff |= (SRER_TXDATA | SRER_TXEMPTY);
2995 else if (tx == 1)
2996 sreron |= SRER_TXDATA;
2997 else if (tx >= 2)
2998 sreron |= SRER_TXEMPTY;
2999 if (rx == 0)
3000 sreroff |= SRER_RXDATA;
3001 else if (rx > 0)
3002 sreron |= SRER_RXDATA;
3003
3004 spin_lock_irqsave(&brd_lock, flags);
3005 BRDENABLE(portp->brdnr, portp->pagenr);
3006 stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
3007 stl_cd1400setreg(portp, SRER,
3008 ((stl_cd1400getreg(portp, SRER) & ~sreroff) | sreron));
3009 BRDDISABLE(portp->brdnr);
3010 if (tx > 0)
3011 set_bit(ASYI_TXBUSY, &portp->istate);
3012 spin_unlock_irqrestore(&brd_lock, flags);
3013}
3014
3015/*****************************************************************************/
3016
3017/*
3018 * Disable all interrupts from this port.
3019 */
3020
3021static void stl_cd1400disableintrs(struct stlport *portp)
3022{
3023 unsigned long flags;
3024
3025 pr_debug("stl_cd1400disableintrs(portp=%p)\n", portp);
3026
3027 spin_lock_irqsave(&brd_lock, flags);
3028 BRDENABLE(portp->brdnr, portp->pagenr);
3029 stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
3030 stl_cd1400setreg(portp, SRER, 0);
3031 BRDDISABLE(portp->brdnr);
3032 spin_unlock_irqrestore(&brd_lock, flags);
3033}
3034
3035/*****************************************************************************/
3036
3037static void stl_cd1400sendbreak(struct stlport *portp, int len)
3038{
3039 unsigned long flags;
3040
3041 pr_debug("stl_cd1400sendbreak(portp=%p,len=%d)\n", portp, len);
3042
3043 spin_lock_irqsave(&brd_lock, flags);
3044 BRDENABLE(portp->brdnr, portp->pagenr);
3045 stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
3046 stl_cd1400setreg(portp, SRER,
3047 ((stl_cd1400getreg(portp, SRER) & ~SRER_TXDATA) |
3048 SRER_TXEMPTY));
3049 BRDDISABLE(portp->brdnr);
3050 portp->brklen = len;
3051 if (len == 1)
3052 portp->stats.txbreaks++;
3053 spin_unlock_irqrestore(&brd_lock, flags);
3054}
3055
3056/*****************************************************************************/
3057
3058/*
3059 * Take flow control actions...
3060 */
3061
3062static void stl_cd1400flowctrl(struct stlport *portp, int state)
3063{
3064 struct tty_struct *tty;
3065 unsigned long flags;
3066
3067 pr_debug("stl_cd1400flowctrl(portp=%p,state=%x)\n", portp, state);
3068
3069 if (portp == NULL)
3070 return;
3071 tty = tty_port_tty_get(&portp->port);
3072 if (tty == NULL)
3073 return;
3074
3075 spin_lock_irqsave(&brd_lock, flags);
3076 BRDENABLE(portp->brdnr, portp->pagenr);
3077 stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
3078
3079 if (state) {
3080 if (tty->termios->c_iflag & IXOFF) {
3081 stl_cd1400ccrwait(portp);
3082 stl_cd1400setreg(portp, CCR, CCR_SENDSCHR1);
3083 portp->stats.rxxon++;
3084 stl_cd1400ccrwait(portp);
3085 }
3086/*
3087 * Question: should we return RTS to what it was before? It may
3088 * have been set by an ioctl... Suppose not, since if you have
3089 * hardware flow control set then it is pretty silly to go and
3090 * set the RTS line by hand.
3091 */
3092 if (tty->termios->c_cflag & CRTSCTS) {
3093 stl_cd1400setreg(portp, MCOR1,
3094 (stl_cd1400getreg(portp, MCOR1) |
3095 FIFO_RTSTHRESHOLD));
3096 stl_cd1400setreg(portp, MSVR2, MSVR2_RTS);
3097 portp->stats.rxrtson++;
3098 }
3099 } else {
3100 if (tty->termios->c_iflag & IXOFF) {
3101 stl_cd1400ccrwait(portp);
3102 stl_cd1400setreg(portp, CCR, CCR_SENDSCHR2);
3103 portp->stats.rxxoff++;
3104 stl_cd1400ccrwait(portp);
3105 }
3106 if (tty->termios->c_cflag & CRTSCTS) {
3107 stl_cd1400setreg(portp, MCOR1,
3108 (stl_cd1400getreg(portp, MCOR1) & 0xf0));
3109 stl_cd1400setreg(portp, MSVR2, 0);
3110 portp->stats.rxrtsoff++;
3111 }
3112 }
3113
3114 BRDDISABLE(portp->brdnr);
3115 spin_unlock_irqrestore(&brd_lock, flags);
3116 tty_kref_put(tty);
3117}
3118
3119/*****************************************************************************/
3120
3121/*
3122 * Send a flow control character...
3123 */
3124
3125static void stl_cd1400sendflow(struct stlport *portp, int state)
3126{
3127 struct tty_struct *tty;
3128 unsigned long flags;
3129
3130 pr_debug("stl_cd1400sendflow(portp=%p,state=%x)\n", portp, state);
3131
3132 if (portp == NULL)
3133 return;
3134 tty = tty_port_tty_get(&portp->port);
3135 if (tty == NULL)
3136 return;
3137
3138 spin_lock_irqsave(&brd_lock, flags);
3139 BRDENABLE(portp->brdnr, portp->pagenr);
3140 stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
3141 if (state) {
3142 stl_cd1400ccrwait(portp);
3143 stl_cd1400setreg(portp, CCR, CCR_SENDSCHR1);
3144 portp->stats.rxxon++;
3145 stl_cd1400ccrwait(portp);
3146 } else {
3147 stl_cd1400ccrwait(portp);
3148 stl_cd1400setreg(portp, CCR, CCR_SENDSCHR2);
3149 portp->stats.rxxoff++;
3150 stl_cd1400ccrwait(portp);
3151 }
3152 BRDDISABLE(portp->brdnr);
3153 spin_unlock_irqrestore(&brd_lock, flags);
3154 tty_kref_put(tty);
3155}
3156
3157/*****************************************************************************/
3158
3159static void stl_cd1400flush(struct stlport *portp)
3160{
3161 unsigned long flags;
3162
3163 pr_debug("stl_cd1400flush(portp=%p)\n", portp);
3164
3165 if (portp == NULL)
3166 return;
3167
3168 spin_lock_irqsave(&brd_lock, flags);
3169 BRDENABLE(portp->brdnr, portp->pagenr);
3170 stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
3171 stl_cd1400ccrwait(portp);
3172 stl_cd1400setreg(portp, CCR, CCR_TXFLUSHFIFO);
3173 stl_cd1400ccrwait(portp);
3174 portp->tx.tail = portp->tx.head;
3175 BRDDISABLE(portp->brdnr);
3176 spin_unlock_irqrestore(&brd_lock, flags);
3177}
3178
3179/*****************************************************************************/
3180
3181/*
3182 * Return the current state of data flow on this port. This is only
3183 * really interresting when determining if data has fully completed
3184 * transmission or not... This is easy for the cd1400, it accurately
3185 * maintains the busy port flag.
3186 */
3187
3188static int stl_cd1400datastate(struct stlport *portp)
3189{
3190 pr_debug("stl_cd1400datastate(portp=%p)\n", portp);
3191
3192 if (portp == NULL)
3193 return 0;
3194
3195 return test_bit(ASYI_TXBUSY, &portp->istate) ? 1 : 0;
3196}
3197
3198/*****************************************************************************/
3199
3200/*
3201 * Interrupt service routine for cd1400 EasyIO boards.
3202 */
3203
3204static void stl_cd1400eiointr(struct stlpanel *panelp, unsigned int iobase)
3205{
3206 unsigned char svrtype;
3207
3208 pr_debug("stl_cd1400eiointr(panelp=%p,iobase=%x)\n", panelp, iobase);
3209
3210 spin_lock(&brd_lock);
3211 outb(SVRR, iobase);
3212 svrtype = inb(iobase + EREG_DATA);
3213 if (panelp->nrports > 4) {
3214 outb((SVRR + 0x80), iobase);
3215 svrtype |= inb(iobase + EREG_DATA);
3216 }
3217
3218 if (svrtype & SVRR_RX)
3219 stl_cd1400rxisr(panelp, iobase);
3220 else if (svrtype & SVRR_TX)
3221 stl_cd1400txisr(panelp, iobase);
3222 else if (svrtype & SVRR_MDM)
3223 stl_cd1400mdmisr(panelp, iobase);
3224
3225 spin_unlock(&brd_lock);
3226}
3227
3228/*****************************************************************************/
3229
3230/*
3231 * Interrupt service routine for cd1400 panels.
3232 */
3233
3234static void stl_cd1400echintr(struct stlpanel *panelp, unsigned int iobase)
3235{
3236 unsigned char svrtype;
3237
3238 pr_debug("stl_cd1400echintr(panelp=%p,iobase=%x)\n", panelp, iobase);
3239
3240 outb(SVRR, iobase);
3241 svrtype = inb(iobase + EREG_DATA);
3242 outb((SVRR + 0x80), iobase);
3243 svrtype |= inb(iobase + EREG_DATA);
3244 if (svrtype & SVRR_RX)
3245 stl_cd1400rxisr(panelp, iobase);
3246 else if (svrtype & SVRR_TX)
3247 stl_cd1400txisr(panelp, iobase);
3248 else if (svrtype & SVRR_MDM)
3249 stl_cd1400mdmisr(panelp, iobase);
3250}
3251
3252
3253/*****************************************************************************/
3254
3255/*
3256 * Unfortunately we need to handle breaks in the TX data stream, since
3257 * this is the only way to generate them on the cd1400.
3258 */
3259
3260static int stl_cd1400breakisr(struct stlport *portp, int ioaddr)
3261{
3262 if (portp->brklen == 1) {
3263 outb((COR2 + portp->uartaddr), ioaddr);
3264 outb((inb(ioaddr + EREG_DATA) | COR2_ETC),
3265 (ioaddr + EREG_DATA));
3266 outb((TDR + portp->uartaddr), ioaddr);
3267 outb(ETC_CMD, (ioaddr + EREG_DATA));
3268 outb(ETC_STARTBREAK, (ioaddr + EREG_DATA));
3269 outb((SRER + portp->uartaddr), ioaddr);
3270 outb((inb(ioaddr + EREG_DATA) & ~(SRER_TXDATA | SRER_TXEMPTY)),
3271 (ioaddr + EREG_DATA));
3272 return 1;
3273 } else if (portp->brklen > 1) {
3274 outb((TDR + portp->uartaddr), ioaddr);
3275 outb(ETC_CMD, (ioaddr + EREG_DATA));
3276 outb(ETC_STOPBREAK, (ioaddr + EREG_DATA));
3277 portp->brklen = -1;
3278 return 1;
3279 } else {
3280 outb((COR2 + portp->uartaddr), ioaddr);
3281 outb((inb(ioaddr + EREG_DATA) & ~COR2_ETC),
3282 (ioaddr + EREG_DATA));
3283 portp->brklen = 0;
3284 }
3285 return 0;
3286}
3287
3288/*****************************************************************************/
3289
3290/*
3291 * Transmit interrupt handler. This has gotta be fast! Handling TX
3292 * chars is pretty simple, stuff as many as possible from the TX buffer
3293 * into the cd1400 FIFO. Must also handle TX breaks here, since they
3294 * are embedded as commands in the data stream. Oh no, had to use a goto!
3295 * This could be optimized more, will do when I get time...
3296 * In practice it is possible that interrupts are enabled but that the
3297 * port has been hung up. Need to handle not having any TX buffer here,
3298 * this is done by using the side effect that head and tail will also
3299 * be NULL if the buffer has been freed.
3300 */
3301
3302static void stl_cd1400txisr(struct stlpanel *panelp, int ioaddr)
3303{
3304 struct stlport *portp;
3305 int len, stlen;
3306 char *head, *tail;
3307 unsigned char ioack, srer;
3308 struct tty_struct *tty;
3309
3310 pr_debug("stl_cd1400txisr(panelp=%p,ioaddr=%x)\n", panelp, ioaddr);
3311
3312 ioack = inb(ioaddr + EREG_TXACK);
3313 if (((ioack & panelp->ackmask) != 0) ||
3314 ((ioack & ACK_TYPMASK) != ACK_TYPTX)) {
3315 printk("STALLION: bad TX interrupt ack value=%x\n", ioack);
3316 return;
3317 }
3318 portp = panelp->ports[(ioack >> 3)];
3319
3320/*
3321 * Unfortunately we need to handle breaks in the data stream, since
3322 * this is the only way to generate them on the cd1400. Do it now if
3323 * a break is to be sent.
3324 */
3325 if (portp->brklen != 0)
3326 if (stl_cd1400breakisr(portp, ioaddr))
3327 goto stl_txalldone;
3328
3329 head = portp->tx.head;
3330 tail = portp->tx.tail;
3331 len = (head >= tail) ? (head - tail) : (STL_TXBUFSIZE - (tail - head));
3332 if ((len == 0) || ((len < STL_TXBUFLOW) &&
3333 (test_bit(ASYI_TXLOW, &portp->istate) == 0))) {
3334 set_bit(ASYI_TXLOW, &portp->istate);
3335 tty = tty_port_tty_get(&portp->port);
3336 if (tty) {
3337 tty_wakeup(tty);
3338 tty_kref_put(tty);
3339 }
3340 }
3341
3342 if (len == 0) {
3343 outb((SRER + portp->uartaddr), ioaddr);
3344 srer = inb(ioaddr + EREG_DATA);
3345 if (srer & SRER_TXDATA) {
3346 srer = (srer & ~SRER_TXDATA) | SRER_TXEMPTY;
3347 } else {
3348 srer &= ~(SRER_TXDATA | SRER_TXEMPTY);
3349 clear_bit(ASYI_TXBUSY, &portp->istate);
3350 }
3351 outb(srer, (ioaddr + EREG_DATA));
3352 } else {
3353 len = min(len, CD1400_TXFIFOSIZE);
3354 portp->stats.txtotal += len;
3355 stlen = min_t(unsigned int, len,
3356 (portp->tx.buf + STL_TXBUFSIZE) - tail);
3357 outb((TDR + portp->uartaddr), ioaddr);
3358 outsb((ioaddr + EREG_DATA), tail, stlen);
3359 len -= stlen;
3360 tail += stlen;
3361 if (tail >= (portp->tx.buf + STL_TXBUFSIZE))
3362 tail = portp->tx.buf;
3363 if (len > 0) {
3364 outsb((ioaddr + EREG_DATA), tail, len);
3365 tail += len;
3366 }
3367 portp->tx.tail = tail;
3368 }
3369
3370stl_txalldone:
3371 outb((EOSRR + portp->uartaddr), ioaddr);
3372 outb(0, (ioaddr + EREG_DATA));
3373}
3374
3375/*****************************************************************************/
3376
3377/*
3378 * Receive character interrupt handler. Determine if we have good chars
3379 * or bad chars and then process appropriately. Good chars are easy
3380 * just shove the lot into the RX buffer and set all status byte to 0.
3381 * If a bad RX char then process as required. This routine needs to be
3382 * fast! In practice it is possible that we get an interrupt on a port
3383 * that is closed. This can happen on hangups - since they completely
3384 * shutdown a port not in user context. Need to handle this case.
3385 */
3386
3387static void stl_cd1400rxisr(struct stlpanel *panelp, int ioaddr)
3388{
3389 struct stlport *portp;
3390 struct tty_struct *tty;
3391 unsigned int ioack, len, buflen;
3392 unsigned char status;
3393 char ch;
3394
3395 pr_debug("stl_cd1400rxisr(panelp=%p,ioaddr=%x)\n", panelp, ioaddr);
3396
3397 ioack = inb(ioaddr + EREG_RXACK);
3398 if ((ioack & panelp->ackmask) != 0) {
3399 printk("STALLION: bad RX interrupt ack value=%x\n", ioack);
3400 return;
3401 }
3402 portp = panelp->ports[(ioack >> 3)];
3403 tty = tty_port_tty_get(&portp->port);
3404
3405 if ((ioack & ACK_TYPMASK) == ACK_TYPRXGOOD) {
3406 outb((RDCR + portp->uartaddr), ioaddr);
3407 len = inb(ioaddr + EREG_DATA);
3408 if (tty == NULL || (buflen = tty_buffer_request_room(tty, len)) == 0) {
3409 len = min_t(unsigned int, len, sizeof(stl_unwanted));
3410 outb((RDSR + portp->uartaddr), ioaddr);
3411 insb((ioaddr + EREG_DATA), &stl_unwanted[0], len);
3412 portp->stats.rxlost += len;
3413 portp->stats.rxtotal += len;
3414 } else {
3415 len = min(len, buflen);
3416 if (len > 0) {
3417 unsigned char *ptr;
3418 outb((RDSR + portp->uartaddr), ioaddr);
3419 tty_prepare_flip_string(tty, &ptr, len);
3420 insb((ioaddr + EREG_DATA), ptr, len);
3421 tty_schedule_flip(tty);
3422 portp->stats.rxtotal += len;
3423 }
3424 }
3425 } else if ((ioack & ACK_TYPMASK) == ACK_TYPRXBAD) {
3426 outb((RDSR + portp->uartaddr), ioaddr);
3427 status = inb(ioaddr + EREG_DATA);
3428 ch = inb(ioaddr + EREG_DATA);
3429 if (status & ST_PARITY)
3430 portp->stats.rxparity++;
3431 if (status & ST_FRAMING)
3432 portp->stats.rxframing++;
3433 if (status & ST_OVERRUN)
3434 portp->stats.rxoverrun++;
3435 if (status & ST_BREAK)
3436 portp->stats.rxbreaks++;
3437 if (status & ST_SCHARMASK) {
3438 if ((status & ST_SCHARMASK) == ST_SCHAR1)
3439 portp->stats.txxon++;
3440 if ((status & ST_SCHARMASK) == ST_SCHAR2)
3441 portp->stats.txxoff++;
3442 goto stl_rxalldone;
3443 }
3444 if (tty != NULL && (portp->rxignoremsk & status) == 0) {
3445 if (portp->rxmarkmsk & status) {
3446 if (status & ST_BREAK) {
3447 status = TTY_BREAK;
3448 if (portp->port.flags & ASYNC_SAK) {
3449 do_SAK(tty);
3450 BRDENABLE(portp->brdnr, portp->pagenr);
3451 }
3452 } else if (status & ST_PARITY)
3453 status = TTY_PARITY;
3454 else if (status & ST_FRAMING)
3455 status = TTY_FRAME;
3456 else if(status & ST_OVERRUN)
3457 status = TTY_OVERRUN;
3458 else
3459 status = 0;
3460 } else
3461 status = 0;
3462 tty_insert_flip_char(tty, ch, status);
3463 tty_schedule_flip(tty);
3464 }
3465 } else {
3466 printk("STALLION: bad RX interrupt ack value=%x\n", ioack);
3467 tty_kref_put(tty);
3468 return;
3469 }
3470
3471stl_rxalldone:
3472 tty_kref_put(tty);
3473 outb((EOSRR + portp->uartaddr), ioaddr);
3474 outb(0, (ioaddr + EREG_DATA));
3475}
3476
3477/*****************************************************************************/
3478
3479/*
3480 * Modem interrupt handler. The is called when the modem signal line
3481 * (DCD) has changed state. Leave most of the work to the off-level
3482 * processing routine.
3483 */
3484
3485static void stl_cd1400mdmisr(struct stlpanel *panelp, int ioaddr)
3486{
3487 struct stlport *portp;
3488 unsigned int ioack;
3489 unsigned char misr;
3490
3491 pr_debug("stl_cd1400mdmisr(panelp=%p)\n", panelp);
3492
3493 ioack = inb(ioaddr + EREG_MDACK);
3494 if (((ioack & panelp->ackmask) != 0) ||
3495 ((ioack & ACK_TYPMASK) != ACK_TYPMDM)) {
3496 printk("STALLION: bad MODEM interrupt ack value=%x\n", ioack);
3497 return;
3498 }
3499 portp = panelp->ports[(ioack >> 3)];
3500
3501 outb((MISR + portp->uartaddr), ioaddr);
3502 misr = inb(ioaddr + EREG_DATA);
3503 if (misr & MISR_DCD) {
3504 stl_cd_change(portp);
3505 portp->stats.modem++;
3506 }
3507
3508 outb((EOSRR + portp->uartaddr), ioaddr);
3509 outb(0, (ioaddr + EREG_DATA));
3510}
3511
3512/*****************************************************************************/
3513/* SC26198 HARDWARE FUNCTIONS */
3514/*****************************************************************************/
3515
3516/*
3517 * These functions get/set/update the registers of the sc26198 UARTs.
3518 * Access to the sc26198 registers is via an address/data io port pair.
3519 * (Maybe should make this inline...)
3520 */
3521
3522static int stl_sc26198getreg(struct stlport *portp, int regnr)
3523{
3524 outb((regnr | portp->uartaddr), (portp->ioaddr + XP_ADDR));
3525 return inb(portp->ioaddr + XP_DATA);
3526}
3527
3528static void stl_sc26198setreg(struct stlport *portp, int regnr, int value)
3529{
3530 outb((regnr | portp->uartaddr), (portp->ioaddr + XP_ADDR));
3531 outb(value, (portp->ioaddr + XP_DATA));
3532}
3533
3534static int stl_sc26198updatereg(struct stlport *portp, int regnr, int value)
3535{
3536 outb((regnr | portp->uartaddr), (portp->ioaddr + XP_ADDR));
3537 if (inb(portp->ioaddr + XP_DATA) != value) {
3538 outb(value, (portp->ioaddr + XP_DATA));
3539 return 1;
3540 }
3541 return 0;
3542}
3543
3544/*****************************************************************************/
3545
3546/*
3547 * Functions to get and set the sc26198 global registers.
3548 */
3549
3550static int stl_sc26198getglobreg(struct stlport *portp, int regnr)
3551{
3552 outb(regnr, (portp->ioaddr + XP_ADDR));
3553 return inb(portp->ioaddr + XP_DATA);
3554}
3555
3556#if 0
3557static void stl_sc26198setglobreg(struct stlport *portp, int regnr, int value)
3558{
3559 outb(regnr, (portp->ioaddr + XP_ADDR));
3560 outb(value, (portp->ioaddr + XP_DATA));
3561}
3562#endif
3563
3564/*****************************************************************************/
3565
3566/*
3567 * Inbitialize the UARTs in a panel. We don't care what sort of board
3568 * these ports are on - since the port io registers are almost
3569 * identical when dealing with ports.
3570 */
3571
3572static int stl_sc26198panelinit(struct stlbrd *brdp, struct stlpanel *panelp)
3573{
3574 int chipmask, i;
3575 int nrchips, ioaddr;
3576
3577 pr_debug("stl_sc26198panelinit(brdp=%p,panelp=%p)\n", brdp, panelp);
3578
3579 BRDENABLE(panelp->brdnr, panelp->pagenr);
3580
3581/*
3582 * Check that each chip is present and started up OK.
3583 */
3584 chipmask = 0;
3585 nrchips = (panelp->nrports + 4) / SC26198_PORTS;
3586 if (brdp->brdtype == BRD_ECHPCI)
3587 outb(panelp->pagenr, brdp->ioctrl);
3588
3589 for (i = 0; i < nrchips; i++) {
3590 ioaddr = panelp->iobase + (i * 4);
3591 outb(SCCR, (ioaddr + XP_ADDR));
3592 outb(CR_RESETALL, (ioaddr + XP_DATA));
3593 outb(TSTR, (ioaddr + XP_ADDR));
3594 if (inb(ioaddr + XP_DATA) != 0) {
3595 printk("STALLION: sc26198 not responding, "
3596 "brd=%d panel=%d chip=%d\n",
3597 panelp->brdnr, panelp->panelnr, i);
3598 continue;
3599 }
3600 chipmask |= (0x1 << i);
3601 outb(GCCR, (ioaddr + XP_ADDR));
3602 outb(GCCR_IVRTYPCHANACK, (ioaddr + XP_DATA));
3603 outb(WDTRCR, (ioaddr + XP_ADDR));
3604 outb(0xff, (ioaddr + XP_DATA));
3605 }
3606
3607 BRDDISABLE(panelp->brdnr);
3608 return chipmask;
3609}
3610
3611/*****************************************************************************/
3612
3613/*
3614 * Initialize hardware specific port registers.
3615 */
3616
3617static void stl_sc26198portinit(struct stlbrd *brdp, struct stlpanel *panelp, struct stlport *portp)
3618{
3619 pr_debug("stl_sc26198portinit(brdp=%p,panelp=%p,portp=%p)\n", brdp,
3620 panelp, portp);
3621
3622 if ((brdp == NULL) || (panelp == NULL) ||
3623 (portp == NULL))
3624 return;
3625
3626 portp->ioaddr = panelp->iobase + ((portp->portnr < 8) ? 0 : 4);
3627 portp->uartaddr = (portp->portnr & 0x07) << 4;
3628 portp->pagenr = panelp->pagenr;
3629 portp->hwid = 0x1;
3630
3631 BRDENABLE(portp->brdnr, portp->pagenr);
3632 stl_sc26198setreg(portp, IOPCR, IOPCR_SETSIGS);
3633 BRDDISABLE(portp->brdnr);
3634}
3635
3636/*****************************************************************************/
3637
3638/*
3639 * Set up the sc26198 registers for a port based on the termios port
3640 * settings.
3641 */
3642
3643static void stl_sc26198setport(struct stlport *portp, struct ktermios *tiosp)
3644{
3645 struct stlbrd *brdp;
3646 unsigned long flags;
3647 unsigned int baudrate;
3648 unsigned char mr0, mr1, mr2, clk;
3649 unsigned char imron, imroff, iopr, ipr;
3650
3651 mr0 = 0;
3652 mr1 = 0;
3653 mr2 = 0;
3654 clk = 0;
3655 iopr = 0;
3656 imron = 0;
3657 imroff = 0;
3658
3659 brdp = stl_brds[portp->brdnr];
3660 if (brdp == NULL)
3661 return;
3662
3663/*
3664 * Set up the RX char ignore mask with those RX error types we
3665 * can ignore.
3666 */
3667 portp->rxignoremsk = 0;
3668 if (tiosp->c_iflag & IGNPAR)
3669 portp->rxignoremsk |= (SR_RXPARITY | SR_RXFRAMING |
3670 SR_RXOVERRUN);
3671 if (tiosp->c_iflag & IGNBRK)
3672 portp->rxignoremsk |= SR_RXBREAK;
3673
3674 portp->rxmarkmsk = SR_RXOVERRUN;
3675 if (tiosp->c_iflag & (INPCK | PARMRK))
3676 portp->rxmarkmsk |= (SR_RXPARITY | SR_RXFRAMING);
3677 if (tiosp->c_iflag & BRKINT)
3678 portp->rxmarkmsk |= SR_RXBREAK;
3679
3680/*
3681 * Go through the char size, parity and stop bits and set all the
3682 * option register appropriately.
3683 */
3684 switch (tiosp->c_cflag & CSIZE) {
3685 case CS5:
3686 mr1 |= MR1_CS5;
3687 break;
3688 case CS6:
3689 mr1 |= MR1_CS6;
3690 break;
3691 case CS7:
3692 mr1 |= MR1_CS7;
3693 break;
3694 default:
3695 mr1 |= MR1_CS8;
3696 break;
3697 }
3698
3699 if (tiosp->c_cflag & CSTOPB)
3700 mr2 |= MR2_STOP2;
3701 else
3702 mr2 |= MR2_STOP1;
3703
3704 if (tiosp->c_cflag & PARENB) {
3705 if (tiosp->c_cflag & PARODD)
3706 mr1 |= (MR1_PARENB | MR1_PARODD);
3707 else
3708 mr1 |= (MR1_PARENB | MR1_PAREVEN);
3709 } else
3710 mr1 |= MR1_PARNONE;
3711
3712 mr1 |= MR1_ERRBLOCK;
3713
3714/*
3715 * Set the RX FIFO threshold at 8 chars. This gives a bit of breathing
3716 * space for hardware flow control and the like. This should be set to
3717 * VMIN.
3718 */
3719 mr2 |= MR2_RXFIFOHALF;
3720
3721/*
3722 * Calculate the baud rate timers. For now we will just assume that
3723 * the input and output baud are the same. The sc26198 has a fixed
3724 * baud rate table, so only discrete baud rates possible.
3725 */
3726 baudrate = tiosp->c_cflag & CBAUD;
3727 if (baudrate & CBAUDEX) {
3728 baudrate &= ~CBAUDEX;
3729 if ((baudrate < 1) || (baudrate > 4))
3730 tiosp->c_cflag &= ~CBAUDEX;
3731 else
3732 baudrate += 15;
3733 }
3734 baudrate = stl_baudrates[baudrate];
3735 if ((tiosp->c_cflag & CBAUD) == B38400) {
3736 if ((portp->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
3737 baudrate = 57600;
3738 else if ((portp->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
3739 baudrate = 115200;
3740 else if ((portp->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
3741 baudrate = 230400;
3742 else if ((portp->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
3743 baudrate = 460800;
3744 else if ((portp->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST)
3745 baudrate = (portp->baud_base / portp->custom_divisor);
3746 }
3747 if (baudrate > STL_SC26198MAXBAUD)
3748 baudrate = STL_SC26198MAXBAUD;
3749
3750 if (baudrate > 0)
3751 for (clk = 0; clk < SC26198_NRBAUDS; clk++)
3752 if (baudrate <= sc26198_baudtable[clk])
3753 break;
3754
3755/*
3756 * Check what form of modem signaling is required and set it up.
3757 */
3758 if (tiosp->c_cflag & CLOCAL) {
3759 portp->port.flags &= ~ASYNC_CHECK_CD;
3760 } else {
3761 iopr |= IOPR_DCDCOS;
3762 imron |= IR_IOPORT;
3763 portp->port.flags |= ASYNC_CHECK_CD;
3764 }
3765
3766/*
3767 * Setup sc26198 enhanced modes if we can. In particular we want to
3768 * handle as much of the flow control as possible automatically. As
3769 * well as saving a few CPU cycles it will also greatly improve flow
3770 * control reliability.
3771 */
3772 if (tiosp->c_iflag & IXON) {
3773 mr0 |= MR0_SWFTX | MR0_SWFT;
3774 imron |= IR_XONXOFF;
3775 } else
3776 imroff |= IR_XONXOFF;
3777
3778 if (tiosp->c_iflag & IXOFF)
3779 mr0 |= MR0_SWFRX;
3780
3781 if (tiosp->c_cflag & CRTSCTS) {
3782 mr2 |= MR2_AUTOCTS;
3783 mr1 |= MR1_AUTORTS;
3784 }
3785
3786/*
3787 * All sc26198 register values calculated so go through and set
3788 * them all up.
3789 */
3790
3791 pr_debug("SETPORT: portnr=%d panelnr=%d brdnr=%d\n",
3792 portp->portnr, portp->panelnr, portp->brdnr);
3793 pr_debug(" mr0=%x mr1=%x mr2=%x clk=%x\n", mr0, mr1, mr2, clk);
3794 pr_debug(" iopr=%x imron=%x imroff=%x\n", iopr, imron, imroff);
3795 pr_debug(" schr1=%x schr2=%x schr3=%x schr4=%x\n",
3796 tiosp->c_cc[VSTART], tiosp->c_cc[VSTOP],
3797 tiosp->c_cc[VSTART], tiosp->c_cc[VSTOP]);
3798
3799 spin_lock_irqsave(&brd_lock, flags);
3800 BRDENABLE(portp->brdnr, portp->pagenr);
3801 stl_sc26198setreg(portp, IMR, 0);
3802 stl_sc26198updatereg(portp, MR0, mr0);
3803 stl_sc26198updatereg(portp, MR1, mr1);
3804 stl_sc26198setreg(portp, SCCR, CR_RXERRBLOCK);
3805 stl_sc26198updatereg(portp, MR2, mr2);
3806 stl_sc26198updatereg(portp, IOPIOR,
3807 ((stl_sc26198getreg(portp, IOPIOR) & ~IPR_CHANGEMASK) | iopr));
3808
3809 if (baudrate > 0) {
3810 stl_sc26198setreg(portp, TXCSR, clk);
3811 stl_sc26198setreg(portp, RXCSR, clk);
3812 }
3813
3814 stl_sc26198setreg(portp, XONCR, tiosp->c_cc[VSTART]);
3815 stl_sc26198setreg(portp, XOFFCR, tiosp->c_cc[VSTOP]);
3816
3817 ipr = stl_sc26198getreg(portp, IPR);
3818 if (ipr & IPR_DCD)
3819 portp->sigs &= ~TIOCM_CD;
3820 else
3821 portp->sigs |= TIOCM_CD;
3822
3823 portp->imr = (portp->imr & ~imroff) | imron;
3824 stl_sc26198setreg(portp, IMR, portp->imr);
3825 BRDDISABLE(portp->brdnr);
3826 spin_unlock_irqrestore(&brd_lock, flags);
3827}
3828
3829/*****************************************************************************/
3830
3831/*
3832 * Set the state of the DTR and RTS signals.
3833 */
3834
3835static void stl_sc26198setsignals(struct stlport *portp, int dtr, int rts)
3836{
3837 unsigned char iopioron, iopioroff;
3838 unsigned long flags;
3839
3840 pr_debug("stl_sc26198setsignals(portp=%p,dtr=%d,rts=%d)\n", portp,
3841 dtr, rts);
3842
3843 iopioron = 0;
3844 iopioroff = 0;
3845 if (dtr == 0)
3846 iopioroff |= IPR_DTR;
3847 else if (dtr > 0)
3848 iopioron |= IPR_DTR;
3849 if (rts == 0)
3850 iopioroff |= IPR_RTS;
3851 else if (rts > 0)
3852 iopioron |= IPR_RTS;
3853
3854 spin_lock_irqsave(&brd_lock, flags);
3855 BRDENABLE(portp->brdnr, portp->pagenr);
3856 stl_sc26198setreg(portp, IOPIOR,
3857 ((stl_sc26198getreg(portp, IOPIOR) & ~iopioroff) | iopioron));
3858 BRDDISABLE(portp->brdnr);
3859 spin_unlock_irqrestore(&brd_lock, flags);
3860}
3861
3862/*****************************************************************************/
3863
3864/*
3865 * Return the state of the signals.
3866 */
3867
3868static int stl_sc26198getsignals(struct stlport *portp)
3869{
3870 unsigned char ipr;
3871 unsigned long flags;
3872 int sigs;
3873
3874 pr_debug("stl_sc26198getsignals(portp=%p)\n", portp);
3875
3876 spin_lock_irqsave(&brd_lock, flags);
3877 BRDENABLE(portp->brdnr, portp->pagenr);
3878 ipr = stl_sc26198getreg(portp, IPR);
3879 BRDDISABLE(portp->brdnr);
3880 spin_unlock_irqrestore(&brd_lock, flags);
3881
3882 sigs = 0;
3883 sigs |= (ipr & IPR_DCD) ? 0 : TIOCM_CD;
3884 sigs |= (ipr & IPR_CTS) ? 0 : TIOCM_CTS;
3885 sigs |= (ipr & IPR_DTR) ? 0: TIOCM_DTR;
3886 sigs |= (ipr & IPR_RTS) ? 0: TIOCM_RTS;
3887 sigs |= TIOCM_DSR;
3888 return sigs;
3889}
3890
3891/*****************************************************************************/
3892
3893/*
3894 * Enable/Disable the Transmitter and/or Receiver.
3895 */
3896
3897static void stl_sc26198enablerxtx(struct stlport *portp, int rx, int tx)
3898{
3899 unsigned char ccr;
3900 unsigned long flags;
3901
3902 pr_debug("stl_sc26198enablerxtx(portp=%p,rx=%d,tx=%d)\n", portp, rx,tx);
3903
3904 ccr = portp->crenable;
3905 if (tx == 0)
3906 ccr &= ~CR_TXENABLE;
3907 else if (tx > 0)
3908 ccr |= CR_TXENABLE;
3909 if (rx == 0)
3910 ccr &= ~CR_RXENABLE;
3911 else if (rx > 0)
3912 ccr |= CR_RXENABLE;
3913
3914 spin_lock_irqsave(&brd_lock, flags);
3915 BRDENABLE(portp->brdnr, portp->pagenr);
3916 stl_sc26198setreg(portp, SCCR, ccr);
3917 BRDDISABLE(portp->brdnr);
3918 portp->crenable = ccr;
3919 spin_unlock_irqrestore(&brd_lock, flags);
3920}
3921
3922/*****************************************************************************/
3923
3924/*
3925 * Start/stop the Transmitter and/or Receiver.
3926 */
3927
3928static void stl_sc26198startrxtx(struct stlport *portp, int rx, int tx)
3929{
3930 unsigned char imr;
3931 unsigned long flags;
3932
3933 pr_debug("stl_sc26198startrxtx(portp=%p,rx=%d,tx=%d)\n", portp, rx, tx);
3934
3935 imr = portp->imr;
3936 if (tx == 0)
3937 imr &= ~IR_TXRDY;
3938 else if (tx == 1)
3939 imr |= IR_TXRDY;
3940 if (rx == 0)
3941 imr &= ~(IR_RXRDY | IR_RXBREAK | IR_RXWATCHDOG);
3942 else if (rx > 0)
3943 imr |= IR_RXRDY | IR_RXBREAK | IR_RXWATCHDOG;
3944
3945 spin_lock_irqsave(&brd_lock, flags);
3946 BRDENABLE(portp->brdnr, portp->pagenr);
3947 stl_sc26198setreg(portp, IMR, imr);
3948 BRDDISABLE(portp->brdnr);
3949 portp->imr = imr;
3950 if (tx > 0)
3951 set_bit(ASYI_TXBUSY, &portp->istate);
3952 spin_unlock_irqrestore(&brd_lock, flags);
3953}
3954
3955/*****************************************************************************/
3956
3957/*
3958 * Disable all interrupts from this port.
3959 */
3960
3961static void stl_sc26198disableintrs(struct stlport *portp)
3962{
3963 unsigned long flags;
3964
3965 pr_debug("stl_sc26198disableintrs(portp=%p)\n", portp);
3966
3967 spin_lock_irqsave(&brd_lock, flags);
3968 BRDENABLE(portp->brdnr, portp->pagenr);
3969 portp->imr = 0;
3970 stl_sc26198setreg(portp, IMR, 0);
3971 BRDDISABLE(portp->brdnr);
3972 spin_unlock_irqrestore(&brd_lock, flags);
3973}
3974
3975/*****************************************************************************/
3976
3977static void stl_sc26198sendbreak(struct stlport *portp, int len)
3978{
3979 unsigned long flags;
3980
3981 pr_debug("stl_sc26198sendbreak(portp=%p,len=%d)\n", portp, len);
3982
3983 spin_lock_irqsave(&brd_lock, flags);
3984 BRDENABLE(portp->brdnr, portp->pagenr);
3985 if (len == 1) {
3986 stl_sc26198setreg(portp, SCCR, CR_TXSTARTBREAK);
3987 portp->stats.txbreaks++;
3988 } else
3989 stl_sc26198setreg(portp, SCCR, CR_TXSTOPBREAK);
3990
3991 BRDDISABLE(portp->brdnr);
3992 spin_unlock_irqrestore(&brd_lock, flags);
3993}
3994
3995/*****************************************************************************/
3996
3997/*
3998 * Take flow control actions...
3999 */
4000
4001static void stl_sc26198flowctrl(struct stlport *portp, int state)
4002{
4003 struct tty_struct *tty;
4004 unsigned long flags;
4005 unsigned char mr0;
4006
4007 pr_debug("stl_sc26198flowctrl(portp=%p,state=%x)\n", portp, state);
4008
4009 if (portp == NULL)
4010 return;
4011 tty = tty_port_tty_get(&portp->port);
4012 if (tty == NULL)
4013 return;
4014
4015 spin_lock_irqsave(&brd_lock, flags);
4016 BRDENABLE(portp->brdnr, portp->pagenr);
4017
4018 if (state) {
4019 if (tty->termios->c_iflag & IXOFF) {
4020 mr0 = stl_sc26198getreg(portp, MR0);
4021 stl_sc26198setreg(portp, MR0, (mr0 & ~MR0_SWFRXTX));
4022 stl_sc26198setreg(portp, SCCR, CR_TXSENDXON);
4023 mr0 |= MR0_SWFRX;
4024 portp->stats.rxxon++;
4025 stl_sc26198wait(portp);
4026 stl_sc26198setreg(portp, MR0, mr0);
4027 }
4028/*
4029 * Question: should we return RTS to what it was before? It may
4030 * have been set by an ioctl... Suppose not, since if you have
4031 * hardware flow control set then it is pretty silly to go and
4032 * set the RTS line by hand.
4033 */
4034 if (tty->termios->c_cflag & CRTSCTS) {
4035 stl_sc26198setreg(portp, MR1,
4036 (stl_sc26198getreg(portp, MR1) | MR1_AUTORTS));
4037 stl_sc26198setreg(portp, IOPIOR,
4038 (stl_sc26198getreg(portp, IOPIOR) | IOPR_RTS));
4039 portp->stats.rxrtson++;
4040 }
4041 } else {
4042 if (tty->termios->c_iflag & IXOFF) {
4043 mr0 = stl_sc26198getreg(portp, MR0);
4044 stl_sc26198setreg(portp, MR0, (mr0 & ~MR0_SWFRXTX));
4045 stl_sc26198setreg(portp, SCCR, CR_TXSENDXOFF);
4046 mr0 &= ~MR0_SWFRX;
4047 portp->stats.rxxoff++;
4048 stl_sc26198wait(portp);
4049 stl_sc26198setreg(portp, MR0, mr0);
4050 }
4051 if (tty->termios->c_cflag & CRTSCTS) {
4052 stl_sc26198setreg(portp, MR1,
4053 (stl_sc26198getreg(portp, MR1) & ~MR1_AUTORTS));
4054 stl_sc26198setreg(portp, IOPIOR,
4055 (stl_sc26198getreg(portp, IOPIOR) & ~IOPR_RTS));
4056 portp->stats.rxrtsoff++;
4057 }
4058 }
4059
4060 BRDDISABLE(portp->brdnr);
4061 spin_unlock_irqrestore(&brd_lock, flags);
4062 tty_kref_put(tty);
4063}
4064
4065/*****************************************************************************/
4066
4067/*
4068 * Send a flow control character.
4069 */
4070
4071static void stl_sc26198sendflow(struct stlport *portp, int state)
4072{
4073 struct tty_struct *tty;
4074 unsigned long flags;
4075 unsigned char mr0;
4076
4077 pr_debug("stl_sc26198sendflow(portp=%p,state=%x)\n", portp, state);
4078
4079 if (portp == NULL)
4080 return;
4081 tty = tty_port_tty_get(&portp->port);
4082 if (tty == NULL)
4083 return;
4084
4085 spin_lock_irqsave(&brd_lock, flags);
4086 BRDENABLE(portp->brdnr, portp->pagenr);
4087 if (state) {
4088 mr0 = stl_sc26198getreg(portp, MR0);
4089 stl_sc26198setreg(portp, MR0, (mr0 & ~MR0_SWFRXTX));
4090 stl_sc26198setreg(portp, SCCR, CR_TXSENDXON);
4091 mr0 |= MR0_SWFRX;
4092 portp->stats.rxxon++;
4093 stl_sc26198wait(portp);
4094 stl_sc26198setreg(portp, MR0, mr0);
4095 } else {
4096 mr0 = stl_sc26198getreg(portp, MR0);
4097 stl_sc26198setreg(portp, MR0, (mr0 & ~MR0_SWFRXTX));
4098 stl_sc26198setreg(portp, SCCR, CR_TXSENDXOFF);
4099 mr0 &= ~MR0_SWFRX;
4100 portp->stats.rxxoff++;
4101 stl_sc26198wait(portp);
4102 stl_sc26198setreg(portp, MR0, mr0);
4103 }
4104 BRDDISABLE(portp->brdnr);
4105 spin_unlock_irqrestore(&brd_lock, flags);
4106 tty_kref_put(tty);
4107}
4108
4109/*****************************************************************************/
4110
4111static void stl_sc26198flush(struct stlport *portp)
4112{
4113 unsigned long flags;
4114
4115 pr_debug("stl_sc26198flush(portp=%p)\n", portp);
4116
4117 if (portp == NULL)
4118 return;
4119
4120 spin_lock_irqsave(&brd_lock, flags);
4121 BRDENABLE(portp->brdnr, portp->pagenr);
4122 stl_sc26198setreg(portp, SCCR, CR_TXRESET);
4123 stl_sc26198setreg(portp, SCCR, portp->crenable);
4124 BRDDISABLE(portp->brdnr);
4125 portp->tx.tail = portp->tx.head;
4126 spin_unlock_irqrestore(&brd_lock, flags);
4127}
4128
4129/*****************************************************************************/
4130
4131/*
4132 * Return the current state of data flow on this port. This is only
4133 * really interresting when determining if data has fully completed
4134 * transmission or not... The sc26198 interrupt scheme cannot
4135 * determine when all data has actually drained, so we need to
4136 * check the port statusy register to be sure.
4137 */
4138
4139static int stl_sc26198datastate(struct stlport *portp)
4140{
4141 unsigned long flags;
4142 unsigned char sr;
4143
4144 pr_debug("stl_sc26198datastate(portp=%p)\n", portp);
4145
4146 if (portp == NULL)
4147 return 0;
4148 if (test_bit(ASYI_TXBUSY, &portp->istate))
4149 return 1;
4150
4151 spin_lock_irqsave(&brd_lock, flags);
4152 BRDENABLE(portp->brdnr, portp->pagenr);
4153 sr = stl_sc26198getreg(portp, SR);
4154 BRDDISABLE(portp->brdnr);
4155 spin_unlock_irqrestore(&brd_lock, flags);
4156
4157 return (sr & SR_TXEMPTY) ? 0 : 1;
4158}
4159
4160/*****************************************************************************/
4161
4162/*
4163 * Delay for a small amount of time, to give the sc26198 a chance
4164 * to process a command...
4165 */
4166
4167static void stl_sc26198wait(struct stlport *portp)
4168{
4169 int i;
4170
4171 pr_debug("stl_sc26198wait(portp=%p)\n", portp);
4172
4173 if (portp == NULL)
4174 return;
4175
4176 for (i = 0; i < 20; i++)
4177 stl_sc26198getglobreg(portp, TSTR);
4178}
4179
4180/*****************************************************************************/
4181
4182/*
4183 * If we are TX flow controlled and in IXANY mode then we may
4184 * need to unflow control here. We gotta do this because of the
4185 * automatic flow control modes of the sc26198.
4186 */
4187
4188static void stl_sc26198txunflow(struct stlport *portp, struct tty_struct *tty)
4189{
4190 unsigned char mr0;
4191
4192 mr0 = stl_sc26198getreg(portp, MR0);
4193 stl_sc26198setreg(portp, MR0, (mr0 & ~MR0_SWFRXTX));
4194 stl_sc26198setreg(portp, SCCR, CR_HOSTXON);
4195 stl_sc26198wait(portp);
4196 stl_sc26198setreg(portp, MR0, mr0);
4197 clear_bit(ASYI_TXFLOWED, &portp->istate);
4198}
4199
4200/*****************************************************************************/
4201
4202/*
4203 * Interrupt service routine for sc26198 panels.
4204 */
4205
4206static void stl_sc26198intr(struct stlpanel *panelp, unsigned int iobase)
4207{
4208 struct stlport *portp;
4209 unsigned int iack;
4210
4211 spin_lock(&brd_lock);
4212
4213/*
4214 * Work around bug in sc26198 chip... Cannot have A6 address
4215 * line of UART high, else iack will be returned as 0.
4216 */
4217 outb(0, (iobase + 1));
4218
4219 iack = inb(iobase + XP_IACK);
4220 portp = panelp->ports[(iack & IVR_CHANMASK) + ((iobase & 0x4) << 1)];
4221
4222 if (iack & IVR_RXDATA)
4223 stl_sc26198rxisr(portp, iack);
4224 else if (iack & IVR_TXDATA)
4225 stl_sc26198txisr(portp);
4226 else
4227 stl_sc26198otherisr(portp, iack);
4228
4229 spin_unlock(&brd_lock);
4230}
4231
4232/*****************************************************************************/
4233
4234/*
4235 * Transmit interrupt handler. This has gotta be fast! Handling TX
4236 * chars is pretty simple, stuff as many as possible from the TX buffer
4237 * into the sc26198 FIFO.
4238 * In practice it is possible that interrupts are enabled but that the
4239 * port has been hung up. Need to handle not having any TX buffer here,
4240 * this is done by using the side effect that head and tail will also
4241 * be NULL if the buffer has been freed.
4242 */
4243
4244static void stl_sc26198txisr(struct stlport *portp)
4245{
4246 struct tty_struct *tty;
4247 unsigned int ioaddr;
4248 unsigned char mr0;
4249 int len, stlen;
4250 char *head, *tail;
4251
4252 pr_debug("stl_sc26198txisr(portp=%p)\n", portp);
4253
4254 ioaddr = portp->ioaddr;
4255 head = portp->tx.head;
4256 tail = portp->tx.tail;
4257 len = (head >= tail) ? (head - tail) : (STL_TXBUFSIZE - (tail - head));
4258 if ((len == 0) || ((len < STL_TXBUFLOW) &&
4259 (test_bit(ASYI_TXLOW, &portp->istate) == 0))) {
4260 set_bit(ASYI_TXLOW, &portp->istate);
4261 tty = tty_port_tty_get(&portp->port);
4262 if (tty) {
4263 tty_wakeup(tty);
4264 tty_kref_put(tty);
4265 }
4266 }
4267
4268 if (len == 0) {
4269 outb((MR0 | portp->uartaddr), (ioaddr + XP_ADDR));
4270 mr0 = inb(ioaddr + XP_DATA);
4271 if ((mr0 & MR0_TXMASK) == MR0_TXEMPTY) {
4272 portp->imr &= ~IR_TXRDY;
4273 outb((IMR | portp->uartaddr), (ioaddr + XP_ADDR));
4274 outb(portp->imr, (ioaddr + XP_DATA));
4275 clear_bit(ASYI_TXBUSY, &portp->istate);
4276 } else {
4277 mr0 |= ((mr0 & ~MR0_TXMASK) | MR0_TXEMPTY);
4278 outb(mr0, (ioaddr + XP_DATA));
4279 }
4280 } else {
4281 len = min(len, SC26198_TXFIFOSIZE);
4282 portp->stats.txtotal += len;
4283 stlen = min_t(unsigned int, len,
4284 (portp->tx.buf + STL_TXBUFSIZE) - tail);
4285 outb(GTXFIFO, (ioaddr + XP_ADDR));
4286 outsb((ioaddr + XP_DATA), tail, stlen);
4287 len -= stlen;
4288 tail += stlen;
4289 if (tail >= (portp->tx.buf + STL_TXBUFSIZE))
4290 tail = portp->tx.buf;
4291 if (len > 0) {
4292 outsb((ioaddr + XP_DATA), tail, len);
4293 tail += len;
4294 }
4295 portp->tx.tail = tail;
4296 }
4297}
4298
4299/*****************************************************************************/
4300
4301/*
4302 * Receive character interrupt handler. Determine if we have good chars
4303 * or bad chars and then process appropriately. Good chars are easy
4304 * just shove the lot into the RX buffer and set all status byte to 0.
4305 * If a bad RX char then process as required. This routine needs to be
4306 * fast! In practice it is possible that we get an interrupt on a port
4307 * that is closed. This can happen on hangups - since they completely
4308 * shutdown a port not in user context. Need to handle this case.
4309 */
4310
4311static void stl_sc26198rxisr(struct stlport *portp, unsigned int iack)
4312{
4313 struct tty_struct *tty;
4314 unsigned int len, buflen, ioaddr;
4315
4316 pr_debug("stl_sc26198rxisr(portp=%p,iack=%x)\n", portp, iack);
4317
4318 tty = tty_port_tty_get(&portp->port);
4319 ioaddr = portp->ioaddr;
4320 outb(GIBCR, (ioaddr + XP_ADDR));
4321 len = inb(ioaddr + XP_DATA) + 1;
4322
4323 if ((iack & IVR_TYPEMASK) == IVR_RXDATA) {
4324 if (tty == NULL || (buflen = tty_buffer_request_room(tty, len)) == 0) {
4325 len = min_t(unsigned int, len, sizeof(stl_unwanted));
4326 outb(GRXFIFO, (ioaddr + XP_ADDR));
4327 insb((ioaddr + XP_DATA), &stl_unwanted[0], len);
4328 portp->stats.rxlost += len;
4329 portp->stats.rxtotal += len;
4330 } else {
4331 len = min(len, buflen);
4332 if (len > 0) {
4333 unsigned char *ptr;
4334 outb(GRXFIFO, (ioaddr + XP_ADDR));
4335 tty_prepare_flip_string(tty, &ptr, len);
4336 insb((ioaddr + XP_DATA), ptr, len);
4337 tty_schedule_flip(tty);
4338 portp->stats.rxtotal += len;
4339 }
4340 }
4341 } else {
4342 stl_sc26198rxbadchars(portp);
4343 }
4344
4345/*
4346 * If we are TX flow controlled and in IXANY mode then we may need
4347 * to unflow control here. We gotta do this because of the automatic
4348 * flow control modes of the sc26198.
4349 */
4350 if (test_bit(ASYI_TXFLOWED, &portp->istate)) {
4351 if ((tty != NULL) &&
4352 (tty->termios != NULL) &&
4353 (tty->termios->c_iflag & IXANY)) {
4354 stl_sc26198txunflow(portp, tty);
4355 }
4356 }
4357 tty_kref_put(tty);
4358}
4359
4360/*****************************************************************************/
4361
4362/*
4363 * Process an RX bad character.
4364 */
4365
4366static void stl_sc26198rxbadch(struct stlport *portp, unsigned char status, char ch)
4367{
4368 struct tty_struct *tty;
4369 unsigned int ioaddr;
4370
4371 tty = tty_port_tty_get(&portp->port);
4372 ioaddr = portp->ioaddr;
4373
4374 if (status & SR_RXPARITY)
4375 portp->stats.rxparity++;
4376 if (status & SR_RXFRAMING)
4377 portp->stats.rxframing++;
4378 if (status & SR_RXOVERRUN)
4379 portp->stats.rxoverrun++;
4380 if (status & SR_RXBREAK)
4381 portp->stats.rxbreaks++;
4382
4383 if ((tty != NULL) &&
4384 ((portp->rxignoremsk & status) == 0)) {
4385 if (portp->rxmarkmsk & status) {
4386 if (status & SR_RXBREAK) {
4387 status = TTY_BREAK;
4388 if (portp->port.flags & ASYNC_SAK) {
4389 do_SAK(tty);
4390 BRDENABLE(portp->brdnr, portp->pagenr);
4391 }
4392 } else if (status & SR_RXPARITY)
4393 status = TTY_PARITY;
4394 else if (status & SR_RXFRAMING)
4395 status = TTY_FRAME;
4396 else if(status & SR_RXOVERRUN)
4397 status = TTY_OVERRUN;
4398 else
4399 status = 0;
4400 } else
4401 status = 0;
4402
4403 tty_insert_flip_char(tty, ch, status);
4404 tty_schedule_flip(tty);
4405
4406 if (status == 0)
4407 portp->stats.rxtotal++;
4408 }
4409 tty_kref_put(tty);
4410}
4411
4412/*****************************************************************************/
4413
4414/*
4415 * Process all characters in the RX FIFO of the UART. Check all char
4416 * status bytes as well, and process as required. We need to check
4417 * all bytes in the FIFO, in case some more enter the FIFO while we
4418 * are here. To get the exact character error type we need to switch
4419 * into CHAR error mode (that is why we need to make sure we empty
4420 * the FIFO).
4421 */
4422
4423static void stl_sc26198rxbadchars(struct stlport *portp)
4424{
4425 unsigned char status, mr1;
4426 char ch;
4427
4428/*
4429 * To get the precise error type for each character we must switch
4430 * back into CHAR error mode.
4431 */
4432 mr1 = stl_sc26198getreg(portp, MR1);
4433 stl_sc26198setreg(portp, MR1, (mr1 & ~MR1_ERRBLOCK));
4434
4435 while ((status = stl_sc26198getreg(portp, SR)) & SR_RXRDY) {
4436 stl_sc26198setreg(portp, SCCR, CR_CLEARRXERR);
4437 ch = stl_sc26198getreg(portp, RXFIFO);
4438 stl_sc26198rxbadch(portp, status, ch);
4439 }
4440
4441/*
4442 * To get correct interrupt class we must switch back into BLOCK
4443 * error mode.
4444 */
4445 stl_sc26198setreg(portp, MR1, mr1);
4446}
4447
4448/*****************************************************************************/
4449
4450/*
4451 * Other interrupt handler. This includes modem signals, flow
4452 * control actions, etc. Most stuff is left to off-level interrupt
4453 * processing time.
4454 */
4455
4456static void stl_sc26198otherisr(struct stlport *portp, unsigned int iack)
4457{
4458 unsigned char cir, ipr, xisr;
4459
4460 pr_debug("stl_sc26198otherisr(portp=%p,iack=%x)\n", portp, iack);
4461
4462 cir = stl_sc26198getglobreg(portp, CIR);
4463
4464 switch (cir & CIR_SUBTYPEMASK) {
4465 case CIR_SUBCOS:
4466 ipr = stl_sc26198getreg(portp, IPR);
4467 if (ipr & IPR_DCDCHANGE) {
4468 stl_cd_change(portp);
4469 portp->stats.modem++;
4470 }
4471 break;
4472 case CIR_SUBXONXOFF:
4473 xisr = stl_sc26198getreg(portp, XISR);
4474 if (xisr & XISR_RXXONGOT) {
4475 set_bit(ASYI_TXFLOWED, &portp->istate);
4476 portp->stats.txxoff++;
4477 }
4478 if (xisr & XISR_RXXOFFGOT) {
4479 clear_bit(ASYI_TXFLOWED, &portp->istate);
4480 portp->stats.txxon++;
4481 }
4482 break;
4483 case CIR_SUBBREAK:
4484 stl_sc26198setreg(portp, SCCR, CR_BREAKRESET);
4485 stl_sc26198rxbadchars(portp);
4486 break;
4487 default:
4488 break;
4489 }
4490}
4491
4492static void stl_free_isabrds(void)
4493{
4494 struct stlbrd *brdp;
4495 unsigned int i;
4496
4497 for (i = 0; i < stl_nrbrds; i++) {
4498 if ((brdp = stl_brds[i]) == NULL || (brdp->state & STL_PROBED))
4499 continue;
4500
4501 free_irq(brdp->irq, brdp);
4502
4503 stl_cleanup_panels(brdp);
4504
4505 release_region(brdp->ioaddr1, brdp->iosize1);
4506 if (brdp->iosize2 > 0)
4507 release_region(brdp->ioaddr2, brdp->iosize2);
4508
4509 kfree(brdp);
4510 stl_brds[i] = NULL;
4511 }
4512}
4513
4514/*
4515 * Loadable module initialization stuff.
4516 */
4517static int __init stallion_module_init(void)
4518{
4519 struct stlbrd *brdp;
4520 struct stlconf conf;
4521 unsigned int i, j;
4522 int retval;
4523
4524 printk(KERN_INFO "%s: version %s\n", stl_drvtitle, stl_drvversion);
4525
4526 spin_lock_init(&stallion_lock);
4527 spin_lock_init(&brd_lock);
4528
4529 stl_serial = alloc_tty_driver(STL_MAXBRDS * STL_MAXPORTS);
4530 if (!stl_serial) {
4531 retval = -ENOMEM;
4532 goto err;
4533 }
4534
4535 stl_serial->owner = THIS_MODULE;
4536 stl_serial->driver_name = stl_drvname;
4537 stl_serial->name = "ttyE";
4538 stl_serial->major = STL_SERIALMAJOR;
4539 stl_serial->minor_start = 0;
4540 stl_serial->type = TTY_DRIVER_TYPE_SERIAL;
4541 stl_serial->subtype = SERIAL_TYPE_NORMAL;
4542 stl_serial->init_termios = stl_deftermios;
4543 stl_serial->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
4544 tty_set_operations(stl_serial, &stl_ops);
4545
4546 retval = tty_register_driver(stl_serial);
4547 if (retval) {
4548 printk("STALLION: failed to register serial driver\n");
4549 goto err_frtty;
4550 }
4551
4552/*
4553 * Find any dynamically supported boards. That is via module load
4554 * line options.
4555 */
4556 for (i = stl_nrbrds; i < stl_nargs; i++) {
4557 memset(&conf, 0, sizeof(conf));
4558 if (stl_parsebrd(&conf, stl_brdsp[i]) == 0)
4559 continue;
4560 if ((brdp = stl_allocbrd()) == NULL)
4561 continue;
4562 brdp->brdnr = i;
4563 brdp->brdtype = conf.brdtype;
4564 brdp->ioaddr1 = conf.ioaddr1;
4565 brdp->ioaddr2 = conf.ioaddr2;
4566 brdp->irq = conf.irq;
4567 brdp->irqtype = conf.irqtype;
4568 stl_brds[brdp->brdnr] = brdp;
4569 if (stl_brdinit(brdp)) {
4570 stl_brds[brdp->brdnr] = NULL;
4571 kfree(brdp);
4572 } else {
4573 for (j = 0; j < brdp->nrports; j++)
4574 tty_register_device(stl_serial,
4575 brdp->brdnr * STL_MAXPORTS + j, NULL);
4576 stl_nrbrds = i + 1;
4577 }
4578 }
4579
4580 /* this has to be _after_ isa finding because of locking */
4581 retval = pci_register_driver(&stl_pcidriver);
4582 if (retval && stl_nrbrds == 0) {
4583 printk(KERN_ERR "STALLION: can't register pci driver\n");
4584 goto err_unrtty;
4585 }
4586
4587/*
4588 * Set up a character driver for per board stuff. This is mainly used
4589 * to do stats ioctls on the ports.
4590 */
4591 if (register_chrdev(STL_SIOMEMMAJOR, "staliomem", &stl_fsiomem))
4592 printk("STALLION: failed to register serial board device\n");
4593
4594 stallion_class = class_create(THIS_MODULE, "staliomem");
4595 if (IS_ERR(stallion_class))
4596 printk("STALLION: failed to create class\n");
4597 for (i = 0; i < 4; i++)
4598 device_create(stallion_class, NULL, MKDEV(STL_SIOMEMMAJOR, i),
4599 NULL, "staliomem%d", i);
4600
4601 return 0;
4602err_unrtty:
4603 tty_unregister_driver(stl_serial);
4604err_frtty:
4605 put_tty_driver(stl_serial);
4606err:
4607 return retval;
4608}
4609
4610static void __exit stallion_module_exit(void)
4611{
4612 struct stlbrd *brdp;
4613 unsigned int i, j;
4614
4615 pr_debug("cleanup_module()\n");
4616
4617 printk(KERN_INFO "Unloading %s: version %s\n", stl_drvtitle,
4618 stl_drvversion);
4619
4620/*
4621 * Free up all allocated resources used by the ports. This includes
4622 * memory and interrupts. As part of this process we will also do
4623 * a hangup on every open port - to try to flush out any processes
4624 * hanging onto ports.
4625 */
4626 for (i = 0; i < stl_nrbrds; i++) {
4627 if ((brdp = stl_brds[i]) == NULL || (brdp->state & STL_PROBED))
4628 continue;
4629 for (j = 0; j < brdp->nrports; j++)
4630 tty_unregister_device(stl_serial,
4631 brdp->brdnr * STL_MAXPORTS + j);
4632 }
4633
4634 for (i = 0; i < 4; i++)
4635 device_destroy(stallion_class, MKDEV(STL_SIOMEMMAJOR, i));
4636 unregister_chrdev(STL_SIOMEMMAJOR, "staliomem");
4637 class_destroy(stallion_class);
4638
4639 pci_unregister_driver(&stl_pcidriver);
4640
4641 stl_free_isabrds();
4642
4643 tty_unregister_driver(stl_serial);
4644 put_tty_driver(stl_serial);
4645}
4646
4647module_init(stallion_module_init);
4648module_exit(stallion_module_exit);
4649
4650MODULE_AUTHOR("Greg Ungerer");
4651MODULE_DESCRIPTION("Stallion Multiport Serial Driver");
4652MODULE_LICENSE("GPL");
diff --git a/drivers/char/sx.c b/drivers/char/sx.c
deleted file mode 100644
index 5b24db4ff7f1..000000000000
--- a/drivers/char/sx.c
+++ /dev/null
@@ -1,2894 +0,0 @@
1/* sx.c -- driver for the Specialix SX series cards.
2 *
3 * This driver will also support the older SI, and XIO cards.
4 *
5 *
6 * (C) 1998 - 2004 R.E.Wolff@BitWizard.nl
7 *
8 * Simon Allen (simonallen@cix.compulink.co.uk) wrote a previous
9 * version of this driver. Some fragments may have been copied. (none
10 * yet :-)
11 *
12 * Specialix pays for the development and support of this driver.
13 * Please DO contact support@specialix.co.uk if you require
14 * support. But please read the documentation (sx.txt) first.
15 *
16 *
17 *
18 * This program is free software; you can redistribute it and/or
19 * modify it under the terms of the GNU General Public License as
20 * published by the Free Software Foundation; either version 2 of
21 * the License, or (at your option) any later version.
22 *
23 * This program is distributed in the hope that it will be
24 * useful, but WITHOUT ANY WARRANTY; without even the implied
25 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
26 * PURPOSE. See the GNU General Public License for more details.
27 *
28 * You should have received a copy of the GNU General Public
29 * License along with this program; if not, write to the Free
30 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,
31 * USA.
32 *
33 * Revision history:
34 * Revision 1.33 2000/03/09 10:00:00 pvdl,wolff
35 * - Fixed module and port counting
36 * - Fixed signal handling
37 * - Fixed an Ooops
38 *
39 * Revision 1.32 2000/03/07 09:00:00 wolff,pvdl
40 * - Fixed some sx_dprintk typos
41 * - added detection for an invalid board/module configuration
42 *
43 * Revision 1.31 2000/03/06 12:00:00 wolff,pvdl
44 * - Added support for EISA
45 *
46 * Revision 1.30 2000/01/21 17:43:06 wolff
47 * - Added support for SX+
48 *
49 * Revision 1.26 1999/08/05 15:22:14 wolff
50 * - Port to 2.3.x
51 * - Reformatted to Linus' liking.
52 *
53 * Revision 1.25 1999/07/30 14:24:08 wolff
54 * Had accidentally left "gs_debug" set to "-1" instead of "off" (=0).
55 *
56 * Revision 1.24 1999/07/28 09:41:52 wolff
57 * - I noticed the remark about use-count straying in sx.txt. I checked
58 * sx_open, and found a few places where that could happen. I hope it's
59 * fixed now.
60 *
61 * Revision 1.23 1999/07/28 08:56:06 wolff
62 * - Fixed crash when sx_firmware run twice.
63 * - Added sx_slowpoll as a module parameter (I guess nobody really wanted
64 * to change it from the default... )
65 * - Fixed a stupid editing problem I introduced in 1.22.
66 * - Fixed dropping characters on a termios change.
67 *
68 * Revision 1.22 1999/07/26 21:01:43 wolff
69 * Russell Brown noticed that I had overlooked 4 out of six modem control
70 * signals in sx_getsignals. Ooops.
71 *
72 * Revision 1.21 1999/07/23 09:11:33 wolff
73 * I forgot to free dynamically allocated memory when the driver is unloaded.
74 *
75 * Revision 1.20 1999/07/20 06:25:26 wolff
76 * The "closing wait" wasn't honoured. Thanks to James Griffiths for
77 * reporting this.
78 *
79 * Revision 1.19 1999/07/11 08:59:59 wolff
80 * Fixed an oops in close, when an open was pending. Changed the memtest
81 * a bit. Should also test the board in word-mode, however my card fails the
82 * memtest then. I still have to figure out what is wrong...
83 *
84 * Revision 1.18 1999/06/10 09:38:42 wolff
85 * Changed the format of the firmware revision from %04x to %x.%02x .
86 *
87 * Revision 1.17 1999/06/04 09:44:35 wolff
88 * fixed problem: reference to pci stuff when config_pci was off...
89 * Thanks to Jorge Novo for noticing this.
90 *
91 * Revision 1.16 1999/06/02 08:30:15 wolff
92 * added/removed the workaround for the DCD bug in the Firmware.
93 * A bit more debugging code to locate that...
94 *
95 * Revision 1.15 1999/06/01 11:35:30 wolff
96 * when DCD is left low (floating?), on TA's the firmware first tells us
97 * that DCD is high, but after a short while suddenly comes to the
98 * conclusion that it is low. All this would be fine, if it weren't that
99 * Unix requires us to send a "hangup" signal in that case. This usually
100 * all happens BEFORE the program has had a chance to ioctl the device
101 * into clocal mode..
102 *
103 * Revision 1.14 1999/05/25 11:18:59 wolff
104 * Added PCI-fix.
105 * Added checks for return code of sx_sendcommand.
106 * Don't issue "reconfig" if port isn't open yet. (bit us on TA modules...)
107 *
108 * Revision 1.13 1999/04/29 15:18:01 wolff
109 * Fixed an "oops" that showed on SuSE 6.0 systems.
110 * Activate DTR again after stty 0.
111 *
112 * Revision 1.12 1999/04/29 07:49:52 wolff
113 * Improved "stty 0" handling a bit. (used to change baud to 9600 assuming
114 * the connection would be dropped anyway. That is not always the case,
115 * and confuses people).
116 * Told the card to always monitor the modem signals.
117 * Added support for dynamic gs_debug adjustments.
118 * Now tells the rest of the system the number of ports.
119 *
120 * Revision 1.11 1999/04/24 11:11:30 wolff
121 * Fixed two stupid typos in the memory test.
122 *
123 * Revision 1.10 1999/04/24 10:53:39 wolff
124 * Added some of Christian's suggestions.
125 * Fixed an HW_COOK_IN bug (ISIG was not in I_OTHER. We used to trust the
126 * card to send the signal to the process.....)
127 *
128 * Revision 1.9 1999/04/23 07:26:38 wolff
129 * Included Christian Lademann's 2.0 compile-warning fixes and interrupt
130 * assignment redesign.
131 * Cleanup of some other stuff.
132 *
133 * Revision 1.8 1999/04/16 13:05:30 wolff
134 * fixed a DCD change unnoticed bug.
135 *
136 * Revision 1.7 1999/04/14 22:19:51 wolff
137 * Fixed typo that showed up in 2.0.x builds (get_user instead of Get_user!)
138 *
139 * Revision 1.6 1999/04/13 18:40:20 wolff
140 * changed misc-minor to 161, as assigned by HPA.
141 *
142 * Revision 1.5 1999/04/13 15:12:25 wolff
143 * Fixed use-count leak when "hangup" occurred.
144 * Added workaround for a stupid-PCIBIOS bug.
145 *
146 *
147 * Revision 1.4 1999/04/01 22:47:40 wolff
148 * Fixed < 1M linux-2.0 problem.
149 * (vremap isn't compatible with ioremap in that case)
150 *
151 * Revision 1.3 1999/03/31 13:45:45 wolff
152 * Firmware loading is now done through a separate IOCTL.
153 *
154 * Revision 1.2 1999/03/28 12:22:29 wolff
155 * rcs cleanup
156 *
157 * Revision 1.1 1999/03/28 12:10:34 wolff
158 * Readying for release on 2.0.x (sorry David, 1.01 becomes 1.1 for RCS).
159 *
160 * Revision 0.12 1999/03/28 09:20:10 wolff
161 * Fixed problem in 0.11, continueing cleanup.
162 *
163 * Revision 0.11 1999/03/28 08:46:44 wolff
164 * cleanup. Not good.
165 *
166 * Revision 0.10 1999/03/28 08:09:43 wolff
167 * Fixed loosing characters on close.
168 *
169 * Revision 0.9 1999/03/21 22:52:01 wolff
170 * Ported back to 2.2.... (minor things)
171 *
172 * Revision 0.8 1999/03/21 22:40:33 wolff
173 * Port to 2.0
174 *
175 * Revision 0.7 1999/03/21 19:06:34 wolff
176 * Fixed hangup processing.
177 *
178 * Revision 0.6 1999/02/05 08:45:14 wolff
179 * fixed real_raw problems. Inclusion into kernel imminent.
180 *
181 * Revision 0.5 1998/12/21 23:51:06 wolff
182 * Snatched a nasty bug: sx_transmit_chars was getting re-entered, and it
183 * shouldn't have. THATs why I want to have transmit interrupts even when
184 * the buffer is empty.
185 *
186 * Revision 0.4 1998/12/17 09:34:46 wolff
187 * PPP works. ioctl works. Basically works!
188 *
189 * Revision 0.3 1998/12/15 13:05:18 wolff
190 * It works! Wow! Gotta start implementing IOCTL and stuff....
191 *
192 * Revision 0.2 1998/12/01 08:33:53 wolff
193 * moved over to 2.1.130
194 *
195 * Revision 0.1 1998/11/03 21:23:51 wolff
196 * Initial revision. Detects SX card.
197 *
198 * */
199
200#define SX_VERSION 1.33
201
202#include <linux/module.h>
203#include <linux/kdev_t.h>
204#include <linux/kernel.h>
205#include <linux/sched.h>
206#include <linux/ioport.h>
207#include <linux/interrupt.h>
208#include <linux/errno.h>
209#include <linux/tty.h>
210#include <linux/tty_flip.h>
211#include <linux/mm.h>
212#include <linux/serial.h>
213#include <linux/fcntl.h>
214#include <linux/major.h>
215#include <linux/delay.h>
216#include <linux/eisa.h>
217#include <linux/pci.h>
218#include <linux/slab.h>
219#include <linux/smp_lock.h>
220#include <linux/init.h>
221#include <linux/miscdevice.h>
222#include <linux/bitops.h>
223
224#include <asm/io.h>
225#include <asm/uaccess.h>
226
227/* The 3.0.0 version of sxboards/sxwindow.h uses BYTE and WORD.... */
228#define BYTE u8
229#define WORD u16
230
231/* .... but the 3.0.4 version uses _u8 and _u16. */
232#define _u8 u8
233#define _u16 u16
234
235#include "sxboards.h"
236#include "sxwindow.h"
237
238#include <linux/generic_serial.h>
239#include "sx.h"
240
241/* I don't think that this driver can handle more than 256 ports on
242 one machine. You'll have to increase the number of boards in sx.h
243 if you want more than 4 boards. */
244
245#ifndef PCI_DEVICE_ID_SPECIALIX_SX_XIO_IO8
246#define PCI_DEVICE_ID_SPECIALIX_SX_XIO_IO8 0x2000
247#endif
248
249/* Configurable options:
250 (Don't be too sure that it'll work if you toggle them) */
251
252/* Am I paranoid or not ? ;-) */
253#undef SX_PARANOIA_CHECK
254
255/* 20 -> 2000 per second. The card should rate-limit interrupts at 100
256 Hz, but it is user configurable. I don't recommend going above 1000
257 Hz. The interrupt ratelimit might trigger if the interrupt is
258 shared with a very active other device. */
259#define IRQ_RATE_LIMIT 20
260
261/* Sharing interrupts is possible now. If the other device wants more
262 than 2000 interrupts per second, we'd gracefully decline further
263 interrupts. That's not what we want. On the other hand, if the
264 other device interrupts 2000 times a second, don't use the SX
265 interrupt. Use polling. */
266#undef IRQ_RATE_LIMIT
267
268#if 0
269/* Not implemented */
270/*
271 * The following defines are mostly for testing purposes. But if you need
272 * some nice reporting in your syslog, you can define them also.
273 */
274#define SX_REPORT_FIFO
275#define SX_REPORT_OVERRUN
276#endif
277
278/* Function prototypes */
279static void sx_disable_tx_interrupts(void *ptr);
280static void sx_enable_tx_interrupts(void *ptr);
281static void sx_disable_rx_interrupts(void *ptr);
282static void sx_enable_rx_interrupts(void *ptr);
283static int sx_carrier_raised(struct tty_port *port);
284static void sx_shutdown_port(void *ptr);
285static int sx_set_real_termios(void *ptr);
286static void sx_close(void *ptr);
287static int sx_chars_in_buffer(void *ptr);
288static int sx_init_board(struct sx_board *board);
289static int sx_init_portstructs(int nboards, int nports);
290static long sx_fw_ioctl(struct file *filp, unsigned int cmd,
291 unsigned long arg);
292static int sx_init_drivers(void);
293
294static struct tty_driver *sx_driver;
295
296static DEFINE_MUTEX(sx_boards_lock);
297static struct sx_board boards[SX_NBOARDS];
298static struct sx_port *sx_ports;
299static int sx_initialized;
300static int sx_nports;
301static int sx_debug;
302
303/* You can have the driver poll your card.
304 - Set sx_poll to 1 to poll every timer tick (10ms on Intel).
305 This is used when the card cannot use an interrupt for some reason.
306
307 - set sx_slowpoll to 100 to do an extra poll once a second (on Intel). If
308 the driver misses an interrupt (report this if it DOES happen to you!)
309 everything will continue to work....
310 */
311static int sx_poll = 1;
312static int sx_slowpoll;
313
314/* The card limits the number of interrupts per second.
315 At 115k2 "100" should be sufficient.
316 If you're using higher baudrates, you can increase this...
317 */
318
319static int sx_maxints = 100;
320
321#ifdef CONFIG_ISA
322
323/* These are the only open spaces in my computer. Yours may have more
324 or less.... -- REW
325 duh: Card at 0xa0000 is possible on HP Netserver?? -- pvdl
326*/
327static int sx_probe_addrs[] = {
328 0xc0000, 0xd0000, 0xe0000,
329 0xc8000, 0xd8000, 0xe8000
330};
331static int si_probe_addrs[] = {
332 0xc0000, 0xd0000, 0xe0000,
333 0xc8000, 0xd8000, 0xe8000, 0xa0000
334};
335static int si1_probe_addrs[] = {
336 0xd0000
337};
338
339#define NR_SX_ADDRS ARRAY_SIZE(sx_probe_addrs)
340#define NR_SI_ADDRS ARRAY_SIZE(si_probe_addrs)
341#define NR_SI1_ADDRS ARRAY_SIZE(si1_probe_addrs)
342
343module_param_array(sx_probe_addrs, int, NULL, 0);
344module_param_array(si_probe_addrs, int, NULL, 0);
345#endif
346
347/* Set the mask to all-ones. This alas, only supports 32 interrupts.
348 Some architectures may need more. */
349static int sx_irqmask = -1;
350
351module_param(sx_poll, int, 0);
352module_param(sx_slowpoll, int, 0);
353module_param(sx_maxints, int, 0);
354module_param(sx_debug, int, 0);
355module_param(sx_irqmask, int, 0);
356
357MODULE_LICENSE("GPL");
358
359static struct real_driver sx_real_driver = {
360 sx_disable_tx_interrupts,
361 sx_enable_tx_interrupts,
362 sx_disable_rx_interrupts,
363 sx_enable_rx_interrupts,
364 sx_shutdown_port,
365 sx_set_real_termios,
366 sx_chars_in_buffer,
367 sx_close,
368};
369
370/*
371 This driver can spew a whole lot of debugging output at you. If you
372 need maximum performance, you should disable the DEBUG define. To
373 aid in debugging in the field, I'm leaving the compile-time debug
374 features enabled, and disable them "runtime". That allows me to
375 instruct people with problems to enable debugging without requiring
376 them to recompile...
377*/
378#define DEBUG
379
380#ifdef DEBUG
381#define sx_dprintk(f, str...) if (sx_debug & f) printk (str)
382#else
383#define sx_dprintk(f, str...) /* nothing */
384#endif
385
386#define func_enter() sx_dprintk(SX_DEBUG_FLOW, "sx: enter %s\n",__func__)
387#define func_exit() sx_dprintk(SX_DEBUG_FLOW, "sx: exit %s\n",__func__)
388
389#define func_enter2() sx_dprintk(SX_DEBUG_FLOW, "sx: enter %s (port %d)\n", \
390 __func__, port->line)
391
392/*
393 * Firmware loader driver specific routines
394 *
395 */
396
397static const struct file_operations sx_fw_fops = {
398 .owner = THIS_MODULE,
399 .unlocked_ioctl = sx_fw_ioctl,
400};
401
402static struct miscdevice sx_fw_device = {
403 SXCTL_MISC_MINOR, "sxctl", &sx_fw_fops
404};
405
406#ifdef SX_PARANOIA_CHECK
407
408/* This doesn't work. Who's paranoid around here? Not me! */
409
410static inline int sx_paranoia_check(struct sx_port const *port,
411 char *name, const char *routine)
412{
413 static const char *badmagic = KERN_ERR "sx: Warning: bad sx port magic "
414 "number for device %s in %s\n";
415 static const char *badinfo = KERN_ERR "sx: Warning: null sx port for "
416 "device %s in %s\n";
417
418 if (!port) {
419 printk(badinfo, name, routine);
420 return 1;
421 }
422 if (port->magic != SX_MAGIC) {
423 printk(badmagic, name, routine);
424 return 1;
425 }
426
427 return 0;
428}
429#else
430#define sx_paranoia_check(a,b,c) 0
431#endif
432
433/* The timeouts. First try 30 times as fast as possible. Then give
434 the card some time to breathe between accesses. (Otherwise the
435 processor on the card might not be able to access its OWN bus... */
436
437#define TIMEOUT_1 30
438#define TIMEOUT_2 1000000
439
440#ifdef DEBUG
441static void my_hd_io(void __iomem *p, int len)
442{
443 int i, j, ch;
444 unsigned char __iomem *addr = p;
445
446 for (i = 0; i < len; i += 16) {
447 printk("%p ", addr + i);
448 for (j = 0; j < 16; j++) {
449 printk("%02x %s", readb(addr + j + i),
450 (j == 7) ? " " : "");
451 }
452 for (j = 0; j < 16; j++) {
453 ch = readb(addr + j + i);
454 printk("%c", (ch < 0x20) ? '.' :
455 ((ch > 0x7f) ? '.' : ch));
456 }
457 printk("\n");
458 }
459}
460static void my_hd(void *p, int len)
461{
462 int i, j, ch;
463 unsigned char *addr = p;
464
465 for (i = 0; i < len; i += 16) {
466 printk("%p ", addr + i);
467 for (j = 0; j < 16; j++) {
468 printk("%02x %s", addr[j + i], (j == 7) ? " " : "");
469 }
470 for (j = 0; j < 16; j++) {
471 ch = addr[j + i];
472 printk("%c", (ch < 0x20) ? '.' :
473 ((ch > 0x7f) ? '.' : ch));
474 }
475 printk("\n");
476 }
477}
478#endif
479
480/* This needs redoing for Alpha -- REW -- Done. */
481
482static inline void write_sx_byte(struct sx_board *board, int offset, u8 byte)
483{
484 writeb(byte, board->base + offset);
485}
486
487static inline u8 read_sx_byte(struct sx_board *board, int offset)
488{
489 return readb(board->base + offset);
490}
491
492static inline void write_sx_word(struct sx_board *board, int offset, u16 word)
493{
494 writew(word, board->base + offset);
495}
496
497static inline u16 read_sx_word(struct sx_board *board, int offset)
498{
499 return readw(board->base + offset);
500}
501
502static int sx_busy_wait_eq(struct sx_board *board,
503 int offset, int mask, int correctval)
504{
505 int i;
506
507 func_enter();
508
509 for (i = 0; i < TIMEOUT_1; i++)
510 if ((read_sx_byte(board, offset) & mask) == correctval) {
511 func_exit();
512 return 1;
513 }
514
515 for (i = 0; i < TIMEOUT_2; i++) {
516 if ((read_sx_byte(board, offset) & mask) == correctval) {
517 func_exit();
518 return 1;
519 }
520 udelay(1);
521 }
522
523 func_exit();
524 return 0;
525}
526
527static int sx_busy_wait_neq(struct sx_board *board,
528 int offset, int mask, int badval)
529{
530 int i;
531
532 func_enter();
533
534 for (i = 0; i < TIMEOUT_1; i++)
535 if ((read_sx_byte(board, offset) & mask) != badval) {
536 func_exit();
537 return 1;
538 }
539
540 for (i = 0; i < TIMEOUT_2; i++) {
541 if ((read_sx_byte(board, offset) & mask) != badval) {
542 func_exit();
543 return 1;
544 }
545 udelay(1);
546 }
547
548 func_exit();
549 return 0;
550}
551
552/* 5.6.4 of 6210028 r2.3 */
553static int sx_reset(struct sx_board *board)
554{
555 func_enter();
556
557 if (IS_SX_BOARD(board)) {
558
559 write_sx_byte(board, SX_CONFIG, 0);
560 write_sx_byte(board, SX_RESET, 1); /* Value doesn't matter */
561
562 if (!sx_busy_wait_eq(board, SX_RESET_STATUS, 1, 0)) {
563 printk(KERN_INFO "sx: Card doesn't respond to "
564 "reset...\n");
565 return 0;
566 }
567 } else if (IS_EISA_BOARD(board)) {
568 outb(board->irq << 4, board->eisa_base + 0xc02);
569 } else if (IS_SI1_BOARD(board)) {
570 write_sx_byte(board, SI1_ISA_RESET, 0); /*value doesn't matter*/
571 } else {
572 /* Gory details of the SI/ISA board */
573 write_sx_byte(board, SI2_ISA_RESET, SI2_ISA_RESET_SET);
574 write_sx_byte(board, SI2_ISA_IRQ11, SI2_ISA_IRQ11_CLEAR);
575 write_sx_byte(board, SI2_ISA_IRQ12, SI2_ISA_IRQ12_CLEAR);
576 write_sx_byte(board, SI2_ISA_IRQ15, SI2_ISA_IRQ15_CLEAR);
577 write_sx_byte(board, SI2_ISA_INTCLEAR, SI2_ISA_INTCLEAR_CLEAR);
578 write_sx_byte(board, SI2_ISA_IRQSET, SI2_ISA_IRQSET_CLEAR);
579 }
580
581 func_exit();
582 return 1;
583}
584
585/* This doesn't work on machines where "NULL" isn't 0 */
586/* If you have one of those, someone will need to write
587 the equivalent of this, which will amount to about 3 lines. I don't
588 want to complicate this right now. -- REW
589 (See, I do write comments every now and then :-) */
590#define OFFSETOF(strct, elem) ((long)&(((struct strct *)NULL)->elem))
591
592#define CHAN_OFFSET(port,elem) (port->ch_base + OFFSETOF (_SXCHANNEL, elem))
593#define MODU_OFFSET(board,addr,elem) (addr + OFFSETOF (_SXMODULE, elem))
594#define BRD_OFFSET(board,elem) (OFFSETOF (_SXCARD, elem))
595
596#define sx_write_channel_byte(port, elem, val) \
597 write_sx_byte (port->board, CHAN_OFFSET (port, elem), val)
598
599#define sx_read_channel_byte(port, elem) \
600 read_sx_byte (port->board, CHAN_OFFSET (port, elem))
601
602#define sx_write_channel_word(port, elem, val) \
603 write_sx_word (port->board, CHAN_OFFSET (port, elem), val)
604
605#define sx_read_channel_word(port, elem) \
606 read_sx_word (port->board, CHAN_OFFSET (port, elem))
607
608#define sx_write_module_byte(board, addr, elem, val) \
609 write_sx_byte (board, MODU_OFFSET (board, addr, elem), val)
610
611#define sx_read_module_byte(board, addr, elem) \
612 read_sx_byte (board, MODU_OFFSET (board, addr, elem))
613
614#define sx_write_module_word(board, addr, elem, val) \
615 write_sx_word (board, MODU_OFFSET (board, addr, elem), val)
616
617#define sx_read_module_word(board, addr, elem) \
618 read_sx_word (board, MODU_OFFSET (board, addr, elem))
619
620#define sx_write_board_byte(board, elem, val) \
621 write_sx_byte (board, BRD_OFFSET (board, elem), val)
622
623#define sx_read_board_byte(board, elem) \
624 read_sx_byte (board, BRD_OFFSET (board, elem))
625
626#define sx_write_board_word(board, elem, val) \
627 write_sx_word (board, BRD_OFFSET (board, elem), val)
628
629#define sx_read_board_word(board, elem) \
630 read_sx_word (board, BRD_OFFSET (board, elem))
631
632static int sx_start_board(struct sx_board *board)
633{
634 if (IS_SX_BOARD(board)) {
635 write_sx_byte(board, SX_CONFIG, SX_CONF_BUSEN);
636 } else if (IS_EISA_BOARD(board)) {
637 write_sx_byte(board, SI2_EISA_OFF, SI2_EISA_VAL);
638 outb((board->irq << 4) | 4, board->eisa_base + 0xc02);
639 } else if (IS_SI1_BOARD(board)) {
640 write_sx_byte(board, SI1_ISA_RESET_CLEAR, 0);
641 write_sx_byte(board, SI1_ISA_INTCL, 0);
642 } else {
643 /* Don't bug me about the clear_set.
644 I haven't the foggiest idea what it's about -- REW */
645 write_sx_byte(board, SI2_ISA_RESET, SI2_ISA_RESET_CLEAR);
646 write_sx_byte(board, SI2_ISA_INTCLEAR, SI2_ISA_INTCLEAR_SET);
647 }
648 return 1;
649}
650
651#define SX_IRQ_REG_VAL(board) \
652 ((board->flags & SX_ISA_BOARD) ? (board->irq << 4) : 0)
653
654/* Note. The SX register is write-only. Therefore, we have to enable the
655 bus too. This is a no-op, if you don't mess with this driver... */
656static int sx_start_interrupts(struct sx_board *board)
657{
658
659 /* Don't call this with board->irq == 0 */
660
661 if (IS_SX_BOARD(board)) {
662 write_sx_byte(board, SX_CONFIG, SX_IRQ_REG_VAL(board) |
663 SX_CONF_BUSEN | SX_CONF_HOSTIRQ);
664 } else if (IS_EISA_BOARD(board)) {
665 inb(board->eisa_base + 0xc03);
666 } else if (IS_SI1_BOARD(board)) {
667 write_sx_byte(board, SI1_ISA_INTCL, 0);
668 write_sx_byte(board, SI1_ISA_INTCL_CLEAR, 0);
669 } else {
670 switch (board->irq) {
671 case 11:
672 write_sx_byte(board, SI2_ISA_IRQ11, SI2_ISA_IRQ11_SET);
673 break;
674 case 12:
675 write_sx_byte(board, SI2_ISA_IRQ12, SI2_ISA_IRQ12_SET);
676 break;
677 case 15:
678 write_sx_byte(board, SI2_ISA_IRQ15, SI2_ISA_IRQ15_SET);
679 break;
680 default:
681 printk(KERN_INFO "sx: SI/XIO card doesn't support "
682 "interrupt %d.\n", board->irq);
683 return 0;
684 }
685 write_sx_byte(board, SI2_ISA_INTCLEAR, SI2_ISA_INTCLEAR_SET);
686 }
687
688 return 1;
689}
690
691static int sx_send_command(struct sx_port *port,
692 int command, int mask, int newstat)
693{
694 func_enter2();
695 write_sx_byte(port->board, CHAN_OFFSET(port, hi_hstat), command);
696 func_exit();
697 return sx_busy_wait_eq(port->board, CHAN_OFFSET(port, hi_hstat), mask,
698 newstat);
699}
700
701static char *mod_type_s(int module_type)
702{
703 switch (module_type) {
704 case TA4:
705 return "TA4";
706 case TA8:
707 return "TA8";
708 case TA4_ASIC:
709 return "TA4_ASIC";
710 case TA8_ASIC:
711 return "TA8_ASIC";
712 case MTA_CD1400:
713 return "MTA_CD1400";
714 case SXDC:
715 return "SXDC";
716 default:
717 return "Unknown/invalid";
718 }
719}
720
721static char *pan_type_s(int pan_type)
722{
723 switch (pan_type) {
724 case MOD_RS232DB25:
725 return "MOD_RS232DB25";
726 case MOD_RS232RJ45:
727 return "MOD_RS232RJ45";
728 case MOD_RS422DB25:
729 return "MOD_RS422DB25";
730 case MOD_PARALLEL:
731 return "MOD_PARALLEL";
732 case MOD_2_RS232DB25:
733 return "MOD_2_RS232DB25";
734 case MOD_2_RS232RJ45:
735 return "MOD_2_RS232RJ45";
736 case MOD_2_RS422DB25:
737 return "MOD_2_RS422DB25";
738 case MOD_RS232DB25MALE:
739 return "MOD_RS232DB25MALE";
740 case MOD_2_PARALLEL:
741 return "MOD_2_PARALLEL";
742 case MOD_BLANK:
743 return "empty";
744 default:
745 return "invalid";
746 }
747}
748
749static int mod_compat_type(int module_type)
750{
751 return module_type >> 4;
752}
753
754static void sx_reconfigure_port(struct sx_port *port)
755{
756 if (sx_read_channel_byte(port, hi_hstat) == HS_IDLE_OPEN) {
757 if (sx_send_command(port, HS_CONFIG, -1, HS_IDLE_OPEN) != 1) {
758 printk(KERN_WARNING "sx: Sent reconfigure command, but "
759 "card didn't react.\n");
760 }
761 } else {
762 sx_dprintk(SX_DEBUG_TERMIOS, "sx: Not sending reconfigure: "
763 "port isn't open (%02x).\n",
764 sx_read_channel_byte(port, hi_hstat));
765 }
766}
767
768static void sx_setsignals(struct sx_port *port, int dtr, int rts)
769{
770 int t;
771 func_enter2();
772
773 t = sx_read_channel_byte(port, hi_op);
774 if (dtr >= 0)
775 t = dtr ? (t | OP_DTR) : (t & ~OP_DTR);
776 if (rts >= 0)
777 t = rts ? (t | OP_RTS) : (t & ~OP_RTS);
778 sx_write_channel_byte(port, hi_op, t);
779 sx_dprintk(SX_DEBUG_MODEMSIGNALS, "setsignals: %d/%d\n", dtr, rts);
780
781 func_exit();
782}
783
784static int sx_getsignals(struct sx_port *port)
785{
786 int i_stat, o_stat;
787
788 o_stat = sx_read_channel_byte(port, hi_op);
789 i_stat = sx_read_channel_byte(port, hi_ip);
790
791 sx_dprintk(SX_DEBUG_MODEMSIGNALS, "getsignals: %d/%d (%d/%d) "
792 "%02x/%02x\n",
793 (o_stat & OP_DTR) != 0, (o_stat & OP_RTS) != 0,
794 port->c_dcd, tty_port_carrier_raised(&port->gs.port),
795 sx_read_channel_byte(port, hi_ip),
796 sx_read_channel_byte(port, hi_state));
797
798 return (((o_stat & OP_DTR) ? TIOCM_DTR : 0) |
799 ((o_stat & OP_RTS) ? TIOCM_RTS : 0) |
800 ((i_stat & IP_CTS) ? TIOCM_CTS : 0) |
801 ((i_stat & IP_DCD) ? TIOCM_CAR : 0) |
802 ((i_stat & IP_DSR) ? TIOCM_DSR : 0) |
803 ((i_stat & IP_RI) ? TIOCM_RNG : 0));
804}
805
806static void sx_set_baud(struct sx_port *port)
807{
808 int t;
809
810 if (port->board->ta_type == MOD_SXDC) {
811 switch (port->gs.baud) {
812 /* Save some typing work... */
813#define e(x) case x: t = BAUD_ ## x; break
814 e(50);
815 e(75);
816 e(110);
817 e(150);
818 e(200);
819 e(300);
820 e(600);
821 e(1200);
822 e(1800);
823 e(2000);
824 e(2400);
825 e(4800);
826 e(7200);
827 e(9600);
828 e(14400);
829 e(19200);
830 e(28800);
831 e(38400);
832 e(56000);
833 e(57600);
834 e(64000);
835 e(76800);
836 e(115200);
837 e(128000);
838 e(150000);
839 e(230400);
840 e(256000);
841 e(460800);
842 e(921600);
843 case 134:
844 t = BAUD_134_5;
845 break;
846 case 0:
847 t = -1;
848 break;
849 default:
850 /* Can I return "invalid"? */
851 t = BAUD_9600;
852 printk(KERN_INFO "sx: unsupported baud rate: %d.\n",
853 port->gs.baud);
854 break;
855 }
856#undef e
857 if (t > 0) {
858/* The baud rate is not set to 0, so we're enabeling DTR... -- REW */
859 sx_setsignals(port, 1, -1);
860 /* XXX This is not TA & MTA compatible */
861 sx_write_channel_byte(port, hi_csr, 0xff);
862
863 sx_write_channel_byte(port, hi_txbaud, t);
864 sx_write_channel_byte(port, hi_rxbaud, t);
865 } else {
866 sx_setsignals(port, 0, -1);
867 }
868 } else {
869 switch (port->gs.baud) {
870#define e(x) case x: t = CSR_ ## x; break
871 e(75);
872 e(150);
873 e(300);
874 e(600);
875 e(1200);
876 e(2400);
877 e(4800);
878 e(1800);
879 e(9600);
880 e(19200);
881 e(57600);
882 e(38400);
883/* TA supports 110, but not 115200, MTA supports 115200, but not 110 */
884 case 110:
885 if (port->board->ta_type == MOD_TA) {
886 t = CSR_110;
887 break;
888 } else {
889 t = CSR_9600;
890 printk(KERN_INFO "sx: Unsupported baud rate: "
891 "%d.\n", port->gs.baud);
892 break;
893 }
894 case 115200:
895 if (port->board->ta_type == MOD_TA) {
896 t = CSR_9600;
897 printk(KERN_INFO "sx: Unsupported baud rate: "
898 "%d.\n", port->gs.baud);
899 break;
900 } else {
901 t = CSR_110;
902 break;
903 }
904 case 0:
905 t = -1;
906 break;
907 default:
908 t = CSR_9600;
909 printk(KERN_INFO "sx: Unsupported baud rate: %d.\n",
910 port->gs.baud);
911 break;
912 }
913#undef e
914 if (t >= 0) {
915 sx_setsignals(port, 1, -1);
916 sx_write_channel_byte(port, hi_csr, t * 0x11);
917 } else {
918 sx_setsignals(port, 0, -1);
919 }
920 }
921}
922
923/* Simon Allen's version of this routine was 225 lines long. 85 is a lot
924 better. -- REW */
925
926static int sx_set_real_termios(void *ptr)
927{
928 struct sx_port *port = ptr;
929
930 func_enter2();
931
932 if (!port->gs.port.tty)
933 return 0;
934
935 /* What is this doing here? -- REW
936 Ha! figured it out. It is to allow you to get DTR active again
937 if you've dropped it with stty 0. Moved to set_baud, where it
938 belongs (next to the drop dtr if baud == 0) -- REW */
939 /* sx_setsignals (port, 1, -1); */
940
941 sx_set_baud(port);
942
943#define CFLAG port->gs.port.tty->termios->c_cflag
944 sx_write_channel_byte(port, hi_mr1,
945 (C_PARENB(port->gs.port.tty) ? MR1_WITH : MR1_NONE) |
946 (C_PARODD(port->gs.port.tty) ? MR1_ODD : MR1_EVEN) |
947 (C_CRTSCTS(port->gs.port.tty) ? MR1_RTS_RXFLOW : 0) |
948 (((CFLAG & CSIZE) == CS8) ? MR1_8_BITS : 0) |
949 (((CFLAG & CSIZE) == CS7) ? MR1_7_BITS : 0) |
950 (((CFLAG & CSIZE) == CS6) ? MR1_6_BITS : 0) |
951 (((CFLAG & CSIZE) == CS5) ? MR1_5_BITS : 0));
952
953 sx_write_channel_byte(port, hi_mr2,
954 (C_CRTSCTS(port->gs.port.tty) ? MR2_CTS_TXFLOW : 0) |
955 (C_CSTOPB(port->gs.port.tty) ? MR2_2_STOP :
956 MR2_1_STOP));
957
958 switch (CFLAG & CSIZE) {
959 case CS8:
960 sx_write_channel_byte(port, hi_mask, 0xff);
961 break;
962 case CS7:
963 sx_write_channel_byte(port, hi_mask, 0x7f);
964 break;
965 case CS6:
966 sx_write_channel_byte(port, hi_mask, 0x3f);
967 break;
968 case CS5:
969 sx_write_channel_byte(port, hi_mask, 0x1f);
970 break;
971 default:
972 printk(KERN_INFO "sx: Invalid wordsize: %u\n",
973 (unsigned int)CFLAG & CSIZE);
974 break;
975 }
976
977 sx_write_channel_byte(port, hi_prtcl,
978 (I_IXON(port->gs.port.tty) ? SP_TXEN : 0) |
979 (I_IXOFF(port->gs.port.tty) ? SP_RXEN : 0) |
980 (I_IXANY(port->gs.port.tty) ? SP_TANY : 0) | SP_DCEN);
981
982 sx_write_channel_byte(port, hi_break,
983 (I_IGNBRK(port->gs.port.tty) ? BR_IGN : 0 |
984 I_BRKINT(port->gs.port.tty) ? BR_INT : 0));
985
986 sx_write_channel_byte(port, hi_txon, START_CHAR(port->gs.port.tty));
987 sx_write_channel_byte(port, hi_rxon, START_CHAR(port->gs.port.tty));
988 sx_write_channel_byte(port, hi_txoff, STOP_CHAR(port->gs.port.tty));
989 sx_write_channel_byte(port, hi_rxoff, STOP_CHAR(port->gs.port.tty));
990
991 sx_reconfigure_port(port);
992
993 /* Tell line discipline whether we will do input cooking */
994 if (I_OTHER(port->gs.port.tty)) {
995 clear_bit(TTY_HW_COOK_IN, &port->gs.port.tty->flags);
996 } else {
997 set_bit(TTY_HW_COOK_IN, &port->gs.port.tty->flags);
998 }
999 sx_dprintk(SX_DEBUG_TERMIOS, "iflags: %x(%d) ",
1000 (unsigned int)port->gs.port.tty->termios->c_iflag,
1001 I_OTHER(port->gs.port.tty));
1002
1003/* Tell line discipline whether we will do output cooking.
1004 * If OPOST is set and no other output flags are set then we can do output
1005 * processing. Even if only *one* other flag in the O_OTHER group is set
1006 * we do cooking in software.
1007 */
1008 if (O_OPOST(port->gs.port.tty) && !O_OTHER(port->gs.port.tty)) {
1009 set_bit(TTY_HW_COOK_OUT, &port->gs.port.tty->flags);
1010 } else {
1011 clear_bit(TTY_HW_COOK_OUT, &port->gs.port.tty->flags);
1012 }
1013 sx_dprintk(SX_DEBUG_TERMIOS, "oflags: %x(%d)\n",
1014 (unsigned int)port->gs.port.tty->termios->c_oflag,
1015 O_OTHER(port->gs.port.tty));
1016 /* port->c_dcd = sx_get_CD (port); */
1017 func_exit();
1018 return 0;
1019}
1020
1021/* ********************************************************************** *
1022 * the interrupt related routines *
1023 * ********************************************************************** */
1024
1025/* Note:
1026 Other drivers use the macro "MIN" to calculate how much to copy.
1027 This has the disadvantage that it will evaluate parts twice. That's
1028 expensive when it's IO (and the compiler cannot optimize those away!).
1029 Moreover, I'm not sure that you're race-free.
1030
1031 I assign a value, and then only allow the value to decrease. This
1032 is always safe. This makes the code a few lines longer, and you
1033 know I'm dead against that, but I think it is required in this
1034 case. */
1035
1036static void sx_transmit_chars(struct sx_port *port)
1037{
1038 int c;
1039 int tx_ip;
1040 int txroom;
1041
1042 func_enter2();
1043 sx_dprintk(SX_DEBUG_TRANSMIT, "Port %p: transmit %d chars\n",
1044 port, port->gs.xmit_cnt);
1045
1046 if (test_and_set_bit(SX_PORT_TRANSMIT_LOCK, &port->locks)) {
1047 return;
1048 }
1049
1050 while (1) {
1051 c = port->gs.xmit_cnt;
1052
1053 sx_dprintk(SX_DEBUG_TRANSMIT, "Copying %d ", c);
1054 tx_ip = sx_read_channel_byte(port, hi_txipos);
1055
1056 /* Took me 5 minutes to deduce this formula.
1057 Luckily it is literally in the manual in section 6.5.4.3.5 */
1058 txroom = (sx_read_channel_byte(port, hi_txopos) - tx_ip - 1) &
1059 0xff;
1060
1061 /* Don't copy more bytes than there is room for in the buffer */
1062 if (c > txroom)
1063 c = txroom;
1064 sx_dprintk(SX_DEBUG_TRANSMIT, " %d(%d) ", c, txroom);
1065
1066 /* Don't copy past the end of the hardware transmit buffer */
1067 if (c > 0x100 - tx_ip)
1068 c = 0x100 - tx_ip;
1069
1070 sx_dprintk(SX_DEBUG_TRANSMIT, " %d(%d) ", c, 0x100 - tx_ip);
1071
1072 /* Don't copy pas the end of the source buffer */
1073 if (c > SERIAL_XMIT_SIZE - port->gs.xmit_tail)
1074 c = SERIAL_XMIT_SIZE - port->gs.xmit_tail;
1075
1076 sx_dprintk(SX_DEBUG_TRANSMIT, " %d(%ld) \n",
1077 c, SERIAL_XMIT_SIZE - port->gs.xmit_tail);
1078
1079 /* If for one reason or another, we can't copy more data, we're
1080 done! */
1081 if (c == 0)
1082 break;
1083
1084 memcpy_toio(port->board->base + CHAN_OFFSET(port, hi_txbuf) +
1085 tx_ip, port->gs.xmit_buf + port->gs.xmit_tail, c);
1086
1087 /* Update the pointer in the card */
1088 sx_write_channel_byte(port, hi_txipos, (tx_ip + c) & 0xff);
1089
1090 /* Update the kernel buffer end */
1091 port->gs.xmit_tail = (port->gs.xmit_tail + c) &
1092 (SERIAL_XMIT_SIZE - 1);
1093
1094 /* This one last. (this is essential)
1095 It would allow others to start putting more data into the
1096 buffer! */
1097 port->gs.xmit_cnt -= c;
1098 }
1099
1100 if (port->gs.xmit_cnt == 0) {
1101 sx_disable_tx_interrupts(port);
1102 }
1103
1104 if ((port->gs.xmit_cnt <= port->gs.wakeup_chars) && port->gs.port.tty) {
1105 tty_wakeup(port->gs.port.tty);
1106 sx_dprintk(SX_DEBUG_TRANSMIT, "Waking up.... ldisc (%d)....\n",
1107 port->gs.wakeup_chars);
1108 }
1109
1110 clear_bit(SX_PORT_TRANSMIT_LOCK, &port->locks);
1111 func_exit();
1112}
1113
1114/* Note the symmetry between receiving chars and transmitting them!
1115 Note: The kernel should have implemented both a receive buffer and
1116 a transmit buffer. */
1117
1118/* Inlined: Called only once. Remove the inline when you add another call */
1119static inline void sx_receive_chars(struct sx_port *port)
1120{
1121 int c;
1122 int rx_op;
1123 struct tty_struct *tty;
1124 int copied = 0;
1125 unsigned char *rp;
1126
1127 func_enter2();
1128 tty = port->gs.port.tty;
1129 while (1) {
1130 rx_op = sx_read_channel_byte(port, hi_rxopos);
1131 c = (sx_read_channel_byte(port, hi_rxipos) - rx_op) & 0xff;
1132
1133 sx_dprintk(SX_DEBUG_RECEIVE, "rxop=%d, c = %d.\n", rx_op, c);
1134
1135 /* Don't copy past the end of the hardware receive buffer */
1136 if (rx_op + c > 0x100)
1137 c = 0x100 - rx_op;
1138
1139 sx_dprintk(SX_DEBUG_RECEIVE, "c = %d.\n", c);
1140
1141 /* Don't copy more bytes than there is room for in the buffer */
1142
1143 c = tty_prepare_flip_string(tty, &rp, c);
1144
1145 sx_dprintk(SX_DEBUG_RECEIVE, "c = %d.\n", c);
1146
1147 /* If for one reason or another, we can't copy more data, we're done! */
1148 if (c == 0)
1149 break;
1150
1151 sx_dprintk(SX_DEBUG_RECEIVE, "Copying over %d chars. First is "
1152 "%d at %lx\n", c, read_sx_byte(port->board,
1153 CHAN_OFFSET(port, hi_rxbuf) + rx_op),
1154 CHAN_OFFSET(port, hi_rxbuf));
1155 memcpy_fromio(rp, port->board->base +
1156 CHAN_OFFSET(port, hi_rxbuf) + rx_op, c);
1157
1158 /* This one last. ( Not essential.)
1159 It allows the card to start putting more data into the
1160 buffer!
1161 Update the pointer in the card */
1162 sx_write_channel_byte(port, hi_rxopos, (rx_op + c) & 0xff);
1163
1164 copied += c;
1165 }
1166 if (copied) {
1167 struct timeval tv;
1168
1169 do_gettimeofday(&tv);
1170 sx_dprintk(SX_DEBUG_RECEIVE, "pushing flipq port %d (%3d "
1171 "chars): %d.%06d (%d/%d)\n", port->line,
1172 copied, (int)(tv.tv_sec % 60), (int)tv.tv_usec,
1173 tty->raw, tty->real_raw);
1174
1175 /* Tell the rest of the system the news. Great news. New
1176 characters! */
1177 tty_flip_buffer_push(tty);
1178 /* tty_schedule_flip (tty); */
1179 }
1180
1181 func_exit();
1182}
1183
1184/* Inlined: it is called only once. Remove the inline if you add another
1185 call */
1186static inline void sx_check_modem_signals(struct sx_port *port)
1187{
1188 int hi_state;
1189 int c_dcd;
1190
1191 hi_state = sx_read_channel_byte(port, hi_state);
1192 sx_dprintk(SX_DEBUG_MODEMSIGNALS, "Checking modem signals (%d/%d)\n",
1193 port->c_dcd, tty_port_carrier_raised(&port->gs.port));
1194
1195 if (hi_state & ST_BREAK) {
1196 hi_state &= ~ST_BREAK;
1197 sx_dprintk(SX_DEBUG_MODEMSIGNALS, "got a break.\n");
1198 sx_write_channel_byte(port, hi_state, hi_state);
1199 gs_got_break(&port->gs);
1200 }
1201 if (hi_state & ST_DCD) {
1202 hi_state &= ~ST_DCD;
1203 sx_dprintk(SX_DEBUG_MODEMSIGNALS, "got a DCD change.\n");
1204 sx_write_channel_byte(port, hi_state, hi_state);
1205 c_dcd = tty_port_carrier_raised(&port->gs.port);
1206 sx_dprintk(SX_DEBUG_MODEMSIGNALS, "DCD is now %d\n", c_dcd);
1207 if (c_dcd != port->c_dcd) {
1208 port->c_dcd = c_dcd;
1209 if (tty_port_carrier_raised(&port->gs.port)) {
1210 /* DCD went UP */
1211 if ((sx_read_channel_byte(port, hi_hstat) !=
1212 HS_IDLE_CLOSED) &&
1213 !(port->gs.port.tty->termios->
1214 c_cflag & CLOCAL)) {
1215 /* Are we blocking in open? */
1216 sx_dprintk(SX_DEBUG_MODEMSIGNALS, "DCD "
1217 "active, unblocking open\n");
1218 wake_up_interruptible(&port->gs.port.
1219 open_wait);
1220 } else {
1221 sx_dprintk(SX_DEBUG_MODEMSIGNALS, "DCD "
1222 "raised. Ignoring.\n");
1223 }
1224 } else {
1225 /* DCD went down! */
1226 if (!(port->gs.port.tty->termios->c_cflag & CLOCAL)){
1227 sx_dprintk(SX_DEBUG_MODEMSIGNALS, "DCD "
1228 "dropped. hanging up....\n");
1229 tty_hangup(port->gs.port.tty);
1230 } else {
1231 sx_dprintk(SX_DEBUG_MODEMSIGNALS, "DCD "
1232 "dropped. ignoring.\n");
1233 }
1234 }
1235 } else {
1236 sx_dprintk(SX_DEBUG_MODEMSIGNALS, "Hmmm. card told us "
1237 "DCD changed, but it didn't.\n");
1238 }
1239 }
1240}
1241
1242/* This is what an interrupt routine should look like.
1243 * Small, elegant, clear.
1244 */
1245
1246static irqreturn_t sx_interrupt(int irq, void *ptr)
1247{
1248 struct sx_board *board = ptr;
1249 struct sx_port *port;
1250 int i;
1251
1252 func_enter();
1253 sx_dprintk(SX_DEBUG_FLOW, "sx: enter sx_interrupt (%d/%d)\n", irq,
1254 board->irq);
1255
1256 /* AAargh! The order in which to do these things is essential and
1257 not trivial.
1258
1259 - Rate limit goes before "recursive". Otherwise a series of
1260 recursive calls will hang the machine in the interrupt routine.
1261
1262 - hardware twiddling goes before "recursive". Otherwise when we
1263 poll the card, and a recursive interrupt happens, we won't
1264 ack the card, so it might keep on interrupting us. (especially
1265 level sensitive interrupt systems like PCI).
1266
1267 - Rate limit goes before hardware twiddling. Otherwise we won't
1268 catch a card that has gone bonkers.
1269
1270 - The "initialized" test goes after the hardware twiddling. Otherwise
1271 the card will stick us in the interrupt routine again.
1272
1273 - The initialized test goes before recursive.
1274 */
1275
1276#ifdef IRQ_RATE_LIMIT
1277 /* Aaargh! I'm ashamed. This costs more lines-of-code than the
1278 actual interrupt routine!. (Well, used to when I wrote that
1279 comment) */
1280 {
1281 static int lastjif;
1282 static int nintr = 0;
1283
1284 if (lastjif == jiffies) {
1285 if (++nintr > IRQ_RATE_LIMIT) {
1286 free_irq(board->irq, board);
1287 printk(KERN_ERR "sx: Too many interrupts. "
1288 "Turning off interrupt %d.\n",
1289 board->irq);
1290 }
1291 } else {
1292 lastjif = jiffies;
1293 nintr = 0;
1294 }
1295 }
1296#endif
1297
1298 if (board->irq == irq) {
1299 /* Tell the card we've noticed the interrupt. */
1300
1301 sx_write_board_word(board, cc_int_pending, 0);
1302 if (IS_SX_BOARD(board)) {
1303 write_sx_byte(board, SX_RESET_IRQ, 1);
1304 } else if (IS_EISA_BOARD(board)) {
1305 inb(board->eisa_base + 0xc03);
1306 write_sx_word(board, 8, 0);
1307 } else {
1308 write_sx_byte(board, SI2_ISA_INTCLEAR,
1309 SI2_ISA_INTCLEAR_CLEAR);
1310 write_sx_byte(board, SI2_ISA_INTCLEAR,
1311 SI2_ISA_INTCLEAR_SET);
1312 }
1313 }
1314
1315 if (!sx_initialized)
1316 return IRQ_HANDLED;
1317 if (!(board->flags & SX_BOARD_INITIALIZED))
1318 return IRQ_HANDLED;
1319
1320 if (test_and_set_bit(SX_BOARD_INTR_LOCK, &board->locks)) {
1321 printk(KERN_ERR "Recursive interrupt! (%d)\n", board->irq);
1322 return IRQ_HANDLED;
1323 }
1324
1325 for (i = 0; i < board->nports; i++) {
1326 port = &board->ports[i];
1327 if (port->gs.port.flags & GS_ACTIVE) {
1328 if (sx_read_channel_byte(port, hi_state)) {
1329 sx_dprintk(SX_DEBUG_INTERRUPTS, "Port %d: "
1330 "modem signal change?... \n",i);
1331 sx_check_modem_signals(port);
1332 }
1333 if (port->gs.xmit_cnt) {
1334 sx_transmit_chars(port);
1335 }
1336 if (!(port->gs.port.flags & SX_RX_THROTTLE)) {
1337 sx_receive_chars(port);
1338 }
1339 }
1340 }
1341
1342 clear_bit(SX_BOARD_INTR_LOCK, &board->locks);
1343
1344 sx_dprintk(SX_DEBUG_FLOW, "sx: exit sx_interrupt (%d/%d)\n", irq,
1345 board->irq);
1346 func_exit();
1347 return IRQ_HANDLED;
1348}
1349
1350static void sx_pollfunc(unsigned long data)
1351{
1352 struct sx_board *board = (struct sx_board *)data;
1353
1354 func_enter();
1355
1356 sx_interrupt(0, board);
1357
1358 mod_timer(&board->timer, jiffies + sx_poll);
1359 func_exit();
1360}
1361
1362/* ********************************************************************** *
1363 * Here are the routines that actually *
1364 * interface with the generic_serial driver *
1365 * ********************************************************************** */
1366
1367/* Ehhm. I don't know how to fiddle with interrupts on the SX card. --REW */
1368/* Hmm. Ok I figured it out. You don't. */
1369
1370static void sx_disable_tx_interrupts(void *ptr)
1371{
1372 struct sx_port *port = ptr;
1373 func_enter2();
1374
1375 port->gs.port.flags &= ~GS_TX_INTEN;
1376
1377 func_exit();
1378}
1379
1380static void sx_enable_tx_interrupts(void *ptr)
1381{
1382 struct sx_port *port = ptr;
1383 int data_in_buffer;
1384 func_enter2();
1385
1386 /* First transmit the characters that we're supposed to */
1387 sx_transmit_chars(port);
1388
1389 /* The sx card will never interrupt us if we don't fill the buffer
1390 past 25%. So we keep considering interrupts off if that's the case. */
1391 data_in_buffer = (sx_read_channel_byte(port, hi_txipos) -
1392 sx_read_channel_byte(port, hi_txopos)) & 0xff;
1393
1394 /* XXX Must be "HIGH_WATER" for SI card according to doc. */
1395 if (data_in_buffer < LOW_WATER)
1396 port->gs.port.flags &= ~GS_TX_INTEN;
1397
1398 func_exit();
1399}
1400
1401static void sx_disable_rx_interrupts(void *ptr)
1402{
1403 /* struct sx_port *port = ptr; */
1404 func_enter();
1405
1406 func_exit();
1407}
1408
1409static void sx_enable_rx_interrupts(void *ptr)
1410{
1411 /* struct sx_port *port = ptr; */
1412 func_enter();
1413
1414 func_exit();
1415}
1416
1417/* Jeez. Isn't this simple? */
1418static int sx_carrier_raised(struct tty_port *port)
1419{
1420 struct sx_port *sp = container_of(port, struct sx_port, gs.port);
1421 return ((sx_read_channel_byte(sp, hi_ip) & IP_DCD) != 0);
1422}
1423
1424/* Jeez. Isn't this simple? */
1425static int sx_chars_in_buffer(void *ptr)
1426{
1427 struct sx_port *port = ptr;
1428 func_enter2();
1429
1430 func_exit();
1431 return ((sx_read_channel_byte(port, hi_txipos) -
1432 sx_read_channel_byte(port, hi_txopos)) & 0xff);
1433}
1434
1435static void sx_shutdown_port(void *ptr)
1436{
1437 struct sx_port *port = ptr;
1438
1439 func_enter();
1440
1441 port->gs.port.flags &= ~GS_ACTIVE;
1442 if (port->gs.port.tty && (port->gs.port.tty->termios->c_cflag & HUPCL)) {
1443 sx_setsignals(port, 0, 0);
1444 sx_reconfigure_port(port);
1445 }
1446
1447 func_exit();
1448}
1449
1450/* ********************************************************************** *
1451 * Here are the routines that actually *
1452 * interface with the rest of the system *
1453 * ********************************************************************** */
1454
1455static int sx_open(struct tty_struct *tty, struct file *filp)
1456{
1457 struct sx_port *port;
1458 int retval, line;
1459 unsigned long flags;
1460
1461 func_enter();
1462
1463 if (!sx_initialized) {
1464 return -EIO;
1465 }
1466
1467 line = tty->index;
1468 sx_dprintk(SX_DEBUG_OPEN, "%d: opening line %d. tty=%p ctty=%p, "
1469 "np=%d)\n", task_pid_nr(current), line, tty,
1470 current->signal->tty, sx_nports);
1471
1472 if ((line < 0) || (line >= SX_NPORTS) || (line >= sx_nports))
1473 return -ENODEV;
1474
1475 port = &sx_ports[line];
1476 port->c_dcd = 0; /* Make sure that the first interrupt doesn't detect a
1477 1 -> 0 transition. */
1478
1479 sx_dprintk(SX_DEBUG_OPEN, "port = %p c_dcd = %d\n", port, port->c_dcd);
1480
1481 spin_lock_irqsave(&port->gs.driver_lock, flags);
1482
1483 tty->driver_data = port;
1484 port->gs.port.tty = tty;
1485 port->gs.port.count++;
1486 spin_unlock_irqrestore(&port->gs.driver_lock, flags);
1487
1488 sx_dprintk(SX_DEBUG_OPEN, "starting port\n");
1489
1490 /*
1491 * Start up serial port
1492 */
1493 retval = gs_init_port(&port->gs);
1494 sx_dprintk(SX_DEBUG_OPEN, "done gs_init\n");
1495 if (retval) {
1496 port->gs.port.count--;
1497 return retval;
1498 }
1499
1500 port->gs.port.flags |= GS_ACTIVE;
1501 if (port->gs.port.count <= 1)
1502 sx_setsignals(port, 1, 1);
1503
1504#if 0
1505 if (sx_debug & SX_DEBUG_OPEN)
1506 my_hd(port, sizeof(*port));
1507#else
1508 if (sx_debug & SX_DEBUG_OPEN)
1509 my_hd_io(port->board->base + port->ch_base, sizeof(*port));
1510#endif
1511
1512 if (port->gs.port.count <= 1) {
1513 if (sx_send_command(port, HS_LOPEN, -1, HS_IDLE_OPEN) != 1) {
1514 printk(KERN_ERR "sx: Card didn't respond to LOPEN "
1515 "command.\n");
1516 spin_lock_irqsave(&port->gs.driver_lock, flags);
1517 port->gs.port.count--;
1518 spin_unlock_irqrestore(&port->gs.driver_lock, flags);
1519 return -EIO;
1520 }
1521 }
1522
1523 retval = gs_block_til_ready(port, filp);
1524 sx_dprintk(SX_DEBUG_OPEN, "Block til ready returned %d. Count=%d\n",
1525 retval, port->gs.port.count);
1526
1527 if (retval) {
1528/*
1529 * Don't lower gs.port.count here because sx_close() will be called later
1530 */
1531
1532 return retval;
1533 }
1534 /* tty->low_latency = 1; */
1535
1536 port->c_dcd = sx_carrier_raised(&port->gs.port);
1537 sx_dprintk(SX_DEBUG_OPEN, "at open: cd=%d\n", port->c_dcd);
1538
1539 func_exit();
1540 return 0;
1541
1542}
1543
1544static void sx_close(void *ptr)
1545{
1546 struct sx_port *port = ptr;
1547 /* Give the port 5 seconds to close down. */
1548 int to = 5 * HZ;
1549
1550 func_enter();
1551
1552 sx_setsignals(port, 0, 0);
1553 sx_reconfigure_port(port);
1554 sx_send_command(port, HS_CLOSE, 0, 0);
1555
1556 while (to-- && (sx_read_channel_byte(port, hi_hstat) != HS_IDLE_CLOSED))
1557 if (msleep_interruptible(10))
1558 break;
1559 if (sx_read_channel_byte(port, hi_hstat) != HS_IDLE_CLOSED) {
1560 if (sx_send_command(port, HS_FORCE_CLOSED, -1, HS_IDLE_CLOSED)
1561 != 1) {
1562 printk(KERN_ERR "sx: sent the force_close command, but "
1563 "card didn't react\n");
1564 } else
1565 sx_dprintk(SX_DEBUG_CLOSE, "sent the force_close "
1566 "command.\n");
1567 }
1568
1569 sx_dprintk(SX_DEBUG_CLOSE, "waited %d jiffies for close. count=%d\n",
1570 5 * HZ - to - 1, port->gs.port.count);
1571
1572 if (port->gs.port.count) {
1573 sx_dprintk(SX_DEBUG_CLOSE, "WARNING port count:%d\n",
1574 port->gs.port.count);
1575 /*printk("%s SETTING port count to zero: %p count: %d\n",
1576 __func__, port, port->gs.port.count);
1577 port->gs.port.count = 0;*/
1578 }
1579
1580 func_exit();
1581}
1582
1583/* This is relatively thorough. But then again it is only 20 lines. */
1584#define MARCHUP for (i = min; i < max; i++)
1585#define MARCHDOWN for (i = max - 1; i >= min; i--)
1586#define W0 write_sx_byte(board, i, 0x55)
1587#define W1 write_sx_byte(board, i, 0xaa)
1588#define R0 if (read_sx_byte(board, i) != 0x55) return 1
1589#define R1 if (read_sx_byte(board, i) != 0xaa) return 1
1590
1591/* This memtest takes a human-noticable time. You normally only do it
1592 once a boot, so I guess that it is worth it. */
1593static int do_memtest(struct sx_board *board, int min, int max)
1594{
1595 int i;
1596
1597 /* This is a marchb. Theoretically, marchb catches much more than
1598 simpler tests. In practise, the longer test just catches more
1599 intermittent errors. -- REW
1600 (For the theory behind memory testing see:
1601 Testing Semiconductor Memories by A.J. van de Goor.) */
1602 MARCHUP {
1603 W0;
1604 }
1605 MARCHUP {
1606 R0;
1607 W1;
1608 R1;
1609 W0;
1610 R0;
1611 W1;
1612 }
1613 MARCHUP {
1614 R1;
1615 W0;
1616 W1;
1617 }
1618 MARCHDOWN {
1619 R1;
1620 W0;
1621 W1;
1622 W0;
1623 }
1624 MARCHDOWN {
1625 R0;
1626 W1;
1627 W0;
1628 }
1629
1630 return 0;
1631}
1632
1633#undef MARCHUP
1634#undef MARCHDOWN
1635#undef W0
1636#undef W1
1637#undef R0
1638#undef R1
1639
1640#define MARCHUP for (i = min; i < max; i += 2)
1641#define MARCHDOWN for (i = max - 1; i >= min; i -= 2)
1642#define W0 write_sx_word(board, i, 0x55aa)
1643#define W1 write_sx_word(board, i, 0xaa55)
1644#define R0 if (read_sx_word(board, i) != 0x55aa) return 1
1645#define R1 if (read_sx_word(board, i) != 0xaa55) return 1
1646
1647#if 0
1648/* This memtest takes a human-noticable time. You normally only do it
1649 once a boot, so I guess that it is worth it. */
1650static int do_memtest_w(struct sx_board *board, int min, int max)
1651{
1652 int i;
1653
1654 MARCHUP {
1655 W0;
1656 }
1657 MARCHUP {
1658 R0;
1659 W1;
1660 R1;
1661 W0;
1662 R0;
1663 W1;
1664 }
1665 MARCHUP {
1666 R1;
1667 W0;
1668 W1;
1669 }
1670 MARCHDOWN {
1671 R1;
1672 W0;
1673 W1;
1674 W0;
1675 }
1676 MARCHDOWN {
1677 R0;
1678 W1;
1679 W0;
1680 }
1681
1682 return 0;
1683}
1684#endif
1685
1686static long sx_fw_ioctl(struct file *filp, unsigned int cmd,
1687 unsigned long arg)
1688{
1689 long rc = 0;
1690 int __user *descr = (int __user *)arg;
1691 int i;
1692 static struct sx_board *board = NULL;
1693 int nbytes, offset;
1694 unsigned long data;
1695 char *tmp;
1696
1697 func_enter();
1698
1699 if (!capable(CAP_SYS_RAWIO))
1700 return -EPERM;
1701
1702 tty_lock();
1703
1704 sx_dprintk(SX_DEBUG_FIRMWARE, "IOCTL %x: %lx\n", cmd, arg);
1705
1706 if (!board)
1707 board = &boards[0];
1708 if (board->flags & SX_BOARD_PRESENT) {
1709 sx_dprintk(SX_DEBUG_FIRMWARE, "Board present! (%x)\n",
1710 board->flags);
1711 } else {
1712 sx_dprintk(SX_DEBUG_FIRMWARE, "Board not present! (%x) all:",
1713 board->flags);
1714 for (i = 0; i < SX_NBOARDS; i++)
1715 sx_dprintk(SX_DEBUG_FIRMWARE, "<%x> ", boards[i].flags);
1716 sx_dprintk(SX_DEBUG_FIRMWARE, "\n");
1717 rc = -EIO;
1718 goto out;
1719 }
1720
1721 switch (cmd) {
1722 case SXIO_SET_BOARD:
1723 sx_dprintk(SX_DEBUG_FIRMWARE, "set board to %ld\n", arg);
1724 rc = -EIO;
1725 if (arg >= SX_NBOARDS)
1726 break;
1727 sx_dprintk(SX_DEBUG_FIRMWARE, "not out of range\n");
1728 if (!(boards[arg].flags & SX_BOARD_PRESENT))
1729 break;
1730 sx_dprintk(SX_DEBUG_FIRMWARE, ".. and present!\n");
1731 board = &boards[arg];
1732 rc = 0;
1733 /* FIXME: And this does ... nothing?? */
1734 break;
1735 case SXIO_GET_TYPE:
1736 rc = -ENOENT; /* If we manage to miss one, return error. */
1737 if (IS_SX_BOARD(board))
1738 rc = SX_TYPE_SX;
1739 if (IS_CF_BOARD(board))
1740 rc = SX_TYPE_CF;
1741 if (IS_SI_BOARD(board))
1742 rc = SX_TYPE_SI;
1743 if (IS_SI1_BOARD(board))
1744 rc = SX_TYPE_SI;
1745 if (IS_EISA_BOARD(board))
1746 rc = SX_TYPE_SI;
1747 sx_dprintk(SX_DEBUG_FIRMWARE, "returning type= %ld\n", rc);
1748 break;
1749 case SXIO_DO_RAMTEST:
1750 if (sx_initialized) { /* Already initialized: better not ramtest the board. */
1751 rc = -EPERM;
1752 break;
1753 }
1754 if (IS_SX_BOARD(board)) {
1755 rc = do_memtest(board, 0, 0x7000);
1756 if (!rc)
1757 rc = do_memtest(board, 0, 0x7000);
1758 /*if (!rc) rc = do_memtest_w (board, 0, 0x7000); */
1759 } else {
1760 rc = do_memtest(board, 0, 0x7ff8);
1761 /* if (!rc) rc = do_memtest_w (board, 0, 0x7ff8); */
1762 }
1763 sx_dprintk(SX_DEBUG_FIRMWARE,
1764 "returning memtest result= %ld\n", rc);
1765 break;
1766 case SXIO_DOWNLOAD:
1767 if (sx_initialized) {/* Already initialized */
1768 rc = -EEXIST;
1769 break;
1770 }
1771 if (!sx_reset(board)) {
1772 rc = -EIO;
1773 break;
1774 }
1775 sx_dprintk(SX_DEBUG_INIT, "reset the board...\n");
1776
1777 tmp = kmalloc(SX_CHUNK_SIZE, GFP_USER);
1778 if (!tmp) {
1779 rc = -ENOMEM;
1780 break;
1781 }
1782 /* FIXME: check returns */
1783 get_user(nbytes, descr++);
1784 get_user(offset, descr++);
1785 get_user(data, descr++);
1786 while (nbytes && data) {
1787 for (i = 0; i < nbytes; i += SX_CHUNK_SIZE) {
1788 if (copy_from_user(tmp, (char __user *)data + i,
1789 (i + SX_CHUNK_SIZE > nbytes) ?
1790 nbytes - i : SX_CHUNK_SIZE)) {
1791 kfree(tmp);
1792 rc = -EFAULT;
1793 goto out;
1794 }
1795 memcpy_toio(board->base2 + offset + i, tmp,
1796 (i + SX_CHUNK_SIZE > nbytes) ?
1797 nbytes - i : SX_CHUNK_SIZE);
1798 }
1799
1800 get_user(nbytes, descr++);
1801 get_user(offset, descr++);
1802 get_user(data, descr++);
1803 }
1804 kfree(tmp);
1805 sx_nports += sx_init_board(board);
1806 rc = sx_nports;
1807 break;
1808 case SXIO_INIT:
1809 if (sx_initialized) { /* Already initialized */
1810 rc = -EEXIST;
1811 break;
1812 }
1813 /* This is not allowed until all boards are initialized... */
1814 for (i = 0; i < SX_NBOARDS; i++) {
1815 if ((boards[i].flags & SX_BOARD_PRESENT) &&
1816 !(boards[i].flags & SX_BOARD_INITIALIZED)) {
1817 rc = -EIO;
1818 break;
1819 }
1820 }
1821 for (i = 0; i < SX_NBOARDS; i++)
1822 if (!(boards[i].flags & SX_BOARD_PRESENT))
1823 break;
1824
1825 sx_dprintk(SX_DEBUG_FIRMWARE, "initing portstructs, %d boards, "
1826 "%d channels, first board: %d ports\n",
1827 i, sx_nports, boards[0].nports);
1828 rc = sx_init_portstructs(i, sx_nports);
1829 sx_init_drivers();
1830 if (rc >= 0)
1831 sx_initialized++;
1832 break;
1833 case SXIO_SETDEBUG:
1834 sx_debug = arg;
1835 break;
1836 case SXIO_GETDEBUG:
1837 rc = sx_debug;
1838 break;
1839 case SXIO_GETGSDEBUG:
1840 case SXIO_SETGSDEBUG:
1841 rc = -EINVAL;
1842 break;
1843 case SXIO_GETNPORTS:
1844 rc = sx_nports;
1845 break;
1846 default:
1847 rc = -ENOTTY;
1848 break;
1849 }
1850out:
1851 tty_unlock();
1852 func_exit();
1853 return rc;
1854}
1855
1856static int sx_break(struct tty_struct *tty, int flag)
1857{
1858 struct sx_port *port = tty->driver_data;
1859 int rv;
1860
1861 func_enter();
1862 tty_lock();
1863
1864 if (flag)
1865 rv = sx_send_command(port, HS_START, -1, HS_IDLE_BREAK);
1866 else
1867 rv = sx_send_command(port, HS_STOP, -1, HS_IDLE_OPEN);
1868 if (rv != 1)
1869 printk(KERN_ERR "sx: couldn't send break (%x).\n",
1870 read_sx_byte(port->board, CHAN_OFFSET(port, hi_hstat)));
1871 tty_unlock();
1872 func_exit();
1873 return 0;
1874}
1875
1876static int sx_tiocmget(struct tty_struct *tty, struct file *file)
1877{
1878 struct sx_port *port = tty->driver_data;
1879 return sx_getsignals(port);
1880}
1881
1882static int sx_tiocmset(struct tty_struct *tty, struct file *file,
1883 unsigned int set, unsigned int clear)
1884{
1885 struct sx_port *port = tty->driver_data;
1886 int rts = -1, dtr = -1;
1887
1888 if (set & TIOCM_RTS)
1889 rts = 1;
1890 if (set & TIOCM_DTR)
1891 dtr = 1;
1892 if (clear & TIOCM_RTS)
1893 rts = 0;
1894 if (clear & TIOCM_DTR)
1895 dtr = 0;
1896
1897 sx_setsignals(port, dtr, rts);
1898 sx_reconfigure_port(port);
1899 return 0;
1900}
1901
1902static int sx_ioctl(struct tty_struct *tty, struct file *filp,
1903 unsigned int cmd, unsigned long arg)
1904{
1905 int rc;
1906 struct sx_port *port = tty->driver_data;
1907 void __user *argp = (void __user *)arg;
1908
1909 /* func_enter2(); */
1910
1911 rc = 0;
1912 tty_lock();
1913 switch (cmd) {
1914 case TIOCGSERIAL:
1915 rc = gs_getserial(&port->gs, argp);
1916 break;
1917 case TIOCSSERIAL:
1918 rc = gs_setserial(&port->gs, argp);
1919 break;
1920 default:
1921 rc = -ENOIOCTLCMD;
1922 break;
1923 }
1924 tty_unlock();
1925
1926 /* func_exit(); */
1927 return rc;
1928}
1929
1930/* The throttle/unthrottle scheme for the Specialix card is different
1931 * from other drivers and deserves some explanation.
1932 * The Specialix hardware takes care of XON/XOFF
1933 * and CTS/RTS flow control itself. This means that all we have to
1934 * do when signalled by the upper tty layer to throttle/unthrottle is
1935 * to make a note of it here. When we come to read characters from the
1936 * rx buffers on the card (sx_receive_chars()) we look to see if the
1937 * upper layer can accept more (as noted here in sx_rx_throt[]).
1938 * If it can't we simply don't remove chars from the cards buffer.
1939 * When the tty layer can accept chars, we again note that here and when
1940 * sx_receive_chars() is called it will remove them from the cards buffer.
1941 * The card will notice that a ports buffer has drained below some low
1942 * water mark and will unflow control the line itself, using whatever
1943 * flow control scheme is in use for that port. -- Simon Allen
1944 */
1945
1946static void sx_throttle(struct tty_struct *tty)
1947{
1948 struct sx_port *port = tty->driver_data;
1949
1950 func_enter2();
1951 /* If the port is using any type of input flow
1952 * control then throttle the port.
1953 */
1954 if ((tty->termios->c_cflag & CRTSCTS) || (I_IXOFF(tty))) {
1955 port->gs.port.flags |= SX_RX_THROTTLE;
1956 }
1957 func_exit();
1958}
1959
1960static void sx_unthrottle(struct tty_struct *tty)
1961{
1962 struct sx_port *port = tty->driver_data;
1963
1964 func_enter2();
1965 /* Always unthrottle even if flow control is not enabled on
1966 * this port in case we disabled flow control while the port
1967 * was throttled
1968 */
1969 port->gs.port.flags &= ~SX_RX_THROTTLE;
1970 func_exit();
1971 return;
1972}
1973
1974/* ********************************************************************** *
1975 * Here are the initialization routines. *
1976 * ********************************************************************** */
1977
1978static int sx_init_board(struct sx_board *board)
1979{
1980 int addr;
1981 int chans;
1982 int type;
1983
1984 func_enter();
1985
1986 /* This is preceded by downloading the download code. */
1987
1988 board->flags |= SX_BOARD_INITIALIZED;
1989
1990 if (read_sx_byte(board, 0))
1991 /* CF boards may need this. */
1992 write_sx_byte(board, 0, 0);
1993
1994 /* This resets the processor again, to make sure it didn't do any
1995 foolish things while we were downloading the image */
1996 if (!sx_reset(board))
1997 return 0;
1998
1999 sx_start_board(board);
2000 udelay(10);
2001 if (!sx_busy_wait_neq(board, 0, 0xff, 0)) {
2002 printk(KERN_ERR "sx: Ooops. Board won't initialize.\n");
2003 return 0;
2004 }
2005
2006 /* Ok. So now the processor on the card is running. It gathered
2007 some info for us... */
2008 sx_dprintk(SX_DEBUG_INIT, "The sxcard structure:\n");
2009 if (sx_debug & SX_DEBUG_INIT)
2010 my_hd_io(board->base, 0x10);
2011 sx_dprintk(SX_DEBUG_INIT, "the first sx_module structure:\n");
2012 if (sx_debug & SX_DEBUG_INIT)
2013 my_hd_io(board->base + 0x80, 0x30);
2014
2015 sx_dprintk(SX_DEBUG_INIT, "init_status: %x, %dk memory, firmware "
2016 "V%x.%02x,\n",
2017 read_sx_byte(board, 0), read_sx_byte(board, 1),
2018 read_sx_byte(board, 5), read_sx_byte(board, 4));
2019
2020 if (read_sx_byte(board, 0) == 0xff) {
2021 printk(KERN_INFO "sx: No modules found. Sorry.\n");
2022 board->nports = 0;
2023 return 0;
2024 }
2025
2026 chans = 0;
2027
2028 if (IS_SX_BOARD(board)) {
2029 sx_write_board_word(board, cc_int_count, sx_maxints);
2030 } else {
2031 if (sx_maxints)
2032 sx_write_board_word(board, cc_int_count,
2033 SI_PROCESSOR_CLOCK / 8 / sx_maxints);
2034 }
2035
2036 /* grab the first module type... */
2037 /* board->ta_type = mod_compat_type (read_sx_byte (board, 0x80 + 0x08)); */
2038 board->ta_type = mod_compat_type(sx_read_module_byte(board, 0x80,
2039 mc_chip));
2040
2041 /* XXX byteorder */
2042 for (addr = 0x80; addr != 0; addr = read_sx_word(board, addr) & 0x7fff){
2043 type = sx_read_module_byte(board, addr, mc_chip);
2044 sx_dprintk(SX_DEBUG_INIT, "Module at %x: %d channels\n",
2045 addr, read_sx_byte(board, addr + 2));
2046
2047 chans += sx_read_module_byte(board, addr, mc_type);
2048
2049 sx_dprintk(SX_DEBUG_INIT, "module is an %s, which has %s/%s "
2050 "panels\n",
2051 mod_type_s(type),
2052 pan_type_s(sx_read_module_byte(board, addr,
2053 mc_mods) & 0xf),
2054 pan_type_s(sx_read_module_byte(board, addr,
2055 mc_mods) >> 4));
2056
2057 sx_dprintk(SX_DEBUG_INIT, "CD1400 versions: %x/%x, ASIC "
2058 "version: %x\n",
2059 sx_read_module_byte(board, addr, mc_rev1),
2060 sx_read_module_byte(board, addr, mc_rev2),
2061 sx_read_module_byte(board, addr, mc_mtaasic_rev));
2062
2063 /* The following combinations are illegal: It should theoretically
2064 work, but timing problems make the bus HANG. */
2065
2066 if (mod_compat_type(type) != board->ta_type) {
2067 printk(KERN_ERR "sx: This is an invalid "
2068 "configuration.\nDon't mix TA/MTA/SXDC on the "
2069 "same hostadapter.\n");
2070 chans = 0;
2071 break;
2072 }
2073 if ((IS_EISA_BOARD(board) ||
2074 IS_SI_BOARD(board)) &&
2075 (mod_compat_type(type) == 4)) {
2076 printk(KERN_ERR "sx: This is an invalid "
2077 "configuration.\nDon't use SXDCs on an SI/XIO "
2078 "adapter.\n");
2079 chans = 0;
2080 break;
2081 }
2082#if 0 /* Problem fixed: firmware 3.05 */
2083 if (IS_SX_BOARD(board) && (type == TA8)) {
2084 /* There are some issues with the firmware and the DCD/RTS
2085 lines. It might work if you tie them together or something.
2086 It might also work if you get a newer sx_firmware. Therefore
2087 this is just a warning. */
2088 printk(KERN_WARNING
2089 "sx: The SX host doesn't work too well "
2090 "with the TA8 adapters.\nSpecialix is working on it.\n");
2091 }
2092#endif
2093 }
2094
2095 if (chans) {
2096 if (board->irq > 0) {
2097 /* fixed irq, probably PCI */
2098 if (sx_irqmask & (1 << board->irq)) { /* may we use this irq? */
2099 if (request_irq(board->irq, sx_interrupt,
2100 IRQF_SHARED | IRQF_DISABLED,
2101 "sx", board)) {
2102 printk(KERN_ERR "sx: Cannot allocate "
2103 "irq %d.\n", board->irq);
2104 board->irq = 0;
2105 }
2106 } else
2107 board->irq = 0;
2108 } else if (board->irq < 0 && sx_irqmask) {
2109 /* auto-allocate irq */
2110 int irqnr;
2111 int irqmask = sx_irqmask & (IS_SX_BOARD(board) ?
2112 SX_ISA_IRQ_MASK : SI2_ISA_IRQ_MASK);
2113 for (irqnr = 15; irqnr > 0; irqnr--)
2114 if (irqmask & (1 << irqnr))
2115 if (!request_irq(irqnr, sx_interrupt,
2116 IRQF_SHARED | IRQF_DISABLED,
2117 "sx", board))
2118 break;
2119 if (!irqnr)
2120 printk(KERN_ERR "sx: Cannot allocate IRQ.\n");
2121 board->irq = irqnr;
2122 } else
2123 board->irq = 0;
2124
2125 if (board->irq) {
2126 /* Found a valid interrupt, start up interrupts! */
2127 sx_dprintk(SX_DEBUG_INIT, "Using irq %d.\n",
2128 board->irq);
2129 sx_start_interrupts(board);
2130 board->poll = sx_slowpoll;
2131 board->flags |= SX_IRQ_ALLOCATED;
2132 } else {
2133 /* no irq: setup board for polled operation */
2134 board->poll = sx_poll;
2135 sx_dprintk(SX_DEBUG_INIT, "Using poll-interval %d.\n",
2136 board->poll);
2137 }
2138
2139 /* The timer should be initialized anyway: That way we can
2140 safely del_timer it when the module is unloaded. */
2141 setup_timer(&board->timer, sx_pollfunc, (unsigned long)board);
2142
2143 if (board->poll)
2144 mod_timer(&board->timer, jiffies + board->poll);
2145 } else {
2146 board->irq = 0;
2147 }
2148
2149 board->nports = chans;
2150 sx_dprintk(SX_DEBUG_INIT, "returning %d ports.", board->nports);
2151
2152 func_exit();
2153 return chans;
2154}
2155
2156static void __devinit printheader(void)
2157{
2158 static int header_printed;
2159
2160 if (!header_printed) {
2161 printk(KERN_INFO "Specialix SX driver "
2162 "(C) 1998/1999 R.E.Wolff@BitWizard.nl\n");
2163 printk(KERN_INFO "sx: version " __stringify(SX_VERSION) "\n");
2164 header_printed = 1;
2165 }
2166}
2167
2168static int __devinit probe_sx(struct sx_board *board)
2169{
2170 struct vpd_prom vpdp;
2171 char *p;
2172 int i;
2173
2174 func_enter();
2175
2176 if (!IS_CF_BOARD(board)) {
2177 sx_dprintk(SX_DEBUG_PROBE, "Going to verify vpd prom at %p.\n",
2178 board->base + SX_VPD_ROM);
2179
2180 if (sx_debug & SX_DEBUG_PROBE)
2181 my_hd_io(board->base + SX_VPD_ROM, 0x40);
2182
2183 p = (char *)&vpdp;
2184 for (i = 0; i < sizeof(struct vpd_prom); i++)
2185 *p++ = read_sx_byte(board, SX_VPD_ROM + i * 2);
2186
2187 if (sx_debug & SX_DEBUG_PROBE)
2188 my_hd(&vpdp, 0x20);
2189
2190 sx_dprintk(SX_DEBUG_PROBE, "checking identifier...\n");
2191
2192 if (strncmp(vpdp.identifier, SX_VPD_IDENT_STRING, 16) != 0) {
2193 sx_dprintk(SX_DEBUG_PROBE, "Got non-SX identifier: "
2194 "'%s'\n", vpdp.identifier);
2195 return 0;
2196 }
2197 }
2198
2199 printheader();
2200
2201 if (!IS_CF_BOARD(board)) {
2202 printk(KERN_DEBUG "sx: Found an SX board at %lx\n",
2203 board->hw_base);
2204 printk(KERN_DEBUG "sx: hw_rev: %d, assembly level: %d, "
2205 "uniq ID:%08x, ",
2206 vpdp.hwrev, vpdp.hwass, vpdp.uniqid);
2207 printk("Manufactured: %d/%d\n", 1970 + vpdp.myear, vpdp.mweek);
2208
2209 if ((((vpdp.uniqid >> 24) & SX_UNIQUEID_MASK) !=
2210 SX_PCI_UNIQUEID1) && (((vpdp.uniqid >> 24) &
2211 SX_UNIQUEID_MASK) != SX_ISA_UNIQUEID1)) {
2212 /* This might be a bit harsh. This was the primary
2213 reason the SX/ISA card didn't work at first... */
2214 printk(KERN_ERR "sx: Hmm. Not an SX/PCI or SX/ISA "
2215 "card. Sorry: giving up.\n");
2216 return (0);
2217 }
2218
2219 if (((vpdp.uniqid >> 24) & SX_UNIQUEID_MASK) ==
2220 SX_ISA_UNIQUEID1) {
2221 if (((unsigned long)board->hw_base) & 0x8000) {
2222 printk(KERN_WARNING "sx: Warning: There may be "
2223 "hardware problems with the card at "
2224 "%lx.\n", board->hw_base);
2225 printk(KERN_WARNING "sx: Read sx.txt for more "
2226 "info.\n");
2227 }
2228 }
2229 }
2230
2231 board->nports = -1;
2232
2233 /* This resets the processor, and keeps it off the bus. */
2234 if (!sx_reset(board))
2235 return 0;
2236 sx_dprintk(SX_DEBUG_INIT, "reset the board...\n");
2237
2238 func_exit();
2239 return 1;
2240}
2241
2242#if defined(CONFIG_ISA) || defined(CONFIG_EISA)
2243
2244/* Specialix probes for this card at 32k increments from 640k to 16M.
2245 I consider machines with less than 16M unlikely nowadays, so I'm
2246 not probing above 1Mb. Also, 0xa0000, 0xb0000, are taken by the VGA
2247 card. 0xe0000 and 0xf0000 are taken by the BIOS. That only leaves
2248 0xc0000, 0xc8000, 0xd0000 and 0xd8000 . */
2249
2250static int __devinit probe_si(struct sx_board *board)
2251{
2252 int i;
2253
2254 func_enter();
2255 sx_dprintk(SX_DEBUG_PROBE, "Going to verify SI signature hw %lx at "
2256 "%p.\n", board->hw_base, board->base + SI2_ISA_ID_BASE);
2257
2258 if (sx_debug & SX_DEBUG_PROBE)
2259 my_hd_io(board->base + SI2_ISA_ID_BASE, 0x8);
2260
2261 if (!IS_EISA_BOARD(board)) {
2262 if (IS_SI1_BOARD(board)) {
2263 for (i = 0; i < 8; i++) {
2264 write_sx_byte(board, SI2_ISA_ID_BASE + 7 - i,i);
2265 }
2266 }
2267 for (i = 0; i < 8; i++) {
2268 if ((read_sx_byte(board, SI2_ISA_ID_BASE + 7 - i) & 7)
2269 != i) {
2270 func_exit();
2271 return 0;
2272 }
2273 }
2274 }
2275
2276 /* Now we're pretty much convinced that there is an SI board here,
2277 but to prevent trouble, we'd better double check that we don't
2278 have an SI1 board when we're probing for an SI2 board.... */
2279
2280 write_sx_byte(board, SI2_ISA_ID_BASE, 0x10);
2281 if (IS_SI1_BOARD(board)) {
2282 /* This should be an SI1 board, which has this
2283 location writable... */
2284 if (read_sx_byte(board, SI2_ISA_ID_BASE) != 0x10) {
2285 func_exit();
2286 return 0;
2287 }
2288 } else {
2289 /* This should be an SI2 board, which has the bottom
2290 3 bits non-writable... */
2291 if (read_sx_byte(board, SI2_ISA_ID_BASE) == 0x10) {
2292 func_exit();
2293 return 0;
2294 }
2295 }
2296
2297 /* Now we're pretty much convinced that there is an SI board here,
2298 but to prevent trouble, we'd better double check that we don't
2299 have an SI1 board when we're probing for an SI2 board.... */
2300
2301 write_sx_byte(board, SI2_ISA_ID_BASE, 0x10);
2302 if (IS_SI1_BOARD(board)) {
2303 /* This should be an SI1 board, which has this
2304 location writable... */
2305 if (read_sx_byte(board, SI2_ISA_ID_BASE) != 0x10) {
2306 func_exit();
2307 return 0;
2308 }
2309 } else {
2310 /* This should be an SI2 board, which has the bottom
2311 3 bits non-writable... */
2312 if (read_sx_byte(board, SI2_ISA_ID_BASE) == 0x10) {
2313 func_exit();
2314 return 0;
2315 }
2316 }
2317
2318 printheader();
2319
2320 printk(KERN_DEBUG "sx: Found an SI board at %lx\n", board->hw_base);
2321 /* Compared to the SX boards, it is a complete guess as to what
2322 this card is up to... */
2323
2324 board->nports = -1;
2325
2326 /* This resets the processor, and keeps it off the bus. */
2327 if (!sx_reset(board))
2328 return 0;
2329 sx_dprintk(SX_DEBUG_INIT, "reset the board...\n");
2330
2331 func_exit();
2332 return 1;
2333}
2334#endif
2335
2336static const struct tty_operations sx_ops = {
2337 .break_ctl = sx_break,
2338 .open = sx_open,
2339 .close = gs_close,
2340 .write = gs_write,
2341 .put_char = gs_put_char,
2342 .flush_chars = gs_flush_chars,
2343 .write_room = gs_write_room,
2344 .chars_in_buffer = gs_chars_in_buffer,
2345 .flush_buffer = gs_flush_buffer,
2346 .ioctl = sx_ioctl,
2347 .throttle = sx_throttle,
2348 .unthrottle = sx_unthrottle,
2349 .set_termios = gs_set_termios,
2350 .stop = gs_stop,
2351 .start = gs_start,
2352 .hangup = gs_hangup,
2353 .tiocmget = sx_tiocmget,
2354 .tiocmset = sx_tiocmset,
2355};
2356
2357static const struct tty_port_operations sx_port_ops = {
2358 .carrier_raised = sx_carrier_raised,
2359};
2360
2361static int sx_init_drivers(void)
2362{
2363 int error;
2364
2365 func_enter();
2366
2367 sx_driver = alloc_tty_driver(sx_nports);
2368 if (!sx_driver)
2369 return 1;
2370 sx_driver->owner = THIS_MODULE;
2371 sx_driver->driver_name = "specialix_sx";
2372 sx_driver->name = "ttyX";
2373 sx_driver->major = SX_NORMAL_MAJOR;
2374 sx_driver->type = TTY_DRIVER_TYPE_SERIAL;
2375 sx_driver->subtype = SERIAL_TYPE_NORMAL;
2376 sx_driver->init_termios = tty_std_termios;
2377 sx_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2378 sx_driver->init_termios.c_ispeed = 9600;
2379 sx_driver->init_termios.c_ospeed = 9600;
2380 sx_driver->flags = TTY_DRIVER_REAL_RAW;
2381 tty_set_operations(sx_driver, &sx_ops);
2382
2383 if ((error = tty_register_driver(sx_driver))) {
2384 put_tty_driver(sx_driver);
2385 printk(KERN_ERR "sx: Couldn't register sx driver, error = %d\n",
2386 error);
2387 return 1;
2388 }
2389 func_exit();
2390 return 0;
2391}
2392
2393static int sx_init_portstructs(int nboards, int nports)
2394{
2395 struct sx_board *board;
2396 struct sx_port *port;
2397 int i, j;
2398 int addr, chans;
2399 int portno;
2400
2401 func_enter();
2402
2403 /* Many drivers statically allocate the maximum number of ports
2404 There is no reason not to allocate them dynamically.
2405 Is there? -- REW */
2406 sx_ports = kcalloc(nports, sizeof(struct sx_port), GFP_KERNEL);
2407 if (!sx_ports)
2408 return -ENOMEM;
2409
2410 port = sx_ports;
2411 for (i = 0; i < nboards; i++) {
2412 board = &boards[i];
2413 board->ports = port;
2414 for (j = 0; j < boards[i].nports; j++) {
2415 sx_dprintk(SX_DEBUG_INIT, "initing port %d\n", j);
2416 tty_port_init(&port->gs.port);
2417 port->gs.port.ops = &sx_port_ops;
2418 port->gs.magic = SX_MAGIC;
2419 port->gs.close_delay = HZ / 2;
2420 port->gs.closing_wait = 30 * HZ;
2421 port->board = board;
2422 port->gs.rd = &sx_real_driver;
2423#ifdef NEW_WRITE_LOCKING
2424 port->gs.port_write_mutex = MUTEX;
2425#endif
2426 spin_lock_init(&port->gs.driver_lock);
2427 /*
2428 * Initializing wait queue
2429 */
2430 port++;
2431 }
2432 }
2433
2434 port = sx_ports;
2435 portno = 0;
2436 for (i = 0; i < nboards; i++) {
2437 board = &boards[i];
2438 board->port_base = portno;
2439 /* Possibly the configuration was rejected. */
2440 sx_dprintk(SX_DEBUG_PROBE, "Board has %d channels\n",
2441 board->nports);
2442 if (board->nports <= 0)
2443 continue;
2444 /* XXX byteorder ?? */
2445 for (addr = 0x80; addr != 0;
2446 addr = read_sx_word(board, addr) & 0x7fff) {
2447 chans = sx_read_module_byte(board, addr, mc_type);
2448 sx_dprintk(SX_DEBUG_PROBE, "Module at %x: %d "
2449 "channels\n", addr, chans);
2450 sx_dprintk(SX_DEBUG_PROBE, "Port at");
2451 for (j = 0; j < chans; j++) {
2452 /* The "sx-way" is the way it SHOULD be done.
2453 That way in the future, the firmware may for
2454 example pack the structures a bit more
2455 efficient. Neil tells me it isn't going to
2456 happen anytime soon though. */
2457 if (IS_SX_BOARD(board))
2458 port->ch_base = sx_read_module_word(
2459 board, addr + j * 2,
2460 mc_chan_pointer);
2461 else
2462 port->ch_base = addr + 0x100 + 0x300 *j;
2463
2464 sx_dprintk(SX_DEBUG_PROBE, " %x",
2465 port->ch_base);
2466 port->line = portno++;
2467 port++;
2468 }
2469 sx_dprintk(SX_DEBUG_PROBE, "\n");
2470 }
2471 /* This has to be done earlier. */
2472 /* board->flags |= SX_BOARD_INITIALIZED; */
2473 }
2474
2475 func_exit();
2476 return 0;
2477}
2478
2479static unsigned int sx_find_free_board(void)
2480{
2481 unsigned int i;
2482
2483 for (i = 0; i < SX_NBOARDS; i++)
2484 if (!(boards[i].flags & SX_BOARD_PRESENT))
2485 break;
2486
2487 return i;
2488}
2489
2490static void __exit sx_release_drivers(void)
2491{
2492 func_enter();
2493 tty_unregister_driver(sx_driver);
2494 put_tty_driver(sx_driver);
2495 func_exit();
2496}
2497
2498static void __devexit sx_remove_card(struct sx_board *board,
2499 struct pci_dev *pdev)
2500{
2501 if (board->flags & SX_BOARD_INITIALIZED) {
2502 /* The board should stop messing with us. (actually I mean the
2503 interrupt) */
2504 sx_reset(board);
2505 if ((board->irq) && (board->flags & SX_IRQ_ALLOCATED))
2506 free_irq(board->irq, board);
2507
2508 /* It is safe/allowed to del_timer a non-active timer */
2509 del_timer(&board->timer);
2510 if (pdev) {
2511#ifdef CONFIG_PCI
2512 iounmap(board->base2);
2513 pci_release_region(pdev, IS_CF_BOARD(board) ? 3 : 2);
2514#endif
2515 } else {
2516 iounmap(board->base);
2517 release_region(board->hw_base, board->hw_len);
2518 }
2519
2520 board->flags &= ~(SX_BOARD_INITIALIZED | SX_BOARD_PRESENT);
2521 }
2522}
2523
2524#ifdef CONFIG_EISA
2525
2526static int __devinit sx_eisa_probe(struct device *dev)
2527{
2528 struct eisa_device *edev = to_eisa_device(dev);
2529 struct sx_board *board;
2530 unsigned long eisa_slot = edev->base_addr;
2531 unsigned int i;
2532 int retval = -EIO;
2533
2534 mutex_lock(&sx_boards_lock);
2535 i = sx_find_free_board();
2536 if (i == SX_NBOARDS) {
2537 mutex_unlock(&sx_boards_lock);
2538 goto err;
2539 }
2540 board = &boards[i];
2541 board->flags |= SX_BOARD_PRESENT;
2542 mutex_unlock(&sx_boards_lock);
2543
2544 dev_info(dev, "XIO : Signature found in EISA slot %lu, "
2545 "Product %d Rev %d (REPORT THIS TO LKLM)\n",
2546 eisa_slot >> 12,
2547 inb(eisa_slot + EISA_VENDOR_ID_OFFSET + 2),
2548 inb(eisa_slot + EISA_VENDOR_ID_OFFSET + 3));
2549
2550 board->eisa_base = eisa_slot;
2551 board->flags &= ~SX_BOARD_TYPE;
2552 board->flags |= SI_EISA_BOARD;
2553
2554 board->hw_base = ((inb(eisa_slot + 0xc01) << 8) +
2555 inb(eisa_slot + 0xc00)) << 16;
2556 board->hw_len = SI2_EISA_WINDOW_LEN;
2557 if (!request_region(board->hw_base, board->hw_len, "sx")) {
2558 dev_err(dev, "can't request region\n");
2559 goto err_flag;
2560 }
2561 board->base2 =
2562 board->base = ioremap_nocache(board->hw_base, SI2_EISA_WINDOW_LEN);
2563 if (!board->base) {
2564 dev_err(dev, "can't remap memory\n");
2565 goto err_reg;
2566 }
2567
2568 sx_dprintk(SX_DEBUG_PROBE, "IO hw_base address: %lx\n", board->hw_base);
2569 sx_dprintk(SX_DEBUG_PROBE, "base: %p\n", board->base);
2570 board->irq = inb(eisa_slot + 0xc02) >> 4;
2571 sx_dprintk(SX_DEBUG_PROBE, "IRQ: %d\n", board->irq);
2572
2573 if (!probe_si(board))
2574 goto err_unmap;
2575
2576 dev_set_drvdata(dev, board);
2577
2578 return 0;
2579err_unmap:
2580 iounmap(board->base);
2581err_reg:
2582 release_region(board->hw_base, board->hw_len);
2583err_flag:
2584 board->flags &= ~SX_BOARD_PRESENT;
2585err:
2586 return retval;
2587}
2588
2589static int __devexit sx_eisa_remove(struct device *dev)
2590{
2591 struct sx_board *board = dev_get_drvdata(dev);
2592
2593 sx_remove_card(board, NULL);
2594
2595 return 0;
2596}
2597
2598static struct eisa_device_id sx_eisa_tbl[] = {
2599 { "SLX" },
2600 { "" }
2601};
2602
2603MODULE_DEVICE_TABLE(eisa, sx_eisa_tbl);
2604
2605static struct eisa_driver sx_eisadriver = {
2606 .id_table = sx_eisa_tbl,
2607 .driver = {
2608 .name = "sx",
2609 .probe = sx_eisa_probe,
2610 .remove = __devexit_p(sx_eisa_remove),
2611 }
2612};
2613
2614#endif
2615
2616#ifdef CONFIG_PCI
2617 /********************************************************
2618 * Setting bit 17 in the CNTRL register of the PLX 9050 *
2619 * chip forces a retry on writes while a read is pending.*
2620 * This is to prevent the card locking up on Intel Xeon *
2621 * multiprocessor systems with the NX chipset. -- NV *
2622 ********************************************************/
2623
2624/* Newer cards are produced with this bit set from the configuration
2625 EEprom. As the bit is read/write for the CPU, we can fix it here,
2626 if we detect that it isn't set correctly. -- REW */
2627
2628static void __devinit fix_sx_pci(struct pci_dev *pdev, struct sx_board *board)
2629{
2630 unsigned int hwbase;
2631 void __iomem *rebase;
2632 unsigned int t;
2633
2634#define CNTRL_REG_OFFSET 0x50
2635#define CNTRL_REG_GOODVALUE 0x18260000
2636
2637 pci_read_config_dword(pdev, PCI_BASE_ADDRESS_0, &hwbase);
2638 hwbase &= PCI_BASE_ADDRESS_MEM_MASK;
2639 rebase = ioremap_nocache(hwbase, 0x80);
2640 t = readl(rebase + CNTRL_REG_OFFSET);
2641 if (t != CNTRL_REG_GOODVALUE) {
2642 printk(KERN_DEBUG "sx: performing cntrl reg fix: %08x -> "
2643 "%08x\n", t, CNTRL_REG_GOODVALUE);
2644 writel(CNTRL_REG_GOODVALUE, rebase + CNTRL_REG_OFFSET);
2645 }
2646 iounmap(rebase);
2647}
2648#endif
2649
2650static int __devinit sx_pci_probe(struct pci_dev *pdev,
2651 const struct pci_device_id *ent)
2652{
2653#ifdef CONFIG_PCI
2654 struct sx_board *board;
2655 unsigned int i, reg;
2656 int retval = -EIO;
2657
2658 mutex_lock(&sx_boards_lock);
2659 i = sx_find_free_board();
2660 if (i == SX_NBOARDS) {
2661 mutex_unlock(&sx_boards_lock);
2662 goto err;
2663 }
2664 board = &boards[i];
2665 board->flags |= SX_BOARD_PRESENT;
2666 mutex_unlock(&sx_boards_lock);
2667
2668 retval = pci_enable_device(pdev);
2669 if (retval)
2670 goto err_flag;
2671
2672 board->flags &= ~SX_BOARD_TYPE;
2673 board->flags |= (pdev->subsystem_vendor == 0x200) ? SX_PCI_BOARD :
2674 SX_CFPCI_BOARD;
2675
2676 /* CF boards use base address 3.... */
2677 reg = IS_CF_BOARD(board) ? 3 : 2;
2678 retval = pci_request_region(pdev, reg, "sx");
2679 if (retval) {
2680 dev_err(&pdev->dev, "can't request region\n");
2681 goto err_flag;
2682 }
2683 board->hw_base = pci_resource_start(pdev, reg);
2684 board->base2 =
2685 board->base = ioremap_nocache(board->hw_base, WINDOW_LEN(board));
2686 if (!board->base) {
2687 dev_err(&pdev->dev, "ioremap failed\n");
2688 goto err_reg;
2689 }
2690
2691 /* Most of the stuff on the CF board is offset by 0x18000 .... */
2692 if (IS_CF_BOARD(board))
2693 board->base += 0x18000;
2694
2695 board->irq = pdev->irq;
2696
2697 dev_info(&pdev->dev, "Got a specialix card: %p(%d) %x.\n", board->base,
2698 board->irq, board->flags);
2699
2700 if (!probe_sx(board)) {
2701 retval = -EIO;
2702 goto err_unmap;
2703 }
2704
2705 fix_sx_pci(pdev, board);
2706
2707 pci_set_drvdata(pdev, board);
2708
2709 return 0;
2710err_unmap:
2711 iounmap(board->base2);
2712err_reg:
2713 pci_release_region(pdev, reg);
2714err_flag:
2715 board->flags &= ~SX_BOARD_PRESENT;
2716err:
2717 return retval;
2718#else
2719 return -ENODEV;
2720#endif
2721}
2722
2723static void __devexit sx_pci_remove(struct pci_dev *pdev)
2724{
2725 struct sx_board *board = pci_get_drvdata(pdev);
2726
2727 sx_remove_card(board, pdev);
2728}
2729
2730/* Specialix has a whole bunch of cards with 0x2000 as the device ID. They say
2731 its because the standard requires it. So check for SUBVENDOR_ID. */
2732static struct pci_device_id sx_pci_tbl[] = {
2733 { PCI_VENDOR_ID_SPECIALIX, PCI_DEVICE_ID_SPECIALIX_SX_XIO_IO8,
2734 .subvendor = PCI_ANY_ID, .subdevice = 0x0200 },
2735 { PCI_VENDOR_ID_SPECIALIX, PCI_DEVICE_ID_SPECIALIX_SX_XIO_IO8,
2736 .subvendor = PCI_ANY_ID, .subdevice = 0x0300 },
2737 { 0 }
2738};
2739
2740MODULE_DEVICE_TABLE(pci, sx_pci_tbl);
2741
2742static struct pci_driver sx_pcidriver = {
2743 .name = "sx",
2744 .id_table = sx_pci_tbl,
2745 .probe = sx_pci_probe,
2746 .remove = __devexit_p(sx_pci_remove)
2747};
2748
2749static int __init sx_init(void)
2750{
2751#ifdef CONFIG_EISA
2752 int retval1;
2753#endif
2754#ifdef CONFIG_ISA
2755 struct sx_board *board;
2756 unsigned int i;
2757#endif
2758 unsigned int found = 0;
2759 int retval;
2760
2761 func_enter();
2762 sx_dprintk(SX_DEBUG_INIT, "Initing sx module... (sx_debug=%d)\n",
2763 sx_debug);
2764 if (abs((long)(&sx_debug) - sx_debug) < 0x10000) {
2765 printk(KERN_WARNING "sx: sx_debug is an address, instead of a "
2766 "value. Assuming -1.\n(%p)\n", &sx_debug);
2767 sx_debug = -1;
2768 }
2769
2770 if (misc_register(&sx_fw_device) < 0) {
2771 printk(KERN_ERR "SX: Unable to register firmware loader "
2772 "driver.\n");
2773 return -EIO;
2774 }
2775#ifdef CONFIG_ISA
2776 for (i = 0; i < NR_SX_ADDRS; i++) {
2777 board = &boards[found];
2778 board->hw_base = sx_probe_addrs[i];
2779 board->hw_len = SX_WINDOW_LEN;
2780 if (!request_region(board->hw_base, board->hw_len, "sx"))
2781 continue;
2782 board->base2 =
2783 board->base = ioremap_nocache(board->hw_base, board->hw_len);
2784 if (!board->base)
2785 goto err_sx_reg;
2786 board->flags &= ~SX_BOARD_TYPE;
2787 board->flags |= SX_ISA_BOARD;
2788 board->irq = sx_irqmask ? -1 : 0;
2789
2790 if (probe_sx(board)) {
2791 board->flags |= SX_BOARD_PRESENT;
2792 found++;
2793 } else {
2794 iounmap(board->base);
2795err_sx_reg:
2796 release_region(board->hw_base, board->hw_len);
2797 }
2798 }
2799
2800 for (i = 0; i < NR_SI_ADDRS; i++) {
2801 board = &boards[found];
2802 board->hw_base = si_probe_addrs[i];
2803 board->hw_len = SI2_ISA_WINDOW_LEN;
2804 if (!request_region(board->hw_base, board->hw_len, "sx"))
2805 continue;
2806 board->base2 =
2807 board->base = ioremap_nocache(board->hw_base, board->hw_len);
2808 if (!board->base)
2809 goto err_si_reg;
2810 board->flags &= ~SX_BOARD_TYPE;
2811 board->flags |= SI_ISA_BOARD;
2812 board->irq = sx_irqmask ? -1 : 0;
2813
2814 if (probe_si(board)) {
2815 board->flags |= SX_BOARD_PRESENT;
2816 found++;
2817 } else {
2818 iounmap(board->base);
2819err_si_reg:
2820 release_region(board->hw_base, board->hw_len);
2821 }
2822 }
2823 for (i = 0; i < NR_SI1_ADDRS; i++) {
2824 board = &boards[found];
2825 board->hw_base = si1_probe_addrs[i];
2826 board->hw_len = SI1_ISA_WINDOW_LEN;
2827 if (!request_region(board->hw_base, board->hw_len, "sx"))
2828 continue;
2829 board->base2 =
2830 board->base = ioremap_nocache(board->hw_base, board->hw_len);
2831 if (!board->base)
2832 goto err_si1_reg;
2833 board->flags &= ~SX_BOARD_TYPE;
2834 board->flags |= SI1_ISA_BOARD;
2835 board->irq = sx_irqmask ? -1 : 0;
2836
2837 if (probe_si(board)) {
2838 board->flags |= SX_BOARD_PRESENT;
2839 found++;
2840 } else {
2841 iounmap(board->base);
2842err_si1_reg:
2843 release_region(board->hw_base, board->hw_len);
2844 }
2845 }
2846#endif
2847#ifdef CONFIG_EISA
2848 retval1 = eisa_driver_register(&sx_eisadriver);
2849#endif
2850 retval = pci_register_driver(&sx_pcidriver);
2851
2852 if (found) {
2853 printk(KERN_INFO "sx: total of %d boards detected.\n", found);
2854 retval = 0;
2855 } else if (retval) {
2856#ifdef CONFIG_EISA
2857 retval = retval1;
2858 if (retval1)
2859#endif
2860 misc_deregister(&sx_fw_device);
2861 }
2862
2863 func_exit();
2864 return retval;
2865}
2866
2867static void __exit sx_exit(void)
2868{
2869 int i;
2870
2871 func_enter();
2872#ifdef CONFIG_EISA
2873 eisa_driver_unregister(&sx_eisadriver);
2874#endif
2875 pci_unregister_driver(&sx_pcidriver);
2876
2877 for (i = 0; i < SX_NBOARDS; i++)
2878 sx_remove_card(&boards[i], NULL);
2879
2880 if (misc_deregister(&sx_fw_device) < 0) {
2881 printk(KERN_INFO "sx: couldn't deregister firmware loader "
2882 "device\n");
2883 }
2884 sx_dprintk(SX_DEBUG_CLEANUP, "Cleaning up drivers (%d)\n",
2885 sx_initialized);
2886 if (sx_initialized)
2887 sx_release_drivers();
2888
2889 kfree(sx_ports);
2890 func_exit();
2891}
2892
2893module_init(sx_init);
2894module_exit(sx_exit);
diff --git a/drivers/char/sx.h b/drivers/char/sx.h
deleted file mode 100644
index 87c2defdead7..000000000000
--- a/drivers/char/sx.h
+++ /dev/null
@@ -1,201 +0,0 @@
1
2/*
3 * sx.h
4 *
5 * Copyright (C) 1998/1999 R.E.Wolff@BitWizard.nl
6 *
7 * SX serial driver.
8 * -- Supports SI, XIO and SX host cards.
9 * -- Supports TAs, MTAs and SXDCs.
10 *
11 * Version 1.3 -- March, 1999.
12 *
13 */
14
15#define SX_NBOARDS 4
16#define SX_PORTSPERBOARD 32
17#define SX_NPORTS (SX_NBOARDS * SX_PORTSPERBOARD)
18
19#ifdef __KERNEL__
20
21#define SX_MAGIC 0x12345678
22
23struct sx_port {
24 struct gs_port gs;
25 struct wait_queue *shutdown_wait;
26 int ch_base;
27 int c_dcd;
28 struct sx_board *board;
29 int line;
30 unsigned long locks;
31};
32
33struct sx_board {
34 int magic;
35 void __iomem *base;
36 void __iomem *base2;
37 unsigned long hw_base;
38 resource_size_t hw_len;
39 int eisa_base;
40 int port_base; /* Number of the first port */
41 struct sx_port *ports;
42 int nports;
43 int flags;
44 int irq;
45 int poll;
46 int ta_type;
47 struct timer_list timer;
48 unsigned long locks;
49};
50
51struct vpd_prom {
52 unsigned short id;
53 char hwrev;
54 char hwass;
55 int uniqid;
56 char myear;
57 char mweek;
58 char hw_feature[5];
59 char oem_id;
60 char identifier[16];
61};
62
63#ifndef MOD_RS232DB25MALE
64#define MOD_RS232DB25MALE 0x0a
65#endif
66
67#define SI_ISA_BOARD 0x00000001
68#define SX_ISA_BOARD 0x00000002
69#define SX_PCI_BOARD 0x00000004
70#define SX_CFPCI_BOARD 0x00000008
71#define SX_CFISA_BOARD 0x00000010
72#define SI_EISA_BOARD 0x00000020
73#define SI1_ISA_BOARD 0x00000040
74
75#define SX_BOARD_PRESENT 0x00001000
76#define SX_BOARD_INITIALIZED 0x00002000
77#define SX_IRQ_ALLOCATED 0x00004000
78
79#define SX_BOARD_TYPE 0x000000ff
80
81#define IS_SX_BOARD(board) (board->flags & (SX_PCI_BOARD | SX_CFPCI_BOARD | \
82 SX_ISA_BOARD | SX_CFISA_BOARD))
83
84#define IS_SI_BOARD(board) (board->flags & SI_ISA_BOARD)
85#define IS_SI1_BOARD(board) (board->flags & SI1_ISA_BOARD)
86
87#define IS_EISA_BOARD(board) (board->flags & SI_EISA_BOARD)
88
89#define IS_CF_BOARD(board) (board->flags & (SX_CFISA_BOARD | SX_CFPCI_BOARD))
90
91/* The SI processor clock is required to calculate the cc_int_count register
92 value for the SI cards. */
93#define SI_PROCESSOR_CLOCK 25000000
94
95
96/* port flags */
97/* Make sure these don't clash with gs flags or async flags */
98#define SX_RX_THROTTLE 0x0000001
99
100
101
102#define SX_PORT_TRANSMIT_LOCK 0
103#define SX_BOARD_INTR_LOCK 0
104
105
106
107/* Debug flags. Add these together to get more debug info. */
108
109#define SX_DEBUG_OPEN 0x00000001
110#define SX_DEBUG_SETTING 0x00000002
111#define SX_DEBUG_FLOW 0x00000004
112#define SX_DEBUG_MODEMSIGNALS 0x00000008
113#define SX_DEBUG_TERMIOS 0x00000010
114#define SX_DEBUG_TRANSMIT 0x00000020
115#define SX_DEBUG_RECEIVE 0x00000040
116#define SX_DEBUG_INTERRUPTS 0x00000080
117#define SX_DEBUG_PROBE 0x00000100
118#define SX_DEBUG_INIT 0x00000200
119#define SX_DEBUG_CLEANUP 0x00000400
120#define SX_DEBUG_CLOSE 0x00000800
121#define SX_DEBUG_FIRMWARE 0x00001000
122#define SX_DEBUG_MEMTEST 0x00002000
123
124#define SX_DEBUG_ALL 0xffffffff
125
126
127#define O_OTHER(tty) \
128 ((O_OLCUC(tty)) ||\
129 (O_ONLCR(tty)) ||\
130 (O_OCRNL(tty)) ||\
131 (O_ONOCR(tty)) ||\
132 (O_ONLRET(tty)) ||\
133 (O_OFILL(tty)) ||\
134 (O_OFDEL(tty)) ||\
135 (O_NLDLY(tty)) ||\
136 (O_CRDLY(tty)) ||\
137 (O_TABDLY(tty)) ||\
138 (O_BSDLY(tty)) ||\
139 (O_VTDLY(tty)) ||\
140 (O_FFDLY(tty)))
141
142/* Same for input. */
143#define I_OTHER(tty) \
144 ((I_INLCR(tty)) ||\
145 (I_IGNCR(tty)) ||\
146 (I_ICRNL(tty)) ||\
147 (I_IUCLC(tty)) ||\
148 (L_ISIG(tty)))
149
150#define MOD_TA ( TA>>4)
151#define MOD_MTA (MTA_CD1400>>4)
152#define MOD_SXDC ( SXDC>>4)
153
154
155/* We copy the download code over to the card in chunks of ... bytes */
156#define SX_CHUNK_SIZE 128
157
158#endif /* __KERNEL__ */
159
160
161
162/* Specialix document 6210046-11 page 3 */
163#define SPX(X) (('S'<<24) | ('P' << 16) | (X))
164
165/* Specialix-Linux specific IOCTLS. */
166#define SPXL(X) (SPX(('L' << 8) | (X)))
167
168
169#define SXIO_SET_BOARD SPXL(0x01)
170#define SXIO_GET_TYPE SPXL(0x02)
171#define SXIO_DOWNLOAD SPXL(0x03)
172#define SXIO_INIT SPXL(0x04)
173#define SXIO_SETDEBUG SPXL(0x05)
174#define SXIO_GETDEBUG SPXL(0x06)
175#define SXIO_DO_RAMTEST SPXL(0x07)
176#define SXIO_SETGSDEBUG SPXL(0x08)
177#define SXIO_GETGSDEBUG SPXL(0x09)
178#define SXIO_GETNPORTS SPXL(0x0a)
179
180
181#ifndef SXCTL_MISC_MINOR
182/* Allow others to gather this into "major.h" or something like that */
183#define SXCTL_MISC_MINOR 167
184#endif
185
186#ifndef SX_NORMAL_MAJOR
187/* This allows overriding on the compiler commandline, or in a "major.h"
188 include or something like that */
189#define SX_NORMAL_MAJOR 32
190#define SX_CALLOUT_MAJOR 33
191#endif
192
193
194#define SX_TYPE_SX 0x01
195#define SX_TYPE_SI 0x02
196#define SX_TYPE_CF 0x03
197
198
199#define WINDOW_LEN(board) (IS_CF_BOARD(board)?0x20000:SX_WINDOW_LEN)
200/* Need a #define for ^^^^^^^ !!! */
201
diff --git a/drivers/char/sxboards.h b/drivers/char/sxboards.h
deleted file mode 100644
index 427927dc7dbf..000000000000
--- a/drivers/char/sxboards.h
+++ /dev/null
@@ -1,206 +0,0 @@
1/************************************************************************/
2/* */
3/* Title : SX/SI/XIO Board Hardware Definitions */
4/* */
5/* Author : N.P.Vassallo */
6/* */
7/* Creation : 16th March 1998 */
8/* */
9/* Version : 3.0.0 */
10/* */
11/* Copyright : (c) Specialix International Ltd. 1998 */
12/* */
13/* Description : Prototypes, structures and definitions */
14/* describing the SX/SI/XIO board hardware */
15/* */
16/************************************************************************/
17
18/* History...
19
203.0.0 16/03/98 NPV Creation.
21
22*/
23
24#ifndef _sxboards_h /* If SXBOARDS.H not already defined */
25#define _sxboards_h 1
26
27/*****************************************************************************
28******************************* ******************************
29******************************* Board Types ******************************
30******************************* ******************************
31*****************************************************************************/
32
33/* BUS types... */
34#define BUS_ISA 0
35#define BUS_MCA 1
36#define BUS_EISA 2
37#define BUS_PCI 3
38
39/* Board phases... */
40#define SI1_Z280 1
41#define SI2_Z280 2
42#define SI3_T225 3
43
44/* Board types... */
45#define CARD_TYPE(bus,phase) (bus<<4|phase)
46#define CARD_BUS(type) ((type>>4)&0xF)
47#define CARD_PHASE(type) (type&0xF)
48
49#define TYPE_SI1_ISA CARD_TYPE(BUS_ISA,SI1_Z280)
50#define TYPE_SI2_ISA CARD_TYPE(BUS_ISA,SI2_Z280)
51#define TYPE_SI2_EISA CARD_TYPE(BUS_EISA,SI2_Z280)
52#define TYPE_SI2_PCI CARD_TYPE(BUS_PCI,SI2_Z280)
53
54#define TYPE_SX_ISA CARD_TYPE(BUS_ISA,SI3_T225)
55#define TYPE_SX_PCI CARD_TYPE(BUS_PCI,SI3_T225)
56/*****************************************************************************
57****************************** ******************************
58****************************** Phase 1 Z280 ******************************
59****************************** ******************************
60*****************************************************************************/
61
62/* ISA board details... */
63#define SI1_ISA_WINDOW_LEN 0x10000 /* 64 Kbyte shared memory window */
64//#define SI1_ISA_MEMORY_LEN 0x8000 /* Usable memory - unused define*/
65//#define SI1_ISA_ADDR_LOW 0x0A0000 /* Lowest address = 640 Kbyte */
66//#define SI1_ISA_ADDR_HIGH 0xFF8000 /* Highest address = 16Mbyte - 32Kbyte */
67//#define SI2_ISA_ADDR_STEP SI2_ISA_WINDOW_LEN/* ISA board address step */
68//#define SI2_ISA_IRQ_MASK 0x9800 /* IRQs 15,12,11 */
69
70/* ISA board, register definitions... */
71//#define SI2_ISA_ID_BASE 0x7FF8 /* READ: Board ID string */
72#define SI1_ISA_RESET 0x8000 /* WRITE: Host Reset */
73#define SI1_ISA_RESET_CLEAR 0xc000 /* WRITE: Host Reset clear*/
74#define SI1_ISA_WAIT 0x9000 /* WRITE: Host wait */
75#define SI1_ISA_WAIT_CLEAR 0xd000 /* WRITE: Host wait clear */
76#define SI1_ISA_INTCL 0xa000 /* WRITE: Host Reset */
77#define SI1_ISA_INTCL_CLEAR 0xe000 /* WRITE: Host Reset */
78
79
80/*****************************************************************************
81****************************** ******************************
82****************************** Phase 2 Z280 ******************************
83****************************** ******************************
84*****************************************************************************/
85
86/* ISA board details... */
87#define SI2_ISA_WINDOW_LEN 0x8000 /* 32 Kbyte shared memory window */
88#define SI2_ISA_MEMORY_LEN 0x7FF8 /* Usable memory */
89#define SI2_ISA_ADDR_LOW 0x0A0000 /* Lowest address = 640 Kbyte */
90#define SI2_ISA_ADDR_HIGH 0xFF8000 /* Highest address = 16Mbyte - 32Kbyte */
91#define SI2_ISA_ADDR_STEP SI2_ISA_WINDOW_LEN/* ISA board address step */
92#define SI2_ISA_IRQ_MASK 0x9800 /* IRQs 15,12,11 */
93
94/* ISA board, register definitions... */
95#define SI2_ISA_ID_BASE 0x7FF8 /* READ: Board ID string */
96#define SI2_ISA_RESET SI2_ISA_ID_BASE /* WRITE: Host Reset */
97#define SI2_ISA_IRQ11 (SI2_ISA_ID_BASE+1) /* WRITE: Set IRQ11 */
98#define SI2_ISA_IRQ12 (SI2_ISA_ID_BASE+2) /* WRITE: Set IRQ12 */
99#define SI2_ISA_IRQ15 (SI2_ISA_ID_BASE+3) /* WRITE: Set IRQ15 */
100#define SI2_ISA_IRQSET (SI2_ISA_ID_BASE+4) /* WRITE: Set Host Interrupt */
101#define SI2_ISA_INTCLEAR (SI2_ISA_ID_BASE+5) /* WRITE: Enable Host Interrupt */
102
103#define SI2_ISA_IRQ11_SET 0x10
104#define SI2_ISA_IRQ11_CLEAR 0x00
105#define SI2_ISA_IRQ12_SET 0x10
106#define SI2_ISA_IRQ12_CLEAR 0x00
107#define SI2_ISA_IRQ15_SET 0x10
108#define SI2_ISA_IRQ15_CLEAR 0x00
109#define SI2_ISA_INTCLEAR_SET 0x10
110#define SI2_ISA_INTCLEAR_CLEAR 0x00
111#define SI2_ISA_IRQSET_CLEAR 0x10
112#define SI2_ISA_IRQSET_SET 0x00
113#define SI2_ISA_RESET_SET 0x00
114#define SI2_ISA_RESET_CLEAR 0x10
115
116/* PCI board details... */
117#define SI2_PCI_WINDOW_LEN 0x100000 /* 1 Mbyte memory window */
118
119/* PCI board register definitions... */
120#define SI2_PCI_SET_IRQ 0x40001 /* Set Host Interrupt */
121#define SI2_PCI_RESET 0xC0001 /* Host Reset */
122
123/*****************************************************************************
124****************************** ******************************
125****************************** Phase 3 T225 ******************************
126****************************** ******************************
127*****************************************************************************/
128
129/* General board details... */
130#define SX_WINDOW_LEN 64*1024 /* 64 Kbyte memory window */
131
132/* ISA board details... */
133#define SX_ISA_ADDR_LOW 0x0A0000 /* Lowest address = 640 Kbyte */
134#define SX_ISA_ADDR_HIGH 0xFF8000 /* Highest address = 16Mbyte - 32Kbyte */
135#define SX_ISA_ADDR_STEP SX_WINDOW_LEN /* ISA board address step */
136#define SX_ISA_IRQ_MASK 0x9E00 /* IRQs 15,12,11,10,9 */
137
138/* Hardware register definitions... */
139#define SX_EVENT_STATUS 0x7800 /* READ: T225 Event Status */
140#define SX_EVENT_STROBE 0x7800 /* WRITE: T225 Event Strobe */
141#define SX_EVENT_ENABLE 0x7880 /* WRITE: T225 Event Enable */
142#define SX_VPD_ROM 0x7C00 /* READ: Vital Product Data ROM */
143#define SX_CONFIG 0x7C00 /* WRITE: Host Configuration Register */
144#define SX_IRQ_STATUS 0x7C80 /* READ: Host Interrupt Status */
145#define SX_SET_IRQ 0x7C80 /* WRITE: Set Host Interrupt */
146#define SX_RESET_STATUS 0x7D00 /* READ: Host Reset Status */
147#define SX_RESET 0x7D00 /* WRITE: Host Reset */
148#define SX_RESET_IRQ 0x7D80 /* WRITE: Reset Host Interrupt */
149
150/* SX_VPD_ROM definitions... */
151#define SX_VPD_SLX_ID1 0x00
152#define SX_VPD_SLX_ID2 0x01
153#define SX_VPD_HW_REV 0x02
154#define SX_VPD_HW_ASSEM 0x03
155#define SX_VPD_UNIQUEID4 0x04
156#define SX_VPD_UNIQUEID3 0x05
157#define SX_VPD_UNIQUEID2 0x06
158#define SX_VPD_UNIQUEID1 0x07
159#define SX_VPD_MANU_YEAR 0x08
160#define SX_VPD_MANU_WEEK 0x09
161#define SX_VPD_IDENT 0x10
162#define SX_VPD_IDENT_STRING "JET HOST BY KEV#"
163
164/* SX unique identifiers... */
165#define SX_UNIQUEID_MASK 0xF0
166#define SX_ISA_UNIQUEID1 0x20
167#define SX_PCI_UNIQUEID1 0x50
168
169/* SX_CONFIG definitions... */
170#define SX_CONF_BUSEN 0x02 /* Enable T225 memory and I/O */
171#define SX_CONF_HOSTIRQ 0x04 /* Enable board to host interrupt */
172
173/* SX bootstrap... */
174#define SX_BOOTSTRAP "\x28\x20\x21\x02\x60\x0a"
175#define SX_BOOTSTRAP_SIZE 6
176#define SX_BOOTSTRAP_ADDR (0x8000-SX_BOOTSTRAP_SIZE)
177
178/*****************************************************************************
179********************************** **********************************
180********************************** EISA **********************************
181********************************** **********************************
182*****************************************************************************/
183
184#define SI2_EISA_OFF 0x42
185#define SI2_EISA_VAL 0x01
186#define SI2_EISA_WINDOW_LEN 0x10000
187
188/*****************************************************************************
189*********************************** **********************************
190*********************************** PCI **********************************
191*********************************** **********************************
192*****************************************************************************/
193
194/* General definitions... */
195
196#define SPX_VENDOR_ID 0x11CB /* Assigned by the PCI SIG */
197#define SPX_DEVICE_ID 0x4000 /* SI/XIO boards */
198#define SPX_PLXDEVICE_ID 0x2000 /* SX boards */
199
200#define SPX_SUB_VENDOR_ID SPX_VENDOR_ID /* Same as vendor id */
201#define SI2_SUB_SYS_ID 0x400 /* Phase 2 (Z280) board */
202#define SX_SUB_SYS_ID 0x200 /* Phase 3 (t225) board */
203
204#endif /*_sxboards_h */
205
206/* End of SXBOARDS.H */
diff --git a/drivers/char/sxwindow.h b/drivers/char/sxwindow.h
deleted file mode 100644
index cf01b662aefc..000000000000
--- a/drivers/char/sxwindow.h
+++ /dev/null
@@ -1,393 +0,0 @@
1/************************************************************************/
2/* */
3/* Title : SX Shared Memory Window Structure */
4/* */
5/* Author : N.P.Vassallo */
6/* */
7/* Creation : 16th March 1998 */
8/* */
9/* Version : 3.0.0 */
10/* */
11/* Copyright : (c) Specialix International Ltd. 1998 */
12/* */
13/* Description : Prototypes, structures and definitions */
14/* describing the SX/SI/XIO cards shared */
15/* memory window structure: */
16/* SXCARD */
17/* SXMODULE */
18/* SXCHANNEL */
19/* */
20/************************************************************************/
21
22/* History...
23
243.0.0 16/03/98 NPV Creation. (based on STRUCT.H)
25
26*/
27
28#ifndef _sxwindow_h /* If SXWINDOW.H not already defined */
29#define _sxwindow_h 1
30
31/*****************************************************************************
32*************************** ***************************
33*************************** Common Definitions ***************************
34*************************** ***************************
35*****************************************************************************/
36
37typedef struct _SXCARD *PSXCARD; /* SXCARD structure pointer */
38typedef struct _SXMODULE *PMOD; /* SXMODULE structure pointer */
39typedef struct _SXCHANNEL *PCHAN; /* SXCHANNEL structure pointer */
40
41/*****************************************************************************
42********************************* *********************************
43********************************* SXCARD *********************************
44********************************* *********************************
45*****************************************************************************/
46
47typedef struct _SXCARD
48{
49 BYTE cc_init_status; /* 0x00 Initialisation status */
50 BYTE cc_mem_size; /* 0x01 Size of memory on card */
51 WORD cc_int_count; /* 0x02 Interrupt count */
52 WORD cc_revision; /* 0x04 Download code revision */
53 BYTE cc_isr_count; /* 0x06 Count when ISR is run */
54 BYTE cc_main_count; /* 0x07 Count when main loop is run */
55 WORD cc_int_pending; /* 0x08 Interrupt pending */
56 WORD cc_poll_count; /* 0x0A Count when poll is run */
57 BYTE cc_int_set_count; /* 0x0C Count when host interrupt is set */
58 BYTE cc_rfu[0x80 - 0x0D]; /* 0x0D Pad structure to 128 bytes (0x80) */
59
60} SXCARD;
61
62/* SXCARD.cc_init_status definitions... */
63#define ADAPTERS_FOUND (BYTE)0x01
64#define NO_ADAPTERS_FOUND (BYTE)0xFF
65
66/* SXCARD.cc_mem_size definitions... */
67#define SX_MEMORY_SIZE (BYTE)0x40
68
69/* SXCARD.cc_int_count definitions... */
70#define INT_COUNT_DEFAULT 100 /* Hz */
71
72/*****************************************************************************
73******************************** ********************************
74******************************** SXMODULE ********************************
75******************************** ********************************
76*****************************************************************************/
77
78#define TOP_POINTER(a) ((a)|0x8000) /* Sets top bit of word */
79#define UNTOP_POINTER(a) ((a)&~0x8000) /* Clears top bit of word */
80
81typedef struct _SXMODULE
82{
83 WORD mc_next; /* 0x00 Next module "pointer" (ORed with 0x8000) */
84 BYTE mc_type; /* 0x02 Type of TA in terms of number of channels */
85 BYTE mc_mod_no; /* 0x03 Module number on SI bus cable (0 closest to card) */
86 BYTE mc_dtr; /* 0x04 Private DTR copy (TA only) */
87 BYTE mc_rfu1; /* 0x05 Reserved */
88 WORD mc_uart; /* 0x06 UART base address for this module */
89 BYTE mc_chip; /* 0x08 Chip type / number of ports */
90 BYTE mc_current_uart; /* 0x09 Current uart selected for this module */
91#ifdef DOWNLOAD
92 PCHAN mc_chan_pointer[8]; /* 0x0A Pointer to each channel structure */
93#else
94 WORD mc_chan_pointer[8]; /* 0x0A Define as WORD if not compiling into download */
95#endif
96 WORD mc_rfu2; /* 0x1A Reserved */
97 BYTE mc_opens1; /* 0x1C Number of open ports on first four ports on MTA/SXDC */
98 BYTE mc_opens2; /* 0x1D Number of open ports on second four ports on MTA/SXDC */
99 BYTE mc_mods; /* 0x1E Types of connector module attached to MTA/SXDC */
100 BYTE mc_rev1; /* 0x1F Revision of first CD1400 on MTA/SXDC */
101 BYTE mc_rev2; /* 0x20 Revision of second CD1400 on MTA/SXDC */
102 BYTE mc_mtaasic_rev; /* 0x21 Revision of MTA ASIC 1..4 -> A, B, C, D */
103 BYTE mc_rfu3[0x100 - 0x22]; /* 0x22 Pad structure to 256 bytes (0x100) */
104
105} SXMODULE;
106
107/* SXMODULE.mc_type definitions... */
108#define FOUR_PORTS (BYTE)4
109#define EIGHT_PORTS (BYTE)8
110
111/* SXMODULE.mc_chip definitions... */
112#define CHIP_MASK 0xF0
113#define TA (BYTE)0
114#define TA4 (TA | FOUR_PORTS)
115#define TA8 (TA | EIGHT_PORTS)
116#define TA4_ASIC (BYTE)0x0A
117#define TA8_ASIC (BYTE)0x0B
118#define MTA_CD1400 (BYTE)0x28
119#define SXDC (BYTE)0x48
120
121/* SXMODULE.mc_mods definitions... */
122#define MOD_RS232DB25 0x00 /* RS232 DB25 (socket/plug) */
123#define MOD_RS232RJ45 0x01 /* RS232 RJ45 (shielded/opto-isolated) */
124#define MOD_RESERVED_2 0x02 /* Reserved (RS485) */
125#define MOD_RS422DB25 0x03 /* RS422 DB25 Socket */
126#define MOD_RESERVED_4 0x04 /* Reserved */
127#define MOD_PARALLEL 0x05 /* Parallel */
128#define MOD_RESERVED_6 0x06 /* Reserved (RS423) */
129#define MOD_RESERVED_7 0x07 /* Reserved */
130#define MOD_2_RS232DB25 0x08 /* Rev 2.0 RS232 DB25 (socket/plug) */
131#define MOD_2_RS232RJ45 0x09 /* Rev 2.0 RS232 RJ45 */
132#define MOD_RESERVED_A 0x0A /* Rev 2.0 Reserved */
133#define MOD_2_RS422DB25 0x0B /* Rev 2.0 RS422 DB25 */
134#define MOD_RESERVED_C 0x0C /* Rev 2.0 Reserved */
135#define MOD_2_PARALLEL 0x0D /* Rev 2.0 Parallel */
136#define MOD_RESERVED_E 0x0E /* Rev 2.0 Reserved */
137#define MOD_BLANK 0x0F /* Blank Panel */
138
139/*****************************************************************************
140******************************** *******************************
141******************************** SXCHANNEL *******************************
142******************************** *******************************
143*****************************************************************************/
144
145#define TX_BUFF_OFFSET 0x60 /* Transmit buffer offset in channel structure */
146#define BUFF_POINTER(a) (((a)+TX_BUFF_OFFSET)|0x8000)
147#define UNBUFF_POINTER(a) (jet_channel*)(((a)&~0x8000)-TX_BUFF_OFFSET)
148#define BUFFER_SIZE 256
149#define HIGH_WATER ((BUFFER_SIZE / 4) * 3)
150#define LOW_WATER (BUFFER_SIZE / 4)
151
152typedef struct _SXCHANNEL
153{
154 WORD next_item; /* 0x00 Offset from window base of next channels hi_txbuf (ORred with 0x8000) */
155 WORD addr_uart; /* 0x02 INTERNAL pointer to uart address. Includes FASTPATH bit */
156 WORD module; /* 0x04 Offset from window base of parent SXMODULE structure */
157 BYTE type; /* 0x06 Chip type / number of ports (copy of mc_chip) */
158 BYTE chan_number; /* 0x07 Channel number on the TA/MTA/SXDC */
159 WORD xc_status; /* 0x08 Flow control and I/O status */
160 BYTE hi_rxipos; /* 0x0A Receive buffer input index */
161 BYTE hi_rxopos; /* 0x0B Receive buffer output index */
162 BYTE hi_txopos; /* 0x0C Transmit buffer output index */
163 BYTE hi_txipos; /* 0x0D Transmit buffer input index */
164 BYTE hi_hstat; /* 0x0E Command register */
165 BYTE dtr_bit; /* 0x0F INTERNAL DTR control byte (TA only) */
166 BYTE txon; /* 0x10 INTERNAL copy of hi_txon */
167 BYTE txoff; /* 0x11 INTERNAL copy of hi_txoff */
168 BYTE rxon; /* 0x12 INTERNAL copy of hi_rxon */
169 BYTE rxoff; /* 0x13 INTERNAL copy of hi_rxoff */
170 BYTE hi_mr1; /* 0x14 Mode Register 1 (databits,parity,RTS rx flow)*/
171 BYTE hi_mr2; /* 0x15 Mode Register 2 (stopbits,local,CTS tx flow)*/
172 BYTE hi_csr; /* 0x16 Clock Select Register (baud rate) */
173 BYTE hi_op; /* 0x17 Modem Output Signal */
174 BYTE hi_ip; /* 0x18 Modem Input Signal */
175 BYTE hi_state; /* 0x19 Channel status */
176 BYTE hi_prtcl; /* 0x1A Channel protocol (flow control) */
177 BYTE hi_txon; /* 0x1B Transmit XON character */
178 BYTE hi_txoff; /* 0x1C Transmit XOFF character */
179 BYTE hi_rxon; /* 0x1D Receive XON character */
180 BYTE hi_rxoff; /* 0x1E Receive XOFF character */
181 BYTE close_prev; /* 0x1F INTERNAL channel previously closed flag */
182 BYTE hi_break; /* 0x20 Break and error control */
183 BYTE break_state; /* 0x21 INTERNAL copy of hi_break */
184 BYTE hi_mask; /* 0x22 Mask for received data */
185 BYTE mask; /* 0x23 INTERNAL copy of hi_mask */
186 BYTE mod_type; /* 0x24 MTA/SXDC hardware module type */
187 BYTE ccr_state; /* 0x25 INTERNAL MTA/SXDC state of CCR register */
188 BYTE ip_mask; /* 0x26 Input handshake mask */
189 BYTE hi_parallel; /* 0x27 Parallel port flag */
190 BYTE par_error; /* 0x28 Error code for parallel loopback test */
191 BYTE any_sent; /* 0x29 INTERNAL data sent flag */
192 BYTE asic_txfifo_size; /* 0x2A INTERNAL SXDC transmit FIFO size */
193 BYTE rfu1[2]; /* 0x2B Reserved */
194 BYTE csr; /* 0x2D INTERNAL copy of hi_csr */
195#ifdef DOWNLOAD
196 PCHAN nextp; /* 0x2E Offset from window base of next channel structure */
197#else
198 WORD nextp; /* 0x2E Define as WORD if not compiling into download */
199#endif
200 BYTE prtcl; /* 0x30 INTERNAL copy of hi_prtcl */
201 BYTE mr1; /* 0x31 INTERNAL copy of hi_mr1 */
202 BYTE mr2; /* 0x32 INTERNAL copy of hi_mr2 */
203 BYTE hi_txbaud; /* 0x33 Extended transmit baud rate (SXDC only if((hi_csr&0x0F)==0x0F) */
204 BYTE hi_rxbaud; /* 0x34 Extended receive baud rate (SXDC only if((hi_csr&0xF0)==0xF0) */
205 BYTE txbreak_state; /* 0x35 INTERNAL MTA/SXDC transmit break state */
206 BYTE txbaud; /* 0x36 INTERNAL copy of hi_txbaud */
207 BYTE rxbaud; /* 0x37 INTERNAL copy of hi_rxbaud */
208 WORD err_framing; /* 0x38 Count of receive framing errors */
209 WORD err_parity; /* 0x3A Count of receive parity errors */
210 WORD err_overrun; /* 0x3C Count of receive overrun errors */
211 WORD err_overflow; /* 0x3E Count of receive buffer overflow errors */
212 BYTE rfu2[TX_BUFF_OFFSET - 0x40]; /* 0x40 Reserved until hi_txbuf */
213 BYTE hi_txbuf[BUFFER_SIZE]; /* 0x060 Transmit buffer */
214 BYTE hi_rxbuf[BUFFER_SIZE]; /* 0x160 Receive buffer */
215 BYTE rfu3[0x300 - 0x260]; /* 0x260 Reserved until 768 bytes (0x300) */
216
217} SXCHANNEL;
218
219/* SXCHANNEL.addr_uart definitions... */
220#define FASTPATH 0x1000 /* Set to indicate fast rx/tx processing (TA only) */
221
222/* SXCHANNEL.xc_status definitions... */
223#define X_TANY 0x0001 /* XON is any character (TA only) */
224#define X_TION 0x0001 /* Tx interrupts on (MTA only) */
225#define X_TXEN 0x0002 /* Tx XON/XOFF enabled (TA only) */
226#define X_RTSEN 0x0002 /* RTS FLOW enabled (MTA only) */
227#define X_TXRC 0x0004 /* XOFF received (TA only) */
228#define X_RTSLOW 0x0004 /* RTS dropped (MTA only) */
229#define X_RXEN 0x0008 /* Rx XON/XOFF enabled */
230#define X_ANYXO 0x0010 /* XOFF pending/sent or RTS dropped */
231#define X_RXSE 0x0020 /* Rx XOFF sent */
232#define X_NPEND 0x0040 /* Rx XON pending or XOFF pending */
233#define X_FPEND 0x0080 /* Rx XOFF pending */
234#define C_CRSE 0x0100 /* Carriage return sent (TA only) */
235#define C_TEMR 0x0100 /* Tx empty requested (MTA only) */
236#define C_TEMA 0x0200 /* Tx empty acked (MTA only) */
237#define C_ANYP 0x0200 /* Any protocol bar tx XON/XOFF (TA only) */
238#define C_EN 0x0400 /* Cooking enabled (on MTA means port is also || */
239#define C_HIGH 0x0800 /* Buffer previously hit high water */
240#define C_CTSEN 0x1000 /* CTS automatic flow-control enabled */
241#define C_DCDEN 0x2000 /* DCD/DTR checking enabled */
242#define C_BREAK 0x4000 /* Break detected */
243#define C_RTSEN 0x8000 /* RTS automatic flow control enabled (MTA only) */
244#define C_PARITY 0x8000 /* Parity checking enabled (TA only) */
245
246/* SXCHANNEL.hi_hstat definitions... */
247#define HS_IDLE_OPEN 0x00 /* Channel open state */
248#define HS_LOPEN 0x02 /* Local open command (no modem monitoring) */
249#define HS_MOPEN 0x04 /* Modem open command (wait for DCD signal) */
250#define HS_IDLE_MPEND 0x06 /* Waiting for DCD signal state */
251#define HS_CONFIG 0x08 /* Configuration command */
252#define HS_CLOSE 0x0A /* Close command */
253#define HS_START 0x0C /* Start transmit break command */
254#define HS_STOP 0x0E /* Stop transmit break command */
255#define HS_IDLE_CLOSED 0x10 /* Closed channel state */
256#define HS_IDLE_BREAK 0x12 /* Transmit break state */
257#define HS_FORCE_CLOSED 0x14 /* Force close command */
258#define HS_RESUME 0x16 /* Clear pending XOFF command */
259#define HS_WFLUSH 0x18 /* Flush transmit buffer command */
260#define HS_RFLUSH 0x1A /* Flush receive buffer command */
261#define HS_SUSPEND 0x1C /* Suspend output command (like XOFF received) */
262#define PARALLEL 0x1E /* Parallel port loopback test command (Diagnostics Only) */
263#define ENABLE_RX_INTS 0x20 /* Enable receive interrupts command (Diagnostics Only) */
264#define ENABLE_TX_INTS 0x22 /* Enable transmit interrupts command (Diagnostics Only) */
265#define ENABLE_MDM_INTS 0x24 /* Enable modem interrupts command (Diagnostics Only) */
266#define DISABLE_INTS 0x26 /* Disable interrupts command (Diagnostics Only) */
267
268/* SXCHANNEL.hi_mr1 definitions... */
269#define MR1_BITS 0x03 /* Data bits mask */
270#define MR1_5_BITS 0x00 /* 5 data bits */
271#define MR1_6_BITS 0x01 /* 6 data bits */
272#define MR1_7_BITS 0x02 /* 7 data bits */
273#define MR1_8_BITS 0x03 /* 8 data bits */
274#define MR1_PARITY 0x1C /* Parity mask */
275#define MR1_ODD 0x04 /* Odd parity */
276#define MR1_EVEN 0x00 /* Even parity */
277#define MR1_WITH 0x00 /* Parity enabled */
278#define MR1_FORCE 0x08 /* Force parity */
279#define MR1_NONE 0x10 /* No parity */
280#define MR1_NOPARITY MR1_NONE /* No parity */
281#define MR1_ODDPARITY (MR1_WITH|MR1_ODD) /* Odd parity */
282#define MR1_EVENPARITY (MR1_WITH|MR1_EVEN) /* Even parity */
283#define MR1_MARKPARITY (MR1_FORCE|MR1_ODD) /* Mark parity */
284#define MR1_SPACEPARITY (MR1_FORCE|MR1_EVEN) /* Space parity */
285#define MR1_RTS_RXFLOW 0x80 /* RTS receive flow control */
286
287/* SXCHANNEL.hi_mr2 definitions... */
288#define MR2_STOP 0x0F /* Stop bits mask */
289#define MR2_1_STOP 0x07 /* 1 stop bit */
290#define MR2_2_STOP 0x0F /* 2 stop bits */
291#define MR2_CTS_TXFLOW 0x10 /* CTS transmit flow control */
292#define MR2_RTS_TOGGLE 0x20 /* RTS toggle on transmit */
293#define MR2_NORMAL 0x00 /* Normal mode */
294#define MR2_AUTO 0x40 /* Auto-echo mode (TA only) */
295#define MR2_LOCAL 0x80 /* Local echo mode */
296#define MR2_REMOTE 0xC0 /* Remote echo mode (TA only) */
297
298/* SXCHANNEL.hi_csr definitions... */
299#define CSR_75 0x0 /* 75 baud */
300#define CSR_110 0x1 /* 110 baud (TA), 115200 (MTA/SXDC) */
301#define CSR_38400 0x2 /* 38400 baud */
302#define CSR_150 0x3 /* 150 baud */
303#define CSR_300 0x4 /* 300 baud */
304#define CSR_600 0x5 /* 600 baud */
305#define CSR_1200 0x6 /* 1200 baud */
306#define CSR_2000 0x7 /* 2000 baud */
307#define CSR_2400 0x8 /* 2400 baud */
308#define CSR_4800 0x9 /* 4800 baud */
309#define CSR_1800 0xA /* 1800 baud */
310#define CSR_9600 0xB /* 9600 baud */
311#define CSR_19200 0xC /* 19200 baud */
312#define CSR_57600 0xD /* 57600 baud */
313#define CSR_EXTBAUD 0xF /* Extended baud rate (hi_txbaud/hi_rxbaud) */
314
315/* SXCHANNEL.hi_op definitions... */
316#define OP_RTS 0x01 /* RTS modem output signal */
317#define OP_DTR 0x02 /* DTR modem output signal */
318
319/* SXCHANNEL.hi_ip definitions... */
320#define IP_CTS 0x02 /* CTS modem input signal */
321#define IP_DCD 0x04 /* DCD modem input signal */
322#define IP_DSR 0x20 /* DTR modem input signal */
323#define IP_RI 0x40 /* RI modem input signal */
324
325/* SXCHANNEL.hi_state definitions... */
326#define ST_BREAK 0x01 /* Break received (clear with config) */
327#define ST_DCD 0x02 /* DCD signal changed state */
328
329/* SXCHANNEL.hi_prtcl definitions... */
330#define SP_TANY 0x01 /* Transmit XON/XANY (if SP_TXEN enabled) */
331#define SP_TXEN 0x02 /* Transmit XON/XOFF flow control */
332#define SP_CEN 0x04 /* Cooking enabled */
333#define SP_RXEN 0x08 /* Rx XON/XOFF enabled */
334#define SP_DCEN 0x20 /* DCD / DTR check */
335#define SP_DTR_RXFLOW 0x40 /* DTR receive flow control */
336#define SP_PAEN 0x80 /* Parity checking enabled */
337
338/* SXCHANNEL.hi_break definitions... */
339#define BR_IGN 0x01 /* Ignore any received breaks */
340#define BR_INT 0x02 /* Interrupt on received break */
341#define BR_PARMRK 0x04 /* Enable parmrk parity error processing */
342#define BR_PARIGN 0x08 /* Ignore chars with parity errors */
343#define BR_ERRINT 0x80 /* Treat parity/framing/overrun errors as exceptions */
344
345/* SXCHANNEL.par_error definitions.. */
346#define DIAG_IRQ_RX 0x01 /* Indicate serial receive interrupt (diags only) */
347#define DIAG_IRQ_TX 0x02 /* Indicate serial transmit interrupt (diags only) */
348#define DIAG_IRQ_MD 0x04 /* Indicate serial modem interrupt (diags only) */
349
350/* SXCHANNEL.hi_txbaud/hi_rxbaud definitions... (SXDC only) */
351#define BAUD_75 0x00 /* 75 baud */
352#define BAUD_115200 0x01 /* 115200 baud */
353#define BAUD_38400 0x02 /* 38400 baud */
354#define BAUD_150 0x03 /* 150 baud */
355#define BAUD_300 0x04 /* 300 baud */
356#define BAUD_600 0x05 /* 600 baud */
357#define BAUD_1200 0x06 /* 1200 baud */
358#define BAUD_2000 0x07 /* 2000 baud */
359#define BAUD_2400 0x08 /* 2400 baud */
360#define BAUD_4800 0x09 /* 4800 baud */
361#define BAUD_1800 0x0A /* 1800 baud */
362#define BAUD_9600 0x0B /* 9600 baud */
363#define BAUD_19200 0x0C /* 19200 baud */
364#define BAUD_57600 0x0D /* 57600 baud */
365#define BAUD_230400 0x0E /* 230400 baud */
366#define BAUD_460800 0x0F /* 460800 baud */
367#define BAUD_921600 0x10 /* 921600 baud */
368#define BAUD_50 0x11 /* 50 baud */
369#define BAUD_110 0x12 /* 110 baud */
370#define BAUD_134_5 0x13 /* 134.5 baud */
371#define BAUD_200 0x14 /* 200 baud */
372#define BAUD_7200 0x15 /* 7200 baud */
373#define BAUD_56000 0x16 /* 56000 baud */
374#define BAUD_64000 0x17 /* 64000 baud */
375#define BAUD_76800 0x18 /* 76800 baud */
376#define BAUD_128000 0x19 /* 128000 baud */
377#define BAUD_150000 0x1A /* 150000 baud */
378#define BAUD_14400 0x1B /* 14400 baud */
379#define BAUD_256000 0x1C /* 256000 baud */
380#define BAUD_28800 0x1D /* 28800 baud */
381
382/* SXCHANNEL.txbreak_state definiions... */
383#define TXBREAK_OFF 0 /* Not sending break */
384#define TXBREAK_START 1 /* Begin sending break */
385#define TXBREAK_START1 2 /* Begin sending break, part 1 */
386#define TXBREAK_ON 3 /* Sending break */
387#define TXBREAK_STOP 4 /* Stop sending break */
388#define TXBREAK_STOP1 5 /* Stop sending break, part 1 */
389
390#endif /* _sxwindow_h */
391
392/* End of SXWINDOW.H */
393
diff --git a/drivers/char/synclink.c b/drivers/char/synclink.c
deleted file mode 100644
index a2a58004e188..000000000000
--- a/drivers/char/synclink.c
+++ /dev/null
@@ -1,8125 +0,0 @@
1/*
2 * linux/drivers/char/synclink.c
3 *
4 * $Id: synclink.c,v 4.38 2005/11/07 16:30:34 paulkf Exp $
5 *
6 * Device driver for Microgate SyncLink ISA and PCI
7 * high speed multiprotocol serial adapters.
8 *
9 * written by Paul Fulghum for Microgate Corporation
10 * paulkf@microgate.com
11 *
12 * Microgate and SyncLink are trademarks of Microgate Corporation
13 *
14 * Derived from serial.c written by Theodore Ts'o and Linus Torvalds
15 *
16 * Original release 01/11/99
17 *
18 * This code is released under the GNU General Public License (GPL)
19 *
20 * This driver is primarily intended for use in synchronous
21 * HDLC mode. Asynchronous mode is also provided.
22 *
23 * When operating in synchronous mode, each call to mgsl_write()
24 * contains exactly one complete HDLC frame. Calling mgsl_put_char
25 * will start assembling an HDLC frame that will not be sent until
26 * mgsl_flush_chars or mgsl_write is called.
27 *
28 * Synchronous receive data is reported as complete frames. To accomplish
29 * this, the TTY flip buffer is bypassed (too small to hold largest
30 * frame and may fragment frames) and the line discipline
31 * receive entry point is called directly.
32 *
33 * This driver has been tested with a slightly modified ppp.c driver
34 * for synchronous PPP.
35 *
36 * 2000/02/16
37 * Added interface for syncppp.c driver (an alternate synchronous PPP
38 * implementation that also supports Cisco HDLC). Each device instance
39 * registers as a tty device AND a network device (if dosyncppp option
40 * is set for the device). The functionality is determined by which
41 * device interface is opened.
42 *
43 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
44 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
45 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
46 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
47 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
48 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
49 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
51 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
52 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
53 * OF THE POSSIBILITY OF SUCH DAMAGE.
54 */
55
56#if defined(__i386__)
57# define BREAKPOINT() asm(" int $3");
58#else
59# define BREAKPOINT() { }
60#endif
61
62#define MAX_ISA_DEVICES 10
63#define MAX_PCI_DEVICES 10
64#define MAX_TOTAL_DEVICES 20
65
66#include <linux/module.h>
67#include <linux/errno.h>
68#include <linux/signal.h>
69#include <linux/sched.h>
70#include <linux/timer.h>
71#include <linux/interrupt.h>
72#include <linux/pci.h>
73#include <linux/tty.h>
74#include <linux/tty_flip.h>
75#include <linux/serial.h>
76#include <linux/major.h>
77#include <linux/string.h>
78#include <linux/fcntl.h>
79#include <linux/ptrace.h>
80#include <linux/ioport.h>
81#include <linux/mm.h>
82#include <linux/seq_file.h>
83#include <linux/slab.h>
84#include <linux/delay.h>
85#include <linux/netdevice.h>
86#include <linux/vmalloc.h>
87#include <linux/init.h>
88#include <linux/ioctl.h>
89#include <linux/synclink.h>
90
91#include <asm/system.h>
92#include <asm/io.h>
93#include <asm/irq.h>
94#include <asm/dma.h>
95#include <linux/bitops.h>
96#include <asm/types.h>
97#include <linux/termios.h>
98#include <linux/workqueue.h>
99#include <linux/hdlc.h>
100#include <linux/dma-mapping.h>
101
102#if defined(CONFIG_HDLC) || (defined(CONFIG_HDLC_MODULE) && defined(CONFIG_SYNCLINK_MODULE))
103#define SYNCLINK_GENERIC_HDLC 1
104#else
105#define SYNCLINK_GENERIC_HDLC 0
106#endif
107
108#define GET_USER(error,value,addr) error = get_user(value,addr)
109#define COPY_FROM_USER(error,dest,src,size) error = copy_from_user(dest,src,size) ? -EFAULT : 0
110#define PUT_USER(error,value,addr) error = put_user(value,addr)
111#define COPY_TO_USER(error,dest,src,size) error = copy_to_user(dest,src,size) ? -EFAULT : 0
112
113#include <asm/uaccess.h>
114
115#define RCLRVALUE 0xffff
116
117static MGSL_PARAMS default_params = {
118 MGSL_MODE_HDLC, /* unsigned long mode */
119 0, /* unsigned char loopback; */
120 HDLC_FLAG_UNDERRUN_ABORT15, /* unsigned short flags; */
121 HDLC_ENCODING_NRZI_SPACE, /* unsigned char encoding; */
122 0, /* unsigned long clock_speed; */
123 0xff, /* unsigned char addr_filter; */
124 HDLC_CRC_16_CCITT, /* unsigned short crc_type; */
125 HDLC_PREAMBLE_LENGTH_8BITS, /* unsigned char preamble_length; */
126 HDLC_PREAMBLE_PATTERN_NONE, /* unsigned char preamble; */
127 9600, /* unsigned long data_rate; */
128 8, /* unsigned char data_bits; */
129 1, /* unsigned char stop_bits; */
130 ASYNC_PARITY_NONE /* unsigned char parity; */
131};
132
133#define SHARED_MEM_ADDRESS_SIZE 0x40000
134#define BUFFERLISTSIZE 4096
135#define DMABUFFERSIZE 4096
136#define MAXRXFRAMES 7
137
138typedef struct _DMABUFFERENTRY
139{
140 u32 phys_addr; /* 32-bit flat physical address of data buffer */
141 volatile u16 count; /* buffer size/data count */
142 volatile u16 status; /* Control/status field */
143 volatile u16 rcc; /* character count field */
144 u16 reserved; /* padding required by 16C32 */
145 u32 link; /* 32-bit flat link to next buffer entry */
146 char *virt_addr; /* virtual address of data buffer */
147 u32 phys_entry; /* physical address of this buffer entry */
148 dma_addr_t dma_addr;
149} DMABUFFERENTRY, *DMAPBUFFERENTRY;
150
151/* The queue of BH actions to be performed */
152
153#define BH_RECEIVE 1
154#define BH_TRANSMIT 2
155#define BH_STATUS 4
156
157#define IO_PIN_SHUTDOWN_LIMIT 100
158
159struct _input_signal_events {
160 int ri_up;
161 int ri_down;
162 int dsr_up;
163 int dsr_down;
164 int dcd_up;
165 int dcd_down;
166 int cts_up;
167 int cts_down;
168};
169
170/* transmit holding buffer definitions*/
171#define MAX_TX_HOLDING_BUFFERS 5
172struct tx_holding_buffer {
173 int buffer_size;
174 unsigned char * buffer;
175};
176
177
178/*
179 * Device instance data structure
180 */
181
182struct mgsl_struct {
183 int magic;
184 struct tty_port port;
185 int line;
186 int hw_version;
187
188 struct mgsl_icount icount;
189
190 int timeout;
191 int x_char; /* xon/xoff character */
192 u16 read_status_mask;
193 u16 ignore_status_mask;
194 unsigned char *xmit_buf;
195 int xmit_head;
196 int xmit_tail;
197 int xmit_cnt;
198
199 wait_queue_head_t status_event_wait_q;
200 wait_queue_head_t event_wait_q;
201 struct timer_list tx_timer; /* HDLC transmit timeout timer */
202 struct mgsl_struct *next_device; /* device list link */
203
204 spinlock_t irq_spinlock; /* spinlock for synchronizing with ISR */
205 struct work_struct task; /* task structure for scheduling bh */
206
207 u32 EventMask; /* event trigger mask */
208 u32 RecordedEvents; /* pending events */
209
210 u32 max_frame_size; /* as set by device config */
211
212 u32 pending_bh;
213
214 bool bh_running; /* Protection from multiple */
215 int isr_overflow;
216 bool bh_requested;
217
218 int dcd_chkcount; /* check counts to prevent */
219 int cts_chkcount; /* too many IRQs if a signal */
220 int dsr_chkcount; /* is floating */
221 int ri_chkcount;
222
223 char *buffer_list; /* virtual address of Rx & Tx buffer lists */
224 u32 buffer_list_phys;
225 dma_addr_t buffer_list_dma_addr;
226
227 unsigned int rx_buffer_count; /* count of total allocated Rx buffers */
228 DMABUFFERENTRY *rx_buffer_list; /* list of receive buffer entries */
229 unsigned int current_rx_buffer;
230
231 int num_tx_dma_buffers; /* number of tx dma frames required */
232 int tx_dma_buffers_used;
233 unsigned int tx_buffer_count; /* count of total allocated Tx buffers */
234 DMABUFFERENTRY *tx_buffer_list; /* list of transmit buffer entries */
235 int start_tx_dma_buffer; /* tx dma buffer to start tx dma operation */
236 int current_tx_buffer; /* next tx dma buffer to be loaded */
237
238 unsigned char *intermediate_rxbuffer;
239
240 int num_tx_holding_buffers; /* number of tx holding buffer allocated */
241 int get_tx_holding_index; /* next tx holding buffer for adapter to load */
242 int put_tx_holding_index; /* next tx holding buffer to store user request */
243 int tx_holding_count; /* number of tx holding buffers waiting */
244 struct tx_holding_buffer tx_holding_buffers[MAX_TX_HOLDING_BUFFERS];
245
246 bool rx_enabled;
247 bool rx_overflow;
248 bool rx_rcc_underrun;
249
250 bool tx_enabled;
251 bool tx_active;
252 u32 idle_mode;
253
254 u16 cmr_value;
255 u16 tcsr_value;
256
257 char device_name[25]; /* device instance name */
258
259 unsigned int bus_type; /* expansion bus type (ISA,EISA,PCI) */
260 unsigned char bus; /* expansion bus number (zero based) */
261 unsigned char function; /* PCI device number */
262
263 unsigned int io_base; /* base I/O address of adapter */
264 unsigned int io_addr_size; /* size of the I/O address range */
265 bool io_addr_requested; /* true if I/O address requested */
266
267 unsigned int irq_level; /* interrupt level */
268 unsigned long irq_flags;
269 bool irq_requested; /* true if IRQ requested */
270
271 unsigned int dma_level; /* DMA channel */
272 bool dma_requested; /* true if dma channel requested */
273
274 u16 mbre_bit;
275 u16 loopback_bits;
276 u16 usc_idle_mode;
277
278 MGSL_PARAMS params; /* communications parameters */
279
280 unsigned char serial_signals; /* current serial signal states */
281
282 bool irq_occurred; /* for diagnostics use */
283 unsigned int init_error; /* Initialization startup error (DIAGS) */
284 int fDiagnosticsmode; /* Driver in Diagnostic mode? (DIAGS) */
285
286 u32 last_mem_alloc;
287 unsigned char* memory_base; /* shared memory address (PCI only) */
288 u32 phys_memory_base;
289 bool shared_mem_requested;
290
291 unsigned char* lcr_base; /* local config registers (PCI only) */
292 u32 phys_lcr_base;
293 u32 lcr_offset;
294 bool lcr_mem_requested;
295
296 u32 misc_ctrl_value;
297 char flag_buf[MAX_ASYNC_BUFFER_SIZE];
298 char char_buf[MAX_ASYNC_BUFFER_SIZE];
299 bool drop_rts_on_tx_done;
300
301 bool loopmode_insert_requested;
302 bool loopmode_send_done_requested;
303
304 struct _input_signal_events input_signal_events;
305
306 /* generic HDLC device parts */
307 int netcount;
308 spinlock_t netlock;
309
310#if SYNCLINK_GENERIC_HDLC
311 struct net_device *netdev;
312#endif
313};
314
315#define MGSL_MAGIC 0x5401
316
317/*
318 * The size of the serial xmit buffer is 1 page, or 4096 bytes
319 */
320#ifndef SERIAL_XMIT_SIZE
321#define SERIAL_XMIT_SIZE 4096
322#endif
323
324/*
325 * These macros define the offsets used in calculating the
326 * I/O address of the specified USC registers.
327 */
328
329
330#define DCPIN 2 /* Bit 1 of I/O address */
331#define SDPIN 4 /* Bit 2 of I/O address */
332
333#define DCAR 0 /* DMA command/address register */
334#define CCAR SDPIN /* channel command/address register */
335#define DATAREG DCPIN + SDPIN /* serial data register */
336#define MSBONLY 0x41
337#define LSBONLY 0x40
338
339/*
340 * These macros define the register address (ordinal number)
341 * used for writing address/value pairs to the USC.
342 */
343
344#define CMR 0x02 /* Channel mode Register */
345#define CCSR 0x04 /* Channel Command/status Register */
346#define CCR 0x06 /* Channel Control Register */
347#define PSR 0x08 /* Port status Register */
348#define PCR 0x0a /* Port Control Register */
349#define TMDR 0x0c /* Test mode Data Register */
350#define TMCR 0x0e /* Test mode Control Register */
351#define CMCR 0x10 /* Clock mode Control Register */
352#define HCR 0x12 /* Hardware Configuration Register */
353#define IVR 0x14 /* Interrupt Vector Register */
354#define IOCR 0x16 /* Input/Output Control Register */
355#define ICR 0x18 /* Interrupt Control Register */
356#define DCCR 0x1a /* Daisy Chain Control Register */
357#define MISR 0x1c /* Misc Interrupt status Register */
358#define SICR 0x1e /* status Interrupt Control Register */
359#define RDR 0x20 /* Receive Data Register */
360#define RMR 0x22 /* Receive mode Register */
361#define RCSR 0x24 /* Receive Command/status Register */
362#define RICR 0x26 /* Receive Interrupt Control Register */
363#define RSR 0x28 /* Receive Sync Register */
364#define RCLR 0x2a /* Receive count Limit Register */
365#define RCCR 0x2c /* Receive Character count Register */
366#define TC0R 0x2e /* Time Constant 0 Register */
367#define TDR 0x30 /* Transmit Data Register */
368#define TMR 0x32 /* Transmit mode Register */
369#define TCSR 0x34 /* Transmit Command/status Register */
370#define TICR 0x36 /* Transmit Interrupt Control Register */
371#define TSR 0x38 /* Transmit Sync Register */
372#define TCLR 0x3a /* Transmit count Limit Register */
373#define TCCR 0x3c /* Transmit Character count Register */
374#define TC1R 0x3e /* Time Constant 1 Register */
375
376
377/*
378 * MACRO DEFINITIONS FOR DMA REGISTERS
379 */
380
381#define DCR 0x06 /* DMA Control Register (shared) */
382#define DACR 0x08 /* DMA Array count Register (shared) */
383#define BDCR 0x12 /* Burst/Dwell Control Register (shared) */
384#define DIVR 0x14 /* DMA Interrupt Vector Register (shared) */
385#define DICR 0x18 /* DMA Interrupt Control Register (shared) */
386#define CDIR 0x1a /* Clear DMA Interrupt Register (shared) */
387#define SDIR 0x1c /* Set DMA Interrupt Register (shared) */
388
389#define TDMR 0x02 /* Transmit DMA mode Register */
390#define TDIAR 0x1e /* Transmit DMA Interrupt Arm Register */
391#define TBCR 0x2a /* Transmit Byte count Register */
392#define TARL 0x2c /* Transmit Address Register (low) */
393#define TARU 0x2e /* Transmit Address Register (high) */
394#define NTBCR 0x3a /* Next Transmit Byte count Register */
395#define NTARL 0x3c /* Next Transmit Address Register (low) */
396#define NTARU 0x3e /* Next Transmit Address Register (high) */
397
398#define RDMR 0x82 /* Receive DMA mode Register (non-shared) */
399#define RDIAR 0x9e /* Receive DMA Interrupt Arm Register */
400#define RBCR 0xaa /* Receive Byte count Register */
401#define RARL 0xac /* Receive Address Register (low) */
402#define RARU 0xae /* Receive Address Register (high) */
403#define NRBCR 0xba /* Next Receive Byte count Register */
404#define NRARL 0xbc /* Next Receive Address Register (low) */
405#define NRARU 0xbe /* Next Receive Address Register (high) */
406
407
408/*
409 * MACRO DEFINITIONS FOR MODEM STATUS BITS
410 */
411
412#define MODEMSTATUS_DTR 0x80
413#define MODEMSTATUS_DSR 0x40
414#define MODEMSTATUS_RTS 0x20
415#define MODEMSTATUS_CTS 0x10
416#define MODEMSTATUS_RI 0x04
417#define MODEMSTATUS_DCD 0x01
418
419
420/*
421 * Channel Command/Address Register (CCAR) Command Codes
422 */
423
424#define RTCmd_Null 0x0000
425#define RTCmd_ResetHighestIus 0x1000
426#define RTCmd_TriggerChannelLoadDma 0x2000
427#define RTCmd_TriggerRxDma 0x2800
428#define RTCmd_TriggerTxDma 0x3000
429#define RTCmd_TriggerRxAndTxDma 0x3800
430#define RTCmd_PurgeRxFifo 0x4800
431#define RTCmd_PurgeTxFifo 0x5000
432#define RTCmd_PurgeRxAndTxFifo 0x5800
433#define RTCmd_LoadRcc 0x6800
434#define RTCmd_LoadTcc 0x7000
435#define RTCmd_LoadRccAndTcc 0x7800
436#define RTCmd_LoadTC0 0x8800
437#define RTCmd_LoadTC1 0x9000
438#define RTCmd_LoadTC0AndTC1 0x9800
439#define RTCmd_SerialDataLSBFirst 0xa000
440#define RTCmd_SerialDataMSBFirst 0xa800
441#define RTCmd_SelectBigEndian 0xb000
442#define RTCmd_SelectLittleEndian 0xb800
443
444
445/*
446 * DMA Command/Address Register (DCAR) Command Codes
447 */
448
449#define DmaCmd_Null 0x0000
450#define DmaCmd_ResetTxChannel 0x1000
451#define DmaCmd_ResetRxChannel 0x1200
452#define DmaCmd_StartTxChannel 0x2000
453#define DmaCmd_StartRxChannel 0x2200
454#define DmaCmd_ContinueTxChannel 0x3000
455#define DmaCmd_ContinueRxChannel 0x3200
456#define DmaCmd_PauseTxChannel 0x4000
457#define DmaCmd_PauseRxChannel 0x4200
458#define DmaCmd_AbortTxChannel 0x5000
459#define DmaCmd_AbortRxChannel 0x5200
460#define DmaCmd_InitTxChannel 0x7000
461#define DmaCmd_InitRxChannel 0x7200
462#define DmaCmd_ResetHighestDmaIus 0x8000
463#define DmaCmd_ResetAllChannels 0x9000
464#define DmaCmd_StartAllChannels 0xa000
465#define DmaCmd_ContinueAllChannels 0xb000
466#define DmaCmd_PauseAllChannels 0xc000
467#define DmaCmd_AbortAllChannels 0xd000
468#define DmaCmd_InitAllChannels 0xf000
469
470#define TCmd_Null 0x0000
471#define TCmd_ClearTxCRC 0x2000
472#define TCmd_SelectTicrTtsaData 0x4000
473#define TCmd_SelectTicrTxFifostatus 0x5000
474#define TCmd_SelectTicrIntLevel 0x6000
475#define TCmd_SelectTicrdma_level 0x7000
476#define TCmd_SendFrame 0x8000
477#define TCmd_SendAbort 0x9000
478#define TCmd_EnableDleInsertion 0xc000
479#define TCmd_DisableDleInsertion 0xd000
480#define TCmd_ClearEofEom 0xe000
481#define TCmd_SetEofEom 0xf000
482
483#define RCmd_Null 0x0000
484#define RCmd_ClearRxCRC 0x2000
485#define RCmd_EnterHuntmode 0x3000
486#define RCmd_SelectRicrRtsaData 0x4000
487#define RCmd_SelectRicrRxFifostatus 0x5000
488#define RCmd_SelectRicrIntLevel 0x6000
489#define RCmd_SelectRicrdma_level 0x7000
490
491/*
492 * Bits for enabling and disabling IRQs in Interrupt Control Register (ICR)
493 */
494
495#define RECEIVE_STATUS BIT5
496#define RECEIVE_DATA BIT4
497#define TRANSMIT_STATUS BIT3
498#define TRANSMIT_DATA BIT2
499#define IO_PIN BIT1
500#define MISC BIT0
501
502
503/*
504 * Receive status Bits in Receive Command/status Register RCSR
505 */
506
507#define RXSTATUS_SHORT_FRAME BIT8
508#define RXSTATUS_CODE_VIOLATION BIT8
509#define RXSTATUS_EXITED_HUNT BIT7
510#define RXSTATUS_IDLE_RECEIVED BIT6
511#define RXSTATUS_BREAK_RECEIVED BIT5
512#define RXSTATUS_ABORT_RECEIVED BIT5
513#define RXSTATUS_RXBOUND BIT4
514#define RXSTATUS_CRC_ERROR BIT3
515#define RXSTATUS_FRAMING_ERROR BIT3
516#define RXSTATUS_ABORT BIT2
517#define RXSTATUS_PARITY_ERROR BIT2
518#define RXSTATUS_OVERRUN BIT1
519#define RXSTATUS_DATA_AVAILABLE BIT0
520#define RXSTATUS_ALL 0x01f6
521#define usc_UnlatchRxstatusBits(a,b) usc_OutReg( (a), RCSR, (u16)((b) & RXSTATUS_ALL) )
522
523/*
524 * Values for setting transmit idle mode in
525 * Transmit Control/status Register (TCSR)
526 */
527#define IDLEMODE_FLAGS 0x0000
528#define IDLEMODE_ALT_ONE_ZERO 0x0100
529#define IDLEMODE_ZERO 0x0200
530#define IDLEMODE_ONE 0x0300
531#define IDLEMODE_ALT_MARK_SPACE 0x0500
532#define IDLEMODE_SPACE 0x0600
533#define IDLEMODE_MARK 0x0700
534#define IDLEMODE_MASK 0x0700
535
536/*
537 * IUSC revision identifiers
538 */
539#define IUSC_SL1660 0x4d44
540#define IUSC_PRE_SL1660 0x4553
541
542/*
543 * Transmit status Bits in Transmit Command/status Register (TCSR)
544 */
545
546#define TCSR_PRESERVE 0x0F00
547
548#define TCSR_UNDERWAIT BIT11
549#define TXSTATUS_PREAMBLE_SENT BIT7
550#define TXSTATUS_IDLE_SENT BIT6
551#define TXSTATUS_ABORT_SENT BIT5
552#define TXSTATUS_EOF_SENT BIT4
553#define TXSTATUS_EOM_SENT BIT4
554#define TXSTATUS_CRC_SENT BIT3
555#define TXSTATUS_ALL_SENT BIT2
556#define TXSTATUS_UNDERRUN BIT1
557#define TXSTATUS_FIFO_EMPTY BIT0
558#define TXSTATUS_ALL 0x00fa
559#define usc_UnlatchTxstatusBits(a,b) usc_OutReg( (a), TCSR, (u16)((a)->tcsr_value + ((b) & 0x00FF)) )
560
561
562#define MISCSTATUS_RXC_LATCHED BIT15
563#define MISCSTATUS_RXC BIT14
564#define MISCSTATUS_TXC_LATCHED BIT13
565#define MISCSTATUS_TXC BIT12
566#define MISCSTATUS_RI_LATCHED BIT11
567#define MISCSTATUS_RI BIT10
568#define MISCSTATUS_DSR_LATCHED BIT9
569#define MISCSTATUS_DSR BIT8
570#define MISCSTATUS_DCD_LATCHED BIT7
571#define MISCSTATUS_DCD BIT6
572#define MISCSTATUS_CTS_LATCHED BIT5
573#define MISCSTATUS_CTS BIT4
574#define MISCSTATUS_RCC_UNDERRUN BIT3
575#define MISCSTATUS_DPLL_NO_SYNC BIT2
576#define MISCSTATUS_BRG1_ZERO BIT1
577#define MISCSTATUS_BRG0_ZERO BIT0
578
579#define usc_UnlatchIostatusBits(a,b) usc_OutReg((a),MISR,(u16)((b) & 0xaaa0))
580#define usc_UnlatchMiscstatusBits(a,b) usc_OutReg((a),MISR,(u16)((b) & 0x000f))
581
582#define SICR_RXC_ACTIVE BIT15
583#define SICR_RXC_INACTIVE BIT14
584#define SICR_RXC (BIT15+BIT14)
585#define SICR_TXC_ACTIVE BIT13
586#define SICR_TXC_INACTIVE BIT12
587#define SICR_TXC (BIT13+BIT12)
588#define SICR_RI_ACTIVE BIT11
589#define SICR_RI_INACTIVE BIT10
590#define SICR_RI (BIT11+BIT10)
591#define SICR_DSR_ACTIVE BIT9
592#define SICR_DSR_INACTIVE BIT8
593#define SICR_DSR (BIT9+BIT8)
594#define SICR_DCD_ACTIVE BIT7
595#define SICR_DCD_INACTIVE BIT6
596#define SICR_DCD (BIT7+BIT6)
597#define SICR_CTS_ACTIVE BIT5
598#define SICR_CTS_INACTIVE BIT4
599#define SICR_CTS (BIT5+BIT4)
600#define SICR_RCC_UNDERFLOW BIT3
601#define SICR_DPLL_NO_SYNC BIT2
602#define SICR_BRG1_ZERO BIT1
603#define SICR_BRG0_ZERO BIT0
604
605void usc_DisableMasterIrqBit( struct mgsl_struct *info );
606void usc_EnableMasterIrqBit( struct mgsl_struct *info );
607void usc_EnableInterrupts( struct mgsl_struct *info, u16 IrqMask );
608void usc_DisableInterrupts( struct mgsl_struct *info, u16 IrqMask );
609void usc_ClearIrqPendingBits( struct mgsl_struct *info, u16 IrqMask );
610
611#define usc_EnableInterrupts( a, b ) \
612 usc_OutReg( (a), ICR, (u16)((usc_InReg((a),ICR) & 0xff00) + 0xc0 + (b)) )
613
614#define usc_DisableInterrupts( a, b ) \
615 usc_OutReg( (a), ICR, (u16)((usc_InReg((a),ICR) & 0xff00) + 0x80 + (b)) )
616
617#define usc_EnableMasterIrqBit(a) \
618 usc_OutReg( (a), ICR, (u16)((usc_InReg((a),ICR) & 0x0f00) + 0xb000) )
619
620#define usc_DisableMasterIrqBit(a) \
621 usc_OutReg( (a), ICR, (u16)(usc_InReg((a),ICR) & 0x7f00) )
622
623#define usc_ClearIrqPendingBits( a, b ) usc_OutReg( (a), DCCR, 0x40 + (b) )
624
625/*
626 * Transmit status Bits in Transmit Control status Register (TCSR)
627 * and Transmit Interrupt Control Register (TICR) (except BIT2, BIT0)
628 */
629
630#define TXSTATUS_PREAMBLE_SENT BIT7
631#define TXSTATUS_IDLE_SENT BIT6
632#define TXSTATUS_ABORT_SENT BIT5
633#define TXSTATUS_EOF BIT4
634#define TXSTATUS_CRC_SENT BIT3
635#define TXSTATUS_ALL_SENT BIT2
636#define TXSTATUS_UNDERRUN BIT1
637#define TXSTATUS_FIFO_EMPTY BIT0
638
639#define DICR_MASTER BIT15
640#define DICR_TRANSMIT BIT0
641#define DICR_RECEIVE BIT1
642
643#define usc_EnableDmaInterrupts(a,b) \
644 usc_OutDmaReg( (a), DICR, (u16)(usc_InDmaReg((a),DICR) | (b)) )
645
646#define usc_DisableDmaInterrupts(a,b) \
647 usc_OutDmaReg( (a), DICR, (u16)(usc_InDmaReg((a),DICR) & ~(b)) )
648
649#define usc_EnableStatusIrqs(a,b) \
650 usc_OutReg( (a), SICR, (u16)(usc_InReg((a),SICR) | (b)) )
651
652#define usc_DisablestatusIrqs(a,b) \
653 usc_OutReg( (a), SICR, (u16)(usc_InReg((a),SICR) & ~(b)) )
654
655/* Transmit status Bits in Transmit Control status Register (TCSR) */
656/* and Transmit Interrupt Control Register (TICR) (except BIT2, BIT0) */
657
658
659#define DISABLE_UNCONDITIONAL 0
660#define DISABLE_END_OF_FRAME 1
661#define ENABLE_UNCONDITIONAL 2
662#define ENABLE_AUTO_CTS 3
663#define ENABLE_AUTO_DCD 3
664#define usc_EnableTransmitter(a,b) \
665 usc_OutReg( (a), TMR, (u16)((usc_InReg((a),TMR) & 0xfffc) | (b)) )
666#define usc_EnableReceiver(a,b) \
667 usc_OutReg( (a), RMR, (u16)((usc_InReg((a),RMR) & 0xfffc) | (b)) )
668
669static u16 usc_InDmaReg( struct mgsl_struct *info, u16 Port );
670static void usc_OutDmaReg( struct mgsl_struct *info, u16 Port, u16 Value );
671static void usc_DmaCmd( struct mgsl_struct *info, u16 Cmd );
672
673static u16 usc_InReg( struct mgsl_struct *info, u16 Port );
674static void usc_OutReg( struct mgsl_struct *info, u16 Port, u16 Value );
675static void usc_RTCmd( struct mgsl_struct *info, u16 Cmd );
676void usc_RCmd( struct mgsl_struct *info, u16 Cmd );
677void usc_TCmd( struct mgsl_struct *info, u16 Cmd );
678
679#define usc_TCmd(a,b) usc_OutReg((a), TCSR, (u16)((a)->tcsr_value + (b)))
680#define usc_RCmd(a,b) usc_OutReg((a), RCSR, (b))
681
682#define usc_SetTransmitSyncChars(a,s0,s1) usc_OutReg((a), TSR, (u16)(((u16)s0<<8)|(u16)s1))
683
684static void usc_process_rxoverrun_sync( struct mgsl_struct *info );
685static void usc_start_receiver( struct mgsl_struct *info );
686static void usc_stop_receiver( struct mgsl_struct *info );
687
688static void usc_start_transmitter( struct mgsl_struct *info );
689static void usc_stop_transmitter( struct mgsl_struct *info );
690static void usc_set_txidle( struct mgsl_struct *info );
691static void usc_load_txfifo( struct mgsl_struct *info );
692
693static void usc_enable_aux_clock( struct mgsl_struct *info, u32 DataRate );
694static void usc_enable_loopback( struct mgsl_struct *info, int enable );
695
696static void usc_get_serial_signals( struct mgsl_struct *info );
697static void usc_set_serial_signals( struct mgsl_struct *info );
698
699static void usc_reset( struct mgsl_struct *info );
700
701static void usc_set_sync_mode( struct mgsl_struct *info );
702static void usc_set_sdlc_mode( struct mgsl_struct *info );
703static void usc_set_async_mode( struct mgsl_struct *info );
704static void usc_enable_async_clock( struct mgsl_struct *info, u32 DataRate );
705
706static void usc_loopback_frame( struct mgsl_struct *info );
707
708static void mgsl_tx_timeout(unsigned long context);
709
710
711static void usc_loopmode_cancel_transmit( struct mgsl_struct * info );
712static void usc_loopmode_insert_request( struct mgsl_struct * info );
713static int usc_loopmode_active( struct mgsl_struct * info);
714static void usc_loopmode_send_done( struct mgsl_struct * info );
715
716static int mgsl_ioctl_common(struct mgsl_struct *info, unsigned int cmd, unsigned long arg);
717
718#if SYNCLINK_GENERIC_HDLC
719#define dev_to_port(D) (dev_to_hdlc(D)->priv)
720static void hdlcdev_tx_done(struct mgsl_struct *info);
721static void hdlcdev_rx(struct mgsl_struct *info, char *buf, int size);
722static int hdlcdev_init(struct mgsl_struct *info);
723static void hdlcdev_exit(struct mgsl_struct *info);
724#endif
725
726/*
727 * Defines a BUS descriptor value for the PCI adapter
728 * local bus address ranges.
729 */
730
731#define BUS_DESCRIPTOR( WrHold, WrDly, RdDly, Nwdd, Nwad, Nxda, Nrdd, Nrad ) \
732(0x00400020 + \
733((WrHold) << 30) + \
734((WrDly) << 28) + \
735((RdDly) << 26) + \
736((Nwdd) << 20) + \
737((Nwad) << 15) + \
738((Nxda) << 13) + \
739((Nrdd) << 11) + \
740((Nrad) << 6) )
741
742static void mgsl_trace_block(struct mgsl_struct *info,const char* data, int count, int xmit);
743
744/*
745 * Adapter diagnostic routines
746 */
747static bool mgsl_register_test( struct mgsl_struct *info );
748static bool mgsl_irq_test( struct mgsl_struct *info );
749static bool mgsl_dma_test( struct mgsl_struct *info );
750static bool mgsl_memory_test( struct mgsl_struct *info );
751static int mgsl_adapter_test( struct mgsl_struct *info );
752
753/*
754 * device and resource management routines
755 */
756static int mgsl_claim_resources(struct mgsl_struct *info);
757static void mgsl_release_resources(struct mgsl_struct *info);
758static void mgsl_add_device(struct mgsl_struct *info);
759static struct mgsl_struct* mgsl_allocate_device(void);
760
761/*
762 * DMA buffer manupulation functions.
763 */
764static void mgsl_free_rx_frame_buffers( struct mgsl_struct *info, unsigned int StartIndex, unsigned int EndIndex );
765static bool mgsl_get_rx_frame( struct mgsl_struct *info );
766static bool mgsl_get_raw_rx_frame( struct mgsl_struct *info );
767static void mgsl_reset_rx_dma_buffers( struct mgsl_struct *info );
768static void mgsl_reset_tx_dma_buffers( struct mgsl_struct *info );
769static int num_free_tx_dma_buffers(struct mgsl_struct *info);
770static void mgsl_load_tx_dma_buffer( struct mgsl_struct *info, const char *Buffer, unsigned int BufferSize);
771static void mgsl_load_pci_memory(char* TargetPtr, const char* SourcePtr, unsigned short count);
772
773/*
774 * DMA and Shared Memory buffer allocation and formatting
775 */
776static int mgsl_allocate_dma_buffers(struct mgsl_struct *info);
777static void mgsl_free_dma_buffers(struct mgsl_struct *info);
778static int mgsl_alloc_frame_memory(struct mgsl_struct *info, DMABUFFERENTRY *BufferList,int Buffercount);
779static void mgsl_free_frame_memory(struct mgsl_struct *info, DMABUFFERENTRY *BufferList,int Buffercount);
780static int mgsl_alloc_buffer_list_memory(struct mgsl_struct *info);
781static void mgsl_free_buffer_list_memory(struct mgsl_struct *info);
782static int mgsl_alloc_intermediate_rxbuffer_memory(struct mgsl_struct *info);
783static void mgsl_free_intermediate_rxbuffer_memory(struct mgsl_struct *info);
784static int mgsl_alloc_intermediate_txbuffer_memory(struct mgsl_struct *info);
785static void mgsl_free_intermediate_txbuffer_memory(struct mgsl_struct *info);
786static bool load_next_tx_holding_buffer(struct mgsl_struct *info);
787static int save_tx_buffer_request(struct mgsl_struct *info,const char *Buffer, unsigned int BufferSize);
788
789/*
790 * Bottom half interrupt handlers
791 */
792static void mgsl_bh_handler(struct work_struct *work);
793static void mgsl_bh_receive(struct mgsl_struct *info);
794static void mgsl_bh_transmit(struct mgsl_struct *info);
795static void mgsl_bh_status(struct mgsl_struct *info);
796
797/*
798 * Interrupt handler routines and dispatch table.
799 */
800static void mgsl_isr_null( struct mgsl_struct *info );
801static void mgsl_isr_transmit_data( struct mgsl_struct *info );
802static void mgsl_isr_receive_data( struct mgsl_struct *info );
803static void mgsl_isr_receive_status( struct mgsl_struct *info );
804static void mgsl_isr_transmit_status( struct mgsl_struct *info );
805static void mgsl_isr_io_pin( struct mgsl_struct *info );
806static void mgsl_isr_misc( struct mgsl_struct *info );
807static void mgsl_isr_receive_dma( struct mgsl_struct *info );
808static void mgsl_isr_transmit_dma( struct mgsl_struct *info );
809
810typedef void (*isr_dispatch_func)(struct mgsl_struct *);
811
812static isr_dispatch_func UscIsrTable[7] =
813{
814 mgsl_isr_null,
815 mgsl_isr_misc,
816 mgsl_isr_io_pin,
817 mgsl_isr_transmit_data,
818 mgsl_isr_transmit_status,
819 mgsl_isr_receive_data,
820 mgsl_isr_receive_status
821};
822
823/*
824 * ioctl call handlers
825 */
826static int tiocmget(struct tty_struct *tty, struct file *file);
827static int tiocmset(struct tty_struct *tty, struct file *file,
828 unsigned int set, unsigned int clear);
829static int mgsl_get_stats(struct mgsl_struct * info, struct mgsl_icount
830 __user *user_icount);
831static int mgsl_get_params(struct mgsl_struct * info, MGSL_PARAMS __user *user_params);
832static int mgsl_set_params(struct mgsl_struct * info, MGSL_PARAMS __user *new_params);
833static int mgsl_get_txidle(struct mgsl_struct * info, int __user *idle_mode);
834static int mgsl_set_txidle(struct mgsl_struct * info, int idle_mode);
835static int mgsl_txenable(struct mgsl_struct * info, int enable);
836static int mgsl_txabort(struct mgsl_struct * info);
837static int mgsl_rxenable(struct mgsl_struct * info, int enable);
838static int mgsl_wait_event(struct mgsl_struct * info, int __user *mask);
839static int mgsl_loopmode_send_done( struct mgsl_struct * info );
840
841/* set non-zero on successful registration with PCI subsystem */
842static bool pci_registered;
843
844/*
845 * Global linked list of SyncLink devices
846 */
847static struct mgsl_struct *mgsl_device_list;
848static int mgsl_device_count;
849
850/*
851 * Set this param to non-zero to load eax with the
852 * .text section address and breakpoint on module load.
853 * This is useful for use with gdb and add-symbol-file command.
854 */
855static int break_on_load;
856
857/*
858 * Driver major number, defaults to zero to get auto
859 * assigned major number. May be forced as module parameter.
860 */
861static int ttymajor;
862
863/*
864 * Array of user specified options for ISA adapters.
865 */
866static int io[MAX_ISA_DEVICES];
867static int irq[MAX_ISA_DEVICES];
868static int dma[MAX_ISA_DEVICES];
869static int debug_level;
870static int maxframe[MAX_TOTAL_DEVICES];
871static int txdmabufs[MAX_TOTAL_DEVICES];
872static int txholdbufs[MAX_TOTAL_DEVICES];
873
874module_param(break_on_load, bool, 0);
875module_param(ttymajor, int, 0);
876module_param_array(io, int, NULL, 0);
877module_param_array(irq, int, NULL, 0);
878module_param_array(dma, int, NULL, 0);
879module_param(debug_level, int, 0);
880module_param_array(maxframe, int, NULL, 0);
881module_param_array(txdmabufs, int, NULL, 0);
882module_param_array(txholdbufs, int, NULL, 0);
883
884static char *driver_name = "SyncLink serial driver";
885static char *driver_version = "$Revision: 4.38 $";
886
887static int synclink_init_one (struct pci_dev *dev,
888 const struct pci_device_id *ent);
889static void synclink_remove_one (struct pci_dev *dev);
890
891static struct pci_device_id synclink_pci_tbl[] = {
892 { PCI_VENDOR_ID_MICROGATE, PCI_DEVICE_ID_MICROGATE_USC, PCI_ANY_ID, PCI_ANY_ID, },
893 { PCI_VENDOR_ID_MICROGATE, 0x0210, PCI_ANY_ID, PCI_ANY_ID, },
894 { 0, }, /* terminate list */
895};
896MODULE_DEVICE_TABLE(pci, synclink_pci_tbl);
897
898MODULE_LICENSE("GPL");
899
900static struct pci_driver synclink_pci_driver = {
901 .name = "synclink",
902 .id_table = synclink_pci_tbl,
903 .probe = synclink_init_one,
904 .remove = __devexit_p(synclink_remove_one),
905};
906
907static struct tty_driver *serial_driver;
908
909/* number of characters left in xmit buffer before we ask for more */
910#define WAKEUP_CHARS 256
911
912
913static void mgsl_change_params(struct mgsl_struct *info);
914static void mgsl_wait_until_sent(struct tty_struct *tty, int timeout);
915
916/*
917 * 1st function defined in .text section. Calling this function in
918 * init_module() followed by a breakpoint allows a remote debugger
919 * (gdb) to get the .text address for the add-symbol-file command.
920 * This allows remote debugging of dynamically loadable modules.
921 */
922static void* mgsl_get_text_ptr(void)
923{
924 return mgsl_get_text_ptr;
925}
926
927static inline int mgsl_paranoia_check(struct mgsl_struct *info,
928 char *name, const char *routine)
929{
930#ifdef MGSL_PARANOIA_CHECK
931 static const char *badmagic =
932 "Warning: bad magic number for mgsl struct (%s) in %s\n";
933 static const char *badinfo =
934 "Warning: null mgsl_struct for (%s) in %s\n";
935
936 if (!info) {
937 printk(badinfo, name, routine);
938 return 1;
939 }
940 if (info->magic != MGSL_MAGIC) {
941 printk(badmagic, name, routine);
942 return 1;
943 }
944#else
945 if (!info)
946 return 1;
947#endif
948 return 0;
949}
950
951/**
952 * line discipline callback wrappers
953 *
954 * The wrappers maintain line discipline references
955 * while calling into the line discipline.
956 *
957 * ldisc_receive_buf - pass receive data to line discipline
958 */
959
960static void ldisc_receive_buf(struct tty_struct *tty,
961 const __u8 *data, char *flags, int count)
962{
963 struct tty_ldisc *ld;
964 if (!tty)
965 return;
966 ld = tty_ldisc_ref(tty);
967 if (ld) {
968 if (ld->ops->receive_buf)
969 ld->ops->receive_buf(tty, data, flags, count);
970 tty_ldisc_deref(ld);
971 }
972}
973
974/* mgsl_stop() throttle (stop) transmitter
975 *
976 * Arguments: tty pointer to tty info structure
977 * Return Value: None
978 */
979static void mgsl_stop(struct tty_struct *tty)
980{
981 struct mgsl_struct *info = tty->driver_data;
982 unsigned long flags;
983
984 if (mgsl_paranoia_check(info, tty->name, "mgsl_stop"))
985 return;
986
987 if ( debug_level >= DEBUG_LEVEL_INFO )
988 printk("mgsl_stop(%s)\n",info->device_name);
989
990 spin_lock_irqsave(&info->irq_spinlock,flags);
991 if (info->tx_enabled)
992 usc_stop_transmitter(info);
993 spin_unlock_irqrestore(&info->irq_spinlock,flags);
994
995} /* end of mgsl_stop() */
996
997/* mgsl_start() release (start) transmitter
998 *
999 * Arguments: tty pointer to tty info structure
1000 * Return Value: None
1001 */
1002static void mgsl_start(struct tty_struct *tty)
1003{
1004 struct mgsl_struct *info = tty->driver_data;
1005 unsigned long flags;
1006
1007 if (mgsl_paranoia_check(info, tty->name, "mgsl_start"))
1008 return;
1009
1010 if ( debug_level >= DEBUG_LEVEL_INFO )
1011 printk("mgsl_start(%s)\n",info->device_name);
1012
1013 spin_lock_irqsave(&info->irq_spinlock,flags);
1014 if (!info->tx_enabled)
1015 usc_start_transmitter(info);
1016 spin_unlock_irqrestore(&info->irq_spinlock,flags);
1017
1018} /* end of mgsl_start() */
1019
1020/*
1021 * Bottom half work queue access functions
1022 */
1023
1024/* mgsl_bh_action() Return next bottom half action to perform.
1025 * Return Value: BH action code or 0 if nothing to do.
1026 */
1027static int mgsl_bh_action(struct mgsl_struct *info)
1028{
1029 unsigned long flags;
1030 int rc = 0;
1031
1032 spin_lock_irqsave(&info->irq_spinlock,flags);
1033
1034 if (info->pending_bh & BH_RECEIVE) {
1035 info->pending_bh &= ~BH_RECEIVE;
1036 rc = BH_RECEIVE;
1037 } else if (info->pending_bh & BH_TRANSMIT) {
1038 info->pending_bh &= ~BH_TRANSMIT;
1039 rc = BH_TRANSMIT;
1040 } else if (info->pending_bh & BH_STATUS) {
1041 info->pending_bh &= ~BH_STATUS;
1042 rc = BH_STATUS;
1043 }
1044
1045 if (!rc) {
1046 /* Mark BH routine as complete */
1047 info->bh_running = false;
1048 info->bh_requested = false;
1049 }
1050
1051 spin_unlock_irqrestore(&info->irq_spinlock,flags);
1052
1053 return rc;
1054}
1055
1056/*
1057 * Perform bottom half processing of work items queued by ISR.
1058 */
1059static void mgsl_bh_handler(struct work_struct *work)
1060{
1061 struct mgsl_struct *info =
1062 container_of(work, struct mgsl_struct, task);
1063 int action;
1064
1065 if (!info)
1066 return;
1067
1068 if ( debug_level >= DEBUG_LEVEL_BH )
1069 printk( "%s(%d):mgsl_bh_handler(%s) entry\n",
1070 __FILE__,__LINE__,info->device_name);
1071
1072 info->bh_running = true;
1073
1074 while((action = mgsl_bh_action(info)) != 0) {
1075
1076 /* Process work item */
1077 if ( debug_level >= DEBUG_LEVEL_BH )
1078 printk( "%s(%d):mgsl_bh_handler() work item action=%d\n",
1079 __FILE__,__LINE__,action);
1080
1081 switch (action) {
1082
1083 case BH_RECEIVE:
1084 mgsl_bh_receive(info);
1085 break;
1086 case BH_TRANSMIT:
1087 mgsl_bh_transmit(info);
1088 break;
1089 case BH_STATUS:
1090 mgsl_bh_status(info);
1091 break;
1092 default:
1093 /* unknown work item ID */
1094 printk("Unknown work item ID=%08X!\n", action);
1095 break;
1096 }
1097 }
1098
1099 if ( debug_level >= DEBUG_LEVEL_BH )
1100 printk( "%s(%d):mgsl_bh_handler(%s) exit\n",
1101 __FILE__,__LINE__,info->device_name);
1102}
1103
1104static void mgsl_bh_receive(struct mgsl_struct *info)
1105{
1106 bool (*get_rx_frame)(struct mgsl_struct *info) =
1107 (info->params.mode == MGSL_MODE_HDLC ? mgsl_get_rx_frame : mgsl_get_raw_rx_frame);
1108
1109 if ( debug_level >= DEBUG_LEVEL_BH )
1110 printk( "%s(%d):mgsl_bh_receive(%s)\n",
1111 __FILE__,__LINE__,info->device_name);
1112
1113 do
1114 {
1115 if (info->rx_rcc_underrun) {
1116 unsigned long flags;
1117 spin_lock_irqsave(&info->irq_spinlock,flags);
1118 usc_start_receiver(info);
1119 spin_unlock_irqrestore(&info->irq_spinlock,flags);
1120 return;
1121 }
1122 } while(get_rx_frame(info));
1123}
1124
1125static void mgsl_bh_transmit(struct mgsl_struct *info)
1126{
1127 struct tty_struct *tty = info->port.tty;
1128 unsigned long flags;
1129
1130 if ( debug_level >= DEBUG_LEVEL_BH )
1131 printk( "%s(%d):mgsl_bh_transmit() entry on %s\n",
1132 __FILE__,__LINE__,info->device_name);
1133
1134 if (tty)
1135 tty_wakeup(tty);
1136
1137 /* if transmitter idle and loopmode_send_done_requested
1138 * then start echoing RxD to TxD
1139 */
1140 spin_lock_irqsave(&info->irq_spinlock,flags);
1141 if ( !info->tx_active && info->loopmode_send_done_requested )
1142 usc_loopmode_send_done( info );
1143 spin_unlock_irqrestore(&info->irq_spinlock,flags);
1144}
1145
1146static void mgsl_bh_status(struct mgsl_struct *info)
1147{
1148 if ( debug_level >= DEBUG_LEVEL_BH )
1149 printk( "%s(%d):mgsl_bh_status() entry on %s\n",
1150 __FILE__,__LINE__,info->device_name);
1151
1152 info->ri_chkcount = 0;
1153 info->dsr_chkcount = 0;
1154 info->dcd_chkcount = 0;
1155 info->cts_chkcount = 0;
1156}
1157
1158/* mgsl_isr_receive_status()
1159 *
1160 * Service a receive status interrupt. The type of status
1161 * interrupt is indicated by the state of the RCSR.
1162 * This is only used for HDLC mode.
1163 *
1164 * Arguments: info pointer to device instance data
1165 * Return Value: None
1166 */
1167static void mgsl_isr_receive_status( struct mgsl_struct *info )
1168{
1169 u16 status = usc_InReg( info, RCSR );
1170
1171 if ( debug_level >= DEBUG_LEVEL_ISR )
1172 printk("%s(%d):mgsl_isr_receive_status status=%04X\n",
1173 __FILE__,__LINE__,status);
1174
1175 if ( (status & RXSTATUS_ABORT_RECEIVED) &&
1176 info->loopmode_insert_requested &&
1177 usc_loopmode_active(info) )
1178 {
1179 ++info->icount.rxabort;
1180 info->loopmode_insert_requested = false;
1181
1182 /* clear CMR:13 to start echoing RxD to TxD */
1183 info->cmr_value &= ~BIT13;
1184 usc_OutReg(info, CMR, info->cmr_value);
1185
1186 /* disable received abort irq (no longer required) */
1187 usc_OutReg(info, RICR,
1188 (usc_InReg(info, RICR) & ~RXSTATUS_ABORT_RECEIVED));
1189 }
1190
1191 if (status & (RXSTATUS_EXITED_HUNT + RXSTATUS_IDLE_RECEIVED)) {
1192 if (status & RXSTATUS_EXITED_HUNT)
1193 info->icount.exithunt++;
1194 if (status & RXSTATUS_IDLE_RECEIVED)
1195 info->icount.rxidle++;
1196 wake_up_interruptible(&info->event_wait_q);
1197 }
1198
1199 if (status & RXSTATUS_OVERRUN){
1200 info->icount.rxover++;
1201 usc_process_rxoverrun_sync( info );
1202 }
1203
1204 usc_ClearIrqPendingBits( info, RECEIVE_STATUS );
1205 usc_UnlatchRxstatusBits( info, status );
1206
1207} /* end of mgsl_isr_receive_status() */
1208
1209/* mgsl_isr_transmit_status()
1210 *
1211 * Service a transmit status interrupt
1212 * HDLC mode :end of transmit frame
1213 * Async mode:all data is sent
1214 * transmit status is indicated by bits in the TCSR.
1215 *
1216 * Arguments: info pointer to device instance data
1217 * Return Value: None
1218 */
1219static void mgsl_isr_transmit_status( struct mgsl_struct *info )
1220{
1221 u16 status = usc_InReg( info, TCSR );
1222
1223 if ( debug_level >= DEBUG_LEVEL_ISR )
1224 printk("%s(%d):mgsl_isr_transmit_status status=%04X\n",
1225 __FILE__,__LINE__,status);
1226
1227 usc_ClearIrqPendingBits( info, TRANSMIT_STATUS );
1228 usc_UnlatchTxstatusBits( info, status );
1229
1230 if ( status & (TXSTATUS_UNDERRUN | TXSTATUS_ABORT_SENT) )
1231 {
1232 /* finished sending HDLC abort. This may leave */
1233 /* the TxFifo with data from the aborted frame */
1234 /* so purge the TxFifo. Also shutdown the DMA */
1235 /* channel in case there is data remaining in */
1236 /* the DMA buffer */
1237 usc_DmaCmd( info, DmaCmd_ResetTxChannel );
1238 usc_RTCmd( info, RTCmd_PurgeTxFifo );
1239 }
1240
1241 if ( status & TXSTATUS_EOF_SENT )
1242 info->icount.txok++;
1243 else if ( status & TXSTATUS_UNDERRUN )
1244 info->icount.txunder++;
1245 else if ( status & TXSTATUS_ABORT_SENT )
1246 info->icount.txabort++;
1247 else
1248 info->icount.txunder++;
1249
1250 info->tx_active = false;
1251 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1252 del_timer(&info->tx_timer);
1253
1254 if ( info->drop_rts_on_tx_done ) {
1255 usc_get_serial_signals( info );
1256 if ( info->serial_signals & SerialSignal_RTS ) {
1257 info->serial_signals &= ~SerialSignal_RTS;
1258 usc_set_serial_signals( info );
1259 }
1260 info->drop_rts_on_tx_done = false;
1261 }
1262
1263#if SYNCLINK_GENERIC_HDLC
1264 if (info->netcount)
1265 hdlcdev_tx_done(info);
1266 else
1267#endif
1268 {
1269 if (info->port.tty->stopped || info->port.tty->hw_stopped) {
1270 usc_stop_transmitter(info);
1271 return;
1272 }
1273 info->pending_bh |= BH_TRANSMIT;
1274 }
1275
1276} /* end of mgsl_isr_transmit_status() */
1277
1278/* mgsl_isr_io_pin()
1279 *
1280 * Service an Input/Output pin interrupt. The type of
1281 * interrupt is indicated by bits in the MISR
1282 *
1283 * Arguments: info pointer to device instance data
1284 * Return Value: None
1285 */
1286static void mgsl_isr_io_pin( struct mgsl_struct *info )
1287{
1288 struct mgsl_icount *icount;
1289 u16 status = usc_InReg( info, MISR );
1290
1291 if ( debug_level >= DEBUG_LEVEL_ISR )
1292 printk("%s(%d):mgsl_isr_io_pin status=%04X\n",
1293 __FILE__,__LINE__,status);
1294
1295 usc_ClearIrqPendingBits( info, IO_PIN );
1296 usc_UnlatchIostatusBits( info, status );
1297
1298 if (status & (MISCSTATUS_CTS_LATCHED | MISCSTATUS_DCD_LATCHED |
1299 MISCSTATUS_DSR_LATCHED | MISCSTATUS_RI_LATCHED) ) {
1300 icount = &info->icount;
1301 /* update input line counters */
1302 if (status & MISCSTATUS_RI_LATCHED) {
1303 if ((info->ri_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
1304 usc_DisablestatusIrqs(info,SICR_RI);
1305 icount->rng++;
1306 if ( status & MISCSTATUS_RI )
1307 info->input_signal_events.ri_up++;
1308 else
1309 info->input_signal_events.ri_down++;
1310 }
1311 if (status & MISCSTATUS_DSR_LATCHED) {
1312 if ((info->dsr_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
1313 usc_DisablestatusIrqs(info,SICR_DSR);
1314 icount->dsr++;
1315 if ( status & MISCSTATUS_DSR )
1316 info->input_signal_events.dsr_up++;
1317 else
1318 info->input_signal_events.dsr_down++;
1319 }
1320 if (status & MISCSTATUS_DCD_LATCHED) {
1321 if ((info->dcd_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
1322 usc_DisablestatusIrqs(info,SICR_DCD);
1323 icount->dcd++;
1324 if (status & MISCSTATUS_DCD) {
1325 info->input_signal_events.dcd_up++;
1326 } else
1327 info->input_signal_events.dcd_down++;
1328#if SYNCLINK_GENERIC_HDLC
1329 if (info->netcount) {
1330 if (status & MISCSTATUS_DCD)
1331 netif_carrier_on(info->netdev);
1332 else
1333 netif_carrier_off(info->netdev);
1334 }
1335#endif
1336 }
1337 if (status & MISCSTATUS_CTS_LATCHED)
1338 {
1339 if ((info->cts_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
1340 usc_DisablestatusIrqs(info,SICR_CTS);
1341 icount->cts++;
1342 if ( status & MISCSTATUS_CTS )
1343 info->input_signal_events.cts_up++;
1344 else
1345 info->input_signal_events.cts_down++;
1346 }
1347 wake_up_interruptible(&info->status_event_wait_q);
1348 wake_up_interruptible(&info->event_wait_q);
1349
1350 if ( (info->port.flags & ASYNC_CHECK_CD) &&
1351 (status & MISCSTATUS_DCD_LATCHED) ) {
1352 if ( debug_level >= DEBUG_LEVEL_ISR )
1353 printk("%s CD now %s...", info->device_name,
1354 (status & MISCSTATUS_DCD) ? "on" : "off");
1355 if (status & MISCSTATUS_DCD)
1356 wake_up_interruptible(&info->port.open_wait);
1357 else {
1358 if ( debug_level >= DEBUG_LEVEL_ISR )
1359 printk("doing serial hangup...");
1360 if (info->port.tty)
1361 tty_hangup(info->port.tty);
1362 }
1363 }
1364
1365 if ( (info->port.flags & ASYNC_CTS_FLOW) &&
1366 (status & MISCSTATUS_CTS_LATCHED) ) {
1367 if (info->port.tty->hw_stopped) {
1368 if (status & MISCSTATUS_CTS) {
1369 if ( debug_level >= DEBUG_LEVEL_ISR )
1370 printk("CTS tx start...");
1371 if (info->port.tty)
1372 info->port.tty->hw_stopped = 0;
1373 usc_start_transmitter(info);
1374 info->pending_bh |= BH_TRANSMIT;
1375 return;
1376 }
1377 } else {
1378 if (!(status & MISCSTATUS_CTS)) {
1379 if ( debug_level >= DEBUG_LEVEL_ISR )
1380 printk("CTS tx stop...");
1381 if (info->port.tty)
1382 info->port.tty->hw_stopped = 1;
1383 usc_stop_transmitter(info);
1384 }
1385 }
1386 }
1387 }
1388
1389 info->pending_bh |= BH_STATUS;
1390
1391 /* for diagnostics set IRQ flag */
1392 if ( status & MISCSTATUS_TXC_LATCHED ){
1393 usc_OutReg( info, SICR,
1394 (unsigned short)(usc_InReg(info,SICR) & ~(SICR_TXC_ACTIVE+SICR_TXC_INACTIVE)) );
1395 usc_UnlatchIostatusBits( info, MISCSTATUS_TXC_LATCHED );
1396 info->irq_occurred = true;
1397 }
1398
1399} /* end of mgsl_isr_io_pin() */
1400
1401/* mgsl_isr_transmit_data()
1402 *
1403 * Service a transmit data interrupt (async mode only).
1404 *
1405 * Arguments: info pointer to device instance data
1406 * Return Value: None
1407 */
1408static void mgsl_isr_transmit_data( struct mgsl_struct *info )
1409{
1410 if ( debug_level >= DEBUG_LEVEL_ISR )
1411 printk("%s(%d):mgsl_isr_transmit_data xmit_cnt=%d\n",
1412 __FILE__,__LINE__,info->xmit_cnt);
1413
1414 usc_ClearIrqPendingBits( info, TRANSMIT_DATA );
1415
1416 if (info->port.tty->stopped || info->port.tty->hw_stopped) {
1417 usc_stop_transmitter(info);
1418 return;
1419 }
1420
1421 if ( info->xmit_cnt )
1422 usc_load_txfifo( info );
1423 else
1424 info->tx_active = false;
1425
1426 if (info->xmit_cnt < WAKEUP_CHARS)
1427 info->pending_bh |= BH_TRANSMIT;
1428
1429} /* end of mgsl_isr_transmit_data() */
1430
1431/* mgsl_isr_receive_data()
1432 *
1433 * Service a receive data interrupt. This occurs
1434 * when operating in asynchronous interrupt transfer mode.
1435 * The receive data FIFO is flushed to the receive data buffers.
1436 *
1437 * Arguments: info pointer to device instance data
1438 * Return Value: None
1439 */
1440static void mgsl_isr_receive_data( struct mgsl_struct *info )
1441{
1442 int Fifocount;
1443 u16 status;
1444 int work = 0;
1445 unsigned char DataByte;
1446 struct tty_struct *tty = info->port.tty;
1447 struct mgsl_icount *icount = &info->icount;
1448
1449 if ( debug_level >= DEBUG_LEVEL_ISR )
1450 printk("%s(%d):mgsl_isr_receive_data\n",
1451 __FILE__,__LINE__);
1452
1453 usc_ClearIrqPendingBits( info, RECEIVE_DATA );
1454
1455 /* select FIFO status for RICR readback */
1456 usc_RCmd( info, RCmd_SelectRicrRxFifostatus );
1457
1458 /* clear the Wordstatus bit so that status readback */
1459 /* only reflects the status of this byte */
1460 usc_OutReg( info, RICR+LSBONLY, (u16)(usc_InReg(info, RICR+LSBONLY) & ~BIT3 ));
1461
1462 /* flush the receive FIFO */
1463
1464 while( (Fifocount = (usc_InReg(info,RICR) >> 8)) ) {
1465 int flag;
1466
1467 /* read one byte from RxFIFO */
1468 outw( (inw(info->io_base + CCAR) & 0x0780) | (RDR+LSBONLY),
1469 info->io_base + CCAR );
1470 DataByte = inb( info->io_base + CCAR );
1471
1472 /* get the status of the received byte */
1473 status = usc_InReg(info, RCSR);
1474 if ( status & (RXSTATUS_FRAMING_ERROR + RXSTATUS_PARITY_ERROR +
1475 RXSTATUS_OVERRUN + RXSTATUS_BREAK_RECEIVED) )
1476 usc_UnlatchRxstatusBits(info,RXSTATUS_ALL);
1477
1478 icount->rx++;
1479
1480 flag = 0;
1481 if ( status & (RXSTATUS_FRAMING_ERROR + RXSTATUS_PARITY_ERROR +
1482 RXSTATUS_OVERRUN + RXSTATUS_BREAK_RECEIVED) ) {
1483 printk("rxerr=%04X\n",status);
1484 /* update error statistics */
1485 if ( status & RXSTATUS_BREAK_RECEIVED ) {
1486 status &= ~(RXSTATUS_FRAMING_ERROR + RXSTATUS_PARITY_ERROR);
1487 icount->brk++;
1488 } else if (status & RXSTATUS_PARITY_ERROR)
1489 icount->parity++;
1490 else if (status & RXSTATUS_FRAMING_ERROR)
1491 icount->frame++;
1492 else if (status & RXSTATUS_OVERRUN) {
1493 /* must issue purge fifo cmd before */
1494 /* 16C32 accepts more receive chars */
1495 usc_RTCmd(info,RTCmd_PurgeRxFifo);
1496 icount->overrun++;
1497 }
1498
1499 /* discard char if tty control flags say so */
1500 if (status & info->ignore_status_mask)
1501 continue;
1502
1503 status &= info->read_status_mask;
1504
1505 if (status & RXSTATUS_BREAK_RECEIVED) {
1506 flag = TTY_BREAK;
1507 if (info->port.flags & ASYNC_SAK)
1508 do_SAK(tty);
1509 } else if (status & RXSTATUS_PARITY_ERROR)
1510 flag = TTY_PARITY;
1511 else if (status & RXSTATUS_FRAMING_ERROR)
1512 flag = TTY_FRAME;
1513 } /* end of if (error) */
1514 tty_insert_flip_char(tty, DataByte, flag);
1515 if (status & RXSTATUS_OVERRUN) {
1516 /* Overrun is special, since it's
1517 * reported immediately, and doesn't
1518 * affect the current character
1519 */
1520 work += tty_insert_flip_char(tty, 0, TTY_OVERRUN);
1521 }
1522 }
1523
1524 if ( debug_level >= DEBUG_LEVEL_ISR ) {
1525 printk("%s(%d):rx=%d brk=%d parity=%d frame=%d overrun=%d\n",
1526 __FILE__,__LINE__,icount->rx,icount->brk,
1527 icount->parity,icount->frame,icount->overrun);
1528 }
1529
1530 if(work)
1531 tty_flip_buffer_push(tty);
1532}
1533
1534/* mgsl_isr_misc()
1535 *
1536 * Service a miscellaneous interrupt source.
1537 *
1538 * Arguments: info pointer to device extension (instance data)
1539 * Return Value: None
1540 */
1541static void mgsl_isr_misc( struct mgsl_struct *info )
1542{
1543 u16 status = usc_InReg( info, MISR );
1544
1545 if ( debug_level >= DEBUG_LEVEL_ISR )
1546 printk("%s(%d):mgsl_isr_misc status=%04X\n",
1547 __FILE__,__LINE__,status);
1548
1549 if ((status & MISCSTATUS_RCC_UNDERRUN) &&
1550 (info->params.mode == MGSL_MODE_HDLC)) {
1551
1552 /* turn off receiver and rx DMA */
1553 usc_EnableReceiver(info,DISABLE_UNCONDITIONAL);
1554 usc_DmaCmd(info, DmaCmd_ResetRxChannel);
1555 usc_UnlatchRxstatusBits(info, RXSTATUS_ALL);
1556 usc_ClearIrqPendingBits(info, RECEIVE_DATA + RECEIVE_STATUS);
1557 usc_DisableInterrupts(info, RECEIVE_DATA + RECEIVE_STATUS);
1558
1559 /* schedule BH handler to restart receiver */
1560 info->pending_bh |= BH_RECEIVE;
1561 info->rx_rcc_underrun = true;
1562 }
1563
1564 usc_ClearIrqPendingBits( info, MISC );
1565 usc_UnlatchMiscstatusBits( info, status );
1566
1567} /* end of mgsl_isr_misc() */
1568
1569/* mgsl_isr_null()
1570 *
1571 * Services undefined interrupt vectors from the
1572 * USC. (hence this function SHOULD never be called)
1573 *
1574 * Arguments: info pointer to device extension (instance data)
1575 * Return Value: None
1576 */
1577static void mgsl_isr_null( struct mgsl_struct *info )
1578{
1579
1580} /* end of mgsl_isr_null() */
1581
1582/* mgsl_isr_receive_dma()
1583 *
1584 * Service a receive DMA channel interrupt.
1585 * For this driver there are two sources of receive DMA interrupts
1586 * as identified in the Receive DMA mode Register (RDMR):
1587 *
1588 * BIT3 EOA/EOL End of List, all receive buffers in receive
1589 * buffer list have been filled (no more free buffers
1590 * available). The DMA controller has shut down.
1591 *
1592 * BIT2 EOB End of Buffer. This interrupt occurs when a receive
1593 * DMA buffer is terminated in response to completion
1594 * of a good frame or a frame with errors. The status
1595 * of the frame is stored in the buffer entry in the
1596 * list of receive buffer entries.
1597 *
1598 * Arguments: info pointer to device instance data
1599 * Return Value: None
1600 */
1601static void mgsl_isr_receive_dma( struct mgsl_struct *info )
1602{
1603 u16 status;
1604
1605 /* clear interrupt pending and IUS bit for Rx DMA IRQ */
1606 usc_OutDmaReg( info, CDIR, BIT9+BIT1 );
1607
1608 /* Read the receive DMA status to identify interrupt type. */
1609 /* This also clears the status bits. */
1610 status = usc_InDmaReg( info, RDMR );
1611
1612 if ( debug_level >= DEBUG_LEVEL_ISR )
1613 printk("%s(%d):mgsl_isr_receive_dma(%s) status=%04X\n",
1614 __FILE__,__LINE__,info->device_name,status);
1615
1616 info->pending_bh |= BH_RECEIVE;
1617
1618 if ( status & BIT3 ) {
1619 info->rx_overflow = true;
1620 info->icount.buf_overrun++;
1621 }
1622
1623} /* end of mgsl_isr_receive_dma() */
1624
1625/* mgsl_isr_transmit_dma()
1626 *
1627 * This function services a transmit DMA channel interrupt.
1628 *
1629 * For this driver there is one source of transmit DMA interrupts
1630 * as identified in the Transmit DMA Mode Register (TDMR):
1631 *
1632 * BIT2 EOB End of Buffer. This interrupt occurs when a
1633 * transmit DMA buffer has been emptied.
1634 *
1635 * The driver maintains enough transmit DMA buffers to hold at least
1636 * one max frame size transmit frame. When operating in a buffered
1637 * transmit mode, there may be enough transmit DMA buffers to hold at
1638 * least two or more max frame size frames. On an EOB condition,
1639 * determine if there are any queued transmit buffers and copy into
1640 * transmit DMA buffers if we have room.
1641 *
1642 * Arguments: info pointer to device instance data
1643 * Return Value: None
1644 */
1645static void mgsl_isr_transmit_dma( struct mgsl_struct *info )
1646{
1647 u16 status;
1648
1649 /* clear interrupt pending and IUS bit for Tx DMA IRQ */
1650 usc_OutDmaReg(info, CDIR, BIT8+BIT0 );
1651
1652 /* Read the transmit DMA status to identify interrupt type. */
1653 /* This also clears the status bits. */
1654
1655 status = usc_InDmaReg( info, TDMR );
1656
1657 if ( debug_level >= DEBUG_LEVEL_ISR )
1658 printk("%s(%d):mgsl_isr_transmit_dma(%s) status=%04X\n",
1659 __FILE__,__LINE__,info->device_name,status);
1660
1661 if ( status & BIT2 ) {
1662 --info->tx_dma_buffers_used;
1663
1664 /* if there are transmit frames queued,
1665 * try to load the next one
1666 */
1667 if ( load_next_tx_holding_buffer(info) ) {
1668 /* if call returns non-zero value, we have
1669 * at least one free tx holding buffer
1670 */
1671 info->pending_bh |= BH_TRANSMIT;
1672 }
1673 }
1674
1675} /* end of mgsl_isr_transmit_dma() */
1676
1677/* mgsl_interrupt()
1678 *
1679 * Interrupt service routine entry point.
1680 *
1681 * Arguments:
1682 *
1683 * irq interrupt number that caused interrupt
1684 * dev_id device ID supplied during interrupt registration
1685 *
1686 * Return Value: None
1687 */
1688static irqreturn_t mgsl_interrupt(int dummy, void *dev_id)
1689{
1690 struct mgsl_struct *info = dev_id;
1691 u16 UscVector;
1692 u16 DmaVector;
1693
1694 if ( debug_level >= DEBUG_LEVEL_ISR )
1695 printk(KERN_DEBUG "%s(%d):mgsl_interrupt(%d)entry.\n",
1696 __FILE__, __LINE__, info->irq_level);
1697
1698 spin_lock(&info->irq_spinlock);
1699
1700 for(;;) {
1701 /* Read the interrupt vectors from hardware. */
1702 UscVector = usc_InReg(info, IVR) >> 9;
1703 DmaVector = usc_InDmaReg(info, DIVR);
1704
1705 if ( debug_level >= DEBUG_LEVEL_ISR )
1706 printk("%s(%d):%s UscVector=%08X DmaVector=%08X\n",
1707 __FILE__,__LINE__,info->device_name,UscVector,DmaVector);
1708
1709 if ( !UscVector && !DmaVector )
1710 break;
1711
1712 /* Dispatch interrupt vector */
1713 if ( UscVector )
1714 (*UscIsrTable[UscVector])(info);
1715 else if ( (DmaVector&(BIT10|BIT9)) == BIT10)
1716 mgsl_isr_transmit_dma(info);
1717 else
1718 mgsl_isr_receive_dma(info);
1719
1720 if ( info->isr_overflow ) {
1721 printk(KERN_ERR "%s(%d):%s isr overflow irq=%d\n",
1722 __FILE__, __LINE__, info->device_name, info->irq_level);
1723 usc_DisableMasterIrqBit(info);
1724 usc_DisableDmaInterrupts(info,DICR_MASTER);
1725 break;
1726 }
1727 }
1728
1729 /* Request bottom half processing if there's something
1730 * for it to do and the bh is not already running
1731 */
1732
1733 if ( info->pending_bh && !info->bh_running && !info->bh_requested ) {
1734 if ( debug_level >= DEBUG_LEVEL_ISR )
1735 printk("%s(%d):%s queueing bh task.\n",
1736 __FILE__,__LINE__,info->device_name);
1737 schedule_work(&info->task);
1738 info->bh_requested = true;
1739 }
1740
1741 spin_unlock(&info->irq_spinlock);
1742
1743 if ( debug_level >= DEBUG_LEVEL_ISR )
1744 printk(KERN_DEBUG "%s(%d):mgsl_interrupt(%d)exit.\n",
1745 __FILE__, __LINE__, info->irq_level);
1746
1747 return IRQ_HANDLED;
1748} /* end of mgsl_interrupt() */
1749
1750/* startup()
1751 *
1752 * Initialize and start device.
1753 *
1754 * Arguments: info pointer to device instance data
1755 * Return Value: 0 if success, otherwise error code
1756 */
1757static int startup(struct mgsl_struct * info)
1758{
1759 int retval = 0;
1760
1761 if ( debug_level >= DEBUG_LEVEL_INFO )
1762 printk("%s(%d):mgsl_startup(%s)\n",__FILE__,__LINE__,info->device_name);
1763
1764 if (info->port.flags & ASYNC_INITIALIZED)
1765 return 0;
1766
1767 if (!info->xmit_buf) {
1768 /* allocate a page of memory for a transmit buffer */
1769 info->xmit_buf = (unsigned char *)get_zeroed_page(GFP_KERNEL);
1770 if (!info->xmit_buf) {
1771 printk(KERN_ERR"%s(%d):%s can't allocate transmit buffer\n",
1772 __FILE__,__LINE__,info->device_name);
1773 return -ENOMEM;
1774 }
1775 }
1776
1777 info->pending_bh = 0;
1778
1779 memset(&info->icount, 0, sizeof(info->icount));
1780
1781 setup_timer(&info->tx_timer, mgsl_tx_timeout, (unsigned long)info);
1782
1783 /* Allocate and claim adapter resources */
1784 retval = mgsl_claim_resources(info);
1785
1786 /* perform existence check and diagnostics */
1787 if ( !retval )
1788 retval = mgsl_adapter_test(info);
1789
1790 if ( retval ) {
1791 if (capable(CAP_SYS_ADMIN) && info->port.tty)
1792 set_bit(TTY_IO_ERROR, &info->port.tty->flags);
1793 mgsl_release_resources(info);
1794 return retval;
1795 }
1796
1797 /* program hardware for current parameters */
1798 mgsl_change_params(info);
1799
1800 if (info->port.tty)
1801 clear_bit(TTY_IO_ERROR, &info->port.tty->flags);
1802
1803 info->port.flags |= ASYNC_INITIALIZED;
1804
1805 return 0;
1806
1807} /* end of startup() */
1808
1809/* shutdown()
1810 *
1811 * Called by mgsl_close() and mgsl_hangup() to shutdown hardware
1812 *
1813 * Arguments: info pointer to device instance data
1814 * Return Value: None
1815 */
1816static void shutdown(struct mgsl_struct * info)
1817{
1818 unsigned long flags;
1819
1820 if (!(info->port.flags & ASYNC_INITIALIZED))
1821 return;
1822
1823 if (debug_level >= DEBUG_LEVEL_INFO)
1824 printk("%s(%d):mgsl_shutdown(%s)\n",
1825 __FILE__,__LINE__, info->device_name );
1826
1827 /* clear status wait queue because status changes */
1828 /* can't happen after shutting down the hardware */
1829 wake_up_interruptible(&info->status_event_wait_q);
1830 wake_up_interruptible(&info->event_wait_q);
1831
1832 del_timer_sync(&info->tx_timer);
1833
1834 if (info->xmit_buf) {
1835 free_page((unsigned long) info->xmit_buf);
1836 info->xmit_buf = NULL;
1837 }
1838
1839 spin_lock_irqsave(&info->irq_spinlock,flags);
1840 usc_DisableMasterIrqBit(info);
1841 usc_stop_receiver(info);
1842 usc_stop_transmitter(info);
1843 usc_DisableInterrupts(info,RECEIVE_DATA + RECEIVE_STATUS +
1844 TRANSMIT_DATA + TRANSMIT_STATUS + IO_PIN + MISC );
1845 usc_DisableDmaInterrupts(info,DICR_MASTER + DICR_TRANSMIT + DICR_RECEIVE);
1846
1847 /* Disable DMAEN (Port 7, Bit 14) */
1848 /* This disconnects the DMA request signal from the ISA bus */
1849 /* on the ISA adapter. This has no effect for the PCI adapter */
1850 usc_OutReg(info, PCR, (u16)((usc_InReg(info, PCR) | BIT15) | BIT14));
1851
1852 /* Disable INTEN (Port 6, Bit12) */
1853 /* This disconnects the IRQ request signal to the ISA bus */
1854 /* on the ISA adapter. This has no effect for the PCI adapter */
1855 usc_OutReg(info, PCR, (u16)((usc_InReg(info, PCR) | BIT13) | BIT12));
1856
1857 if (!info->port.tty || info->port.tty->termios->c_cflag & HUPCL) {
1858 info->serial_signals &= ~(SerialSignal_DTR + SerialSignal_RTS);
1859 usc_set_serial_signals(info);
1860 }
1861
1862 spin_unlock_irqrestore(&info->irq_spinlock,flags);
1863
1864 mgsl_release_resources(info);
1865
1866 if (info->port.tty)
1867 set_bit(TTY_IO_ERROR, &info->port.tty->flags);
1868
1869 info->port.flags &= ~ASYNC_INITIALIZED;
1870
1871} /* end of shutdown() */
1872
1873static void mgsl_program_hw(struct mgsl_struct *info)
1874{
1875 unsigned long flags;
1876
1877 spin_lock_irqsave(&info->irq_spinlock,flags);
1878
1879 usc_stop_receiver(info);
1880 usc_stop_transmitter(info);
1881 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1882
1883 if (info->params.mode == MGSL_MODE_HDLC ||
1884 info->params.mode == MGSL_MODE_RAW ||
1885 info->netcount)
1886 usc_set_sync_mode(info);
1887 else
1888 usc_set_async_mode(info);
1889
1890 usc_set_serial_signals(info);
1891
1892 info->dcd_chkcount = 0;
1893 info->cts_chkcount = 0;
1894 info->ri_chkcount = 0;
1895 info->dsr_chkcount = 0;
1896
1897 usc_EnableStatusIrqs(info,SICR_CTS+SICR_DSR+SICR_DCD+SICR_RI);
1898 usc_EnableInterrupts(info, IO_PIN);
1899 usc_get_serial_signals(info);
1900
1901 if (info->netcount || info->port.tty->termios->c_cflag & CREAD)
1902 usc_start_receiver(info);
1903
1904 spin_unlock_irqrestore(&info->irq_spinlock,flags);
1905}
1906
1907/* Reconfigure adapter based on new parameters
1908 */
1909static void mgsl_change_params(struct mgsl_struct *info)
1910{
1911 unsigned cflag;
1912 int bits_per_char;
1913
1914 if (!info->port.tty || !info->port.tty->termios)
1915 return;
1916
1917 if (debug_level >= DEBUG_LEVEL_INFO)
1918 printk("%s(%d):mgsl_change_params(%s)\n",
1919 __FILE__,__LINE__, info->device_name );
1920
1921 cflag = info->port.tty->termios->c_cflag;
1922
1923 /* if B0 rate (hangup) specified then negate DTR and RTS */
1924 /* otherwise assert DTR and RTS */
1925 if (cflag & CBAUD)
1926 info->serial_signals |= SerialSignal_RTS + SerialSignal_DTR;
1927 else
1928 info->serial_signals &= ~(SerialSignal_RTS + SerialSignal_DTR);
1929
1930 /* byte size and parity */
1931
1932 switch (cflag & CSIZE) {
1933 case CS5: info->params.data_bits = 5; break;
1934 case CS6: info->params.data_bits = 6; break;
1935 case CS7: info->params.data_bits = 7; break;
1936 case CS8: info->params.data_bits = 8; break;
1937 /* Never happens, but GCC is too dumb to figure it out */
1938 default: info->params.data_bits = 7; break;
1939 }
1940
1941 if (cflag & CSTOPB)
1942 info->params.stop_bits = 2;
1943 else
1944 info->params.stop_bits = 1;
1945
1946 info->params.parity = ASYNC_PARITY_NONE;
1947 if (cflag & PARENB) {
1948 if (cflag & PARODD)
1949 info->params.parity = ASYNC_PARITY_ODD;
1950 else
1951 info->params.parity = ASYNC_PARITY_EVEN;
1952#ifdef CMSPAR
1953 if (cflag & CMSPAR)
1954 info->params.parity = ASYNC_PARITY_SPACE;
1955#endif
1956 }
1957
1958 /* calculate number of jiffies to transmit a full
1959 * FIFO (32 bytes) at specified data rate
1960 */
1961 bits_per_char = info->params.data_bits +
1962 info->params.stop_bits + 1;
1963
1964 /* if port data rate is set to 460800 or less then
1965 * allow tty settings to override, otherwise keep the
1966 * current data rate.
1967 */
1968 if (info->params.data_rate <= 460800)
1969 info->params.data_rate = tty_get_baud_rate(info->port.tty);
1970
1971 if ( info->params.data_rate ) {
1972 info->timeout = (32*HZ*bits_per_char) /
1973 info->params.data_rate;
1974 }
1975 info->timeout += HZ/50; /* Add .02 seconds of slop */
1976
1977 if (cflag & CRTSCTS)
1978 info->port.flags |= ASYNC_CTS_FLOW;
1979 else
1980 info->port.flags &= ~ASYNC_CTS_FLOW;
1981
1982 if (cflag & CLOCAL)
1983 info->port.flags &= ~ASYNC_CHECK_CD;
1984 else
1985 info->port.flags |= ASYNC_CHECK_CD;
1986
1987 /* process tty input control flags */
1988
1989 info->read_status_mask = RXSTATUS_OVERRUN;
1990 if (I_INPCK(info->port.tty))
1991 info->read_status_mask |= RXSTATUS_PARITY_ERROR | RXSTATUS_FRAMING_ERROR;
1992 if (I_BRKINT(info->port.tty) || I_PARMRK(info->port.tty))
1993 info->read_status_mask |= RXSTATUS_BREAK_RECEIVED;
1994
1995 if (I_IGNPAR(info->port.tty))
1996 info->ignore_status_mask |= RXSTATUS_PARITY_ERROR | RXSTATUS_FRAMING_ERROR;
1997 if (I_IGNBRK(info->port.tty)) {
1998 info->ignore_status_mask |= RXSTATUS_BREAK_RECEIVED;
1999 /* If ignoring parity and break indicators, ignore
2000 * overruns too. (For real raw support).
2001 */
2002 if (I_IGNPAR(info->port.tty))
2003 info->ignore_status_mask |= RXSTATUS_OVERRUN;
2004 }
2005
2006 mgsl_program_hw(info);
2007
2008} /* end of mgsl_change_params() */
2009
2010/* mgsl_put_char()
2011 *
2012 * Add a character to the transmit buffer.
2013 *
2014 * Arguments: tty pointer to tty information structure
2015 * ch character to add to transmit buffer
2016 *
2017 * Return Value: None
2018 */
2019static int mgsl_put_char(struct tty_struct *tty, unsigned char ch)
2020{
2021 struct mgsl_struct *info = tty->driver_data;
2022 unsigned long flags;
2023 int ret = 0;
2024
2025 if (debug_level >= DEBUG_LEVEL_INFO) {
2026 printk(KERN_DEBUG "%s(%d):mgsl_put_char(%d) on %s\n",
2027 __FILE__, __LINE__, ch, info->device_name);
2028 }
2029
2030 if (mgsl_paranoia_check(info, tty->name, "mgsl_put_char"))
2031 return 0;
2032
2033 if (!info->xmit_buf)
2034 return 0;
2035
2036 spin_lock_irqsave(&info->irq_spinlock, flags);
2037
2038 if ((info->params.mode == MGSL_MODE_ASYNC ) || !info->tx_active) {
2039 if (info->xmit_cnt < SERIAL_XMIT_SIZE - 1) {
2040 info->xmit_buf[info->xmit_head++] = ch;
2041 info->xmit_head &= SERIAL_XMIT_SIZE-1;
2042 info->xmit_cnt++;
2043 ret = 1;
2044 }
2045 }
2046 spin_unlock_irqrestore(&info->irq_spinlock, flags);
2047 return ret;
2048
2049} /* end of mgsl_put_char() */
2050
2051/* mgsl_flush_chars()
2052 *
2053 * Enable transmitter so remaining characters in the
2054 * transmit buffer are sent.
2055 *
2056 * Arguments: tty pointer to tty information structure
2057 * Return Value: None
2058 */
2059static void mgsl_flush_chars(struct tty_struct *tty)
2060{
2061 struct mgsl_struct *info = tty->driver_data;
2062 unsigned long flags;
2063
2064 if ( debug_level >= DEBUG_LEVEL_INFO )
2065 printk( "%s(%d):mgsl_flush_chars() entry on %s xmit_cnt=%d\n",
2066 __FILE__,__LINE__,info->device_name,info->xmit_cnt);
2067
2068 if (mgsl_paranoia_check(info, tty->name, "mgsl_flush_chars"))
2069 return;
2070
2071 if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
2072 !info->xmit_buf)
2073 return;
2074
2075 if ( debug_level >= DEBUG_LEVEL_INFO )
2076 printk( "%s(%d):mgsl_flush_chars() entry on %s starting transmitter\n",
2077 __FILE__,__LINE__,info->device_name );
2078
2079 spin_lock_irqsave(&info->irq_spinlock,flags);
2080
2081 if (!info->tx_active) {
2082 if ( (info->params.mode == MGSL_MODE_HDLC ||
2083 info->params.mode == MGSL_MODE_RAW) && info->xmit_cnt ) {
2084 /* operating in synchronous (frame oriented) mode */
2085 /* copy data from circular xmit_buf to */
2086 /* transmit DMA buffer. */
2087 mgsl_load_tx_dma_buffer(info,
2088 info->xmit_buf,info->xmit_cnt);
2089 }
2090 usc_start_transmitter(info);
2091 }
2092
2093 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2094
2095} /* end of mgsl_flush_chars() */
2096
2097/* mgsl_write()
2098 *
2099 * Send a block of data
2100 *
2101 * Arguments:
2102 *
2103 * tty pointer to tty information structure
2104 * buf pointer to buffer containing send data
2105 * count size of send data in bytes
2106 *
2107 * Return Value: number of characters written
2108 */
2109static int mgsl_write(struct tty_struct * tty,
2110 const unsigned char *buf, int count)
2111{
2112 int c, ret = 0;
2113 struct mgsl_struct *info = tty->driver_data;
2114 unsigned long flags;
2115
2116 if ( debug_level >= DEBUG_LEVEL_INFO )
2117 printk( "%s(%d):mgsl_write(%s) count=%d\n",
2118 __FILE__,__LINE__,info->device_name,count);
2119
2120 if (mgsl_paranoia_check(info, tty->name, "mgsl_write"))
2121 goto cleanup;
2122
2123 if (!info->xmit_buf)
2124 goto cleanup;
2125
2126 if ( info->params.mode == MGSL_MODE_HDLC ||
2127 info->params.mode == MGSL_MODE_RAW ) {
2128 /* operating in synchronous (frame oriented) mode */
2129 /* operating in synchronous (frame oriented) mode */
2130 if (info->tx_active) {
2131
2132 if ( info->params.mode == MGSL_MODE_HDLC ) {
2133 ret = 0;
2134 goto cleanup;
2135 }
2136 /* transmitter is actively sending data -
2137 * if we have multiple transmit dma and
2138 * holding buffers, attempt to queue this
2139 * frame for transmission at a later time.
2140 */
2141 if (info->tx_holding_count >= info->num_tx_holding_buffers ) {
2142 /* no tx holding buffers available */
2143 ret = 0;
2144 goto cleanup;
2145 }
2146
2147 /* queue transmit frame request */
2148 ret = count;
2149 save_tx_buffer_request(info,buf,count);
2150
2151 /* if we have sufficient tx dma buffers,
2152 * load the next buffered tx request
2153 */
2154 spin_lock_irqsave(&info->irq_spinlock,flags);
2155 load_next_tx_holding_buffer(info);
2156 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2157 goto cleanup;
2158 }
2159
2160 /* if operating in HDLC LoopMode and the adapter */
2161 /* has yet to be inserted into the loop, we can't */
2162 /* transmit */
2163
2164 if ( (info->params.flags & HDLC_FLAG_HDLC_LOOPMODE) &&
2165 !usc_loopmode_active(info) )
2166 {
2167 ret = 0;
2168 goto cleanup;
2169 }
2170
2171 if ( info->xmit_cnt ) {
2172 /* Send accumulated from send_char() calls */
2173 /* as frame and wait before accepting more data. */
2174 ret = 0;
2175
2176 /* copy data from circular xmit_buf to */
2177 /* transmit DMA buffer. */
2178 mgsl_load_tx_dma_buffer(info,
2179 info->xmit_buf,info->xmit_cnt);
2180 if ( debug_level >= DEBUG_LEVEL_INFO )
2181 printk( "%s(%d):mgsl_write(%s) sync xmit_cnt flushing\n",
2182 __FILE__,__LINE__,info->device_name);
2183 } else {
2184 if ( debug_level >= DEBUG_LEVEL_INFO )
2185 printk( "%s(%d):mgsl_write(%s) sync transmit accepted\n",
2186 __FILE__,__LINE__,info->device_name);
2187 ret = count;
2188 info->xmit_cnt = count;
2189 mgsl_load_tx_dma_buffer(info,buf,count);
2190 }
2191 } else {
2192 while (1) {
2193 spin_lock_irqsave(&info->irq_spinlock,flags);
2194 c = min_t(int, count,
2195 min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
2196 SERIAL_XMIT_SIZE - info->xmit_head));
2197 if (c <= 0) {
2198 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2199 break;
2200 }
2201 memcpy(info->xmit_buf + info->xmit_head, buf, c);
2202 info->xmit_head = ((info->xmit_head + c) &
2203 (SERIAL_XMIT_SIZE-1));
2204 info->xmit_cnt += c;
2205 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2206 buf += c;
2207 count -= c;
2208 ret += c;
2209 }
2210 }
2211
2212 if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped) {
2213 spin_lock_irqsave(&info->irq_spinlock,flags);
2214 if (!info->tx_active)
2215 usc_start_transmitter(info);
2216 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2217 }
2218cleanup:
2219 if ( debug_level >= DEBUG_LEVEL_INFO )
2220 printk( "%s(%d):mgsl_write(%s) returning=%d\n",
2221 __FILE__,__LINE__,info->device_name,ret);
2222
2223 return ret;
2224
2225} /* end of mgsl_write() */
2226
2227/* mgsl_write_room()
2228 *
2229 * Return the count of free bytes in transmit buffer
2230 *
2231 * Arguments: tty pointer to tty info structure
2232 * Return Value: None
2233 */
2234static int mgsl_write_room(struct tty_struct *tty)
2235{
2236 struct mgsl_struct *info = tty->driver_data;
2237 int ret;
2238
2239 if (mgsl_paranoia_check(info, tty->name, "mgsl_write_room"))
2240 return 0;
2241 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
2242 if (ret < 0)
2243 ret = 0;
2244
2245 if (debug_level >= DEBUG_LEVEL_INFO)
2246 printk("%s(%d):mgsl_write_room(%s)=%d\n",
2247 __FILE__,__LINE__, info->device_name,ret );
2248
2249 if ( info->params.mode == MGSL_MODE_HDLC ||
2250 info->params.mode == MGSL_MODE_RAW ) {
2251 /* operating in synchronous (frame oriented) mode */
2252 if ( info->tx_active )
2253 return 0;
2254 else
2255 return HDLC_MAX_FRAME_SIZE;
2256 }
2257
2258 return ret;
2259
2260} /* end of mgsl_write_room() */
2261
2262/* mgsl_chars_in_buffer()
2263 *
2264 * Return the count of bytes in transmit buffer
2265 *
2266 * Arguments: tty pointer to tty info structure
2267 * Return Value: None
2268 */
2269static int mgsl_chars_in_buffer(struct tty_struct *tty)
2270{
2271 struct mgsl_struct *info = tty->driver_data;
2272
2273 if (debug_level >= DEBUG_LEVEL_INFO)
2274 printk("%s(%d):mgsl_chars_in_buffer(%s)\n",
2275 __FILE__,__LINE__, info->device_name );
2276
2277 if (mgsl_paranoia_check(info, tty->name, "mgsl_chars_in_buffer"))
2278 return 0;
2279
2280 if (debug_level >= DEBUG_LEVEL_INFO)
2281 printk("%s(%d):mgsl_chars_in_buffer(%s)=%d\n",
2282 __FILE__,__LINE__, info->device_name,info->xmit_cnt );
2283
2284 if ( info->params.mode == MGSL_MODE_HDLC ||
2285 info->params.mode == MGSL_MODE_RAW ) {
2286 /* operating in synchronous (frame oriented) mode */
2287 if ( info->tx_active )
2288 return info->max_frame_size;
2289 else
2290 return 0;
2291 }
2292
2293 return info->xmit_cnt;
2294} /* end of mgsl_chars_in_buffer() */
2295
2296/* mgsl_flush_buffer()
2297 *
2298 * Discard all data in the send buffer
2299 *
2300 * Arguments: tty pointer to tty info structure
2301 * Return Value: None
2302 */
2303static void mgsl_flush_buffer(struct tty_struct *tty)
2304{
2305 struct mgsl_struct *info = tty->driver_data;
2306 unsigned long flags;
2307
2308 if (debug_level >= DEBUG_LEVEL_INFO)
2309 printk("%s(%d):mgsl_flush_buffer(%s) entry\n",
2310 __FILE__,__LINE__, info->device_name );
2311
2312 if (mgsl_paranoia_check(info, tty->name, "mgsl_flush_buffer"))
2313 return;
2314
2315 spin_lock_irqsave(&info->irq_spinlock,flags);
2316 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
2317 del_timer(&info->tx_timer);
2318 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2319
2320 tty_wakeup(tty);
2321}
2322
2323/* mgsl_send_xchar()
2324 *
2325 * Send a high-priority XON/XOFF character
2326 *
2327 * Arguments: tty pointer to tty info structure
2328 * ch character to send
2329 * Return Value: None
2330 */
2331static void mgsl_send_xchar(struct tty_struct *tty, char ch)
2332{
2333 struct mgsl_struct *info = tty->driver_data;
2334 unsigned long flags;
2335
2336 if (debug_level >= DEBUG_LEVEL_INFO)
2337 printk("%s(%d):mgsl_send_xchar(%s,%d)\n",
2338 __FILE__,__LINE__, info->device_name, ch );
2339
2340 if (mgsl_paranoia_check(info, tty->name, "mgsl_send_xchar"))
2341 return;
2342
2343 info->x_char = ch;
2344 if (ch) {
2345 /* Make sure transmit interrupts are on */
2346 spin_lock_irqsave(&info->irq_spinlock,flags);
2347 if (!info->tx_enabled)
2348 usc_start_transmitter(info);
2349 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2350 }
2351} /* end of mgsl_send_xchar() */
2352
2353/* mgsl_throttle()
2354 *
2355 * Signal remote device to throttle send data (our receive data)
2356 *
2357 * Arguments: tty pointer to tty info structure
2358 * Return Value: None
2359 */
2360static void mgsl_throttle(struct tty_struct * tty)
2361{
2362 struct mgsl_struct *info = tty->driver_data;
2363 unsigned long flags;
2364
2365 if (debug_level >= DEBUG_LEVEL_INFO)
2366 printk("%s(%d):mgsl_throttle(%s) entry\n",
2367 __FILE__,__LINE__, info->device_name );
2368
2369 if (mgsl_paranoia_check(info, tty->name, "mgsl_throttle"))
2370 return;
2371
2372 if (I_IXOFF(tty))
2373 mgsl_send_xchar(tty, STOP_CHAR(tty));
2374
2375 if (tty->termios->c_cflag & CRTSCTS) {
2376 spin_lock_irqsave(&info->irq_spinlock,flags);
2377 info->serial_signals &= ~SerialSignal_RTS;
2378 usc_set_serial_signals(info);
2379 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2380 }
2381} /* end of mgsl_throttle() */
2382
2383/* mgsl_unthrottle()
2384 *
2385 * Signal remote device to stop throttling send data (our receive data)
2386 *
2387 * Arguments: tty pointer to tty info structure
2388 * Return Value: None
2389 */
2390static void mgsl_unthrottle(struct tty_struct * tty)
2391{
2392 struct mgsl_struct *info = tty->driver_data;
2393 unsigned long flags;
2394
2395 if (debug_level >= DEBUG_LEVEL_INFO)
2396 printk("%s(%d):mgsl_unthrottle(%s) entry\n",
2397 __FILE__,__LINE__, info->device_name );
2398
2399 if (mgsl_paranoia_check(info, tty->name, "mgsl_unthrottle"))
2400 return;
2401
2402 if (I_IXOFF(tty)) {
2403 if (info->x_char)
2404 info->x_char = 0;
2405 else
2406 mgsl_send_xchar(tty, START_CHAR(tty));
2407 }
2408
2409 if (tty->termios->c_cflag & CRTSCTS) {
2410 spin_lock_irqsave(&info->irq_spinlock,flags);
2411 info->serial_signals |= SerialSignal_RTS;
2412 usc_set_serial_signals(info);
2413 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2414 }
2415
2416} /* end of mgsl_unthrottle() */
2417
2418/* mgsl_get_stats()
2419 *
2420 * get the current serial parameters information
2421 *
2422 * Arguments: info pointer to device instance data
2423 * user_icount pointer to buffer to hold returned stats
2424 *
2425 * Return Value: 0 if success, otherwise error code
2426 */
2427static int mgsl_get_stats(struct mgsl_struct * info, struct mgsl_icount __user *user_icount)
2428{
2429 int err;
2430
2431 if (debug_level >= DEBUG_LEVEL_INFO)
2432 printk("%s(%d):mgsl_get_params(%s)\n",
2433 __FILE__,__LINE__, info->device_name);
2434
2435 if (!user_icount) {
2436 memset(&info->icount, 0, sizeof(info->icount));
2437 } else {
2438 mutex_lock(&info->port.mutex);
2439 COPY_TO_USER(err, user_icount, &info->icount, sizeof(struct mgsl_icount));
2440 mutex_unlock(&info->port.mutex);
2441 if (err)
2442 return -EFAULT;
2443 }
2444
2445 return 0;
2446
2447} /* end of mgsl_get_stats() */
2448
2449/* mgsl_get_params()
2450 *
2451 * get the current serial parameters information
2452 *
2453 * Arguments: info pointer to device instance data
2454 * user_params pointer to buffer to hold returned params
2455 *
2456 * Return Value: 0 if success, otherwise error code
2457 */
2458static int mgsl_get_params(struct mgsl_struct * info, MGSL_PARAMS __user *user_params)
2459{
2460 int err;
2461 if (debug_level >= DEBUG_LEVEL_INFO)
2462 printk("%s(%d):mgsl_get_params(%s)\n",
2463 __FILE__,__LINE__, info->device_name);
2464
2465 mutex_lock(&info->port.mutex);
2466 COPY_TO_USER(err,user_params, &info->params, sizeof(MGSL_PARAMS));
2467 mutex_unlock(&info->port.mutex);
2468 if (err) {
2469 if ( debug_level >= DEBUG_LEVEL_INFO )
2470 printk( "%s(%d):mgsl_get_params(%s) user buffer copy failed\n",
2471 __FILE__,__LINE__,info->device_name);
2472 return -EFAULT;
2473 }
2474
2475 return 0;
2476
2477} /* end of mgsl_get_params() */
2478
2479/* mgsl_set_params()
2480 *
2481 * set the serial parameters
2482 *
2483 * Arguments:
2484 *
2485 * info pointer to device instance data
2486 * new_params user buffer containing new serial params
2487 *
2488 * Return Value: 0 if success, otherwise error code
2489 */
2490static int mgsl_set_params(struct mgsl_struct * info, MGSL_PARAMS __user *new_params)
2491{
2492 unsigned long flags;
2493 MGSL_PARAMS tmp_params;
2494 int err;
2495
2496 if (debug_level >= DEBUG_LEVEL_INFO)
2497 printk("%s(%d):mgsl_set_params %s\n", __FILE__,__LINE__,
2498 info->device_name );
2499 COPY_FROM_USER(err,&tmp_params, new_params, sizeof(MGSL_PARAMS));
2500 if (err) {
2501 if ( debug_level >= DEBUG_LEVEL_INFO )
2502 printk( "%s(%d):mgsl_set_params(%s) user buffer copy failed\n",
2503 __FILE__,__LINE__,info->device_name);
2504 return -EFAULT;
2505 }
2506
2507 mutex_lock(&info->port.mutex);
2508 spin_lock_irqsave(&info->irq_spinlock,flags);
2509 memcpy(&info->params,&tmp_params,sizeof(MGSL_PARAMS));
2510 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2511
2512 mgsl_change_params(info);
2513 mutex_unlock(&info->port.mutex);
2514
2515 return 0;
2516
2517} /* end of mgsl_set_params() */
2518
2519/* mgsl_get_txidle()
2520 *
2521 * get the current transmit idle mode
2522 *
2523 * Arguments: info pointer to device instance data
2524 * idle_mode pointer to buffer to hold returned idle mode
2525 *
2526 * Return Value: 0 if success, otherwise error code
2527 */
2528static int mgsl_get_txidle(struct mgsl_struct * info, int __user *idle_mode)
2529{
2530 int err;
2531
2532 if (debug_level >= DEBUG_LEVEL_INFO)
2533 printk("%s(%d):mgsl_get_txidle(%s)=%d\n",
2534 __FILE__,__LINE__, info->device_name, info->idle_mode);
2535
2536 COPY_TO_USER(err,idle_mode, &info->idle_mode, sizeof(int));
2537 if (err) {
2538 if ( debug_level >= DEBUG_LEVEL_INFO )
2539 printk( "%s(%d):mgsl_get_txidle(%s) user buffer copy failed\n",
2540 __FILE__,__LINE__,info->device_name);
2541 return -EFAULT;
2542 }
2543
2544 return 0;
2545
2546} /* end of mgsl_get_txidle() */
2547
2548/* mgsl_set_txidle() service ioctl to set transmit idle mode
2549 *
2550 * Arguments: info pointer to device instance data
2551 * idle_mode new idle mode
2552 *
2553 * Return Value: 0 if success, otherwise error code
2554 */
2555static int mgsl_set_txidle(struct mgsl_struct * info, int idle_mode)
2556{
2557 unsigned long flags;
2558
2559 if (debug_level >= DEBUG_LEVEL_INFO)
2560 printk("%s(%d):mgsl_set_txidle(%s,%d)\n", __FILE__,__LINE__,
2561 info->device_name, idle_mode );
2562
2563 spin_lock_irqsave(&info->irq_spinlock,flags);
2564 info->idle_mode = idle_mode;
2565 usc_set_txidle( info );
2566 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2567 return 0;
2568
2569} /* end of mgsl_set_txidle() */
2570
2571/* mgsl_txenable()
2572 *
2573 * enable or disable the transmitter
2574 *
2575 * Arguments:
2576 *
2577 * info pointer to device instance data
2578 * enable 1 = enable, 0 = disable
2579 *
2580 * Return Value: 0 if success, otherwise error code
2581 */
2582static int mgsl_txenable(struct mgsl_struct * info, int enable)
2583{
2584 unsigned long flags;
2585
2586 if (debug_level >= DEBUG_LEVEL_INFO)
2587 printk("%s(%d):mgsl_txenable(%s,%d)\n", __FILE__,__LINE__,
2588 info->device_name, enable);
2589
2590 spin_lock_irqsave(&info->irq_spinlock,flags);
2591 if ( enable ) {
2592 if ( !info->tx_enabled ) {
2593
2594 usc_start_transmitter(info);
2595 /*--------------------------------------------------
2596 * if HDLC/SDLC Loop mode, attempt to insert the
2597 * station in the 'loop' by setting CMR:13. Upon
2598 * receipt of the next GoAhead (RxAbort) sequence,
2599 * the OnLoop indicator (CCSR:7) should go active
2600 * to indicate that we are on the loop
2601 *--------------------------------------------------*/
2602 if ( info->params.flags & HDLC_FLAG_HDLC_LOOPMODE )
2603 usc_loopmode_insert_request( info );
2604 }
2605 } else {
2606 if ( info->tx_enabled )
2607 usc_stop_transmitter(info);
2608 }
2609 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2610 return 0;
2611
2612} /* end of mgsl_txenable() */
2613
2614/* mgsl_txabort() abort send HDLC frame
2615 *
2616 * Arguments: info pointer to device instance data
2617 * Return Value: 0 if success, otherwise error code
2618 */
2619static int mgsl_txabort(struct mgsl_struct * info)
2620{
2621 unsigned long flags;
2622
2623 if (debug_level >= DEBUG_LEVEL_INFO)
2624 printk("%s(%d):mgsl_txabort(%s)\n", __FILE__,__LINE__,
2625 info->device_name);
2626
2627 spin_lock_irqsave(&info->irq_spinlock,flags);
2628 if ( info->tx_active && info->params.mode == MGSL_MODE_HDLC )
2629 {
2630 if ( info->params.flags & HDLC_FLAG_HDLC_LOOPMODE )
2631 usc_loopmode_cancel_transmit( info );
2632 else
2633 usc_TCmd(info,TCmd_SendAbort);
2634 }
2635 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2636 return 0;
2637
2638} /* end of mgsl_txabort() */
2639
2640/* mgsl_rxenable() enable or disable the receiver
2641 *
2642 * Arguments: info pointer to device instance data
2643 * enable 1 = enable, 0 = disable
2644 * Return Value: 0 if success, otherwise error code
2645 */
2646static int mgsl_rxenable(struct mgsl_struct * info, int enable)
2647{
2648 unsigned long flags;
2649
2650 if (debug_level >= DEBUG_LEVEL_INFO)
2651 printk("%s(%d):mgsl_rxenable(%s,%d)\n", __FILE__,__LINE__,
2652 info->device_name, enable);
2653
2654 spin_lock_irqsave(&info->irq_spinlock,flags);
2655 if ( enable ) {
2656 if ( !info->rx_enabled )
2657 usc_start_receiver(info);
2658 } else {
2659 if ( info->rx_enabled )
2660 usc_stop_receiver(info);
2661 }
2662 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2663 return 0;
2664
2665} /* end of mgsl_rxenable() */
2666
2667/* mgsl_wait_event() wait for specified event to occur
2668 *
2669 * Arguments: info pointer to device instance data
2670 * mask pointer to bitmask of events to wait for
2671 * Return Value: 0 if successful and bit mask updated with
2672 * of events triggerred,
2673 * otherwise error code
2674 */
2675static int mgsl_wait_event(struct mgsl_struct * info, int __user * mask_ptr)
2676{
2677 unsigned long flags;
2678 int s;
2679 int rc=0;
2680 struct mgsl_icount cprev, cnow;
2681 int events;
2682 int mask;
2683 struct _input_signal_events oldsigs, newsigs;
2684 DECLARE_WAITQUEUE(wait, current);
2685
2686 COPY_FROM_USER(rc,&mask, mask_ptr, sizeof(int));
2687 if (rc) {
2688 return -EFAULT;
2689 }
2690
2691 if (debug_level >= DEBUG_LEVEL_INFO)
2692 printk("%s(%d):mgsl_wait_event(%s,%d)\n", __FILE__,__LINE__,
2693 info->device_name, mask);
2694
2695 spin_lock_irqsave(&info->irq_spinlock,flags);
2696
2697 /* return immediately if state matches requested events */
2698 usc_get_serial_signals(info);
2699 s = info->serial_signals;
2700 events = mask &
2701 ( ((s & SerialSignal_DSR) ? MgslEvent_DsrActive:MgslEvent_DsrInactive) +
2702 ((s & SerialSignal_DCD) ? MgslEvent_DcdActive:MgslEvent_DcdInactive) +
2703 ((s & SerialSignal_CTS) ? MgslEvent_CtsActive:MgslEvent_CtsInactive) +
2704 ((s & SerialSignal_RI) ? MgslEvent_RiActive :MgslEvent_RiInactive) );
2705 if (events) {
2706 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2707 goto exit;
2708 }
2709
2710 /* save current irq counts */
2711 cprev = info->icount;
2712 oldsigs = info->input_signal_events;
2713
2714 /* enable hunt and idle irqs if needed */
2715 if (mask & (MgslEvent_ExitHuntMode + MgslEvent_IdleReceived)) {
2716 u16 oldreg = usc_InReg(info,RICR);
2717 u16 newreg = oldreg +
2718 (mask & MgslEvent_ExitHuntMode ? RXSTATUS_EXITED_HUNT:0) +
2719 (mask & MgslEvent_IdleReceived ? RXSTATUS_IDLE_RECEIVED:0);
2720 if (oldreg != newreg)
2721 usc_OutReg(info, RICR, newreg);
2722 }
2723
2724 set_current_state(TASK_INTERRUPTIBLE);
2725 add_wait_queue(&info->event_wait_q, &wait);
2726
2727 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2728
2729
2730 for(;;) {
2731 schedule();
2732 if (signal_pending(current)) {
2733 rc = -ERESTARTSYS;
2734 break;
2735 }
2736
2737 /* get current irq counts */
2738 spin_lock_irqsave(&info->irq_spinlock,flags);
2739 cnow = info->icount;
2740 newsigs = info->input_signal_events;
2741 set_current_state(TASK_INTERRUPTIBLE);
2742 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2743
2744 /* if no change, wait aborted for some reason */
2745 if (newsigs.dsr_up == oldsigs.dsr_up &&
2746 newsigs.dsr_down == oldsigs.dsr_down &&
2747 newsigs.dcd_up == oldsigs.dcd_up &&
2748 newsigs.dcd_down == oldsigs.dcd_down &&
2749 newsigs.cts_up == oldsigs.cts_up &&
2750 newsigs.cts_down == oldsigs.cts_down &&
2751 newsigs.ri_up == oldsigs.ri_up &&
2752 newsigs.ri_down == oldsigs.ri_down &&
2753 cnow.exithunt == cprev.exithunt &&
2754 cnow.rxidle == cprev.rxidle) {
2755 rc = -EIO;
2756 break;
2757 }
2758
2759 events = mask &
2760 ( (newsigs.dsr_up != oldsigs.dsr_up ? MgslEvent_DsrActive:0) +
2761 (newsigs.dsr_down != oldsigs.dsr_down ? MgslEvent_DsrInactive:0) +
2762 (newsigs.dcd_up != oldsigs.dcd_up ? MgslEvent_DcdActive:0) +
2763 (newsigs.dcd_down != oldsigs.dcd_down ? MgslEvent_DcdInactive:0) +
2764 (newsigs.cts_up != oldsigs.cts_up ? MgslEvent_CtsActive:0) +
2765 (newsigs.cts_down != oldsigs.cts_down ? MgslEvent_CtsInactive:0) +
2766 (newsigs.ri_up != oldsigs.ri_up ? MgslEvent_RiActive:0) +
2767 (newsigs.ri_down != oldsigs.ri_down ? MgslEvent_RiInactive:0) +
2768 (cnow.exithunt != cprev.exithunt ? MgslEvent_ExitHuntMode:0) +
2769 (cnow.rxidle != cprev.rxidle ? MgslEvent_IdleReceived:0) );
2770 if (events)
2771 break;
2772
2773 cprev = cnow;
2774 oldsigs = newsigs;
2775 }
2776
2777 remove_wait_queue(&info->event_wait_q, &wait);
2778 set_current_state(TASK_RUNNING);
2779
2780 if (mask & (MgslEvent_ExitHuntMode + MgslEvent_IdleReceived)) {
2781 spin_lock_irqsave(&info->irq_spinlock,flags);
2782 if (!waitqueue_active(&info->event_wait_q)) {
2783 /* disable enable exit hunt mode/idle rcvd IRQs */
2784 usc_OutReg(info, RICR, usc_InReg(info,RICR) &
2785 ~(RXSTATUS_EXITED_HUNT + RXSTATUS_IDLE_RECEIVED));
2786 }
2787 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2788 }
2789exit:
2790 if ( rc == 0 )
2791 PUT_USER(rc, events, mask_ptr);
2792
2793 return rc;
2794
2795} /* end of mgsl_wait_event() */
2796
2797static int modem_input_wait(struct mgsl_struct *info,int arg)
2798{
2799 unsigned long flags;
2800 int rc;
2801 struct mgsl_icount cprev, cnow;
2802 DECLARE_WAITQUEUE(wait, current);
2803
2804 /* save current irq counts */
2805 spin_lock_irqsave(&info->irq_spinlock,flags);
2806 cprev = info->icount;
2807 add_wait_queue(&info->status_event_wait_q, &wait);
2808 set_current_state(TASK_INTERRUPTIBLE);
2809 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2810
2811 for(;;) {
2812 schedule();
2813 if (signal_pending(current)) {
2814 rc = -ERESTARTSYS;
2815 break;
2816 }
2817
2818 /* get new irq counts */
2819 spin_lock_irqsave(&info->irq_spinlock,flags);
2820 cnow = info->icount;
2821 set_current_state(TASK_INTERRUPTIBLE);
2822 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2823
2824 /* if no change, wait aborted for some reason */
2825 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
2826 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts) {
2827 rc = -EIO;
2828 break;
2829 }
2830
2831 /* check for change in caller specified modem input */
2832 if ((arg & TIOCM_RNG && cnow.rng != cprev.rng) ||
2833 (arg & TIOCM_DSR && cnow.dsr != cprev.dsr) ||
2834 (arg & TIOCM_CD && cnow.dcd != cprev.dcd) ||
2835 (arg & TIOCM_CTS && cnow.cts != cprev.cts)) {
2836 rc = 0;
2837 break;
2838 }
2839
2840 cprev = cnow;
2841 }
2842 remove_wait_queue(&info->status_event_wait_q, &wait);
2843 set_current_state(TASK_RUNNING);
2844 return rc;
2845}
2846
2847/* return the state of the serial control and status signals
2848 */
2849static int tiocmget(struct tty_struct *tty, struct file *file)
2850{
2851 struct mgsl_struct *info = tty->driver_data;
2852 unsigned int result;
2853 unsigned long flags;
2854
2855 spin_lock_irqsave(&info->irq_spinlock,flags);
2856 usc_get_serial_signals(info);
2857 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2858
2859 result = ((info->serial_signals & SerialSignal_RTS) ? TIOCM_RTS:0) +
2860 ((info->serial_signals & SerialSignal_DTR) ? TIOCM_DTR:0) +
2861 ((info->serial_signals & SerialSignal_DCD) ? TIOCM_CAR:0) +
2862 ((info->serial_signals & SerialSignal_RI) ? TIOCM_RNG:0) +
2863 ((info->serial_signals & SerialSignal_DSR) ? TIOCM_DSR:0) +
2864 ((info->serial_signals & SerialSignal_CTS) ? TIOCM_CTS:0);
2865
2866 if (debug_level >= DEBUG_LEVEL_INFO)
2867 printk("%s(%d):%s tiocmget() value=%08X\n",
2868 __FILE__,__LINE__, info->device_name, result );
2869 return result;
2870}
2871
2872/* set modem control signals (DTR/RTS)
2873 */
2874static int tiocmset(struct tty_struct *tty, struct file *file,
2875 unsigned int set, unsigned int clear)
2876{
2877 struct mgsl_struct *info = tty->driver_data;
2878 unsigned long flags;
2879
2880 if (debug_level >= DEBUG_LEVEL_INFO)
2881 printk("%s(%d):%s tiocmset(%x,%x)\n",
2882 __FILE__,__LINE__,info->device_name, set, clear);
2883
2884 if (set & TIOCM_RTS)
2885 info->serial_signals |= SerialSignal_RTS;
2886 if (set & TIOCM_DTR)
2887 info->serial_signals |= SerialSignal_DTR;
2888 if (clear & TIOCM_RTS)
2889 info->serial_signals &= ~SerialSignal_RTS;
2890 if (clear & TIOCM_DTR)
2891 info->serial_signals &= ~SerialSignal_DTR;
2892
2893 spin_lock_irqsave(&info->irq_spinlock,flags);
2894 usc_set_serial_signals(info);
2895 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2896
2897 return 0;
2898}
2899
2900/* mgsl_break() Set or clear transmit break condition
2901 *
2902 * Arguments: tty pointer to tty instance data
2903 * break_state -1=set break condition, 0=clear
2904 * Return Value: error code
2905 */
2906static int mgsl_break(struct tty_struct *tty, int break_state)
2907{
2908 struct mgsl_struct * info = tty->driver_data;
2909 unsigned long flags;
2910
2911 if (debug_level >= DEBUG_LEVEL_INFO)
2912 printk("%s(%d):mgsl_break(%s,%d)\n",
2913 __FILE__,__LINE__, info->device_name, break_state);
2914
2915 if (mgsl_paranoia_check(info, tty->name, "mgsl_break"))
2916 return -EINVAL;
2917
2918 spin_lock_irqsave(&info->irq_spinlock,flags);
2919 if (break_state == -1)
2920 usc_OutReg(info,IOCR,(u16)(usc_InReg(info,IOCR) | BIT7));
2921 else
2922 usc_OutReg(info,IOCR,(u16)(usc_InReg(info,IOCR) & ~BIT7));
2923 spin_unlock_irqrestore(&info->irq_spinlock,flags);
2924 return 0;
2925
2926} /* end of mgsl_break() */
2927
2928/* mgsl_ioctl() Service an IOCTL request
2929 *
2930 * Arguments:
2931 *
2932 * tty pointer to tty instance data
2933 * file pointer to associated file object for device
2934 * cmd IOCTL command code
2935 * arg command argument/context
2936 *
2937 * Return Value: 0 if success, otherwise error code
2938 */
2939static int mgsl_ioctl(struct tty_struct *tty, struct file * file,
2940 unsigned int cmd, unsigned long arg)
2941{
2942 struct mgsl_struct * info = tty->driver_data;
2943
2944 if (debug_level >= DEBUG_LEVEL_INFO)
2945 printk("%s(%d):mgsl_ioctl %s cmd=%08X\n", __FILE__,__LINE__,
2946 info->device_name, cmd );
2947
2948 if (mgsl_paranoia_check(info, tty->name, "mgsl_ioctl"))
2949 return -ENODEV;
2950
2951 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
2952 (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
2953 if (tty->flags & (1 << TTY_IO_ERROR))
2954 return -EIO;
2955 }
2956
2957 return mgsl_ioctl_common(info, cmd, arg);
2958}
2959
2960static int mgsl_ioctl_common(struct mgsl_struct *info, unsigned int cmd, unsigned long arg)
2961{
2962 int error;
2963 struct mgsl_icount cnow; /* kernel counter temps */
2964 void __user *argp = (void __user *)arg;
2965 struct serial_icounter_struct __user *p_cuser; /* user space */
2966 unsigned long flags;
2967
2968 switch (cmd) {
2969 case MGSL_IOCGPARAMS:
2970 return mgsl_get_params(info, argp);
2971 case MGSL_IOCSPARAMS:
2972 return mgsl_set_params(info, argp);
2973 case MGSL_IOCGTXIDLE:
2974 return mgsl_get_txidle(info, argp);
2975 case MGSL_IOCSTXIDLE:
2976 return mgsl_set_txidle(info,(int)arg);
2977 case MGSL_IOCTXENABLE:
2978 return mgsl_txenable(info,(int)arg);
2979 case MGSL_IOCRXENABLE:
2980 return mgsl_rxenable(info,(int)arg);
2981 case MGSL_IOCTXABORT:
2982 return mgsl_txabort(info);
2983 case MGSL_IOCGSTATS:
2984 return mgsl_get_stats(info, argp);
2985 case MGSL_IOCWAITEVENT:
2986 return mgsl_wait_event(info, argp);
2987 case MGSL_IOCLOOPTXDONE:
2988 return mgsl_loopmode_send_done(info);
2989 /* Wait for modem input (DCD,RI,DSR,CTS) change
2990 * as specified by mask in arg (TIOCM_RNG/DSR/CD/CTS)
2991 */
2992 case TIOCMIWAIT:
2993 return modem_input_wait(info,(int)arg);
2994
2995 /*
2996 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
2997 * Return: write counters to the user passed counter struct
2998 * NB: both 1->0 and 0->1 transitions are counted except for
2999 * RI where only 0->1 is counted.
3000 */
3001 case TIOCGICOUNT:
3002 spin_lock_irqsave(&info->irq_spinlock,flags);
3003 cnow = info->icount;
3004 spin_unlock_irqrestore(&info->irq_spinlock,flags);
3005 p_cuser = argp;
3006 PUT_USER(error,cnow.cts, &p_cuser->cts);
3007 if (error) return error;
3008 PUT_USER(error,cnow.dsr, &p_cuser->dsr);
3009 if (error) return error;
3010 PUT_USER(error,cnow.rng, &p_cuser->rng);
3011 if (error) return error;
3012 PUT_USER(error,cnow.dcd, &p_cuser->dcd);
3013 if (error) return error;
3014 PUT_USER(error,cnow.rx, &p_cuser->rx);
3015 if (error) return error;
3016 PUT_USER(error,cnow.tx, &p_cuser->tx);
3017 if (error) return error;
3018 PUT_USER(error,cnow.frame, &p_cuser->frame);
3019 if (error) return error;
3020 PUT_USER(error,cnow.overrun, &p_cuser->overrun);
3021 if (error) return error;
3022 PUT_USER(error,cnow.parity, &p_cuser->parity);
3023 if (error) return error;
3024 PUT_USER(error,cnow.brk, &p_cuser->brk);
3025 if (error) return error;
3026 PUT_USER(error,cnow.buf_overrun, &p_cuser->buf_overrun);
3027 if (error) return error;
3028 return 0;
3029 default:
3030 return -ENOIOCTLCMD;
3031 }
3032 return 0;
3033}
3034
3035/* mgsl_set_termios()
3036 *
3037 * Set new termios settings
3038 *
3039 * Arguments:
3040 *
3041 * tty pointer to tty structure
3042 * termios pointer to buffer to hold returned old termios
3043 *
3044 * Return Value: None
3045 */
3046static void mgsl_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
3047{
3048 struct mgsl_struct *info = tty->driver_data;
3049 unsigned long flags;
3050
3051 if (debug_level >= DEBUG_LEVEL_INFO)
3052 printk("%s(%d):mgsl_set_termios %s\n", __FILE__,__LINE__,
3053 tty->driver->name );
3054
3055 mgsl_change_params(info);
3056
3057 /* Handle transition to B0 status */
3058 if (old_termios->c_cflag & CBAUD &&
3059 !(tty->termios->c_cflag & CBAUD)) {
3060 info->serial_signals &= ~(SerialSignal_RTS + SerialSignal_DTR);
3061 spin_lock_irqsave(&info->irq_spinlock,flags);
3062 usc_set_serial_signals(info);
3063 spin_unlock_irqrestore(&info->irq_spinlock,flags);
3064 }
3065
3066 /* Handle transition away from B0 status */
3067 if (!(old_termios->c_cflag & CBAUD) &&
3068 tty->termios->c_cflag & CBAUD) {
3069 info->serial_signals |= SerialSignal_DTR;
3070 if (!(tty->termios->c_cflag & CRTSCTS) ||
3071 !test_bit(TTY_THROTTLED, &tty->flags)) {
3072 info->serial_signals |= SerialSignal_RTS;
3073 }
3074 spin_lock_irqsave(&info->irq_spinlock,flags);
3075 usc_set_serial_signals(info);
3076 spin_unlock_irqrestore(&info->irq_spinlock,flags);
3077 }
3078
3079 /* Handle turning off CRTSCTS */
3080 if (old_termios->c_cflag & CRTSCTS &&
3081 !(tty->termios->c_cflag & CRTSCTS)) {
3082 tty->hw_stopped = 0;
3083 mgsl_start(tty);
3084 }
3085
3086} /* end of mgsl_set_termios() */
3087
3088/* mgsl_close()
3089 *
3090 * Called when port is closed. Wait for remaining data to be
3091 * sent. Disable port and free resources.
3092 *
3093 * Arguments:
3094 *
3095 * tty pointer to open tty structure
3096 * filp pointer to open file object
3097 *
3098 * Return Value: None
3099 */
3100static void mgsl_close(struct tty_struct *tty, struct file * filp)
3101{
3102 struct mgsl_struct * info = tty->driver_data;
3103
3104 if (mgsl_paranoia_check(info, tty->name, "mgsl_close"))
3105 return;
3106
3107 if (debug_level >= DEBUG_LEVEL_INFO)
3108 printk("%s(%d):mgsl_close(%s) entry, count=%d\n",
3109 __FILE__,__LINE__, info->device_name, info->port.count);
3110
3111 if (tty_port_close_start(&info->port, tty, filp) == 0)
3112 goto cleanup;
3113
3114 mutex_lock(&info->port.mutex);
3115 if (info->port.flags & ASYNC_INITIALIZED)
3116 mgsl_wait_until_sent(tty, info->timeout);
3117 mgsl_flush_buffer(tty);
3118 tty_ldisc_flush(tty);
3119 shutdown(info);
3120 mutex_unlock(&info->port.mutex);
3121
3122 tty_port_close_end(&info->port, tty);
3123 info->port.tty = NULL;
3124cleanup:
3125 if (debug_level >= DEBUG_LEVEL_INFO)
3126 printk("%s(%d):mgsl_close(%s) exit, count=%d\n", __FILE__,__LINE__,
3127 tty->driver->name, info->port.count);
3128
3129} /* end of mgsl_close() */
3130
3131/* mgsl_wait_until_sent()
3132 *
3133 * Wait until the transmitter is empty.
3134 *
3135 * Arguments:
3136 *
3137 * tty pointer to tty info structure
3138 * timeout time to wait for send completion
3139 *
3140 * Return Value: None
3141 */
3142static void mgsl_wait_until_sent(struct tty_struct *tty, int timeout)
3143{
3144 struct mgsl_struct * info = tty->driver_data;
3145 unsigned long orig_jiffies, char_time;
3146
3147 if (!info )
3148 return;
3149
3150 if (debug_level >= DEBUG_LEVEL_INFO)
3151 printk("%s(%d):mgsl_wait_until_sent(%s) entry\n",
3152 __FILE__,__LINE__, info->device_name );
3153
3154 if (mgsl_paranoia_check(info, tty->name, "mgsl_wait_until_sent"))
3155 return;
3156
3157 if (!(info->port.flags & ASYNC_INITIALIZED))
3158 goto exit;
3159
3160 orig_jiffies = jiffies;
3161
3162 /* Set check interval to 1/5 of estimated time to
3163 * send a character, and make it at least 1. The check
3164 * interval should also be less than the timeout.
3165 * Note: use tight timings here to satisfy the NIST-PCTS.
3166 */
3167
3168 if ( info->params.data_rate ) {
3169 char_time = info->timeout/(32 * 5);
3170 if (!char_time)
3171 char_time++;
3172 } else
3173 char_time = 1;
3174
3175 if (timeout)
3176 char_time = min_t(unsigned long, char_time, timeout);
3177
3178 if ( info->params.mode == MGSL_MODE_HDLC ||
3179 info->params.mode == MGSL_MODE_RAW ) {
3180 while (info->tx_active) {
3181 msleep_interruptible(jiffies_to_msecs(char_time));
3182 if (signal_pending(current))
3183 break;
3184 if (timeout && time_after(jiffies, orig_jiffies + timeout))
3185 break;
3186 }
3187 } else {
3188 while (!(usc_InReg(info,TCSR) & TXSTATUS_ALL_SENT) &&
3189 info->tx_enabled) {
3190 msleep_interruptible(jiffies_to_msecs(char_time));
3191 if (signal_pending(current))
3192 break;
3193 if (timeout && time_after(jiffies, orig_jiffies + timeout))
3194 break;
3195 }
3196 }
3197
3198exit:
3199 if (debug_level >= DEBUG_LEVEL_INFO)
3200 printk("%s(%d):mgsl_wait_until_sent(%s) exit\n",
3201 __FILE__,__LINE__, info->device_name );
3202
3203} /* end of mgsl_wait_until_sent() */
3204
3205/* mgsl_hangup()
3206 *
3207 * Called by tty_hangup() when a hangup is signaled.
3208 * This is the same as to closing all open files for the port.
3209 *
3210 * Arguments: tty pointer to associated tty object
3211 * Return Value: None
3212 */
3213static void mgsl_hangup(struct tty_struct *tty)
3214{
3215 struct mgsl_struct * info = tty->driver_data;
3216
3217 if (debug_level >= DEBUG_LEVEL_INFO)
3218 printk("%s(%d):mgsl_hangup(%s)\n",
3219 __FILE__,__LINE__, info->device_name );
3220
3221 if (mgsl_paranoia_check(info, tty->name, "mgsl_hangup"))
3222 return;
3223
3224 mgsl_flush_buffer(tty);
3225 shutdown(info);
3226
3227 info->port.count = 0;
3228 info->port.flags &= ~ASYNC_NORMAL_ACTIVE;
3229 info->port.tty = NULL;
3230
3231 wake_up_interruptible(&info->port.open_wait);
3232
3233} /* end of mgsl_hangup() */
3234
3235/*
3236 * carrier_raised()
3237 *
3238 * Return true if carrier is raised
3239 */
3240
3241static int carrier_raised(struct tty_port *port)
3242{
3243 unsigned long flags;
3244 struct mgsl_struct *info = container_of(port, struct mgsl_struct, port);
3245
3246 spin_lock_irqsave(&info->irq_spinlock, flags);
3247 usc_get_serial_signals(info);
3248 spin_unlock_irqrestore(&info->irq_spinlock, flags);
3249 return (info->serial_signals & SerialSignal_DCD) ? 1 : 0;
3250}
3251
3252static void dtr_rts(struct tty_port *port, int on)
3253{
3254 struct mgsl_struct *info = container_of(port, struct mgsl_struct, port);
3255 unsigned long flags;
3256
3257 spin_lock_irqsave(&info->irq_spinlock,flags);
3258 if (on)
3259 info->serial_signals |= SerialSignal_RTS + SerialSignal_DTR;
3260 else
3261 info->serial_signals &= ~(SerialSignal_RTS + SerialSignal_DTR);
3262 usc_set_serial_signals(info);
3263 spin_unlock_irqrestore(&info->irq_spinlock,flags);
3264}
3265
3266
3267/* block_til_ready()
3268 *
3269 * Block the current process until the specified port
3270 * is ready to be opened.
3271 *
3272 * Arguments:
3273 *
3274 * tty pointer to tty info structure
3275 * filp pointer to open file object
3276 * info pointer to device instance data
3277 *
3278 * Return Value: 0 if success, otherwise error code
3279 */
3280static int block_til_ready(struct tty_struct *tty, struct file * filp,
3281 struct mgsl_struct *info)
3282{
3283 DECLARE_WAITQUEUE(wait, current);
3284 int retval;
3285 bool do_clocal = false;
3286 bool extra_count = false;
3287 unsigned long flags;
3288 int dcd;
3289 struct tty_port *port = &info->port;
3290
3291 if (debug_level >= DEBUG_LEVEL_INFO)
3292 printk("%s(%d):block_til_ready on %s\n",
3293 __FILE__,__LINE__, tty->driver->name );
3294
3295 if (filp->f_flags & O_NONBLOCK || tty->flags & (1 << TTY_IO_ERROR)){
3296 /* nonblock mode is set or port is not enabled */
3297 port->flags |= ASYNC_NORMAL_ACTIVE;
3298 return 0;
3299 }
3300
3301 if (tty->termios->c_cflag & CLOCAL)
3302 do_clocal = true;
3303
3304 /* Wait for carrier detect and the line to become
3305 * free (i.e., not in use by the callout). While we are in
3306 * this loop, port->count is dropped by one, so that
3307 * mgsl_close() knows when to free things. We restore it upon
3308 * exit, either normal or abnormal.
3309 */
3310
3311 retval = 0;
3312 add_wait_queue(&port->open_wait, &wait);
3313
3314 if (debug_level >= DEBUG_LEVEL_INFO)
3315 printk("%s(%d):block_til_ready before block on %s count=%d\n",
3316 __FILE__,__LINE__, tty->driver->name, port->count );
3317
3318 spin_lock_irqsave(&info->irq_spinlock, flags);
3319 if (!tty_hung_up_p(filp)) {
3320 extra_count = true;
3321 port->count--;
3322 }
3323 spin_unlock_irqrestore(&info->irq_spinlock, flags);
3324 port->blocked_open++;
3325
3326 while (1) {
3327 if (tty->termios->c_cflag & CBAUD)
3328 tty_port_raise_dtr_rts(port);
3329
3330 set_current_state(TASK_INTERRUPTIBLE);
3331
3332 if (tty_hung_up_p(filp) || !(port->flags & ASYNC_INITIALIZED)){
3333 retval = (port->flags & ASYNC_HUP_NOTIFY) ?
3334 -EAGAIN : -ERESTARTSYS;
3335 break;
3336 }
3337
3338 dcd = tty_port_carrier_raised(&info->port);
3339
3340 if (!(port->flags & ASYNC_CLOSING) && (do_clocal || dcd))
3341 break;
3342
3343 if (signal_pending(current)) {
3344 retval = -ERESTARTSYS;
3345 break;
3346 }
3347
3348 if (debug_level >= DEBUG_LEVEL_INFO)
3349 printk("%s(%d):block_til_ready blocking on %s count=%d\n",
3350 __FILE__,__LINE__, tty->driver->name, port->count );
3351
3352 tty_unlock();
3353 schedule();
3354 tty_lock();
3355 }
3356
3357 set_current_state(TASK_RUNNING);
3358 remove_wait_queue(&port->open_wait, &wait);
3359
3360 /* FIXME: Racy on hangup during close wait */
3361 if (extra_count)
3362 port->count++;
3363 port->blocked_open--;
3364
3365 if (debug_level >= DEBUG_LEVEL_INFO)
3366 printk("%s(%d):block_til_ready after blocking on %s count=%d\n",
3367 __FILE__,__LINE__, tty->driver->name, port->count );
3368
3369 if (!retval)
3370 port->flags |= ASYNC_NORMAL_ACTIVE;
3371
3372 return retval;
3373
3374} /* end of block_til_ready() */
3375
3376/* mgsl_open()
3377 *
3378 * Called when a port is opened. Init and enable port.
3379 * Perform serial-specific initialization for the tty structure.
3380 *
3381 * Arguments: tty pointer to tty info structure
3382 * filp associated file pointer
3383 *
3384 * Return Value: 0 if success, otherwise error code
3385 */
3386static int mgsl_open(struct tty_struct *tty, struct file * filp)
3387{
3388 struct mgsl_struct *info;
3389 int retval, line;
3390 unsigned long flags;
3391
3392 /* verify range of specified line number */
3393 line = tty->index;
3394 if ((line < 0) || (line >= mgsl_device_count)) {
3395 printk("%s(%d):mgsl_open with invalid line #%d.\n",
3396 __FILE__,__LINE__,line);
3397 return -ENODEV;
3398 }
3399
3400 /* find the info structure for the specified line */
3401 info = mgsl_device_list;
3402 while(info && info->line != line)
3403 info = info->next_device;
3404 if (mgsl_paranoia_check(info, tty->name, "mgsl_open"))
3405 return -ENODEV;
3406
3407 tty->driver_data = info;
3408 info->port.tty = tty;
3409
3410 if (debug_level >= DEBUG_LEVEL_INFO)
3411 printk("%s(%d):mgsl_open(%s), old ref count = %d\n",
3412 __FILE__,__LINE__,tty->driver->name, info->port.count);
3413
3414 /* If port is closing, signal caller to try again */
3415 if (tty_hung_up_p(filp) || info->port.flags & ASYNC_CLOSING){
3416 if (info->port.flags & ASYNC_CLOSING)
3417 interruptible_sleep_on(&info->port.close_wait);
3418 retval = ((info->port.flags & ASYNC_HUP_NOTIFY) ?
3419 -EAGAIN : -ERESTARTSYS);
3420 goto cleanup;
3421 }
3422
3423 info->port.tty->low_latency = (info->port.flags & ASYNC_LOW_LATENCY) ? 1 : 0;
3424
3425 spin_lock_irqsave(&info->netlock, flags);
3426 if (info->netcount) {
3427 retval = -EBUSY;
3428 spin_unlock_irqrestore(&info->netlock, flags);
3429 goto cleanup;
3430 }
3431 info->port.count++;
3432 spin_unlock_irqrestore(&info->netlock, flags);
3433
3434 if (info->port.count == 1) {
3435 /* 1st open on this device, init hardware */
3436 retval = startup(info);
3437 if (retval < 0)
3438 goto cleanup;
3439 }
3440
3441 retval = block_til_ready(tty, filp, info);
3442 if (retval) {
3443 if (debug_level >= DEBUG_LEVEL_INFO)
3444 printk("%s(%d):block_til_ready(%s) returned %d\n",
3445 __FILE__,__LINE__, info->device_name, retval);
3446 goto cleanup;
3447 }
3448
3449 if (debug_level >= DEBUG_LEVEL_INFO)
3450 printk("%s(%d):mgsl_open(%s) success\n",
3451 __FILE__,__LINE__, info->device_name);
3452 retval = 0;
3453
3454cleanup:
3455 if (retval) {
3456 if (tty->count == 1)
3457 info->port.tty = NULL; /* tty layer will release tty struct */
3458 if(info->port.count)
3459 info->port.count--;
3460 }
3461
3462 return retval;
3463
3464} /* end of mgsl_open() */
3465
3466/*
3467 * /proc fs routines....
3468 */
3469
3470static inline void line_info(struct seq_file *m, struct mgsl_struct *info)
3471{
3472 char stat_buf[30];
3473 unsigned long flags;
3474
3475 if (info->bus_type == MGSL_BUS_TYPE_PCI) {
3476 seq_printf(m, "%s:PCI io:%04X irq:%d mem:%08X lcr:%08X",
3477 info->device_name, info->io_base, info->irq_level,
3478 info->phys_memory_base, info->phys_lcr_base);
3479 } else {
3480 seq_printf(m, "%s:(E)ISA io:%04X irq:%d dma:%d",
3481 info->device_name, info->io_base,
3482 info->irq_level, info->dma_level);
3483 }
3484
3485 /* output current serial signal states */
3486 spin_lock_irqsave(&info->irq_spinlock,flags);
3487 usc_get_serial_signals(info);
3488 spin_unlock_irqrestore(&info->irq_spinlock,flags);
3489
3490 stat_buf[0] = 0;
3491 stat_buf[1] = 0;
3492 if (info->serial_signals & SerialSignal_RTS)
3493 strcat(stat_buf, "|RTS");
3494 if (info->serial_signals & SerialSignal_CTS)
3495 strcat(stat_buf, "|CTS");
3496 if (info->serial_signals & SerialSignal_DTR)
3497 strcat(stat_buf, "|DTR");
3498 if (info->serial_signals & SerialSignal_DSR)
3499 strcat(stat_buf, "|DSR");
3500 if (info->serial_signals & SerialSignal_DCD)
3501 strcat(stat_buf, "|CD");
3502 if (info->serial_signals & SerialSignal_RI)
3503 strcat(stat_buf, "|RI");
3504
3505 if (info->params.mode == MGSL_MODE_HDLC ||
3506 info->params.mode == MGSL_MODE_RAW ) {
3507 seq_printf(m, " HDLC txok:%d rxok:%d",
3508 info->icount.txok, info->icount.rxok);
3509 if (info->icount.txunder)
3510 seq_printf(m, " txunder:%d", info->icount.txunder);
3511 if (info->icount.txabort)
3512 seq_printf(m, " txabort:%d", info->icount.txabort);
3513 if (info->icount.rxshort)
3514 seq_printf(m, " rxshort:%d", info->icount.rxshort);
3515 if (info->icount.rxlong)
3516 seq_printf(m, " rxlong:%d", info->icount.rxlong);
3517 if (info->icount.rxover)
3518 seq_printf(m, " rxover:%d", info->icount.rxover);
3519 if (info->icount.rxcrc)
3520 seq_printf(m, " rxcrc:%d", info->icount.rxcrc);
3521 } else {
3522 seq_printf(m, " ASYNC tx:%d rx:%d",
3523 info->icount.tx, info->icount.rx);
3524 if (info->icount.frame)
3525 seq_printf(m, " fe:%d", info->icount.frame);
3526 if (info->icount.parity)
3527 seq_printf(m, " pe:%d", info->icount.parity);
3528 if (info->icount.brk)
3529 seq_printf(m, " brk:%d", info->icount.brk);
3530 if (info->icount.overrun)
3531 seq_printf(m, " oe:%d", info->icount.overrun);
3532 }
3533
3534 /* Append serial signal status to end */
3535 seq_printf(m, " %s\n", stat_buf+1);
3536
3537 seq_printf(m, "txactive=%d bh_req=%d bh_run=%d pending_bh=%x\n",
3538 info->tx_active,info->bh_requested,info->bh_running,
3539 info->pending_bh);
3540
3541 spin_lock_irqsave(&info->irq_spinlock,flags);
3542 {
3543 u16 Tcsr = usc_InReg( info, TCSR );
3544 u16 Tdmr = usc_InDmaReg( info, TDMR );
3545 u16 Ticr = usc_InReg( info, TICR );
3546 u16 Rscr = usc_InReg( info, RCSR );
3547 u16 Rdmr = usc_InDmaReg( info, RDMR );
3548 u16 Ricr = usc_InReg( info, RICR );
3549 u16 Icr = usc_InReg( info, ICR );
3550 u16 Dccr = usc_InReg( info, DCCR );
3551 u16 Tmr = usc_InReg( info, TMR );
3552 u16 Tccr = usc_InReg( info, TCCR );
3553 u16 Ccar = inw( info->io_base + CCAR );
3554 seq_printf(m, "tcsr=%04X tdmr=%04X ticr=%04X rcsr=%04X rdmr=%04X\n"
3555 "ricr=%04X icr =%04X dccr=%04X tmr=%04X tccr=%04X ccar=%04X\n",
3556 Tcsr,Tdmr,Ticr,Rscr,Rdmr,Ricr,Icr,Dccr,Tmr,Tccr,Ccar );
3557 }
3558 spin_unlock_irqrestore(&info->irq_spinlock,flags);
3559}
3560
3561/* Called to print information about devices */
3562static int mgsl_proc_show(struct seq_file *m, void *v)
3563{
3564 struct mgsl_struct *info;
3565
3566 seq_printf(m, "synclink driver:%s\n", driver_version);
3567
3568 info = mgsl_device_list;
3569 while( info ) {
3570 line_info(m, info);
3571 info = info->next_device;
3572 }
3573 return 0;
3574}
3575
3576static int mgsl_proc_open(struct inode *inode, struct file *file)
3577{
3578 return single_open(file, mgsl_proc_show, NULL);
3579}
3580
3581static const struct file_operations mgsl_proc_fops = {
3582 .owner = THIS_MODULE,
3583 .open = mgsl_proc_open,
3584 .read = seq_read,
3585 .llseek = seq_lseek,
3586 .release = single_release,
3587};
3588
3589/* mgsl_allocate_dma_buffers()
3590 *
3591 * Allocate and format DMA buffers (ISA adapter)
3592 * or format shared memory buffers (PCI adapter).
3593 *
3594 * Arguments: info pointer to device instance data
3595 * Return Value: 0 if success, otherwise error
3596 */
3597static int mgsl_allocate_dma_buffers(struct mgsl_struct *info)
3598{
3599 unsigned short BuffersPerFrame;
3600
3601 info->last_mem_alloc = 0;
3602
3603 /* Calculate the number of DMA buffers necessary to hold the */
3604 /* largest allowable frame size. Note: If the max frame size is */
3605 /* not an even multiple of the DMA buffer size then we need to */
3606 /* round the buffer count per frame up one. */
3607
3608 BuffersPerFrame = (unsigned short)(info->max_frame_size/DMABUFFERSIZE);
3609 if ( info->max_frame_size % DMABUFFERSIZE )
3610 BuffersPerFrame++;
3611
3612 if ( info->bus_type == MGSL_BUS_TYPE_PCI ) {
3613 /*
3614 * The PCI adapter has 256KBytes of shared memory to use.
3615 * This is 64 PAGE_SIZE buffers.
3616 *
3617 * The first page is used for padding at this time so the
3618 * buffer list does not begin at offset 0 of the PCI
3619 * adapter's shared memory.
3620 *
3621 * The 2nd page is used for the buffer list. A 4K buffer
3622 * list can hold 128 DMA_BUFFER structures at 32 bytes
3623 * each.
3624 *
3625 * This leaves 62 4K pages.
3626 *
3627 * The next N pages are used for transmit frame(s). We
3628 * reserve enough 4K page blocks to hold the required
3629 * number of transmit dma buffers (num_tx_dma_buffers),
3630 * each of MaxFrameSize size.
3631 *
3632 * Of the remaining pages (62-N), determine how many can
3633 * be used to receive full MaxFrameSize inbound frames
3634 */
3635 info->tx_buffer_count = info->num_tx_dma_buffers * BuffersPerFrame;
3636 info->rx_buffer_count = 62 - info->tx_buffer_count;
3637 } else {
3638 /* Calculate the number of PAGE_SIZE buffers needed for */
3639 /* receive and transmit DMA buffers. */
3640
3641
3642 /* Calculate the number of DMA buffers necessary to */
3643 /* hold 7 max size receive frames and one max size transmit frame. */
3644 /* The receive buffer count is bumped by one so we avoid an */
3645 /* End of List condition if all receive buffers are used when */
3646 /* using linked list DMA buffers. */
3647
3648 info->tx_buffer_count = info->num_tx_dma_buffers * BuffersPerFrame;
3649 info->rx_buffer_count = (BuffersPerFrame * MAXRXFRAMES) + 6;
3650
3651 /*
3652 * limit total TxBuffers & RxBuffers to 62 4K total
3653 * (ala PCI Allocation)
3654 */
3655
3656 if ( (info->tx_buffer_count + info->rx_buffer_count) > 62 )
3657 info->rx_buffer_count = 62 - info->tx_buffer_count;
3658
3659 }
3660
3661 if ( debug_level >= DEBUG_LEVEL_INFO )
3662 printk("%s(%d):Allocating %d TX and %d RX DMA buffers.\n",
3663 __FILE__,__LINE__, info->tx_buffer_count,info->rx_buffer_count);
3664
3665 if ( mgsl_alloc_buffer_list_memory( info ) < 0 ||
3666 mgsl_alloc_frame_memory(info, info->rx_buffer_list, info->rx_buffer_count) < 0 ||
3667 mgsl_alloc_frame_memory(info, info->tx_buffer_list, info->tx_buffer_count) < 0 ||
3668 mgsl_alloc_intermediate_rxbuffer_memory(info) < 0 ||
3669 mgsl_alloc_intermediate_txbuffer_memory(info) < 0 ) {
3670 printk("%s(%d):Can't allocate DMA buffer memory\n",__FILE__,__LINE__);
3671 return -ENOMEM;
3672 }
3673
3674 mgsl_reset_rx_dma_buffers( info );
3675 mgsl_reset_tx_dma_buffers( info );
3676
3677 return 0;
3678
3679} /* end of mgsl_allocate_dma_buffers() */
3680
3681/*
3682 * mgsl_alloc_buffer_list_memory()
3683 *
3684 * Allocate a common DMA buffer for use as the
3685 * receive and transmit buffer lists.
3686 *
3687 * A buffer list is a set of buffer entries where each entry contains
3688 * a pointer to an actual buffer and a pointer to the next buffer entry
3689 * (plus some other info about the buffer).
3690 *
3691 * The buffer entries for a list are built to form a circular list so
3692 * that when the entire list has been traversed you start back at the
3693 * beginning.
3694 *
3695 * This function allocates memory for just the buffer entries.
3696 * The links (pointer to next entry) are filled in with the physical
3697 * address of the next entry so the adapter can navigate the list
3698 * using bus master DMA. The pointers to the actual buffers are filled
3699 * out later when the actual buffers are allocated.
3700 *
3701 * Arguments: info pointer to device instance data
3702 * Return Value: 0 if success, otherwise error
3703 */
3704static int mgsl_alloc_buffer_list_memory( struct mgsl_struct *info )
3705{
3706 unsigned int i;
3707
3708 if ( info->bus_type == MGSL_BUS_TYPE_PCI ) {
3709 /* PCI adapter uses shared memory. */
3710 info->buffer_list = info->memory_base + info->last_mem_alloc;
3711 info->buffer_list_phys = info->last_mem_alloc;
3712 info->last_mem_alloc += BUFFERLISTSIZE;
3713 } else {
3714 /* ISA adapter uses system memory. */
3715 /* The buffer lists are allocated as a common buffer that both */
3716 /* the processor and adapter can access. This allows the driver to */
3717 /* inspect portions of the buffer while other portions are being */
3718 /* updated by the adapter using Bus Master DMA. */
3719
3720 info->buffer_list = dma_alloc_coherent(NULL, BUFFERLISTSIZE, &info->buffer_list_dma_addr, GFP_KERNEL);
3721 if (info->buffer_list == NULL)
3722 return -ENOMEM;
3723 info->buffer_list_phys = (u32)(info->buffer_list_dma_addr);
3724 }
3725
3726 /* We got the memory for the buffer entry lists. */
3727 /* Initialize the memory block to all zeros. */
3728 memset( info->buffer_list, 0, BUFFERLISTSIZE );
3729
3730 /* Save virtual address pointers to the receive and */
3731 /* transmit buffer lists. (Receive 1st). These pointers will */
3732 /* be used by the processor to access the lists. */
3733 info->rx_buffer_list = (DMABUFFERENTRY *)info->buffer_list;
3734 info->tx_buffer_list = (DMABUFFERENTRY *)info->buffer_list;
3735 info->tx_buffer_list += info->rx_buffer_count;
3736
3737 /*
3738 * Build the links for the buffer entry lists such that
3739 * two circular lists are built. (Transmit and Receive).
3740 *
3741 * Note: the links are physical addresses
3742 * which are read by the adapter to determine the next
3743 * buffer entry to use.
3744 */
3745
3746 for ( i = 0; i < info->rx_buffer_count; i++ ) {
3747 /* calculate and store physical address of this buffer entry */
3748 info->rx_buffer_list[i].phys_entry =
3749 info->buffer_list_phys + (i * sizeof(DMABUFFERENTRY));
3750
3751 /* calculate and store physical address of */
3752 /* next entry in cirular list of entries */
3753
3754 info->rx_buffer_list[i].link = info->buffer_list_phys;
3755
3756 if ( i < info->rx_buffer_count - 1 )
3757 info->rx_buffer_list[i].link += (i + 1) * sizeof(DMABUFFERENTRY);
3758 }
3759
3760 for ( i = 0; i < info->tx_buffer_count; i++ ) {
3761 /* calculate and store physical address of this buffer entry */
3762 info->tx_buffer_list[i].phys_entry = info->buffer_list_phys +
3763 ((info->rx_buffer_count + i) * sizeof(DMABUFFERENTRY));
3764
3765 /* calculate and store physical address of */
3766 /* next entry in cirular list of entries */
3767
3768 info->tx_buffer_list[i].link = info->buffer_list_phys +
3769 info->rx_buffer_count * sizeof(DMABUFFERENTRY);
3770
3771 if ( i < info->tx_buffer_count - 1 )
3772 info->tx_buffer_list[i].link += (i + 1) * sizeof(DMABUFFERENTRY);
3773 }
3774
3775 return 0;
3776
3777} /* end of mgsl_alloc_buffer_list_memory() */
3778
3779/* Free DMA buffers allocated for use as the
3780 * receive and transmit buffer lists.
3781 * Warning:
3782 *
3783 * The data transfer buffers associated with the buffer list
3784 * MUST be freed before freeing the buffer list itself because
3785 * the buffer list contains the information necessary to free
3786 * the individual buffers!
3787 */
3788static void mgsl_free_buffer_list_memory( struct mgsl_struct *info )
3789{
3790 if (info->buffer_list && info->bus_type != MGSL_BUS_TYPE_PCI)
3791 dma_free_coherent(NULL, BUFFERLISTSIZE, info->buffer_list, info->buffer_list_dma_addr);
3792
3793 info->buffer_list = NULL;
3794 info->rx_buffer_list = NULL;
3795 info->tx_buffer_list = NULL;
3796
3797} /* end of mgsl_free_buffer_list_memory() */
3798
3799/*
3800 * mgsl_alloc_frame_memory()
3801 *
3802 * Allocate the frame DMA buffers used by the specified buffer list.
3803 * Each DMA buffer will be one memory page in size. This is necessary
3804 * because memory can fragment enough that it may be impossible
3805 * contiguous pages.
3806 *
3807 * Arguments:
3808 *
3809 * info pointer to device instance data
3810 * BufferList pointer to list of buffer entries
3811 * Buffercount count of buffer entries in buffer list
3812 *
3813 * Return Value: 0 if success, otherwise -ENOMEM
3814 */
3815static int mgsl_alloc_frame_memory(struct mgsl_struct *info,DMABUFFERENTRY *BufferList,int Buffercount)
3816{
3817 int i;
3818 u32 phys_addr;
3819
3820 /* Allocate page sized buffers for the receive buffer list */
3821
3822 for ( i = 0; i < Buffercount; i++ ) {
3823 if ( info->bus_type == MGSL_BUS_TYPE_PCI ) {
3824 /* PCI adapter uses shared memory buffers. */
3825 BufferList[i].virt_addr = info->memory_base + info->last_mem_alloc;
3826 phys_addr = info->last_mem_alloc;
3827 info->last_mem_alloc += DMABUFFERSIZE;
3828 } else {
3829 /* ISA adapter uses system memory. */
3830 BufferList[i].virt_addr = dma_alloc_coherent(NULL, DMABUFFERSIZE, &BufferList[i].dma_addr, GFP_KERNEL);
3831 if (BufferList[i].virt_addr == NULL)
3832 return -ENOMEM;
3833 phys_addr = (u32)(BufferList[i].dma_addr);
3834 }
3835 BufferList[i].phys_addr = phys_addr;
3836 }
3837
3838 return 0;
3839
3840} /* end of mgsl_alloc_frame_memory() */
3841
3842/*
3843 * mgsl_free_frame_memory()
3844 *
3845 * Free the buffers associated with
3846 * each buffer entry of a buffer list.
3847 *
3848 * Arguments:
3849 *
3850 * info pointer to device instance data
3851 * BufferList pointer to list of buffer entries
3852 * Buffercount count of buffer entries in buffer list
3853 *
3854 * Return Value: None
3855 */
3856static void mgsl_free_frame_memory(struct mgsl_struct *info, DMABUFFERENTRY *BufferList, int Buffercount)
3857{
3858 int i;
3859
3860 if ( BufferList ) {
3861 for ( i = 0 ; i < Buffercount ; i++ ) {
3862 if ( BufferList[i].virt_addr ) {
3863 if ( info->bus_type != MGSL_BUS_TYPE_PCI )
3864 dma_free_coherent(NULL, DMABUFFERSIZE, BufferList[i].virt_addr, BufferList[i].dma_addr);
3865 BufferList[i].virt_addr = NULL;
3866 }
3867 }
3868 }
3869
3870} /* end of mgsl_free_frame_memory() */
3871
3872/* mgsl_free_dma_buffers()
3873 *
3874 * Free DMA buffers
3875 *
3876 * Arguments: info pointer to device instance data
3877 * Return Value: None
3878 */
3879static void mgsl_free_dma_buffers( struct mgsl_struct *info )
3880{
3881 mgsl_free_frame_memory( info, info->rx_buffer_list, info->rx_buffer_count );
3882 mgsl_free_frame_memory( info, info->tx_buffer_list, info->tx_buffer_count );
3883 mgsl_free_buffer_list_memory( info );
3884
3885} /* end of mgsl_free_dma_buffers() */
3886
3887
3888/*
3889 * mgsl_alloc_intermediate_rxbuffer_memory()
3890 *
3891 * Allocate a buffer large enough to hold max_frame_size. This buffer
3892 * is used to pass an assembled frame to the line discipline.
3893 *
3894 * Arguments:
3895 *
3896 * info pointer to device instance data
3897 *
3898 * Return Value: 0 if success, otherwise -ENOMEM
3899 */
3900static int mgsl_alloc_intermediate_rxbuffer_memory(struct mgsl_struct *info)
3901{
3902 info->intermediate_rxbuffer = kmalloc(info->max_frame_size, GFP_KERNEL | GFP_DMA);
3903 if ( info->intermediate_rxbuffer == NULL )
3904 return -ENOMEM;
3905
3906 return 0;
3907
3908} /* end of mgsl_alloc_intermediate_rxbuffer_memory() */
3909
3910/*
3911 * mgsl_free_intermediate_rxbuffer_memory()
3912 *
3913 *
3914 * Arguments:
3915 *
3916 * info pointer to device instance data
3917 *
3918 * Return Value: None
3919 */
3920static void mgsl_free_intermediate_rxbuffer_memory(struct mgsl_struct *info)
3921{
3922 kfree(info->intermediate_rxbuffer);
3923 info->intermediate_rxbuffer = NULL;
3924
3925} /* end of mgsl_free_intermediate_rxbuffer_memory() */
3926
3927/*
3928 * mgsl_alloc_intermediate_txbuffer_memory()
3929 *
3930 * Allocate intermdiate transmit buffer(s) large enough to hold max_frame_size.
3931 * This buffer is used to load transmit frames into the adapter's dma transfer
3932 * buffers when there is sufficient space.
3933 *
3934 * Arguments:
3935 *
3936 * info pointer to device instance data
3937 *
3938 * Return Value: 0 if success, otherwise -ENOMEM
3939 */
3940static int mgsl_alloc_intermediate_txbuffer_memory(struct mgsl_struct *info)
3941{
3942 int i;
3943
3944 if ( debug_level >= DEBUG_LEVEL_INFO )
3945 printk("%s %s(%d) allocating %d tx holding buffers\n",
3946 info->device_name, __FILE__,__LINE__,info->num_tx_holding_buffers);
3947
3948 memset(info->tx_holding_buffers,0,sizeof(info->tx_holding_buffers));
3949
3950 for ( i=0; i<info->num_tx_holding_buffers; ++i) {
3951 info->tx_holding_buffers[i].buffer =
3952 kmalloc(info->max_frame_size, GFP_KERNEL);
3953 if (info->tx_holding_buffers[i].buffer == NULL) {
3954 for (--i; i >= 0; i--) {
3955 kfree(info->tx_holding_buffers[i].buffer);
3956 info->tx_holding_buffers[i].buffer = NULL;
3957 }
3958 return -ENOMEM;
3959 }
3960 }
3961
3962 return 0;
3963
3964} /* end of mgsl_alloc_intermediate_txbuffer_memory() */
3965
3966/*
3967 * mgsl_free_intermediate_txbuffer_memory()
3968 *
3969 *
3970 * Arguments:
3971 *
3972 * info pointer to device instance data
3973 *
3974 * Return Value: None
3975 */
3976static void mgsl_free_intermediate_txbuffer_memory(struct mgsl_struct *info)
3977{
3978 int i;
3979
3980 for ( i=0; i<info->num_tx_holding_buffers; ++i ) {
3981 kfree(info->tx_holding_buffers[i].buffer);
3982 info->tx_holding_buffers[i].buffer = NULL;
3983 }
3984
3985 info->get_tx_holding_index = 0;
3986 info->put_tx_holding_index = 0;
3987 info->tx_holding_count = 0;
3988
3989} /* end of mgsl_free_intermediate_txbuffer_memory() */
3990
3991
3992/*
3993 * load_next_tx_holding_buffer()
3994 *
3995 * attempts to load the next buffered tx request into the
3996 * tx dma buffers
3997 *
3998 * Arguments:
3999 *
4000 * info pointer to device instance data
4001 *
4002 * Return Value: true if next buffered tx request loaded
4003 * into adapter's tx dma buffer,
4004 * false otherwise
4005 */
4006static bool load_next_tx_holding_buffer(struct mgsl_struct *info)
4007{
4008 bool ret = false;
4009
4010 if ( info->tx_holding_count ) {
4011 /* determine if we have enough tx dma buffers
4012 * to accommodate the next tx frame
4013 */
4014 struct tx_holding_buffer *ptx =
4015 &info->tx_holding_buffers[info->get_tx_holding_index];
4016 int num_free = num_free_tx_dma_buffers(info);
4017 int num_needed = ptx->buffer_size / DMABUFFERSIZE;
4018 if ( ptx->buffer_size % DMABUFFERSIZE )
4019 ++num_needed;
4020
4021 if (num_needed <= num_free) {
4022 info->xmit_cnt = ptx->buffer_size;
4023 mgsl_load_tx_dma_buffer(info,ptx->buffer,ptx->buffer_size);
4024
4025 --info->tx_holding_count;
4026 if ( ++info->get_tx_holding_index >= info->num_tx_holding_buffers)
4027 info->get_tx_holding_index=0;
4028
4029 /* restart transmit timer */
4030 mod_timer(&info->tx_timer, jiffies + msecs_to_jiffies(5000));
4031
4032 ret = true;
4033 }
4034 }
4035
4036 return ret;
4037}
4038
4039/*
4040 * save_tx_buffer_request()
4041 *
4042 * attempt to store transmit frame request for later transmission
4043 *
4044 * Arguments:
4045 *
4046 * info pointer to device instance data
4047 * Buffer pointer to buffer containing frame to load
4048 * BufferSize size in bytes of frame in Buffer
4049 *
4050 * Return Value: 1 if able to store, 0 otherwise
4051 */
4052static int save_tx_buffer_request(struct mgsl_struct *info,const char *Buffer, unsigned int BufferSize)
4053{
4054 struct tx_holding_buffer *ptx;
4055
4056 if ( info->tx_holding_count >= info->num_tx_holding_buffers ) {
4057 return 0; /* all buffers in use */
4058 }
4059
4060 ptx = &info->tx_holding_buffers[info->put_tx_holding_index];
4061 ptx->buffer_size = BufferSize;
4062 memcpy( ptx->buffer, Buffer, BufferSize);
4063
4064 ++info->tx_holding_count;
4065 if ( ++info->put_tx_holding_index >= info->num_tx_holding_buffers)
4066 info->put_tx_holding_index=0;
4067
4068 return 1;
4069}
4070
4071static int mgsl_claim_resources(struct mgsl_struct *info)
4072{
4073 if (request_region(info->io_base,info->io_addr_size,"synclink") == NULL) {
4074 printk( "%s(%d):I/O address conflict on device %s Addr=%08X\n",
4075 __FILE__,__LINE__,info->device_name, info->io_base);
4076 return -ENODEV;
4077 }
4078 info->io_addr_requested = true;
4079
4080 if ( request_irq(info->irq_level,mgsl_interrupt,info->irq_flags,
4081 info->device_name, info ) < 0 ) {
4082 printk( "%s(%d):Cant request interrupt on device %s IRQ=%d\n",
4083 __FILE__,__LINE__,info->device_name, info->irq_level );
4084 goto errout;
4085 }
4086 info->irq_requested = true;
4087
4088 if ( info->bus_type == MGSL_BUS_TYPE_PCI ) {
4089 if (request_mem_region(info->phys_memory_base,0x40000,"synclink") == NULL) {
4090 printk( "%s(%d):mem addr conflict device %s Addr=%08X\n",
4091 __FILE__,__LINE__,info->device_name, info->phys_memory_base);
4092 goto errout;
4093 }
4094 info->shared_mem_requested = true;
4095 if (request_mem_region(info->phys_lcr_base + info->lcr_offset,128,"synclink") == NULL) {
4096 printk( "%s(%d):lcr mem addr conflict device %s Addr=%08X\n",
4097 __FILE__,__LINE__,info->device_name, info->phys_lcr_base + info->lcr_offset);
4098 goto errout;
4099 }
4100 info->lcr_mem_requested = true;
4101
4102 info->memory_base = ioremap_nocache(info->phys_memory_base,
4103 0x40000);
4104 if (!info->memory_base) {
4105 printk( "%s(%d):Cant map shared memory on device %s MemAddr=%08X\n",
4106 __FILE__,__LINE__,info->device_name, info->phys_memory_base );
4107 goto errout;
4108 }
4109
4110 if ( !mgsl_memory_test(info) ) {
4111 printk( "%s(%d):Failed shared memory test %s MemAddr=%08X\n",
4112 __FILE__,__LINE__,info->device_name, info->phys_memory_base );
4113 goto errout;
4114 }
4115
4116 info->lcr_base = ioremap_nocache(info->phys_lcr_base,
4117 PAGE_SIZE);
4118 if (!info->lcr_base) {
4119 printk( "%s(%d):Cant map LCR memory on device %s MemAddr=%08X\n",
4120 __FILE__,__LINE__,info->device_name, info->phys_lcr_base );
4121 goto errout;
4122 }
4123 info->lcr_base += info->lcr_offset;
4124
4125 } else {
4126 /* claim DMA channel */
4127
4128 if (request_dma(info->dma_level,info->device_name) < 0){
4129 printk( "%s(%d):Cant request DMA channel on device %s DMA=%d\n",
4130 __FILE__,__LINE__,info->device_name, info->dma_level );
4131 mgsl_release_resources( info );
4132 return -ENODEV;
4133 }
4134 info->dma_requested = true;
4135
4136 /* ISA adapter uses bus master DMA */
4137 set_dma_mode(info->dma_level,DMA_MODE_CASCADE);
4138 enable_dma(info->dma_level);
4139 }
4140
4141 if ( mgsl_allocate_dma_buffers(info) < 0 ) {
4142 printk( "%s(%d):Cant allocate DMA buffers on device %s DMA=%d\n",
4143 __FILE__,__LINE__,info->device_name, info->dma_level );
4144 goto errout;
4145 }
4146
4147 return 0;
4148errout:
4149 mgsl_release_resources(info);
4150 return -ENODEV;
4151
4152} /* end of mgsl_claim_resources() */
4153
4154static void mgsl_release_resources(struct mgsl_struct *info)
4155{
4156 if ( debug_level >= DEBUG_LEVEL_INFO )
4157 printk( "%s(%d):mgsl_release_resources(%s) entry\n",
4158 __FILE__,__LINE__,info->device_name );
4159
4160 if ( info->irq_requested ) {
4161 free_irq(info->irq_level, info);
4162 info->irq_requested = false;
4163 }
4164 if ( info->dma_requested ) {
4165 disable_dma(info->dma_level);
4166 free_dma(info->dma_level);
4167 info->dma_requested = false;
4168 }
4169 mgsl_free_dma_buffers(info);
4170 mgsl_free_intermediate_rxbuffer_memory(info);
4171 mgsl_free_intermediate_txbuffer_memory(info);
4172
4173 if ( info->io_addr_requested ) {
4174 release_region(info->io_base,info->io_addr_size);
4175 info->io_addr_requested = false;
4176 }
4177 if ( info->shared_mem_requested ) {
4178 release_mem_region(info->phys_memory_base,0x40000);
4179 info->shared_mem_requested = false;
4180 }
4181 if ( info->lcr_mem_requested ) {
4182 release_mem_region(info->phys_lcr_base + info->lcr_offset,128);
4183 info->lcr_mem_requested = false;
4184 }
4185 if (info->memory_base){
4186 iounmap(info->memory_base);
4187 info->memory_base = NULL;
4188 }
4189 if (info->lcr_base){
4190 iounmap(info->lcr_base - info->lcr_offset);
4191 info->lcr_base = NULL;
4192 }
4193
4194 if ( debug_level >= DEBUG_LEVEL_INFO )
4195 printk( "%s(%d):mgsl_release_resources(%s) exit\n",
4196 __FILE__,__LINE__,info->device_name );
4197
4198} /* end of mgsl_release_resources() */
4199
4200/* mgsl_add_device()
4201 *
4202 * Add the specified device instance data structure to the
4203 * global linked list of devices and increment the device count.
4204 *
4205 * Arguments: info pointer to device instance data
4206 * Return Value: None
4207 */
4208static void mgsl_add_device( struct mgsl_struct *info )
4209{
4210 info->next_device = NULL;
4211 info->line = mgsl_device_count;
4212 sprintf(info->device_name,"ttySL%d",info->line);
4213
4214 if (info->line < MAX_TOTAL_DEVICES) {
4215 if (maxframe[info->line])
4216 info->max_frame_size = maxframe[info->line];
4217
4218 if (txdmabufs[info->line]) {
4219 info->num_tx_dma_buffers = txdmabufs[info->line];
4220 if (info->num_tx_dma_buffers < 1)
4221 info->num_tx_dma_buffers = 1;
4222 }
4223
4224 if (txholdbufs[info->line]) {
4225 info->num_tx_holding_buffers = txholdbufs[info->line];
4226 if (info->num_tx_holding_buffers < 1)
4227 info->num_tx_holding_buffers = 1;
4228 else if (info->num_tx_holding_buffers > MAX_TX_HOLDING_BUFFERS)
4229 info->num_tx_holding_buffers = MAX_TX_HOLDING_BUFFERS;
4230 }
4231 }
4232
4233 mgsl_device_count++;
4234
4235 if ( !mgsl_device_list )
4236 mgsl_device_list = info;
4237 else {
4238 struct mgsl_struct *current_dev = mgsl_device_list;
4239 while( current_dev->next_device )
4240 current_dev = current_dev->next_device;
4241 current_dev->next_device = info;
4242 }
4243
4244 if ( info->max_frame_size < 4096 )
4245 info->max_frame_size = 4096;
4246 else if ( info->max_frame_size > 65535 )
4247 info->max_frame_size = 65535;
4248
4249 if ( info->bus_type == MGSL_BUS_TYPE_PCI ) {
4250 printk( "SyncLink PCI v%d %s: IO=%04X IRQ=%d Mem=%08X,%08X MaxFrameSize=%u\n",
4251 info->hw_version + 1, info->device_name, info->io_base, info->irq_level,
4252 info->phys_memory_base, info->phys_lcr_base,
4253 info->max_frame_size );
4254 } else {
4255 printk( "SyncLink ISA %s: IO=%04X IRQ=%d DMA=%d MaxFrameSize=%u\n",
4256 info->device_name, info->io_base, info->irq_level, info->dma_level,
4257 info->max_frame_size );
4258 }
4259
4260#if SYNCLINK_GENERIC_HDLC
4261 hdlcdev_init(info);
4262#endif
4263
4264} /* end of mgsl_add_device() */
4265
4266static const struct tty_port_operations mgsl_port_ops = {
4267 .carrier_raised = carrier_raised,
4268 .dtr_rts = dtr_rts,
4269};
4270
4271
4272/* mgsl_allocate_device()
4273 *
4274 * Allocate and initialize a device instance structure
4275 *
4276 * Arguments: none
4277 * Return Value: pointer to mgsl_struct if success, otherwise NULL
4278 */
4279static struct mgsl_struct* mgsl_allocate_device(void)
4280{
4281 struct mgsl_struct *info;
4282
4283 info = kzalloc(sizeof(struct mgsl_struct),
4284 GFP_KERNEL);
4285
4286 if (!info) {
4287 printk("Error can't allocate device instance data\n");
4288 } else {
4289 tty_port_init(&info->port);
4290 info->port.ops = &mgsl_port_ops;
4291 info->magic = MGSL_MAGIC;
4292 INIT_WORK(&info->task, mgsl_bh_handler);
4293 info->max_frame_size = 4096;
4294 info->port.close_delay = 5*HZ/10;
4295 info->port.closing_wait = 30*HZ;
4296 init_waitqueue_head(&info->status_event_wait_q);
4297 init_waitqueue_head(&info->event_wait_q);
4298 spin_lock_init(&info->irq_spinlock);
4299 spin_lock_init(&info->netlock);
4300 memcpy(&info->params,&default_params,sizeof(MGSL_PARAMS));
4301 info->idle_mode = HDLC_TXIDLE_FLAGS;
4302 info->num_tx_dma_buffers = 1;
4303 info->num_tx_holding_buffers = 0;
4304 }
4305
4306 return info;
4307
4308} /* end of mgsl_allocate_device()*/
4309
4310static const struct tty_operations mgsl_ops = {
4311 .open = mgsl_open,
4312 .close = mgsl_close,
4313 .write = mgsl_write,
4314 .put_char = mgsl_put_char,
4315 .flush_chars = mgsl_flush_chars,
4316 .write_room = mgsl_write_room,
4317 .chars_in_buffer = mgsl_chars_in_buffer,
4318 .flush_buffer = mgsl_flush_buffer,
4319 .ioctl = mgsl_ioctl,
4320 .throttle = mgsl_throttle,
4321 .unthrottle = mgsl_unthrottle,
4322 .send_xchar = mgsl_send_xchar,
4323 .break_ctl = mgsl_break,
4324 .wait_until_sent = mgsl_wait_until_sent,
4325 .set_termios = mgsl_set_termios,
4326 .stop = mgsl_stop,
4327 .start = mgsl_start,
4328 .hangup = mgsl_hangup,
4329 .tiocmget = tiocmget,
4330 .tiocmset = tiocmset,
4331 .proc_fops = &mgsl_proc_fops,
4332};
4333
4334/*
4335 * perform tty device initialization
4336 */
4337static int mgsl_init_tty(void)
4338{
4339 int rc;
4340
4341 serial_driver = alloc_tty_driver(128);
4342 if (!serial_driver)
4343 return -ENOMEM;
4344
4345 serial_driver->owner = THIS_MODULE;
4346 serial_driver->driver_name = "synclink";
4347 serial_driver->name = "ttySL";
4348 serial_driver->major = ttymajor;
4349 serial_driver->minor_start = 64;
4350 serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
4351 serial_driver->subtype = SERIAL_TYPE_NORMAL;
4352 serial_driver->init_termios = tty_std_termios;
4353 serial_driver->init_termios.c_cflag =
4354 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
4355 serial_driver->init_termios.c_ispeed = 9600;
4356 serial_driver->init_termios.c_ospeed = 9600;
4357 serial_driver->flags = TTY_DRIVER_REAL_RAW;
4358 tty_set_operations(serial_driver, &mgsl_ops);
4359 if ((rc = tty_register_driver(serial_driver)) < 0) {
4360 printk("%s(%d):Couldn't register serial driver\n",
4361 __FILE__,__LINE__);
4362 put_tty_driver(serial_driver);
4363 serial_driver = NULL;
4364 return rc;
4365 }
4366
4367 printk("%s %s, tty major#%d\n",
4368 driver_name, driver_version,
4369 serial_driver->major);
4370 return 0;
4371}
4372
4373/* enumerate user specified ISA adapters
4374 */
4375static void mgsl_enum_isa_devices(void)
4376{
4377 struct mgsl_struct *info;
4378 int i;
4379
4380 /* Check for user specified ISA devices */
4381
4382 for (i=0 ;(i < MAX_ISA_DEVICES) && io[i] && irq[i]; i++){
4383 if ( debug_level >= DEBUG_LEVEL_INFO )
4384 printk("ISA device specified io=%04X,irq=%d,dma=%d\n",
4385 io[i], irq[i], dma[i] );
4386
4387 info = mgsl_allocate_device();
4388 if ( !info ) {
4389 /* error allocating device instance data */
4390 if ( debug_level >= DEBUG_LEVEL_ERROR )
4391 printk( "can't allocate device instance data.\n");
4392 continue;
4393 }
4394
4395 /* Copy user configuration info to device instance data */
4396 info->io_base = (unsigned int)io[i];
4397 info->irq_level = (unsigned int)irq[i];
4398 info->irq_level = irq_canonicalize(info->irq_level);
4399 info->dma_level = (unsigned int)dma[i];
4400 info->bus_type = MGSL_BUS_TYPE_ISA;
4401 info->io_addr_size = 16;
4402 info->irq_flags = 0;
4403
4404 mgsl_add_device( info );
4405 }
4406}
4407
4408static void synclink_cleanup(void)
4409{
4410 int rc;
4411 struct mgsl_struct *info;
4412 struct mgsl_struct *tmp;
4413
4414 printk("Unloading %s: %s\n", driver_name, driver_version);
4415
4416 if (serial_driver) {
4417 if ((rc = tty_unregister_driver(serial_driver)))
4418 printk("%s(%d) failed to unregister tty driver err=%d\n",
4419 __FILE__,__LINE__,rc);
4420 put_tty_driver(serial_driver);
4421 }
4422
4423 info = mgsl_device_list;
4424 while(info) {
4425#if SYNCLINK_GENERIC_HDLC
4426 hdlcdev_exit(info);
4427#endif
4428 mgsl_release_resources(info);
4429 tmp = info;
4430 info = info->next_device;
4431 kfree(tmp);
4432 }
4433
4434 if (pci_registered)
4435 pci_unregister_driver(&synclink_pci_driver);
4436}
4437
4438static int __init synclink_init(void)
4439{
4440 int rc;
4441
4442 if (break_on_load) {
4443 mgsl_get_text_ptr();
4444 BREAKPOINT();
4445 }
4446
4447 printk("%s %s\n", driver_name, driver_version);
4448
4449 mgsl_enum_isa_devices();
4450 if ((rc = pci_register_driver(&synclink_pci_driver)) < 0)
4451 printk("%s:failed to register PCI driver, error=%d\n",__FILE__,rc);
4452 else
4453 pci_registered = true;
4454
4455 if ((rc = mgsl_init_tty()) < 0)
4456 goto error;
4457
4458 return 0;
4459
4460error:
4461 synclink_cleanup();
4462 return rc;
4463}
4464
4465static void __exit synclink_exit(void)
4466{
4467 synclink_cleanup();
4468}
4469
4470module_init(synclink_init);
4471module_exit(synclink_exit);
4472
4473/*
4474 * usc_RTCmd()
4475 *
4476 * Issue a USC Receive/Transmit command to the
4477 * Channel Command/Address Register (CCAR).
4478 *
4479 * Notes:
4480 *
4481 * The command is encoded in the most significant 5 bits <15..11>
4482 * of the CCAR value. Bits <10..7> of the CCAR must be preserved
4483 * and Bits <6..0> must be written as zeros.
4484 *
4485 * Arguments:
4486 *
4487 * info pointer to device information structure
4488 * Cmd command mask (use symbolic macros)
4489 *
4490 * Return Value:
4491 *
4492 * None
4493 */
4494static void usc_RTCmd( struct mgsl_struct *info, u16 Cmd )
4495{
4496 /* output command to CCAR in bits <15..11> */
4497 /* preserve bits <10..7>, bits <6..0> must be zero */
4498
4499 outw( Cmd + info->loopback_bits, info->io_base + CCAR );
4500
4501 /* Read to flush write to CCAR */
4502 if ( info->bus_type == MGSL_BUS_TYPE_PCI )
4503 inw( info->io_base + CCAR );
4504
4505} /* end of usc_RTCmd() */
4506
4507/*
4508 * usc_DmaCmd()
4509 *
4510 * Issue a DMA command to the DMA Command/Address Register (DCAR).
4511 *
4512 * Arguments:
4513 *
4514 * info pointer to device information structure
4515 * Cmd DMA command mask (usc_DmaCmd_XX Macros)
4516 *
4517 * Return Value:
4518 *
4519 * None
4520 */
4521static void usc_DmaCmd( struct mgsl_struct *info, u16 Cmd )
4522{
4523 /* write command mask to DCAR */
4524 outw( Cmd + info->mbre_bit, info->io_base );
4525
4526 /* Read to flush write to DCAR */
4527 if ( info->bus_type == MGSL_BUS_TYPE_PCI )
4528 inw( info->io_base );
4529
4530} /* end of usc_DmaCmd() */
4531
4532/*
4533 * usc_OutDmaReg()
4534 *
4535 * Write a 16-bit value to a USC DMA register
4536 *
4537 * Arguments:
4538 *
4539 * info pointer to device info structure
4540 * RegAddr register address (number) for write
4541 * RegValue 16-bit value to write to register
4542 *
4543 * Return Value:
4544 *
4545 * None
4546 *
4547 */
4548static void usc_OutDmaReg( struct mgsl_struct *info, u16 RegAddr, u16 RegValue )
4549{
4550 /* Note: The DCAR is located at the adapter base address */
4551 /* Note: must preserve state of BIT8 in DCAR */
4552
4553 outw( RegAddr + info->mbre_bit, info->io_base );
4554 outw( RegValue, info->io_base );
4555
4556 /* Read to flush write to DCAR */
4557 if ( info->bus_type == MGSL_BUS_TYPE_PCI )
4558 inw( info->io_base );
4559
4560} /* end of usc_OutDmaReg() */
4561
4562/*
4563 * usc_InDmaReg()
4564 *
4565 * Read a 16-bit value from a DMA register
4566 *
4567 * Arguments:
4568 *
4569 * info pointer to device info structure
4570 * RegAddr register address (number) to read from
4571 *
4572 * Return Value:
4573 *
4574 * The 16-bit value read from register
4575 *
4576 */
4577static u16 usc_InDmaReg( struct mgsl_struct *info, u16 RegAddr )
4578{
4579 /* Note: The DCAR is located at the adapter base address */
4580 /* Note: must preserve state of BIT8 in DCAR */
4581
4582 outw( RegAddr + info->mbre_bit, info->io_base );
4583 return inw( info->io_base );
4584
4585} /* end of usc_InDmaReg() */
4586
4587/*
4588 *
4589 * usc_OutReg()
4590 *
4591 * Write a 16-bit value to a USC serial channel register
4592 *
4593 * Arguments:
4594 *
4595 * info pointer to device info structure
4596 * RegAddr register address (number) to write to
4597 * RegValue 16-bit value to write to register
4598 *
4599 * Return Value:
4600 *
4601 * None
4602 *
4603 */
4604static void usc_OutReg( struct mgsl_struct *info, u16 RegAddr, u16 RegValue )
4605{
4606 outw( RegAddr + info->loopback_bits, info->io_base + CCAR );
4607 outw( RegValue, info->io_base + CCAR );
4608
4609 /* Read to flush write to CCAR */
4610 if ( info->bus_type == MGSL_BUS_TYPE_PCI )
4611 inw( info->io_base + CCAR );
4612
4613} /* end of usc_OutReg() */
4614
4615/*
4616 * usc_InReg()
4617 *
4618 * Reads a 16-bit value from a USC serial channel register
4619 *
4620 * Arguments:
4621 *
4622 * info pointer to device extension
4623 * RegAddr register address (number) to read from
4624 *
4625 * Return Value:
4626 *
4627 * 16-bit value read from register
4628 */
4629static u16 usc_InReg( struct mgsl_struct *info, u16 RegAddr )
4630{
4631 outw( RegAddr + info->loopback_bits, info->io_base + CCAR );
4632 return inw( info->io_base + CCAR );
4633
4634} /* end of usc_InReg() */
4635
4636/* usc_set_sdlc_mode()
4637 *
4638 * Set up the adapter for SDLC DMA communications.
4639 *
4640 * Arguments: info pointer to device instance data
4641 * Return Value: NONE
4642 */
4643static void usc_set_sdlc_mode( struct mgsl_struct *info )
4644{
4645 u16 RegValue;
4646 bool PreSL1660;
4647
4648 /*
4649 * determine if the IUSC on the adapter is pre-SL1660. If
4650 * not, take advantage of the UnderWait feature of more
4651 * modern chips. If an underrun occurs and this bit is set,
4652 * the transmitter will idle the programmed idle pattern
4653 * until the driver has time to service the underrun. Otherwise,
4654 * the dma controller may get the cycles previously requested
4655 * and begin transmitting queued tx data.
4656 */
4657 usc_OutReg(info,TMCR,0x1f);
4658 RegValue=usc_InReg(info,TMDR);
4659 PreSL1660 = (RegValue == IUSC_PRE_SL1660);
4660
4661 if ( info->params.flags & HDLC_FLAG_HDLC_LOOPMODE )
4662 {
4663 /*
4664 ** Channel Mode Register (CMR)
4665 **
4666 ** <15..14> 10 Tx Sub Modes, Send Flag on Underrun
4667 ** <13> 0 0 = Transmit Disabled (initially)
4668 ** <12> 0 1 = Consecutive Idles share common 0
4669 ** <11..8> 1110 Transmitter Mode = HDLC/SDLC Loop
4670 ** <7..4> 0000 Rx Sub Modes, addr/ctrl field handling
4671 ** <3..0> 0110 Receiver Mode = HDLC/SDLC
4672 **
4673 ** 1000 1110 0000 0110 = 0x8e06
4674 */
4675 RegValue = 0x8e06;
4676
4677 /*--------------------------------------------------
4678 * ignore user options for UnderRun Actions and
4679 * preambles
4680 *--------------------------------------------------*/
4681 }
4682 else
4683 {
4684 /* Channel mode Register (CMR)
4685 *
4686 * <15..14> 00 Tx Sub modes, Underrun Action
4687 * <13> 0 1 = Send Preamble before opening flag
4688 * <12> 0 1 = Consecutive Idles share common 0
4689 * <11..8> 0110 Transmitter mode = HDLC/SDLC
4690 * <7..4> 0000 Rx Sub modes, addr/ctrl field handling
4691 * <3..0> 0110 Receiver mode = HDLC/SDLC
4692 *
4693 * 0000 0110 0000 0110 = 0x0606
4694 */
4695 if (info->params.mode == MGSL_MODE_RAW) {
4696 RegValue = 0x0001; /* Set Receive mode = external sync */
4697
4698 usc_OutReg( info, IOCR, /* Set IOCR DCD is RxSync Detect Input */
4699 (unsigned short)((usc_InReg(info, IOCR) & ~(BIT13|BIT12)) | BIT12));
4700
4701 /*
4702 * TxSubMode:
4703 * CMR <15> 0 Don't send CRC on Tx Underrun
4704 * CMR <14> x undefined
4705 * CMR <13> 0 Send preamble before openning sync
4706 * CMR <12> 0 Send 8-bit syncs, 1=send Syncs per TxLength
4707 *
4708 * TxMode:
4709 * CMR <11-8) 0100 MonoSync
4710 *
4711 * 0x00 0100 xxxx xxxx 04xx
4712 */
4713 RegValue |= 0x0400;
4714 }
4715 else {
4716
4717 RegValue = 0x0606;
4718
4719 if ( info->params.flags & HDLC_FLAG_UNDERRUN_ABORT15 )
4720 RegValue |= BIT14;
4721 else if ( info->params.flags & HDLC_FLAG_UNDERRUN_FLAG )
4722 RegValue |= BIT15;
4723 else if ( info->params.flags & HDLC_FLAG_UNDERRUN_CRC )
4724 RegValue |= BIT15 + BIT14;
4725 }
4726
4727 if ( info->params.preamble != HDLC_PREAMBLE_PATTERN_NONE )
4728 RegValue |= BIT13;
4729 }
4730
4731 if ( info->params.mode == MGSL_MODE_HDLC &&
4732 (info->params.flags & HDLC_FLAG_SHARE_ZERO) )
4733 RegValue |= BIT12;
4734
4735 if ( info->params.addr_filter != 0xff )
4736 {
4737 /* set up receive address filtering */
4738 usc_OutReg( info, RSR, info->params.addr_filter );
4739 RegValue |= BIT4;
4740 }
4741
4742 usc_OutReg( info, CMR, RegValue );
4743 info->cmr_value = RegValue;
4744
4745 /* Receiver mode Register (RMR)
4746 *
4747 * <15..13> 000 encoding
4748 * <12..11> 00 FCS = 16bit CRC CCITT (x15 + x12 + x5 + 1)
4749 * <10> 1 1 = Set CRC to all 1s (use for SDLC/HDLC)
4750 * <9> 0 1 = Include Receive chars in CRC
4751 * <8> 1 1 = Use Abort/PE bit as abort indicator
4752 * <7..6> 00 Even parity
4753 * <5> 0 parity disabled
4754 * <4..2> 000 Receive Char Length = 8 bits
4755 * <1..0> 00 Disable Receiver
4756 *
4757 * 0000 0101 0000 0000 = 0x0500
4758 */
4759
4760 RegValue = 0x0500;
4761
4762 switch ( info->params.encoding ) {
4763 case HDLC_ENCODING_NRZB: RegValue |= BIT13; break;
4764 case HDLC_ENCODING_NRZI_MARK: RegValue |= BIT14; break;
4765 case HDLC_ENCODING_NRZI_SPACE: RegValue |= BIT14 + BIT13; break;
4766 case HDLC_ENCODING_BIPHASE_MARK: RegValue |= BIT15; break;
4767 case HDLC_ENCODING_BIPHASE_SPACE: RegValue |= BIT15 + BIT13; break;
4768 case HDLC_ENCODING_BIPHASE_LEVEL: RegValue |= BIT15 + BIT14; break;
4769 case HDLC_ENCODING_DIFF_BIPHASE_LEVEL: RegValue |= BIT15 + BIT14 + BIT13; break;
4770 }
4771
4772 if ( (info->params.crc_type & HDLC_CRC_MASK) == HDLC_CRC_16_CCITT )
4773 RegValue |= BIT9;
4774 else if ( (info->params.crc_type & HDLC_CRC_MASK) == HDLC_CRC_32_CCITT )
4775 RegValue |= ( BIT12 | BIT10 | BIT9 );
4776
4777 usc_OutReg( info, RMR, RegValue );
4778
4779 /* Set the Receive count Limit Register (RCLR) to 0xffff. */
4780 /* When an opening flag of an SDLC frame is recognized the */
4781 /* Receive Character count (RCC) is loaded with the value in */
4782 /* RCLR. The RCC is decremented for each received byte. The */
4783 /* value of RCC is stored after the closing flag of the frame */
4784 /* allowing the frame size to be computed. */
4785
4786 usc_OutReg( info, RCLR, RCLRVALUE );
4787
4788 usc_RCmd( info, RCmd_SelectRicrdma_level );
4789
4790 /* Receive Interrupt Control Register (RICR)
4791 *
4792 * <15..8> ? RxFIFO DMA Request Level
4793 * <7> 0 Exited Hunt IA (Interrupt Arm)
4794 * <6> 0 Idle Received IA
4795 * <5> 0 Break/Abort IA
4796 * <4> 0 Rx Bound IA
4797 * <3> 1 Queued status reflects oldest 2 bytes in FIFO
4798 * <2> 0 Abort/PE IA
4799 * <1> 1 Rx Overrun IA
4800 * <0> 0 Select TC0 value for readback
4801 *
4802 * 0000 0000 0000 1000 = 0x000a
4803 */
4804
4805 /* Carry over the Exit Hunt and Idle Received bits */
4806 /* in case they have been armed by usc_ArmEvents. */
4807
4808 RegValue = usc_InReg( info, RICR ) & 0xc0;
4809
4810 if ( info->bus_type == MGSL_BUS_TYPE_PCI )
4811 usc_OutReg( info, RICR, (u16)(0x030a | RegValue) );
4812 else
4813 usc_OutReg( info, RICR, (u16)(0x140a | RegValue) );
4814
4815 /* Unlatch all Rx status bits and clear Rx status IRQ Pending */
4816
4817 usc_UnlatchRxstatusBits( info, RXSTATUS_ALL );
4818 usc_ClearIrqPendingBits( info, RECEIVE_STATUS );
4819
4820 /* Transmit mode Register (TMR)
4821 *
4822 * <15..13> 000 encoding
4823 * <12..11> 00 FCS = 16bit CRC CCITT (x15 + x12 + x5 + 1)
4824 * <10> 1 1 = Start CRC as all 1s (use for SDLC/HDLC)
4825 * <9> 0 1 = Tx CRC Enabled
4826 * <8> 0 1 = Append CRC to end of transmit frame
4827 * <7..6> 00 Transmit parity Even
4828 * <5> 0 Transmit parity Disabled
4829 * <4..2> 000 Tx Char Length = 8 bits
4830 * <1..0> 00 Disable Transmitter
4831 *
4832 * 0000 0100 0000 0000 = 0x0400
4833 */
4834
4835 RegValue = 0x0400;
4836
4837 switch ( info->params.encoding ) {
4838 case HDLC_ENCODING_NRZB: RegValue |= BIT13; break;
4839 case HDLC_ENCODING_NRZI_MARK: RegValue |= BIT14; break;
4840 case HDLC_ENCODING_NRZI_SPACE: RegValue |= BIT14 + BIT13; break;
4841 case HDLC_ENCODING_BIPHASE_MARK: RegValue |= BIT15; break;
4842 case HDLC_ENCODING_BIPHASE_SPACE: RegValue |= BIT15 + BIT13; break;
4843 case HDLC_ENCODING_BIPHASE_LEVEL: RegValue |= BIT15 + BIT14; break;
4844 case HDLC_ENCODING_DIFF_BIPHASE_LEVEL: RegValue |= BIT15 + BIT14 + BIT13; break;
4845 }
4846
4847 if ( (info->params.crc_type & HDLC_CRC_MASK) == HDLC_CRC_16_CCITT )
4848 RegValue |= BIT9 + BIT8;
4849 else if ( (info->params.crc_type & HDLC_CRC_MASK) == HDLC_CRC_32_CCITT )
4850 RegValue |= ( BIT12 | BIT10 | BIT9 | BIT8);
4851
4852 usc_OutReg( info, TMR, RegValue );
4853
4854 usc_set_txidle( info );
4855
4856
4857 usc_TCmd( info, TCmd_SelectTicrdma_level );
4858
4859 /* Transmit Interrupt Control Register (TICR)
4860 *
4861 * <15..8> ? Transmit FIFO DMA Level
4862 * <7> 0 Present IA (Interrupt Arm)
4863 * <6> 0 Idle Sent IA
4864 * <5> 1 Abort Sent IA
4865 * <4> 1 EOF/EOM Sent IA
4866 * <3> 0 CRC Sent IA
4867 * <2> 1 1 = Wait for SW Trigger to Start Frame
4868 * <1> 1 Tx Underrun IA
4869 * <0> 0 TC0 constant on read back
4870 *
4871 * 0000 0000 0011 0110 = 0x0036
4872 */
4873
4874 if ( info->bus_type == MGSL_BUS_TYPE_PCI )
4875 usc_OutReg( info, TICR, 0x0736 );
4876 else
4877 usc_OutReg( info, TICR, 0x1436 );
4878
4879 usc_UnlatchTxstatusBits( info, TXSTATUS_ALL );
4880 usc_ClearIrqPendingBits( info, TRANSMIT_STATUS );
4881
4882 /*
4883 ** Transmit Command/Status Register (TCSR)
4884 **
4885 ** <15..12> 0000 TCmd
4886 ** <11> 0/1 UnderWait
4887 ** <10..08> 000 TxIdle
4888 ** <7> x PreSent
4889 ** <6> x IdleSent
4890 ** <5> x AbortSent
4891 ** <4> x EOF/EOM Sent
4892 ** <3> x CRC Sent
4893 ** <2> x All Sent
4894 ** <1> x TxUnder
4895 ** <0> x TxEmpty
4896 **
4897 ** 0000 0000 0000 0000 = 0x0000
4898 */
4899 info->tcsr_value = 0;
4900
4901 if ( !PreSL1660 )
4902 info->tcsr_value |= TCSR_UNDERWAIT;
4903
4904 usc_OutReg( info, TCSR, info->tcsr_value );
4905
4906 /* Clock mode Control Register (CMCR)
4907 *
4908 * <15..14> 00 counter 1 Source = Disabled
4909 * <13..12> 00 counter 0 Source = Disabled
4910 * <11..10> 11 BRG1 Input is TxC Pin
4911 * <9..8> 11 BRG0 Input is TxC Pin
4912 * <7..6> 01 DPLL Input is BRG1 Output
4913 * <5..3> XXX TxCLK comes from Port 0
4914 * <2..0> XXX RxCLK comes from Port 1
4915 *
4916 * 0000 1111 0111 0111 = 0x0f77
4917 */
4918
4919 RegValue = 0x0f40;
4920
4921 if ( info->params.flags & HDLC_FLAG_RXC_DPLL )
4922 RegValue |= 0x0003; /* RxCLK from DPLL */
4923 else if ( info->params.flags & HDLC_FLAG_RXC_BRG )
4924 RegValue |= 0x0004; /* RxCLK from BRG0 */
4925 else if ( info->params.flags & HDLC_FLAG_RXC_TXCPIN)
4926 RegValue |= 0x0006; /* RxCLK from TXC Input */
4927 else
4928 RegValue |= 0x0007; /* RxCLK from Port1 */
4929
4930 if ( info->params.flags & HDLC_FLAG_TXC_DPLL )
4931 RegValue |= 0x0018; /* TxCLK from DPLL */
4932 else if ( info->params.flags & HDLC_FLAG_TXC_BRG )
4933 RegValue |= 0x0020; /* TxCLK from BRG0 */
4934 else if ( info->params.flags & HDLC_FLAG_TXC_RXCPIN)
4935 RegValue |= 0x0038; /* RxCLK from TXC Input */
4936 else
4937 RegValue |= 0x0030; /* TxCLK from Port0 */
4938
4939 usc_OutReg( info, CMCR, RegValue );
4940
4941
4942 /* Hardware Configuration Register (HCR)
4943 *
4944 * <15..14> 00 CTR0 Divisor:00=32,01=16,10=8,11=4
4945 * <13> 0 CTR1DSel:0=CTR0Div determines CTR0Div
4946 * <12> 0 CVOK:0=report code violation in biphase
4947 * <11..10> 00 DPLL Divisor:00=32,01=16,10=8,11=4
4948 * <9..8> XX DPLL mode:00=disable,01=NRZ,10=Biphase,11=Biphase Level
4949 * <7..6> 00 reserved
4950 * <5> 0 BRG1 mode:0=continuous,1=single cycle
4951 * <4> X BRG1 Enable
4952 * <3..2> 00 reserved
4953 * <1> 0 BRG0 mode:0=continuous,1=single cycle
4954 * <0> 0 BRG0 Enable
4955 */
4956
4957 RegValue = 0x0000;
4958
4959 if ( info->params.flags & (HDLC_FLAG_RXC_DPLL + HDLC_FLAG_TXC_DPLL) ) {
4960 u32 XtalSpeed;
4961 u32 DpllDivisor;
4962 u16 Tc;
4963
4964 /* DPLL is enabled. Use BRG1 to provide continuous reference clock */
4965 /* for DPLL. DPLL mode in HCR is dependent on the encoding used. */
4966
4967 if ( info->bus_type == MGSL_BUS_TYPE_PCI )
4968 XtalSpeed = 11059200;
4969 else
4970 XtalSpeed = 14745600;
4971
4972 if ( info->params.flags & HDLC_FLAG_DPLL_DIV16 ) {
4973 DpllDivisor = 16;
4974 RegValue |= BIT10;
4975 }
4976 else if ( info->params.flags & HDLC_FLAG_DPLL_DIV8 ) {
4977 DpllDivisor = 8;
4978 RegValue |= BIT11;
4979 }
4980 else
4981 DpllDivisor = 32;
4982
4983 /* Tc = (Xtal/Speed) - 1 */
4984 /* If twice the remainder of (Xtal/Speed) is greater than Speed */
4985 /* then rounding up gives a more precise time constant. Instead */
4986 /* of rounding up and then subtracting 1 we just don't subtract */
4987 /* the one in this case. */
4988
4989 /*--------------------------------------------------
4990 * ejz: for DPLL mode, application should use the
4991 * same clock speed as the partner system, even
4992 * though clocking is derived from the input RxData.
4993 * In case the user uses a 0 for the clock speed,
4994 * default to 0xffffffff and don't try to divide by
4995 * zero
4996 *--------------------------------------------------*/
4997 if ( info->params.clock_speed )
4998 {
4999 Tc = (u16)((XtalSpeed/DpllDivisor)/info->params.clock_speed);
5000 if ( !((((XtalSpeed/DpllDivisor) % info->params.clock_speed) * 2)
5001 / info->params.clock_speed) )
5002 Tc--;
5003 }
5004 else
5005 Tc = -1;
5006
5007
5008 /* Write 16-bit Time Constant for BRG1 */
5009 usc_OutReg( info, TC1R, Tc );
5010
5011 RegValue |= BIT4; /* enable BRG1 */
5012
5013 switch ( info->params.encoding ) {
5014 case HDLC_ENCODING_NRZ:
5015 case HDLC_ENCODING_NRZB:
5016 case HDLC_ENCODING_NRZI_MARK:
5017 case HDLC_ENCODING_NRZI_SPACE: RegValue |= BIT8; break;
5018 case HDLC_ENCODING_BIPHASE_MARK:
5019 case HDLC_ENCODING_BIPHASE_SPACE: RegValue |= BIT9; break;
5020 case HDLC_ENCODING_BIPHASE_LEVEL:
5021 case HDLC_ENCODING_DIFF_BIPHASE_LEVEL: RegValue |= BIT9 + BIT8; break;
5022 }
5023 }
5024
5025 usc_OutReg( info, HCR, RegValue );
5026
5027
5028 /* Channel Control/status Register (CCSR)
5029 *
5030 * <15> X RCC FIFO Overflow status (RO)
5031 * <14> X RCC FIFO Not Empty status (RO)
5032 * <13> 0 1 = Clear RCC FIFO (WO)
5033 * <12> X DPLL Sync (RW)
5034 * <11> X DPLL 2 Missed Clocks status (RO)
5035 * <10> X DPLL 1 Missed Clock status (RO)
5036 * <9..8> 00 DPLL Resync on rising and falling edges (RW)
5037 * <7> X SDLC Loop On status (RO)
5038 * <6> X SDLC Loop Send status (RO)
5039 * <5> 1 Bypass counters for TxClk and RxClk (RW)
5040 * <4..2> 000 Last Char of SDLC frame has 8 bits (RW)
5041 * <1..0> 00 reserved
5042 *
5043 * 0000 0000 0010 0000 = 0x0020
5044 */
5045
5046 usc_OutReg( info, CCSR, 0x1020 );
5047
5048
5049 if ( info->params.flags & HDLC_FLAG_AUTO_CTS ) {
5050 usc_OutReg( info, SICR,
5051 (u16)(usc_InReg(info,SICR) | SICR_CTS_INACTIVE) );
5052 }
5053
5054
5055 /* enable Master Interrupt Enable bit (MIE) */
5056 usc_EnableMasterIrqBit( info );
5057
5058 usc_ClearIrqPendingBits( info, RECEIVE_STATUS + RECEIVE_DATA +
5059 TRANSMIT_STATUS + TRANSMIT_DATA + MISC);
5060
5061 /* arm RCC underflow interrupt */
5062 usc_OutReg(info, SICR, (u16)(usc_InReg(info,SICR) | BIT3));
5063 usc_EnableInterrupts(info, MISC);
5064
5065 info->mbre_bit = 0;
5066 outw( 0, info->io_base ); /* clear Master Bus Enable (DCAR) */
5067 usc_DmaCmd( info, DmaCmd_ResetAllChannels ); /* disable both DMA channels */
5068 info->mbre_bit = BIT8;
5069 outw( BIT8, info->io_base ); /* set Master Bus Enable (DCAR) */
5070
5071 if (info->bus_type == MGSL_BUS_TYPE_ISA) {
5072 /* Enable DMAEN (Port 7, Bit 14) */
5073 /* This connects the DMA request signal to the ISA bus */
5074 usc_OutReg(info, PCR, (u16)((usc_InReg(info, PCR) | BIT15) & ~BIT14));
5075 }
5076
5077 /* DMA Control Register (DCR)
5078 *
5079 * <15..14> 10 Priority mode = Alternating Tx/Rx
5080 * 01 Rx has priority
5081 * 00 Tx has priority
5082 *
5083 * <13> 1 Enable Priority Preempt per DCR<15..14>
5084 * (WARNING DCR<11..10> must be 00 when this is 1)
5085 * 0 Choose activate channel per DCR<11..10>
5086 *
5087 * <12> 0 Little Endian for Array/List
5088 * <11..10> 00 Both Channels can use each bus grant
5089 * <9..6> 0000 reserved
5090 * <5> 0 7 CLK - Minimum Bus Re-request Interval
5091 * <4> 0 1 = drive D/C and S/D pins
5092 * <3> 1 1 = Add one wait state to all DMA cycles.
5093 * <2> 0 1 = Strobe /UAS on every transfer.
5094 * <1..0> 11 Addr incrementing only affects LS24 bits
5095 *
5096 * 0110 0000 0000 1011 = 0x600b
5097 */
5098
5099 if ( info->bus_type == MGSL_BUS_TYPE_PCI ) {
5100 /* PCI adapter does not need DMA wait state */
5101 usc_OutDmaReg( info, DCR, 0xa00b );
5102 }
5103 else
5104 usc_OutDmaReg( info, DCR, 0x800b );
5105
5106
5107 /* Receive DMA mode Register (RDMR)
5108 *
5109 * <15..14> 11 DMA mode = Linked List Buffer mode
5110 * <13> 1 RSBinA/L = store Rx status Block in Arrary/List entry
5111 * <12> 1 Clear count of List Entry after fetching
5112 * <11..10> 00 Address mode = Increment
5113 * <9> 1 Terminate Buffer on RxBound
5114 * <8> 0 Bus Width = 16bits
5115 * <7..0> ? status Bits (write as 0s)
5116 *
5117 * 1111 0010 0000 0000 = 0xf200
5118 */
5119
5120 usc_OutDmaReg( info, RDMR, 0xf200 );
5121
5122
5123 /* Transmit DMA mode Register (TDMR)
5124 *
5125 * <15..14> 11 DMA mode = Linked List Buffer mode
5126 * <13> 1 TCBinA/L = fetch Tx Control Block from List entry
5127 * <12> 1 Clear count of List Entry after fetching
5128 * <11..10> 00 Address mode = Increment
5129 * <9> 1 Terminate Buffer on end of frame
5130 * <8> 0 Bus Width = 16bits
5131 * <7..0> ? status Bits (Read Only so write as 0)
5132 *
5133 * 1111 0010 0000 0000 = 0xf200
5134 */
5135
5136 usc_OutDmaReg( info, TDMR, 0xf200 );
5137
5138
5139 /* DMA Interrupt Control Register (DICR)
5140 *
5141 * <15> 1 DMA Interrupt Enable
5142 * <14> 0 1 = Disable IEO from USC
5143 * <13> 0 1 = Don't provide vector during IntAck
5144 * <12> 1 1 = Include status in Vector
5145 * <10..2> 0 reserved, Must be 0s
5146 * <1> 0 1 = Rx DMA Interrupt Enabled
5147 * <0> 0 1 = Tx DMA Interrupt Enabled
5148 *
5149 * 1001 0000 0000 0000 = 0x9000
5150 */
5151
5152 usc_OutDmaReg( info, DICR, 0x9000 );
5153
5154 usc_InDmaReg( info, RDMR ); /* clear pending receive DMA IRQ bits */
5155 usc_InDmaReg( info, TDMR ); /* clear pending transmit DMA IRQ bits */
5156 usc_OutDmaReg( info, CDIR, 0x0303 ); /* clear IUS and Pending for Tx and Rx */
5157
5158 /* Channel Control Register (CCR)
5159 *
5160 * <15..14> 10 Use 32-bit Tx Control Blocks (TCBs)
5161 * <13> 0 Trigger Tx on SW Command Disabled
5162 * <12> 0 Flag Preamble Disabled
5163 * <11..10> 00 Preamble Length
5164 * <9..8> 00 Preamble Pattern
5165 * <7..6> 10 Use 32-bit Rx status Blocks (RSBs)
5166 * <5> 0 Trigger Rx on SW Command Disabled
5167 * <4..0> 0 reserved
5168 *
5169 * 1000 0000 1000 0000 = 0x8080
5170 */
5171
5172 RegValue = 0x8080;
5173
5174 switch ( info->params.preamble_length ) {
5175 case HDLC_PREAMBLE_LENGTH_16BITS: RegValue |= BIT10; break;
5176 case HDLC_PREAMBLE_LENGTH_32BITS: RegValue |= BIT11; break;
5177 case HDLC_PREAMBLE_LENGTH_64BITS: RegValue |= BIT11 + BIT10; break;
5178 }
5179
5180 switch ( info->params.preamble ) {
5181 case HDLC_PREAMBLE_PATTERN_FLAGS: RegValue |= BIT8 + BIT12; break;
5182 case HDLC_PREAMBLE_PATTERN_ONES: RegValue |= BIT8; break;
5183 case HDLC_PREAMBLE_PATTERN_10: RegValue |= BIT9; break;
5184 case HDLC_PREAMBLE_PATTERN_01: RegValue |= BIT9 + BIT8; break;
5185 }
5186
5187 usc_OutReg( info, CCR, RegValue );
5188
5189
5190 /*
5191 * Burst/Dwell Control Register
5192 *
5193 * <15..8> 0x20 Maximum number of transfers per bus grant
5194 * <7..0> 0x00 Maximum number of clock cycles per bus grant
5195 */
5196
5197 if ( info->bus_type == MGSL_BUS_TYPE_PCI ) {
5198 /* don't limit bus occupancy on PCI adapter */
5199 usc_OutDmaReg( info, BDCR, 0x0000 );
5200 }
5201 else
5202 usc_OutDmaReg( info, BDCR, 0x2000 );
5203
5204 usc_stop_transmitter(info);
5205 usc_stop_receiver(info);
5206
5207} /* end of usc_set_sdlc_mode() */
5208
5209/* usc_enable_loopback()
5210 *
5211 * Set the 16C32 for internal loopback mode.
5212 * The TxCLK and RxCLK signals are generated from the BRG0 and
5213 * the TxD is looped back to the RxD internally.
5214 *
5215 * Arguments: info pointer to device instance data
5216 * enable 1 = enable loopback, 0 = disable
5217 * Return Value: None
5218 */
5219static void usc_enable_loopback(struct mgsl_struct *info, int enable)
5220{
5221 if (enable) {
5222 /* blank external TXD output */
5223 usc_OutReg(info,IOCR,usc_InReg(info,IOCR) | (BIT7+BIT6));
5224
5225 /* Clock mode Control Register (CMCR)
5226 *
5227 * <15..14> 00 counter 1 Disabled
5228 * <13..12> 00 counter 0 Disabled
5229 * <11..10> 11 BRG1 Input is TxC Pin
5230 * <9..8> 11 BRG0 Input is TxC Pin
5231 * <7..6> 01 DPLL Input is BRG1 Output
5232 * <5..3> 100 TxCLK comes from BRG0
5233 * <2..0> 100 RxCLK comes from BRG0
5234 *
5235 * 0000 1111 0110 0100 = 0x0f64
5236 */
5237
5238 usc_OutReg( info, CMCR, 0x0f64 );
5239
5240 /* Write 16-bit Time Constant for BRG0 */
5241 /* use clock speed if available, otherwise use 8 for diagnostics */
5242 if (info->params.clock_speed) {
5243 if (info->bus_type == MGSL_BUS_TYPE_PCI)
5244 usc_OutReg(info, TC0R, (u16)((11059200/info->params.clock_speed)-1));
5245 else
5246 usc_OutReg(info, TC0R, (u16)((14745600/info->params.clock_speed)-1));
5247 } else
5248 usc_OutReg(info, TC0R, (u16)8);
5249
5250 /* Hardware Configuration Register (HCR) Clear Bit 1, BRG0
5251 mode = Continuous Set Bit 0 to enable BRG0. */
5252 usc_OutReg( info, HCR, (u16)((usc_InReg( info, HCR ) & ~BIT1) | BIT0) );
5253
5254 /* Input/Output Control Reg, <2..0> = 100, Drive RxC pin with BRG0 */
5255 usc_OutReg(info, IOCR, (u16)((usc_InReg(info, IOCR) & 0xfff8) | 0x0004));
5256
5257 /* set Internal Data loopback mode */
5258 info->loopback_bits = 0x300;
5259 outw( 0x0300, info->io_base + CCAR );
5260 } else {
5261 /* enable external TXD output */
5262 usc_OutReg(info,IOCR,usc_InReg(info,IOCR) & ~(BIT7+BIT6));
5263
5264 /* clear Internal Data loopback mode */
5265 info->loopback_bits = 0;
5266 outw( 0,info->io_base + CCAR );
5267 }
5268
5269} /* end of usc_enable_loopback() */
5270
5271/* usc_enable_aux_clock()
5272 *
5273 * Enabled the AUX clock output at the specified frequency.
5274 *
5275 * Arguments:
5276 *
5277 * info pointer to device extension
5278 * data_rate data rate of clock in bits per second
5279 * A data rate of 0 disables the AUX clock.
5280 *
5281 * Return Value: None
5282 */
5283static void usc_enable_aux_clock( struct mgsl_struct *info, u32 data_rate )
5284{
5285 u32 XtalSpeed;
5286 u16 Tc;
5287
5288 if ( data_rate ) {
5289 if ( info->bus_type == MGSL_BUS_TYPE_PCI )
5290 XtalSpeed = 11059200;
5291 else
5292 XtalSpeed = 14745600;
5293
5294
5295 /* Tc = (Xtal/Speed) - 1 */
5296 /* If twice the remainder of (Xtal/Speed) is greater than Speed */
5297 /* then rounding up gives a more precise time constant. Instead */
5298 /* of rounding up and then subtracting 1 we just don't subtract */
5299 /* the one in this case. */
5300
5301
5302 Tc = (u16)(XtalSpeed/data_rate);
5303 if ( !(((XtalSpeed % data_rate) * 2) / data_rate) )
5304 Tc--;
5305
5306 /* Write 16-bit Time Constant for BRG0 */
5307 usc_OutReg( info, TC0R, Tc );
5308
5309 /*
5310 * Hardware Configuration Register (HCR)
5311 * Clear Bit 1, BRG0 mode = Continuous
5312 * Set Bit 0 to enable BRG0.
5313 */
5314
5315 usc_OutReg( info, HCR, (u16)((usc_InReg( info, HCR ) & ~BIT1) | BIT0) );
5316
5317 /* Input/Output Control Reg, <2..0> = 100, Drive RxC pin with BRG0 */
5318 usc_OutReg( info, IOCR, (u16)((usc_InReg(info, IOCR) & 0xfff8) | 0x0004) );
5319 } else {
5320 /* data rate == 0 so turn off BRG0 */
5321 usc_OutReg( info, HCR, (u16)(usc_InReg( info, HCR ) & ~BIT0) );
5322 }
5323
5324} /* end of usc_enable_aux_clock() */
5325
5326/*
5327 *
5328 * usc_process_rxoverrun_sync()
5329 *
5330 * This function processes a receive overrun by resetting the
5331 * receive DMA buffers and issuing a Purge Rx FIFO command
5332 * to allow the receiver to continue receiving.
5333 *
5334 * Arguments:
5335 *
5336 * info pointer to device extension
5337 *
5338 * Return Value: None
5339 */
5340static void usc_process_rxoverrun_sync( struct mgsl_struct *info )
5341{
5342 int start_index;
5343 int end_index;
5344 int frame_start_index;
5345 bool start_of_frame_found = false;
5346 bool end_of_frame_found = false;
5347 bool reprogram_dma = false;
5348
5349 DMABUFFERENTRY *buffer_list = info->rx_buffer_list;
5350 u32 phys_addr;
5351
5352 usc_DmaCmd( info, DmaCmd_PauseRxChannel );
5353 usc_RCmd( info, RCmd_EnterHuntmode );
5354 usc_RTCmd( info, RTCmd_PurgeRxFifo );
5355
5356 /* CurrentRxBuffer points to the 1st buffer of the next */
5357 /* possibly available receive frame. */
5358
5359 frame_start_index = start_index = end_index = info->current_rx_buffer;
5360
5361 /* Search for an unfinished string of buffers. This means */
5362 /* that a receive frame started (at least one buffer with */
5363 /* count set to zero) but there is no terminiting buffer */
5364 /* (status set to non-zero). */
5365
5366 while( !buffer_list[end_index].count )
5367 {
5368 /* Count field has been reset to zero by 16C32. */
5369 /* This buffer is currently in use. */
5370
5371 if ( !start_of_frame_found )
5372 {
5373 start_of_frame_found = true;
5374 frame_start_index = end_index;
5375 end_of_frame_found = false;
5376 }
5377
5378 if ( buffer_list[end_index].status )
5379 {
5380 /* Status field has been set by 16C32. */
5381 /* This is the last buffer of a received frame. */
5382
5383 /* We want to leave the buffers for this frame intact. */
5384 /* Move on to next possible frame. */
5385
5386 start_of_frame_found = false;
5387 end_of_frame_found = true;
5388 }
5389
5390 /* advance to next buffer entry in linked list */
5391 end_index++;
5392 if ( end_index == info->rx_buffer_count )
5393 end_index = 0;
5394
5395 if ( start_index == end_index )
5396 {
5397 /* The entire list has been searched with all Counts == 0 and */
5398 /* all Status == 0. The receive buffers are */
5399 /* completely screwed, reset all receive buffers! */
5400 mgsl_reset_rx_dma_buffers( info );
5401 frame_start_index = 0;
5402 start_of_frame_found = false;
5403 reprogram_dma = true;
5404 break;
5405 }
5406 }
5407
5408 if ( start_of_frame_found && !end_of_frame_found )
5409 {
5410 /* There is an unfinished string of receive DMA buffers */
5411 /* as a result of the receiver overrun. */
5412
5413 /* Reset the buffers for the unfinished frame */
5414 /* and reprogram the receive DMA controller to start */
5415 /* at the 1st buffer of unfinished frame. */
5416
5417 start_index = frame_start_index;
5418
5419 do
5420 {
5421 *((unsigned long *)&(info->rx_buffer_list[start_index++].count)) = DMABUFFERSIZE;
5422
5423 /* Adjust index for wrap around. */
5424 if ( start_index == info->rx_buffer_count )
5425 start_index = 0;
5426
5427 } while( start_index != end_index );
5428
5429 reprogram_dma = true;
5430 }
5431
5432 if ( reprogram_dma )
5433 {
5434 usc_UnlatchRxstatusBits(info,RXSTATUS_ALL);
5435 usc_ClearIrqPendingBits(info, RECEIVE_DATA|RECEIVE_STATUS);
5436 usc_UnlatchRxstatusBits(info, RECEIVE_DATA|RECEIVE_STATUS);
5437
5438 usc_EnableReceiver(info,DISABLE_UNCONDITIONAL);
5439
5440 /* This empties the receive FIFO and loads the RCC with RCLR */
5441 usc_OutReg( info, CCSR, (u16)(usc_InReg(info,CCSR) | BIT13) );
5442
5443 /* program 16C32 with physical address of 1st DMA buffer entry */
5444 phys_addr = info->rx_buffer_list[frame_start_index].phys_entry;
5445 usc_OutDmaReg( info, NRARL, (u16)phys_addr );
5446 usc_OutDmaReg( info, NRARU, (u16)(phys_addr >> 16) );
5447
5448 usc_UnlatchRxstatusBits( info, RXSTATUS_ALL );
5449 usc_ClearIrqPendingBits( info, RECEIVE_DATA + RECEIVE_STATUS );
5450 usc_EnableInterrupts( info, RECEIVE_STATUS );
5451
5452 /* 1. Arm End of Buffer (EOB) Receive DMA Interrupt (BIT2 of RDIAR) */
5453 /* 2. Enable Receive DMA Interrupts (BIT1 of DICR) */
5454
5455 usc_OutDmaReg( info, RDIAR, BIT3 + BIT2 );
5456 usc_OutDmaReg( info, DICR, (u16)(usc_InDmaReg(info,DICR) | BIT1) );
5457 usc_DmaCmd( info, DmaCmd_InitRxChannel );
5458 if ( info->params.flags & HDLC_FLAG_AUTO_DCD )
5459 usc_EnableReceiver(info,ENABLE_AUTO_DCD);
5460 else
5461 usc_EnableReceiver(info,ENABLE_UNCONDITIONAL);
5462 }
5463 else
5464 {
5465 /* This empties the receive FIFO and loads the RCC with RCLR */
5466 usc_OutReg( info, CCSR, (u16)(usc_InReg(info,CCSR) | BIT13) );
5467 usc_RTCmd( info, RTCmd_PurgeRxFifo );
5468 }
5469
5470} /* end of usc_process_rxoverrun_sync() */
5471
5472/* usc_stop_receiver()
5473 *
5474 * Disable USC receiver
5475 *
5476 * Arguments: info pointer to device instance data
5477 * Return Value: None
5478 */
5479static void usc_stop_receiver( struct mgsl_struct *info )
5480{
5481 if (debug_level >= DEBUG_LEVEL_ISR)
5482 printk("%s(%d):usc_stop_receiver(%s)\n",
5483 __FILE__,__LINE__, info->device_name );
5484
5485 /* Disable receive DMA channel. */
5486 /* This also disables receive DMA channel interrupts */
5487 usc_DmaCmd( info, DmaCmd_ResetRxChannel );
5488
5489 usc_UnlatchRxstatusBits( info, RXSTATUS_ALL );
5490 usc_ClearIrqPendingBits( info, RECEIVE_DATA + RECEIVE_STATUS );
5491 usc_DisableInterrupts( info, RECEIVE_DATA + RECEIVE_STATUS );
5492
5493 usc_EnableReceiver(info,DISABLE_UNCONDITIONAL);
5494
5495 /* This empties the receive FIFO and loads the RCC with RCLR */
5496 usc_OutReg( info, CCSR, (u16)(usc_InReg(info,CCSR) | BIT13) );
5497 usc_RTCmd( info, RTCmd_PurgeRxFifo );
5498
5499 info->rx_enabled = false;
5500 info->rx_overflow = false;
5501 info->rx_rcc_underrun = false;
5502
5503} /* end of stop_receiver() */
5504
5505/* usc_start_receiver()
5506 *
5507 * Enable the USC receiver
5508 *
5509 * Arguments: info pointer to device instance data
5510 * Return Value: None
5511 */
5512static void usc_start_receiver( struct mgsl_struct *info )
5513{
5514 u32 phys_addr;
5515
5516 if (debug_level >= DEBUG_LEVEL_ISR)
5517 printk("%s(%d):usc_start_receiver(%s)\n",
5518 __FILE__,__LINE__, info->device_name );
5519
5520 mgsl_reset_rx_dma_buffers( info );
5521 usc_stop_receiver( info );
5522
5523 usc_OutReg( info, CCSR, (u16)(usc_InReg(info,CCSR) | BIT13) );
5524 usc_RTCmd( info, RTCmd_PurgeRxFifo );
5525
5526 if ( info->params.mode == MGSL_MODE_HDLC ||
5527 info->params.mode == MGSL_MODE_RAW ) {
5528 /* DMA mode Transfers */
5529 /* Program the DMA controller. */
5530 /* Enable the DMA controller end of buffer interrupt. */
5531
5532 /* program 16C32 with physical address of 1st DMA buffer entry */
5533 phys_addr = info->rx_buffer_list[0].phys_entry;
5534 usc_OutDmaReg( info, NRARL, (u16)phys_addr );
5535 usc_OutDmaReg( info, NRARU, (u16)(phys_addr >> 16) );
5536
5537 usc_UnlatchRxstatusBits( info, RXSTATUS_ALL );
5538 usc_ClearIrqPendingBits( info, RECEIVE_DATA + RECEIVE_STATUS );
5539 usc_EnableInterrupts( info, RECEIVE_STATUS );
5540
5541 /* 1. Arm End of Buffer (EOB) Receive DMA Interrupt (BIT2 of RDIAR) */
5542 /* 2. Enable Receive DMA Interrupts (BIT1 of DICR) */
5543
5544 usc_OutDmaReg( info, RDIAR, BIT3 + BIT2 );
5545 usc_OutDmaReg( info, DICR, (u16)(usc_InDmaReg(info,DICR) | BIT1) );
5546 usc_DmaCmd( info, DmaCmd_InitRxChannel );
5547 if ( info->params.flags & HDLC_FLAG_AUTO_DCD )
5548 usc_EnableReceiver(info,ENABLE_AUTO_DCD);
5549 else
5550 usc_EnableReceiver(info,ENABLE_UNCONDITIONAL);
5551 } else {
5552 usc_UnlatchRxstatusBits(info, RXSTATUS_ALL);
5553 usc_ClearIrqPendingBits(info, RECEIVE_DATA + RECEIVE_STATUS);
5554 usc_EnableInterrupts(info, RECEIVE_DATA);
5555
5556 usc_RTCmd( info, RTCmd_PurgeRxFifo );
5557 usc_RCmd( info, RCmd_EnterHuntmode );
5558
5559 usc_EnableReceiver(info,ENABLE_UNCONDITIONAL);
5560 }
5561
5562 usc_OutReg( info, CCSR, 0x1020 );
5563
5564 info->rx_enabled = true;
5565
5566} /* end of usc_start_receiver() */
5567
5568/* usc_start_transmitter()
5569 *
5570 * Enable the USC transmitter and send a transmit frame if
5571 * one is loaded in the DMA buffers.
5572 *
5573 * Arguments: info pointer to device instance data
5574 * Return Value: None
5575 */
5576static void usc_start_transmitter( struct mgsl_struct *info )
5577{
5578 u32 phys_addr;
5579 unsigned int FrameSize;
5580
5581 if (debug_level >= DEBUG_LEVEL_ISR)
5582 printk("%s(%d):usc_start_transmitter(%s)\n",
5583 __FILE__,__LINE__, info->device_name );
5584
5585 if ( info->xmit_cnt ) {
5586
5587 /* If auto RTS enabled and RTS is inactive, then assert */
5588 /* RTS and set a flag indicating that the driver should */
5589 /* negate RTS when the transmission completes. */
5590
5591 info->drop_rts_on_tx_done = false;
5592
5593 if ( info->params.flags & HDLC_FLAG_AUTO_RTS ) {
5594 usc_get_serial_signals( info );
5595 if ( !(info->serial_signals & SerialSignal_RTS) ) {
5596 info->serial_signals |= SerialSignal_RTS;
5597 usc_set_serial_signals( info );
5598 info->drop_rts_on_tx_done = true;
5599 }
5600 }
5601
5602
5603 if ( info->params.mode == MGSL_MODE_ASYNC ) {
5604 if ( !info->tx_active ) {
5605 usc_UnlatchTxstatusBits(info, TXSTATUS_ALL);
5606 usc_ClearIrqPendingBits(info, TRANSMIT_STATUS + TRANSMIT_DATA);
5607 usc_EnableInterrupts(info, TRANSMIT_DATA);
5608 usc_load_txfifo(info);
5609 }
5610 } else {
5611 /* Disable transmit DMA controller while programming. */
5612 usc_DmaCmd( info, DmaCmd_ResetTxChannel );
5613
5614 /* Transmit DMA buffer is loaded, so program USC */
5615 /* to send the frame contained in the buffers. */
5616
5617 FrameSize = info->tx_buffer_list[info->start_tx_dma_buffer].rcc;
5618
5619 /* if operating in Raw sync mode, reset the rcc component
5620 * of the tx dma buffer entry, otherwise, the serial controller
5621 * will send a closing sync char after this count.
5622 */
5623 if ( info->params.mode == MGSL_MODE_RAW )
5624 info->tx_buffer_list[info->start_tx_dma_buffer].rcc = 0;
5625
5626 /* Program the Transmit Character Length Register (TCLR) */
5627 /* and clear FIFO (TCC is loaded with TCLR on FIFO clear) */
5628 usc_OutReg( info, TCLR, (u16)FrameSize );
5629
5630 usc_RTCmd( info, RTCmd_PurgeTxFifo );
5631
5632 /* Program the address of the 1st DMA Buffer Entry in linked list */
5633 phys_addr = info->tx_buffer_list[info->start_tx_dma_buffer].phys_entry;
5634 usc_OutDmaReg( info, NTARL, (u16)phys_addr );
5635 usc_OutDmaReg( info, NTARU, (u16)(phys_addr >> 16) );
5636
5637 usc_UnlatchTxstatusBits( info, TXSTATUS_ALL );
5638 usc_ClearIrqPendingBits( info, TRANSMIT_STATUS );
5639 usc_EnableInterrupts( info, TRANSMIT_STATUS );
5640
5641 if ( info->params.mode == MGSL_MODE_RAW &&
5642 info->num_tx_dma_buffers > 1 ) {
5643 /* When running external sync mode, attempt to 'stream' transmit */
5644 /* by filling tx dma buffers as they become available. To do this */
5645 /* we need to enable Tx DMA EOB Status interrupts : */
5646 /* */
5647 /* 1. Arm End of Buffer (EOB) Transmit DMA Interrupt (BIT2 of TDIAR) */
5648 /* 2. Enable Transmit DMA Interrupts (BIT0 of DICR) */
5649
5650 usc_OutDmaReg( info, TDIAR, BIT2|BIT3 );
5651 usc_OutDmaReg( info, DICR, (u16)(usc_InDmaReg(info,DICR) | BIT0) );
5652 }
5653
5654 /* Initialize Transmit DMA Channel */
5655 usc_DmaCmd( info, DmaCmd_InitTxChannel );
5656
5657 usc_TCmd( info, TCmd_SendFrame );
5658
5659 mod_timer(&info->tx_timer, jiffies +
5660 msecs_to_jiffies(5000));
5661 }
5662 info->tx_active = true;
5663 }
5664
5665 if ( !info->tx_enabled ) {
5666 info->tx_enabled = true;
5667 if ( info->params.flags & HDLC_FLAG_AUTO_CTS )
5668 usc_EnableTransmitter(info,ENABLE_AUTO_CTS);
5669 else
5670 usc_EnableTransmitter(info,ENABLE_UNCONDITIONAL);
5671 }
5672
5673} /* end of usc_start_transmitter() */
5674
5675/* usc_stop_transmitter()
5676 *
5677 * Stops the transmitter and DMA
5678 *
5679 * Arguments: info pointer to device isntance data
5680 * Return Value: None
5681 */
5682static void usc_stop_transmitter( struct mgsl_struct *info )
5683{
5684 if (debug_level >= DEBUG_LEVEL_ISR)
5685 printk("%s(%d):usc_stop_transmitter(%s)\n",
5686 __FILE__,__LINE__, info->device_name );
5687
5688 del_timer(&info->tx_timer);
5689
5690 usc_UnlatchTxstatusBits( info, TXSTATUS_ALL );
5691 usc_ClearIrqPendingBits( info, TRANSMIT_STATUS + TRANSMIT_DATA );
5692 usc_DisableInterrupts( info, TRANSMIT_STATUS + TRANSMIT_DATA );
5693
5694 usc_EnableTransmitter(info,DISABLE_UNCONDITIONAL);
5695 usc_DmaCmd( info, DmaCmd_ResetTxChannel );
5696 usc_RTCmd( info, RTCmd_PurgeTxFifo );
5697
5698 info->tx_enabled = false;
5699 info->tx_active = false;
5700
5701} /* end of usc_stop_transmitter() */
5702
5703/* usc_load_txfifo()
5704 *
5705 * Fill the transmit FIFO until the FIFO is full or
5706 * there is no more data to load.
5707 *
5708 * Arguments: info pointer to device extension (instance data)
5709 * Return Value: None
5710 */
5711static void usc_load_txfifo( struct mgsl_struct *info )
5712{
5713 int Fifocount;
5714 u8 TwoBytes[2];
5715
5716 if ( !info->xmit_cnt && !info->x_char )
5717 return;
5718
5719 /* Select transmit FIFO status readback in TICR */
5720 usc_TCmd( info, TCmd_SelectTicrTxFifostatus );
5721
5722 /* load the Transmit FIFO until FIFOs full or all data sent */
5723
5724 while( (Fifocount = usc_InReg(info, TICR) >> 8) && info->xmit_cnt ) {
5725 /* there is more space in the transmit FIFO and */
5726 /* there is more data in transmit buffer */
5727
5728 if ( (info->xmit_cnt > 1) && (Fifocount > 1) && !info->x_char ) {
5729 /* write a 16-bit word from transmit buffer to 16C32 */
5730
5731 TwoBytes[0] = info->xmit_buf[info->xmit_tail++];
5732 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
5733 TwoBytes[1] = info->xmit_buf[info->xmit_tail++];
5734 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
5735
5736 outw( *((u16 *)TwoBytes), info->io_base + DATAREG);
5737
5738 info->xmit_cnt -= 2;
5739 info->icount.tx += 2;
5740 } else {
5741 /* only 1 byte left to transmit or 1 FIFO slot left */
5742
5743 outw( (inw( info->io_base + CCAR) & 0x0780) | (TDR+LSBONLY),
5744 info->io_base + CCAR );
5745
5746 if (info->x_char) {
5747 /* transmit pending high priority char */
5748 outw( info->x_char,info->io_base + CCAR );
5749 info->x_char = 0;
5750 } else {
5751 outw( info->xmit_buf[info->xmit_tail++],info->io_base + CCAR );
5752 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
5753 info->xmit_cnt--;
5754 }
5755 info->icount.tx++;
5756 }
5757 }
5758
5759} /* end of usc_load_txfifo() */
5760
5761/* usc_reset()
5762 *
5763 * Reset the adapter to a known state and prepare it for further use.
5764 *
5765 * Arguments: info pointer to device instance data
5766 * Return Value: None
5767 */
5768static void usc_reset( struct mgsl_struct *info )
5769{
5770 if ( info->bus_type == MGSL_BUS_TYPE_PCI ) {
5771 int i;
5772 u32 readval;
5773
5774 /* Set BIT30 of Misc Control Register */
5775 /* (Local Control Register 0x50) to force reset of USC. */
5776
5777 volatile u32 *MiscCtrl = (u32 *)(info->lcr_base + 0x50);
5778 u32 *LCR0BRDR = (u32 *)(info->lcr_base + 0x28);
5779
5780 info->misc_ctrl_value |= BIT30;
5781 *MiscCtrl = info->misc_ctrl_value;
5782
5783 /*
5784 * Force at least 170ns delay before clearing
5785 * reset bit. Each read from LCR takes at least
5786 * 30ns so 10 times for 300ns to be safe.
5787 */
5788 for(i=0;i<10;i++)
5789 readval = *MiscCtrl;
5790
5791 info->misc_ctrl_value &= ~BIT30;
5792 *MiscCtrl = info->misc_ctrl_value;
5793
5794 *LCR0BRDR = BUS_DESCRIPTOR(
5795 1, // Write Strobe Hold (0-3)
5796 2, // Write Strobe Delay (0-3)
5797 2, // Read Strobe Delay (0-3)
5798 0, // NWDD (Write data-data) (0-3)
5799 4, // NWAD (Write Addr-data) (0-31)
5800 0, // NXDA (Read/Write Data-Addr) (0-3)
5801 0, // NRDD (Read Data-Data) (0-3)
5802 5 // NRAD (Read Addr-Data) (0-31)
5803 );
5804 } else {
5805 /* do HW reset */
5806 outb( 0,info->io_base + 8 );
5807 }
5808
5809 info->mbre_bit = 0;
5810 info->loopback_bits = 0;
5811 info->usc_idle_mode = 0;
5812
5813 /*
5814 * Program the Bus Configuration Register (BCR)
5815 *
5816 * <15> 0 Don't use separate address
5817 * <14..6> 0 reserved
5818 * <5..4> 00 IAckmode = Default, don't care
5819 * <3> 1 Bus Request Totem Pole output
5820 * <2> 1 Use 16 Bit data bus
5821 * <1> 0 IRQ Totem Pole output
5822 * <0> 0 Don't Shift Right Addr
5823 *
5824 * 0000 0000 0000 1100 = 0x000c
5825 *
5826 * By writing to io_base + SDPIN the Wait/Ack pin is
5827 * programmed to work as a Wait pin.
5828 */
5829
5830 outw( 0x000c,info->io_base + SDPIN );
5831
5832
5833 outw( 0,info->io_base );
5834 outw( 0,info->io_base + CCAR );
5835
5836 /* select little endian byte ordering */
5837 usc_RTCmd( info, RTCmd_SelectLittleEndian );
5838
5839
5840 /* Port Control Register (PCR)
5841 *
5842 * <15..14> 11 Port 7 is Output (~DMAEN, Bit 14 : 0 = Enabled)
5843 * <13..12> 11 Port 6 is Output (~INTEN, Bit 12 : 0 = Enabled)
5844 * <11..10> 00 Port 5 is Input (No Connect, Don't Care)
5845 * <9..8> 00 Port 4 is Input (No Connect, Don't Care)
5846 * <7..6> 11 Port 3 is Output (~RTS, Bit 6 : 0 = Enabled )
5847 * <5..4> 11 Port 2 is Output (~DTR, Bit 4 : 0 = Enabled )
5848 * <3..2> 01 Port 1 is Input (Dedicated RxC)
5849 * <1..0> 01 Port 0 is Input (Dedicated TxC)
5850 *
5851 * 1111 0000 1111 0101 = 0xf0f5
5852 */
5853
5854 usc_OutReg( info, PCR, 0xf0f5 );
5855
5856
5857 /*
5858 * Input/Output Control Register
5859 *
5860 * <15..14> 00 CTS is active low input
5861 * <13..12> 00 DCD is active low input
5862 * <11..10> 00 TxREQ pin is input (DSR)
5863 * <9..8> 00 RxREQ pin is input (RI)
5864 * <7..6> 00 TxD is output (Transmit Data)
5865 * <5..3> 000 TxC Pin in Input (14.7456MHz Clock)
5866 * <2..0> 100 RxC is Output (drive with BRG0)
5867 *
5868 * 0000 0000 0000 0100 = 0x0004
5869 */
5870
5871 usc_OutReg( info, IOCR, 0x0004 );
5872
5873} /* end of usc_reset() */
5874
5875/* usc_set_async_mode()
5876 *
5877 * Program adapter for asynchronous communications.
5878 *
5879 * Arguments: info pointer to device instance data
5880 * Return Value: None
5881 */
5882static void usc_set_async_mode( struct mgsl_struct *info )
5883{
5884 u16 RegValue;
5885
5886 /* disable interrupts while programming USC */
5887 usc_DisableMasterIrqBit( info );
5888
5889 outw( 0, info->io_base ); /* clear Master Bus Enable (DCAR) */
5890 usc_DmaCmd( info, DmaCmd_ResetAllChannels ); /* disable both DMA channels */
5891
5892 usc_loopback_frame( info );
5893
5894 /* Channel mode Register (CMR)
5895 *
5896 * <15..14> 00 Tx Sub modes, 00 = 1 Stop Bit
5897 * <13..12> 00 00 = 16X Clock
5898 * <11..8> 0000 Transmitter mode = Asynchronous
5899 * <7..6> 00 reserved?
5900 * <5..4> 00 Rx Sub modes, 00 = 16X Clock
5901 * <3..0> 0000 Receiver mode = Asynchronous
5902 *
5903 * 0000 0000 0000 0000 = 0x0
5904 */
5905
5906 RegValue = 0;
5907 if ( info->params.stop_bits != 1 )
5908 RegValue |= BIT14;
5909 usc_OutReg( info, CMR, RegValue );
5910
5911
5912 /* Receiver mode Register (RMR)
5913 *
5914 * <15..13> 000 encoding = None
5915 * <12..08> 00000 reserved (Sync Only)
5916 * <7..6> 00 Even parity
5917 * <5> 0 parity disabled
5918 * <4..2> 000 Receive Char Length = 8 bits
5919 * <1..0> 00 Disable Receiver
5920 *
5921 * 0000 0000 0000 0000 = 0x0
5922 */
5923
5924 RegValue = 0;
5925
5926 if ( info->params.data_bits != 8 )
5927 RegValue |= BIT4+BIT3+BIT2;
5928
5929 if ( info->params.parity != ASYNC_PARITY_NONE ) {
5930 RegValue |= BIT5;
5931 if ( info->params.parity != ASYNC_PARITY_ODD )
5932 RegValue |= BIT6;
5933 }
5934
5935 usc_OutReg( info, RMR, RegValue );
5936
5937
5938 /* Set IRQ trigger level */
5939
5940 usc_RCmd( info, RCmd_SelectRicrIntLevel );
5941
5942
5943 /* Receive Interrupt Control Register (RICR)
5944 *
5945 * <15..8> ? RxFIFO IRQ Request Level
5946 *
5947 * Note: For async mode the receive FIFO level must be set
5948 * to 0 to avoid the situation where the FIFO contains fewer bytes
5949 * than the trigger level and no more data is expected.
5950 *
5951 * <7> 0 Exited Hunt IA (Interrupt Arm)
5952 * <6> 0 Idle Received IA
5953 * <5> 0 Break/Abort IA
5954 * <4> 0 Rx Bound IA
5955 * <3> 0 Queued status reflects oldest byte in FIFO
5956 * <2> 0 Abort/PE IA
5957 * <1> 0 Rx Overrun IA
5958 * <0> 0 Select TC0 value for readback
5959 *
5960 * 0000 0000 0100 0000 = 0x0000 + (FIFOLEVEL in MSB)
5961 */
5962
5963 usc_OutReg( info, RICR, 0x0000 );
5964
5965 usc_UnlatchRxstatusBits( info, RXSTATUS_ALL );
5966 usc_ClearIrqPendingBits( info, RECEIVE_STATUS );
5967
5968
5969 /* Transmit mode Register (TMR)
5970 *
5971 * <15..13> 000 encoding = None
5972 * <12..08> 00000 reserved (Sync Only)
5973 * <7..6> 00 Transmit parity Even
5974 * <5> 0 Transmit parity Disabled
5975 * <4..2> 000 Tx Char Length = 8 bits
5976 * <1..0> 00 Disable Transmitter
5977 *
5978 * 0000 0000 0000 0000 = 0x0
5979 */
5980
5981 RegValue = 0;
5982
5983 if ( info->params.data_bits != 8 )
5984 RegValue |= BIT4+BIT3+BIT2;
5985
5986 if ( info->params.parity != ASYNC_PARITY_NONE ) {
5987 RegValue |= BIT5;
5988 if ( info->params.parity != ASYNC_PARITY_ODD )
5989 RegValue |= BIT6;
5990 }
5991
5992 usc_OutReg( info, TMR, RegValue );
5993
5994 usc_set_txidle( info );
5995
5996
5997 /* Set IRQ trigger level */
5998
5999 usc_TCmd( info, TCmd_SelectTicrIntLevel );
6000
6001
6002 /* Transmit Interrupt Control Register (TICR)
6003 *
6004 * <15..8> ? Transmit FIFO IRQ Level
6005 * <7> 0 Present IA (Interrupt Arm)
6006 * <6> 1 Idle Sent IA
6007 * <5> 0 Abort Sent IA
6008 * <4> 0 EOF/EOM Sent IA
6009 * <3> 0 CRC Sent IA
6010 * <2> 0 1 = Wait for SW Trigger to Start Frame
6011 * <1> 0 Tx Underrun IA
6012 * <0> 0 TC0 constant on read back
6013 *
6014 * 0000 0000 0100 0000 = 0x0040
6015 */
6016
6017 usc_OutReg( info, TICR, 0x1f40 );
6018
6019 usc_UnlatchTxstatusBits( info, TXSTATUS_ALL );
6020 usc_ClearIrqPendingBits( info, TRANSMIT_STATUS );
6021
6022 usc_enable_async_clock( info, info->params.data_rate );
6023
6024
6025 /* Channel Control/status Register (CCSR)
6026 *
6027 * <15> X RCC FIFO Overflow status (RO)
6028 * <14> X RCC FIFO Not Empty status (RO)
6029 * <13> 0 1 = Clear RCC FIFO (WO)
6030 * <12> X DPLL in Sync status (RO)
6031 * <11> X DPLL 2 Missed Clocks status (RO)
6032 * <10> X DPLL 1 Missed Clock status (RO)
6033 * <9..8> 00 DPLL Resync on rising and falling edges (RW)
6034 * <7> X SDLC Loop On status (RO)
6035 * <6> X SDLC Loop Send status (RO)
6036 * <5> 1 Bypass counters for TxClk and RxClk (RW)
6037 * <4..2> 000 Last Char of SDLC frame has 8 bits (RW)
6038 * <1..0> 00 reserved
6039 *
6040 * 0000 0000 0010 0000 = 0x0020
6041 */
6042
6043 usc_OutReg( info, CCSR, 0x0020 );
6044
6045 usc_DisableInterrupts( info, TRANSMIT_STATUS + TRANSMIT_DATA +
6046 RECEIVE_DATA + RECEIVE_STATUS );
6047
6048 usc_ClearIrqPendingBits( info, TRANSMIT_STATUS + TRANSMIT_DATA +
6049 RECEIVE_DATA + RECEIVE_STATUS );
6050
6051 usc_EnableMasterIrqBit( info );
6052
6053 if (info->bus_type == MGSL_BUS_TYPE_ISA) {
6054 /* Enable INTEN (Port 6, Bit12) */
6055 /* This connects the IRQ request signal to the ISA bus */
6056 usc_OutReg(info, PCR, (u16)((usc_InReg(info, PCR) | BIT13) & ~BIT12));
6057 }
6058
6059 if (info->params.loopback) {
6060 info->loopback_bits = 0x300;
6061 outw(0x0300, info->io_base + CCAR);
6062 }
6063
6064} /* end of usc_set_async_mode() */
6065
6066/* usc_loopback_frame()
6067 *
6068 * Loop back a small (2 byte) dummy SDLC frame.
6069 * Interrupts and DMA are NOT used. The purpose of this is to
6070 * clear any 'stale' status info left over from running in async mode.
6071 *
6072 * The 16C32 shows the strange behaviour of marking the 1st
6073 * received SDLC frame with a CRC error even when there is no
6074 * CRC error. To get around this a small dummy from of 2 bytes
6075 * is looped back when switching from async to sync mode.
6076 *
6077 * Arguments: info pointer to device instance data
6078 * Return Value: None
6079 */
6080static void usc_loopback_frame( struct mgsl_struct *info )
6081{
6082 int i;
6083 unsigned long oldmode = info->params.mode;
6084
6085 info->params.mode = MGSL_MODE_HDLC;
6086
6087 usc_DisableMasterIrqBit( info );
6088
6089 usc_set_sdlc_mode( info );
6090 usc_enable_loopback( info, 1 );
6091
6092 /* Write 16-bit Time Constant for BRG0 */
6093 usc_OutReg( info, TC0R, 0 );
6094
6095 /* Channel Control Register (CCR)
6096 *
6097 * <15..14> 00 Don't use 32-bit Tx Control Blocks (TCBs)
6098 * <13> 0 Trigger Tx on SW Command Disabled
6099 * <12> 0 Flag Preamble Disabled
6100 * <11..10> 00 Preamble Length = 8-Bits
6101 * <9..8> 01 Preamble Pattern = flags
6102 * <7..6> 10 Don't use 32-bit Rx status Blocks (RSBs)
6103 * <5> 0 Trigger Rx on SW Command Disabled
6104 * <4..0> 0 reserved
6105 *
6106 * 0000 0001 0000 0000 = 0x0100
6107 */
6108
6109 usc_OutReg( info, CCR, 0x0100 );
6110
6111 /* SETUP RECEIVER */
6112 usc_RTCmd( info, RTCmd_PurgeRxFifo );
6113 usc_EnableReceiver(info,ENABLE_UNCONDITIONAL);
6114
6115 /* SETUP TRANSMITTER */
6116 /* Program the Transmit Character Length Register (TCLR) */
6117 /* and clear FIFO (TCC is loaded with TCLR on FIFO clear) */
6118 usc_OutReg( info, TCLR, 2 );
6119 usc_RTCmd( info, RTCmd_PurgeTxFifo );
6120
6121 /* unlatch Tx status bits, and start transmit channel. */
6122 usc_UnlatchTxstatusBits(info,TXSTATUS_ALL);
6123 outw(0,info->io_base + DATAREG);
6124
6125 /* ENABLE TRANSMITTER */
6126 usc_TCmd( info, TCmd_SendFrame );
6127 usc_EnableTransmitter(info,ENABLE_UNCONDITIONAL);
6128
6129 /* WAIT FOR RECEIVE COMPLETE */
6130 for (i=0 ; i<1000 ; i++)
6131 if (usc_InReg( info, RCSR ) & (BIT8 + BIT4 + BIT3 + BIT1))
6132 break;
6133
6134 /* clear Internal Data loopback mode */
6135 usc_enable_loopback(info, 0);
6136
6137 usc_EnableMasterIrqBit(info);
6138
6139 info->params.mode = oldmode;
6140
6141} /* end of usc_loopback_frame() */
6142
6143/* usc_set_sync_mode() Programs the USC for SDLC communications.
6144 *
6145 * Arguments: info pointer to adapter info structure
6146 * Return Value: None
6147 */
6148static void usc_set_sync_mode( struct mgsl_struct *info )
6149{
6150 usc_loopback_frame( info );
6151 usc_set_sdlc_mode( info );
6152
6153 if (info->bus_type == MGSL_BUS_TYPE_ISA) {
6154 /* Enable INTEN (Port 6, Bit12) */
6155 /* This connects the IRQ request signal to the ISA bus */
6156 usc_OutReg(info, PCR, (u16)((usc_InReg(info, PCR) | BIT13) & ~BIT12));
6157 }
6158
6159 usc_enable_aux_clock(info, info->params.clock_speed);
6160
6161 if (info->params.loopback)
6162 usc_enable_loopback(info,1);
6163
6164} /* end of mgsl_set_sync_mode() */
6165
6166/* usc_set_txidle() Set the HDLC idle mode for the transmitter.
6167 *
6168 * Arguments: info pointer to device instance data
6169 * Return Value: None
6170 */
6171static void usc_set_txidle( struct mgsl_struct *info )
6172{
6173 u16 usc_idle_mode = IDLEMODE_FLAGS;
6174
6175 /* Map API idle mode to USC register bits */
6176
6177 switch( info->idle_mode ){
6178 case HDLC_TXIDLE_FLAGS: usc_idle_mode = IDLEMODE_FLAGS; break;
6179 case HDLC_TXIDLE_ALT_ZEROS_ONES: usc_idle_mode = IDLEMODE_ALT_ONE_ZERO; break;
6180 case HDLC_TXIDLE_ZEROS: usc_idle_mode = IDLEMODE_ZERO; break;
6181 case HDLC_TXIDLE_ONES: usc_idle_mode = IDLEMODE_ONE; break;
6182 case HDLC_TXIDLE_ALT_MARK_SPACE: usc_idle_mode = IDLEMODE_ALT_MARK_SPACE; break;
6183 case HDLC_TXIDLE_SPACE: usc_idle_mode = IDLEMODE_SPACE; break;
6184 case HDLC_TXIDLE_MARK: usc_idle_mode = IDLEMODE_MARK; break;
6185 }
6186
6187 info->usc_idle_mode = usc_idle_mode;
6188 //usc_OutReg(info, TCSR, usc_idle_mode);
6189 info->tcsr_value &= ~IDLEMODE_MASK; /* clear idle mode bits */
6190 info->tcsr_value += usc_idle_mode;
6191 usc_OutReg(info, TCSR, info->tcsr_value);
6192
6193 /*
6194 * if SyncLink WAN adapter is running in external sync mode, the
6195 * transmitter has been set to Monosync in order to try to mimic
6196 * a true raw outbound bit stream. Monosync still sends an open/close
6197 * sync char at the start/end of a frame. Try to match those sync
6198 * patterns to the idle mode set here
6199 */
6200 if ( info->params.mode == MGSL_MODE_RAW ) {
6201 unsigned char syncpat = 0;
6202 switch( info->idle_mode ) {
6203 case HDLC_TXIDLE_FLAGS:
6204 syncpat = 0x7e;
6205 break;
6206 case HDLC_TXIDLE_ALT_ZEROS_ONES:
6207 syncpat = 0x55;
6208 break;
6209 case HDLC_TXIDLE_ZEROS:
6210 case HDLC_TXIDLE_SPACE:
6211 syncpat = 0x00;
6212 break;
6213 case HDLC_TXIDLE_ONES:
6214 case HDLC_TXIDLE_MARK:
6215 syncpat = 0xff;
6216 break;
6217 case HDLC_TXIDLE_ALT_MARK_SPACE:
6218 syncpat = 0xaa;
6219 break;
6220 }
6221
6222 usc_SetTransmitSyncChars(info,syncpat,syncpat);
6223 }
6224
6225} /* end of usc_set_txidle() */
6226
6227/* usc_get_serial_signals()
6228 *
6229 * Query the adapter for the state of the V24 status (input) signals.
6230 *
6231 * Arguments: info pointer to device instance data
6232 * Return Value: None
6233 */
6234static void usc_get_serial_signals( struct mgsl_struct *info )
6235{
6236 u16 status;
6237
6238 /* clear all serial signals except DTR and RTS */
6239 info->serial_signals &= SerialSignal_DTR + SerialSignal_RTS;
6240
6241 /* Read the Misc Interrupt status Register (MISR) to get */
6242 /* the V24 status signals. */
6243
6244 status = usc_InReg( info, MISR );
6245
6246 /* set serial signal bits to reflect MISR */
6247
6248 if ( status & MISCSTATUS_CTS )
6249 info->serial_signals |= SerialSignal_CTS;
6250
6251 if ( status & MISCSTATUS_DCD )
6252 info->serial_signals |= SerialSignal_DCD;
6253
6254 if ( status & MISCSTATUS_RI )
6255 info->serial_signals |= SerialSignal_RI;
6256
6257 if ( status & MISCSTATUS_DSR )
6258 info->serial_signals |= SerialSignal_DSR;
6259
6260} /* end of usc_get_serial_signals() */
6261
6262/* usc_set_serial_signals()
6263 *
6264 * Set the state of DTR and RTS based on contents of
6265 * serial_signals member of device extension.
6266 *
6267 * Arguments: info pointer to device instance data
6268 * Return Value: None
6269 */
6270static void usc_set_serial_signals( struct mgsl_struct *info )
6271{
6272 u16 Control;
6273 unsigned char V24Out = info->serial_signals;
6274
6275 /* get the current value of the Port Control Register (PCR) */
6276
6277 Control = usc_InReg( info, PCR );
6278
6279 if ( V24Out & SerialSignal_RTS )
6280 Control &= ~(BIT6);
6281 else
6282 Control |= BIT6;
6283
6284 if ( V24Out & SerialSignal_DTR )
6285 Control &= ~(BIT4);
6286 else
6287 Control |= BIT4;
6288
6289 usc_OutReg( info, PCR, Control );
6290
6291} /* end of usc_set_serial_signals() */
6292
6293/* usc_enable_async_clock()
6294 *
6295 * Enable the async clock at the specified frequency.
6296 *
6297 * Arguments: info pointer to device instance data
6298 * data_rate data rate of clock in bps
6299 * 0 disables the AUX clock.
6300 * Return Value: None
6301 */
6302static void usc_enable_async_clock( struct mgsl_struct *info, u32 data_rate )
6303{
6304 if ( data_rate ) {
6305 /*
6306 * Clock mode Control Register (CMCR)
6307 *
6308 * <15..14> 00 counter 1 Disabled
6309 * <13..12> 00 counter 0 Disabled
6310 * <11..10> 11 BRG1 Input is TxC Pin
6311 * <9..8> 11 BRG0 Input is TxC Pin
6312 * <7..6> 01 DPLL Input is BRG1 Output
6313 * <5..3> 100 TxCLK comes from BRG0
6314 * <2..0> 100 RxCLK comes from BRG0
6315 *
6316 * 0000 1111 0110 0100 = 0x0f64
6317 */
6318
6319 usc_OutReg( info, CMCR, 0x0f64 );
6320
6321
6322 /*
6323 * Write 16-bit Time Constant for BRG0
6324 * Time Constant = (ClkSpeed / data_rate) - 1
6325 * ClkSpeed = 921600 (ISA), 691200 (PCI)
6326 */
6327
6328 if ( info->bus_type == MGSL_BUS_TYPE_PCI )
6329 usc_OutReg( info, TC0R, (u16)((691200/data_rate) - 1) );
6330 else
6331 usc_OutReg( info, TC0R, (u16)((921600/data_rate) - 1) );
6332
6333
6334 /*
6335 * Hardware Configuration Register (HCR)
6336 * Clear Bit 1, BRG0 mode = Continuous
6337 * Set Bit 0 to enable BRG0.
6338 */
6339
6340 usc_OutReg( info, HCR,
6341 (u16)((usc_InReg( info, HCR ) & ~BIT1) | BIT0) );
6342
6343
6344 /* Input/Output Control Reg, <2..0> = 100, Drive RxC pin with BRG0 */
6345
6346 usc_OutReg( info, IOCR,
6347 (u16)((usc_InReg(info, IOCR) & 0xfff8) | 0x0004) );
6348 } else {
6349 /* data rate == 0 so turn off BRG0 */
6350 usc_OutReg( info, HCR, (u16)(usc_InReg( info, HCR ) & ~BIT0) );
6351 }
6352
6353} /* end of usc_enable_async_clock() */
6354
6355/*
6356 * Buffer Structures:
6357 *
6358 * Normal memory access uses virtual addresses that can make discontiguous
6359 * physical memory pages appear to be contiguous in the virtual address
6360 * space (the processors memory mapping handles the conversions).
6361 *
6362 * DMA transfers require physically contiguous memory. This is because
6363 * the DMA system controller and DMA bus masters deal with memory using
6364 * only physical addresses.
6365 *
6366 * This causes a problem under Windows NT when large DMA buffers are
6367 * needed. Fragmentation of the nonpaged pool prevents allocations of
6368 * physically contiguous buffers larger than the PAGE_SIZE.
6369 *
6370 * However the 16C32 supports Bus Master Scatter/Gather DMA which
6371 * allows DMA transfers to physically discontiguous buffers. Information
6372 * about each data transfer buffer is contained in a memory structure
6373 * called a 'buffer entry'. A list of buffer entries is maintained
6374 * to track and control the use of the data transfer buffers.
6375 *
6376 * To support this strategy we will allocate sufficient PAGE_SIZE
6377 * contiguous memory buffers to allow for the total required buffer
6378 * space.
6379 *
6380 * The 16C32 accesses the list of buffer entries using Bus Master
6381 * DMA. Control information is read from the buffer entries by the
6382 * 16C32 to control data transfers. status information is written to
6383 * the buffer entries by the 16C32 to indicate the status of completed
6384 * transfers.
6385 *
6386 * The CPU writes control information to the buffer entries to control
6387 * the 16C32 and reads status information from the buffer entries to
6388 * determine information about received and transmitted frames.
6389 *
6390 * Because the CPU and 16C32 (adapter) both need simultaneous access
6391 * to the buffer entries, the buffer entry memory is allocated with
6392 * HalAllocateCommonBuffer(). This restricts the size of the buffer
6393 * entry list to PAGE_SIZE.
6394 *
6395 * The actual data buffers on the other hand will only be accessed
6396 * by the CPU or the adapter but not by both simultaneously. This allows
6397 * Scatter/Gather packet based DMA procedures for using physically
6398 * discontiguous pages.
6399 */
6400
6401/*
6402 * mgsl_reset_tx_dma_buffers()
6403 *
6404 * Set the count for all transmit buffers to 0 to indicate the
6405 * buffer is available for use and set the current buffer to the
6406 * first buffer. This effectively makes all buffers free and
6407 * discards any data in buffers.
6408 *
6409 * Arguments: info pointer to device instance data
6410 * Return Value: None
6411 */
6412static void mgsl_reset_tx_dma_buffers( struct mgsl_struct *info )
6413{
6414 unsigned int i;
6415
6416 for ( i = 0; i < info->tx_buffer_count; i++ ) {
6417 *((unsigned long *)&(info->tx_buffer_list[i].count)) = 0;
6418 }
6419
6420 info->current_tx_buffer = 0;
6421 info->start_tx_dma_buffer = 0;
6422 info->tx_dma_buffers_used = 0;
6423
6424 info->get_tx_holding_index = 0;
6425 info->put_tx_holding_index = 0;
6426 info->tx_holding_count = 0;
6427
6428} /* end of mgsl_reset_tx_dma_buffers() */
6429
6430/*
6431 * num_free_tx_dma_buffers()
6432 *
6433 * returns the number of free tx dma buffers available
6434 *
6435 * Arguments: info pointer to device instance data
6436 * Return Value: number of free tx dma buffers
6437 */
6438static int num_free_tx_dma_buffers(struct mgsl_struct *info)
6439{
6440 return info->tx_buffer_count - info->tx_dma_buffers_used;
6441}
6442
6443/*
6444 * mgsl_reset_rx_dma_buffers()
6445 *
6446 * Set the count for all receive buffers to DMABUFFERSIZE
6447 * and set the current buffer to the first buffer. This effectively
6448 * makes all buffers free and discards any data in buffers.
6449 *
6450 * Arguments: info pointer to device instance data
6451 * Return Value: None
6452 */
6453static void mgsl_reset_rx_dma_buffers( struct mgsl_struct *info )
6454{
6455 unsigned int i;
6456
6457 for ( i = 0; i < info->rx_buffer_count; i++ ) {
6458 *((unsigned long *)&(info->rx_buffer_list[i].count)) = DMABUFFERSIZE;
6459// info->rx_buffer_list[i].count = DMABUFFERSIZE;
6460// info->rx_buffer_list[i].status = 0;
6461 }
6462
6463 info->current_rx_buffer = 0;
6464
6465} /* end of mgsl_reset_rx_dma_buffers() */
6466
6467/*
6468 * mgsl_free_rx_frame_buffers()
6469 *
6470 * Free the receive buffers used by a received SDLC
6471 * frame such that the buffers can be reused.
6472 *
6473 * Arguments:
6474 *
6475 * info pointer to device instance data
6476 * StartIndex index of 1st receive buffer of frame
6477 * EndIndex index of last receive buffer of frame
6478 *
6479 * Return Value: None
6480 */
6481static void mgsl_free_rx_frame_buffers( struct mgsl_struct *info, unsigned int StartIndex, unsigned int EndIndex )
6482{
6483 bool Done = false;
6484 DMABUFFERENTRY *pBufEntry;
6485 unsigned int Index;
6486
6487 /* Starting with 1st buffer entry of the frame clear the status */
6488 /* field and set the count field to DMA Buffer Size. */
6489
6490 Index = StartIndex;
6491
6492 while( !Done ) {
6493 pBufEntry = &(info->rx_buffer_list[Index]);
6494
6495 if ( Index == EndIndex ) {
6496 /* This is the last buffer of the frame! */
6497 Done = true;
6498 }
6499
6500 /* reset current buffer for reuse */
6501// pBufEntry->status = 0;
6502// pBufEntry->count = DMABUFFERSIZE;
6503 *((unsigned long *)&(pBufEntry->count)) = DMABUFFERSIZE;
6504
6505 /* advance to next buffer entry in linked list */
6506 Index++;
6507 if ( Index == info->rx_buffer_count )
6508 Index = 0;
6509 }
6510
6511 /* set current buffer to next buffer after last buffer of frame */
6512 info->current_rx_buffer = Index;
6513
6514} /* end of free_rx_frame_buffers() */
6515
6516/* mgsl_get_rx_frame()
6517 *
6518 * This function attempts to return a received SDLC frame from the
6519 * receive DMA buffers. Only frames received without errors are returned.
6520 *
6521 * Arguments: info pointer to device extension
6522 * Return Value: true if frame returned, otherwise false
6523 */
6524static bool mgsl_get_rx_frame(struct mgsl_struct *info)
6525{
6526 unsigned int StartIndex, EndIndex; /* index of 1st and last buffers of Rx frame */
6527 unsigned short status;
6528 DMABUFFERENTRY *pBufEntry;
6529 unsigned int framesize = 0;
6530 bool ReturnCode = false;
6531 unsigned long flags;
6532 struct tty_struct *tty = info->port.tty;
6533 bool return_frame = false;
6534
6535 /*
6536 * current_rx_buffer points to the 1st buffer of the next available
6537 * receive frame. To find the last buffer of the frame look for
6538 * a non-zero status field in the buffer entries. (The status
6539 * field is set by the 16C32 after completing a receive frame.
6540 */
6541
6542 StartIndex = EndIndex = info->current_rx_buffer;
6543
6544 while( !info->rx_buffer_list[EndIndex].status ) {
6545 /*
6546 * If the count field of the buffer entry is non-zero then
6547 * this buffer has not been used. (The 16C32 clears the count
6548 * field when it starts using the buffer.) If an unused buffer
6549 * is encountered then there are no frames available.
6550 */
6551
6552 if ( info->rx_buffer_list[EndIndex].count )
6553 goto Cleanup;
6554
6555 /* advance to next buffer entry in linked list */
6556 EndIndex++;
6557 if ( EndIndex == info->rx_buffer_count )
6558 EndIndex = 0;
6559
6560 /* if entire list searched then no frame available */
6561 if ( EndIndex == StartIndex ) {
6562 /* If this occurs then something bad happened,
6563 * all buffers have been 'used' but none mark
6564 * the end of a frame. Reset buffers and receiver.
6565 */
6566
6567 if ( info->rx_enabled ){
6568 spin_lock_irqsave(&info->irq_spinlock,flags);
6569 usc_start_receiver(info);
6570 spin_unlock_irqrestore(&info->irq_spinlock,flags);
6571 }
6572 goto Cleanup;
6573 }
6574 }
6575
6576
6577 /* check status of receive frame */
6578
6579 status = info->rx_buffer_list[EndIndex].status;
6580
6581 if ( status & (RXSTATUS_SHORT_FRAME + RXSTATUS_OVERRUN +
6582 RXSTATUS_CRC_ERROR + RXSTATUS_ABORT) ) {
6583 if ( status & RXSTATUS_SHORT_FRAME )
6584 info->icount.rxshort++;
6585 else if ( status & RXSTATUS_ABORT )
6586 info->icount.rxabort++;
6587 else if ( status & RXSTATUS_OVERRUN )
6588 info->icount.rxover++;
6589 else {
6590 info->icount.rxcrc++;
6591 if ( info->params.crc_type & HDLC_CRC_RETURN_EX )
6592 return_frame = true;
6593 }
6594 framesize = 0;
6595#if SYNCLINK_GENERIC_HDLC
6596 {
6597 info->netdev->stats.rx_errors++;
6598 info->netdev->stats.rx_frame_errors++;
6599 }
6600#endif
6601 } else
6602 return_frame = true;
6603
6604 if ( return_frame ) {
6605 /* receive frame has no errors, get frame size.
6606 * The frame size is the starting value of the RCC (which was
6607 * set to 0xffff) minus the ending value of the RCC (decremented
6608 * once for each receive character) minus 2 for the 16-bit CRC.
6609 */
6610
6611 framesize = RCLRVALUE - info->rx_buffer_list[EndIndex].rcc;
6612
6613 /* adjust frame size for CRC if any */
6614 if ( info->params.crc_type == HDLC_CRC_16_CCITT )
6615 framesize -= 2;
6616 else if ( info->params.crc_type == HDLC_CRC_32_CCITT )
6617 framesize -= 4;
6618 }
6619
6620 if ( debug_level >= DEBUG_LEVEL_BH )
6621 printk("%s(%d):mgsl_get_rx_frame(%s) status=%04X size=%d\n",
6622 __FILE__,__LINE__,info->device_name,status,framesize);
6623
6624 if ( debug_level >= DEBUG_LEVEL_DATA )
6625 mgsl_trace_block(info,info->rx_buffer_list[StartIndex].virt_addr,
6626 min_t(int, framesize, DMABUFFERSIZE),0);
6627
6628 if (framesize) {
6629 if ( ( (info->params.crc_type & HDLC_CRC_RETURN_EX) &&
6630 ((framesize+1) > info->max_frame_size) ) ||
6631 (framesize > info->max_frame_size) )
6632 info->icount.rxlong++;
6633 else {
6634 /* copy dma buffer(s) to contiguous intermediate buffer */
6635 int copy_count = framesize;
6636 int index = StartIndex;
6637 unsigned char *ptmp = info->intermediate_rxbuffer;
6638
6639 if ( !(status & RXSTATUS_CRC_ERROR))
6640 info->icount.rxok++;
6641
6642 while(copy_count) {
6643 int partial_count;
6644 if ( copy_count > DMABUFFERSIZE )
6645 partial_count = DMABUFFERSIZE;
6646 else
6647 partial_count = copy_count;
6648
6649 pBufEntry = &(info->rx_buffer_list[index]);
6650 memcpy( ptmp, pBufEntry->virt_addr, partial_count );
6651 ptmp += partial_count;
6652 copy_count -= partial_count;
6653
6654 if ( ++index == info->rx_buffer_count )
6655 index = 0;
6656 }
6657
6658 if ( info->params.crc_type & HDLC_CRC_RETURN_EX ) {
6659 ++framesize;
6660 *ptmp = (status & RXSTATUS_CRC_ERROR ?
6661 RX_CRC_ERROR :
6662 RX_OK);
6663
6664 if ( debug_level >= DEBUG_LEVEL_DATA )
6665 printk("%s(%d):mgsl_get_rx_frame(%s) rx frame status=%d\n",
6666 __FILE__,__LINE__,info->device_name,
6667 *ptmp);
6668 }
6669
6670#if SYNCLINK_GENERIC_HDLC
6671 if (info->netcount)
6672 hdlcdev_rx(info,info->intermediate_rxbuffer,framesize);
6673 else
6674#endif
6675 ldisc_receive_buf(tty, info->intermediate_rxbuffer, info->flag_buf, framesize);
6676 }
6677 }
6678 /* Free the buffers used by this frame. */
6679 mgsl_free_rx_frame_buffers( info, StartIndex, EndIndex );
6680
6681 ReturnCode = true;
6682
6683Cleanup:
6684
6685 if ( info->rx_enabled && info->rx_overflow ) {
6686 /* The receiver needs to restarted because of
6687 * a receive overflow (buffer or FIFO). If the
6688 * receive buffers are now empty, then restart receiver.
6689 */
6690
6691 if ( !info->rx_buffer_list[EndIndex].status &&
6692 info->rx_buffer_list[EndIndex].count ) {
6693 spin_lock_irqsave(&info->irq_spinlock,flags);
6694 usc_start_receiver(info);
6695 spin_unlock_irqrestore(&info->irq_spinlock,flags);
6696 }
6697 }
6698
6699 return ReturnCode;
6700
6701} /* end of mgsl_get_rx_frame() */
6702
6703/* mgsl_get_raw_rx_frame()
6704 *
6705 * This function attempts to return a received frame from the
6706 * receive DMA buffers when running in external loop mode. In this mode,
6707 * we will return at most one DMABUFFERSIZE frame to the application.
6708 * The USC receiver is triggering off of DCD going active to start a new
6709 * frame, and DCD going inactive to terminate the frame (similar to
6710 * processing a closing flag character).
6711 *
6712 * In this routine, we will return DMABUFFERSIZE "chunks" at a time.
6713 * If DCD goes inactive, the last Rx DMA Buffer will have a non-zero
6714 * status field and the RCC field will indicate the length of the
6715 * entire received frame. We take this RCC field and get the modulus
6716 * of RCC and DMABUFFERSIZE to determine if number of bytes in the
6717 * last Rx DMA buffer and return that last portion of the frame.
6718 *
6719 * Arguments: info pointer to device extension
6720 * Return Value: true if frame returned, otherwise false
6721 */
6722static bool mgsl_get_raw_rx_frame(struct mgsl_struct *info)
6723{
6724 unsigned int CurrentIndex, NextIndex;
6725 unsigned short status;
6726 DMABUFFERENTRY *pBufEntry;
6727 unsigned int framesize = 0;
6728 bool ReturnCode = false;
6729 unsigned long flags;
6730 struct tty_struct *tty = info->port.tty;
6731
6732 /*
6733 * current_rx_buffer points to the 1st buffer of the next available
6734 * receive frame. The status field is set by the 16C32 after
6735 * completing a receive frame. If the status field of this buffer
6736 * is zero, either the USC is still filling this buffer or this
6737 * is one of a series of buffers making up a received frame.
6738 *
6739 * If the count field of this buffer is zero, the USC is either
6740 * using this buffer or has used this buffer. Look at the count
6741 * field of the next buffer. If that next buffer's count is
6742 * non-zero, the USC is still actively using the current buffer.
6743 * Otherwise, if the next buffer's count field is zero, the
6744 * current buffer is complete and the USC is using the next
6745 * buffer.
6746 */
6747 CurrentIndex = NextIndex = info->current_rx_buffer;
6748 ++NextIndex;
6749 if ( NextIndex == info->rx_buffer_count )
6750 NextIndex = 0;
6751
6752 if ( info->rx_buffer_list[CurrentIndex].status != 0 ||
6753 (info->rx_buffer_list[CurrentIndex].count == 0 &&
6754 info->rx_buffer_list[NextIndex].count == 0)) {
6755 /*
6756 * Either the status field of this dma buffer is non-zero
6757 * (indicating the last buffer of a receive frame) or the next
6758 * buffer is marked as in use -- implying this buffer is complete
6759 * and an intermediate buffer for this received frame.
6760 */
6761
6762 status = info->rx_buffer_list[CurrentIndex].status;
6763
6764 if ( status & (RXSTATUS_SHORT_FRAME + RXSTATUS_OVERRUN +
6765 RXSTATUS_CRC_ERROR + RXSTATUS_ABORT) ) {
6766 if ( status & RXSTATUS_SHORT_FRAME )
6767 info->icount.rxshort++;
6768 else if ( status & RXSTATUS_ABORT )
6769 info->icount.rxabort++;
6770 else if ( status & RXSTATUS_OVERRUN )
6771 info->icount.rxover++;
6772 else
6773 info->icount.rxcrc++;
6774 framesize = 0;
6775 } else {
6776 /*
6777 * A receive frame is available, get frame size and status.
6778 *
6779 * The frame size is the starting value of the RCC (which was
6780 * set to 0xffff) minus the ending value of the RCC (decremented
6781 * once for each receive character) minus 2 or 4 for the 16-bit
6782 * or 32-bit CRC.
6783 *
6784 * If the status field is zero, this is an intermediate buffer.
6785 * It's size is 4K.
6786 *
6787 * If the DMA Buffer Entry's Status field is non-zero, the
6788 * receive operation completed normally (ie: DCD dropped). The
6789 * RCC field is valid and holds the received frame size.
6790 * It is possible that the RCC field will be zero on a DMA buffer
6791 * entry with a non-zero status. This can occur if the total
6792 * frame size (number of bytes between the time DCD goes active
6793 * to the time DCD goes inactive) exceeds 65535 bytes. In this
6794 * case the 16C32 has underrun on the RCC count and appears to
6795 * stop updating this counter to let us know the actual received
6796 * frame size. If this happens (non-zero status and zero RCC),
6797 * simply return the entire RxDMA Buffer
6798 */
6799 if ( status ) {
6800 /*
6801 * In the event that the final RxDMA Buffer is
6802 * terminated with a non-zero status and the RCC
6803 * field is zero, we interpret this as the RCC
6804 * having underflowed (received frame > 65535 bytes).
6805 *
6806 * Signal the event to the user by passing back
6807 * a status of RxStatus_CrcError returning the full
6808 * buffer and let the app figure out what data is
6809 * actually valid
6810 */
6811 if ( info->rx_buffer_list[CurrentIndex].rcc )
6812 framesize = RCLRVALUE - info->rx_buffer_list[CurrentIndex].rcc;
6813 else
6814 framesize = DMABUFFERSIZE;
6815 }
6816 else
6817 framesize = DMABUFFERSIZE;
6818 }
6819
6820 if ( framesize > DMABUFFERSIZE ) {
6821 /*
6822 * if running in raw sync mode, ISR handler for
6823 * End Of Buffer events terminates all buffers at 4K.
6824 * If this frame size is said to be >4K, get the
6825 * actual number of bytes of the frame in this buffer.
6826 */
6827 framesize = framesize % DMABUFFERSIZE;
6828 }
6829
6830
6831 if ( debug_level >= DEBUG_LEVEL_BH )
6832 printk("%s(%d):mgsl_get_raw_rx_frame(%s) status=%04X size=%d\n",
6833 __FILE__,__LINE__,info->device_name,status,framesize);
6834
6835 if ( debug_level >= DEBUG_LEVEL_DATA )
6836 mgsl_trace_block(info,info->rx_buffer_list[CurrentIndex].virt_addr,
6837 min_t(int, framesize, DMABUFFERSIZE),0);
6838
6839 if (framesize) {
6840 /* copy dma buffer(s) to contiguous intermediate buffer */
6841 /* NOTE: we never copy more than DMABUFFERSIZE bytes */
6842
6843 pBufEntry = &(info->rx_buffer_list[CurrentIndex]);
6844 memcpy( info->intermediate_rxbuffer, pBufEntry->virt_addr, framesize);
6845 info->icount.rxok++;
6846
6847 ldisc_receive_buf(tty, info->intermediate_rxbuffer, info->flag_buf, framesize);
6848 }
6849
6850 /* Free the buffers used by this frame. */
6851 mgsl_free_rx_frame_buffers( info, CurrentIndex, CurrentIndex );
6852
6853 ReturnCode = true;
6854 }
6855
6856
6857 if ( info->rx_enabled && info->rx_overflow ) {
6858 /* The receiver needs to restarted because of
6859 * a receive overflow (buffer or FIFO). If the
6860 * receive buffers are now empty, then restart receiver.
6861 */
6862
6863 if ( !info->rx_buffer_list[CurrentIndex].status &&
6864 info->rx_buffer_list[CurrentIndex].count ) {
6865 spin_lock_irqsave(&info->irq_spinlock,flags);
6866 usc_start_receiver(info);
6867 spin_unlock_irqrestore(&info->irq_spinlock,flags);
6868 }
6869 }
6870
6871 return ReturnCode;
6872
6873} /* end of mgsl_get_raw_rx_frame() */
6874
6875/* mgsl_load_tx_dma_buffer()
6876 *
6877 * Load the transmit DMA buffer with the specified data.
6878 *
6879 * Arguments:
6880 *
6881 * info pointer to device extension
6882 * Buffer pointer to buffer containing frame to load
6883 * BufferSize size in bytes of frame in Buffer
6884 *
6885 * Return Value: None
6886 */
6887static void mgsl_load_tx_dma_buffer(struct mgsl_struct *info,
6888 const char *Buffer, unsigned int BufferSize)
6889{
6890 unsigned short Copycount;
6891 unsigned int i = 0;
6892 DMABUFFERENTRY *pBufEntry;
6893
6894 if ( debug_level >= DEBUG_LEVEL_DATA )
6895 mgsl_trace_block(info,Buffer, min_t(int, BufferSize, DMABUFFERSIZE), 1);
6896
6897 if (info->params.flags & HDLC_FLAG_HDLC_LOOPMODE) {
6898 /* set CMR:13 to start transmit when
6899 * next GoAhead (abort) is received
6900 */
6901 info->cmr_value |= BIT13;
6902 }
6903
6904 /* begin loading the frame in the next available tx dma
6905 * buffer, remember it's starting location for setting
6906 * up tx dma operation
6907 */
6908 i = info->current_tx_buffer;
6909 info->start_tx_dma_buffer = i;
6910
6911 /* Setup the status and RCC (Frame Size) fields of the 1st */
6912 /* buffer entry in the transmit DMA buffer list. */
6913
6914 info->tx_buffer_list[i].status = info->cmr_value & 0xf000;
6915 info->tx_buffer_list[i].rcc = BufferSize;
6916 info->tx_buffer_list[i].count = BufferSize;
6917
6918 /* Copy frame data from 1st source buffer to the DMA buffers. */
6919 /* The frame data may span multiple DMA buffers. */
6920
6921 while( BufferSize ){
6922 /* Get a pointer to next DMA buffer entry. */
6923 pBufEntry = &info->tx_buffer_list[i++];
6924
6925 if ( i == info->tx_buffer_count )
6926 i=0;
6927
6928 /* Calculate the number of bytes that can be copied from */
6929 /* the source buffer to this DMA buffer. */
6930 if ( BufferSize > DMABUFFERSIZE )
6931 Copycount = DMABUFFERSIZE;
6932 else
6933 Copycount = BufferSize;
6934
6935 /* Actually copy data from source buffer to DMA buffer. */
6936 /* Also set the data count for this individual DMA buffer. */
6937 if ( info->bus_type == MGSL_BUS_TYPE_PCI )
6938 mgsl_load_pci_memory(pBufEntry->virt_addr, Buffer,Copycount);
6939 else
6940 memcpy(pBufEntry->virt_addr, Buffer, Copycount);
6941
6942 pBufEntry->count = Copycount;
6943
6944 /* Advance source pointer and reduce remaining data count. */
6945 Buffer += Copycount;
6946 BufferSize -= Copycount;
6947
6948 ++info->tx_dma_buffers_used;
6949 }
6950
6951 /* remember next available tx dma buffer */
6952 info->current_tx_buffer = i;
6953
6954} /* end of mgsl_load_tx_dma_buffer() */
6955
6956/*
6957 * mgsl_register_test()
6958 *
6959 * Performs a register test of the 16C32.
6960 *
6961 * Arguments: info pointer to device instance data
6962 * Return Value: true if test passed, otherwise false
6963 */
6964static bool mgsl_register_test( struct mgsl_struct *info )
6965{
6966 static unsigned short BitPatterns[] =
6967 { 0x0000, 0xffff, 0xaaaa, 0x5555, 0x1234, 0x6969, 0x9696, 0x0f0f };
6968 static unsigned int Patterncount = ARRAY_SIZE(BitPatterns);
6969 unsigned int i;
6970 bool rc = true;
6971 unsigned long flags;
6972
6973 spin_lock_irqsave(&info->irq_spinlock,flags);
6974 usc_reset(info);
6975
6976 /* Verify the reset state of some registers. */
6977
6978 if ( (usc_InReg( info, SICR ) != 0) ||
6979 (usc_InReg( info, IVR ) != 0) ||
6980 (usc_InDmaReg( info, DIVR ) != 0) ){
6981 rc = false;
6982 }
6983
6984 if ( rc ){
6985 /* Write bit patterns to various registers but do it out of */
6986 /* sync, then read back and verify values. */
6987
6988 for ( i = 0 ; i < Patterncount ; i++ ) {
6989 usc_OutReg( info, TC0R, BitPatterns[i] );
6990 usc_OutReg( info, TC1R, BitPatterns[(i+1)%Patterncount] );
6991 usc_OutReg( info, TCLR, BitPatterns[(i+2)%Patterncount] );
6992 usc_OutReg( info, RCLR, BitPatterns[(i+3)%Patterncount] );
6993 usc_OutReg( info, RSR, BitPatterns[(i+4)%Patterncount] );
6994 usc_OutDmaReg( info, TBCR, BitPatterns[(i+5)%Patterncount] );
6995
6996 if ( (usc_InReg( info, TC0R ) != BitPatterns[i]) ||
6997 (usc_InReg( info, TC1R ) != BitPatterns[(i+1)%Patterncount]) ||
6998 (usc_InReg( info, TCLR ) != BitPatterns[(i+2)%Patterncount]) ||
6999 (usc_InReg( info, RCLR ) != BitPatterns[(i+3)%Patterncount]) ||
7000 (usc_InReg( info, RSR ) != BitPatterns[(i+4)%Patterncount]) ||
7001 (usc_InDmaReg( info, TBCR ) != BitPatterns[(i+5)%Patterncount]) ){
7002 rc = false;
7003 break;
7004 }
7005 }
7006 }
7007
7008 usc_reset(info);
7009 spin_unlock_irqrestore(&info->irq_spinlock,flags);
7010
7011 return rc;
7012
7013} /* end of mgsl_register_test() */
7014
7015/* mgsl_irq_test() Perform interrupt test of the 16C32.
7016 *
7017 * Arguments: info pointer to device instance data
7018 * Return Value: true if test passed, otherwise false
7019 */
7020static bool mgsl_irq_test( struct mgsl_struct *info )
7021{
7022 unsigned long EndTime;
7023 unsigned long flags;
7024
7025 spin_lock_irqsave(&info->irq_spinlock,flags);
7026 usc_reset(info);
7027
7028 /*
7029 * Setup 16C32 to interrupt on TxC pin (14MHz clock) transition.
7030 * The ISR sets irq_occurred to true.
7031 */
7032
7033 info->irq_occurred = false;
7034
7035 /* Enable INTEN gate for ISA adapter (Port 6, Bit12) */
7036 /* Enable INTEN (Port 6, Bit12) */
7037 /* This connects the IRQ request signal to the ISA bus */
7038 /* on the ISA adapter. This has no effect for the PCI adapter */
7039 usc_OutReg( info, PCR, (unsigned short)((usc_InReg(info, PCR) | BIT13) & ~BIT12) );
7040
7041 usc_EnableMasterIrqBit(info);
7042 usc_EnableInterrupts(info, IO_PIN);
7043 usc_ClearIrqPendingBits(info, IO_PIN);
7044
7045 usc_UnlatchIostatusBits(info, MISCSTATUS_TXC_LATCHED);
7046 usc_EnableStatusIrqs(info, SICR_TXC_ACTIVE + SICR_TXC_INACTIVE);
7047
7048 spin_unlock_irqrestore(&info->irq_spinlock,flags);
7049
7050 EndTime=100;
7051 while( EndTime-- && !info->irq_occurred ) {
7052 msleep_interruptible(10);
7053 }
7054
7055 spin_lock_irqsave(&info->irq_spinlock,flags);
7056 usc_reset(info);
7057 spin_unlock_irqrestore(&info->irq_spinlock,flags);
7058
7059 return info->irq_occurred;
7060
7061} /* end of mgsl_irq_test() */
7062
7063/* mgsl_dma_test()
7064 *
7065 * Perform a DMA test of the 16C32. A small frame is
7066 * transmitted via DMA from a transmit buffer to a receive buffer
7067 * using single buffer DMA mode.
7068 *
7069 * Arguments: info pointer to device instance data
7070 * Return Value: true if test passed, otherwise false
7071 */
7072static bool mgsl_dma_test( struct mgsl_struct *info )
7073{
7074 unsigned short FifoLevel;
7075 unsigned long phys_addr;
7076 unsigned int FrameSize;
7077 unsigned int i;
7078 char *TmpPtr;
7079 bool rc = true;
7080 unsigned short status=0;
7081 unsigned long EndTime;
7082 unsigned long flags;
7083 MGSL_PARAMS tmp_params;
7084
7085 /* save current port options */
7086 memcpy(&tmp_params,&info->params,sizeof(MGSL_PARAMS));
7087 /* load default port options */
7088 memcpy(&info->params,&default_params,sizeof(MGSL_PARAMS));
7089
7090#define TESTFRAMESIZE 40
7091
7092 spin_lock_irqsave(&info->irq_spinlock,flags);
7093
7094 /* setup 16C32 for SDLC DMA transfer mode */
7095
7096 usc_reset(info);
7097 usc_set_sdlc_mode(info);
7098 usc_enable_loopback(info,1);
7099
7100 /* Reprogram the RDMR so that the 16C32 does NOT clear the count
7101 * field of the buffer entry after fetching buffer address. This
7102 * way we can detect a DMA failure for a DMA read (which should be
7103 * non-destructive to system memory) before we try and write to
7104 * memory (where a failure could corrupt system memory).
7105 */
7106
7107 /* Receive DMA mode Register (RDMR)
7108 *
7109 * <15..14> 11 DMA mode = Linked List Buffer mode
7110 * <13> 1 RSBinA/L = store Rx status Block in List entry
7111 * <12> 0 1 = Clear count of List Entry after fetching
7112 * <11..10> 00 Address mode = Increment
7113 * <9> 1 Terminate Buffer on RxBound
7114 * <8> 0 Bus Width = 16bits
7115 * <7..0> ? status Bits (write as 0s)
7116 *
7117 * 1110 0010 0000 0000 = 0xe200
7118 */
7119
7120 usc_OutDmaReg( info, RDMR, 0xe200 );
7121
7122 spin_unlock_irqrestore(&info->irq_spinlock,flags);
7123
7124
7125 /* SETUP TRANSMIT AND RECEIVE DMA BUFFERS */
7126
7127 FrameSize = TESTFRAMESIZE;
7128
7129 /* setup 1st transmit buffer entry: */
7130 /* with frame size and transmit control word */
7131
7132 info->tx_buffer_list[0].count = FrameSize;
7133 info->tx_buffer_list[0].rcc = FrameSize;
7134 info->tx_buffer_list[0].status = 0x4000;
7135
7136 /* build a transmit frame in 1st transmit DMA buffer */
7137
7138 TmpPtr = info->tx_buffer_list[0].virt_addr;
7139 for (i = 0; i < FrameSize; i++ )
7140 *TmpPtr++ = i;
7141
7142 /* setup 1st receive buffer entry: */
7143 /* clear status, set max receive buffer size */
7144
7145 info->rx_buffer_list[0].status = 0;
7146 info->rx_buffer_list[0].count = FrameSize + 4;
7147
7148 /* zero out the 1st receive buffer */
7149
7150 memset( info->rx_buffer_list[0].virt_addr, 0, FrameSize + 4 );
7151
7152 /* Set count field of next buffer entries to prevent */
7153 /* 16C32 from using buffers after the 1st one. */
7154
7155 info->tx_buffer_list[1].count = 0;
7156 info->rx_buffer_list[1].count = 0;
7157
7158
7159 /***************************/
7160 /* Program 16C32 receiver. */
7161 /***************************/
7162
7163 spin_lock_irqsave(&info->irq_spinlock,flags);
7164
7165 /* setup DMA transfers */
7166 usc_RTCmd( info, RTCmd_PurgeRxFifo );
7167
7168 /* program 16C32 receiver with physical address of 1st DMA buffer entry */
7169 phys_addr = info->rx_buffer_list[0].phys_entry;
7170 usc_OutDmaReg( info, NRARL, (unsigned short)phys_addr );
7171 usc_OutDmaReg( info, NRARU, (unsigned short)(phys_addr >> 16) );
7172
7173 /* Clear the Rx DMA status bits (read RDMR) and start channel */
7174 usc_InDmaReg( info, RDMR );
7175 usc_DmaCmd( info, DmaCmd_InitRxChannel );
7176
7177 /* Enable Receiver (RMR <1..0> = 10) */
7178 usc_OutReg( info, RMR, (unsigned short)((usc_InReg(info, RMR) & 0xfffc) | 0x0002) );
7179
7180 spin_unlock_irqrestore(&info->irq_spinlock,flags);
7181
7182
7183 /*************************************************************/
7184 /* WAIT FOR RECEIVER TO DMA ALL PARAMETERS FROM BUFFER ENTRY */
7185 /*************************************************************/
7186
7187 /* Wait 100ms for interrupt. */
7188 EndTime = jiffies + msecs_to_jiffies(100);
7189
7190 for(;;) {
7191 if (time_after(jiffies, EndTime)) {
7192 rc = false;
7193 break;
7194 }
7195
7196 spin_lock_irqsave(&info->irq_spinlock,flags);
7197 status = usc_InDmaReg( info, RDMR );
7198 spin_unlock_irqrestore(&info->irq_spinlock,flags);
7199
7200 if ( !(status & BIT4) && (status & BIT5) ) {
7201 /* INITG (BIT 4) is inactive (no entry read in progress) AND */
7202 /* BUSY (BIT 5) is active (channel still active). */
7203 /* This means the buffer entry read has completed. */
7204 break;
7205 }
7206 }
7207
7208
7209 /******************************/
7210 /* Program 16C32 transmitter. */
7211 /******************************/
7212
7213 spin_lock_irqsave(&info->irq_spinlock,flags);
7214
7215 /* Program the Transmit Character Length Register (TCLR) */
7216 /* and clear FIFO (TCC is loaded with TCLR on FIFO clear) */
7217
7218 usc_OutReg( info, TCLR, (unsigned short)info->tx_buffer_list[0].count );
7219 usc_RTCmd( info, RTCmd_PurgeTxFifo );
7220
7221 /* Program the address of the 1st DMA Buffer Entry in linked list */
7222
7223 phys_addr = info->tx_buffer_list[0].phys_entry;
7224 usc_OutDmaReg( info, NTARL, (unsigned short)phys_addr );
7225 usc_OutDmaReg( info, NTARU, (unsigned short)(phys_addr >> 16) );
7226
7227 /* unlatch Tx status bits, and start transmit channel. */
7228
7229 usc_OutReg( info, TCSR, (unsigned short)(( usc_InReg(info, TCSR) & 0x0f00) | 0xfa) );
7230 usc_DmaCmd( info, DmaCmd_InitTxChannel );
7231
7232 /* wait for DMA controller to fill transmit FIFO */
7233
7234 usc_TCmd( info, TCmd_SelectTicrTxFifostatus );
7235
7236 spin_unlock_irqrestore(&info->irq_spinlock,flags);
7237
7238
7239 /**********************************/
7240 /* WAIT FOR TRANSMIT FIFO TO FILL */
7241 /**********************************/
7242
7243 /* Wait 100ms */
7244 EndTime = jiffies + msecs_to_jiffies(100);
7245
7246 for(;;) {
7247 if (time_after(jiffies, EndTime)) {
7248 rc = false;
7249 break;
7250 }
7251
7252 spin_lock_irqsave(&info->irq_spinlock,flags);
7253 FifoLevel = usc_InReg(info, TICR) >> 8;
7254 spin_unlock_irqrestore(&info->irq_spinlock,flags);
7255
7256 if ( FifoLevel < 16 )
7257 break;
7258 else
7259 if ( FrameSize < 32 ) {
7260 /* This frame is smaller than the entire transmit FIFO */
7261 /* so wait for the entire frame to be loaded. */
7262 if ( FifoLevel <= (32 - FrameSize) )
7263 break;
7264 }
7265 }
7266
7267
7268 if ( rc )
7269 {
7270 /* Enable 16C32 transmitter. */
7271
7272 spin_lock_irqsave(&info->irq_spinlock,flags);
7273
7274 /* Transmit mode Register (TMR), <1..0> = 10, Enable Transmitter */
7275 usc_TCmd( info, TCmd_SendFrame );
7276 usc_OutReg( info, TMR, (unsigned short)((usc_InReg(info, TMR) & 0xfffc) | 0x0002) );
7277
7278 spin_unlock_irqrestore(&info->irq_spinlock,flags);
7279
7280
7281 /******************************/
7282 /* WAIT FOR TRANSMIT COMPLETE */
7283 /******************************/
7284
7285 /* Wait 100ms */
7286 EndTime = jiffies + msecs_to_jiffies(100);
7287
7288 /* While timer not expired wait for transmit complete */
7289
7290 spin_lock_irqsave(&info->irq_spinlock,flags);
7291 status = usc_InReg( info, TCSR );
7292 spin_unlock_irqrestore(&info->irq_spinlock,flags);
7293
7294 while ( !(status & (BIT6+BIT5+BIT4+BIT2+BIT1)) ) {
7295 if (time_after(jiffies, EndTime)) {
7296 rc = false;
7297 break;
7298 }
7299
7300 spin_lock_irqsave(&info->irq_spinlock,flags);
7301 status = usc_InReg( info, TCSR );
7302 spin_unlock_irqrestore(&info->irq_spinlock,flags);
7303 }
7304 }
7305
7306
7307 if ( rc ){
7308 /* CHECK FOR TRANSMIT ERRORS */
7309 if ( status & (BIT5 + BIT1) )
7310 rc = false;
7311 }
7312
7313 if ( rc ) {
7314 /* WAIT FOR RECEIVE COMPLETE */
7315
7316 /* Wait 100ms */
7317 EndTime = jiffies + msecs_to_jiffies(100);
7318
7319 /* Wait for 16C32 to write receive status to buffer entry. */
7320 status=info->rx_buffer_list[0].status;
7321 while ( status == 0 ) {
7322 if (time_after(jiffies, EndTime)) {
7323 rc = false;
7324 break;
7325 }
7326 status=info->rx_buffer_list[0].status;
7327 }
7328 }
7329
7330
7331 if ( rc ) {
7332 /* CHECK FOR RECEIVE ERRORS */
7333 status = info->rx_buffer_list[0].status;
7334
7335 if ( status & (BIT8 + BIT3 + BIT1) ) {
7336 /* receive error has occurred */
7337 rc = false;
7338 } else {
7339 if ( memcmp( info->tx_buffer_list[0].virt_addr ,
7340 info->rx_buffer_list[0].virt_addr, FrameSize ) ){
7341 rc = false;
7342 }
7343 }
7344 }
7345
7346 spin_lock_irqsave(&info->irq_spinlock,flags);
7347 usc_reset( info );
7348 spin_unlock_irqrestore(&info->irq_spinlock,flags);
7349
7350 /* restore current port options */
7351 memcpy(&info->params,&tmp_params,sizeof(MGSL_PARAMS));
7352
7353 return rc;
7354
7355} /* end of mgsl_dma_test() */
7356
7357/* mgsl_adapter_test()
7358 *
7359 * Perform the register, IRQ, and DMA tests for the 16C32.
7360 *
7361 * Arguments: info pointer to device instance data
7362 * Return Value: 0 if success, otherwise -ENODEV
7363 */
7364static int mgsl_adapter_test( struct mgsl_struct *info )
7365{
7366 if ( debug_level >= DEBUG_LEVEL_INFO )
7367 printk( "%s(%d):Testing device %s\n",
7368 __FILE__,__LINE__,info->device_name );
7369
7370 if ( !mgsl_register_test( info ) ) {
7371 info->init_error = DiagStatus_AddressFailure;
7372 printk( "%s(%d):Register test failure for device %s Addr=%04X\n",
7373 __FILE__,__LINE__,info->device_name, (unsigned short)(info->io_base) );
7374 return -ENODEV;
7375 }
7376
7377 if ( !mgsl_irq_test( info ) ) {
7378 info->init_error = DiagStatus_IrqFailure;
7379 printk( "%s(%d):Interrupt test failure for device %s IRQ=%d\n",
7380 __FILE__,__LINE__,info->device_name, (unsigned short)(info->irq_level) );
7381 return -ENODEV;
7382 }
7383
7384 if ( !mgsl_dma_test( info ) ) {
7385 info->init_error = DiagStatus_DmaFailure;
7386 printk( "%s(%d):DMA test failure for device %s DMA=%d\n",
7387 __FILE__,__LINE__,info->device_name, (unsigned short)(info->dma_level) );
7388 return -ENODEV;
7389 }
7390
7391 if ( debug_level >= DEBUG_LEVEL_INFO )
7392 printk( "%s(%d):device %s passed diagnostics\n",
7393 __FILE__,__LINE__,info->device_name );
7394
7395 return 0;
7396
7397} /* end of mgsl_adapter_test() */
7398
7399/* mgsl_memory_test()
7400 *
7401 * Test the shared memory on a PCI adapter.
7402 *
7403 * Arguments: info pointer to device instance data
7404 * Return Value: true if test passed, otherwise false
7405 */
7406static bool mgsl_memory_test( struct mgsl_struct *info )
7407{
7408 static unsigned long BitPatterns[] =
7409 { 0x0, 0x55555555, 0xaaaaaaaa, 0x66666666, 0x99999999, 0xffffffff, 0x12345678 };
7410 unsigned long Patterncount = ARRAY_SIZE(BitPatterns);
7411 unsigned long i;
7412 unsigned long TestLimit = SHARED_MEM_ADDRESS_SIZE/sizeof(unsigned long);
7413 unsigned long * TestAddr;
7414
7415 if ( info->bus_type != MGSL_BUS_TYPE_PCI )
7416 return true;
7417
7418 TestAddr = (unsigned long *)info->memory_base;
7419
7420 /* Test data lines with test pattern at one location. */
7421
7422 for ( i = 0 ; i < Patterncount ; i++ ) {
7423 *TestAddr = BitPatterns[i];
7424 if ( *TestAddr != BitPatterns[i] )
7425 return false;
7426 }
7427
7428 /* Test address lines with incrementing pattern over */
7429 /* entire address range. */
7430
7431 for ( i = 0 ; i < TestLimit ; i++ ) {
7432 *TestAddr = i * 4;
7433 TestAddr++;
7434 }
7435
7436 TestAddr = (unsigned long *)info->memory_base;
7437
7438 for ( i = 0 ; i < TestLimit ; i++ ) {
7439 if ( *TestAddr != i * 4 )
7440 return false;
7441 TestAddr++;
7442 }
7443
7444 memset( info->memory_base, 0, SHARED_MEM_ADDRESS_SIZE );
7445
7446 return true;
7447
7448} /* End Of mgsl_memory_test() */
7449
7450
7451/* mgsl_load_pci_memory()
7452 *
7453 * Load a large block of data into the PCI shared memory.
7454 * Use this instead of memcpy() or memmove() to move data
7455 * into the PCI shared memory.
7456 *
7457 * Notes:
7458 *
7459 * This function prevents the PCI9050 interface chip from hogging
7460 * the adapter local bus, which can starve the 16C32 by preventing
7461 * 16C32 bus master cycles.
7462 *
7463 * The PCI9050 documentation says that the 9050 will always release
7464 * control of the local bus after completing the current read
7465 * or write operation.
7466 *
7467 * It appears that as long as the PCI9050 write FIFO is full, the
7468 * PCI9050 treats all of the writes as a single burst transaction
7469 * and will not release the bus. This causes DMA latency problems
7470 * at high speeds when copying large data blocks to the shared
7471 * memory.
7472 *
7473 * This function in effect, breaks the a large shared memory write
7474 * into multiple transations by interleaving a shared memory read
7475 * which will flush the write FIFO and 'complete' the write
7476 * transation. This allows any pending DMA request to gain control
7477 * of the local bus in a timely fasion.
7478 *
7479 * Arguments:
7480 *
7481 * TargetPtr pointer to target address in PCI shared memory
7482 * SourcePtr pointer to source buffer for data
7483 * count count in bytes of data to copy
7484 *
7485 * Return Value: None
7486 */
7487static void mgsl_load_pci_memory( char* TargetPtr, const char* SourcePtr,
7488 unsigned short count )
7489{
7490 /* 16 32-bit writes @ 60ns each = 960ns max latency on local bus */
7491#define PCI_LOAD_INTERVAL 64
7492
7493 unsigned short Intervalcount = count / PCI_LOAD_INTERVAL;
7494 unsigned short Index;
7495 unsigned long Dummy;
7496
7497 for ( Index = 0 ; Index < Intervalcount ; Index++ )
7498 {
7499 memcpy(TargetPtr, SourcePtr, PCI_LOAD_INTERVAL);
7500 Dummy = *((volatile unsigned long *)TargetPtr);
7501 TargetPtr += PCI_LOAD_INTERVAL;
7502 SourcePtr += PCI_LOAD_INTERVAL;
7503 }
7504
7505 memcpy( TargetPtr, SourcePtr, count % PCI_LOAD_INTERVAL );
7506
7507} /* End Of mgsl_load_pci_memory() */
7508
7509static void mgsl_trace_block(struct mgsl_struct *info,const char* data, int count, int xmit)
7510{
7511 int i;
7512 int linecount;
7513 if (xmit)
7514 printk("%s tx data:\n",info->device_name);
7515 else
7516 printk("%s rx data:\n",info->device_name);
7517
7518 while(count) {
7519 if (count > 16)
7520 linecount = 16;
7521 else
7522 linecount = count;
7523
7524 for(i=0;i<linecount;i++)
7525 printk("%02X ",(unsigned char)data[i]);
7526 for(;i<17;i++)
7527 printk(" ");
7528 for(i=0;i<linecount;i++) {
7529 if (data[i]>=040 && data[i]<=0176)
7530 printk("%c",data[i]);
7531 else
7532 printk(".");
7533 }
7534 printk("\n");
7535
7536 data += linecount;
7537 count -= linecount;
7538 }
7539} /* end of mgsl_trace_block() */
7540
7541/* mgsl_tx_timeout()
7542 *
7543 * called when HDLC frame times out
7544 * update stats and do tx completion processing
7545 *
7546 * Arguments: context pointer to device instance data
7547 * Return Value: None
7548 */
7549static void mgsl_tx_timeout(unsigned long context)
7550{
7551 struct mgsl_struct *info = (struct mgsl_struct*)context;
7552 unsigned long flags;
7553
7554 if ( debug_level >= DEBUG_LEVEL_INFO )
7555 printk( "%s(%d):mgsl_tx_timeout(%s)\n",
7556 __FILE__,__LINE__,info->device_name);
7557 if(info->tx_active &&
7558 (info->params.mode == MGSL_MODE_HDLC ||
7559 info->params.mode == MGSL_MODE_RAW) ) {
7560 info->icount.txtimeout++;
7561 }
7562 spin_lock_irqsave(&info->irq_spinlock,flags);
7563 info->tx_active = false;
7564 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
7565
7566 if ( info->params.flags & HDLC_FLAG_HDLC_LOOPMODE )
7567 usc_loopmode_cancel_transmit( info );
7568
7569 spin_unlock_irqrestore(&info->irq_spinlock,flags);
7570
7571#if SYNCLINK_GENERIC_HDLC
7572 if (info->netcount)
7573 hdlcdev_tx_done(info);
7574 else
7575#endif
7576 mgsl_bh_transmit(info);
7577
7578} /* end of mgsl_tx_timeout() */
7579
7580/* signal that there are no more frames to send, so that
7581 * line is 'released' by echoing RxD to TxD when current
7582 * transmission is complete (or immediately if no tx in progress).
7583 */
7584static int mgsl_loopmode_send_done( struct mgsl_struct * info )
7585{
7586 unsigned long flags;
7587
7588 spin_lock_irqsave(&info->irq_spinlock,flags);
7589 if (info->params.flags & HDLC_FLAG_HDLC_LOOPMODE) {
7590 if (info->tx_active)
7591 info->loopmode_send_done_requested = true;
7592 else
7593 usc_loopmode_send_done(info);
7594 }
7595 spin_unlock_irqrestore(&info->irq_spinlock,flags);
7596
7597 return 0;
7598}
7599
7600/* release the line by echoing RxD to TxD
7601 * upon completion of a transmit frame
7602 */
7603static void usc_loopmode_send_done( struct mgsl_struct * info )
7604{
7605 info->loopmode_send_done_requested = false;
7606 /* clear CMR:13 to 0 to start echoing RxData to TxData */
7607 info->cmr_value &= ~BIT13;
7608 usc_OutReg(info, CMR, info->cmr_value);
7609}
7610
7611/* abort a transmit in progress while in HDLC LoopMode
7612 */
7613static void usc_loopmode_cancel_transmit( struct mgsl_struct * info )
7614{
7615 /* reset tx dma channel and purge TxFifo */
7616 usc_RTCmd( info, RTCmd_PurgeTxFifo );
7617 usc_DmaCmd( info, DmaCmd_ResetTxChannel );
7618 usc_loopmode_send_done( info );
7619}
7620
7621/* for HDLC/SDLC LoopMode, setting CMR:13 after the transmitter is enabled
7622 * is an Insert Into Loop action. Upon receipt of a GoAhead sequence (RxAbort)
7623 * we must clear CMR:13 to begin repeating TxData to RxData
7624 */
7625static void usc_loopmode_insert_request( struct mgsl_struct * info )
7626{
7627 info->loopmode_insert_requested = true;
7628
7629 /* enable RxAbort irq. On next RxAbort, clear CMR:13 to
7630 * begin repeating TxData on RxData (complete insertion)
7631 */
7632 usc_OutReg( info, RICR,
7633 (usc_InReg( info, RICR ) | RXSTATUS_ABORT_RECEIVED ) );
7634
7635 /* set CMR:13 to insert into loop on next GoAhead (RxAbort) */
7636 info->cmr_value |= BIT13;
7637 usc_OutReg(info, CMR, info->cmr_value);
7638}
7639
7640/* return 1 if station is inserted into the loop, otherwise 0
7641 */
7642static int usc_loopmode_active( struct mgsl_struct * info)
7643{
7644 return usc_InReg( info, CCSR ) & BIT7 ? 1 : 0 ;
7645}
7646
7647#if SYNCLINK_GENERIC_HDLC
7648
7649/**
7650 * called by generic HDLC layer when protocol selected (PPP, frame relay, etc.)
7651 * set encoding and frame check sequence (FCS) options
7652 *
7653 * dev pointer to network device structure
7654 * encoding serial encoding setting
7655 * parity FCS setting
7656 *
7657 * returns 0 if success, otherwise error code
7658 */
7659static int hdlcdev_attach(struct net_device *dev, unsigned short encoding,
7660 unsigned short parity)
7661{
7662 struct mgsl_struct *info = dev_to_port(dev);
7663 unsigned char new_encoding;
7664 unsigned short new_crctype;
7665
7666 /* return error if TTY interface open */
7667 if (info->port.count)
7668 return -EBUSY;
7669
7670 switch (encoding)
7671 {
7672 case ENCODING_NRZ: new_encoding = HDLC_ENCODING_NRZ; break;
7673 case ENCODING_NRZI: new_encoding = HDLC_ENCODING_NRZI_SPACE; break;
7674 case ENCODING_FM_MARK: new_encoding = HDLC_ENCODING_BIPHASE_MARK; break;
7675 case ENCODING_FM_SPACE: new_encoding = HDLC_ENCODING_BIPHASE_SPACE; break;
7676 case ENCODING_MANCHESTER: new_encoding = HDLC_ENCODING_BIPHASE_LEVEL; break;
7677 default: return -EINVAL;
7678 }
7679
7680 switch (parity)
7681 {
7682 case PARITY_NONE: new_crctype = HDLC_CRC_NONE; break;
7683 case PARITY_CRC16_PR1_CCITT: new_crctype = HDLC_CRC_16_CCITT; break;
7684 case PARITY_CRC32_PR1_CCITT: new_crctype = HDLC_CRC_32_CCITT; break;
7685 default: return -EINVAL;
7686 }
7687
7688 info->params.encoding = new_encoding;
7689 info->params.crc_type = new_crctype;
7690
7691 /* if network interface up, reprogram hardware */
7692 if (info->netcount)
7693 mgsl_program_hw(info);
7694
7695 return 0;
7696}
7697
7698/**
7699 * called by generic HDLC layer to send frame
7700 *
7701 * skb socket buffer containing HDLC frame
7702 * dev pointer to network device structure
7703 */
7704static netdev_tx_t hdlcdev_xmit(struct sk_buff *skb,
7705 struct net_device *dev)
7706{
7707 struct mgsl_struct *info = dev_to_port(dev);
7708 unsigned long flags;
7709
7710 if (debug_level >= DEBUG_LEVEL_INFO)
7711 printk(KERN_INFO "%s:hdlc_xmit(%s)\n",__FILE__,dev->name);
7712
7713 /* stop sending until this frame completes */
7714 netif_stop_queue(dev);
7715
7716 /* copy data to device buffers */
7717 info->xmit_cnt = skb->len;
7718 mgsl_load_tx_dma_buffer(info, skb->data, skb->len);
7719
7720 /* update network statistics */
7721 dev->stats.tx_packets++;
7722 dev->stats.tx_bytes += skb->len;
7723
7724 /* done with socket buffer, so free it */
7725 dev_kfree_skb(skb);
7726
7727 /* save start time for transmit timeout detection */
7728 dev->trans_start = jiffies;
7729
7730 /* start hardware transmitter if necessary */
7731 spin_lock_irqsave(&info->irq_spinlock,flags);
7732 if (!info->tx_active)
7733 usc_start_transmitter(info);
7734 spin_unlock_irqrestore(&info->irq_spinlock,flags);
7735
7736 return NETDEV_TX_OK;
7737}
7738
7739/**
7740 * called by network layer when interface enabled
7741 * claim resources and initialize hardware
7742 *
7743 * dev pointer to network device structure
7744 *
7745 * returns 0 if success, otherwise error code
7746 */
7747static int hdlcdev_open(struct net_device *dev)
7748{
7749 struct mgsl_struct *info = dev_to_port(dev);
7750 int rc;
7751 unsigned long flags;
7752
7753 if (debug_level >= DEBUG_LEVEL_INFO)
7754 printk("%s:hdlcdev_open(%s)\n",__FILE__,dev->name);
7755
7756 /* generic HDLC layer open processing */
7757 if ((rc = hdlc_open(dev)))
7758 return rc;
7759
7760 /* arbitrate between network and tty opens */
7761 spin_lock_irqsave(&info->netlock, flags);
7762 if (info->port.count != 0 || info->netcount != 0) {
7763 printk(KERN_WARNING "%s: hdlc_open returning busy\n", dev->name);
7764 spin_unlock_irqrestore(&info->netlock, flags);
7765 return -EBUSY;
7766 }
7767 info->netcount=1;
7768 spin_unlock_irqrestore(&info->netlock, flags);
7769
7770 /* claim resources and init adapter */
7771 if ((rc = startup(info)) != 0) {
7772 spin_lock_irqsave(&info->netlock, flags);
7773 info->netcount=0;
7774 spin_unlock_irqrestore(&info->netlock, flags);
7775 return rc;
7776 }
7777
7778 /* assert DTR and RTS, apply hardware settings */
7779 info->serial_signals |= SerialSignal_RTS + SerialSignal_DTR;
7780 mgsl_program_hw(info);
7781
7782 /* enable network layer transmit */
7783 dev->trans_start = jiffies;
7784 netif_start_queue(dev);
7785
7786 /* inform generic HDLC layer of current DCD status */
7787 spin_lock_irqsave(&info->irq_spinlock, flags);
7788 usc_get_serial_signals(info);
7789 spin_unlock_irqrestore(&info->irq_spinlock, flags);
7790 if (info->serial_signals & SerialSignal_DCD)
7791 netif_carrier_on(dev);
7792 else
7793 netif_carrier_off(dev);
7794 return 0;
7795}
7796
7797/**
7798 * called by network layer when interface is disabled
7799 * shutdown hardware and release resources
7800 *
7801 * dev pointer to network device structure
7802 *
7803 * returns 0 if success, otherwise error code
7804 */
7805static int hdlcdev_close(struct net_device *dev)
7806{
7807 struct mgsl_struct *info = dev_to_port(dev);
7808 unsigned long flags;
7809
7810 if (debug_level >= DEBUG_LEVEL_INFO)
7811 printk("%s:hdlcdev_close(%s)\n",__FILE__,dev->name);
7812
7813 netif_stop_queue(dev);
7814
7815 /* shutdown adapter and release resources */
7816 shutdown(info);
7817
7818 hdlc_close(dev);
7819
7820 spin_lock_irqsave(&info->netlock, flags);
7821 info->netcount=0;
7822 spin_unlock_irqrestore(&info->netlock, flags);
7823
7824 return 0;
7825}
7826
7827/**
7828 * called by network layer to process IOCTL call to network device
7829 *
7830 * dev pointer to network device structure
7831 * ifr pointer to network interface request structure
7832 * cmd IOCTL command code
7833 *
7834 * returns 0 if success, otherwise error code
7835 */
7836static int hdlcdev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
7837{
7838 const size_t size = sizeof(sync_serial_settings);
7839 sync_serial_settings new_line;
7840 sync_serial_settings __user *line = ifr->ifr_settings.ifs_ifsu.sync;
7841 struct mgsl_struct *info = dev_to_port(dev);
7842 unsigned int flags;
7843
7844 if (debug_level >= DEBUG_LEVEL_INFO)
7845 printk("%s:hdlcdev_ioctl(%s)\n",__FILE__,dev->name);
7846
7847 /* return error if TTY interface open */
7848 if (info->port.count)
7849 return -EBUSY;
7850
7851 if (cmd != SIOCWANDEV)
7852 return hdlc_ioctl(dev, ifr, cmd);
7853
7854 switch(ifr->ifr_settings.type) {
7855 case IF_GET_IFACE: /* return current sync_serial_settings */
7856
7857 ifr->ifr_settings.type = IF_IFACE_SYNC_SERIAL;
7858 if (ifr->ifr_settings.size < size) {
7859 ifr->ifr_settings.size = size; /* data size wanted */
7860 return -ENOBUFS;
7861 }
7862
7863 flags = info->params.flags & (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
7864 HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
7865 HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
7866 HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN);
7867
7868 switch (flags){
7869 case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_TXCPIN): new_line.clock_type = CLOCK_EXT; break;
7870 case (HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG): new_line.clock_type = CLOCK_INT; break;
7871 case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_BRG): new_line.clock_type = CLOCK_TXINT; break;
7872 case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_RXCPIN): new_line.clock_type = CLOCK_TXFROMRX; break;
7873 default: new_line.clock_type = CLOCK_DEFAULT;
7874 }
7875
7876 new_line.clock_rate = info->params.clock_speed;
7877 new_line.loopback = info->params.loopback ? 1:0;
7878
7879 if (copy_to_user(line, &new_line, size))
7880 return -EFAULT;
7881 return 0;
7882
7883 case IF_IFACE_SYNC_SERIAL: /* set sync_serial_settings */
7884
7885 if(!capable(CAP_NET_ADMIN))
7886 return -EPERM;
7887 if (copy_from_user(&new_line, line, size))
7888 return -EFAULT;
7889
7890 switch (new_line.clock_type)
7891 {
7892 case CLOCK_EXT: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_TXCPIN; break;
7893 case CLOCK_TXFROMRX: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_RXCPIN; break;
7894 case CLOCK_INT: flags = HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG; break;
7895 case CLOCK_TXINT: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_BRG; break;
7896 case CLOCK_DEFAULT: flags = info->params.flags &
7897 (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
7898 HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
7899 HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
7900 HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN); break;
7901 default: return -EINVAL;
7902 }
7903
7904 if (new_line.loopback != 0 && new_line.loopback != 1)
7905 return -EINVAL;
7906
7907 info->params.flags &= ~(HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
7908 HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
7909 HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
7910 HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN);
7911 info->params.flags |= flags;
7912
7913 info->params.loopback = new_line.loopback;
7914
7915 if (flags & (HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG))
7916 info->params.clock_speed = new_line.clock_rate;
7917 else
7918 info->params.clock_speed = 0;
7919
7920 /* if network interface up, reprogram hardware */
7921 if (info->netcount)
7922 mgsl_program_hw(info);
7923 return 0;
7924
7925 default:
7926 return hdlc_ioctl(dev, ifr, cmd);
7927 }
7928}
7929
7930/**
7931 * called by network layer when transmit timeout is detected
7932 *
7933 * dev pointer to network device structure
7934 */
7935static void hdlcdev_tx_timeout(struct net_device *dev)
7936{
7937 struct mgsl_struct *info = dev_to_port(dev);
7938 unsigned long flags;
7939
7940 if (debug_level >= DEBUG_LEVEL_INFO)
7941 printk("hdlcdev_tx_timeout(%s)\n",dev->name);
7942
7943 dev->stats.tx_errors++;
7944 dev->stats.tx_aborted_errors++;
7945
7946 spin_lock_irqsave(&info->irq_spinlock,flags);
7947 usc_stop_transmitter(info);
7948 spin_unlock_irqrestore(&info->irq_spinlock,flags);
7949
7950 netif_wake_queue(dev);
7951}
7952
7953/**
7954 * called by device driver when transmit completes
7955 * reenable network layer transmit if stopped
7956 *
7957 * info pointer to device instance information
7958 */
7959static void hdlcdev_tx_done(struct mgsl_struct *info)
7960{
7961 if (netif_queue_stopped(info->netdev))
7962 netif_wake_queue(info->netdev);
7963}
7964
7965/**
7966 * called by device driver when frame received
7967 * pass frame to network layer
7968 *
7969 * info pointer to device instance information
7970 * buf pointer to buffer contianing frame data
7971 * size count of data bytes in buf
7972 */
7973static void hdlcdev_rx(struct mgsl_struct *info, char *buf, int size)
7974{
7975 struct sk_buff *skb = dev_alloc_skb(size);
7976 struct net_device *dev = info->netdev;
7977
7978 if (debug_level >= DEBUG_LEVEL_INFO)
7979 printk("hdlcdev_rx(%s)\n", dev->name);
7980
7981 if (skb == NULL) {
7982 printk(KERN_NOTICE "%s: can't alloc skb, dropping packet\n",
7983 dev->name);
7984 dev->stats.rx_dropped++;
7985 return;
7986 }
7987
7988 memcpy(skb_put(skb, size), buf, size);
7989
7990 skb->protocol = hdlc_type_trans(skb, dev);
7991
7992 dev->stats.rx_packets++;
7993 dev->stats.rx_bytes += size;
7994
7995 netif_rx(skb);
7996}
7997
7998static const struct net_device_ops hdlcdev_ops = {
7999 .ndo_open = hdlcdev_open,
8000 .ndo_stop = hdlcdev_close,
8001 .ndo_change_mtu = hdlc_change_mtu,
8002 .ndo_start_xmit = hdlc_start_xmit,
8003 .ndo_do_ioctl = hdlcdev_ioctl,
8004 .ndo_tx_timeout = hdlcdev_tx_timeout,
8005};
8006
8007/**
8008 * called by device driver when adding device instance
8009 * do generic HDLC initialization
8010 *
8011 * info pointer to device instance information
8012 *
8013 * returns 0 if success, otherwise error code
8014 */
8015static int hdlcdev_init(struct mgsl_struct *info)
8016{
8017 int rc;
8018 struct net_device *dev;
8019 hdlc_device *hdlc;
8020
8021 /* allocate and initialize network and HDLC layer objects */
8022
8023 if (!(dev = alloc_hdlcdev(info))) {
8024 printk(KERN_ERR "%s:hdlc device allocation failure\n",__FILE__);
8025 return -ENOMEM;
8026 }
8027
8028 /* for network layer reporting purposes only */
8029 dev->base_addr = info->io_base;
8030 dev->irq = info->irq_level;
8031 dev->dma = info->dma_level;
8032
8033 /* network layer callbacks and settings */
8034 dev->netdev_ops = &hdlcdev_ops;
8035 dev->watchdog_timeo = 10 * HZ;
8036 dev->tx_queue_len = 50;
8037
8038 /* generic HDLC layer callbacks and settings */
8039 hdlc = dev_to_hdlc(dev);
8040 hdlc->attach = hdlcdev_attach;
8041 hdlc->xmit = hdlcdev_xmit;
8042
8043 /* register objects with HDLC layer */
8044 if ((rc = register_hdlc_device(dev))) {
8045 printk(KERN_WARNING "%s:unable to register hdlc device\n",__FILE__);
8046 free_netdev(dev);
8047 return rc;
8048 }
8049
8050 info->netdev = dev;
8051 return 0;
8052}
8053
8054/**
8055 * called by device driver when removing device instance
8056 * do generic HDLC cleanup
8057 *
8058 * info pointer to device instance information
8059 */
8060static void hdlcdev_exit(struct mgsl_struct *info)
8061{
8062 unregister_hdlc_device(info->netdev);
8063 free_netdev(info->netdev);
8064 info->netdev = NULL;
8065}
8066
8067#endif /* CONFIG_HDLC */
8068
8069
8070static int __devinit synclink_init_one (struct pci_dev *dev,
8071 const struct pci_device_id *ent)
8072{
8073 struct mgsl_struct *info;
8074
8075 if (pci_enable_device(dev)) {
8076 printk("error enabling pci device %p\n", dev);
8077 return -EIO;
8078 }
8079
8080 if (!(info = mgsl_allocate_device())) {
8081 printk("can't allocate device instance data.\n");
8082 return -EIO;
8083 }
8084
8085 /* Copy user configuration info to device instance data */
8086
8087 info->io_base = pci_resource_start(dev, 2);
8088 info->irq_level = dev->irq;
8089 info->phys_memory_base = pci_resource_start(dev, 3);
8090
8091 /* Because veremap only works on page boundaries we must map
8092 * a larger area than is actually implemented for the LCR
8093 * memory range. We map a full page starting at the page boundary.
8094 */
8095 info->phys_lcr_base = pci_resource_start(dev, 0);
8096 info->lcr_offset = info->phys_lcr_base & (PAGE_SIZE-1);
8097 info->phys_lcr_base &= ~(PAGE_SIZE-1);
8098
8099 info->bus_type = MGSL_BUS_TYPE_PCI;
8100 info->io_addr_size = 8;
8101 info->irq_flags = IRQF_SHARED;
8102
8103 if (dev->device == 0x0210) {
8104 /* Version 1 PCI9030 based universal PCI adapter */
8105 info->misc_ctrl_value = 0x007c4080;
8106 info->hw_version = 1;
8107 } else {
8108 /* Version 0 PCI9050 based 5V PCI adapter
8109 * A PCI9050 bug prevents reading LCR registers if
8110 * LCR base address bit 7 is set. Maintain shadow
8111 * value so we can write to LCR misc control reg.
8112 */
8113 info->misc_ctrl_value = 0x087e4546;
8114 info->hw_version = 0;
8115 }
8116
8117 mgsl_add_device(info);
8118
8119 return 0;
8120}
8121
8122static void __devexit synclink_remove_one (struct pci_dev *dev)
8123{
8124}
8125
diff --git a/drivers/char/synclink_gt.c b/drivers/char/synclink_gt.c
deleted file mode 100644
index e63b830c86cc..000000000000
--- a/drivers/char/synclink_gt.c
+++ /dev/null
@@ -1,5049 +0,0 @@
1/*
2 * Device driver for Microgate SyncLink GT serial adapters.
3 *
4 * written by Paul Fulghum for Microgate Corporation
5 * paulkf@microgate.com
6 *
7 * Microgate and SyncLink are trademarks of Microgate Corporation
8 *
9 * This code is released under the GNU General Public License (GPL)
10 *
11 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
12 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
13 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
14 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
15 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
16 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
17 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
18 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
19 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
20 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
21 * OF THE POSSIBILITY OF SUCH DAMAGE.
22 */
23
24/*
25 * DEBUG OUTPUT DEFINITIONS
26 *
27 * uncomment lines below to enable specific types of debug output
28 *
29 * DBGINFO information - most verbose output
30 * DBGERR serious errors
31 * DBGBH bottom half service routine debugging
32 * DBGISR interrupt service routine debugging
33 * DBGDATA output receive and transmit data
34 * DBGTBUF output transmit DMA buffers and registers
35 * DBGRBUF output receive DMA buffers and registers
36 */
37
38#define DBGINFO(fmt) if (debug_level >= DEBUG_LEVEL_INFO) printk fmt
39#define DBGERR(fmt) if (debug_level >= DEBUG_LEVEL_ERROR) printk fmt
40#define DBGBH(fmt) if (debug_level >= DEBUG_LEVEL_BH) printk fmt
41#define DBGISR(fmt) if (debug_level >= DEBUG_LEVEL_ISR) printk fmt
42#define DBGDATA(info, buf, size, label) if (debug_level >= DEBUG_LEVEL_DATA) trace_block((info), (buf), (size), (label))
43/*#define DBGTBUF(info) dump_tbufs(info)*/
44/*#define DBGRBUF(info) dump_rbufs(info)*/
45
46
47#include <linux/module.h>
48#include <linux/errno.h>
49#include <linux/signal.h>
50#include <linux/sched.h>
51#include <linux/timer.h>
52#include <linux/interrupt.h>
53#include <linux/pci.h>
54#include <linux/tty.h>
55#include <linux/tty_flip.h>
56#include <linux/serial.h>
57#include <linux/major.h>
58#include <linux/string.h>
59#include <linux/fcntl.h>
60#include <linux/ptrace.h>
61#include <linux/ioport.h>
62#include <linux/mm.h>
63#include <linux/seq_file.h>
64#include <linux/slab.h>
65#include <linux/netdevice.h>
66#include <linux/vmalloc.h>
67#include <linux/init.h>
68#include <linux/delay.h>
69#include <linux/ioctl.h>
70#include <linux/termios.h>
71#include <linux/bitops.h>
72#include <linux/workqueue.h>
73#include <linux/hdlc.h>
74#include <linux/synclink.h>
75
76#include <asm/system.h>
77#include <asm/io.h>
78#include <asm/irq.h>
79#include <asm/dma.h>
80#include <asm/types.h>
81#include <asm/uaccess.h>
82
83#if defined(CONFIG_HDLC) || (defined(CONFIG_HDLC_MODULE) && defined(CONFIG_SYNCLINK_GT_MODULE))
84#define SYNCLINK_GENERIC_HDLC 1
85#else
86#define SYNCLINK_GENERIC_HDLC 0
87#endif
88
89/*
90 * module identification
91 */
92static char *driver_name = "SyncLink GT";
93static char *tty_driver_name = "synclink_gt";
94static char *tty_dev_prefix = "ttySLG";
95MODULE_LICENSE("GPL");
96#define MGSL_MAGIC 0x5401
97#define MAX_DEVICES 32
98
99static struct pci_device_id pci_table[] = {
100 {PCI_VENDOR_ID_MICROGATE, SYNCLINK_GT_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
101 {PCI_VENDOR_ID_MICROGATE, SYNCLINK_GT2_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
102 {PCI_VENDOR_ID_MICROGATE, SYNCLINK_GT4_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
103 {PCI_VENDOR_ID_MICROGATE, SYNCLINK_AC_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
104 {0,}, /* terminate list */
105};
106MODULE_DEVICE_TABLE(pci, pci_table);
107
108static int init_one(struct pci_dev *dev,const struct pci_device_id *ent);
109static void remove_one(struct pci_dev *dev);
110static struct pci_driver pci_driver = {
111 .name = "synclink_gt",
112 .id_table = pci_table,
113 .probe = init_one,
114 .remove = __devexit_p(remove_one),
115};
116
117static bool pci_registered;
118
119/*
120 * module configuration and status
121 */
122static struct slgt_info *slgt_device_list;
123static int slgt_device_count;
124
125static int ttymajor;
126static int debug_level;
127static int maxframe[MAX_DEVICES];
128
129module_param(ttymajor, int, 0);
130module_param(debug_level, int, 0);
131module_param_array(maxframe, int, NULL, 0);
132
133MODULE_PARM_DESC(ttymajor, "TTY major device number override: 0=auto assigned");
134MODULE_PARM_DESC(debug_level, "Debug syslog output: 0=disabled, 1 to 5=increasing detail");
135MODULE_PARM_DESC(maxframe, "Maximum frame size used by device (4096 to 65535)");
136
137/*
138 * tty support and callbacks
139 */
140static struct tty_driver *serial_driver;
141
142static int open(struct tty_struct *tty, struct file * filp);
143static void close(struct tty_struct *tty, struct file * filp);
144static void hangup(struct tty_struct *tty);
145static void set_termios(struct tty_struct *tty, struct ktermios *old_termios);
146
147static int write(struct tty_struct *tty, const unsigned char *buf, int count);
148static int put_char(struct tty_struct *tty, unsigned char ch);
149static void send_xchar(struct tty_struct *tty, char ch);
150static void wait_until_sent(struct tty_struct *tty, int timeout);
151static int write_room(struct tty_struct *tty);
152static void flush_chars(struct tty_struct *tty);
153static void flush_buffer(struct tty_struct *tty);
154static void tx_hold(struct tty_struct *tty);
155static void tx_release(struct tty_struct *tty);
156
157static int ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg);
158static int chars_in_buffer(struct tty_struct *tty);
159static void throttle(struct tty_struct * tty);
160static void unthrottle(struct tty_struct * tty);
161static int set_break(struct tty_struct *tty, int break_state);
162
163/*
164 * generic HDLC support and callbacks
165 */
166#if SYNCLINK_GENERIC_HDLC
167#define dev_to_port(D) (dev_to_hdlc(D)->priv)
168static void hdlcdev_tx_done(struct slgt_info *info);
169static void hdlcdev_rx(struct slgt_info *info, char *buf, int size);
170static int hdlcdev_init(struct slgt_info *info);
171static void hdlcdev_exit(struct slgt_info *info);
172#endif
173
174
175/*
176 * device specific structures, macros and functions
177 */
178
179#define SLGT_MAX_PORTS 4
180#define SLGT_REG_SIZE 256
181
182/*
183 * conditional wait facility
184 */
185struct cond_wait {
186 struct cond_wait *next;
187 wait_queue_head_t q;
188 wait_queue_t wait;
189 unsigned int data;
190};
191static void init_cond_wait(struct cond_wait *w, unsigned int data);
192static void add_cond_wait(struct cond_wait **head, struct cond_wait *w);
193static void remove_cond_wait(struct cond_wait **head, struct cond_wait *w);
194static void flush_cond_wait(struct cond_wait **head);
195
196/*
197 * DMA buffer descriptor and access macros
198 */
199struct slgt_desc
200{
201 __le16 count;
202 __le16 status;
203 __le32 pbuf; /* physical address of data buffer */
204 __le32 next; /* physical address of next descriptor */
205
206 /* driver book keeping */
207 char *buf; /* virtual address of data buffer */
208 unsigned int pdesc; /* physical address of this descriptor */
209 dma_addr_t buf_dma_addr;
210 unsigned short buf_count;
211};
212
213#define set_desc_buffer(a,b) (a).pbuf = cpu_to_le32((unsigned int)(b))
214#define set_desc_next(a,b) (a).next = cpu_to_le32((unsigned int)(b))
215#define set_desc_count(a,b)(a).count = cpu_to_le16((unsigned short)(b))
216#define set_desc_eof(a,b) (a).status = cpu_to_le16((b) ? (le16_to_cpu((a).status) | BIT0) : (le16_to_cpu((a).status) & ~BIT0))
217#define set_desc_status(a, b) (a).status = cpu_to_le16((unsigned short)(b))
218#define desc_count(a) (le16_to_cpu((a).count))
219#define desc_status(a) (le16_to_cpu((a).status))
220#define desc_complete(a) (le16_to_cpu((a).status) & BIT15)
221#define desc_eof(a) (le16_to_cpu((a).status) & BIT2)
222#define desc_crc_error(a) (le16_to_cpu((a).status) & BIT1)
223#define desc_abort(a) (le16_to_cpu((a).status) & BIT0)
224#define desc_residue(a) ((le16_to_cpu((a).status) & 0x38) >> 3)
225
226struct _input_signal_events {
227 int ri_up;
228 int ri_down;
229 int dsr_up;
230 int dsr_down;
231 int dcd_up;
232 int dcd_down;
233 int cts_up;
234 int cts_down;
235};
236
237/*
238 * device instance data structure
239 */
240struct slgt_info {
241 void *if_ptr; /* General purpose pointer (used by SPPP) */
242 struct tty_port port;
243
244 struct slgt_info *next_device; /* device list link */
245
246 int magic;
247
248 char device_name[25];
249 struct pci_dev *pdev;
250
251 int port_count; /* count of ports on adapter */
252 int adapter_num; /* adapter instance number */
253 int port_num; /* port instance number */
254
255 /* array of pointers to port contexts on this adapter */
256 struct slgt_info *port_array[SLGT_MAX_PORTS];
257
258 int line; /* tty line instance number */
259
260 struct mgsl_icount icount;
261
262 int timeout;
263 int x_char; /* xon/xoff character */
264 unsigned int read_status_mask;
265 unsigned int ignore_status_mask;
266
267 wait_queue_head_t status_event_wait_q;
268 wait_queue_head_t event_wait_q;
269 struct timer_list tx_timer;
270 struct timer_list rx_timer;
271
272 unsigned int gpio_present;
273 struct cond_wait *gpio_wait_q;
274
275 spinlock_t lock; /* spinlock for synchronizing with ISR */
276
277 struct work_struct task;
278 u32 pending_bh;
279 bool bh_requested;
280 bool bh_running;
281
282 int isr_overflow;
283 bool irq_requested; /* true if IRQ requested */
284 bool irq_occurred; /* for diagnostics use */
285
286 /* device configuration */
287
288 unsigned int bus_type;
289 unsigned int irq_level;
290 unsigned long irq_flags;
291
292 unsigned char __iomem * reg_addr; /* memory mapped registers address */
293 u32 phys_reg_addr;
294 bool reg_addr_requested;
295
296 MGSL_PARAMS params; /* communications parameters */
297 u32 idle_mode;
298 u32 max_frame_size; /* as set by device config */
299
300 unsigned int rbuf_fill_level;
301 unsigned int rx_pio;
302 unsigned int if_mode;
303 unsigned int base_clock;
304
305 /* device status */
306
307 bool rx_enabled;
308 bool rx_restart;
309
310 bool tx_enabled;
311 bool tx_active;
312
313 unsigned char signals; /* serial signal states */
314 int init_error; /* initialization error */
315
316 unsigned char *tx_buf;
317 int tx_count;
318
319 char flag_buf[MAX_ASYNC_BUFFER_SIZE];
320 char char_buf[MAX_ASYNC_BUFFER_SIZE];
321 bool drop_rts_on_tx_done;
322 struct _input_signal_events input_signal_events;
323
324 int dcd_chkcount; /* check counts to prevent */
325 int cts_chkcount; /* too many IRQs if a signal */
326 int dsr_chkcount; /* is floating */
327 int ri_chkcount;
328
329 char *bufs; /* virtual address of DMA buffer lists */
330 dma_addr_t bufs_dma_addr; /* physical address of buffer descriptors */
331
332 unsigned int rbuf_count;
333 struct slgt_desc *rbufs;
334 unsigned int rbuf_current;
335 unsigned int rbuf_index;
336 unsigned int rbuf_fill_index;
337 unsigned short rbuf_fill_count;
338
339 unsigned int tbuf_count;
340 struct slgt_desc *tbufs;
341 unsigned int tbuf_current;
342 unsigned int tbuf_start;
343
344 unsigned char *tmp_rbuf;
345 unsigned int tmp_rbuf_count;
346
347 /* SPPP/Cisco HDLC device parts */
348
349 int netcount;
350 spinlock_t netlock;
351#if SYNCLINK_GENERIC_HDLC
352 struct net_device *netdev;
353#endif
354
355};
356
357static MGSL_PARAMS default_params = {
358 .mode = MGSL_MODE_HDLC,
359 .loopback = 0,
360 .flags = HDLC_FLAG_UNDERRUN_ABORT15,
361 .encoding = HDLC_ENCODING_NRZI_SPACE,
362 .clock_speed = 0,
363 .addr_filter = 0xff,
364 .crc_type = HDLC_CRC_16_CCITT,
365 .preamble_length = HDLC_PREAMBLE_LENGTH_8BITS,
366 .preamble = HDLC_PREAMBLE_PATTERN_NONE,
367 .data_rate = 9600,
368 .data_bits = 8,
369 .stop_bits = 1,
370 .parity = ASYNC_PARITY_NONE
371};
372
373
374#define BH_RECEIVE 1
375#define BH_TRANSMIT 2
376#define BH_STATUS 4
377#define IO_PIN_SHUTDOWN_LIMIT 100
378
379#define DMABUFSIZE 256
380#define DESC_LIST_SIZE 4096
381
382#define MASK_PARITY BIT1
383#define MASK_FRAMING BIT0
384#define MASK_BREAK BIT14
385#define MASK_OVERRUN BIT4
386
387#define GSR 0x00 /* global status */
388#define JCR 0x04 /* JTAG control */
389#define IODR 0x08 /* GPIO direction */
390#define IOER 0x0c /* GPIO interrupt enable */
391#define IOVR 0x10 /* GPIO value */
392#define IOSR 0x14 /* GPIO interrupt status */
393#define TDR 0x80 /* tx data */
394#define RDR 0x80 /* rx data */
395#define TCR 0x82 /* tx control */
396#define TIR 0x84 /* tx idle */
397#define TPR 0x85 /* tx preamble */
398#define RCR 0x86 /* rx control */
399#define VCR 0x88 /* V.24 control */
400#define CCR 0x89 /* clock control */
401#define BDR 0x8a /* baud divisor */
402#define SCR 0x8c /* serial control */
403#define SSR 0x8e /* serial status */
404#define RDCSR 0x90 /* rx DMA control/status */
405#define TDCSR 0x94 /* tx DMA control/status */
406#define RDDAR 0x98 /* rx DMA descriptor address */
407#define TDDAR 0x9c /* tx DMA descriptor address */
408
409#define RXIDLE BIT14
410#define RXBREAK BIT14
411#define IRQ_TXDATA BIT13
412#define IRQ_TXIDLE BIT12
413#define IRQ_TXUNDER BIT11 /* HDLC */
414#define IRQ_RXDATA BIT10
415#define IRQ_RXIDLE BIT9 /* HDLC */
416#define IRQ_RXBREAK BIT9 /* async */
417#define IRQ_RXOVER BIT8
418#define IRQ_DSR BIT7
419#define IRQ_CTS BIT6
420#define IRQ_DCD BIT5
421#define IRQ_RI BIT4
422#define IRQ_ALL 0x3ff0
423#define IRQ_MASTER BIT0
424
425#define slgt_irq_on(info, mask) \
426 wr_reg16((info), SCR, (unsigned short)(rd_reg16((info), SCR) | (mask)))
427#define slgt_irq_off(info, mask) \
428 wr_reg16((info), SCR, (unsigned short)(rd_reg16((info), SCR) & ~(mask)))
429
430static __u8 rd_reg8(struct slgt_info *info, unsigned int addr);
431static void wr_reg8(struct slgt_info *info, unsigned int addr, __u8 value);
432static __u16 rd_reg16(struct slgt_info *info, unsigned int addr);
433static void wr_reg16(struct slgt_info *info, unsigned int addr, __u16 value);
434static __u32 rd_reg32(struct slgt_info *info, unsigned int addr);
435static void wr_reg32(struct slgt_info *info, unsigned int addr, __u32 value);
436
437static void msc_set_vcr(struct slgt_info *info);
438
439static int startup(struct slgt_info *info);
440static int block_til_ready(struct tty_struct *tty, struct file * filp,struct slgt_info *info);
441static void shutdown(struct slgt_info *info);
442static void program_hw(struct slgt_info *info);
443static void change_params(struct slgt_info *info);
444
445static int register_test(struct slgt_info *info);
446static int irq_test(struct slgt_info *info);
447static int loopback_test(struct slgt_info *info);
448static int adapter_test(struct slgt_info *info);
449
450static void reset_adapter(struct slgt_info *info);
451static void reset_port(struct slgt_info *info);
452static void async_mode(struct slgt_info *info);
453static void sync_mode(struct slgt_info *info);
454
455static void rx_stop(struct slgt_info *info);
456static void rx_start(struct slgt_info *info);
457static void reset_rbufs(struct slgt_info *info);
458static void free_rbufs(struct slgt_info *info, unsigned int first, unsigned int last);
459static void rdma_reset(struct slgt_info *info);
460static bool rx_get_frame(struct slgt_info *info);
461static bool rx_get_buf(struct slgt_info *info);
462
463static void tx_start(struct slgt_info *info);
464static void tx_stop(struct slgt_info *info);
465static void tx_set_idle(struct slgt_info *info);
466static unsigned int free_tbuf_count(struct slgt_info *info);
467static unsigned int tbuf_bytes(struct slgt_info *info);
468static void reset_tbufs(struct slgt_info *info);
469static void tdma_reset(struct slgt_info *info);
470static bool tx_load(struct slgt_info *info, const char *buf, unsigned int count);
471
472static void get_signals(struct slgt_info *info);
473static void set_signals(struct slgt_info *info);
474static void enable_loopback(struct slgt_info *info);
475static void set_rate(struct slgt_info *info, u32 data_rate);
476
477static int bh_action(struct slgt_info *info);
478static void bh_handler(struct work_struct *work);
479static void bh_transmit(struct slgt_info *info);
480static void isr_serial(struct slgt_info *info);
481static void isr_rdma(struct slgt_info *info);
482static void isr_txeom(struct slgt_info *info, unsigned short status);
483static void isr_tdma(struct slgt_info *info);
484
485static int alloc_dma_bufs(struct slgt_info *info);
486static void free_dma_bufs(struct slgt_info *info);
487static int alloc_desc(struct slgt_info *info);
488static void free_desc(struct slgt_info *info);
489static int alloc_bufs(struct slgt_info *info, struct slgt_desc *bufs, int count);
490static void free_bufs(struct slgt_info *info, struct slgt_desc *bufs, int count);
491
492static int alloc_tmp_rbuf(struct slgt_info *info);
493static void free_tmp_rbuf(struct slgt_info *info);
494
495static void tx_timeout(unsigned long context);
496static void rx_timeout(unsigned long context);
497
498/*
499 * ioctl handlers
500 */
501static int get_stats(struct slgt_info *info, struct mgsl_icount __user *user_icount);
502static int get_params(struct slgt_info *info, MGSL_PARAMS __user *params);
503static int set_params(struct slgt_info *info, MGSL_PARAMS __user *params);
504static int get_txidle(struct slgt_info *info, int __user *idle_mode);
505static int set_txidle(struct slgt_info *info, int idle_mode);
506static int tx_enable(struct slgt_info *info, int enable);
507static int tx_abort(struct slgt_info *info);
508static int rx_enable(struct slgt_info *info, int enable);
509static int modem_input_wait(struct slgt_info *info,int arg);
510static int wait_mgsl_event(struct slgt_info *info, int __user *mask_ptr);
511static int tiocmget(struct tty_struct *tty, struct file *file);
512static int tiocmset(struct tty_struct *tty, struct file *file,
513 unsigned int set, unsigned int clear);
514static int set_break(struct tty_struct *tty, int break_state);
515static int get_interface(struct slgt_info *info, int __user *if_mode);
516static int set_interface(struct slgt_info *info, int if_mode);
517static int set_gpio(struct slgt_info *info, struct gpio_desc __user *gpio);
518static int get_gpio(struct slgt_info *info, struct gpio_desc __user *gpio);
519static int wait_gpio(struct slgt_info *info, struct gpio_desc __user *gpio);
520
521/*
522 * driver functions
523 */
524static void add_device(struct slgt_info *info);
525static void device_init(int adapter_num, struct pci_dev *pdev);
526static int claim_resources(struct slgt_info *info);
527static void release_resources(struct slgt_info *info);
528
529/*
530 * DEBUG OUTPUT CODE
531 */
532#ifndef DBGINFO
533#define DBGINFO(fmt)
534#endif
535#ifndef DBGERR
536#define DBGERR(fmt)
537#endif
538#ifndef DBGBH
539#define DBGBH(fmt)
540#endif
541#ifndef DBGISR
542#define DBGISR(fmt)
543#endif
544
545#ifdef DBGDATA
546static void trace_block(struct slgt_info *info, const char *data, int count, const char *label)
547{
548 int i;
549 int linecount;
550 printk("%s %s data:\n",info->device_name, label);
551 while(count) {
552 linecount = (count > 16) ? 16 : count;
553 for(i=0; i < linecount; i++)
554 printk("%02X ",(unsigned char)data[i]);
555 for(;i<17;i++)
556 printk(" ");
557 for(i=0;i<linecount;i++) {
558 if (data[i]>=040 && data[i]<=0176)
559 printk("%c",data[i]);
560 else
561 printk(".");
562 }
563 printk("\n");
564 data += linecount;
565 count -= linecount;
566 }
567}
568#else
569#define DBGDATA(info, buf, size, label)
570#endif
571
572#ifdef DBGTBUF
573static void dump_tbufs(struct slgt_info *info)
574{
575 int i;
576 printk("tbuf_current=%d\n", info->tbuf_current);
577 for (i=0 ; i < info->tbuf_count ; i++) {
578 printk("%d: count=%04X status=%04X\n",
579 i, le16_to_cpu(info->tbufs[i].count), le16_to_cpu(info->tbufs[i].status));
580 }
581}
582#else
583#define DBGTBUF(info)
584#endif
585
586#ifdef DBGRBUF
587static void dump_rbufs(struct slgt_info *info)
588{
589 int i;
590 printk("rbuf_current=%d\n", info->rbuf_current);
591 for (i=0 ; i < info->rbuf_count ; i++) {
592 printk("%d: count=%04X status=%04X\n",
593 i, le16_to_cpu(info->rbufs[i].count), le16_to_cpu(info->rbufs[i].status));
594 }
595}
596#else
597#define DBGRBUF(info)
598#endif
599
600static inline int sanity_check(struct slgt_info *info, char *devname, const char *name)
601{
602#ifdef SANITY_CHECK
603 if (!info) {
604 printk("null struct slgt_info for (%s) in %s\n", devname, name);
605 return 1;
606 }
607 if (info->magic != MGSL_MAGIC) {
608 printk("bad magic number struct slgt_info (%s) in %s\n", devname, name);
609 return 1;
610 }
611#else
612 if (!info)
613 return 1;
614#endif
615 return 0;
616}
617
618/**
619 * line discipline callback wrappers
620 *
621 * The wrappers maintain line discipline references
622 * while calling into the line discipline.
623 *
624 * ldisc_receive_buf - pass receive data to line discipline
625 */
626static void ldisc_receive_buf(struct tty_struct *tty,
627 const __u8 *data, char *flags, int count)
628{
629 struct tty_ldisc *ld;
630 if (!tty)
631 return;
632 ld = tty_ldisc_ref(tty);
633 if (ld) {
634 if (ld->ops->receive_buf)
635 ld->ops->receive_buf(tty, data, flags, count);
636 tty_ldisc_deref(ld);
637 }
638}
639
640/* tty callbacks */
641
642static int open(struct tty_struct *tty, struct file *filp)
643{
644 struct slgt_info *info;
645 int retval, line;
646 unsigned long flags;
647
648 line = tty->index;
649 if ((line < 0) || (line >= slgt_device_count)) {
650 DBGERR(("%s: open with invalid line #%d.\n", driver_name, line));
651 return -ENODEV;
652 }
653
654 info = slgt_device_list;
655 while(info && info->line != line)
656 info = info->next_device;
657 if (sanity_check(info, tty->name, "open"))
658 return -ENODEV;
659 if (info->init_error) {
660 DBGERR(("%s init error=%d\n", info->device_name, info->init_error));
661 return -ENODEV;
662 }
663
664 tty->driver_data = info;
665 info->port.tty = tty;
666
667 DBGINFO(("%s open, old ref count = %d\n", info->device_name, info->port.count));
668
669 /* If port is closing, signal caller to try again */
670 if (tty_hung_up_p(filp) || info->port.flags & ASYNC_CLOSING){
671 if (info->port.flags & ASYNC_CLOSING)
672 interruptible_sleep_on(&info->port.close_wait);
673 retval = ((info->port.flags & ASYNC_HUP_NOTIFY) ?
674 -EAGAIN : -ERESTARTSYS);
675 goto cleanup;
676 }
677
678 mutex_lock(&info->port.mutex);
679 info->port.tty->low_latency = (info->port.flags & ASYNC_LOW_LATENCY) ? 1 : 0;
680
681 spin_lock_irqsave(&info->netlock, flags);
682 if (info->netcount) {
683 retval = -EBUSY;
684 spin_unlock_irqrestore(&info->netlock, flags);
685 mutex_unlock(&info->port.mutex);
686 goto cleanup;
687 }
688 info->port.count++;
689 spin_unlock_irqrestore(&info->netlock, flags);
690
691 if (info->port.count == 1) {
692 /* 1st open on this device, init hardware */
693 retval = startup(info);
694 if (retval < 0) {
695 mutex_unlock(&info->port.mutex);
696 goto cleanup;
697 }
698 }
699 mutex_unlock(&info->port.mutex);
700 retval = block_til_ready(tty, filp, info);
701 if (retval) {
702 DBGINFO(("%s block_til_ready rc=%d\n", info->device_name, retval));
703 goto cleanup;
704 }
705
706 retval = 0;
707
708cleanup:
709 if (retval) {
710 if (tty->count == 1)
711 info->port.tty = NULL; /* tty layer will release tty struct */
712 if(info->port.count)
713 info->port.count--;
714 }
715
716 DBGINFO(("%s open rc=%d\n", info->device_name, retval));
717 return retval;
718}
719
720static void close(struct tty_struct *tty, struct file *filp)
721{
722 struct slgt_info *info = tty->driver_data;
723
724 if (sanity_check(info, tty->name, "close"))
725 return;
726 DBGINFO(("%s close entry, count=%d\n", info->device_name, info->port.count));
727
728 if (tty_port_close_start(&info->port, tty, filp) == 0)
729 goto cleanup;
730
731 mutex_lock(&info->port.mutex);
732 if (info->port.flags & ASYNC_INITIALIZED)
733 wait_until_sent(tty, info->timeout);
734 flush_buffer(tty);
735 tty_ldisc_flush(tty);
736
737 shutdown(info);
738 mutex_unlock(&info->port.mutex);
739
740 tty_port_close_end(&info->port, tty);
741 info->port.tty = NULL;
742cleanup:
743 DBGINFO(("%s close exit, count=%d\n", tty->driver->name, info->port.count));
744}
745
746static void hangup(struct tty_struct *tty)
747{
748 struct slgt_info *info = tty->driver_data;
749 unsigned long flags;
750
751 if (sanity_check(info, tty->name, "hangup"))
752 return;
753 DBGINFO(("%s hangup\n", info->device_name));
754
755 flush_buffer(tty);
756
757 mutex_lock(&info->port.mutex);
758 shutdown(info);
759
760 spin_lock_irqsave(&info->port.lock, flags);
761 info->port.count = 0;
762 info->port.flags &= ~ASYNC_NORMAL_ACTIVE;
763 info->port.tty = NULL;
764 spin_unlock_irqrestore(&info->port.lock, flags);
765 mutex_unlock(&info->port.mutex);
766
767 wake_up_interruptible(&info->port.open_wait);
768}
769
770static void set_termios(struct tty_struct *tty, struct ktermios *old_termios)
771{
772 struct slgt_info *info = tty->driver_data;
773 unsigned long flags;
774
775 DBGINFO(("%s set_termios\n", tty->driver->name));
776
777 change_params(info);
778
779 /* Handle transition to B0 status */
780 if (old_termios->c_cflag & CBAUD &&
781 !(tty->termios->c_cflag & CBAUD)) {
782 info->signals &= ~(SerialSignal_RTS + SerialSignal_DTR);
783 spin_lock_irqsave(&info->lock,flags);
784 set_signals(info);
785 spin_unlock_irqrestore(&info->lock,flags);
786 }
787
788 /* Handle transition away from B0 status */
789 if (!(old_termios->c_cflag & CBAUD) &&
790 tty->termios->c_cflag & CBAUD) {
791 info->signals |= SerialSignal_DTR;
792 if (!(tty->termios->c_cflag & CRTSCTS) ||
793 !test_bit(TTY_THROTTLED, &tty->flags)) {
794 info->signals |= SerialSignal_RTS;
795 }
796 spin_lock_irqsave(&info->lock,flags);
797 set_signals(info);
798 spin_unlock_irqrestore(&info->lock,flags);
799 }
800
801 /* Handle turning off CRTSCTS */
802 if (old_termios->c_cflag & CRTSCTS &&
803 !(tty->termios->c_cflag & CRTSCTS)) {
804 tty->hw_stopped = 0;
805 tx_release(tty);
806 }
807}
808
809static void update_tx_timer(struct slgt_info *info)
810{
811 /*
812 * use worst case speed of 1200bps to calculate transmit timeout
813 * based on data in buffers (tbuf_bytes) and FIFO (128 bytes)
814 */
815 if (info->params.mode == MGSL_MODE_HDLC) {
816 int timeout = (tbuf_bytes(info) * 7) + 1000;
817 mod_timer(&info->tx_timer, jiffies + msecs_to_jiffies(timeout));
818 }
819}
820
821static int write(struct tty_struct *tty,
822 const unsigned char *buf, int count)
823{
824 int ret = 0;
825 struct slgt_info *info = tty->driver_data;
826 unsigned long flags;
827
828 if (sanity_check(info, tty->name, "write"))
829 return -EIO;
830
831 DBGINFO(("%s write count=%d\n", info->device_name, count));
832
833 if (!info->tx_buf || (count > info->max_frame_size))
834 return -EIO;
835
836 if (!count || tty->stopped || tty->hw_stopped)
837 return 0;
838
839 spin_lock_irqsave(&info->lock, flags);
840
841 if (info->tx_count) {
842 /* send accumulated data from send_char() */
843 if (!tx_load(info, info->tx_buf, info->tx_count))
844 goto cleanup;
845 info->tx_count = 0;
846 }
847
848 if (tx_load(info, buf, count))
849 ret = count;
850
851cleanup:
852 spin_unlock_irqrestore(&info->lock, flags);
853 DBGINFO(("%s write rc=%d\n", info->device_name, ret));
854 return ret;
855}
856
857static int put_char(struct tty_struct *tty, unsigned char ch)
858{
859 struct slgt_info *info = tty->driver_data;
860 unsigned long flags;
861 int ret = 0;
862
863 if (sanity_check(info, tty->name, "put_char"))
864 return 0;
865 DBGINFO(("%s put_char(%d)\n", info->device_name, ch));
866 if (!info->tx_buf)
867 return 0;
868 spin_lock_irqsave(&info->lock,flags);
869 if (info->tx_count < info->max_frame_size) {
870 info->tx_buf[info->tx_count++] = ch;
871 ret = 1;
872 }
873 spin_unlock_irqrestore(&info->lock,flags);
874 return ret;
875}
876
877static void send_xchar(struct tty_struct *tty, char ch)
878{
879 struct slgt_info *info = tty->driver_data;
880 unsigned long flags;
881
882 if (sanity_check(info, tty->name, "send_xchar"))
883 return;
884 DBGINFO(("%s send_xchar(%d)\n", info->device_name, ch));
885 info->x_char = ch;
886 if (ch) {
887 spin_lock_irqsave(&info->lock,flags);
888 if (!info->tx_enabled)
889 tx_start(info);
890 spin_unlock_irqrestore(&info->lock,flags);
891 }
892}
893
894static void wait_until_sent(struct tty_struct *tty, int timeout)
895{
896 struct slgt_info *info = tty->driver_data;
897 unsigned long orig_jiffies, char_time;
898
899 if (!info )
900 return;
901 if (sanity_check(info, tty->name, "wait_until_sent"))
902 return;
903 DBGINFO(("%s wait_until_sent entry\n", info->device_name));
904 if (!(info->port.flags & ASYNC_INITIALIZED))
905 goto exit;
906
907 orig_jiffies = jiffies;
908
909 /* Set check interval to 1/5 of estimated time to
910 * send a character, and make it at least 1. The check
911 * interval should also be less than the timeout.
912 * Note: use tight timings here to satisfy the NIST-PCTS.
913 */
914
915 if (info->params.data_rate) {
916 char_time = info->timeout/(32 * 5);
917 if (!char_time)
918 char_time++;
919 } else
920 char_time = 1;
921
922 if (timeout)
923 char_time = min_t(unsigned long, char_time, timeout);
924
925 while (info->tx_active) {
926 msleep_interruptible(jiffies_to_msecs(char_time));
927 if (signal_pending(current))
928 break;
929 if (timeout && time_after(jiffies, orig_jiffies + timeout))
930 break;
931 }
932exit:
933 DBGINFO(("%s wait_until_sent exit\n", info->device_name));
934}
935
936static int write_room(struct tty_struct *tty)
937{
938 struct slgt_info *info = tty->driver_data;
939 int ret;
940
941 if (sanity_check(info, tty->name, "write_room"))
942 return 0;
943 ret = (info->tx_active) ? 0 : HDLC_MAX_FRAME_SIZE;
944 DBGINFO(("%s write_room=%d\n", info->device_name, ret));
945 return ret;
946}
947
948static void flush_chars(struct tty_struct *tty)
949{
950 struct slgt_info *info = tty->driver_data;
951 unsigned long flags;
952
953 if (sanity_check(info, tty->name, "flush_chars"))
954 return;
955 DBGINFO(("%s flush_chars entry tx_count=%d\n", info->device_name, info->tx_count));
956
957 if (info->tx_count <= 0 || tty->stopped ||
958 tty->hw_stopped || !info->tx_buf)
959 return;
960
961 DBGINFO(("%s flush_chars start transmit\n", info->device_name));
962
963 spin_lock_irqsave(&info->lock,flags);
964 if (info->tx_count && tx_load(info, info->tx_buf, info->tx_count))
965 info->tx_count = 0;
966 spin_unlock_irqrestore(&info->lock,flags);
967}
968
969static void flush_buffer(struct tty_struct *tty)
970{
971 struct slgt_info *info = tty->driver_data;
972 unsigned long flags;
973
974 if (sanity_check(info, tty->name, "flush_buffer"))
975 return;
976 DBGINFO(("%s flush_buffer\n", info->device_name));
977
978 spin_lock_irqsave(&info->lock, flags);
979 info->tx_count = 0;
980 spin_unlock_irqrestore(&info->lock, flags);
981
982 tty_wakeup(tty);
983}
984
985/*
986 * throttle (stop) transmitter
987 */
988static void tx_hold(struct tty_struct *tty)
989{
990 struct slgt_info *info = tty->driver_data;
991 unsigned long flags;
992
993 if (sanity_check(info, tty->name, "tx_hold"))
994 return;
995 DBGINFO(("%s tx_hold\n", info->device_name));
996 spin_lock_irqsave(&info->lock,flags);
997 if (info->tx_enabled && info->params.mode == MGSL_MODE_ASYNC)
998 tx_stop(info);
999 spin_unlock_irqrestore(&info->lock,flags);
1000}
1001
1002/*
1003 * release (start) transmitter
1004 */
1005static void tx_release(struct tty_struct *tty)
1006{
1007 struct slgt_info *info = tty->driver_data;
1008 unsigned long flags;
1009
1010 if (sanity_check(info, tty->name, "tx_release"))
1011 return;
1012 DBGINFO(("%s tx_release\n", info->device_name));
1013 spin_lock_irqsave(&info->lock, flags);
1014 if (info->tx_count && tx_load(info, info->tx_buf, info->tx_count))
1015 info->tx_count = 0;
1016 spin_unlock_irqrestore(&info->lock, flags);
1017}
1018
1019/*
1020 * Service an IOCTL request
1021 *
1022 * Arguments
1023 *
1024 * tty pointer to tty instance data
1025 * file pointer to associated file object for device
1026 * cmd IOCTL command code
1027 * arg command argument/context
1028 *
1029 * Return 0 if success, otherwise error code
1030 */
1031static int ioctl(struct tty_struct *tty, struct file *file,
1032 unsigned int cmd, unsigned long arg)
1033{
1034 struct slgt_info *info = tty->driver_data;
1035 struct mgsl_icount cnow; /* kernel counter temps */
1036 struct serial_icounter_struct __user *p_cuser; /* user space */
1037 unsigned long flags;
1038 void __user *argp = (void __user *)arg;
1039 int ret;
1040
1041 if (sanity_check(info, tty->name, "ioctl"))
1042 return -ENODEV;
1043 DBGINFO(("%s ioctl() cmd=%08X\n", info->device_name, cmd));
1044
1045 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1046 (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
1047 if (tty->flags & (1 << TTY_IO_ERROR))
1048 return -EIO;
1049 }
1050
1051 switch (cmd) {
1052 case MGSL_IOCWAITEVENT:
1053 return wait_mgsl_event(info, argp);
1054 case TIOCMIWAIT:
1055 return modem_input_wait(info,(int)arg);
1056 case TIOCGICOUNT:
1057 spin_lock_irqsave(&info->lock,flags);
1058 cnow = info->icount;
1059 spin_unlock_irqrestore(&info->lock,flags);
1060 p_cuser = argp;
1061 if (put_user(cnow.cts, &p_cuser->cts) ||
1062 put_user(cnow.dsr, &p_cuser->dsr) ||
1063 put_user(cnow.rng, &p_cuser->rng) ||
1064 put_user(cnow.dcd, &p_cuser->dcd) ||
1065 put_user(cnow.rx, &p_cuser->rx) ||
1066 put_user(cnow.tx, &p_cuser->tx) ||
1067 put_user(cnow.frame, &p_cuser->frame) ||
1068 put_user(cnow.overrun, &p_cuser->overrun) ||
1069 put_user(cnow.parity, &p_cuser->parity) ||
1070 put_user(cnow.brk, &p_cuser->brk) ||
1071 put_user(cnow.buf_overrun, &p_cuser->buf_overrun))
1072 return -EFAULT;
1073 return 0;
1074 case MGSL_IOCSGPIO:
1075 return set_gpio(info, argp);
1076 case MGSL_IOCGGPIO:
1077 return get_gpio(info, argp);
1078 case MGSL_IOCWAITGPIO:
1079 return wait_gpio(info, argp);
1080 }
1081 mutex_lock(&info->port.mutex);
1082 switch (cmd) {
1083 case MGSL_IOCGPARAMS:
1084 ret = get_params(info, argp);
1085 break;
1086 case MGSL_IOCSPARAMS:
1087 ret = set_params(info, argp);
1088 break;
1089 case MGSL_IOCGTXIDLE:
1090 ret = get_txidle(info, argp);
1091 break;
1092 case MGSL_IOCSTXIDLE:
1093 ret = set_txidle(info, (int)arg);
1094 break;
1095 case MGSL_IOCTXENABLE:
1096 ret = tx_enable(info, (int)arg);
1097 break;
1098 case MGSL_IOCRXENABLE:
1099 ret = rx_enable(info, (int)arg);
1100 break;
1101 case MGSL_IOCTXABORT:
1102 ret = tx_abort(info);
1103 break;
1104 case MGSL_IOCGSTATS:
1105 ret = get_stats(info, argp);
1106 break;
1107 case MGSL_IOCGIF:
1108 ret = get_interface(info, argp);
1109 break;
1110 case MGSL_IOCSIF:
1111 ret = set_interface(info,(int)arg);
1112 break;
1113 default:
1114 ret = -ENOIOCTLCMD;
1115 }
1116 mutex_unlock(&info->port.mutex);
1117 return ret;
1118}
1119
1120/*
1121 * support for 32 bit ioctl calls on 64 bit systems
1122 */
1123#ifdef CONFIG_COMPAT
1124static long get_params32(struct slgt_info *info, struct MGSL_PARAMS32 __user *user_params)
1125{
1126 struct MGSL_PARAMS32 tmp_params;
1127
1128 DBGINFO(("%s get_params32\n", info->device_name));
1129 tmp_params.mode = (compat_ulong_t)info->params.mode;
1130 tmp_params.loopback = info->params.loopback;
1131 tmp_params.flags = info->params.flags;
1132 tmp_params.encoding = info->params.encoding;
1133 tmp_params.clock_speed = (compat_ulong_t)info->params.clock_speed;
1134 tmp_params.addr_filter = info->params.addr_filter;
1135 tmp_params.crc_type = info->params.crc_type;
1136 tmp_params.preamble_length = info->params.preamble_length;
1137 tmp_params.preamble = info->params.preamble;
1138 tmp_params.data_rate = (compat_ulong_t)info->params.data_rate;
1139 tmp_params.data_bits = info->params.data_bits;
1140 tmp_params.stop_bits = info->params.stop_bits;
1141 tmp_params.parity = info->params.parity;
1142 if (copy_to_user(user_params, &tmp_params, sizeof(struct MGSL_PARAMS32)))
1143 return -EFAULT;
1144 return 0;
1145}
1146
1147static long set_params32(struct slgt_info *info, struct MGSL_PARAMS32 __user *new_params)
1148{
1149 struct MGSL_PARAMS32 tmp_params;
1150
1151 DBGINFO(("%s set_params32\n", info->device_name));
1152 if (copy_from_user(&tmp_params, new_params, sizeof(struct MGSL_PARAMS32)))
1153 return -EFAULT;
1154
1155 spin_lock(&info->lock);
1156 if (tmp_params.mode == MGSL_MODE_BASE_CLOCK) {
1157 info->base_clock = tmp_params.clock_speed;
1158 } else {
1159 info->params.mode = tmp_params.mode;
1160 info->params.loopback = tmp_params.loopback;
1161 info->params.flags = tmp_params.flags;
1162 info->params.encoding = tmp_params.encoding;
1163 info->params.clock_speed = tmp_params.clock_speed;
1164 info->params.addr_filter = tmp_params.addr_filter;
1165 info->params.crc_type = tmp_params.crc_type;
1166 info->params.preamble_length = tmp_params.preamble_length;
1167 info->params.preamble = tmp_params.preamble;
1168 info->params.data_rate = tmp_params.data_rate;
1169 info->params.data_bits = tmp_params.data_bits;
1170 info->params.stop_bits = tmp_params.stop_bits;
1171 info->params.parity = tmp_params.parity;
1172 }
1173 spin_unlock(&info->lock);
1174
1175 program_hw(info);
1176
1177 return 0;
1178}
1179
1180static long slgt_compat_ioctl(struct tty_struct *tty, struct file *file,
1181 unsigned int cmd, unsigned long arg)
1182{
1183 struct slgt_info *info = tty->driver_data;
1184 int rc = -ENOIOCTLCMD;
1185
1186 if (sanity_check(info, tty->name, "compat_ioctl"))
1187 return -ENODEV;
1188 DBGINFO(("%s compat_ioctl() cmd=%08X\n", info->device_name, cmd));
1189
1190 switch (cmd) {
1191
1192 case MGSL_IOCSPARAMS32:
1193 rc = set_params32(info, compat_ptr(arg));
1194 break;
1195
1196 case MGSL_IOCGPARAMS32:
1197 rc = get_params32(info, compat_ptr(arg));
1198 break;
1199
1200 case MGSL_IOCGPARAMS:
1201 case MGSL_IOCSPARAMS:
1202 case MGSL_IOCGTXIDLE:
1203 case MGSL_IOCGSTATS:
1204 case MGSL_IOCWAITEVENT:
1205 case MGSL_IOCGIF:
1206 case MGSL_IOCSGPIO:
1207 case MGSL_IOCGGPIO:
1208 case MGSL_IOCWAITGPIO:
1209 case TIOCGICOUNT:
1210 rc = ioctl(tty, file, cmd, (unsigned long)(compat_ptr(arg)));
1211 break;
1212
1213 case MGSL_IOCSTXIDLE:
1214 case MGSL_IOCTXENABLE:
1215 case MGSL_IOCRXENABLE:
1216 case MGSL_IOCTXABORT:
1217 case TIOCMIWAIT:
1218 case MGSL_IOCSIF:
1219 rc = ioctl(tty, file, cmd, arg);
1220 break;
1221 }
1222
1223 DBGINFO(("%s compat_ioctl() cmd=%08X rc=%d\n", info->device_name, cmd, rc));
1224 return rc;
1225}
1226#else
1227#define slgt_compat_ioctl NULL
1228#endif /* ifdef CONFIG_COMPAT */
1229
1230/*
1231 * proc fs support
1232 */
1233static inline void line_info(struct seq_file *m, struct slgt_info *info)
1234{
1235 char stat_buf[30];
1236 unsigned long flags;
1237
1238 seq_printf(m, "%s: IO=%08X IRQ=%d MaxFrameSize=%u\n",
1239 info->device_name, info->phys_reg_addr,
1240 info->irq_level, info->max_frame_size);
1241
1242 /* output current serial signal states */
1243 spin_lock_irqsave(&info->lock,flags);
1244 get_signals(info);
1245 spin_unlock_irqrestore(&info->lock,flags);
1246
1247 stat_buf[0] = 0;
1248 stat_buf[1] = 0;
1249 if (info->signals & SerialSignal_RTS)
1250 strcat(stat_buf, "|RTS");
1251 if (info->signals & SerialSignal_CTS)
1252 strcat(stat_buf, "|CTS");
1253 if (info->signals & SerialSignal_DTR)
1254 strcat(stat_buf, "|DTR");
1255 if (info->signals & SerialSignal_DSR)
1256 strcat(stat_buf, "|DSR");
1257 if (info->signals & SerialSignal_DCD)
1258 strcat(stat_buf, "|CD");
1259 if (info->signals & SerialSignal_RI)
1260 strcat(stat_buf, "|RI");
1261
1262 if (info->params.mode != MGSL_MODE_ASYNC) {
1263 seq_printf(m, "\tHDLC txok:%d rxok:%d",
1264 info->icount.txok, info->icount.rxok);
1265 if (info->icount.txunder)
1266 seq_printf(m, " txunder:%d", info->icount.txunder);
1267 if (info->icount.txabort)
1268 seq_printf(m, " txabort:%d", info->icount.txabort);
1269 if (info->icount.rxshort)
1270 seq_printf(m, " rxshort:%d", info->icount.rxshort);
1271 if (info->icount.rxlong)
1272 seq_printf(m, " rxlong:%d", info->icount.rxlong);
1273 if (info->icount.rxover)
1274 seq_printf(m, " rxover:%d", info->icount.rxover);
1275 if (info->icount.rxcrc)
1276 seq_printf(m, " rxcrc:%d", info->icount.rxcrc);
1277 } else {
1278 seq_printf(m, "\tASYNC tx:%d rx:%d",
1279 info->icount.tx, info->icount.rx);
1280 if (info->icount.frame)
1281 seq_printf(m, " fe:%d", info->icount.frame);
1282 if (info->icount.parity)
1283 seq_printf(m, " pe:%d", info->icount.parity);
1284 if (info->icount.brk)
1285 seq_printf(m, " brk:%d", info->icount.brk);
1286 if (info->icount.overrun)
1287 seq_printf(m, " oe:%d", info->icount.overrun);
1288 }
1289
1290 /* Append serial signal status to end */
1291 seq_printf(m, " %s\n", stat_buf+1);
1292
1293 seq_printf(m, "\ttxactive=%d bh_req=%d bh_run=%d pending_bh=%x\n",
1294 info->tx_active,info->bh_requested,info->bh_running,
1295 info->pending_bh);
1296}
1297
1298/* Called to print information about devices
1299 */
1300static int synclink_gt_proc_show(struct seq_file *m, void *v)
1301{
1302 struct slgt_info *info;
1303
1304 seq_puts(m, "synclink_gt driver\n");
1305
1306 info = slgt_device_list;
1307 while( info ) {
1308 line_info(m, info);
1309 info = info->next_device;
1310 }
1311 return 0;
1312}
1313
1314static int synclink_gt_proc_open(struct inode *inode, struct file *file)
1315{
1316 return single_open(file, synclink_gt_proc_show, NULL);
1317}
1318
1319static const struct file_operations synclink_gt_proc_fops = {
1320 .owner = THIS_MODULE,
1321 .open = synclink_gt_proc_open,
1322 .read = seq_read,
1323 .llseek = seq_lseek,
1324 .release = single_release,
1325};
1326
1327/*
1328 * return count of bytes in transmit buffer
1329 */
1330static int chars_in_buffer(struct tty_struct *tty)
1331{
1332 struct slgt_info *info = tty->driver_data;
1333 int count;
1334 if (sanity_check(info, tty->name, "chars_in_buffer"))
1335 return 0;
1336 count = tbuf_bytes(info);
1337 DBGINFO(("%s chars_in_buffer()=%d\n", info->device_name, count));
1338 return count;
1339}
1340
1341/*
1342 * signal remote device to throttle send data (our receive data)
1343 */
1344static void throttle(struct tty_struct * tty)
1345{
1346 struct slgt_info *info = tty->driver_data;
1347 unsigned long flags;
1348
1349 if (sanity_check(info, tty->name, "throttle"))
1350 return;
1351 DBGINFO(("%s throttle\n", info->device_name));
1352 if (I_IXOFF(tty))
1353 send_xchar(tty, STOP_CHAR(tty));
1354 if (tty->termios->c_cflag & CRTSCTS) {
1355 spin_lock_irqsave(&info->lock,flags);
1356 info->signals &= ~SerialSignal_RTS;
1357 set_signals(info);
1358 spin_unlock_irqrestore(&info->lock,flags);
1359 }
1360}
1361
1362/*
1363 * signal remote device to stop throttling send data (our receive data)
1364 */
1365static void unthrottle(struct tty_struct * tty)
1366{
1367 struct slgt_info *info = tty->driver_data;
1368 unsigned long flags;
1369
1370 if (sanity_check(info, tty->name, "unthrottle"))
1371 return;
1372 DBGINFO(("%s unthrottle\n", info->device_name));
1373 if (I_IXOFF(tty)) {
1374 if (info->x_char)
1375 info->x_char = 0;
1376 else
1377 send_xchar(tty, START_CHAR(tty));
1378 }
1379 if (tty->termios->c_cflag & CRTSCTS) {
1380 spin_lock_irqsave(&info->lock,flags);
1381 info->signals |= SerialSignal_RTS;
1382 set_signals(info);
1383 spin_unlock_irqrestore(&info->lock,flags);
1384 }
1385}
1386
1387/*
1388 * set or clear transmit break condition
1389 * break_state -1=set break condition, 0=clear
1390 */
1391static int set_break(struct tty_struct *tty, int break_state)
1392{
1393 struct slgt_info *info = tty->driver_data;
1394 unsigned short value;
1395 unsigned long flags;
1396
1397 if (sanity_check(info, tty->name, "set_break"))
1398 return -EINVAL;
1399 DBGINFO(("%s set_break(%d)\n", info->device_name, break_state));
1400
1401 spin_lock_irqsave(&info->lock,flags);
1402 value = rd_reg16(info, TCR);
1403 if (break_state == -1)
1404 value |= BIT6;
1405 else
1406 value &= ~BIT6;
1407 wr_reg16(info, TCR, value);
1408 spin_unlock_irqrestore(&info->lock,flags);
1409 return 0;
1410}
1411
1412#if SYNCLINK_GENERIC_HDLC
1413
1414/**
1415 * called by generic HDLC layer when protocol selected (PPP, frame relay, etc.)
1416 * set encoding and frame check sequence (FCS) options
1417 *
1418 * dev pointer to network device structure
1419 * encoding serial encoding setting
1420 * parity FCS setting
1421 *
1422 * returns 0 if success, otherwise error code
1423 */
1424static int hdlcdev_attach(struct net_device *dev, unsigned short encoding,
1425 unsigned short parity)
1426{
1427 struct slgt_info *info = dev_to_port(dev);
1428 unsigned char new_encoding;
1429 unsigned short new_crctype;
1430
1431 /* return error if TTY interface open */
1432 if (info->port.count)
1433 return -EBUSY;
1434
1435 DBGINFO(("%s hdlcdev_attach\n", info->device_name));
1436
1437 switch (encoding)
1438 {
1439 case ENCODING_NRZ: new_encoding = HDLC_ENCODING_NRZ; break;
1440 case ENCODING_NRZI: new_encoding = HDLC_ENCODING_NRZI_SPACE; break;
1441 case ENCODING_FM_MARK: new_encoding = HDLC_ENCODING_BIPHASE_MARK; break;
1442 case ENCODING_FM_SPACE: new_encoding = HDLC_ENCODING_BIPHASE_SPACE; break;
1443 case ENCODING_MANCHESTER: new_encoding = HDLC_ENCODING_BIPHASE_LEVEL; break;
1444 default: return -EINVAL;
1445 }
1446
1447 switch (parity)
1448 {
1449 case PARITY_NONE: new_crctype = HDLC_CRC_NONE; break;
1450 case PARITY_CRC16_PR1_CCITT: new_crctype = HDLC_CRC_16_CCITT; break;
1451 case PARITY_CRC32_PR1_CCITT: new_crctype = HDLC_CRC_32_CCITT; break;
1452 default: return -EINVAL;
1453 }
1454
1455 info->params.encoding = new_encoding;
1456 info->params.crc_type = new_crctype;
1457
1458 /* if network interface up, reprogram hardware */
1459 if (info->netcount)
1460 program_hw(info);
1461
1462 return 0;
1463}
1464
1465/**
1466 * called by generic HDLC layer to send frame
1467 *
1468 * skb socket buffer containing HDLC frame
1469 * dev pointer to network device structure
1470 */
1471static netdev_tx_t hdlcdev_xmit(struct sk_buff *skb,
1472 struct net_device *dev)
1473{
1474 struct slgt_info *info = dev_to_port(dev);
1475 unsigned long flags;
1476
1477 DBGINFO(("%s hdlc_xmit\n", dev->name));
1478
1479 if (!skb->len)
1480 return NETDEV_TX_OK;
1481
1482 /* stop sending until this frame completes */
1483 netif_stop_queue(dev);
1484
1485 /* update network statistics */
1486 dev->stats.tx_packets++;
1487 dev->stats.tx_bytes += skb->len;
1488
1489 /* save start time for transmit timeout detection */
1490 dev->trans_start = jiffies;
1491
1492 spin_lock_irqsave(&info->lock, flags);
1493 tx_load(info, skb->data, skb->len);
1494 spin_unlock_irqrestore(&info->lock, flags);
1495
1496 /* done with socket buffer, so free it */
1497 dev_kfree_skb(skb);
1498
1499 return NETDEV_TX_OK;
1500}
1501
1502/**
1503 * called by network layer when interface enabled
1504 * claim resources and initialize hardware
1505 *
1506 * dev pointer to network device structure
1507 *
1508 * returns 0 if success, otherwise error code
1509 */
1510static int hdlcdev_open(struct net_device *dev)
1511{
1512 struct slgt_info *info = dev_to_port(dev);
1513 int rc;
1514 unsigned long flags;
1515
1516 if (!try_module_get(THIS_MODULE))
1517 return -EBUSY;
1518
1519 DBGINFO(("%s hdlcdev_open\n", dev->name));
1520
1521 /* generic HDLC layer open processing */
1522 if ((rc = hdlc_open(dev)))
1523 return rc;
1524
1525 /* arbitrate between network and tty opens */
1526 spin_lock_irqsave(&info->netlock, flags);
1527 if (info->port.count != 0 || info->netcount != 0) {
1528 DBGINFO(("%s hdlc_open busy\n", dev->name));
1529 spin_unlock_irqrestore(&info->netlock, flags);
1530 return -EBUSY;
1531 }
1532 info->netcount=1;
1533 spin_unlock_irqrestore(&info->netlock, flags);
1534
1535 /* claim resources and init adapter */
1536 if ((rc = startup(info)) != 0) {
1537 spin_lock_irqsave(&info->netlock, flags);
1538 info->netcount=0;
1539 spin_unlock_irqrestore(&info->netlock, flags);
1540 return rc;
1541 }
1542
1543 /* assert DTR and RTS, apply hardware settings */
1544 info->signals |= SerialSignal_RTS + SerialSignal_DTR;
1545 program_hw(info);
1546
1547 /* enable network layer transmit */
1548 dev->trans_start = jiffies;
1549 netif_start_queue(dev);
1550
1551 /* inform generic HDLC layer of current DCD status */
1552 spin_lock_irqsave(&info->lock, flags);
1553 get_signals(info);
1554 spin_unlock_irqrestore(&info->lock, flags);
1555 if (info->signals & SerialSignal_DCD)
1556 netif_carrier_on(dev);
1557 else
1558 netif_carrier_off(dev);
1559 return 0;
1560}
1561
1562/**
1563 * called by network layer when interface is disabled
1564 * shutdown hardware and release resources
1565 *
1566 * dev pointer to network device structure
1567 *
1568 * returns 0 if success, otherwise error code
1569 */
1570static int hdlcdev_close(struct net_device *dev)
1571{
1572 struct slgt_info *info = dev_to_port(dev);
1573 unsigned long flags;
1574
1575 DBGINFO(("%s hdlcdev_close\n", dev->name));
1576
1577 netif_stop_queue(dev);
1578
1579 /* shutdown adapter and release resources */
1580 shutdown(info);
1581
1582 hdlc_close(dev);
1583
1584 spin_lock_irqsave(&info->netlock, flags);
1585 info->netcount=0;
1586 spin_unlock_irqrestore(&info->netlock, flags);
1587
1588 module_put(THIS_MODULE);
1589 return 0;
1590}
1591
1592/**
1593 * called by network layer to process IOCTL call to network device
1594 *
1595 * dev pointer to network device structure
1596 * ifr pointer to network interface request structure
1597 * cmd IOCTL command code
1598 *
1599 * returns 0 if success, otherwise error code
1600 */
1601static int hdlcdev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1602{
1603 const size_t size = sizeof(sync_serial_settings);
1604 sync_serial_settings new_line;
1605 sync_serial_settings __user *line = ifr->ifr_settings.ifs_ifsu.sync;
1606 struct slgt_info *info = dev_to_port(dev);
1607 unsigned int flags;
1608
1609 DBGINFO(("%s hdlcdev_ioctl\n", dev->name));
1610
1611 /* return error if TTY interface open */
1612 if (info->port.count)
1613 return -EBUSY;
1614
1615 if (cmd != SIOCWANDEV)
1616 return hdlc_ioctl(dev, ifr, cmd);
1617
1618 switch(ifr->ifr_settings.type) {
1619 case IF_GET_IFACE: /* return current sync_serial_settings */
1620
1621 ifr->ifr_settings.type = IF_IFACE_SYNC_SERIAL;
1622 if (ifr->ifr_settings.size < size) {
1623 ifr->ifr_settings.size = size; /* data size wanted */
1624 return -ENOBUFS;
1625 }
1626
1627 flags = info->params.flags & (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
1628 HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
1629 HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
1630 HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN);
1631
1632 switch (flags){
1633 case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_TXCPIN): new_line.clock_type = CLOCK_EXT; break;
1634 case (HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG): new_line.clock_type = CLOCK_INT; break;
1635 case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_BRG): new_line.clock_type = CLOCK_TXINT; break;
1636 case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_RXCPIN): new_line.clock_type = CLOCK_TXFROMRX; break;
1637 default: new_line.clock_type = CLOCK_DEFAULT;
1638 }
1639
1640 new_line.clock_rate = info->params.clock_speed;
1641 new_line.loopback = info->params.loopback ? 1:0;
1642
1643 if (copy_to_user(line, &new_line, size))
1644 return -EFAULT;
1645 return 0;
1646
1647 case IF_IFACE_SYNC_SERIAL: /* set sync_serial_settings */
1648
1649 if(!capable(CAP_NET_ADMIN))
1650 return -EPERM;
1651 if (copy_from_user(&new_line, line, size))
1652 return -EFAULT;
1653
1654 switch (new_line.clock_type)
1655 {
1656 case CLOCK_EXT: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_TXCPIN; break;
1657 case CLOCK_TXFROMRX: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_RXCPIN; break;
1658 case CLOCK_INT: flags = HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG; break;
1659 case CLOCK_TXINT: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_BRG; break;
1660 case CLOCK_DEFAULT: flags = info->params.flags &
1661 (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
1662 HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
1663 HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
1664 HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN); break;
1665 default: return -EINVAL;
1666 }
1667
1668 if (new_line.loopback != 0 && new_line.loopback != 1)
1669 return -EINVAL;
1670
1671 info->params.flags &= ~(HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
1672 HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
1673 HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
1674 HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN);
1675 info->params.flags |= flags;
1676
1677 info->params.loopback = new_line.loopback;
1678
1679 if (flags & (HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG))
1680 info->params.clock_speed = new_line.clock_rate;
1681 else
1682 info->params.clock_speed = 0;
1683
1684 /* if network interface up, reprogram hardware */
1685 if (info->netcount)
1686 program_hw(info);
1687 return 0;
1688
1689 default:
1690 return hdlc_ioctl(dev, ifr, cmd);
1691 }
1692}
1693
1694/**
1695 * called by network layer when transmit timeout is detected
1696 *
1697 * dev pointer to network device structure
1698 */
1699static void hdlcdev_tx_timeout(struct net_device *dev)
1700{
1701 struct slgt_info *info = dev_to_port(dev);
1702 unsigned long flags;
1703
1704 DBGINFO(("%s hdlcdev_tx_timeout\n", dev->name));
1705
1706 dev->stats.tx_errors++;
1707 dev->stats.tx_aborted_errors++;
1708
1709 spin_lock_irqsave(&info->lock,flags);
1710 tx_stop(info);
1711 spin_unlock_irqrestore(&info->lock,flags);
1712
1713 netif_wake_queue(dev);
1714}
1715
1716/**
1717 * called by device driver when transmit completes
1718 * reenable network layer transmit if stopped
1719 *
1720 * info pointer to device instance information
1721 */
1722static void hdlcdev_tx_done(struct slgt_info *info)
1723{
1724 if (netif_queue_stopped(info->netdev))
1725 netif_wake_queue(info->netdev);
1726}
1727
1728/**
1729 * called by device driver when frame received
1730 * pass frame to network layer
1731 *
1732 * info pointer to device instance information
1733 * buf pointer to buffer contianing frame data
1734 * size count of data bytes in buf
1735 */
1736static void hdlcdev_rx(struct slgt_info *info, char *buf, int size)
1737{
1738 struct sk_buff *skb = dev_alloc_skb(size);
1739 struct net_device *dev = info->netdev;
1740
1741 DBGINFO(("%s hdlcdev_rx\n", dev->name));
1742
1743 if (skb == NULL) {
1744 DBGERR(("%s: can't alloc skb, drop packet\n", dev->name));
1745 dev->stats.rx_dropped++;
1746 return;
1747 }
1748
1749 memcpy(skb_put(skb, size), buf, size);
1750
1751 skb->protocol = hdlc_type_trans(skb, dev);
1752
1753 dev->stats.rx_packets++;
1754 dev->stats.rx_bytes += size;
1755
1756 netif_rx(skb);
1757}
1758
1759static const struct net_device_ops hdlcdev_ops = {
1760 .ndo_open = hdlcdev_open,
1761 .ndo_stop = hdlcdev_close,
1762 .ndo_change_mtu = hdlc_change_mtu,
1763 .ndo_start_xmit = hdlc_start_xmit,
1764 .ndo_do_ioctl = hdlcdev_ioctl,
1765 .ndo_tx_timeout = hdlcdev_tx_timeout,
1766};
1767
1768/**
1769 * called by device driver when adding device instance
1770 * do generic HDLC initialization
1771 *
1772 * info pointer to device instance information
1773 *
1774 * returns 0 if success, otherwise error code
1775 */
1776static int hdlcdev_init(struct slgt_info *info)
1777{
1778 int rc;
1779 struct net_device *dev;
1780 hdlc_device *hdlc;
1781
1782 /* allocate and initialize network and HDLC layer objects */
1783
1784 if (!(dev = alloc_hdlcdev(info))) {
1785 printk(KERN_ERR "%s hdlc device alloc failure\n", info->device_name);
1786 return -ENOMEM;
1787 }
1788
1789 /* for network layer reporting purposes only */
1790 dev->mem_start = info->phys_reg_addr;
1791 dev->mem_end = info->phys_reg_addr + SLGT_REG_SIZE - 1;
1792 dev->irq = info->irq_level;
1793
1794 /* network layer callbacks and settings */
1795 dev->netdev_ops = &hdlcdev_ops;
1796 dev->watchdog_timeo = 10 * HZ;
1797 dev->tx_queue_len = 50;
1798
1799 /* generic HDLC layer callbacks and settings */
1800 hdlc = dev_to_hdlc(dev);
1801 hdlc->attach = hdlcdev_attach;
1802 hdlc->xmit = hdlcdev_xmit;
1803
1804 /* register objects with HDLC layer */
1805 if ((rc = register_hdlc_device(dev))) {
1806 printk(KERN_WARNING "%s:unable to register hdlc device\n",__FILE__);
1807 free_netdev(dev);
1808 return rc;
1809 }
1810
1811 info->netdev = dev;
1812 return 0;
1813}
1814
1815/**
1816 * called by device driver when removing device instance
1817 * do generic HDLC cleanup
1818 *
1819 * info pointer to device instance information
1820 */
1821static void hdlcdev_exit(struct slgt_info *info)
1822{
1823 unregister_hdlc_device(info->netdev);
1824 free_netdev(info->netdev);
1825 info->netdev = NULL;
1826}
1827
1828#endif /* ifdef CONFIG_HDLC */
1829
1830/*
1831 * get async data from rx DMA buffers
1832 */
1833static void rx_async(struct slgt_info *info)
1834{
1835 struct tty_struct *tty = info->port.tty;
1836 struct mgsl_icount *icount = &info->icount;
1837 unsigned int start, end;
1838 unsigned char *p;
1839 unsigned char status;
1840 struct slgt_desc *bufs = info->rbufs;
1841 int i, count;
1842 int chars = 0;
1843 int stat;
1844 unsigned char ch;
1845
1846 start = end = info->rbuf_current;
1847
1848 while(desc_complete(bufs[end])) {
1849 count = desc_count(bufs[end]) - info->rbuf_index;
1850 p = bufs[end].buf + info->rbuf_index;
1851
1852 DBGISR(("%s rx_async count=%d\n", info->device_name, count));
1853 DBGDATA(info, p, count, "rx");
1854
1855 for(i=0 ; i < count; i+=2, p+=2) {
1856 ch = *p;
1857 icount->rx++;
1858
1859 stat = 0;
1860
1861 if ((status = *(p+1) & (BIT1 + BIT0))) {
1862 if (status & BIT1)
1863 icount->parity++;
1864 else if (status & BIT0)
1865 icount->frame++;
1866 /* discard char if tty control flags say so */
1867 if (status & info->ignore_status_mask)
1868 continue;
1869 if (status & BIT1)
1870 stat = TTY_PARITY;
1871 else if (status & BIT0)
1872 stat = TTY_FRAME;
1873 }
1874 if (tty) {
1875 tty_insert_flip_char(tty, ch, stat);
1876 chars++;
1877 }
1878 }
1879
1880 if (i < count) {
1881 /* receive buffer not completed */
1882 info->rbuf_index += i;
1883 mod_timer(&info->rx_timer, jiffies + 1);
1884 break;
1885 }
1886
1887 info->rbuf_index = 0;
1888 free_rbufs(info, end, end);
1889
1890 if (++end == info->rbuf_count)
1891 end = 0;
1892
1893 /* if entire list searched then no frame available */
1894 if (end == start)
1895 break;
1896 }
1897
1898 if (tty && chars)
1899 tty_flip_buffer_push(tty);
1900}
1901
1902/*
1903 * return next bottom half action to perform
1904 */
1905static int bh_action(struct slgt_info *info)
1906{
1907 unsigned long flags;
1908 int rc;
1909
1910 spin_lock_irqsave(&info->lock,flags);
1911
1912 if (info->pending_bh & BH_RECEIVE) {
1913 info->pending_bh &= ~BH_RECEIVE;
1914 rc = BH_RECEIVE;
1915 } else if (info->pending_bh & BH_TRANSMIT) {
1916 info->pending_bh &= ~BH_TRANSMIT;
1917 rc = BH_TRANSMIT;
1918 } else if (info->pending_bh & BH_STATUS) {
1919 info->pending_bh &= ~BH_STATUS;
1920 rc = BH_STATUS;
1921 } else {
1922 /* Mark BH routine as complete */
1923 info->bh_running = false;
1924 info->bh_requested = false;
1925 rc = 0;
1926 }
1927
1928 spin_unlock_irqrestore(&info->lock,flags);
1929
1930 return rc;
1931}
1932
1933/*
1934 * perform bottom half processing
1935 */
1936static void bh_handler(struct work_struct *work)
1937{
1938 struct slgt_info *info = container_of(work, struct slgt_info, task);
1939 int action;
1940
1941 if (!info)
1942 return;
1943 info->bh_running = true;
1944
1945 while((action = bh_action(info))) {
1946 switch (action) {
1947 case BH_RECEIVE:
1948 DBGBH(("%s bh receive\n", info->device_name));
1949 switch(info->params.mode) {
1950 case MGSL_MODE_ASYNC:
1951 rx_async(info);
1952 break;
1953 case MGSL_MODE_HDLC:
1954 while(rx_get_frame(info));
1955 break;
1956 case MGSL_MODE_RAW:
1957 case MGSL_MODE_MONOSYNC:
1958 case MGSL_MODE_BISYNC:
1959 while(rx_get_buf(info));
1960 break;
1961 }
1962 /* restart receiver if rx DMA buffers exhausted */
1963 if (info->rx_restart)
1964 rx_start(info);
1965 break;
1966 case BH_TRANSMIT:
1967 bh_transmit(info);
1968 break;
1969 case BH_STATUS:
1970 DBGBH(("%s bh status\n", info->device_name));
1971 info->ri_chkcount = 0;
1972 info->dsr_chkcount = 0;
1973 info->dcd_chkcount = 0;
1974 info->cts_chkcount = 0;
1975 break;
1976 default:
1977 DBGBH(("%s unknown action\n", info->device_name));
1978 break;
1979 }
1980 }
1981 DBGBH(("%s bh_handler exit\n", info->device_name));
1982}
1983
1984static void bh_transmit(struct slgt_info *info)
1985{
1986 struct tty_struct *tty = info->port.tty;
1987
1988 DBGBH(("%s bh_transmit\n", info->device_name));
1989 if (tty)
1990 tty_wakeup(tty);
1991}
1992
1993static void dsr_change(struct slgt_info *info, unsigned short status)
1994{
1995 if (status & BIT3) {
1996 info->signals |= SerialSignal_DSR;
1997 info->input_signal_events.dsr_up++;
1998 } else {
1999 info->signals &= ~SerialSignal_DSR;
2000 info->input_signal_events.dsr_down++;
2001 }
2002 DBGISR(("dsr_change %s signals=%04X\n", info->device_name, info->signals));
2003 if ((info->dsr_chkcount)++ == IO_PIN_SHUTDOWN_LIMIT) {
2004 slgt_irq_off(info, IRQ_DSR);
2005 return;
2006 }
2007 info->icount.dsr++;
2008 wake_up_interruptible(&info->status_event_wait_q);
2009 wake_up_interruptible(&info->event_wait_q);
2010 info->pending_bh |= BH_STATUS;
2011}
2012
2013static void cts_change(struct slgt_info *info, unsigned short status)
2014{
2015 if (status & BIT2) {
2016 info->signals |= SerialSignal_CTS;
2017 info->input_signal_events.cts_up++;
2018 } else {
2019 info->signals &= ~SerialSignal_CTS;
2020 info->input_signal_events.cts_down++;
2021 }
2022 DBGISR(("cts_change %s signals=%04X\n", info->device_name, info->signals));
2023 if ((info->cts_chkcount)++ == IO_PIN_SHUTDOWN_LIMIT) {
2024 slgt_irq_off(info, IRQ_CTS);
2025 return;
2026 }
2027 info->icount.cts++;
2028 wake_up_interruptible(&info->status_event_wait_q);
2029 wake_up_interruptible(&info->event_wait_q);
2030 info->pending_bh |= BH_STATUS;
2031
2032 if (info->port.flags & ASYNC_CTS_FLOW) {
2033 if (info->port.tty) {
2034 if (info->port.tty->hw_stopped) {
2035 if (info->signals & SerialSignal_CTS) {
2036 info->port.tty->hw_stopped = 0;
2037 info->pending_bh |= BH_TRANSMIT;
2038 return;
2039 }
2040 } else {
2041 if (!(info->signals & SerialSignal_CTS))
2042 info->port.tty->hw_stopped = 1;
2043 }
2044 }
2045 }
2046}
2047
2048static void dcd_change(struct slgt_info *info, unsigned short status)
2049{
2050 if (status & BIT1) {
2051 info->signals |= SerialSignal_DCD;
2052 info->input_signal_events.dcd_up++;
2053 } else {
2054 info->signals &= ~SerialSignal_DCD;
2055 info->input_signal_events.dcd_down++;
2056 }
2057 DBGISR(("dcd_change %s signals=%04X\n", info->device_name, info->signals));
2058 if ((info->dcd_chkcount)++ == IO_PIN_SHUTDOWN_LIMIT) {
2059 slgt_irq_off(info, IRQ_DCD);
2060 return;
2061 }
2062 info->icount.dcd++;
2063#if SYNCLINK_GENERIC_HDLC
2064 if (info->netcount) {
2065 if (info->signals & SerialSignal_DCD)
2066 netif_carrier_on(info->netdev);
2067 else
2068 netif_carrier_off(info->netdev);
2069 }
2070#endif
2071 wake_up_interruptible(&info->status_event_wait_q);
2072 wake_up_interruptible(&info->event_wait_q);
2073 info->pending_bh |= BH_STATUS;
2074
2075 if (info->port.flags & ASYNC_CHECK_CD) {
2076 if (info->signals & SerialSignal_DCD)
2077 wake_up_interruptible(&info->port.open_wait);
2078 else {
2079 if (info->port.tty)
2080 tty_hangup(info->port.tty);
2081 }
2082 }
2083}
2084
2085static void ri_change(struct slgt_info *info, unsigned short status)
2086{
2087 if (status & BIT0) {
2088 info->signals |= SerialSignal_RI;
2089 info->input_signal_events.ri_up++;
2090 } else {
2091 info->signals &= ~SerialSignal_RI;
2092 info->input_signal_events.ri_down++;
2093 }
2094 DBGISR(("ri_change %s signals=%04X\n", info->device_name, info->signals));
2095 if ((info->ri_chkcount)++ == IO_PIN_SHUTDOWN_LIMIT) {
2096 slgt_irq_off(info, IRQ_RI);
2097 return;
2098 }
2099 info->icount.rng++;
2100 wake_up_interruptible(&info->status_event_wait_q);
2101 wake_up_interruptible(&info->event_wait_q);
2102 info->pending_bh |= BH_STATUS;
2103}
2104
2105static void isr_rxdata(struct slgt_info *info)
2106{
2107 unsigned int count = info->rbuf_fill_count;
2108 unsigned int i = info->rbuf_fill_index;
2109 unsigned short reg;
2110
2111 while (rd_reg16(info, SSR) & IRQ_RXDATA) {
2112 reg = rd_reg16(info, RDR);
2113 DBGISR(("isr_rxdata %s RDR=%04X\n", info->device_name, reg));
2114 if (desc_complete(info->rbufs[i])) {
2115 /* all buffers full */
2116 rx_stop(info);
2117 info->rx_restart = 1;
2118 continue;
2119 }
2120 info->rbufs[i].buf[count++] = (unsigned char)reg;
2121 /* async mode saves status byte to buffer for each data byte */
2122 if (info->params.mode == MGSL_MODE_ASYNC)
2123 info->rbufs[i].buf[count++] = (unsigned char)(reg >> 8);
2124 if (count == info->rbuf_fill_level || (reg & BIT10)) {
2125 /* buffer full or end of frame */
2126 set_desc_count(info->rbufs[i], count);
2127 set_desc_status(info->rbufs[i], BIT15 | (reg >> 8));
2128 info->rbuf_fill_count = count = 0;
2129 if (++i == info->rbuf_count)
2130 i = 0;
2131 info->pending_bh |= BH_RECEIVE;
2132 }
2133 }
2134
2135 info->rbuf_fill_index = i;
2136 info->rbuf_fill_count = count;
2137}
2138
2139static void isr_serial(struct slgt_info *info)
2140{
2141 unsigned short status = rd_reg16(info, SSR);
2142
2143 DBGISR(("%s isr_serial status=%04X\n", info->device_name, status));
2144
2145 wr_reg16(info, SSR, status); /* clear pending */
2146
2147 info->irq_occurred = true;
2148
2149 if (info->params.mode == MGSL_MODE_ASYNC) {
2150 if (status & IRQ_TXIDLE) {
2151 if (info->tx_active)
2152 isr_txeom(info, status);
2153 }
2154 if (info->rx_pio && (status & IRQ_RXDATA))
2155 isr_rxdata(info);
2156 if ((status & IRQ_RXBREAK) && (status & RXBREAK)) {
2157 info->icount.brk++;
2158 /* process break detection if tty control allows */
2159 if (info->port.tty) {
2160 if (!(status & info->ignore_status_mask)) {
2161 if (info->read_status_mask & MASK_BREAK) {
2162 tty_insert_flip_char(info->port.tty, 0, TTY_BREAK);
2163 if (info->port.flags & ASYNC_SAK)
2164 do_SAK(info->port.tty);
2165 }
2166 }
2167 }
2168 }
2169 } else {
2170 if (status & (IRQ_TXIDLE + IRQ_TXUNDER))
2171 isr_txeom(info, status);
2172 if (info->rx_pio && (status & IRQ_RXDATA))
2173 isr_rxdata(info);
2174 if (status & IRQ_RXIDLE) {
2175 if (status & RXIDLE)
2176 info->icount.rxidle++;
2177 else
2178 info->icount.exithunt++;
2179 wake_up_interruptible(&info->event_wait_q);
2180 }
2181
2182 if (status & IRQ_RXOVER)
2183 rx_start(info);
2184 }
2185
2186 if (status & IRQ_DSR)
2187 dsr_change(info, status);
2188 if (status & IRQ_CTS)
2189 cts_change(info, status);
2190 if (status & IRQ_DCD)
2191 dcd_change(info, status);
2192 if (status & IRQ_RI)
2193 ri_change(info, status);
2194}
2195
2196static void isr_rdma(struct slgt_info *info)
2197{
2198 unsigned int status = rd_reg32(info, RDCSR);
2199
2200 DBGISR(("%s isr_rdma status=%08x\n", info->device_name, status));
2201
2202 /* RDCSR (rx DMA control/status)
2203 *
2204 * 31..07 reserved
2205 * 06 save status byte to DMA buffer
2206 * 05 error
2207 * 04 eol (end of list)
2208 * 03 eob (end of buffer)
2209 * 02 IRQ enable
2210 * 01 reset
2211 * 00 enable
2212 */
2213 wr_reg32(info, RDCSR, status); /* clear pending */
2214
2215 if (status & (BIT5 + BIT4)) {
2216 DBGISR(("%s isr_rdma rx_restart=1\n", info->device_name));
2217 info->rx_restart = true;
2218 }
2219 info->pending_bh |= BH_RECEIVE;
2220}
2221
2222static void isr_tdma(struct slgt_info *info)
2223{
2224 unsigned int status = rd_reg32(info, TDCSR);
2225
2226 DBGISR(("%s isr_tdma status=%08x\n", info->device_name, status));
2227
2228 /* TDCSR (tx DMA control/status)
2229 *
2230 * 31..06 reserved
2231 * 05 error
2232 * 04 eol (end of list)
2233 * 03 eob (end of buffer)
2234 * 02 IRQ enable
2235 * 01 reset
2236 * 00 enable
2237 */
2238 wr_reg32(info, TDCSR, status); /* clear pending */
2239
2240 if (status & (BIT5 + BIT4 + BIT3)) {
2241 // another transmit buffer has completed
2242 // run bottom half to get more send data from user
2243 info->pending_bh |= BH_TRANSMIT;
2244 }
2245}
2246
2247/*
2248 * return true if there are unsent tx DMA buffers, otherwise false
2249 *
2250 * if there are unsent buffers then info->tbuf_start
2251 * is set to index of first unsent buffer
2252 */
2253static bool unsent_tbufs(struct slgt_info *info)
2254{
2255 unsigned int i = info->tbuf_current;
2256 bool rc = false;
2257
2258 /*
2259 * search backwards from last loaded buffer (precedes tbuf_current)
2260 * for first unsent buffer (desc_count > 0)
2261 */
2262
2263 do {
2264 if (i)
2265 i--;
2266 else
2267 i = info->tbuf_count - 1;
2268 if (!desc_count(info->tbufs[i]))
2269 break;
2270 info->tbuf_start = i;
2271 rc = true;
2272 } while (i != info->tbuf_current);
2273
2274 return rc;
2275}
2276
2277static void isr_txeom(struct slgt_info *info, unsigned short status)
2278{
2279 DBGISR(("%s txeom status=%04x\n", info->device_name, status));
2280
2281 slgt_irq_off(info, IRQ_TXDATA + IRQ_TXIDLE + IRQ_TXUNDER);
2282 tdma_reset(info);
2283 if (status & IRQ_TXUNDER) {
2284 unsigned short val = rd_reg16(info, TCR);
2285 wr_reg16(info, TCR, (unsigned short)(val | BIT2)); /* set reset bit */
2286 wr_reg16(info, TCR, val); /* clear reset bit */
2287 }
2288
2289 if (info->tx_active) {
2290 if (info->params.mode != MGSL_MODE_ASYNC) {
2291 if (status & IRQ_TXUNDER)
2292 info->icount.txunder++;
2293 else if (status & IRQ_TXIDLE)
2294 info->icount.txok++;
2295 }
2296
2297 if (unsent_tbufs(info)) {
2298 tx_start(info);
2299 update_tx_timer(info);
2300 return;
2301 }
2302 info->tx_active = false;
2303
2304 del_timer(&info->tx_timer);
2305
2306 if (info->params.mode != MGSL_MODE_ASYNC && info->drop_rts_on_tx_done) {
2307 info->signals &= ~SerialSignal_RTS;
2308 info->drop_rts_on_tx_done = false;
2309 set_signals(info);
2310 }
2311
2312#if SYNCLINK_GENERIC_HDLC
2313 if (info->netcount)
2314 hdlcdev_tx_done(info);
2315 else
2316#endif
2317 {
2318 if (info->port.tty && (info->port.tty->stopped || info->port.tty->hw_stopped)) {
2319 tx_stop(info);
2320 return;
2321 }
2322 info->pending_bh |= BH_TRANSMIT;
2323 }
2324 }
2325}
2326
2327static void isr_gpio(struct slgt_info *info, unsigned int changed, unsigned int state)
2328{
2329 struct cond_wait *w, *prev;
2330
2331 /* wake processes waiting for specific transitions */
2332 for (w = info->gpio_wait_q, prev = NULL ; w != NULL ; w = w->next) {
2333 if (w->data & changed) {
2334 w->data = state;
2335 wake_up_interruptible(&w->q);
2336 if (prev != NULL)
2337 prev->next = w->next;
2338 else
2339 info->gpio_wait_q = w->next;
2340 } else
2341 prev = w;
2342 }
2343}
2344
2345/* interrupt service routine
2346 *
2347 * irq interrupt number
2348 * dev_id device ID supplied during interrupt registration
2349 */
2350static irqreturn_t slgt_interrupt(int dummy, void *dev_id)
2351{
2352 struct slgt_info *info = dev_id;
2353 unsigned int gsr;
2354 unsigned int i;
2355
2356 DBGISR(("slgt_interrupt irq=%d entry\n", info->irq_level));
2357
2358 spin_lock(&info->lock);
2359
2360 while((gsr = rd_reg32(info, GSR) & 0xffffff00)) {
2361 DBGISR(("%s gsr=%08x\n", info->device_name, gsr));
2362 info->irq_occurred = true;
2363 for(i=0; i < info->port_count ; i++) {
2364 if (info->port_array[i] == NULL)
2365 continue;
2366 if (gsr & (BIT8 << i))
2367 isr_serial(info->port_array[i]);
2368 if (gsr & (BIT16 << (i*2)))
2369 isr_rdma(info->port_array[i]);
2370 if (gsr & (BIT17 << (i*2)))
2371 isr_tdma(info->port_array[i]);
2372 }
2373 }
2374
2375 if (info->gpio_present) {
2376 unsigned int state;
2377 unsigned int changed;
2378 while ((changed = rd_reg32(info, IOSR)) != 0) {
2379 DBGISR(("%s iosr=%08x\n", info->device_name, changed));
2380 /* read latched state of GPIO signals */
2381 state = rd_reg32(info, IOVR);
2382 /* clear pending GPIO interrupt bits */
2383 wr_reg32(info, IOSR, changed);
2384 for (i=0 ; i < info->port_count ; i++) {
2385 if (info->port_array[i] != NULL)
2386 isr_gpio(info->port_array[i], changed, state);
2387 }
2388 }
2389 }
2390
2391 for(i=0; i < info->port_count ; i++) {
2392 struct slgt_info *port = info->port_array[i];
2393
2394 if (port && (port->port.count || port->netcount) &&
2395 port->pending_bh && !port->bh_running &&
2396 !port->bh_requested) {
2397 DBGISR(("%s bh queued\n", port->device_name));
2398 schedule_work(&port->task);
2399 port->bh_requested = true;
2400 }
2401 }
2402
2403 spin_unlock(&info->lock);
2404
2405 DBGISR(("slgt_interrupt irq=%d exit\n", info->irq_level));
2406 return IRQ_HANDLED;
2407}
2408
2409static int startup(struct slgt_info *info)
2410{
2411 DBGINFO(("%s startup\n", info->device_name));
2412
2413 if (info->port.flags & ASYNC_INITIALIZED)
2414 return 0;
2415
2416 if (!info->tx_buf) {
2417 info->tx_buf = kmalloc(info->max_frame_size, GFP_KERNEL);
2418 if (!info->tx_buf) {
2419 DBGERR(("%s can't allocate tx buffer\n", info->device_name));
2420 return -ENOMEM;
2421 }
2422 }
2423
2424 info->pending_bh = 0;
2425
2426 memset(&info->icount, 0, sizeof(info->icount));
2427
2428 /* program hardware for current parameters */
2429 change_params(info);
2430
2431 if (info->port.tty)
2432 clear_bit(TTY_IO_ERROR, &info->port.tty->flags);
2433
2434 info->port.flags |= ASYNC_INITIALIZED;
2435
2436 return 0;
2437}
2438
2439/*
2440 * called by close() and hangup() to shutdown hardware
2441 */
2442static void shutdown(struct slgt_info *info)
2443{
2444 unsigned long flags;
2445
2446 if (!(info->port.flags & ASYNC_INITIALIZED))
2447 return;
2448
2449 DBGINFO(("%s shutdown\n", info->device_name));
2450
2451 /* clear status wait queue because status changes */
2452 /* can't happen after shutting down the hardware */
2453 wake_up_interruptible(&info->status_event_wait_q);
2454 wake_up_interruptible(&info->event_wait_q);
2455
2456 del_timer_sync(&info->tx_timer);
2457 del_timer_sync(&info->rx_timer);
2458
2459 kfree(info->tx_buf);
2460 info->tx_buf = NULL;
2461
2462 spin_lock_irqsave(&info->lock,flags);
2463
2464 tx_stop(info);
2465 rx_stop(info);
2466
2467 slgt_irq_off(info, IRQ_ALL | IRQ_MASTER);
2468
2469 if (!info->port.tty || info->port.tty->termios->c_cflag & HUPCL) {
2470 info->signals &= ~(SerialSignal_DTR + SerialSignal_RTS);
2471 set_signals(info);
2472 }
2473
2474 flush_cond_wait(&info->gpio_wait_q);
2475
2476 spin_unlock_irqrestore(&info->lock,flags);
2477
2478 if (info->port.tty)
2479 set_bit(TTY_IO_ERROR, &info->port.tty->flags);
2480
2481 info->port.flags &= ~ASYNC_INITIALIZED;
2482}
2483
2484static void program_hw(struct slgt_info *info)
2485{
2486 unsigned long flags;
2487
2488 spin_lock_irqsave(&info->lock,flags);
2489
2490 rx_stop(info);
2491 tx_stop(info);
2492
2493 if (info->params.mode != MGSL_MODE_ASYNC ||
2494 info->netcount)
2495 sync_mode(info);
2496 else
2497 async_mode(info);
2498
2499 set_signals(info);
2500
2501 info->dcd_chkcount = 0;
2502 info->cts_chkcount = 0;
2503 info->ri_chkcount = 0;
2504 info->dsr_chkcount = 0;
2505
2506 slgt_irq_on(info, IRQ_DCD | IRQ_CTS | IRQ_DSR | IRQ_RI);
2507 get_signals(info);
2508
2509 if (info->netcount ||
2510 (info->port.tty && info->port.tty->termios->c_cflag & CREAD))
2511 rx_start(info);
2512
2513 spin_unlock_irqrestore(&info->lock,flags);
2514}
2515
2516/*
2517 * reconfigure adapter based on new parameters
2518 */
2519static void change_params(struct slgt_info *info)
2520{
2521 unsigned cflag;
2522 int bits_per_char;
2523
2524 if (!info->port.tty || !info->port.tty->termios)
2525 return;
2526 DBGINFO(("%s change_params\n", info->device_name));
2527
2528 cflag = info->port.tty->termios->c_cflag;
2529
2530 /* if B0 rate (hangup) specified then negate DTR and RTS */
2531 /* otherwise assert DTR and RTS */
2532 if (cflag & CBAUD)
2533 info->signals |= SerialSignal_RTS + SerialSignal_DTR;
2534 else
2535 info->signals &= ~(SerialSignal_RTS + SerialSignal_DTR);
2536
2537 /* byte size and parity */
2538
2539 switch (cflag & CSIZE) {
2540 case CS5: info->params.data_bits = 5; break;
2541 case CS6: info->params.data_bits = 6; break;
2542 case CS7: info->params.data_bits = 7; break;
2543 case CS8: info->params.data_bits = 8; break;
2544 default: info->params.data_bits = 7; break;
2545 }
2546
2547 info->params.stop_bits = (cflag & CSTOPB) ? 2 : 1;
2548
2549 if (cflag & PARENB)
2550 info->params.parity = (cflag & PARODD) ? ASYNC_PARITY_ODD : ASYNC_PARITY_EVEN;
2551 else
2552 info->params.parity = ASYNC_PARITY_NONE;
2553
2554 /* calculate number of jiffies to transmit a full
2555 * FIFO (32 bytes) at specified data rate
2556 */
2557 bits_per_char = info->params.data_bits +
2558 info->params.stop_bits + 1;
2559
2560 info->params.data_rate = tty_get_baud_rate(info->port.tty);
2561
2562 if (info->params.data_rate) {
2563 info->timeout = (32*HZ*bits_per_char) /
2564 info->params.data_rate;
2565 }
2566 info->timeout += HZ/50; /* Add .02 seconds of slop */
2567
2568 if (cflag & CRTSCTS)
2569 info->port.flags |= ASYNC_CTS_FLOW;
2570 else
2571 info->port.flags &= ~ASYNC_CTS_FLOW;
2572
2573 if (cflag & CLOCAL)
2574 info->port.flags &= ~ASYNC_CHECK_CD;
2575 else
2576 info->port.flags |= ASYNC_CHECK_CD;
2577
2578 /* process tty input control flags */
2579
2580 info->read_status_mask = IRQ_RXOVER;
2581 if (I_INPCK(info->port.tty))
2582 info->read_status_mask |= MASK_PARITY | MASK_FRAMING;
2583 if (I_BRKINT(info->port.tty) || I_PARMRK(info->port.tty))
2584 info->read_status_mask |= MASK_BREAK;
2585 if (I_IGNPAR(info->port.tty))
2586 info->ignore_status_mask |= MASK_PARITY | MASK_FRAMING;
2587 if (I_IGNBRK(info->port.tty)) {
2588 info->ignore_status_mask |= MASK_BREAK;
2589 /* If ignoring parity and break indicators, ignore
2590 * overruns too. (For real raw support).
2591 */
2592 if (I_IGNPAR(info->port.tty))
2593 info->ignore_status_mask |= MASK_OVERRUN;
2594 }
2595
2596 program_hw(info);
2597}
2598
2599static int get_stats(struct slgt_info *info, struct mgsl_icount __user *user_icount)
2600{
2601 DBGINFO(("%s get_stats\n", info->device_name));
2602 if (!user_icount) {
2603 memset(&info->icount, 0, sizeof(info->icount));
2604 } else {
2605 if (copy_to_user(user_icount, &info->icount, sizeof(struct mgsl_icount)))
2606 return -EFAULT;
2607 }
2608 return 0;
2609}
2610
2611static int get_params(struct slgt_info *info, MGSL_PARAMS __user *user_params)
2612{
2613 DBGINFO(("%s get_params\n", info->device_name));
2614 if (copy_to_user(user_params, &info->params, sizeof(MGSL_PARAMS)))
2615 return -EFAULT;
2616 return 0;
2617}
2618
2619static int set_params(struct slgt_info *info, MGSL_PARAMS __user *new_params)
2620{
2621 unsigned long flags;
2622 MGSL_PARAMS tmp_params;
2623
2624 DBGINFO(("%s set_params\n", info->device_name));
2625 if (copy_from_user(&tmp_params, new_params, sizeof(MGSL_PARAMS)))
2626 return -EFAULT;
2627
2628 spin_lock_irqsave(&info->lock, flags);
2629 if (tmp_params.mode == MGSL_MODE_BASE_CLOCK)
2630 info->base_clock = tmp_params.clock_speed;
2631 else
2632 memcpy(&info->params, &tmp_params, sizeof(MGSL_PARAMS));
2633 spin_unlock_irqrestore(&info->lock, flags);
2634
2635 program_hw(info);
2636
2637 return 0;
2638}
2639
2640static int get_txidle(struct slgt_info *info, int __user *idle_mode)
2641{
2642 DBGINFO(("%s get_txidle=%d\n", info->device_name, info->idle_mode));
2643 if (put_user(info->idle_mode, idle_mode))
2644 return -EFAULT;
2645 return 0;
2646}
2647
2648static int set_txidle(struct slgt_info *info, int idle_mode)
2649{
2650 unsigned long flags;
2651 DBGINFO(("%s set_txidle(%d)\n", info->device_name, idle_mode));
2652 spin_lock_irqsave(&info->lock,flags);
2653 info->idle_mode = idle_mode;
2654 if (info->params.mode != MGSL_MODE_ASYNC)
2655 tx_set_idle(info);
2656 spin_unlock_irqrestore(&info->lock,flags);
2657 return 0;
2658}
2659
2660static int tx_enable(struct slgt_info *info, int enable)
2661{
2662 unsigned long flags;
2663 DBGINFO(("%s tx_enable(%d)\n", info->device_name, enable));
2664 spin_lock_irqsave(&info->lock,flags);
2665 if (enable) {
2666 if (!info->tx_enabled)
2667 tx_start(info);
2668 } else {
2669 if (info->tx_enabled)
2670 tx_stop(info);
2671 }
2672 spin_unlock_irqrestore(&info->lock,flags);
2673 return 0;
2674}
2675
2676/*
2677 * abort transmit HDLC frame
2678 */
2679static int tx_abort(struct slgt_info *info)
2680{
2681 unsigned long flags;
2682 DBGINFO(("%s tx_abort\n", info->device_name));
2683 spin_lock_irqsave(&info->lock,flags);
2684 tdma_reset(info);
2685 spin_unlock_irqrestore(&info->lock,flags);
2686 return 0;
2687}
2688
2689static int rx_enable(struct slgt_info *info, int enable)
2690{
2691 unsigned long flags;
2692 unsigned int rbuf_fill_level;
2693 DBGINFO(("%s rx_enable(%08x)\n", info->device_name, enable));
2694 spin_lock_irqsave(&info->lock,flags);
2695 /*
2696 * enable[31..16] = receive DMA buffer fill level
2697 * 0 = noop (leave fill level unchanged)
2698 * fill level must be multiple of 4 and <= buffer size
2699 */
2700 rbuf_fill_level = ((unsigned int)enable) >> 16;
2701 if (rbuf_fill_level) {
2702 if ((rbuf_fill_level > DMABUFSIZE) || (rbuf_fill_level % 4)) {
2703 spin_unlock_irqrestore(&info->lock, flags);
2704 return -EINVAL;
2705 }
2706 info->rbuf_fill_level = rbuf_fill_level;
2707 if (rbuf_fill_level < 128)
2708 info->rx_pio = 1; /* PIO mode */
2709 else
2710 info->rx_pio = 0; /* DMA mode */
2711 rx_stop(info); /* restart receiver to use new fill level */
2712 }
2713
2714 /*
2715 * enable[1..0] = receiver enable command
2716 * 0 = disable
2717 * 1 = enable
2718 * 2 = enable or force hunt mode if already enabled
2719 */
2720 enable &= 3;
2721 if (enable) {
2722 if (!info->rx_enabled)
2723 rx_start(info);
2724 else if (enable == 2) {
2725 /* force hunt mode (write 1 to RCR[3]) */
2726 wr_reg16(info, RCR, rd_reg16(info, RCR) | BIT3);
2727 }
2728 } else {
2729 if (info->rx_enabled)
2730 rx_stop(info);
2731 }
2732 spin_unlock_irqrestore(&info->lock,flags);
2733 return 0;
2734}
2735
2736/*
2737 * wait for specified event to occur
2738 */
2739static int wait_mgsl_event(struct slgt_info *info, int __user *mask_ptr)
2740{
2741 unsigned long flags;
2742 int s;
2743 int rc=0;
2744 struct mgsl_icount cprev, cnow;
2745 int events;
2746 int mask;
2747 struct _input_signal_events oldsigs, newsigs;
2748 DECLARE_WAITQUEUE(wait, current);
2749
2750 if (get_user(mask, mask_ptr))
2751 return -EFAULT;
2752
2753 DBGINFO(("%s wait_mgsl_event(%d)\n", info->device_name, mask));
2754
2755 spin_lock_irqsave(&info->lock,flags);
2756
2757 /* return immediately if state matches requested events */
2758 get_signals(info);
2759 s = info->signals;
2760
2761 events = mask &
2762 ( ((s & SerialSignal_DSR) ? MgslEvent_DsrActive:MgslEvent_DsrInactive) +
2763 ((s & SerialSignal_DCD) ? MgslEvent_DcdActive:MgslEvent_DcdInactive) +
2764 ((s & SerialSignal_CTS) ? MgslEvent_CtsActive:MgslEvent_CtsInactive) +
2765 ((s & SerialSignal_RI) ? MgslEvent_RiActive :MgslEvent_RiInactive) );
2766 if (events) {
2767 spin_unlock_irqrestore(&info->lock,flags);
2768 goto exit;
2769 }
2770
2771 /* save current irq counts */
2772 cprev = info->icount;
2773 oldsigs = info->input_signal_events;
2774
2775 /* enable hunt and idle irqs if needed */
2776 if (mask & (MgslEvent_ExitHuntMode+MgslEvent_IdleReceived)) {
2777 unsigned short val = rd_reg16(info, SCR);
2778 if (!(val & IRQ_RXIDLE))
2779 wr_reg16(info, SCR, (unsigned short)(val | IRQ_RXIDLE));
2780 }
2781
2782 set_current_state(TASK_INTERRUPTIBLE);
2783 add_wait_queue(&info->event_wait_q, &wait);
2784
2785 spin_unlock_irqrestore(&info->lock,flags);
2786
2787 for(;;) {
2788 schedule();
2789 if (signal_pending(current)) {
2790 rc = -ERESTARTSYS;
2791 break;
2792 }
2793
2794 /* get current irq counts */
2795 spin_lock_irqsave(&info->lock,flags);
2796 cnow = info->icount;
2797 newsigs = info->input_signal_events;
2798 set_current_state(TASK_INTERRUPTIBLE);
2799 spin_unlock_irqrestore(&info->lock,flags);
2800
2801 /* if no change, wait aborted for some reason */
2802 if (newsigs.dsr_up == oldsigs.dsr_up &&
2803 newsigs.dsr_down == oldsigs.dsr_down &&
2804 newsigs.dcd_up == oldsigs.dcd_up &&
2805 newsigs.dcd_down == oldsigs.dcd_down &&
2806 newsigs.cts_up == oldsigs.cts_up &&
2807 newsigs.cts_down == oldsigs.cts_down &&
2808 newsigs.ri_up == oldsigs.ri_up &&
2809 newsigs.ri_down == oldsigs.ri_down &&
2810 cnow.exithunt == cprev.exithunt &&
2811 cnow.rxidle == cprev.rxidle) {
2812 rc = -EIO;
2813 break;
2814 }
2815
2816 events = mask &
2817 ( (newsigs.dsr_up != oldsigs.dsr_up ? MgslEvent_DsrActive:0) +
2818 (newsigs.dsr_down != oldsigs.dsr_down ? MgslEvent_DsrInactive:0) +
2819 (newsigs.dcd_up != oldsigs.dcd_up ? MgslEvent_DcdActive:0) +
2820 (newsigs.dcd_down != oldsigs.dcd_down ? MgslEvent_DcdInactive:0) +
2821 (newsigs.cts_up != oldsigs.cts_up ? MgslEvent_CtsActive:0) +
2822 (newsigs.cts_down != oldsigs.cts_down ? MgslEvent_CtsInactive:0) +
2823 (newsigs.ri_up != oldsigs.ri_up ? MgslEvent_RiActive:0) +
2824 (newsigs.ri_down != oldsigs.ri_down ? MgslEvent_RiInactive:0) +
2825 (cnow.exithunt != cprev.exithunt ? MgslEvent_ExitHuntMode:0) +
2826 (cnow.rxidle != cprev.rxidle ? MgslEvent_IdleReceived:0) );
2827 if (events)
2828 break;
2829
2830 cprev = cnow;
2831 oldsigs = newsigs;
2832 }
2833
2834 remove_wait_queue(&info->event_wait_q, &wait);
2835 set_current_state(TASK_RUNNING);
2836
2837
2838 if (mask & (MgslEvent_ExitHuntMode + MgslEvent_IdleReceived)) {
2839 spin_lock_irqsave(&info->lock,flags);
2840 if (!waitqueue_active(&info->event_wait_q)) {
2841 /* disable enable exit hunt mode/idle rcvd IRQs */
2842 wr_reg16(info, SCR,
2843 (unsigned short)(rd_reg16(info, SCR) & ~IRQ_RXIDLE));
2844 }
2845 spin_unlock_irqrestore(&info->lock,flags);
2846 }
2847exit:
2848 if (rc == 0)
2849 rc = put_user(events, mask_ptr);
2850 return rc;
2851}
2852
2853static int get_interface(struct slgt_info *info, int __user *if_mode)
2854{
2855 DBGINFO(("%s get_interface=%x\n", info->device_name, info->if_mode));
2856 if (put_user(info->if_mode, if_mode))
2857 return -EFAULT;
2858 return 0;
2859}
2860
2861static int set_interface(struct slgt_info *info, int if_mode)
2862{
2863 unsigned long flags;
2864 unsigned short val;
2865
2866 DBGINFO(("%s set_interface=%x)\n", info->device_name, if_mode));
2867 spin_lock_irqsave(&info->lock,flags);
2868 info->if_mode = if_mode;
2869
2870 msc_set_vcr(info);
2871
2872 /* TCR (tx control) 07 1=RTS driver control */
2873 val = rd_reg16(info, TCR);
2874 if (info->if_mode & MGSL_INTERFACE_RTS_EN)
2875 val |= BIT7;
2876 else
2877 val &= ~BIT7;
2878 wr_reg16(info, TCR, val);
2879
2880 spin_unlock_irqrestore(&info->lock,flags);
2881 return 0;
2882}
2883
2884/*
2885 * set general purpose IO pin state and direction
2886 *
2887 * user_gpio fields:
2888 * state each bit indicates a pin state
2889 * smask set bit indicates pin state to set
2890 * dir each bit indicates a pin direction (0=input, 1=output)
2891 * dmask set bit indicates pin direction to set
2892 */
2893static int set_gpio(struct slgt_info *info, struct gpio_desc __user *user_gpio)
2894{
2895 unsigned long flags;
2896 struct gpio_desc gpio;
2897 __u32 data;
2898
2899 if (!info->gpio_present)
2900 return -EINVAL;
2901 if (copy_from_user(&gpio, user_gpio, sizeof(gpio)))
2902 return -EFAULT;
2903 DBGINFO(("%s set_gpio state=%08x smask=%08x dir=%08x dmask=%08x\n",
2904 info->device_name, gpio.state, gpio.smask,
2905 gpio.dir, gpio.dmask));
2906
2907 spin_lock_irqsave(&info->lock,flags);
2908 if (gpio.dmask) {
2909 data = rd_reg32(info, IODR);
2910 data |= gpio.dmask & gpio.dir;
2911 data &= ~(gpio.dmask & ~gpio.dir);
2912 wr_reg32(info, IODR, data);
2913 }
2914 if (gpio.smask) {
2915 data = rd_reg32(info, IOVR);
2916 data |= gpio.smask & gpio.state;
2917 data &= ~(gpio.smask & ~gpio.state);
2918 wr_reg32(info, IOVR, data);
2919 }
2920 spin_unlock_irqrestore(&info->lock,flags);
2921
2922 return 0;
2923}
2924
2925/*
2926 * get general purpose IO pin state and direction
2927 */
2928static int get_gpio(struct slgt_info *info, struct gpio_desc __user *user_gpio)
2929{
2930 struct gpio_desc gpio;
2931 if (!info->gpio_present)
2932 return -EINVAL;
2933 gpio.state = rd_reg32(info, IOVR);
2934 gpio.smask = 0xffffffff;
2935 gpio.dir = rd_reg32(info, IODR);
2936 gpio.dmask = 0xffffffff;
2937 if (copy_to_user(user_gpio, &gpio, sizeof(gpio)))
2938 return -EFAULT;
2939 DBGINFO(("%s get_gpio state=%08x dir=%08x\n",
2940 info->device_name, gpio.state, gpio.dir));
2941 return 0;
2942}
2943
2944/*
2945 * conditional wait facility
2946 */
2947static void init_cond_wait(struct cond_wait *w, unsigned int data)
2948{
2949 init_waitqueue_head(&w->q);
2950 init_waitqueue_entry(&w->wait, current);
2951 w->data = data;
2952}
2953
2954static void add_cond_wait(struct cond_wait **head, struct cond_wait *w)
2955{
2956 set_current_state(TASK_INTERRUPTIBLE);
2957 add_wait_queue(&w->q, &w->wait);
2958 w->next = *head;
2959 *head = w;
2960}
2961
2962static void remove_cond_wait(struct cond_wait **head, struct cond_wait *cw)
2963{
2964 struct cond_wait *w, *prev;
2965 remove_wait_queue(&cw->q, &cw->wait);
2966 set_current_state(TASK_RUNNING);
2967 for (w = *head, prev = NULL ; w != NULL ; prev = w, w = w->next) {
2968 if (w == cw) {
2969 if (prev != NULL)
2970 prev->next = w->next;
2971 else
2972 *head = w->next;
2973 break;
2974 }
2975 }
2976}
2977
2978static void flush_cond_wait(struct cond_wait **head)
2979{
2980 while (*head != NULL) {
2981 wake_up_interruptible(&(*head)->q);
2982 *head = (*head)->next;
2983 }
2984}
2985
2986/*
2987 * wait for general purpose I/O pin(s) to enter specified state
2988 *
2989 * user_gpio fields:
2990 * state - bit indicates target pin state
2991 * smask - set bit indicates watched pin
2992 *
2993 * The wait ends when at least one watched pin enters the specified
2994 * state. When 0 (no error) is returned, user_gpio->state is set to the
2995 * state of all GPIO pins when the wait ends.
2996 *
2997 * Note: Each pin may be a dedicated input, dedicated output, or
2998 * configurable input/output. The number and configuration of pins
2999 * varies with the specific adapter model. Only input pins (dedicated
3000 * or configured) can be monitored with this function.
3001 */
3002static int wait_gpio(struct slgt_info *info, struct gpio_desc __user *user_gpio)
3003{
3004 unsigned long flags;
3005 int rc = 0;
3006 struct gpio_desc gpio;
3007 struct cond_wait wait;
3008 u32 state;
3009
3010 if (!info->gpio_present)
3011 return -EINVAL;
3012 if (copy_from_user(&gpio, user_gpio, sizeof(gpio)))
3013 return -EFAULT;
3014 DBGINFO(("%s wait_gpio() state=%08x smask=%08x\n",
3015 info->device_name, gpio.state, gpio.smask));
3016 /* ignore output pins identified by set IODR bit */
3017 if ((gpio.smask &= ~rd_reg32(info, IODR)) == 0)
3018 return -EINVAL;
3019 init_cond_wait(&wait, gpio.smask);
3020
3021 spin_lock_irqsave(&info->lock, flags);
3022 /* enable interrupts for watched pins */
3023 wr_reg32(info, IOER, rd_reg32(info, IOER) | gpio.smask);
3024 /* get current pin states */
3025 state = rd_reg32(info, IOVR);
3026
3027 if (gpio.smask & ~(state ^ gpio.state)) {
3028 /* already in target state */
3029 gpio.state = state;
3030 } else {
3031 /* wait for target state */
3032 add_cond_wait(&info->gpio_wait_q, &wait);
3033 spin_unlock_irqrestore(&info->lock, flags);
3034 schedule();
3035 if (signal_pending(current))
3036 rc = -ERESTARTSYS;
3037 else
3038 gpio.state = wait.data;
3039 spin_lock_irqsave(&info->lock, flags);
3040 remove_cond_wait(&info->gpio_wait_q, &wait);
3041 }
3042
3043 /* disable all GPIO interrupts if no waiting processes */
3044 if (info->gpio_wait_q == NULL)
3045 wr_reg32(info, IOER, 0);
3046 spin_unlock_irqrestore(&info->lock,flags);
3047
3048 if ((rc == 0) && copy_to_user(user_gpio, &gpio, sizeof(gpio)))
3049 rc = -EFAULT;
3050 return rc;
3051}
3052
3053static int modem_input_wait(struct slgt_info *info,int arg)
3054{
3055 unsigned long flags;
3056 int rc;
3057 struct mgsl_icount cprev, cnow;
3058 DECLARE_WAITQUEUE(wait, current);
3059
3060 /* save current irq counts */
3061 spin_lock_irqsave(&info->lock,flags);
3062 cprev = info->icount;
3063 add_wait_queue(&info->status_event_wait_q, &wait);
3064 set_current_state(TASK_INTERRUPTIBLE);
3065 spin_unlock_irqrestore(&info->lock,flags);
3066
3067 for(;;) {
3068 schedule();
3069 if (signal_pending(current)) {
3070 rc = -ERESTARTSYS;
3071 break;
3072 }
3073
3074 /* get new irq counts */
3075 spin_lock_irqsave(&info->lock,flags);
3076 cnow = info->icount;
3077 set_current_state(TASK_INTERRUPTIBLE);
3078 spin_unlock_irqrestore(&info->lock,flags);
3079
3080 /* if no change, wait aborted for some reason */
3081 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
3082 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts) {
3083 rc = -EIO;
3084 break;
3085 }
3086
3087 /* check for change in caller specified modem input */
3088 if ((arg & TIOCM_RNG && cnow.rng != cprev.rng) ||
3089 (arg & TIOCM_DSR && cnow.dsr != cprev.dsr) ||
3090 (arg & TIOCM_CD && cnow.dcd != cprev.dcd) ||
3091 (arg & TIOCM_CTS && cnow.cts != cprev.cts)) {
3092 rc = 0;
3093 break;
3094 }
3095
3096 cprev = cnow;
3097 }
3098 remove_wait_queue(&info->status_event_wait_q, &wait);
3099 set_current_state(TASK_RUNNING);
3100 return rc;
3101}
3102
3103/*
3104 * return state of serial control and status signals
3105 */
3106static int tiocmget(struct tty_struct *tty, struct file *file)
3107{
3108 struct slgt_info *info = tty->driver_data;
3109 unsigned int result;
3110 unsigned long flags;
3111
3112 spin_lock_irqsave(&info->lock,flags);
3113 get_signals(info);
3114 spin_unlock_irqrestore(&info->lock,flags);
3115
3116 result = ((info->signals & SerialSignal_RTS) ? TIOCM_RTS:0) +
3117 ((info->signals & SerialSignal_DTR) ? TIOCM_DTR:0) +
3118 ((info->signals & SerialSignal_DCD) ? TIOCM_CAR:0) +
3119 ((info->signals & SerialSignal_RI) ? TIOCM_RNG:0) +
3120 ((info->signals & SerialSignal_DSR) ? TIOCM_DSR:0) +
3121 ((info->signals & SerialSignal_CTS) ? TIOCM_CTS:0);
3122
3123 DBGINFO(("%s tiocmget value=%08X\n", info->device_name, result));
3124 return result;
3125}
3126
3127/*
3128 * set modem control signals (DTR/RTS)
3129 *
3130 * cmd signal command: TIOCMBIS = set bit TIOCMBIC = clear bit
3131 * TIOCMSET = set/clear signal values
3132 * value bit mask for command
3133 */
3134static int tiocmset(struct tty_struct *tty, struct file *file,
3135 unsigned int set, unsigned int clear)
3136{
3137 struct slgt_info *info = tty->driver_data;
3138 unsigned long flags;
3139
3140 DBGINFO(("%s tiocmset(%x,%x)\n", info->device_name, set, clear));
3141
3142 if (set & TIOCM_RTS)
3143 info->signals |= SerialSignal_RTS;
3144 if (set & TIOCM_DTR)
3145 info->signals |= SerialSignal_DTR;
3146 if (clear & TIOCM_RTS)
3147 info->signals &= ~SerialSignal_RTS;
3148 if (clear & TIOCM_DTR)
3149 info->signals &= ~SerialSignal_DTR;
3150
3151 spin_lock_irqsave(&info->lock,flags);
3152 set_signals(info);
3153 spin_unlock_irqrestore(&info->lock,flags);
3154 return 0;
3155}
3156
3157static int carrier_raised(struct tty_port *port)
3158{
3159 unsigned long flags;
3160 struct slgt_info *info = container_of(port, struct slgt_info, port);
3161
3162 spin_lock_irqsave(&info->lock,flags);
3163 get_signals(info);
3164 spin_unlock_irqrestore(&info->lock,flags);
3165 return (info->signals & SerialSignal_DCD) ? 1 : 0;
3166}
3167
3168static void dtr_rts(struct tty_port *port, int on)
3169{
3170 unsigned long flags;
3171 struct slgt_info *info = container_of(port, struct slgt_info, port);
3172
3173 spin_lock_irqsave(&info->lock,flags);
3174 if (on)
3175 info->signals |= SerialSignal_RTS + SerialSignal_DTR;
3176 else
3177 info->signals &= ~(SerialSignal_RTS + SerialSignal_DTR);
3178 set_signals(info);
3179 spin_unlock_irqrestore(&info->lock,flags);
3180}
3181
3182
3183/*
3184 * block current process until the device is ready to open
3185 */
3186static int block_til_ready(struct tty_struct *tty, struct file *filp,
3187 struct slgt_info *info)
3188{
3189 DECLARE_WAITQUEUE(wait, current);
3190 int retval;
3191 bool do_clocal = false;
3192 bool extra_count = false;
3193 unsigned long flags;
3194 int cd;
3195 struct tty_port *port = &info->port;
3196
3197 DBGINFO(("%s block_til_ready\n", tty->driver->name));
3198
3199 if (filp->f_flags & O_NONBLOCK || tty->flags & (1 << TTY_IO_ERROR)){
3200 /* nonblock mode is set or port is not enabled */
3201 port->flags |= ASYNC_NORMAL_ACTIVE;
3202 return 0;
3203 }
3204
3205 if (tty->termios->c_cflag & CLOCAL)
3206 do_clocal = true;
3207
3208 /* Wait for carrier detect and the line to become
3209 * free (i.e., not in use by the callout). While we are in
3210 * this loop, port->count is dropped by one, so that
3211 * close() knows when to free things. We restore it upon
3212 * exit, either normal or abnormal.
3213 */
3214
3215 retval = 0;
3216 add_wait_queue(&port->open_wait, &wait);
3217
3218 spin_lock_irqsave(&info->lock, flags);
3219 if (!tty_hung_up_p(filp)) {
3220 extra_count = true;
3221 port->count--;
3222 }
3223 spin_unlock_irqrestore(&info->lock, flags);
3224 port->blocked_open++;
3225
3226 while (1) {
3227 if ((tty->termios->c_cflag & CBAUD))
3228 tty_port_raise_dtr_rts(port);
3229
3230 set_current_state(TASK_INTERRUPTIBLE);
3231
3232 if (tty_hung_up_p(filp) || !(port->flags & ASYNC_INITIALIZED)){
3233 retval = (port->flags & ASYNC_HUP_NOTIFY) ?
3234 -EAGAIN : -ERESTARTSYS;
3235 break;
3236 }
3237
3238 cd = tty_port_carrier_raised(port);
3239
3240 if (!(port->flags & ASYNC_CLOSING) && (do_clocal || cd ))
3241 break;
3242
3243 if (signal_pending(current)) {
3244 retval = -ERESTARTSYS;
3245 break;
3246 }
3247
3248 DBGINFO(("%s block_til_ready wait\n", tty->driver->name));
3249 tty_unlock();
3250 schedule();
3251 tty_lock();
3252 }
3253
3254 set_current_state(TASK_RUNNING);
3255 remove_wait_queue(&port->open_wait, &wait);
3256
3257 if (extra_count)
3258 port->count++;
3259 port->blocked_open--;
3260
3261 if (!retval)
3262 port->flags |= ASYNC_NORMAL_ACTIVE;
3263
3264 DBGINFO(("%s block_til_ready ready, rc=%d\n", tty->driver->name, retval));
3265 return retval;
3266}
3267
3268static int alloc_tmp_rbuf(struct slgt_info *info)
3269{
3270 info->tmp_rbuf = kmalloc(info->max_frame_size + 5, GFP_KERNEL);
3271 if (info->tmp_rbuf == NULL)
3272 return -ENOMEM;
3273 return 0;
3274}
3275
3276static void free_tmp_rbuf(struct slgt_info *info)
3277{
3278 kfree(info->tmp_rbuf);
3279 info->tmp_rbuf = NULL;
3280}
3281
3282/*
3283 * allocate DMA descriptor lists.
3284 */
3285static int alloc_desc(struct slgt_info *info)
3286{
3287 unsigned int i;
3288 unsigned int pbufs;
3289
3290 /* allocate memory to hold descriptor lists */
3291 info->bufs = pci_alloc_consistent(info->pdev, DESC_LIST_SIZE, &info->bufs_dma_addr);
3292 if (info->bufs == NULL)
3293 return -ENOMEM;
3294
3295 memset(info->bufs, 0, DESC_LIST_SIZE);
3296
3297 info->rbufs = (struct slgt_desc*)info->bufs;
3298 info->tbufs = ((struct slgt_desc*)info->bufs) + info->rbuf_count;
3299
3300 pbufs = (unsigned int)info->bufs_dma_addr;
3301
3302 /*
3303 * Build circular lists of descriptors
3304 */
3305
3306 for (i=0; i < info->rbuf_count; i++) {
3307 /* physical address of this descriptor */
3308 info->rbufs[i].pdesc = pbufs + (i * sizeof(struct slgt_desc));
3309
3310 /* physical address of next descriptor */
3311 if (i == info->rbuf_count - 1)
3312 info->rbufs[i].next = cpu_to_le32(pbufs);
3313 else
3314 info->rbufs[i].next = cpu_to_le32(pbufs + ((i+1) * sizeof(struct slgt_desc)));
3315 set_desc_count(info->rbufs[i], DMABUFSIZE);
3316 }
3317
3318 for (i=0; i < info->tbuf_count; i++) {
3319 /* physical address of this descriptor */
3320 info->tbufs[i].pdesc = pbufs + ((info->rbuf_count + i) * sizeof(struct slgt_desc));
3321
3322 /* physical address of next descriptor */
3323 if (i == info->tbuf_count - 1)
3324 info->tbufs[i].next = cpu_to_le32(pbufs + info->rbuf_count * sizeof(struct slgt_desc));
3325 else
3326 info->tbufs[i].next = cpu_to_le32(pbufs + ((info->rbuf_count + i + 1) * sizeof(struct slgt_desc)));
3327 }
3328
3329 return 0;
3330}
3331
3332static void free_desc(struct slgt_info *info)
3333{
3334 if (info->bufs != NULL) {
3335 pci_free_consistent(info->pdev, DESC_LIST_SIZE, info->bufs, info->bufs_dma_addr);
3336 info->bufs = NULL;
3337 info->rbufs = NULL;
3338 info->tbufs = NULL;
3339 }
3340}
3341
3342static int alloc_bufs(struct slgt_info *info, struct slgt_desc *bufs, int count)
3343{
3344 int i;
3345 for (i=0; i < count; i++) {
3346 if ((bufs[i].buf = pci_alloc_consistent(info->pdev, DMABUFSIZE, &bufs[i].buf_dma_addr)) == NULL)
3347 return -ENOMEM;
3348 bufs[i].pbuf = cpu_to_le32((unsigned int)bufs[i].buf_dma_addr);
3349 }
3350 return 0;
3351}
3352
3353static void free_bufs(struct slgt_info *info, struct slgt_desc *bufs, int count)
3354{
3355 int i;
3356 for (i=0; i < count; i++) {
3357 if (bufs[i].buf == NULL)
3358 continue;
3359 pci_free_consistent(info->pdev, DMABUFSIZE, bufs[i].buf, bufs[i].buf_dma_addr);
3360 bufs[i].buf = NULL;
3361 }
3362}
3363
3364static int alloc_dma_bufs(struct slgt_info *info)
3365{
3366 info->rbuf_count = 32;
3367 info->tbuf_count = 32;
3368
3369 if (alloc_desc(info) < 0 ||
3370 alloc_bufs(info, info->rbufs, info->rbuf_count) < 0 ||
3371 alloc_bufs(info, info->tbufs, info->tbuf_count) < 0 ||
3372 alloc_tmp_rbuf(info) < 0) {
3373 DBGERR(("%s DMA buffer alloc fail\n", info->device_name));
3374 return -ENOMEM;
3375 }
3376 reset_rbufs(info);
3377 return 0;
3378}
3379
3380static void free_dma_bufs(struct slgt_info *info)
3381{
3382 if (info->bufs) {
3383 free_bufs(info, info->rbufs, info->rbuf_count);
3384 free_bufs(info, info->tbufs, info->tbuf_count);
3385 free_desc(info);
3386 }
3387 free_tmp_rbuf(info);
3388}
3389
3390static int claim_resources(struct slgt_info *info)
3391{
3392 if (request_mem_region(info->phys_reg_addr, SLGT_REG_SIZE, "synclink_gt") == NULL) {
3393 DBGERR(("%s reg addr conflict, addr=%08X\n",
3394 info->device_name, info->phys_reg_addr));
3395 info->init_error = DiagStatus_AddressConflict;
3396 goto errout;
3397 }
3398 else
3399 info->reg_addr_requested = true;
3400
3401 info->reg_addr = ioremap_nocache(info->phys_reg_addr, SLGT_REG_SIZE);
3402 if (!info->reg_addr) {
3403 DBGERR(("%s cant map device registers, addr=%08X\n",
3404 info->device_name, info->phys_reg_addr));
3405 info->init_error = DiagStatus_CantAssignPciResources;
3406 goto errout;
3407 }
3408 return 0;
3409
3410errout:
3411 release_resources(info);
3412 return -ENODEV;
3413}
3414
3415static void release_resources(struct slgt_info *info)
3416{
3417 if (info->irq_requested) {
3418 free_irq(info->irq_level, info);
3419 info->irq_requested = false;
3420 }
3421
3422 if (info->reg_addr_requested) {
3423 release_mem_region(info->phys_reg_addr, SLGT_REG_SIZE);
3424 info->reg_addr_requested = false;
3425 }
3426
3427 if (info->reg_addr) {
3428 iounmap(info->reg_addr);
3429 info->reg_addr = NULL;
3430 }
3431}
3432
3433/* Add the specified device instance data structure to the
3434 * global linked list of devices and increment the device count.
3435 */
3436static void add_device(struct slgt_info *info)
3437{
3438 char *devstr;
3439
3440 info->next_device = NULL;
3441 info->line = slgt_device_count;
3442 sprintf(info->device_name, "%s%d", tty_dev_prefix, info->line);
3443
3444 if (info->line < MAX_DEVICES) {
3445 if (maxframe[info->line])
3446 info->max_frame_size = maxframe[info->line];
3447 }
3448
3449 slgt_device_count++;
3450
3451 if (!slgt_device_list)
3452 slgt_device_list = info;
3453 else {
3454 struct slgt_info *current_dev = slgt_device_list;
3455 while(current_dev->next_device)
3456 current_dev = current_dev->next_device;
3457 current_dev->next_device = info;
3458 }
3459
3460 if (info->max_frame_size < 4096)
3461 info->max_frame_size = 4096;
3462 else if (info->max_frame_size > 65535)
3463 info->max_frame_size = 65535;
3464
3465 switch(info->pdev->device) {
3466 case SYNCLINK_GT_DEVICE_ID:
3467 devstr = "GT";
3468 break;
3469 case SYNCLINK_GT2_DEVICE_ID:
3470 devstr = "GT2";
3471 break;
3472 case SYNCLINK_GT4_DEVICE_ID:
3473 devstr = "GT4";
3474 break;
3475 case SYNCLINK_AC_DEVICE_ID:
3476 devstr = "AC";
3477 info->params.mode = MGSL_MODE_ASYNC;
3478 break;
3479 default:
3480 devstr = "(unknown model)";
3481 }
3482 printk("SyncLink %s %s IO=%08x IRQ=%d MaxFrameSize=%u\n",
3483 devstr, info->device_name, info->phys_reg_addr,
3484 info->irq_level, info->max_frame_size);
3485
3486#if SYNCLINK_GENERIC_HDLC
3487 hdlcdev_init(info);
3488#endif
3489}
3490
3491static const struct tty_port_operations slgt_port_ops = {
3492 .carrier_raised = carrier_raised,
3493 .dtr_rts = dtr_rts,
3494};
3495
3496/*
3497 * allocate device instance structure, return NULL on failure
3498 */
3499static struct slgt_info *alloc_dev(int adapter_num, int port_num, struct pci_dev *pdev)
3500{
3501 struct slgt_info *info;
3502
3503 info = kzalloc(sizeof(struct slgt_info), GFP_KERNEL);
3504
3505 if (!info) {
3506 DBGERR(("%s device alloc failed adapter=%d port=%d\n",
3507 driver_name, adapter_num, port_num));
3508 } else {
3509 tty_port_init(&info->port);
3510 info->port.ops = &slgt_port_ops;
3511 info->magic = MGSL_MAGIC;
3512 INIT_WORK(&info->task, bh_handler);
3513 info->max_frame_size = 4096;
3514 info->base_clock = 14745600;
3515 info->rbuf_fill_level = DMABUFSIZE;
3516 info->port.close_delay = 5*HZ/10;
3517 info->port.closing_wait = 30*HZ;
3518 init_waitqueue_head(&info->status_event_wait_q);
3519 init_waitqueue_head(&info->event_wait_q);
3520 spin_lock_init(&info->netlock);
3521 memcpy(&info->params,&default_params,sizeof(MGSL_PARAMS));
3522 info->idle_mode = HDLC_TXIDLE_FLAGS;
3523 info->adapter_num = adapter_num;
3524 info->port_num = port_num;
3525
3526 setup_timer(&info->tx_timer, tx_timeout, (unsigned long)info);
3527 setup_timer(&info->rx_timer, rx_timeout, (unsigned long)info);
3528
3529 /* Copy configuration info to device instance data */
3530 info->pdev = pdev;
3531 info->irq_level = pdev->irq;
3532 info->phys_reg_addr = pci_resource_start(pdev,0);
3533
3534 info->bus_type = MGSL_BUS_TYPE_PCI;
3535 info->irq_flags = IRQF_SHARED;
3536
3537 info->init_error = -1; /* assume error, set to 0 on successful init */
3538 }
3539
3540 return info;
3541}
3542
3543static void device_init(int adapter_num, struct pci_dev *pdev)
3544{
3545 struct slgt_info *port_array[SLGT_MAX_PORTS];
3546 int i;
3547 int port_count = 1;
3548
3549 if (pdev->device == SYNCLINK_GT2_DEVICE_ID)
3550 port_count = 2;
3551 else if (pdev->device == SYNCLINK_GT4_DEVICE_ID)
3552 port_count = 4;
3553
3554 /* allocate device instances for all ports */
3555 for (i=0; i < port_count; ++i) {
3556 port_array[i] = alloc_dev(adapter_num, i, pdev);
3557 if (port_array[i] == NULL) {
3558 for (--i; i >= 0; --i)
3559 kfree(port_array[i]);
3560 return;
3561 }
3562 }
3563
3564 /* give copy of port_array to all ports and add to device list */
3565 for (i=0; i < port_count; ++i) {
3566 memcpy(port_array[i]->port_array, port_array, sizeof(port_array));
3567 add_device(port_array[i]);
3568 port_array[i]->port_count = port_count;
3569 spin_lock_init(&port_array[i]->lock);
3570 }
3571
3572 /* Allocate and claim adapter resources */
3573 if (!claim_resources(port_array[0])) {
3574
3575 alloc_dma_bufs(port_array[0]);
3576
3577 /* copy resource information from first port to others */
3578 for (i = 1; i < port_count; ++i) {
3579 port_array[i]->lock = port_array[0]->lock;
3580 port_array[i]->irq_level = port_array[0]->irq_level;
3581 port_array[i]->reg_addr = port_array[0]->reg_addr;
3582 alloc_dma_bufs(port_array[i]);
3583 }
3584
3585 if (request_irq(port_array[0]->irq_level,
3586 slgt_interrupt,
3587 port_array[0]->irq_flags,
3588 port_array[0]->device_name,
3589 port_array[0]) < 0) {
3590 DBGERR(("%s request_irq failed IRQ=%d\n",
3591 port_array[0]->device_name,
3592 port_array[0]->irq_level));
3593 } else {
3594 port_array[0]->irq_requested = true;
3595 adapter_test(port_array[0]);
3596 for (i=1 ; i < port_count ; i++) {
3597 port_array[i]->init_error = port_array[0]->init_error;
3598 port_array[i]->gpio_present = port_array[0]->gpio_present;
3599 }
3600 }
3601 }
3602
3603 for (i=0; i < port_count; ++i)
3604 tty_register_device(serial_driver, port_array[i]->line, &(port_array[i]->pdev->dev));
3605}
3606
3607static int __devinit init_one(struct pci_dev *dev,
3608 const struct pci_device_id *ent)
3609{
3610 if (pci_enable_device(dev)) {
3611 printk("error enabling pci device %p\n", dev);
3612 return -EIO;
3613 }
3614 pci_set_master(dev);
3615 device_init(slgt_device_count, dev);
3616 return 0;
3617}
3618
3619static void __devexit remove_one(struct pci_dev *dev)
3620{
3621}
3622
3623static const struct tty_operations ops = {
3624 .open = open,
3625 .close = close,
3626 .write = write,
3627 .put_char = put_char,
3628 .flush_chars = flush_chars,
3629 .write_room = write_room,
3630 .chars_in_buffer = chars_in_buffer,
3631 .flush_buffer = flush_buffer,
3632 .ioctl = ioctl,
3633 .compat_ioctl = slgt_compat_ioctl,
3634 .throttle = throttle,
3635 .unthrottle = unthrottle,
3636 .send_xchar = send_xchar,
3637 .break_ctl = set_break,
3638 .wait_until_sent = wait_until_sent,
3639 .set_termios = set_termios,
3640 .stop = tx_hold,
3641 .start = tx_release,
3642 .hangup = hangup,
3643 .tiocmget = tiocmget,
3644 .tiocmset = tiocmset,
3645 .proc_fops = &synclink_gt_proc_fops,
3646};
3647
3648static void slgt_cleanup(void)
3649{
3650 int rc;
3651 struct slgt_info *info;
3652 struct slgt_info *tmp;
3653
3654 printk(KERN_INFO "unload %s\n", driver_name);
3655
3656 if (serial_driver) {
3657 for (info=slgt_device_list ; info != NULL ; info=info->next_device)
3658 tty_unregister_device(serial_driver, info->line);
3659 if ((rc = tty_unregister_driver(serial_driver)))
3660 DBGERR(("tty_unregister_driver error=%d\n", rc));
3661 put_tty_driver(serial_driver);
3662 }
3663
3664 /* reset devices */
3665 info = slgt_device_list;
3666 while(info) {
3667 reset_port(info);
3668 info = info->next_device;
3669 }
3670
3671 /* release devices */
3672 info = slgt_device_list;
3673 while(info) {
3674#if SYNCLINK_GENERIC_HDLC
3675 hdlcdev_exit(info);
3676#endif
3677 free_dma_bufs(info);
3678 free_tmp_rbuf(info);
3679 if (info->port_num == 0)
3680 release_resources(info);
3681 tmp = info;
3682 info = info->next_device;
3683 kfree(tmp);
3684 }
3685
3686 if (pci_registered)
3687 pci_unregister_driver(&pci_driver);
3688}
3689
3690/*
3691 * Driver initialization entry point.
3692 */
3693static int __init slgt_init(void)
3694{
3695 int rc;
3696
3697 printk(KERN_INFO "%s\n", driver_name);
3698
3699 serial_driver = alloc_tty_driver(MAX_DEVICES);
3700 if (!serial_driver) {
3701 printk("%s can't allocate tty driver\n", driver_name);
3702 return -ENOMEM;
3703 }
3704
3705 /* Initialize the tty_driver structure */
3706
3707 serial_driver->owner = THIS_MODULE;
3708 serial_driver->driver_name = tty_driver_name;
3709 serial_driver->name = tty_dev_prefix;
3710 serial_driver->major = ttymajor;
3711 serial_driver->minor_start = 64;
3712 serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
3713 serial_driver->subtype = SERIAL_TYPE_NORMAL;
3714 serial_driver->init_termios = tty_std_termios;
3715 serial_driver->init_termios.c_cflag =
3716 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
3717 serial_driver->init_termios.c_ispeed = 9600;
3718 serial_driver->init_termios.c_ospeed = 9600;
3719 serial_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
3720 tty_set_operations(serial_driver, &ops);
3721 if ((rc = tty_register_driver(serial_driver)) < 0) {
3722 DBGERR(("%s can't register serial driver\n", driver_name));
3723 put_tty_driver(serial_driver);
3724 serial_driver = NULL;
3725 goto error;
3726 }
3727
3728 printk(KERN_INFO "%s, tty major#%d\n",
3729 driver_name, serial_driver->major);
3730
3731 slgt_device_count = 0;
3732 if ((rc = pci_register_driver(&pci_driver)) < 0) {
3733 printk("%s pci_register_driver error=%d\n", driver_name, rc);
3734 goto error;
3735 }
3736 pci_registered = true;
3737
3738 if (!slgt_device_list)
3739 printk("%s no devices found\n",driver_name);
3740
3741 return 0;
3742
3743error:
3744 slgt_cleanup();
3745 return rc;
3746}
3747
3748static void __exit slgt_exit(void)
3749{
3750 slgt_cleanup();
3751}
3752
3753module_init(slgt_init);
3754module_exit(slgt_exit);
3755
3756/*
3757 * register access routines
3758 */
3759
3760#define CALC_REGADDR() \
3761 unsigned long reg_addr = ((unsigned long)info->reg_addr) + addr; \
3762 if (addr >= 0x80) \
3763 reg_addr += (info->port_num) * 32;
3764
3765static __u8 rd_reg8(struct slgt_info *info, unsigned int addr)
3766{
3767 CALC_REGADDR();
3768 return readb((void __iomem *)reg_addr);
3769}
3770
3771static void wr_reg8(struct slgt_info *info, unsigned int addr, __u8 value)
3772{
3773 CALC_REGADDR();
3774 writeb(value, (void __iomem *)reg_addr);
3775}
3776
3777static __u16 rd_reg16(struct slgt_info *info, unsigned int addr)
3778{
3779 CALC_REGADDR();
3780 return readw((void __iomem *)reg_addr);
3781}
3782
3783static void wr_reg16(struct slgt_info *info, unsigned int addr, __u16 value)
3784{
3785 CALC_REGADDR();
3786 writew(value, (void __iomem *)reg_addr);
3787}
3788
3789static __u32 rd_reg32(struct slgt_info *info, unsigned int addr)
3790{
3791 CALC_REGADDR();
3792 return readl((void __iomem *)reg_addr);
3793}
3794
3795static void wr_reg32(struct slgt_info *info, unsigned int addr, __u32 value)
3796{
3797 CALC_REGADDR();
3798 writel(value, (void __iomem *)reg_addr);
3799}
3800
3801static void rdma_reset(struct slgt_info *info)
3802{
3803 unsigned int i;
3804
3805 /* set reset bit */
3806 wr_reg32(info, RDCSR, BIT1);
3807
3808 /* wait for enable bit cleared */
3809 for(i=0 ; i < 1000 ; i++)
3810 if (!(rd_reg32(info, RDCSR) & BIT0))
3811 break;
3812}
3813
3814static void tdma_reset(struct slgt_info *info)
3815{
3816 unsigned int i;
3817
3818 /* set reset bit */
3819 wr_reg32(info, TDCSR, BIT1);
3820
3821 /* wait for enable bit cleared */
3822 for(i=0 ; i < 1000 ; i++)
3823 if (!(rd_reg32(info, TDCSR) & BIT0))
3824 break;
3825}
3826
3827/*
3828 * enable internal loopback
3829 * TxCLK and RxCLK are generated from BRG
3830 * and TxD is looped back to RxD internally.
3831 */
3832static void enable_loopback(struct slgt_info *info)
3833{
3834 /* SCR (serial control) BIT2=looopback enable */
3835 wr_reg16(info, SCR, (unsigned short)(rd_reg16(info, SCR) | BIT2));
3836
3837 if (info->params.mode != MGSL_MODE_ASYNC) {
3838 /* CCR (clock control)
3839 * 07..05 tx clock source (010 = BRG)
3840 * 04..02 rx clock source (010 = BRG)
3841 * 01 auxclk enable (0 = disable)
3842 * 00 BRG enable (1 = enable)
3843 *
3844 * 0100 1001
3845 */
3846 wr_reg8(info, CCR, 0x49);
3847
3848 /* set speed if available, otherwise use default */
3849 if (info->params.clock_speed)
3850 set_rate(info, info->params.clock_speed);
3851 else
3852 set_rate(info, 3686400);
3853 }
3854}
3855
3856/*
3857 * set baud rate generator to specified rate
3858 */
3859static void set_rate(struct slgt_info *info, u32 rate)
3860{
3861 unsigned int div;
3862 unsigned int osc = info->base_clock;
3863
3864 /* div = osc/rate - 1
3865 *
3866 * Round div up if osc/rate is not integer to
3867 * force to next slowest rate.
3868 */
3869
3870 if (rate) {
3871 div = osc/rate;
3872 if (!(osc % rate) && div)
3873 div--;
3874 wr_reg16(info, BDR, (unsigned short)div);
3875 }
3876}
3877
3878static void rx_stop(struct slgt_info *info)
3879{
3880 unsigned short val;
3881
3882 /* disable and reset receiver */
3883 val = rd_reg16(info, RCR) & ~BIT1; /* clear enable bit */
3884 wr_reg16(info, RCR, (unsigned short)(val | BIT2)); /* set reset bit */
3885 wr_reg16(info, RCR, val); /* clear reset bit */
3886
3887 slgt_irq_off(info, IRQ_RXOVER + IRQ_RXDATA + IRQ_RXIDLE);
3888
3889 /* clear pending rx interrupts */
3890 wr_reg16(info, SSR, IRQ_RXIDLE + IRQ_RXOVER);
3891
3892 rdma_reset(info);
3893
3894 info->rx_enabled = false;
3895 info->rx_restart = false;
3896}
3897
3898static void rx_start(struct slgt_info *info)
3899{
3900 unsigned short val;
3901
3902 slgt_irq_off(info, IRQ_RXOVER + IRQ_RXDATA);
3903
3904 /* clear pending rx overrun IRQ */
3905 wr_reg16(info, SSR, IRQ_RXOVER);
3906
3907 /* reset and disable receiver */
3908 val = rd_reg16(info, RCR) & ~BIT1; /* clear enable bit */
3909 wr_reg16(info, RCR, (unsigned short)(val | BIT2)); /* set reset bit */
3910 wr_reg16(info, RCR, val); /* clear reset bit */
3911
3912 rdma_reset(info);
3913 reset_rbufs(info);
3914
3915 if (info->rx_pio) {
3916 /* rx request when rx FIFO not empty */
3917 wr_reg16(info, SCR, (unsigned short)(rd_reg16(info, SCR) & ~BIT14));
3918 slgt_irq_on(info, IRQ_RXDATA);
3919 if (info->params.mode == MGSL_MODE_ASYNC) {
3920 /* enable saving of rx status */
3921 wr_reg32(info, RDCSR, BIT6);
3922 }
3923 } else {
3924 /* rx request when rx FIFO half full */
3925 wr_reg16(info, SCR, (unsigned short)(rd_reg16(info, SCR) | BIT14));
3926 /* set 1st descriptor address */
3927 wr_reg32(info, RDDAR, info->rbufs[0].pdesc);
3928
3929 if (info->params.mode != MGSL_MODE_ASYNC) {
3930 /* enable rx DMA and DMA interrupt */
3931 wr_reg32(info, RDCSR, (BIT2 + BIT0));
3932 } else {
3933 /* enable saving of rx status, rx DMA and DMA interrupt */
3934 wr_reg32(info, RDCSR, (BIT6 + BIT2 + BIT0));
3935 }
3936 }
3937
3938 slgt_irq_on(info, IRQ_RXOVER);
3939
3940 /* enable receiver */
3941 wr_reg16(info, RCR, (unsigned short)(rd_reg16(info, RCR) | BIT1));
3942
3943 info->rx_restart = false;
3944 info->rx_enabled = true;
3945}
3946
3947static void tx_start(struct slgt_info *info)
3948{
3949 if (!info->tx_enabled) {
3950 wr_reg16(info, TCR,
3951 (unsigned short)((rd_reg16(info, TCR) | BIT1) & ~BIT2));
3952 info->tx_enabled = true;
3953 }
3954
3955 if (desc_count(info->tbufs[info->tbuf_start])) {
3956 info->drop_rts_on_tx_done = false;
3957
3958 if (info->params.mode != MGSL_MODE_ASYNC) {
3959 if (info->params.flags & HDLC_FLAG_AUTO_RTS) {
3960 get_signals(info);
3961 if (!(info->signals & SerialSignal_RTS)) {
3962 info->signals |= SerialSignal_RTS;
3963 set_signals(info);
3964 info->drop_rts_on_tx_done = true;
3965 }
3966 }
3967
3968 slgt_irq_off(info, IRQ_TXDATA);
3969 slgt_irq_on(info, IRQ_TXUNDER + IRQ_TXIDLE);
3970 /* clear tx idle and underrun status bits */
3971 wr_reg16(info, SSR, (unsigned short)(IRQ_TXIDLE + IRQ_TXUNDER));
3972 } else {
3973 slgt_irq_off(info, IRQ_TXDATA);
3974 slgt_irq_on(info, IRQ_TXIDLE);
3975 /* clear tx idle status bit */
3976 wr_reg16(info, SSR, IRQ_TXIDLE);
3977 }
3978 /* set 1st descriptor address and start DMA */
3979 wr_reg32(info, TDDAR, info->tbufs[info->tbuf_start].pdesc);
3980 wr_reg32(info, TDCSR, BIT2 + BIT0);
3981 info->tx_active = true;
3982 }
3983}
3984
3985static void tx_stop(struct slgt_info *info)
3986{
3987 unsigned short val;
3988
3989 del_timer(&info->tx_timer);
3990
3991 tdma_reset(info);
3992
3993 /* reset and disable transmitter */
3994 val = rd_reg16(info, TCR) & ~BIT1; /* clear enable bit */
3995 wr_reg16(info, TCR, (unsigned short)(val | BIT2)); /* set reset bit */
3996
3997 slgt_irq_off(info, IRQ_TXDATA + IRQ_TXIDLE + IRQ_TXUNDER);
3998
3999 /* clear tx idle and underrun status bit */
4000 wr_reg16(info, SSR, (unsigned short)(IRQ_TXIDLE + IRQ_TXUNDER));
4001
4002 reset_tbufs(info);
4003
4004 info->tx_enabled = false;
4005 info->tx_active = false;
4006}
4007
4008static void reset_port(struct slgt_info *info)
4009{
4010 if (!info->reg_addr)
4011 return;
4012
4013 tx_stop(info);
4014 rx_stop(info);
4015
4016 info->signals &= ~(SerialSignal_DTR + SerialSignal_RTS);
4017 set_signals(info);
4018
4019 slgt_irq_off(info, IRQ_ALL | IRQ_MASTER);
4020}
4021
4022static void reset_adapter(struct slgt_info *info)
4023{
4024 int i;
4025 for (i=0; i < info->port_count; ++i) {
4026 if (info->port_array[i])
4027 reset_port(info->port_array[i]);
4028 }
4029}
4030
4031static void async_mode(struct slgt_info *info)
4032{
4033 unsigned short val;
4034
4035 slgt_irq_off(info, IRQ_ALL | IRQ_MASTER);
4036 tx_stop(info);
4037 rx_stop(info);
4038
4039 /* TCR (tx control)
4040 *
4041 * 15..13 mode, 010=async
4042 * 12..10 encoding, 000=NRZ
4043 * 09 parity enable
4044 * 08 1=odd parity, 0=even parity
4045 * 07 1=RTS driver control
4046 * 06 1=break enable
4047 * 05..04 character length
4048 * 00=5 bits
4049 * 01=6 bits
4050 * 10=7 bits
4051 * 11=8 bits
4052 * 03 0=1 stop bit, 1=2 stop bits
4053 * 02 reset
4054 * 01 enable
4055 * 00 auto-CTS enable
4056 */
4057 val = 0x4000;
4058
4059 if (info->if_mode & MGSL_INTERFACE_RTS_EN)
4060 val |= BIT7;
4061
4062 if (info->params.parity != ASYNC_PARITY_NONE) {
4063 val |= BIT9;
4064 if (info->params.parity == ASYNC_PARITY_ODD)
4065 val |= BIT8;
4066 }
4067
4068 switch (info->params.data_bits)
4069 {
4070 case 6: val |= BIT4; break;
4071 case 7: val |= BIT5; break;
4072 case 8: val |= BIT5 + BIT4; break;
4073 }
4074
4075 if (info->params.stop_bits != 1)
4076 val |= BIT3;
4077
4078 if (info->params.flags & HDLC_FLAG_AUTO_CTS)
4079 val |= BIT0;
4080
4081 wr_reg16(info, TCR, val);
4082
4083 /* RCR (rx control)
4084 *
4085 * 15..13 mode, 010=async
4086 * 12..10 encoding, 000=NRZ
4087 * 09 parity enable
4088 * 08 1=odd parity, 0=even parity
4089 * 07..06 reserved, must be 0
4090 * 05..04 character length
4091 * 00=5 bits
4092 * 01=6 bits
4093 * 10=7 bits
4094 * 11=8 bits
4095 * 03 reserved, must be zero
4096 * 02 reset
4097 * 01 enable
4098 * 00 auto-DCD enable
4099 */
4100 val = 0x4000;
4101
4102 if (info->params.parity != ASYNC_PARITY_NONE) {
4103 val |= BIT9;
4104 if (info->params.parity == ASYNC_PARITY_ODD)
4105 val |= BIT8;
4106 }
4107
4108 switch (info->params.data_bits)
4109 {
4110 case 6: val |= BIT4; break;
4111 case 7: val |= BIT5; break;
4112 case 8: val |= BIT5 + BIT4; break;
4113 }
4114
4115 if (info->params.flags & HDLC_FLAG_AUTO_DCD)
4116 val |= BIT0;
4117
4118 wr_reg16(info, RCR, val);
4119
4120 /* CCR (clock control)
4121 *
4122 * 07..05 011 = tx clock source is BRG/16
4123 * 04..02 010 = rx clock source is BRG
4124 * 01 0 = auxclk disabled
4125 * 00 1 = BRG enabled
4126 *
4127 * 0110 1001
4128 */
4129 wr_reg8(info, CCR, 0x69);
4130
4131 msc_set_vcr(info);
4132
4133 /* SCR (serial control)
4134 *
4135 * 15 1=tx req on FIFO half empty
4136 * 14 1=rx req on FIFO half full
4137 * 13 tx data IRQ enable
4138 * 12 tx idle IRQ enable
4139 * 11 rx break on IRQ enable
4140 * 10 rx data IRQ enable
4141 * 09 rx break off IRQ enable
4142 * 08 overrun IRQ enable
4143 * 07 DSR IRQ enable
4144 * 06 CTS IRQ enable
4145 * 05 DCD IRQ enable
4146 * 04 RI IRQ enable
4147 * 03 0=16x sampling, 1=8x sampling
4148 * 02 1=txd->rxd internal loopback enable
4149 * 01 reserved, must be zero
4150 * 00 1=master IRQ enable
4151 */
4152 val = BIT15 + BIT14 + BIT0;
4153 /* JCR[8] : 1 = x8 async mode feature available */
4154 if ((rd_reg32(info, JCR) & BIT8) && info->params.data_rate &&
4155 ((info->base_clock < (info->params.data_rate * 16)) ||
4156 (info->base_clock % (info->params.data_rate * 16)))) {
4157 /* use 8x sampling */
4158 val |= BIT3;
4159 set_rate(info, info->params.data_rate * 8);
4160 } else {
4161 /* use 16x sampling */
4162 set_rate(info, info->params.data_rate * 16);
4163 }
4164 wr_reg16(info, SCR, val);
4165
4166 slgt_irq_on(info, IRQ_RXBREAK | IRQ_RXOVER);
4167
4168 if (info->params.loopback)
4169 enable_loopback(info);
4170}
4171
4172static void sync_mode(struct slgt_info *info)
4173{
4174 unsigned short val;
4175
4176 slgt_irq_off(info, IRQ_ALL | IRQ_MASTER);
4177 tx_stop(info);
4178 rx_stop(info);
4179
4180 /* TCR (tx control)
4181 *
4182 * 15..13 mode, 000=HDLC 001=raw 010=async 011=monosync 100=bisync
4183 * 12..10 encoding
4184 * 09 CRC enable
4185 * 08 CRC32
4186 * 07 1=RTS driver control
4187 * 06 preamble enable
4188 * 05..04 preamble length
4189 * 03 share open/close flag
4190 * 02 reset
4191 * 01 enable
4192 * 00 auto-CTS enable
4193 */
4194 val = BIT2;
4195
4196 switch(info->params.mode) {
4197 case MGSL_MODE_MONOSYNC: val |= BIT14 + BIT13; break;
4198 case MGSL_MODE_BISYNC: val |= BIT15; break;
4199 case MGSL_MODE_RAW: val |= BIT13; break;
4200 }
4201 if (info->if_mode & MGSL_INTERFACE_RTS_EN)
4202 val |= BIT7;
4203
4204 switch(info->params.encoding)
4205 {
4206 case HDLC_ENCODING_NRZB: val |= BIT10; break;
4207 case HDLC_ENCODING_NRZI_MARK: val |= BIT11; break;
4208 case HDLC_ENCODING_NRZI: val |= BIT11 + BIT10; break;
4209 case HDLC_ENCODING_BIPHASE_MARK: val |= BIT12; break;
4210 case HDLC_ENCODING_BIPHASE_SPACE: val |= BIT12 + BIT10; break;
4211 case HDLC_ENCODING_BIPHASE_LEVEL: val |= BIT12 + BIT11; break;
4212 case HDLC_ENCODING_DIFF_BIPHASE_LEVEL: val |= BIT12 + BIT11 + BIT10; break;
4213 }
4214
4215 switch (info->params.crc_type & HDLC_CRC_MASK)
4216 {
4217 case HDLC_CRC_16_CCITT: val |= BIT9; break;
4218 case HDLC_CRC_32_CCITT: val |= BIT9 + BIT8; break;
4219 }
4220
4221 if (info->params.preamble != HDLC_PREAMBLE_PATTERN_NONE)
4222 val |= BIT6;
4223
4224 switch (info->params.preamble_length)
4225 {
4226 case HDLC_PREAMBLE_LENGTH_16BITS: val |= BIT5; break;
4227 case HDLC_PREAMBLE_LENGTH_32BITS: val |= BIT4; break;
4228 case HDLC_PREAMBLE_LENGTH_64BITS: val |= BIT5 + BIT4; break;
4229 }
4230
4231 if (info->params.flags & HDLC_FLAG_AUTO_CTS)
4232 val |= BIT0;
4233
4234 wr_reg16(info, TCR, val);
4235
4236 /* TPR (transmit preamble) */
4237
4238 switch (info->params.preamble)
4239 {
4240 case HDLC_PREAMBLE_PATTERN_FLAGS: val = 0x7e; break;
4241 case HDLC_PREAMBLE_PATTERN_ONES: val = 0xff; break;
4242 case HDLC_PREAMBLE_PATTERN_ZEROS: val = 0x00; break;
4243 case HDLC_PREAMBLE_PATTERN_10: val = 0x55; break;
4244 case HDLC_PREAMBLE_PATTERN_01: val = 0xaa; break;
4245 default: val = 0x7e; break;
4246 }
4247 wr_reg8(info, TPR, (unsigned char)val);
4248
4249 /* RCR (rx control)
4250 *
4251 * 15..13 mode, 000=HDLC 001=raw 010=async 011=monosync 100=bisync
4252 * 12..10 encoding
4253 * 09 CRC enable
4254 * 08 CRC32
4255 * 07..03 reserved, must be 0
4256 * 02 reset
4257 * 01 enable
4258 * 00 auto-DCD enable
4259 */
4260 val = 0;
4261
4262 switch(info->params.mode) {
4263 case MGSL_MODE_MONOSYNC: val |= BIT14 + BIT13; break;
4264 case MGSL_MODE_BISYNC: val |= BIT15; break;
4265 case MGSL_MODE_RAW: val |= BIT13; break;
4266 }
4267
4268 switch(info->params.encoding)
4269 {
4270 case HDLC_ENCODING_NRZB: val |= BIT10; break;
4271 case HDLC_ENCODING_NRZI_MARK: val |= BIT11; break;
4272 case HDLC_ENCODING_NRZI: val |= BIT11 + BIT10; break;
4273 case HDLC_ENCODING_BIPHASE_MARK: val |= BIT12; break;
4274 case HDLC_ENCODING_BIPHASE_SPACE: val |= BIT12 + BIT10; break;
4275 case HDLC_ENCODING_BIPHASE_LEVEL: val |= BIT12 + BIT11; break;
4276 case HDLC_ENCODING_DIFF_BIPHASE_LEVEL: val |= BIT12 + BIT11 + BIT10; break;
4277 }
4278
4279 switch (info->params.crc_type & HDLC_CRC_MASK)
4280 {
4281 case HDLC_CRC_16_CCITT: val |= BIT9; break;
4282 case HDLC_CRC_32_CCITT: val |= BIT9 + BIT8; break;
4283 }
4284
4285 if (info->params.flags & HDLC_FLAG_AUTO_DCD)
4286 val |= BIT0;
4287
4288 wr_reg16(info, RCR, val);
4289
4290 /* CCR (clock control)
4291 *
4292 * 07..05 tx clock source
4293 * 04..02 rx clock source
4294 * 01 auxclk enable
4295 * 00 BRG enable
4296 */
4297 val = 0;
4298
4299 if (info->params.flags & HDLC_FLAG_TXC_BRG)
4300 {
4301 // when RxC source is DPLL, BRG generates 16X DPLL
4302 // reference clock, so take TxC from BRG/16 to get
4303 // transmit clock at actual data rate
4304 if (info->params.flags & HDLC_FLAG_RXC_DPLL)
4305 val |= BIT6 + BIT5; /* 011, txclk = BRG/16 */
4306 else
4307 val |= BIT6; /* 010, txclk = BRG */
4308 }
4309 else if (info->params.flags & HDLC_FLAG_TXC_DPLL)
4310 val |= BIT7; /* 100, txclk = DPLL Input */
4311 else if (info->params.flags & HDLC_FLAG_TXC_RXCPIN)
4312 val |= BIT5; /* 001, txclk = RXC Input */
4313
4314 if (info->params.flags & HDLC_FLAG_RXC_BRG)
4315 val |= BIT3; /* 010, rxclk = BRG */
4316 else if (info->params.flags & HDLC_FLAG_RXC_DPLL)
4317 val |= BIT4; /* 100, rxclk = DPLL */
4318 else if (info->params.flags & HDLC_FLAG_RXC_TXCPIN)
4319 val |= BIT2; /* 001, rxclk = TXC Input */
4320
4321 if (info->params.clock_speed)
4322 val |= BIT1 + BIT0;
4323
4324 wr_reg8(info, CCR, (unsigned char)val);
4325
4326 if (info->params.flags & (HDLC_FLAG_TXC_DPLL + HDLC_FLAG_RXC_DPLL))
4327 {
4328 // program DPLL mode
4329 switch(info->params.encoding)
4330 {
4331 case HDLC_ENCODING_BIPHASE_MARK:
4332 case HDLC_ENCODING_BIPHASE_SPACE:
4333 val = BIT7; break;
4334 case HDLC_ENCODING_BIPHASE_LEVEL:
4335 case HDLC_ENCODING_DIFF_BIPHASE_LEVEL:
4336 val = BIT7 + BIT6; break;
4337 default: val = BIT6; // NRZ encodings
4338 }
4339 wr_reg16(info, RCR, (unsigned short)(rd_reg16(info, RCR) | val));
4340
4341 // DPLL requires a 16X reference clock from BRG
4342 set_rate(info, info->params.clock_speed * 16);
4343 }
4344 else
4345 set_rate(info, info->params.clock_speed);
4346
4347 tx_set_idle(info);
4348
4349 msc_set_vcr(info);
4350
4351 /* SCR (serial control)
4352 *
4353 * 15 1=tx req on FIFO half empty
4354 * 14 1=rx req on FIFO half full
4355 * 13 tx data IRQ enable
4356 * 12 tx idle IRQ enable
4357 * 11 underrun IRQ enable
4358 * 10 rx data IRQ enable
4359 * 09 rx idle IRQ enable
4360 * 08 overrun IRQ enable
4361 * 07 DSR IRQ enable
4362 * 06 CTS IRQ enable
4363 * 05 DCD IRQ enable
4364 * 04 RI IRQ enable
4365 * 03 reserved, must be zero
4366 * 02 1=txd->rxd internal loopback enable
4367 * 01 reserved, must be zero
4368 * 00 1=master IRQ enable
4369 */
4370 wr_reg16(info, SCR, BIT15 + BIT14 + BIT0);
4371
4372 if (info->params.loopback)
4373 enable_loopback(info);
4374}
4375
4376/*
4377 * set transmit idle mode
4378 */
4379static void tx_set_idle(struct slgt_info *info)
4380{
4381 unsigned char val;
4382 unsigned short tcr;
4383
4384 /* if preamble enabled (tcr[6] == 1) then tx idle size = 8 bits
4385 * else tcr[5:4] = tx idle size: 00 = 8 bits, 01 = 16 bits
4386 */
4387 tcr = rd_reg16(info, TCR);
4388 if (info->idle_mode & HDLC_TXIDLE_CUSTOM_16) {
4389 /* disable preamble, set idle size to 16 bits */
4390 tcr = (tcr & ~(BIT6 + BIT5)) | BIT4;
4391 /* MSB of 16 bit idle specified in tx preamble register (TPR) */
4392 wr_reg8(info, TPR, (unsigned char)((info->idle_mode >> 8) & 0xff));
4393 } else if (!(tcr & BIT6)) {
4394 /* preamble is disabled, set idle size to 8 bits */
4395 tcr &= ~(BIT5 + BIT4);
4396 }
4397 wr_reg16(info, TCR, tcr);
4398
4399 if (info->idle_mode & (HDLC_TXIDLE_CUSTOM_8 | HDLC_TXIDLE_CUSTOM_16)) {
4400 /* LSB of custom tx idle specified in tx idle register */
4401 val = (unsigned char)(info->idle_mode & 0xff);
4402 } else {
4403 /* standard 8 bit idle patterns */
4404 switch(info->idle_mode)
4405 {
4406 case HDLC_TXIDLE_FLAGS: val = 0x7e; break;
4407 case HDLC_TXIDLE_ALT_ZEROS_ONES:
4408 case HDLC_TXIDLE_ALT_MARK_SPACE: val = 0xaa; break;
4409 case HDLC_TXIDLE_ZEROS:
4410 case HDLC_TXIDLE_SPACE: val = 0x00; break;
4411 default: val = 0xff;
4412 }
4413 }
4414
4415 wr_reg8(info, TIR, val);
4416}
4417
4418/*
4419 * get state of V24 status (input) signals
4420 */
4421static void get_signals(struct slgt_info *info)
4422{
4423 unsigned short status = rd_reg16(info, SSR);
4424
4425 /* clear all serial signals except DTR and RTS */
4426 info->signals &= SerialSignal_DTR + SerialSignal_RTS;
4427
4428 if (status & BIT3)
4429 info->signals |= SerialSignal_DSR;
4430 if (status & BIT2)
4431 info->signals |= SerialSignal_CTS;
4432 if (status & BIT1)
4433 info->signals |= SerialSignal_DCD;
4434 if (status & BIT0)
4435 info->signals |= SerialSignal_RI;
4436}
4437
4438/*
4439 * set V.24 Control Register based on current configuration
4440 */
4441static void msc_set_vcr(struct slgt_info *info)
4442{
4443 unsigned char val = 0;
4444
4445 /* VCR (V.24 control)
4446 *
4447 * 07..04 serial IF select
4448 * 03 DTR
4449 * 02 RTS
4450 * 01 LL
4451 * 00 RL
4452 */
4453
4454 switch(info->if_mode & MGSL_INTERFACE_MASK)
4455 {
4456 case MGSL_INTERFACE_RS232:
4457 val |= BIT5; /* 0010 */
4458 break;
4459 case MGSL_INTERFACE_V35:
4460 val |= BIT7 + BIT6 + BIT5; /* 1110 */
4461 break;
4462 case MGSL_INTERFACE_RS422:
4463 val |= BIT6; /* 0100 */
4464 break;
4465 }
4466
4467 if (info->if_mode & MGSL_INTERFACE_MSB_FIRST)
4468 val |= BIT4;
4469 if (info->signals & SerialSignal_DTR)
4470 val |= BIT3;
4471 if (info->signals & SerialSignal_RTS)
4472 val |= BIT2;
4473 if (info->if_mode & MGSL_INTERFACE_LL)
4474 val |= BIT1;
4475 if (info->if_mode & MGSL_INTERFACE_RL)
4476 val |= BIT0;
4477 wr_reg8(info, VCR, val);
4478}
4479
4480/*
4481 * set state of V24 control (output) signals
4482 */
4483static void set_signals(struct slgt_info *info)
4484{
4485 unsigned char val = rd_reg8(info, VCR);
4486 if (info->signals & SerialSignal_DTR)
4487 val |= BIT3;
4488 else
4489 val &= ~BIT3;
4490 if (info->signals & SerialSignal_RTS)
4491 val |= BIT2;
4492 else
4493 val &= ~BIT2;
4494 wr_reg8(info, VCR, val);
4495}
4496
4497/*
4498 * free range of receive DMA buffers (i to last)
4499 */
4500static void free_rbufs(struct slgt_info *info, unsigned int i, unsigned int last)
4501{
4502 int done = 0;
4503
4504 while(!done) {
4505 /* reset current buffer for reuse */
4506 info->rbufs[i].status = 0;
4507 set_desc_count(info->rbufs[i], info->rbuf_fill_level);
4508 if (i == last)
4509 done = 1;
4510 if (++i == info->rbuf_count)
4511 i = 0;
4512 }
4513 info->rbuf_current = i;
4514}
4515
4516/*
4517 * mark all receive DMA buffers as free
4518 */
4519static void reset_rbufs(struct slgt_info *info)
4520{
4521 free_rbufs(info, 0, info->rbuf_count - 1);
4522 info->rbuf_fill_index = 0;
4523 info->rbuf_fill_count = 0;
4524}
4525
4526/*
4527 * pass receive HDLC frame to upper layer
4528 *
4529 * return true if frame available, otherwise false
4530 */
4531static bool rx_get_frame(struct slgt_info *info)
4532{
4533 unsigned int start, end;
4534 unsigned short status;
4535 unsigned int framesize = 0;
4536 unsigned long flags;
4537 struct tty_struct *tty = info->port.tty;
4538 unsigned char addr_field = 0xff;
4539 unsigned int crc_size = 0;
4540
4541 switch (info->params.crc_type & HDLC_CRC_MASK) {
4542 case HDLC_CRC_16_CCITT: crc_size = 2; break;
4543 case HDLC_CRC_32_CCITT: crc_size = 4; break;
4544 }
4545
4546check_again:
4547
4548 framesize = 0;
4549 addr_field = 0xff;
4550 start = end = info->rbuf_current;
4551
4552 for (;;) {
4553 if (!desc_complete(info->rbufs[end]))
4554 goto cleanup;
4555
4556 if (framesize == 0 && info->params.addr_filter != 0xff)
4557 addr_field = info->rbufs[end].buf[0];
4558
4559 framesize += desc_count(info->rbufs[end]);
4560
4561 if (desc_eof(info->rbufs[end]))
4562 break;
4563
4564 if (++end == info->rbuf_count)
4565 end = 0;
4566
4567 if (end == info->rbuf_current) {
4568 if (info->rx_enabled){
4569 spin_lock_irqsave(&info->lock,flags);
4570 rx_start(info);
4571 spin_unlock_irqrestore(&info->lock,flags);
4572 }
4573 goto cleanup;
4574 }
4575 }
4576
4577 /* status
4578 *
4579 * 15 buffer complete
4580 * 14..06 reserved
4581 * 05..04 residue
4582 * 02 eof (end of frame)
4583 * 01 CRC error
4584 * 00 abort
4585 */
4586 status = desc_status(info->rbufs[end]);
4587
4588 /* ignore CRC bit if not using CRC (bit is undefined) */
4589 if ((info->params.crc_type & HDLC_CRC_MASK) == HDLC_CRC_NONE)
4590 status &= ~BIT1;
4591
4592 if (framesize == 0 ||
4593 (addr_field != 0xff && addr_field != info->params.addr_filter)) {
4594 free_rbufs(info, start, end);
4595 goto check_again;
4596 }
4597
4598 if (framesize < (2 + crc_size) || status & BIT0) {
4599 info->icount.rxshort++;
4600 framesize = 0;
4601 } else if (status & BIT1) {
4602 info->icount.rxcrc++;
4603 if (!(info->params.crc_type & HDLC_CRC_RETURN_EX))
4604 framesize = 0;
4605 }
4606
4607#if SYNCLINK_GENERIC_HDLC
4608 if (framesize == 0) {
4609 info->netdev->stats.rx_errors++;
4610 info->netdev->stats.rx_frame_errors++;
4611 }
4612#endif
4613
4614 DBGBH(("%s rx frame status=%04X size=%d\n",
4615 info->device_name, status, framesize));
4616 DBGDATA(info, info->rbufs[start].buf, min_t(int, framesize, info->rbuf_fill_level), "rx");
4617
4618 if (framesize) {
4619 if (!(info->params.crc_type & HDLC_CRC_RETURN_EX)) {
4620 framesize -= crc_size;
4621 crc_size = 0;
4622 }
4623
4624 if (framesize > info->max_frame_size + crc_size)
4625 info->icount.rxlong++;
4626 else {
4627 /* copy dma buffer(s) to contiguous temp buffer */
4628 int copy_count = framesize;
4629 int i = start;
4630 unsigned char *p = info->tmp_rbuf;
4631 info->tmp_rbuf_count = framesize;
4632
4633 info->icount.rxok++;
4634
4635 while(copy_count) {
4636 int partial_count = min_t(int, copy_count, info->rbuf_fill_level);
4637 memcpy(p, info->rbufs[i].buf, partial_count);
4638 p += partial_count;
4639 copy_count -= partial_count;
4640 if (++i == info->rbuf_count)
4641 i = 0;
4642 }
4643
4644 if (info->params.crc_type & HDLC_CRC_RETURN_EX) {
4645 *p = (status & BIT1) ? RX_CRC_ERROR : RX_OK;
4646 framesize++;
4647 }
4648
4649#if SYNCLINK_GENERIC_HDLC
4650 if (info->netcount)
4651 hdlcdev_rx(info,info->tmp_rbuf, framesize);
4652 else
4653#endif
4654 ldisc_receive_buf(tty, info->tmp_rbuf, info->flag_buf, framesize);
4655 }
4656 }
4657 free_rbufs(info, start, end);
4658 return true;
4659
4660cleanup:
4661 return false;
4662}
4663
4664/*
4665 * pass receive buffer (RAW synchronous mode) to tty layer
4666 * return true if buffer available, otherwise false
4667 */
4668static bool rx_get_buf(struct slgt_info *info)
4669{
4670 unsigned int i = info->rbuf_current;
4671 unsigned int count;
4672
4673 if (!desc_complete(info->rbufs[i]))
4674 return false;
4675 count = desc_count(info->rbufs[i]);
4676 switch(info->params.mode) {
4677 case MGSL_MODE_MONOSYNC:
4678 case MGSL_MODE_BISYNC:
4679 /* ignore residue in byte synchronous modes */
4680 if (desc_residue(info->rbufs[i]))
4681 count--;
4682 break;
4683 }
4684 DBGDATA(info, info->rbufs[i].buf, count, "rx");
4685 DBGINFO(("rx_get_buf size=%d\n", count));
4686 if (count)
4687 ldisc_receive_buf(info->port.tty, info->rbufs[i].buf,
4688 info->flag_buf, count);
4689 free_rbufs(info, i, i);
4690 return true;
4691}
4692
4693static void reset_tbufs(struct slgt_info *info)
4694{
4695 unsigned int i;
4696 info->tbuf_current = 0;
4697 for (i=0 ; i < info->tbuf_count ; i++) {
4698 info->tbufs[i].status = 0;
4699 info->tbufs[i].count = 0;
4700 }
4701}
4702
4703/*
4704 * return number of free transmit DMA buffers
4705 */
4706static unsigned int free_tbuf_count(struct slgt_info *info)
4707{
4708 unsigned int count = 0;
4709 unsigned int i = info->tbuf_current;
4710
4711 do
4712 {
4713 if (desc_count(info->tbufs[i]))
4714 break; /* buffer in use */
4715 ++count;
4716 if (++i == info->tbuf_count)
4717 i=0;
4718 } while (i != info->tbuf_current);
4719
4720 /* if tx DMA active, last zero count buffer is in use */
4721 if (count && (rd_reg32(info, TDCSR) & BIT0))
4722 --count;
4723
4724 return count;
4725}
4726
4727/*
4728 * return number of bytes in unsent transmit DMA buffers
4729 * and the serial controller tx FIFO
4730 */
4731static unsigned int tbuf_bytes(struct slgt_info *info)
4732{
4733 unsigned int total_count = 0;
4734 unsigned int i = info->tbuf_current;
4735 unsigned int reg_value;
4736 unsigned int count;
4737 unsigned int active_buf_count = 0;
4738
4739 /*
4740 * Add descriptor counts for all tx DMA buffers.
4741 * If count is zero (cleared by DMA controller after read),
4742 * the buffer is complete or is actively being read from.
4743 *
4744 * Record buf_count of last buffer with zero count starting
4745 * from current ring position. buf_count is mirror
4746 * copy of count and is not cleared by serial controller.
4747 * If DMA controller is active, that buffer is actively
4748 * being read so add to total.
4749 */
4750 do {
4751 count = desc_count(info->tbufs[i]);
4752 if (count)
4753 total_count += count;
4754 else if (!total_count)
4755 active_buf_count = info->tbufs[i].buf_count;
4756 if (++i == info->tbuf_count)
4757 i = 0;
4758 } while (i != info->tbuf_current);
4759
4760 /* read tx DMA status register */
4761 reg_value = rd_reg32(info, TDCSR);
4762
4763 /* if tx DMA active, last zero count buffer is in use */
4764 if (reg_value & BIT0)
4765 total_count += active_buf_count;
4766
4767 /* add tx FIFO count = reg_value[15..8] */
4768 total_count += (reg_value >> 8) & 0xff;
4769
4770 /* if transmitter active add one byte for shift register */
4771 if (info->tx_active)
4772 total_count++;
4773
4774 return total_count;
4775}
4776
4777/*
4778 * load data into transmit DMA buffer ring and start transmitter if needed
4779 * return true if data accepted, otherwise false (buffers full)
4780 */
4781static bool tx_load(struct slgt_info *info, const char *buf, unsigned int size)
4782{
4783 unsigned short count;
4784 unsigned int i;
4785 struct slgt_desc *d;
4786
4787 /* check required buffer space */
4788 if (DIV_ROUND_UP(size, DMABUFSIZE) > free_tbuf_count(info))
4789 return false;
4790
4791 DBGDATA(info, buf, size, "tx");
4792
4793 /*
4794 * copy data to one or more DMA buffers in circular ring
4795 * tbuf_start = first buffer for this data
4796 * tbuf_current = next free buffer
4797 *
4798 * Copy all data before making data visible to DMA controller by
4799 * setting descriptor count of the first buffer.
4800 * This prevents an active DMA controller from reading the first DMA
4801 * buffers of a frame and stopping before the final buffers are filled.
4802 */
4803
4804 info->tbuf_start = i = info->tbuf_current;
4805
4806 while (size) {
4807 d = &info->tbufs[i];
4808
4809 count = (unsigned short)((size > DMABUFSIZE) ? DMABUFSIZE : size);
4810 memcpy(d->buf, buf, count);
4811
4812 size -= count;
4813 buf += count;
4814
4815 /*
4816 * set EOF bit for last buffer of HDLC frame or
4817 * for every buffer in raw mode
4818 */
4819 if ((!size && info->params.mode == MGSL_MODE_HDLC) ||
4820 info->params.mode == MGSL_MODE_RAW)
4821 set_desc_eof(*d, 1);
4822 else
4823 set_desc_eof(*d, 0);
4824
4825 /* set descriptor count for all but first buffer */
4826 if (i != info->tbuf_start)
4827 set_desc_count(*d, count);
4828 d->buf_count = count;
4829
4830 if (++i == info->tbuf_count)
4831 i = 0;
4832 }
4833
4834 info->tbuf_current = i;
4835
4836 /* set first buffer count to make new data visible to DMA controller */
4837 d = &info->tbufs[info->tbuf_start];
4838 set_desc_count(*d, d->buf_count);
4839
4840 /* start transmitter if needed and update transmit timeout */
4841 if (!info->tx_active)
4842 tx_start(info);
4843 update_tx_timer(info);
4844
4845 return true;
4846}
4847
4848static int register_test(struct slgt_info *info)
4849{
4850 static unsigned short patterns[] =
4851 {0x0000, 0xffff, 0xaaaa, 0x5555, 0x6969, 0x9696};
4852 static unsigned int count = ARRAY_SIZE(patterns);
4853 unsigned int i;
4854 int rc = 0;
4855
4856 for (i=0 ; i < count ; i++) {
4857 wr_reg16(info, TIR, patterns[i]);
4858 wr_reg16(info, BDR, patterns[(i+1)%count]);
4859 if ((rd_reg16(info, TIR) != patterns[i]) ||
4860 (rd_reg16(info, BDR) != patterns[(i+1)%count])) {
4861 rc = -ENODEV;
4862 break;
4863 }
4864 }
4865 info->gpio_present = (rd_reg32(info, JCR) & BIT5) ? 1 : 0;
4866 info->init_error = rc ? 0 : DiagStatus_AddressFailure;
4867 return rc;
4868}
4869
4870static int irq_test(struct slgt_info *info)
4871{
4872 unsigned long timeout;
4873 unsigned long flags;
4874 struct tty_struct *oldtty = info->port.tty;
4875 u32 speed = info->params.data_rate;
4876
4877 info->params.data_rate = 921600;
4878 info->port.tty = NULL;
4879
4880 spin_lock_irqsave(&info->lock, flags);
4881 async_mode(info);
4882 slgt_irq_on(info, IRQ_TXIDLE);
4883
4884 /* enable transmitter */
4885 wr_reg16(info, TCR,
4886 (unsigned short)(rd_reg16(info, TCR) | BIT1));
4887
4888 /* write one byte and wait for tx idle */
4889 wr_reg16(info, TDR, 0);
4890
4891 /* assume failure */
4892 info->init_error = DiagStatus_IrqFailure;
4893 info->irq_occurred = false;
4894
4895 spin_unlock_irqrestore(&info->lock, flags);
4896
4897 timeout=100;
4898 while(timeout-- && !info->irq_occurred)
4899 msleep_interruptible(10);
4900
4901 spin_lock_irqsave(&info->lock,flags);
4902 reset_port(info);
4903 spin_unlock_irqrestore(&info->lock,flags);
4904
4905 info->params.data_rate = speed;
4906 info->port.tty = oldtty;
4907
4908 info->init_error = info->irq_occurred ? 0 : DiagStatus_IrqFailure;
4909 return info->irq_occurred ? 0 : -ENODEV;
4910}
4911
4912static int loopback_test_rx(struct slgt_info *info)
4913{
4914 unsigned char *src, *dest;
4915 int count;
4916
4917 if (desc_complete(info->rbufs[0])) {
4918 count = desc_count(info->rbufs[0]);
4919 src = info->rbufs[0].buf;
4920 dest = info->tmp_rbuf;
4921
4922 for( ; count ; count-=2, src+=2) {
4923 /* src=data byte (src+1)=status byte */
4924 if (!(*(src+1) & (BIT9 + BIT8))) {
4925 *dest = *src;
4926 dest++;
4927 info->tmp_rbuf_count++;
4928 }
4929 }
4930 DBGDATA(info, info->tmp_rbuf, info->tmp_rbuf_count, "rx");
4931 return 1;
4932 }
4933 return 0;
4934}
4935
4936static int loopback_test(struct slgt_info *info)
4937{
4938#define TESTFRAMESIZE 20
4939
4940 unsigned long timeout;
4941 u16 count = TESTFRAMESIZE;
4942 unsigned char buf[TESTFRAMESIZE];
4943 int rc = -ENODEV;
4944 unsigned long flags;
4945
4946 struct tty_struct *oldtty = info->port.tty;
4947 MGSL_PARAMS params;
4948
4949 memcpy(&params, &info->params, sizeof(params));
4950
4951 info->params.mode = MGSL_MODE_ASYNC;
4952 info->params.data_rate = 921600;
4953 info->params.loopback = 1;
4954 info->port.tty = NULL;
4955
4956 /* build and send transmit frame */
4957 for (count = 0; count < TESTFRAMESIZE; ++count)
4958 buf[count] = (unsigned char)count;
4959
4960 info->tmp_rbuf_count = 0;
4961 memset(info->tmp_rbuf, 0, TESTFRAMESIZE);
4962
4963 /* program hardware for HDLC and enabled receiver */
4964 spin_lock_irqsave(&info->lock,flags);
4965 async_mode(info);
4966 rx_start(info);
4967 tx_load(info, buf, count);
4968 spin_unlock_irqrestore(&info->lock, flags);
4969
4970 /* wait for receive complete */
4971 for (timeout = 100; timeout; --timeout) {
4972 msleep_interruptible(10);
4973 if (loopback_test_rx(info)) {
4974 rc = 0;
4975 break;
4976 }
4977 }
4978
4979 /* verify received frame length and contents */
4980 if (!rc && (info->tmp_rbuf_count != count ||
4981 memcmp(buf, info->tmp_rbuf, count))) {
4982 rc = -ENODEV;
4983 }
4984
4985 spin_lock_irqsave(&info->lock,flags);
4986 reset_adapter(info);
4987 spin_unlock_irqrestore(&info->lock,flags);
4988
4989 memcpy(&info->params, &params, sizeof(info->params));
4990 info->port.tty = oldtty;
4991
4992 info->init_error = rc ? DiagStatus_DmaFailure : 0;
4993 return rc;
4994}
4995
4996static int adapter_test(struct slgt_info *info)
4997{
4998 DBGINFO(("testing %s\n", info->device_name));
4999 if (register_test(info) < 0) {
5000 printk("register test failure %s addr=%08X\n",
5001 info->device_name, info->phys_reg_addr);
5002 } else if (irq_test(info) < 0) {
5003 printk("IRQ test failure %s IRQ=%d\n",
5004 info->device_name, info->irq_level);
5005 } else if (loopback_test(info) < 0) {
5006 printk("loopback test failure %s\n", info->device_name);
5007 }
5008 return info->init_error;
5009}
5010
5011/*
5012 * transmit timeout handler
5013 */
5014static void tx_timeout(unsigned long context)
5015{
5016 struct slgt_info *info = (struct slgt_info*)context;
5017 unsigned long flags;
5018
5019 DBGINFO(("%s tx_timeout\n", info->device_name));
5020 if(info->tx_active && info->params.mode == MGSL_MODE_HDLC) {
5021 info->icount.txtimeout++;
5022 }
5023 spin_lock_irqsave(&info->lock,flags);
5024 tx_stop(info);
5025 spin_unlock_irqrestore(&info->lock,flags);
5026
5027#if SYNCLINK_GENERIC_HDLC
5028 if (info->netcount)
5029 hdlcdev_tx_done(info);
5030 else
5031#endif
5032 bh_transmit(info);
5033}
5034
5035/*
5036 * receive buffer polling timer
5037 */
5038static void rx_timeout(unsigned long context)
5039{
5040 struct slgt_info *info = (struct slgt_info*)context;
5041 unsigned long flags;
5042
5043 DBGINFO(("%s rx_timeout\n", info->device_name));
5044 spin_lock_irqsave(&info->lock, flags);
5045 info->pending_bh |= BH_RECEIVE;
5046 spin_unlock_irqrestore(&info->lock, flags);
5047 bh_handler(&info->task);
5048}
5049
diff --git a/drivers/char/synclinkmp.c b/drivers/char/synclinkmp.c
deleted file mode 100644
index e56caf7d82aa..000000000000
--- a/drivers/char/synclinkmp.c
+++ /dev/null
@@ -1,5606 +0,0 @@
1/*
2 * $Id: synclinkmp.c,v 4.38 2005/07/15 13:29:44 paulkf Exp $
3 *
4 * Device driver for Microgate SyncLink Multiport
5 * high speed multiprotocol serial adapter.
6 *
7 * written by Paul Fulghum for Microgate Corporation
8 * paulkf@microgate.com
9 *
10 * Microgate and SyncLink are trademarks of Microgate Corporation
11 *
12 * Derived from serial.c written by Theodore Ts'o and Linus Torvalds
13 * This code is released under the GNU General Public License (GPL)
14 *
15 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
16 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
19 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
23 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
25 * OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#define VERSION(ver,rel,seq) (((ver)<<16) | ((rel)<<8) | (seq))
29#if defined(__i386__)
30# define BREAKPOINT() asm(" int $3");
31#else
32# define BREAKPOINT() { }
33#endif
34
35#define MAX_DEVICES 12
36
37#include <linux/module.h>
38#include <linux/errno.h>
39#include <linux/signal.h>
40#include <linux/sched.h>
41#include <linux/timer.h>
42#include <linux/interrupt.h>
43#include <linux/pci.h>
44#include <linux/tty.h>
45#include <linux/tty_flip.h>
46#include <linux/serial.h>
47#include <linux/major.h>
48#include <linux/string.h>
49#include <linux/fcntl.h>
50#include <linux/ptrace.h>
51#include <linux/ioport.h>
52#include <linux/mm.h>
53#include <linux/seq_file.h>
54#include <linux/slab.h>
55#include <linux/netdevice.h>
56#include <linux/vmalloc.h>
57#include <linux/init.h>
58#include <linux/delay.h>
59#include <linux/ioctl.h>
60
61#include <asm/system.h>
62#include <asm/io.h>
63#include <asm/irq.h>
64#include <asm/dma.h>
65#include <linux/bitops.h>
66#include <asm/types.h>
67#include <linux/termios.h>
68#include <linux/workqueue.h>
69#include <linux/hdlc.h>
70#include <linux/synclink.h>
71
72#if defined(CONFIG_HDLC) || (defined(CONFIG_HDLC_MODULE) && defined(CONFIG_SYNCLINKMP_MODULE))
73#define SYNCLINK_GENERIC_HDLC 1
74#else
75#define SYNCLINK_GENERIC_HDLC 0
76#endif
77
78#define GET_USER(error,value,addr) error = get_user(value,addr)
79#define COPY_FROM_USER(error,dest,src,size) error = copy_from_user(dest,src,size) ? -EFAULT : 0
80#define PUT_USER(error,value,addr) error = put_user(value,addr)
81#define COPY_TO_USER(error,dest,src,size) error = copy_to_user(dest,src,size) ? -EFAULT : 0
82
83#include <asm/uaccess.h>
84
85static MGSL_PARAMS default_params = {
86 MGSL_MODE_HDLC, /* unsigned long mode */
87 0, /* unsigned char loopback; */
88 HDLC_FLAG_UNDERRUN_ABORT15, /* unsigned short flags; */
89 HDLC_ENCODING_NRZI_SPACE, /* unsigned char encoding; */
90 0, /* unsigned long clock_speed; */
91 0xff, /* unsigned char addr_filter; */
92 HDLC_CRC_16_CCITT, /* unsigned short crc_type; */
93 HDLC_PREAMBLE_LENGTH_8BITS, /* unsigned char preamble_length; */
94 HDLC_PREAMBLE_PATTERN_NONE, /* unsigned char preamble; */
95 9600, /* unsigned long data_rate; */
96 8, /* unsigned char data_bits; */
97 1, /* unsigned char stop_bits; */
98 ASYNC_PARITY_NONE /* unsigned char parity; */
99};
100
101/* size in bytes of DMA data buffers */
102#define SCABUFSIZE 1024
103#define SCA_MEM_SIZE 0x40000
104#define SCA_BASE_SIZE 512
105#define SCA_REG_SIZE 16
106#define SCA_MAX_PORTS 4
107#define SCAMAXDESC 128
108
109#define BUFFERLISTSIZE 4096
110
111/* SCA-I style DMA buffer descriptor */
112typedef struct _SCADESC
113{
114 u16 next; /* lower l6 bits of next descriptor addr */
115 u16 buf_ptr; /* lower 16 bits of buffer addr */
116 u8 buf_base; /* upper 8 bits of buffer addr */
117 u8 pad1;
118 u16 length; /* length of buffer */
119 u8 status; /* status of buffer */
120 u8 pad2;
121} SCADESC, *PSCADESC;
122
123typedef struct _SCADESC_EX
124{
125 /* device driver bookkeeping section */
126 char *virt_addr; /* virtual address of data buffer */
127 u16 phys_entry; /* lower 16-bits of physical address of this descriptor */
128} SCADESC_EX, *PSCADESC_EX;
129
130/* The queue of BH actions to be performed */
131
132#define BH_RECEIVE 1
133#define BH_TRANSMIT 2
134#define BH_STATUS 4
135
136#define IO_PIN_SHUTDOWN_LIMIT 100
137
138struct _input_signal_events {
139 int ri_up;
140 int ri_down;
141 int dsr_up;
142 int dsr_down;
143 int dcd_up;
144 int dcd_down;
145 int cts_up;
146 int cts_down;
147};
148
149/*
150 * Device instance data structure
151 */
152typedef struct _synclinkmp_info {
153 void *if_ptr; /* General purpose pointer (used by SPPP) */
154 int magic;
155 struct tty_port port;
156 int line;
157 unsigned short close_delay;
158 unsigned short closing_wait; /* time to wait before closing */
159
160 struct mgsl_icount icount;
161
162 int timeout;
163 int x_char; /* xon/xoff character */
164 u16 read_status_mask1; /* break detection (SR1 indications) */
165 u16 read_status_mask2; /* parity/framing/overun (SR2 indications) */
166 unsigned char ignore_status_mask1; /* break detection (SR1 indications) */
167 unsigned char ignore_status_mask2; /* parity/framing/overun (SR2 indications) */
168 unsigned char *tx_buf;
169 int tx_put;
170 int tx_get;
171 int tx_count;
172
173 wait_queue_head_t status_event_wait_q;
174 wait_queue_head_t event_wait_q;
175 struct timer_list tx_timer; /* HDLC transmit timeout timer */
176 struct _synclinkmp_info *next_device; /* device list link */
177 struct timer_list status_timer; /* input signal status check timer */
178
179 spinlock_t lock; /* spinlock for synchronizing with ISR */
180 struct work_struct task; /* task structure for scheduling bh */
181
182 u32 max_frame_size; /* as set by device config */
183
184 u32 pending_bh;
185
186 bool bh_running; /* Protection from multiple */
187 int isr_overflow;
188 bool bh_requested;
189
190 int dcd_chkcount; /* check counts to prevent */
191 int cts_chkcount; /* too many IRQs if a signal */
192 int dsr_chkcount; /* is floating */
193 int ri_chkcount;
194
195 char *buffer_list; /* virtual address of Rx & Tx buffer lists */
196 unsigned long buffer_list_phys;
197
198 unsigned int rx_buf_count; /* count of total allocated Rx buffers */
199 SCADESC *rx_buf_list; /* list of receive buffer entries */
200 SCADESC_EX rx_buf_list_ex[SCAMAXDESC]; /* list of receive buffer entries */
201 unsigned int current_rx_buf;
202
203 unsigned int tx_buf_count; /* count of total allocated Tx buffers */
204 SCADESC *tx_buf_list; /* list of transmit buffer entries */
205 SCADESC_EX tx_buf_list_ex[SCAMAXDESC]; /* list of transmit buffer entries */
206 unsigned int last_tx_buf;
207
208 unsigned char *tmp_rx_buf;
209 unsigned int tmp_rx_buf_count;
210
211 bool rx_enabled;
212 bool rx_overflow;
213
214 bool tx_enabled;
215 bool tx_active;
216 u32 idle_mode;
217
218 unsigned char ie0_value;
219 unsigned char ie1_value;
220 unsigned char ie2_value;
221 unsigned char ctrlreg_value;
222 unsigned char old_signals;
223
224 char device_name[25]; /* device instance name */
225
226 int port_count;
227 int adapter_num;
228 int port_num;
229
230 struct _synclinkmp_info *port_array[SCA_MAX_PORTS];
231
232 unsigned int bus_type; /* expansion bus type (ISA,EISA,PCI) */
233
234 unsigned int irq_level; /* interrupt level */
235 unsigned long irq_flags;
236 bool irq_requested; /* true if IRQ requested */
237
238 MGSL_PARAMS params; /* communications parameters */
239
240 unsigned char serial_signals; /* current serial signal states */
241
242 bool irq_occurred; /* for diagnostics use */
243 unsigned int init_error; /* Initialization startup error */
244
245 u32 last_mem_alloc;
246 unsigned char* memory_base; /* shared memory address (PCI only) */
247 u32 phys_memory_base;
248 int shared_mem_requested;
249
250 unsigned char* sca_base; /* HD64570 SCA Memory address */
251 u32 phys_sca_base;
252 u32 sca_offset;
253 bool sca_base_requested;
254
255 unsigned char* lcr_base; /* local config registers (PCI only) */
256 u32 phys_lcr_base;
257 u32 lcr_offset;
258 int lcr_mem_requested;
259
260 unsigned char* statctrl_base; /* status/control register memory */
261 u32 phys_statctrl_base;
262 u32 statctrl_offset;
263 bool sca_statctrl_requested;
264
265 u32 misc_ctrl_value;
266 char flag_buf[MAX_ASYNC_BUFFER_SIZE];
267 char char_buf[MAX_ASYNC_BUFFER_SIZE];
268 bool drop_rts_on_tx_done;
269
270 struct _input_signal_events input_signal_events;
271
272 /* SPPP/Cisco HDLC device parts */
273 int netcount;
274 spinlock_t netlock;
275
276#if SYNCLINK_GENERIC_HDLC
277 struct net_device *netdev;
278#endif
279
280} SLMP_INFO;
281
282#define MGSL_MAGIC 0x5401
283
284/*
285 * define serial signal status change macros
286 */
287#define MISCSTATUS_DCD_LATCHED (SerialSignal_DCD<<8) /* indicates change in DCD */
288#define MISCSTATUS_RI_LATCHED (SerialSignal_RI<<8) /* indicates change in RI */
289#define MISCSTATUS_CTS_LATCHED (SerialSignal_CTS<<8) /* indicates change in CTS */
290#define MISCSTATUS_DSR_LATCHED (SerialSignal_DSR<<8) /* change in DSR */
291
292/* Common Register macros */
293#define LPR 0x00
294#define PABR0 0x02
295#define PABR1 0x03
296#define WCRL 0x04
297#define WCRM 0x05
298#define WCRH 0x06
299#define DPCR 0x08
300#define DMER 0x09
301#define ISR0 0x10
302#define ISR1 0x11
303#define ISR2 0x12
304#define IER0 0x14
305#define IER1 0x15
306#define IER2 0x16
307#define ITCR 0x18
308#define INTVR 0x1a
309#define IMVR 0x1c
310
311/* MSCI Register macros */
312#define TRB 0x20
313#define TRBL 0x20
314#define TRBH 0x21
315#define SR0 0x22
316#define SR1 0x23
317#define SR2 0x24
318#define SR3 0x25
319#define FST 0x26
320#define IE0 0x28
321#define IE1 0x29
322#define IE2 0x2a
323#define FIE 0x2b
324#define CMD 0x2c
325#define MD0 0x2e
326#define MD1 0x2f
327#define MD2 0x30
328#define CTL 0x31
329#define SA0 0x32
330#define SA1 0x33
331#define IDL 0x34
332#define TMC 0x35
333#define RXS 0x36
334#define TXS 0x37
335#define TRC0 0x38
336#define TRC1 0x39
337#define RRC 0x3a
338#define CST0 0x3c
339#define CST1 0x3d
340
341/* Timer Register Macros */
342#define TCNT 0x60
343#define TCNTL 0x60
344#define TCNTH 0x61
345#define TCONR 0x62
346#define TCONRL 0x62
347#define TCONRH 0x63
348#define TMCS 0x64
349#define TEPR 0x65
350
351/* DMA Controller Register macros */
352#define DARL 0x80
353#define DARH 0x81
354#define DARB 0x82
355#define BAR 0x80
356#define BARL 0x80
357#define BARH 0x81
358#define BARB 0x82
359#define SAR 0x84
360#define SARL 0x84
361#define SARH 0x85
362#define SARB 0x86
363#define CPB 0x86
364#define CDA 0x88
365#define CDAL 0x88
366#define CDAH 0x89
367#define EDA 0x8a
368#define EDAL 0x8a
369#define EDAH 0x8b
370#define BFL 0x8c
371#define BFLL 0x8c
372#define BFLH 0x8d
373#define BCR 0x8e
374#define BCRL 0x8e
375#define BCRH 0x8f
376#define DSR 0x90
377#define DMR 0x91
378#define FCT 0x93
379#define DIR 0x94
380#define DCMD 0x95
381
382/* combine with timer or DMA register address */
383#define TIMER0 0x00
384#define TIMER1 0x08
385#define TIMER2 0x10
386#define TIMER3 0x18
387#define RXDMA 0x00
388#define TXDMA 0x20
389
390/* SCA Command Codes */
391#define NOOP 0x00
392#define TXRESET 0x01
393#define TXENABLE 0x02
394#define TXDISABLE 0x03
395#define TXCRCINIT 0x04
396#define TXCRCEXCL 0x05
397#define TXEOM 0x06
398#define TXABORT 0x07
399#define MPON 0x08
400#define TXBUFCLR 0x09
401#define RXRESET 0x11
402#define RXENABLE 0x12
403#define RXDISABLE 0x13
404#define RXCRCINIT 0x14
405#define RXREJECT 0x15
406#define SEARCHMP 0x16
407#define RXCRCEXCL 0x17
408#define RXCRCCALC 0x18
409#define CHRESET 0x21
410#define HUNT 0x31
411
412/* DMA command codes */
413#define SWABORT 0x01
414#define FEICLEAR 0x02
415
416/* IE0 */
417#define TXINTE BIT7
418#define RXINTE BIT6
419#define TXRDYE BIT1
420#define RXRDYE BIT0
421
422/* IE1 & SR1 */
423#define UDRN BIT7
424#define IDLE BIT6
425#define SYNCD BIT4
426#define FLGD BIT4
427#define CCTS BIT3
428#define CDCD BIT2
429#define BRKD BIT1
430#define ABTD BIT1
431#define GAPD BIT1
432#define BRKE BIT0
433#define IDLD BIT0
434
435/* IE2 & SR2 */
436#define EOM BIT7
437#define PMP BIT6
438#define SHRT BIT6
439#define PE BIT5
440#define ABT BIT5
441#define FRME BIT4
442#define RBIT BIT4
443#define OVRN BIT3
444#define CRCE BIT2
445
446
447/*
448 * Global linked list of SyncLink devices
449 */
450static SLMP_INFO *synclinkmp_device_list = NULL;
451static int synclinkmp_adapter_count = -1;
452static int synclinkmp_device_count = 0;
453
454/*
455 * Set this param to non-zero to load eax with the
456 * .text section address and breakpoint on module load.
457 * This is useful for use with gdb and add-symbol-file command.
458 */
459static int break_on_load = 0;
460
461/*
462 * Driver major number, defaults to zero to get auto
463 * assigned major number. May be forced as module parameter.
464 */
465static int ttymajor = 0;
466
467/*
468 * Array of user specified options for ISA adapters.
469 */
470static int debug_level = 0;
471static int maxframe[MAX_DEVICES] = {0,};
472
473module_param(break_on_load, bool, 0);
474module_param(ttymajor, int, 0);
475module_param(debug_level, int, 0);
476module_param_array(maxframe, int, NULL, 0);
477
478static char *driver_name = "SyncLink MultiPort driver";
479static char *driver_version = "$Revision: 4.38 $";
480
481static int synclinkmp_init_one(struct pci_dev *dev,const struct pci_device_id *ent);
482static void synclinkmp_remove_one(struct pci_dev *dev);
483
484static struct pci_device_id synclinkmp_pci_tbl[] = {
485 { PCI_VENDOR_ID_MICROGATE, PCI_DEVICE_ID_MICROGATE_SCA, PCI_ANY_ID, PCI_ANY_ID, },
486 { 0, }, /* terminate list */
487};
488MODULE_DEVICE_TABLE(pci, synclinkmp_pci_tbl);
489
490MODULE_LICENSE("GPL");
491
492static struct pci_driver synclinkmp_pci_driver = {
493 .name = "synclinkmp",
494 .id_table = synclinkmp_pci_tbl,
495 .probe = synclinkmp_init_one,
496 .remove = __devexit_p(synclinkmp_remove_one),
497};
498
499
500static struct tty_driver *serial_driver;
501
502/* number of characters left in xmit buffer before we ask for more */
503#define WAKEUP_CHARS 256
504
505
506/* tty callbacks */
507
508static int open(struct tty_struct *tty, struct file * filp);
509static void close(struct tty_struct *tty, struct file * filp);
510static void hangup(struct tty_struct *tty);
511static void set_termios(struct tty_struct *tty, struct ktermios *old_termios);
512
513static int write(struct tty_struct *tty, const unsigned char *buf, int count);
514static int put_char(struct tty_struct *tty, unsigned char ch);
515static void send_xchar(struct tty_struct *tty, char ch);
516static void wait_until_sent(struct tty_struct *tty, int timeout);
517static int write_room(struct tty_struct *tty);
518static void flush_chars(struct tty_struct *tty);
519static void flush_buffer(struct tty_struct *tty);
520static void tx_hold(struct tty_struct *tty);
521static void tx_release(struct tty_struct *tty);
522
523static int ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg);
524static int chars_in_buffer(struct tty_struct *tty);
525static void throttle(struct tty_struct * tty);
526static void unthrottle(struct tty_struct * tty);
527static int set_break(struct tty_struct *tty, int break_state);
528
529#if SYNCLINK_GENERIC_HDLC
530#define dev_to_port(D) (dev_to_hdlc(D)->priv)
531static void hdlcdev_tx_done(SLMP_INFO *info);
532static void hdlcdev_rx(SLMP_INFO *info, char *buf, int size);
533static int hdlcdev_init(SLMP_INFO *info);
534static void hdlcdev_exit(SLMP_INFO *info);
535#endif
536
537/* ioctl handlers */
538
539static int get_stats(SLMP_INFO *info, struct mgsl_icount __user *user_icount);
540static int get_params(SLMP_INFO *info, MGSL_PARAMS __user *params);
541static int set_params(SLMP_INFO *info, MGSL_PARAMS __user *params);
542static int get_txidle(SLMP_INFO *info, int __user *idle_mode);
543static int set_txidle(SLMP_INFO *info, int idle_mode);
544static int tx_enable(SLMP_INFO *info, int enable);
545static int tx_abort(SLMP_INFO *info);
546static int rx_enable(SLMP_INFO *info, int enable);
547static int modem_input_wait(SLMP_INFO *info,int arg);
548static int wait_mgsl_event(SLMP_INFO *info, int __user *mask_ptr);
549static int tiocmget(struct tty_struct *tty, struct file *file);
550static int tiocmset(struct tty_struct *tty, struct file *file,
551 unsigned int set, unsigned int clear);
552static int set_break(struct tty_struct *tty, int break_state);
553
554static void add_device(SLMP_INFO *info);
555static void device_init(int adapter_num, struct pci_dev *pdev);
556static int claim_resources(SLMP_INFO *info);
557static void release_resources(SLMP_INFO *info);
558
559static int startup(SLMP_INFO *info);
560static int block_til_ready(struct tty_struct *tty, struct file * filp,SLMP_INFO *info);
561static int carrier_raised(struct tty_port *port);
562static void shutdown(SLMP_INFO *info);
563static void program_hw(SLMP_INFO *info);
564static void change_params(SLMP_INFO *info);
565
566static bool init_adapter(SLMP_INFO *info);
567static bool register_test(SLMP_INFO *info);
568static bool irq_test(SLMP_INFO *info);
569static bool loopback_test(SLMP_INFO *info);
570static int adapter_test(SLMP_INFO *info);
571static bool memory_test(SLMP_INFO *info);
572
573static void reset_adapter(SLMP_INFO *info);
574static void reset_port(SLMP_INFO *info);
575static void async_mode(SLMP_INFO *info);
576static void hdlc_mode(SLMP_INFO *info);
577
578static void rx_stop(SLMP_INFO *info);
579static void rx_start(SLMP_INFO *info);
580static void rx_reset_buffers(SLMP_INFO *info);
581static void rx_free_frame_buffers(SLMP_INFO *info, unsigned int first, unsigned int last);
582static bool rx_get_frame(SLMP_INFO *info);
583
584static void tx_start(SLMP_INFO *info);
585static void tx_stop(SLMP_INFO *info);
586static void tx_load_fifo(SLMP_INFO *info);
587static void tx_set_idle(SLMP_INFO *info);
588static void tx_load_dma_buffer(SLMP_INFO *info, const char *buf, unsigned int count);
589
590static void get_signals(SLMP_INFO *info);
591static void set_signals(SLMP_INFO *info);
592static void enable_loopback(SLMP_INFO *info, int enable);
593static void set_rate(SLMP_INFO *info, u32 data_rate);
594
595static int bh_action(SLMP_INFO *info);
596static void bh_handler(struct work_struct *work);
597static void bh_receive(SLMP_INFO *info);
598static void bh_transmit(SLMP_INFO *info);
599static void bh_status(SLMP_INFO *info);
600static void isr_timer(SLMP_INFO *info);
601static void isr_rxint(SLMP_INFO *info);
602static void isr_rxrdy(SLMP_INFO *info);
603static void isr_txint(SLMP_INFO *info);
604static void isr_txrdy(SLMP_INFO *info);
605static void isr_rxdmaok(SLMP_INFO *info);
606static void isr_rxdmaerror(SLMP_INFO *info);
607static void isr_txdmaok(SLMP_INFO *info);
608static void isr_txdmaerror(SLMP_INFO *info);
609static void isr_io_pin(SLMP_INFO *info, u16 status);
610
611static int alloc_dma_bufs(SLMP_INFO *info);
612static void free_dma_bufs(SLMP_INFO *info);
613static int alloc_buf_list(SLMP_INFO *info);
614static int alloc_frame_bufs(SLMP_INFO *info, SCADESC *list, SCADESC_EX *list_ex,int count);
615static int alloc_tmp_rx_buf(SLMP_INFO *info);
616static void free_tmp_rx_buf(SLMP_INFO *info);
617
618static void load_pci_memory(SLMP_INFO *info, char* dest, const char* src, unsigned short count);
619static void trace_block(SLMP_INFO *info, const char* data, int count, int xmit);
620static void tx_timeout(unsigned long context);
621static void status_timeout(unsigned long context);
622
623static unsigned char read_reg(SLMP_INFO *info, unsigned char addr);
624static void write_reg(SLMP_INFO *info, unsigned char addr, unsigned char val);
625static u16 read_reg16(SLMP_INFO *info, unsigned char addr);
626static void write_reg16(SLMP_INFO *info, unsigned char addr, u16 val);
627static unsigned char read_status_reg(SLMP_INFO * info);
628static void write_control_reg(SLMP_INFO * info);
629
630
631static unsigned char rx_active_fifo_level = 16; // rx request FIFO activation level in bytes
632static unsigned char tx_active_fifo_level = 16; // tx request FIFO activation level in bytes
633static unsigned char tx_negate_fifo_level = 32; // tx request FIFO negation level in bytes
634
635static u32 misc_ctrl_value = 0x007e4040;
636static u32 lcr1_brdr_value = 0x00800028;
637
638static u32 read_ahead_count = 8;
639
640/* DPCR, DMA Priority Control
641 *
642 * 07..05 Not used, must be 0
643 * 04 BRC, bus release condition: 0=all transfers complete
644 * 1=release after 1 xfer on all channels
645 * 03 CCC, channel change condition: 0=every cycle
646 * 1=after each channel completes all xfers
647 * 02..00 PR<2..0>, priority 100=round robin
648 *
649 * 00000100 = 0x00
650 */
651static unsigned char dma_priority = 0x04;
652
653// Number of bytes that can be written to shared RAM
654// in a single write operation
655static u32 sca_pci_load_interval = 64;
656
657/*
658 * 1st function defined in .text section. Calling this function in
659 * init_module() followed by a breakpoint allows a remote debugger
660 * (gdb) to get the .text address for the add-symbol-file command.
661 * This allows remote debugging of dynamically loadable modules.
662 */
663static void* synclinkmp_get_text_ptr(void);
664static void* synclinkmp_get_text_ptr(void) {return synclinkmp_get_text_ptr;}
665
666static inline int sanity_check(SLMP_INFO *info,
667 char *name, const char *routine)
668{
669#ifdef SANITY_CHECK
670 static const char *badmagic =
671 "Warning: bad magic number for synclinkmp_struct (%s) in %s\n";
672 static const char *badinfo =
673 "Warning: null synclinkmp_struct for (%s) in %s\n";
674
675 if (!info) {
676 printk(badinfo, name, routine);
677 return 1;
678 }
679 if (info->magic != MGSL_MAGIC) {
680 printk(badmagic, name, routine);
681 return 1;
682 }
683#else
684 if (!info)
685 return 1;
686#endif
687 return 0;
688}
689
690/**
691 * line discipline callback wrappers
692 *
693 * The wrappers maintain line discipline references
694 * while calling into the line discipline.
695 *
696 * ldisc_receive_buf - pass receive data to line discipline
697 */
698
699static void ldisc_receive_buf(struct tty_struct *tty,
700 const __u8 *data, char *flags, int count)
701{
702 struct tty_ldisc *ld;
703 if (!tty)
704 return;
705 ld = tty_ldisc_ref(tty);
706 if (ld) {
707 if (ld->ops->receive_buf)
708 ld->ops->receive_buf(tty, data, flags, count);
709 tty_ldisc_deref(ld);
710 }
711}
712
713/* tty callbacks */
714
715/* Called when a port is opened. Init and enable port.
716 */
717static int open(struct tty_struct *tty, struct file *filp)
718{
719 SLMP_INFO *info;
720 int retval, line;
721 unsigned long flags;
722
723 line = tty->index;
724 if ((line < 0) || (line >= synclinkmp_device_count)) {
725 printk("%s(%d): open with invalid line #%d.\n",
726 __FILE__,__LINE__,line);
727 return -ENODEV;
728 }
729
730 info = synclinkmp_device_list;
731 while(info && info->line != line)
732 info = info->next_device;
733 if (sanity_check(info, tty->name, "open"))
734 return -ENODEV;
735 if ( info->init_error ) {
736 printk("%s(%d):%s device is not allocated, init error=%d\n",
737 __FILE__,__LINE__,info->device_name,info->init_error);
738 return -ENODEV;
739 }
740
741 tty->driver_data = info;
742 info->port.tty = tty;
743
744 if (debug_level >= DEBUG_LEVEL_INFO)
745 printk("%s(%d):%s open(), old ref count = %d\n",
746 __FILE__,__LINE__,tty->driver->name, info->port.count);
747
748 /* If port is closing, signal caller to try again */
749 if (tty_hung_up_p(filp) || info->port.flags & ASYNC_CLOSING){
750 if (info->port.flags & ASYNC_CLOSING)
751 interruptible_sleep_on(&info->port.close_wait);
752 retval = ((info->port.flags & ASYNC_HUP_NOTIFY) ?
753 -EAGAIN : -ERESTARTSYS);
754 goto cleanup;
755 }
756
757 info->port.tty->low_latency = (info->port.flags & ASYNC_LOW_LATENCY) ? 1 : 0;
758
759 spin_lock_irqsave(&info->netlock, flags);
760 if (info->netcount) {
761 retval = -EBUSY;
762 spin_unlock_irqrestore(&info->netlock, flags);
763 goto cleanup;
764 }
765 info->port.count++;
766 spin_unlock_irqrestore(&info->netlock, flags);
767
768 if (info->port.count == 1) {
769 /* 1st open on this device, init hardware */
770 retval = startup(info);
771 if (retval < 0)
772 goto cleanup;
773 }
774
775 retval = block_til_ready(tty, filp, info);
776 if (retval) {
777 if (debug_level >= DEBUG_LEVEL_INFO)
778 printk("%s(%d):%s block_til_ready() returned %d\n",
779 __FILE__,__LINE__, info->device_name, retval);
780 goto cleanup;
781 }
782
783 if (debug_level >= DEBUG_LEVEL_INFO)
784 printk("%s(%d):%s open() success\n",
785 __FILE__,__LINE__, info->device_name);
786 retval = 0;
787
788cleanup:
789 if (retval) {
790 if (tty->count == 1)
791 info->port.tty = NULL; /* tty layer will release tty struct */
792 if(info->port.count)
793 info->port.count--;
794 }
795
796 return retval;
797}
798
799/* Called when port is closed. Wait for remaining data to be
800 * sent. Disable port and free resources.
801 */
802static void close(struct tty_struct *tty, struct file *filp)
803{
804 SLMP_INFO * info = tty->driver_data;
805
806 if (sanity_check(info, tty->name, "close"))
807 return;
808
809 if (debug_level >= DEBUG_LEVEL_INFO)
810 printk("%s(%d):%s close() entry, count=%d\n",
811 __FILE__,__LINE__, info->device_name, info->port.count);
812
813 if (tty_port_close_start(&info->port, tty, filp) == 0)
814 goto cleanup;
815
816 mutex_lock(&info->port.mutex);
817 if (info->port.flags & ASYNC_INITIALIZED)
818 wait_until_sent(tty, info->timeout);
819
820 flush_buffer(tty);
821 tty_ldisc_flush(tty);
822 shutdown(info);
823 mutex_unlock(&info->port.mutex);
824
825 tty_port_close_end(&info->port, tty);
826 info->port.tty = NULL;
827cleanup:
828 if (debug_level >= DEBUG_LEVEL_INFO)
829 printk("%s(%d):%s close() exit, count=%d\n", __FILE__,__LINE__,
830 tty->driver->name, info->port.count);
831}
832
833/* Called by tty_hangup() when a hangup is signaled.
834 * This is the same as closing all open descriptors for the port.
835 */
836static void hangup(struct tty_struct *tty)
837{
838 SLMP_INFO *info = tty->driver_data;
839 unsigned long flags;
840
841 if (debug_level >= DEBUG_LEVEL_INFO)
842 printk("%s(%d):%s hangup()\n",
843 __FILE__,__LINE__, info->device_name );
844
845 if (sanity_check(info, tty->name, "hangup"))
846 return;
847
848 mutex_lock(&info->port.mutex);
849 flush_buffer(tty);
850 shutdown(info);
851
852 spin_lock_irqsave(&info->port.lock, flags);
853 info->port.count = 0;
854 info->port.flags &= ~ASYNC_NORMAL_ACTIVE;
855 info->port.tty = NULL;
856 spin_unlock_irqrestore(&info->port.lock, flags);
857 mutex_unlock(&info->port.mutex);
858
859 wake_up_interruptible(&info->port.open_wait);
860}
861
862/* Set new termios settings
863 */
864static void set_termios(struct tty_struct *tty, struct ktermios *old_termios)
865{
866 SLMP_INFO *info = tty->driver_data;
867 unsigned long flags;
868
869 if (debug_level >= DEBUG_LEVEL_INFO)
870 printk("%s(%d):%s set_termios()\n", __FILE__,__LINE__,
871 tty->driver->name );
872
873 change_params(info);
874
875 /* Handle transition to B0 status */
876 if (old_termios->c_cflag & CBAUD &&
877 !(tty->termios->c_cflag & CBAUD)) {
878 info->serial_signals &= ~(SerialSignal_RTS + SerialSignal_DTR);
879 spin_lock_irqsave(&info->lock,flags);
880 set_signals(info);
881 spin_unlock_irqrestore(&info->lock,flags);
882 }
883
884 /* Handle transition away from B0 status */
885 if (!(old_termios->c_cflag & CBAUD) &&
886 tty->termios->c_cflag & CBAUD) {
887 info->serial_signals |= SerialSignal_DTR;
888 if (!(tty->termios->c_cflag & CRTSCTS) ||
889 !test_bit(TTY_THROTTLED, &tty->flags)) {
890 info->serial_signals |= SerialSignal_RTS;
891 }
892 spin_lock_irqsave(&info->lock,flags);
893 set_signals(info);
894 spin_unlock_irqrestore(&info->lock,flags);
895 }
896
897 /* Handle turning off CRTSCTS */
898 if (old_termios->c_cflag & CRTSCTS &&
899 !(tty->termios->c_cflag & CRTSCTS)) {
900 tty->hw_stopped = 0;
901 tx_release(tty);
902 }
903}
904
905/* Send a block of data
906 *
907 * Arguments:
908 *
909 * tty pointer to tty information structure
910 * buf pointer to buffer containing send data
911 * count size of send data in bytes
912 *
913 * Return Value: number of characters written
914 */
915static int write(struct tty_struct *tty,
916 const unsigned char *buf, int count)
917{
918 int c, ret = 0;
919 SLMP_INFO *info = tty->driver_data;
920 unsigned long flags;
921
922 if (debug_level >= DEBUG_LEVEL_INFO)
923 printk("%s(%d):%s write() count=%d\n",
924 __FILE__,__LINE__,info->device_name,count);
925
926 if (sanity_check(info, tty->name, "write"))
927 goto cleanup;
928
929 if (!info->tx_buf)
930 goto cleanup;
931
932 if (info->params.mode == MGSL_MODE_HDLC) {
933 if (count > info->max_frame_size) {
934 ret = -EIO;
935 goto cleanup;
936 }
937 if (info->tx_active)
938 goto cleanup;
939 if (info->tx_count) {
940 /* send accumulated data from send_char() calls */
941 /* as frame and wait before accepting more data. */
942 tx_load_dma_buffer(info, info->tx_buf, info->tx_count);
943 goto start;
944 }
945 ret = info->tx_count = count;
946 tx_load_dma_buffer(info, buf, count);
947 goto start;
948 }
949
950 for (;;) {
951 c = min_t(int, count,
952 min(info->max_frame_size - info->tx_count - 1,
953 info->max_frame_size - info->tx_put));
954 if (c <= 0)
955 break;
956
957 memcpy(info->tx_buf + info->tx_put, buf, c);
958
959 spin_lock_irqsave(&info->lock,flags);
960 info->tx_put += c;
961 if (info->tx_put >= info->max_frame_size)
962 info->tx_put -= info->max_frame_size;
963 info->tx_count += c;
964 spin_unlock_irqrestore(&info->lock,flags);
965
966 buf += c;
967 count -= c;
968 ret += c;
969 }
970
971 if (info->params.mode == MGSL_MODE_HDLC) {
972 if (count) {
973 ret = info->tx_count = 0;
974 goto cleanup;
975 }
976 tx_load_dma_buffer(info, info->tx_buf, info->tx_count);
977 }
978start:
979 if (info->tx_count && !tty->stopped && !tty->hw_stopped) {
980 spin_lock_irqsave(&info->lock,flags);
981 if (!info->tx_active)
982 tx_start(info);
983 spin_unlock_irqrestore(&info->lock,flags);
984 }
985
986cleanup:
987 if (debug_level >= DEBUG_LEVEL_INFO)
988 printk( "%s(%d):%s write() returning=%d\n",
989 __FILE__,__LINE__,info->device_name,ret);
990 return ret;
991}
992
993/* Add a character to the transmit buffer.
994 */
995static int put_char(struct tty_struct *tty, unsigned char ch)
996{
997 SLMP_INFO *info = tty->driver_data;
998 unsigned long flags;
999 int ret = 0;
1000
1001 if ( debug_level >= DEBUG_LEVEL_INFO ) {
1002 printk( "%s(%d):%s put_char(%d)\n",
1003 __FILE__,__LINE__,info->device_name,ch);
1004 }
1005
1006 if (sanity_check(info, tty->name, "put_char"))
1007 return 0;
1008
1009 if (!info->tx_buf)
1010 return 0;
1011
1012 spin_lock_irqsave(&info->lock,flags);
1013
1014 if ( (info->params.mode != MGSL_MODE_HDLC) ||
1015 !info->tx_active ) {
1016
1017 if (info->tx_count < info->max_frame_size - 1) {
1018 info->tx_buf[info->tx_put++] = ch;
1019 if (info->tx_put >= info->max_frame_size)
1020 info->tx_put -= info->max_frame_size;
1021 info->tx_count++;
1022 ret = 1;
1023 }
1024 }
1025
1026 spin_unlock_irqrestore(&info->lock,flags);
1027 return ret;
1028}
1029
1030/* Send a high-priority XON/XOFF character
1031 */
1032static void send_xchar(struct tty_struct *tty, char ch)
1033{
1034 SLMP_INFO *info = tty->driver_data;
1035 unsigned long flags;
1036
1037 if (debug_level >= DEBUG_LEVEL_INFO)
1038 printk("%s(%d):%s send_xchar(%d)\n",
1039 __FILE__,__LINE__, info->device_name, ch );
1040
1041 if (sanity_check(info, tty->name, "send_xchar"))
1042 return;
1043
1044 info->x_char = ch;
1045 if (ch) {
1046 /* Make sure transmit interrupts are on */
1047 spin_lock_irqsave(&info->lock,flags);
1048 if (!info->tx_enabled)
1049 tx_start(info);
1050 spin_unlock_irqrestore(&info->lock,flags);
1051 }
1052}
1053
1054/* Wait until the transmitter is empty.
1055 */
1056static void wait_until_sent(struct tty_struct *tty, int timeout)
1057{
1058 SLMP_INFO * info = tty->driver_data;
1059 unsigned long orig_jiffies, char_time;
1060
1061 if (!info )
1062 return;
1063
1064 if (debug_level >= DEBUG_LEVEL_INFO)
1065 printk("%s(%d):%s wait_until_sent() entry\n",
1066 __FILE__,__LINE__, info->device_name );
1067
1068 if (sanity_check(info, tty->name, "wait_until_sent"))
1069 return;
1070
1071 if (!test_bit(ASYNCB_INITIALIZED, &info->port.flags))
1072 goto exit;
1073
1074 orig_jiffies = jiffies;
1075
1076 /* Set check interval to 1/5 of estimated time to
1077 * send a character, and make it at least 1. The check
1078 * interval should also be less than the timeout.
1079 * Note: use tight timings here to satisfy the NIST-PCTS.
1080 */
1081
1082 if ( info->params.data_rate ) {
1083 char_time = info->timeout/(32 * 5);
1084 if (!char_time)
1085 char_time++;
1086 } else
1087 char_time = 1;
1088
1089 if (timeout)
1090 char_time = min_t(unsigned long, char_time, timeout);
1091
1092 if ( info->params.mode == MGSL_MODE_HDLC ) {
1093 while (info->tx_active) {
1094 msleep_interruptible(jiffies_to_msecs(char_time));
1095 if (signal_pending(current))
1096 break;
1097 if (timeout && time_after(jiffies, orig_jiffies + timeout))
1098 break;
1099 }
1100 } else {
1101 /*
1102 * TODO: determine if there is something similar to USC16C32
1103 * TXSTATUS_ALL_SENT status
1104 */
1105 while ( info->tx_active && info->tx_enabled) {
1106 msleep_interruptible(jiffies_to_msecs(char_time));
1107 if (signal_pending(current))
1108 break;
1109 if (timeout && time_after(jiffies, orig_jiffies + timeout))
1110 break;
1111 }
1112 }
1113
1114exit:
1115 if (debug_level >= DEBUG_LEVEL_INFO)
1116 printk("%s(%d):%s wait_until_sent() exit\n",
1117 __FILE__,__LINE__, info->device_name );
1118}
1119
1120/* Return the count of free bytes in transmit buffer
1121 */
1122static int write_room(struct tty_struct *tty)
1123{
1124 SLMP_INFO *info = tty->driver_data;
1125 int ret;
1126
1127 if (sanity_check(info, tty->name, "write_room"))
1128 return 0;
1129
1130 if (info->params.mode == MGSL_MODE_HDLC) {
1131 ret = (info->tx_active) ? 0 : HDLC_MAX_FRAME_SIZE;
1132 } else {
1133 ret = info->max_frame_size - info->tx_count - 1;
1134 if (ret < 0)
1135 ret = 0;
1136 }
1137
1138 if (debug_level >= DEBUG_LEVEL_INFO)
1139 printk("%s(%d):%s write_room()=%d\n",
1140 __FILE__, __LINE__, info->device_name, ret);
1141
1142 return ret;
1143}
1144
1145/* enable transmitter and send remaining buffered characters
1146 */
1147static void flush_chars(struct tty_struct *tty)
1148{
1149 SLMP_INFO *info = tty->driver_data;
1150 unsigned long flags;
1151
1152 if ( debug_level >= DEBUG_LEVEL_INFO )
1153 printk( "%s(%d):%s flush_chars() entry tx_count=%d\n",
1154 __FILE__,__LINE__,info->device_name,info->tx_count);
1155
1156 if (sanity_check(info, tty->name, "flush_chars"))
1157 return;
1158
1159 if (info->tx_count <= 0 || tty->stopped || tty->hw_stopped ||
1160 !info->tx_buf)
1161 return;
1162
1163 if ( debug_level >= DEBUG_LEVEL_INFO )
1164 printk( "%s(%d):%s flush_chars() entry, starting transmitter\n",
1165 __FILE__,__LINE__,info->device_name );
1166
1167 spin_lock_irqsave(&info->lock,flags);
1168
1169 if (!info->tx_active) {
1170 if ( (info->params.mode == MGSL_MODE_HDLC) &&
1171 info->tx_count ) {
1172 /* operating in synchronous (frame oriented) mode */
1173 /* copy data from circular tx_buf to */
1174 /* transmit DMA buffer. */
1175 tx_load_dma_buffer(info,
1176 info->tx_buf,info->tx_count);
1177 }
1178 tx_start(info);
1179 }
1180
1181 spin_unlock_irqrestore(&info->lock,flags);
1182}
1183
1184/* Discard all data in the send buffer
1185 */
1186static void flush_buffer(struct tty_struct *tty)
1187{
1188 SLMP_INFO *info = tty->driver_data;
1189 unsigned long flags;
1190
1191 if (debug_level >= DEBUG_LEVEL_INFO)
1192 printk("%s(%d):%s flush_buffer() entry\n",
1193 __FILE__,__LINE__, info->device_name );
1194
1195 if (sanity_check(info, tty->name, "flush_buffer"))
1196 return;
1197
1198 spin_lock_irqsave(&info->lock,flags);
1199 info->tx_count = info->tx_put = info->tx_get = 0;
1200 del_timer(&info->tx_timer);
1201 spin_unlock_irqrestore(&info->lock,flags);
1202
1203 tty_wakeup(tty);
1204}
1205
1206/* throttle (stop) transmitter
1207 */
1208static void tx_hold(struct tty_struct *tty)
1209{
1210 SLMP_INFO *info = tty->driver_data;
1211 unsigned long flags;
1212
1213 if (sanity_check(info, tty->name, "tx_hold"))
1214 return;
1215
1216 if ( debug_level >= DEBUG_LEVEL_INFO )
1217 printk("%s(%d):%s tx_hold()\n",
1218 __FILE__,__LINE__,info->device_name);
1219
1220 spin_lock_irqsave(&info->lock,flags);
1221 if (info->tx_enabled)
1222 tx_stop(info);
1223 spin_unlock_irqrestore(&info->lock,flags);
1224}
1225
1226/* release (start) transmitter
1227 */
1228static void tx_release(struct tty_struct *tty)
1229{
1230 SLMP_INFO *info = tty->driver_data;
1231 unsigned long flags;
1232
1233 if (sanity_check(info, tty->name, "tx_release"))
1234 return;
1235
1236 if ( debug_level >= DEBUG_LEVEL_INFO )
1237 printk("%s(%d):%s tx_release()\n",
1238 __FILE__,__LINE__,info->device_name);
1239
1240 spin_lock_irqsave(&info->lock,flags);
1241 if (!info->tx_enabled)
1242 tx_start(info);
1243 spin_unlock_irqrestore(&info->lock,flags);
1244}
1245
1246/* Service an IOCTL request
1247 *
1248 * Arguments:
1249 *
1250 * tty pointer to tty instance data
1251 * file pointer to associated file object for device
1252 * cmd IOCTL command code
1253 * arg command argument/context
1254 *
1255 * Return Value: 0 if success, otherwise error code
1256 */
1257static int ioctl(struct tty_struct *tty, struct file *file,
1258 unsigned int cmd, unsigned long arg)
1259{
1260 SLMP_INFO *info = tty->driver_data;
1261 int error;
1262 struct mgsl_icount cnow; /* kernel counter temps */
1263 struct serial_icounter_struct __user *p_cuser; /* user space */
1264 unsigned long flags;
1265 void __user *argp = (void __user *)arg;
1266
1267 if (debug_level >= DEBUG_LEVEL_INFO)
1268 printk("%s(%d):%s ioctl() cmd=%08X\n", __FILE__,__LINE__,
1269 info->device_name, cmd );
1270
1271 if (sanity_check(info, tty->name, "ioctl"))
1272 return -ENODEV;
1273
1274 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1275 (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
1276 if (tty->flags & (1 << TTY_IO_ERROR))
1277 return -EIO;
1278 }
1279
1280 switch (cmd) {
1281 case MGSL_IOCGPARAMS:
1282 return get_params(info, argp);
1283 case MGSL_IOCSPARAMS:
1284 return set_params(info, argp);
1285 case MGSL_IOCGTXIDLE:
1286 return get_txidle(info, argp);
1287 case MGSL_IOCSTXIDLE:
1288 return set_txidle(info, (int)arg);
1289 case MGSL_IOCTXENABLE:
1290 return tx_enable(info, (int)arg);
1291 case MGSL_IOCRXENABLE:
1292 return rx_enable(info, (int)arg);
1293 case MGSL_IOCTXABORT:
1294 return tx_abort(info);
1295 case MGSL_IOCGSTATS:
1296 return get_stats(info, argp);
1297 case MGSL_IOCWAITEVENT:
1298 return wait_mgsl_event(info, argp);
1299 case MGSL_IOCLOOPTXDONE:
1300 return 0; // TODO: Not supported, need to document
1301 /* Wait for modem input (DCD,RI,DSR,CTS) change
1302 * as specified by mask in arg (TIOCM_RNG/DSR/CD/CTS)
1303 */
1304 case TIOCMIWAIT:
1305 return modem_input_wait(info,(int)arg);
1306
1307 /*
1308 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1309 * Return: write counters to the user passed counter struct
1310 * NB: both 1->0 and 0->1 transitions are counted except for
1311 * RI where only 0->1 is counted.
1312 */
1313 case TIOCGICOUNT:
1314 spin_lock_irqsave(&info->lock,flags);
1315 cnow = info->icount;
1316 spin_unlock_irqrestore(&info->lock,flags);
1317 p_cuser = argp;
1318 PUT_USER(error,cnow.cts, &p_cuser->cts);
1319 if (error) return error;
1320 PUT_USER(error,cnow.dsr, &p_cuser->dsr);
1321 if (error) return error;
1322 PUT_USER(error,cnow.rng, &p_cuser->rng);
1323 if (error) return error;
1324 PUT_USER(error,cnow.dcd, &p_cuser->dcd);
1325 if (error) return error;
1326 PUT_USER(error,cnow.rx, &p_cuser->rx);
1327 if (error) return error;
1328 PUT_USER(error,cnow.tx, &p_cuser->tx);
1329 if (error) return error;
1330 PUT_USER(error,cnow.frame, &p_cuser->frame);
1331 if (error) return error;
1332 PUT_USER(error,cnow.overrun, &p_cuser->overrun);
1333 if (error) return error;
1334 PUT_USER(error,cnow.parity, &p_cuser->parity);
1335 if (error) return error;
1336 PUT_USER(error,cnow.brk, &p_cuser->brk);
1337 if (error) return error;
1338 PUT_USER(error,cnow.buf_overrun, &p_cuser->buf_overrun);
1339 if (error) return error;
1340 return 0;
1341 default:
1342 return -ENOIOCTLCMD;
1343 }
1344 return 0;
1345}
1346
1347/*
1348 * /proc fs routines....
1349 */
1350
1351static inline void line_info(struct seq_file *m, SLMP_INFO *info)
1352{
1353 char stat_buf[30];
1354 unsigned long flags;
1355
1356 seq_printf(m, "%s: SCABase=%08x Mem=%08X StatusControl=%08x LCR=%08X\n"
1357 "\tIRQ=%d MaxFrameSize=%u\n",
1358 info->device_name,
1359 info->phys_sca_base,
1360 info->phys_memory_base,
1361 info->phys_statctrl_base,
1362 info->phys_lcr_base,
1363 info->irq_level,
1364 info->max_frame_size );
1365
1366 /* output current serial signal states */
1367 spin_lock_irqsave(&info->lock,flags);
1368 get_signals(info);
1369 spin_unlock_irqrestore(&info->lock,flags);
1370
1371 stat_buf[0] = 0;
1372 stat_buf[1] = 0;
1373 if (info->serial_signals & SerialSignal_RTS)
1374 strcat(stat_buf, "|RTS");
1375 if (info->serial_signals & SerialSignal_CTS)
1376 strcat(stat_buf, "|CTS");
1377 if (info->serial_signals & SerialSignal_DTR)
1378 strcat(stat_buf, "|DTR");
1379 if (info->serial_signals & SerialSignal_DSR)
1380 strcat(stat_buf, "|DSR");
1381 if (info->serial_signals & SerialSignal_DCD)
1382 strcat(stat_buf, "|CD");
1383 if (info->serial_signals & SerialSignal_RI)
1384 strcat(stat_buf, "|RI");
1385
1386 if (info->params.mode == MGSL_MODE_HDLC) {
1387 seq_printf(m, "\tHDLC txok:%d rxok:%d",
1388 info->icount.txok, info->icount.rxok);
1389 if (info->icount.txunder)
1390 seq_printf(m, " txunder:%d", info->icount.txunder);
1391 if (info->icount.txabort)
1392 seq_printf(m, " txabort:%d", info->icount.txabort);
1393 if (info->icount.rxshort)
1394 seq_printf(m, " rxshort:%d", info->icount.rxshort);
1395 if (info->icount.rxlong)
1396 seq_printf(m, " rxlong:%d", info->icount.rxlong);
1397 if (info->icount.rxover)
1398 seq_printf(m, " rxover:%d", info->icount.rxover);
1399 if (info->icount.rxcrc)
1400 seq_printf(m, " rxlong:%d", info->icount.rxcrc);
1401 } else {
1402 seq_printf(m, "\tASYNC tx:%d rx:%d",
1403 info->icount.tx, info->icount.rx);
1404 if (info->icount.frame)
1405 seq_printf(m, " fe:%d", info->icount.frame);
1406 if (info->icount.parity)
1407 seq_printf(m, " pe:%d", info->icount.parity);
1408 if (info->icount.brk)
1409 seq_printf(m, " brk:%d", info->icount.brk);
1410 if (info->icount.overrun)
1411 seq_printf(m, " oe:%d", info->icount.overrun);
1412 }
1413
1414 /* Append serial signal status to end */
1415 seq_printf(m, " %s\n", stat_buf+1);
1416
1417 seq_printf(m, "\ttxactive=%d bh_req=%d bh_run=%d pending_bh=%x\n",
1418 info->tx_active,info->bh_requested,info->bh_running,
1419 info->pending_bh);
1420}
1421
1422/* Called to print information about devices
1423 */
1424static int synclinkmp_proc_show(struct seq_file *m, void *v)
1425{
1426 SLMP_INFO *info;
1427
1428 seq_printf(m, "synclinkmp driver:%s\n", driver_version);
1429
1430 info = synclinkmp_device_list;
1431 while( info ) {
1432 line_info(m, info);
1433 info = info->next_device;
1434 }
1435 return 0;
1436}
1437
1438static int synclinkmp_proc_open(struct inode *inode, struct file *file)
1439{
1440 return single_open(file, synclinkmp_proc_show, NULL);
1441}
1442
1443static const struct file_operations synclinkmp_proc_fops = {
1444 .owner = THIS_MODULE,
1445 .open = synclinkmp_proc_open,
1446 .read = seq_read,
1447 .llseek = seq_lseek,
1448 .release = single_release,
1449};
1450
1451/* Return the count of bytes in transmit buffer
1452 */
1453static int chars_in_buffer(struct tty_struct *tty)
1454{
1455 SLMP_INFO *info = tty->driver_data;
1456
1457 if (sanity_check(info, tty->name, "chars_in_buffer"))
1458 return 0;
1459
1460 if (debug_level >= DEBUG_LEVEL_INFO)
1461 printk("%s(%d):%s chars_in_buffer()=%d\n",
1462 __FILE__, __LINE__, info->device_name, info->tx_count);
1463
1464 return info->tx_count;
1465}
1466
1467/* Signal remote device to throttle send data (our receive data)
1468 */
1469static void throttle(struct tty_struct * tty)
1470{
1471 SLMP_INFO *info = tty->driver_data;
1472 unsigned long flags;
1473
1474 if (debug_level >= DEBUG_LEVEL_INFO)
1475 printk("%s(%d):%s throttle() entry\n",
1476 __FILE__,__LINE__, info->device_name );
1477
1478 if (sanity_check(info, tty->name, "throttle"))
1479 return;
1480
1481 if (I_IXOFF(tty))
1482 send_xchar(tty, STOP_CHAR(tty));
1483
1484 if (tty->termios->c_cflag & CRTSCTS) {
1485 spin_lock_irqsave(&info->lock,flags);
1486 info->serial_signals &= ~SerialSignal_RTS;
1487 set_signals(info);
1488 spin_unlock_irqrestore(&info->lock,flags);
1489 }
1490}
1491
1492/* Signal remote device to stop throttling send data (our receive data)
1493 */
1494static void unthrottle(struct tty_struct * tty)
1495{
1496 SLMP_INFO *info = tty->driver_data;
1497 unsigned long flags;
1498
1499 if (debug_level >= DEBUG_LEVEL_INFO)
1500 printk("%s(%d):%s unthrottle() entry\n",
1501 __FILE__,__LINE__, info->device_name );
1502
1503 if (sanity_check(info, tty->name, "unthrottle"))
1504 return;
1505
1506 if (I_IXOFF(tty)) {
1507 if (info->x_char)
1508 info->x_char = 0;
1509 else
1510 send_xchar(tty, START_CHAR(tty));
1511 }
1512
1513 if (tty->termios->c_cflag & CRTSCTS) {
1514 spin_lock_irqsave(&info->lock,flags);
1515 info->serial_signals |= SerialSignal_RTS;
1516 set_signals(info);
1517 spin_unlock_irqrestore(&info->lock,flags);
1518 }
1519}
1520
1521/* set or clear transmit break condition
1522 * break_state -1=set break condition, 0=clear
1523 */
1524static int set_break(struct tty_struct *tty, int break_state)
1525{
1526 unsigned char RegValue;
1527 SLMP_INFO * info = tty->driver_data;
1528 unsigned long flags;
1529
1530 if (debug_level >= DEBUG_LEVEL_INFO)
1531 printk("%s(%d):%s set_break(%d)\n",
1532 __FILE__,__LINE__, info->device_name, break_state);
1533
1534 if (sanity_check(info, tty->name, "set_break"))
1535 return -EINVAL;
1536
1537 spin_lock_irqsave(&info->lock,flags);
1538 RegValue = read_reg(info, CTL);
1539 if (break_state == -1)
1540 RegValue |= BIT3;
1541 else
1542 RegValue &= ~BIT3;
1543 write_reg(info, CTL, RegValue);
1544 spin_unlock_irqrestore(&info->lock,flags);
1545 return 0;
1546}
1547
1548#if SYNCLINK_GENERIC_HDLC
1549
1550/**
1551 * called by generic HDLC layer when protocol selected (PPP, frame relay, etc.)
1552 * set encoding and frame check sequence (FCS) options
1553 *
1554 * dev pointer to network device structure
1555 * encoding serial encoding setting
1556 * parity FCS setting
1557 *
1558 * returns 0 if success, otherwise error code
1559 */
1560static int hdlcdev_attach(struct net_device *dev, unsigned short encoding,
1561 unsigned short parity)
1562{
1563 SLMP_INFO *info = dev_to_port(dev);
1564 unsigned char new_encoding;
1565 unsigned short new_crctype;
1566
1567 /* return error if TTY interface open */
1568 if (info->port.count)
1569 return -EBUSY;
1570
1571 switch (encoding)
1572 {
1573 case ENCODING_NRZ: new_encoding = HDLC_ENCODING_NRZ; break;
1574 case ENCODING_NRZI: new_encoding = HDLC_ENCODING_NRZI_SPACE; break;
1575 case ENCODING_FM_MARK: new_encoding = HDLC_ENCODING_BIPHASE_MARK; break;
1576 case ENCODING_FM_SPACE: new_encoding = HDLC_ENCODING_BIPHASE_SPACE; break;
1577 case ENCODING_MANCHESTER: new_encoding = HDLC_ENCODING_BIPHASE_LEVEL; break;
1578 default: return -EINVAL;
1579 }
1580
1581 switch (parity)
1582 {
1583 case PARITY_NONE: new_crctype = HDLC_CRC_NONE; break;
1584 case PARITY_CRC16_PR1_CCITT: new_crctype = HDLC_CRC_16_CCITT; break;
1585 case PARITY_CRC32_PR1_CCITT: new_crctype = HDLC_CRC_32_CCITT; break;
1586 default: return -EINVAL;
1587 }
1588
1589 info->params.encoding = new_encoding;
1590 info->params.crc_type = new_crctype;
1591
1592 /* if network interface up, reprogram hardware */
1593 if (info->netcount)
1594 program_hw(info);
1595
1596 return 0;
1597}
1598
1599/**
1600 * called by generic HDLC layer to send frame
1601 *
1602 * skb socket buffer containing HDLC frame
1603 * dev pointer to network device structure
1604 */
1605static netdev_tx_t hdlcdev_xmit(struct sk_buff *skb,
1606 struct net_device *dev)
1607{
1608 SLMP_INFO *info = dev_to_port(dev);
1609 unsigned long flags;
1610
1611 if (debug_level >= DEBUG_LEVEL_INFO)
1612 printk(KERN_INFO "%s:hdlc_xmit(%s)\n",__FILE__,dev->name);
1613
1614 /* stop sending until this frame completes */
1615 netif_stop_queue(dev);
1616
1617 /* copy data to device buffers */
1618 info->tx_count = skb->len;
1619 tx_load_dma_buffer(info, skb->data, skb->len);
1620
1621 /* update network statistics */
1622 dev->stats.tx_packets++;
1623 dev->stats.tx_bytes += skb->len;
1624
1625 /* done with socket buffer, so free it */
1626 dev_kfree_skb(skb);
1627
1628 /* save start time for transmit timeout detection */
1629 dev->trans_start = jiffies;
1630
1631 /* start hardware transmitter if necessary */
1632 spin_lock_irqsave(&info->lock,flags);
1633 if (!info->tx_active)
1634 tx_start(info);
1635 spin_unlock_irqrestore(&info->lock,flags);
1636
1637 return NETDEV_TX_OK;
1638}
1639
1640/**
1641 * called by network layer when interface enabled
1642 * claim resources and initialize hardware
1643 *
1644 * dev pointer to network device structure
1645 *
1646 * returns 0 if success, otherwise error code
1647 */
1648static int hdlcdev_open(struct net_device *dev)
1649{
1650 SLMP_INFO *info = dev_to_port(dev);
1651 int rc;
1652 unsigned long flags;
1653
1654 if (debug_level >= DEBUG_LEVEL_INFO)
1655 printk("%s:hdlcdev_open(%s)\n",__FILE__,dev->name);
1656
1657 /* generic HDLC layer open processing */
1658 if ((rc = hdlc_open(dev)))
1659 return rc;
1660
1661 /* arbitrate between network and tty opens */
1662 spin_lock_irqsave(&info->netlock, flags);
1663 if (info->port.count != 0 || info->netcount != 0) {
1664 printk(KERN_WARNING "%s: hdlc_open returning busy\n", dev->name);
1665 spin_unlock_irqrestore(&info->netlock, flags);
1666 return -EBUSY;
1667 }
1668 info->netcount=1;
1669 spin_unlock_irqrestore(&info->netlock, flags);
1670
1671 /* claim resources and init adapter */
1672 if ((rc = startup(info)) != 0) {
1673 spin_lock_irqsave(&info->netlock, flags);
1674 info->netcount=0;
1675 spin_unlock_irqrestore(&info->netlock, flags);
1676 return rc;
1677 }
1678
1679 /* assert DTR and RTS, apply hardware settings */
1680 info->serial_signals |= SerialSignal_RTS + SerialSignal_DTR;
1681 program_hw(info);
1682
1683 /* enable network layer transmit */
1684 dev->trans_start = jiffies;
1685 netif_start_queue(dev);
1686
1687 /* inform generic HDLC layer of current DCD status */
1688 spin_lock_irqsave(&info->lock, flags);
1689 get_signals(info);
1690 spin_unlock_irqrestore(&info->lock, flags);
1691 if (info->serial_signals & SerialSignal_DCD)
1692 netif_carrier_on(dev);
1693 else
1694 netif_carrier_off(dev);
1695 return 0;
1696}
1697
1698/**
1699 * called by network layer when interface is disabled
1700 * shutdown hardware and release resources
1701 *
1702 * dev pointer to network device structure
1703 *
1704 * returns 0 if success, otherwise error code
1705 */
1706static int hdlcdev_close(struct net_device *dev)
1707{
1708 SLMP_INFO *info = dev_to_port(dev);
1709 unsigned long flags;
1710
1711 if (debug_level >= DEBUG_LEVEL_INFO)
1712 printk("%s:hdlcdev_close(%s)\n",__FILE__,dev->name);
1713
1714 netif_stop_queue(dev);
1715
1716 /* shutdown adapter and release resources */
1717 shutdown(info);
1718
1719 hdlc_close(dev);
1720
1721 spin_lock_irqsave(&info->netlock, flags);
1722 info->netcount=0;
1723 spin_unlock_irqrestore(&info->netlock, flags);
1724
1725 return 0;
1726}
1727
1728/**
1729 * called by network layer to process IOCTL call to network device
1730 *
1731 * dev pointer to network device structure
1732 * ifr pointer to network interface request structure
1733 * cmd IOCTL command code
1734 *
1735 * returns 0 if success, otherwise error code
1736 */
1737static int hdlcdev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1738{
1739 const size_t size = sizeof(sync_serial_settings);
1740 sync_serial_settings new_line;
1741 sync_serial_settings __user *line = ifr->ifr_settings.ifs_ifsu.sync;
1742 SLMP_INFO *info = dev_to_port(dev);
1743 unsigned int flags;
1744
1745 if (debug_level >= DEBUG_LEVEL_INFO)
1746 printk("%s:hdlcdev_ioctl(%s)\n",__FILE__,dev->name);
1747
1748 /* return error if TTY interface open */
1749 if (info->port.count)
1750 return -EBUSY;
1751
1752 if (cmd != SIOCWANDEV)
1753 return hdlc_ioctl(dev, ifr, cmd);
1754
1755 switch(ifr->ifr_settings.type) {
1756 case IF_GET_IFACE: /* return current sync_serial_settings */
1757
1758 ifr->ifr_settings.type = IF_IFACE_SYNC_SERIAL;
1759 if (ifr->ifr_settings.size < size) {
1760 ifr->ifr_settings.size = size; /* data size wanted */
1761 return -ENOBUFS;
1762 }
1763
1764 flags = info->params.flags & (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
1765 HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
1766 HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
1767 HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN);
1768
1769 switch (flags){
1770 case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_TXCPIN): new_line.clock_type = CLOCK_EXT; break;
1771 case (HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG): new_line.clock_type = CLOCK_INT; break;
1772 case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_BRG): new_line.clock_type = CLOCK_TXINT; break;
1773 case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_RXCPIN): new_line.clock_type = CLOCK_TXFROMRX; break;
1774 default: new_line.clock_type = CLOCK_DEFAULT;
1775 }
1776
1777 new_line.clock_rate = info->params.clock_speed;
1778 new_line.loopback = info->params.loopback ? 1:0;
1779
1780 if (copy_to_user(line, &new_line, size))
1781 return -EFAULT;
1782 return 0;
1783
1784 case IF_IFACE_SYNC_SERIAL: /* set sync_serial_settings */
1785
1786 if(!capable(CAP_NET_ADMIN))
1787 return -EPERM;
1788 if (copy_from_user(&new_line, line, size))
1789 return -EFAULT;
1790
1791 switch (new_line.clock_type)
1792 {
1793 case CLOCK_EXT: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_TXCPIN; break;
1794 case CLOCK_TXFROMRX: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_RXCPIN; break;
1795 case CLOCK_INT: flags = HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG; break;
1796 case CLOCK_TXINT: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_BRG; break;
1797 case CLOCK_DEFAULT: flags = info->params.flags &
1798 (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
1799 HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
1800 HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
1801 HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN); break;
1802 default: return -EINVAL;
1803 }
1804
1805 if (new_line.loopback != 0 && new_line.loopback != 1)
1806 return -EINVAL;
1807
1808 info->params.flags &= ~(HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
1809 HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
1810 HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
1811 HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN);
1812 info->params.flags |= flags;
1813
1814 info->params.loopback = new_line.loopback;
1815
1816 if (flags & (HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG))
1817 info->params.clock_speed = new_line.clock_rate;
1818 else
1819 info->params.clock_speed = 0;
1820
1821 /* if network interface up, reprogram hardware */
1822 if (info->netcount)
1823 program_hw(info);
1824 return 0;
1825
1826 default:
1827 return hdlc_ioctl(dev, ifr, cmd);
1828 }
1829}
1830
1831/**
1832 * called by network layer when transmit timeout is detected
1833 *
1834 * dev pointer to network device structure
1835 */
1836static void hdlcdev_tx_timeout(struct net_device *dev)
1837{
1838 SLMP_INFO *info = dev_to_port(dev);
1839 unsigned long flags;
1840
1841 if (debug_level >= DEBUG_LEVEL_INFO)
1842 printk("hdlcdev_tx_timeout(%s)\n",dev->name);
1843
1844 dev->stats.tx_errors++;
1845 dev->stats.tx_aborted_errors++;
1846
1847 spin_lock_irqsave(&info->lock,flags);
1848 tx_stop(info);
1849 spin_unlock_irqrestore(&info->lock,flags);
1850
1851 netif_wake_queue(dev);
1852}
1853
1854/**
1855 * called by device driver when transmit completes
1856 * reenable network layer transmit if stopped
1857 *
1858 * info pointer to device instance information
1859 */
1860static void hdlcdev_tx_done(SLMP_INFO *info)
1861{
1862 if (netif_queue_stopped(info->netdev))
1863 netif_wake_queue(info->netdev);
1864}
1865
1866/**
1867 * called by device driver when frame received
1868 * pass frame to network layer
1869 *
1870 * info pointer to device instance information
1871 * buf pointer to buffer contianing frame data
1872 * size count of data bytes in buf
1873 */
1874static void hdlcdev_rx(SLMP_INFO *info, char *buf, int size)
1875{
1876 struct sk_buff *skb = dev_alloc_skb(size);
1877 struct net_device *dev = info->netdev;
1878
1879 if (debug_level >= DEBUG_LEVEL_INFO)
1880 printk("hdlcdev_rx(%s)\n",dev->name);
1881
1882 if (skb == NULL) {
1883 printk(KERN_NOTICE "%s: can't alloc skb, dropping packet\n",
1884 dev->name);
1885 dev->stats.rx_dropped++;
1886 return;
1887 }
1888
1889 memcpy(skb_put(skb, size), buf, size);
1890
1891 skb->protocol = hdlc_type_trans(skb, dev);
1892
1893 dev->stats.rx_packets++;
1894 dev->stats.rx_bytes += size;
1895
1896 netif_rx(skb);
1897}
1898
1899static const struct net_device_ops hdlcdev_ops = {
1900 .ndo_open = hdlcdev_open,
1901 .ndo_stop = hdlcdev_close,
1902 .ndo_change_mtu = hdlc_change_mtu,
1903 .ndo_start_xmit = hdlc_start_xmit,
1904 .ndo_do_ioctl = hdlcdev_ioctl,
1905 .ndo_tx_timeout = hdlcdev_tx_timeout,
1906};
1907
1908/**
1909 * called by device driver when adding device instance
1910 * do generic HDLC initialization
1911 *
1912 * info pointer to device instance information
1913 *
1914 * returns 0 if success, otherwise error code
1915 */
1916static int hdlcdev_init(SLMP_INFO *info)
1917{
1918 int rc;
1919 struct net_device *dev;
1920 hdlc_device *hdlc;
1921
1922 /* allocate and initialize network and HDLC layer objects */
1923
1924 if (!(dev = alloc_hdlcdev(info))) {
1925 printk(KERN_ERR "%s:hdlc device allocation failure\n",__FILE__);
1926 return -ENOMEM;
1927 }
1928
1929 /* for network layer reporting purposes only */
1930 dev->mem_start = info->phys_sca_base;
1931 dev->mem_end = info->phys_sca_base + SCA_BASE_SIZE - 1;
1932 dev->irq = info->irq_level;
1933
1934 /* network layer callbacks and settings */
1935 dev->netdev_ops = &hdlcdev_ops;
1936 dev->watchdog_timeo = 10 * HZ;
1937 dev->tx_queue_len = 50;
1938
1939 /* generic HDLC layer callbacks and settings */
1940 hdlc = dev_to_hdlc(dev);
1941 hdlc->attach = hdlcdev_attach;
1942 hdlc->xmit = hdlcdev_xmit;
1943
1944 /* register objects with HDLC layer */
1945 if ((rc = register_hdlc_device(dev))) {
1946 printk(KERN_WARNING "%s:unable to register hdlc device\n",__FILE__);
1947 free_netdev(dev);
1948 return rc;
1949 }
1950
1951 info->netdev = dev;
1952 return 0;
1953}
1954
1955/**
1956 * called by device driver when removing device instance
1957 * do generic HDLC cleanup
1958 *
1959 * info pointer to device instance information
1960 */
1961static void hdlcdev_exit(SLMP_INFO *info)
1962{
1963 unregister_hdlc_device(info->netdev);
1964 free_netdev(info->netdev);
1965 info->netdev = NULL;
1966}
1967
1968#endif /* CONFIG_HDLC */
1969
1970
1971/* Return next bottom half action to perform.
1972 * Return Value: BH action code or 0 if nothing to do.
1973 */
1974static int bh_action(SLMP_INFO *info)
1975{
1976 unsigned long flags;
1977 int rc = 0;
1978
1979 spin_lock_irqsave(&info->lock,flags);
1980
1981 if (info->pending_bh & BH_RECEIVE) {
1982 info->pending_bh &= ~BH_RECEIVE;
1983 rc = BH_RECEIVE;
1984 } else if (info->pending_bh & BH_TRANSMIT) {
1985 info->pending_bh &= ~BH_TRANSMIT;
1986 rc = BH_TRANSMIT;
1987 } else if (info->pending_bh & BH_STATUS) {
1988 info->pending_bh &= ~BH_STATUS;
1989 rc = BH_STATUS;
1990 }
1991
1992 if (!rc) {
1993 /* Mark BH routine as complete */
1994 info->bh_running = false;
1995 info->bh_requested = false;
1996 }
1997
1998 spin_unlock_irqrestore(&info->lock,flags);
1999
2000 return rc;
2001}
2002
2003/* Perform bottom half processing of work items queued by ISR.
2004 */
2005static void bh_handler(struct work_struct *work)
2006{
2007 SLMP_INFO *info = container_of(work, SLMP_INFO, task);
2008 int action;
2009
2010 if (!info)
2011 return;
2012
2013 if ( debug_level >= DEBUG_LEVEL_BH )
2014 printk( "%s(%d):%s bh_handler() entry\n",
2015 __FILE__,__LINE__,info->device_name);
2016
2017 info->bh_running = true;
2018
2019 while((action = bh_action(info)) != 0) {
2020
2021 /* Process work item */
2022 if ( debug_level >= DEBUG_LEVEL_BH )
2023 printk( "%s(%d):%s bh_handler() work item action=%d\n",
2024 __FILE__,__LINE__,info->device_name, action);
2025
2026 switch (action) {
2027
2028 case BH_RECEIVE:
2029 bh_receive(info);
2030 break;
2031 case BH_TRANSMIT:
2032 bh_transmit(info);
2033 break;
2034 case BH_STATUS:
2035 bh_status(info);
2036 break;
2037 default:
2038 /* unknown work item ID */
2039 printk("%s(%d):%s Unknown work item ID=%08X!\n",
2040 __FILE__,__LINE__,info->device_name,action);
2041 break;
2042 }
2043 }
2044
2045 if ( debug_level >= DEBUG_LEVEL_BH )
2046 printk( "%s(%d):%s bh_handler() exit\n",
2047 __FILE__,__LINE__,info->device_name);
2048}
2049
2050static void bh_receive(SLMP_INFO *info)
2051{
2052 if ( debug_level >= DEBUG_LEVEL_BH )
2053 printk( "%s(%d):%s bh_receive()\n",
2054 __FILE__,__LINE__,info->device_name);
2055
2056 while( rx_get_frame(info) );
2057}
2058
2059static void bh_transmit(SLMP_INFO *info)
2060{
2061 struct tty_struct *tty = info->port.tty;
2062
2063 if ( debug_level >= DEBUG_LEVEL_BH )
2064 printk( "%s(%d):%s bh_transmit() entry\n",
2065 __FILE__,__LINE__,info->device_name);
2066
2067 if (tty)
2068 tty_wakeup(tty);
2069}
2070
2071static void bh_status(SLMP_INFO *info)
2072{
2073 if ( debug_level >= DEBUG_LEVEL_BH )
2074 printk( "%s(%d):%s bh_status() entry\n",
2075 __FILE__,__LINE__,info->device_name);
2076
2077 info->ri_chkcount = 0;
2078 info->dsr_chkcount = 0;
2079 info->dcd_chkcount = 0;
2080 info->cts_chkcount = 0;
2081}
2082
2083static void isr_timer(SLMP_INFO * info)
2084{
2085 unsigned char timer = (info->port_num & 1) ? TIMER2 : TIMER0;
2086
2087 /* IER2<7..4> = timer<3..0> interrupt enables (0=disabled) */
2088 write_reg(info, IER2, 0);
2089
2090 /* TMCS, Timer Control/Status Register
2091 *
2092 * 07 CMF, Compare match flag (read only) 1=match
2093 * 06 ECMI, CMF Interrupt Enable: 0=disabled
2094 * 05 Reserved, must be 0
2095 * 04 TME, Timer Enable
2096 * 03..00 Reserved, must be 0
2097 *
2098 * 0000 0000
2099 */
2100 write_reg(info, (unsigned char)(timer + TMCS), 0);
2101
2102 info->irq_occurred = true;
2103
2104 if ( debug_level >= DEBUG_LEVEL_ISR )
2105 printk("%s(%d):%s isr_timer()\n",
2106 __FILE__,__LINE__,info->device_name);
2107}
2108
2109static void isr_rxint(SLMP_INFO * info)
2110{
2111 struct tty_struct *tty = info->port.tty;
2112 struct mgsl_icount *icount = &info->icount;
2113 unsigned char status = read_reg(info, SR1) & info->ie1_value & (FLGD + IDLD + CDCD + BRKD);
2114 unsigned char status2 = read_reg(info, SR2) & info->ie2_value & OVRN;
2115
2116 /* clear status bits */
2117 if (status)
2118 write_reg(info, SR1, status);
2119
2120 if (status2)
2121 write_reg(info, SR2, status2);
2122
2123 if ( debug_level >= DEBUG_LEVEL_ISR )
2124 printk("%s(%d):%s isr_rxint status=%02X %02x\n",
2125 __FILE__,__LINE__,info->device_name,status,status2);
2126
2127 if (info->params.mode == MGSL_MODE_ASYNC) {
2128 if (status & BRKD) {
2129 icount->brk++;
2130
2131 /* process break detection if tty control
2132 * is not set to ignore it
2133 */
2134 if ( tty ) {
2135 if (!(status & info->ignore_status_mask1)) {
2136 if (info->read_status_mask1 & BRKD) {
2137 tty_insert_flip_char(tty, 0, TTY_BREAK);
2138 if (info->port.flags & ASYNC_SAK)
2139 do_SAK(tty);
2140 }
2141 }
2142 }
2143 }
2144 }
2145 else {
2146 if (status & (FLGD|IDLD)) {
2147 if (status & FLGD)
2148 info->icount.exithunt++;
2149 else if (status & IDLD)
2150 info->icount.rxidle++;
2151 wake_up_interruptible(&info->event_wait_q);
2152 }
2153 }
2154
2155 if (status & CDCD) {
2156 /* simulate a common modem status change interrupt
2157 * for our handler
2158 */
2159 get_signals( info );
2160 isr_io_pin(info,
2161 MISCSTATUS_DCD_LATCHED|(info->serial_signals&SerialSignal_DCD));
2162 }
2163}
2164
2165/*
2166 * handle async rx data interrupts
2167 */
2168static void isr_rxrdy(SLMP_INFO * info)
2169{
2170 u16 status;
2171 unsigned char DataByte;
2172 struct tty_struct *tty = info->port.tty;
2173 struct mgsl_icount *icount = &info->icount;
2174
2175 if ( debug_level >= DEBUG_LEVEL_ISR )
2176 printk("%s(%d):%s isr_rxrdy\n",
2177 __FILE__,__LINE__,info->device_name);
2178
2179 while((status = read_reg(info,CST0)) & BIT0)
2180 {
2181 int flag = 0;
2182 bool over = false;
2183 DataByte = read_reg(info,TRB);
2184
2185 icount->rx++;
2186
2187 if ( status & (PE + FRME + OVRN) ) {
2188 printk("%s(%d):%s rxerr=%04X\n",
2189 __FILE__,__LINE__,info->device_name,status);
2190
2191 /* update error statistics */
2192 if (status & PE)
2193 icount->parity++;
2194 else if (status & FRME)
2195 icount->frame++;
2196 else if (status & OVRN)
2197 icount->overrun++;
2198
2199 /* discard char if tty control flags say so */
2200 if (status & info->ignore_status_mask2)
2201 continue;
2202
2203 status &= info->read_status_mask2;
2204
2205 if ( tty ) {
2206 if (status & PE)
2207 flag = TTY_PARITY;
2208 else if (status & FRME)
2209 flag = TTY_FRAME;
2210 if (status & OVRN) {
2211 /* Overrun is special, since it's
2212 * reported immediately, and doesn't
2213 * affect the current character
2214 */
2215 over = true;
2216 }
2217 }
2218 } /* end of if (error) */
2219
2220 if ( tty ) {
2221 tty_insert_flip_char(tty, DataByte, flag);
2222 if (over)
2223 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
2224 }
2225 }
2226
2227 if ( debug_level >= DEBUG_LEVEL_ISR ) {
2228 printk("%s(%d):%s rx=%d brk=%d parity=%d frame=%d overrun=%d\n",
2229 __FILE__,__LINE__,info->device_name,
2230 icount->rx,icount->brk,icount->parity,
2231 icount->frame,icount->overrun);
2232 }
2233
2234 if ( tty )
2235 tty_flip_buffer_push(tty);
2236}
2237
2238static void isr_txeom(SLMP_INFO * info, unsigned char status)
2239{
2240 if ( debug_level >= DEBUG_LEVEL_ISR )
2241 printk("%s(%d):%s isr_txeom status=%02x\n",
2242 __FILE__,__LINE__,info->device_name,status);
2243
2244 write_reg(info, TXDMA + DIR, 0x00); /* disable Tx DMA IRQs */
2245 write_reg(info, TXDMA + DSR, 0xc0); /* clear IRQs and disable DMA */
2246 write_reg(info, TXDMA + DCMD, SWABORT); /* reset/init DMA channel */
2247
2248 if (status & UDRN) {
2249 write_reg(info, CMD, TXRESET);
2250 write_reg(info, CMD, TXENABLE);
2251 } else
2252 write_reg(info, CMD, TXBUFCLR);
2253
2254 /* disable and clear tx interrupts */
2255 info->ie0_value &= ~TXRDYE;
2256 info->ie1_value &= ~(IDLE + UDRN);
2257 write_reg16(info, IE0, (unsigned short)((info->ie1_value << 8) + info->ie0_value));
2258 write_reg(info, SR1, (unsigned char)(UDRN + IDLE));
2259
2260 if ( info->tx_active ) {
2261 if (info->params.mode != MGSL_MODE_ASYNC) {
2262 if (status & UDRN)
2263 info->icount.txunder++;
2264 else if (status & IDLE)
2265 info->icount.txok++;
2266 }
2267
2268 info->tx_active = false;
2269 info->tx_count = info->tx_put = info->tx_get = 0;
2270
2271 del_timer(&info->tx_timer);
2272
2273 if (info->params.mode != MGSL_MODE_ASYNC && info->drop_rts_on_tx_done ) {
2274 info->serial_signals &= ~SerialSignal_RTS;
2275 info->drop_rts_on_tx_done = false;
2276 set_signals(info);
2277 }
2278
2279#if SYNCLINK_GENERIC_HDLC
2280 if (info->netcount)
2281 hdlcdev_tx_done(info);
2282 else
2283#endif
2284 {
2285 if (info->port.tty && (info->port.tty->stopped || info->port.tty->hw_stopped)) {
2286 tx_stop(info);
2287 return;
2288 }
2289 info->pending_bh |= BH_TRANSMIT;
2290 }
2291 }
2292}
2293
2294
2295/*
2296 * handle tx status interrupts
2297 */
2298static void isr_txint(SLMP_INFO * info)
2299{
2300 unsigned char status = read_reg(info, SR1) & info->ie1_value & (UDRN + IDLE + CCTS);
2301
2302 /* clear status bits */
2303 write_reg(info, SR1, status);
2304
2305 if ( debug_level >= DEBUG_LEVEL_ISR )
2306 printk("%s(%d):%s isr_txint status=%02x\n",
2307 __FILE__,__LINE__,info->device_name,status);
2308
2309 if (status & (UDRN + IDLE))
2310 isr_txeom(info, status);
2311
2312 if (status & CCTS) {
2313 /* simulate a common modem status change interrupt
2314 * for our handler
2315 */
2316 get_signals( info );
2317 isr_io_pin(info,
2318 MISCSTATUS_CTS_LATCHED|(info->serial_signals&SerialSignal_CTS));
2319
2320 }
2321}
2322
2323/*
2324 * handle async tx data interrupts
2325 */
2326static void isr_txrdy(SLMP_INFO * info)
2327{
2328 if ( debug_level >= DEBUG_LEVEL_ISR )
2329 printk("%s(%d):%s isr_txrdy() tx_count=%d\n",
2330 __FILE__,__LINE__,info->device_name,info->tx_count);
2331
2332 if (info->params.mode != MGSL_MODE_ASYNC) {
2333 /* disable TXRDY IRQ, enable IDLE IRQ */
2334 info->ie0_value &= ~TXRDYE;
2335 info->ie1_value |= IDLE;
2336 write_reg16(info, IE0, (unsigned short)((info->ie1_value << 8) + info->ie0_value));
2337 return;
2338 }
2339
2340 if (info->port.tty && (info->port.tty->stopped || info->port.tty->hw_stopped)) {
2341 tx_stop(info);
2342 return;
2343 }
2344
2345 if ( info->tx_count )
2346 tx_load_fifo( info );
2347 else {
2348 info->tx_active = false;
2349 info->ie0_value &= ~TXRDYE;
2350 write_reg(info, IE0, info->ie0_value);
2351 }
2352
2353 if (info->tx_count < WAKEUP_CHARS)
2354 info->pending_bh |= BH_TRANSMIT;
2355}
2356
2357static void isr_rxdmaok(SLMP_INFO * info)
2358{
2359 /* BIT7 = EOT (end of transfer)
2360 * BIT6 = EOM (end of message/frame)
2361 */
2362 unsigned char status = read_reg(info,RXDMA + DSR) & 0xc0;
2363
2364 /* clear IRQ (BIT0 must be 1 to prevent clearing DE bit) */
2365 write_reg(info, RXDMA + DSR, (unsigned char)(status | 1));
2366
2367 if ( debug_level >= DEBUG_LEVEL_ISR )
2368 printk("%s(%d):%s isr_rxdmaok(), status=%02x\n",
2369 __FILE__,__LINE__,info->device_name,status);
2370
2371 info->pending_bh |= BH_RECEIVE;
2372}
2373
2374static void isr_rxdmaerror(SLMP_INFO * info)
2375{
2376 /* BIT5 = BOF (buffer overflow)
2377 * BIT4 = COF (counter overflow)
2378 */
2379 unsigned char status = read_reg(info,RXDMA + DSR) & 0x30;
2380
2381 /* clear IRQ (BIT0 must be 1 to prevent clearing DE bit) */
2382 write_reg(info, RXDMA + DSR, (unsigned char)(status | 1));
2383
2384 if ( debug_level >= DEBUG_LEVEL_ISR )
2385 printk("%s(%d):%s isr_rxdmaerror(), status=%02x\n",
2386 __FILE__,__LINE__,info->device_name,status);
2387
2388 info->rx_overflow = true;
2389 info->pending_bh |= BH_RECEIVE;
2390}
2391
2392static void isr_txdmaok(SLMP_INFO * info)
2393{
2394 unsigned char status_reg1 = read_reg(info, SR1);
2395
2396 write_reg(info, TXDMA + DIR, 0x00); /* disable Tx DMA IRQs */
2397 write_reg(info, TXDMA + DSR, 0xc0); /* clear IRQs and disable DMA */
2398 write_reg(info, TXDMA + DCMD, SWABORT); /* reset/init DMA channel */
2399
2400 if ( debug_level >= DEBUG_LEVEL_ISR )
2401 printk("%s(%d):%s isr_txdmaok(), status=%02x\n",
2402 __FILE__,__LINE__,info->device_name,status_reg1);
2403
2404 /* program TXRDY as FIFO empty flag, enable TXRDY IRQ */
2405 write_reg16(info, TRC0, 0);
2406 info->ie0_value |= TXRDYE;
2407 write_reg(info, IE0, info->ie0_value);
2408}
2409
2410static void isr_txdmaerror(SLMP_INFO * info)
2411{
2412 /* BIT5 = BOF (buffer overflow)
2413 * BIT4 = COF (counter overflow)
2414 */
2415 unsigned char status = read_reg(info,TXDMA + DSR) & 0x30;
2416
2417 /* clear IRQ (BIT0 must be 1 to prevent clearing DE bit) */
2418 write_reg(info, TXDMA + DSR, (unsigned char)(status | 1));
2419
2420 if ( debug_level >= DEBUG_LEVEL_ISR )
2421 printk("%s(%d):%s isr_txdmaerror(), status=%02x\n",
2422 __FILE__,__LINE__,info->device_name,status);
2423}
2424
2425/* handle input serial signal changes
2426 */
2427static void isr_io_pin( SLMP_INFO *info, u16 status )
2428{
2429 struct mgsl_icount *icount;
2430
2431 if ( debug_level >= DEBUG_LEVEL_ISR )
2432 printk("%s(%d):isr_io_pin status=%04X\n",
2433 __FILE__,__LINE__,status);
2434
2435 if (status & (MISCSTATUS_CTS_LATCHED | MISCSTATUS_DCD_LATCHED |
2436 MISCSTATUS_DSR_LATCHED | MISCSTATUS_RI_LATCHED) ) {
2437 icount = &info->icount;
2438 /* update input line counters */
2439 if (status & MISCSTATUS_RI_LATCHED) {
2440 icount->rng++;
2441 if ( status & SerialSignal_RI )
2442 info->input_signal_events.ri_up++;
2443 else
2444 info->input_signal_events.ri_down++;
2445 }
2446 if (status & MISCSTATUS_DSR_LATCHED) {
2447 icount->dsr++;
2448 if ( status & SerialSignal_DSR )
2449 info->input_signal_events.dsr_up++;
2450 else
2451 info->input_signal_events.dsr_down++;
2452 }
2453 if (status & MISCSTATUS_DCD_LATCHED) {
2454 if ((info->dcd_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT) {
2455 info->ie1_value &= ~CDCD;
2456 write_reg(info, IE1, info->ie1_value);
2457 }
2458 icount->dcd++;
2459 if (status & SerialSignal_DCD) {
2460 info->input_signal_events.dcd_up++;
2461 } else
2462 info->input_signal_events.dcd_down++;
2463#if SYNCLINK_GENERIC_HDLC
2464 if (info->netcount) {
2465 if (status & SerialSignal_DCD)
2466 netif_carrier_on(info->netdev);
2467 else
2468 netif_carrier_off(info->netdev);
2469 }
2470#endif
2471 }
2472 if (status & MISCSTATUS_CTS_LATCHED)
2473 {
2474 if ((info->cts_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT) {
2475 info->ie1_value &= ~CCTS;
2476 write_reg(info, IE1, info->ie1_value);
2477 }
2478 icount->cts++;
2479 if ( status & SerialSignal_CTS )
2480 info->input_signal_events.cts_up++;
2481 else
2482 info->input_signal_events.cts_down++;
2483 }
2484 wake_up_interruptible(&info->status_event_wait_q);
2485 wake_up_interruptible(&info->event_wait_q);
2486
2487 if ( (info->port.flags & ASYNC_CHECK_CD) &&
2488 (status & MISCSTATUS_DCD_LATCHED) ) {
2489 if ( debug_level >= DEBUG_LEVEL_ISR )
2490 printk("%s CD now %s...", info->device_name,
2491 (status & SerialSignal_DCD) ? "on" : "off");
2492 if (status & SerialSignal_DCD)
2493 wake_up_interruptible(&info->port.open_wait);
2494 else {
2495 if ( debug_level >= DEBUG_LEVEL_ISR )
2496 printk("doing serial hangup...");
2497 if (info->port.tty)
2498 tty_hangup(info->port.tty);
2499 }
2500 }
2501
2502 if ( (info->port.flags & ASYNC_CTS_FLOW) &&
2503 (status & MISCSTATUS_CTS_LATCHED) ) {
2504 if ( info->port.tty ) {
2505 if (info->port.tty->hw_stopped) {
2506 if (status & SerialSignal_CTS) {
2507 if ( debug_level >= DEBUG_LEVEL_ISR )
2508 printk("CTS tx start...");
2509 info->port.tty->hw_stopped = 0;
2510 tx_start(info);
2511 info->pending_bh |= BH_TRANSMIT;
2512 return;
2513 }
2514 } else {
2515 if (!(status & SerialSignal_CTS)) {
2516 if ( debug_level >= DEBUG_LEVEL_ISR )
2517 printk("CTS tx stop...");
2518 info->port.tty->hw_stopped = 1;
2519 tx_stop(info);
2520 }
2521 }
2522 }
2523 }
2524 }
2525
2526 info->pending_bh |= BH_STATUS;
2527}
2528
2529/* Interrupt service routine entry point.
2530 *
2531 * Arguments:
2532 * irq interrupt number that caused interrupt
2533 * dev_id device ID supplied during interrupt registration
2534 * regs interrupted processor context
2535 */
2536static irqreturn_t synclinkmp_interrupt(int dummy, void *dev_id)
2537{
2538 SLMP_INFO *info = dev_id;
2539 unsigned char status, status0, status1=0;
2540 unsigned char dmastatus, dmastatus0, dmastatus1=0;
2541 unsigned char timerstatus0, timerstatus1=0;
2542 unsigned char shift;
2543 unsigned int i;
2544 unsigned short tmp;
2545
2546 if ( debug_level >= DEBUG_LEVEL_ISR )
2547 printk(KERN_DEBUG "%s(%d): synclinkmp_interrupt(%d)entry.\n",
2548 __FILE__, __LINE__, info->irq_level);
2549
2550 spin_lock(&info->lock);
2551
2552 for(;;) {
2553
2554 /* get status for SCA0 (ports 0-1) */
2555 tmp = read_reg16(info, ISR0); /* get ISR0 and ISR1 in one read */
2556 status0 = (unsigned char)tmp;
2557 dmastatus0 = (unsigned char)(tmp>>8);
2558 timerstatus0 = read_reg(info, ISR2);
2559
2560 if ( debug_level >= DEBUG_LEVEL_ISR )
2561 printk(KERN_DEBUG "%s(%d):%s status0=%02x, dmastatus0=%02x, timerstatus0=%02x\n",
2562 __FILE__, __LINE__, info->device_name,
2563 status0, dmastatus0, timerstatus0);
2564
2565 if (info->port_count == 4) {
2566 /* get status for SCA1 (ports 2-3) */
2567 tmp = read_reg16(info->port_array[2], ISR0);
2568 status1 = (unsigned char)tmp;
2569 dmastatus1 = (unsigned char)(tmp>>8);
2570 timerstatus1 = read_reg(info->port_array[2], ISR2);
2571
2572 if ( debug_level >= DEBUG_LEVEL_ISR )
2573 printk("%s(%d):%s status1=%02x, dmastatus1=%02x, timerstatus1=%02x\n",
2574 __FILE__,__LINE__,info->device_name,
2575 status1,dmastatus1,timerstatus1);
2576 }
2577
2578 if (!status0 && !dmastatus0 && !timerstatus0 &&
2579 !status1 && !dmastatus1 && !timerstatus1)
2580 break;
2581
2582 for(i=0; i < info->port_count ; i++) {
2583 if (info->port_array[i] == NULL)
2584 continue;
2585 if (i < 2) {
2586 status = status0;
2587 dmastatus = dmastatus0;
2588 } else {
2589 status = status1;
2590 dmastatus = dmastatus1;
2591 }
2592
2593 shift = i & 1 ? 4 :0;
2594
2595 if (status & BIT0 << shift)
2596 isr_rxrdy(info->port_array[i]);
2597 if (status & BIT1 << shift)
2598 isr_txrdy(info->port_array[i]);
2599 if (status & BIT2 << shift)
2600 isr_rxint(info->port_array[i]);
2601 if (status & BIT3 << shift)
2602 isr_txint(info->port_array[i]);
2603
2604 if (dmastatus & BIT0 << shift)
2605 isr_rxdmaerror(info->port_array[i]);
2606 if (dmastatus & BIT1 << shift)
2607 isr_rxdmaok(info->port_array[i]);
2608 if (dmastatus & BIT2 << shift)
2609 isr_txdmaerror(info->port_array[i]);
2610 if (dmastatus & BIT3 << shift)
2611 isr_txdmaok(info->port_array[i]);
2612 }
2613
2614 if (timerstatus0 & (BIT5 | BIT4))
2615 isr_timer(info->port_array[0]);
2616 if (timerstatus0 & (BIT7 | BIT6))
2617 isr_timer(info->port_array[1]);
2618 if (timerstatus1 & (BIT5 | BIT4))
2619 isr_timer(info->port_array[2]);
2620 if (timerstatus1 & (BIT7 | BIT6))
2621 isr_timer(info->port_array[3]);
2622 }
2623
2624 for(i=0; i < info->port_count ; i++) {
2625 SLMP_INFO * port = info->port_array[i];
2626
2627 /* Request bottom half processing if there's something
2628 * for it to do and the bh is not already running.
2629 *
2630 * Note: startup adapter diags require interrupts.
2631 * do not request bottom half processing if the
2632 * device is not open in a normal mode.
2633 */
2634 if ( port && (port->port.count || port->netcount) &&
2635 port->pending_bh && !port->bh_running &&
2636 !port->bh_requested ) {
2637 if ( debug_level >= DEBUG_LEVEL_ISR )
2638 printk("%s(%d):%s queueing bh task.\n",
2639 __FILE__,__LINE__,port->device_name);
2640 schedule_work(&port->task);
2641 port->bh_requested = true;
2642 }
2643 }
2644
2645 spin_unlock(&info->lock);
2646
2647 if ( debug_level >= DEBUG_LEVEL_ISR )
2648 printk(KERN_DEBUG "%s(%d):synclinkmp_interrupt(%d)exit.\n",
2649 __FILE__, __LINE__, info->irq_level);
2650 return IRQ_HANDLED;
2651}
2652
2653/* Initialize and start device.
2654 */
2655static int startup(SLMP_INFO * info)
2656{
2657 if ( debug_level >= DEBUG_LEVEL_INFO )
2658 printk("%s(%d):%s tx_releaseup()\n",__FILE__,__LINE__,info->device_name);
2659
2660 if (info->port.flags & ASYNC_INITIALIZED)
2661 return 0;
2662
2663 if (!info->tx_buf) {
2664 info->tx_buf = kmalloc(info->max_frame_size, GFP_KERNEL);
2665 if (!info->tx_buf) {
2666 printk(KERN_ERR"%s(%d):%s can't allocate transmit buffer\n",
2667 __FILE__,__LINE__,info->device_name);
2668 return -ENOMEM;
2669 }
2670 }
2671
2672 info->pending_bh = 0;
2673
2674 memset(&info->icount, 0, sizeof(info->icount));
2675
2676 /* program hardware for current parameters */
2677 reset_port(info);
2678
2679 change_params(info);
2680
2681 mod_timer(&info->status_timer, jiffies + msecs_to_jiffies(10));
2682
2683 if (info->port.tty)
2684 clear_bit(TTY_IO_ERROR, &info->port.tty->flags);
2685
2686 info->port.flags |= ASYNC_INITIALIZED;
2687
2688 return 0;
2689}
2690
2691/* Called by close() and hangup() to shutdown hardware
2692 */
2693static void shutdown(SLMP_INFO * info)
2694{
2695 unsigned long flags;
2696
2697 if (!(info->port.flags & ASYNC_INITIALIZED))
2698 return;
2699
2700 if (debug_level >= DEBUG_LEVEL_INFO)
2701 printk("%s(%d):%s synclinkmp_shutdown()\n",
2702 __FILE__,__LINE__, info->device_name );
2703
2704 /* clear status wait queue because status changes */
2705 /* can't happen after shutting down the hardware */
2706 wake_up_interruptible(&info->status_event_wait_q);
2707 wake_up_interruptible(&info->event_wait_q);
2708
2709 del_timer(&info->tx_timer);
2710 del_timer(&info->status_timer);
2711
2712 kfree(info->tx_buf);
2713 info->tx_buf = NULL;
2714
2715 spin_lock_irqsave(&info->lock,flags);
2716
2717 reset_port(info);
2718
2719 if (!info->port.tty || info->port.tty->termios->c_cflag & HUPCL) {
2720 info->serial_signals &= ~(SerialSignal_DTR + SerialSignal_RTS);
2721 set_signals(info);
2722 }
2723
2724 spin_unlock_irqrestore(&info->lock,flags);
2725
2726 if (info->port.tty)
2727 set_bit(TTY_IO_ERROR, &info->port.tty->flags);
2728
2729 info->port.flags &= ~ASYNC_INITIALIZED;
2730}
2731
2732static void program_hw(SLMP_INFO *info)
2733{
2734 unsigned long flags;
2735
2736 spin_lock_irqsave(&info->lock,flags);
2737
2738 rx_stop(info);
2739 tx_stop(info);
2740
2741 info->tx_count = info->tx_put = info->tx_get = 0;
2742
2743 if (info->params.mode == MGSL_MODE_HDLC || info->netcount)
2744 hdlc_mode(info);
2745 else
2746 async_mode(info);
2747
2748 set_signals(info);
2749
2750 info->dcd_chkcount = 0;
2751 info->cts_chkcount = 0;
2752 info->ri_chkcount = 0;
2753 info->dsr_chkcount = 0;
2754
2755 info->ie1_value |= (CDCD|CCTS);
2756 write_reg(info, IE1, info->ie1_value);
2757
2758 get_signals(info);
2759
2760 if (info->netcount || (info->port.tty && info->port.tty->termios->c_cflag & CREAD) )
2761 rx_start(info);
2762
2763 spin_unlock_irqrestore(&info->lock,flags);
2764}
2765
2766/* Reconfigure adapter based on new parameters
2767 */
2768static void change_params(SLMP_INFO *info)
2769{
2770 unsigned cflag;
2771 int bits_per_char;
2772
2773 if (!info->port.tty || !info->port.tty->termios)
2774 return;
2775
2776 if (debug_level >= DEBUG_LEVEL_INFO)
2777 printk("%s(%d):%s change_params()\n",
2778 __FILE__,__LINE__, info->device_name );
2779
2780 cflag = info->port.tty->termios->c_cflag;
2781
2782 /* if B0 rate (hangup) specified then negate DTR and RTS */
2783 /* otherwise assert DTR and RTS */
2784 if (cflag & CBAUD)
2785 info->serial_signals |= SerialSignal_RTS + SerialSignal_DTR;
2786 else
2787 info->serial_signals &= ~(SerialSignal_RTS + SerialSignal_DTR);
2788
2789 /* byte size and parity */
2790
2791 switch (cflag & CSIZE) {
2792 case CS5: info->params.data_bits = 5; break;
2793 case CS6: info->params.data_bits = 6; break;
2794 case CS7: info->params.data_bits = 7; break;
2795 case CS8: info->params.data_bits = 8; break;
2796 /* Never happens, but GCC is too dumb to figure it out */
2797 default: info->params.data_bits = 7; break;
2798 }
2799
2800 if (cflag & CSTOPB)
2801 info->params.stop_bits = 2;
2802 else
2803 info->params.stop_bits = 1;
2804
2805 info->params.parity = ASYNC_PARITY_NONE;
2806 if (cflag & PARENB) {
2807 if (cflag & PARODD)
2808 info->params.parity = ASYNC_PARITY_ODD;
2809 else
2810 info->params.parity = ASYNC_PARITY_EVEN;
2811#ifdef CMSPAR
2812 if (cflag & CMSPAR)
2813 info->params.parity = ASYNC_PARITY_SPACE;
2814#endif
2815 }
2816
2817 /* calculate number of jiffies to transmit a full
2818 * FIFO (32 bytes) at specified data rate
2819 */
2820 bits_per_char = info->params.data_bits +
2821 info->params.stop_bits + 1;
2822
2823 /* if port data rate is set to 460800 or less then
2824 * allow tty settings to override, otherwise keep the
2825 * current data rate.
2826 */
2827 if (info->params.data_rate <= 460800) {
2828 info->params.data_rate = tty_get_baud_rate(info->port.tty);
2829 }
2830
2831 if ( info->params.data_rate ) {
2832 info->timeout = (32*HZ*bits_per_char) /
2833 info->params.data_rate;
2834 }
2835 info->timeout += HZ/50; /* Add .02 seconds of slop */
2836
2837 if (cflag & CRTSCTS)
2838 info->port.flags |= ASYNC_CTS_FLOW;
2839 else
2840 info->port.flags &= ~ASYNC_CTS_FLOW;
2841
2842 if (cflag & CLOCAL)
2843 info->port.flags &= ~ASYNC_CHECK_CD;
2844 else
2845 info->port.flags |= ASYNC_CHECK_CD;
2846
2847 /* process tty input control flags */
2848
2849 info->read_status_mask2 = OVRN;
2850 if (I_INPCK(info->port.tty))
2851 info->read_status_mask2 |= PE | FRME;
2852 if (I_BRKINT(info->port.tty) || I_PARMRK(info->port.tty))
2853 info->read_status_mask1 |= BRKD;
2854 if (I_IGNPAR(info->port.tty))
2855 info->ignore_status_mask2 |= PE | FRME;
2856 if (I_IGNBRK(info->port.tty)) {
2857 info->ignore_status_mask1 |= BRKD;
2858 /* If ignoring parity and break indicators, ignore
2859 * overruns too. (For real raw support).
2860 */
2861 if (I_IGNPAR(info->port.tty))
2862 info->ignore_status_mask2 |= OVRN;
2863 }
2864
2865 program_hw(info);
2866}
2867
2868static int get_stats(SLMP_INFO * info, struct mgsl_icount __user *user_icount)
2869{
2870 int err;
2871
2872 if (debug_level >= DEBUG_LEVEL_INFO)
2873 printk("%s(%d):%s get_params()\n",
2874 __FILE__,__LINE__, info->device_name);
2875
2876 if (!user_icount) {
2877 memset(&info->icount, 0, sizeof(info->icount));
2878 } else {
2879 mutex_lock(&info->port.mutex);
2880 COPY_TO_USER(err, user_icount, &info->icount, sizeof(struct mgsl_icount));
2881 mutex_unlock(&info->port.mutex);
2882 if (err)
2883 return -EFAULT;
2884 }
2885
2886 return 0;
2887}
2888
2889static int get_params(SLMP_INFO * info, MGSL_PARAMS __user *user_params)
2890{
2891 int err;
2892 if (debug_level >= DEBUG_LEVEL_INFO)
2893 printk("%s(%d):%s get_params()\n",
2894 __FILE__,__LINE__, info->device_name);
2895
2896 mutex_lock(&info->port.mutex);
2897 COPY_TO_USER(err,user_params, &info->params, sizeof(MGSL_PARAMS));
2898 mutex_unlock(&info->port.mutex);
2899 if (err) {
2900 if ( debug_level >= DEBUG_LEVEL_INFO )
2901 printk( "%s(%d):%s get_params() user buffer copy failed\n",
2902 __FILE__,__LINE__,info->device_name);
2903 return -EFAULT;
2904 }
2905
2906 return 0;
2907}
2908
2909static int set_params(SLMP_INFO * info, MGSL_PARAMS __user *new_params)
2910{
2911 unsigned long flags;
2912 MGSL_PARAMS tmp_params;
2913 int err;
2914
2915 if (debug_level >= DEBUG_LEVEL_INFO)
2916 printk("%s(%d):%s set_params\n",
2917 __FILE__,__LINE__,info->device_name );
2918 COPY_FROM_USER(err,&tmp_params, new_params, sizeof(MGSL_PARAMS));
2919 if (err) {
2920 if ( debug_level >= DEBUG_LEVEL_INFO )
2921 printk( "%s(%d):%s set_params() user buffer copy failed\n",
2922 __FILE__,__LINE__,info->device_name);
2923 return -EFAULT;
2924 }
2925
2926 mutex_lock(&info->port.mutex);
2927 spin_lock_irqsave(&info->lock,flags);
2928 memcpy(&info->params,&tmp_params,sizeof(MGSL_PARAMS));
2929 spin_unlock_irqrestore(&info->lock,flags);
2930
2931 change_params(info);
2932 mutex_unlock(&info->port.mutex);
2933
2934 return 0;
2935}
2936
2937static int get_txidle(SLMP_INFO * info, int __user *idle_mode)
2938{
2939 int err;
2940
2941 if (debug_level >= DEBUG_LEVEL_INFO)
2942 printk("%s(%d):%s get_txidle()=%d\n",
2943 __FILE__,__LINE__, info->device_name, info->idle_mode);
2944
2945 COPY_TO_USER(err,idle_mode, &info->idle_mode, sizeof(int));
2946 if (err) {
2947 if ( debug_level >= DEBUG_LEVEL_INFO )
2948 printk( "%s(%d):%s get_txidle() user buffer copy failed\n",
2949 __FILE__,__LINE__,info->device_name);
2950 return -EFAULT;
2951 }
2952
2953 return 0;
2954}
2955
2956static int set_txidle(SLMP_INFO * info, int idle_mode)
2957{
2958 unsigned long flags;
2959
2960 if (debug_level >= DEBUG_LEVEL_INFO)
2961 printk("%s(%d):%s set_txidle(%d)\n",
2962 __FILE__,__LINE__,info->device_name, idle_mode );
2963
2964 spin_lock_irqsave(&info->lock,flags);
2965 info->idle_mode = idle_mode;
2966 tx_set_idle( info );
2967 spin_unlock_irqrestore(&info->lock,flags);
2968 return 0;
2969}
2970
2971static int tx_enable(SLMP_INFO * info, int enable)
2972{
2973 unsigned long flags;
2974
2975 if (debug_level >= DEBUG_LEVEL_INFO)
2976 printk("%s(%d):%s tx_enable(%d)\n",
2977 __FILE__,__LINE__,info->device_name, enable);
2978
2979 spin_lock_irqsave(&info->lock,flags);
2980 if ( enable ) {
2981 if ( !info->tx_enabled ) {
2982 tx_start(info);
2983 }
2984 } else {
2985 if ( info->tx_enabled )
2986 tx_stop(info);
2987 }
2988 spin_unlock_irqrestore(&info->lock,flags);
2989 return 0;
2990}
2991
2992/* abort send HDLC frame
2993 */
2994static int tx_abort(SLMP_INFO * info)
2995{
2996 unsigned long flags;
2997
2998 if (debug_level >= DEBUG_LEVEL_INFO)
2999 printk("%s(%d):%s tx_abort()\n",
3000 __FILE__,__LINE__,info->device_name);
3001
3002 spin_lock_irqsave(&info->lock,flags);
3003 if ( info->tx_active && info->params.mode == MGSL_MODE_HDLC ) {
3004 info->ie1_value &= ~UDRN;
3005 info->ie1_value |= IDLE;
3006 write_reg(info, IE1, info->ie1_value); /* disable tx status interrupts */
3007 write_reg(info, SR1, (unsigned char)(IDLE + UDRN)); /* clear pending */
3008
3009 write_reg(info, TXDMA + DSR, 0); /* disable DMA channel */
3010 write_reg(info, TXDMA + DCMD, SWABORT); /* reset/init DMA channel */
3011
3012 write_reg(info, CMD, TXABORT);
3013 }
3014 spin_unlock_irqrestore(&info->lock,flags);
3015 return 0;
3016}
3017
3018static int rx_enable(SLMP_INFO * info, int enable)
3019{
3020 unsigned long flags;
3021
3022 if (debug_level >= DEBUG_LEVEL_INFO)
3023 printk("%s(%d):%s rx_enable(%d)\n",
3024 __FILE__,__LINE__,info->device_name,enable);
3025
3026 spin_lock_irqsave(&info->lock,flags);
3027 if ( enable ) {
3028 if ( !info->rx_enabled )
3029 rx_start(info);
3030 } else {
3031 if ( info->rx_enabled )
3032 rx_stop(info);
3033 }
3034 spin_unlock_irqrestore(&info->lock,flags);
3035 return 0;
3036}
3037
3038/* wait for specified event to occur
3039 */
3040static int wait_mgsl_event(SLMP_INFO * info, int __user *mask_ptr)
3041{
3042 unsigned long flags;
3043 int s;
3044 int rc=0;
3045 struct mgsl_icount cprev, cnow;
3046 int events;
3047 int mask;
3048 struct _input_signal_events oldsigs, newsigs;
3049 DECLARE_WAITQUEUE(wait, current);
3050
3051 COPY_FROM_USER(rc,&mask, mask_ptr, sizeof(int));
3052 if (rc) {
3053 return -EFAULT;
3054 }
3055
3056 if (debug_level >= DEBUG_LEVEL_INFO)
3057 printk("%s(%d):%s wait_mgsl_event(%d)\n",
3058 __FILE__,__LINE__,info->device_name,mask);
3059
3060 spin_lock_irqsave(&info->lock,flags);
3061
3062 /* return immediately if state matches requested events */
3063 get_signals(info);
3064 s = info->serial_signals;
3065
3066 events = mask &
3067 ( ((s & SerialSignal_DSR) ? MgslEvent_DsrActive:MgslEvent_DsrInactive) +
3068 ((s & SerialSignal_DCD) ? MgslEvent_DcdActive:MgslEvent_DcdInactive) +
3069 ((s & SerialSignal_CTS) ? MgslEvent_CtsActive:MgslEvent_CtsInactive) +
3070 ((s & SerialSignal_RI) ? MgslEvent_RiActive :MgslEvent_RiInactive) );
3071 if (events) {
3072 spin_unlock_irqrestore(&info->lock,flags);
3073 goto exit;
3074 }
3075
3076 /* save current irq counts */
3077 cprev = info->icount;
3078 oldsigs = info->input_signal_events;
3079
3080 /* enable hunt and idle irqs if needed */
3081 if (mask & (MgslEvent_ExitHuntMode+MgslEvent_IdleReceived)) {
3082 unsigned char oldval = info->ie1_value;
3083 unsigned char newval = oldval +
3084 (mask & MgslEvent_ExitHuntMode ? FLGD:0) +
3085 (mask & MgslEvent_IdleReceived ? IDLD:0);
3086 if ( oldval != newval ) {
3087 info->ie1_value = newval;
3088 write_reg(info, IE1, info->ie1_value);
3089 }
3090 }
3091
3092 set_current_state(TASK_INTERRUPTIBLE);
3093 add_wait_queue(&info->event_wait_q, &wait);
3094
3095 spin_unlock_irqrestore(&info->lock,flags);
3096
3097 for(;;) {
3098 schedule();
3099 if (signal_pending(current)) {
3100 rc = -ERESTARTSYS;
3101 break;
3102 }
3103
3104 /* get current irq counts */
3105 spin_lock_irqsave(&info->lock,flags);
3106 cnow = info->icount;
3107 newsigs = info->input_signal_events;
3108 set_current_state(TASK_INTERRUPTIBLE);
3109 spin_unlock_irqrestore(&info->lock,flags);
3110
3111 /* if no change, wait aborted for some reason */
3112 if (newsigs.dsr_up == oldsigs.dsr_up &&
3113 newsigs.dsr_down == oldsigs.dsr_down &&
3114 newsigs.dcd_up == oldsigs.dcd_up &&
3115 newsigs.dcd_down == oldsigs.dcd_down &&
3116 newsigs.cts_up == oldsigs.cts_up &&
3117 newsigs.cts_down == oldsigs.cts_down &&
3118 newsigs.ri_up == oldsigs.ri_up &&
3119 newsigs.ri_down == oldsigs.ri_down &&
3120 cnow.exithunt == cprev.exithunt &&
3121 cnow.rxidle == cprev.rxidle) {
3122 rc = -EIO;
3123 break;
3124 }
3125
3126 events = mask &
3127 ( (newsigs.dsr_up != oldsigs.dsr_up ? MgslEvent_DsrActive:0) +
3128 (newsigs.dsr_down != oldsigs.dsr_down ? MgslEvent_DsrInactive:0) +
3129 (newsigs.dcd_up != oldsigs.dcd_up ? MgslEvent_DcdActive:0) +
3130 (newsigs.dcd_down != oldsigs.dcd_down ? MgslEvent_DcdInactive:0) +
3131 (newsigs.cts_up != oldsigs.cts_up ? MgslEvent_CtsActive:0) +
3132 (newsigs.cts_down != oldsigs.cts_down ? MgslEvent_CtsInactive:0) +
3133 (newsigs.ri_up != oldsigs.ri_up ? MgslEvent_RiActive:0) +
3134 (newsigs.ri_down != oldsigs.ri_down ? MgslEvent_RiInactive:0) +
3135 (cnow.exithunt != cprev.exithunt ? MgslEvent_ExitHuntMode:0) +
3136 (cnow.rxidle != cprev.rxidle ? MgslEvent_IdleReceived:0) );
3137 if (events)
3138 break;
3139
3140 cprev = cnow;
3141 oldsigs = newsigs;
3142 }
3143
3144 remove_wait_queue(&info->event_wait_q, &wait);
3145 set_current_state(TASK_RUNNING);
3146
3147
3148 if (mask & (MgslEvent_ExitHuntMode + MgslEvent_IdleReceived)) {
3149 spin_lock_irqsave(&info->lock,flags);
3150 if (!waitqueue_active(&info->event_wait_q)) {
3151 /* disable enable exit hunt mode/idle rcvd IRQs */
3152 info->ie1_value &= ~(FLGD|IDLD);
3153 write_reg(info, IE1, info->ie1_value);
3154 }
3155 spin_unlock_irqrestore(&info->lock,flags);
3156 }
3157exit:
3158 if ( rc == 0 )
3159 PUT_USER(rc, events, mask_ptr);
3160
3161 return rc;
3162}
3163
3164static int modem_input_wait(SLMP_INFO *info,int arg)
3165{
3166 unsigned long flags;
3167 int rc;
3168 struct mgsl_icount cprev, cnow;
3169 DECLARE_WAITQUEUE(wait, current);
3170
3171 /* save current irq counts */
3172 spin_lock_irqsave(&info->lock,flags);
3173 cprev = info->icount;
3174 add_wait_queue(&info->status_event_wait_q, &wait);
3175 set_current_state(TASK_INTERRUPTIBLE);
3176 spin_unlock_irqrestore(&info->lock,flags);
3177
3178 for(;;) {
3179 schedule();
3180 if (signal_pending(current)) {
3181 rc = -ERESTARTSYS;
3182 break;
3183 }
3184
3185 /* get new irq counts */
3186 spin_lock_irqsave(&info->lock,flags);
3187 cnow = info->icount;
3188 set_current_state(TASK_INTERRUPTIBLE);
3189 spin_unlock_irqrestore(&info->lock,flags);
3190
3191 /* if no change, wait aborted for some reason */
3192 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
3193 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts) {
3194 rc = -EIO;
3195 break;
3196 }
3197
3198 /* check for change in caller specified modem input */
3199 if ((arg & TIOCM_RNG && cnow.rng != cprev.rng) ||
3200 (arg & TIOCM_DSR && cnow.dsr != cprev.dsr) ||
3201 (arg & TIOCM_CD && cnow.dcd != cprev.dcd) ||
3202 (arg & TIOCM_CTS && cnow.cts != cprev.cts)) {
3203 rc = 0;
3204 break;
3205 }
3206
3207 cprev = cnow;
3208 }
3209 remove_wait_queue(&info->status_event_wait_q, &wait);
3210 set_current_state(TASK_RUNNING);
3211 return rc;
3212}
3213
3214/* return the state of the serial control and status signals
3215 */
3216static int tiocmget(struct tty_struct *tty, struct file *file)
3217{
3218 SLMP_INFO *info = tty->driver_data;
3219 unsigned int result;
3220 unsigned long flags;
3221
3222 spin_lock_irqsave(&info->lock,flags);
3223 get_signals(info);
3224 spin_unlock_irqrestore(&info->lock,flags);
3225
3226 result = ((info->serial_signals & SerialSignal_RTS) ? TIOCM_RTS:0) +
3227 ((info->serial_signals & SerialSignal_DTR) ? TIOCM_DTR:0) +
3228 ((info->serial_signals & SerialSignal_DCD) ? TIOCM_CAR:0) +
3229 ((info->serial_signals & SerialSignal_RI) ? TIOCM_RNG:0) +
3230 ((info->serial_signals & SerialSignal_DSR) ? TIOCM_DSR:0) +
3231 ((info->serial_signals & SerialSignal_CTS) ? TIOCM_CTS:0);
3232
3233 if (debug_level >= DEBUG_LEVEL_INFO)
3234 printk("%s(%d):%s tiocmget() value=%08X\n",
3235 __FILE__,__LINE__, info->device_name, result );
3236 return result;
3237}
3238
3239/* set modem control signals (DTR/RTS)
3240 */
3241static int tiocmset(struct tty_struct *tty, struct file *file,
3242 unsigned int set, unsigned int clear)
3243{
3244 SLMP_INFO *info = tty->driver_data;
3245 unsigned long flags;
3246
3247 if (debug_level >= DEBUG_LEVEL_INFO)
3248 printk("%s(%d):%s tiocmset(%x,%x)\n",
3249 __FILE__,__LINE__,info->device_name, set, clear);
3250
3251 if (set & TIOCM_RTS)
3252 info->serial_signals |= SerialSignal_RTS;
3253 if (set & TIOCM_DTR)
3254 info->serial_signals |= SerialSignal_DTR;
3255 if (clear & TIOCM_RTS)
3256 info->serial_signals &= ~SerialSignal_RTS;
3257 if (clear & TIOCM_DTR)
3258 info->serial_signals &= ~SerialSignal_DTR;
3259
3260 spin_lock_irqsave(&info->lock,flags);
3261 set_signals(info);
3262 spin_unlock_irqrestore(&info->lock,flags);
3263
3264 return 0;
3265}
3266
3267static int carrier_raised(struct tty_port *port)
3268{
3269 SLMP_INFO *info = container_of(port, SLMP_INFO, port);
3270 unsigned long flags;
3271
3272 spin_lock_irqsave(&info->lock,flags);
3273 get_signals(info);
3274 spin_unlock_irqrestore(&info->lock,flags);
3275
3276 return (info->serial_signals & SerialSignal_DCD) ? 1 : 0;
3277}
3278
3279static void dtr_rts(struct tty_port *port, int on)
3280{
3281 SLMP_INFO *info = container_of(port, SLMP_INFO, port);
3282 unsigned long flags;
3283
3284 spin_lock_irqsave(&info->lock,flags);
3285 if (on)
3286 info->serial_signals |= SerialSignal_RTS + SerialSignal_DTR;
3287 else
3288 info->serial_signals &= ~(SerialSignal_RTS + SerialSignal_DTR);
3289 set_signals(info);
3290 spin_unlock_irqrestore(&info->lock,flags);
3291}
3292
3293/* Block the current process until the specified port is ready to open.
3294 */
3295static int block_til_ready(struct tty_struct *tty, struct file *filp,
3296 SLMP_INFO *info)
3297{
3298 DECLARE_WAITQUEUE(wait, current);
3299 int retval;
3300 bool do_clocal = false;
3301 bool extra_count = false;
3302 unsigned long flags;
3303 int cd;
3304 struct tty_port *port = &info->port;
3305
3306 if (debug_level >= DEBUG_LEVEL_INFO)
3307 printk("%s(%d):%s block_til_ready()\n",
3308 __FILE__,__LINE__, tty->driver->name );
3309
3310 if (filp->f_flags & O_NONBLOCK || tty->flags & (1 << TTY_IO_ERROR)){
3311 /* nonblock mode is set or port is not enabled */
3312 /* just verify that callout device is not active */
3313 port->flags |= ASYNC_NORMAL_ACTIVE;
3314 return 0;
3315 }
3316
3317 if (tty->termios->c_cflag & CLOCAL)
3318 do_clocal = true;
3319
3320 /* Wait for carrier detect and the line to become
3321 * free (i.e., not in use by the callout). While we are in
3322 * this loop, port->count is dropped by one, so that
3323 * close() knows when to free things. We restore it upon
3324 * exit, either normal or abnormal.
3325 */
3326
3327 retval = 0;
3328 add_wait_queue(&port->open_wait, &wait);
3329
3330 if (debug_level >= DEBUG_LEVEL_INFO)
3331 printk("%s(%d):%s block_til_ready() before block, count=%d\n",
3332 __FILE__,__LINE__, tty->driver->name, port->count );
3333
3334 spin_lock_irqsave(&info->lock, flags);
3335 if (!tty_hung_up_p(filp)) {
3336 extra_count = true;
3337 port->count--;
3338 }
3339 spin_unlock_irqrestore(&info->lock, flags);
3340 port->blocked_open++;
3341
3342 while (1) {
3343 if (tty->termios->c_cflag & CBAUD)
3344 tty_port_raise_dtr_rts(port);
3345
3346 set_current_state(TASK_INTERRUPTIBLE);
3347
3348 if (tty_hung_up_p(filp) || !(port->flags & ASYNC_INITIALIZED)){
3349 retval = (port->flags & ASYNC_HUP_NOTIFY) ?
3350 -EAGAIN : -ERESTARTSYS;
3351 break;
3352 }
3353
3354 cd = tty_port_carrier_raised(port);
3355
3356 if (!(port->flags & ASYNC_CLOSING) && (do_clocal || cd))
3357 break;
3358
3359 if (signal_pending(current)) {
3360 retval = -ERESTARTSYS;
3361 break;
3362 }
3363
3364 if (debug_level >= DEBUG_LEVEL_INFO)
3365 printk("%s(%d):%s block_til_ready() count=%d\n",
3366 __FILE__,__LINE__, tty->driver->name, port->count );
3367
3368 tty_unlock();
3369 schedule();
3370 tty_lock();
3371 }
3372
3373 set_current_state(TASK_RUNNING);
3374 remove_wait_queue(&port->open_wait, &wait);
3375
3376 if (extra_count)
3377 port->count++;
3378 port->blocked_open--;
3379
3380 if (debug_level >= DEBUG_LEVEL_INFO)
3381 printk("%s(%d):%s block_til_ready() after, count=%d\n",
3382 __FILE__,__LINE__, tty->driver->name, port->count );
3383
3384 if (!retval)
3385 port->flags |= ASYNC_NORMAL_ACTIVE;
3386
3387 return retval;
3388}
3389
3390static int alloc_dma_bufs(SLMP_INFO *info)
3391{
3392 unsigned short BuffersPerFrame;
3393 unsigned short BufferCount;
3394
3395 // Force allocation to start at 64K boundary for each port.
3396 // This is necessary because *all* buffer descriptors for a port
3397 // *must* be in the same 64K block. All descriptors on a port
3398 // share a common 'base' address (upper 8 bits of 24 bits) programmed
3399 // into the CBP register.
3400 info->port_array[0]->last_mem_alloc = (SCA_MEM_SIZE/4) * info->port_num;
3401
3402 /* Calculate the number of DMA buffers necessary to hold the */
3403 /* largest allowable frame size. Note: If the max frame size is */
3404 /* not an even multiple of the DMA buffer size then we need to */
3405 /* round the buffer count per frame up one. */
3406
3407 BuffersPerFrame = (unsigned short)(info->max_frame_size/SCABUFSIZE);
3408 if ( info->max_frame_size % SCABUFSIZE )
3409 BuffersPerFrame++;
3410
3411 /* calculate total number of data buffers (SCABUFSIZE) possible
3412 * in one ports memory (SCA_MEM_SIZE/4) after allocating memory
3413 * for the descriptor list (BUFFERLISTSIZE).
3414 */
3415 BufferCount = (SCA_MEM_SIZE/4 - BUFFERLISTSIZE)/SCABUFSIZE;
3416
3417 /* limit number of buffers to maximum amount of descriptors */
3418 if (BufferCount > BUFFERLISTSIZE/sizeof(SCADESC))
3419 BufferCount = BUFFERLISTSIZE/sizeof(SCADESC);
3420
3421 /* use enough buffers to transmit one max size frame */
3422 info->tx_buf_count = BuffersPerFrame + 1;
3423
3424 /* never use more than half the available buffers for transmit */
3425 if (info->tx_buf_count > (BufferCount/2))
3426 info->tx_buf_count = BufferCount/2;
3427
3428 if (info->tx_buf_count > SCAMAXDESC)
3429 info->tx_buf_count = SCAMAXDESC;
3430
3431 /* use remaining buffers for receive */
3432 info->rx_buf_count = BufferCount - info->tx_buf_count;
3433
3434 if (info->rx_buf_count > SCAMAXDESC)
3435 info->rx_buf_count = SCAMAXDESC;
3436
3437 if ( debug_level >= DEBUG_LEVEL_INFO )
3438 printk("%s(%d):%s Allocating %d TX and %d RX DMA buffers.\n",
3439 __FILE__,__LINE__, info->device_name,
3440 info->tx_buf_count,info->rx_buf_count);
3441
3442 if ( alloc_buf_list( info ) < 0 ||
3443 alloc_frame_bufs(info,
3444 info->rx_buf_list,
3445 info->rx_buf_list_ex,
3446 info->rx_buf_count) < 0 ||
3447 alloc_frame_bufs(info,
3448 info->tx_buf_list,
3449 info->tx_buf_list_ex,
3450 info->tx_buf_count) < 0 ||
3451 alloc_tmp_rx_buf(info) < 0 ) {
3452 printk("%s(%d):%s Can't allocate DMA buffer memory\n",
3453 __FILE__,__LINE__, info->device_name);
3454 return -ENOMEM;
3455 }
3456
3457 rx_reset_buffers( info );
3458
3459 return 0;
3460}
3461
3462/* Allocate DMA buffers for the transmit and receive descriptor lists.
3463 */
3464static int alloc_buf_list(SLMP_INFO *info)
3465{
3466 unsigned int i;
3467
3468 /* build list in adapter shared memory */
3469 info->buffer_list = info->memory_base + info->port_array[0]->last_mem_alloc;
3470 info->buffer_list_phys = info->port_array[0]->last_mem_alloc;
3471 info->port_array[0]->last_mem_alloc += BUFFERLISTSIZE;
3472
3473 memset(info->buffer_list, 0, BUFFERLISTSIZE);
3474
3475 /* Save virtual address pointers to the receive and */
3476 /* transmit buffer lists. (Receive 1st). These pointers will */
3477 /* be used by the processor to access the lists. */
3478 info->rx_buf_list = (SCADESC *)info->buffer_list;
3479
3480 info->tx_buf_list = (SCADESC *)info->buffer_list;
3481 info->tx_buf_list += info->rx_buf_count;
3482
3483 /* Build links for circular buffer entry lists (tx and rx)
3484 *
3485 * Note: links are physical addresses read by the SCA device
3486 * to determine the next buffer entry to use.
3487 */
3488
3489 for ( i = 0; i < info->rx_buf_count; i++ ) {
3490 /* calculate and store physical address of this buffer entry */
3491 info->rx_buf_list_ex[i].phys_entry =
3492 info->buffer_list_phys + (i * sizeof(SCABUFSIZE));
3493
3494 /* calculate and store physical address of */
3495 /* next entry in cirular list of entries */
3496 info->rx_buf_list[i].next = info->buffer_list_phys;
3497 if ( i < info->rx_buf_count - 1 )
3498 info->rx_buf_list[i].next += (i + 1) * sizeof(SCADESC);
3499
3500 info->rx_buf_list[i].length = SCABUFSIZE;
3501 }
3502
3503 for ( i = 0; i < info->tx_buf_count; i++ ) {
3504 /* calculate and store physical address of this buffer entry */
3505 info->tx_buf_list_ex[i].phys_entry = info->buffer_list_phys +
3506 ((info->rx_buf_count + i) * sizeof(SCADESC));
3507
3508 /* calculate and store physical address of */
3509 /* next entry in cirular list of entries */
3510
3511 info->tx_buf_list[i].next = info->buffer_list_phys +
3512 info->rx_buf_count * sizeof(SCADESC);
3513
3514 if ( i < info->tx_buf_count - 1 )
3515 info->tx_buf_list[i].next += (i + 1) * sizeof(SCADESC);
3516 }
3517
3518 return 0;
3519}
3520
3521/* Allocate the frame DMA buffers used by the specified buffer list.
3522 */
3523static int alloc_frame_bufs(SLMP_INFO *info, SCADESC *buf_list,SCADESC_EX *buf_list_ex,int count)
3524{
3525 int i;
3526 unsigned long phys_addr;
3527
3528 for ( i = 0; i < count; i++ ) {
3529 buf_list_ex[i].virt_addr = info->memory_base + info->port_array[0]->last_mem_alloc;
3530 phys_addr = info->port_array[0]->last_mem_alloc;
3531 info->port_array[0]->last_mem_alloc += SCABUFSIZE;
3532
3533 buf_list[i].buf_ptr = (unsigned short)phys_addr;
3534 buf_list[i].buf_base = (unsigned char)(phys_addr >> 16);
3535 }
3536
3537 return 0;
3538}
3539
3540static void free_dma_bufs(SLMP_INFO *info)
3541{
3542 info->buffer_list = NULL;
3543 info->rx_buf_list = NULL;
3544 info->tx_buf_list = NULL;
3545}
3546
3547/* allocate buffer large enough to hold max_frame_size.
3548 * This buffer is used to pass an assembled frame to the line discipline.
3549 */
3550static int alloc_tmp_rx_buf(SLMP_INFO *info)
3551{
3552 info->tmp_rx_buf = kmalloc(info->max_frame_size, GFP_KERNEL);
3553 if (info->tmp_rx_buf == NULL)
3554 return -ENOMEM;
3555 return 0;
3556}
3557
3558static void free_tmp_rx_buf(SLMP_INFO *info)
3559{
3560 kfree(info->tmp_rx_buf);
3561 info->tmp_rx_buf = NULL;
3562}
3563
3564static int claim_resources(SLMP_INFO *info)
3565{
3566 if (request_mem_region(info->phys_memory_base,SCA_MEM_SIZE,"synclinkmp") == NULL) {
3567 printk( "%s(%d):%s mem addr conflict, Addr=%08X\n",
3568 __FILE__,__LINE__,info->device_name, info->phys_memory_base);
3569 info->init_error = DiagStatus_AddressConflict;
3570 goto errout;
3571 }
3572 else
3573 info->shared_mem_requested = true;
3574
3575 if (request_mem_region(info->phys_lcr_base + info->lcr_offset,128,"synclinkmp") == NULL) {
3576 printk( "%s(%d):%s lcr mem addr conflict, Addr=%08X\n",
3577 __FILE__,__LINE__,info->device_name, info->phys_lcr_base);
3578 info->init_error = DiagStatus_AddressConflict;
3579 goto errout;
3580 }
3581 else
3582 info->lcr_mem_requested = true;
3583
3584 if (request_mem_region(info->phys_sca_base + info->sca_offset,SCA_BASE_SIZE,"synclinkmp") == NULL) {
3585 printk( "%s(%d):%s sca mem addr conflict, Addr=%08X\n",
3586 __FILE__,__LINE__,info->device_name, info->phys_sca_base);
3587 info->init_error = DiagStatus_AddressConflict;
3588 goto errout;
3589 }
3590 else
3591 info->sca_base_requested = true;
3592
3593 if (request_mem_region(info->phys_statctrl_base + info->statctrl_offset,SCA_REG_SIZE,"synclinkmp") == NULL) {
3594 printk( "%s(%d):%s stat/ctrl mem addr conflict, Addr=%08X\n",
3595 __FILE__,__LINE__,info->device_name, info->phys_statctrl_base);
3596 info->init_error = DiagStatus_AddressConflict;
3597 goto errout;
3598 }
3599 else
3600 info->sca_statctrl_requested = true;
3601
3602 info->memory_base = ioremap_nocache(info->phys_memory_base,
3603 SCA_MEM_SIZE);
3604 if (!info->memory_base) {
3605 printk( "%s(%d):%s Cant map shared memory, MemAddr=%08X\n",
3606 __FILE__,__LINE__,info->device_name, info->phys_memory_base );
3607 info->init_error = DiagStatus_CantAssignPciResources;
3608 goto errout;
3609 }
3610
3611 info->lcr_base = ioremap_nocache(info->phys_lcr_base, PAGE_SIZE);
3612 if (!info->lcr_base) {
3613 printk( "%s(%d):%s Cant map LCR memory, MemAddr=%08X\n",
3614 __FILE__,__LINE__,info->device_name, info->phys_lcr_base );
3615 info->init_error = DiagStatus_CantAssignPciResources;
3616 goto errout;
3617 }
3618 info->lcr_base += info->lcr_offset;
3619
3620 info->sca_base = ioremap_nocache(info->phys_sca_base, PAGE_SIZE);
3621 if (!info->sca_base) {
3622 printk( "%s(%d):%s Cant map SCA memory, MemAddr=%08X\n",
3623 __FILE__,__LINE__,info->device_name, info->phys_sca_base );
3624 info->init_error = DiagStatus_CantAssignPciResources;
3625 goto errout;
3626 }
3627 info->sca_base += info->sca_offset;
3628
3629 info->statctrl_base = ioremap_nocache(info->phys_statctrl_base,
3630 PAGE_SIZE);
3631 if (!info->statctrl_base) {
3632 printk( "%s(%d):%s Cant map SCA Status/Control memory, MemAddr=%08X\n",
3633 __FILE__,__LINE__,info->device_name, info->phys_statctrl_base );
3634 info->init_error = DiagStatus_CantAssignPciResources;
3635 goto errout;
3636 }
3637 info->statctrl_base += info->statctrl_offset;
3638
3639 if ( !memory_test(info) ) {
3640 printk( "%s(%d):Shared Memory Test failed for device %s MemAddr=%08X\n",
3641 __FILE__,__LINE__,info->device_name, info->phys_memory_base );
3642 info->init_error = DiagStatus_MemoryError;
3643 goto errout;
3644 }
3645
3646 return 0;
3647
3648errout:
3649 release_resources( info );
3650 return -ENODEV;
3651}
3652
3653static void release_resources(SLMP_INFO *info)
3654{
3655 if ( debug_level >= DEBUG_LEVEL_INFO )
3656 printk( "%s(%d):%s release_resources() entry\n",
3657 __FILE__,__LINE__,info->device_name );
3658
3659 if ( info->irq_requested ) {
3660 free_irq(info->irq_level, info);
3661 info->irq_requested = false;
3662 }
3663
3664 if ( info->shared_mem_requested ) {
3665 release_mem_region(info->phys_memory_base,SCA_MEM_SIZE);
3666 info->shared_mem_requested = false;
3667 }
3668 if ( info->lcr_mem_requested ) {
3669 release_mem_region(info->phys_lcr_base + info->lcr_offset,128);
3670 info->lcr_mem_requested = false;
3671 }
3672 if ( info->sca_base_requested ) {
3673 release_mem_region(info->phys_sca_base + info->sca_offset,SCA_BASE_SIZE);
3674 info->sca_base_requested = false;
3675 }
3676 if ( info->sca_statctrl_requested ) {
3677 release_mem_region(info->phys_statctrl_base + info->statctrl_offset,SCA_REG_SIZE);
3678 info->sca_statctrl_requested = false;
3679 }
3680
3681 if (info->memory_base){
3682 iounmap(info->memory_base);
3683 info->memory_base = NULL;
3684 }
3685
3686 if (info->sca_base) {
3687 iounmap(info->sca_base - info->sca_offset);
3688 info->sca_base=NULL;
3689 }
3690
3691 if (info->statctrl_base) {
3692 iounmap(info->statctrl_base - info->statctrl_offset);
3693 info->statctrl_base=NULL;
3694 }
3695
3696 if (info->lcr_base){
3697 iounmap(info->lcr_base - info->lcr_offset);
3698 info->lcr_base = NULL;
3699 }
3700
3701 if ( debug_level >= DEBUG_LEVEL_INFO )
3702 printk( "%s(%d):%s release_resources() exit\n",
3703 __FILE__,__LINE__,info->device_name );
3704}
3705
3706/* Add the specified device instance data structure to the
3707 * global linked list of devices and increment the device count.
3708 */
3709static void add_device(SLMP_INFO *info)
3710{
3711 info->next_device = NULL;
3712 info->line = synclinkmp_device_count;
3713 sprintf(info->device_name,"ttySLM%dp%d",info->adapter_num,info->port_num);
3714
3715 if (info->line < MAX_DEVICES) {
3716 if (maxframe[info->line])
3717 info->max_frame_size = maxframe[info->line];
3718 }
3719
3720 synclinkmp_device_count++;
3721
3722 if ( !synclinkmp_device_list )
3723 synclinkmp_device_list = info;
3724 else {
3725 SLMP_INFO *current_dev = synclinkmp_device_list;
3726 while( current_dev->next_device )
3727 current_dev = current_dev->next_device;
3728 current_dev->next_device = info;
3729 }
3730
3731 if ( info->max_frame_size < 4096 )
3732 info->max_frame_size = 4096;
3733 else if ( info->max_frame_size > 65535 )
3734 info->max_frame_size = 65535;
3735
3736 printk( "SyncLink MultiPort %s: "
3737 "Mem=(%08x %08X %08x %08X) IRQ=%d MaxFrameSize=%u\n",
3738 info->device_name,
3739 info->phys_sca_base,
3740 info->phys_memory_base,
3741 info->phys_statctrl_base,
3742 info->phys_lcr_base,
3743 info->irq_level,
3744 info->max_frame_size );
3745
3746#if SYNCLINK_GENERIC_HDLC
3747 hdlcdev_init(info);
3748#endif
3749}
3750
3751static const struct tty_port_operations port_ops = {
3752 .carrier_raised = carrier_raised,
3753 .dtr_rts = dtr_rts,
3754};
3755
3756/* Allocate and initialize a device instance structure
3757 *
3758 * Return Value: pointer to SLMP_INFO if success, otherwise NULL
3759 */
3760static SLMP_INFO *alloc_dev(int adapter_num, int port_num, struct pci_dev *pdev)
3761{
3762 SLMP_INFO *info;
3763
3764 info = kzalloc(sizeof(SLMP_INFO),
3765 GFP_KERNEL);
3766
3767 if (!info) {
3768 printk("%s(%d) Error can't allocate device instance data for adapter %d, port %d\n",
3769 __FILE__,__LINE__, adapter_num, port_num);
3770 } else {
3771 tty_port_init(&info->port);
3772 info->port.ops = &port_ops;
3773 info->magic = MGSL_MAGIC;
3774 INIT_WORK(&info->task, bh_handler);
3775 info->max_frame_size = 4096;
3776 info->port.close_delay = 5*HZ/10;
3777 info->port.closing_wait = 30*HZ;
3778 init_waitqueue_head(&info->status_event_wait_q);
3779 init_waitqueue_head(&info->event_wait_q);
3780 spin_lock_init(&info->netlock);
3781 memcpy(&info->params,&default_params,sizeof(MGSL_PARAMS));
3782 info->idle_mode = HDLC_TXIDLE_FLAGS;
3783 info->adapter_num = adapter_num;
3784 info->port_num = port_num;
3785
3786 /* Copy configuration info to device instance data */
3787 info->irq_level = pdev->irq;
3788 info->phys_lcr_base = pci_resource_start(pdev,0);
3789 info->phys_sca_base = pci_resource_start(pdev,2);
3790 info->phys_memory_base = pci_resource_start(pdev,3);
3791 info->phys_statctrl_base = pci_resource_start(pdev,4);
3792
3793 /* Because veremap only works on page boundaries we must map
3794 * a larger area than is actually implemented for the LCR
3795 * memory range. We map a full page starting at the page boundary.
3796 */
3797 info->lcr_offset = info->phys_lcr_base & (PAGE_SIZE-1);
3798 info->phys_lcr_base &= ~(PAGE_SIZE-1);
3799
3800 info->sca_offset = info->phys_sca_base & (PAGE_SIZE-1);
3801 info->phys_sca_base &= ~(PAGE_SIZE-1);
3802
3803 info->statctrl_offset = info->phys_statctrl_base & (PAGE_SIZE-1);
3804 info->phys_statctrl_base &= ~(PAGE_SIZE-1);
3805
3806 info->bus_type = MGSL_BUS_TYPE_PCI;
3807 info->irq_flags = IRQF_SHARED;
3808
3809 setup_timer(&info->tx_timer, tx_timeout, (unsigned long)info);
3810 setup_timer(&info->status_timer, status_timeout,
3811 (unsigned long)info);
3812
3813 /* Store the PCI9050 misc control register value because a flaw
3814 * in the PCI9050 prevents LCR registers from being read if
3815 * BIOS assigns an LCR base address with bit 7 set.
3816 *
3817 * Only the misc control register is accessed for which only
3818 * write access is needed, so set an initial value and change
3819 * bits to the device instance data as we write the value
3820 * to the actual misc control register.
3821 */
3822 info->misc_ctrl_value = 0x087e4546;
3823
3824 /* initial port state is unknown - if startup errors
3825 * occur, init_error will be set to indicate the
3826 * problem. Once the port is fully initialized,
3827 * this value will be set to 0 to indicate the
3828 * port is available.
3829 */
3830 info->init_error = -1;
3831 }
3832
3833 return info;
3834}
3835
3836static void device_init(int adapter_num, struct pci_dev *pdev)
3837{
3838 SLMP_INFO *port_array[SCA_MAX_PORTS];
3839 int port;
3840
3841 /* allocate device instances for up to SCA_MAX_PORTS devices */
3842 for ( port = 0; port < SCA_MAX_PORTS; ++port ) {
3843 port_array[port] = alloc_dev(adapter_num,port,pdev);
3844 if( port_array[port] == NULL ) {
3845 for ( --port; port >= 0; --port )
3846 kfree(port_array[port]);
3847 return;
3848 }
3849 }
3850
3851 /* give copy of port_array to all ports and add to device list */
3852 for ( port = 0; port < SCA_MAX_PORTS; ++port ) {
3853 memcpy(port_array[port]->port_array,port_array,sizeof(port_array));
3854 add_device( port_array[port] );
3855 spin_lock_init(&port_array[port]->lock);
3856 }
3857
3858 /* Allocate and claim adapter resources */
3859 if ( !claim_resources(port_array[0]) ) {
3860
3861 alloc_dma_bufs(port_array[0]);
3862
3863 /* copy resource information from first port to others */
3864 for ( port = 1; port < SCA_MAX_PORTS; ++port ) {
3865 port_array[port]->lock = port_array[0]->lock;
3866 port_array[port]->irq_level = port_array[0]->irq_level;
3867 port_array[port]->memory_base = port_array[0]->memory_base;
3868 port_array[port]->sca_base = port_array[0]->sca_base;
3869 port_array[port]->statctrl_base = port_array[0]->statctrl_base;
3870 port_array[port]->lcr_base = port_array[0]->lcr_base;
3871 alloc_dma_bufs(port_array[port]);
3872 }
3873
3874 if ( request_irq(port_array[0]->irq_level,
3875 synclinkmp_interrupt,
3876 port_array[0]->irq_flags,
3877 port_array[0]->device_name,
3878 port_array[0]) < 0 ) {
3879 printk( "%s(%d):%s Cant request interrupt, IRQ=%d\n",
3880 __FILE__,__LINE__,
3881 port_array[0]->device_name,
3882 port_array[0]->irq_level );
3883 }
3884 else {
3885 port_array[0]->irq_requested = true;
3886 adapter_test(port_array[0]);
3887 }
3888 }
3889}
3890
3891static const struct tty_operations ops = {
3892 .open = open,
3893 .close = close,
3894 .write = write,
3895 .put_char = put_char,
3896 .flush_chars = flush_chars,
3897 .write_room = write_room,
3898 .chars_in_buffer = chars_in_buffer,
3899 .flush_buffer = flush_buffer,
3900 .ioctl = ioctl,
3901 .throttle = throttle,
3902 .unthrottle = unthrottle,
3903 .send_xchar = send_xchar,
3904 .break_ctl = set_break,
3905 .wait_until_sent = wait_until_sent,
3906 .set_termios = set_termios,
3907 .stop = tx_hold,
3908 .start = tx_release,
3909 .hangup = hangup,
3910 .tiocmget = tiocmget,
3911 .tiocmset = tiocmset,
3912 .proc_fops = &synclinkmp_proc_fops,
3913};
3914
3915
3916static void synclinkmp_cleanup(void)
3917{
3918 int rc;
3919 SLMP_INFO *info;
3920 SLMP_INFO *tmp;
3921
3922 printk("Unloading %s %s\n", driver_name, driver_version);
3923
3924 if (serial_driver) {
3925 if ((rc = tty_unregister_driver(serial_driver)))
3926 printk("%s(%d) failed to unregister tty driver err=%d\n",
3927 __FILE__,__LINE__,rc);
3928 put_tty_driver(serial_driver);
3929 }
3930
3931 /* reset devices */
3932 info = synclinkmp_device_list;
3933 while(info) {
3934 reset_port(info);
3935 info = info->next_device;
3936 }
3937
3938 /* release devices */
3939 info = synclinkmp_device_list;
3940 while(info) {
3941#if SYNCLINK_GENERIC_HDLC
3942 hdlcdev_exit(info);
3943#endif
3944 free_dma_bufs(info);
3945 free_tmp_rx_buf(info);
3946 if ( info->port_num == 0 ) {
3947 if (info->sca_base)
3948 write_reg(info, LPR, 1); /* set low power mode */
3949 release_resources(info);
3950 }
3951 tmp = info;
3952 info = info->next_device;
3953 kfree(tmp);
3954 }
3955
3956 pci_unregister_driver(&synclinkmp_pci_driver);
3957}
3958
3959/* Driver initialization entry point.
3960 */
3961
3962static int __init synclinkmp_init(void)
3963{
3964 int rc;
3965
3966 if (break_on_load) {
3967 synclinkmp_get_text_ptr();
3968 BREAKPOINT();
3969 }
3970
3971 printk("%s %s\n", driver_name, driver_version);
3972
3973 if ((rc = pci_register_driver(&synclinkmp_pci_driver)) < 0) {
3974 printk("%s:failed to register PCI driver, error=%d\n",__FILE__,rc);
3975 return rc;
3976 }
3977
3978 serial_driver = alloc_tty_driver(128);
3979 if (!serial_driver) {
3980 rc = -ENOMEM;
3981 goto error;
3982 }
3983
3984 /* Initialize the tty_driver structure */
3985
3986 serial_driver->owner = THIS_MODULE;
3987 serial_driver->driver_name = "synclinkmp";
3988 serial_driver->name = "ttySLM";
3989 serial_driver->major = ttymajor;
3990 serial_driver->minor_start = 64;
3991 serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
3992 serial_driver->subtype = SERIAL_TYPE_NORMAL;
3993 serial_driver->init_termios = tty_std_termios;
3994 serial_driver->init_termios.c_cflag =
3995 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
3996 serial_driver->init_termios.c_ispeed = 9600;
3997 serial_driver->init_termios.c_ospeed = 9600;
3998 serial_driver->flags = TTY_DRIVER_REAL_RAW;
3999 tty_set_operations(serial_driver, &ops);
4000 if ((rc = tty_register_driver(serial_driver)) < 0) {
4001 printk("%s(%d):Couldn't register serial driver\n",
4002 __FILE__,__LINE__);
4003 put_tty_driver(serial_driver);
4004 serial_driver = NULL;
4005 goto error;
4006 }
4007
4008 printk("%s %s, tty major#%d\n",
4009 driver_name, driver_version,
4010 serial_driver->major);
4011
4012 return 0;
4013
4014error:
4015 synclinkmp_cleanup();
4016 return rc;
4017}
4018
4019static void __exit synclinkmp_exit(void)
4020{
4021 synclinkmp_cleanup();
4022}
4023
4024module_init(synclinkmp_init);
4025module_exit(synclinkmp_exit);
4026
4027/* Set the port for internal loopback mode.
4028 * The TxCLK and RxCLK signals are generated from the BRG and
4029 * the TxD is looped back to the RxD internally.
4030 */
4031static void enable_loopback(SLMP_INFO *info, int enable)
4032{
4033 if (enable) {
4034 /* MD2 (Mode Register 2)
4035 * 01..00 CNCT<1..0> Channel Connection 11=Local Loopback
4036 */
4037 write_reg(info, MD2, (unsigned char)(read_reg(info, MD2) | (BIT1 + BIT0)));
4038
4039 /* degate external TxC clock source */
4040 info->port_array[0]->ctrlreg_value |= (BIT0 << (info->port_num * 2));
4041 write_control_reg(info);
4042
4043 /* RXS/TXS (Rx/Tx clock source)
4044 * 07 Reserved, must be 0
4045 * 06..04 Clock Source, 100=BRG
4046 * 03..00 Clock Divisor, 0000=1
4047 */
4048 write_reg(info, RXS, 0x40);
4049 write_reg(info, TXS, 0x40);
4050
4051 } else {
4052 /* MD2 (Mode Register 2)
4053 * 01..00 CNCT<1..0> Channel connection, 0=normal
4054 */
4055 write_reg(info, MD2, (unsigned char)(read_reg(info, MD2) & ~(BIT1 + BIT0)));
4056
4057 /* RXS/TXS (Rx/Tx clock source)
4058 * 07 Reserved, must be 0
4059 * 06..04 Clock Source, 000=RxC/TxC Pin
4060 * 03..00 Clock Divisor, 0000=1
4061 */
4062 write_reg(info, RXS, 0x00);
4063 write_reg(info, TXS, 0x00);
4064 }
4065
4066 /* set LinkSpeed if available, otherwise default to 2Mbps */
4067 if (info->params.clock_speed)
4068 set_rate(info, info->params.clock_speed);
4069 else
4070 set_rate(info, 3686400);
4071}
4072
4073/* Set the baud rate register to the desired speed
4074 *
4075 * data_rate data rate of clock in bits per second
4076 * A data rate of 0 disables the AUX clock.
4077 */
4078static void set_rate( SLMP_INFO *info, u32 data_rate )
4079{
4080 u32 TMCValue;
4081 unsigned char BRValue;
4082 u32 Divisor=0;
4083
4084 /* fBRG = fCLK/(TMC * 2^BR)
4085 */
4086 if (data_rate != 0) {
4087 Divisor = 14745600/data_rate;
4088 if (!Divisor)
4089 Divisor = 1;
4090
4091 TMCValue = Divisor;
4092
4093 BRValue = 0;
4094 if (TMCValue != 1 && TMCValue != 2) {
4095 /* BRValue of 0 provides 50/50 duty cycle *only* when
4096 * TMCValue is 1 or 2. BRValue of 1 to 9 always provides
4097 * 50/50 duty cycle.
4098 */
4099 BRValue = 1;
4100 TMCValue >>= 1;
4101 }
4102
4103 /* while TMCValue is too big for TMC register, divide
4104 * by 2 and increment BR exponent.
4105 */
4106 for(; TMCValue > 256 && BRValue < 10; BRValue++)
4107 TMCValue >>= 1;
4108
4109 write_reg(info, TXS,
4110 (unsigned char)((read_reg(info, TXS) & 0xf0) | BRValue));
4111 write_reg(info, RXS,
4112 (unsigned char)((read_reg(info, RXS) & 0xf0) | BRValue));
4113 write_reg(info, TMC, (unsigned char)TMCValue);
4114 }
4115 else {
4116 write_reg(info, TXS,0);
4117 write_reg(info, RXS,0);
4118 write_reg(info, TMC, 0);
4119 }
4120}
4121
4122/* Disable receiver
4123 */
4124static void rx_stop(SLMP_INFO *info)
4125{
4126 if (debug_level >= DEBUG_LEVEL_ISR)
4127 printk("%s(%d):%s rx_stop()\n",
4128 __FILE__,__LINE__, info->device_name );
4129
4130 write_reg(info, CMD, RXRESET);
4131
4132 info->ie0_value &= ~RXRDYE;
4133 write_reg(info, IE0, info->ie0_value); /* disable Rx data interrupts */
4134
4135 write_reg(info, RXDMA + DSR, 0); /* disable Rx DMA */
4136 write_reg(info, RXDMA + DCMD, SWABORT); /* reset/init Rx DMA */
4137 write_reg(info, RXDMA + DIR, 0); /* disable Rx DMA interrupts */
4138
4139 info->rx_enabled = false;
4140 info->rx_overflow = false;
4141}
4142
4143/* enable the receiver
4144 */
4145static void rx_start(SLMP_INFO *info)
4146{
4147 int i;
4148
4149 if (debug_level >= DEBUG_LEVEL_ISR)
4150 printk("%s(%d):%s rx_start()\n",
4151 __FILE__,__LINE__, info->device_name );
4152
4153 write_reg(info, CMD, RXRESET);
4154
4155 if ( info->params.mode == MGSL_MODE_HDLC ) {
4156 /* HDLC, disabe IRQ on rxdata */
4157 info->ie0_value &= ~RXRDYE;
4158 write_reg(info, IE0, info->ie0_value);
4159
4160 /* Reset all Rx DMA buffers and program rx dma */
4161 write_reg(info, RXDMA + DSR, 0); /* disable Rx DMA */
4162 write_reg(info, RXDMA + DCMD, SWABORT); /* reset/init Rx DMA */
4163
4164 for (i = 0; i < info->rx_buf_count; i++) {
4165 info->rx_buf_list[i].status = 0xff;
4166
4167 // throttle to 4 shared memory writes at a time to prevent
4168 // hogging local bus (keep latency time for DMA requests low).
4169 if (!(i % 4))
4170 read_status_reg(info);
4171 }
4172 info->current_rx_buf = 0;
4173
4174 /* set current/1st descriptor address */
4175 write_reg16(info, RXDMA + CDA,
4176 info->rx_buf_list_ex[0].phys_entry);
4177
4178 /* set new last rx descriptor address */
4179 write_reg16(info, RXDMA + EDA,
4180 info->rx_buf_list_ex[info->rx_buf_count - 1].phys_entry);
4181
4182 /* set buffer length (shared by all rx dma data buffers) */
4183 write_reg16(info, RXDMA + BFL, SCABUFSIZE);
4184
4185 write_reg(info, RXDMA + DIR, 0x60); /* enable Rx DMA interrupts (EOM/BOF) */
4186 write_reg(info, RXDMA + DSR, 0xf2); /* clear Rx DMA IRQs, enable Rx DMA */
4187 } else {
4188 /* async, enable IRQ on rxdata */
4189 info->ie0_value |= RXRDYE;
4190 write_reg(info, IE0, info->ie0_value);
4191 }
4192
4193 write_reg(info, CMD, RXENABLE);
4194
4195 info->rx_overflow = false;
4196 info->rx_enabled = true;
4197}
4198
4199/* Enable the transmitter and send a transmit frame if
4200 * one is loaded in the DMA buffers.
4201 */
4202static void tx_start(SLMP_INFO *info)
4203{
4204 if (debug_level >= DEBUG_LEVEL_ISR)
4205 printk("%s(%d):%s tx_start() tx_count=%d\n",
4206 __FILE__,__LINE__, info->device_name,info->tx_count );
4207
4208 if (!info->tx_enabled ) {
4209 write_reg(info, CMD, TXRESET);
4210 write_reg(info, CMD, TXENABLE);
4211 info->tx_enabled = true;
4212 }
4213
4214 if ( info->tx_count ) {
4215
4216 /* If auto RTS enabled and RTS is inactive, then assert */
4217 /* RTS and set a flag indicating that the driver should */
4218 /* negate RTS when the transmission completes. */
4219
4220 info->drop_rts_on_tx_done = false;
4221
4222 if (info->params.mode != MGSL_MODE_ASYNC) {
4223
4224 if ( info->params.flags & HDLC_FLAG_AUTO_RTS ) {
4225 get_signals( info );
4226 if ( !(info->serial_signals & SerialSignal_RTS) ) {
4227 info->serial_signals |= SerialSignal_RTS;
4228 set_signals( info );
4229 info->drop_rts_on_tx_done = true;
4230 }
4231 }
4232
4233 write_reg16(info, TRC0,
4234 (unsigned short)(((tx_negate_fifo_level-1)<<8) + tx_active_fifo_level));
4235
4236 write_reg(info, TXDMA + DSR, 0); /* disable DMA channel */
4237 write_reg(info, TXDMA + DCMD, SWABORT); /* reset/init DMA channel */
4238
4239 /* set TX CDA (current descriptor address) */
4240 write_reg16(info, TXDMA + CDA,
4241 info->tx_buf_list_ex[0].phys_entry);
4242
4243 /* set TX EDA (last descriptor address) */
4244 write_reg16(info, TXDMA + EDA,
4245 info->tx_buf_list_ex[info->last_tx_buf].phys_entry);
4246
4247 /* enable underrun IRQ */
4248 info->ie1_value &= ~IDLE;
4249 info->ie1_value |= UDRN;
4250 write_reg(info, IE1, info->ie1_value);
4251 write_reg(info, SR1, (unsigned char)(IDLE + UDRN));
4252
4253 write_reg(info, TXDMA + DIR, 0x40); /* enable Tx DMA interrupts (EOM) */
4254 write_reg(info, TXDMA + DSR, 0xf2); /* clear Tx DMA IRQs, enable Tx DMA */
4255
4256 mod_timer(&info->tx_timer, jiffies +
4257 msecs_to_jiffies(5000));
4258 }
4259 else {
4260 tx_load_fifo(info);
4261 /* async, enable IRQ on txdata */
4262 info->ie0_value |= TXRDYE;
4263 write_reg(info, IE0, info->ie0_value);
4264 }
4265
4266 info->tx_active = true;
4267 }
4268}
4269
4270/* stop the transmitter and DMA
4271 */
4272static void tx_stop( SLMP_INFO *info )
4273{
4274 if (debug_level >= DEBUG_LEVEL_ISR)
4275 printk("%s(%d):%s tx_stop()\n",
4276 __FILE__,__LINE__, info->device_name );
4277
4278 del_timer(&info->tx_timer);
4279
4280 write_reg(info, TXDMA + DSR, 0); /* disable DMA channel */
4281 write_reg(info, TXDMA + DCMD, SWABORT); /* reset/init DMA channel */
4282
4283 write_reg(info, CMD, TXRESET);
4284
4285 info->ie1_value &= ~(UDRN + IDLE);
4286 write_reg(info, IE1, info->ie1_value); /* disable tx status interrupts */
4287 write_reg(info, SR1, (unsigned char)(IDLE + UDRN)); /* clear pending */
4288
4289 info->ie0_value &= ~TXRDYE;
4290 write_reg(info, IE0, info->ie0_value); /* disable tx data interrupts */
4291
4292 info->tx_enabled = false;
4293 info->tx_active = false;
4294}
4295
4296/* Fill the transmit FIFO until the FIFO is full or
4297 * there is no more data to load.
4298 */
4299static void tx_load_fifo(SLMP_INFO *info)
4300{
4301 u8 TwoBytes[2];
4302
4303 /* do nothing is now tx data available and no XON/XOFF pending */
4304
4305 if ( !info->tx_count && !info->x_char )
4306 return;
4307
4308 /* load the Transmit FIFO until FIFOs full or all data sent */
4309
4310 while( info->tx_count && (read_reg(info,SR0) & BIT1) ) {
4311
4312 /* there is more space in the transmit FIFO and */
4313 /* there is more data in transmit buffer */
4314
4315 if ( (info->tx_count > 1) && !info->x_char ) {
4316 /* write 16-bits */
4317 TwoBytes[0] = info->tx_buf[info->tx_get++];
4318 if (info->tx_get >= info->max_frame_size)
4319 info->tx_get -= info->max_frame_size;
4320 TwoBytes[1] = info->tx_buf[info->tx_get++];
4321 if (info->tx_get >= info->max_frame_size)
4322 info->tx_get -= info->max_frame_size;
4323
4324 write_reg16(info, TRB, *((u16 *)TwoBytes));
4325
4326 info->tx_count -= 2;
4327 info->icount.tx += 2;
4328 } else {
4329 /* only 1 byte left to transmit or 1 FIFO slot left */
4330
4331 if (info->x_char) {
4332 /* transmit pending high priority char */
4333 write_reg(info, TRB, info->x_char);
4334 info->x_char = 0;
4335 } else {
4336 write_reg(info, TRB, info->tx_buf[info->tx_get++]);
4337 if (info->tx_get >= info->max_frame_size)
4338 info->tx_get -= info->max_frame_size;
4339 info->tx_count--;
4340 }
4341 info->icount.tx++;
4342 }
4343 }
4344}
4345
4346/* Reset a port to a known state
4347 */
4348static void reset_port(SLMP_INFO *info)
4349{
4350 if (info->sca_base) {
4351
4352 tx_stop(info);
4353 rx_stop(info);
4354
4355 info->serial_signals &= ~(SerialSignal_DTR + SerialSignal_RTS);
4356 set_signals(info);
4357
4358 /* disable all port interrupts */
4359 info->ie0_value = 0;
4360 info->ie1_value = 0;
4361 info->ie2_value = 0;
4362 write_reg(info, IE0, info->ie0_value);
4363 write_reg(info, IE1, info->ie1_value);
4364 write_reg(info, IE2, info->ie2_value);
4365
4366 write_reg(info, CMD, CHRESET);
4367 }
4368}
4369
4370/* Reset all the ports to a known state.
4371 */
4372static void reset_adapter(SLMP_INFO *info)
4373{
4374 int i;
4375
4376 for ( i=0; i < SCA_MAX_PORTS; ++i) {
4377 if (info->port_array[i])
4378 reset_port(info->port_array[i]);
4379 }
4380}
4381
4382/* Program port for asynchronous communications.
4383 */
4384static void async_mode(SLMP_INFO *info)
4385{
4386
4387 unsigned char RegValue;
4388
4389 tx_stop(info);
4390 rx_stop(info);
4391
4392 /* MD0, Mode Register 0
4393 *
4394 * 07..05 PRCTL<2..0>, Protocol Mode, 000=async
4395 * 04 AUTO, Auto-enable (RTS/CTS/DCD)
4396 * 03 Reserved, must be 0
4397 * 02 CRCCC, CRC Calculation, 0=disabled
4398 * 01..00 STOP<1..0> Stop bits (00=1,10=2)
4399 *
4400 * 0000 0000
4401 */
4402 RegValue = 0x00;
4403 if (info->params.stop_bits != 1)
4404 RegValue |= BIT1;
4405 write_reg(info, MD0, RegValue);
4406
4407 /* MD1, Mode Register 1
4408 *
4409 * 07..06 BRATE<1..0>, bit rate, 00=1/1 01=1/16 10=1/32 11=1/64
4410 * 05..04 TXCHR<1..0>, tx char size, 00=8 bits,01=7,10=6,11=5
4411 * 03..02 RXCHR<1..0>, rx char size
4412 * 01..00 PMPM<1..0>, Parity mode, 00=none 10=even 11=odd
4413 *
4414 * 0100 0000
4415 */
4416 RegValue = 0x40;
4417 switch (info->params.data_bits) {
4418 case 7: RegValue |= BIT4 + BIT2; break;
4419 case 6: RegValue |= BIT5 + BIT3; break;
4420 case 5: RegValue |= BIT5 + BIT4 + BIT3 + BIT2; break;
4421 }
4422 if (info->params.parity != ASYNC_PARITY_NONE) {
4423 RegValue |= BIT1;
4424 if (info->params.parity == ASYNC_PARITY_ODD)
4425 RegValue |= BIT0;
4426 }
4427 write_reg(info, MD1, RegValue);
4428
4429 /* MD2, Mode Register 2
4430 *
4431 * 07..02 Reserved, must be 0
4432 * 01..00 CNCT<1..0> Channel connection, 00=normal 11=local loopback
4433 *
4434 * 0000 0000
4435 */
4436 RegValue = 0x00;
4437 if (info->params.loopback)
4438 RegValue |= (BIT1 + BIT0);
4439 write_reg(info, MD2, RegValue);
4440
4441 /* RXS, Receive clock source
4442 *
4443 * 07 Reserved, must be 0
4444 * 06..04 RXCS<2..0>, clock source, 000=RxC Pin, 100=BRG, 110=DPLL
4445 * 03..00 RXBR<3..0>, rate divisor, 0000=1
4446 */
4447 RegValue=BIT6;
4448 write_reg(info, RXS, RegValue);
4449
4450 /* TXS, Transmit clock source
4451 *
4452 * 07 Reserved, must be 0
4453 * 06..04 RXCS<2..0>, clock source, 000=TxC Pin, 100=BRG, 110=Receive Clock
4454 * 03..00 RXBR<3..0>, rate divisor, 0000=1
4455 */
4456 RegValue=BIT6;
4457 write_reg(info, TXS, RegValue);
4458
4459 /* Control Register
4460 *
4461 * 6,4,2,0 CLKSEL<3..0>, 0 = TcCLK in, 1 = Auxclk out
4462 */
4463 info->port_array[0]->ctrlreg_value |= (BIT0 << (info->port_num * 2));
4464 write_control_reg(info);
4465
4466 tx_set_idle(info);
4467
4468 /* RRC Receive Ready Control 0
4469 *
4470 * 07..05 Reserved, must be 0
4471 * 04..00 RRC<4..0> Rx FIFO trigger active 0x00 = 1 byte
4472 */
4473 write_reg(info, RRC, 0x00);
4474
4475 /* TRC0 Transmit Ready Control 0
4476 *
4477 * 07..05 Reserved, must be 0
4478 * 04..00 TRC<4..0> Tx FIFO trigger active 0x10 = 16 bytes
4479 */
4480 write_reg(info, TRC0, 0x10);
4481
4482 /* TRC1 Transmit Ready Control 1
4483 *
4484 * 07..05 Reserved, must be 0
4485 * 04..00 TRC<4..0> Tx FIFO trigger inactive 0x1e = 31 bytes (full-1)
4486 */
4487 write_reg(info, TRC1, 0x1e);
4488
4489 /* CTL, MSCI control register
4490 *
4491 * 07..06 Reserved, set to 0
4492 * 05 UDRNC, underrun control, 0=abort 1=CRC+flag (HDLC/BSC)
4493 * 04 IDLC, idle control, 0=mark 1=idle register
4494 * 03 BRK, break, 0=off 1 =on (async)
4495 * 02 SYNCLD, sync char load enable (BSC) 1=enabled
4496 * 01 GOP, go active on poll (LOOP mode) 1=enabled
4497 * 00 RTS, RTS output control, 0=active 1=inactive
4498 *
4499 * 0001 0001
4500 */
4501 RegValue = 0x10;
4502 if (!(info->serial_signals & SerialSignal_RTS))
4503 RegValue |= 0x01;
4504 write_reg(info, CTL, RegValue);
4505
4506 /* enable status interrupts */
4507 info->ie0_value |= TXINTE + RXINTE;
4508 write_reg(info, IE0, info->ie0_value);
4509
4510 /* enable break detect interrupt */
4511 info->ie1_value = BRKD;
4512 write_reg(info, IE1, info->ie1_value);
4513
4514 /* enable rx overrun interrupt */
4515 info->ie2_value = OVRN;
4516 write_reg(info, IE2, info->ie2_value);
4517
4518 set_rate( info, info->params.data_rate * 16 );
4519}
4520
4521/* Program the SCA for HDLC communications.
4522 */
4523static void hdlc_mode(SLMP_INFO *info)
4524{
4525 unsigned char RegValue;
4526 u32 DpllDivisor;
4527
4528 // Can't use DPLL because SCA outputs recovered clock on RxC when
4529 // DPLL mode selected. This causes output contention with RxC receiver.
4530 // Use of DPLL would require external hardware to disable RxC receiver
4531 // when DPLL mode selected.
4532 info->params.flags &= ~(HDLC_FLAG_TXC_DPLL + HDLC_FLAG_RXC_DPLL);
4533
4534 /* disable DMA interrupts */
4535 write_reg(info, TXDMA + DIR, 0);
4536 write_reg(info, RXDMA + DIR, 0);
4537
4538 /* MD0, Mode Register 0
4539 *
4540 * 07..05 PRCTL<2..0>, Protocol Mode, 100=HDLC
4541 * 04 AUTO, Auto-enable (RTS/CTS/DCD)
4542 * 03 Reserved, must be 0
4543 * 02 CRCCC, CRC Calculation, 1=enabled
4544 * 01 CRC1, CRC selection, 0=CRC-16,1=CRC-CCITT-16
4545 * 00 CRC0, CRC initial value, 1 = all 1s
4546 *
4547 * 1000 0001
4548 */
4549 RegValue = 0x81;
4550 if (info->params.flags & HDLC_FLAG_AUTO_CTS)
4551 RegValue |= BIT4;
4552 if (info->params.flags & HDLC_FLAG_AUTO_DCD)
4553 RegValue |= BIT4;
4554 if (info->params.crc_type == HDLC_CRC_16_CCITT)
4555 RegValue |= BIT2 + BIT1;
4556 write_reg(info, MD0, RegValue);
4557
4558 /* MD1, Mode Register 1
4559 *
4560 * 07..06 ADDRS<1..0>, Address detect, 00=no addr check
4561 * 05..04 TXCHR<1..0>, tx char size, 00=8 bits
4562 * 03..02 RXCHR<1..0>, rx char size, 00=8 bits
4563 * 01..00 PMPM<1..0>, Parity mode, 00=no parity
4564 *
4565 * 0000 0000
4566 */
4567 RegValue = 0x00;
4568 write_reg(info, MD1, RegValue);
4569
4570 /* MD2, Mode Register 2
4571 *
4572 * 07 NRZFM, 0=NRZ, 1=FM
4573 * 06..05 CODE<1..0> Encoding, 00=NRZ
4574 * 04..03 DRATE<1..0> DPLL Divisor, 00=8
4575 * 02 Reserved, must be 0
4576 * 01..00 CNCT<1..0> Channel connection, 0=normal
4577 *
4578 * 0000 0000
4579 */
4580 RegValue = 0x00;
4581 switch(info->params.encoding) {
4582 case HDLC_ENCODING_NRZI: RegValue |= BIT5; break;
4583 case HDLC_ENCODING_BIPHASE_MARK: RegValue |= BIT7 + BIT5; break; /* aka FM1 */
4584 case HDLC_ENCODING_BIPHASE_SPACE: RegValue |= BIT7 + BIT6; break; /* aka FM0 */
4585 case HDLC_ENCODING_BIPHASE_LEVEL: RegValue |= BIT7; break; /* aka Manchester */
4586#if 0
4587 case HDLC_ENCODING_NRZB: /* not supported */
4588 case HDLC_ENCODING_NRZI_MARK: /* not supported */
4589 case HDLC_ENCODING_DIFF_BIPHASE_LEVEL: /* not supported */
4590#endif
4591 }
4592 if ( info->params.flags & HDLC_FLAG_DPLL_DIV16 ) {
4593 DpllDivisor = 16;
4594 RegValue |= BIT3;
4595 } else if ( info->params.flags & HDLC_FLAG_DPLL_DIV8 ) {
4596 DpllDivisor = 8;
4597 } else {
4598 DpllDivisor = 32;
4599 RegValue |= BIT4;
4600 }
4601 write_reg(info, MD2, RegValue);
4602
4603
4604 /* RXS, Receive clock source
4605 *
4606 * 07 Reserved, must be 0
4607 * 06..04 RXCS<2..0>, clock source, 000=RxC Pin, 100=BRG, 110=DPLL
4608 * 03..00 RXBR<3..0>, rate divisor, 0000=1
4609 */
4610 RegValue=0;
4611 if (info->params.flags & HDLC_FLAG_RXC_BRG)
4612 RegValue |= BIT6;
4613 if (info->params.flags & HDLC_FLAG_RXC_DPLL)
4614 RegValue |= BIT6 + BIT5;
4615 write_reg(info, RXS, RegValue);
4616
4617 /* TXS, Transmit clock source
4618 *
4619 * 07 Reserved, must be 0
4620 * 06..04 RXCS<2..0>, clock source, 000=TxC Pin, 100=BRG, 110=Receive Clock
4621 * 03..00 RXBR<3..0>, rate divisor, 0000=1
4622 */
4623 RegValue=0;
4624 if (info->params.flags & HDLC_FLAG_TXC_BRG)
4625 RegValue |= BIT6;
4626 if (info->params.flags & HDLC_FLAG_TXC_DPLL)
4627 RegValue |= BIT6 + BIT5;
4628 write_reg(info, TXS, RegValue);
4629
4630 if (info->params.flags & HDLC_FLAG_RXC_DPLL)
4631 set_rate(info, info->params.clock_speed * DpllDivisor);
4632 else
4633 set_rate(info, info->params.clock_speed);
4634
4635 /* GPDATA (General Purpose I/O Data Register)
4636 *
4637 * 6,4,2,0 CLKSEL<3..0>, 0 = TcCLK in, 1 = Auxclk out
4638 */
4639 if (info->params.flags & HDLC_FLAG_TXC_BRG)
4640 info->port_array[0]->ctrlreg_value |= (BIT0 << (info->port_num * 2));
4641 else
4642 info->port_array[0]->ctrlreg_value &= ~(BIT0 << (info->port_num * 2));
4643 write_control_reg(info);
4644
4645 /* RRC Receive Ready Control 0
4646 *
4647 * 07..05 Reserved, must be 0
4648 * 04..00 RRC<4..0> Rx FIFO trigger active
4649 */
4650 write_reg(info, RRC, rx_active_fifo_level);
4651
4652 /* TRC0 Transmit Ready Control 0
4653 *
4654 * 07..05 Reserved, must be 0
4655 * 04..00 TRC<4..0> Tx FIFO trigger active
4656 */
4657 write_reg(info, TRC0, tx_active_fifo_level);
4658
4659 /* TRC1 Transmit Ready Control 1
4660 *
4661 * 07..05 Reserved, must be 0
4662 * 04..00 TRC<4..0> Tx FIFO trigger inactive 0x1f = 32 bytes (full)
4663 */
4664 write_reg(info, TRC1, (unsigned char)(tx_negate_fifo_level - 1));
4665
4666 /* DMR, DMA Mode Register
4667 *
4668 * 07..05 Reserved, must be 0
4669 * 04 TMOD, Transfer Mode: 1=chained-block
4670 * 03 Reserved, must be 0
4671 * 02 NF, Number of Frames: 1=multi-frame
4672 * 01 CNTE, Frame End IRQ Counter enable: 0=disabled
4673 * 00 Reserved, must be 0
4674 *
4675 * 0001 0100
4676 */
4677 write_reg(info, TXDMA + DMR, 0x14);
4678 write_reg(info, RXDMA + DMR, 0x14);
4679
4680 /* Set chain pointer base (upper 8 bits of 24 bit addr) */
4681 write_reg(info, RXDMA + CPB,
4682 (unsigned char)(info->buffer_list_phys >> 16));
4683
4684 /* Set chain pointer base (upper 8 bits of 24 bit addr) */
4685 write_reg(info, TXDMA + CPB,
4686 (unsigned char)(info->buffer_list_phys >> 16));
4687
4688 /* enable status interrupts. other code enables/disables
4689 * the individual sources for these two interrupt classes.
4690 */
4691 info->ie0_value |= TXINTE + RXINTE;
4692 write_reg(info, IE0, info->ie0_value);
4693
4694 /* CTL, MSCI control register
4695 *
4696 * 07..06 Reserved, set to 0
4697 * 05 UDRNC, underrun control, 0=abort 1=CRC+flag (HDLC/BSC)
4698 * 04 IDLC, idle control, 0=mark 1=idle register
4699 * 03 BRK, break, 0=off 1 =on (async)
4700 * 02 SYNCLD, sync char load enable (BSC) 1=enabled
4701 * 01 GOP, go active on poll (LOOP mode) 1=enabled
4702 * 00 RTS, RTS output control, 0=active 1=inactive
4703 *
4704 * 0001 0001
4705 */
4706 RegValue = 0x10;
4707 if (!(info->serial_signals & SerialSignal_RTS))
4708 RegValue |= 0x01;
4709 write_reg(info, CTL, RegValue);
4710
4711 /* preamble not supported ! */
4712
4713 tx_set_idle(info);
4714 tx_stop(info);
4715 rx_stop(info);
4716
4717 set_rate(info, info->params.clock_speed);
4718
4719 if (info->params.loopback)
4720 enable_loopback(info,1);
4721}
4722
4723/* Set the transmit HDLC idle mode
4724 */
4725static void tx_set_idle(SLMP_INFO *info)
4726{
4727 unsigned char RegValue = 0xff;
4728
4729 /* Map API idle mode to SCA register bits */
4730 switch(info->idle_mode) {
4731 case HDLC_TXIDLE_FLAGS: RegValue = 0x7e; break;
4732 case HDLC_TXIDLE_ALT_ZEROS_ONES: RegValue = 0xaa; break;
4733 case HDLC_TXIDLE_ZEROS: RegValue = 0x00; break;
4734 case HDLC_TXIDLE_ONES: RegValue = 0xff; break;
4735 case HDLC_TXIDLE_ALT_MARK_SPACE: RegValue = 0xaa; break;
4736 case HDLC_TXIDLE_SPACE: RegValue = 0x00; break;
4737 case HDLC_TXIDLE_MARK: RegValue = 0xff; break;
4738 }
4739
4740 write_reg(info, IDL, RegValue);
4741}
4742
4743/* Query the adapter for the state of the V24 status (input) signals.
4744 */
4745static void get_signals(SLMP_INFO *info)
4746{
4747 u16 status = read_reg(info, SR3);
4748 u16 gpstatus = read_status_reg(info);
4749 u16 testbit;
4750
4751 /* clear all serial signals except DTR and RTS */
4752 info->serial_signals &= SerialSignal_DTR + SerialSignal_RTS;
4753
4754 /* set serial signal bits to reflect MISR */
4755
4756 if (!(status & BIT3))
4757 info->serial_signals |= SerialSignal_CTS;
4758
4759 if ( !(status & BIT2))
4760 info->serial_signals |= SerialSignal_DCD;
4761
4762 testbit = BIT1 << (info->port_num * 2); // Port 0..3 RI is GPDATA<1,3,5,7>
4763 if (!(gpstatus & testbit))
4764 info->serial_signals |= SerialSignal_RI;
4765
4766 testbit = BIT0 << (info->port_num * 2); // Port 0..3 DSR is GPDATA<0,2,4,6>
4767 if (!(gpstatus & testbit))
4768 info->serial_signals |= SerialSignal_DSR;
4769}
4770
4771/* Set the state of DTR and RTS based on contents of
4772 * serial_signals member of device context.
4773 */
4774static void set_signals(SLMP_INFO *info)
4775{
4776 unsigned char RegValue;
4777 u16 EnableBit;
4778
4779 RegValue = read_reg(info, CTL);
4780 if (info->serial_signals & SerialSignal_RTS)
4781 RegValue &= ~BIT0;
4782 else
4783 RegValue |= BIT0;
4784 write_reg(info, CTL, RegValue);
4785
4786 // Port 0..3 DTR is ctrl reg <1,3,5,7>
4787 EnableBit = BIT1 << (info->port_num*2);
4788 if (info->serial_signals & SerialSignal_DTR)
4789 info->port_array[0]->ctrlreg_value &= ~EnableBit;
4790 else
4791 info->port_array[0]->ctrlreg_value |= EnableBit;
4792 write_control_reg(info);
4793}
4794
4795/*******************/
4796/* DMA Buffer Code */
4797/*******************/
4798
4799/* Set the count for all receive buffers to SCABUFSIZE
4800 * and set the current buffer to the first buffer. This effectively
4801 * makes all buffers free and discards any data in buffers.
4802 */
4803static void rx_reset_buffers(SLMP_INFO *info)
4804{
4805 rx_free_frame_buffers(info, 0, info->rx_buf_count - 1);
4806}
4807
4808/* Free the buffers used by a received frame
4809 *
4810 * info pointer to device instance data
4811 * first index of 1st receive buffer of frame
4812 * last index of last receive buffer of frame
4813 */
4814static void rx_free_frame_buffers(SLMP_INFO *info, unsigned int first, unsigned int last)
4815{
4816 bool done = false;
4817
4818 while(!done) {
4819 /* reset current buffer for reuse */
4820 info->rx_buf_list[first].status = 0xff;
4821
4822 if (first == last) {
4823 done = true;
4824 /* set new last rx descriptor address */
4825 write_reg16(info, RXDMA + EDA, info->rx_buf_list_ex[first].phys_entry);
4826 }
4827
4828 first++;
4829 if (first == info->rx_buf_count)
4830 first = 0;
4831 }
4832
4833 /* set current buffer to next buffer after last buffer of frame */
4834 info->current_rx_buf = first;
4835}
4836
4837/* Return a received frame from the receive DMA buffers.
4838 * Only frames received without errors are returned.
4839 *
4840 * Return Value: true if frame returned, otherwise false
4841 */
4842static bool rx_get_frame(SLMP_INFO *info)
4843{
4844 unsigned int StartIndex, EndIndex; /* index of 1st and last buffers of Rx frame */
4845 unsigned short status;
4846 unsigned int framesize = 0;
4847 bool ReturnCode = false;
4848 unsigned long flags;
4849 struct tty_struct *tty = info->port.tty;
4850 unsigned char addr_field = 0xff;
4851 SCADESC *desc;
4852 SCADESC_EX *desc_ex;
4853
4854CheckAgain:
4855 /* assume no frame returned, set zero length */
4856 framesize = 0;
4857 addr_field = 0xff;
4858
4859 /*
4860 * current_rx_buf points to the 1st buffer of the next available
4861 * receive frame. To find the last buffer of the frame look for
4862 * a non-zero status field in the buffer entries. (The status
4863 * field is set by the 16C32 after completing a receive frame.
4864 */
4865 StartIndex = EndIndex = info->current_rx_buf;
4866
4867 for ( ;; ) {
4868 desc = &info->rx_buf_list[EndIndex];
4869 desc_ex = &info->rx_buf_list_ex[EndIndex];
4870
4871 if (desc->status == 0xff)
4872 goto Cleanup; /* current desc still in use, no frames available */
4873
4874 if (framesize == 0 && info->params.addr_filter != 0xff)
4875 addr_field = desc_ex->virt_addr[0];
4876
4877 framesize += desc->length;
4878
4879 /* Status != 0 means last buffer of frame */
4880 if (desc->status)
4881 break;
4882
4883 EndIndex++;
4884 if (EndIndex == info->rx_buf_count)
4885 EndIndex = 0;
4886
4887 if (EndIndex == info->current_rx_buf) {
4888 /* all buffers have been 'used' but none mark */
4889 /* the end of a frame. Reset buffers and receiver. */
4890 if ( info->rx_enabled ){
4891 spin_lock_irqsave(&info->lock,flags);
4892 rx_start(info);
4893 spin_unlock_irqrestore(&info->lock,flags);
4894 }
4895 goto Cleanup;
4896 }
4897
4898 }
4899
4900 /* check status of receive frame */
4901
4902 /* frame status is byte stored after frame data
4903 *
4904 * 7 EOM (end of msg), 1 = last buffer of frame
4905 * 6 Short Frame, 1 = short frame
4906 * 5 Abort, 1 = frame aborted
4907 * 4 Residue, 1 = last byte is partial
4908 * 3 Overrun, 1 = overrun occurred during frame reception
4909 * 2 CRC, 1 = CRC error detected
4910 *
4911 */
4912 status = desc->status;
4913
4914 /* ignore CRC bit if not using CRC (bit is undefined) */
4915 /* Note:CRC is not save to data buffer */
4916 if (info->params.crc_type == HDLC_CRC_NONE)
4917 status &= ~BIT2;
4918
4919 if (framesize == 0 ||
4920 (addr_field != 0xff && addr_field != info->params.addr_filter)) {
4921 /* discard 0 byte frames, this seems to occur sometime
4922 * when remote is idling flags.
4923 */
4924 rx_free_frame_buffers(info, StartIndex, EndIndex);
4925 goto CheckAgain;
4926 }
4927
4928 if (framesize < 2)
4929 status |= BIT6;
4930
4931 if (status & (BIT6+BIT5+BIT3+BIT2)) {
4932 /* received frame has errors,
4933 * update counts and mark frame size as 0
4934 */
4935 if (status & BIT6)
4936 info->icount.rxshort++;
4937 else if (status & BIT5)
4938 info->icount.rxabort++;
4939 else if (status & BIT3)
4940 info->icount.rxover++;
4941 else
4942 info->icount.rxcrc++;
4943
4944 framesize = 0;
4945#if SYNCLINK_GENERIC_HDLC
4946 {
4947 info->netdev->stats.rx_errors++;
4948 info->netdev->stats.rx_frame_errors++;
4949 }
4950#endif
4951 }
4952
4953 if ( debug_level >= DEBUG_LEVEL_BH )
4954 printk("%s(%d):%s rx_get_frame() status=%04X size=%d\n",
4955 __FILE__,__LINE__,info->device_name,status,framesize);
4956
4957 if ( debug_level >= DEBUG_LEVEL_DATA )
4958 trace_block(info,info->rx_buf_list_ex[StartIndex].virt_addr,
4959 min_t(int, framesize,SCABUFSIZE),0);
4960
4961 if (framesize) {
4962 if (framesize > info->max_frame_size)
4963 info->icount.rxlong++;
4964 else {
4965 /* copy dma buffer(s) to contiguous intermediate buffer */
4966 int copy_count = framesize;
4967 int index = StartIndex;
4968 unsigned char *ptmp = info->tmp_rx_buf;
4969 info->tmp_rx_buf_count = framesize;
4970
4971 info->icount.rxok++;
4972
4973 while(copy_count) {
4974 int partial_count = min(copy_count,SCABUFSIZE);
4975 memcpy( ptmp,
4976 info->rx_buf_list_ex[index].virt_addr,
4977 partial_count );
4978 ptmp += partial_count;
4979 copy_count -= partial_count;
4980
4981 if ( ++index == info->rx_buf_count )
4982 index = 0;
4983 }
4984
4985#if SYNCLINK_GENERIC_HDLC
4986 if (info->netcount)
4987 hdlcdev_rx(info,info->tmp_rx_buf,framesize);
4988 else
4989#endif
4990 ldisc_receive_buf(tty,info->tmp_rx_buf,
4991 info->flag_buf, framesize);
4992 }
4993 }
4994 /* Free the buffers used by this frame. */
4995 rx_free_frame_buffers( info, StartIndex, EndIndex );
4996
4997 ReturnCode = true;
4998
4999Cleanup:
5000 if ( info->rx_enabled && info->rx_overflow ) {
5001 /* Receiver is enabled, but needs to restarted due to
5002 * rx buffer overflow. If buffers are empty, restart receiver.
5003 */
5004 if (info->rx_buf_list[EndIndex].status == 0xff) {
5005 spin_lock_irqsave(&info->lock,flags);
5006 rx_start(info);
5007 spin_unlock_irqrestore(&info->lock,flags);
5008 }
5009 }
5010
5011 return ReturnCode;
5012}
5013
5014/* load the transmit DMA buffer with data
5015 */
5016static void tx_load_dma_buffer(SLMP_INFO *info, const char *buf, unsigned int count)
5017{
5018 unsigned short copy_count;
5019 unsigned int i = 0;
5020 SCADESC *desc;
5021 SCADESC_EX *desc_ex;
5022
5023 if ( debug_level >= DEBUG_LEVEL_DATA )
5024 trace_block(info,buf, min_t(int, count,SCABUFSIZE), 1);
5025
5026 /* Copy source buffer to one or more DMA buffers, starting with
5027 * the first transmit dma buffer.
5028 */
5029 for(i=0;;)
5030 {
5031 copy_count = min_t(unsigned short,count,SCABUFSIZE);
5032
5033 desc = &info->tx_buf_list[i];
5034 desc_ex = &info->tx_buf_list_ex[i];
5035
5036 load_pci_memory(info, desc_ex->virt_addr,buf,copy_count);
5037
5038 desc->length = copy_count;
5039 desc->status = 0;
5040
5041 buf += copy_count;
5042 count -= copy_count;
5043
5044 if (!count)
5045 break;
5046
5047 i++;
5048 if (i >= info->tx_buf_count)
5049 i = 0;
5050 }
5051
5052 info->tx_buf_list[i].status = 0x81; /* set EOM and EOT status */
5053 info->last_tx_buf = ++i;
5054}
5055
5056static bool register_test(SLMP_INFO *info)
5057{
5058 static unsigned char testval[] = {0x00, 0xff, 0xaa, 0x55, 0x69, 0x96};
5059 static unsigned int count = ARRAY_SIZE(testval);
5060 unsigned int i;
5061 bool rc = true;
5062 unsigned long flags;
5063
5064 spin_lock_irqsave(&info->lock,flags);
5065 reset_port(info);
5066
5067 /* assume failure */
5068 info->init_error = DiagStatus_AddressFailure;
5069
5070 /* Write bit patterns to various registers but do it out of */
5071 /* sync, then read back and verify values. */
5072
5073 for (i = 0 ; i < count ; i++) {
5074 write_reg(info, TMC, testval[i]);
5075 write_reg(info, IDL, testval[(i+1)%count]);
5076 write_reg(info, SA0, testval[(i+2)%count]);
5077 write_reg(info, SA1, testval[(i+3)%count]);
5078
5079 if ( (read_reg(info, TMC) != testval[i]) ||
5080 (read_reg(info, IDL) != testval[(i+1)%count]) ||
5081 (read_reg(info, SA0) != testval[(i+2)%count]) ||
5082 (read_reg(info, SA1) != testval[(i+3)%count]) )
5083 {
5084 rc = false;
5085 break;
5086 }
5087 }
5088
5089 reset_port(info);
5090 spin_unlock_irqrestore(&info->lock,flags);
5091
5092 return rc;
5093}
5094
5095static bool irq_test(SLMP_INFO *info)
5096{
5097 unsigned long timeout;
5098 unsigned long flags;
5099
5100 unsigned char timer = (info->port_num & 1) ? TIMER2 : TIMER0;
5101
5102 spin_lock_irqsave(&info->lock,flags);
5103 reset_port(info);
5104
5105 /* assume failure */
5106 info->init_error = DiagStatus_IrqFailure;
5107 info->irq_occurred = false;
5108
5109 /* setup timer0 on SCA0 to interrupt */
5110
5111 /* IER2<7..4> = timer<3..0> interrupt enables (1=enabled) */
5112 write_reg(info, IER2, (unsigned char)((info->port_num & 1) ? BIT6 : BIT4));
5113
5114 write_reg(info, (unsigned char)(timer + TEPR), 0); /* timer expand prescale */
5115 write_reg16(info, (unsigned char)(timer + TCONR), 1); /* timer constant */
5116
5117
5118 /* TMCS, Timer Control/Status Register
5119 *
5120 * 07 CMF, Compare match flag (read only) 1=match
5121 * 06 ECMI, CMF Interrupt Enable: 1=enabled
5122 * 05 Reserved, must be 0
5123 * 04 TME, Timer Enable
5124 * 03..00 Reserved, must be 0
5125 *
5126 * 0101 0000
5127 */
5128 write_reg(info, (unsigned char)(timer + TMCS), 0x50);
5129
5130 spin_unlock_irqrestore(&info->lock,flags);
5131
5132 timeout=100;
5133 while( timeout-- && !info->irq_occurred ) {
5134 msleep_interruptible(10);
5135 }
5136
5137 spin_lock_irqsave(&info->lock,flags);
5138 reset_port(info);
5139 spin_unlock_irqrestore(&info->lock,flags);
5140
5141 return info->irq_occurred;
5142}
5143
5144/* initialize individual SCA device (2 ports)
5145 */
5146static bool sca_init(SLMP_INFO *info)
5147{
5148 /* set wait controller to single mem partition (low), no wait states */
5149 write_reg(info, PABR0, 0); /* wait controller addr boundary 0 */
5150 write_reg(info, PABR1, 0); /* wait controller addr boundary 1 */
5151 write_reg(info, WCRL, 0); /* wait controller low range */
5152 write_reg(info, WCRM, 0); /* wait controller mid range */
5153 write_reg(info, WCRH, 0); /* wait controller high range */
5154
5155 /* DPCR, DMA Priority Control
5156 *
5157 * 07..05 Not used, must be 0
5158 * 04 BRC, bus release condition: 0=all transfers complete
5159 * 03 CCC, channel change condition: 0=every cycle
5160 * 02..00 PR<2..0>, priority 100=round robin
5161 *
5162 * 00000100 = 0x04
5163 */
5164 write_reg(info, DPCR, dma_priority);
5165
5166 /* DMA Master Enable, BIT7: 1=enable all channels */
5167 write_reg(info, DMER, 0x80);
5168
5169 /* enable all interrupt classes */
5170 write_reg(info, IER0, 0xff); /* TxRDY,RxRDY,TxINT,RxINT (ports 0-1) */
5171 write_reg(info, IER1, 0xff); /* DMIB,DMIA (channels 0-3) */
5172 write_reg(info, IER2, 0xf0); /* TIRQ (timers 0-3) */
5173
5174 /* ITCR, interrupt control register
5175 * 07 IPC, interrupt priority, 0=MSCI->DMA
5176 * 06..05 IAK<1..0>, Acknowledge cycle, 00=non-ack cycle
5177 * 04 VOS, Vector Output, 0=unmodified vector
5178 * 03..00 Reserved, must be 0
5179 */
5180 write_reg(info, ITCR, 0);
5181
5182 return true;
5183}
5184
5185/* initialize adapter hardware
5186 */
5187static bool init_adapter(SLMP_INFO *info)
5188{
5189 int i;
5190
5191 /* Set BIT30 of Local Control Reg 0x50 to reset SCA */
5192 volatile u32 *MiscCtrl = (u32 *)(info->lcr_base + 0x50);
5193 u32 readval;
5194
5195 info->misc_ctrl_value |= BIT30;
5196 *MiscCtrl = info->misc_ctrl_value;
5197
5198 /*
5199 * Force at least 170ns delay before clearing
5200 * reset bit. Each read from LCR takes at least
5201 * 30ns so 10 times for 300ns to be safe.
5202 */
5203 for(i=0;i<10;i++)
5204 readval = *MiscCtrl;
5205
5206 info->misc_ctrl_value &= ~BIT30;
5207 *MiscCtrl = info->misc_ctrl_value;
5208
5209 /* init control reg (all DTRs off, all clksel=input) */
5210 info->ctrlreg_value = 0xaa;
5211 write_control_reg(info);
5212
5213 {
5214 volatile u32 *LCR1BRDR = (u32 *)(info->lcr_base + 0x2c);
5215 lcr1_brdr_value &= ~(BIT5 + BIT4 + BIT3);
5216
5217 switch(read_ahead_count)
5218 {
5219 case 16:
5220 lcr1_brdr_value |= BIT5 + BIT4 + BIT3;
5221 break;
5222 case 8:
5223 lcr1_brdr_value |= BIT5 + BIT4;
5224 break;
5225 case 4:
5226 lcr1_brdr_value |= BIT5 + BIT3;
5227 break;
5228 case 0:
5229 lcr1_brdr_value |= BIT5;
5230 break;
5231 }
5232
5233 *LCR1BRDR = lcr1_brdr_value;
5234 *MiscCtrl = misc_ctrl_value;
5235 }
5236
5237 sca_init(info->port_array[0]);
5238 sca_init(info->port_array[2]);
5239
5240 return true;
5241}
5242
5243/* Loopback an HDLC frame to test the hardware
5244 * interrupt and DMA functions.
5245 */
5246static bool loopback_test(SLMP_INFO *info)
5247{
5248#define TESTFRAMESIZE 20
5249
5250 unsigned long timeout;
5251 u16 count = TESTFRAMESIZE;
5252 unsigned char buf[TESTFRAMESIZE];
5253 bool rc = false;
5254 unsigned long flags;
5255
5256 struct tty_struct *oldtty = info->port.tty;
5257 u32 speed = info->params.clock_speed;
5258
5259 info->params.clock_speed = 3686400;
5260 info->port.tty = NULL;
5261
5262 /* assume failure */
5263 info->init_error = DiagStatus_DmaFailure;
5264
5265 /* build and send transmit frame */
5266 for (count = 0; count < TESTFRAMESIZE;++count)
5267 buf[count] = (unsigned char)count;
5268
5269 memset(info->tmp_rx_buf,0,TESTFRAMESIZE);
5270
5271 /* program hardware for HDLC and enabled receiver */
5272 spin_lock_irqsave(&info->lock,flags);
5273 hdlc_mode(info);
5274 enable_loopback(info,1);
5275 rx_start(info);
5276 info->tx_count = count;
5277 tx_load_dma_buffer(info,buf,count);
5278 tx_start(info);
5279 spin_unlock_irqrestore(&info->lock,flags);
5280
5281 /* wait for receive complete */
5282 /* Set a timeout for waiting for interrupt. */
5283 for ( timeout = 100; timeout; --timeout ) {
5284 msleep_interruptible(10);
5285
5286 if (rx_get_frame(info)) {
5287 rc = true;
5288 break;
5289 }
5290 }
5291
5292 /* verify received frame length and contents */
5293 if (rc &&
5294 ( info->tmp_rx_buf_count != count ||
5295 memcmp(buf, info->tmp_rx_buf,count))) {
5296 rc = false;
5297 }
5298
5299 spin_lock_irqsave(&info->lock,flags);
5300 reset_adapter(info);
5301 spin_unlock_irqrestore(&info->lock,flags);
5302
5303 info->params.clock_speed = speed;
5304 info->port.tty = oldtty;
5305
5306 return rc;
5307}
5308
5309/* Perform diagnostics on hardware
5310 */
5311static int adapter_test( SLMP_INFO *info )
5312{
5313 unsigned long flags;
5314 if ( debug_level >= DEBUG_LEVEL_INFO )
5315 printk( "%s(%d):Testing device %s\n",
5316 __FILE__,__LINE__,info->device_name );
5317
5318 spin_lock_irqsave(&info->lock,flags);
5319 init_adapter(info);
5320 spin_unlock_irqrestore(&info->lock,flags);
5321
5322 info->port_array[0]->port_count = 0;
5323
5324 if ( register_test(info->port_array[0]) &&
5325 register_test(info->port_array[1])) {
5326
5327 info->port_array[0]->port_count = 2;
5328
5329 if ( register_test(info->port_array[2]) &&
5330 register_test(info->port_array[3]) )
5331 info->port_array[0]->port_count += 2;
5332 }
5333 else {
5334 printk( "%s(%d):Register test failure for device %s Addr=%08lX\n",
5335 __FILE__,__LINE__,info->device_name, (unsigned long)(info->phys_sca_base));
5336 return -ENODEV;
5337 }
5338
5339 if ( !irq_test(info->port_array[0]) ||
5340 !irq_test(info->port_array[1]) ||
5341 (info->port_count == 4 && !irq_test(info->port_array[2])) ||
5342 (info->port_count == 4 && !irq_test(info->port_array[3]))) {
5343 printk( "%s(%d):Interrupt test failure for device %s IRQ=%d\n",
5344 __FILE__,__LINE__,info->device_name, (unsigned short)(info->irq_level) );
5345 return -ENODEV;
5346 }
5347
5348 if (!loopback_test(info->port_array[0]) ||
5349 !loopback_test(info->port_array[1]) ||
5350 (info->port_count == 4 && !loopback_test(info->port_array[2])) ||
5351 (info->port_count == 4 && !loopback_test(info->port_array[3]))) {
5352 printk( "%s(%d):DMA test failure for device %s\n",
5353 __FILE__,__LINE__,info->device_name);
5354 return -ENODEV;
5355 }
5356
5357 if ( debug_level >= DEBUG_LEVEL_INFO )
5358 printk( "%s(%d):device %s passed diagnostics\n",
5359 __FILE__,__LINE__,info->device_name );
5360
5361 info->port_array[0]->init_error = 0;
5362 info->port_array[1]->init_error = 0;
5363 if ( info->port_count > 2 ) {
5364 info->port_array[2]->init_error = 0;
5365 info->port_array[3]->init_error = 0;
5366 }
5367
5368 return 0;
5369}
5370
5371/* Test the shared memory on a PCI adapter.
5372 */
5373static bool memory_test(SLMP_INFO *info)
5374{
5375 static unsigned long testval[] = { 0x0, 0x55555555, 0xaaaaaaaa,
5376 0x66666666, 0x99999999, 0xffffffff, 0x12345678 };
5377 unsigned long count = ARRAY_SIZE(testval);
5378 unsigned long i;
5379 unsigned long limit = SCA_MEM_SIZE/sizeof(unsigned long);
5380 unsigned long * addr = (unsigned long *)info->memory_base;
5381
5382 /* Test data lines with test pattern at one location. */
5383
5384 for ( i = 0 ; i < count ; i++ ) {
5385 *addr = testval[i];
5386 if ( *addr != testval[i] )
5387 return false;
5388 }
5389
5390 /* Test address lines with incrementing pattern over */
5391 /* entire address range. */
5392
5393 for ( i = 0 ; i < limit ; i++ ) {
5394 *addr = i * 4;
5395 addr++;
5396 }
5397
5398 addr = (unsigned long *)info->memory_base;
5399
5400 for ( i = 0 ; i < limit ; i++ ) {
5401 if ( *addr != i * 4 )
5402 return false;
5403 addr++;
5404 }
5405
5406 memset( info->memory_base, 0, SCA_MEM_SIZE );
5407 return true;
5408}
5409
5410/* Load data into PCI adapter shared memory.
5411 *
5412 * The PCI9050 releases control of the local bus
5413 * after completing the current read or write operation.
5414 *
5415 * While the PCI9050 write FIFO not empty, the
5416 * PCI9050 treats all of the writes as a single transaction
5417 * and does not release the bus. This causes DMA latency problems
5418 * at high speeds when copying large data blocks to the shared memory.
5419 *
5420 * This function breaks a write into multiple transations by
5421 * interleaving a read which flushes the write FIFO and 'completes'
5422 * the write transation. This allows any pending DMA request to gain control
5423 * of the local bus in a timely fasion.
5424 */
5425static void load_pci_memory(SLMP_INFO *info, char* dest, const char* src, unsigned short count)
5426{
5427 /* A load interval of 16 allows for 4 32-bit writes at */
5428 /* 136ns each for a maximum latency of 542ns on the local bus.*/
5429
5430 unsigned short interval = count / sca_pci_load_interval;
5431 unsigned short i;
5432
5433 for ( i = 0 ; i < interval ; i++ )
5434 {
5435 memcpy(dest, src, sca_pci_load_interval);
5436 read_status_reg(info);
5437 dest += sca_pci_load_interval;
5438 src += sca_pci_load_interval;
5439 }
5440
5441 memcpy(dest, src, count % sca_pci_load_interval);
5442}
5443
5444static void trace_block(SLMP_INFO *info,const char* data, int count, int xmit)
5445{
5446 int i;
5447 int linecount;
5448 if (xmit)
5449 printk("%s tx data:\n",info->device_name);
5450 else
5451 printk("%s rx data:\n",info->device_name);
5452
5453 while(count) {
5454 if (count > 16)
5455 linecount = 16;
5456 else
5457 linecount = count;
5458
5459 for(i=0;i<linecount;i++)
5460 printk("%02X ",(unsigned char)data[i]);
5461 for(;i<17;i++)
5462 printk(" ");
5463 for(i=0;i<linecount;i++) {
5464 if (data[i]>=040 && data[i]<=0176)
5465 printk("%c",data[i]);
5466 else
5467 printk(".");
5468 }
5469 printk("\n");
5470
5471 data += linecount;
5472 count -= linecount;
5473 }
5474} /* end of trace_block() */
5475
5476/* called when HDLC frame times out
5477 * update stats and do tx completion processing
5478 */
5479static void tx_timeout(unsigned long context)
5480{
5481 SLMP_INFO *info = (SLMP_INFO*)context;
5482 unsigned long flags;
5483
5484 if ( debug_level >= DEBUG_LEVEL_INFO )
5485 printk( "%s(%d):%s tx_timeout()\n",
5486 __FILE__,__LINE__,info->device_name);
5487 if(info->tx_active && info->params.mode == MGSL_MODE_HDLC) {
5488 info->icount.txtimeout++;
5489 }
5490 spin_lock_irqsave(&info->lock,flags);
5491 info->tx_active = false;
5492 info->tx_count = info->tx_put = info->tx_get = 0;
5493
5494 spin_unlock_irqrestore(&info->lock,flags);
5495
5496#if SYNCLINK_GENERIC_HDLC
5497 if (info->netcount)
5498 hdlcdev_tx_done(info);
5499 else
5500#endif
5501 bh_transmit(info);
5502}
5503
5504/* called to periodically check the DSR/RI modem signal input status
5505 */
5506static void status_timeout(unsigned long context)
5507{
5508 u16 status = 0;
5509 SLMP_INFO *info = (SLMP_INFO*)context;
5510 unsigned long flags;
5511 unsigned char delta;
5512
5513
5514 spin_lock_irqsave(&info->lock,flags);
5515 get_signals(info);
5516 spin_unlock_irqrestore(&info->lock,flags);
5517
5518 /* check for DSR/RI state change */
5519
5520 delta = info->old_signals ^ info->serial_signals;
5521 info->old_signals = info->serial_signals;
5522
5523 if (delta & SerialSignal_DSR)
5524 status |= MISCSTATUS_DSR_LATCHED|(info->serial_signals&SerialSignal_DSR);
5525
5526 if (delta & SerialSignal_RI)
5527 status |= MISCSTATUS_RI_LATCHED|(info->serial_signals&SerialSignal_RI);
5528
5529 if (delta & SerialSignal_DCD)
5530 status |= MISCSTATUS_DCD_LATCHED|(info->serial_signals&SerialSignal_DCD);
5531
5532 if (delta & SerialSignal_CTS)
5533 status |= MISCSTATUS_CTS_LATCHED|(info->serial_signals&SerialSignal_CTS);
5534
5535 if (status)
5536 isr_io_pin(info,status);
5537
5538 mod_timer(&info->status_timer, jiffies + msecs_to_jiffies(10));
5539}
5540
5541
5542/* Register Access Routines -
5543 * All registers are memory mapped
5544 */
5545#define CALC_REGADDR() \
5546 unsigned char * RegAddr = (unsigned char*)(info->sca_base + Addr); \
5547 if (info->port_num > 1) \
5548 RegAddr += 256; /* port 0-1 SCA0, 2-3 SCA1 */ \
5549 if ( info->port_num & 1) { \
5550 if (Addr > 0x7f) \
5551 RegAddr += 0x40; /* DMA access */ \
5552 else if (Addr > 0x1f && Addr < 0x60) \
5553 RegAddr += 0x20; /* MSCI access */ \
5554 }
5555
5556
5557static unsigned char read_reg(SLMP_INFO * info, unsigned char Addr)
5558{
5559 CALC_REGADDR();
5560 return *RegAddr;
5561}
5562static void write_reg(SLMP_INFO * info, unsigned char Addr, unsigned char Value)
5563{
5564 CALC_REGADDR();
5565 *RegAddr = Value;
5566}
5567
5568static u16 read_reg16(SLMP_INFO * info, unsigned char Addr)
5569{
5570 CALC_REGADDR();
5571 return *((u16 *)RegAddr);
5572}
5573
5574static void write_reg16(SLMP_INFO * info, unsigned char Addr, u16 Value)
5575{
5576 CALC_REGADDR();
5577 *((u16 *)RegAddr) = Value;
5578}
5579
5580static unsigned char read_status_reg(SLMP_INFO * info)
5581{
5582 unsigned char *RegAddr = (unsigned char *)info->statctrl_base;
5583 return *RegAddr;
5584}
5585
5586static void write_control_reg(SLMP_INFO * info)
5587{
5588 unsigned char *RegAddr = (unsigned char *)info->statctrl_base;
5589 *RegAddr = info->port_array[0]->ctrlreg_value;
5590}
5591
5592
5593static int __devinit synclinkmp_init_one (struct pci_dev *dev,
5594 const struct pci_device_id *ent)
5595{
5596 if (pci_enable_device(dev)) {
5597 printk("error enabling pci device %p\n", dev);
5598 return -EIO;
5599 }
5600 device_init( ++synclinkmp_adapter_count, dev );
5601 return 0;
5602}
5603
5604static void __devexit synclinkmp_remove_one (struct pci_dev *dev)
5605{
5606}
diff --git a/drivers/char/sysrq.c b/drivers/char/sysrq.c
deleted file mode 100644
index ef31bb81e843..000000000000
--- a/drivers/char/sysrq.c
+++ /dev/null
@@ -1,801 +0,0 @@
1/*
2 * Linux Magic System Request Key Hacks
3 *
4 * (c) 1997 Martin Mares <mj@atrey.karlin.mff.cuni.cz>
5 * based on ideas by Pavel Machek <pavel@atrey.karlin.mff.cuni.cz>
6 *
7 * (c) 2000 Crutcher Dunnavant <crutcher+kernel@datastacks.com>
8 * overhauled to use key registration
9 * based upon discusions in irc://irc.openprojects.net/#kernelnewbies
10 *
11 * Copyright (c) 2010 Dmitry Torokhov
12 * Input handler conversion
13 */
14
15#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16
17#include <linux/sched.h>
18#include <linux/interrupt.h>
19#include <linux/mm.h>
20#include <linux/fs.h>
21#include <linux/mount.h>
22#include <linux/kdev_t.h>
23#include <linux/major.h>
24#include <linux/reboot.h>
25#include <linux/sysrq.h>
26#include <linux/kbd_kern.h>
27#include <linux/proc_fs.h>
28#include <linux/nmi.h>
29#include <linux/quotaops.h>
30#include <linux/perf_event.h>
31#include <linux/kernel.h>
32#include <linux/module.h>
33#include <linux/suspend.h>
34#include <linux/writeback.h>
35#include <linux/buffer_head.h> /* for fsync_bdev() */
36#include <linux/swap.h>
37#include <linux/spinlock.h>
38#include <linux/vt_kern.h>
39#include <linux/workqueue.h>
40#include <linux/hrtimer.h>
41#include <linux/oom.h>
42#include <linux/slab.h>
43#include <linux/input.h>
44
45#include <asm/ptrace.h>
46#include <asm/irq_regs.h>
47
48/* Whether we react on sysrq keys or just ignore them */
49static int __read_mostly sysrq_enabled = 1;
50static bool __read_mostly sysrq_always_enabled;
51
52static bool sysrq_on(void)
53{
54 return sysrq_enabled || sysrq_always_enabled;
55}
56
57/*
58 * A value of 1 means 'all', other nonzero values are an op mask:
59 */
60static bool sysrq_on_mask(int mask)
61{
62 return sysrq_always_enabled ||
63 sysrq_enabled == 1 ||
64 (sysrq_enabled & mask);
65}
66
67static int __init sysrq_always_enabled_setup(char *str)
68{
69 sysrq_always_enabled = true;
70 pr_info("sysrq always enabled.\n");
71
72 return 1;
73}
74
75__setup("sysrq_always_enabled", sysrq_always_enabled_setup);
76
77
78static void sysrq_handle_loglevel(int key)
79{
80 int i;
81
82 i = key - '0';
83 console_loglevel = 7;
84 printk("Loglevel set to %d\n", i);
85 console_loglevel = i;
86}
87static struct sysrq_key_op sysrq_loglevel_op = {
88 .handler = sysrq_handle_loglevel,
89 .help_msg = "loglevel(0-9)",
90 .action_msg = "Changing Loglevel",
91 .enable_mask = SYSRQ_ENABLE_LOG,
92};
93
94#ifdef CONFIG_VT
95static void sysrq_handle_SAK(int key)
96{
97 struct work_struct *SAK_work = &vc_cons[fg_console].SAK_work;
98 schedule_work(SAK_work);
99}
100static struct sysrq_key_op sysrq_SAK_op = {
101 .handler = sysrq_handle_SAK,
102 .help_msg = "saK",
103 .action_msg = "SAK",
104 .enable_mask = SYSRQ_ENABLE_KEYBOARD,
105};
106#else
107#define sysrq_SAK_op (*(struct sysrq_key_op *)NULL)
108#endif
109
110#ifdef CONFIG_VT
111static void sysrq_handle_unraw(int key)
112{
113 struct kbd_struct *kbd = &kbd_table[fg_console];
114
115 if (kbd)
116 kbd->kbdmode = default_utf8 ? VC_UNICODE : VC_XLATE;
117}
118static struct sysrq_key_op sysrq_unraw_op = {
119 .handler = sysrq_handle_unraw,
120 .help_msg = "unRaw",
121 .action_msg = "Keyboard mode set to system default",
122 .enable_mask = SYSRQ_ENABLE_KEYBOARD,
123};
124#else
125#define sysrq_unraw_op (*(struct sysrq_key_op *)NULL)
126#endif /* CONFIG_VT */
127
128static void sysrq_handle_crash(int key)
129{
130 char *killer = NULL;
131
132 panic_on_oops = 1; /* force panic */
133 wmb();
134 *killer = 1;
135}
136static struct sysrq_key_op sysrq_crash_op = {
137 .handler = sysrq_handle_crash,
138 .help_msg = "Crash",
139 .action_msg = "Trigger a crash",
140 .enable_mask = SYSRQ_ENABLE_DUMP,
141};
142
143static void sysrq_handle_reboot(int key)
144{
145 lockdep_off();
146 local_irq_enable();
147 emergency_restart();
148}
149static struct sysrq_key_op sysrq_reboot_op = {
150 .handler = sysrq_handle_reboot,
151 .help_msg = "reBoot",
152 .action_msg = "Resetting",
153 .enable_mask = SYSRQ_ENABLE_BOOT,
154};
155
156static void sysrq_handle_sync(int key)
157{
158 emergency_sync();
159}
160static struct sysrq_key_op sysrq_sync_op = {
161 .handler = sysrq_handle_sync,
162 .help_msg = "Sync",
163 .action_msg = "Emergency Sync",
164 .enable_mask = SYSRQ_ENABLE_SYNC,
165};
166
167static void sysrq_handle_show_timers(int key)
168{
169 sysrq_timer_list_show();
170}
171
172static struct sysrq_key_op sysrq_show_timers_op = {
173 .handler = sysrq_handle_show_timers,
174 .help_msg = "show-all-timers(Q)",
175 .action_msg = "Show clockevent devices & pending hrtimers (no others)",
176};
177
178static void sysrq_handle_mountro(int key)
179{
180 emergency_remount();
181}
182static struct sysrq_key_op sysrq_mountro_op = {
183 .handler = sysrq_handle_mountro,
184 .help_msg = "Unmount",
185 .action_msg = "Emergency Remount R/O",
186 .enable_mask = SYSRQ_ENABLE_REMOUNT,
187};
188
189#ifdef CONFIG_LOCKDEP
190static void sysrq_handle_showlocks(int key)
191{
192 debug_show_all_locks();
193}
194
195static struct sysrq_key_op sysrq_showlocks_op = {
196 .handler = sysrq_handle_showlocks,
197 .help_msg = "show-all-locks(D)",
198 .action_msg = "Show Locks Held",
199};
200#else
201#define sysrq_showlocks_op (*(struct sysrq_key_op *)NULL)
202#endif
203
204#ifdef CONFIG_SMP
205static DEFINE_SPINLOCK(show_lock);
206
207static void showacpu(void *dummy)
208{
209 unsigned long flags;
210
211 /* Idle CPUs have no interesting backtrace. */
212 if (idle_cpu(smp_processor_id()))
213 return;
214
215 spin_lock_irqsave(&show_lock, flags);
216 printk(KERN_INFO "CPU%d:\n", smp_processor_id());
217 show_stack(NULL, NULL);
218 spin_unlock_irqrestore(&show_lock, flags);
219}
220
221static void sysrq_showregs_othercpus(struct work_struct *dummy)
222{
223 smp_call_function(showacpu, NULL, 0);
224}
225
226static DECLARE_WORK(sysrq_showallcpus, sysrq_showregs_othercpus);
227
228static void sysrq_handle_showallcpus(int key)
229{
230 /*
231 * Fall back to the workqueue based printing if the
232 * backtrace printing did not succeed or the
233 * architecture has no support for it:
234 */
235 if (!trigger_all_cpu_backtrace()) {
236 struct pt_regs *regs = get_irq_regs();
237
238 if (regs) {
239 printk(KERN_INFO "CPU%d:\n", smp_processor_id());
240 show_regs(regs);
241 }
242 schedule_work(&sysrq_showallcpus);
243 }
244}
245
246static struct sysrq_key_op sysrq_showallcpus_op = {
247 .handler = sysrq_handle_showallcpus,
248 .help_msg = "show-backtrace-all-active-cpus(L)",
249 .action_msg = "Show backtrace of all active CPUs",
250 .enable_mask = SYSRQ_ENABLE_DUMP,
251};
252#endif
253
254static void sysrq_handle_showregs(int key)
255{
256 struct pt_regs *regs = get_irq_regs();
257 if (regs)
258 show_regs(regs);
259 perf_event_print_debug();
260}
261static struct sysrq_key_op sysrq_showregs_op = {
262 .handler = sysrq_handle_showregs,
263 .help_msg = "show-registers(P)",
264 .action_msg = "Show Regs",
265 .enable_mask = SYSRQ_ENABLE_DUMP,
266};
267
268static void sysrq_handle_showstate(int key)
269{
270 show_state();
271}
272static struct sysrq_key_op sysrq_showstate_op = {
273 .handler = sysrq_handle_showstate,
274 .help_msg = "show-task-states(T)",
275 .action_msg = "Show State",
276 .enable_mask = SYSRQ_ENABLE_DUMP,
277};
278
279static void sysrq_handle_showstate_blocked(int key)
280{
281 show_state_filter(TASK_UNINTERRUPTIBLE);
282}
283static struct sysrq_key_op sysrq_showstate_blocked_op = {
284 .handler = sysrq_handle_showstate_blocked,
285 .help_msg = "show-blocked-tasks(W)",
286 .action_msg = "Show Blocked State",
287 .enable_mask = SYSRQ_ENABLE_DUMP,
288};
289
290#ifdef CONFIG_TRACING
291#include <linux/ftrace.h>
292
293static void sysrq_ftrace_dump(int key)
294{
295 ftrace_dump(DUMP_ALL);
296}
297static struct sysrq_key_op sysrq_ftrace_dump_op = {
298 .handler = sysrq_ftrace_dump,
299 .help_msg = "dump-ftrace-buffer(Z)",
300 .action_msg = "Dump ftrace buffer",
301 .enable_mask = SYSRQ_ENABLE_DUMP,
302};
303#else
304#define sysrq_ftrace_dump_op (*(struct sysrq_key_op *)NULL)
305#endif
306
307static void sysrq_handle_showmem(int key)
308{
309 show_mem();
310}
311static struct sysrq_key_op sysrq_showmem_op = {
312 .handler = sysrq_handle_showmem,
313 .help_msg = "show-memory-usage(M)",
314 .action_msg = "Show Memory",
315 .enable_mask = SYSRQ_ENABLE_DUMP,
316};
317
318/*
319 * Signal sysrq helper function. Sends a signal to all user processes.
320 */
321static void send_sig_all(int sig)
322{
323 struct task_struct *p;
324
325 for_each_process(p) {
326 if (p->mm && !is_global_init(p))
327 /* Not swapper, init nor kernel thread */
328 force_sig(sig, p);
329 }
330}
331
332static void sysrq_handle_term(int key)
333{
334 send_sig_all(SIGTERM);
335 console_loglevel = 8;
336}
337static struct sysrq_key_op sysrq_term_op = {
338 .handler = sysrq_handle_term,
339 .help_msg = "terminate-all-tasks(E)",
340 .action_msg = "Terminate All Tasks",
341 .enable_mask = SYSRQ_ENABLE_SIGNAL,
342};
343
344static void moom_callback(struct work_struct *ignored)
345{
346 out_of_memory(node_zonelist(0, GFP_KERNEL), GFP_KERNEL, 0, NULL);
347}
348
349static DECLARE_WORK(moom_work, moom_callback);
350
351static void sysrq_handle_moom(int key)
352{
353 schedule_work(&moom_work);
354}
355static struct sysrq_key_op sysrq_moom_op = {
356 .handler = sysrq_handle_moom,
357 .help_msg = "memory-full-oom-kill(F)",
358 .action_msg = "Manual OOM execution",
359 .enable_mask = SYSRQ_ENABLE_SIGNAL,
360};
361
362#ifdef CONFIG_BLOCK
363static void sysrq_handle_thaw(int key)
364{
365 emergency_thaw_all();
366}
367static struct sysrq_key_op sysrq_thaw_op = {
368 .handler = sysrq_handle_thaw,
369 .help_msg = "thaw-filesystems(J)",
370 .action_msg = "Emergency Thaw of all frozen filesystems",
371 .enable_mask = SYSRQ_ENABLE_SIGNAL,
372};
373#endif
374
375static void sysrq_handle_kill(int key)
376{
377 send_sig_all(SIGKILL);
378 console_loglevel = 8;
379}
380static struct sysrq_key_op sysrq_kill_op = {
381 .handler = sysrq_handle_kill,
382 .help_msg = "kill-all-tasks(I)",
383 .action_msg = "Kill All Tasks",
384 .enable_mask = SYSRQ_ENABLE_SIGNAL,
385};
386
387static void sysrq_handle_unrt(int key)
388{
389 normalize_rt_tasks();
390}
391static struct sysrq_key_op sysrq_unrt_op = {
392 .handler = sysrq_handle_unrt,
393 .help_msg = "nice-all-RT-tasks(N)",
394 .action_msg = "Nice All RT Tasks",
395 .enable_mask = SYSRQ_ENABLE_RTNICE,
396};
397
398/* Key Operations table and lock */
399static DEFINE_SPINLOCK(sysrq_key_table_lock);
400
401static struct sysrq_key_op *sysrq_key_table[36] = {
402 &sysrq_loglevel_op, /* 0 */
403 &sysrq_loglevel_op, /* 1 */
404 &sysrq_loglevel_op, /* 2 */
405 &sysrq_loglevel_op, /* 3 */
406 &sysrq_loglevel_op, /* 4 */
407 &sysrq_loglevel_op, /* 5 */
408 &sysrq_loglevel_op, /* 6 */
409 &sysrq_loglevel_op, /* 7 */
410 &sysrq_loglevel_op, /* 8 */
411 &sysrq_loglevel_op, /* 9 */
412
413 /*
414 * a: Don't use for system provided sysrqs, it is handled specially on
415 * sparc and will never arrive.
416 */
417 NULL, /* a */
418 &sysrq_reboot_op, /* b */
419 &sysrq_crash_op, /* c & ibm_emac driver debug */
420 &sysrq_showlocks_op, /* d */
421 &sysrq_term_op, /* e */
422 &sysrq_moom_op, /* f */
423 /* g: May be registered for the kernel debugger */
424 NULL, /* g */
425 NULL, /* h - reserved for help */
426 &sysrq_kill_op, /* i */
427#ifdef CONFIG_BLOCK
428 &sysrq_thaw_op, /* j */
429#else
430 NULL, /* j */
431#endif
432 &sysrq_SAK_op, /* k */
433#ifdef CONFIG_SMP
434 &sysrq_showallcpus_op, /* l */
435#else
436 NULL, /* l */
437#endif
438 &sysrq_showmem_op, /* m */
439 &sysrq_unrt_op, /* n */
440 /* o: This will often be registered as 'Off' at init time */
441 NULL, /* o */
442 &sysrq_showregs_op, /* p */
443 &sysrq_show_timers_op, /* q */
444 &sysrq_unraw_op, /* r */
445 &sysrq_sync_op, /* s */
446 &sysrq_showstate_op, /* t */
447 &sysrq_mountro_op, /* u */
448 /* v: May be registered for frame buffer console restore */
449 NULL, /* v */
450 &sysrq_showstate_blocked_op, /* w */
451 /* x: May be registered on ppc/powerpc for xmon */
452 NULL, /* x */
453 /* y: May be registered on sparc64 for global register dump */
454 NULL, /* y */
455 &sysrq_ftrace_dump_op, /* z */
456};
457
458/* key2index calculation, -1 on invalid index */
459static int sysrq_key_table_key2index(int key)
460{
461 int retval;
462
463 if ((key >= '0') && (key <= '9'))
464 retval = key - '0';
465 else if ((key >= 'a') && (key <= 'z'))
466 retval = key + 10 - 'a';
467 else
468 retval = -1;
469 return retval;
470}
471
472/*
473 * get and put functions for the table, exposed to modules.
474 */
475struct sysrq_key_op *__sysrq_get_key_op(int key)
476{
477 struct sysrq_key_op *op_p = NULL;
478 int i;
479
480 i = sysrq_key_table_key2index(key);
481 if (i != -1)
482 op_p = sysrq_key_table[i];
483
484 return op_p;
485}
486
487static void __sysrq_put_key_op(int key, struct sysrq_key_op *op_p)
488{
489 int i = sysrq_key_table_key2index(key);
490
491 if (i != -1)
492 sysrq_key_table[i] = op_p;
493}
494
495void __handle_sysrq(int key, bool check_mask)
496{
497 struct sysrq_key_op *op_p;
498 int orig_log_level;
499 int i;
500 unsigned long flags;
501
502 spin_lock_irqsave(&sysrq_key_table_lock, flags);
503 /*
504 * Raise the apparent loglevel to maximum so that the sysrq header
505 * is shown to provide the user with positive feedback. We do not
506 * simply emit this at KERN_EMERG as that would change message
507 * routing in the consumers of /proc/kmsg.
508 */
509 orig_log_level = console_loglevel;
510 console_loglevel = 7;
511 printk(KERN_INFO "SysRq : ");
512
513 op_p = __sysrq_get_key_op(key);
514 if (op_p) {
515 /*
516 * Should we check for enabled operations (/proc/sysrq-trigger
517 * should not) and is the invoked operation enabled?
518 */
519 if (!check_mask || sysrq_on_mask(op_p->enable_mask)) {
520 printk("%s\n", op_p->action_msg);
521 console_loglevel = orig_log_level;
522 op_p->handler(key);
523 } else {
524 printk("This sysrq operation is disabled.\n");
525 }
526 } else {
527 printk("HELP : ");
528 /* Only print the help msg once per handler */
529 for (i = 0; i < ARRAY_SIZE(sysrq_key_table); i++) {
530 if (sysrq_key_table[i]) {
531 int j;
532
533 for (j = 0; sysrq_key_table[i] !=
534 sysrq_key_table[j]; j++)
535 ;
536 if (j != i)
537 continue;
538 printk("%s ", sysrq_key_table[i]->help_msg);
539 }
540 }
541 printk("\n");
542 console_loglevel = orig_log_level;
543 }
544 spin_unlock_irqrestore(&sysrq_key_table_lock, flags);
545}
546
547void handle_sysrq(int key)
548{
549 if (sysrq_on())
550 __handle_sysrq(key, true);
551}
552EXPORT_SYMBOL(handle_sysrq);
553
554#ifdef CONFIG_INPUT
555
556/* Simple translation table for the SysRq keys */
557static const unsigned char sysrq_xlate[KEY_MAX + 1] =
558 "\000\0331234567890-=\177\t" /* 0x00 - 0x0f */
559 "qwertyuiop[]\r\000as" /* 0x10 - 0x1f */
560 "dfghjkl;'`\000\\zxcv" /* 0x20 - 0x2f */
561 "bnm,./\000*\000 \000\201\202\203\204\205" /* 0x30 - 0x3f */
562 "\206\207\210\211\212\000\000789-456+1" /* 0x40 - 0x4f */
563 "230\177\000\000\213\214\000\000\000\000\000\000\000\000\000\000" /* 0x50 - 0x5f */
564 "\r\000/"; /* 0x60 - 0x6f */
565
566static bool sysrq_down;
567static int sysrq_alt_use;
568static int sysrq_alt;
569
570static bool sysrq_filter(struct input_handle *handle, unsigned int type,
571 unsigned int code, int value)
572{
573 if (type != EV_KEY)
574 goto out;
575
576 switch (code) {
577
578 case KEY_LEFTALT:
579 case KEY_RIGHTALT:
580 if (value)
581 sysrq_alt = code;
582 else {
583 if (sysrq_down && code == sysrq_alt_use)
584 sysrq_down = false;
585
586 sysrq_alt = 0;
587 }
588 break;
589
590 case KEY_SYSRQ:
591 if (value == 1 && sysrq_alt) {
592 sysrq_down = true;
593 sysrq_alt_use = sysrq_alt;
594 }
595 break;
596
597 default:
598 if (sysrq_down && value && value != 2)
599 __handle_sysrq(sysrq_xlate[code], true);
600 break;
601 }
602
603out:
604 return sysrq_down;
605}
606
607static int sysrq_connect(struct input_handler *handler,
608 struct input_dev *dev,
609 const struct input_device_id *id)
610{
611 struct input_handle *handle;
612 int error;
613
614 sysrq_down = false;
615 sysrq_alt = 0;
616
617 handle = kzalloc(sizeof(struct input_handle), GFP_KERNEL);
618 if (!handle)
619 return -ENOMEM;
620
621 handle->dev = dev;
622 handle->handler = handler;
623 handle->name = "sysrq";
624
625 error = input_register_handle(handle);
626 if (error) {
627 pr_err("Failed to register input sysrq handler, error %d\n",
628 error);
629 goto err_free;
630 }
631
632 error = input_open_device(handle);
633 if (error) {
634 pr_err("Failed to open input device, error %d\n", error);
635 goto err_unregister;
636 }
637
638 return 0;
639
640 err_unregister:
641 input_unregister_handle(handle);
642 err_free:
643 kfree(handle);
644 return error;
645}
646
647static void sysrq_disconnect(struct input_handle *handle)
648{
649 input_close_device(handle);
650 input_unregister_handle(handle);
651 kfree(handle);
652}
653
654/*
655 * We are matching on KEY_LEFTALT insteard of KEY_SYSRQ because not all
656 * keyboards have SysRq ikey predefined and so user may add it to keymap
657 * later, but we expect all such keyboards to have left alt.
658 */
659static const struct input_device_id sysrq_ids[] = {
660 {
661 .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
662 INPUT_DEVICE_ID_MATCH_KEYBIT,
663 .evbit = { BIT_MASK(EV_KEY) },
664 .keybit = { BIT_MASK(KEY_LEFTALT) },
665 },
666 { },
667};
668
669static struct input_handler sysrq_handler = {
670 .filter = sysrq_filter,
671 .connect = sysrq_connect,
672 .disconnect = sysrq_disconnect,
673 .name = "sysrq",
674 .id_table = sysrq_ids,
675};
676
677static bool sysrq_handler_registered;
678
679static inline void sysrq_register_handler(void)
680{
681 int error;
682
683 error = input_register_handler(&sysrq_handler);
684 if (error)
685 pr_err("Failed to register input handler, error %d", error);
686 else
687 sysrq_handler_registered = true;
688}
689
690static inline void sysrq_unregister_handler(void)
691{
692 if (sysrq_handler_registered) {
693 input_unregister_handler(&sysrq_handler);
694 sysrq_handler_registered = false;
695 }
696}
697
698#else
699
700static inline void sysrq_register_handler(void)
701{
702}
703
704static inline void sysrq_unregister_handler(void)
705{
706}
707
708#endif /* CONFIG_INPUT */
709
710int sysrq_toggle_support(int enable_mask)
711{
712 bool was_enabled = sysrq_on();
713
714 sysrq_enabled = enable_mask;
715
716 if (was_enabled != sysrq_on()) {
717 if (sysrq_on())
718 sysrq_register_handler();
719 else
720 sysrq_unregister_handler();
721 }
722
723 return 0;
724}
725
726static int __sysrq_swap_key_ops(int key, struct sysrq_key_op *insert_op_p,
727 struct sysrq_key_op *remove_op_p)
728{
729 int retval;
730 unsigned long flags;
731
732 spin_lock_irqsave(&sysrq_key_table_lock, flags);
733 if (__sysrq_get_key_op(key) == remove_op_p) {
734 __sysrq_put_key_op(key, insert_op_p);
735 retval = 0;
736 } else {
737 retval = -1;
738 }
739 spin_unlock_irqrestore(&sysrq_key_table_lock, flags);
740 return retval;
741}
742
743int register_sysrq_key(int key, struct sysrq_key_op *op_p)
744{
745 return __sysrq_swap_key_ops(key, op_p, NULL);
746}
747EXPORT_SYMBOL(register_sysrq_key);
748
749int unregister_sysrq_key(int key, struct sysrq_key_op *op_p)
750{
751 return __sysrq_swap_key_ops(key, NULL, op_p);
752}
753EXPORT_SYMBOL(unregister_sysrq_key);
754
755#ifdef CONFIG_PROC_FS
756/*
757 * writing 'C' to /proc/sysrq-trigger is like sysrq-C
758 */
759static ssize_t write_sysrq_trigger(struct file *file, const char __user *buf,
760 size_t count, loff_t *ppos)
761{
762 if (count) {
763 char c;
764
765 if (get_user(c, buf))
766 return -EFAULT;
767 __handle_sysrq(c, false);
768 }
769
770 return count;
771}
772
773static const struct file_operations proc_sysrq_trigger_operations = {
774 .write = write_sysrq_trigger,
775};
776
777static void sysrq_init_procfs(void)
778{
779 if (!proc_create("sysrq-trigger", S_IWUSR, NULL,
780 &proc_sysrq_trigger_operations))
781 pr_err("Failed to register proc interface\n");
782}
783
784#else
785
786static inline void sysrq_init_procfs(void)
787{
788}
789
790#endif /* CONFIG_PROC_FS */
791
792static int __init sysrq_init(void)
793{
794 sysrq_init_procfs();
795
796 if (sysrq_on())
797 sysrq_register_handler();
798
799 return 0;
800}
801module_init(sysrq_init);
diff --git a/drivers/char/tb0219.c b/drivers/char/tb0219.c
index cad4eb65f13d..ad264185eb10 100644
--- a/drivers/char/tb0219.c
+++ b/drivers/char/tb0219.c
@@ -261,6 +261,7 @@ static const struct file_operations tb0219_fops = {
261 .write = tanbac_tb0219_write, 261 .write = tanbac_tb0219_write,
262 .open = tanbac_tb0219_open, 262 .open = tanbac_tb0219_open,
263 .release = tanbac_tb0219_release, 263 .release = tanbac_tb0219_release,
264 .llseek = no_llseek,
264}; 265};
265 266
266static void tb0219_restart(char *command) 267static void tb0219_restart(char *command)
diff --git a/drivers/char/tlclk.c b/drivers/char/tlclk.c
index 80ea6bcfffdc..0c964cdcc223 100644
--- a/drivers/char/tlclk.c
+++ b/drivers/char/tlclk.c
@@ -37,7 +37,7 @@
37#include <linux/ioport.h> 37#include <linux/ioport.h>
38#include <linux/interrupt.h> 38#include <linux/interrupt.h>
39#include <linux/spinlock.h> 39#include <linux/spinlock.h>
40#include <linux/smp_lock.h> 40#include <linux/mutex.h>
41#include <linux/timer.h> 41#include <linux/timer.h>
42#include <linux/sysfs.h> 42#include <linux/sysfs.h>
43#include <linux/device.h> 43#include <linux/device.h>
@@ -206,7 +206,7 @@ static int tlclk_open(struct inode *inode, struct file *filp)
206{ 206{
207 int result; 207 int result;
208 208
209 lock_kernel(); 209 mutex_lock(&tlclk_mutex);
210 if (test_and_set_bit(0, &useflags)) { 210 if (test_and_set_bit(0, &useflags)) {
211 result = -EBUSY; 211 result = -EBUSY;
212 /* this legacy device is always one per system and it doesn't 212 /* this legacy device is always one per system and it doesn't
@@ -229,7 +229,7 @@ static int tlclk_open(struct inode *inode, struct file *filp)
229 inb(TLCLK_REG6); /* Clear interrupt events */ 229 inb(TLCLK_REG6); /* Clear interrupt events */
230 230
231out: 231out:
232 unlock_kernel(); 232 mutex_unlock(&tlclk_mutex);
233 return result; 233 return result;
234} 234}
235 235
@@ -267,6 +267,7 @@ static const struct file_operations tlclk_fops = {
267 .read = tlclk_read, 267 .read = tlclk_read,
268 .open = tlclk_open, 268 .open = tlclk_open,
269 .release = tlclk_release, 269 .release = tlclk_release,
270 .llseek = noop_llseek,
270 271
271}; 272};
272 273
diff --git a/drivers/char/toshiba.c b/drivers/char/toshiba.c
index f8bc79f6de34..014c9d90d297 100644
--- a/drivers/char/toshiba.c
+++ b/drivers/char/toshiba.c
@@ -68,7 +68,7 @@
68#include <linux/stat.h> 68#include <linux/stat.h>
69#include <linux/proc_fs.h> 69#include <linux/proc_fs.h>
70#include <linux/seq_file.h> 70#include <linux/seq_file.h>
71#include <linux/smp_lock.h> 71#include <linux/mutex.h>
72#include <linux/toshiba.h> 72#include <linux/toshiba.h>
73 73
74#define TOSH_MINOR_DEV 181 74#define TOSH_MINOR_DEV 181
@@ -78,6 +78,7 @@ MODULE_AUTHOR("Jonathan Buzzard <jonathan@buzzard.org.uk>");
78MODULE_DESCRIPTION("Toshiba laptop SMM driver"); 78MODULE_DESCRIPTION("Toshiba laptop SMM driver");
79MODULE_SUPPORTED_DEVICE("toshiba"); 79MODULE_SUPPORTED_DEVICE("toshiba");
80 80
81static DEFINE_MUTEX(tosh_mutex);
81static int tosh_fn; 82static int tosh_fn;
82module_param_named(fn, tosh_fn, int, 0); 83module_param_named(fn, tosh_fn, int, 0);
83MODULE_PARM_DESC(fn, "User specified Fn key detection port"); 84MODULE_PARM_DESC(fn, "User specified Fn key detection port");
@@ -95,6 +96,7 @@ static long tosh_ioctl(struct file *, unsigned int,
95static const struct file_operations tosh_fops = { 96static const struct file_operations tosh_fops = {
96 .owner = THIS_MODULE, 97 .owner = THIS_MODULE,
97 .unlocked_ioctl = tosh_ioctl, 98 .unlocked_ioctl = tosh_ioctl,
99 .llseek = noop_llseek,
98}; 100};
99 101
100static struct miscdevice tosh_device = { 102static struct miscdevice tosh_device = {
@@ -274,16 +276,16 @@ static long tosh_ioctl(struct file *fp, unsigned int cmd, unsigned long arg)
274 return -EINVAL; 276 return -EINVAL;
275 277
276 /* do we need to emulate the fan ? */ 278 /* do we need to emulate the fan ? */
277 lock_kernel(); 279 mutex_lock(&tosh_mutex);
278 if (tosh_fan==1) { 280 if (tosh_fan==1) {
279 if (((ax==0xf300) || (ax==0xf400)) && (bx==0x0004)) { 281 if (((ax==0xf300) || (ax==0xf400)) && (bx==0x0004)) {
280 err = tosh_emulate_fan(&regs); 282 err = tosh_emulate_fan(&regs);
281 unlock_kernel(); 283 mutex_unlock(&tosh_mutex);
282 break; 284 break;
283 } 285 }
284 } 286 }
285 err = tosh_smm(&regs); 287 err = tosh_smm(&regs);
286 unlock_kernel(); 288 mutex_unlock(&tosh_mutex);
287 break; 289 break;
288 default: 290 default:
289 return -EINVAL; 291 return -EINVAL;
diff --git a/drivers/char/tpm/Kconfig b/drivers/char/tpm/Kconfig
index 4dc338f3d1aa..f6595aba4f0f 100644
--- a/drivers/char/tpm/Kconfig
+++ b/drivers/char/tpm/Kconfig
@@ -58,6 +58,6 @@ config TCG_INFINEON
58 To compile this driver as a module, choose M here; the module 58 To compile this driver as a module, choose M here; the module
59 will be called tpm_infineon. 59 will be called tpm_infineon.
60 Further information on this driver and the supported hardware 60 Further information on this driver and the supported hardware
61 can be found at http://www.prosec.rub.de/tpm 61 can be found at http://www.trust.rub.de/projects/linux-device-driver-infineon-tpm/
62 62
63endif # TCG_TPM 63endif # TCG_TPM
diff --git a/drivers/char/tpm/tpm.c b/drivers/char/tpm/tpm.c
index 05ad4a17a28f..7beb0e25f1e1 100644
--- a/drivers/char/tpm/tpm.c
+++ b/drivers/char/tpm/tpm.c
@@ -47,6 +47,16 @@ enum tpm_duration {
47#define TPM_MAX_PROTECTED_ORDINAL 12 47#define TPM_MAX_PROTECTED_ORDINAL 12
48#define TPM_PROTECTED_ORDINAL_MASK 0xFF 48#define TPM_PROTECTED_ORDINAL_MASK 0xFF
49 49
50/*
51 * Bug workaround - some TPM's don't flush the most
52 * recently changed pcr on suspend, so force the flush
53 * with an extend to the selected _unused_ non-volatile pcr.
54 */
55static int tpm_suspend_pcr;
56module_param_named(suspend_pcr, tpm_suspend_pcr, uint, 0644);
57MODULE_PARM_DESC(suspend_pcr,
58 "PCR to use for dummy writes to faciltate flush on suspend.");
59
50static LIST_HEAD(tpm_chip_list); 60static LIST_HEAD(tpm_chip_list);
51static DEFINE_SPINLOCK(driver_lock); 61static DEFINE_SPINLOCK(driver_lock);
52static DECLARE_BITMAP(dev_mask, TPM_NUM_DEVICES); 62static DECLARE_BITMAP(dev_mask, TPM_NUM_DEVICES);
@@ -726,7 +736,7 @@ int tpm_pcr_read(u32 chip_num, int pcr_idx, u8 *res_buf)
726 if (chip == NULL) 736 if (chip == NULL)
727 return -ENODEV; 737 return -ENODEV;
728 rc = __tpm_pcr_read(chip, pcr_idx, res_buf); 738 rc = __tpm_pcr_read(chip, pcr_idx, res_buf);
729 module_put(chip->dev->driver->owner); 739 tpm_chip_put(chip);
730 return rc; 740 return rc;
731} 741}
732EXPORT_SYMBOL_GPL(tpm_pcr_read); 742EXPORT_SYMBOL_GPL(tpm_pcr_read);
@@ -765,11 +775,27 @@ int tpm_pcr_extend(u32 chip_num, int pcr_idx, const u8 *hash)
765 rc = transmit_cmd(chip, &cmd, EXTEND_PCR_RESULT_SIZE, 775 rc = transmit_cmd(chip, &cmd, EXTEND_PCR_RESULT_SIZE,
766 "attempting extend a PCR value"); 776 "attempting extend a PCR value");
767 777
768 module_put(chip->dev->driver->owner); 778 tpm_chip_put(chip);
769 return rc; 779 return rc;
770} 780}
771EXPORT_SYMBOL_GPL(tpm_pcr_extend); 781EXPORT_SYMBOL_GPL(tpm_pcr_extend);
772 782
783int tpm_send(u32 chip_num, void *cmd, size_t buflen)
784{
785 struct tpm_chip *chip;
786 int rc;
787
788 chip = tpm_chip_find_get(chip_num);
789 if (chip == NULL)
790 return -ENODEV;
791
792 rc = transmit_cmd(chip, cmd, buflen, "attempting tpm_cmd");
793
794 tpm_chip_put(chip);
795 return rc;
796}
797EXPORT_SYMBOL_GPL(tpm_send);
798
773ssize_t tpm_show_pcrs(struct device *dev, struct device_attribute *attr, 799ssize_t tpm_show_pcrs(struct device *dev, struct device_attribute *attr,
774 char *buf) 800 char *buf)
775{ 801{
@@ -954,7 +980,7 @@ int tpm_open(struct inode *inode, struct file *file)
954 return -EBUSY; 980 return -EBUSY;
955 } 981 }
956 982
957 chip->data_buffer = kmalloc(TPM_BUFSIZE * sizeof(u8), GFP_KERNEL); 983 chip->data_buffer = kzalloc(TPM_BUFSIZE, GFP_KERNEL);
958 if (chip->data_buffer == NULL) { 984 if (chip->data_buffer == NULL) {
959 clear_bit(0, &chip->is_open); 985 clear_bit(0, &chip->is_open);
960 put_device(chip->dev); 986 put_device(chip->dev);
@@ -976,7 +1002,7 @@ int tpm_release(struct inode *inode, struct file *file)
976 struct tpm_chip *chip = file->private_data; 1002 struct tpm_chip *chip = file->private_data;
977 1003
978 del_singleshot_timer_sync(&chip->user_read_timer); 1004 del_singleshot_timer_sync(&chip->user_read_timer);
979 flush_scheduled_work(); 1005 flush_work_sync(&chip->work);
980 file->private_data = NULL; 1006 file->private_data = NULL;
981 atomic_set(&chip->data_pending, 0); 1007 atomic_set(&chip->data_pending, 0);
982 kfree(chip->data_buffer); 1008 kfree(chip->data_buffer);
@@ -1028,7 +1054,7 @@ ssize_t tpm_read(struct file *file, char __user *buf,
1028 ssize_t ret_size; 1054 ssize_t ret_size;
1029 1055
1030 del_singleshot_timer_sync(&chip->user_read_timer); 1056 del_singleshot_timer_sync(&chip->user_read_timer);
1031 flush_scheduled_work(); 1057 flush_work_sync(&chip->work);
1032 ret_size = atomic_read(&chip->data_pending); 1058 ret_size = atomic_read(&chip->data_pending);
1033 atomic_set(&chip->data_pending, 0); 1059 atomic_set(&chip->data_pending, 0);
1034 if (ret_size > 0) { /* relay data */ 1060 if (ret_size > 0) { /* relay data */
@@ -1077,18 +1103,6 @@ static struct tpm_input_header savestate_header = {
1077 .ordinal = TPM_ORD_SAVESTATE 1103 .ordinal = TPM_ORD_SAVESTATE
1078}; 1104};
1079 1105
1080/* Bug workaround - some TPM's don't flush the most
1081 * recently changed pcr on suspend, so force the flush
1082 * with an extend to the selected _unused_ non-volatile pcr.
1083 */
1084static int tpm_suspend_pcr;
1085static int __init tpm_suspend_setup(char *str)
1086{
1087 get_option(&str, &tpm_suspend_pcr);
1088 return 1;
1089}
1090__setup("tpm_suspend_pcr=", tpm_suspend_setup);
1091
1092/* 1106/*
1093 * We are about to suspend. Save the TPM state 1107 * We are about to suspend. Save the TPM state
1094 * so that it can be restored. 1108 * so that it can be restored.
diff --git a/drivers/char/tpm/tpm.h b/drivers/char/tpm/tpm.h
index 792868d24f2a..72ddb031b69a 100644
--- a/drivers/char/tpm/tpm.h
+++ b/drivers/char/tpm/tpm.h
@@ -113,6 +113,11 @@ struct tpm_chip {
113 113
114#define to_tpm_chip(n) container_of(n, struct tpm_chip, vendor) 114#define to_tpm_chip(n) container_of(n, struct tpm_chip, vendor)
115 115
116static inline void tpm_chip_put(struct tpm_chip *chip)
117{
118 module_put(chip->dev->driver->owner);
119}
120
116static inline int tpm_read_index(int base, int index) 121static inline int tpm_read_index(int base, int index)
117{ 122{
118 outb(index, base); 123 outb(index, base);
diff --git a/drivers/char/tpm/tpm_infineon.c b/drivers/char/tpm/tpm_infineon.c
index f58440791e65..76da32e11f18 100644
--- a/drivers/char/tpm/tpm_infineon.c
+++ b/drivers/char/tpm/tpm_infineon.c
@@ -7,7 +7,7 @@
7 * Copyright (C) 2005, Marcel Selhorst <m.selhorst@sirrix.com> 7 * Copyright (C) 2005, Marcel Selhorst <m.selhorst@sirrix.com>
8 * Sirrix AG - security technologies, http://www.sirrix.com and 8 * Sirrix AG - security technologies, http://www.sirrix.com and
9 * Applied Data Security Group, Ruhr-University Bochum, Germany 9 * Applied Data Security Group, Ruhr-University Bochum, Germany
10 * Project-Homepage: http://www.prosec.rub.de/tpm 10 * Project-Homepage: http://www.trust.rub.de/projects/linux-device-driver-infineon-tpm/
11 * 11 *
12 * This program is free software; you can redistribute it and/or 12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License as 13 * modify it under the terms of the GNU General Public License as
diff --git a/drivers/char/tpm/tpm_tis.c b/drivers/char/tpm/tpm_tis.c
index 1030f8420137..dd21df55689d 100644
--- a/drivers/char/tpm/tpm_tis.c
+++ b/drivers/char/tpm/tpm_tis.c
@@ -25,6 +25,7 @@
25#include <linux/slab.h> 25#include <linux/slab.h>
26#include <linux/interrupt.h> 26#include <linux/interrupt.h>
27#include <linux/wait.h> 27#include <linux/wait.h>
28#include <linux/acpi.h>
28#include "tpm.h" 29#include "tpm.h"
29 30
30#define TPM_HEADER_SIZE 10 31#define TPM_HEADER_SIZE 10
@@ -78,6 +79,26 @@ enum tis_defaults {
78static LIST_HEAD(tis_chips); 79static LIST_HEAD(tis_chips);
79static DEFINE_SPINLOCK(tis_lock); 80static DEFINE_SPINLOCK(tis_lock);
80 81
82#ifdef CONFIG_ACPI
83static int is_itpm(struct pnp_dev *dev)
84{
85 struct acpi_device *acpi = pnp_acpi_device(dev);
86 struct acpi_hardware_id *id;
87
88 list_for_each_entry(id, &acpi->pnp.ids, list) {
89 if (!strcmp("INTC0102", id->id))
90 return 1;
91 }
92
93 return 0;
94}
95#else
96static int is_itpm(struct pnp_dev *dev)
97{
98 return 0;
99}
100#endif
101
81static int check_locality(struct tpm_chip *chip, int l) 102static int check_locality(struct tpm_chip *chip, int l)
82{ 103{
83 if ((ioread8(chip->vendor.iobase + TPM_ACCESS(l)) & 104 if ((ioread8(chip->vendor.iobase + TPM_ACCESS(l)) &
@@ -613,6 +634,9 @@ static int __devinit tpm_tis_pnp_init(struct pnp_dev *pnp_dev,
613 else 634 else
614 interrupts = 0; 635 interrupts = 0;
615 636
637 if (is_itpm(pnp_dev))
638 itpm = 1;
639
616 return tpm_tis_init(&pnp_dev->dev, start, len, irq); 640 return tpm_tis_init(&pnp_dev->dev, start, len, irq);
617} 641}
618 642
diff --git a/drivers/char/tty_audit.c b/drivers/char/tty_audit.c
deleted file mode 100644
index 1b8ee590b4ca..000000000000
--- a/drivers/char/tty_audit.c
+++ /dev/null
@@ -1,340 +0,0 @@
1/*
2 * Creating audit events from TTY input.
3 *
4 * Copyright (C) 2007 Red Hat, Inc. All rights reserved. This copyrighted
5 * material is made available to anyone wishing to use, modify, copy, or
6 * redistribute it subject to the terms and conditions of the GNU General
7 * Public License v.2.
8 *
9 * Authors: Miloslav Trmac <mitr@redhat.com>
10 */
11
12#include <linux/audit.h>
13#include <linux/slab.h>
14#include <linux/tty.h>
15
16struct tty_audit_buf {
17 atomic_t count;
18 struct mutex mutex; /* Protects all data below */
19 int major, minor; /* The TTY which the data is from */
20 unsigned icanon:1;
21 size_t valid;
22 unsigned char *data; /* Allocated size N_TTY_BUF_SIZE */
23};
24
25static struct tty_audit_buf *tty_audit_buf_alloc(int major, int minor,
26 int icanon)
27{
28 struct tty_audit_buf *buf;
29
30 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
31 if (!buf)
32 goto err;
33 buf->data = kmalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
34 if (!buf->data)
35 goto err_buf;
36 atomic_set(&buf->count, 1);
37 mutex_init(&buf->mutex);
38 buf->major = major;
39 buf->minor = minor;
40 buf->icanon = icanon;
41 buf->valid = 0;
42 return buf;
43
44err_buf:
45 kfree(buf);
46err:
47 return NULL;
48}
49
50static void tty_audit_buf_free(struct tty_audit_buf *buf)
51{
52 WARN_ON(buf->valid != 0);
53 kfree(buf->data);
54 kfree(buf);
55}
56
57static void tty_audit_buf_put(struct tty_audit_buf *buf)
58{
59 if (atomic_dec_and_test(&buf->count))
60 tty_audit_buf_free(buf);
61}
62
63static void tty_audit_log(const char *description, struct task_struct *tsk,
64 uid_t loginuid, unsigned sessionid, int major,
65 int minor, unsigned char *data, size_t size)
66{
67 struct audit_buffer *ab;
68
69 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_TTY);
70 if (ab) {
71 char name[sizeof(tsk->comm)];
72 uid_t uid = task_uid(tsk);
73
74 audit_log_format(ab, "%s pid=%u uid=%u auid=%u ses=%u "
75 "major=%d minor=%d comm=", description,
76 tsk->pid, uid, loginuid, sessionid,
77 major, minor);
78 get_task_comm(name, tsk);
79 audit_log_untrustedstring(ab, name);
80 audit_log_format(ab, " data=");
81 audit_log_n_hex(ab, data, size);
82 audit_log_end(ab);
83 }
84}
85
86/**
87 * tty_audit_buf_push - Push buffered data out
88 *
89 * Generate an audit message from the contents of @buf, which is owned by
90 * @tsk with @loginuid. @buf->mutex must be locked.
91 */
92static void tty_audit_buf_push(struct task_struct *tsk, uid_t loginuid,
93 unsigned int sessionid,
94 struct tty_audit_buf *buf)
95{
96 if (buf->valid == 0)
97 return;
98 if (audit_enabled == 0)
99 return;
100 tty_audit_log("tty", tsk, loginuid, sessionid, buf->major, buf->minor,
101 buf->data, buf->valid);
102 buf->valid = 0;
103}
104
105/**
106 * tty_audit_buf_push_current - Push buffered data out
107 *
108 * Generate an audit message from the contents of @buf, which is owned by
109 * the current task. @buf->mutex must be locked.
110 */
111static void tty_audit_buf_push_current(struct tty_audit_buf *buf)
112{
113 uid_t auid = audit_get_loginuid(current);
114 unsigned int sessionid = audit_get_sessionid(current);
115 tty_audit_buf_push(current, auid, sessionid, buf);
116}
117
118/**
119 * tty_audit_exit - Handle a task exit
120 *
121 * Make sure all buffered data is written out and deallocate the buffer.
122 * Only needs to be called if current->signal->tty_audit_buf != %NULL.
123 */
124void tty_audit_exit(void)
125{
126 struct tty_audit_buf *buf;
127
128 spin_lock_irq(&current->sighand->siglock);
129 buf = current->signal->tty_audit_buf;
130 current->signal->tty_audit_buf = NULL;
131 spin_unlock_irq(&current->sighand->siglock);
132 if (!buf)
133 return;
134
135 mutex_lock(&buf->mutex);
136 tty_audit_buf_push_current(buf);
137 mutex_unlock(&buf->mutex);
138
139 tty_audit_buf_put(buf);
140}
141
142/**
143 * tty_audit_fork - Copy TTY audit state for a new task
144 *
145 * Set up TTY audit state in @sig from current. @sig needs no locking.
146 */
147void tty_audit_fork(struct signal_struct *sig)
148{
149 spin_lock_irq(&current->sighand->siglock);
150 sig->audit_tty = current->signal->audit_tty;
151 spin_unlock_irq(&current->sighand->siglock);
152}
153
154/**
155 * tty_audit_tiocsti - Log TIOCSTI
156 */
157void tty_audit_tiocsti(struct tty_struct *tty, char ch)
158{
159 struct tty_audit_buf *buf;
160 int major, minor, should_audit;
161
162 spin_lock_irq(&current->sighand->siglock);
163 should_audit = current->signal->audit_tty;
164 buf = current->signal->tty_audit_buf;
165 if (buf)
166 atomic_inc(&buf->count);
167 spin_unlock_irq(&current->sighand->siglock);
168
169 major = tty->driver->major;
170 minor = tty->driver->minor_start + tty->index;
171 if (buf) {
172 mutex_lock(&buf->mutex);
173 if (buf->major == major && buf->minor == minor)
174 tty_audit_buf_push_current(buf);
175 mutex_unlock(&buf->mutex);
176 tty_audit_buf_put(buf);
177 }
178
179 if (should_audit && audit_enabled) {
180 uid_t auid;
181 unsigned int sessionid;
182
183 auid = audit_get_loginuid(current);
184 sessionid = audit_get_sessionid(current);
185 tty_audit_log("ioctl=TIOCSTI", current, auid, sessionid, major,
186 minor, &ch, 1);
187 }
188}
189
190/**
191 * tty_audit_push_task - Flush task's pending audit data
192 */
193void tty_audit_push_task(struct task_struct *tsk, uid_t loginuid, u32 sessionid)
194{
195 struct tty_audit_buf *buf;
196
197 spin_lock_irq(&tsk->sighand->siglock);
198 buf = tsk->signal->tty_audit_buf;
199 if (buf)
200 atomic_inc(&buf->count);
201 spin_unlock_irq(&tsk->sighand->siglock);
202 if (!buf)
203 return;
204
205 mutex_lock(&buf->mutex);
206 tty_audit_buf_push(tsk, loginuid, sessionid, buf);
207 mutex_unlock(&buf->mutex);
208
209 tty_audit_buf_put(buf);
210}
211
212/**
213 * tty_audit_buf_get - Get an audit buffer.
214 *
215 * Get an audit buffer for @tty, allocate it if necessary. Return %NULL
216 * if TTY auditing is disabled or out of memory. Otherwise, return a new
217 * reference to the buffer.
218 */
219static struct tty_audit_buf *tty_audit_buf_get(struct tty_struct *tty)
220{
221 struct tty_audit_buf *buf, *buf2;
222
223 buf = NULL;
224 buf2 = NULL;
225 spin_lock_irq(&current->sighand->siglock);
226 if (likely(!current->signal->audit_tty))
227 goto out;
228 buf = current->signal->tty_audit_buf;
229 if (buf) {
230 atomic_inc(&buf->count);
231 goto out;
232 }
233 spin_unlock_irq(&current->sighand->siglock);
234
235 buf2 = tty_audit_buf_alloc(tty->driver->major,
236 tty->driver->minor_start + tty->index,
237 tty->icanon);
238 if (buf2 == NULL) {
239 audit_log_lost("out of memory in TTY auditing");
240 return NULL;
241 }
242
243 spin_lock_irq(&current->sighand->siglock);
244 if (!current->signal->audit_tty)
245 goto out;
246 buf = current->signal->tty_audit_buf;
247 if (!buf) {
248 current->signal->tty_audit_buf = buf2;
249 buf = buf2;
250 buf2 = NULL;
251 }
252 atomic_inc(&buf->count);
253 /* Fall through */
254 out:
255 spin_unlock_irq(&current->sighand->siglock);
256 if (buf2)
257 tty_audit_buf_free(buf2);
258 return buf;
259}
260
261/**
262 * tty_audit_add_data - Add data for TTY auditing.
263 *
264 * Audit @data of @size from @tty, if necessary.
265 */
266void tty_audit_add_data(struct tty_struct *tty, unsigned char *data,
267 size_t size)
268{
269 struct tty_audit_buf *buf;
270 int major, minor;
271
272 if (unlikely(size == 0))
273 return;
274
275 if (tty->driver->type == TTY_DRIVER_TYPE_PTY
276 && tty->driver->subtype == PTY_TYPE_MASTER)
277 return;
278
279 buf = tty_audit_buf_get(tty);
280 if (!buf)
281 return;
282
283 mutex_lock(&buf->mutex);
284 major = tty->driver->major;
285 minor = tty->driver->minor_start + tty->index;
286 if (buf->major != major || buf->minor != minor
287 || buf->icanon != tty->icanon) {
288 tty_audit_buf_push_current(buf);
289 buf->major = major;
290 buf->minor = minor;
291 buf->icanon = tty->icanon;
292 }
293 do {
294 size_t run;
295
296 run = N_TTY_BUF_SIZE - buf->valid;
297 if (run > size)
298 run = size;
299 memcpy(buf->data + buf->valid, data, run);
300 buf->valid += run;
301 data += run;
302 size -= run;
303 if (buf->valid == N_TTY_BUF_SIZE)
304 tty_audit_buf_push_current(buf);
305 } while (size != 0);
306 mutex_unlock(&buf->mutex);
307 tty_audit_buf_put(buf);
308}
309
310/**
311 * tty_audit_push - Push buffered data out
312 *
313 * Make sure no audit data is pending for @tty on the current process.
314 */
315void tty_audit_push(struct tty_struct *tty)
316{
317 struct tty_audit_buf *buf;
318
319 spin_lock_irq(&current->sighand->siglock);
320 if (likely(!current->signal->audit_tty)) {
321 spin_unlock_irq(&current->sighand->siglock);
322 return;
323 }
324 buf = current->signal->tty_audit_buf;
325 if (buf)
326 atomic_inc(&buf->count);
327 spin_unlock_irq(&current->sighand->siglock);
328
329 if (buf) {
330 int major, minor;
331
332 major = tty->driver->major;
333 minor = tty->driver->minor_start + tty->index;
334 mutex_lock(&buf->mutex);
335 if (buf->major == major && buf->minor == minor)
336 tty_audit_buf_push_current(buf);
337 mutex_unlock(&buf->mutex);
338 tty_audit_buf_put(buf);
339 }
340}
diff --git a/drivers/char/tty_buffer.c b/drivers/char/tty_buffer.c
deleted file mode 100644
index cc1e9850d655..000000000000
--- a/drivers/char/tty_buffer.c
+++ /dev/null
@@ -1,524 +0,0 @@
1/*
2 * Tty buffer allocation management
3 */
4
5#include <linux/types.h>
6#include <linux/errno.h>
7#include <linux/tty.h>
8#include <linux/tty_driver.h>
9#include <linux/tty_flip.h>
10#include <linux/timer.h>
11#include <linux/string.h>
12#include <linux/slab.h>
13#include <linux/sched.h>
14#include <linux/init.h>
15#include <linux/wait.h>
16#include <linux/bitops.h>
17#include <linux/delay.h>
18#include <linux/module.h>
19
20/**
21 * tty_buffer_free_all - free buffers used by a tty
22 * @tty: tty to free from
23 *
24 * Remove all the buffers pending on a tty whether queued with data
25 * or in the free ring. Must be called when the tty is no longer in use
26 *
27 * Locking: none
28 */
29
30void tty_buffer_free_all(struct tty_struct *tty)
31{
32 struct tty_buffer *thead;
33 while ((thead = tty->buf.head) != NULL) {
34 tty->buf.head = thead->next;
35 kfree(thead);
36 }
37 while ((thead = tty->buf.free) != NULL) {
38 tty->buf.free = thead->next;
39 kfree(thead);
40 }
41 tty->buf.tail = NULL;
42 tty->buf.memory_used = 0;
43}
44
45/**
46 * tty_buffer_alloc - allocate a tty buffer
47 * @tty: tty device
48 * @size: desired size (characters)
49 *
50 * Allocate a new tty buffer to hold the desired number of characters.
51 * Return NULL if out of memory or the allocation would exceed the
52 * per device queue
53 *
54 * Locking: Caller must hold tty->buf.lock
55 */
56
57static struct tty_buffer *tty_buffer_alloc(struct tty_struct *tty, size_t size)
58{
59 struct tty_buffer *p;
60
61 if (tty->buf.memory_used + size > 65536)
62 return NULL;
63 p = kmalloc(sizeof(struct tty_buffer) + 2 * size, GFP_ATOMIC);
64 if (p == NULL)
65 return NULL;
66 p->used = 0;
67 p->size = size;
68 p->next = NULL;
69 p->commit = 0;
70 p->read = 0;
71 p->char_buf_ptr = (char *)(p->data);
72 p->flag_buf_ptr = (unsigned char *)p->char_buf_ptr + size;
73 tty->buf.memory_used += size;
74 return p;
75}
76
77/**
78 * tty_buffer_free - free a tty buffer
79 * @tty: tty owning the buffer
80 * @b: the buffer to free
81 *
82 * Free a tty buffer, or add it to the free list according to our
83 * internal strategy
84 *
85 * Locking: Caller must hold tty->buf.lock
86 */
87
88static void tty_buffer_free(struct tty_struct *tty, struct tty_buffer *b)
89{
90 /* Dumb strategy for now - should keep some stats */
91 tty->buf.memory_used -= b->size;
92 WARN_ON(tty->buf.memory_used < 0);
93
94 if (b->size >= 512)
95 kfree(b);
96 else {
97 b->next = tty->buf.free;
98 tty->buf.free = b;
99 }
100}
101
102/**
103 * __tty_buffer_flush - flush full tty buffers
104 * @tty: tty to flush
105 *
106 * flush all the buffers containing receive data. Caller must
107 * hold the buffer lock and must have ensured no parallel flush to
108 * ldisc is running.
109 *
110 * Locking: Caller must hold tty->buf.lock
111 */
112
113static void __tty_buffer_flush(struct tty_struct *tty)
114{
115 struct tty_buffer *thead;
116
117 while ((thead = tty->buf.head) != NULL) {
118 tty->buf.head = thead->next;
119 tty_buffer_free(tty, thead);
120 }
121 tty->buf.tail = NULL;
122}
123
124/**
125 * tty_buffer_flush - flush full tty buffers
126 * @tty: tty to flush
127 *
128 * flush all the buffers containing receive data. If the buffer is
129 * being processed by flush_to_ldisc then we defer the processing
130 * to that function
131 *
132 * Locking: none
133 */
134
135void tty_buffer_flush(struct tty_struct *tty)
136{
137 unsigned long flags;
138 spin_lock_irqsave(&tty->buf.lock, flags);
139
140 /* If the data is being pushed to the tty layer then we can't
141 process it here. Instead set a flag and the flush_to_ldisc
142 path will process the flush request before it exits */
143 if (test_bit(TTY_FLUSHING, &tty->flags)) {
144 set_bit(TTY_FLUSHPENDING, &tty->flags);
145 spin_unlock_irqrestore(&tty->buf.lock, flags);
146 wait_event(tty->read_wait,
147 test_bit(TTY_FLUSHPENDING, &tty->flags) == 0);
148 return;
149 } else
150 __tty_buffer_flush(tty);
151 spin_unlock_irqrestore(&tty->buf.lock, flags);
152}
153
154/**
155 * tty_buffer_find - find a free tty buffer
156 * @tty: tty owning the buffer
157 * @size: characters wanted
158 *
159 * Locate an existing suitable tty buffer or if we are lacking one then
160 * allocate a new one. We round our buffers off in 256 character chunks
161 * to get better allocation behaviour.
162 *
163 * Locking: Caller must hold tty->buf.lock
164 */
165
166static struct tty_buffer *tty_buffer_find(struct tty_struct *tty, size_t size)
167{
168 struct tty_buffer **tbh = &tty->buf.free;
169 while ((*tbh) != NULL) {
170 struct tty_buffer *t = *tbh;
171 if (t->size >= size) {
172 *tbh = t->next;
173 t->next = NULL;
174 t->used = 0;
175 t->commit = 0;
176 t->read = 0;
177 tty->buf.memory_used += t->size;
178 return t;
179 }
180 tbh = &((*tbh)->next);
181 }
182 /* Round the buffer size out */
183 size = (size + 0xFF) & ~0xFF;
184 return tty_buffer_alloc(tty, size);
185 /* Should possibly check if this fails for the largest buffer we
186 have queued and recycle that ? */
187}
188
189/**
190 * tty_buffer_request_room - grow tty buffer if needed
191 * @tty: tty structure
192 * @size: size desired
193 *
194 * Make at least size bytes of linear space available for the tty
195 * buffer. If we fail return the size we managed to find.
196 *
197 * Locking: Takes tty->buf.lock
198 */
199int tty_buffer_request_room(struct tty_struct *tty, size_t size)
200{
201 struct tty_buffer *b, *n;
202 int left;
203 unsigned long flags;
204
205 spin_lock_irqsave(&tty->buf.lock, flags);
206
207 /* OPTIMISATION: We could keep a per tty "zero" sized buffer to
208 remove this conditional if its worth it. This would be invisible
209 to the callers */
210 if ((b = tty->buf.tail) != NULL)
211 left = b->size - b->used;
212 else
213 left = 0;
214
215 if (left < size) {
216 /* This is the slow path - looking for new buffers to use */
217 if ((n = tty_buffer_find(tty, size)) != NULL) {
218 if (b != NULL) {
219 b->next = n;
220 b->commit = b->used;
221 } else
222 tty->buf.head = n;
223 tty->buf.tail = n;
224 } else
225 size = left;
226 }
227
228 spin_unlock_irqrestore(&tty->buf.lock, flags);
229 return size;
230}
231EXPORT_SYMBOL_GPL(tty_buffer_request_room);
232
233/**
234 * tty_insert_flip_string_fixed_flag - Add characters to the tty buffer
235 * @tty: tty structure
236 * @chars: characters
237 * @flag: flag value for each character
238 * @size: size
239 *
240 * Queue a series of bytes to the tty buffering. All the characters
241 * passed are marked with the supplied flag. Returns the number added.
242 *
243 * Locking: Called functions may take tty->buf.lock
244 */
245
246int tty_insert_flip_string_fixed_flag(struct tty_struct *tty,
247 const unsigned char *chars, char flag, size_t size)
248{
249 int copied = 0;
250 do {
251 int goal = min_t(size_t, size - copied, TTY_BUFFER_PAGE);
252 int space = tty_buffer_request_room(tty, goal);
253 struct tty_buffer *tb = tty->buf.tail;
254 /* If there is no space then tb may be NULL */
255 if (unlikely(space == 0))
256 break;
257 memcpy(tb->char_buf_ptr + tb->used, chars, space);
258 memset(tb->flag_buf_ptr + tb->used, flag, space);
259 tb->used += space;
260 copied += space;
261 chars += space;
262 /* There is a small chance that we need to split the data over
263 several buffers. If this is the case we must loop */
264 } while (unlikely(size > copied));
265 return copied;
266}
267EXPORT_SYMBOL(tty_insert_flip_string_fixed_flag);
268
269/**
270 * tty_insert_flip_string_flags - Add characters to the tty buffer
271 * @tty: tty structure
272 * @chars: characters
273 * @flags: flag bytes
274 * @size: size
275 *
276 * Queue a series of bytes to the tty buffering. For each character
277 * the flags array indicates the status of the character. Returns the
278 * number added.
279 *
280 * Locking: Called functions may take tty->buf.lock
281 */
282
283int tty_insert_flip_string_flags(struct tty_struct *tty,
284 const unsigned char *chars, const char *flags, size_t size)
285{
286 int copied = 0;
287 do {
288 int goal = min_t(size_t, size - copied, TTY_BUFFER_PAGE);
289 int space = tty_buffer_request_room(tty, goal);
290 struct tty_buffer *tb = tty->buf.tail;
291 /* If there is no space then tb may be NULL */
292 if (unlikely(space == 0))
293 break;
294 memcpy(tb->char_buf_ptr + tb->used, chars, space);
295 memcpy(tb->flag_buf_ptr + tb->used, flags, space);
296 tb->used += space;
297 copied += space;
298 chars += space;
299 flags += space;
300 /* There is a small chance that we need to split the data over
301 several buffers. If this is the case we must loop */
302 } while (unlikely(size > copied));
303 return copied;
304}
305EXPORT_SYMBOL(tty_insert_flip_string_flags);
306
307/**
308 * tty_schedule_flip - push characters to ldisc
309 * @tty: tty to push from
310 *
311 * Takes any pending buffers and transfers their ownership to the
312 * ldisc side of the queue. It then schedules those characters for
313 * processing by the line discipline.
314 *
315 * Locking: Takes tty->buf.lock
316 */
317
318void tty_schedule_flip(struct tty_struct *tty)
319{
320 unsigned long flags;
321 spin_lock_irqsave(&tty->buf.lock, flags);
322 if (tty->buf.tail != NULL)
323 tty->buf.tail->commit = tty->buf.tail->used;
324 spin_unlock_irqrestore(&tty->buf.lock, flags);
325 schedule_delayed_work(&tty->buf.work, 1);
326}
327EXPORT_SYMBOL(tty_schedule_flip);
328
329/**
330 * tty_prepare_flip_string - make room for characters
331 * @tty: tty
332 * @chars: return pointer for character write area
333 * @size: desired size
334 *
335 * Prepare a block of space in the buffer for data. Returns the length
336 * available and buffer pointer to the space which is now allocated and
337 * accounted for as ready for normal characters. This is used for drivers
338 * that need their own block copy routines into the buffer. There is no
339 * guarantee the buffer is a DMA target!
340 *
341 * Locking: May call functions taking tty->buf.lock
342 */
343
344int tty_prepare_flip_string(struct tty_struct *tty, unsigned char **chars,
345 size_t size)
346{
347 int space = tty_buffer_request_room(tty, size);
348 if (likely(space)) {
349 struct tty_buffer *tb = tty->buf.tail;
350 *chars = tb->char_buf_ptr + tb->used;
351 memset(tb->flag_buf_ptr + tb->used, TTY_NORMAL, space);
352 tb->used += space;
353 }
354 return space;
355}
356EXPORT_SYMBOL_GPL(tty_prepare_flip_string);
357
358/**
359 * tty_prepare_flip_string_flags - make room for characters
360 * @tty: tty
361 * @chars: return pointer for character write area
362 * @flags: return pointer for status flag write area
363 * @size: desired size
364 *
365 * Prepare a block of space in the buffer for data. Returns the length
366 * available and buffer pointer to the space which is now allocated and
367 * accounted for as ready for characters. This is used for drivers
368 * that need their own block copy routines into the buffer. There is no
369 * guarantee the buffer is a DMA target!
370 *
371 * Locking: May call functions taking tty->buf.lock
372 */
373
374int tty_prepare_flip_string_flags(struct tty_struct *tty,
375 unsigned char **chars, char **flags, size_t size)
376{
377 int space = tty_buffer_request_room(tty, size);
378 if (likely(space)) {
379 struct tty_buffer *tb = tty->buf.tail;
380 *chars = tb->char_buf_ptr + tb->used;
381 *flags = tb->flag_buf_ptr + tb->used;
382 tb->used += space;
383 }
384 return space;
385}
386EXPORT_SYMBOL_GPL(tty_prepare_flip_string_flags);
387
388
389
390/**
391 * flush_to_ldisc
392 * @work: tty structure passed from work queue.
393 *
394 * This routine is called out of the software interrupt to flush data
395 * from the buffer chain to the line discipline.
396 *
397 * Locking: holds tty->buf.lock to guard buffer list. Drops the lock
398 * while invoking the line discipline receive_buf method. The
399 * receive_buf method is single threaded for each tty instance.
400 */
401
402static void flush_to_ldisc(struct work_struct *work)
403{
404 struct tty_struct *tty =
405 container_of(work, struct tty_struct, buf.work.work);
406 unsigned long flags;
407 struct tty_ldisc *disc;
408
409 disc = tty_ldisc_ref(tty);
410 if (disc == NULL) /* !TTY_LDISC */
411 return;
412
413 spin_lock_irqsave(&tty->buf.lock, flags);
414
415 if (!test_and_set_bit(TTY_FLUSHING, &tty->flags)) {
416 struct tty_buffer *head;
417 while ((head = tty->buf.head) != NULL) {
418 int count;
419 char *char_buf;
420 unsigned char *flag_buf;
421
422 count = head->commit - head->read;
423 if (!count) {
424 if (head->next == NULL)
425 break;
426 tty->buf.head = head->next;
427 tty_buffer_free(tty, head);
428 continue;
429 }
430 /* Ldisc or user is trying to flush the buffers
431 we are feeding to the ldisc, stop feeding the
432 line discipline as we want to empty the queue */
433 if (test_bit(TTY_FLUSHPENDING, &tty->flags))
434 break;
435 if (!tty->receive_room) {
436 schedule_delayed_work(&tty->buf.work, 1);
437 break;
438 }
439 if (count > tty->receive_room)
440 count = tty->receive_room;
441 char_buf = head->char_buf_ptr + head->read;
442 flag_buf = head->flag_buf_ptr + head->read;
443 head->read += count;
444 spin_unlock_irqrestore(&tty->buf.lock, flags);
445 disc->ops->receive_buf(tty, char_buf,
446 flag_buf, count);
447 spin_lock_irqsave(&tty->buf.lock, flags);
448 }
449 clear_bit(TTY_FLUSHING, &tty->flags);
450 }
451
452 /* We may have a deferred request to flush the input buffer,
453 if so pull the chain under the lock and empty the queue */
454 if (test_bit(TTY_FLUSHPENDING, &tty->flags)) {
455 __tty_buffer_flush(tty);
456 clear_bit(TTY_FLUSHPENDING, &tty->flags);
457 wake_up(&tty->read_wait);
458 }
459 spin_unlock_irqrestore(&tty->buf.lock, flags);
460
461 tty_ldisc_deref(disc);
462}
463
464/**
465 * tty_flush_to_ldisc
466 * @tty: tty to push
467 *
468 * Push the terminal flip buffers to the line discipline.
469 *
470 * Must not be called from IRQ context.
471 */
472void tty_flush_to_ldisc(struct tty_struct *tty)
473{
474 flush_delayed_work(&tty->buf.work);
475}
476
477/**
478 * tty_flip_buffer_push - terminal
479 * @tty: tty to push
480 *
481 * Queue a push of the terminal flip buffers to the line discipline. This
482 * function must not be called from IRQ context if tty->low_latency is set.
483 *
484 * In the event of the queue being busy for flipping the work will be
485 * held off and retried later.
486 *
487 * Locking: tty buffer lock. Driver locks in low latency mode.
488 */
489
490void tty_flip_buffer_push(struct tty_struct *tty)
491{
492 unsigned long flags;
493 spin_lock_irqsave(&tty->buf.lock, flags);
494 if (tty->buf.tail != NULL)
495 tty->buf.tail->commit = tty->buf.tail->used;
496 spin_unlock_irqrestore(&tty->buf.lock, flags);
497
498 if (tty->low_latency)
499 flush_to_ldisc(&tty->buf.work.work);
500 else
501 schedule_delayed_work(&tty->buf.work, 1);
502}
503EXPORT_SYMBOL(tty_flip_buffer_push);
504
505/**
506 * tty_buffer_init - prepare a tty buffer structure
507 * @tty: tty to initialise
508 *
509 * Set up the initial state of the buffer management for a tty device.
510 * Must be called before the other tty buffer functions are used.
511 *
512 * Locking: none
513 */
514
515void tty_buffer_init(struct tty_struct *tty)
516{
517 spin_lock_init(&tty->buf.lock);
518 tty->buf.head = NULL;
519 tty->buf.tail = NULL;
520 tty->buf.free = NULL;
521 tty->buf.memory_used = 0;
522 INIT_DELAYED_WORK(&tty->buf.work, flush_to_ldisc);
523}
524
diff --git a/drivers/char/tty_io.c b/drivers/char/tty_io.c
deleted file mode 100644
index 613c852ee0fe..000000000000
--- a/drivers/char/tty_io.c
+++ /dev/null
@@ -1,3198 +0,0 @@
1/*
2 * linux/drivers/char/tty_io.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
6
7/*
8 * 'tty_io.c' gives an orthogonal feeling to tty's, be they consoles
9 * or rs-channels. It also implements echoing, cooked mode etc.
10 *
11 * Kill-line thanks to John T Kohl, who also corrected VMIN = VTIME = 0.
12 *
13 * Modified by Theodore Ts'o, 9/14/92, to dynamically allocate the
14 * tty_struct and tty_queue structures. Previously there was an array
15 * of 256 tty_struct's which was statically allocated, and the
16 * tty_queue structures were allocated at boot time. Both are now
17 * dynamically allocated only when the tty is open.
18 *
19 * Also restructured routines so that there is more of a separation
20 * between the high-level tty routines (tty_io.c and tty_ioctl.c) and
21 * the low-level tty routines (serial.c, pty.c, console.c). This
22 * makes for cleaner and more compact code. -TYT, 9/17/92
23 *
24 * Modified by Fred N. van Kempen, 01/29/93, to add line disciplines
25 * which can be dynamically activated and de-activated by the line
26 * discipline handling modules (like SLIP).
27 *
28 * NOTE: pay no attention to the line discipline code (yet); its
29 * interface is still subject to change in this version...
30 * -- TYT, 1/31/92
31 *
32 * Added functionality to the OPOST tty handling. No delays, but all
33 * other bits should be there.
34 * -- Nick Holloway <alfie@dcs.warwick.ac.uk>, 27th May 1993.
35 *
36 * Rewrote canonical mode and added more termios flags.
37 * -- julian@uhunix.uhcc.hawaii.edu (J. Cowley), 13Jan94
38 *
39 * Reorganized FASYNC support so mouse code can share it.
40 * -- ctm@ardi.com, 9Sep95
41 *
42 * New TIOCLINUX variants added.
43 * -- mj@k332.feld.cvut.cz, 19-Nov-95
44 *
45 * Restrict vt switching via ioctl()
46 * -- grif@cs.ucr.edu, 5-Dec-95
47 *
48 * Move console and virtual terminal code to more appropriate files,
49 * implement CONFIG_VT and generalize console device interface.
50 * -- Marko Kohtala <Marko.Kohtala@hut.fi>, March 97
51 *
52 * Rewrote tty_init_dev and tty_release_dev to eliminate races.
53 * -- Bill Hawes <whawes@star.net>, June 97
54 *
55 * Added devfs support.
56 * -- C. Scott Ananian <cananian@alumni.princeton.edu>, 13-Jan-1998
57 *
58 * Added support for a Unix98-style ptmx device.
59 * -- C. Scott Ananian <cananian@alumni.princeton.edu>, 14-Jan-1998
60 *
61 * Reduced memory usage for older ARM systems
62 * -- Russell King <rmk@arm.linux.org.uk>
63 *
64 * Move do_SAK() into process context. Less stack use in devfs functions.
65 * alloc_tty_struct() always uses kmalloc()
66 * -- Andrew Morton <andrewm@uow.edu.eu> 17Mar01
67 */
68
69#include <linux/types.h>
70#include <linux/major.h>
71#include <linux/errno.h>
72#include <linux/signal.h>
73#include <linux/fcntl.h>
74#include <linux/sched.h>
75#include <linux/interrupt.h>
76#include <linux/tty.h>
77#include <linux/tty_driver.h>
78#include <linux/tty_flip.h>
79#include <linux/devpts_fs.h>
80#include <linux/file.h>
81#include <linux/fdtable.h>
82#include <linux/console.h>
83#include <linux/timer.h>
84#include <linux/ctype.h>
85#include <linux/kd.h>
86#include <linux/mm.h>
87#include <linux/string.h>
88#include <linux/slab.h>
89#include <linux/poll.h>
90#include <linux/proc_fs.h>
91#include <linux/init.h>
92#include <linux/module.h>
93#include <linux/smp_lock.h>
94#include <linux/device.h>
95#include <linux/wait.h>
96#include <linux/bitops.h>
97#include <linux/delay.h>
98#include <linux/seq_file.h>
99
100#include <linux/uaccess.h>
101#include <asm/system.h>
102
103#include <linux/kbd_kern.h>
104#include <linux/vt_kern.h>
105#include <linux/selection.h>
106
107#include <linux/kmod.h>
108#include <linux/nsproxy.h>
109
110#undef TTY_DEBUG_HANGUP
111
112#define TTY_PARANOIA_CHECK 1
113#define CHECK_TTY_COUNT 1
114
115struct ktermios tty_std_termios = { /* for the benefit of tty drivers */
116 .c_iflag = ICRNL | IXON,
117 .c_oflag = OPOST | ONLCR,
118 .c_cflag = B38400 | CS8 | CREAD | HUPCL,
119 .c_lflag = ISIG | ICANON | ECHO | ECHOE | ECHOK |
120 ECHOCTL | ECHOKE | IEXTEN,
121 .c_cc = INIT_C_CC,
122 .c_ispeed = 38400,
123 .c_ospeed = 38400
124};
125
126EXPORT_SYMBOL(tty_std_termios);
127
128/* This list gets poked at by procfs and various bits of boot up code. This
129 could do with some rationalisation such as pulling the tty proc function
130 into this file */
131
132LIST_HEAD(tty_drivers); /* linked list of tty drivers */
133
134/* Mutex to protect creating and releasing a tty. This is shared with
135 vt.c for deeply disgusting hack reasons */
136DEFINE_MUTEX(tty_mutex);
137EXPORT_SYMBOL(tty_mutex);
138
139/* Spinlock to protect the tty->tty_files list */
140DEFINE_SPINLOCK(tty_files_lock);
141
142static ssize_t tty_read(struct file *, char __user *, size_t, loff_t *);
143static ssize_t tty_write(struct file *, const char __user *, size_t, loff_t *);
144ssize_t redirected_tty_write(struct file *, const char __user *,
145 size_t, loff_t *);
146static unsigned int tty_poll(struct file *, poll_table *);
147static int tty_open(struct inode *, struct file *);
148long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
149#ifdef CONFIG_COMPAT
150static long tty_compat_ioctl(struct file *file, unsigned int cmd,
151 unsigned long arg);
152#else
153#define tty_compat_ioctl NULL
154#endif
155static int __tty_fasync(int fd, struct file *filp, int on);
156static int tty_fasync(int fd, struct file *filp, int on);
157static void release_tty(struct tty_struct *tty, int idx);
158static void __proc_set_tty(struct task_struct *tsk, struct tty_struct *tty);
159static void proc_set_tty(struct task_struct *tsk, struct tty_struct *tty);
160
161/**
162 * alloc_tty_struct - allocate a tty object
163 *
164 * Return a new empty tty structure. The data fields have not
165 * been initialized in any way but has been zeroed
166 *
167 * Locking: none
168 */
169
170struct tty_struct *alloc_tty_struct(void)
171{
172 return kzalloc(sizeof(struct tty_struct), GFP_KERNEL);
173}
174
175/**
176 * free_tty_struct - free a disused tty
177 * @tty: tty struct to free
178 *
179 * Free the write buffers, tty queue and tty memory itself.
180 *
181 * Locking: none. Must be called after tty is definitely unused
182 */
183
184void free_tty_struct(struct tty_struct *tty)
185{
186 kfree(tty->write_buf);
187 tty_buffer_free_all(tty);
188 kfree(tty);
189}
190
191static inline struct tty_struct *file_tty(struct file *file)
192{
193 return ((struct tty_file_private *)file->private_data)->tty;
194}
195
196/* Associate a new file with the tty structure */
197void tty_add_file(struct tty_struct *tty, struct file *file)
198{
199 struct tty_file_private *priv;
200
201 /* XXX: must implement proper error handling in callers */
202 priv = kmalloc(sizeof(*priv), GFP_KERNEL|__GFP_NOFAIL);
203
204 priv->tty = tty;
205 priv->file = file;
206 file->private_data = priv;
207
208 spin_lock(&tty_files_lock);
209 list_add(&priv->list, &tty->tty_files);
210 spin_unlock(&tty_files_lock);
211}
212
213/* Delete file from its tty */
214void tty_del_file(struct file *file)
215{
216 struct tty_file_private *priv = file->private_data;
217
218 spin_lock(&tty_files_lock);
219 list_del(&priv->list);
220 spin_unlock(&tty_files_lock);
221 file->private_data = NULL;
222 kfree(priv);
223}
224
225
226#define TTY_NUMBER(tty) ((tty)->index + (tty)->driver->name_base)
227
228/**
229 * tty_name - return tty naming
230 * @tty: tty structure
231 * @buf: buffer for output
232 *
233 * Convert a tty structure into a name. The name reflects the kernel
234 * naming policy and if udev is in use may not reflect user space
235 *
236 * Locking: none
237 */
238
239char *tty_name(struct tty_struct *tty, char *buf)
240{
241 if (!tty) /* Hmm. NULL pointer. That's fun. */
242 strcpy(buf, "NULL tty");
243 else
244 strcpy(buf, tty->name);
245 return buf;
246}
247
248EXPORT_SYMBOL(tty_name);
249
250int tty_paranoia_check(struct tty_struct *tty, struct inode *inode,
251 const char *routine)
252{
253#ifdef TTY_PARANOIA_CHECK
254 if (!tty) {
255 printk(KERN_WARNING
256 "null TTY for (%d:%d) in %s\n",
257 imajor(inode), iminor(inode), routine);
258 return 1;
259 }
260 if (tty->magic != TTY_MAGIC) {
261 printk(KERN_WARNING
262 "bad magic number for tty struct (%d:%d) in %s\n",
263 imajor(inode), iminor(inode), routine);
264 return 1;
265 }
266#endif
267 return 0;
268}
269
270static int check_tty_count(struct tty_struct *tty, const char *routine)
271{
272#ifdef CHECK_TTY_COUNT
273 struct list_head *p;
274 int count = 0;
275
276 spin_lock(&tty_files_lock);
277 list_for_each(p, &tty->tty_files) {
278 count++;
279 }
280 spin_unlock(&tty_files_lock);
281 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
282 tty->driver->subtype == PTY_TYPE_SLAVE &&
283 tty->link && tty->link->count)
284 count++;
285 if (tty->count != count) {
286 printk(KERN_WARNING "Warning: dev (%s) tty->count(%d) "
287 "!= #fd's(%d) in %s\n",
288 tty->name, tty->count, count, routine);
289 return count;
290 }
291#endif
292 return 0;
293}
294
295/**
296 * get_tty_driver - find device of a tty
297 * @dev_t: device identifier
298 * @index: returns the index of the tty
299 *
300 * This routine returns a tty driver structure, given a device number
301 * and also passes back the index number.
302 *
303 * Locking: caller must hold tty_mutex
304 */
305
306static struct tty_driver *get_tty_driver(dev_t device, int *index)
307{
308 struct tty_driver *p;
309
310 list_for_each_entry(p, &tty_drivers, tty_drivers) {
311 dev_t base = MKDEV(p->major, p->minor_start);
312 if (device < base || device >= base + p->num)
313 continue;
314 *index = device - base;
315 return tty_driver_kref_get(p);
316 }
317 return NULL;
318}
319
320#ifdef CONFIG_CONSOLE_POLL
321
322/**
323 * tty_find_polling_driver - find device of a polled tty
324 * @name: name string to match
325 * @line: pointer to resulting tty line nr
326 *
327 * This routine returns a tty driver structure, given a name
328 * and the condition that the tty driver is capable of polled
329 * operation.
330 */
331struct tty_driver *tty_find_polling_driver(char *name, int *line)
332{
333 struct tty_driver *p, *res = NULL;
334 int tty_line = 0;
335 int len;
336 char *str, *stp;
337
338 for (str = name; *str; str++)
339 if ((*str >= '0' && *str <= '9') || *str == ',')
340 break;
341 if (!*str)
342 return NULL;
343
344 len = str - name;
345 tty_line = simple_strtoul(str, &str, 10);
346
347 mutex_lock(&tty_mutex);
348 /* Search through the tty devices to look for a match */
349 list_for_each_entry(p, &tty_drivers, tty_drivers) {
350 if (strncmp(name, p->name, len) != 0)
351 continue;
352 stp = str;
353 if (*stp == ',')
354 stp++;
355 if (*stp == '\0')
356 stp = NULL;
357
358 if (tty_line >= 0 && tty_line < p->num && p->ops &&
359 p->ops->poll_init && !p->ops->poll_init(p, tty_line, stp)) {
360 res = tty_driver_kref_get(p);
361 *line = tty_line;
362 break;
363 }
364 }
365 mutex_unlock(&tty_mutex);
366
367 return res;
368}
369EXPORT_SYMBOL_GPL(tty_find_polling_driver);
370#endif
371
372/**
373 * tty_check_change - check for POSIX terminal changes
374 * @tty: tty to check
375 *
376 * If we try to write to, or set the state of, a terminal and we're
377 * not in the foreground, send a SIGTTOU. If the signal is blocked or
378 * ignored, go ahead and perform the operation. (POSIX 7.2)
379 *
380 * Locking: ctrl_lock
381 */
382
383int tty_check_change(struct tty_struct *tty)
384{
385 unsigned long flags;
386 int ret = 0;
387
388 if (current->signal->tty != tty)
389 return 0;
390
391 spin_lock_irqsave(&tty->ctrl_lock, flags);
392
393 if (!tty->pgrp) {
394 printk(KERN_WARNING "tty_check_change: tty->pgrp == NULL!\n");
395 goto out_unlock;
396 }
397 if (task_pgrp(current) == tty->pgrp)
398 goto out_unlock;
399 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
400 if (is_ignored(SIGTTOU))
401 goto out;
402 if (is_current_pgrp_orphaned()) {
403 ret = -EIO;
404 goto out;
405 }
406 kill_pgrp(task_pgrp(current), SIGTTOU, 1);
407 set_thread_flag(TIF_SIGPENDING);
408 ret = -ERESTARTSYS;
409out:
410 return ret;
411out_unlock:
412 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
413 return ret;
414}
415
416EXPORT_SYMBOL(tty_check_change);
417
418static ssize_t hung_up_tty_read(struct file *file, char __user *buf,
419 size_t count, loff_t *ppos)
420{
421 return 0;
422}
423
424static ssize_t hung_up_tty_write(struct file *file, const char __user *buf,
425 size_t count, loff_t *ppos)
426{
427 return -EIO;
428}
429
430/* No kernel lock held - none needed ;) */
431static unsigned int hung_up_tty_poll(struct file *filp, poll_table *wait)
432{
433 return POLLIN | POLLOUT | POLLERR | POLLHUP | POLLRDNORM | POLLWRNORM;
434}
435
436static long hung_up_tty_ioctl(struct file *file, unsigned int cmd,
437 unsigned long arg)
438{
439 return cmd == TIOCSPGRP ? -ENOTTY : -EIO;
440}
441
442static long hung_up_tty_compat_ioctl(struct file *file,
443 unsigned int cmd, unsigned long arg)
444{
445 return cmd == TIOCSPGRP ? -ENOTTY : -EIO;
446}
447
448static const struct file_operations tty_fops = {
449 .llseek = no_llseek,
450 .read = tty_read,
451 .write = tty_write,
452 .poll = tty_poll,
453 .unlocked_ioctl = tty_ioctl,
454 .compat_ioctl = tty_compat_ioctl,
455 .open = tty_open,
456 .release = tty_release,
457 .fasync = tty_fasync,
458};
459
460static const struct file_operations console_fops = {
461 .llseek = no_llseek,
462 .read = tty_read,
463 .write = redirected_tty_write,
464 .poll = tty_poll,
465 .unlocked_ioctl = tty_ioctl,
466 .compat_ioctl = tty_compat_ioctl,
467 .open = tty_open,
468 .release = tty_release,
469 .fasync = tty_fasync,
470};
471
472static const struct file_operations hung_up_tty_fops = {
473 .llseek = no_llseek,
474 .read = hung_up_tty_read,
475 .write = hung_up_tty_write,
476 .poll = hung_up_tty_poll,
477 .unlocked_ioctl = hung_up_tty_ioctl,
478 .compat_ioctl = hung_up_tty_compat_ioctl,
479 .release = tty_release,
480};
481
482static DEFINE_SPINLOCK(redirect_lock);
483static struct file *redirect;
484
485/**
486 * tty_wakeup - request more data
487 * @tty: terminal
488 *
489 * Internal and external helper for wakeups of tty. This function
490 * informs the line discipline if present that the driver is ready
491 * to receive more output data.
492 */
493
494void tty_wakeup(struct tty_struct *tty)
495{
496 struct tty_ldisc *ld;
497
498 if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) {
499 ld = tty_ldisc_ref(tty);
500 if (ld) {
501 if (ld->ops->write_wakeup)
502 ld->ops->write_wakeup(tty);
503 tty_ldisc_deref(ld);
504 }
505 }
506 wake_up_interruptible_poll(&tty->write_wait, POLLOUT);
507}
508
509EXPORT_SYMBOL_GPL(tty_wakeup);
510
511/**
512 * __tty_hangup - actual handler for hangup events
513 * @work: tty device
514 *
515 * This can be called by the "eventd" kernel thread. That is process
516 * synchronous but doesn't hold any locks, so we need to make sure we
517 * have the appropriate locks for what we're doing.
518 *
519 * The hangup event clears any pending redirections onto the hung up
520 * device. It ensures future writes will error and it does the needed
521 * line discipline hangup and signal delivery. The tty object itself
522 * remains intact.
523 *
524 * Locking:
525 * BTM
526 * redirect lock for undoing redirection
527 * file list lock for manipulating list of ttys
528 * tty_ldisc_lock from called functions
529 * termios_mutex resetting termios data
530 * tasklist_lock to walk task list for hangup event
531 * ->siglock to protect ->signal/->sighand
532 */
533void __tty_hangup(struct tty_struct *tty)
534{
535 struct file *cons_filp = NULL;
536 struct file *filp, *f = NULL;
537 struct task_struct *p;
538 struct tty_file_private *priv;
539 int closecount = 0, n;
540 unsigned long flags;
541 int refs = 0;
542
543 if (!tty)
544 return;
545
546
547 spin_lock(&redirect_lock);
548 if (redirect && file_tty(redirect) == tty) {
549 f = redirect;
550 redirect = NULL;
551 }
552 spin_unlock(&redirect_lock);
553
554 tty_lock();
555
556 /* inuse_filps is protected by the single tty lock,
557 this really needs to change if we want to flush the
558 workqueue with the lock held */
559 check_tty_count(tty, "tty_hangup");
560
561 spin_lock(&tty_files_lock);
562 /* This breaks for file handles being sent over AF_UNIX sockets ? */
563 list_for_each_entry(priv, &tty->tty_files, list) {
564 filp = priv->file;
565 if (filp->f_op->write == redirected_tty_write)
566 cons_filp = filp;
567 if (filp->f_op->write != tty_write)
568 continue;
569 closecount++;
570 __tty_fasync(-1, filp, 0); /* can't block */
571 filp->f_op = &hung_up_tty_fops;
572 }
573 spin_unlock(&tty_files_lock);
574
575 tty_ldisc_hangup(tty);
576
577 read_lock(&tasklist_lock);
578 if (tty->session) {
579 do_each_pid_task(tty->session, PIDTYPE_SID, p) {
580 spin_lock_irq(&p->sighand->siglock);
581 if (p->signal->tty == tty) {
582 p->signal->tty = NULL;
583 /* We defer the dereferences outside fo
584 the tasklist lock */
585 refs++;
586 }
587 if (!p->signal->leader) {
588 spin_unlock_irq(&p->sighand->siglock);
589 continue;
590 }
591 __group_send_sig_info(SIGHUP, SEND_SIG_PRIV, p);
592 __group_send_sig_info(SIGCONT, SEND_SIG_PRIV, p);
593 put_pid(p->signal->tty_old_pgrp); /* A noop */
594 spin_lock_irqsave(&tty->ctrl_lock, flags);
595 if (tty->pgrp)
596 p->signal->tty_old_pgrp = get_pid(tty->pgrp);
597 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
598 spin_unlock_irq(&p->sighand->siglock);
599 } while_each_pid_task(tty->session, PIDTYPE_SID, p);
600 }
601 read_unlock(&tasklist_lock);
602
603 spin_lock_irqsave(&tty->ctrl_lock, flags);
604 clear_bit(TTY_THROTTLED, &tty->flags);
605 clear_bit(TTY_PUSH, &tty->flags);
606 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
607 put_pid(tty->session);
608 put_pid(tty->pgrp);
609 tty->session = NULL;
610 tty->pgrp = NULL;
611 tty->ctrl_status = 0;
612 set_bit(TTY_HUPPED, &tty->flags);
613 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
614
615 /* Account for the p->signal references we killed */
616 while (refs--)
617 tty_kref_put(tty);
618
619 /*
620 * If one of the devices matches a console pointer, we
621 * cannot just call hangup() because that will cause
622 * tty->count and state->count to go out of sync.
623 * So we just call close() the right number of times.
624 */
625 if (cons_filp) {
626 if (tty->ops->close)
627 for (n = 0; n < closecount; n++)
628 tty->ops->close(tty, cons_filp);
629 } else if (tty->ops->hangup)
630 (tty->ops->hangup)(tty);
631 /*
632 * We don't want to have driver/ldisc interactions beyond
633 * the ones we did here. The driver layer expects no
634 * calls after ->hangup() from the ldisc side. However we
635 * can't yet guarantee all that.
636 */
637 set_bit(TTY_HUPPED, &tty->flags);
638 tty_ldisc_enable(tty);
639
640 tty_unlock();
641
642 if (f)
643 fput(f);
644}
645
646static void do_tty_hangup(struct work_struct *work)
647{
648 struct tty_struct *tty =
649 container_of(work, struct tty_struct, hangup_work);
650
651 __tty_hangup(tty);
652}
653
654/**
655 * tty_hangup - trigger a hangup event
656 * @tty: tty to hangup
657 *
658 * A carrier loss (virtual or otherwise) has occurred on this like
659 * schedule a hangup sequence to run after this event.
660 */
661
662void tty_hangup(struct tty_struct *tty)
663{
664#ifdef TTY_DEBUG_HANGUP
665 char buf[64];
666 printk(KERN_DEBUG "%s hangup...\n", tty_name(tty, buf));
667#endif
668 schedule_work(&tty->hangup_work);
669}
670
671EXPORT_SYMBOL(tty_hangup);
672
673/**
674 * tty_vhangup - process vhangup
675 * @tty: tty to hangup
676 *
677 * The user has asked via system call for the terminal to be hung up.
678 * We do this synchronously so that when the syscall returns the process
679 * is complete. That guarantee is necessary for security reasons.
680 */
681
682void tty_vhangup(struct tty_struct *tty)
683{
684#ifdef TTY_DEBUG_HANGUP
685 char buf[64];
686
687 printk(KERN_DEBUG "%s vhangup...\n", tty_name(tty, buf));
688#endif
689 __tty_hangup(tty);
690}
691
692EXPORT_SYMBOL(tty_vhangup);
693
694
695/**
696 * tty_vhangup_self - process vhangup for own ctty
697 *
698 * Perform a vhangup on the current controlling tty
699 */
700
701void tty_vhangup_self(void)
702{
703 struct tty_struct *tty;
704
705 tty = get_current_tty();
706 if (tty) {
707 tty_vhangup(tty);
708 tty_kref_put(tty);
709 }
710}
711
712/**
713 * tty_hung_up_p - was tty hung up
714 * @filp: file pointer of tty
715 *
716 * Return true if the tty has been subject to a vhangup or a carrier
717 * loss
718 */
719
720int tty_hung_up_p(struct file *filp)
721{
722 return (filp->f_op == &hung_up_tty_fops);
723}
724
725EXPORT_SYMBOL(tty_hung_up_p);
726
727static void session_clear_tty(struct pid *session)
728{
729 struct task_struct *p;
730 do_each_pid_task(session, PIDTYPE_SID, p) {
731 proc_clear_tty(p);
732 } while_each_pid_task(session, PIDTYPE_SID, p);
733}
734
735/**
736 * disassociate_ctty - disconnect controlling tty
737 * @on_exit: true if exiting so need to "hang up" the session
738 *
739 * This function is typically called only by the session leader, when
740 * it wants to disassociate itself from its controlling tty.
741 *
742 * It performs the following functions:
743 * (1) Sends a SIGHUP and SIGCONT to the foreground process group
744 * (2) Clears the tty from being controlling the session
745 * (3) Clears the controlling tty for all processes in the
746 * session group.
747 *
748 * The argument on_exit is set to 1 if called when a process is
749 * exiting; it is 0 if called by the ioctl TIOCNOTTY.
750 *
751 * Locking:
752 * BTM is taken for hysterical raisins, and held when
753 * called from no_tty().
754 * tty_mutex is taken to protect tty
755 * ->siglock is taken to protect ->signal/->sighand
756 * tasklist_lock is taken to walk process list for sessions
757 * ->siglock is taken to protect ->signal/->sighand
758 */
759
760void disassociate_ctty(int on_exit)
761{
762 struct tty_struct *tty;
763 struct pid *tty_pgrp = NULL;
764
765 if (!current->signal->leader)
766 return;
767
768 tty = get_current_tty();
769 if (tty) {
770 tty_pgrp = get_pid(tty->pgrp);
771 if (on_exit) {
772 if (tty->driver->type != TTY_DRIVER_TYPE_PTY)
773 tty_vhangup(tty);
774 }
775 tty_kref_put(tty);
776 } else if (on_exit) {
777 struct pid *old_pgrp;
778 spin_lock_irq(&current->sighand->siglock);
779 old_pgrp = current->signal->tty_old_pgrp;
780 current->signal->tty_old_pgrp = NULL;
781 spin_unlock_irq(&current->sighand->siglock);
782 if (old_pgrp) {
783 kill_pgrp(old_pgrp, SIGHUP, on_exit);
784 kill_pgrp(old_pgrp, SIGCONT, on_exit);
785 put_pid(old_pgrp);
786 }
787 return;
788 }
789 if (tty_pgrp) {
790 kill_pgrp(tty_pgrp, SIGHUP, on_exit);
791 if (!on_exit)
792 kill_pgrp(tty_pgrp, SIGCONT, on_exit);
793 put_pid(tty_pgrp);
794 }
795
796 spin_lock_irq(&current->sighand->siglock);
797 put_pid(current->signal->tty_old_pgrp);
798 current->signal->tty_old_pgrp = NULL;
799 spin_unlock_irq(&current->sighand->siglock);
800
801 tty = get_current_tty();
802 if (tty) {
803 unsigned long flags;
804 spin_lock_irqsave(&tty->ctrl_lock, flags);
805 put_pid(tty->session);
806 put_pid(tty->pgrp);
807 tty->session = NULL;
808 tty->pgrp = NULL;
809 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
810 tty_kref_put(tty);
811 } else {
812#ifdef TTY_DEBUG_HANGUP
813 printk(KERN_DEBUG "error attempted to write to tty [0x%p]"
814 " = NULL", tty);
815#endif
816 }
817
818 /* Now clear signal->tty under the lock */
819 read_lock(&tasklist_lock);
820 session_clear_tty(task_session(current));
821 read_unlock(&tasklist_lock);
822}
823
824/**
825 *
826 * no_tty - Ensure the current process does not have a controlling tty
827 */
828void no_tty(void)
829{
830 struct task_struct *tsk = current;
831 tty_lock();
832 disassociate_ctty(0);
833 tty_unlock();
834 proc_clear_tty(tsk);
835}
836
837
838/**
839 * stop_tty - propagate flow control
840 * @tty: tty to stop
841 *
842 * Perform flow control to the driver. For PTY/TTY pairs we
843 * must also propagate the TIOCKPKT status. May be called
844 * on an already stopped device and will not re-call the driver
845 * method.
846 *
847 * This functionality is used by both the line disciplines for
848 * halting incoming flow and by the driver. It may therefore be
849 * called from any context, may be under the tty atomic_write_lock
850 * but not always.
851 *
852 * Locking:
853 * Uses the tty control lock internally
854 */
855
856void stop_tty(struct tty_struct *tty)
857{
858 unsigned long flags;
859 spin_lock_irqsave(&tty->ctrl_lock, flags);
860 if (tty->stopped) {
861 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
862 return;
863 }
864 tty->stopped = 1;
865 if (tty->link && tty->link->packet) {
866 tty->ctrl_status &= ~TIOCPKT_START;
867 tty->ctrl_status |= TIOCPKT_STOP;
868 wake_up_interruptible_poll(&tty->link->read_wait, POLLIN);
869 }
870 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
871 if (tty->ops->stop)
872 (tty->ops->stop)(tty);
873}
874
875EXPORT_SYMBOL(stop_tty);
876
877/**
878 * start_tty - propagate flow control
879 * @tty: tty to start
880 *
881 * Start a tty that has been stopped if at all possible. Perform
882 * any necessary wakeups and propagate the TIOCPKT status. If this
883 * is the tty was previous stopped and is being started then the
884 * driver start method is invoked and the line discipline woken.
885 *
886 * Locking:
887 * ctrl_lock
888 */
889
890void start_tty(struct tty_struct *tty)
891{
892 unsigned long flags;
893 spin_lock_irqsave(&tty->ctrl_lock, flags);
894 if (!tty->stopped || tty->flow_stopped) {
895 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
896 return;
897 }
898 tty->stopped = 0;
899 if (tty->link && tty->link->packet) {
900 tty->ctrl_status &= ~TIOCPKT_STOP;
901 tty->ctrl_status |= TIOCPKT_START;
902 wake_up_interruptible_poll(&tty->link->read_wait, POLLIN);
903 }
904 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
905 if (tty->ops->start)
906 (tty->ops->start)(tty);
907 /* If we have a running line discipline it may need kicking */
908 tty_wakeup(tty);
909}
910
911EXPORT_SYMBOL(start_tty);
912
913/**
914 * tty_read - read method for tty device files
915 * @file: pointer to tty file
916 * @buf: user buffer
917 * @count: size of user buffer
918 * @ppos: unused
919 *
920 * Perform the read system call function on this terminal device. Checks
921 * for hung up devices before calling the line discipline method.
922 *
923 * Locking:
924 * Locks the line discipline internally while needed. Multiple
925 * read calls may be outstanding in parallel.
926 */
927
928static ssize_t tty_read(struct file *file, char __user *buf, size_t count,
929 loff_t *ppos)
930{
931 int i;
932 struct inode *inode = file->f_path.dentry->d_inode;
933 struct tty_struct *tty = file_tty(file);
934 struct tty_ldisc *ld;
935
936 if (tty_paranoia_check(tty, inode, "tty_read"))
937 return -EIO;
938 if (!tty || (test_bit(TTY_IO_ERROR, &tty->flags)))
939 return -EIO;
940
941 /* We want to wait for the line discipline to sort out in this
942 situation */
943 ld = tty_ldisc_ref_wait(tty);
944 if (ld->ops->read)
945 i = (ld->ops->read)(tty, file, buf, count);
946 else
947 i = -EIO;
948 tty_ldisc_deref(ld);
949 if (i > 0)
950 inode->i_atime = current_fs_time(inode->i_sb);
951 return i;
952}
953
954void tty_write_unlock(struct tty_struct *tty)
955{
956 mutex_unlock(&tty->atomic_write_lock);
957 wake_up_interruptible_poll(&tty->write_wait, POLLOUT);
958}
959
960int tty_write_lock(struct tty_struct *tty, int ndelay)
961{
962 if (!mutex_trylock(&tty->atomic_write_lock)) {
963 if (ndelay)
964 return -EAGAIN;
965 if (mutex_lock_interruptible(&tty->atomic_write_lock))
966 return -ERESTARTSYS;
967 }
968 return 0;
969}
970
971/*
972 * Split writes up in sane blocksizes to avoid
973 * denial-of-service type attacks
974 */
975static inline ssize_t do_tty_write(
976 ssize_t (*write)(struct tty_struct *, struct file *, const unsigned char *, size_t),
977 struct tty_struct *tty,
978 struct file *file,
979 const char __user *buf,
980 size_t count)
981{
982 ssize_t ret, written = 0;
983 unsigned int chunk;
984
985 ret = tty_write_lock(tty, file->f_flags & O_NDELAY);
986 if (ret < 0)
987 return ret;
988
989 /*
990 * We chunk up writes into a temporary buffer. This
991 * simplifies low-level drivers immensely, since they
992 * don't have locking issues and user mode accesses.
993 *
994 * But if TTY_NO_WRITE_SPLIT is set, we should use a
995 * big chunk-size..
996 *
997 * The default chunk-size is 2kB, because the NTTY
998 * layer has problems with bigger chunks. It will
999 * claim to be able to handle more characters than
1000 * it actually does.
1001 *
1002 * FIXME: This can probably go away now except that 64K chunks
1003 * are too likely to fail unless switched to vmalloc...
1004 */
1005 chunk = 2048;
1006 if (test_bit(TTY_NO_WRITE_SPLIT, &tty->flags))
1007 chunk = 65536;
1008 if (count < chunk)
1009 chunk = count;
1010
1011 /* write_buf/write_cnt is protected by the atomic_write_lock mutex */
1012 if (tty->write_cnt < chunk) {
1013 unsigned char *buf_chunk;
1014
1015 if (chunk < 1024)
1016 chunk = 1024;
1017
1018 buf_chunk = kmalloc(chunk, GFP_KERNEL);
1019 if (!buf_chunk) {
1020 ret = -ENOMEM;
1021 goto out;
1022 }
1023 kfree(tty->write_buf);
1024 tty->write_cnt = chunk;
1025 tty->write_buf = buf_chunk;
1026 }
1027
1028 /* Do the write .. */
1029 for (;;) {
1030 size_t size = count;
1031 if (size > chunk)
1032 size = chunk;
1033 ret = -EFAULT;
1034 if (copy_from_user(tty->write_buf, buf, size))
1035 break;
1036 ret = write(tty, file, tty->write_buf, size);
1037 if (ret <= 0)
1038 break;
1039 written += ret;
1040 buf += ret;
1041 count -= ret;
1042 if (!count)
1043 break;
1044 ret = -ERESTARTSYS;
1045 if (signal_pending(current))
1046 break;
1047 cond_resched();
1048 }
1049 if (written) {
1050 struct inode *inode = file->f_path.dentry->d_inode;
1051 inode->i_mtime = current_fs_time(inode->i_sb);
1052 ret = written;
1053 }
1054out:
1055 tty_write_unlock(tty);
1056 return ret;
1057}
1058
1059/**
1060 * tty_write_message - write a message to a certain tty, not just the console.
1061 * @tty: the destination tty_struct
1062 * @msg: the message to write
1063 *
1064 * This is used for messages that need to be redirected to a specific tty.
1065 * We don't put it into the syslog queue right now maybe in the future if
1066 * really needed.
1067 *
1068 * We must still hold the BTM and test the CLOSING flag for the moment.
1069 */
1070
1071void tty_write_message(struct tty_struct *tty, char *msg)
1072{
1073 if (tty) {
1074 mutex_lock(&tty->atomic_write_lock);
1075 tty_lock();
1076 if (tty->ops->write && !test_bit(TTY_CLOSING, &tty->flags)) {
1077 tty_unlock();
1078 tty->ops->write(tty, msg, strlen(msg));
1079 } else
1080 tty_unlock();
1081 tty_write_unlock(tty);
1082 }
1083 return;
1084}
1085
1086
1087/**
1088 * tty_write - write method for tty device file
1089 * @file: tty file pointer
1090 * @buf: user data to write
1091 * @count: bytes to write
1092 * @ppos: unused
1093 *
1094 * Write data to a tty device via the line discipline.
1095 *
1096 * Locking:
1097 * Locks the line discipline as required
1098 * Writes to the tty driver are serialized by the atomic_write_lock
1099 * and are then processed in chunks to the device. The line discipline
1100 * write method will not be invoked in parallel for each device.
1101 */
1102
1103static ssize_t tty_write(struct file *file, const char __user *buf,
1104 size_t count, loff_t *ppos)
1105{
1106 struct inode *inode = file->f_path.dentry->d_inode;
1107 struct tty_struct *tty = file_tty(file);
1108 struct tty_ldisc *ld;
1109 ssize_t ret;
1110
1111 if (tty_paranoia_check(tty, inode, "tty_write"))
1112 return -EIO;
1113 if (!tty || !tty->ops->write ||
1114 (test_bit(TTY_IO_ERROR, &tty->flags)))
1115 return -EIO;
1116 /* Short term debug to catch buggy drivers */
1117 if (tty->ops->write_room == NULL)
1118 printk(KERN_ERR "tty driver %s lacks a write_room method.\n",
1119 tty->driver->name);
1120 ld = tty_ldisc_ref_wait(tty);
1121 if (!ld->ops->write)
1122 ret = -EIO;
1123 else
1124 ret = do_tty_write(ld->ops->write, tty, file, buf, count);
1125 tty_ldisc_deref(ld);
1126 return ret;
1127}
1128
1129ssize_t redirected_tty_write(struct file *file, const char __user *buf,
1130 size_t count, loff_t *ppos)
1131{
1132 struct file *p = NULL;
1133
1134 spin_lock(&redirect_lock);
1135 if (redirect) {
1136 get_file(redirect);
1137 p = redirect;
1138 }
1139 spin_unlock(&redirect_lock);
1140
1141 if (p) {
1142 ssize_t res;
1143 res = vfs_write(p, buf, count, &p->f_pos);
1144 fput(p);
1145 return res;
1146 }
1147 return tty_write(file, buf, count, ppos);
1148}
1149
1150static char ptychar[] = "pqrstuvwxyzabcde";
1151
1152/**
1153 * pty_line_name - generate name for a pty
1154 * @driver: the tty driver in use
1155 * @index: the minor number
1156 * @p: output buffer of at least 6 bytes
1157 *
1158 * Generate a name from a driver reference and write it to the output
1159 * buffer.
1160 *
1161 * Locking: None
1162 */
1163static void pty_line_name(struct tty_driver *driver, int index, char *p)
1164{
1165 int i = index + driver->name_base;
1166 /* ->name is initialized to "ttyp", but "tty" is expected */
1167 sprintf(p, "%s%c%x",
1168 driver->subtype == PTY_TYPE_SLAVE ? "tty" : driver->name,
1169 ptychar[i >> 4 & 0xf], i & 0xf);
1170}
1171
1172/**
1173 * tty_line_name - generate name for a tty
1174 * @driver: the tty driver in use
1175 * @index: the minor number
1176 * @p: output buffer of at least 7 bytes
1177 *
1178 * Generate a name from a driver reference and write it to the output
1179 * buffer.
1180 *
1181 * Locking: None
1182 */
1183static void tty_line_name(struct tty_driver *driver, int index, char *p)
1184{
1185 sprintf(p, "%s%d", driver->name, index + driver->name_base);
1186}
1187
1188/**
1189 * tty_driver_lookup_tty() - find an existing tty, if any
1190 * @driver: the driver for the tty
1191 * @idx: the minor number
1192 *
1193 * Return the tty, if found or ERR_PTR() otherwise.
1194 *
1195 * Locking: tty_mutex must be held. If tty is found, the mutex must
1196 * be held until the 'fast-open' is also done. Will change once we
1197 * have refcounting in the driver and per driver locking
1198 */
1199static struct tty_struct *tty_driver_lookup_tty(struct tty_driver *driver,
1200 struct inode *inode, int idx)
1201{
1202 struct tty_struct *tty;
1203
1204 if (driver->ops->lookup)
1205 return driver->ops->lookup(driver, inode, idx);
1206
1207 tty = driver->ttys[idx];
1208 return tty;
1209}
1210
1211/**
1212 * tty_init_termios - helper for termios setup
1213 * @tty: the tty to set up
1214 *
1215 * Initialise the termios structures for this tty. Thus runs under
1216 * the tty_mutex currently so we can be relaxed about ordering.
1217 */
1218
1219int tty_init_termios(struct tty_struct *tty)
1220{
1221 struct ktermios *tp;
1222 int idx = tty->index;
1223
1224 tp = tty->driver->termios[idx];
1225 if (tp == NULL) {
1226 tp = kzalloc(sizeof(struct ktermios[2]), GFP_KERNEL);
1227 if (tp == NULL)
1228 return -ENOMEM;
1229 memcpy(tp, &tty->driver->init_termios,
1230 sizeof(struct ktermios));
1231 tty->driver->termios[idx] = tp;
1232 }
1233 tty->termios = tp;
1234 tty->termios_locked = tp + 1;
1235
1236 /* Compatibility until drivers always set this */
1237 tty->termios->c_ispeed = tty_termios_input_baud_rate(tty->termios);
1238 tty->termios->c_ospeed = tty_termios_baud_rate(tty->termios);
1239 return 0;
1240}
1241EXPORT_SYMBOL_GPL(tty_init_termios);
1242
1243/**
1244 * tty_driver_install_tty() - install a tty entry in the driver
1245 * @driver: the driver for the tty
1246 * @tty: the tty
1247 *
1248 * Install a tty object into the driver tables. The tty->index field
1249 * will be set by the time this is called. This method is responsible
1250 * for ensuring any need additional structures are allocated and
1251 * configured.
1252 *
1253 * Locking: tty_mutex for now
1254 */
1255static int tty_driver_install_tty(struct tty_driver *driver,
1256 struct tty_struct *tty)
1257{
1258 int idx = tty->index;
1259 int ret;
1260
1261 if (driver->ops->install) {
1262 ret = driver->ops->install(driver, tty);
1263 return ret;
1264 }
1265
1266 if (tty_init_termios(tty) == 0) {
1267 tty_driver_kref_get(driver);
1268 tty->count++;
1269 driver->ttys[idx] = tty;
1270 return 0;
1271 }
1272 return -ENOMEM;
1273}
1274
1275/**
1276 * tty_driver_remove_tty() - remove a tty from the driver tables
1277 * @driver: the driver for the tty
1278 * @idx: the minor number
1279 *
1280 * Remvoe a tty object from the driver tables. The tty->index field
1281 * will be set by the time this is called.
1282 *
1283 * Locking: tty_mutex for now
1284 */
1285static void tty_driver_remove_tty(struct tty_driver *driver,
1286 struct tty_struct *tty)
1287{
1288 if (driver->ops->remove)
1289 driver->ops->remove(driver, tty);
1290 else
1291 driver->ttys[tty->index] = NULL;
1292}
1293
1294/*
1295 * tty_reopen() - fast re-open of an open tty
1296 * @tty - the tty to open
1297 *
1298 * Return 0 on success, -errno on error.
1299 *
1300 * Locking: tty_mutex must be held from the time the tty was found
1301 * till this open completes.
1302 */
1303static int tty_reopen(struct tty_struct *tty)
1304{
1305 struct tty_driver *driver = tty->driver;
1306
1307 if (test_bit(TTY_CLOSING, &tty->flags))
1308 return -EIO;
1309
1310 if (driver->type == TTY_DRIVER_TYPE_PTY &&
1311 driver->subtype == PTY_TYPE_MASTER) {
1312 /*
1313 * special case for PTY masters: only one open permitted,
1314 * and the slave side open count is incremented as well.
1315 */
1316 if (tty->count)
1317 return -EIO;
1318
1319 tty->link->count++;
1320 }
1321 tty->count++;
1322 tty->driver = driver; /* N.B. why do this every time?? */
1323
1324 mutex_lock(&tty->ldisc_mutex);
1325 WARN_ON(!test_bit(TTY_LDISC, &tty->flags));
1326 mutex_unlock(&tty->ldisc_mutex);
1327
1328 return 0;
1329}
1330
1331/**
1332 * tty_init_dev - initialise a tty device
1333 * @driver: tty driver we are opening a device on
1334 * @idx: device index
1335 * @ret_tty: returned tty structure
1336 * @first_ok: ok to open a new device (used by ptmx)
1337 *
1338 * Prepare a tty device. This may not be a "new" clean device but
1339 * could also be an active device. The pty drivers require special
1340 * handling because of this.
1341 *
1342 * Locking:
1343 * The function is called under the tty_mutex, which
1344 * protects us from the tty struct or driver itself going away.
1345 *
1346 * On exit the tty device has the line discipline attached and
1347 * a reference count of 1. If a pair was created for pty/tty use
1348 * and the other was a pty master then it too has a reference count of 1.
1349 *
1350 * WSH 06/09/97: Rewritten to remove races and properly clean up after a
1351 * failed open. The new code protects the open with a mutex, so it's
1352 * really quite straightforward. The mutex locking can probably be
1353 * relaxed for the (most common) case of reopening a tty.
1354 */
1355
1356struct tty_struct *tty_init_dev(struct tty_driver *driver, int idx,
1357 int first_ok)
1358{
1359 struct tty_struct *tty;
1360 int retval;
1361
1362 /* Check if pty master is being opened multiple times */
1363 if (driver->subtype == PTY_TYPE_MASTER &&
1364 (driver->flags & TTY_DRIVER_DEVPTS_MEM) && !first_ok) {
1365 return ERR_PTR(-EIO);
1366 }
1367
1368 /*
1369 * First time open is complex, especially for PTY devices.
1370 * This code guarantees that either everything succeeds and the
1371 * TTY is ready for operation, or else the table slots are vacated
1372 * and the allocated memory released. (Except that the termios
1373 * and locked termios may be retained.)
1374 */
1375
1376 if (!try_module_get(driver->owner))
1377 return ERR_PTR(-ENODEV);
1378
1379 tty = alloc_tty_struct();
1380 if (!tty)
1381 goto fail_no_mem;
1382 initialize_tty_struct(tty, driver, idx);
1383
1384 retval = tty_driver_install_tty(driver, tty);
1385 if (retval < 0) {
1386 free_tty_struct(tty);
1387 module_put(driver->owner);
1388 return ERR_PTR(retval);
1389 }
1390
1391 /*
1392 * Structures all installed ... call the ldisc open routines.
1393 * If we fail here just call release_tty to clean up. No need
1394 * to decrement the use counts, as release_tty doesn't care.
1395 */
1396 retval = tty_ldisc_setup(tty, tty->link);
1397 if (retval)
1398 goto release_mem_out;
1399 return tty;
1400
1401fail_no_mem:
1402 module_put(driver->owner);
1403 return ERR_PTR(-ENOMEM);
1404
1405 /* call the tty release_tty routine to clean out this slot */
1406release_mem_out:
1407 if (printk_ratelimit())
1408 printk(KERN_INFO "tty_init_dev: ldisc open failed, "
1409 "clearing slot %d\n", idx);
1410 release_tty(tty, idx);
1411 return ERR_PTR(retval);
1412}
1413
1414void tty_free_termios(struct tty_struct *tty)
1415{
1416 struct ktermios *tp;
1417 int idx = tty->index;
1418 /* Kill this flag and push into drivers for locking etc */
1419 if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS) {
1420 /* FIXME: Locking on ->termios array */
1421 tp = tty->termios;
1422 tty->driver->termios[idx] = NULL;
1423 kfree(tp);
1424 }
1425}
1426EXPORT_SYMBOL(tty_free_termios);
1427
1428void tty_shutdown(struct tty_struct *tty)
1429{
1430 tty_driver_remove_tty(tty->driver, tty);
1431 tty_free_termios(tty);
1432}
1433EXPORT_SYMBOL(tty_shutdown);
1434
1435/**
1436 * release_one_tty - release tty structure memory
1437 * @kref: kref of tty we are obliterating
1438 *
1439 * Releases memory associated with a tty structure, and clears out the
1440 * driver table slots. This function is called when a device is no longer
1441 * in use. It also gets called when setup of a device fails.
1442 *
1443 * Locking:
1444 * tty_mutex - sometimes only
1445 * takes the file list lock internally when working on the list
1446 * of ttys that the driver keeps.
1447 *
1448 * This method gets called from a work queue so that the driver private
1449 * cleanup ops can sleep (needed for USB at least)
1450 */
1451static void release_one_tty(struct work_struct *work)
1452{
1453 struct tty_struct *tty =
1454 container_of(work, struct tty_struct, hangup_work);
1455 struct tty_driver *driver = tty->driver;
1456
1457 if (tty->ops->cleanup)
1458 tty->ops->cleanup(tty);
1459
1460 tty->magic = 0;
1461 tty_driver_kref_put(driver);
1462 module_put(driver->owner);
1463
1464 spin_lock(&tty_files_lock);
1465 list_del_init(&tty->tty_files);
1466 spin_unlock(&tty_files_lock);
1467
1468 put_pid(tty->pgrp);
1469 put_pid(tty->session);
1470 free_tty_struct(tty);
1471}
1472
1473static void queue_release_one_tty(struct kref *kref)
1474{
1475 struct tty_struct *tty = container_of(kref, struct tty_struct, kref);
1476
1477 if (tty->ops->shutdown)
1478 tty->ops->shutdown(tty);
1479 else
1480 tty_shutdown(tty);
1481
1482 /* The hangup queue is now free so we can reuse it rather than
1483 waste a chunk of memory for each port */
1484 INIT_WORK(&tty->hangup_work, release_one_tty);
1485 schedule_work(&tty->hangup_work);
1486}
1487
1488/**
1489 * tty_kref_put - release a tty kref
1490 * @tty: tty device
1491 *
1492 * Release a reference to a tty device and if need be let the kref
1493 * layer destruct the object for us
1494 */
1495
1496void tty_kref_put(struct tty_struct *tty)
1497{
1498 if (tty)
1499 kref_put(&tty->kref, queue_release_one_tty);
1500}
1501EXPORT_SYMBOL(tty_kref_put);
1502
1503/**
1504 * release_tty - release tty structure memory
1505 *
1506 * Release both @tty and a possible linked partner (think pty pair),
1507 * and decrement the refcount of the backing module.
1508 *
1509 * Locking:
1510 * tty_mutex - sometimes only
1511 * takes the file list lock internally when working on the list
1512 * of ttys that the driver keeps.
1513 * FIXME: should we require tty_mutex is held here ??
1514 *
1515 */
1516static void release_tty(struct tty_struct *tty, int idx)
1517{
1518 /* This should always be true but check for the moment */
1519 WARN_ON(tty->index != idx);
1520
1521 if (tty->link)
1522 tty_kref_put(tty->link);
1523 tty_kref_put(tty);
1524}
1525
1526/**
1527 * tty_release - vfs callback for close
1528 * @inode: inode of tty
1529 * @filp: file pointer for handle to tty
1530 *
1531 * Called the last time each file handle is closed that references
1532 * this tty. There may however be several such references.
1533 *
1534 * Locking:
1535 * Takes bkl. See tty_release_dev
1536 *
1537 * Even releasing the tty structures is a tricky business.. We have
1538 * to be very careful that the structures are all released at the
1539 * same time, as interrupts might otherwise get the wrong pointers.
1540 *
1541 * WSH 09/09/97: rewritten to avoid some nasty race conditions that could
1542 * lead to double frees or releasing memory still in use.
1543 */
1544
1545int tty_release(struct inode *inode, struct file *filp)
1546{
1547 struct tty_struct *tty = file_tty(filp);
1548 struct tty_struct *o_tty;
1549 int pty_master, tty_closing, o_tty_closing, do_sleep;
1550 int devpts;
1551 int idx;
1552 char buf[64];
1553
1554 if (tty_paranoia_check(tty, inode, "tty_release_dev"))
1555 return 0;
1556
1557 tty_lock();
1558 check_tty_count(tty, "tty_release_dev");
1559
1560 __tty_fasync(-1, filp, 0);
1561
1562 idx = tty->index;
1563 pty_master = (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
1564 tty->driver->subtype == PTY_TYPE_MASTER);
1565 devpts = (tty->driver->flags & TTY_DRIVER_DEVPTS_MEM) != 0;
1566 o_tty = tty->link;
1567
1568#ifdef TTY_PARANOIA_CHECK
1569 if (idx < 0 || idx >= tty->driver->num) {
1570 printk(KERN_DEBUG "tty_release_dev: bad idx when trying to "
1571 "free (%s)\n", tty->name);
1572 tty_unlock();
1573 return 0;
1574 }
1575 if (!devpts) {
1576 if (tty != tty->driver->ttys[idx]) {
1577 tty_unlock();
1578 printk(KERN_DEBUG "tty_release_dev: driver.table[%d] not tty "
1579 "for (%s)\n", idx, tty->name);
1580 return 0;
1581 }
1582 if (tty->termios != tty->driver->termios[idx]) {
1583 tty_unlock();
1584 printk(KERN_DEBUG "tty_release_dev: driver.termios[%d] not termios "
1585 "for (%s)\n",
1586 idx, tty->name);
1587 return 0;
1588 }
1589 }
1590#endif
1591
1592#ifdef TTY_DEBUG_HANGUP
1593 printk(KERN_DEBUG "tty_release_dev of %s (tty count=%d)...",
1594 tty_name(tty, buf), tty->count);
1595#endif
1596
1597#ifdef TTY_PARANOIA_CHECK
1598 if (tty->driver->other &&
1599 !(tty->driver->flags & TTY_DRIVER_DEVPTS_MEM)) {
1600 if (o_tty != tty->driver->other->ttys[idx]) {
1601 tty_unlock();
1602 printk(KERN_DEBUG "tty_release_dev: other->table[%d] "
1603 "not o_tty for (%s)\n",
1604 idx, tty->name);
1605 return 0 ;
1606 }
1607 if (o_tty->termios != tty->driver->other->termios[idx]) {
1608 tty_unlock();
1609 printk(KERN_DEBUG "tty_release_dev: other->termios[%d] "
1610 "not o_termios for (%s)\n",
1611 idx, tty->name);
1612 return 0;
1613 }
1614 if (o_tty->link != tty) {
1615 tty_unlock();
1616 printk(KERN_DEBUG "tty_release_dev: bad pty pointers\n");
1617 return 0;
1618 }
1619 }
1620#endif
1621 if (tty->ops->close)
1622 tty->ops->close(tty, filp);
1623
1624 tty_unlock();
1625 /*
1626 * Sanity check: if tty->count is going to zero, there shouldn't be
1627 * any waiters on tty->read_wait or tty->write_wait. We test the
1628 * wait queues and kick everyone out _before_ actually starting to
1629 * close. This ensures that we won't block while releasing the tty
1630 * structure.
1631 *
1632 * The test for the o_tty closing is necessary, since the master and
1633 * slave sides may close in any order. If the slave side closes out
1634 * first, its count will be one, since the master side holds an open.
1635 * Thus this test wouldn't be triggered at the time the slave closes,
1636 * so we do it now.
1637 *
1638 * Note that it's possible for the tty to be opened again while we're
1639 * flushing out waiters. By recalculating the closing flags before
1640 * each iteration we avoid any problems.
1641 */
1642 while (1) {
1643 /* Guard against races with tty->count changes elsewhere and
1644 opens on /dev/tty */
1645
1646 mutex_lock(&tty_mutex);
1647 tty_lock();
1648 tty_closing = tty->count <= 1;
1649 o_tty_closing = o_tty &&
1650 (o_tty->count <= (pty_master ? 1 : 0));
1651 do_sleep = 0;
1652
1653 if (tty_closing) {
1654 if (waitqueue_active(&tty->read_wait)) {
1655 wake_up_poll(&tty->read_wait, POLLIN);
1656 do_sleep++;
1657 }
1658 if (waitqueue_active(&tty->write_wait)) {
1659 wake_up_poll(&tty->write_wait, POLLOUT);
1660 do_sleep++;
1661 }
1662 }
1663 if (o_tty_closing) {
1664 if (waitqueue_active(&o_tty->read_wait)) {
1665 wake_up_poll(&o_tty->read_wait, POLLIN);
1666 do_sleep++;
1667 }
1668 if (waitqueue_active(&o_tty->write_wait)) {
1669 wake_up_poll(&o_tty->write_wait, POLLOUT);
1670 do_sleep++;
1671 }
1672 }
1673 if (!do_sleep)
1674 break;
1675
1676 printk(KERN_WARNING "tty_release_dev: %s: read/write wait queue "
1677 "active!\n", tty_name(tty, buf));
1678 tty_unlock();
1679 mutex_unlock(&tty_mutex);
1680 schedule();
1681 }
1682
1683 /*
1684 * The closing flags are now consistent with the open counts on
1685 * both sides, and we've completed the last operation that could
1686 * block, so it's safe to proceed with closing.
1687 */
1688 if (pty_master) {
1689 if (--o_tty->count < 0) {
1690 printk(KERN_WARNING "tty_release_dev: bad pty slave count "
1691 "(%d) for %s\n",
1692 o_tty->count, tty_name(o_tty, buf));
1693 o_tty->count = 0;
1694 }
1695 }
1696 if (--tty->count < 0) {
1697 printk(KERN_WARNING "tty_release_dev: bad tty->count (%d) for %s\n",
1698 tty->count, tty_name(tty, buf));
1699 tty->count = 0;
1700 }
1701
1702 /*
1703 * We've decremented tty->count, so we need to remove this file
1704 * descriptor off the tty->tty_files list; this serves two
1705 * purposes:
1706 * - check_tty_count sees the correct number of file descriptors
1707 * associated with this tty.
1708 * - do_tty_hangup no longer sees this file descriptor as
1709 * something that needs to be handled for hangups.
1710 */
1711 tty_del_file(filp);
1712
1713 /*
1714 * Perform some housekeeping before deciding whether to return.
1715 *
1716 * Set the TTY_CLOSING flag if this was the last open. In the
1717 * case of a pty we may have to wait around for the other side
1718 * to close, and TTY_CLOSING makes sure we can't be reopened.
1719 */
1720 if (tty_closing)
1721 set_bit(TTY_CLOSING, &tty->flags);
1722 if (o_tty_closing)
1723 set_bit(TTY_CLOSING, &o_tty->flags);
1724
1725 /*
1726 * If _either_ side is closing, make sure there aren't any
1727 * processes that still think tty or o_tty is their controlling
1728 * tty.
1729 */
1730 if (tty_closing || o_tty_closing) {
1731 read_lock(&tasklist_lock);
1732 session_clear_tty(tty->session);
1733 if (o_tty)
1734 session_clear_tty(o_tty->session);
1735 read_unlock(&tasklist_lock);
1736 }
1737
1738 mutex_unlock(&tty_mutex);
1739
1740 /* check whether both sides are closing ... */
1741 if (!tty_closing || (o_tty && !o_tty_closing)) {
1742 tty_unlock();
1743 return 0;
1744 }
1745
1746#ifdef TTY_DEBUG_HANGUP
1747 printk(KERN_DEBUG "freeing tty structure...");
1748#endif
1749 /*
1750 * Ask the line discipline code to release its structures
1751 */
1752 tty_ldisc_release(tty, o_tty);
1753 /*
1754 * The release_tty function takes care of the details of clearing
1755 * the slots and preserving the termios structure.
1756 */
1757 release_tty(tty, idx);
1758
1759 /* Make this pty number available for reallocation */
1760 if (devpts)
1761 devpts_kill_index(inode, idx);
1762 tty_unlock();
1763 return 0;
1764}
1765
1766/**
1767 * tty_open - open a tty device
1768 * @inode: inode of device file
1769 * @filp: file pointer to tty
1770 *
1771 * tty_open and tty_release keep up the tty count that contains the
1772 * number of opens done on a tty. We cannot use the inode-count, as
1773 * different inodes might point to the same tty.
1774 *
1775 * Open-counting is needed for pty masters, as well as for keeping
1776 * track of serial lines: DTR is dropped when the last close happens.
1777 * (This is not done solely through tty->count, now. - Ted 1/27/92)
1778 *
1779 * The termios state of a pty is reset on first open so that
1780 * settings don't persist across reuse.
1781 *
1782 * Locking: tty_mutex protects tty, get_tty_driver and tty_init_dev work.
1783 * tty->count should protect the rest.
1784 * ->siglock protects ->signal/->sighand
1785 */
1786
1787static int tty_open(struct inode *inode, struct file *filp)
1788{
1789 struct tty_struct *tty = NULL;
1790 int noctty, retval;
1791 struct tty_driver *driver;
1792 int index;
1793 dev_t device = inode->i_rdev;
1794 unsigned saved_flags = filp->f_flags;
1795
1796 nonseekable_open(inode, filp);
1797
1798retry_open:
1799 noctty = filp->f_flags & O_NOCTTY;
1800 index = -1;
1801 retval = 0;
1802
1803 mutex_lock(&tty_mutex);
1804 tty_lock();
1805
1806 if (device == MKDEV(TTYAUX_MAJOR, 0)) {
1807 tty = get_current_tty();
1808 if (!tty) {
1809 tty_unlock();
1810 mutex_unlock(&tty_mutex);
1811 return -ENXIO;
1812 }
1813 driver = tty_driver_kref_get(tty->driver);
1814 index = tty->index;
1815 filp->f_flags |= O_NONBLOCK; /* Don't let /dev/tty block */
1816 /* noctty = 1; */
1817 /* FIXME: Should we take a driver reference ? */
1818 tty_kref_put(tty);
1819 goto got_driver;
1820 }
1821#ifdef CONFIG_VT
1822 if (device == MKDEV(TTY_MAJOR, 0)) {
1823 extern struct tty_driver *console_driver;
1824 driver = tty_driver_kref_get(console_driver);
1825 index = fg_console;
1826 noctty = 1;
1827 goto got_driver;
1828 }
1829#endif
1830 if (device == MKDEV(TTYAUX_MAJOR, 1)) {
1831 struct tty_driver *console_driver = console_device(&index);
1832 if (console_driver) {
1833 driver = tty_driver_kref_get(console_driver);
1834 if (driver) {
1835 /* Don't let /dev/console block */
1836 filp->f_flags |= O_NONBLOCK;
1837 noctty = 1;
1838 goto got_driver;
1839 }
1840 }
1841 tty_unlock();
1842 mutex_unlock(&tty_mutex);
1843 return -ENODEV;
1844 }
1845
1846 driver = get_tty_driver(device, &index);
1847 if (!driver) {
1848 tty_unlock();
1849 mutex_unlock(&tty_mutex);
1850 return -ENODEV;
1851 }
1852got_driver:
1853 if (!tty) {
1854 /* check whether we're reopening an existing tty */
1855 tty = tty_driver_lookup_tty(driver, inode, index);
1856
1857 if (IS_ERR(tty)) {
1858 tty_unlock();
1859 mutex_unlock(&tty_mutex);
1860 return PTR_ERR(tty);
1861 }
1862 }
1863
1864 if (tty) {
1865 retval = tty_reopen(tty);
1866 if (retval)
1867 tty = ERR_PTR(retval);
1868 } else
1869 tty = tty_init_dev(driver, index, 0);
1870
1871 mutex_unlock(&tty_mutex);
1872 tty_driver_kref_put(driver);
1873 if (IS_ERR(tty)) {
1874 tty_unlock();
1875 return PTR_ERR(tty);
1876 }
1877
1878 tty_add_file(tty, filp);
1879
1880 check_tty_count(tty, "tty_open");
1881 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
1882 tty->driver->subtype == PTY_TYPE_MASTER)
1883 noctty = 1;
1884#ifdef TTY_DEBUG_HANGUP
1885 printk(KERN_DEBUG "opening %s...", tty->name);
1886#endif
1887 if (!retval) {
1888 if (tty->ops->open)
1889 retval = tty->ops->open(tty, filp);
1890 else
1891 retval = -ENODEV;
1892 }
1893 filp->f_flags = saved_flags;
1894
1895 if (!retval && test_bit(TTY_EXCLUSIVE, &tty->flags) &&
1896 !capable(CAP_SYS_ADMIN))
1897 retval = -EBUSY;
1898
1899 if (retval) {
1900#ifdef TTY_DEBUG_HANGUP
1901 printk(KERN_DEBUG "error %d in opening %s...", retval,
1902 tty->name);
1903#endif
1904 tty_unlock(); /* need to call tty_release without BTM */
1905 tty_release(inode, filp);
1906 if (retval != -ERESTARTSYS)
1907 return retval;
1908
1909 if (signal_pending(current))
1910 return retval;
1911
1912 schedule();
1913 /*
1914 * Need to reset f_op in case a hangup happened.
1915 */
1916 tty_lock();
1917 if (filp->f_op == &hung_up_tty_fops)
1918 filp->f_op = &tty_fops;
1919 tty_unlock();
1920 goto retry_open;
1921 }
1922 tty_unlock();
1923
1924
1925 mutex_lock(&tty_mutex);
1926 tty_lock();
1927 spin_lock_irq(&current->sighand->siglock);
1928 if (!noctty &&
1929 current->signal->leader &&
1930 !current->signal->tty &&
1931 tty->session == NULL)
1932 __proc_set_tty(current, tty);
1933 spin_unlock_irq(&current->sighand->siglock);
1934 tty_unlock();
1935 mutex_unlock(&tty_mutex);
1936 return 0;
1937}
1938
1939
1940
1941/**
1942 * tty_poll - check tty status
1943 * @filp: file being polled
1944 * @wait: poll wait structures to update
1945 *
1946 * Call the line discipline polling method to obtain the poll
1947 * status of the device.
1948 *
1949 * Locking: locks called line discipline but ldisc poll method
1950 * may be re-entered freely by other callers.
1951 */
1952
1953static unsigned int tty_poll(struct file *filp, poll_table *wait)
1954{
1955 struct tty_struct *tty = file_tty(filp);
1956 struct tty_ldisc *ld;
1957 int ret = 0;
1958
1959 if (tty_paranoia_check(tty, filp->f_path.dentry->d_inode, "tty_poll"))
1960 return 0;
1961
1962 ld = tty_ldisc_ref_wait(tty);
1963 if (ld->ops->poll)
1964 ret = (ld->ops->poll)(tty, filp, wait);
1965 tty_ldisc_deref(ld);
1966 return ret;
1967}
1968
1969static int __tty_fasync(int fd, struct file *filp, int on)
1970{
1971 struct tty_struct *tty = file_tty(filp);
1972 unsigned long flags;
1973 int retval = 0;
1974
1975 if (tty_paranoia_check(tty, filp->f_path.dentry->d_inode, "tty_fasync"))
1976 goto out;
1977
1978 retval = fasync_helper(fd, filp, on, &tty->fasync);
1979 if (retval <= 0)
1980 goto out;
1981
1982 if (on) {
1983 enum pid_type type;
1984 struct pid *pid;
1985 if (!waitqueue_active(&tty->read_wait))
1986 tty->minimum_to_wake = 1;
1987 spin_lock_irqsave(&tty->ctrl_lock, flags);
1988 if (tty->pgrp) {
1989 pid = tty->pgrp;
1990 type = PIDTYPE_PGID;
1991 } else {
1992 pid = task_pid(current);
1993 type = PIDTYPE_PID;
1994 }
1995 get_pid(pid);
1996 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
1997 retval = __f_setown(filp, pid, type, 0);
1998 put_pid(pid);
1999 if (retval)
2000 goto out;
2001 } else {
2002 if (!tty->fasync && !waitqueue_active(&tty->read_wait))
2003 tty->minimum_to_wake = N_TTY_BUF_SIZE;
2004 }
2005 retval = 0;
2006out:
2007 return retval;
2008}
2009
2010static int tty_fasync(int fd, struct file *filp, int on)
2011{
2012 int retval;
2013 tty_lock();
2014 retval = __tty_fasync(fd, filp, on);
2015 tty_unlock();
2016 return retval;
2017}
2018
2019/**
2020 * tiocsti - fake input character
2021 * @tty: tty to fake input into
2022 * @p: pointer to character
2023 *
2024 * Fake input to a tty device. Does the necessary locking and
2025 * input management.
2026 *
2027 * FIXME: does not honour flow control ??
2028 *
2029 * Locking:
2030 * Called functions take tty_ldisc_lock
2031 * current->signal->tty check is safe without locks
2032 *
2033 * FIXME: may race normal receive processing
2034 */
2035
2036static int tiocsti(struct tty_struct *tty, char __user *p)
2037{
2038 char ch, mbz = 0;
2039 struct tty_ldisc *ld;
2040
2041 if ((current->signal->tty != tty) && !capable(CAP_SYS_ADMIN))
2042 return -EPERM;
2043 if (get_user(ch, p))
2044 return -EFAULT;
2045 tty_audit_tiocsti(tty, ch);
2046 ld = tty_ldisc_ref_wait(tty);
2047 ld->ops->receive_buf(tty, &ch, &mbz, 1);
2048 tty_ldisc_deref(ld);
2049 return 0;
2050}
2051
2052/**
2053 * tiocgwinsz - implement window query ioctl
2054 * @tty; tty
2055 * @arg: user buffer for result
2056 *
2057 * Copies the kernel idea of the window size into the user buffer.
2058 *
2059 * Locking: tty->termios_mutex is taken to ensure the winsize data
2060 * is consistent.
2061 */
2062
2063static int tiocgwinsz(struct tty_struct *tty, struct winsize __user *arg)
2064{
2065 int err;
2066
2067 mutex_lock(&tty->termios_mutex);
2068 err = copy_to_user(arg, &tty->winsize, sizeof(*arg));
2069 mutex_unlock(&tty->termios_mutex);
2070
2071 return err ? -EFAULT: 0;
2072}
2073
2074/**
2075 * tty_do_resize - resize event
2076 * @tty: tty being resized
2077 * @rows: rows (character)
2078 * @cols: cols (character)
2079 *
2080 * Update the termios variables and send the necessary signals to
2081 * peform a terminal resize correctly
2082 */
2083
2084int tty_do_resize(struct tty_struct *tty, struct winsize *ws)
2085{
2086 struct pid *pgrp;
2087 unsigned long flags;
2088
2089 /* Lock the tty */
2090 mutex_lock(&tty->termios_mutex);
2091 if (!memcmp(ws, &tty->winsize, sizeof(*ws)))
2092 goto done;
2093 /* Get the PID values and reference them so we can
2094 avoid holding the tty ctrl lock while sending signals */
2095 spin_lock_irqsave(&tty->ctrl_lock, flags);
2096 pgrp = get_pid(tty->pgrp);
2097 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
2098
2099 if (pgrp)
2100 kill_pgrp(pgrp, SIGWINCH, 1);
2101 put_pid(pgrp);
2102
2103 tty->winsize = *ws;
2104done:
2105 mutex_unlock(&tty->termios_mutex);
2106 return 0;
2107}
2108
2109/**
2110 * tiocswinsz - implement window size set ioctl
2111 * @tty; tty side of tty
2112 * @arg: user buffer for result
2113 *
2114 * Copies the user idea of the window size to the kernel. Traditionally
2115 * this is just advisory information but for the Linux console it
2116 * actually has driver level meaning and triggers a VC resize.
2117 *
2118 * Locking:
2119 * Driver dependant. The default do_resize method takes the
2120 * tty termios mutex and ctrl_lock. The console takes its own lock
2121 * then calls into the default method.
2122 */
2123
2124static int tiocswinsz(struct tty_struct *tty, struct winsize __user *arg)
2125{
2126 struct winsize tmp_ws;
2127 if (copy_from_user(&tmp_ws, arg, sizeof(*arg)))
2128 return -EFAULT;
2129
2130 if (tty->ops->resize)
2131 return tty->ops->resize(tty, &tmp_ws);
2132 else
2133 return tty_do_resize(tty, &tmp_ws);
2134}
2135
2136/**
2137 * tioccons - allow admin to move logical console
2138 * @file: the file to become console
2139 *
2140 * Allow the adminstrator to move the redirected console device
2141 *
2142 * Locking: uses redirect_lock to guard the redirect information
2143 */
2144
2145static int tioccons(struct file *file)
2146{
2147 if (!capable(CAP_SYS_ADMIN))
2148 return -EPERM;
2149 if (file->f_op->write == redirected_tty_write) {
2150 struct file *f;
2151 spin_lock(&redirect_lock);
2152 f = redirect;
2153 redirect = NULL;
2154 spin_unlock(&redirect_lock);
2155 if (f)
2156 fput(f);
2157 return 0;
2158 }
2159 spin_lock(&redirect_lock);
2160 if (redirect) {
2161 spin_unlock(&redirect_lock);
2162 return -EBUSY;
2163 }
2164 get_file(file);
2165 redirect = file;
2166 spin_unlock(&redirect_lock);
2167 return 0;
2168}
2169
2170/**
2171 * fionbio - non blocking ioctl
2172 * @file: file to set blocking value
2173 * @p: user parameter
2174 *
2175 * Historical tty interfaces had a blocking control ioctl before
2176 * the generic functionality existed. This piece of history is preserved
2177 * in the expected tty API of posix OS's.
2178 *
2179 * Locking: none, the open file handle ensures it won't go away.
2180 */
2181
2182static int fionbio(struct file *file, int __user *p)
2183{
2184 int nonblock;
2185
2186 if (get_user(nonblock, p))
2187 return -EFAULT;
2188
2189 spin_lock(&file->f_lock);
2190 if (nonblock)
2191 file->f_flags |= O_NONBLOCK;
2192 else
2193 file->f_flags &= ~O_NONBLOCK;
2194 spin_unlock(&file->f_lock);
2195 return 0;
2196}
2197
2198/**
2199 * tiocsctty - set controlling tty
2200 * @tty: tty structure
2201 * @arg: user argument
2202 *
2203 * This ioctl is used to manage job control. It permits a session
2204 * leader to set this tty as the controlling tty for the session.
2205 *
2206 * Locking:
2207 * Takes tty_mutex() to protect tty instance
2208 * Takes tasklist_lock internally to walk sessions
2209 * Takes ->siglock() when updating signal->tty
2210 */
2211
2212static int tiocsctty(struct tty_struct *tty, int arg)
2213{
2214 int ret = 0;
2215 if (current->signal->leader && (task_session(current) == tty->session))
2216 return ret;
2217
2218 mutex_lock(&tty_mutex);
2219 /*
2220 * The process must be a session leader and
2221 * not have a controlling tty already.
2222 */
2223 if (!current->signal->leader || current->signal->tty) {
2224 ret = -EPERM;
2225 goto unlock;
2226 }
2227
2228 if (tty->session) {
2229 /*
2230 * This tty is already the controlling
2231 * tty for another session group!
2232 */
2233 if (arg == 1 && capable(CAP_SYS_ADMIN)) {
2234 /*
2235 * Steal it away
2236 */
2237 read_lock(&tasklist_lock);
2238 session_clear_tty(tty->session);
2239 read_unlock(&tasklist_lock);
2240 } else {
2241 ret = -EPERM;
2242 goto unlock;
2243 }
2244 }
2245 proc_set_tty(current, tty);
2246unlock:
2247 mutex_unlock(&tty_mutex);
2248 return ret;
2249}
2250
2251/**
2252 * tty_get_pgrp - return a ref counted pgrp pid
2253 * @tty: tty to read
2254 *
2255 * Returns a refcounted instance of the pid struct for the process
2256 * group controlling the tty.
2257 */
2258
2259struct pid *tty_get_pgrp(struct tty_struct *tty)
2260{
2261 unsigned long flags;
2262 struct pid *pgrp;
2263
2264 spin_lock_irqsave(&tty->ctrl_lock, flags);
2265 pgrp = get_pid(tty->pgrp);
2266 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
2267
2268 return pgrp;
2269}
2270EXPORT_SYMBOL_GPL(tty_get_pgrp);
2271
2272/**
2273 * tiocgpgrp - get process group
2274 * @tty: tty passed by user
2275 * @real_tty: tty side of the tty pased by the user if a pty else the tty
2276 * @p: returned pid
2277 *
2278 * Obtain the process group of the tty. If there is no process group
2279 * return an error.
2280 *
2281 * Locking: none. Reference to current->signal->tty is safe.
2282 */
2283
2284static int tiocgpgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
2285{
2286 struct pid *pid;
2287 int ret;
2288 /*
2289 * (tty == real_tty) is a cheap way of
2290 * testing if the tty is NOT a master pty.
2291 */
2292 if (tty == real_tty && current->signal->tty != real_tty)
2293 return -ENOTTY;
2294 pid = tty_get_pgrp(real_tty);
2295 ret = put_user(pid_vnr(pid), p);
2296 put_pid(pid);
2297 return ret;
2298}
2299
2300/**
2301 * tiocspgrp - attempt to set process group
2302 * @tty: tty passed by user
2303 * @real_tty: tty side device matching tty passed by user
2304 * @p: pid pointer
2305 *
2306 * Set the process group of the tty to the session passed. Only
2307 * permitted where the tty session is our session.
2308 *
2309 * Locking: RCU, ctrl lock
2310 */
2311
2312static int tiocspgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
2313{
2314 struct pid *pgrp;
2315 pid_t pgrp_nr;
2316 int retval = tty_check_change(real_tty);
2317 unsigned long flags;
2318
2319 if (retval == -EIO)
2320 return -ENOTTY;
2321 if (retval)
2322 return retval;
2323 if (!current->signal->tty ||
2324 (current->signal->tty != real_tty) ||
2325 (real_tty->session != task_session(current)))
2326 return -ENOTTY;
2327 if (get_user(pgrp_nr, p))
2328 return -EFAULT;
2329 if (pgrp_nr < 0)
2330 return -EINVAL;
2331 rcu_read_lock();
2332 pgrp = find_vpid(pgrp_nr);
2333 retval = -ESRCH;
2334 if (!pgrp)
2335 goto out_unlock;
2336 retval = -EPERM;
2337 if (session_of_pgrp(pgrp) != task_session(current))
2338 goto out_unlock;
2339 retval = 0;
2340 spin_lock_irqsave(&tty->ctrl_lock, flags);
2341 put_pid(real_tty->pgrp);
2342 real_tty->pgrp = get_pid(pgrp);
2343 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
2344out_unlock:
2345 rcu_read_unlock();
2346 return retval;
2347}
2348
2349/**
2350 * tiocgsid - get session id
2351 * @tty: tty passed by user
2352 * @real_tty: tty side of the tty pased by the user if a pty else the tty
2353 * @p: pointer to returned session id
2354 *
2355 * Obtain the session id of the tty. If there is no session
2356 * return an error.
2357 *
2358 * Locking: none. Reference to current->signal->tty is safe.
2359 */
2360
2361static int tiocgsid(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
2362{
2363 /*
2364 * (tty == real_tty) is a cheap way of
2365 * testing if the tty is NOT a master pty.
2366 */
2367 if (tty == real_tty && current->signal->tty != real_tty)
2368 return -ENOTTY;
2369 if (!real_tty->session)
2370 return -ENOTTY;
2371 return put_user(pid_vnr(real_tty->session), p);
2372}
2373
2374/**
2375 * tiocsetd - set line discipline
2376 * @tty: tty device
2377 * @p: pointer to user data
2378 *
2379 * Set the line discipline according to user request.
2380 *
2381 * Locking: see tty_set_ldisc, this function is just a helper
2382 */
2383
2384static int tiocsetd(struct tty_struct *tty, int __user *p)
2385{
2386 int ldisc;
2387 int ret;
2388
2389 if (get_user(ldisc, p))
2390 return -EFAULT;
2391
2392 ret = tty_set_ldisc(tty, ldisc);
2393
2394 return ret;
2395}
2396
2397/**
2398 * send_break - performed time break
2399 * @tty: device to break on
2400 * @duration: timeout in mS
2401 *
2402 * Perform a timed break on hardware that lacks its own driver level
2403 * timed break functionality.
2404 *
2405 * Locking:
2406 * atomic_write_lock serializes
2407 *
2408 */
2409
2410static int send_break(struct tty_struct *tty, unsigned int duration)
2411{
2412 int retval;
2413
2414 if (tty->ops->break_ctl == NULL)
2415 return 0;
2416
2417 if (tty->driver->flags & TTY_DRIVER_HARDWARE_BREAK)
2418 retval = tty->ops->break_ctl(tty, duration);
2419 else {
2420 /* Do the work ourselves */
2421 if (tty_write_lock(tty, 0) < 0)
2422 return -EINTR;
2423 retval = tty->ops->break_ctl(tty, -1);
2424 if (retval)
2425 goto out;
2426 if (!signal_pending(current))
2427 msleep_interruptible(duration);
2428 retval = tty->ops->break_ctl(tty, 0);
2429out:
2430 tty_write_unlock(tty);
2431 if (signal_pending(current))
2432 retval = -EINTR;
2433 }
2434 return retval;
2435}
2436
2437/**
2438 * tty_tiocmget - get modem status
2439 * @tty: tty device
2440 * @file: user file pointer
2441 * @p: pointer to result
2442 *
2443 * Obtain the modem status bits from the tty driver if the feature
2444 * is supported. Return -EINVAL if it is not available.
2445 *
2446 * Locking: none (up to the driver)
2447 */
2448
2449static int tty_tiocmget(struct tty_struct *tty, struct file *file, int __user *p)
2450{
2451 int retval = -EINVAL;
2452
2453 if (tty->ops->tiocmget) {
2454 retval = tty->ops->tiocmget(tty, file);
2455
2456 if (retval >= 0)
2457 retval = put_user(retval, p);
2458 }
2459 return retval;
2460}
2461
2462/**
2463 * tty_tiocmset - set modem status
2464 * @tty: tty device
2465 * @file: user file pointer
2466 * @cmd: command - clear bits, set bits or set all
2467 * @p: pointer to desired bits
2468 *
2469 * Set the modem status bits from the tty driver if the feature
2470 * is supported. Return -EINVAL if it is not available.
2471 *
2472 * Locking: none (up to the driver)
2473 */
2474
2475static int tty_tiocmset(struct tty_struct *tty, struct file *file, unsigned int cmd,
2476 unsigned __user *p)
2477{
2478 int retval;
2479 unsigned int set, clear, val;
2480
2481 if (tty->ops->tiocmset == NULL)
2482 return -EINVAL;
2483
2484 retval = get_user(val, p);
2485 if (retval)
2486 return retval;
2487 set = clear = 0;
2488 switch (cmd) {
2489 case TIOCMBIS:
2490 set = val;
2491 break;
2492 case TIOCMBIC:
2493 clear = val;
2494 break;
2495 case TIOCMSET:
2496 set = val;
2497 clear = ~val;
2498 break;
2499 }
2500 set &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP;
2501 clear &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP;
2502 return tty->ops->tiocmset(tty, file, set, clear);
2503}
2504
2505struct tty_struct *tty_pair_get_tty(struct tty_struct *tty)
2506{
2507 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
2508 tty->driver->subtype == PTY_TYPE_MASTER)
2509 tty = tty->link;
2510 return tty;
2511}
2512EXPORT_SYMBOL(tty_pair_get_tty);
2513
2514struct tty_struct *tty_pair_get_pty(struct tty_struct *tty)
2515{
2516 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
2517 tty->driver->subtype == PTY_TYPE_MASTER)
2518 return tty;
2519 return tty->link;
2520}
2521EXPORT_SYMBOL(tty_pair_get_pty);
2522
2523/*
2524 * Split this up, as gcc can choke on it otherwise..
2525 */
2526long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2527{
2528 struct tty_struct *tty = file_tty(file);
2529 struct tty_struct *real_tty;
2530 void __user *p = (void __user *)arg;
2531 int retval;
2532 struct tty_ldisc *ld;
2533 struct inode *inode = file->f_dentry->d_inode;
2534
2535 if (tty_paranoia_check(tty, inode, "tty_ioctl"))
2536 return -EINVAL;
2537
2538 real_tty = tty_pair_get_tty(tty);
2539
2540 /*
2541 * Factor out some common prep work
2542 */
2543 switch (cmd) {
2544 case TIOCSETD:
2545 case TIOCSBRK:
2546 case TIOCCBRK:
2547 case TCSBRK:
2548 case TCSBRKP:
2549 retval = tty_check_change(tty);
2550 if (retval)
2551 return retval;
2552 if (cmd != TIOCCBRK) {
2553 tty_wait_until_sent(tty, 0);
2554 if (signal_pending(current))
2555 return -EINTR;
2556 }
2557 break;
2558 }
2559
2560 /*
2561 * Now do the stuff.
2562 */
2563 switch (cmd) {
2564 case TIOCSTI:
2565 return tiocsti(tty, p);
2566 case TIOCGWINSZ:
2567 return tiocgwinsz(real_tty, p);
2568 case TIOCSWINSZ:
2569 return tiocswinsz(real_tty, p);
2570 case TIOCCONS:
2571 return real_tty != tty ? -EINVAL : tioccons(file);
2572 case FIONBIO:
2573 return fionbio(file, p);
2574 case TIOCEXCL:
2575 set_bit(TTY_EXCLUSIVE, &tty->flags);
2576 return 0;
2577 case TIOCNXCL:
2578 clear_bit(TTY_EXCLUSIVE, &tty->flags);
2579 return 0;
2580 case TIOCNOTTY:
2581 if (current->signal->tty != tty)
2582 return -ENOTTY;
2583 no_tty();
2584 return 0;
2585 case TIOCSCTTY:
2586 return tiocsctty(tty, arg);
2587 case TIOCGPGRP:
2588 return tiocgpgrp(tty, real_tty, p);
2589 case TIOCSPGRP:
2590 return tiocspgrp(tty, real_tty, p);
2591 case TIOCGSID:
2592 return tiocgsid(tty, real_tty, p);
2593 case TIOCGETD:
2594 return put_user(tty->ldisc->ops->num, (int __user *)p);
2595 case TIOCSETD:
2596 return tiocsetd(tty, p);
2597 /*
2598 * Break handling
2599 */
2600 case TIOCSBRK: /* Turn break on, unconditionally */
2601 if (tty->ops->break_ctl)
2602 return tty->ops->break_ctl(tty, -1);
2603 return 0;
2604 case TIOCCBRK: /* Turn break off, unconditionally */
2605 if (tty->ops->break_ctl)
2606 return tty->ops->break_ctl(tty, 0);
2607 return 0;
2608 case TCSBRK: /* SVID version: non-zero arg --> no break */
2609 /* non-zero arg means wait for all output data
2610 * to be sent (performed above) but don't send break.
2611 * This is used by the tcdrain() termios function.
2612 */
2613 if (!arg)
2614 return send_break(tty, 250);
2615 return 0;
2616 case TCSBRKP: /* support for POSIX tcsendbreak() */
2617 return send_break(tty, arg ? arg*100 : 250);
2618
2619 case TIOCMGET:
2620 return tty_tiocmget(tty, file, p);
2621 case TIOCMSET:
2622 case TIOCMBIC:
2623 case TIOCMBIS:
2624 return tty_tiocmset(tty, file, cmd, p);
2625 case TCFLSH:
2626 switch (arg) {
2627 case TCIFLUSH:
2628 case TCIOFLUSH:
2629 /* flush tty buffer and allow ldisc to process ioctl */
2630 tty_buffer_flush(tty);
2631 break;
2632 }
2633 break;
2634 }
2635 if (tty->ops->ioctl) {
2636 retval = (tty->ops->ioctl)(tty, file, cmd, arg);
2637 if (retval != -ENOIOCTLCMD)
2638 return retval;
2639 }
2640 ld = tty_ldisc_ref_wait(tty);
2641 retval = -EINVAL;
2642 if (ld->ops->ioctl) {
2643 retval = ld->ops->ioctl(tty, file, cmd, arg);
2644 if (retval == -ENOIOCTLCMD)
2645 retval = -EINVAL;
2646 }
2647 tty_ldisc_deref(ld);
2648 return retval;
2649}
2650
2651#ifdef CONFIG_COMPAT
2652static long tty_compat_ioctl(struct file *file, unsigned int cmd,
2653 unsigned long arg)
2654{
2655 struct inode *inode = file->f_dentry->d_inode;
2656 struct tty_struct *tty = file_tty(file);
2657 struct tty_ldisc *ld;
2658 int retval = -ENOIOCTLCMD;
2659
2660 if (tty_paranoia_check(tty, inode, "tty_ioctl"))
2661 return -EINVAL;
2662
2663 if (tty->ops->compat_ioctl) {
2664 retval = (tty->ops->compat_ioctl)(tty, file, cmd, arg);
2665 if (retval != -ENOIOCTLCMD)
2666 return retval;
2667 }
2668
2669 ld = tty_ldisc_ref_wait(tty);
2670 if (ld->ops->compat_ioctl)
2671 retval = ld->ops->compat_ioctl(tty, file, cmd, arg);
2672 tty_ldisc_deref(ld);
2673
2674 return retval;
2675}
2676#endif
2677
2678/*
2679 * This implements the "Secure Attention Key" --- the idea is to
2680 * prevent trojan horses by killing all processes associated with this
2681 * tty when the user hits the "Secure Attention Key". Required for
2682 * super-paranoid applications --- see the Orange Book for more details.
2683 *
2684 * This code could be nicer; ideally it should send a HUP, wait a few
2685 * seconds, then send a INT, and then a KILL signal. But you then
2686 * have to coordinate with the init process, since all processes associated
2687 * with the current tty must be dead before the new getty is allowed
2688 * to spawn.
2689 *
2690 * Now, if it would be correct ;-/ The current code has a nasty hole -
2691 * it doesn't catch files in flight. We may send the descriptor to ourselves
2692 * via AF_UNIX socket, close it and later fetch from socket. FIXME.
2693 *
2694 * Nasty bug: do_SAK is being called in interrupt context. This can
2695 * deadlock. We punt it up to process context. AKPM - 16Mar2001
2696 */
2697void __do_SAK(struct tty_struct *tty)
2698{
2699#ifdef TTY_SOFT_SAK
2700 tty_hangup(tty);
2701#else
2702 struct task_struct *g, *p;
2703 struct pid *session;
2704 int i;
2705 struct file *filp;
2706 struct fdtable *fdt;
2707
2708 if (!tty)
2709 return;
2710 session = tty->session;
2711
2712 tty_ldisc_flush(tty);
2713
2714 tty_driver_flush_buffer(tty);
2715
2716 read_lock(&tasklist_lock);
2717 /* Kill the entire session */
2718 do_each_pid_task(session, PIDTYPE_SID, p) {
2719 printk(KERN_NOTICE "SAK: killed process %d"
2720 " (%s): task_session(p)==tty->session\n",
2721 task_pid_nr(p), p->comm);
2722 send_sig(SIGKILL, p, 1);
2723 } while_each_pid_task(session, PIDTYPE_SID, p);
2724 /* Now kill any processes that happen to have the
2725 * tty open.
2726 */
2727 do_each_thread(g, p) {
2728 if (p->signal->tty == tty) {
2729 printk(KERN_NOTICE "SAK: killed process %d"
2730 " (%s): task_session(p)==tty->session\n",
2731 task_pid_nr(p), p->comm);
2732 send_sig(SIGKILL, p, 1);
2733 continue;
2734 }
2735 task_lock(p);
2736 if (p->files) {
2737 /*
2738 * We don't take a ref to the file, so we must
2739 * hold ->file_lock instead.
2740 */
2741 spin_lock(&p->files->file_lock);
2742 fdt = files_fdtable(p->files);
2743 for (i = 0; i < fdt->max_fds; i++) {
2744 filp = fcheck_files(p->files, i);
2745 if (!filp)
2746 continue;
2747 if (filp->f_op->read == tty_read &&
2748 file_tty(filp) == tty) {
2749 printk(KERN_NOTICE "SAK: killed process %d"
2750 " (%s): fd#%d opened to the tty\n",
2751 task_pid_nr(p), p->comm, i);
2752 force_sig(SIGKILL, p);
2753 break;
2754 }
2755 }
2756 spin_unlock(&p->files->file_lock);
2757 }
2758 task_unlock(p);
2759 } while_each_thread(g, p);
2760 read_unlock(&tasklist_lock);
2761#endif
2762}
2763
2764static void do_SAK_work(struct work_struct *work)
2765{
2766 struct tty_struct *tty =
2767 container_of(work, struct tty_struct, SAK_work);
2768 __do_SAK(tty);
2769}
2770
2771/*
2772 * The tq handling here is a little racy - tty->SAK_work may already be queued.
2773 * Fortunately we don't need to worry, because if ->SAK_work is already queued,
2774 * the values which we write to it will be identical to the values which it
2775 * already has. --akpm
2776 */
2777void do_SAK(struct tty_struct *tty)
2778{
2779 if (!tty)
2780 return;
2781 schedule_work(&tty->SAK_work);
2782}
2783
2784EXPORT_SYMBOL(do_SAK);
2785
2786/**
2787 * initialize_tty_struct
2788 * @tty: tty to initialize
2789 *
2790 * This subroutine initializes a tty structure that has been newly
2791 * allocated.
2792 *
2793 * Locking: none - tty in question must not be exposed at this point
2794 */
2795
2796void initialize_tty_struct(struct tty_struct *tty,
2797 struct tty_driver *driver, int idx)
2798{
2799 memset(tty, 0, sizeof(struct tty_struct));
2800 kref_init(&tty->kref);
2801 tty->magic = TTY_MAGIC;
2802 tty_ldisc_init(tty);
2803 tty->session = NULL;
2804 tty->pgrp = NULL;
2805 tty->overrun_time = jiffies;
2806 tty->buf.head = tty->buf.tail = NULL;
2807 tty_buffer_init(tty);
2808 mutex_init(&tty->termios_mutex);
2809 mutex_init(&tty->ldisc_mutex);
2810 init_waitqueue_head(&tty->write_wait);
2811 init_waitqueue_head(&tty->read_wait);
2812 INIT_WORK(&tty->hangup_work, do_tty_hangup);
2813 mutex_init(&tty->atomic_read_lock);
2814 mutex_init(&tty->atomic_write_lock);
2815 mutex_init(&tty->output_lock);
2816 mutex_init(&tty->echo_lock);
2817 spin_lock_init(&tty->read_lock);
2818 spin_lock_init(&tty->ctrl_lock);
2819 INIT_LIST_HEAD(&tty->tty_files);
2820 INIT_WORK(&tty->SAK_work, do_SAK_work);
2821
2822 tty->driver = driver;
2823 tty->ops = driver->ops;
2824 tty->index = idx;
2825 tty_line_name(driver, idx, tty->name);
2826}
2827
2828/**
2829 * tty_put_char - write one character to a tty
2830 * @tty: tty
2831 * @ch: character
2832 *
2833 * Write one byte to the tty using the provided put_char method
2834 * if present. Returns the number of characters successfully output.
2835 *
2836 * Note: the specific put_char operation in the driver layer may go
2837 * away soon. Don't call it directly, use this method
2838 */
2839
2840int tty_put_char(struct tty_struct *tty, unsigned char ch)
2841{
2842 if (tty->ops->put_char)
2843 return tty->ops->put_char(tty, ch);
2844 return tty->ops->write(tty, &ch, 1);
2845}
2846EXPORT_SYMBOL_GPL(tty_put_char);
2847
2848struct class *tty_class;
2849
2850/**
2851 * tty_register_device - register a tty device
2852 * @driver: the tty driver that describes the tty device
2853 * @index: the index in the tty driver for this tty device
2854 * @device: a struct device that is associated with this tty device.
2855 * This field is optional, if there is no known struct device
2856 * for this tty device it can be set to NULL safely.
2857 *
2858 * Returns a pointer to the struct device for this tty device
2859 * (or ERR_PTR(-EFOO) on error).
2860 *
2861 * This call is required to be made to register an individual tty device
2862 * if the tty driver's flags have the TTY_DRIVER_DYNAMIC_DEV bit set. If
2863 * that bit is not set, this function should not be called by a tty
2864 * driver.
2865 *
2866 * Locking: ??
2867 */
2868
2869struct device *tty_register_device(struct tty_driver *driver, unsigned index,
2870 struct device *device)
2871{
2872 char name[64];
2873 dev_t dev = MKDEV(driver->major, driver->minor_start) + index;
2874
2875 if (index >= driver->num) {
2876 printk(KERN_ERR "Attempt to register invalid tty line number "
2877 " (%d).\n", index);
2878 return ERR_PTR(-EINVAL);
2879 }
2880
2881 if (driver->type == TTY_DRIVER_TYPE_PTY)
2882 pty_line_name(driver, index, name);
2883 else
2884 tty_line_name(driver, index, name);
2885
2886 return device_create(tty_class, device, dev, NULL, name);
2887}
2888EXPORT_SYMBOL(tty_register_device);
2889
2890/**
2891 * tty_unregister_device - unregister a tty device
2892 * @driver: the tty driver that describes the tty device
2893 * @index: the index in the tty driver for this tty device
2894 *
2895 * If a tty device is registered with a call to tty_register_device() then
2896 * this function must be called when the tty device is gone.
2897 *
2898 * Locking: ??
2899 */
2900
2901void tty_unregister_device(struct tty_driver *driver, unsigned index)
2902{
2903 device_destroy(tty_class,
2904 MKDEV(driver->major, driver->minor_start) + index);
2905}
2906EXPORT_SYMBOL(tty_unregister_device);
2907
2908struct tty_driver *alloc_tty_driver(int lines)
2909{
2910 struct tty_driver *driver;
2911
2912 driver = kzalloc(sizeof(struct tty_driver), GFP_KERNEL);
2913 if (driver) {
2914 kref_init(&driver->kref);
2915 driver->magic = TTY_DRIVER_MAGIC;
2916 driver->num = lines;
2917 /* later we'll move allocation of tables here */
2918 }
2919 return driver;
2920}
2921EXPORT_SYMBOL(alloc_tty_driver);
2922
2923static void destruct_tty_driver(struct kref *kref)
2924{
2925 struct tty_driver *driver = container_of(kref, struct tty_driver, kref);
2926 int i;
2927 struct ktermios *tp;
2928 void *p;
2929
2930 if (driver->flags & TTY_DRIVER_INSTALLED) {
2931 /*
2932 * Free the termios and termios_locked structures because
2933 * we don't want to get memory leaks when modular tty
2934 * drivers are removed from the kernel.
2935 */
2936 for (i = 0; i < driver->num; i++) {
2937 tp = driver->termios[i];
2938 if (tp) {
2939 driver->termios[i] = NULL;
2940 kfree(tp);
2941 }
2942 if (!(driver->flags & TTY_DRIVER_DYNAMIC_DEV))
2943 tty_unregister_device(driver, i);
2944 }
2945 p = driver->ttys;
2946 proc_tty_unregister_driver(driver);
2947 driver->ttys = NULL;
2948 driver->termios = NULL;
2949 kfree(p);
2950 cdev_del(&driver->cdev);
2951 }
2952 kfree(driver);
2953}
2954
2955void tty_driver_kref_put(struct tty_driver *driver)
2956{
2957 kref_put(&driver->kref, destruct_tty_driver);
2958}
2959EXPORT_SYMBOL(tty_driver_kref_put);
2960
2961void tty_set_operations(struct tty_driver *driver,
2962 const struct tty_operations *op)
2963{
2964 driver->ops = op;
2965};
2966EXPORT_SYMBOL(tty_set_operations);
2967
2968void put_tty_driver(struct tty_driver *d)
2969{
2970 tty_driver_kref_put(d);
2971}
2972EXPORT_SYMBOL(put_tty_driver);
2973
2974/*
2975 * Called by a tty driver to register itself.
2976 */
2977int tty_register_driver(struct tty_driver *driver)
2978{
2979 int error;
2980 int i;
2981 dev_t dev;
2982 void **p = NULL;
2983
2984 if (!(driver->flags & TTY_DRIVER_DEVPTS_MEM) && driver->num) {
2985 p = kzalloc(driver->num * 2 * sizeof(void *), GFP_KERNEL);
2986 if (!p)
2987 return -ENOMEM;
2988 }
2989
2990 if (!driver->major) {
2991 error = alloc_chrdev_region(&dev, driver->minor_start,
2992 driver->num, driver->name);
2993 if (!error) {
2994 driver->major = MAJOR(dev);
2995 driver->minor_start = MINOR(dev);
2996 }
2997 } else {
2998 dev = MKDEV(driver->major, driver->minor_start);
2999 error = register_chrdev_region(dev, driver->num, driver->name);
3000 }
3001 if (error < 0) {
3002 kfree(p);
3003 return error;
3004 }
3005
3006 if (p) {
3007 driver->ttys = (struct tty_struct **)p;
3008 driver->termios = (struct ktermios **)(p + driver->num);
3009 } else {
3010 driver->ttys = NULL;
3011 driver->termios = NULL;
3012 }
3013
3014 cdev_init(&driver->cdev, &tty_fops);
3015 driver->cdev.owner = driver->owner;
3016 error = cdev_add(&driver->cdev, dev, driver->num);
3017 if (error) {
3018 unregister_chrdev_region(dev, driver->num);
3019 driver->ttys = NULL;
3020 driver->termios = NULL;
3021 kfree(p);
3022 return error;
3023 }
3024
3025 mutex_lock(&tty_mutex);
3026 list_add(&driver->tty_drivers, &tty_drivers);
3027 mutex_unlock(&tty_mutex);
3028
3029 if (!(driver->flags & TTY_DRIVER_DYNAMIC_DEV)) {
3030 for (i = 0; i < driver->num; i++)
3031 tty_register_device(driver, i, NULL);
3032 }
3033 proc_tty_register_driver(driver);
3034 driver->flags |= TTY_DRIVER_INSTALLED;
3035 return 0;
3036}
3037
3038EXPORT_SYMBOL(tty_register_driver);
3039
3040/*
3041 * Called by a tty driver to unregister itself.
3042 */
3043int tty_unregister_driver(struct tty_driver *driver)
3044{
3045#if 0
3046 /* FIXME */
3047 if (driver->refcount)
3048 return -EBUSY;
3049#endif
3050 unregister_chrdev_region(MKDEV(driver->major, driver->minor_start),
3051 driver->num);
3052 mutex_lock(&tty_mutex);
3053 list_del(&driver->tty_drivers);
3054 mutex_unlock(&tty_mutex);
3055 return 0;
3056}
3057
3058EXPORT_SYMBOL(tty_unregister_driver);
3059
3060dev_t tty_devnum(struct tty_struct *tty)
3061{
3062 return MKDEV(tty->driver->major, tty->driver->minor_start) + tty->index;
3063}
3064EXPORT_SYMBOL(tty_devnum);
3065
3066void proc_clear_tty(struct task_struct *p)
3067{
3068 unsigned long flags;
3069 struct tty_struct *tty;
3070 spin_lock_irqsave(&p->sighand->siglock, flags);
3071 tty = p->signal->tty;
3072 p->signal->tty = NULL;
3073 spin_unlock_irqrestore(&p->sighand->siglock, flags);
3074 tty_kref_put(tty);
3075}
3076
3077/* Called under the sighand lock */
3078
3079static void __proc_set_tty(struct task_struct *tsk, struct tty_struct *tty)
3080{
3081 if (tty) {
3082 unsigned long flags;
3083 /* We should not have a session or pgrp to put here but.... */
3084 spin_lock_irqsave(&tty->ctrl_lock, flags);
3085 put_pid(tty->session);
3086 put_pid(tty->pgrp);
3087 tty->pgrp = get_pid(task_pgrp(tsk));
3088 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
3089 tty->session = get_pid(task_session(tsk));
3090 if (tsk->signal->tty) {
3091 printk(KERN_DEBUG "tty not NULL!!\n");
3092 tty_kref_put(tsk->signal->tty);
3093 }
3094 }
3095 put_pid(tsk->signal->tty_old_pgrp);
3096 tsk->signal->tty = tty_kref_get(tty);
3097 tsk->signal->tty_old_pgrp = NULL;
3098}
3099
3100static void proc_set_tty(struct task_struct *tsk, struct tty_struct *tty)
3101{
3102 spin_lock_irq(&tsk->sighand->siglock);
3103 __proc_set_tty(tsk, tty);
3104 spin_unlock_irq(&tsk->sighand->siglock);
3105}
3106
3107struct tty_struct *get_current_tty(void)
3108{
3109 struct tty_struct *tty;
3110 unsigned long flags;
3111
3112 spin_lock_irqsave(&current->sighand->siglock, flags);
3113 tty = tty_kref_get(current->signal->tty);
3114 spin_unlock_irqrestore(&current->sighand->siglock, flags);
3115 return tty;
3116}
3117EXPORT_SYMBOL_GPL(get_current_tty);
3118
3119void tty_default_fops(struct file_operations *fops)
3120{
3121 *fops = tty_fops;
3122}
3123
3124/*
3125 * Initialize the console device. This is called *early*, so
3126 * we can't necessarily depend on lots of kernel help here.
3127 * Just do some early initializations, and do the complex setup
3128 * later.
3129 */
3130void __init console_init(void)
3131{
3132 initcall_t *call;
3133
3134 /* Setup the default TTY line discipline. */
3135 tty_ldisc_begin();
3136
3137 /*
3138 * set up the console device so that later boot sequences can
3139 * inform about problems etc..
3140 */
3141 call = __con_initcall_start;
3142 while (call < __con_initcall_end) {
3143 (*call)();
3144 call++;
3145 }
3146}
3147
3148static char *tty_devnode(struct device *dev, mode_t *mode)
3149{
3150 if (!mode)
3151 return NULL;
3152 if (dev->devt == MKDEV(TTYAUX_MAJOR, 0) ||
3153 dev->devt == MKDEV(TTYAUX_MAJOR, 2))
3154 *mode = 0666;
3155 return NULL;
3156}
3157
3158static int __init tty_class_init(void)
3159{
3160 tty_class = class_create(THIS_MODULE, "tty");
3161 if (IS_ERR(tty_class))
3162 return PTR_ERR(tty_class);
3163 tty_class->devnode = tty_devnode;
3164 return 0;
3165}
3166
3167postcore_initcall(tty_class_init);
3168
3169/* 3/2004 jmc: why do these devices exist? */
3170
3171static struct cdev tty_cdev, console_cdev;
3172
3173/*
3174 * Ok, now we can initialize the rest of the tty devices and can count
3175 * on memory allocations, interrupts etc..
3176 */
3177int __init tty_init(void)
3178{
3179 cdev_init(&tty_cdev, &tty_fops);
3180 if (cdev_add(&tty_cdev, MKDEV(TTYAUX_MAJOR, 0), 1) ||
3181 register_chrdev_region(MKDEV(TTYAUX_MAJOR, 0), 1, "/dev/tty") < 0)
3182 panic("Couldn't register /dev/tty driver\n");
3183 device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 0), NULL,
3184 "tty");
3185
3186 cdev_init(&console_cdev, &console_fops);
3187 if (cdev_add(&console_cdev, MKDEV(TTYAUX_MAJOR, 1), 1) ||
3188 register_chrdev_region(MKDEV(TTYAUX_MAJOR, 1), 1, "/dev/console") < 0)
3189 panic("Couldn't register /dev/console driver\n");
3190 device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 1), NULL,
3191 "console");
3192
3193#ifdef CONFIG_VT
3194 vty_init(&console_fops);
3195#endif
3196 return 0;
3197}
3198
diff --git a/drivers/char/tty_ioctl.c b/drivers/char/tty_ioctl.c
deleted file mode 100644
index 0c1889971459..000000000000
--- a/drivers/char/tty_ioctl.c
+++ /dev/null
@@ -1,1179 +0,0 @@
1/*
2 * linux/drivers/char/tty_ioctl.c
3 *
4 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
5 *
6 * Modified by Fred N. van Kempen, 01/29/93, to add line disciplines
7 * which can be dynamically activated and de-activated by the line
8 * discipline handling modules (like SLIP).
9 */
10
11#include <linux/types.h>
12#include <linux/termios.h>
13#include <linux/errno.h>
14#include <linux/sched.h>
15#include <linux/kernel.h>
16#include <linux/major.h>
17#include <linux/tty.h>
18#include <linux/fcntl.h>
19#include <linux/string.h>
20#include <linux/mm.h>
21#include <linux/module.h>
22#include <linux/bitops.h>
23#include <linux/mutex.h>
24
25#include <asm/io.h>
26#include <asm/uaccess.h>
27#include <asm/system.h>
28
29#undef TTY_DEBUG_WAIT_UNTIL_SENT
30
31#undef DEBUG
32
33/*
34 * Internal flag options for termios setting behavior
35 */
36#define TERMIOS_FLUSH 1
37#define TERMIOS_WAIT 2
38#define TERMIOS_TERMIO 4
39#define TERMIOS_OLD 8
40
41
42/**
43 * tty_chars_in_buffer - characters pending
44 * @tty: terminal
45 *
46 * Return the number of bytes of data in the device private
47 * output queue. If no private method is supplied there is assumed
48 * to be no queue on the device.
49 */
50
51int tty_chars_in_buffer(struct tty_struct *tty)
52{
53 if (tty->ops->chars_in_buffer)
54 return tty->ops->chars_in_buffer(tty);
55 else
56 return 0;
57}
58EXPORT_SYMBOL(tty_chars_in_buffer);
59
60/**
61 * tty_write_room - write queue space
62 * @tty: terminal
63 *
64 * Return the number of bytes that can be queued to this device
65 * at the present time. The result should be treated as a guarantee
66 * and the driver cannot offer a value it later shrinks by more than
67 * the number of bytes written. If no method is provided 2K is always
68 * returned and data may be lost as there will be no flow control.
69 */
70
71int tty_write_room(struct tty_struct *tty)
72{
73 if (tty->ops->write_room)
74 return tty->ops->write_room(tty);
75 return 2048;
76}
77EXPORT_SYMBOL(tty_write_room);
78
79/**
80 * tty_driver_flush_buffer - discard internal buffer
81 * @tty: terminal
82 *
83 * Discard the internal output buffer for this device. If no method
84 * is provided then either the buffer cannot be hardware flushed or
85 * there is no buffer driver side.
86 */
87void tty_driver_flush_buffer(struct tty_struct *tty)
88{
89 if (tty->ops->flush_buffer)
90 tty->ops->flush_buffer(tty);
91}
92EXPORT_SYMBOL(tty_driver_flush_buffer);
93
94/**
95 * tty_throttle - flow control
96 * @tty: terminal
97 *
98 * Indicate that a tty should stop transmitting data down the stack.
99 * Takes the termios mutex to protect against parallel throttle/unthrottle
100 * and also to ensure the driver can consistently reference its own
101 * termios data at this point when implementing software flow control.
102 */
103
104void tty_throttle(struct tty_struct *tty)
105{
106 mutex_lock(&tty->termios_mutex);
107 /* check TTY_THROTTLED first so it indicates our state */
108 if (!test_and_set_bit(TTY_THROTTLED, &tty->flags) &&
109 tty->ops->throttle)
110 tty->ops->throttle(tty);
111 mutex_unlock(&tty->termios_mutex);
112}
113EXPORT_SYMBOL(tty_throttle);
114
115/**
116 * tty_unthrottle - flow control
117 * @tty: terminal
118 *
119 * Indicate that a tty may continue transmitting data down the stack.
120 * Takes the termios mutex to protect against parallel throttle/unthrottle
121 * and also to ensure the driver can consistently reference its own
122 * termios data at this point when implementing software flow control.
123 *
124 * Drivers should however remember that the stack can issue a throttle,
125 * then change flow control method, then unthrottle.
126 */
127
128void tty_unthrottle(struct tty_struct *tty)
129{
130 mutex_lock(&tty->termios_mutex);
131 if (test_and_clear_bit(TTY_THROTTLED, &tty->flags) &&
132 tty->ops->unthrottle)
133 tty->ops->unthrottle(tty);
134 mutex_unlock(&tty->termios_mutex);
135}
136EXPORT_SYMBOL(tty_unthrottle);
137
138/**
139 * tty_wait_until_sent - wait for I/O to finish
140 * @tty: tty we are waiting for
141 * @timeout: how long we will wait
142 *
143 * Wait for characters pending in a tty driver to hit the wire, or
144 * for a timeout to occur (eg due to flow control)
145 *
146 * Locking: none
147 */
148
149void tty_wait_until_sent(struct tty_struct *tty, long timeout)
150{
151#ifdef TTY_DEBUG_WAIT_UNTIL_SENT
152 char buf[64];
153
154 printk(KERN_DEBUG "%s wait until sent...\n", tty_name(tty, buf));
155#endif
156 if (!timeout)
157 timeout = MAX_SCHEDULE_TIMEOUT;
158 if (wait_event_interruptible_timeout(tty->write_wait,
159 !tty_chars_in_buffer(tty), timeout) >= 0) {
160 if (tty->ops->wait_until_sent)
161 tty->ops->wait_until_sent(tty, timeout);
162 }
163}
164EXPORT_SYMBOL(tty_wait_until_sent);
165
166
167/*
168 * Termios Helper Methods
169 */
170
171static void unset_locked_termios(struct ktermios *termios,
172 struct ktermios *old,
173 struct ktermios *locked)
174{
175 int i;
176
177#define NOSET_MASK(x, y, z) (x = ((x) & ~(z)) | ((y) & (z)))
178
179 if (!locked) {
180 printk(KERN_WARNING "Warning?!? termios_locked is NULL.\n");
181 return;
182 }
183
184 NOSET_MASK(termios->c_iflag, old->c_iflag, locked->c_iflag);
185 NOSET_MASK(termios->c_oflag, old->c_oflag, locked->c_oflag);
186 NOSET_MASK(termios->c_cflag, old->c_cflag, locked->c_cflag);
187 NOSET_MASK(termios->c_lflag, old->c_lflag, locked->c_lflag);
188 termios->c_line = locked->c_line ? old->c_line : termios->c_line;
189 for (i = 0; i < NCCS; i++)
190 termios->c_cc[i] = locked->c_cc[i] ?
191 old->c_cc[i] : termios->c_cc[i];
192 /* FIXME: What should we do for i/ospeed */
193}
194
195/*
196 * Routine which returns the baud rate of the tty
197 *
198 * Note that the baud_table needs to be kept in sync with the
199 * include/asm/termbits.h file.
200 */
201static const speed_t baud_table[] = {
202 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
203 9600, 19200, 38400, 57600, 115200, 230400, 460800,
204#ifdef __sparc__
205 76800, 153600, 307200, 614400, 921600
206#else
207 500000, 576000, 921600, 1000000, 1152000, 1500000, 2000000,
208 2500000, 3000000, 3500000, 4000000
209#endif
210};
211
212#ifndef __sparc__
213static const tcflag_t baud_bits[] = {
214 B0, B50, B75, B110, B134, B150, B200, B300, B600,
215 B1200, B1800, B2400, B4800, B9600, B19200, B38400,
216 B57600, B115200, B230400, B460800, B500000, B576000,
217 B921600, B1000000, B1152000, B1500000, B2000000, B2500000,
218 B3000000, B3500000, B4000000
219};
220#else
221static const tcflag_t baud_bits[] = {
222 B0, B50, B75, B110, B134, B150, B200, B300, B600,
223 B1200, B1800, B2400, B4800, B9600, B19200, B38400,
224 B57600, B115200, B230400, B460800, B76800, B153600,
225 B307200, B614400, B921600
226};
227#endif
228
229static int n_baud_table = ARRAY_SIZE(baud_table);
230
231/**
232 * tty_termios_baud_rate
233 * @termios: termios structure
234 *
235 * Convert termios baud rate data into a speed. This should be called
236 * with the termios lock held if this termios is a terminal termios
237 * structure. May change the termios data. Device drivers can call this
238 * function but should use ->c_[io]speed directly as they are updated.
239 *
240 * Locking: none
241 */
242
243speed_t tty_termios_baud_rate(struct ktermios *termios)
244{
245 unsigned int cbaud;
246
247 cbaud = termios->c_cflag & CBAUD;
248
249#ifdef BOTHER
250 /* Magic token for arbitary speed via c_ispeed/c_ospeed */
251 if (cbaud == BOTHER)
252 return termios->c_ospeed;
253#endif
254 if (cbaud & CBAUDEX) {
255 cbaud &= ~CBAUDEX;
256
257 if (cbaud < 1 || cbaud + 15 > n_baud_table)
258 termios->c_cflag &= ~CBAUDEX;
259 else
260 cbaud += 15;
261 }
262 return baud_table[cbaud];
263}
264EXPORT_SYMBOL(tty_termios_baud_rate);
265
266/**
267 * tty_termios_input_baud_rate
268 * @termios: termios structure
269 *
270 * Convert termios baud rate data into a speed. This should be called
271 * with the termios lock held if this termios is a terminal termios
272 * structure. May change the termios data. Device drivers can call this
273 * function but should use ->c_[io]speed directly as they are updated.
274 *
275 * Locking: none
276 */
277
278speed_t tty_termios_input_baud_rate(struct ktermios *termios)
279{
280#ifdef IBSHIFT
281 unsigned int cbaud = (termios->c_cflag >> IBSHIFT) & CBAUD;
282
283 if (cbaud == B0)
284 return tty_termios_baud_rate(termios);
285
286 /* Magic token for arbitary speed via c_ispeed*/
287 if (cbaud == BOTHER)
288 return termios->c_ispeed;
289
290 if (cbaud & CBAUDEX) {
291 cbaud &= ~CBAUDEX;
292
293 if (cbaud < 1 || cbaud + 15 > n_baud_table)
294 termios->c_cflag &= ~(CBAUDEX << IBSHIFT);
295 else
296 cbaud += 15;
297 }
298 return baud_table[cbaud];
299#else
300 return tty_termios_baud_rate(termios);
301#endif
302}
303EXPORT_SYMBOL(tty_termios_input_baud_rate);
304
305/**
306 * tty_termios_encode_baud_rate
307 * @termios: ktermios structure holding user requested state
308 * @ispeed: input speed
309 * @ospeed: output speed
310 *
311 * Encode the speeds set into the passed termios structure. This is
312 * used as a library helper for drivers os that they can report back
313 * the actual speed selected when it differs from the speed requested
314 *
315 * For maximal back compatibility with legacy SYS5/POSIX *nix behaviour
316 * we need to carefully set the bits when the user does not get the
317 * desired speed. We allow small margins and preserve as much of possible
318 * of the input intent to keep compatibility.
319 *
320 * Locking: Caller should hold termios lock. This is already held
321 * when calling this function from the driver termios handler.
322 *
323 * The ifdefs deal with platforms whose owners have yet to update them
324 * and will all go away once this is done.
325 */
326
327void tty_termios_encode_baud_rate(struct ktermios *termios,
328 speed_t ibaud, speed_t obaud)
329{
330 int i = 0;
331 int ifound = -1, ofound = -1;
332 int iclose = ibaud/50, oclose = obaud/50;
333 int ibinput = 0;
334
335 if (obaud == 0) /* CD dropped */
336 ibaud = 0; /* Clear ibaud to be sure */
337
338 termios->c_ispeed = ibaud;
339 termios->c_ospeed = obaud;
340
341#ifdef BOTHER
342 /* If the user asked for a precise weird speed give a precise weird
343 answer. If they asked for a Bfoo speed they many have problems
344 digesting non-exact replies so fuzz a bit */
345
346 if ((termios->c_cflag & CBAUD) == BOTHER)
347 oclose = 0;
348 if (((termios->c_cflag >> IBSHIFT) & CBAUD) == BOTHER)
349 iclose = 0;
350 if ((termios->c_cflag >> IBSHIFT) & CBAUD)
351 ibinput = 1; /* An input speed was specified */
352#endif
353 termios->c_cflag &= ~CBAUD;
354
355 /*
356 * Our goal is to find a close match to the standard baud rate
357 * returned. Walk the baud rate table and if we get a very close
358 * match then report back the speed as a POSIX Bxxxx value by
359 * preference
360 */
361
362 do {
363 if (obaud - oclose <= baud_table[i] &&
364 obaud + oclose >= baud_table[i]) {
365 termios->c_cflag |= baud_bits[i];
366 ofound = i;
367 }
368 if (ibaud - iclose <= baud_table[i] &&
369 ibaud + iclose >= baud_table[i]) {
370 /* For the case input == output don't set IBAUD bits
371 if the user didn't do so */
372 if (ofound == i && !ibinput)
373 ifound = i;
374#ifdef IBSHIFT
375 else {
376 ifound = i;
377 termios->c_cflag |= (baud_bits[i] << IBSHIFT);
378 }
379#endif
380 }
381 } while (++i < n_baud_table);
382
383 /*
384 * If we found no match then use BOTHER if provided or warn
385 * the user their platform maintainer needs to wake up if not.
386 */
387#ifdef BOTHER
388 if (ofound == -1)
389 termios->c_cflag |= BOTHER;
390 /* Set exact input bits only if the input and output differ or the
391 user already did */
392 if (ifound == -1 && (ibaud != obaud || ibinput))
393 termios->c_cflag |= (BOTHER << IBSHIFT);
394#else
395 if (ifound == -1 || ofound == -1) {
396 printk_once(KERN_WARNING "tty: Unable to return correct "
397 "speed data as your architecture needs updating.\n");
398 }
399#endif
400}
401EXPORT_SYMBOL_GPL(tty_termios_encode_baud_rate);
402
403/**
404 * tty_encode_baud_rate - set baud rate of the tty
405 * @ibaud: input baud rate
406 * @obad: output baud rate
407 *
408 * Update the current termios data for the tty with the new speed
409 * settings. The caller must hold the termios_mutex for the tty in
410 * question.
411 */
412
413void tty_encode_baud_rate(struct tty_struct *tty, speed_t ibaud, speed_t obaud)
414{
415 tty_termios_encode_baud_rate(tty->termios, ibaud, obaud);
416}
417EXPORT_SYMBOL_GPL(tty_encode_baud_rate);
418
419/**
420 * tty_get_baud_rate - get tty bit rates
421 * @tty: tty to query
422 *
423 * Returns the baud rate as an integer for this terminal. The
424 * termios lock must be held by the caller and the terminal bit
425 * flags may be updated.
426 *
427 * Locking: none
428 */
429
430speed_t tty_get_baud_rate(struct tty_struct *tty)
431{
432 speed_t baud = tty_termios_baud_rate(tty->termios);
433
434 if (baud == 38400 && tty->alt_speed) {
435 if (!tty->warned) {
436 printk(KERN_WARNING "Use of setserial/setrocket to "
437 "set SPD_* flags is deprecated\n");
438 tty->warned = 1;
439 }
440 baud = tty->alt_speed;
441 }
442
443 return baud;
444}
445EXPORT_SYMBOL(tty_get_baud_rate);
446
447/**
448 * tty_termios_copy_hw - copy hardware settings
449 * @new: New termios
450 * @old: Old termios
451 *
452 * Propogate the hardware specific terminal setting bits from
453 * the old termios structure to the new one. This is used in cases
454 * where the hardware does not support reconfiguration or as a helper
455 * in some cases where only minimal reconfiguration is supported
456 */
457
458void tty_termios_copy_hw(struct ktermios *new, struct ktermios *old)
459{
460 /* The bits a dumb device handles in software. Smart devices need
461 to always provide a set_termios method */
462 new->c_cflag &= HUPCL | CREAD | CLOCAL;
463 new->c_cflag |= old->c_cflag & ~(HUPCL | CREAD | CLOCAL);
464 new->c_ispeed = old->c_ispeed;
465 new->c_ospeed = old->c_ospeed;
466}
467EXPORT_SYMBOL(tty_termios_copy_hw);
468
469/**
470 * tty_termios_hw_change - check for setting change
471 * @a: termios
472 * @b: termios to compare
473 *
474 * Check if any of the bits that affect a dumb device have changed
475 * between the two termios structures, or a speed change is needed.
476 */
477
478int tty_termios_hw_change(struct ktermios *a, struct ktermios *b)
479{
480 if (a->c_ispeed != b->c_ispeed || a->c_ospeed != b->c_ospeed)
481 return 1;
482 if ((a->c_cflag ^ b->c_cflag) & ~(HUPCL | CREAD | CLOCAL))
483 return 1;
484 return 0;
485}
486EXPORT_SYMBOL(tty_termios_hw_change);
487
488/**
489 * change_termios - update termios values
490 * @tty: tty to update
491 * @new_termios: desired new value
492 *
493 * Perform updates to the termios values set on this terminal. There
494 * is a bit of layering violation here with n_tty in terms of the
495 * internal knowledge of this function.
496 *
497 * Locking: termios_mutex
498 */
499
500static void change_termios(struct tty_struct *tty, struct ktermios *new_termios)
501{
502 struct ktermios old_termios;
503 struct tty_ldisc *ld;
504 unsigned long flags;
505
506 /*
507 * Perform the actual termios internal changes under lock.
508 */
509
510
511 /* FIXME: we need to decide on some locking/ordering semantics
512 for the set_termios notification eventually */
513 mutex_lock(&tty->termios_mutex);
514 old_termios = *tty->termios;
515 *tty->termios = *new_termios;
516 unset_locked_termios(tty->termios, &old_termios, tty->termios_locked);
517
518 /* See if packet mode change of state. */
519 if (tty->link && tty->link->packet) {
520 int extproc = (old_termios.c_lflag & EXTPROC) |
521 (tty->termios->c_lflag & EXTPROC);
522 int old_flow = ((old_termios.c_iflag & IXON) &&
523 (old_termios.c_cc[VSTOP] == '\023') &&
524 (old_termios.c_cc[VSTART] == '\021'));
525 int new_flow = (I_IXON(tty) &&
526 STOP_CHAR(tty) == '\023' &&
527 START_CHAR(tty) == '\021');
528 if ((old_flow != new_flow) || extproc) {
529 spin_lock_irqsave(&tty->ctrl_lock, flags);
530 if (old_flow != new_flow) {
531 tty->ctrl_status &= ~(TIOCPKT_DOSTOP | TIOCPKT_NOSTOP);
532 if (new_flow)
533 tty->ctrl_status |= TIOCPKT_DOSTOP;
534 else
535 tty->ctrl_status |= TIOCPKT_NOSTOP;
536 }
537 if (extproc)
538 tty->ctrl_status |= TIOCPKT_IOCTL;
539 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
540 wake_up_interruptible(&tty->link->read_wait);
541 }
542 }
543
544 if (tty->ops->set_termios)
545 (*tty->ops->set_termios)(tty, &old_termios);
546 else
547 tty_termios_copy_hw(tty->termios, &old_termios);
548
549 ld = tty_ldisc_ref(tty);
550 if (ld != NULL) {
551 if (ld->ops->set_termios)
552 (ld->ops->set_termios)(tty, &old_termios);
553 tty_ldisc_deref(ld);
554 }
555 mutex_unlock(&tty->termios_mutex);
556}
557
558/**
559 * set_termios - set termios values for a tty
560 * @tty: terminal device
561 * @arg: user data
562 * @opt: option information
563 *
564 * Helper function to prepare termios data and run necessary other
565 * functions before using change_termios to do the actual changes.
566 *
567 * Locking:
568 * Called functions take ldisc and termios_mutex locks
569 */
570
571static int set_termios(struct tty_struct *tty, void __user *arg, int opt)
572{
573 struct ktermios tmp_termios;
574 struct tty_ldisc *ld;
575 int retval = tty_check_change(tty);
576
577 if (retval)
578 return retval;
579
580 mutex_lock(&tty->termios_mutex);
581 memcpy(&tmp_termios, tty->termios, sizeof(struct ktermios));
582 mutex_unlock(&tty->termios_mutex);
583
584 if (opt & TERMIOS_TERMIO) {
585 if (user_termio_to_kernel_termios(&tmp_termios,
586 (struct termio __user *)arg))
587 return -EFAULT;
588#ifdef TCGETS2
589 } else if (opt & TERMIOS_OLD) {
590 if (user_termios_to_kernel_termios_1(&tmp_termios,
591 (struct termios __user *)arg))
592 return -EFAULT;
593 } else {
594 if (user_termios_to_kernel_termios(&tmp_termios,
595 (struct termios2 __user *)arg))
596 return -EFAULT;
597 }
598#else
599 } else if (user_termios_to_kernel_termios(&tmp_termios,
600 (struct termios __user *)arg))
601 return -EFAULT;
602#endif
603
604 /* If old style Bfoo values are used then load c_ispeed/c_ospeed
605 * with the real speed so its unconditionally usable */
606 tmp_termios.c_ispeed = tty_termios_input_baud_rate(&tmp_termios);
607 tmp_termios.c_ospeed = tty_termios_baud_rate(&tmp_termios);
608
609 ld = tty_ldisc_ref(tty);
610
611 if (ld != NULL) {
612 if ((opt & TERMIOS_FLUSH) && ld->ops->flush_buffer)
613 ld->ops->flush_buffer(tty);
614 tty_ldisc_deref(ld);
615 }
616
617 if (opt & TERMIOS_WAIT) {
618 tty_wait_until_sent(tty, 0);
619 if (signal_pending(current))
620 return -EINTR;
621 }
622
623 change_termios(tty, &tmp_termios);
624
625 /* FIXME: Arguably if tmp_termios == tty->termios AND the
626 actual requested termios was not tmp_termios then we may
627 want to return an error as no user requested change has
628 succeeded */
629 return 0;
630}
631
632static void copy_termios(struct tty_struct *tty, struct ktermios *kterm)
633{
634 mutex_lock(&tty->termios_mutex);
635 memcpy(kterm, tty->termios, sizeof(struct ktermios));
636 mutex_unlock(&tty->termios_mutex);
637}
638
639static void copy_termios_locked(struct tty_struct *tty, struct ktermios *kterm)
640{
641 mutex_lock(&tty->termios_mutex);
642 memcpy(kterm, tty->termios_locked, sizeof(struct ktermios));
643 mutex_unlock(&tty->termios_mutex);
644}
645
646static int get_termio(struct tty_struct *tty, struct termio __user *termio)
647{
648 struct ktermios kterm;
649 copy_termios(tty, &kterm);
650 if (kernel_termios_to_user_termio(termio, &kterm))
651 return -EFAULT;
652 return 0;
653}
654
655
656#ifdef TCGETX
657
658/**
659 * set_termiox - set termiox fields if possible
660 * @tty: terminal
661 * @arg: termiox structure from user
662 * @opt: option flags for ioctl type
663 *
664 * Implement the device calling points for the SYS5 termiox ioctl
665 * interface in Linux
666 */
667
668static int set_termiox(struct tty_struct *tty, void __user *arg, int opt)
669{
670 struct termiox tnew;
671 struct tty_ldisc *ld;
672
673 if (tty->termiox == NULL)
674 return -EINVAL;
675 if (copy_from_user(&tnew, arg, sizeof(struct termiox)))
676 return -EFAULT;
677
678 ld = tty_ldisc_ref(tty);
679 if (ld != NULL) {
680 if ((opt & TERMIOS_FLUSH) && ld->ops->flush_buffer)
681 ld->ops->flush_buffer(tty);
682 tty_ldisc_deref(ld);
683 }
684 if (opt & TERMIOS_WAIT) {
685 tty_wait_until_sent(tty, 0);
686 if (signal_pending(current))
687 return -EINTR;
688 }
689
690 mutex_lock(&tty->termios_mutex);
691 if (tty->ops->set_termiox)
692 tty->ops->set_termiox(tty, &tnew);
693 mutex_unlock(&tty->termios_mutex);
694 return 0;
695}
696
697#endif
698
699
700#ifdef TIOCGETP
701/*
702 * These are deprecated, but there is limited support..
703 *
704 * The "sg_flags" translation is a joke..
705 */
706static int get_sgflags(struct tty_struct *tty)
707{
708 int flags = 0;
709
710 if (!(tty->termios->c_lflag & ICANON)) {
711 if (tty->termios->c_lflag & ISIG)
712 flags |= 0x02; /* cbreak */
713 else
714 flags |= 0x20; /* raw */
715 }
716 if (tty->termios->c_lflag & ECHO)
717 flags |= 0x08; /* echo */
718 if (tty->termios->c_oflag & OPOST)
719 if (tty->termios->c_oflag & ONLCR)
720 flags |= 0x10; /* crmod */
721 return flags;
722}
723
724static int get_sgttyb(struct tty_struct *tty, struct sgttyb __user *sgttyb)
725{
726 struct sgttyb tmp;
727
728 mutex_lock(&tty->termios_mutex);
729 tmp.sg_ispeed = tty->termios->c_ispeed;
730 tmp.sg_ospeed = tty->termios->c_ospeed;
731 tmp.sg_erase = tty->termios->c_cc[VERASE];
732 tmp.sg_kill = tty->termios->c_cc[VKILL];
733 tmp.sg_flags = get_sgflags(tty);
734 mutex_unlock(&tty->termios_mutex);
735
736 return copy_to_user(sgttyb, &tmp, sizeof(tmp)) ? -EFAULT : 0;
737}
738
739static void set_sgflags(struct ktermios *termios, int flags)
740{
741 termios->c_iflag = ICRNL | IXON;
742 termios->c_oflag = 0;
743 termios->c_lflag = ISIG | ICANON;
744 if (flags & 0x02) { /* cbreak */
745 termios->c_iflag = 0;
746 termios->c_lflag &= ~ICANON;
747 }
748 if (flags & 0x08) { /* echo */
749 termios->c_lflag |= ECHO | ECHOE | ECHOK |
750 ECHOCTL | ECHOKE | IEXTEN;
751 }
752 if (flags & 0x10) { /* crmod */
753 termios->c_oflag |= OPOST | ONLCR;
754 }
755 if (flags & 0x20) { /* raw */
756 termios->c_iflag = 0;
757 termios->c_lflag &= ~(ISIG | ICANON);
758 }
759 if (!(termios->c_lflag & ICANON)) {
760 termios->c_cc[VMIN] = 1;
761 termios->c_cc[VTIME] = 0;
762 }
763}
764
765/**
766 * set_sgttyb - set legacy terminal values
767 * @tty: tty structure
768 * @sgttyb: pointer to old style terminal structure
769 *
770 * Updates a terminal from the legacy BSD style terminal information
771 * structure.
772 *
773 * Locking: termios_mutex
774 */
775
776static int set_sgttyb(struct tty_struct *tty, struct sgttyb __user *sgttyb)
777{
778 int retval;
779 struct sgttyb tmp;
780 struct ktermios termios;
781
782 retval = tty_check_change(tty);
783 if (retval)
784 return retval;
785
786 if (copy_from_user(&tmp, sgttyb, sizeof(tmp)))
787 return -EFAULT;
788
789 mutex_lock(&tty->termios_mutex);
790 termios = *tty->termios;
791 termios.c_cc[VERASE] = tmp.sg_erase;
792 termios.c_cc[VKILL] = tmp.sg_kill;
793 set_sgflags(&termios, tmp.sg_flags);
794 /* Try and encode into Bfoo format */
795#ifdef BOTHER
796 tty_termios_encode_baud_rate(&termios, termios.c_ispeed,
797 termios.c_ospeed);
798#endif
799 mutex_unlock(&tty->termios_mutex);
800 change_termios(tty, &termios);
801 return 0;
802}
803#endif
804
805#ifdef TIOCGETC
806static int get_tchars(struct tty_struct *tty, struct tchars __user *tchars)
807{
808 struct tchars tmp;
809
810 mutex_lock(&tty->termios_mutex);
811 tmp.t_intrc = tty->termios->c_cc[VINTR];
812 tmp.t_quitc = tty->termios->c_cc[VQUIT];
813 tmp.t_startc = tty->termios->c_cc[VSTART];
814 tmp.t_stopc = tty->termios->c_cc[VSTOP];
815 tmp.t_eofc = tty->termios->c_cc[VEOF];
816 tmp.t_brkc = tty->termios->c_cc[VEOL2]; /* what is brkc anyway? */
817 mutex_unlock(&tty->termios_mutex);
818 return copy_to_user(tchars, &tmp, sizeof(tmp)) ? -EFAULT : 0;
819}
820
821static int set_tchars(struct tty_struct *tty, struct tchars __user *tchars)
822{
823 struct tchars tmp;
824
825 if (copy_from_user(&tmp, tchars, sizeof(tmp)))
826 return -EFAULT;
827 mutex_lock(&tty->termios_mutex);
828 tty->termios->c_cc[VINTR] = tmp.t_intrc;
829 tty->termios->c_cc[VQUIT] = tmp.t_quitc;
830 tty->termios->c_cc[VSTART] = tmp.t_startc;
831 tty->termios->c_cc[VSTOP] = tmp.t_stopc;
832 tty->termios->c_cc[VEOF] = tmp.t_eofc;
833 tty->termios->c_cc[VEOL2] = tmp.t_brkc; /* what is brkc anyway? */
834 mutex_unlock(&tty->termios_mutex);
835 return 0;
836}
837#endif
838
839#ifdef TIOCGLTC
840static int get_ltchars(struct tty_struct *tty, struct ltchars __user *ltchars)
841{
842 struct ltchars tmp;
843
844 mutex_lock(&tty->termios_mutex);
845 tmp.t_suspc = tty->termios->c_cc[VSUSP];
846 /* what is dsuspc anyway? */
847 tmp.t_dsuspc = tty->termios->c_cc[VSUSP];
848 tmp.t_rprntc = tty->termios->c_cc[VREPRINT];
849 /* what is flushc anyway? */
850 tmp.t_flushc = tty->termios->c_cc[VEOL2];
851 tmp.t_werasc = tty->termios->c_cc[VWERASE];
852 tmp.t_lnextc = tty->termios->c_cc[VLNEXT];
853 mutex_unlock(&tty->termios_mutex);
854 return copy_to_user(ltchars, &tmp, sizeof(tmp)) ? -EFAULT : 0;
855}
856
857static int set_ltchars(struct tty_struct *tty, struct ltchars __user *ltchars)
858{
859 struct ltchars tmp;
860
861 if (copy_from_user(&tmp, ltchars, sizeof(tmp)))
862 return -EFAULT;
863
864 mutex_lock(&tty->termios_mutex);
865 tty->termios->c_cc[VSUSP] = tmp.t_suspc;
866 /* what is dsuspc anyway? */
867 tty->termios->c_cc[VEOL2] = tmp.t_dsuspc;
868 tty->termios->c_cc[VREPRINT] = tmp.t_rprntc;
869 /* what is flushc anyway? */
870 tty->termios->c_cc[VEOL2] = tmp.t_flushc;
871 tty->termios->c_cc[VWERASE] = tmp.t_werasc;
872 tty->termios->c_cc[VLNEXT] = tmp.t_lnextc;
873 mutex_unlock(&tty->termios_mutex);
874 return 0;
875}
876#endif
877
878/**
879 * send_prio_char - send priority character
880 *
881 * Send a high priority character to the tty even if stopped
882 *
883 * Locking: none for xchar method, write ordering for write method.
884 */
885
886static int send_prio_char(struct tty_struct *tty, char ch)
887{
888 int was_stopped = tty->stopped;
889
890 if (tty->ops->send_xchar) {
891 tty->ops->send_xchar(tty, ch);
892 return 0;
893 }
894
895 if (tty_write_lock(tty, 0) < 0)
896 return -ERESTARTSYS;
897
898 if (was_stopped)
899 start_tty(tty);
900 tty->ops->write(tty, &ch, 1);
901 if (was_stopped)
902 stop_tty(tty);
903 tty_write_unlock(tty);
904 return 0;
905}
906
907/**
908 * tty_change_softcar - carrier change ioctl helper
909 * @tty: tty to update
910 * @arg: enable/disable CLOCAL
911 *
912 * Perform a change to the CLOCAL state and call into the driver
913 * layer to make it visible. All done with the termios mutex
914 */
915
916static int tty_change_softcar(struct tty_struct *tty, int arg)
917{
918 int ret = 0;
919 int bit = arg ? CLOCAL : 0;
920 struct ktermios old;
921
922 mutex_lock(&tty->termios_mutex);
923 old = *tty->termios;
924 tty->termios->c_cflag &= ~CLOCAL;
925 tty->termios->c_cflag |= bit;
926 if (tty->ops->set_termios)
927 tty->ops->set_termios(tty, &old);
928 if ((tty->termios->c_cflag & CLOCAL) != bit)
929 ret = -EINVAL;
930 mutex_unlock(&tty->termios_mutex);
931 return ret;
932}
933
934/**
935 * tty_mode_ioctl - mode related ioctls
936 * @tty: tty for the ioctl
937 * @file: file pointer for the tty
938 * @cmd: command
939 * @arg: ioctl argument
940 *
941 * Perform non line discipline specific mode control ioctls. This
942 * is designed to be called by line disciplines to ensure they provide
943 * consistent mode setting.
944 */
945
946int tty_mode_ioctl(struct tty_struct *tty, struct file *file,
947 unsigned int cmd, unsigned long arg)
948{
949 struct tty_struct *real_tty;
950 void __user *p = (void __user *)arg;
951 int ret = 0;
952 struct ktermios kterm;
953
954 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
955 tty->driver->subtype == PTY_TYPE_MASTER)
956 real_tty = tty->link;
957 else
958 real_tty = tty;
959
960 switch (cmd) {
961#ifdef TIOCGETP
962 case TIOCGETP:
963 return get_sgttyb(real_tty, (struct sgttyb __user *) arg);
964 case TIOCSETP:
965 case TIOCSETN:
966 return set_sgttyb(real_tty, (struct sgttyb __user *) arg);
967#endif
968#ifdef TIOCGETC
969 case TIOCGETC:
970 return get_tchars(real_tty, p);
971 case TIOCSETC:
972 return set_tchars(real_tty, p);
973#endif
974#ifdef TIOCGLTC
975 case TIOCGLTC:
976 return get_ltchars(real_tty, p);
977 case TIOCSLTC:
978 return set_ltchars(real_tty, p);
979#endif
980 case TCSETSF:
981 return set_termios(real_tty, p, TERMIOS_FLUSH | TERMIOS_WAIT | TERMIOS_OLD);
982 case TCSETSW:
983 return set_termios(real_tty, p, TERMIOS_WAIT | TERMIOS_OLD);
984 case TCSETS:
985 return set_termios(real_tty, p, TERMIOS_OLD);
986#ifndef TCGETS2
987 case TCGETS:
988 copy_termios(real_tty, &kterm);
989 if (kernel_termios_to_user_termios((struct termios __user *)arg, &kterm))
990 ret = -EFAULT;
991 return ret;
992#else
993 case TCGETS:
994 copy_termios(real_tty, &kterm);
995 if (kernel_termios_to_user_termios_1((struct termios __user *)arg, &kterm))
996 ret = -EFAULT;
997 return ret;
998 case TCGETS2:
999 copy_termios(real_tty, &kterm);
1000 if (kernel_termios_to_user_termios((struct termios2 __user *)arg, &kterm))
1001 ret = -EFAULT;
1002 return ret;
1003 case TCSETSF2:
1004 return set_termios(real_tty, p, TERMIOS_FLUSH | TERMIOS_WAIT);
1005 case TCSETSW2:
1006 return set_termios(real_tty, p, TERMIOS_WAIT);
1007 case TCSETS2:
1008 return set_termios(real_tty, p, 0);
1009#endif
1010 case TCGETA:
1011 return get_termio(real_tty, p);
1012 case TCSETAF:
1013 return set_termios(real_tty, p, TERMIOS_FLUSH | TERMIOS_WAIT | TERMIOS_TERMIO);
1014 case TCSETAW:
1015 return set_termios(real_tty, p, TERMIOS_WAIT | TERMIOS_TERMIO);
1016 case TCSETA:
1017 return set_termios(real_tty, p, TERMIOS_TERMIO);
1018#ifndef TCGETS2
1019 case TIOCGLCKTRMIOS:
1020 copy_termios_locked(real_tty, &kterm);
1021 if (kernel_termios_to_user_termios((struct termios __user *)arg, &kterm))
1022 ret = -EFAULT;
1023 return ret;
1024 case TIOCSLCKTRMIOS:
1025 if (!capable(CAP_SYS_ADMIN))
1026 return -EPERM;
1027 copy_termios_locked(real_tty, &kterm);
1028 if (user_termios_to_kernel_termios(&kterm,
1029 (struct termios __user *) arg))
1030 return -EFAULT;
1031 mutex_lock(&real_tty->termios_mutex);
1032 memcpy(real_tty->termios_locked, &kterm, sizeof(struct ktermios));
1033 mutex_unlock(&real_tty->termios_mutex);
1034 return 0;
1035#else
1036 case TIOCGLCKTRMIOS:
1037 copy_termios_locked(real_tty, &kterm);
1038 if (kernel_termios_to_user_termios_1((struct termios __user *)arg, &kterm))
1039 ret = -EFAULT;
1040 return ret;
1041 case TIOCSLCKTRMIOS:
1042 if (!capable(CAP_SYS_ADMIN))
1043 return -EPERM;
1044 copy_termios_locked(real_tty, &kterm);
1045 if (user_termios_to_kernel_termios_1(&kterm,
1046 (struct termios __user *) arg))
1047 return -EFAULT;
1048 mutex_lock(&real_tty->termios_mutex);
1049 memcpy(real_tty->termios_locked, &kterm, sizeof(struct ktermios));
1050 mutex_unlock(&real_tty->termios_mutex);
1051 return ret;
1052#endif
1053#ifdef TCGETX
1054 case TCGETX: {
1055 struct termiox ktermx;
1056 if (real_tty->termiox == NULL)
1057 return -EINVAL;
1058 mutex_lock(&real_tty->termios_mutex);
1059 memcpy(&ktermx, real_tty->termiox, sizeof(struct termiox));
1060 mutex_unlock(&real_tty->termios_mutex);
1061 if (copy_to_user(p, &ktermx, sizeof(struct termiox)))
1062 ret = -EFAULT;
1063 return ret;
1064 }
1065 case TCSETX:
1066 return set_termiox(real_tty, p, 0);
1067 case TCSETXW:
1068 return set_termiox(real_tty, p, TERMIOS_WAIT);
1069 case TCSETXF:
1070 return set_termiox(real_tty, p, TERMIOS_FLUSH);
1071#endif
1072 case TIOCGSOFTCAR:
1073 copy_termios(real_tty, &kterm);
1074 ret = put_user((kterm.c_cflag & CLOCAL) ? 1 : 0,
1075 (int __user *)arg);
1076 return ret;
1077 case TIOCSSOFTCAR:
1078 if (get_user(arg, (unsigned int __user *) arg))
1079 return -EFAULT;
1080 return tty_change_softcar(real_tty, arg);
1081 default:
1082 return -ENOIOCTLCMD;
1083 }
1084}
1085EXPORT_SYMBOL_GPL(tty_mode_ioctl);
1086
1087int tty_perform_flush(struct tty_struct *tty, unsigned long arg)
1088{
1089 struct tty_ldisc *ld;
1090 int retval = tty_check_change(tty);
1091 if (retval)
1092 return retval;
1093
1094 ld = tty_ldisc_ref_wait(tty);
1095 switch (arg) {
1096 case TCIFLUSH:
1097 if (ld && ld->ops->flush_buffer)
1098 ld->ops->flush_buffer(tty);
1099 break;
1100 case TCIOFLUSH:
1101 if (ld && ld->ops->flush_buffer)
1102 ld->ops->flush_buffer(tty);
1103 /* fall through */
1104 case TCOFLUSH:
1105 tty_driver_flush_buffer(tty);
1106 break;
1107 default:
1108 tty_ldisc_deref(ld);
1109 return -EINVAL;
1110 }
1111 tty_ldisc_deref(ld);
1112 return 0;
1113}
1114EXPORT_SYMBOL_GPL(tty_perform_flush);
1115
1116int n_tty_ioctl_helper(struct tty_struct *tty, struct file *file,
1117 unsigned int cmd, unsigned long arg)
1118{
1119 unsigned long flags;
1120 int retval;
1121
1122 switch (cmd) {
1123 case TCXONC:
1124 retval = tty_check_change(tty);
1125 if (retval)
1126 return retval;
1127 switch (arg) {
1128 case TCOOFF:
1129 if (!tty->flow_stopped) {
1130 tty->flow_stopped = 1;
1131 stop_tty(tty);
1132 }
1133 break;
1134 case TCOON:
1135 if (tty->flow_stopped) {
1136 tty->flow_stopped = 0;
1137 start_tty(tty);
1138 }
1139 break;
1140 case TCIOFF:
1141 if (STOP_CHAR(tty) != __DISABLED_CHAR)
1142 return send_prio_char(tty, STOP_CHAR(tty));
1143 break;
1144 case TCION:
1145 if (START_CHAR(tty) != __DISABLED_CHAR)
1146 return send_prio_char(tty, START_CHAR(tty));
1147 break;
1148 default:
1149 return -EINVAL;
1150 }
1151 return 0;
1152 case TCFLSH:
1153 return tty_perform_flush(tty, arg);
1154 case TIOCPKT:
1155 {
1156 int pktmode;
1157
1158 if (tty->driver->type != TTY_DRIVER_TYPE_PTY ||
1159 tty->driver->subtype != PTY_TYPE_MASTER)
1160 return -ENOTTY;
1161 if (get_user(pktmode, (int __user *) arg))
1162 return -EFAULT;
1163 spin_lock_irqsave(&tty->ctrl_lock, flags);
1164 if (pktmode) {
1165 if (!tty->packet) {
1166 tty->packet = 1;
1167 tty->link->ctrl_status = 0;
1168 }
1169 } else
1170 tty->packet = 0;
1171 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
1172 return 0;
1173 }
1174 default:
1175 /* Try the mode commands */
1176 return tty_mode_ioctl(tty, file, cmd, arg);
1177 }
1178}
1179EXPORT_SYMBOL(n_tty_ioctl_helper);
diff --git a/drivers/char/tty_ldisc.c b/drivers/char/tty_ldisc.c
deleted file mode 100644
index 412f9775d19c..000000000000
--- a/drivers/char/tty_ldisc.c
+++ /dev/null
@@ -1,915 +0,0 @@
1#include <linux/types.h>
2#include <linux/major.h>
3#include <linux/errno.h>
4#include <linux/signal.h>
5#include <linux/fcntl.h>
6#include <linux/sched.h>
7#include <linux/interrupt.h>
8#include <linux/tty.h>
9#include <linux/tty_driver.h>
10#include <linux/tty_flip.h>
11#include <linux/devpts_fs.h>
12#include <linux/file.h>
13#include <linux/console.h>
14#include <linux/timer.h>
15#include <linux/ctype.h>
16#include <linux/kd.h>
17#include <linux/mm.h>
18#include <linux/string.h>
19#include <linux/slab.h>
20#include <linux/poll.h>
21#include <linux/proc_fs.h>
22#include <linux/init.h>
23#include <linux/module.h>
24#include <linux/device.h>
25#include <linux/wait.h>
26#include <linux/bitops.h>
27#include <linux/delay.h>
28#include <linux/seq_file.h>
29
30#include <linux/uaccess.h>
31#include <asm/system.h>
32
33#include <linux/kbd_kern.h>
34#include <linux/vt_kern.h>
35#include <linux/selection.h>
36
37#include <linux/smp_lock.h> /* For the moment */
38
39#include <linux/kmod.h>
40#include <linux/nsproxy.h>
41
42/*
43 * This guards the refcounted line discipline lists. The lock
44 * must be taken with irqs off because there are hangup path
45 * callers who will do ldisc lookups and cannot sleep.
46 */
47
48static DEFINE_SPINLOCK(tty_ldisc_lock);
49static DECLARE_WAIT_QUEUE_HEAD(tty_ldisc_wait);
50/* Line disc dispatch table */
51static struct tty_ldisc_ops *tty_ldiscs[NR_LDISCS];
52
53static inline struct tty_ldisc *get_ldisc(struct tty_ldisc *ld)
54{
55 if (ld)
56 atomic_inc(&ld->users);
57 return ld;
58}
59
60static void put_ldisc(struct tty_ldisc *ld)
61{
62 unsigned long flags;
63
64 if (WARN_ON_ONCE(!ld))
65 return;
66
67 /*
68 * If this is the last user, free the ldisc, and
69 * release the ldisc ops.
70 *
71 * We really want an "atomic_dec_and_lock_irqsave()",
72 * but we don't have it, so this does it by hand.
73 */
74 local_irq_save(flags);
75 if (atomic_dec_and_lock(&ld->users, &tty_ldisc_lock)) {
76 struct tty_ldisc_ops *ldo = ld->ops;
77
78 ldo->refcount--;
79 module_put(ldo->owner);
80 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
81
82 kfree(ld);
83 return;
84 }
85 local_irq_restore(flags);
86}
87
88/**
89 * tty_register_ldisc - install a line discipline
90 * @disc: ldisc number
91 * @new_ldisc: pointer to the ldisc object
92 *
93 * Installs a new line discipline into the kernel. The discipline
94 * is set up as unreferenced and then made available to the kernel
95 * from this point onwards.
96 *
97 * Locking:
98 * takes tty_ldisc_lock to guard against ldisc races
99 */
100
101int tty_register_ldisc(int disc, struct tty_ldisc_ops *new_ldisc)
102{
103 unsigned long flags;
104 int ret = 0;
105
106 if (disc < N_TTY || disc >= NR_LDISCS)
107 return -EINVAL;
108
109 spin_lock_irqsave(&tty_ldisc_lock, flags);
110 tty_ldiscs[disc] = new_ldisc;
111 new_ldisc->num = disc;
112 new_ldisc->refcount = 0;
113 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
114
115 return ret;
116}
117EXPORT_SYMBOL(tty_register_ldisc);
118
119/**
120 * tty_unregister_ldisc - unload a line discipline
121 * @disc: ldisc number
122 * @new_ldisc: pointer to the ldisc object
123 *
124 * Remove a line discipline from the kernel providing it is not
125 * currently in use.
126 *
127 * Locking:
128 * takes tty_ldisc_lock to guard against ldisc races
129 */
130
131int tty_unregister_ldisc(int disc)
132{
133 unsigned long flags;
134 int ret = 0;
135
136 if (disc < N_TTY || disc >= NR_LDISCS)
137 return -EINVAL;
138
139 spin_lock_irqsave(&tty_ldisc_lock, flags);
140 if (tty_ldiscs[disc]->refcount)
141 ret = -EBUSY;
142 else
143 tty_ldiscs[disc] = NULL;
144 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
145
146 return ret;
147}
148EXPORT_SYMBOL(tty_unregister_ldisc);
149
150static struct tty_ldisc_ops *get_ldops(int disc)
151{
152 unsigned long flags;
153 struct tty_ldisc_ops *ldops, *ret;
154
155 spin_lock_irqsave(&tty_ldisc_lock, flags);
156 ret = ERR_PTR(-EINVAL);
157 ldops = tty_ldiscs[disc];
158 if (ldops) {
159 ret = ERR_PTR(-EAGAIN);
160 if (try_module_get(ldops->owner)) {
161 ldops->refcount++;
162 ret = ldops;
163 }
164 }
165 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
166 return ret;
167}
168
169static void put_ldops(struct tty_ldisc_ops *ldops)
170{
171 unsigned long flags;
172
173 spin_lock_irqsave(&tty_ldisc_lock, flags);
174 ldops->refcount--;
175 module_put(ldops->owner);
176 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
177}
178
179/**
180 * tty_ldisc_get - take a reference to an ldisc
181 * @disc: ldisc number
182 *
183 * Takes a reference to a line discipline. Deals with refcounts and
184 * module locking counts. Returns NULL if the discipline is not available.
185 * Returns a pointer to the discipline and bumps the ref count if it is
186 * available
187 *
188 * Locking:
189 * takes tty_ldisc_lock to guard against ldisc races
190 */
191
192static struct tty_ldisc *tty_ldisc_get(int disc)
193{
194 struct tty_ldisc *ld;
195 struct tty_ldisc_ops *ldops;
196
197 if (disc < N_TTY || disc >= NR_LDISCS)
198 return ERR_PTR(-EINVAL);
199
200 /*
201 * Get the ldisc ops - we may need to request them to be loaded
202 * dynamically and try again.
203 */
204 ldops = get_ldops(disc);
205 if (IS_ERR(ldops)) {
206 request_module("tty-ldisc-%d", disc);
207 ldops = get_ldops(disc);
208 if (IS_ERR(ldops))
209 return ERR_CAST(ldops);
210 }
211
212 ld = kmalloc(sizeof(struct tty_ldisc), GFP_KERNEL);
213 if (ld == NULL) {
214 put_ldops(ldops);
215 return ERR_PTR(-ENOMEM);
216 }
217
218 ld->ops = ldops;
219 atomic_set(&ld->users, 1);
220 return ld;
221}
222
223static void *tty_ldiscs_seq_start(struct seq_file *m, loff_t *pos)
224{
225 return (*pos < NR_LDISCS) ? pos : NULL;
226}
227
228static void *tty_ldiscs_seq_next(struct seq_file *m, void *v, loff_t *pos)
229{
230 (*pos)++;
231 return (*pos < NR_LDISCS) ? pos : NULL;
232}
233
234static void tty_ldiscs_seq_stop(struct seq_file *m, void *v)
235{
236}
237
238static int tty_ldiscs_seq_show(struct seq_file *m, void *v)
239{
240 int i = *(loff_t *)v;
241 struct tty_ldisc_ops *ldops;
242
243 ldops = get_ldops(i);
244 if (IS_ERR(ldops))
245 return 0;
246 seq_printf(m, "%-10s %2d\n", ldops->name ? ldops->name : "???", i);
247 put_ldops(ldops);
248 return 0;
249}
250
251static const struct seq_operations tty_ldiscs_seq_ops = {
252 .start = tty_ldiscs_seq_start,
253 .next = tty_ldiscs_seq_next,
254 .stop = tty_ldiscs_seq_stop,
255 .show = tty_ldiscs_seq_show,
256};
257
258static int proc_tty_ldiscs_open(struct inode *inode, struct file *file)
259{
260 return seq_open(file, &tty_ldiscs_seq_ops);
261}
262
263const struct file_operations tty_ldiscs_proc_fops = {
264 .owner = THIS_MODULE,
265 .open = proc_tty_ldiscs_open,
266 .read = seq_read,
267 .llseek = seq_lseek,
268 .release = seq_release,
269};
270
271/**
272 * tty_ldisc_assign - set ldisc on a tty
273 * @tty: tty to assign
274 * @ld: line discipline
275 *
276 * Install an instance of a line discipline into a tty structure. The
277 * ldisc must have a reference count above zero to ensure it remains.
278 * The tty instance refcount starts at zero.
279 *
280 * Locking:
281 * Caller must hold references
282 */
283
284static void tty_ldisc_assign(struct tty_struct *tty, struct tty_ldisc *ld)
285{
286 tty->ldisc = ld;
287}
288
289/**
290 * tty_ldisc_try - internal helper
291 * @tty: the tty
292 *
293 * Make a single attempt to grab and bump the refcount on
294 * the tty ldisc. Return 0 on failure or 1 on success. This is
295 * used to implement both the waiting and non waiting versions
296 * of tty_ldisc_ref
297 *
298 * Locking: takes tty_ldisc_lock
299 */
300
301static struct tty_ldisc *tty_ldisc_try(struct tty_struct *tty)
302{
303 unsigned long flags;
304 struct tty_ldisc *ld;
305
306 spin_lock_irqsave(&tty_ldisc_lock, flags);
307 ld = NULL;
308 if (test_bit(TTY_LDISC, &tty->flags))
309 ld = get_ldisc(tty->ldisc);
310 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
311 return ld;
312}
313
314/**
315 * tty_ldisc_ref_wait - wait for the tty ldisc
316 * @tty: tty device
317 *
318 * Dereference the line discipline for the terminal and take a
319 * reference to it. If the line discipline is in flux then
320 * wait patiently until it changes.
321 *
322 * Note: Must not be called from an IRQ/timer context. The caller
323 * must also be careful not to hold other locks that will deadlock
324 * against a discipline change, such as an existing ldisc reference
325 * (which we check for)
326 *
327 * Locking: call functions take tty_ldisc_lock
328 */
329
330struct tty_ldisc *tty_ldisc_ref_wait(struct tty_struct *tty)
331{
332 struct tty_ldisc *ld;
333
334 /* wait_event is a macro */
335 wait_event(tty_ldisc_wait, (ld = tty_ldisc_try(tty)) != NULL);
336 return ld;
337}
338EXPORT_SYMBOL_GPL(tty_ldisc_ref_wait);
339
340/**
341 * tty_ldisc_ref - get the tty ldisc
342 * @tty: tty device
343 *
344 * Dereference the line discipline for the terminal and take a
345 * reference to it. If the line discipline is in flux then
346 * return NULL. Can be called from IRQ and timer functions.
347 *
348 * Locking: called functions take tty_ldisc_lock
349 */
350
351struct tty_ldisc *tty_ldisc_ref(struct tty_struct *tty)
352{
353 return tty_ldisc_try(tty);
354}
355EXPORT_SYMBOL_GPL(tty_ldisc_ref);
356
357/**
358 * tty_ldisc_deref - free a tty ldisc reference
359 * @ld: reference to free up
360 *
361 * Undoes the effect of tty_ldisc_ref or tty_ldisc_ref_wait. May
362 * be called in IRQ context.
363 *
364 * Locking: takes tty_ldisc_lock
365 */
366
367void tty_ldisc_deref(struct tty_ldisc *ld)
368{
369 put_ldisc(ld);
370}
371EXPORT_SYMBOL_GPL(tty_ldisc_deref);
372
373static inline void tty_ldisc_put(struct tty_ldisc *ld)
374{
375 put_ldisc(ld);
376}
377
378/**
379 * tty_ldisc_enable - allow ldisc use
380 * @tty: terminal to activate ldisc on
381 *
382 * Set the TTY_LDISC flag when the line discipline can be called
383 * again. Do necessary wakeups for existing sleepers. Clear the LDISC
384 * changing flag to indicate any ldisc change is now over.
385 *
386 * Note: nobody should set the TTY_LDISC bit except via this function.
387 * Clearing directly is allowed.
388 */
389
390void tty_ldisc_enable(struct tty_struct *tty)
391{
392 set_bit(TTY_LDISC, &tty->flags);
393 clear_bit(TTY_LDISC_CHANGING, &tty->flags);
394 wake_up(&tty_ldisc_wait);
395}
396
397/**
398 * tty_ldisc_flush - flush line discipline queue
399 * @tty: tty
400 *
401 * Flush the line discipline queue (if any) for this tty. If there
402 * is no line discipline active this is a no-op.
403 */
404
405void tty_ldisc_flush(struct tty_struct *tty)
406{
407 struct tty_ldisc *ld = tty_ldisc_ref(tty);
408 if (ld) {
409 if (ld->ops->flush_buffer)
410 ld->ops->flush_buffer(tty);
411 tty_ldisc_deref(ld);
412 }
413 tty_buffer_flush(tty);
414}
415EXPORT_SYMBOL_GPL(tty_ldisc_flush);
416
417/**
418 * tty_set_termios_ldisc - set ldisc field
419 * @tty: tty structure
420 * @num: line discipline number
421 *
422 * This is probably overkill for real world processors but
423 * they are not on hot paths so a little discipline won't do
424 * any harm.
425 *
426 * Locking: takes termios_mutex
427 */
428
429static void tty_set_termios_ldisc(struct tty_struct *tty, int num)
430{
431 mutex_lock(&tty->termios_mutex);
432 tty->termios->c_line = num;
433 mutex_unlock(&tty->termios_mutex);
434}
435
436/**
437 * tty_ldisc_open - open a line discipline
438 * @tty: tty we are opening the ldisc on
439 * @ld: discipline to open
440 *
441 * A helper opening method. Also a convenient debugging and check
442 * point.
443 *
444 * Locking: always called with BTM already held.
445 */
446
447static int tty_ldisc_open(struct tty_struct *tty, struct tty_ldisc *ld)
448{
449 WARN_ON(test_and_set_bit(TTY_LDISC_OPEN, &tty->flags));
450 if (ld->ops->open) {
451 int ret;
452 /* BTM here locks versus a hangup event */
453 WARN_ON(!tty_locked());
454 ret = ld->ops->open(tty);
455 return ret;
456 }
457 return 0;
458}
459
460/**
461 * tty_ldisc_close - close a line discipline
462 * @tty: tty we are opening the ldisc on
463 * @ld: discipline to close
464 *
465 * A helper close method. Also a convenient debugging and check
466 * point.
467 */
468
469static void tty_ldisc_close(struct tty_struct *tty, struct tty_ldisc *ld)
470{
471 WARN_ON(!test_bit(TTY_LDISC_OPEN, &tty->flags));
472 clear_bit(TTY_LDISC_OPEN, &tty->flags);
473 if (ld->ops->close)
474 ld->ops->close(tty);
475}
476
477/**
478 * tty_ldisc_restore - helper for tty ldisc change
479 * @tty: tty to recover
480 * @old: previous ldisc
481 *
482 * Restore the previous line discipline or N_TTY when a line discipline
483 * change fails due to an open error
484 */
485
486static void tty_ldisc_restore(struct tty_struct *tty, struct tty_ldisc *old)
487{
488 char buf[64];
489 struct tty_ldisc *new_ldisc;
490 int r;
491
492 /* There is an outstanding reference here so this is safe */
493 old = tty_ldisc_get(old->ops->num);
494 WARN_ON(IS_ERR(old));
495 tty_ldisc_assign(tty, old);
496 tty_set_termios_ldisc(tty, old->ops->num);
497 if (tty_ldisc_open(tty, old) < 0) {
498 tty_ldisc_put(old);
499 /* This driver is always present */
500 new_ldisc = tty_ldisc_get(N_TTY);
501 if (IS_ERR(new_ldisc))
502 panic("n_tty: get");
503 tty_ldisc_assign(tty, new_ldisc);
504 tty_set_termios_ldisc(tty, N_TTY);
505 r = tty_ldisc_open(tty, new_ldisc);
506 if (r < 0)
507 panic("Couldn't open N_TTY ldisc for "
508 "%s --- error %d.",
509 tty_name(tty, buf), r);
510 }
511}
512
513/**
514 * tty_ldisc_halt - shut down the line discipline
515 * @tty: tty device
516 *
517 * Shut down the line discipline and work queue for this tty device.
518 * The TTY_LDISC flag being cleared ensures no further references can
519 * be obtained while the delayed work queue halt ensures that no more
520 * data is fed to the ldisc.
521 *
522 * You need to do a 'flush_scheduled_work()' (outside the ldisc_mutex)
523 * in order to make sure any currently executing ldisc work is also
524 * flushed.
525 */
526
527static int tty_ldisc_halt(struct tty_struct *tty)
528{
529 clear_bit(TTY_LDISC, &tty->flags);
530 return cancel_delayed_work_sync(&tty->buf.work);
531}
532
533/**
534 * tty_set_ldisc - set line discipline
535 * @tty: the terminal to set
536 * @ldisc: the line discipline
537 *
538 * Set the discipline of a tty line. Must be called from a process
539 * context. The ldisc change logic has to protect itself against any
540 * overlapping ldisc change (including on the other end of pty pairs),
541 * the close of one side of a tty/pty pair, and eventually hangup.
542 *
543 * Locking: takes tty_ldisc_lock, termios_mutex
544 */
545
546int tty_set_ldisc(struct tty_struct *tty, int ldisc)
547{
548 int retval;
549 struct tty_ldisc *o_ldisc, *new_ldisc;
550 int work, o_work = 0;
551 struct tty_struct *o_tty;
552
553 new_ldisc = tty_ldisc_get(ldisc);
554 if (IS_ERR(new_ldisc))
555 return PTR_ERR(new_ldisc);
556
557 tty_lock();
558 /*
559 * We need to look at the tty locking here for pty/tty pairs
560 * when both sides try to change in parallel.
561 */
562
563 o_tty = tty->link; /* o_tty is the pty side or NULL */
564
565
566 /*
567 * Check the no-op case
568 */
569
570 if (tty->ldisc->ops->num == ldisc) {
571 tty_unlock();
572 tty_ldisc_put(new_ldisc);
573 return 0;
574 }
575
576 tty_unlock();
577 /*
578 * Problem: What do we do if this blocks ?
579 * We could deadlock here
580 */
581
582 tty_wait_until_sent(tty, 0);
583
584 tty_lock();
585 mutex_lock(&tty->ldisc_mutex);
586
587 /*
588 * We could be midstream of another ldisc change which has
589 * dropped the lock during processing. If so we need to wait.
590 */
591
592 while (test_bit(TTY_LDISC_CHANGING, &tty->flags)) {
593 mutex_unlock(&tty->ldisc_mutex);
594 tty_unlock();
595 wait_event(tty_ldisc_wait,
596 test_bit(TTY_LDISC_CHANGING, &tty->flags) == 0);
597 tty_lock();
598 mutex_lock(&tty->ldisc_mutex);
599 }
600
601 set_bit(TTY_LDISC_CHANGING, &tty->flags);
602
603 /*
604 * No more input please, we are switching. The new ldisc
605 * will update this value in the ldisc open function
606 */
607
608 tty->receive_room = 0;
609
610 o_ldisc = tty->ldisc;
611
612 tty_unlock();
613 /*
614 * Make sure we don't change while someone holds a
615 * reference to the line discipline. The TTY_LDISC bit
616 * prevents anyone taking a reference once it is clear.
617 * We need the lock to avoid racing reference takers.
618 *
619 * We must clear the TTY_LDISC bit here to avoid a livelock
620 * with a userspace app continually trying to use the tty in
621 * parallel to the change and re-referencing the tty.
622 */
623
624 work = tty_ldisc_halt(tty);
625 if (o_tty)
626 o_work = tty_ldisc_halt(o_tty);
627
628 /*
629 * Wait for ->hangup_work and ->buf.work handlers to terminate.
630 * We must drop the mutex here in case a hangup is also in process.
631 */
632
633 mutex_unlock(&tty->ldisc_mutex);
634
635 flush_scheduled_work();
636
637 tty_lock();
638 mutex_lock(&tty->ldisc_mutex);
639 if (test_bit(TTY_HUPPED, &tty->flags)) {
640 /* We were raced by the hangup method. It will have stomped
641 the ldisc data and closed the ldisc down */
642 clear_bit(TTY_LDISC_CHANGING, &tty->flags);
643 mutex_unlock(&tty->ldisc_mutex);
644 tty_ldisc_put(new_ldisc);
645 tty_unlock();
646 return -EIO;
647 }
648
649 /* Shutdown the current discipline. */
650 tty_ldisc_close(tty, o_ldisc);
651
652 /* Now set up the new line discipline. */
653 tty_ldisc_assign(tty, new_ldisc);
654 tty_set_termios_ldisc(tty, ldisc);
655
656 retval = tty_ldisc_open(tty, new_ldisc);
657 if (retval < 0) {
658 /* Back to the old one or N_TTY if we can't */
659 tty_ldisc_put(new_ldisc);
660 tty_ldisc_restore(tty, o_ldisc);
661 }
662
663 /* At this point we hold a reference to the new ldisc and a
664 a reference to the old ldisc. If we ended up flipping back
665 to the existing ldisc we have two references to it */
666
667 if (tty->ldisc->ops->num != o_ldisc->ops->num && tty->ops->set_ldisc)
668 tty->ops->set_ldisc(tty);
669
670 tty_ldisc_put(o_ldisc);
671
672 /*
673 * Allow ldisc referencing to occur again
674 */
675
676 tty_ldisc_enable(tty);
677 if (o_tty)
678 tty_ldisc_enable(o_tty);
679
680 /* Restart the work queue in case no characters kick it off. Safe if
681 already running */
682 if (work)
683 schedule_delayed_work(&tty->buf.work, 1);
684 if (o_work)
685 schedule_delayed_work(&o_tty->buf.work, 1);
686 mutex_unlock(&tty->ldisc_mutex);
687 tty_unlock();
688 return retval;
689}
690
691/**
692 * tty_reset_termios - reset terminal state
693 * @tty: tty to reset
694 *
695 * Restore a terminal to the driver default state.
696 */
697
698static void tty_reset_termios(struct tty_struct *tty)
699{
700 mutex_lock(&tty->termios_mutex);
701 *tty->termios = tty->driver->init_termios;
702 tty->termios->c_ispeed = tty_termios_input_baud_rate(tty->termios);
703 tty->termios->c_ospeed = tty_termios_baud_rate(tty->termios);
704 mutex_unlock(&tty->termios_mutex);
705}
706
707
708/**
709 * tty_ldisc_reinit - reinitialise the tty ldisc
710 * @tty: tty to reinit
711 * @ldisc: line discipline to reinitialize
712 *
713 * Switch the tty to a line discipline and leave the ldisc
714 * state closed
715 */
716
717static void tty_ldisc_reinit(struct tty_struct *tty, int ldisc)
718{
719 struct tty_ldisc *ld;
720
721 tty_ldisc_close(tty, tty->ldisc);
722 tty_ldisc_put(tty->ldisc);
723 tty->ldisc = NULL;
724 /*
725 * Switch the line discipline back
726 */
727 ld = tty_ldisc_get(ldisc);
728 BUG_ON(IS_ERR(ld));
729 tty_ldisc_assign(tty, ld);
730 tty_set_termios_ldisc(tty, ldisc);
731}
732
733/**
734 * tty_ldisc_hangup - hangup ldisc reset
735 * @tty: tty being hung up
736 *
737 * Some tty devices reset their termios when they receive a hangup
738 * event. In that situation we must also switch back to N_TTY properly
739 * before we reset the termios data.
740 *
741 * Locking: We can take the ldisc mutex as the rest of the code is
742 * careful to allow for this.
743 *
744 * In the pty pair case this occurs in the close() path of the
745 * tty itself so we must be careful about locking rules.
746 */
747
748void tty_ldisc_hangup(struct tty_struct *tty)
749{
750 struct tty_ldisc *ld;
751 int reset = tty->driver->flags & TTY_DRIVER_RESET_TERMIOS;
752 int err = 0;
753
754 /*
755 * FIXME! What are the locking issues here? This may me overdoing
756 * things... This question is especially important now that we've
757 * removed the irqlock.
758 */
759 ld = tty_ldisc_ref(tty);
760 if (ld != NULL) {
761 /* We may have no line discipline at this point */
762 if (ld->ops->flush_buffer)
763 ld->ops->flush_buffer(tty);
764 tty_driver_flush_buffer(tty);
765 if ((test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) &&
766 ld->ops->write_wakeup)
767 ld->ops->write_wakeup(tty);
768 if (ld->ops->hangup)
769 ld->ops->hangup(tty);
770 tty_ldisc_deref(ld);
771 }
772 /*
773 * FIXME: Once we trust the LDISC code better we can wait here for
774 * ldisc completion and fix the driver call race
775 */
776 wake_up_interruptible_poll(&tty->write_wait, POLLOUT);
777 wake_up_interruptible_poll(&tty->read_wait, POLLIN);
778 /*
779 * Shutdown the current line discipline, and reset it to
780 * N_TTY if need be.
781 *
782 * Avoid racing set_ldisc or tty_ldisc_release
783 */
784 mutex_lock(&tty->ldisc_mutex);
785
786 /*
787 * this is like tty_ldisc_halt, but we need to give up
788 * the BTM before calling cancel_delayed_work_sync,
789 * which may need to wait for another function taking the BTM
790 */
791 clear_bit(TTY_LDISC, &tty->flags);
792 tty_unlock();
793 cancel_delayed_work_sync(&tty->buf.work);
794 mutex_unlock(&tty->ldisc_mutex);
795
796 tty_lock();
797 mutex_lock(&tty->ldisc_mutex);
798
799 /* At this point we have a closed ldisc and we want to
800 reopen it. We could defer this to the next open but
801 it means auditing a lot of other paths so this is
802 a FIXME */
803 if (tty->ldisc) { /* Not yet closed */
804 if (reset == 0) {
805 tty_ldisc_reinit(tty, tty->termios->c_line);
806 err = tty_ldisc_open(tty, tty->ldisc);
807 }
808 /* If the re-open fails or we reset then go to N_TTY. The
809 N_TTY open cannot fail */
810 if (reset || err) {
811 tty_ldisc_reinit(tty, N_TTY);
812 WARN_ON(tty_ldisc_open(tty, tty->ldisc));
813 }
814 tty_ldisc_enable(tty);
815 }
816 mutex_unlock(&tty->ldisc_mutex);
817 if (reset)
818 tty_reset_termios(tty);
819}
820
821/**
822 * tty_ldisc_setup - open line discipline
823 * @tty: tty being shut down
824 * @o_tty: pair tty for pty/tty pairs
825 *
826 * Called during the initial open of a tty/pty pair in order to set up the
827 * line disciplines and bind them to the tty. This has no locking issues
828 * as the device isn't yet active.
829 */
830
831int tty_ldisc_setup(struct tty_struct *tty, struct tty_struct *o_tty)
832{
833 struct tty_ldisc *ld = tty->ldisc;
834 int retval;
835
836 retval = tty_ldisc_open(tty, ld);
837 if (retval)
838 return retval;
839
840 if (o_tty) {
841 retval = tty_ldisc_open(o_tty, o_tty->ldisc);
842 if (retval) {
843 tty_ldisc_close(tty, ld);
844 return retval;
845 }
846 tty_ldisc_enable(o_tty);
847 }
848 tty_ldisc_enable(tty);
849 return 0;
850}
851/**
852 * tty_ldisc_release - release line discipline
853 * @tty: tty being shut down
854 * @o_tty: pair tty for pty/tty pairs
855 *
856 * Called during the final close of a tty/pty pair in order to shut down
857 * the line discpline layer. On exit the ldisc assigned is N_TTY and the
858 * ldisc has not been opened.
859 */
860
861void tty_ldisc_release(struct tty_struct *tty, struct tty_struct *o_tty)
862{
863 /*
864 * Prevent flush_to_ldisc() from rescheduling the work for later. Then
865 * kill any delayed work. As this is the final close it does not
866 * race with the set_ldisc code path.
867 */
868
869 tty_unlock();
870 tty_ldisc_halt(tty);
871 flush_scheduled_work();
872 tty_lock();
873
874 mutex_lock(&tty->ldisc_mutex);
875 /*
876 * Now kill off the ldisc
877 */
878 tty_ldisc_close(tty, tty->ldisc);
879 tty_ldisc_put(tty->ldisc);
880 /* Force an oops if we mess this up */
881 tty->ldisc = NULL;
882
883 /* Ensure the next open requests the N_TTY ldisc */
884 tty_set_termios_ldisc(tty, N_TTY);
885 mutex_unlock(&tty->ldisc_mutex);
886
887 /* This will need doing differently if we need to lock */
888 if (o_tty)
889 tty_ldisc_release(o_tty, NULL);
890
891 /* And the memory resources remaining (buffers, termios) will be
892 disposed of when the kref hits zero */
893}
894
895/**
896 * tty_ldisc_init - ldisc setup for new tty
897 * @tty: tty being allocated
898 *
899 * Set up the line discipline objects for a newly allocated tty. Note that
900 * the tty structure is not completely set up when this call is made.
901 */
902
903void tty_ldisc_init(struct tty_struct *tty)
904{
905 struct tty_ldisc *ld = tty_ldisc_get(N_TTY);
906 if (IS_ERR(ld))
907 panic("n_tty: init_tty");
908 tty_ldisc_assign(tty, ld);
909}
910
911void tty_ldisc_begin(void)
912{
913 /* Setup the default TTY line discipline. */
914 (void) tty_register_ldisc(N_TTY, &tty_ldisc_N_TTY);
915}
diff --git a/drivers/char/tty_mutex.c b/drivers/char/tty_mutex.c
deleted file mode 100644
index 133697540c73..000000000000
--- a/drivers/char/tty_mutex.c
+++ /dev/null
@@ -1,47 +0,0 @@
1/*
2 * drivers/char/tty_lock.c
3 */
4#include <linux/tty.h>
5#include <linux/module.h>
6#include <linux/kallsyms.h>
7#include <linux/semaphore.h>
8#include <linux/sched.h>
9
10/*
11 * The 'big tty mutex'
12 *
13 * This mutex is taken and released by tty_lock() and tty_unlock(),
14 * replacing the older big kernel lock.
15 * It can no longer be taken recursively, and does not get
16 * released implicitly while sleeping.
17 *
18 * Don't use in new code.
19 */
20static DEFINE_MUTEX(big_tty_mutex);
21struct task_struct *__big_tty_mutex_owner;
22EXPORT_SYMBOL_GPL(__big_tty_mutex_owner);
23
24/*
25 * Getting the big tty mutex.
26 */
27void __lockfunc tty_lock(void)
28{
29 struct task_struct *task = current;
30
31 WARN_ON(__big_tty_mutex_owner == task);
32
33 mutex_lock(&big_tty_mutex);
34 __big_tty_mutex_owner = task;
35}
36EXPORT_SYMBOL(tty_lock);
37
38void __lockfunc tty_unlock(void)
39{
40 struct task_struct *task = current;
41
42 WARN_ON(__big_tty_mutex_owner != task);
43 __big_tty_mutex_owner = NULL;
44
45 mutex_unlock(&big_tty_mutex);
46}
47EXPORT_SYMBOL(tty_unlock);
diff --git a/drivers/char/tty_port.c b/drivers/char/tty_port.c
deleted file mode 100644
index 33d37d230f8f..000000000000
--- a/drivers/char/tty_port.c
+++ /dev/null
@@ -1,446 +0,0 @@
1/*
2 * Tty port functions
3 */
4
5#include <linux/types.h>
6#include <linux/errno.h>
7#include <linux/tty.h>
8#include <linux/tty_driver.h>
9#include <linux/tty_flip.h>
10#include <linux/serial.h>
11#include <linux/timer.h>
12#include <linux/string.h>
13#include <linux/slab.h>
14#include <linux/sched.h>
15#include <linux/init.h>
16#include <linux/wait.h>
17#include <linux/bitops.h>
18#include <linux/delay.h>
19#include <linux/module.h>
20
21void tty_port_init(struct tty_port *port)
22{
23 memset(port, 0, sizeof(*port));
24 init_waitqueue_head(&port->open_wait);
25 init_waitqueue_head(&port->close_wait);
26 init_waitqueue_head(&port->delta_msr_wait);
27 mutex_init(&port->mutex);
28 mutex_init(&port->buf_mutex);
29 spin_lock_init(&port->lock);
30 port->close_delay = (50 * HZ) / 100;
31 port->closing_wait = (3000 * HZ) / 100;
32 kref_init(&port->kref);
33}
34EXPORT_SYMBOL(tty_port_init);
35
36int tty_port_alloc_xmit_buf(struct tty_port *port)
37{
38 /* We may sleep in get_zeroed_page() */
39 mutex_lock(&port->buf_mutex);
40 if (port->xmit_buf == NULL)
41 port->xmit_buf = (unsigned char *)get_zeroed_page(GFP_KERNEL);
42 mutex_unlock(&port->buf_mutex);
43 if (port->xmit_buf == NULL)
44 return -ENOMEM;
45 return 0;
46}
47EXPORT_SYMBOL(tty_port_alloc_xmit_buf);
48
49void tty_port_free_xmit_buf(struct tty_port *port)
50{
51 mutex_lock(&port->buf_mutex);
52 if (port->xmit_buf != NULL) {
53 free_page((unsigned long)port->xmit_buf);
54 port->xmit_buf = NULL;
55 }
56 mutex_unlock(&port->buf_mutex);
57}
58EXPORT_SYMBOL(tty_port_free_xmit_buf);
59
60static void tty_port_destructor(struct kref *kref)
61{
62 struct tty_port *port = container_of(kref, struct tty_port, kref);
63 if (port->xmit_buf)
64 free_page((unsigned long)port->xmit_buf);
65 if (port->ops->destruct)
66 port->ops->destruct(port);
67 else
68 kfree(port);
69}
70
71void tty_port_put(struct tty_port *port)
72{
73 if (port)
74 kref_put(&port->kref, tty_port_destructor);
75}
76EXPORT_SYMBOL(tty_port_put);
77
78/**
79 * tty_port_tty_get - get a tty reference
80 * @port: tty port
81 *
82 * Return a refcount protected tty instance or NULL if the port is not
83 * associated with a tty (eg due to close or hangup)
84 */
85
86struct tty_struct *tty_port_tty_get(struct tty_port *port)
87{
88 unsigned long flags;
89 struct tty_struct *tty;
90
91 spin_lock_irqsave(&port->lock, flags);
92 tty = tty_kref_get(port->tty);
93 spin_unlock_irqrestore(&port->lock, flags);
94 return tty;
95}
96EXPORT_SYMBOL(tty_port_tty_get);
97
98/**
99 * tty_port_tty_set - set the tty of a port
100 * @port: tty port
101 * @tty: the tty
102 *
103 * Associate the port and tty pair. Manages any internal refcounts.
104 * Pass NULL to deassociate a port
105 */
106
107void tty_port_tty_set(struct tty_port *port, struct tty_struct *tty)
108{
109 unsigned long flags;
110
111 spin_lock_irqsave(&port->lock, flags);
112 if (port->tty)
113 tty_kref_put(port->tty);
114 port->tty = tty_kref_get(tty);
115 spin_unlock_irqrestore(&port->lock, flags);
116}
117EXPORT_SYMBOL(tty_port_tty_set);
118
119static void tty_port_shutdown(struct tty_port *port)
120{
121 mutex_lock(&port->mutex);
122 if (port->ops->shutdown && !port->console &&
123 test_and_clear_bit(ASYNCB_INITIALIZED, &port->flags))
124 port->ops->shutdown(port);
125 mutex_unlock(&port->mutex);
126}
127
128/**
129 * tty_port_hangup - hangup helper
130 * @port: tty port
131 *
132 * Perform port level tty hangup flag and count changes. Drop the tty
133 * reference.
134 */
135
136void tty_port_hangup(struct tty_port *port)
137{
138 unsigned long flags;
139
140 spin_lock_irqsave(&port->lock, flags);
141 port->count = 0;
142 port->flags &= ~ASYNC_NORMAL_ACTIVE;
143 if (port->tty) {
144 set_bit(TTY_IO_ERROR, &port->tty->flags);
145 tty_kref_put(port->tty);
146 }
147 port->tty = NULL;
148 spin_unlock_irqrestore(&port->lock, flags);
149 wake_up_interruptible(&port->open_wait);
150 wake_up_interruptible(&port->delta_msr_wait);
151 tty_port_shutdown(port);
152}
153EXPORT_SYMBOL(tty_port_hangup);
154
155/**
156 * tty_port_carrier_raised - carrier raised check
157 * @port: tty port
158 *
159 * Wrapper for the carrier detect logic. For the moment this is used
160 * to hide some internal details. This will eventually become entirely
161 * internal to the tty port.
162 */
163
164int tty_port_carrier_raised(struct tty_port *port)
165{
166 if (port->ops->carrier_raised == NULL)
167 return 1;
168 return port->ops->carrier_raised(port);
169}
170EXPORT_SYMBOL(tty_port_carrier_raised);
171
172/**
173 * tty_port_raise_dtr_rts - Raise DTR/RTS
174 * @port: tty port
175 *
176 * Wrapper for the DTR/RTS raise logic. For the moment this is used
177 * to hide some internal details. This will eventually become entirely
178 * internal to the tty port.
179 */
180
181void tty_port_raise_dtr_rts(struct tty_port *port)
182{
183 if (port->ops->dtr_rts)
184 port->ops->dtr_rts(port, 1);
185}
186EXPORT_SYMBOL(tty_port_raise_dtr_rts);
187
188/**
189 * tty_port_lower_dtr_rts - Lower DTR/RTS
190 * @port: tty port
191 *
192 * Wrapper for the DTR/RTS raise logic. For the moment this is used
193 * to hide some internal details. This will eventually become entirely
194 * internal to the tty port.
195 */
196
197void tty_port_lower_dtr_rts(struct tty_port *port)
198{
199 if (port->ops->dtr_rts)
200 port->ops->dtr_rts(port, 0);
201}
202EXPORT_SYMBOL(tty_port_lower_dtr_rts);
203
204/**
205 * tty_port_block_til_ready - Waiting logic for tty open
206 * @port: the tty port being opened
207 * @tty: the tty device being bound
208 * @filp: the file pointer of the opener
209 *
210 * Implement the core POSIX/SuS tty behaviour when opening a tty device.
211 * Handles:
212 * - hangup (both before and during)
213 * - non blocking open
214 * - rts/dtr/dcd
215 * - signals
216 * - port flags and counts
217 *
218 * The passed tty_port must implement the carrier_raised method if it can
219 * do carrier detect and the dtr_rts method if it supports software
220 * management of these lines. Note that the dtr/rts raise is done each
221 * iteration as a hangup may have previously dropped them while we wait.
222 */
223
224int tty_port_block_til_ready(struct tty_port *port,
225 struct tty_struct *tty, struct file *filp)
226{
227 int do_clocal = 0, retval;
228 unsigned long flags;
229 DEFINE_WAIT(wait);
230 int cd;
231
232 /* block if port is in the process of being closed */
233 if (tty_hung_up_p(filp) || port->flags & ASYNC_CLOSING) {
234 wait_event_interruptible_tty(port->close_wait,
235 !(port->flags & ASYNC_CLOSING));
236 if (port->flags & ASYNC_HUP_NOTIFY)
237 return -EAGAIN;
238 else
239 return -ERESTARTSYS;
240 }
241
242 /* if non-blocking mode is set we can pass directly to open unless
243 the port has just hung up or is in another error state */
244 if (tty->flags & (1 << TTY_IO_ERROR)) {
245 port->flags |= ASYNC_NORMAL_ACTIVE;
246 return 0;
247 }
248 if (filp->f_flags & O_NONBLOCK) {
249 /* Indicate we are open */
250 if (tty->termios->c_cflag & CBAUD)
251 tty_port_raise_dtr_rts(port);
252 port->flags |= ASYNC_NORMAL_ACTIVE;
253 return 0;
254 }
255
256 if (C_CLOCAL(tty))
257 do_clocal = 1;
258
259 /* Block waiting until we can proceed. We may need to wait for the
260 carrier, but we must also wait for any close that is in progress
261 before the next open may complete */
262
263 retval = 0;
264
265 /* The port lock protects the port counts */
266 spin_lock_irqsave(&port->lock, flags);
267 if (!tty_hung_up_p(filp))
268 port->count--;
269 port->blocked_open++;
270 spin_unlock_irqrestore(&port->lock, flags);
271
272 while (1) {
273 /* Indicate we are open */
274 if (tty->termios->c_cflag & CBAUD)
275 tty_port_raise_dtr_rts(port);
276
277 prepare_to_wait(&port->open_wait, &wait, TASK_INTERRUPTIBLE);
278 /* Check for a hangup or uninitialised port.
279 Return accordingly */
280 if (tty_hung_up_p(filp) || !(port->flags & ASYNC_INITIALIZED)) {
281 if (port->flags & ASYNC_HUP_NOTIFY)
282 retval = -EAGAIN;
283 else
284 retval = -ERESTARTSYS;
285 break;
286 }
287 /* Probe the carrier. For devices with no carrier detect this
288 will always return true */
289 cd = tty_port_carrier_raised(port);
290 if (!(port->flags & ASYNC_CLOSING) &&
291 (do_clocal || cd))
292 break;
293 if (signal_pending(current)) {
294 retval = -ERESTARTSYS;
295 break;
296 }
297 tty_unlock();
298 schedule();
299 tty_lock();
300 }
301 finish_wait(&port->open_wait, &wait);
302
303 /* Update counts. A parallel hangup will have set count to zero and
304 we must not mess that up further */
305 spin_lock_irqsave(&port->lock, flags);
306 if (!tty_hung_up_p(filp))
307 port->count++;
308 port->blocked_open--;
309 if (retval == 0)
310 port->flags |= ASYNC_NORMAL_ACTIVE;
311 spin_unlock_irqrestore(&port->lock, flags);
312 return retval;
313}
314EXPORT_SYMBOL(tty_port_block_til_ready);
315
316int tty_port_close_start(struct tty_port *port,
317 struct tty_struct *tty, struct file *filp)
318{
319 unsigned long flags;
320
321 spin_lock_irqsave(&port->lock, flags);
322 if (tty_hung_up_p(filp)) {
323 spin_unlock_irqrestore(&port->lock, flags);
324 return 0;
325 }
326
327 if (tty->count == 1 && port->count != 1) {
328 printk(KERN_WARNING
329 "tty_port_close_start: tty->count = 1 port count = %d.\n",
330 port->count);
331 port->count = 1;
332 }
333 if (--port->count < 0) {
334 printk(KERN_WARNING "tty_port_close_start: count = %d\n",
335 port->count);
336 port->count = 0;
337 }
338
339 if (port->count) {
340 spin_unlock_irqrestore(&port->lock, flags);
341 if (port->ops->drop)
342 port->ops->drop(port);
343 return 0;
344 }
345 set_bit(ASYNCB_CLOSING, &port->flags);
346 tty->closing = 1;
347 spin_unlock_irqrestore(&port->lock, flags);
348 /* Don't block on a stalled port, just pull the chain */
349 if (tty->flow_stopped)
350 tty_driver_flush_buffer(tty);
351 if (test_bit(ASYNCB_INITIALIZED, &port->flags) &&
352 port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
353 tty_wait_until_sent(tty, port->closing_wait);
354 if (port->drain_delay) {
355 unsigned int bps = tty_get_baud_rate(tty);
356 long timeout;
357
358 if (bps > 1200)
359 timeout = max_t(long,
360 (HZ * 10 * port->drain_delay) / bps, HZ / 10);
361 else
362 timeout = 2 * HZ;
363 schedule_timeout_interruptible(timeout);
364 }
365 /* Flush the ldisc buffering */
366 tty_ldisc_flush(tty);
367
368 /* Drop DTR/RTS if HUPCL is set. This causes any attached modem to
369 hang up the line */
370 if (tty->termios->c_cflag & HUPCL)
371 tty_port_lower_dtr_rts(port);
372
373 /* Don't call port->drop for the last reference. Callers will want
374 to drop the last active reference in ->shutdown() or the tty
375 shutdown path */
376 return 1;
377}
378EXPORT_SYMBOL(tty_port_close_start);
379
380void tty_port_close_end(struct tty_port *port, struct tty_struct *tty)
381{
382 unsigned long flags;
383
384 spin_lock_irqsave(&port->lock, flags);
385 tty->closing = 0;
386
387 if (port->blocked_open) {
388 spin_unlock_irqrestore(&port->lock, flags);
389 if (port->close_delay) {
390 msleep_interruptible(
391 jiffies_to_msecs(port->close_delay));
392 }
393 spin_lock_irqsave(&port->lock, flags);
394 wake_up_interruptible(&port->open_wait);
395 }
396 port->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
397 wake_up_interruptible(&port->close_wait);
398 spin_unlock_irqrestore(&port->lock, flags);
399}
400EXPORT_SYMBOL(tty_port_close_end);
401
402void tty_port_close(struct tty_port *port, struct tty_struct *tty,
403 struct file *filp)
404{
405 if (tty_port_close_start(port, tty, filp) == 0)
406 return;
407 tty_port_shutdown(port);
408 set_bit(TTY_IO_ERROR, &tty->flags);
409 tty_port_close_end(port, tty);
410 tty_port_tty_set(port, NULL);
411}
412EXPORT_SYMBOL(tty_port_close);
413
414int tty_port_open(struct tty_port *port, struct tty_struct *tty,
415 struct file *filp)
416{
417 spin_lock_irq(&port->lock);
418 if (!tty_hung_up_p(filp))
419 ++port->count;
420 spin_unlock_irq(&port->lock);
421 tty_port_tty_set(port, tty);
422
423 /*
424 * Do the device-specific open only if the hardware isn't
425 * already initialized. Serialize open and shutdown using the
426 * port mutex.
427 */
428
429 mutex_lock(&port->mutex);
430
431 if (!test_bit(ASYNCB_INITIALIZED, &port->flags)) {
432 clear_bit(TTY_IO_ERROR, &tty->flags);
433 if (port->ops->activate) {
434 int retval = port->ops->activate(port, tty);
435 if (retval) {
436 mutex_unlock(&port->mutex);
437 return retval;
438 }
439 }
440 set_bit(ASYNCB_INITIALIZED, &port->flags);
441 }
442 mutex_unlock(&port->mutex);
443 return tty_port_block_til_ready(port, tty, filp);
444}
445
446EXPORT_SYMBOL(tty_port_open);
diff --git a/drivers/char/ttyprintk.c b/drivers/char/ttyprintk.c
new file mode 100644
index 000000000000..a1f68af4ccf4
--- /dev/null
+++ b/drivers/char/ttyprintk.c
@@ -0,0 +1,225 @@
1/*
2 * linux/drivers/char/ttyprintk.c
3 *
4 * Copyright (C) 2010 Samo Pogacnik
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the smems of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 */
10
11/*
12 * This pseudo device allows user to make printk messages. It is possible
13 * to store "console" messages inline with kernel messages for better analyses
14 * of the boot process, for example.
15 */
16
17#include <linux/device.h>
18#include <linux/serial.h>
19#include <linux/tty.h>
20
21struct ttyprintk_port {
22 struct tty_port port;
23 struct mutex port_write_mutex;
24};
25
26static struct ttyprintk_port tpk_port;
27
28/*
29 * Our simple preformatting supports transparent output of (time-stamped)
30 * printk messages (also suitable for logging service):
31 * - any cr is replaced by nl
32 * - adds a ttyprintk source tag in front of each line
33 * - too long message is fragmeted, with '\'nl between fragments
34 * - TPK_STR_SIZE isn't really the write_room limiting factor, bcause
35 * it is emptied on the fly during preformatting.
36 */
37#define TPK_STR_SIZE 508 /* should be bigger then max expected line length */
38#define TPK_MAX_ROOM 4096 /* we could assume 4K for instance */
39static const char *tpk_tag = "[U] "; /* U for User */
40static int tpk_curr;
41
42static int tpk_printk(const unsigned char *buf, int count)
43{
44 static char tmp[TPK_STR_SIZE + 4];
45 int i = tpk_curr;
46
47 if (buf == NULL) {
48 /* flush tmp[] */
49 if (tpk_curr > 0) {
50 /* non nl or cr terminated message - add nl */
51 tmp[tpk_curr + 0] = '\n';
52 tmp[tpk_curr + 1] = '\0';
53 printk(KERN_INFO "%s%s", tpk_tag, tmp);
54 tpk_curr = 0;
55 }
56 return i;
57 }
58
59 for (i = 0; i < count; i++) {
60 tmp[tpk_curr] = buf[i];
61 if (tpk_curr < TPK_STR_SIZE) {
62 switch (buf[i]) {
63 case '\r':
64 /* replace cr with nl */
65 tmp[tpk_curr + 0] = '\n';
66 tmp[tpk_curr + 1] = '\0';
67 printk(KERN_INFO "%s%s", tpk_tag, tmp);
68 tpk_curr = 0;
69 if (buf[i + 1] == '\n')
70 i++;
71 break;
72 case '\n':
73 tmp[tpk_curr + 1] = '\0';
74 printk(KERN_INFO "%s%s", tpk_tag, tmp);
75 tpk_curr = 0;
76 break;
77 default:
78 tpk_curr++;
79 }
80 } else {
81 /* end of tmp buffer reached: cut the message in two */
82 tmp[tpk_curr + 1] = '\\';
83 tmp[tpk_curr + 2] = '\n';
84 tmp[tpk_curr + 3] = '\0';
85 printk(KERN_INFO "%s%s", tpk_tag, tmp);
86 tpk_curr = 0;
87 }
88 }
89
90 return count;
91}
92
93/*
94 * TTY operations open function.
95 */
96static int tpk_open(struct tty_struct *tty, struct file *filp)
97{
98 tty->driver_data = &tpk_port;
99
100 return tty_port_open(&tpk_port.port, tty, filp);
101}
102
103/*
104 * TTY operations close function.
105 */
106static void tpk_close(struct tty_struct *tty, struct file *filp)
107{
108 struct ttyprintk_port *tpkp = tty->driver_data;
109
110 mutex_lock(&tpkp->port_write_mutex);
111 /* flush tpk_printk buffer */
112 tpk_printk(NULL, 0);
113 mutex_unlock(&tpkp->port_write_mutex);
114
115 tty_port_close(&tpkp->port, tty, filp);
116}
117
118/*
119 * TTY operations write function.
120 */
121static int tpk_write(struct tty_struct *tty,
122 const unsigned char *buf, int count)
123{
124 struct ttyprintk_port *tpkp = tty->driver_data;
125 int ret;
126
127
128 /* exclusive use of tpk_printk within this tty */
129 mutex_lock(&tpkp->port_write_mutex);
130 ret = tpk_printk(buf, count);
131 mutex_unlock(&tpkp->port_write_mutex);
132
133 return ret;
134}
135
136/*
137 * TTY operations write_room function.
138 */
139static int tpk_write_room(struct tty_struct *tty)
140{
141 return TPK_MAX_ROOM;
142}
143
144/*
145 * TTY operations ioctl function.
146 */
147static int tpk_ioctl(struct tty_struct *tty,
148 unsigned int cmd, unsigned long arg)
149{
150 struct ttyprintk_port *tpkp = tty->driver_data;
151
152 if (!tpkp)
153 return -EINVAL;
154
155 switch (cmd) {
156 /* Stop TIOCCONS */
157 case TIOCCONS:
158 return -EOPNOTSUPP;
159 default:
160 return -ENOIOCTLCMD;
161 }
162 return 0;
163}
164
165static const struct tty_operations ttyprintk_ops = {
166 .open = tpk_open,
167 .close = tpk_close,
168 .write = tpk_write,
169 .write_room = tpk_write_room,
170 .ioctl = tpk_ioctl,
171};
172
173struct tty_port_operations null_ops = { };
174
175static struct tty_driver *ttyprintk_driver;
176
177static int __init ttyprintk_init(void)
178{
179 int ret = -ENOMEM;
180 void *rp;
181
182 ttyprintk_driver = alloc_tty_driver(1);
183 if (!ttyprintk_driver)
184 return ret;
185
186 ttyprintk_driver->owner = THIS_MODULE;
187 ttyprintk_driver->driver_name = "ttyprintk";
188 ttyprintk_driver->name = "ttyprintk";
189 ttyprintk_driver->major = TTYAUX_MAJOR;
190 ttyprintk_driver->minor_start = 3;
191 ttyprintk_driver->num = 1;
192 ttyprintk_driver->type = TTY_DRIVER_TYPE_CONSOLE;
193 ttyprintk_driver->init_termios = tty_std_termios;
194 ttyprintk_driver->init_termios.c_oflag = OPOST | OCRNL | ONOCR | ONLRET;
195 ttyprintk_driver->flags = TTY_DRIVER_RESET_TERMIOS |
196 TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
197 tty_set_operations(ttyprintk_driver, &ttyprintk_ops);
198
199 ret = tty_register_driver(ttyprintk_driver);
200 if (ret < 0) {
201 printk(KERN_ERR "Couldn't register ttyprintk driver\n");
202 goto error;
203 }
204
205 /* create our unnumbered device */
206 rp = device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 3), NULL,
207 ttyprintk_driver->name);
208 if (IS_ERR(rp)) {
209 printk(KERN_ERR "Couldn't create ttyprintk device\n");
210 ret = PTR_ERR(rp);
211 goto error;
212 }
213
214 tty_port_init(&tpk_port.port);
215 tpk_port.port.ops = &null_ops;
216 mutex_init(&tpk_port.port_write_mutex);
217
218 return 0;
219
220error:
221 put_tty_driver(ttyprintk_driver);
222 ttyprintk_driver = NULL;
223 return ret;
224}
225module_init(ttyprintk_init);
diff --git a/drivers/char/uv_mmtimer.c b/drivers/char/uv_mmtimer.c
index c7072ba14f48..956ebe2080a5 100644
--- a/drivers/char/uv_mmtimer.c
+++ b/drivers/char/uv_mmtimer.c
@@ -23,7 +23,6 @@
23#include <linux/interrupt.h> 23#include <linux/interrupt.h>
24#include <linux/time.h> 24#include <linux/time.h>
25#include <linux/math64.h> 25#include <linux/math64.h>
26#include <linux/smp_lock.h>
27 26
28#include <asm/genapic.h> 27#include <asm/genapic.h>
29#include <asm/uv/uv_hub.h> 28#include <asm/uv/uv_hub.h>
@@ -52,6 +51,7 @@ static const struct file_operations uv_mmtimer_fops = {
52 .owner = THIS_MODULE, 51 .owner = THIS_MODULE,
53 .mmap = uv_mmtimer_mmap, 52 .mmap = uv_mmtimer_mmap,
54 .unlocked_ioctl = uv_mmtimer_ioctl, 53 .unlocked_ioctl = uv_mmtimer_ioctl,
54 .llseek = noop_llseek,
55}; 55};
56 56
57/** 57/**
diff --git a/drivers/char/vc_screen.c b/drivers/char/vc_screen.c
deleted file mode 100644
index bcce46c96b88..000000000000
--- a/drivers/char/vc_screen.c
+++ /dev/null
@@ -1,509 +0,0 @@
1/*
2 * linux/drivers/char/vc_screen.c
3 *
4 * Provide access to virtual console memory.
5 * /dev/vcs0: the screen as it is being viewed right now (possibly scrolled)
6 * /dev/vcsN: the screen of /dev/ttyN (1 <= N <= 63)
7 * [minor: N]
8 *
9 * /dev/vcsaN: idem, but including attributes, and prefixed with
10 * the 4 bytes lines,columns,x,y (as screendump used to give).
11 * Attribute/character pair is in native endianity.
12 * [minor: N+128]
13 *
14 * This replaces screendump and part of selection, so that the system
15 * administrator can control access using file system permissions.
16 *
17 * aeb@cwi.nl - efter Friedas begravelse - 950211
18 *
19 * machek@k332.feld.cvut.cz - modified not to send characters to wrong console
20 * - fixed some fatal off-by-one bugs (0-- no longer == -1 -> looping and looping and looping...)
21 * - making it shorter - scr_readw are macros which expand in PRETTY long code
22 */
23
24#include <linux/kernel.h>
25#include <linux/major.h>
26#include <linux/errno.h>
27#include <linux/tty.h>
28#include <linux/interrupt.h>
29#include <linux/mm.h>
30#include <linux/init.h>
31#include <linux/mutex.h>
32#include <linux/vt_kern.h>
33#include <linux/selection.h>
34#include <linux/kbd_kern.h>
35#include <linux/console.h>
36#include <linux/device.h>
37#include <linux/smp_lock.h>
38
39#include <asm/uaccess.h>
40#include <asm/byteorder.h>
41#include <asm/unaligned.h>
42
43#undef attr
44#undef org
45#undef addr
46#define HEADER_SIZE 4
47
48static int
49vcs_size(struct inode *inode)
50{
51 int size;
52 int minor = iminor(inode);
53 int currcons = minor & 127;
54 struct vc_data *vc;
55
56 if (currcons == 0)
57 currcons = fg_console;
58 else
59 currcons--;
60 if (!vc_cons_allocated(currcons))
61 return -ENXIO;
62 vc = vc_cons[currcons].d;
63
64 size = vc->vc_rows * vc->vc_cols;
65
66 if (minor & 128)
67 size = 2*size + HEADER_SIZE;
68 return size;
69}
70
71static loff_t vcs_lseek(struct file *file, loff_t offset, int orig)
72{
73 int size;
74
75 mutex_lock(&con_buf_mtx);
76 size = vcs_size(file->f_path.dentry->d_inode);
77 switch (orig) {
78 default:
79 mutex_unlock(&con_buf_mtx);
80 return -EINVAL;
81 case 2:
82 offset += size;
83 break;
84 case 1:
85 offset += file->f_pos;
86 case 0:
87 break;
88 }
89 if (offset < 0 || offset > size) {
90 mutex_unlock(&con_buf_mtx);
91 return -EINVAL;
92 }
93 file->f_pos = offset;
94 mutex_unlock(&con_buf_mtx);
95 return file->f_pos;
96}
97
98
99static ssize_t
100vcs_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
101{
102 struct inode *inode = file->f_path.dentry->d_inode;
103 unsigned int currcons = iminor(inode);
104 struct vc_data *vc;
105 long pos;
106 long viewed, attr, read;
107 int col, maxcol;
108 unsigned short *org = NULL;
109 ssize_t ret;
110
111 mutex_lock(&con_buf_mtx);
112
113 pos = *ppos;
114
115 /* Select the proper current console and verify
116 * sanity of the situation under the console lock.
117 */
118 acquire_console_sem();
119
120 attr = (currcons & 128);
121 currcons = (currcons & 127);
122 if (currcons == 0) {
123 currcons = fg_console;
124 viewed = 1;
125 } else {
126 currcons--;
127 viewed = 0;
128 }
129 ret = -ENXIO;
130 if (!vc_cons_allocated(currcons))
131 goto unlock_out;
132 vc = vc_cons[currcons].d;
133
134 ret = -EINVAL;
135 if (pos < 0)
136 goto unlock_out;
137 read = 0;
138 ret = 0;
139 while (count) {
140 char *con_buf0, *con_buf_start;
141 long this_round, size;
142 ssize_t orig_count;
143 long p = pos;
144
145 /* Check whether we are above size each round,
146 * as copy_to_user at the end of this loop
147 * could sleep.
148 */
149 size = vcs_size(inode);
150 if (pos >= size)
151 break;
152 if (count > size - pos)
153 count = size - pos;
154
155 this_round = count;
156 if (this_round > CON_BUF_SIZE)
157 this_round = CON_BUF_SIZE;
158
159 /* Perform the whole read into the local con_buf.
160 * Then we can drop the console spinlock and safely
161 * attempt to move it to userspace.
162 */
163
164 con_buf_start = con_buf0 = con_buf;
165 orig_count = this_round;
166 maxcol = vc->vc_cols;
167 if (!attr) {
168 org = screen_pos(vc, p, viewed);
169 col = p % maxcol;
170 p += maxcol - col;
171 while (this_round-- > 0) {
172 *con_buf0++ = (vcs_scr_readw(vc, org++) & 0xff);
173 if (++col == maxcol) {
174 org = screen_pos(vc, p, viewed);
175 col = 0;
176 p += maxcol;
177 }
178 }
179 } else {
180 if (p < HEADER_SIZE) {
181 size_t tmp_count;
182
183 con_buf0[0] = (char)vc->vc_rows;
184 con_buf0[1] = (char)vc->vc_cols;
185 getconsxy(vc, con_buf0 + 2);
186
187 con_buf_start += p;
188 this_round += p;
189 if (this_round > CON_BUF_SIZE) {
190 this_round = CON_BUF_SIZE;
191 orig_count = this_round - p;
192 }
193
194 tmp_count = HEADER_SIZE;
195 if (tmp_count > this_round)
196 tmp_count = this_round;
197
198 /* Advance state pointers and move on. */
199 this_round -= tmp_count;
200 p = HEADER_SIZE;
201 con_buf0 = con_buf + HEADER_SIZE;
202 /* If this_round >= 0, then p is even... */
203 } else if (p & 1) {
204 /* Skip first byte for output if start address is odd
205 * Update region sizes up/down depending on free
206 * space in buffer.
207 */
208 con_buf_start++;
209 if (this_round < CON_BUF_SIZE)
210 this_round++;
211 else
212 orig_count--;
213 }
214 if (this_round > 0) {
215 unsigned short *tmp_buf = (unsigned short *)con_buf0;
216
217 p -= HEADER_SIZE;
218 p /= 2;
219 col = p % maxcol;
220
221 org = screen_pos(vc, p, viewed);
222 p += maxcol - col;
223
224 /* Buffer has even length, so we can always copy
225 * character + attribute. We do not copy last byte
226 * to userspace if this_round is odd.
227 */
228 this_round = (this_round + 1) >> 1;
229
230 while (this_round) {
231 *tmp_buf++ = vcs_scr_readw(vc, org++);
232 this_round --;
233 if (++col == maxcol) {
234 org = screen_pos(vc, p, viewed);
235 col = 0;
236 p += maxcol;
237 }
238 }
239 }
240 }
241
242 /* Finally, release the console semaphore while we push
243 * all the data to userspace from our temporary buffer.
244 *
245 * AKPM: Even though it's a semaphore, we should drop it because
246 * the pagefault handling code may want to call printk().
247 */
248
249 release_console_sem();
250 ret = copy_to_user(buf, con_buf_start, orig_count);
251 acquire_console_sem();
252
253 if (ret) {
254 read += (orig_count - ret);
255 ret = -EFAULT;
256 break;
257 }
258 buf += orig_count;
259 pos += orig_count;
260 read += orig_count;
261 count -= orig_count;
262 }
263 *ppos += read;
264 if (read)
265 ret = read;
266unlock_out:
267 release_console_sem();
268 mutex_unlock(&con_buf_mtx);
269 return ret;
270}
271
272static ssize_t
273vcs_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
274{
275 struct inode *inode = file->f_path.dentry->d_inode;
276 unsigned int currcons = iminor(inode);
277 struct vc_data *vc;
278 long pos;
279 long viewed, attr, size, written;
280 char *con_buf0;
281 int col, maxcol;
282 u16 *org0 = NULL, *org = NULL;
283 size_t ret;
284
285 mutex_lock(&con_buf_mtx);
286
287 pos = *ppos;
288
289 /* Select the proper current console and verify
290 * sanity of the situation under the console lock.
291 */
292 acquire_console_sem();
293
294 attr = (currcons & 128);
295 currcons = (currcons & 127);
296
297 if (currcons == 0) {
298 currcons = fg_console;
299 viewed = 1;
300 } else {
301 currcons--;
302 viewed = 0;
303 }
304 ret = -ENXIO;
305 if (!vc_cons_allocated(currcons))
306 goto unlock_out;
307 vc = vc_cons[currcons].d;
308
309 size = vcs_size(inode);
310 ret = -EINVAL;
311 if (pos < 0 || pos > size)
312 goto unlock_out;
313 if (count > size - pos)
314 count = size - pos;
315 written = 0;
316 while (count) {
317 long this_round = count;
318 size_t orig_count;
319 long p;
320
321 if (this_round > CON_BUF_SIZE)
322 this_round = CON_BUF_SIZE;
323
324 /* Temporarily drop the console lock so that we can read
325 * in the write data from userspace safely.
326 */
327 release_console_sem();
328 ret = copy_from_user(con_buf, buf, this_round);
329 acquire_console_sem();
330
331 if (ret) {
332 this_round -= ret;
333 if (!this_round) {
334 /* Abort loop if no data were copied. Otherwise
335 * fail with -EFAULT.
336 */
337 if (written)
338 break;
339 ret = -EFAULT;
340 goto unlock_out;
341 }
342 }
343
344 /* The vcs_size might have changed while we slept to grab
345 * the user buffer, so recheck.
346 * Return data written up to now on failure.
347 */
348 size = vcs_size(inode);
349 if (pos >= size)
350 break;
351 if (this_round > size - pos)
352 this_round = size - pos;
353
354 /* OK, now actually push the write to the console
355 * under the lock using the local kernel buffer.
356 */
357
358 con_buf0 = con_buf;
359 orig_count = this_round;
360 maxcol = vc->vc_cols;
361 p = pos;
362 if (!attr) {
363 org0 = org = screen_pos(vc, p, viewed);
364 col = p % maxcol;
365 p += maxcol - col;
366
367 while (this_round > 0) {
368 unsigned char c = *con_buf0++;
369
370 this_round--;
371 vcs_scr_writew(vc,
372 (vcs_scr_readw(vc, org) & 0xff00) | c, org);
373 org++;
374 if (++col == maxcol) {
375 org = screen_pos(vc, p, viewed);
376 col = 0;
377 p += maxcol;
378 }
379 }
380 } else {
381 if (p < HEADER_SIZE) {
382 char header[HEADER_SIZE];
383
384 getconsxy(vc, header + 2);
385 while (p < HEADER_SIZE && this_round > 0) {
386 this_round--;
387 header[p++] = *con_buf0++;
388 }
389 if (!viewed)
390 putconsxy(vc, header + 2);
391 }
392 p -= HEADER_SIZE;
393 col = (p/2) % maxcol;
394 if (this_round > 0) {
395 org0 = org = screen_pos(vc, p/2, viewed);
396 if ((p & 1) && this_round > 0) {
397 char c;
398
399 this_round--;
400 c = *con_buf0++;
401#ifdef __BIG_ENDIAN
402 vcs_scr_writew(vc, c |
403 (vcs_scr_readw(vc, org) & 0xff00), org);
404#else
405 vcs_scr_writew(vc, (c << 8) |
406 (vcs_scr_readw(vc, org) & 0xff), org);
407#endif
408 org++;
409 p++;
410 if (++col == maxcol) {
411 org = screen_pos(vc, p/2, viewed);
412 col = 0;
413 }
414 }
415 p /= 2;
416 p += maxcol - col;
417 }
418 while (this_round > 1) {
419 unsigned short w;
420
421 w = get_unaligned(((unsigned short *)con_buf0));
422 vcs_scr_writew(vc, w, org++);
423 con_buf0 += 2;
424 this_round -= 2;
425 if (++col == maxcol) {
426 org = screen_pos(vc, p, viewed);
427 col = 0;
428 p += maxcol;
429 }
430 }
431 if (this_round > 0) {
432 unsigned char c;
433
434 c = *con_buf0++;
435#ifdef __BIG_ENDIAN
436 vcs_scr_writew(vc, (vcs_scr_readw(vc, org) & 0xff) | (c << 8), org);
437#else
438 vcs_scr_writew(vc, (vcs_scr_readw(vc, org) & 0xff00) | c, org);
439#endif
440 }
441 }
442 count -= orig_count;
443 written += orig_count;
444 buf += orig_count;
445 pos += orig_count;
446 if (org0)
447 update_region(vc, (unsigned long)(org0), org - org0);
448 }
449 *ppos += written;
450 ret = written;
451
452unlock_out:
453 release_console_sem();
454
455 mutex_unlock(&con_buf_mtx);
456
457 return ret;
458}
459
460static int
461vcs_open(struct inode *inode, struct file *filp)
462{
463 unsigned int currcons = iminor(inode) & 127;
464 int ret = 0;
465
466 tty_lock();
467 if(currcons && !vc_cons_allocated(currcons-1))
468 ret = -ENXIO;
469 tty_unlock();
470 return ret;
471}
472
473static const struct file_operations vcs_fops = {
474 .llseek = vcs_lseek,
475 .read = vcs_read,
476 .write = vcs_write,
477 .open = vcs_open,
478};
479
480static struct class *vc_class;
481
482void vcs_make_sysfs(int index)
483{
484 device_create(vc_class, NULL, MKDEV(VCS_MAJOR, index + 1), NULL,
485 "vcs%u", index + 1);
486 device_create(vc_class, NULL, MKDEV(VCS_MAJOR, index + 129), NULL,
487 "vcsa%u", index + 1);
488}
489
490void vcs_remove_sysfs(int index)
491{
492 device_destroy(vc_class, MKDEV(VCS_MAJOR, index + 1));
493 device_destroy(vc_class, MKDEV(VCS_MAJOR, index + 129));
494}
495
496int __init vcs_init(void)
497{
498 unsigned int i;
499
500 if (register_chrdev(VCS_MAJOR, "vcs", &vcs_fops))
501 panic("unable to get major %d for vcs device", VCS_MAJOR);
502 vc_class = class_create(THIS_MODULE, "vc");
503
504 device_create(vc_class, NULL, MKDEV(VCS_MAJOR, 0), NULL, "vcs");
505 device_create(vc_class, NULL, MKDEV(VCS_MAJOR, 128), NULL, "vcsa");
506 for (i = 0; i < MIN_NR_CONSOLES; i++)
507 vcs_make_sysfs(i);
508 return 0;
509}
diff --git a/drivers/char/viotape.c b/drivers/char/viotape.c
index 42f7fa442ff8..ad6e64a2912d 100644
--- a/drivers/char/viotape.c
+++ b/drivers/char/viotape.c
@@ -46,7 +46,7 @@
46#include <linux/completion.h> 46#include <linux/completion.h>
47#include <linux/proc_fs.h> 47#include <linux/proc_fs.h>
48#include <linux/seq_file.h> 48#include <linux/seq_file.h>
49#include <linux/smp_lock.h> 49#include <linux/mutex.h>
50#include <linux/slab.h> 50#include <linux/slab.h>
51 51
52#include <asm/uaccess.h> 52#include <asm/uaccess.h>
@@ -64,6 +64,7 @@
64#define VIOTAPE_KERN_WARN KERN_WARNING "viotape: " 64#define VIOTAPE_KERN_WARN KERN_WARNING "viotape: "
65#define VIOTAPE_KERN_INFO KERN_INFO "viotape: " 65#define VIOTAPE_KERN_INFO KERN_INFO "viotape: "
66 66
67static DEFINE_MUTEX(proc_viotape_mutex);
67static int viotape_numdev; 68static int viotape_numdev;
68 69
69/* 70/*
@@ -684,9 +685,9 @@ static long viotap_unlocked_ioctl(struct file *file,
684{ 685{
685 long rc; 686 long rc;
686 687
687 lock_kernel(); 688 mutex_lock(&proc_viotape_mutex);
688 rc = viotap_ioctl(file->f_path.dentry->d_inode, file, cmd, arg); 689 rc = viotap_ioctl(file->f_path.dentry->d_inode, file, cmd, arg);
689 unlock_kernel(); 690 mutex_unlock(&proc_viotape_mutex);
690 return rc; 691 return rc;
691} 692}
692 693
@@ -700,7 +701,7 @@ static int viotap_open(struct inode *inode, struct file *file)
700 if (op == NULL) 701 if (op == NULL)
701 return -ENOMEM; 702 return -ENOMEM;
702 703
703 lock_kernel(); 704 mutex_lock(&proc_viotape_mutex);
704 get_dev_info(file->f_path.dentry->d_inode, &devi); 705 get_dev_info(file->f_path.dentry->d_inode, &devi);
705 706
706 /* Note: We currently only support one mode! */ 707 /* Note: We currently only support one mode! */
@@ -731,7 +732,7 @@ static int viotap_open(struct inode *inode, struct file *file)
731 732
732free_op: 733free_op:
733 free_op_struct(op); 734 free_op_struct(op);
734 unlock_kernel(); 735 mutex_unlock(&proc_viotape_mutex);
735 return ret; 736 return ret;
736} 737}
737 738
@@ -804,6 +805,7 @@ const struct file_operations viotap_fops = {
804 .unlocked_ioctl = viotap_unlocked_ioctl, 805 .unlocked_ioctl = viotap_unlocked_ioctl,
805 .open = viotap_open, 806 .open = viotap_open,
806 .release = viotap_release, 807 .release = viotap_release,
808 .llseek = noop_llseek,
807}; 809};
808 810
809/* Handle interrupt events for tape */ 811/* Handle interrupt events for tape */
diff --git a/drivers/char/virtio_console.c b/drivers/char/virtio_console.c
index 0f69c5ec0ecd..fb68b1295373 100644
--- a/drivers/char/virtio_console.c
+++ b/drivers/char/virtio_console.c
@@ -1,6 +1,7 @@
1/* 1/*
2 * Copyright (C) 2006, 2007, 2009 Rusty Russell, IBM Corporation 2 * Copyright (C) 2006, 2007, 2009 Rusty Russell, IBM Corporation
3 * Copyright (C) 2009, 2010 Red Hat, Inc. 3 * Copyright (C) 2009, 2010, 2011 Red Hat, Inc.
4 * Copyright (C) 2009, 2010, 2011 Amit Shah <amit.shah@redhat.com>
4 * 5 *
5 * This program is free software; you can redistribute it and/or modify 6 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by 7 * it under the terms of the GNU General Public License as published by
@@ -31,7 +32,7 @@
31#include <linux/virtio_console.h> 32#include <linux/virtio_console.h>
32#include <linux/wait.h> 33#include <linux/wait.h>
33#include <linux/workqueue.h> 34#include <linux/workqueue.h>
34#include "hvc_console.h" 35#include "../tty/hvc/hvc_console.h"
35 36
36/* 37/*
37 * This is a global struct for storing common data for all the devices 38 * This is a global struct for storing common data for all the devices
@@ -48,6 +49,9 @@ struct ports_driver_data {
48 /* Used for exporting per-port information to debugfs */ 49 /* Used for exporting per-port information to debugfs */
49 struct dentry *debugfs_dir; 50 struct dentry *debugfs_dir;
50 51
52 /* List of all the devices we're handling */
53 struct list_head portdevs;
54
51 /* Number of devices this driver is handling */ 55 /* Number of devices this driver is handling */
52 unsigned int index; 56 unsigned int index;
53 57
@@ -108,6 +112,9 @@ struct port_buffer {
108 * ports for that device (vdev->priv). 112 * ports for that device (vdev->priv).
109 */ 113 */
110struct ports_device { 114struct ports_device {
115 /* Next portdev in the list, head is in the pdrvdata struct */
116 struct list_head list;
117
111 /* 118 /*
112 * Workqueue handlers where we process deferred work after 119 * Workqueue handlers where we process deferred work after
113 * notification 120 * notification
@@ -178,15 +185,21 @@ struct port {
178 struct console cons; 185 struct console cons;
179 186
180 /* Each port associates with a separate char device */ 187 /* Each port associates with a separate char device */
181 struct cdev cdev; 188 struct cdev *cdev;
182 struct device *dev; 189 struct device *dev;
183 190
191 /* Reference-counting to handle port hot-unplugs and file operations */
192 struct kref kref;
193
184 /* A waitqueue for poll() or blocking read operations */ 194 /* A waitqueue for poll() or blocking read operations */
185 wait_queue_head_t waitqueue; 195 wait_queue_head_t waitqueue;
186 196
187 /* The 'name' of the port that we expose via sysfs properties */ 197 /* The 'name' of the port that we expose via sysfs properties */
188 char *name; 198 char *name;
189 199
200 /* We can notify apps of host connect / disconnect events via SIGIO */
201 struct fasync_struct *async_queue;
202
190 /* The 'id' to identify the port with the Host */ 203 /* The 'id' to identify the port with the Host */
191 u32 id; 204 u32 id;
192 205
@@ -221,6 +234,41 @@ out:
221 return port; 234 return port;
222} 235}
223 236
237static struct port *find_port_by_devt_in_portdev(struct ports_device *portdev,
238 dev_t dev)
239{
240 struct port *port;
241 unsigned long flags;
242
243 spin_lock_irqsave(&portdev->ports_lock, flags);
244 list_for_each_entry(port, &portdev->ports, list)
245 if (port->cdev->dev == dev)
246 goto out;
247 port = NULL;
248out:
249 spin_unlock_irqrestore(&portdev->ports_lock, flags);
250
251 return port;
252}
253
254static struct port *find_port_by_devt(dev_t dev)
255{
256 struct ports_device *portdev;
257 struct port *port;
258 unsigned long flags;
259
260 spin_lock_irqsave(&pdrvdata_lock, flags);
261 list_for_each_entry(portdev, &pdrvdata.portdevs, list) {
262 port = find_port_by_devt_in_portdev(portdev, dev);
263 if (port)
264 goto out;
265 }
266 port = NULL;
267out:
268 spin_unlock_irqrestore(&pdrvdata_lock, flags);
269 return port;
270}
271
224static struct port *find_port_by_id(struct ports_device *portdev, u32 id) 272static struct port *find_port_by_id(struct ports_device *portdev, u32 id)
225{ 273{
226 struct port *port; 274 struct port *port;
@@ -340,6 +388,10 @@ static void discard_port_data(struct port *port)
340 unsigned int len; 388 unsigned int len;
341 int ret; 389 int ret;
342 390
391 if (!port->portdev) {
392 /* Device has been unplugged. vqs are already gone. */
393 return;
394 }
343 vq = port->in_vq; 395 vq = port->in_vq;
344 if (port->inbuf) 396 if (port->inbuf)
345 buf = port->inbuf; 397 buf = port->inbuf;
@@ -410,7 +462,10 @@ static ssize_t __send_control_msg(struct ports_device *portdev, u32 port_id,
410static ssize_t send_control_msg(struct port *port, unsigned int event, 462static ssize_t send_control_msg(struct port *port, unsigned int event,
411 unsigned int value) 463 unsigned int value)
412{ 464{
413 return __send_control_msg(port->portdev, port->id, event, value); 465 /* Did the port get unplugged before userspace closed it? */
466 if (port->portdev)
467 return __send_control_msg(port->portdev, port->id, event, value);
468 return 0;
414} 469}
415 470
416/* Callers must take the port->outvq_lock */ 471/* Callers must take the port->outvq_lock */
@@ -419,6 +474,10 @@ static void reclaim_consumed_buffers(struct port *port)
419 void *buf; 474 void *buf;
420 unsigned int len; 475 unsigned int len;
421 476
477 if (!port->portdev) {
478 /* Device has been unplugged. vqs are already gone. */
479 return;
480 }
422 while ((buf = virtqueue_get_buf(port->out_vq, &len))) { 481 while ((buf = virtqueue_get_buf(port->out_vq, &len))) {
423 kfree(buf); 482 kfree(buf);
424 port->outvq_full = false; 483 port->outvq_full = false;
@@ -525,6 +584,10 @@ static ssize_t fill_readbuf(struct port *port, char *out_buf, size_t out_count,
525/* The condition that must be true for polling to end */ 584/* The condition that must be true for polling to end */
526static bool will_read_block(struct port *port) 585static bool will_read_block(struct port *port)
527{ 586{
587 if (!port->guest_connected) {
588 /* Port got hot-unplugged. Let's exit. */
589 return false;
590 }
528 return !port_has_data(port) && port->host_connected; 591 return !port_has_data(port) && port->host_connected;
529} 592}
530 593
@@ -575,6 +638,9 @@ static ssize_t port_fops_read(struct file *filp, char __user *ubuf,
575 if (ret < 0) 638 if (ret < 0)
576 return ret; 639 return ret;
577 } 640 }
641 /* Port got hot-unplugged. */
642 if (!port->guest_connected)
643 return -ENODEV;
578 /* 644 /*
579 * We could've received a disconnection message while we were 645 * We could've received a disconnection message while we were
580 * waiting for more data. 646 * waiting for more data.
@@ -616,6 +682,9 @@ static ssize_t port_fops_write(struct file *filp, const char __user *ubuf,
616 if (ret < 0) 682 if (ret < 0)
617 return ret; 683 return ret;
618 } 684 }
685 /* Port got hot-unplugged. */
686 if (!port->guest_connected)
687 return -ENODEV;
619 688
620 count = min((size_t)(32 * 1024), count); 689 count = min((size_t)(32 * 1024), count);
621 690
@@ -656,6 +725,10 @@ static unsigned int port_fops_poll(struct file *filp, poll_table *wait)
656 port = filp->private_data; 725 port = filp->private_data;
657 poll_wait(filp, &port->waitqueue, wait); 726 poll_wait(filp, &port->waitqueue, wait);
658 727
728 if (!port->guest_connected) {
729 /* Port got unplugged */
730 return POLLHUP;
731 }
659 ret = 0; 732 ret = 0;
660 if (!will_read_block(port)) 733 if (!will_read_block(port))
661 ret |= POLLIN | POLLRDNORM; 734 ret |= POLLIN | POLLRDNORM;
@@ -667,6 +740,8 @@ static unsigned int port_fops_poll(struct file *filp, poll_table *wait)
667 return ret; 740 return ret;
668} 741}
669 742
743static void remove_port(struct kref *kref);
744
670static int port_fops_release(struct inode *inode, struct file *filp) 745static int port_fops_release(struct inode *inode, struct file *filp)
671{ 746{
672 struct port *port; 747 struct port *port;
@@ -687,6 +762,16 @@ static int port_fops_release(struct inode *inode, struct file *filp)
687 reclaim_consumed_buffers(port); 762 reclaim_consumed_buffers(port);
688 spin_unlock_irq(&port->outvq_lock); 763 spin_unlock_irq(&port->outvq_lock);
689 764
765 /*
766 * Locks aren't necessary here as a port can't be opened after
767 * unplug, and if a port isn't unplugged, a kref would already
768 * exist for the port. Plus, taking ports_lock here would
769 * create a dependency on other locks taken by functions
770 * inside remove_port if we're the last holder of the port,
771 * creating many problems.
772 */
773 kref_put(&port->kref, remove_port);
774
690 return 0; 775 return 0;
691} 776}
692 777
@@ -694,22 +779,31 @@ static int port_fops_open(struct inode *inode, struct file *filp)
694{ 779{
695 struct cdev *cdev = inode->i_cdev; 780 struct cdev *cdev = inode->i_cdev;
696 struct port *port; 781 struct port *port;
782 int ret;
697 783
698 port = container_of(cdev, struct port, cdev); 784 port = find_port_by_devt(cdev->dev);
699 filp->private_data = port; 785 filp->private_data = port;
700 786
787 /* Prevent against a port getting hot-unplugged at the same time */
788 spin_lock_irq(&port->portdev->ports_lock);
789 kref_get(&port->kref);
790 spin_unlock_irq(&port->portdev->ports_lock);
791
701 /* 792 /*
702 * Don't allow opening of console port devices -- that's done 793 * Don't allow opening of console port devices -- that's done
703 * via /dev/hvc 794 * via /dev/hvc
704 */ 795 */
705 if (is_console_port(port)) 796 if (is_console_port(port)) {
706 return -ENXIO; 797 ret = -ENXIO;
798 goto out;
799 }
707 800
708 /* Allow only one process to open a particular port at a time */ 801 /* Allow only one process to open a particular port at a time */
709 spin_lock_irq(&port->inbuf_lock); 802 spin_lock_irq(&port->inbuf_lock);
710 if (port->guest_connected) { 803 if (port->guest_connected) {
711 spin_unlock_irq(&port->inbuf_lock); 804 spin_unlock_irq(&port->inbuf_lock);
712 return -EMFILE; 805 ret = -EMFILE;
806 goto out;
713 } 807 }
714 808
715 port->guest_connected = true; 809 port->guest_connected = true;
@@ -724,10 +818,23 @@ static int port_fops_open(struct inode *inode, struct file *filp)
724 reclaim_consumed_buffers(port); 818 reclaim_consumed_buffers(port);
725 spin_unlock_irq(&port->outvq_lock); 819 spin_unlock_irq(&port->outvq_lock);
726 820
821 nonseekable_open(inode, filp);
822
727 /* Notify host of port being opened */ 823 /* Notify host of port being opened */
728 send_control_msg(filp->private_data, VIRTIO_CONSOLE_PORT_OPEN, 1); 824 send_control_msg(filp->private_data, VIRTIO_CONSOLE_PORT_OPEN, 1);
729 825
730 return 0; 826 return 0;
827out:
828 kref_put(&port->kref, remove_port);
829 return ret;
830}
831
832static int port_fops_fasync(int fd, struct file *filp, int mode)
833{
834 struct port *port;
835
836 port = filp->private_data;
837 return fasync_helper(fd, filp, mode, &port->async_queue);
731} 838}
732 839
733/* 840/*
@@ -743,6 +850,8 @@ static const struct file_operations port_fops = {
743 .write = port_fops_write, 850 .write = port_fops_write,
744 .poll = port_fops_poll, 851 .poll = port_fops_poll,
745 .release = port_fops_release, 852 .release = port_fops_release,
853 .fasync = port_fops_fasync,
854 .llseek = no_llseek,
746}; 855};
747 856
748/* 857/*
@@ -1001,6 +1110,12 @@ static unsigned int fill_queue(struct virtqueue *vq, spinlock_t *lock)
1001 return nr_added_bufs; 1110 return nr_added_bufs;
1002} 1111}
1003 1112
1113static void send_sigio_to_port(struct port *port)
1114{
1115 if (port->async_queue && port->guest_connected)
1116 kill_fasync(&port->async_queue, SIGIO, POLL_OUT);
1117}
1118
1004static int add_port(struct ports_device *portdev, u32 id) 1119static int add_port(struct ports_device *portdev, u32 id)
1005{ 1120{
1006 char debugfs_name[16]; 1121 char debugfs_name[16];
@@ -1015,6 +1130,7 @@ static int add_port(struct ports_device *portdev, u32 id)
1015 err = -ENOMEM; 1130 err = -ENOMEM;
1016 goto fail; 1131 goto fail;
1017 } 1132 }
1133 kref_init(&port->kref);
1018 1134
1019 port->portdev = portdev; 1135 port->portdev = portdev;
1020 port->id = id; 1136 port->id = id;
@@ -1022,6 +1138,7 @@ static int add_port(struct ports_device *portdev, u32 id)
1022 port->name = NULL; 1138 port->name = NULL;
1023 port->inbuf = NULL; 1139 port->inbuf = NULL;
1024 port->cons.hvc = NULL; 1140 port->cons.hvc = NULL;
1141 port->async_queue = NULL;
1025 1142
1026 port->cons.ws.ws_row = port->cons.ws.ws_col = 0; 1143 port->cons.ws.ws_row = port->cons.ws.ws_col = 0;
1027 1144
@@ -1032,14 +1149,20 @@ static int add_port(struct ports_device *portdev, u32 id)
1032 port->in_vq = portdev->in_vqs[port->id]; 1149 port->in_vq = portdev->in_vqs[port->id];
1033 port->out_vq = portdev->out_vqs[port->id]; 1150 port->out_vq = portdev->out_vqs[port->id];
1034 1151
1035 cdev_init(&port->cdev, &port_fops); 1152 port->cdev = cdev_alloc();
1153 if (!port->cdev) {
1154 dev_err(&port->portdev->vdev->dev, "Error allocating cdev\n");
1155 err = -ENOMEM;
1156 goto free_port;
1157 }
1158 port->cdev->ops = &port_fops;
1036 1159
1037 devt = MKDEV(portdev->chr_major, id); 1160 devt = MKDEV(portdev->chr_major, id);
1038 err = cdev_add(&port->cdev, devt, 1); 1161 err = cdev_add(port->cdev, devt, 1);
1039 if (err < 0) { 1162 if (err < 0) {
1040 dev_err(&port->portdev->vdev->dev, 1163 dev_err(&port->portdev->vdev->dev,
1041 "Error %d adding cdev for port %u\n", err, id); 1164 "Error %d adding cdev for port %u\n", err, id);
1042 goto free_port; 1165 goto free_cdev;
1043 } 1166 }
1044 port->dev = device_create(pdrvdata.class, &port->portdev->vdev->dev, 1167 port->dev = device_create(pdrvdata.class, &port->portdev->vdev->dev,
1045 devt, port, "vport%up%u", 1168 devt, port, "vport%up%u",
@@ -1104,7 +1227,7 @@ free_inbufs:
1104free_device: 1227free_device:
1105 device_destroy(pdrvdata.class, port->dev->devt); 1228 device_destroy(pdrvdata.class, port->dev->devt);
1106free_cdev: 1229free_cdev:
1107 cdev_del(&port->cdev); 1230 cdev_del(port->cdev);
1108free_port: 1231free_port:
1109 kfree(port); 1232 kfree(port);
1110fail: 1233fail:
@@ -1113,42 +1236,52 @@ fail:
1113 return err; 1236 return err;
1114} 1237}
1115 1238
1116/* Remove all port-specific data. */ 1239/* No users remain, remove all port-specific data. */
1117static int remove_port(struct port *port) 1240static void remove_port(struct kref *kref)
1241{
1242 struct port *port;
1243
1244 port = container_of(kref, struct port, kref);
1245
1246 sysfs_remove_group(&port->dev->kobj, &port_attribute_group);
1247 device_destroy(pdrvdata.class, port->dev->devt);
1248 cdev_del(port->cdev);
1249
1250 kfree(port->name);
1251
1252 debugfs_remove(port->debugfs_file);
1253
1254 kfree(port);
1255}
1256
1257/*
1258 * Port got unplugged. Remove port from portdev's list and drop the
1259 * kref reference. If no userspace has this port opened, it will
1260 * result in immediate removal the port.
1261 */
1262static void unplug_port(struct port *port)
1118{ 1263{
1119 struct port_buffer *buf; 1264 struct port_buffer *buf;
1120 1265
1266 spin_lock_irq(&port->portdev->ports_lock);
1267 list_del(&port->list);
1268 spin_unlock_irq(&port->portdev->ports_lock);
1269
1121 if (port->guest_connected) { 1270 if (port->guest_connected) {
1122 port->guest_connected = false; 1271 port->guest_connected = false;
1123 port->host_connected = false; 1272 port->host_connected = false;
1124 wake_up_interruptible(&port->waitqueue); 1273 wake_up_interruptible(&port->waitqueue);
1125 send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 0);
1126 }
1127 1274
1128 spin_lock_irq(&port->portdev->ports_lock); 1275 /* Let the app know the port is going down. */
1129 list_del(&port->list); 1276 send_sigio_to_port(port);
1130 spin_unlock_irq(&port->portdev->ports_lock); 1277 }
1131 1278
1132 if (is_console_port(port)) { 1279 if (is_console_port(port)) {
1133 spin_lock_irq(&pdrvdata_lock); 1280 spin_lock_irq(&pdrvdata_lock);
1134 list_del(&port->cons.list); 1281 list_del(&port->cons.list);
1135 spin_unlock_irq(&pdrvdata_lock); 1282 spin_unlock_irq(&pdrvdata_lock);
1136#if 0
1137 /*
1138 * hvc_remove() not called as removing one hvc port
1139 * results in other hvc ports getting frozen.
1140 *
1141 * Once this is resolved in hvc, this functionality
1142 * will be enabled. Till that is done, the -EPIPE
1143 * return from get_chars() above will help
1144 * hvc_console.c to clean up on ports we remove here.
1145 */
1146 hvc_remove(port->cons.hvc); 1283 hvc_remove(port->cons.hvc);
1147#endif
1148 } 1284 }
1149 sysfs_remove_group(&port->dev->kobj, &port_attribute_group);
1150 device_destroy(pdrvdata.class, port->dev->devt);
1151 cdev_del(&port->cdev);
1152 1285
1153 /* Remove unused data this port might have received. */ 1286 /* Remove unused data this port might have received. */
1154 discard_port_data(port); 1287 discard_port_data(port);
@@ -1159,12 +1292,19 @@ static int remove_port(struct port *port)
1159 while ((buf = virtqueue_detach_unused_buf(port->in_vq))) 1292 while ((buf = virtqueue_detach_unused_buf(port->in_vq)))
1160 free_buf(buf); 1293 free_buf(buf);
1161 1294
1162 kfree(port->name); 1295 /*
1163 1296 * We should just assume the device itself has gone off --
1164 debugfs_remove(port->debugfs_file); 1297 * else a close on an open port later will try to send out a
1298 * control message.
1299 */
1300 port->portdev = NULL;
1165 1301
1166 kfree(port); 1302 /*
1167 return 0; 1303 * Locks around here are not necessary - a port can't be
1304 * opened after we removed the port struct from ports_list
1305 * above.
1306 */
1307 kref_put(&port->kref, remove_port);
1168} 1308}
1169 1309
1170/* Any private messages that the Host and Guest want to share */ 1310/* Any private messages that the Host and Guest want to share */
@@ -1203,7 +1343,7 @@ static void handle_control_message(struct ports_device *portdev,
1203 add_port(portdev, cpkt->id); 1343 add_port(portdev, cpkt->id);
1204 break; 1344 break;
1205 case VIRTIO_CONSOLE_PORT_REMOVE: 1345 case VIRTIO_CONSOLE_PORT_REMOVE:
1206 remove_port(port); 1346 unplug_port(port);
1207 break; 1347 break;
1208 case VIRTIO_CONSOLE_CONSOLE_PORT: 1348 case VIRTIO_CONSOLE_CONSOLE_PORT:
1209 if (!cpkt->value) 1349 if (!cpkt->value)
@@ -1245,6 +1385,12 @@ static void handle_control_message(struct ports_device *portdev,
1245 spin_lock_irq(&port->outvq_lock); 1385 spin_lock_irq(&port->outvq_lock);
1246 reclaim_consumed_buffers(port); 1386 reclaim_consumed_buffers(port);
1247 spin_unlock_irq(&port->outvq_lock); 1387 spin_unlock_irq(&port->outvq_lock);
1388
1389 /*
1390 * If the guest is connected, it'll be interested in
1391 * knowing the host connection state changed.
1392 */
1393 send_sigio_to_port(port);
1248 break; 1394 break;
1249 case VIRTIO_CONSOLE_PORT_NAME: 1395 case VIRTIO_CONSOLE_PORT_NAME:
1250 /* 1396 /*
@@ -1314,6 +1460,17 @@ static void control_work_handler(struct work_struct *work)
1314 spin_unlock(&portdev->cvq_lock); 1460 spin_unlock(&portdev->cvq_lock);
1315} 1461}
1316 1462
1463static void out_intr(struct virtqueue *vq)
1464{
1465 struct port *port;
1466
1467 port = find_port_by_vq(vq->vdev->priv, vq);
1468 if (!port)
1469 return;
1470
1471 wake_up_interruptible(&port->waitqueue);
1472}
1473
1317static void in_intr(struct virtqueue *vq) 1474static void in_intr(struct virtqueue *vq)
1318{ 1475{
1319 struct port *port; 1476 struct port *port;
@@ -1341,6 +1498,9 @@ static void in_intr(struct virtqueue *vq)
1341 1498
1342 wake_up_interruptible(&port->waitqueue); 1499 wake_up_interruptible(&port->waitqueue);
1343 1500
1501 /* Send a SIGIO indicating new data in case the process asked for it */
1502 send_sigio_to_port(port);
1503
1344 if (is_console_port(port) && hvc_poll(port->cons.hvc)) 1504 if (is_console_port(port) && hvc_poll(port->cons.hvc))
1345 hvc_kick(); 1505 hvc_kick();
1346} 1506}
@@ -1396,31 +1556,16 @@ static int init_vqs(struct ports_device *portdev)
1396 nr_queues = use_multiport(portdev) ? (nr_ports + 1) * 2 : 2; 1556 nr_queues = use_multiport(portdev) ? (nr_ports + 1) * 2 : 2;
1397 1557
1398 vqs = kmalloc(nr_queues * sizeof(struct virtqueue *), GFP_KERNEL); 1558 vqs = kmalloc(nr_queues * sizeof(struct virtqueue *), GFP_KERNEL);
1399 if (!vqs) {
1400 err = -ENOMEM;
1401 goto fail;
1402 }
1403 io_callbacks = kmalloc(nr_queues * sizeof(vq_callback_t *), GFP_KERNEL); 1559 io_callbacks = kmalloc(nr_queues * sizeof(vq_callback_t *), GFP_KERNEL);
1404 if (!io_callbacks) {
1405 err = -ENOMEM;
1406 goto free_vqs;
1407 }
1408 io_names = kmalloc(nr_queues * sizeof(char *), GFP_KERNEL); 1560 io_names = kmalloc(nr_queues * sizeof(char *), GFP_KERNEL);
1409 if (!io_names) {
1410 err = -ENOMEM;
1411 goto free_callbacks;
1412 }
1413 portdev->in_vqs = kmalloc(nr_ports * sizeof(struct virtqueue *), 1561 portdev->in_vqs = kmalloc(nr_ports * sizeof(struct virtqueue *),
1414 GFP_KERNEL); 1562 GFP_KERNEL);
1415 if (!portdev->in_vqs) {
1416 err = -ENOMEM;
1417 goto free_names;
1418 }
1419 portdev->out_vqs = kmalloc(nr_ports * sizeof(struct virtqueue *), 1563 portdev->out_vqs = kmalloc(nr_ports * sizeof(struct virtqueue *),
1420 GFP_KERNEL); 1564 GFP_KERNEL);
1421 if (!portdev->out_vqs) { 1565 if (!vqs || !io_callbacks || !io_names || !portdev->in_vqs ||
1566 !portdev->out_vqs) {
1422 err = -ENOMEM; 1567 err = -ENOMEM;
1423 goto free_invqs; 1568 goto free;
1424 } 1569 }
1425 1570
1426 /* 1571 /*
@@ -1430,7 +1575,7 @@ static int init_vqs(struct ports_device *portdev)
1430 */ 1575 */
1431 j = 0; 1576 j = 0;
1432 io_callbacks[j] = in_intr; 1577 io_callbacks[j] = in_intr;
1433 io_callbacks[j + 1] = NULL; 1578 io_callbacks[j + 1] = out_intr;
1434 io_names[j] = "input"; 1579 io_names[j] = "input";
1435 io_names[j + 1] = "output"; 1580 io_names[j + 1] = "output";
1436 j += 2; 1581 j += 2;
@@ -1444,7 +1589,7 @@ static int init_vqs(struct ports_device *portdev)
1444 for (i = 1; i < nr_ports; i++) { 1589 for (i = 1; i < nr_ports; i++) {
1445 j += 2; 1590 j += 2;
1446 io_callbacks[j] = in_intr; 1591 io_callbacks[j] = in_intr;
1447 io_callbacks[j + 1] = NULL; 1592 io_callbacks[j + 1] = out_intr;
1448 io_names[j] = "input"; 1593 io_names[j] = "input";
1449 io_names[j + 1] = "output"; 1594 io_names[j + 1] = "output";
1450 } 1595 }
@@ -1454,7 +1599,7 @@ static int init_vqs(struct ports_device *portdev)
1454 io_callbacks, 1599 io_callbacks,
1455 (const char **)io_names); 1600 (const char **)io_names);
1456 if (err) 1601 if (err)
1457 goto free_outvqs; 1602 goto free;
1458 1603
1459 j = 0; 1604 j = 0;
1460 portdev->in_vqs[0] = vqs[0]; 1605 portdev->in_vqs[0] = vqs[0];
@@ -1470,23 +1615,19 @@ static int init_vqs(struct ports_device *portdev)
1470 portdev->out_vqs[i] = vqs[j + 1]; 1615 portdev->out_vqs[i] = vqs[j + 1];
1471 } 1616 }
1472 } 1617 }
1473 kfree(io_callbacks);
1474 kfree(io_names); 1618 kfree(io_names);
1619 kfree(io_callbacks);
1475 kfree(vqs); 1620 kfree(vqs);
1476 1621
1477 return 0; 1622 return 0;
1478 1623
1479free_names: 1624free:
1480 kfree(io_names);
1481free_callbacks:
1482 kfree(io_callbacks);
1483free_outvqs:
1484 kfree(portdev->out_vqs); 1625 kfree(portdev->out_vqs);
1485free_invqs:
1486 kfree(portdev->in_vqs); 1626 kfree(portdev->in_vqs);
1487free_vqs: 1627 kfree(io_names);
1628 kfree(io_callbacks);
1488 kfree(vqs); 1629 kfree(vqs);
1489fail: 1630
1490 return err; 1631 return err;
1491} 1632}
1492 1633
@@ -1536,17 +1677,12 @@ static int __devinit virtcons_probe(struct virtio_device *vdev)
1536 portdev->config.max_nr_ports = 1; 1677 portdev->config.max_nr_ports = 1;
1537 if (virtio_has_feature(vdev, VIRTIO_CONSOLE_F_MULTIPORT)) { 1678 if (virtio_has_feature(vdev, VIRTIO_CONSOLE_F_MULTIPORT)) {
1538 multiport = true; 1679 multiport = true;
1539 vdev->features[0] |= 1 << VIRTIO_CONSOLE_F_MULTIPORT;
1540
1541 vdev->config->get(vdev, offsetof(struct virtio_console_config, 1680 vdev->config->get(vdev, offsetof(struct virtio_console_config,
1542 max_nr_ports), 1681 max_nr_ports),
1543 &portdev->config.max_nr_ports, 1682 &portdev->config.max_nr_ports,
1544 sizeof(portdev->config.max_nr_ports)); 1683 sizeof(portdev->config.max_nr_ports));
1545 } 1684 }
1546 1685
1547 /* Let the Host know we support multiple ports.*/
1548 vdev->config->finalize_features(vdev);
1549
1550 err = init_vqs(portdev); 1686 err = init_vqs(portdev);
1551 if (err < 0) { 1687 if (err < 0) {
1552 dev_err(&vdev->dev, "Error %d initializing vqs\n", err); 1688 dev_err(&vdev->dev, "Error %d initializing vqs\n", err);
@@ -1577,6 +1713,10 @@ static int __devinit virtcons_probe(struct virtio_device *vdev)
1577 add_port(portdev, 0); 1713 add_port(portdev, 0);
1578 } 1714 }
1579 1715
1716 spin_lock_irq(&pdrvdata_lock);
1717 list_add_tail(&portdev->list, &pdrvdata.portdevs);
1718 spin_unlock_irq(&pdrvdata_lock);
1719
1580 __send_control_msg(portdev, VIRTIO_CONSOLE_BAD_ID, 1720 __send_control_msg(portdev, VIRTIO_CONSOLE_BAD_ID,
1581 VIRTIO_CONSOLE_DEVICE_READY, 1); 1721 VIRTIO_CONSOLE_DEVICE_READY, 1);
1582 return 0; 1722 return 0;
@@ -1600,23 +1740,41 @@ static void virtcons_remove(struct virtio_device *vdev)
1600{ 1740{
1601 struct ports_device *portdev; 1741 struct ports_device *portdev;
1602 struct port *port, *port2; 1742 struct port *port, *port2;
1603 struct port_buffer *buf;
1604 unsigned int len;
1605 1743
1606 portdev = vdev->priv; 1744 portdev = vdev->priv;
1607 1745
1746 spin_lock_irq(&pdrvdata_lock);
1747 list_del(&portdev->list);
1748 spin_unlock_irq(&pdrvdata_lock);
1749
1750 /* Disable interrupts for vqs */
1751 vdev->config->reset(vdev);
1752 /* Finish up work that's lined up */
1608 cancel_work_sync(&portdev->control_work); 1753 cancel_work_sync(&portdev->control_work);
1609 1754
1610 list_for_each_entry_safe(port, port2, &portdev->ports, list) 1755 list_for_each_entry_safe(port, port2, &portdev->ports, list)
1611 remove_port(port); 1756 unplug_port(port);
1612 1757
1613 unregister_chrdev(portdev->chr_major, "virtio-portsdev"); 1758 unregister_chrdev(portdev->chr_major, "virtio-portsdev");
1614 1759
1615 while ((buf = virtqueue_get_buf(portdev->c_ivq, &len))) 1760 /*
1616 free_buf(buf); 1761 * When yanking out a device, we immediately lose the
1762 * (device-side) queues. So there's no point in keeping the
1763 * guest side around till we drop our final reference. This
1764 * also means that any ports which are in an open state will
1765 * have to just stop using the port, as the vqs are going
1766 * away.
1767 */
1768 if (use_multiport(portdev)) {
1769 struct port_buffer *buf;
1770 unsigned int len;
1617 1771
1618 while ((buf = virtqueue_detach_unused_buf(portdev->c_ivq))) 1772 while ((buf = virtqueue_get_buf(portdev->c_ivq, &len)))
1619 free_buf(buf); 1773 free_buf(buf);
1774
1775 while ((buf = virtqueue_detach_unused_buf(portdev->c_ivq)))
1776 free_buf(buf);
1777 }
1620 1778
1621 vdev->config->del_vqs(vdev); 1779 vdev->config->del_vqs(vdev);
1622 kfree(portdev->in_vqs); 1780 kfree(portdev->in_vqs);
@@ -1663,6 +1821,7 @@ static int __init init(void)
1663 PTR_ERR(pdrvdata.debugfs_dir)); 1821 PTR_ERR(pdrvdata.debugfs_dir));
1664 } 1822 }
1665 INIT_LIST_HEAD(&pdrvdata.consoles); 1823 INIT_LIST_HEAD(&pdrvdata.consoles);
1824 INIT_LIST_HEAD(&pdrvdata.portdevs);
1666 1825
1667 return register_virtio_driver(&virtio_console); 1826 return register_virtio_driver(&virtio_console);
1668} 1827}
diff --git a/drivers/char/vme_scc.c b/drivers/char/vme_scc.c
deleted file mode 100644
index 12de1202d22c..000000000000
--- a/drivers/char/vme_scc.c
+++ /dev/null
@@ -1,1145 +0,0 @@
1/*
2 * drivers/char/vme_scc.c: MVME147, MVME162, BVME6000 SCC serial ports
3 * implementation.
4 * Copyright 1999 Richard Hirst <richard@sleepie.demon.co.uk>
5 *
6 * Based on atari_SCC.c which was
7 * Copyright 1994-95 Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de>
8 * Partially based on PC-Linux serial.c by Linus Torvalds and Theodore Ts'o
9 *
10 * This file is subject to the terms and conditions of the GNU General Public
11 * License. See the file COPYING in the main directory of this archive
12 * for more details.
13 *
14 */
15
16#include <linux/module.h>
17#include <linux/kdev_t.h>
18#include <asm/io.h>
19#include <linux/kernel.h>
20#include <linux/ioport.h>
21#include <linux/interrupt.h>
22#include <linux/errno.h>
23#include <linux/tty.h>
24#include <linux/tty_flip.h>
25#include <linux/mm.h>
26#include <linux/serial.h>
27#include <linux/fcntl.h>
28#include <linux/major.h>
29#include <linux/delay.h>
30#include <linux/miscdevice.h>
31#include <linux/console.h>
32#include <linux/init.h>
33#include <asm/setup.h>
34#include <asm/bootinfo.h>
35
36#ifdef CONFIG_MVME147_SCC
37#include <asm/mvme147hw.h>
38#endif
39#ifdef CONFIG_MVME162_SCC
40#include <asm/mvme16xhw.h>
41#endif
42#ifdef CONFIG_BVME6000_SCC
43#include <asm/bvme6000hw.h>
44#endif
45
46#include <linux/generic_serial.h>
47#include "scc.h"
48
49
50#define CHANNEL_A 0
51#define CHANNEL_B 1
52
53#define SCC_MINOR_BASE 64
54
55/* Shadows for all SCC write registers */
56static unsigned char scc_shadow[2][16];
57
58/* Location to access for SCC register access delay */
59static volatile unsigned char *scc_del = NULL;
60
61/* To keep track of STATUS_REG state for detection of Ext/Status int source */
62static unsigned char scc_last_status_reg[2];
63
64/***************************** Prototypes *****************************/
65
66/* Function prototypes */
67static void scc_disable_tx_interrupts(void * ptr);
68static void scc_enable_tx_interrupts(void * ptr);
69static void scc_disable_rx_interrupts(void * ptr);
70static void scc_enable_rx_interrupts(void * ptr);
71static int scc_carrier_raised(struct tty_port *port);
72static void scc_shutdown_port(void * ptr);
73static int scc_set_real_termios(void *ptr);
74static void scc_hungup(void *ptr);
75static void scc_close(void *ptr);
76static int scc_chars_in_buffer(void * ptr);
77static int scc_open(struct tty_struct * tty, struct file * filp);
78static int scc_ioctl(struct tty_struct * tty, struct file * filp,
79 unsigned int cmd, unsigned long arg);
80static void scc_throttle(struct tty_struct *tty);
81static void scc_unthrottle(struct tty_struct *tty);
82static irqreturn_t scc_tx_int(int irq, void *data);
83static irqreturn_t scc_rx_int(int irq, void *data);
84static irqreturn_t scc_stat_int(int irq, void *data);
85static irqreturn_t scc_spcond_int(int irq, void *data);
86static void scc_setsignals(struct scc_port *port, int dtr, int rts);
87static int scc_break_ctl(struct tty_struct *tty, int break_state);
88
89static struct tty_driver *scc_driver;
90
91static struct scc_port scc_ports[2];
92
93/*---------------------------------------------------------------------------
94 * Interface from generic_serial.c back here
95 *--------------------------------------------------------------------------*/
96
97static struct real_driver scc_real_driver = {
98 scc_disable_tx_interrupts,
99 scc_enable_tx_interrupts,
100 scc_disable_rx_interrupts,
101 scc_enable_rx_interrupts,
102 scc_shutdown_port,
103 scc_set_real_termios,
104 scc_chars_in_buffer,
105 scc_close,
106 scc_hungup,
107 NULL
108};
109
110
111static const struct tty_operations scc_ops = {
112 .open = scc_open,
113 .close = gs_close,
114 .write = gs_write,
115 .put_char = gs_put_char,
116 .flush_chars = gs_flush_chars,
117 .write_room = gs_write_room,
118 .chars_in_buffer = gs_chars_in_buffer,
119 .flush_buffer = gs_flush_buffer,
120 .ioctl = scc_ioctl,
121 .throttle = scc_throttle,
122 .unthrottle = scc_unthrottle,
123 .set_termios = gs_set_termios,
124 .stop = gs_stop,
125 .start = gs_start,
126 .hangup = gs_hangup,
127 .break_ctl = scc_break_ctl,
128};
129
130static const struct tty_port_operations scc_port_ops = {
131 .carrier_raised = scc_carrier_raised,
132};
133
134/*----------------------------------------------------------------------------
135 * vme_scc_init() and support functions
136 *---------------------------------------------------------------------------*/
137
138static int __init scc_init_drivers(void)
139{
140 int error;
141
142 scc_driver = alloc_tty_driver(2);
143 if (!scc_driver)
144 return -ENOMEM;
145 scc_driver->owner = THIS_MODULE;
146 scc_driver->driver_name = "scc";
147 scc_driver->name = "ttyS";
148 scc_driver->major = TTY_MAJOR;
149 scc_driver->minor_start = SCC_MINOR_BASE;
150 scc_driver->type = TTY_DRIVER_TYPE_SERIAL;
151 scc_driver->subtype = SERIAL_TYPE_NORMAL;
152 scc_driver->init_termios = tty_std_termios;
153 scc_driver->init_termios.c_cflag =
154 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
155 scc_driver->init_termios.c_ispeed = 9600;
156 scc_driver->init_termios.c_ospeed = 9600;
157 scc_driver->flags = TTY_DRIVER_REAL_RAW;
158 tty_set_operations(scc_driver, &scc_ops);
159
160 if ((error = tty_register_driver(scc_driver))) {
161 printk(KERN_ERR "scc: Couldn't register scc driver, error = %d\n",
162 error);
163 put_tty_driver(scc_driver);
164 return 1;
165 }
166
167 return 0;
168}
169
170
171/* ports[] array is indexed by line no (i.e. [0] for ttyS0, [1] for ttyS1).
172 */
173
174static void __init scc_init_portstructs(void)
175{
176 struct scc_port *port;
177 int i;
178
179 for (i = 0; i < 2; i++) {
180 port = scc_ports + i;
181 tty_port_init(&port->gs.port);
182 port->gs.port.ops = &scc_port_ops;
183 port->gs.magic = SCC_MAGIC;
184 port->gs.close_delay = HZ/2;
185 port->gs.closing_wait = 30 * HZ;
186 port->gs.rd = &scc_real_driver;
187#ifdef NEW_WRITE_LOCKING
188 port->gs.port_write_mutex = MUTEX;
189#endif
190 init_waitqueue_head(&port->gs.port.open_wait);
191 init_waitqueue_head(&port->gs.port.close_wait);
192 }
193}
194
195
196#ifdef CONFIG_MVME147_SCC
197static int __init mvme147_scc_init(void)
198{
199 struct scc_port *port;
200 int error;
201
202 printk(KERN_INFO "SCC: MVME147 Serial Driver\n");
203 /* Init channel A */
204 port = &scc_ports[0];
205 port->channel = CHANNEL_A;
206 port->ctrlp = (volatile unsigned char *)M147_SCC_A_ADDR;
207 port->datap = port->ctrlp + 1;
208 port->port_a = &scc_ports[0];
209 port->port_b = &scc_ports[1];
210 error = request_irq(MVME147_IRQ_SCCA_TX, scc_tx_int, IRQF_DISABLED,
211 "SCC-A TX", port);
212 if (error)
213 goto fail;
214 error = request_irq(MVME147_IRQ_SCCA_STAT, scc_stat_int, IRQF_DISABLED,
215 "SCC-A status", port);
216 if (error)
217 goto fail_free_a_tx;
218 error = request_irq(MVME147_IRQ_SCCA_RX, scc_rx_int, IRQF_DISABLED,
219 "SCC-A RX", port);
220 if (error)
221 goto fail_free_a_stat;
222 error = request_irq(MVME147_IRQ_SCCA_SPCOND, scc_spcond_int,
223 IRQF_DISABLED, "SCC-A special cond", port);
224 if (error)
225 goto fail_free_a_rx;
226
227 {
228 SCC_ACCESS_INIT(port);
229
230 /* disable interrupts for this channel */
231 SCCwrite(INT_AND_DMA_REG, 0);
232 /* Set the interrupt vector */
233 SCCwrite(INT_VECTOR_REG, MVME147_IRQ_SCC_BASE);
234 /* Interrupt parameters: vector includes status, status low */
235 SCCwrite(MASTER_INT_CTRL, MIC_VEC_INCL_STAT);
236 SCCmod(MASTER_INT_CTRL, 0xff, MIC_MASTER_INT_ENAB);
237 }
238
239 /* Init channel B */
240 port = &scc_ports[1];
241 port->channel = CHANNEL_B;
242 port->ctrlp = (volatile unsigned char *)M147_SCC_B_ADDR;
243 port->datap = port->ctrlp + 1;
244 port->port_a = &scc_ports[0];
245 port->port_b = &scc_ports[1];
246 error = request_irq(MVME147_IRQ_SCCB_TX, scc_tx_int, IRQF_DISABLED,
247 "SCC-B TX", port);
248 if (error)
249 goto fail_free_a_spcond;
250 error = request_irq(MVME147_IRQ_SCCB_STAT, scc_stat_int, IRQF_DISABLED,
251 "SCC-B status", port);
252 if (error)
253 goto fail_free_b_tx;
254 error = request_irq(MVME147_IRQ_SCCB_RX, scc_rx_int, IRQF_DISABLED,
255 "SCC-B RX", port);
256 if (error)
257 goto fail_free_b_stat;
258 error = request_irq(MVME147_IRQ_SCCB_SPCOND, scc_spcond_int,
259 IRQF_DISABLED, "SCC-B special cond", port);
260 if (error)
261 goto fail_free_b_rx;
262
263 {
264 SCC_ACCESS_INIT(port);
265
266 /* disable interrupts for this channel */
267 SCCwrite(INT_AND_DMA_REG, 0);
268 }
269
270 /* Ensure interrupts are enabled in the PCC chip */
271 m147_pcc->serial_cntrl=PCC_LEVEL_SERIAL|PCC_INT_ENAB;
272
273 /* Initialise the tty driver structures and register */
274 scc_init_portstructs();
275 scc_init_drivers();
276
277 return 0;
278
279fail_free_b_rx:
280 free_irq(MVME147_IRQ_SCCB_RX, port);
281fail_free_b_stat:
282 free_irq(MVME147_IRQ_SCCB_STAT, port);
283fail_free_b_tx:
284 free_irq(MVME147_IRQ_SCCB_TX, port);
285fail_free_a_spcond:
286 free_irq(MVME147_IRQ_SCCA_SPCOND, port);
287fail_free_a_rx:
288 free_irq(MVME147_IRQ_SCCA_RX, port);
289fail_free_a_stat:
290 free_irq(MVME147_IRQ_SCCA_STAT, port);
291fail_free_a_tx:
292 free_irq(MVME147_IRQ_SCCA_TX, port);
293fail:
294 return error;
295}
296#endif
297
298
299#ifdef CONFIG_MVME162_SCC
300static int __init mvme162_scc_init(void)
301{
302 struct scc_port *port;
303 int error;
304
305 if (!(mvme16x_config & MVME16x_CONFIG_GOT_SCCA))
306 return (-ENODEV);
307
308 printk(KERN_INFO "SCC: MVME162 Serial Driver\n");
309 /* Init channel A */
310 port = &scc_ports[0];
311 port->channel = CHANNEL_A;
312 port->ctrlp = (volatile unsigned char *)MVME_SCC_A_ADDR;
313 port->datap = port->ctrlp + 2;
314 port->port_a = &scc_ports[0];
315 port->port_b = &scc_ports[1];
316 error = request_irq(MVME162_IRQ_SCCA_TX, scc_tx_int, IRQF_DISABLED,
317 "SCC-A TX", port);
318 if (error)
319 goto fail;
320 error = request_irq(MVME162_IRQ_SCCA_STAT, scc_stat_int, IRQF_DISABLED,
321 "SCC-A status", port);
322 if (error)
323 goto fail_free_a_tx;
324 error = request_irq(MVME162_IRQ_SCCA_RX, scc_rx_int, IRQF_DISABLED,
325 "SCC-A RX", port);
326 if (error)
327 goto fail_free_a_stat;
328 error = request_irq(MVME162_IRQ_SCCA_SPCOND, scc_spcond_int,
329 IRQF_DISABLED, "SCC-A special cond", port);
330 if (error)
331 goto fail_free_a_rx;
332
333 {
334 SCC_ACCESS_INIT(port);
335
336 /* disable interrupts for this channel */
337 SCCwrite(INT_AND_DMA_REG, 0);
338 /* Set the interrupt vector */
339 SCCwrite(INT_VECTOR_REG, MVME162_IRQ_SCC_BASE);
340 /* Interrupt parameters: vector includes status, status low */
341 SCCwrite(MASTER_INT_CTRL, MIC_VEC_INCL_STAT);
342 SCCmod(MASTER_INT_CTRL, 0xff, MIC_MASTER_INT_ENAB);
343 }
344
345 /* Init channel B */
346 port = &scc_ports[1];
347 port->channel = CHANNEL_B;
348 port->ctrlp = (volatile unsigned char *)MVME_SCC_B_ADDR;
349 port->datap = port->ctrlp + 2;
350 port->port_a = &scc_ports[0];
351 port->port_b = &scc_ports[1];
352 error = request_irq(MVME162_IRQ_SCCB_TX, scc_tx_int, IRQF_DISABLED,
353 "SCC-B TX", port);
354 if (error)
355 goto fail_free_a_spcond;
356 error = request_irq(MVME162_IRQ_SCCB_STAT, scc_stat_int, IRQF_DISABLED,
357 "SCC-B status", port);
358 if (error)
359 goto fail_free_b_tx;
360 error = request_irq(MVME162_IRQ_SCCB_RX, scc_rx_int, IRQF_DISABLED,
361 "SCC-B RX", port);
362 if (error)
363 goto fail_free_b_stat;
364 error = request_irq(MVME162_IRQ_SCCB_SPCOND, scc_spcond_int,
365 IRQF_DISABLED, "SCC-B special cond", port);
366 if (error)
367 goto fail_free_b_rx;
368
369 {
370 SCC_ACCESS_INIT(port); /* Either channel will do */
371
372 /* disable interrupts for this channel */
373 SCCwrite(INT_AND_DMA_REG, 0);
374 }
375
376 /* Ensure interrupts are enabled in the MC2 chip */
377 *(volatile char *)0xfff4201d = 0x14;
378
379 /* Initialise the tty driver structures and register */
380 scc_init_portstructs();
381 scc_init_drivers();
382
383 return 0;
384
385fail_free_b_rx:
386 free_irq(MVME162_IRQ_SCCB_RX, port);
387fail_free_b_stat:
388 free_irq(MVME162_IRQ_SCCB_STAT, port);
389fail_free_b_tx:
390 free_irq(MVME162_IRQ_SCCB_TX, port);
391fail_free_a_spcond:
392 free_irq(MVME162_IRQ_SCCA_SPCOND, port);
393fail_free_a_rx:
394 free_irq(MVME162_IRQ_SCCA_RX, port);
395fail_free_a_stat:
396 free_irq(MVME162_IRQ_SCCA_STAT, port);
397fail_free_a_tx:
398 free_irq(MVME162_IRQ_SCCA_TX, port);
399fail:
400 return error;
401}
402#endif
403
404
405#ifdef CONFIG_BVME6000_SCC
406static int __init bvme6000_scc_init(void)
407{
408 struct scc_port *port;
409 int error;
410
411 printk(KERN_INFO "SCC: BVME6000 Serial Driver\n");
412 /* Init channel A */
413 port = &scc_ports[0];
414 port->channel = CHANNEL_A;
415 port->ctrlp = (volatile unsigned char *)BVME_SCC_A_ADDR;
416 port->datap = port->ctrlp + 4;
417 port->port_a = &scc_ports[0];
418 port->port_b = &scc_ports[1];
419 error = request_irq(BVME_IRQ_SCCA_TX, scc_tx_int, IRQF_DISABLED,
420 "SCC-A TX", port);
421 if (error)
422 goto fail;
423 error = request_irq(BVME_IRQ_SCCA_STAT, scc_stat_int, IRQF_DISABLED,
424 "SCC-A status", port);
425 if (error)
426 goto fail_free_a_tx;
427 error = request_irq(BVME_IRQ_SCCA_RX, scc_rx_int, IRQF_DISABLED,
428 "SCC-A RX", port);
429 if (error)
430 goto fail_free_a_stat;
431 error = request_irq(BVME_IRQ_SCCA_SPCOND, scc_spcond_int,
432 IRQF_DISABLED, "SCC-A special cond", port);
433 if (error)
434 goto fail_free_a_rx;
435
436 {
437 SCC_ACCESS_INIT(port);
438
439 /* disable interrupts for this channel */
440 SCCwrite(INT_AND_DMA_REG, 0);
441 /* Set the interrupt vector */
442 SCCwrite(INT_VECTOR_REG, BVME_IRQ_SCC_BASE);
443 /* Interrupt parameters: vector includes status, status low */
444 SCCwrite(MASTER_INT_CTRL, MIC_VEC_INCL_STAT);
445 SCCmod(MASTER_INT_CTRL, 0xff, MIC_MASTER_INT_ENAB);
446 }
447
448 /* Init channel B */
449 port = &scc_ports[1];
450 port->channel = CHANNEL_B;
451 port->ctrlp = (volatile unsigned char *)BVME_SCC_B_ADDR;
452 port->datap = port->ctrlp + 4;
453 port->port_a = &scc_ports[0];
454 port->port_b = &scc_ports[1];
455 error = request_irq(BVME_IRQ_SCCB_TX, scc_tx_int, IRQF_DISABLED,
456 "SCC-B TX", port);
457 if (error)
458 goto fail_free_a_spcond;
459 error = request_irq(BVME_IRQ_SCCB_STAT, scc_stat_int, IRQF_DISABLED,
460 "SCC-B status", port);
461 if (error)
462 goto fail_free_b_tx;
463 error = request_irq(BVME_IRQ_SCCB_RX, scc_rx_int, IRQF_DISABLED,
464 "SCC-B RX", port);
465 if (error)
466 goto fail_free_b_stat;
467 error = request_irq(BVME_IRQ_SCCB_SPCOND, scc_spcond_int,
468 IRQF_DISABLED, "SCC-B special cond", port);
469 if (error)
470 goto fail_free_b_rx;
471
472 {
473 SCC_ACCESS_INIT(port); /* Either channel will do */
474
475 /* disable interrupts for this channel */
476 SCCwrite(INT_AND_DMA_REG, 0);
477 }
478
479 /* Initialise the tty driver structures and register */
480 scc_init_portstructs();
481 scc_init_drivers();
482
483 return 0;
484
485fail:
486 free_irq(BVME_IRQ_SCCA_STAT, port);
487fail_free_a_tx:
488 free_irq(BVME_IRQ_SCCA_RX, port);
489fail_free_a_stat:
490 free_irq(BVME_IRQ_SCCA_SPCOND, port);
491fail_free_a_rx:
492 free_irq(BVME_IRQ_SCCB_TX, port);
493fail_free_a_spcond:
494 free_irq(BVME_IRQ_SCCB_STAT, port);
495fail_free_b_tx:
496 free_irq(BVME_IRQ_SCCB_RX, port);
497fail_free_b_stat:
498 free_irq(BVME_IRQ_SCCB_SPCOND, port);
499fail_free_b_rx:
500 return error;
501}
502#endif
503
504
505static int __init vme_scc_init(void)
506{
507 int res = -ENODEV;
508
509#ifdef CONFIG_MVME147_SCC
510 if (MACH_IS_MVME147)
511 res = mvme147_scc_init();
512#endif
513#ifdef CONFIG_MVME162_SCC
514 if (MACH_IS_MVME16x)
515 res = mvme162_scc_init();
516#endif
517#ifdef CONFIG_BVME6000_SCC
518 if (MACH_IS_BVME6000)
519 res = bvme6000_scc_init();
520#endif
521 return res;
522}
523
524module_init(vme_scc_init);
525
526
527/*---------------------------------------------------------------------------
528 * Interrupt handlers
529 *--------------------------------------------------------------------------*/
530
531static irqreturn_t scc_rx_int(int irq, void *data)
532{
533 unsigned char ch;
534 struct scc_port *port = data;
535 struct tty_struct *tty = port->gs.port.tty;
536 SCC_ACCESS_INIT(port);
537
538 ch = SCCread_NB(RX_DATA_REG);
539 if (!tty) {
540 printk(KERN_WARNING "scc_rx_int with NULL tty!\n");
541 SCCwrite_NB(COMMAND_REG, CR_HIGHEST_IUS_RESET);
542 return IRQ_HANDLED;
543 }
544 tty_insert_flip_char(tty, ch, 0);
545
546 /* Check if another character is already ready; in that case, the
547 * spcond_int() function must be used, because this character may have an
548 * error condition that isn't signalled by the interrupt vector used!
549 */
550 if (SCCread(INT_PENDING_REG) &
551 (port->channel == CHANNEL_A ? IPR_A_RX : IPR_B_RX)) {
552 scc_spcond_int (irq, data);
553 return IRQ_HANDLED;
554 }
555
556 SCCwrite_NB(COMMAND_REG, CR_HIGHEST_IUS_RESET);
557
558 tty_flip_buffer_push(tty);
559 return IRQ_HANDLED;
560}
561
562
563static irqreturn_t scc_spcond_int(int irq, void *data)
564{
565 struct scc_port *port = data;
566 struct tty_struct *tty = port->gs.port.tty;
567 unsigned char stat, ch, err;
568 int int_pending_mask = port->channel == CHANNEL_A ?
569 IPR_A_RX : IPR_B_RX;
570 SCC_ACCESS_INIT(port);
571
572 if (!tty) {
573 printk(KERN_WARNING "scc_spcond_int with NULL tty!\n");
574 SCCwrite(COMMAND_REG, CR_ERROR_RESET);
575 SCCwrite_NB(COMMAND_REG, CR_HIGHEST_IUS_RESET);
576 return IRQ_HANDLED;
577 }
578 do {
579 stat = SCCread(SPCOND_STATUS_REG);
580 ch = SCCread_NB(RX_DATA_REG);
581
582 if (stat & SCSR_RX_OVERRUN)
583 err = TTY_OVERRUN;
584 else if (stat & SCSR_PARITY_ERR)
585 err = TTY_PARITY;
586 else if (stat & SCSR_CRC_FRAME_ERR)
587 err = TTY_FRAME;
588 else
589 err = 0;
590
591 tty_insert_flip_char(tty, ch, err);
592
593 /* ++TeSche: *All* errors have to be cleared manually,
594 * else the condition persists for the next chars
595 */
596 if (err)
597 SCCwrite(COMMAND_REG, CR_ERROR_RESET);
598
599 } while(SCCread(INT_PENDING_REG) & int_pending_mask);
600
601 SCCwrite_NB(COMMAND_REG, CR_HIGHEST_IUS_RESET);
602
603 tty_flip_buffer_push(tty);
604 return IRQ_HANDLED;
605}
606
607
608static irqreturn_t scc_tx_int(int irq, void *data)
609{
610 struct scc_port *port = data;
611 SCC_ACCESS_INIT(port);
612
613 if (!port->gs.port.tty) {
614 printk(KERN_WARNING "scc_tx_int with NULL tty!\n");
615 SCCmod (INT_AND_DMA_REG, ~IDR_TX_INT_ENAB, 0);
616 SCCwrite(COMMAND_REG, CR_TX_PENDING_RESET);
617 SCCwrite_NB(COMMAND_REG, CR_HIGHEST_IUS_RESET);
618 return IRQ_HANDLED;
619 }
620 while ((SCCread_NB(STATUS_REG) & SR_TX_BUF_EMPTY)) {
621 if (port->x_char) {
622 SCCwrite(TX_DATA_REG, port->x_char);
623 port->x_char = 0;
624 }
625 else if ((port->gs.xmit_cnt <= 0) ||
626 port->gs.port.tty->stopped ||
627 port->gs.port.tty->hw_stopped)
628 break;
629 else {
630 SCCwrite(TX_DATA_REG, port->gs.xmit_buf[port->gs.xmit_tail++]);
631 port->gs.xmit_tail = port->gs.xmit_tail & (SERIAL_XMIT_SIZE-1);
632 if (--port->gs.xmit_cnt <= 0)
633 break;
634 }
635 }
636 if ((port->gs.xmit_cnt <= 0) || port->gs.port.tty->stopped ||
637 port->gs.port.tty->hw_stopped) {
638 /* disable tx interrupts */
639 SCCmod (INT_AND_DMA_REG, ~IDR_TX_INT_ENAB, 0);
640 SCCwrite(COMMAND_REG, CR_TX_PENDING_RESET); /* disable tx_int on next tx underrun? */
641 port->gs.port.flags &= ~GS_TX_INTEN;
642 }
643 if (port->gs.port.tty && port->gs.xmit_cnt <= port->gs.wakeup_chars)
644 tty_wakeup(port->gs.port.tty);
645
646 SCCwrite_NB(COMMAND_REG, CR_HIGHEST_IUS_RESET);
647 return IRQ_HANDLED;
648}
649
650
651static irqreturn_t scc_stat_int(int irq, void *data)
652{
653 struct scc_port *port = data;
654 unsigned channel = port->channel;
655 unsigned char last_sr, sr, changed;
656 SCC_ACCESS_INIT(port);
657
658 last_sr = scc_last_status_reg[channel];
659 sr = scc_last_status_reg[channel] = SCCread_NB(STATUS_REG);
660 changed = last_sr ^ sr;
661
662 if (changed & SR_DCD) {
663 port->c_dcd = !!(sr & SR_DCD);
664 if (!(port->gs.port.flags & ASYNC_CHECK_CD))
665 ; /* Don't report DCD changes */
666 else if (port->c_dcd) {
667 wake_up_interruptible(&port->gs.port.open_wait);
668 }
669 else {
670 if (port->gs.port.tty)
671 tty_hangup (port->gs.port.tty);
672 }
673 }
674 SCCwrite(COMMAND_REG, CR_EXTSTAT_RESET);
675 SCCwrite_NB(COMMAND_REG, CR_HIGHEST_IUS_RESET);
676 return IRQ_HANDLED;
677}
678
679
680/*---------------------------------------------------------------------------
681 * generic_serial.c callback funtions
682 *--------------------------------------------------------------------------*/
683
684static void scc_disable_tx_interrupts(void *ptr)
685{
686 struct scc_port *port = ptr;
687 unsigned long flags;
688 SCC_ACCESS_INIT(port);
689
690 local_irq_save(flags);
691 SCCmod(INT_AND_DMA_REG, ~IDR_TX_INT_ENAB, 0);
692 port->gs.port.flags &= ~GS_TX_INTEN;
693 local_irq_restore(flags);
694}
695
696
697static void scc_enable_tx_interrupts(void *ptr)
698{
699 struct scc_port *port = ptr;
700 unsigned long flags;
701 SCC_ACCESS_INIT(port);
702
703 local_irq_save(flags);
704 SCCmod(INT_AND_DMA_REG, 0xff, IDR_TX_INT_ENAB);
705 /* restart the transmitter */
706 scc_tx_int (0, port);
707 local_irq_restore(flags);
708}
709
710
711static void scc_disable_rx_interrupts(void *ptr)
712{
713 struct scc_port *port = ptr;
714 unsigned long flags;
715 SCC_ACCESS_INIT(port);
716
717 local_irq_save(flags);
718 SCCmod(INT_AND_DMA_REG,
719 ~(IDR_RX_INT_MASK|IDR_PARERR_AS_SPCOND|IDR_EXTSTAT_INT_ENAB), 0);
720 local_irq_restore(flags);
721}
722
723
724static void scc_enable_rx_interrupts(void *ptr)
725{
726 struct scc_port *port = ptr;
727 unsigned long flags;
728 SCC_ACCESS_INIT(port);
729
730 local_irq_save(flags);
731 SCCmod(INT_AND_DMA_REG, 0xff,
732 IDR_EXTSTAT_INT_ENAB|IDR_PARERR_AS_SPCOND|IDR_RX_INT_ALL);
733 local_irq_restore(flags);
734}
735
736
737static int scc_carrier_raised(struct tty_port *port)
738{
739 struct scc_port *sc = container_of(port, struct scc_port, gs.port);
740 unsigned channel = sc->channel;
741
742 return !!(scc_last_status_reg[channel] & SR_DCD);
743}
744
745
746static void scc_shutdown_port(void *ptr)
747{
748 struct scc_port *port = ptr;
749
750 port->gs.port.flags &= ~ GS_ACTIVE;
751 if (port->gs.port.tty && (port->gs.port.tty->termios->c_cflag & HUPCL)) {
752 scc_setsignals (port, 0, 0);
753 }
754}
755
756
757static int scc_set_real_termios (void *ptr)
758{
759 /* the SCC has char sizes 5,7,6,8 in that order! */
760 static int chsize_map[4] = { 0, 2, 1, 3 };
761 unsigned cflag, baud, chsize, channel, brgval = 0;
762 unsigned long flags;
763 struct scc_port *port = ptr;
764 SCC_ACCESS_INIT(port);
765
766 if (!port->gs.port.tty || !port->gs.port.tty->termios) return 0;
767
768 channel = port->channel;
769
770 if (channel == CHANNEL_A)
771 return 0; /* Settings controlled by boot PROM */
772
773 cflag = port->gs.port.tty->termios->c_cflag;
774 baud = port->gs.baud;
775 chsize = (cflag & CSIZE) >> 4;
776
777 if (baud == 0) {
778 /* speed == 0 -> drop DTR */
779 local_irq_save(flags);
780 SCCmod(TX_CTRL_REG, ~TCR_DTR, 0);
781 local_irq_restore(flags);
782 return 0;
783 }
784 else if ((MACH_IS_MVME16x && (baud < 50 || baud > 38400)) ||
785 (MACH_IS_MVME147 && (baud < 50 || baud > 19200)) ||
786 (MACH_IS_BVME6000 &&(baud < 50 || baud > 76800))) {
787 printk(KERN_NOTICE "SCC: Bad speed requested, %d\n", baud);
788 return 0;
789 }
790
791 if (cflag & CLOCAL)
792 port->gs.port.flags &= ~ASYNC_CHECK_CD;
793 else
794 port->gs.port.flags |= ASYNC_CHECK_CD;
795
796#ifdef CONFIG_MVME147_SCC
797 if (MACH_IS_MVME147)
798 brgval = (M147_SCC_PCLK + baud/2) / (16 * 2 * baud) - 2;
799#endif
800#ifdef CONFIG_MVME162_SCC
801 if (MACH_IS_MVME16x)
802 brgval = (MVME_SCC_PCLK + baud/2) / (16 * 2 * baud) - 2;
803#endif
804#ifdef CONFIG_BVME6000_SCC
805 if (MACH_IS_BVME6000)
806 brgval = (BVME_SCC_RTxC + baud/2) / (16 * 2 * baud) - 2;
807#endif
808 /* Now we have all parameters and can go to set them: */
809 local_irq_save(flags);
810
811 /* receiver's character size and auto-enables */
812 SCCmod(RX_CTRL_REG, ~(RCR_CHSIZE_MASK|RCR_AUTO_ENAB_MODE),
813 (chsize_map[chsize] << 6) |
814 ((cflag & CRTSCTS) ? RCR_AUTO_ENAB_MODE : 0));
815 /* parity and stop bits (both, Tx and Rx), clock mode never changes */
816 SCCmod (AUX1_CTRL_REG,
817 ~(A1CR_PARITY_MASK | A1CR_MODE_MASK),
818 ((cflag & PARENB
819 ? (cflag & PARODD ? A1CR_PARITY_ODD : A1CR_PARITY_EVEN)
820 : A1CR_PARITY_NONE)
821 | (cflag & CSTOPB ? A1CR_MODE_ASYNC_2 : A1CR_MODE_ASYNC_1)));
822 /* sender's character size, set DTR for valid baud rate */
823 SCCmod(TX_CTRL_REG, ~TCR_CHSIZE_MASK, chsize_map[chsize] << 5 | TCR_DTR);
824 /* clock sources never change */
825 /* disable BRG before changing the value */
826 SCCmod(DPLL_CTRL_REG, ~DCR_BRG_ENAB, 0);
827 /* BRG value */
828 SCCwrite(TIMER_LOW_REG, brgval & 0xff);
829 SCCwrite(TIMER_HIGH_REG, (brgval >> 8) & 0xff);
830 /* BRG enable, and clock source never changes */
831 SCCmod(DPLL_CTRL_REG, 0xff, DCR_BRG_ENAB);
832
833 local_irq_restore(flags);
834
835 return 0;
836}
837
838
839static int scc_chars_in_buffer (void *ptr)
840{
841 struct scc_port *port = ptr;
842 SCC_ACCESS_INIT(port);
843
844 return (SCCread (SPCOND_STATUS_REG) & SCSR_ALL_SENT) ? 0 : 1;
845}
846
847
848/* Comment taken from sx.c (2.4.0):
849 I haven't the foggiest why the decrement use count has to happen
850 here. The whole linux serial drivers stuff needs to be redesigned.
851 My guess is that this is a hack to minimize the impact of a bug
852 elsewhere. Thinking about it some more. (try it sometime) Try
853 running minicom on a serial port that is driven by a modularized
854 driver. Have the modem hangup. Then remove the driver module. Then
855 exit minicom. I expect an "oops". -- REW */
856
857static void scc_hungup(void *ptr)
858{
859 scc_disable_tx_interrupts(ptr);
860 scc_disable_rx_interrupts(ptr);
861}
862
863
864static void scc_close(void *ptr)
865{
866 scc_disable_tx_interrupts(ptr);
867 scc_disable_rx_interrupts(ptr);
868}
869
870
871/*---------------------------------------------------------------------------
872 * Internal support functions
873 *--------------------------------------------------------------------------*/
874
875static void scc_setsignals(struct scc_port *port, int dtr, int rts)
876{
877 unsigned long flags;
878 unsigned char t;
879 SCC_ACCESS_INIT(port);
880
881 local_irq_save(flags);
882 t = SCCread(TX_CTRL_REG);
883 if (dtr >= 0) t = dtr? (t | TCR_DTR): (t & ~TCR_DTR);
884 if (rts >= 0) t = rts? (t | TCR_RTS): (t & ~TCR_RTS);
885 SCCwrite(TX_CTRL_REG, t);
886 local_irq_restore(flags);
887}
888
889
890static void scc_send_xchar(struct tty_struct *tty, char ch)
891{
892 struct scc_port *port = tty->driver_data;
893
894 port->x_char = ch;
895 if (ch)
896 scc_enable_tx_interrupts(port);
897}
898
899
900/*---------------------------------------------------------------------------
901 * Driver entrypoints referenced from above
902 *--------------------------------------------------------------------------*/
903
904static int scc_open (struct tty_struct * tty, struct file * filp)
905{
906 int line = tty->index;
907 int retval;
908 struct scc_port *port = &scc_ports[line];
909 int i, channel = port->channel;
910 unsigned long flags;
911 SCC_ACCESS_INIT(port);
912#if defined(CONFIG_MVME162_SCC) || defined(CONFIG_MVME147_SCC)
913 static const struct {
914 unsigned reg, val;
915 } mvme_init_tab[] = {
916 /* Values for MVME162 and MVME147 */
917 /* no parity, 1 stop bit, async, 1:16 */
918 { AUX1_CTRL_REG, A1CR_PARITY_NONE|A1CR_MODE_ASYNC_1|A1CR_CLKMODE_x16 },
919 /* parity error is special cond, ints disabled, no DMA */
920 { INT_AND_DMA_REG, IDR_PARERR_AS_SPCOND | IDR_RX_INT_DISAB },
921 /* Rx 8 bits/char, no auto enable, Rx off */
922 { RX_CTRL_REG, RCR_CHSIZE_8 },
923 /* DTR off, Tx 8 bits/char, RTS off, Tx off */
924 { TX_CTRL_REG, TCR_CHSIZE_8 },
925 /* special features off */
926 { AUX2_CTRL_REG, 0 },
927 { CLK_CTRL_REG, CCR_RXCLK_BRG | CCR_TXCLK_BRG },
928 { DPLL_CTRL_REG, DCR_BRG_ENAB | DCR_BRG_USE_PCLK },
929 /* Start Rx */
930 { RX_CTRL_REG, RCR_RX_ENAB | RCR_CHSIZE_8 },
931 /* Start Tx */
932 { TX_CTRL_REG, TCR_TX_ENAB | TCR_RTS | TCR_DTR | TCR_CHSIZE_8 },
933 /* Ext/Stat ints: DCD only */
934 { INT_CTRL_REG, ICR_ENAB_DCD_INT },
935 /* Reset Ext/Stat ints */
936 { COMMAND_REG, CR_EXTSTAT_RESET },
937 /* ...again */
938 { COMMAND_REG, CR_EXTSTAT_RESET },
939 };
940#endif
941#if defined(CONFIG_BVME6000_SCC)
942 static const struct {
943 unsigned reg, val;
944 } bvme_init_tab[] = {
945 /* Values for BVME6000 */
946 /* no parity, 1 stop bit, async, 1:16 */
947 { AUX1_CTRL_REG, A1CR_PARITY_NONE|A1CR_MODE_ASYNC_1|A1CR_CLKMODE_x16 },
948 /* parity error is special cond, ints disabled, no DMA */
949 { INT_AND_DMA_REG, IDR_PARERR_AS_SPCOND | IDR_RX_INT_DISAB },
950 /* Rx 8 bits/char, no auto enable, Rx off */
951 { RX_CTRL_REG, RCR_CHSIZE_8 },
952 /* DTR off, Tx 8 bits/char, RTS off, Tx off */
953 { TX_CTRL_REG, TCR_CHSIZE_8 },
954 /* special features off */
955 { AUX2_CTRL_REG, 0 },
956 { CLK_CTRL_REG, CCR_RTxC_XTAL | CCR_RXCLK_BRG | CCR_TXCLK_BRG },
957 { DPLL_CTRL_REG, DCR_BRG_ENAB },
958 /* Start Rx */
959 { RX_CTRL_REG, RCR_RX_ENAB | RCR_CHSIZE_8 },
960 /* Start Tx */
961 { TX_CTRL_REG, TCR_TX_ENAB | TCR_RTS | TCR_DTR | TCR_CHSIZE_8 },
962 /* Ext/Stat ints: DCD only */
963 { INT_CTRL_REG, ICR_ENAB_DCD_INT },
964 /* Reset Ext/Stat ints */
965 { COMMAND_REG, CR_EXTSTAT_RESET },
966 /* ...again */
967 { COMMAND_REG, CR_EXTSTAT_RESET },
968 };
969#endif
970 if (!(port->gs.port.flags & ASYNC_INITIALIZED)) {
971 local_irq_save(flags);
972#if defined(CONFIG_MVME147_SCC) || defined(CONFIG_MVME162_SCC)
973 if (MACH_IS_MVME147 || MACH_IS_MVME16x) {
974 for (i = 0; i < ARRAY_SIZE(mvme_init_tab); ++i)
975 SCCwrite(mvme_init_tab[i].reg, mvme_init_tab[i].val);
976 }
977#endif
978#if defined(CONFIG_BVME6000_SCC)
979 if (MACH_IS_BVME6000) {
980 for (i = 0; i < ARRAY_SIZE(bvme_init_tab); ++i)
981 SCCwrite(bvme_init_tab[i].reg, bvme_init_tab[i].val);
982 }
983#endif
984
985 /* remember status register for detection of DCD and CTS changes */
986 scc_last_status_reg[channel] = SCCread(STATUS_REG);
987
988 port->c_dcd = 0; /* Prevent initial 1->0 interrupt */
989 scc_setsignals (port, 1,1);
990 local_irq_restore(flags);
991 }
992
993 tty->driver_data = port;
994 port->gs.port.tty = tty;
995 port->gs.port.count++;
996 retval = gs_init_port(&port->gs);
997 if (retval) {
998 port->gs.port.count--;
999 return retval;
1000 }
1001 port->gs.port.flags |= GS_ACTIVE;
1002 retval = gs_block_til_ready(port, filp);
1003
1004 if (retval) {
1005 port->gs.port.count--;
1006 return retval;
1007 }
1008
1009 port->c_dcd = tty_port_carrier_raised(&port->gs.port);
1010
1011 scc_enable_rx_interrupts(port);
1012
1013 return 0;
1014}
1015
1016
1017static void scc_throttle (struct tty_struct * tty)
1018{
1019 struct scc_port *port = tty->driver_data;
1020 unsigned long flags;
1021 SCC_ACCESS_INIT(port);
1022
1023 if (tty->termios->c_cflag & CRTSCTS) {
1024 local_irq_save(flags);
1025 SCCmod(TX_CTRL_REG, ~TCR_RTS, 0);
1026 local_irq_restore(flags);
1027 }
1028 if (I_IXOFF(tty))
1029 scc_send_xchar(tty, STOP_CHAR(tty));
1030}
1031
1032
1033static void scc_unthrottle (struct tty_struct * tty)
1034{
1035 struct scc_port *port = tty->driver_data;
1036 unsigned long flags;
1037 SCC_ACCESS_INIT(port);
1038
1039 if (tty->termios->c_cflag & CRTSCTS) {
1040 local_irq_save(flags);
1041 SCCmod(TX_CTRL_REG, 0xff, TCR_RTS);
1042 local_irq_restore(flags);
1043 }
1044 if (I_IXOFF(tty))
1045 scc_send_xchar(tty, START_CHAR(tty));
1046}
1047
1048
1049static int scc_ioctl(struct tty_struct *tty, struct file *file,
1050 unsigned int cmd, unsigned long arg)
1051{
1052 return -ENOIOCTLCMD;
1053}
1054
1055
1056static int scc_break_ctl(struct tty_struct *tty, int break_state)
1057{
1058 struct scc_port *port = tty->driver_data;
1059 unsigned long flags;
1060 SCC_ACCESS_INIT(port);
1061
1062 local_irq_save(flags);
1063 SCCmod(TX_CTRL_REG, ~TCR_SEND_BREAK,
1064 break_state ? TCR_SEND_BREAK : 0);
1065 local_irq_restore(flags);
1066 return 0;
1067}
1068
1069
1070/*---------------------------------------------------------------------------
1071 * Serial console stuff...
1072 *--------------------------------------------------------------------------*/
1073
1074#define scc_delay() do { __asm__ __volatile__ (" nop; nop"); } while (0)
1075
1076static void scc_ch_write (char ch)
1077{
1078 volatile char *p = NULL;
1079
1080#ifdef CONFIG_MVME147_SCC
1081 if (MACH_IS_MVME147)
1082 p = (volatile char *)M147_SCC_A_ADDR;
1083#endif
1084#ifdef CONFIG_MVME162_SCC
1085 if (MACH_IS_MVME16x)
1086 p = (volatile char *)MVME_SCC_A_ADDR;
1087#endif
1088#ifdef CONFIG_BVME6000_SCC
1089 if (MACH_IS_BVME6000)
1090 p = (volatile char *)BVME_SCC_A_ADDR;
1091#endif
1092
1093 do {
1094 scc_delay();
1095 }
1096 while (!(*p & 4));
1097 scc_delay();
1098 *p = 8;
1099 scc_delay();
1100 *p = ch;
1101}
1102
1103/* The console must be locked when we get here. */
1104
1105static void scc_console_write (struct console *co, const char *str, unsigned count)
1106{
1107 unsigned long flags;
1108
1109 local_irq_save(flags);
1110
1111 while (count--)
1112 {
1113 if (*str == '\n')
1114 scc_ch_write ('\r');
1115 scc_ch_write (*str++);
1116 }
1117 local_irq_restore(flags);
1118}
1119
1120static struct tty_driver *scc_console_device(struct console *c, int *index)
1121{
1122 *index = c->index;
1123 return scc_driver;
1124}
1125
1126static struct console sercons = {
1127 .name = "ttyS",
1128 .write = scc_console_write,
1129 .device = scc_console_device,
1130 .flags = CON_PRINTBUFFER,
1131 .index = -1,
1132};
1133
1134
1135static int __init vme_scc_console_init(void)
1136{
1137 if (vme_brdtype == VME_TYPE_MVME147 ||
1138 vme_brdtype == VME_TYPE_MVME162 ||
1139 vme_brdtype == VME_TYPE_MVME172 ||
1140 vme_brdtype == VME_TYPE_BVME4000 ||
1141 vme_brdtype == VME_TYPE_BVME6000)
1142 register_console(&sercons);
1143 return 0;
1144}
1145console_initcall(vme_scc_console_init);
diff --git a/drivers/char/vt.c b/drivers/char/vt.c
deleted file mode 100644
index 281aada7b4a1..000000000000
--- a/drivers/char/vt.c
+++ /dev/null
@@ -1,4204 +0,0 @@
1/*
2 * linux/drivers/char/vt.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
6
7/*
8 * Hopefully this will be a rather complete VT102 implementation.
9 *
10 * Beeping thanks to John T Kohl.
11 *
12 * Virtual Consoles, Screen Blanking, Screen Dumping, Color, Graphics
13 * Chars, and VT100 enhancements by Peter MacDonald.
14 *
15 * Copy and paste function by Andrew Haylett,
16 * some enhancements by Alessandro Rubini.
17 *
18 * Code to check for different video-cards mostly by Galen Hunt,
19 * <g-hunt@ee.utah.edu>
20 *
21 * Rudimentary ISO 10646/Unicode/UTF-8 character set support by
22 * Markus Kuhn, <mskuhn@immd4.informatik.uni-erlangen.de>.
23 *
24 * Dynamic allocation of consoles, aeb@cwi.nl, May 1994
25 * Resizing of consoles, aeb, 940926
26 *
27 * Code for xterm like mouse click reporting by Peter Orbaek 20-Jul-94
28 * <poe@daimi.aau.dk>
29 *
30 * User-defined bell sound, new setterm control sequences and printk
31 * redirection by Martin Mares <mj@k332.feld.cvut.cz> 19-Nov-95
32 *
33 * APM screenblank bug fixed Takashi Manabe <manabe@roy.dsl.tutics.tut.jp>
34 *
35 * Merge with the abstract console driver by Geert Uytterhoeven
36 * <geert@linux-m68k.org>, Jan 1997.
37 *
38 * Original m68k console driver modifications by
39 *
40 * - Arno Griffioen <arno@usn.nl>
41 * - David Carter <carter@cs.bris.ac.uk>
42 *
43 * The abstract console driver provides a generic interface for a text
44 * console. It supports VGA text mode, frame buffer based graphical consoles
45 * and special graphics processors that are only accessible through some
46 * registers (e.g. a TMS340x0 GSP).
47 *
48 * The interface to the hardware is specified using a special structure
49 * (struct consw) which contains function pointers to console operations
50 * (see <linux/console.h> for more information).
51 *
52 * Support for changeable cursor shape
53 * by Pavel Machek <pavel@atrey.karlin.mff.cuni.cz>, August 1997
54 *
55 * Ported to i386 and con_scrolldelta fixed
56 * by Emmanuel Marty <core@ggi-project.org>, April 1998
57 *
58 * Resurrected character buffers in videoram plus lots of other trickery
59 * by Martin Mares <mj@atrey.karlin.mff.cuni.cz>, July 1998
60 *
61 * Removed old-style timers, introduced console_timer, made timer
62 * deletion SMP-safe. 17Jun00, Andrew Morton
63 *
64 * Removed console_lock, enabled interrupts across all console operations
65 * 13 March 2001, Andrew Morton
66 *
67 * Fixed UTF-8 mode so alternate charset modes always work according
68 * to control sequences interpreted in do_con_trol function
69 * preserving backward VT100 semigraphics compatibility,
70 * malformed UTF sequences represented as sequences of replacement glyphs,
71 * original codes or '?' as a last resort if replacement glyph is undefined
72 * by Adam Tla/lka <atlka@pg.gda.pl>, Aug 2006
73 */
74
75#include <linux/module.h>
76#include <linux/types.h>
77#include <linux/sched.h>
78#include <linux/tty.h>
79#include <linux/tty_flip.h>
80#include <linux/kernel.h>
81#include <linux/string.h>
82#include <linux/errno.h>
83#include <linux/kd.h>
84#include <linux/slab.h>
85#include <linux/major.h>
86#include <linux/mm.h>
87#include <linux/console.h>
88#include <linux/init.h>
89#include <linux/mutex.h>
90#include <linux/vt_kern.h>
91#include <linux/selection.h>
92#include <linux/smp_lock.h>
93#include <linux/tiocl.h>
94#include <linux/kbd_kern.h>
95#include <linux/consolemap.h>
96#include <linux/timer.h>
97#include <linux/interrupt.h>
98#include <linux/workqueue.h>
99#include <linux/pm.h>
100#include <linux/font.h>
101#include <linux/bitops.h>
102#include <linux/notifier.h>
103#include <linux/device.h>
104#include <linux/io.h>
105#include <asm/system.h>
106#include <linux/uaccess.h>
107#include <linux/kdb.h>
108#include <linux/ctype.h>
109
110#define MAX_NR_CON_DRIVER 16
111
112#define CON_DRIVER_FLAG_MODULE 1
113#define CON_DRIVER_FLAG_INIT 2
114#define CON_DRIVER_FLAG_ATTR 4
115
116struct con_driver {
117 const struct consw *con;
118 const char *desc;
119 struct device *dev;
120 int node;
121 int first;
122 int last;
123 int flag;
124};
125
126static struct con_driver registered_con_driver[MAX_NR_CON_DRIVER];
127const struct consw *conswitchp;
128
129/* A bitmap for codes <32. A bit of 1 indicates that the code
130 * corresponding to that bit number invokes some special action
131 * (such as cursor movement) and should not be displayed as a
132 * glyph unless the disp_ctrl mode is explicitly enabled.
133 */
134#define CTRL_ACTION 0x0d00ff81
135#define CTRL_ALWAYS 0x0800f501 /* Cannot be overridden by disp_ctrl */
136
137/*
138 * Here is the default bell parameters: 750HZ, 1/8th of a second
139 */
140#define DEFAULT_BELL_PITCH 750
141#define DEFAULT_BELL_DURATION (HZ/8)
142
143struct vc vc_cons [MAX_NR_CONSOLES];
144
145#ifndef VT_SINGLE_DRIVER
146static const struct consw *con_driver_map[MAX_NR_CONSOLES];
147#endif
148
149static int con_open(struct tty_struct *, struct file *);
150static void vc_init(struct vc_data *vc, unsigned int rows,
151 unsigned int cols, int do_clear);
152static void gotoxy(struct vc_data *vc, int new_x, int new_y);
153static void save_cur(struct vc_data *vc);
154static void reset_terminal(struct vc_data *vc, int do_clear);
155static void con_flush_chars(struct tty_struct *tty);
156static int set_vesa_blanking(char __user *p);
157static void set_cursor(struct vc_data *vc);
158static void hide_cursor(struct vc_data *vc);
159static void console_callback(struct work_struct *ignored);
160static void blank_screen_t(unsigned long dummy);
161static void set_palette(struct vc_data *vc);
162
163static int printable; /* Is console ready for printing? */
164int default_utf8 = true;
165module_param(default_utf8, int, S_IRUGO | S_IWUSR);
166int global_cursor_default = -1;
167module_param(global_cursor_default, int, S_IRUGO | S_IWUSR);
168
169static int cur_default = CUR_DEFAULT;
170module_param(cur_default, int, S_IRUGO | S_IWUSR);
171
172/*
173 * ignore_poke: don't unblank the screen when things are typed. This is
174 * mainly for the privacy of braille terminal users.
175 */
176static int ignore_poke;
177
178int do_poke_blanked_console;
179int console_blanked;
180
181static int vesa_blank_mode; /* 0:none 1:suspendV 2:suspendH 3:powerdown */
182static int vesa_off_interval;
183static int blankinterval = 10*60;
184core_param(consoleblank, blankinterval, int, 0444);
185
186static DECLARE_WORK(console_work, console_callback);
187
188/*
189 * fg_console is the current virtual console,
190 * last_console is the last used one,
191 * want_console is the console we want to switch to,
192 * saved_* variants are for save/restore around kernel debugger enter/leave
193 */
194int fg_console;
195int last_console;
196int want_console = -1;
197static int saved_fg_console;
198static int saved_last_console;
199static int saved_want_console;
200static int saved_vc_mode;
201static int saved_console_blanked;
202
203/*
204 * For each existing display, we have a pointer to console currently visible
205 * on that display, allowing consoles other than fg_console to be refreshed
206 * appropriately. Unless the low-level driver supplies its own display_fg
207 * variable, we use this one for the "master display".
208 */
209static struct vc_data *master_display_fg;
210
211/*
212 * Unfortunately, we need to delay tty echo when we're currently writing to the
213 * console since the code is (and always was) not re-entrant, so we schedule
214 * all flip requests to process context with schedule-task() and run it from
215 * console_callback().
216 */
217
218/*
219 * For the same reason, we defer scrollback to the console callback.
220 */
221static int scrollback_delta;
222
223/*
224 * Hook so that the power management routines can (un)blank
225 * the console on our behalf.
226 */
227int (*console_blank_hook)(int);
228
229static DEFINE_TIMER(console_timer, blank_screen_t, 0, 0);
230static int blank_state;
231static int blank_timer_expired;
232enum {
233 blank_off = 0,
234 blank_normal_wait,
235 blank_vesa_wait,
236};
237
238/*
239 * Notifier list for console events.
240 */
241static ATOMIC_NOTIFIER_HEAD(vt_notifier_list);
242
243int register_vt_notifier(struct notifier_block *nb)
244{
245 return atomic_notifier_chain_register(&vt_notifier_list, nb);
246}
247EXPORT_SYMBOL_GPL(register_vt_notifier);
248
249int unregister_vt_notifier(struct notifier_block *nb)
250{
251 return atomic_notifier_chain_unregister(&vt_notifier_list, nb);
252}
253EXPORT_SYMBOL_GPL(unregister_vt_notifier);
254
255static void notify_write(struct vc_data *vc, unsigned int unicode)
256{
257 struct vt_notifier_param param = { .vc = vc, unicode = unicode };
258 atomic_notifier_call_chain(&vt_notifier_list, VT_WRITE, &param);
259}
260
261static void notify_update(struct vc_data *vc)
262{
263 struct vt_notifier_param param = { .vc = vc };
264 atomic_notifier_call_chain(&vt_notifier_list, VT_UPDATE, &param);
265}
266/*
267 * Low-Level Functions
268 */
269
270#define IS_FG(vc) ((vc)->vc_num == fg_console)
271
272#ifdef VT_BUF_VRAM_ONLY
273#define DO_UPDATE(vc) 0
274#else
275#define DO_UPDATE(vc) (CON_IS_VISIBLE(vc) && !console_blanked)
276#endif
277
278static inline unsigned short *screenpos(struct vc_data *vc, int offset, int viewed)
279{
280 unsigned short *p;
281
282 if (!viewed)
283 p = (unsigned short *)(vc->vc_origin + offset);
284 else if (!vc->vc_sw->con_screen_pos)
285 p = (unsigned short *)(vc->vc_visible_origin + offset);
286 else
287 p = vc->vc_sw->con_screen_pos(vc, offset);
288 return p;
289}
290
291/* Called from the keyboard irq path.. */
292static inline void scrolldelta(int lines)
293{
294 /* FIXME */
295 /* scrolldelta needs some kind of consistency lock, but the BKL was
296 and still is not protecting versus the scheduled back end */
297 scrollback_delta += lines;
298 schedule_console_callback();
299}
300
301void schedule_console_callback(void)
302{
303 schedule_work(&console_work);
304}
305
306static void scrup(struct vc_data *vc, unsigned int t, unsigned int b, int nr)
307{
308 unsigned short *d, *s;
309
310 if (t+nr >= b)
311 nr = b - t - 1;
312 if (b > vc->vc_rows || t >= b || nr < 1)
313 return;
314 if (CON_IS_VISIBLE(vc) && vc->vc_sw->con_scroll(vc, t, b, SM_UP, nr))
315 return;
316 d = (unsigned short *)(vc->vc_origin + vc->vc_size_row * t);
317 s = (unsigned short *)(vc->vc_origin + vc->vc_size_row * (t + nr));
318 scr_memmovew(d, s, (b - t - nr) * vc->vc_size_row);
319 scr_memsetw(d + (b - t - nr) * vc->vc_cols, vc->vc_video_erase_char,
320 vc->vc_size_row * nr);
321}
322
323static void scrdown(struct vc_data *vc, unsigned int t, unsigned int b, int nr)
324{
325 unsigned short *s;
326 unsigned int step;
327
328 if (t+nr >= b)
329 nr = b - t - 1;
330 if (b > vc->vc_rows || t >= b || nr < 1)
331 return;
332 if (CON_IS_VISIBLE(vc) && vc->vc_sw->con_scroll(vc, t, b, SM_DOWN, nr))
333 return;
334 s = (unsigned short *)(vc->vc_origin + vc->vc_size_row * t);
335 step = vc->vc_cols * nr;
336 scr_memmovew(s + step, s, (b - t - nr) * vc->vc_size_row);
337 scr_memsetw(s, vc->vc_video_erase_char, 2 * step);
338}
339
340static void do_update_region(struct vc_data *vc, unsigned long start, int count)
341{
342#ifndef VT_BUF_VRAM_ONLY
343 unsigned int xx, yy, offset;
344 u16 *p;
345
346 p = (u16 *) start;
347 if (!vc->vc_sw->con_getxy) {
348 offset = (start - vc->vc_origin) / 2;
349 xx = offset % vc->vc_cols;
350 yy = offset / vc->vc_cols;
351 } else {
352 int nxx, nyy;
353 start = vc->vc_sw->con_getxy(vc, start, &nxx, &nyy);
354 xx = nxx; yy = nyy;
355 }
356 for(;;) {
357 u16 attrib = scr_readw(p) & 0xff00;
358 int startx = xx;
359 u16 *q = p;
360 while (xx < vc->vc_cols && count) {
361 if (attrib != (scr_readw(p) & 0xff00)) {
362 if (p > q)
363 vc->vc_sw->con_putcs(vc, q, p-q, yy, startx);
364 startx = xx;
365 q = p;
366 attrib = scr_readw(p) & 0xff00;
367 }
368 p++;
369 xx++;
370 count--;
371 }
372 if (p > q)
373 vc->vc_sw->con_putcs(vc, q, p-q, yy, startx);
374 if (!count)
375 break;
376 xx = 0;
377 yy++;
378 if (vc->vc_sw->con_getxy) {
379 p = (u16 *)start;
380 start = vc->vc_sw->con_getxy(vc, start, NULL, NULL);
381 }
382 }
383#endif
384}
385
386void update_region(struct vc_data *vc, unsigned long start, int count)
387{
388 WARN_CONSOLE_UNLOCKED();
389
390 if (DO_UPDATE(vc)) {
391 hide_cursor(vc);
392 do_update_region(vc, start, count);
393 set_cursor(vc);
394 }
395}
396
397/* Structure of attributes is hardware-dependent */
398
399static u8 build_attr(struct vc_data *vc, u8 _color, u8 _intensity, u8 _blink,
400 u8 _underline, u8 _reverse, u8 _italic)
401{
402 if (vc->vc_sw->con_build_attr)
403 return vc->vc_sw->con_build_attr(vc, _color, _intensity,
404 _blink, _underline, _reverse, _italic);
405
406#ifndef VT_BUF_VRAM_ONLY
407/*
408 * ++roman: I completely changed the attribute format for monochrome
409 * mode (!can_do_color). The formerly used MDA (monochrome display
410 * adapter) format didn't allow the combination of certain effects.
411 * Now the attribute is just a bit vector:
412 * Bit 0..1: intensity (0..2)
413 * Bit 2 : underline
414 * Bit 3 : reverse
415 * Bit 7 : blink
416 */
417 {
418 u8 a = _color;
419 if (!vc->vc_can_do_color)
420 return _intensity |
421 (_italic ? 2 : 0) |
422 (_underline ? 4 : 0) |
423 (_reverse ? 8 : 0) |
424 (_blink ? 0x80 : 0);
425 if (_italic)
426 a = (a & 0xF0) | vc->vc_itcolor;
427 else if (_underline)
428 a = (a & 0xf0) | vc->vc_ulcolor;
429 else if (_intensity == 0)
430 a = (a & 0xf0) | vc->vc_ulcolor;
431 if (_reverse)
432 a = ((a) & 0x88) | ((((a) >> 4) | ((a) << 4)) & 0x77);
433 if (_blink)
434 a ^= 0x80;
435 if (_intensity == 2)
436 a ^= 0x08;
437 if (vc->vc_hi_font_mask == 0x100)
438 a <<= 1;
439 return a;
440 }
441#else
442 return 0;
443#endif
444}
445
446static void update_attr(struct vc_data *vc)
447{
448 vc->vc_attr = build_attr(vc, vc->vc_color, vc->vc_intensity,
449 vc->vc_blink, vc->vc_underline,
450 vc->vc_reverse ^ vc->vc_decscnm, vc->vc_italic);
451 vc->vc_video_erase_char = (build_attr(vc, vc->vc_color, 1, vc->vc_blink, 0, vc->vc_decscnm, 0) << 8) | ' ';
452}
453
454/* Note: inverting the screen twice should revert to the original state */
455void invert_screen(struct vc_data *vc, int offset, int count, int viewed)
456{
457 unsigned short *p;
458
459 WARN_CONSOLE_UNLOCKED();
460
461 count /= 2;
462 p = screenpos(vc, offset, viewed);
463 if (vc->vc_sw->con_invert_region)
464 vc->vc_sw->con_invert_region(vc, p, count);
465#ifndef VT_BUF_VRAM_ONLY
466 else {
467 u16 *q = p;
468 int cnt = count;
469 u16 a;
470
471 if (!vc->vc_can_do_color) {
472 while (cnt--) {
473 a = scr_readw(q);
474 a ^= 0x0800;
475 scr_writew(a, q);
476 q++;
477 }
478 } else if (vc->vc_hi_font_mask == 0x100) {
479 while (cnt--) {
480 a = scr_readw(q);
481 a = ((a) & 0x11ff) | (((a) & 0xe000) >> 4) | (((a) & 0x0e00) << 4);
482 scr_writew(a, q);
483 q++;
484 }
485 } else {
486 while (cnt--) {
487 a = scr_readw(q);
488 a = ((a) & 0x88ff) | (((a) & 0x7000) >> 4) | (((a) & 0x0700) << 4);
489 scr_writew(a, q);
490 q++;
491 }
492 }
493 }
494#endif
495 if (DO_UPDATE(vc))
496 do_update_region(vc, (unsigned long) p, count);
497}
498
499/* used by selection: complement pointer position */
500void complement_pos(struct vc_data *vc, int offset)
501{
502 static int old_offset = -1;
503 static unsigned short old;
504 static unsigned short oldx, oldy;
505
506 WARN_CONSOLE_UNLOCKED();
507
508 if (old_offset != -1 && old_offset >= 0 &&
509 old_offset < vc->vc_screenbuf_size) {
510 scr_writew(old, screenpos(vc, old_offset, 1));
511 if (DO_UPDATE(vc))
512 vc->vc_sw->con_putc(vc, old, oldy, oldx);
513 }
514
515 old_offset = offset;
516
517 if (offset != -1 && offset >= 0 &&
518 offset < vc->vc_screenbuf_size) {
519 unsigned short new;
520 unsigned short *p;
521 p = screenpos(vc, offset, 1);
522 old = scr_readw(p);
523 new = old ^ vc->vc_complement_mask;
524 scr_writew(new, p);
525 if (DO_UPDATE(vc)) {
526 oldx = (offset >> 1) % vc->vc_cols;
527 oldy = (offset >> 1) / vc->vc_cols;
528 vc->vc_sw->con_putc(vc, new, oldy, oldx);
529 }
530 }
531
532}
533
534static void insert_char(struct vc_data *vc, unsigned int nr)
535{
536 unsigned short *p, *q = (unsigned short *)vc->vc_pos;
537
538 p = q + vc->vc_cols - nr - vc->vc_x;
539 while (--p >= q)
540 scr_writew(scr_readw(p), p + nr);
541 scr_memsetw(q, vc->vc_video_erase_char, nr * 2);
542 vc->vc_need_wrap = 0;
543 if (DO_UPDATE(vc)) {
544 unsigned short oldattr = vc->vc_attr;
545 vc->vc_sw->con_bmove(vc, vc->vc_y, vc->vc_x, vc->vc_y, vc->vc_x + nr, 1,
546 vc->vc_cols - vc->vc_x - nr);
547 vc->vc_attr = vc->vc_video_erase_char >> 8;
548 while (nr--)
549 vc->vc_sw->con_putc(vc, vc->vc_video_erase_char, vc->vc_y, vc->vc_x + nr);
550 vc->vc_attr = oldattr;
551 }
552}
553
554static void delete_char(struct vc_data *vc, unsigned int nr)
555{
556 unsigned int i = vc->vc_x;
557 unsigned short *p = (unsigned short *)vc->vc_pos;
558
559 while (++i <= vc->vc_cols - nr) {
560 scr_writew(scr_readw(p+nr), p);
561 p++;
562 }
563 scr_memsetw(p, vc->vc_video_erase_char, nr * 2);
564 vc->vc_need_wrap = 0;
565 if (DO_UPDATE(vc)) {
566 unsigned short oldattr = vc->vc_attr;
567 vc->vc_sw->con_bmove(vc, vc->vc_y, vc->vc_x + nr, vc->vc_y, vc->vc_x, 1,
568 vc->vc_cols - vc->vc_x - nr);
569 vc->vc_attr = vc->vc_video_erase_char >> 8;
570 while (nr--)
571 vc->vc_sw->con_putc(vc, vc->vc_video_erase_char, vc->vc_y,
572 vc->vc_cols - 1 - nr);
573 vc->vc_attr = oldattr;
574 }
575}
576
577static int softcursor_original;
578
579static void add_softcursor(struct vc_data *vc)
580{
581 int i = scr_readw((u16 *) vc->vc_pos);
582 u32 type = vc->vc_cursor_type;
583
584 if (! (type & 0x10)) return;
585 if (softcursor_original != -1) return;
586 softcursor_original = i;
587 i |= ((type >> 8) & 0xff00 );
588 i ^= ((type) & 0xff00 );
589 if ((type & 0x20) && ((softcursor_original & 0x7000) == (i & 0x7000))) i ^= 0x7000;
590 if ((type & 0x40) && ((i & 0x700) == ((i & 0x7000) >> 4))) i ^= 0x0700;
591 scr_writew(i, (u16 *) vc->vc_pos);
592 if (DO_UPDATE(vc))
593 vc->vc_sw->con_putc(vc, i, vc->vc_y, vc->vc_x);
594}
595
596static void hide_softcursor(struct vc_data *vc)
597{
598 if (softcursor_original != -1) {
599 scr_writew(softcursor_original, (u16 *)vc->vc_pos);
600 if (DO_UPDATE(vc))
601 vc->vc_sw->con_putc(vc, softcursor_original,
602 vc->vc_y, vc->vc_x);
603 softcursor_original = -1;
604 }
605}
606
607static void hide_cursor(struct vc_data *vc)
608{
609 if (vc == sel_cons)
610 clear_selection();
611 vc->vc_sw->con_cursor(vc, CM_ERASE);
612 hide_softcursor(vc);
613}
614
615static void set_cursor(struct vc_data *vc)
616{
617 if (!IS_FG(vc) || console_blanked ||
618 vc->vc_mode == KD_GRAPHICS)
619 return;
620 if (vc->vc_deccm) {
621 if (vc == sel_cons)
622 clear_selection();
623 add_softcursor(vc);
624 if ((vc->vc_cursor_type & 0x0f) != 1)
625 vc->vc_sw->con_cursor(vc, CM_DRAW);
626 } else
627 hide_cursor(vc);
628}
629
630static void set_origin(struct vc_data *vc)
631{
632 WARN_CONSOLE_UNLOCKED();
633
634 if (!CON_IS_VISIBLE(vc) ||
635 !vc->vc_sw->con_set_origin ||
636 !vc->vc_sw->con_set_origin(vc))
637 vc->vc_origin = (unsigned long)vc->vc_screenbuf;
638 vc->vc_visible_origin = vc->vc_origin;
639 vc->vc_scr_end = vc->vc_origin + vc->vc_screenbuf_size;
640 vc->vc_pos = vc->vc_origin + vc->vc_size_row * vc->vc_y + 2 * vc->vc_x;
641}
642
643static inline void save_screen(struct vc_data *vc)
644{
645 WARN_CONSOLE_UNLOCKED();
646
647 if (vc->vc_sw->con_save_screen)
648 vc->vc_sw->con_save_screen(vc);
649}
650
651/*
652 * Redrawing of screen
653 */
654
655static void clear_buffer_attributes(struct vc_data *vc)
656{
657 unsigned short *p = (unsigned short *)vc->vc_origin;
658 int count = vc->vc_screenbuf_size / 2;
659 int mask = vc->vc_hi_font_mask | 0xff;
660
661 for (; count > 0; count--, p++) {
662 scr_writew((scr_readw(p)&mask) | (vc->vc_video_erase_char & ~mask), p);
663 }
664}
665
666void redraw_screen(struct vc_data *vc, int is_switch)
667{
668 int redraw = 0;
669
670 WARN_CONSOLE_UNLOCKED();
671
672 if (!vc) {
673 /* strange ... */
674 /* printk("redraw_screen: tty %d not allocated ??\n", new_console+1); */
675 return;
676 }
677
678 if (is_switch) {
679 struct vc_data *old_vc = vc_cons[fg_console].d;
680 if (old_vc == vc)
681 return;
682 if (!CON_IS_VISIBLE(vc))
683 redraw = 1;
684 *vc->vc_display_fg = vc;
685 fg_console = vc->vc_num;
686 hide_cursor(old_vc);
687 if (!CON_IS_VISIBLE(old_vc)) {
688 save_screen(old_vc);
689 set_origin(old_vc);
690 }
691 } else {
692 hide_cursor(vc);
693 redraw = 1;
694 }
695
696 if (redraw) {
697 int update;
698 int old_was_color = vc->vc_can_do_color;
699
700 set_origin(vc);
701 update = vc->vc_sw->con_switch(vc);
702 set_palette(vc);
703 /*
704 * If console changed from mono<->color, the best we can do
705 * is to clear the buffer attributes. As it currently stands,
706 * rebuilding new attributes from the old buffer is not doable
707 * without overly complex code.
708 */
709 if (old_was_color != vc->vc_can_do_color) {
710 update_attr(vc);
711 clear_buffer_attributes(vc);
712 }
713
714 /* Forcibly update if we're panicing */
715 if ((update && vc->vc_mode != KD_GRAPHICS) ||
716 vt_force_oops_output(vc))
717 do_update_region(vc, vc->vc_origin, vc->vc_screenbuf_size / 2);
718 }
719 set_cursor(vc);
720 if (is_switch) {
721 set_leds();
722 compute_shiftstate();
723 notify_update(vc);
724 }
725}
726
727/*
728 * Allocation, freeing and resizing of VTs.
729 */
730
731int vc_cons_allocated(unsigned int i)
732{
733 return (i < MAX_NR_CONSOLES && vc_cons[i].d);
734}
735
736static void visual_init(struct vc_data *vc, int num, int init)
737{
738 /* ++Geert: vc->vc_sw->con_init determines console size */
739 if (vc->vc_sw)
740 module_put(vc->vc_sw->owner);
741 vc->vc_sw = conswitchp;
742#ifndef VT_SINGLE_DRIVER
743 if (con_driver_map[num])
744 vc->vc_sw = con_driver_map[num];
745#endif
746 __module_get(vc->vc_sw->owner);
747 vc->vc_num = num;
748 vc->vc_display_fg = &master_display_fg;
749 vc->vc_uni_pagedir_loc = &vc->vc_uni_pagedir;
750 vc->vc_uni_pagedir = 0;
751 vc->vc_hi_font_mask = 0;
752 vc->vc_complement_mask = 0;
753 vc->vc_can_do_color = 0;
754 vc->vc_panic_force_write = false;
755 vc->vc_sw->con_init(vc, init);
756 if (!vc->vc_complement_mask)
757 vc->vc_complement_mask = vc->vc_can_do_color ? 0x7700 : 0x0800;
758 vc->vc_s_complement_mask = vc->vc_complement_mask;
759 vc->vc_size_row = vc->vc_cols << 1;
760 vc->vc_screenbuf_size = vc->vc_rows * vc->vc_size_row;
761}
762
763int vc_allocate(unsigned int currcons) /* return 0 on success */
764{
765 WARN_CONSOLE_UNLOCKED();
766
767 if (currcons >= MAX_NR_CONSOLES)
768 return -ENXIO;
769 if (!vc_cons[currcons].d) {
770 struct vc_data *vc;
771 struct vt_notifier_param param;
772
773 /* prevent users from taking too much memory */
774 if (currcons >= MAX_NR_USER_CONSOLES && !capable(CAP_SYS_RESOURCE))
775 return -EPERM;
776
777 /* due to the granularity of kmalloc, we waste some memory here */
778 /* the alloc is done in two steps, to optimize the common situation
779 of a 25x80 console (structsize=216, screenbuf_size=4000) */
780 /* although the numbers above are not valid since long ago, the
781 point is still up-to-date and the comment still has its value
782 even if only as a historical artifact. --mj, July 1998 */
783 param.vc = vc = kzalloc(sizeof(struct vc_data), GFP_KERNEL);
784 if (!vc)
785 return -ENOMEM;
786 vc_cons[currcons].d = vc;
787 tty_port_init(&vc->port);
788 INIT_WORK(&vc_cons[currcons].SAK_work, vc_SAK);
789 visual_init(vc, currcons, 1);
790 if (!*vc->vc_uni_pagedir_loc)
791 con_set_default_unimap(vc);
792 vc->vc_screenbuf = kmalloc(vc->vc_screenbuf_size, GFP_KERNEL);
793 if (!vc->vc_screenbuf) {
794 kfree(vc);
795 vc_cons[currcons].d = NULL;
796 return -ENOMEM;
797 }
798
799 /* If no drivers have overridden us and the user didn't pass a
800 boot option, default to displaying the cursor */
801 if (global_cursor_default == -1)
802 global_cursor_default = 1;
803
804 vc_init(vc, vc->vc_rows, vc->vc_cols, 1);
805 vcs_make_sysfs(currcons);
806 atomic_notifier_call_chain(&vt_notifier_list, VT_ALLOCATE, &param);
807 }
808 return 0;
809}
810
811static inline int resize_screen(struct vc_data *vc, int width, int height,
812 int user)
813{
814 /* Resizes the resolution of the display adapater */
815 int err = 0;
816
817 if (vc->vc_mode != KD_GRAPHICS && vc->vc_sw->con_resize)
818 err = vc->vc_sw->con_resize(vc, width, height, user);
819
820 return err;
821}
822
823/*
824 * Change # of rows and columns (0 means unchanged/the size of fg_console)
825 * [this is to be used together with some user program
826 * like resize that changes the hardware videomode]
827 */
828#define VC_RESIZE_MAXCOL (32767)
829#define VC_RESIZE_MAXROW (32767)
830
831/**
832 * vc_do_resize - resizing method for the tty
833 * @tty: tty being resized
834 * @real_tty: real tty (different to tty if a pty/tty pair)
835 * @vc: virtual console private data
836 * @cols: columns
837 * @lines: lines
838 *
839 * Resize a virtual console, clipping according to the actual constraints.
840 * If the caller passes a tty structure then update the termios winsize
841 * information and perform any necessary signal handling.
842 *
843 * Caller must hold the console semaphore. Takes the termios mutex and
844 * ctrl_lock of the tty IFF a tty is passed.
845 */
846
847static int vc_do_resize(struct tty_struct *tty, struct vc_data *vc,
848 unsigned int cols, unsigned int lines)
849{
850 unsigned long old_origin, new_origin, new_scr_end, rlth, rrem, err = 0;
851 unsigned long end;
852 unsigned int old_cols, old_rows, old_row_size, old_screen_size;
853 unsigned int new_cols, new_rows, new_row_size, new_screen_size;
854 unsigned int user;
855 unsigned short *newscreen;
856
857 WARN_CONSOLE_UNLOCKED();
858
859 if (!vc)
860 return -ENXIO;
861
862 user = vc->vc_resize_user;
863 vc->vc_resize_user = 0;
864
865 if (cols > VC_RESIZE_MAXCOL || lines > VC_RESIZE_MAXROW)
866 return -EINVAL;
867
868 new_cols = (cols ? cols : vc->vc_cols);
869 new_rows = (lines ? lines : vc->vc_rows);
870 new_row_size = new_cols << 1;
871 new_screen_size = new_row_size * new_rows;
872
873 if (new_cols == vc->vc_cols && new_rows == vc->vc_rows)
874 return 0;
875
876 newscreen = kmalloc(new_screen_size, GFP_USER);
877 if (!newscreen)
878 return -ENOMEM;
879
880 old_rows = vc->vc_rows;
881 old_cols = vc->vc_cols;
882 old_row_size = vc->vc_size_row;
883 old_screen_size = vc->vc_screenbuf_size;
884
885 err = resize_screen(vc, new_cols, new_rows, user);
886 if (err) {
887 kfree(newscreen);
888 return err;
889 }
890
891 vc->vc_rows = new_rows;
892 vc->vc_cols = new_cols;
893 vc->vc_size_row = new_row_size;
894 vc->vc_screenbuf_size = new_screen_size;
895
896 rlth = min(old_row_size, new_row_size);
897 rrem = new_row_size - rlth;
898 old_origin = vc->vc_origin;
899 new_origin = (long) newscreen;
900 new_scr_end = new_origin + new_screen_size;
901
902 if (vc->vc_y > new_rows) {
903 if (old_rows - vc->vc_y < new_rows) {
904 /*
905 * Cursor near the bottom, copy contents from the
906 * bottom of buffer
907 */
908 old_origin += (old_rows - new_rows) * old_row_size;
909 } else {
910 /*
911 * Cursor is in no man's land, copy 1/2 screenful
912 * from the top and bottom of cursor position
913 */
914 old_origin += (vc->vc_y - new_rows/2) * old_row_size;
915 }
916 }
917
918 end = old_origin + old_row_size * min(old_rows, new_rows);
919
920 update_attr(vc);
921
922 while (old_origin < end) {
923 scr_memcpyw((unsigned short *) new_origin,
924 (unsigned short *) old_origin, rlth);
925 if (rrem)
926 scr_memsetw((void *)(new_origin + rlth),
927 vc->vc_video_erase_char, rrem);
928 old_origin += old_row_size;
929 new_origin += new_row_size;
930 }
931 if (new_scr_end > new_origin)
932 scr_memsetw((void *)new_origin, vc->vc_video_erase_char,
933 new_scr_end - new_origin);
934 kfree(vc->vc_screenbuf);
935 vc->vc_screenbuf = newscreen;
936 vc->vc_screenbuf_size = new_screen_size;
937 set_origin(vc);
938
939 /* do part of a reset_terminal() */
940 vc->vc_top = 0;
941 vc->vc_bottom = vc->vc_rows;
942 gotoxy(vc, vc->vc_x, vc->vc_y);
943 save_cur(vc);
944
945 if (tty) {
946 /* Rewrite the requested winsize data with the actual
947 resulting sizes */
948 struct winsize ws;
949 memset(&ws, 0, sizeof(ws));
950 ws.ws_row = vc->vc_rows;
951 ws.ws_col = vc->vc_cols;
952 ws.ws_ypixel = vc->vc_scan_lines;
953 tty_do_resize(tty, &ws);
954 }
955
956 if (CON_IS_VISIBLE(vc))
957 update_screen(vc);
958 vt_event_post(VT_EVENT_RESIZE, vc->vc_num, vc->vc_num);
959 return err;
960}
961
962/**
963 * vc_resize - resize a VT
964 * @vc: virtual console
965 * @cols: columns
966 * @rows: rows
967 *
968 * Resize a virtual console as seen from the console end of things. We
969 * use the common vc_do_resize methods to update the structures. The
970 * caller must hold the console sem to protect console internals and
971 * vc->port.tty
972 */
973
974int vc_resize(struct vc_data *vc, unsigned int cols, unsigned int rows)
975{
976 return vc_do_resize(vc->port.tty, vc, cols, rows);
977}
978
979/**
980 * vt_resize - resize a VT
981 * @tty: tty to resize
982 * @ws: winsize attributes
983 *
984 * Resize a virtual terminal. This is called by the tty layer as we
985 * register our own handler for resizing. The mutual helper does all
986 * the actual work.
987 *
988 * Takes the console sem and the called methods then take the tty
989 * termios_mutex and the tty ctrl_lock in that order.
990 */
991static int vt_resize(struct tty_struct *tty, struct winsize *ws)
992{
993 struct vc_data *vc = tty->driver_data;
994 int ret;
995
996 acquire_console_sem();
997 ret = vc_do_resize(tty, vc, ws->ws_col, ws->ws_row);
998 release_console_sem();
999 return ret;
1000}
1001
1002void vc_deallocate(unsigned int currcons)
1003{
1004 WARN_CONSOLE_UNLOCKED();
1005
1006 if (vc_cons_allocated(currcons)) {
1007 struct vc_data *vc = vc_cons[currcons].d;
1008 struct vt_notifier_param param = { .vc = vc };
1009
1010 atomic_notifier_call_chain(&vt_notifier_list, VT_DEALLOCATE, &param);
1011 vcs_remove_sysfs(currcons);
1012 vc->vc_sw->con_deinit(vc);
1013 put_pid(vc->vt_pid);
1014 module_put(vc->vc_sw->owner);
1015 kfree(vc->vc_screenbuf);
1016 if (currcons >= MIN_NR_CONSOLES)
1017 kfree(vc);
1018 vc_cons[currcons].d = NULL;
1019 }
1020}
1021
1022/*
1023 * VT102 emulator
1024 */
1025
1026#define set_kbd(vc, x) set_vc_kbd_mode(kbd_table + (vc)->vc_num, (x))
1027#define clr_kbd(vc, x) clr_vc_kbd_mode(kbd_table + (vc)->vc_num, (x))
1028#define is_kbd(vc, x) vc_kbd_mode(kbd_table + (vc)->vc_num, (x))
1029
1030#define decarm VC_REPEAT
1031#define decckm VC_CKMODE
1032#define kbdapplic VC_APPLIC
1033#define lnm VC_CRLF
1034
1035/*
1036 * this is what the terminal answers to a ESC-Z or csi0c query.
1037 */
1038#define VT100ID "\033[?1;2c"
1039#define VT102ID "\033[?6c"
1040
1041unsigned char color_table[] = { 0, 4, 2, 6, 1, 5, 3, 7,
1042 8,12,10,14, 9,13,11,15 };
1043
1044/* the default colour table, for VGA+ colour systems */
1045int default_red[] = {0x00,0xaa,0x00,0xaa,0x00,0xaa,0x00,0xaa,
1046 0x55,0xff,0x55,0xff,0x55,0xff,0x55,0xff};
1047int default_grn[] = {0x00,0x00,0xaa,0x55,0x00,0x00,0xaa,0xaa,
1048 0x55,0x55,0xff,0xff,0x55,0x55,0xff,0xff};
1049int default_blu[] = {0x00,0x00,0x00,0x00,0xaa,0xaa,0xaa,0xaa,
1050 0x55,0x55,0x55,0x55,0xff,0xff,0xff,0xff};
1051
1052module_param_array(default_red, int, NULL, S_IRUGO | S_IWUSR);
1053module_param_array(default_grn, int, NULL, S_IRUGO | S_IWUSR);
1054module_param_array(default_blu, int, NULL, S_IRUGO | S_IWUSR);
1055
1056/*
1057 * gotoxy() must verify all boundaries, because the arguments
1058 * might also be negative. If the given position is out of
1059 * bounds, the cursor is placed at the nearest margin.
1060 */
1061static void gotoxy(struct vc_data *vc, int new_x, int new_y)
1062{
1063 int min_y, max_y;
1064
1065 if (new_x < 0)
1066 vc->vc_x = 0;
1067 else {
1068 if (new_x >= vc->vc_cols)
1069 vc->vc_x = vc->vc_cols - 1;
1070 else
1071 vc->vc_x = new_x;
1072 }
1073
1074 if (vc->vc_decom) {
1075 min_y = vc->vc_top;
1076 max_y = vc->vc_bottom;
1077 } else {
1078 min_y = 0;
1079 max_y = vc->vc_rows;
1080 }
1081 if (new_y < min_y)
1082 vc->vc_y = min_y;
1083 else if (new_y >= max_y)
1084 vc->vc_y = max_y - 1;
1085 else
1086 vc->vc_y = new_y;
1087 vc->vc_pos = vc->vc_origin + vc->vc_y * vc->vc_size_row + (vc->vc_x<<1);
1088 vc->vc_need_wrap = 0;
1089}
1090
1091/* for absolute user moves, when decom is set */
1092static void gotoxay(struct vc_data *vc, int new_x, int new_y)
1093{
1094 gotoxy(vc, new_x, vc->vc_decom ? (vc->vc_top + new_y) : new_y);
1095}
1096
1097void scrollback(struct vc_data *vc, int lines)
1098{
1099 if (!lines)
1100 lines = vc->vc_rows / 2;
1101 scrolldelta(-lines);
1102}
1103
1104void scrollfront(struct vc_data *vc, int lines)
1105{
1106 if (!lines)
1107 lines = vc->vc_rows / 2;
1108 scrolldelta(lines);
1109}
1110
1111static void lf(struct vc_data *vc)
1112{
1113 /* don't scroll if above bottom of scrolling region, or
1114 * if below scrolling region
1115 */
1116 if (vc->vc_y + 1 == vc->vc_bottom)
1117 scrup(vc, vc->vc_top, vc->vc_bottom, 1);
1118 else if (vc->vc_y < vc->vc_rows - 1) {
1119 vc->vc_y++;
1120 vc->vc_pos += vc->vc_size_row;
1121 }
1122 vc->vc_need_wrap = 0;
1123 notify_write(vc, '\n');
1124}
1125
1126static void ri(struct vc_data *vc)
1127{
1128 /* don't scroll if below top of scrolling region, or
1129 * if above scrolling region
1130 */
1131 if (vc->vc_y == vc->vc_top)
1132 scrdown(vc, vc->vc_top, vc->vc_bottom, 1);
1133 else if (vc->vc_y > 0) {
1134 vc->vc_y--;
1135 vc->vc_pos -= vc->vc_size_row;
1136 }
1137 vc->vc_need_wrap = 0;
1138}
1139
1140static inline void cr(struct vc_data *vc)
1141{
1142 vc->vc_pos -= vc->vc_x << 1;
1143 vc->vc_need_wrap = vc->vc_x = 0;
1144 notify_write(vc, '\r');
1145}
1146
1147static inline void bs(struct vc_data *vc)
1148{
1149 if (vc->vc_x) {
1150 vc->vc_pos -= 2;
1151 vc->vc_x--;
1152 vc->vc_need_wrap = 0;
1153 notify_write(vc, '\b');
1154 }
1155}
1156
1157static inline void del(struct vc_data *vc)
1158{
1159 /* ignored */
1160}
1161
1162static void csi_J(struct vc_data *vc, int vpar)
1163{
1164 unsigned int count;
1165 unsigned short * start;
1166
1167 switch (vpar) {
1168 case 0: /* erase from cursor to end of display */
1169 count = (vc->vc_scr_end - vc->vc_pos) >> 1;
1170 start = (unsigned short *)vc->vc_pos;
1171 if (DO_UPDATE(vc)) {
1172 /* do in two stages */
1173 vc->vc_sw->con_clear(vc, vc->vc_y, vc->vc_x, 1,
1174 vc->vc_cols - vc->vc_x);
1175 vc->vc_sw->con_clear(vc, vc->vc_y + 1, 0,
1176 vc->vc_rows - vc->vc_y - 1,
1177 vc->vc_cols);
1178 }
1179 break;
1180 case 1: /* erase from start to cursor */
1181 count = ((vc->vc_pos - vc->vc_origin) >> 1) + 1;
1182 start = (unsigned short *)vc->vc_origin;
1183 if (DO_UPDATE(vc)) {
1184 /* do in two stages */
1185 vc->vc_sw->con_clear(vc, 0, 0, vc->vc_y,
1186 vc->vc_cols);
1187 vc->vc_sw->con_clear(vc, vc->vc_y, 0, 1,
1188 vc->vc_x + 1);
1189 }
1190 break;
1191 case 2: /* erase whole display */
1192 count = vc->vc_cols * vc->vc_rows;
1193 start = (unsigned short *)vc->vc_origin;
1194 if (DO_UPDATE(vc))
1195 vc->vc_sw->con_clear(vc, 0, 0,
1196 vc->vc_rows,
1197 vc->vc_cols);
1198 break;
1199 default:
1200 return;
1201 }
1202 scr_memsetw(start, vc->vc_video_erase_char, 2 * count);
1203 vc->vc_need_wrap = 0;
1204}
1205
1206static void csi_K(struct vc_data *vc, int vpar)
1207{
1208 unsigned int count;
1209 unsigned short * start;
1210
1211 switch (vpar) {
1212 case 0: /* erase from cursor to end of line */
1213 count = vc->vc_cols - vc->vc_x;
1214 start = (unsigned short *)vc->vc_pos;
1215 if (DO_UPDATE(vc))
1216 vc->vc_sw->con_clear(vc, vc->vc_y, vc->vc_x, 1,
1217 vc->vc_cols - vc->vc_x);
1218 break;
1219 case 1: /* erase from start of line to cursor */
1220 start = (unsigned short *)(vc->vc_pos - (vc->vc_x << 1));
1221 count = vc->vc_x + 1;
1222 if (DO_UPDATE(vc))
1223 vc->vc_sw->con_clear(vc, vc->vc_y, 0, 1,
1224 vc->vc_x + 1);
1225 break;
1226 case 2: /* erase whole line */
1227 start = (unsigned short *)(vc->vc_pos - (vc->vc_x << 1));
1228 count = vc->vc_cols;
1229 if (DO_UPDATE(vc))
1230 vc->vc_sw->con_clear(vc, vc->vc_y, 0, 1,
1231 vc->vc_cols);
1232 break;
1233 default:
1234 return;
1235 }
1236 scr_memsetw(start, vc->vc_video_erase_char, 2 * count);
1237 vc->vc_need_wrap = 0;
1238}
1239
1240static void csi_X(struct vc_data *vc, int vpar) /* erase the following vpar positions */
1241{ /* not vt100? */
1242 int count;
1243
1244 if (!vpar)
1245 vpar++;
1246 count = (vpar > vc->vc_cols - vc->vc_x) ? (vc->vc_cols - vc->vc_x) : vpar;
1247
1248 scr_memsetw((unsigned short *)vc->vc_pos, vc->vc_video_erase_char, 2 * count);
1249 if (DO_UPDATE(vc))
1250 vc->vc_sw->con_clear(vc, vc->vc_y, vc->vc_x, 1, count);
1251 vc->vc_need_wrap = 0;
1252}
1253
1254static void default_attr(struct vc_data *vc)
1255{
1256 vc->vc_intensity = 1;
1257 vc->vc_italic = 0;
1258 vc->vc_underline = 0;
1259 vc->vc_reverse = 0;
1260 vc->vc_blink = 0;
1261 vc->vc_color = vc->vc_def_color;
1262}
1263
1264/* console_sem is held */
1265static void csi_m(struct vc_data *vc)
1266{
1267 int i;
1268
1269 for (i = 0; i <= vc->vc_npar; i++)
1270 switch (vc->vc_par[i]) {
1271 case 0: /* all attributes off */
1272 default_attr(vc);
1273 break;
1274 case 1:
1275 vc->vc_intensity = 2;
1276 break;
1277 case 2:
1278 vc->vc_intensity = 0;
1279 break;
1280 case 3:
1281 vc->vc_italic = 1;
1282 break;
1283 case 4:
1284 vc->vc_underline = 1;
1285 break;
1286 case 5:
1287 vc->vc_blink = 1;
1288 break;
1289 case 7:
1290 vc->vc_reverse = 1;
1291 break;
1292 case 10: /* ANSI X3.64-1979 (SCO-ish?)
1293 * Select primary font, don't display
1294 * control chars if defined, don't set
1295 * bit 8 on output.
1296 */
1297 vc->vc_translate = set_translate(vc->vc_charset == 0
1298 ? vc->vc_G0_charset
1299 : vc->vc_G1_charset, vc);
1300 vc->vc_disp_ctrl = 0;
1301 vc->vc_toggle_meta = 0;
1302 break;
1303 case 11: /* ANSI X3.64-1979 (SCO-ish?)
1304 * Select first alternate font, lets
1305 * chars < 32 be displayed as ROM chars.
1306 */
1307 vc->vc_translate = set_translate(IBMPC_MAP, vc);
1308 vc->vc_disp_ctrl = 1;
1309 vc->vc_toggle_meta = 0;
1310 break;
1311 case 12: /* ANSI X3.64-1979 (SCO-ish?)
1312 * Select second alternate font, toggle
1313 * high bit before displaying as ROM char.
1314 */
1315 vc->vc_translate = set_translate(IBMPC_MAP, vc);
1316 vc->vc_disp_ctrl = 1;
1317 vc->vc_toggle_meta = 1;
1318 break;
1319 case 21:
1320 case 22:
1321 vc->vc_intensity = 1;
1322 break;
1323 case 23:
1324 vc->vc_italic = 0;
1325 break;
1326 case 24:
1327 vc->vc_underline = 0;
1328 break;
1329 case 25:
1330 vc->vc_blink = 0;
1331 break;
1332 case 27:
1333 vc->vc_reverse = 0;
1334 break;
1335 case 38: /* ANSI X3.64-1979 (SCO-ish?)
1336 * Enables underscore, white foreground
1337 * with white underscore (Linux - use
1338 * default foreground).
1339 */
1340 vc->vc_color = (vc->vc_def_color & 0x0f) | (vc->vc_color & 0xf0);
1341 vc->vc_underline = 1;
1342 break;
1343 case 39: /* ANSI X3.64-1979 (SCO-ish?)
1344 * Disable underline option.
1345 * Reset colour to default? It did this
1346 * before...
1347 */
1348 vc->vc_color = (vc->vc_def_color & 0x0f) | (vc->vc_color & 0xf0);
1349 vc->vc_underline = 0;
1350 break;
1351 case 49:
1352 vc->vc_color = (vc->vc_def_color & 0xf0) | (vc->vc_color & 0x0f);
1353 break;
1354 default:
1355 if (vc->vc_par[i] >= 30 && vc->vc_par[i] <= 37)
1356 vc->vc_color = color_table[vc->vc_par[i] - 30]
1357 | (vc->vc_color & 0xf0);
1358 else if (vc->vc_par[i] >= 40 && vc->vc_par[i] <= 47)
1359 vc->vc_color = (color_table[vc->vc_par[i] - 40] << 4)
1360 | (vc->vc_color & 0x0f);
1361 break;
1362 }
1363 update_attr(vc);
1364}
1365
1366static void respond_string(const char *p, struct tty_struct *tty)
1367{
1368 while (*p) {
1369 tty_insert_flip_char(tty, *p, 0);
1370 p++;
1371 }
1372 con_schedule_flip(tty);
1373}
1374
1375static void cursor_report(struct vc_data *vc, struct tty_struct *tty)
1376{
1377 char buf[40];
1378
1379 sprintf(buf, "\033[%d;%dR", vc->vc_y + (vc->vc_decom ? vc->vc_top + 1 : 1), vc->vc_x + 1);
1380 respond_string(buf, tty);
1381}
1382
1383static inline void status_report(struct tty_struct *tty)
1384{
1385 respond_string("\033[0n", tty); /* Terminal ok */
1386}
1387
1388static inline void respond_ID(struct tty_struct * tty)
1389{
1390 respond_string(VT102ID, tty);
1391}
1392
1393void mouse_report(struct tty_struct *tty, int butt, int mrx, int mry)
1394{
1395 char buf[8];
1396
1397 sprintf(buf, "\033[M%c%c%c", (char)(' ' + butt), (char)('!' + mrx),
1398 (char)('!' + mry));
1399 respond_string(buf, tty);
1400}
1401
1402/* invoked via ioctl(TIOCLINUX) and through set_selection */
1403int mouse_reporting(void)
1404{
1405 return vc_cons[fg_console].d->vc_report_mouse;
1406}
1407
1408/* console_sem is held */
1409static void set_mode(struct vc_data *vc, int on_off)
1410{
1411 int i;
1412
1413 for (i = 0; i <= vc->vc_npar; i++)
1414 if (vc->vc_ques) {
1415 switch(vc->vc_par[i]) { /* DEC private modes set/reset */
1416 case 1: /* Cursor keys send ^[Ox/^[[x */
1417 if (on_off)
1418 set_kbd(vc, decckm);
1419 else
1420 clr_kbd(vc, decckm);
1421 break;
1422 case 3: /* 80/132 mode switch unimplemented */
1423 vc->vc_deccolm = on_off;
1424#if 0
1425 vc_resize(deccolm ? 132 : 80, vc->vc_rows);
1426 /* this alone does not suffice; some user mode
1427 utility has to change the hardware regs */
1428#endif
1429 break;
1430 case 5: /* Inverted screen on/off */
1431 if (vc->vc_decscnm != on_off) {
1432 vc->vc_decscnm = on_off;
1433 invert_screen(vc, 0, vc->vc_screenbuf_size, 0);
1434 update_attr(vc);
1435 }
1436 break;
1437 case 6: /* Origin relative/absolute */
1438 vc->vc_decom = on_off;
1439 gotoxay(vc, 0, 0);
1440 break;
1441 case 7: /* Autowrap on/off */
1442 vc->vc_decawm = on_off;
1443 break;
1444 case 8: /* Autorepeat on/off */
1445 if (on_off)
1446 set_kbd(vc, decarm);
1447 else
1448 clr_kbd(vc, decarm);
1449 break;
1450 case 9:
1451 vc->vc_report_mouse = on_off ? 1 : 0;
1452 break;
1453 case 25: /* Cursor on/off */
1454 vc->vc_deccm = on_off;
1455 break;
1456 case 1000:
1457 vc->vc_report_mouse = on_off ? 2 : 0;
1458 break;
1459 }
1460 } else {
1461 switch(vc->vc_par[i]) { /* ANSI modes set/reset */
1462 case 3: /* Monitor (display ctrls) */
1463 vc->vc_disp_ctrl = on_off;
1464 break;
1465 case 4: /* Insert Mode on/off */
1466 vc->vc_decim = on_off;
1467 break;
1468 case 20: /* Lf, Enter == CrLf/Lf */
1469 if (on_off)
1470 set_kbd(vc, lnm);
1471 else
1472 clr_kbd(vc, lnm);
1473 break;
1474 }
1475 }
1476}
1477
1478/* console_sem is held */
1479static void setterm_command(struct vc_data *vc)
1480{
1481 switch(vc->vc_par[0]) {
1482 case 1: /* set color for underline mode */
1483 if (vc->vc_can_do_color &&
1484 vc->vc_par[1] < 16) {
1485 vc->vc_ulcolor = color_table[vc->vc_par[1]];
1486 if (vc->vc_underline)
1487 update_attr(vc);
1488 }
1489 break;
1490 case 2: /* set color for half intensity mode */
1491 if (vc->vc_can_do_color &&
1492 vc->vc_par[1] < 16) {
1493 vc->vc_halfcolor = color_table[vc->vc_par[1]];
1494 if (vc->vc_intensity == 0)
1495 update_attr(vc);
1496 }
1497 break;
1498 case 8: /* store colors as defaults */
1499 vc->vc_def_color = vc->vc_attr;
1500 if (vc->vc_hi_font_mask == 0x100)
1501 vc->vc_def_color >>= 1;
1502 default_attr(vc);
1503 update_attr(vc);
1504 break;
1505 case 9: /* set blanking interval */
1506 blankinterval = ((vc->vc_par[1] < 60) ? vc->vc_par[1] : 60) * 60;
1507 poke_blanked_console();
1508 break;
1509 case 10: /* set bell frequency in Hz */
1510 if (vc->vc_npar >= 1)
1511 vc->vc_bell_pitch = vc->vc_par[1];
1512 else
1513 vc->vc_bell_pitch = DEFAULT_BELL_PITCH;
1514 break;
1515 case 11: /* set bell duration in msec */
1516 if (vc->vc_npar >= 1)
1517 vc->vc_bell_duration = (vc->vc_par[1] < 2000) ?
1518 vc->vc_par[1] * HZ / 1000 : 0;
1519 else
1520 vc->vc_bell_duration = DEFAULT_BELL_DURATION;
1521 break;
1522 case 12: /* bring specified console to the front */
1523 if (vc->vc_par[1] >= 1 && vc_cons_allocated(vc->vc_par[1] - 1))
1524 set_console(vc->vc_par[1] - 1);
1525 break;
1526 case 13: /* unblank the screen */
1527 poke_blanked_console();
1528 break;
1529 case 14: /* set vesa powerdown interval */
1530 vesa_off_interval = ((vc->vc_par[1] < 60) ? vc->vc_par[1] : 60) * 60 * HZ;
1531 break;
1532 case 15: /* activate the previous console */
1533 set_console(last_console);
1534 break;
1535 }
1536}
1537
1538/* console_sem is held */
1539static void csi_at(struct vc_data *vc, unsigned int nr)
1540{
1541 if (nr > vc->vc_cols - vc->vc_x)
1542 nr = vc->vc_cols - vc->vc_x;
1543 else if (!nr)
1544 nr = 1;
1545 insert_char(vc, nr);
1546}
1547
1548/* console_sem is held */
1549static void csi_L(struct vc_data *vc, unsigned int nr)
1550{
1551 if (nr > vc->vc_rows - vc->vc_y)
1552 nr = vc->vc_rows - vc->vc_y;
1553 else if (!nr)
1554 nr = 1;
1555 scrdown(vc, vc->vc_y, vc->vc_bottom, nr);
1556 vc->vc_need_wrap = 0;
1557}
1558
1559/* console_sem is held */
1560static void csi_P(struct vc_data *vc, unsigned int nr)
1561{
1562 if (nr > vc->vc_cols - vc->vc_x)
1563 nr = vc->vc_cols - vc->vc_x;
1564 else if (!nr)
1565 nr = 1;
1566 delete_char(vc, nr);
1567}
1568
1569/* console_sem is held */
1570static void csi_M(struct vc_data *vc, unsigned int nr)
1571{
1572 if (nr > vc->vc_rows - vc->vc_y)
1573 nr = vc->vc_rows - vc->vc_y;
1574 else if (!nr)
1575 nr=1;
1576 scrup(vc, vc->vc_y, vc->vc_bottom, nr);
1577 vc->vc_need_wrap = 0;
1578}
1579
1580/* console_sem is held (except via vc_init->reset_terminal */
1581static void save_cur(struct vc_data *vc)
1582{
1583 vc->vc_saved_x = vc->vc_x;
1584 vc->vc_saved_y = vc->vc_y;
1585 vc->vc_s_intensity = vc->vc_intensity;
1586 vc->vc_s_italic = vc->vc_italic;
1587 vc->vc_s_underline = vc->vc_underline;
1588 vc->vc_s_blink = vc->vc_blink;
1589 vc->vc_s_reverse = vc->vc_reverse;
1590 vc->vc_s_charset = vc->vc_charset;
1591 vc->vc_s_color = vc->vc_color;
1592 vc->vc_saved_G0 = vc->vc_G0_charset;
1593 vc->vc_saved_G1 = vc->vc_G1_charset;
1594}
1595
1596/* console_sem is held */
1597static void restore_cur(struct vc_data *vc)
1598{
1599 gotoxy(vc, vc->vc_saved_x, vc->vc_saved_y);
1600 vc->vc_intensity = vc->vc_s_intensity;
1601 vc->vc_italic = vc->vc_s_italic;
1602 vc->vc_underline = vc->vc_s_underline;
1603 vc->vc_blink = vc->vc_s_blink;
1604 vc->vc_reverse = vc->vc_s_reverse;
1605 vc->vc_charset = vc->vc_s_charset;
1606 vc->vc_color = vc->vc_s_color;
1607 vc->vc_G0_charset = vc->vc_saved_G0;
1608 vc->vc_G1_charset = vc->vc_saved_G1;
1609 vc->vc_translate = set_translate(vc->vc_charset ? vc->vc_G1_charset : vc->vc_G0_charset, vc);
1610 update_attr(vc);
1611 vc->vc_need_wrap = 0;
1612}
1613
1614enum { ESnormal, ESesc, ESsquare, ESgetpars, ESgotpars, ESfunckey,
1615 EShash, ESsetG0, ESsetG1, ESpercent, ESignore, ESnonstd,
1616 ESpalette };
1617
1618/* console_sem is held (except via vc_init()) */
1619static void reset_terminal(struct vc_data *vc, int do_clear)
1620{
1621 vc->vc_top = 0;
1622 vc->vc_bottom = vc->vc_rows;
1623 vc->vc_state = ESnormal;
1624 vc->vc_ques = 0;
1625 vc->vc_translate = set_translate(LAT1_MAP, vc);
1626 vc->vc_G0_charset = LAT1_MAP;
1627 vc->vc_G1_charset = GRAF_MAP;
1628 vc->vc_charset = 0;
1629 vc->vc_need_wrap = 0;
1630 vc->vc_report_mouse = 0;
1631 vc->vc_utf = default_utf8;
1632 vc->vc_utf_count = 0;
1633
1634 vc->vc_disp_ctrl = 0;
1635 vc->vc_toggle_meta = 0;
1636
1637 vc->vc_decscnm = 0;
1638 vc->vc_decom = 0;
1639 vc->vc_decawm = 1;
1640 vc->vc_deccm = global_cursor_default;
1641 vc->vc_decim = 0;
1642
1643 set_kbd(vc, decarm);
1644 clr_kbd(vc, decckm);
1645 clr_kbd(vc, kbdapplic);
1646 clr_kbd(vc, lnm);
1647 kbd_table[vc->vc_num].lockstate = 0;
1648 kbd_table[vc->vc_num].slockstate = 0;
1649 kbd_table[vc->vc_num].ledmode = LED_SHOW_FLAGS;
1650 kbd_table[vc->vc_num].ledflagstate = kbd_table[vc->vc_num].default_ledflagstate;
1651 /* do not do set_leds here because this causes an endless tasklet loop
1652 when the keyboard hasn't been initialized yet */
1653
1654 vc->vc_cursor_type = cur_default;
1655 vc->vc_complement_mask = vc->vc_s_complement_mask;
1656
1657 default_attr(vc);
1658 update_attr(vc);
1659
1660 vc->vc_tab_stop[0] = 0x01010100;
1661 vc->vc_tab_stop[1] =
1662 vc->vc_tab_stop[2] =
1663 vc->vc_tab_stop[3] =
1664 vc->vc_tab_stop[4] =
1665 vc->vc_tab_stop[5] =
1666 vc->vc_tab_stop[6] =
1667 vc->vc_tab_stop[7] = 0x01010101;
1668
1669 vc->vc_bell_pitch = DEFAULT_BELL_PITCH;
1670 vc->vc_bell_duration = DEFAULT_BELL_DURATION;
1671
1672 gotoxy(vc, 0, 0);
1673 save_cur(vc);
1674 if (do_clear)
1675 csi_J(vc, 2);
1676}
1677
1678/* console_sem is held */
1679static void do_con_trol(struct tty_struct *tty, struct vc_data *vc, int c)
1680{
1681 /*
1682 * Control characters can be used in the _middle_
1683 * of an escape sequence.
1684 */
1685 switch (c) {
1686 case 0:
1687 return;
1688 case 7:
1689 if (vc->vc_bell_duration)
1690 kd_mksound(vc->vc_bell_pitch, vc->vc_bell_duration);
1691 return;
1692 case 8:
1693 bs(vc);
1694 return;
1695 case 9:
1696 vc->vc_pos -= (vc->vc_x << 1);
1697 while (vc->vc_x < vc->vc_cols - 1) {
1698 vc->vc_x++;
1699 if (vc->vc_tab_stop[vc->vc_x >> 5] & (1 << (vc->vc_x & 31)))
1700 break;
1701 }
1702 vc->vc_pos += (vc->vc_x << 1);
1703 notify_write(vc, '\t');
1704 return;
1705 case 10: case 11: case 12:
1706 lf(vc);
1707 if (!is_kbd(vc, lnm))
1708 return;
1709 case 13:
1710 cr(vc);
1711 return;
1712 case 14:
1713 vc->vc_charset = 1;
1714 vc->vc_translate = set_translate(vc->vc_G1_charset, vc);
1715 vc->vc_disp_ctrl = 1;
1716 return;
1717 case 15:
1718 vc->vc_charset = 0;
1719 vc->vc_translate = set_translate(vc->vc_G0_charset, vc);
1720 vc->vc_disp_ctrl = 0;
1721 return;
1722 case 24: case 26:
1723 vc->vc_state = ESnormal;
1724 return;
1725 case 27:
1726 vc->vc_state = ESesc;
1727 return;
1728 case 127:
1729 del(vc);
1730 return;
1731 case 128+27:
1732 vc->vc_state = ESsquare;
1733 return;
1734 }
1735 switch(vc->vc_state) {
1736 case ESesc:
1737 vc->vc_state = ESnormal;
1738 switch (c) {
1739 case '[':
1740 vc->vc_state = ESsquare;
1741 return;
1742 case ']':
1743 vc->vc_state = ESnonstd;
1744 return;
1745 case '%':
1746 vc->vc_state = ESpercent;
1747 return;
1748 case 'E':
1749 cr(vc);
1750 lf(vc);
1751 return;
1752 case 'M':
1753 ri(vc);
1754 return;
1755 case 'D':
1756 lf(vc);
1757 return;
1758 case 'H':
1759 vc->vc_tab_stop[vc->vc_x >> 5] |= (1 << (vc->vc_x & 31));
1760 return;
1761 case 'Z':
1762 respond_ID(tty);
1763 return;
1764 case '7':
1765 save_cur(vc);
1766 return;
1767 case '8':
1768 restore_cur(vc);
1769 return;
1770 case '(':
1771 vc->vc_state = ESsetG0;
1772 return;
1773 case ')':
1774 vc->vc_state = ESsetG1;
1775 return;
1776 case '#':
1777 vc->vc_state = EShash;
1778 return;
1779 case 'c':
1780 reset_terminal(vc, 1);
1781 return;
1782 case '>': /* Numeric keypad */
1783 clr_kbd(vc, kbdapplic);
1784 return;
1785 case '=': /* Appl. keypad */
1786 set_kbd(vc, kbdapplic);
1787 return;
1788 }
1789 return;
1790 case ESnonstd:
1791 if (c=='P') { /* palette escape sequence */
1792 for (vc->vc_npar = 0; vc->vc_npar < NPAR; vc->vc_npar++)
1793 vc->vc_par[vc->vc_npar] = 0;
1794 vc->vc_npar = 0;
1795 vc->vc_state = ESpalette;
1796 return;
1797 } else if (c=='R') { /* reset palette */
1798 reset_palette(vc);
1799 vc->vc_state = ESnormal;
1800 } else
1801 vc->vc_state = ESnormal;
1802 return;
1803 case ESpalette:
1804 if (isxdigit(c)) {
1805 vc->vc_par[vc->vc_npar++] = hex_to_bin(c);
1806 if (vc->vc_npar == 7) {
1807 int i = vc->vc_par[0] * 3, j = 1;
1808 vc->vc_palette[i] = 16 * vc->vc_par[j++];
1809 vc->vc_palette[i++] += vc->vc_par[j++];
1810 vc->vc_palette[i] = 16 * vc->vc_par[j++];
1811 vc->vc_palette[i++] += vc->vc_par[j++];
1812 vc->vc_palette[i] = 16 * vc->vc_par[j++];
1813 vc->vc_palette[i] += vc->vc_par[j];
1814 set_palette(vc);
1815 vc->vc_state = ESnormal;
1816 }
1817 } else
1818 vc->vc_state = ESnormal;
1819 return;
1820 case ESsquare:
1821 for (vc->vc_npar = 0; vc->vc_npar < NPAR; vc->vc_npar++)
1822 vc->vc_par[vc->vc_npar] = 0;
1823 vc->vc_npar = 0;
1824 vc->vc_state = ESgetpars;
1825 if (c == '[') { /* Function key */
1826 vc->vc_state=ESfunckey;
1827 return;
1828 }
1829 vc->vc_ques = (c == '?');
1830 if (vc->vc_ques)
1831 return;
1832 case ESgetpars:
1833 if (c == ';' && vc->vc_npar < NPAR - 1) {
1834 vc->vc_npar++;
1835 return;
1836 } else if (c>='0' && c<='9') {
1837 vc->vc_par[vc->vc_npar] *= 10;
1838 vc->vc_par[vc->vc_npar] += c - '0';
1839 return;
1840 } else
1841 vc->vc_state = ESgotpars;
1842 case ESgotpars:
1843 vc->vc_state = ESnormal;
1844 switch(c) {
1845 case 'h':
1846 set_mode(vc, 1);
1847 return;
1848 case 'l':
1849 set_mode(vc, 0);
1850 return;
1851 case 'c':
1852 if (vc->vc_ques) {
1853 if (vc->vc_par[0])
1854 vc->vc_cursor_type = vc->vc_par[0] | (vc->vc_par[1] << 8) | (vc->vc_par[2] << 16);
1855 else
1856 vc->vc_cursor_type = cur_default;
1857 return;
1858 }
1859 break;
1860 case 'm':
1861 if (vc->vc_ques) {
1862 clear_selection();
1863 if (vc->vc_par[0])
1864 vc->vc_complement_mask = vc->vc_par[0] << 8 | vc->vc_par[1];
1865 else
1866 vc->vc_complement_mask = vc->vc_s_complement_mask;
1867 return;
1868 }
1869 break;
1870 case 'n':
1871 if (!vc->vc_ques) {
1872 if (vc->vc_par[0] == 5)
1873 status_report(tty);
1874 else if (vc->vc_par[0] == 6)
1875 cursor_report(vc, tty);
1876 }
1877 return;
1878 }
1879 if (vc->vc_ques) {
1880 vc->vc_ques = 0;
1881 return;
1882 }
1883 switch(c) {
1884 case 'G': case '`':
1885 if (vc->vc_par[0])
1886 vc->vc_par[0]--;
1887 gotoxy(vc, vc->vc_par[0], vc->vc_y);
1888 return;
1889 case 'A':
1890 if (!vc->vc_par[0])
1891 vc->vc_par[0]++;
1892 gotoxy(vc, vc->vc_x, vc->vc_y - vc->vc_par[0]);
1893 return;
1894 case 'B': case 'e':
1895 if (!vc->vc_par[0])
1896 vc->vc_par[0]++;
1897 gotoxy(vc, vc->vc_x, vc->vc_y + vc->vc_par[0]);
1898 return;
1899 case 'C': case 'a':
1900 if (!vc->vc_par[0])
1901 vc->vc_par[0]++;
1902 gotoxy(vc, vc->vc_x + vc->vc_par[0], vc->vc_y);
1903 return;
1904 case 'D':
1905 if (!vc->vc_par[0])
1906 vc->vc_par[0]++;
1907 gotoxy(vc, vc->vc_x - vc->vc_par[0], vc->vc_y);
1908 return;
1909 case 'E':
1910 if (!vc->vc_par[0])
1911 vc->vc_par[0]++;
1912 gotoxy(vc, 0, vc->vc_y + vc->vc_par[0]);
1913 return;
1914 case 'F':
1915 if (!vc->vc_par[0])
1916 vc->vc_par[0]++;
1917 gotoxy(vc, 0, vc->vc_y - vc->vc_par[0]);
1918 return;
1919 case 'd':
1920 if (vc->vc_par[0])
1921 vc->vc_par[0]--;
1922 gotoxay(vc, vc->vc_x ,vc->vc_par[0]);
1923 return;
1924 case 'H': case 'f':
1925 if (vc->vc_par[0])
1926 vc->vc_par[0]--;
1927 if (vc->vc_par[1])
1928 vc->vc_par[1]--;
1929 gotoxay(vc, vc->vc_par[1], vc->vc_par[0]);
1930 return;
1931 case 'J':
1932 csi_J(vc, vc->vc_par[0]);
1933 return;
1934 case 'K':
1935 csi_K(vc, vc->vc_par[0]);
1936 return;
1937 case 'L':
1938 csi_L(vc, vc->vc_par[0]);
1939 return;
1940 case 'M':
1941 csi_M(vc, vc->vc_par[0]);
1942 return;
1943 case 'P':
1944 csi_P(vc, vc->vc_par[0]);
1945 return;
1946 case 'c':
1947 if (!vc->vc_par[0])
1948 respond_ID(tty);
1949 return;
1950 case 'g':
1951 if (!vc->vc_par[0])
1952 vc->vc_tab_stop[vc->vc_x >> 5] &= ~(1 << (vc->vc_x & 31));
1953 else if (vc->vc_par[0] == 3) {
1954 vc->vc_tab_stop[0] =
1955 vc->vc_tab_stop[1] =
1956 vc->vc_tab_stop[2] =
1957 vc->vc_tab_stop[3] =
1958 vc->vc_tab_stop[4] =
1959 vc->vc_tab_stop[5] =
1960 vc->vc_tab_stop[6] =
1961 vc->vc_tab_stop[7] = 0;
1962 }
1963 return;
1964 case 'm':
1965 csi_m(vc);
1966 return;
1967 case 'q': /* DECLL - but only 3 leds */
1968 /* map 0,1,2,3 to 0,1,2,4 */
1969 if (vc->vc_par[0] < 4)
1970 setledstate(kbd_table + vc->vc_num,
1971 (vc->vc_par[0] < 3) ? vc->vc_par[0] : 4);
1972 return;
1973 case 'r':
1974 if (!vc->vc_par[0])
1975 vc->vc_par[0]++;
1976 if (!vc->vc_par[1])
1977 vc->vc_par[1] = vc->vc_rows;
1978 /* Minimum allowed region is 2 lines */
1979 if (vc->vc_par[0] < vc->vc_par[1] &&
1980 vc->vc_par[1] <= vc->vc_rows) {
1981 vc->vc_top = vc->vc_par[0] - 1;
1982 vc->vc_bottom = vc->vc_par[1];
1983 gotoxay(vc, 0, 0);
1984 }
1985 return;
1986 case 's':
1987 save_cur(vc);
1988 return;
1989 case 'u':
1990 restore_cur(vc);
1991 return;
1992 case 'X':
1993 csi_X(vc, vc->vc_par[0]);
1994 return;
1995 case '@':
1996 csi_at(vc, vc->vc_par[0]);
1997 return;
1998 case ']': /* setterm functions */
1999 setterm_command(vc);
2000 return;
2001 }
2002 return;
2003 case ESpercent:
2004 vc->vc_state = ESnormal;
2005 switch (c) {
2006 case '@': /* defined in ISO 2022 */
2007 vc->vc_utf = 0;
2008 return;
2009 case 'G': /* prelim official escape code */
2010 case '8': /* retained for compatibility */
2011 vc->vc_utf = 1;
2012 return;
2013 }
2014 return;
2015 case ESfunckey:
2016 vc->vc_state = ESnormal;
2017 return;
2018 case EShash:
2019 vc->vc_state = ESnormal;
2020 if (c == '8') {
2021 /* DEC screen alignment test. kludge :-) */
2022 vc->vc_video_erase_char =
2023 (vc->vc_video_erase_char & 0xff00) | 'E';
2024 csi_J(vc, 2);
2025 vc->vc_video_erase_char =
2026 (vc->vc_video_erase_char & 0xff00) | ' ';
2027 do_update_region(vc, vc->vc_origin, vc->vc_screenbuf_size / 2);
2028 }
2029 return;
2030 case ESsetG0:
2031 if (c == '0')
2032 vc->vc_G0_charset = GRAF_MAP;
2033 else if (c == 'B')
2034 vc->vc_G0_charset = LAT1_MAP;
2035 else if (c == 'U')
2036 vc->vc_G0_charset = IBMPC_MAP;
2037 else if (c == 'K')
2038 vc->vc_G0_charset = USER_MAP;
2039 if (vc->vc_charset == 0)
2040 vc->vc_translate = set_translate(vc->vc_G0_charset, vc);
2041 vc->vc_state = ESnormal;
2042 return;
2043 case ESsetG1:
2044 if (c == '0')
2045 vc->vc_G1_charset = GRAF_MAP;
2046 else if (c == 'B')
2047 vc->vc_G1_charset = LAT1_MAP;
2048 else if (c == 'U')
2049 vc->vc_G1_charset = IBMPC_MAP;
2050 else if (c == 'K')
2051 vc->vc_G1_charset = USER_MAP;
2052 if (vc->vc_charset == 1)
2053 vc->vc_translate = set_translate(vc->vc_G1_charset, vc);
2054 vc->vc_state = ESnormal;
2055 return;
2056 default:
2057 vc->vc_state = ESnormal;
2058 }
2059}
2060
2061/* This is a temporary buffer used to prepare a tty console write
2062 * so that we can easily avoid touching user space while holding the
2063 * console spinlock. It is allocated in con_init and is shared by
2064 * this code and the vc_screen read/write tty calls.
2065 *
2066 * We have to allocate this statically in the kernel data section
2067 * since console_init (and thus con_init) are called before any
2068 * kernel memory allocation is available.
2069 */
2070char con_buf[CON_BUF_SIZE];
2071DEFINE_MUTEX(con_buf_mtx);
2072
2073/* is_double_width() is based on the wcwidth() implementation by
2074 * Markus Kuhn -- 2007-05-26 (Unicode 5.0)
2075 * Latest version: http://www.cl.cam.ac.uk/~mgk25/ucs/wcwidth.c
2076 */
2077struct interval {
2078 uint32_t first;
2079 uint32_t last;
2080};
2081
2082static int bisearch(uint32_t ucs, const struct interval *table, int max)
2083{
2084 int min = 0;
2085 int mid;
2086
2087 if (ucs < table[0].first || ucs > table[max].last)
2088 return 0;
2089 while (max >= min) {
2090 mid = (min + max) / 2;
2091 if (ucs > table[mid].last)
2092 min = mid + 1;
2093 else if (ucs < table[mid].first)
2094 max = mid - 1;
2095 else
2096 return 1;
2097 }
2098 return 0;
2099}
2100
2101static int is_double_width(uint32_t ucs)
2102{
2103 static const struct interval double_width[] = {
2104 { 0x1100, 0x115F }, { 0x2329, 0x232A }, { 0x2E80, 0x303E },
2105 { 0x3040, 0xA4CF }, { 0xAC00, 0xD7A3 }, { 0xF900, 0xFAFF },
2106 { 0xFE10, 0xFE19 }, { 0xFE30, 0xFE6F }, { 0xFF00, 0xFF60 },
2107 { 0xFFE0, 0xFFE6 }, { 0x20000, 0x2FFFD }, { 0x30000, 0x3FFFD }
2108 };
2109 return bisearch(ucs, double_width, ARRAY_SIZE(double_width) - 1);
2110}
2111
2112/* acquires console_sem */
2113static int do_con_write(struct tty_struct *tty, const unsigned char *buf, int count)
2114{
2115#ifdef VT_BUF_VRAM_ONLY
2116#define FLUSH do { } while(0);
2117#else
2118#define FLUSH if (draw_x >= 0) { \
2119 vc->vc_sw->con_putcs(vc, (u16 *)draw_from, (u16 *)draw_to - (u16 *)draw_from, vc->vc_y, draw_x); \
2120 draw_x = -1; \
2121 }
2122#endif
2123
2124 int c, tc, ok, n = 0, draw_x = -1;
2125 unsigned int currcons;
2126 unsigned long draw_from = 0, draw_to = 0;
2127 struct vc_data *vc;
2128 unsigned char vc_attr;
2129 struct vt_notifier_param param;
2130 uint8_t rescan;
2131 uint8_t inverse;
2132 uint8_t width;
2133 u16 himask, charmask;
2134
2135 if (in_interrupt())
2136 return count;
2137
2138 might_sleep();
2139
2140 acquire_console_sem();
2141 vc = tty->driver_data;
2142 if (vc == NULL) {
2143 printk(KERN_ERR "vt: argh, driver_data is NULL !\n");
2144 release_console_sem();
2145 return 0;
2146 }
2147
2148 currcons = vc->vc_num;
2149 if (!vc_cons_allocated(currcons)) {
2150 /* could this happen? */
2151 printk_once("con_write: tty %d not allocated\n", currcons+1);
2152 release_console_sem();
2153 return 0;
2154 }
2155
2156 himask = vc->vc_hi_font_mask;
2157 charmask = himask ? 0x1ff : 0xff;
2158
2159 /* undraw cursor first */
2160 if (IS_FG(vc))
2161 hide_cursor(vc);
2162
2163 param.vc = vc;
2164
2165 while (!tty->stopped && count) {
2166 int orig = *buf;
2167 c = orig;
2168 buf++;
2169 n++;
2170 count--;
2171 rescan = 0;
2172 inverse = 0;
2173 width = 1;
2174
2175 /* Do no translation at all in control states */
2176 if (vc->vc_state != ESnormal) {
2177 tc = c;
2178 } else if (vc->vc_utf && !vc->vc_disp_ctrl) {
2179 /* Combine UTF-8 into Unicode in vc_utf_char.
2180 * vc_utf_count is the number of continuation bytes still
2181 * expected to arrive.
2182 * vc_npar is the number of continuation bytes arrived so
2183 * far
2184 */
2185rescan_last_byte:
2186 if ((c & 0xc0) == 0x80) {
2187 /* Continuation byte received */
2188 static const uint32_t utf8_length_changes[] = { 0x0000007f, 0x000007ff, 0x0000ffff, 0x001fffff, 0x03ffffff, 0x7fffffff };
2189 if (vc->vc_utf_count) {
2190 vc->vc_utf_char = (vc->vc_utf_char << 6) | (c & 0x3f);
2191 vc->vc_npar++;
2192 if (--vc->vc_utf_count) {
2193 /* Still need some bytes */
2194 continue;
2195 }
2196 /* Got a whole character */
2197 c = vc->vc_utf_char;
2198 /* Reject overlong sequences */
2199 if (c <= utf8_length_changes[vc->vc_npar - 1] ||
2200 c > utf8_length_changes[vc->vc_npar])
2201 c = 0xfffd;
2202 } else {
2203 /* Unexpected continuation byte */
2204 vc->vc_utf_count = 0;
2205 c = 0xfffd;
2206 }
2207 } else {
2208 /* Single ASCII byte or first byte of a sequence received */
2209 if (vc->vc_utf_count) {
2210 /* Continuation byte expected */
2211 rescan = 1;
2212 vc->vc_utf_count = 0;
2213 c = 0xfffd;
2214 } else if (c > 0x7f) {
2215 /* First byte of a multibyte sequence received */
2216 vc->vc_npar = 0;
2217 if ((c & 0xe0) == 0xc0) {
2218 vc->vc_utf_count = 1;
2219 vc->vc_utf_char = (c & 0x1f);
2220 } else if ((c & 0xf0) == 0xe0) {
2221 vc->vc_utf_count = 2;
2222 vc->vc_utf_char = (c & 0x0f);
2223 } else if ((c & 0xf8) == 0xf0) {
2224 vc->vc_utf_count = 3;
2225 vc->vc_utf_char = (c & 0x07);
2226 } else if ((c & 0xfc) == 0xf8) {
2227 vc->vc_utf_count = 4;
2228 vc->vc_utf_char = (c & 0x03);
2229 } else if ((c & 0xfe) == 0xfc) {
2230 vc->vc_utf_count = 5;
2231 vc->vc_utf_char = (c & 0x01);
2232 } else {
2233 /* 254 and 255 are invalid */
2234 c = 0xfffd;
2235 }
2236 if (vc->vc_utf_count) {
2237 /* Still need some bytes */
2238 continue;
2239 }
2240 }
2241 /* Nothing to do if an ASCII byte was received */
2242 }
2243 /* End of UTF-8 decoding. */
2244 /* c is the received character, or U+FFFD for invalid sequences. */
2245 /* Replace invalid Unicode code points with U+FFFD too */
2246 if ((c >= 0xd800 && c <= 0xdfff) || c == 0xfffe || c == 0xffff)
2247 c = 0xfffd;
2248 tc = c;
2249 } else { /* no utf or alternate charset mode */
2250 tc = vc_translate(vc, c);
2251 }
2252
2253 param.c = tc;
2254 if (atomic_notifier_call_chain(&vt_notifier_list, VT_PREWRITE,
2255 &param) == NOTIFY_STOP)
2256 continue;
2257
2258 /* If the original code was a control character we
2259 * only allow a glyph to be displayed if the code is
2260 * not normally used (such as for cursor movement) or
2261 * if the disp_ctrl mode has been explicitly enabled.
2262 * Certain characters (as given by the CTRL_ALWAYS
2263 * bitmap) are always displayed as control characters,
2264 * as the console would be pretty useless without
2265 * them; to display an arbitrary font position use the
2266 * direct-to-font zone in UTF-8 mode.
2267 */
2268 ok = tc && (c >= 32 ||
2269 !(vc->vc_disp_ctrl ? (CTRL_ALWAYS >> c) & 1 :
2270 vc->vc_utf || ((CTRL_ACTION >> c) & 1)))
2271 && (c != 127 || vc->vc_disp_ctrl)
2272 && (c != 128+27);
2273
2274 if (vc->vc_state == ESnormal && ok) {
2275 if (vc->vc_utf && !vc->vc_disp_ctrl) {
2276 if (is_double_width(c))
2277 width = 2;
2278 }
2279 /* Now try to find out how to display it */
2280 tc = conv_uni_to_pc(vc, tc);
2281 if (tc & ~charmask) {
2282 if (tc == -1 || tc == -2) {
2283 continue; /* nothing to display */
2284 }
2285 /* Glyph not found */
2286 if ((!(vc->vc_utf && !vc->vc_disp_ctrl) || c < 128) && !(c & ~charmask)) {
2287 /* In legacy mode use the glyph we get by a 1:1 mapping.
2288 This would make absolutely no sense with Unicode in mind,
2289 but do this for ASCII characters since a font may lack
2290 Unicode mapping info and we don't want to end up with
2291 having question marks only. */
2292 tc = c;
2293 } else {
2294 /* Display U+FFFD. If it's not found, display an inverse question mark. */
2295 tc = conv_uni_to_pc(vc, 0xfffd);
2296 if (tc < 0) {
2297 inverse = 1;
2298 tc = conv_uni_to_pc(vc, '?');
2299 if (tc < 0) tc = '?';
2300 }
2301 }
2302 }
2303
2304 if (!inverse) {
2305 vc_attr = vc->vc_attr;
2306 } else {
2307 /* invert vc_attr */
2308 if (!vc->vc_can_do_color) {
2309 vc_attr = (vc->vc_attr) ^ 0x08;
2310 } else if (vc->vc_hi_font_mask == 0x100) {
2311 vc_attr = ((vc->vc_attr) & 0x11) | (((vc->vc_attr) & 0xe0) >> 4) | (((vc->vc_attr) & 0x0e) << 4);
2312 } else {
2313 vc_attr = ((vc->vc_attr) & 0x88) | (((vc->vc_attr) & 0x70) >> 4) | (((vc->vc_attr) & 0x07) << 4);
2314 }
2315 FLUSH
2316 }
2317
2318 while (1) {
2319 if (vc->vc_need_wrap || vc->vc_decim)
2320 FLUSH
2321 if (vc->vc_need_wrap) {
2322 cr(vc);
2323 lf(vc);
2324 }
2325 if (vc->vc_decim)
2326 insert_char(vc, 1);
2327 scr_writew(himask ?
2328 ((vc_attr << 8) & ~himask) + ((tc & 0x100) ? himask : 0) + (tc & 0xff) :
2329 (vc_attr << 8) + tc,
2330 (u16 *) vc->vc_pos);
2331 if (DO_UPDATE(vc) && draw_x < 0) {
2332 draw_x = vc->vc_x;
2333 draw_from = vc->vc_pos;
2334 }
2335 if (vc->vc_x == vc->vc_cols - 1) {
2336 vc->vc_need_wrap = vc->vc_decawm;
2337 draw_to = vc->vc_pos + 2;
2338 } else {
2339 vc->vc_x++;
2340 draw_to = (vc->vc_pos += 2);
2341 }
2342
2343 if (!--width) break;
2344
2345 tc = conv_uni_to_pc(vc, ' '); /* A space is printed in the second column */
2346 if (tc < 0) tc = ' ';
2347 }
2348 notify_write(vc, c);
2349
2350 if (inverse) {
2351 FLUSH
2352 }
2353
2354 if (rescan) {
2355 rescan = 0;
2356 inverse = 0;
2357 width = 1;
2358 c = orig;
2359 goto rescan_last_byte;
2360 }
2361 continue;
2362 }
2363 FLUSH
2364 do_con_trol(tty, vc, orig);
2365 }
2366 FLUSH
2367 console_conditional_schedule();
2368 release_console_sem();
2369 notify_update(vc);
2370 return n;
2371#undef FLUSH
2372}
2373
2374/*
2375 * This is the console switching callback.
2376 *
2377 * Doing console switching in a process context allows
2378 * us to do the switches asynchronously (needed when we want
2379 * to switch due to a keyboard interrupt). Synchronization
2380 * with other console code and prevention of re-entrancy is
2381 * ensured with console_sem.
2382 */
2383static void console_callback(struct work_struct *ignored)
2384{
2385 acquire_console_sem();
2386
2387 if (want_console >= 0) {
2388 if (want_console != fg_console &&
2389 vc_cons_allocated(want_console)) {
2390 hide_cursor(vc_cons[fg_console].d);
2391 change_console(vc_cons[want_console].d);
2392 /* we only changed when the console had already
2393 been allocated - a new console is not created
2394 in an interrupt routine */
2395 }
2396 want_console = -1;
2397 }
2398 if (do_poke_blanked_console) { /* do not unblank for a LED change */
2399 do_poke_blanked_console = 0;
2400 poke_blanked_console();
2401 }
2402 if (scrollback_delta) {
2403 struct vc_data *vc = vc_cons[fg_console].d;
2404 clear_selection();
2405 if (vc->vc_mode == KD_TEXT)
2406 vc->vc_sw->con_scrolldelta(vc, scrollback_delta);
2407 scrollback_delta = 0;
2408 }
2409 if (blank_timer_expired) {
2410 do_blank_screen(0);
2411 blank_timer_expired = 0;
2412 }
2413 notify_update(vc_cons[fg_console].d);
2414
2415 release_console_sem();
2416}
2417
2418int set_console(int nr)
2419{
2420 struct vc_data *vc = vc_cons[fg_console].d;
2421
2422 if (!vc_cons_allocated(nr) || vt_dont_switch ||
2423 (vc->vt_mode.mode == VT_AUTO && vc->vc_mode == KD_GRAPHICS)) {
2424
2425 /*
2426 * Console switch will fail in console_callback() or
2427 * change_console() so there is no point scheduling
2428 * the callback
2429 *
2430 * Existing set_console() users don't check the return
2431 * value so this shouldn't break anything
2432 */
2433 return -EINVAL;
2434 }
2435
2436 want_console = nr;
2437 schedule_console_callback();
2438
2439 return 0;
2440}
2441
2442struct tty_driver *console_driver;
2443
2444#ifdef CONFIG_VT_CONSOLE
2445
2446/**
2447 * vt_kmsg_redirect() - Sets/gets the kernel message console
2448 * @new: The new virtual terminal number or -1 if the console should stay
2449 * unchanged
2450 *
2451 * By default, the kernel messages are always printed on the current virtual
2452 * console. However, the user may modify that default with the
2453 * TIOCL_SETKMSGREDIRECT ioctl call.
2454 *
2455 * This function sets the kernel message console to be @new. It returns the old
2456 * virtual console number. The virtual terminal number 0 (both as parameter and
2457 * return value) means no redirection (i.e. always printed on the currently
2458 * active console).
2459 *
2460 * The parameter -1 means that only the current console is returned, but the
2461 * value is not modified. You may use the macro vt_get_kmsg_redirect() in that
2462 * case to make the code more understandable.
2463 *
2464 * When the kernel is compiled without CONFIG_VT_CONSOLE, this function ignores
2465 * the parameter and always returns 0.
2466 */
2467int vt_kmsg_redirect(int new)
2468{
2469 static int kmsg_con;
2470
2471 if (new != -1)
2472 return xchg(&kmsg_con, new);
2473 else
2474 return kmsg_con;
2475}
2476
2477/*
2478 * Console on virtual terminal
2479 *
2480 * The console must be locked when we get here.
2481 */
2482
2483static void vt_console_print(struct console *co, const char *b, unsigned count)
2484{
2485 struct vc_data *vc = vc_cons[fg_console].d;
2486 unsigned char c;
2487 static DEFINE_SPINLOCK(printing_lock);
2488 const ushort *start;
2489 ushort cnt = 0;
2490 ushort myx;
2491 int kmsg_console;
2492
2493 /* console busy or not yet initialized */
2494 if (!printable)
2495 return;
2496 if (!spin_trylock(&printing_lock))
2497 return;
2498
2499 kmsg_console = vt_get_kmsg_redirect();
2500 if (kmsg_console && vc_cons_allocated(kmsg_console - 1))
2501 vc = vc_cons[kmsg_console - 1].d;
2502
2503 /* read `x' only after setting currcons properly (otherwise
2504 the `x' macro will read the x of the foreground console). */
2505 myx = vc->vc_x;
2506
2507 if (!vc_cons_allocated(fg_console)) {
2508 /* impossible */
2509 /* printk("vt_console_print: tty %d not allocated ??\n", currcons+1); */
2510 goto quit;
2511 }
2512
2513 if (vc->vc_mode != KD_TEXT && !vt_force_oops_output(vc))
2514 goto quit;
2515
2516 /* undraw cursor first */
2517 if (IS_FG(vc))
2518 hide_cursor(vc);
2519
2520 start = (ushort *)vc->vc_pos;
2521
2522 /* Contrived structure to try to emulate original need_wrap behaviour
2523 * Problems caused when we have need_wrap set on '\n' character */
2524 while (count--) {
2525 c = *b++;
2526 if (c == 10 || c == 13 || c == 8 || vc->vc_need_wrap) {
2527 if (cnt > 0) {
2528 if (CON_IS_VISIBLE(vc))
2529 vc->vc_sw->con_putcs(vc, start, cnt, vc->vc_y, vc->vc_x);
2530 vc->vc_x += cnt;
2531 if (vc->vc_need_wrap)
2532 vc->vc_x--;
2533 cnt = 0;
2534 }
2535 if (c == 8) { /* backspace */
2536 bs(vc);
2537 start = (ushort *)vc->vc_pos;
2538 myx = vc->vc_x;
2539 continue;
2540 }
2541 if (c != 13)
2542 lf(vc);
2543 cr(vc);
2544 start = (ushort *)vc->vc_pos;
2545 myx = vc->vc_x;
2546 if (c == 10 || c == 13)
2547 continue;
2548 }
2549 scr_writew((vc->vc_attr << 8) + c, (unsigned short *)vc->vc_pos);
2550 notify_write(vc, c);
2551 cnt++;
2552 if (myx == vc->vc_cols - 1) {
2553 vc->vc_need_wrap = 1;
2554 continue;
2555 }
2556 vc->vc_pos += 2;
2557 myx++;
2558 }
2559 if (cnt > 0) {
2560 if (CON_IS_VISIBLE(vc))
2561 vc->vc_sw->con_putcs(vc, start, cnt, vc->vc_y, vc->vc_x);
2562 vc->vc_x += cnt;
2563 if (vc->vc_x == vc->vc_cols) {
2564 vc->vc_x--;
2565 vc->vc_need_wrap = 1;
2566 }
2567 }
2568 set_cursor(vc);
2569 notify_update(vc);
2570
2571quit:
2572 spin_unlock(&printing_lock);
2573}
2574
2575static struct tty_driver *vt_console_device(struct console *c, int *index)
2576{
2577 *index = c->index ? c->index-1 : fg_console;
2578 return console_driver;
2579}
2580
2581static struct console vt_console_driver = {
2582 .name = "tty",
2583 .write = vt_console_print,
2584 .device = vt_console_device,
2585 .unblank = unblank_screen,
2586 .flags = CON_PRINTBUFFER,
2587 .index = -1,
2588};
2589#endif
2590
2591/*
2592 * Handling of Linux-specific VC ioctls
2593 */
2594
2595/*
2596 * Generally a bit racy with respect to console_sem().
2597 *
2598 * There are some functions which don't need it.
2599 *
2600 * There are some functions which can sleep for arbitrary periods
2601 * (paste_selection) but we don't need the lock there anyway.
2602 *
2603 * set_selection has locking, and definitely needs it
2604 */
2605
2606int tioclinux(struct tty_struct *tty, unsigned long arg)
2607{
2608 char type, data;
2609 char __user *p = (char __user *)arg;
2610 int lines;
2611 int ret;
2612
2613 if (current->signal->tty != tty && !capable(CAP_SYS_ADMIN))
2614 return -EPERM;
2615 if (get_user(type, p))
2616 return -EFAULT;
2617 ret = 0;
2618
2619 switch (type)
2620 {
2621 case TIOCL_SETSEL:
2622 acquire_console_sem();
2623 ret = set_selection((struct tiocl_selection __user *)(p+1), tty);
2624 release_console_sem();
2625 break;
2626 case TIOCL_PASTESEL:
2627 ret = paste_selection(tty);
2628 break;
2629 case TIOCL_UNBLANKSCREEN:
2630 acquire_console_sem();
2631 unblank_screen();
2632 release_console_sem();
2633 break;
2634 case TIOCL_SELLOADLUT:
2635 ret = sel_loadlut(p);
2636 break;
2637 case TIOCL_GETSHIFTSTATE:
2638
2639 /*
2640 * Make it possible to react to Shift+Mousebutton.
2641 * Note that 'shift_state' is an undocumented
2642 * kernel-internal variable; programs not closely
2643 * related to the kernel should not use this.
2644 */
2645 data = shift_state;
2646 ret = __put_user(data, p);
2647 break;
2648 case TIOCL_GETMOUSEREPORTING:
2649 data = mouse_reporting();
2650 ret = __put_user(data, p);
2651 break;
2652 case TIOCL_SETVESABLANK:
2653 ret = set_vesa_blanking(p);
2654 break;
2655 case TIOCL_GETKMSGREDIRECT:
2656 data = vt_get_kmsg_redirect();
2657 ret = __put_user(data, p);
2658 break;
2659 case TIOCL_SETKMSGREDIRECT:
2660 if (!capable(CAP_SYS_ADMIN)) {
2661 ret = -EPERM;
2662 } else {
2663 if (get_user(data, p+1))
2664 ret = -EFAULT;
2665 else
2666 vt_kmsg_redirect(data);
2667 }
2668 break;
2669 case TIOCL_GETFGCONSOLE:
2670 ret = fg_console;
2671 break;
2672 case TIOCL_SCROLLCONSOLE:
2673 if (get_user(lines, (s32 __user *)(p+4))) {
2674 ret = -EFAULT;
2675 } else {
2676 scrollfront(vc_cons[fg_console].d, lines);
2677 ret = 0;
2678 }
2679 break;
2680 case TIOCL_BLANKSCREEN: /* until explicitly unblanked, not only poked */
2681 acquire_console_sem();
2682 ignore_poke = 1;
2683 do_blank_screen(0);
2684 release_console_sem();
2685 break;
2686 case TIOCL_BLANKEDSCREEN:
2687 ret = console_blanked;
2688 break;
2689 default:
2690 ret = -EINVAL;
2691 break;
2692 }
2693 return ret;
2694}
2695
2696/*
2697 * /dev/ttyN handling
2698 */
2699
2700static int con_write(struct tty_struct *tty, const unsigned char *buf, int count)
2701{
2702 int retval;
2703
2704 retval = do_con_write(tty, buf, count);
2705 con_flush_chars(tty);
2706
2707 return retval;
2708}
2709
2710static int con_put_char(struct tty_struct *tty, unsigned char ch)
2711{
2712 if (in_interrupt())
2713 return 0; /* n_r3964 calls put_char() from interrupt context */
2714 return do_con_write(tty, &ch, 1);
2715}
2716
2717static int con_write_room(struct tty_struct *tty)
2718{
2719 if (tty->stopped)
2720 return 0;
2721 return 32768; /* No limit, really; we're not buffering */
2722}
2723
2724static int con_chars_in_buffer(struct tty_struct *tty)
2725{
2726 return 0; /* we're not buffering */
2727}
2728
2729/*
2730 * con_throttle and con_unthrottle are only used for
2731 * paste_selection(), which has to stuff in a large number of
2732 * characters...
2733 */
2734static void con_throttle(struct tty_struct *tty)
2735{
2736}
2737
2738static void con_unthrottle(struct tty_struct *tty)
2739{
2740 struct vc_data *vc = tty->driver_data;
2741
2742 wake_up_interruptible(&vc->paste_wait);
2743}
2744
2745/*
2746 * Turn the Scroll-Lock LED on when the tty is stopped
2747 */
2748static void con_stop(struct tty_struct *tty)
2749{
2750 int console_num;
2751 if (!tty)
2752 return;
2753 console_num = tty->index;
2754 if (!vc_cons_allocated(console_num))
2755 return;
2756 set_vc_kbd_led(kbd_table + console_num, VC_SCROLLOCK);
2757 set_leds();
2758}
2759
2760/*
2761 * Turn the Scroll-Lock LED off when the console is started
2762 */
2763static void con_start(struct tty_struct *tty)
2764{
2765 int console_num;
2766 if (!tty)
2767 return;
2768 console_num = tty->index;
2769 if (!vc_cons_allocated(console_num))
2770 return;
2771 clr_vc_kbd_led(kbd_table + console_num, VC_SCROLLOCK);
2772 set_leds();
2773}
2774
2775static void con_flush_chars(struct tty_struct *tty)
2776{
2777 struct vc_data *vc;
2778
2779 if (in_interrupt()) /* from flush_to_ldisc */
2780 return;
2781
2782 /* if we race with con_close(), vt may be null */
2783 acquire_console_sem();
2784 vc = tty->driver_data;
2785 if (vc)
2786 set_cursor(vc);
2787 release_console_sem();
2788}
2789
2790/*
2791 * Allocate the console screen memory.
2792 */
2793static int con_open(struct tty_struct *tty, struct file *filp)
2794{
2795 unsigned int currcons = tty->index;
2796 int ret = 0;
2797
2798 acquire_console_sem();
2799 if (tty->driver_data == NULL) {
2800 ret = vc_allocate(currcons);
2801 if (ret == 0) {
2802 struct vc_data *vc = vc_cons[currcons].d;
2803
2804 /* Still being freed */
2805 if (vc->port.tty) {
2806 release_console_sem();
2807 return -ERESTARTSYS;
2808 }
2809 tty->driver_data = vc;
2810 vc->port.tty = tty;
2811
2812 if (!tty->winsize.ws_row && !tty->winsize.ws_col) {
2813 tty->winsize.ws_row = vc_cons[currcons].d->vc_rows;
2814 tty->winsize.ws_col = vc_cons[currcons].d->vc_cols;
2815 }
2816 if (vc->vc_utf)
2817 tty->termios->c_iflag |= IUTF8;
2818 else
2819 tty->termios->c_iflag &= ~IUTF8;
2820 release_console_sem();
2821 return ret;
2822 }
2823 }
2824 release_console_sem();
2825 return ret;
2826}
2827
2828static void con_close(struct tty_struct *tty, struct file *filp)
2829{
2830 /* Nothing to do - we defer to shutdown */
2831}
2832
2833static void con_shutdown(struct tty_struct *tty)
2834{
2835 struct vc_data *vc = tty->driver_data;
2836 BUG_ON(vc == NULL);
2837 acquire_console_sem();
2838 vc->port.tty = NULL;
2839 release_console_sem();
2840 tty_shutdown(tty);
2841}
2842
2843static int default_italic_color = 2; // green (ASCII)
2844static int default_underline_color = 3; // cyan (ASCII)
2845module_param_named(italic, default_italic_color, int, S_IRUGO | S_IWUSR);
2846module_param_named(underline, default_underline_color, int, S_IRUGO | S_IWUSR);
2847
2848static void vc_init(struct vc_data *vc, unsigned int rows,
2849 unsigned int cols, int do_clear)
2850{
2851 int j, k ;
2852
2853 vc->vc_cols = cols;
2854 vc->vc_rows = rows;
2855 vc->vc_size_row = cols << 1;
2856 vc->vc_screenbuf_size = vc->vc_rows * vc->vc_size_row;
2857
2858 set_origin(vc);
2859 vc->vc_pos = vc->vc_origin;
2860 reset_vc(vc);
2861 for (j=k=0; j<16; j++) {
2862 vc->vc_palette[k++] = default_red[j] ;
2863 vc->vc_palette[k++] = default_grn[j] ;
2864 vc->vc_palette[k++] = default_blu[j] ;
2865 }
2866 vc->vc_def_color = 0x07; /* white */
2867 vc->vc_ulcolor = default_underline_color;
2868 vc->vc_itcolor = default_italic_color;
2869 vc->vc_halfcolor = 0x08; /* grey */
2870 init_waitqueue_head(&vc->paste_wait);
2871 reset_terminal(vc, do_clear);
2872}
2873
2874/*
2875 * This routine initializes console interrupts, and does nothing
2876 * else. If you want the screen to clear, call tty_write with
2877 * the appropriate escape-sequence.
2878 */
2879
2880static int __init con_init(void)
2881{
2882 const char *display_desc = NULL;
2883 struct vc_data *vc;
2884 unsigned int currcons = 0, i;
2885
2886 acquire_console_sem();
2887
2888 if (conswitchp)
2889 display_desc = conswitchp->con_startup();
2890 if (!display_desc) {
2891 fg_console = 0;
2892 release_console_sem();
2893 return 0;
2894 }
2895
2896 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
2897 struct con_driver *con_driver = &registered_con_driver[i];
2898
2899 if (con_driver->con == NULL) {
2900 con_driver->con = conswitchp;
2901 con_driver->desc = display_desc;
2902 con_driver->flag = CON_DRIVER_FLAG_INIT;
2903 con_driver->first = 0;
2904 con_driver->last = MAX_NR_CONSOLES - 1;
2905 break;
2906 }
2907 }
2908
2909 for (i = 0; i < MAX_NR_CONSOLES; i++)
2910 con_driver_map[i] = conswitchp;
2911
2912 if (blankinterval) {
2913 blank_state = blank_normal_wait;
2914 mod_timer(&console_timer, jiffies + (blankinterval * HZ));
2915 }
2916
2917 for (currcons = 0; currcons < MIN_NR_CONSOLES; currcons++) {
2918 vc_cons[currcons].d = vc = kzalloc(sizeof(struct vc_data), GFP_NOWAIT);
2919 INIT_WORK(&vc_cons[currcons].SAK_work, vc_SAK);
2920 tty_port_init(&vc->port);
2921 visual_init(vc, currcons, 1);
2922 vc->vc_screenbuf = kzalloc(vc->vc_screenbuf_size, GFP_NOWAIT);
2923 vc_init(vc, vc->vc_rows, vc->vc_cols,
2924 currcons || !vc->vc_sw->con_save_screen);
2925 }
2926 currcons = fg_console = 0;
2927 master_display_fg = vc = vc_cons[currcons].d;
2928 set_origin(vc);
2929 save_screen(vc);
2930 gotoxy(vc, vc->vc_x, vc->vc_y);
2931 csi_J(vc, 0);
2932 update_screen(vc);
2933 printk("Console: %s %s %dx%d",
2934 vc->vc_can_do_color ? "colour" : "mono",
2935 display_desc, vc->vc_cols, vc->vc_rows);
2936 printable = 1;
2937 printk("\n");
2938
2939 release_console_sem();
2940
2941#ifdef CONFIG_VT_CONSOLE
2942 register_console(&vt_console_driver);
2943#endif
2944 return 0;
2945}
2946console_initcall(con_init);
2947
2948static const struct tty_operations con_ops = {
2949 .open = con_open,
2950 .close = con_close,
2951 .write = con_write,
2952 .write_room = con_write_room,
2953 .put_char = con_put_char,
2954 .flush_chars = con_flush_chars,
2955 .chars_in_buffer = con_chars_in_buffer,
2956 .ioctl = vt_ioctl,
2957#ifdef CONFIG_COMPAT
2958 .compat_ioctl = vt_compat_ioctl,
2959#endif
2960 .stop = con_stop,
2961 .start = con_start,
2962 .throttle = con_throttle,
2963 .unthrottle = con_unthrottle,
2964 .resize = vt_resize,
2965 .shutdown = con_shutdown
2966};
2967
2968static struct cdev vc0_cdev;
2969
2970int __init vty_init(const struct file_operations *console_fops)
2971{
2972 cdev_init(&vc0_cdev, console_fops);
2973 if (cdev_add(&vc0_cdev, MKDEV(TTY_MAJOR, 0), 1) ||
2974 register_chrdev_region(MKDEV(TTY_MAJOR, 0), 1, "/dev/vc/0") < 0)
2975 panic("Couldn't register /dev/tty0 driver\n");
2976 device_create(tty_class, NULL, MKDEV(TTY_MAJOR, 0), NULL, "tty0");
2977
2978 vcs_init();
2979
2980 console_driver = alloc_tty_driver(MAX_NR_CONSOLES);
2981 if (!console_driver)
2982 panic("Couldn't allocate console driver\n");
2983 console_driver->owner = THIS_MODULE;
2984 console_driver->name = "tty";
2985 console_driver->name_base = 1;
2986 console_driver->major = TTY_MAJOR;
2987 console_driver->minor_start = 1;
2988 console_driver->type = TTY_DRIVER_TYPE_CONSOLE;
2989 console_driver->init_termios = tty_std_termios;
2990 if (default_utf8)
2991 console_driver->init_termios.c_iflag |= IUTF8;
2992 console_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_RESET_TERMIOS;
2993 tty_set_operations(console_driver, &con_ops);
2994 if (tty_register_driver(console_driver))
2995 panic("Couldn't register console driver\n");
2996 kbd_init();
2997 console_map_init();
2998#ifdef CONFIG_MDA_CONSOLE
2999 mda_console_init();
3000#endif
3001 return 0;
3002}
3003
3004#ifndef VT_SINGLE_DRIVER
3005
3006static struct class *vtconsole_class;
3007
3008static int bind_con_driver(const struct consw *csw, int first, int last,
3009 int deflt)
3010{
3011 struct module *owner = csw->owner;
3012 const char *desc = NULL;
3013 struct con_driver *con_driver;
3014 int i, j = -1, k = -1, retval = -ENODEV;
3015
3016 if (!try_module_get(owner))
3017 return -ENODEV;
3018
3019 acquire_console_sem();
3020
3021 /* check if driver is registered */
3022 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3023 con_driver = &registered_con_driver[i];
3024
3025 if (con_driver->con == csw) {
3026 desc = con_driver->desc;
3027 retval = 0;
3028 break;
3029 }
3030 }
3031
3032 if (retval)
3033 goto err;
3034
3035 if (!(con_driver->flag & CON_DRIVER_FLAG_INIT)) {
3036 csw->con_startup();
3037 con_driver->flag |= CON_DRIVER_FLAG_INIT;
3038 }
3039
3040 if (deflt) {
3041 if (conswitchp)
3042 module_put(conswitchp->owner);
3043
3044 __module_get(owner);
3045 conswitchp = csw;
3046 }
3047
3048 first = max(first, con_driver->first);
3049 last = min(last, con_driver->last);
3050
3051 for (i = first; i <= last; i++) {
3052 int old_was_color;
3053 struct vc_data *vc = vc_cons[i].d;
3054
3055 if (con_driver_map[i])
3056 module_put(con_driver_map[i]->owner);
3057 __module_get(owner);
3058 con_driver_map[i] = csw;
3059
3060 if (!vc || !vc->vc_sw)
3061 continue;
3062
3063 j = i;
3064
3065 if (CON_IS_VISIBLE(vc)) {
3066 k = i;
3067 save_screen(vc);
3068 }
3069
3070 old_was_color = vc->vc_can_do_color;
3071 vc->vc_sw->con_deinit(vc);
3072 vc->vc_origin = (unsigned long)vc->vc_screenbuf;
3073 visual_init(vc, i, 0);
3074 set_origin(vc);
3075 update_attr(vc);
3076
3077 /* If the console changed between mono <-> color, then
3078 * the attributes in the screenbuf will be wrong. The
3079 * following resets all attributes to something sane.
3080 */
3081 if (old_was_color != vc->vc_can_do_color)
3082 clear_buffer_attributes(vc);
3083 }
3084
3085 printk("Console: switching ");
3086 if (!deflt)
3087 printk("consoles %d-%d ", first+1, last+1);
3088 if (j >= 0) {
3089 struct vc_data *vc = vc_cons[j].d;
3090
3091 printk("to %s %s %dx%d\n",
3092 vc->vc_can_do_color ? "colour" : "mono",
3093 desc, vc->vc_cols, vc->vc_rows);
3094
3095 if (k >= 0) {
3096 vc = vc_cons[k].d;
3097 update_screen(vc);
3098 }
3099 } else
3100 printk("to %s\n", desc);
3101
3102 retval = 0;
3103err:
3104 release_console_sem();
3105 module_put(owner);
3106 return retval;
3107};
3108
3109#ifdef CONFIG_VT_HW_CONSOLE_BINDING
3110static int con_is_graphics(const struct consw *csw, int first, int last)
3111{
3112 int i, retval = 0;
3113
3114 for (i = first; i <= last; i++) {
3115 struct vc_data *vc = vc_cons[i].d;
3116
3117 if (vc && vc->vc_mode == KD_GRAPHICS) {
3118 retval = 1;
3119 break;
3120 }
3121 }
3122
3123 return retval;
3124}
3125
3126/**
3127 * unbind_con_driver - unbind a console driver
3128 * @csw: pointer to console driver to unregister
3129 * @first: first in range of consoles that @csw should be unbound from
3130 * @last: last in range of consoles that @csw should be unbound from
3131 * @deflt: should next bound console driver be default after @csw is unbound?
3132 *
3133 * To unbind a driver from all possible consoles, pass 0 as @first and
3134 * %MAX_NR_CONSOLES as @last.
3135 *
3136 * @deflt controls whether the console that ends up replacing @csw should be
3137 * the default console.
3138 *
3139 * RETURNS:
3140 * -ENODEV if @csw isn't a registered console driver or can't be unregistered
3141 * or 0 on success.
3142 */
3143int unbind_con_driver(const struct consw *csw, int first, int last, int deflt)
3144{
3145 struct module *owner = csw->owner;
3146 const struct consw *defcsw = NULL;
3147 struct con_driver *con_driver = NULL, *con_back = NULL;
3148 int i, retval = -ENODEV;
3149
3150 if (!try_module_get(owner))
3151 return -ENODEV;
3152
3153 acquire_console_sem();
3154
3155 /* check if driver is registered and if it is unbindable */
3156 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3157 con_driver = &registered_con_driver[i];
3158
3159 if (con_driver->con == csw &&
3160 con_driver->flag & CON_DRIVER_FLAG_MODULE) {
3161 retval = 0;
3162 break;
3163 }
3164 }
3165
3166 if (retval) {
3167 release_console_sem();
3168 goto err;
3169 }
3170
3171 retval = -ENODEV;
3172
3173 /* check if backup driver exists */
3174 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3175 con_back = &registered_con_driver[i];
3176
3177 if (con_back->con &&
3178 !(con_back->flag & CON_DRIVER_FLAG_MODULE)) {
3179 defcsw = con_back->con;
3180 retval = 0;
3181 break;
3182 }
3183 }
3184
3185 if (retval) {
3186 release_console_sem();
3187 goto err;
3188 }
3189
3190 if (!con_is_bound(csw)) {
3191 release_console_sem();
3192 goto err;
3193 }
3194
3195 first = max(first, con_driver->first);
3196 last = min(last, con_driver->last);
3197
3198 for (i = first; i <= last; i++) {
3199 if (con_driver_map[i] == csw) {
3200 module_put(csw->owner);
3201 con_driver_map[i] = NULL;
3202 }
3203 }
3204
3205 if (!con_is_bound(defcsw)) {
3206 const struct consw *defconsw = conswitchp;
3207
3208 defcsw->con_startup();
3209 con_back->flag |= CON_DRIVER_FLAG_INIT;
3210 /*
3211 * vgacon may change the default driver to point
3212 * to dummycon, we restore it here...
3213 */
3214 conswitchp = defconsw;
3215 }
3216
3217 if (!con_is_bound(csw))
3218 con_driver->flag &= ~CON_DRIVER_FLAG_INIT;
3219
3220 release_console_sem();
3221 /* ignore return value, binding should not fail */
3222 bind_con_driver(defcsw, first, last, deflt);
3223err:
3224 module_put(owner);
3225 return retval;
3226
3227}
3228EXPORT_SYMBOL(unbind_con_driver);
3229
3230static int vt_bind(struct con_driver *con)
3231{
3232 const struct consw *defcsw = NULL, *csw = NULL;
3233 int i, more = 1, first = -1, last = -1, deflt = 0;
3234
3235 if (!con->con || !(con->flag & CON_DRIVER_FLAG_MODULE) ||
3236 con_is_graphics(con->con, con->first, con->last))
3237 goto err;
3238
3239 csw = con->con;
3240
3241 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3242 struct con_driver *con = &registered_con_driver[i];
3243
3244 if (con->con && !(con->flag & CON_DRIVER_FLAG_MODULE)) {
3245 defcsw = con->con;
3246 break;
3247 }
3248 }
3249
3250 if (!defcsw)
3251 goto err;
3252
3253 while (more) {
3254 more = 0;
3255
3256 for (i = con->first; i <= con->last; i++) {
3257 if (con_driver_map[i] == defcsw) {
3258 if (first == -1)
3259 first = i;
3260 last = i;
3261 more = 1;
3262 } else if (first != -1)
3263 break;
3264 }
3265
3266 if (first == 0 && last == MAX_NR_CONSOLES -1)
3267 deflt = 1;
3268
3269 if (first != -1)
3270 bind_con_driver(csw, first, last, deflt);
3271
3272 first = -1;
3273 last = -1;
3274 deflt = 0;
3275 }
3276
3277err:
3278 return 0;
3279}
3280
3281static int vt_unbind(struct con_driver *con)
3282{
3283 const struct consw *csw = NULL;
3284 int i, more = 1, first = -1, last = -1, deflt = 0;
3285
3286 if (!con->con || !(con->flag & CON_DRIVER_FLAG_MODULE) ||
3287 con_is_graphics(con->con, con->first, con->last))
3288 goto err;
3289
3290 csw = con->con;
3291
3292 while (more) {
3293 more = 0;
3294
3295 for (i = con->first; i <= con->last; i++) {
3296 if (con_driver_map[i] == csw) {
3297 if (first == -1)
3298 first = i;
3299 last = i;
3300 more = 1;
3301 } else if (first != -1)
3302 break;
3303 }
3304
3305 if (first == 0 && last == MAX_NR_CONSOLES -1)
3306 deflt = 1;
3307
3308 if (first != -1)
3309 unbind_con_driver(csw, first, last, deflt);
3310
3311 first = -1;
3312 last = -1;
3313 deflt = 0;
3314 }
3315
3316err:
3317 return 0;
3318}
3319#else
3320static inline int vt_bind(struct con_driver *con)
3321{
3322 return 0;
3323}
3324static inline int vt_unbind(struct con_driver *con)
3325{
3326 return 0;
3327}
3328#endif /* CONFIG_VT_HW_CONSOLE_BINDING */
3329
3330static ssize_t store_bind(struct device *dev, struct device_attribute *attr,
3331 const char *buf, size_t count)
3332{
3333 struct con_driver *con = dev_get_drvdata(dev);
3334 int bind = simple_strtoul(buf, NULL, 0);
3335
3336 if (bind)
3337 vt_bind(con);
3338 else
3339 vt_unbind(con);
3340
3341 return count;
3342}
3343
3344static ssize_t show_bind(struct device *dev, struct device_attribute *attr,
3345 char *buf)
3346{
3347 struct con_driver *con = dev_get_drvdata(dev);
3348 int bind = con_is_bound(con->con);
3349
3350 return snprintf(buf, PAGE_SIZE, "%i\n", bind);
3351}
3352
3353static ssize_t show_name(struct device *dev, struct device_attribute *attr,
3354 char *buf)
3355{
3356 struct con_driver *con = dev_get_drvdata(dev);
3357
3358 return snprintf(buf, PAGE_SIZE, "%s %s\n",
3359 (con->flag & CON_DRIVER_FLAG_MODULE) ? "(M)" : "(S)",
3360 con->desc);
3361
3362}
3363
3364static struct device_attribute device_attrs[] = {
3365 __ATTR(bind, S_IRUGO|S_IWUSR, show_bind, store_bind),
3366 __ATTR(name, S_IRUGO, show_name, NULL),
3367};
3368
3369static int vtconsole_init_device(struct con_driver *con)
3370{
3371 int i;
3372 int error = 0;
3373
3374 con->flag |= CON_DRIVER_FLAG_ATTR;
3375 dev_set_drvdata(con->dev, con);
3376 for (i = 0; i < ARRAY_SIZE(device_attrs); i++) {
3377 error = device_create_file(con->dev, &device_attrs[i]);
3378 if (error)
3379 break;
3380 }
3381
3382 if (error) {
3383 while (--i >= 0)
3384 device_remove_file(con->dev, &device_attrs[i]);
3385 con->flag &= ~CON_DRIVER_FLAG_ATTR;
3386 }
3387
3388 return error;
3389}
3390
3391static void vtconsole_deinit_device(struct con_driver *con)
3392{
3393 int i;
3394
3395 if (con->flag & CON_DRIVER_FLAG_ATTR) {
3396 for (i = 0; i < ARRAY_SIZE(device_attrs); i++)
3397 device_remove_file(con->dev, &device_attrs[i]);
3398 con->flag &= ~CON_DRIVER_FLAG_ATTR;
3399 }
3400}
3401
3402/**
3403 * con_is_bound - checks if driver is bound to the console
3404 * @csw: console driver
3405 *
3406 * RETURNS: zero if unbound, nonzero if bound
3407 *
3408 * Drivers can call this and if zero, they should release
3409 * all resources allocated on con_startup()
3410 */
3411int con_is_bound(const struct consw *csw)
3412{
3413 int i, bound = 0;
3414
3415 for (i = 0; i < MAX_NR_CONSOLES; i++) {
3416 if (con_driver_map[i] == csw) {
3417 bound = 1;
3418 break;
3419 }
3420 }
3421
3422 return bound;
3423}
3424EXPORT_SYMBOL(con_is_bound);
3425
3426/**
3427 * con_debug_enter - prepare the console for the kernel debugger
3428 * @sw: console driver
3429 *
3430 * Called when the console is taken over by the kernel debugger, this
3431 * function needs to save the current console state, then put the console
3432 * into a state suitable for the kernel debugger.
3433 *
3434 * RETURNS:
3435 * Zero on success, nonzero if a failure occurred when trying to prepare
3436 * the console for the debugger.
3437 */
3438int con_debug_enter(struct vc_data *vc)
3439{
3440 int ret = 0;
3441
3442 saved_fg_console = fg_console;
3443 saved_last_console = last_console;
3444 saved_want_console = want_console;
3445 saved_vc_mode = vc->vc_mode;
3446 saved_console_blanked = console_blanked;
3447 vc->vc_mode = KD_TEXT;
3448 console_blanked = 0;
3449 if (vc->vc_sw->con_debug_enter)
3450 ret = vc->vc_sw->con_debug_enter(vc);
3451#ifdef CONFIG_KGDB_KDB
3452 /* Set the initial LINES variable if it is not already set */
3453 if (vc->vc_rows < 999) {
3454 int linecount;
3455 char lns[4];
3456 const char *setargs[3] = {
3457 "set",
3458 "LINES",
3459 lns,
3460 };
3461 if (kdbgetintenv(setargs[0], &linecount)) {
3462 snprintf(lns, 4, "%i", vc->vc_rows);
3463 kdb_set(2, setargs);
3464 }
3465 }
3466#endif /* CONFIG_KGDB_KDB */
3467 return ret;
3468}
3469EXPORT_SYMBOL_GPL(con_debug_enter);
3470
3471/**
3472 * con_debug_leave - restore console state
3473 * @sw: console driver
3474 *
3475 * Restore the console state to what it was before the kernel debugger
3476 * was invoked.
3477 *
3478 * RETURNS:
3479 * Zero on success, nonzero if a failure occurred when trying to restore
3480 * the console.
3481 */
3482int con_debug_leave(void)
3483{
3484 struct vc_data *vc;
3485 int ret = 0;
3486
3487 fg_console = saved_fg_console;
3488 last_console = saved_last_console;
3489 want_console = saved_want_console;
3490 console_blanked = saved_console_blanked;
3491 vc_cons[fg_console].d->vc_mode = saved_vc_mode;
3492
3493 vc = vc_cons[fg_console].d;
3494 if (vc->vc_sw->con_debug_leave)
3495 ret = vc->vc_sw->con_debug_leave(vc);
3496 return ret;
3497}
3498EXPORT_SYMBOL_GPL(con_debug_leave);
3499
3500/**
3501 * register_con_driver - register console driver to console layer
3502 * @csw: console driver
3503 * @first: the first console to take over, minimum value is 0
3504 * @last: the last console to take over, maximum value is MAX_NR_CONSOLES -1
3505 *
3506 * DESCRIPTION: This function registers a console driver which can later
3507 * bind to a range of consoles specified by @first and @last. It will
3508 * also initialize the console driver by calling con_startup().
3509 */
3510int register_con_driver(const struct consw *csw, int first, int last)
3511{
3512 struct module *owner = csw->owner;
3513 struct con_driver *con_driver;
3514 const char *desc;
3515 int i, retval = 0;
3516
3517 if (!try_module_get(owner))
3518 return -ENODEV;
3519
3520 acquire_console_sem();
3521
3522 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3523 con_driver = &registered_con_driver[i];
3524
3525 /* already registered */
3526 if (con_driver->con == csw)
3527 retval = -EINVAL;
3528 }
3529
3530 if (retval)
3531 goto err;
3532
3533 desc = csw->con_startup();
3534
3535 if (!desc)
3536 goto err;
3537
3538 retval = -EINVAL;
3539
3540 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3541 con_driver = &registered_con_driver[i];
3542
3543 if (con_driver->con == NULL) {
3544 con_driver->con = csw;
3545 con_driver->desc = desc;
3546 con_driver->node = i;
3547 con_driver->flag = CON_DRIVER_FLAG_MODULE |
3548 CON_DRIVER_FLAG_INIT;
3549 con_driver->first = first;
3550 con_driver->last = last;
3551 retval = 0;
3552 break;
3553 }
3554 }
3555
3556 if (retval)
3557 goto err;
3558
3559 con_driver->dev = device_create(vtconsole_class, NULL,
3560 MKDEV(0, con_driver->node),
3561 NULL, "vtcon%i",
3562 con_driver->node);
3563
3564 if (IS_ERR(con_driver->dev)) {
3565 printk(KERN_WARNING "Unable to create device for %s; "
3566 "errno = %ld\n", con_driver->desc,
3567 PTR_ERR(con_driver->dev));
3568 con_driver->dev = NULL;
3569 } else {
3570 vtconsole_init_device(con_driver);
3571 }
3572
3573err:
3574 release_console_sem();
3575 module_put(owner);
3576 return retval;
3577}
3578EXPORT_SYMBOL(register_con_driver);
3579
3580/**
3581 * unregister_con_driver - unregister console driver from console layer
3582 * @csw: console driver
3583 *
3584 * DESCRIPTION: All drivers that registers to the console layer must
3585 * call this function upon exit, or if the console driver is in a state
3586 * where it won't be able to handle console services, such as the
3587 * framebuffer console without loaded framebuffer drivers.
3588 *
3589 * The driver must unbind first prior to unregistration.
3590 */
3591int unregister_con_driver(const struct consw *csw)
3592{
3593 int i, retval = -ENODEV;
3594
3595 acquire_console_sem();
3596
3597 /* cannot unregister a bound driver */
3598 if (con_is_bound(csw))
3599 goto err;
3600
3601 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3602 struct con_driver *con_driver = &registered_con_driver[i];
3603
3604 if (con_driver->con == csw &&
3605 con_driver->flag & CON_DRIVER_FLAG_MODULE) {
3606 vtconsole_deinit_device(con_driver);
3607 device_destroy(vtconsole_class,
3608 MKDEV(0, con_driver->node));
3609 con_driver->con = NULL;
3610 con_driver->desc = NULL;
3611 con_driver->dev = NULL;
3612 con_driver->node = 0;
3613 con_driver->flag = 0;
3614 con_driver->first = 0;
3615 con_driver->last = 0;
3616 retval = 0;
3617 break;
3618 }
3619 }
3620err:
3621 release_console_sem();
3622 return retval;
3623}
3624EXPORT_SYMBOL(unregister_con_driver);
3625
3626/*
3627 * If we support more console drivers, this function is used
3628 * when a driver wants to take over some existing consoles
3629 * and become default driver for newly opened ones.
3630 *
3631 * take_over_console is basically a register followed by unbind
3632 */
3633int take_over_console(const struct consw *csw, int first, int last, int deflt)
3634{
3635 int err;
3636
3637 err = register_con_driver(csw, first, last);
3638
3639 if (!err)
3640 bind_con_driver(csw, first, last, deflt);
3641
3642 return err;
3643}
3644
3645/*
3646 * give_up_console is a wrapper to unregister_con_driver. It will only
3647 * work if driver is fully unbound.
3648 */
3649void give_up_console(const struct consw *csw)
3650{
3651 unregister_con_driver(csw);
3652}
3653
3654static int __init vtconsole_class_init(void)
3655{
3656 int i;
3657
3658 vtconsole_class = class_create(THIS_MODULE, "vtconsole");
3659 if (IS_ERR(vtconsole_class)) {
3660 printk(KERN_WARNING "Unable to create vt console class; "
3661 "errno = %ld\n", PTR_ERR(vtconsole_class));
3662 vtconsole_class = NULL;
3663 }
3664
3665 /* Add system drivers to sysfs */
3666 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3667 struct con_driver *con = &registered_con_driver[i];
3668
3669 if (con->con && !con->dev) {
3670 con->dev = device_create(vtconsole_class, NULL,
3671 MKDEV(0, con->node),
3672 NULL, "vtcon%i",
3673 con->node);
3674
3675 if (IS_ERR(con->dev)) {
3676 printk(KERN_WARNING "Unable to create "
3677 "device for %s; errno = %ld\n",
3678 con->desc, PTR_ERR(con->dev));
3679 con->dev = NULL;
3680 } else {
3681 vtconsole_init_device(con);
3682 }
3683 }
3684 }
3685
3686 return 0;
3687}
3688postcore_initcall(vtconsole_class_init);
3689
3690#endif
3691
3692/*
3693 * Screen blanking
3694 */
3695
3696static int set_vesa_blanking(char __user *p)
3697{
3698 unsigned int mode;
3699
3700 if (get_user(mode, p + 1))
3701 return -EFAULT;
3702
3703 vesa_blank_mode = (mode < 4) ? mode : 0;
3704 return 0;
3705}
3706
3707void do_blank_screen(int entering_gfx)
3708{
3709 struct vc_data *vc = vc_cons[fg_console].d;
3710 int i;
3711
3712 WARN_CONSOLE_UNLOCKED();
3713
3714 if (console_blanked) {
3715 if (blank_state == blank_vesa_wait) {
3716 blank_state = blank_off;
3717 vc->vc_sw->con_blank(vc, vesa_blank_mode + 1, 0);
3718 }
3719 return;
3720 }
3721
3722 /* entering graphics mode? */
3723 if (entering_gfx) {
3724 hide_cursor(vc);
3725 save_screen(vc);
3726 vc->vc_sw->con_blank(vc, -1, 1);
3727 console_blanked = fg_console + 1;
3728 blank_state = blank_off;
3729 set_origin(vc);
3730 return;
3731 }
3732
3733 if (blank_state != blank_normal_wait)
3734 return;
3735 blank_state = blank_off;
3736
3737 /* don't blank graphics */
3738 if (vc->vc_mode != KD_TEXT) {
3739 console_blanked = fg_console + 1;
3740 return;
3741 }
3742
3743 hide_cursor(vc);
3744 del_timer_sync(&console_timer);
3745 blank_timer_expired = 0;
3746
3747 save_screen(vc);
3748 /* In case we need to reset origin, blanking hook returns 1 */
3749 i = vc->vc_sw->con_blank(vc, vesa_off_interval ? 1 : (vesa_blank_mode + 1), 0);
3750 console_blanked = fg_console + 1;
3751 if (i)
3752 set_origin(vc);
3753
3754 if (console_blank_hook && console_blank_hook(1))
3755 return;
3756
3757 if (vesa_off_interval && vesa_blank_mode) {
3758 blank_state = blank_vesa_wait;
3759 mod_timer(&console_timer, jiffies + vesa_off_interval);
3760 }
3761 vt_event_post(VT_EVENT_BLANK, vc->vc_num, vc->vc_num);
3762}
3763EXPORT_SYMBOL(do_blank_screen);
3764
3765/*
3766 * Called by timer as well as from vt_console_driver
3767 */
3768void do_unblank_screen(int leaving_gfx)
3769{
3770 struct vc_data *vc;
3771
3772 /* This should now always be called from a "sane" (read: can schedule)
3773 * context for the sake of the low level drivers, except in the special
3774 * case of oops_in_progress
3775 */
3776 if (!oops_in_progress)
3777 might_sleep();
3778
3779 WARN_CONSOLE_UNLOCKED();
3780
3781 ignore_poke = 0;
3782 if (!console_blanked)
3783 return;
3784 if (!vc_cons_allocated(fg_console)) {
3785 /* impossible */
3786 printk("unblank_screen: tty %d not allocated ??\n", fg_console+1);
3787 return;
3788 }
3789 vc = vc_cons[fg_console].d;
3790 /* Try to unblank in oops case too */
3791 if (vc->vc_mode != KD_TEXT && !vt_force_oops_output(vc))
3792 return; /* but leave console_blanked != 0 */
3793
3794 if (blankinterval) {
3795 mod_timer(&console_timer, jiffies + (blankinterval * HZ));
3796 blank_state = blank_normal_wait;
3797 }
3798
3799 console_blanked = 0;
3800 if (vc->vc_sw->con_blank(vc, 0, leaving_gfx) || vt_force_oops_output(vc))
3801 /* Low-level driver cannot restore -> do it ourselves */
3802 update_screen(vc);
3803 if (console_blank_hook)
3804 console_blank_hook(0);
3805 set_palette(vc);
3806 set_cursor(vc);
3807 vt_event_post(VT_EVENT_UNBLANK, vc->vc_num, vc->vc_num);
3808}
3809EXPORT_SYMBOL(do_unblank_screen);
3810
3811/*
3812 * This is called by the outside world to cause a forced unblank, mostly for
3813 * oopses. Currently, I just call do_unblank_screen(0), but we could eventually
3814 * call it with 1 as an argument and so force a mode restore... that may kill
3815 * X or at least garbage the screen but would also make the Oops visible...
3816 */
3817void unblank_screen(void)
3818{
3819 do_unblank_screen(0);
3820}
3821
3822/*
3823 * We defer the timer blanking to work queue so it can take the console mutex
3824 * (console operations can still happen at irq time, but only from printk which
3825 * has the console mutex. Not perfect yet, but better than no locking
3826 */
3827static void blank_screen_t(unsigned long dummy)
3828{
3829 if (unlikely(!keventd_up())) {
3830 mod_timer(&console_timer, jiffies + (blankinterval * HZ));
3831 return;
3832 }
3833 blank_timer_expired = 1;
3834 schedule_work(&console_work);
3835}
3836
3837void poke_blanked_console(void)
3838{
3839 WARN_CONSOLE_UNLOCKED();
3840
3841 /* Add this so we quickly catch whoever might call us in a non
3842 * safe context. Nowadays, unblank_screen() isn't to be called in
3843 * atomic contexts and is allowed to schedule (with the special case
3844 * of oops_in_progress, but that isn't of any concern for this
3845 * function. --BenH.
3846 */
3847 might_sleep();
3848
3849 /* This isn't perfectly race free, but a race here would be mostly harmless,
3850 * at worse, we'll do a spurrious blank and it's unlikely
3851 */
3852 del_timer(&console_timer);
3853 blank_timer_expired = 0;
3854
3855 if (ignore_poke || !vc_cons[fg_console].d || vc_cons[fg_console].d->vc_mode == KD_GRAPHICS)
3856 return;
3857 if (console_blanked)
3858 unblank_screen();
3859 else if (blankinterval) {
3860 mod_timer(&console_timer, jiffies + (blankinterval * HZ));
3861 blank_state = blank_normal_wait;
3862 }
3863}
3864
3865/*
3866 * Palettes
3867 */
3868
3869static void set_palette(struct vc_data *vc)
3870{
3871 WARN_CONSOLE_UNLOCKED();
3872
3873 if (vc->vc_mode != KD_GRAPHICS)
3874 vc->vc_sw->con_set_palette(vc, color_table);
3875}
3876
3877static int set_get_cmap(unsigned char __user *arg, int set)
3878{
3879 int i, j, k;
3880
3881 WARN_CONSOLE_UNLOCKED();
3882
3883 for (i = 0; i < 16; i++)
3884 if (set) {
3885 get_user(default_red[i], arg++);
3886 get_user(default_grn[i], arg++);
3887 get_user(default_blu[i], arg++);
3888 } else {
3889 put_user(default_red[i], arg++);
3890 put_user(default_grn[i], arg++);
3891 put_user(default_blu[i], arg++);
3892 }
3893 if (set) {
3894 for (i = 0; i < MAX_NR_CONSOLES; i++)
3895 if (vc_cons_allocated(i)) {
3896 for (j = k = 0; j < 16; j++) {
3897 vc_cons[i].d->vc_palette[k++] = default_red[j];
3898 vc_cons[i].d->vc_palette[k++] = default_grn[j];
3899 vc_cons[i].d->vc_palette[k++] = default_blu[j];
3900 }
3901 set_palette(vc_cons[i].d);
3902 }
3903 }
3904 return 0;
3905}
3906
3907/*
3908 * Load palette into the DAC registers. arg points to a colour
3909 * map, 3 bytes per colour, 16 colours, range from 0 to 255.
3910 */
3911
3912int con_set_cmap(unsigned char __user *arg)
3913{
3914 int rc;
3915
3916 acquire_console_sem();
3917 rc = set_get_cmap (arg,1);
3918 release_console_sem();
3919
3920 return rc;
3921}
3922
3923int con_get_cmap(unsigned char __user *arg)
3924{
3925 int rc;
3926
3927 acquire_console_sem();
3928 rc = set_get_cmap (arg,0);
3929 release_console_sem();
3930
3931 return rc;
3932}
3933
3934void reset_palette(struct vc_data *vc)
3935{
3936 int j, k;
3937 for (j=k=0; j<16; j++) {
3938 vc->vc_palette[k++] = default_red[j];
3939 vc->vc_palette[k++] = default_grn[j];
3940 vc->vc_palette[k++] = default_blu[j];
3941 }
3942 set_palette(vc);
3943}
3944
3945/*
3946 * Font switching
3947 *
3948 * Currently we only support fonts up to 32 pixels wide, at a maximum height
3949 * of 32 pixels. Userspace fontdata is stored with 32 bytes (shorts/ints,
3950 * depending on width) reserved for each character which is kinda wasty, but
3951 * this is done in order to maintain compatibility with the EGA/VGA fonts. It
3952 * is upto the actual low-level console-driver convert data into its favorite
3953 * format (maybe we should add a `fontoffset' field to the `display'
3954 * structure so we won't have to convert the fontdata all the time.
3955 * /Jes
3956 */
3957
3958#define max_font_size 65536
3959
3960static int con_font_get(struct vc_data *vc, struct console_font_op *op)
3961{
3962 struct console_font font;
3963 int rc = -EINVAL;
3964 int c;
3965
3966 if (vc->vc_mode != KD_TEXT)
3967 return -EINVAL;
3968
3969 if (op->data) {
3970 font.data = kmalloc(max_font_size, GFP_KERNEL);
3971 if (!font.data)
3972 return -ENOMEM;
3973 } else
3974 font.data = NULL;
3975
3976 acquire_console_sem();
3977 if (vc->vc_sw->con_font_get)
3978 rc = vc->vc_sw->con_font_get(vc, &font);
3979 else
3980 rc = -ENOSYS;
3981 release_console_sem();
3982
3983 if (rc)
3984 goto out;
3985
3986 c = (font.width+7)/8 * 32 * font.charcount;
3987
3988 if (op->data && font.charcount > op->charcount)
3989 rc = -ENOSPC;
3990 if (!(op->flags & KD_FONT_FLAG_OLD)) {
3991 if (font.width > op->width || font.height > op->height)
3992 rc = -ENOSPC;
3993 } else {
3994 if (font.width != 8)
3995 rc = -EIO;
3996 else if ((op->height && font.height > op->height) ||
3997 font.height > 32)
3998 rc = -ENOSPC;
3999 }
4000 if (rc)
4001 goto out;
4002
4003 op->height = font.height;
4004 op->width = font.width;
4005 op->charcount = font.charcount;
4006
4007 if (op->data && copy_to_user(op->data, font.data, c))
4008 rc = -EFAULT;
4009
4010out:
4011 kfree(font.data);
4012 return rc;
4013}
4014
4015static int con_font_set(struct vc_data *vc, struct console_font_op *op)
4016{
4017 struct console_font font;
4018 int rc = -EINVAL;
4019 int size;
4020
4021 if (vc->vc_mode != KD_TEXT)
4022 return -EINVAL;
4023 if (!op->data)
4024 return -EINVAL;
4025 if (op->charcount > 512)
4026 return -EINVAL;
4027 if (!op->height) { /* Need to guess font height [compat] */
4028 int h, i;
4029 u8 __user *charmap = op->data;
4030 u8 tmp;
4031
4032 /* If from KDFONTOP ioctl, don't allow things which can be done in userland,
4033 so that we can get rid of this soon */
4034 if (!(op->flags & KD_FONT_FLAG_OLD))
4035 return -EINVAL;
4036 for (h = 32; h > 0; h--)
4037 for (i = 0; i < op->charcount; i++) {
4038 if (get_user(tmp, &charmap[32*i+h-1]))
4039 return -EFAULT;
4040 if (tmp)
4041 goto nonzero;
4042 }
4043 return -EINVAL;
4044 nonzero:
4045 op->height = h;
4046 }
4047 if (op->width <= 0 || op->width > 32 || op->height > 32)
4048 return -EINVAL;
4049 size = (op->width+7)/8 * 32 * op->charcount;
4050 if (size > max_font_size)
4051 return -ENOSPC;
4052 font.charcount = op->charcount;
4053 font.height = op->height;
4054 font.width = op->width;
4055 font.data = memdup_user(op->data, size);
4056 if (IS_ERR(font.data))
4057 return PTR_ERR(font.data);
4058 acquire_console_sem();
4059 if (vc->vc_sw->con_font_set)
4060 rc = vc->vc_sw->con_font_set(vc, &font, op->flags);
4061 else
4062 rc = -ENOSYS;
4063 release_console_sem();
4064 kfree(font.data);
4065 return rc;
4066}
4067
4068static int con_font_default(struct vc_data *vc, struct console_font_op *op)
4069{
4070 struct console_font font = {.width = op->width, .height = op->height};
4071 char name[MAX_FONT_NAME];
4072 char *s = name;
4073 int rc;
4074
4075 if (vc->vc_mode != KD_TEXT)
4076 return -EINVAL;
4077
4078 if (!op->data)
4079 s = NULL;
4080 else if (strncpy_from_user(name, op->data, MAX_FONT_NAME - 1) < 0)
4081 return -EFAULT;
4082 else
4083 name[MAX_FONT_NAME - 1] = 0;
4084
4085 acquire_console_sem();
4086 if (vc->vc_sw->con_font_default)
4087 rc = vc->vc_sw->con_font_default(vc, &font, s);
4088 else
4089 rc = -ENOSYS;
4090 release_console_sem();
4091 if (!rc) {
4092 op->width = font.width;
4093 op->height = font.height;
4094 }
4095 return rc;
4096}
4097
4098static int con_font_copy(struct vc_data *vc, struct console_font_op *op)
4099{
4100 int con = op->height;
4101 int rc;
4102
4103 if (vc->vc_mode != KD_TEXT)
4104 return -EINVAL;
4105
4106 acquire_console_sem();
4107 if (!vc->vc_sw->con_font_copy)
4108 rc = -ENOSYS;
4109 else if (con < 0 || !vc_cons_allocated(con))
4110 rc = -ENOTTY;
4111 else if (con == vc->vc_num) /* nothing to do */
4112 rc = 0;
4113 else
4114 rc = vc->vc_sw->con_font_copy(vc, con);
4115 release_console_sem();
4116 return rc;
4117}
4118
4119int con_font_op(struct vc_data *vc, struct console_font_op *op)
4120{
4121 switch (op->op) {
4122 case KD_FONT_OP_SET:
4123 return con_font_set(vc, op);
4124 case KD_FONT_OP_GET:
4125 return con_font_get(vc, op);
4126 case KD_FONT_OP_SET_DEFAULT:
4127 return con_font_default(vc, op);
4128 case KD_FONT_OP_COPY:
4129 return con_font_copy(vc, op);
4130 }
4131 return -ENOSYS;
4132}
4133
4134/*
4135 * Interface exported to selection and vcs.
4136 */
4137
4138/* used by selection */
4139u16 screen_glyph(struct vc_data *vc, int offset)
4140{
4141 u16 w = scr_readw(screenpos(vc, offset, 1));
4142 u16 c = w & 0xff;
4143
4144 if (w & vc->vc_hi_font_mask)
4145 c |= 0x100;
4146 return c;
4147}
4148EXPORT_SYMBOL_GPL(screen_glyph);
4149
4150/* used by vcs - note the word offset */
4151unsigned short *screen_pos(struct vc_data *vc, int w_offset, int viewed)
4152{
4153 return screenpos(vc, 2 * w_offset, viewed);
4154}
4155
4156void getconsxy(struct vc_data *vc, unsigned char *p)
4157{
4158 p[0] = vc->vc_x;
4159 p[1] = vc->vc_y;
4160}
4161
4162void putconsxy(struct vc_data *vc, unsigned char *p)
4163{
4164 hide_cursor(vc);
4165 gotoxy(vc, p[0], p[1]);
4166 set_cursor(vc);
4167}
4168
4169u16 vcs_scr_readw(struct vc_data *vc, const u16 *org)
4170{
4171 if ((unsigned long)org == vc->vc_pos && softcursor_original != -1)
4172 return softcursor_original;
4173 return scr_readw(org);
4174}
4175
4176void vcs_scr_writew(struct vc_data *vc, u16 val, u16 *org)
4177{
4178 scr_writew(val, org);
4179 if ((unsigned long)org == vc->vc_pos) {
4180 softcursor_original = -1;
4181 add_softcursor(vc);
4182 }
4183}
4184
4185/*
4186 * Visible symbols for modules
4187 */
4188
4189EXPORT_SYMBOL(color_table);
4190EXPORT_SYMBOL(default_red);
4191EXPORT_SYMBOL(default_grn);
4192EXPORT_SYMBOL(default_blu);
4193EXPORT_SYMBOL(update_region);
4194EXPORT_SYMBOL(redraw_screen);
4195EXPORT_SYMBOL(vc_resize);
4196EXPORT_SYMBOL(fg_console);
4197EXPORT_SYMBOL(console_blank_hook);
4198EXPORT_SYMBOL(console_blanked);
4199EXPORT_SYMBOL(vc_cons);
4200EXPORT_SYMBOL(global_cursor_default);
4201#ifndef VT_SINGLE_DRIVER
4202EXPORT_SYMBOL(take_over_console);
4203EXPORT_SYMBOL(give_up_console);
4204#endif
diff --git a/drivers/char/vt_ioctl.c b/drivers/char/vt_ioctl.c
deleted file mode 100644
index 38df8c19e74c..000000000000
--- a/drivers/char/vt_ioctl.c
+++ /dev/null
@@ -1,1787 +0,0 @@
1/*
2 * linux/drivers/char/vt_ioctl.c
3 *
4 * Copyright (C) 1992 obz under the linux copyright
5 *
6 * Dynamic diacritical handling - aeb@cwi.nl - Dec 1993
7 * Dynamic keymap and string allocation - aeb@cwi.nl - May 1994
8 * Restrict VT switching via ioctl() - grif@cs.ucr.edu - Dec 1995
9 * Some code moved for less code duplication - Andi Kleen - Mar 1997
10 * Check put/get_user, cleanups - acme@conectiva.com.br - Jun 2001
11 */
12
13#include <linux/types.h>
14#include <linux/errno.h>
15#include <linux/sched.h>
16#include <linux/tty.h>
17#include <linux/timer.h>
18#include <linux/kernel.h>
19#include <linux/compat.h>
20#include <linux/module.h>
21#include <linux/kd.h>
22#include <linux/vt.h>
23#include <linux/string.h>
24#include <linux/slab.h>
25#include <linux/major.h>
26#include <linux/fs.h>
27#include <linux/console.h>
28#include <linux/consolemap.h>
29#include <linux/signal.h>
30#include <linux/smp_lock.h>
31#include <linux/timex.h>
32
33#include <asm/io.h>
34#include <asm/uaccess.h>
35
36#include <linux/kbd_kern.h>
37#include <linux/vt_kern.h>
38#include <linux/kbd_diacr.h>
39#include <linux/selection.h>
40
41char vt_dont_switch;
42extern struct tty_driver *console_driver;
43
44#define VT_IS_IN_USE(i) (console_driver->ttys[i] && console_driver->ttys[i]->count)
45#define VT_BUSY(i) (VT_IS_IN_USE(i) || i == fg_console || vc_cons[i].d == sel_cons)
46
47/*
48 * Console (vt and kd) routines, as defined by USL SVR4 manual, and by
49 * experimentation and study of X386 SYSV handling.
50 *
51 * One point of difference: SYSV vt's are /dev/vtX, which X >= 0, and
52 * /dev/console is a separate ttyp. Under Linux, /dev/tty0 is /dev/console,
53 * and the vc start at /dev/ttyX, X >= 1. We maintain that here, so we will
54 * always treat our set of vt as numbered 1..MAX_NR_CONSOLES (corresponding to
55 * ttys 0..MAX_NR_CONSOLES-1). Explicitly naming VT 0 is illegal, but using
56 * /dev/tty0 (fg_console) as a target is legal, since an implicit aliasing
57 * to the current console is done by the main ioctl code.
58 */
59
60#ifdef CONFIG_X86
61#include <linux/syscalls.h>
62#endif
63
64static void complete_change_console(struct vc_data *vc);
65
66/*
67 * User space VT_EVENT handlers
68 */
69
70struct vt_event_wait {
71 struct list_head list;
72 struct vt_event event;
73 int done;
74};
75
76static LIST_HEAD(vt_events);
77static DEFINE_SPINLOCK(vt_event_lock);
78static DECLARE_WAIT_QUEUE_HEAD(vt_event_waitqueue);
79
80/**
81 * vt_event_post
82 * @event: the event that occurred
83 * @old: old console
84 * @new: new console
85 *
86 * Post an VT event to interested VT handlers
87 */
88
89void vt_event_post(unsigned int event, unsigned int old, unsigned int new)
90{
91 struct list_head *pos, *head;
92 unsigned long flags;
93 int wake = 0;
94
95 spin_lock_irqsave(&vt_event_lock, flags);
96 head = &vt_events;
97
98 list_for_each(pos, head) {
99 struct vt_event_wait *ve = list_entry(pos,
100 struct vt_event_wait, list);
101 if (!(ve->event.event & event))
102 continue;
103 ve->event.event = event;
104 /* kernel view is consoles 0..n-1, user space view is
105 console 1..n with 0 meaning current, so we must bias */
106 ve->event.oldev = old + 1;
107 ve->event.newev = new + 1;
108 wake = 1;
109 ve->done = 1;
110 }
111 spin_unlock_irqrestore(&vt_event_lock, flags);
112 if (wake)
113 wake_up_interruptible(&vt_event_waitqueue);
114}
115
116/**
117 * vt_event_wait - wait for an event
118 * @vw: our event
119 *
120 * Waits for an event to occur which completes our vt_event_wait
121 * structure. On return the structure has wv->done set to 1 for success
122 * or 0 if some event such as a signal ended the wait.
123 */
124
125static void vt_event_wait(struct vt_event_wait *vw)
126{
127 unsigned long flags;
128 /* Prepare the event */
129 INIT_LIST_HEAD(&vw->list);
130 vw->done = 0;
131 /* Queue our event */
132 spin_lock_irqsave(&vt_event_lock, flags);
133 list_add(&vw->list, &vt_events);
134 spin_unlock_irqrestore(&vt_event_lock, flags);
135 /* Wait for it to pass */
136 wait_event_interruptible_tty(vt_event_waitqueue, vw->done);
137 /* Dequeue it */
138 spin_lock_irqsave(&vt_event_lock, flags);
139 list_del(&vw->list);
140 spin_unlock_irqrestore(&vt_event_lock, flags);
141}
142
143/**
144 * vt_event_wait_ioctl - event ioctl handler
145 * @arg: argument to ioctl
146 *
147 * Implement the VT_WAITEVENT ioctl using the VT event interface
148 */
149
150static int vt_event_wait_ioctl(struct vt_event __user *event)
151{
152 struct vt_event_wait vw;
153
154 if (copy_from_user(&vw.event, event, sizeof(struct vt_event)))
155 return -EFAULT;
156 /* Highest supported event for now */
157 if (vw.event.event & ~VT_MAX_EVENT)
158 return -EINVAL;
159
160 vt_event_wait(&vw);
161 /* If it occurred report it */
162 if (vw.done) {
163 if (copy_to_user(event, &vw.event, sizeof(struct vt_event)))
164 return -EFAULT;
165 return 0;
166 }
167 return -EINTR;
168}
169
170/**
171 * vt_waitactive - active console wait
172 * @event: event code
173 * @n: new console
174 *
175 * Helper for event waits. Used to implement the legacy
176 * event waiting ioctls in terms of events
177 */
178
179int vt_waitactive(int n)
180{
181 struct vt_event_wait vw;
182 do {
183 if (n == fg_console + 1)
184 break;
185 vw.event.event = VT_EVENT_SWITCH;
186 vt_event_wait(&vw);
187 if (vw.done == 0)
188 return -EINTR;
189 } while (vw.event.newev != n);
190 return 0;
191}
192
193/*
194 * these are the valid i/o ports we're allowed to change. they map all the
195 * video ports
196 */
197#define GPFIRST 0x3b4
198#define GPLAST 0x3df
199#define GPNUM (GPLAST - GPFIRST + 1)
200
201#define i (tmp.kb_index)
202#define s (tmp.kb_table)
203#define v (tmp.kb_value)
204static inline int
205do_kdsk_ioctl(int cmd, struct kbentry __user *user_kbe, int perm, struct kbd_struct *kbd)
206{
207 struct kbentry tmp;
208 ushort *key_map, val, ov;
209
210 if (copy_from_user(&tmp, user_kbe, sizeof(struct kbentry)))
211 return -EFAULT;
212
213 if (!capable(CAP_SYS_TTY_CONFIG))
214 perm = 0;
215
216 switch (cmd) {
217 case KDGKBENT:
218 key_map = key_maps[s];
219 if (key_map) {
220 val = U(key_map[i]);
221 if (kbd->kbdmode != VC_UNICODE && KTYP(val) >= NR_TYPES)
222 val = K_HOLE;
223 } else
224 val = (i ? K_HOLE : K_NOSUCHMAP);
225 return put_user(val, &user_kbe->kb_value);
226 case KDSKBENT:
227 if (!perm)
228 return -EPERM;
229 if (!i && v == K_NOSUCHMAP) {
230 /* deallocate map */
231 key_map = key_maps[s];
232 if (s && key_map) {
233 key_maps[s] = NULL;
234 if (key_map[0] == U(K_ALLOCATED)) {
235 kfree(key_map);
236 keymap_count--;
237 }
238 }
239 break;
240 }
241
242 if (KTYP(v) < NR_TYPES) {
243 if (KVAL(v) > max_vals[KTYP(v)])
244 return -EINVAL;
245 } else
246 if (kbd->kbdmode != VC_UNICODE)
247 return -EINVAL;
248
249 /* ++Geert: non-PC keyboards may generate keycode zero */
250#if !defined(__mc68000__) && !defined(__powerpc__)
251 /* assignment to entry 0 only tests validity of args */
252 if (!i)
253 break;
254#endif
255
256 if (!(key_map = key_maps[s])) {
257 int j;
258
259 if (keymap_count >= MAX_NR_OF_USER_KEYMAPS &&
260 !capable(CAP_SYS_RESOURCE))
261 return -EPERM;
262
263 key_map = kmalloc(sizeof(plain_map),
264 GFP_KERNEL);
265 if (!key_map)
266 return -ENOMEM;
267 key_maps[s] = key_map;
268 key_map[0] = U(K_ALLOCATED);
269 for (j = 1; j < NR_KEYS; j++)
270 key_map[j] = U(K_HOLE);
271 keymap_count++;
272 }
273 ov = U(key_map[i]);
274 if (v == ov)
275 break; /* nothing to do */
276 /*
277 * Attention Key.
278 */
279 if (((ov == K_SAK) || (v == K_SAK)) && !capable(CAP_SYS_ADMIN))
280 return -EPERM;
281 key_map[i] = U(v);
282 if (!s && (KTYP(ov) == KT_SHIFT || KTYP(v) == KT_SHIFT))
283 compute_shiftstate();
284 break;
285 }
286 return 0;
287}
288#undef i
289#undef s
290#undef v
291
292static inline int
293do_kbkeycode_ioctl(int cmd, struct kbkeycode __user *user_kbkc, int perm)
294{
295 struct kbkeycode tmp;
296 int kc = 0;
297
298 if (copy_from_user(&tmp, user_kbkc, sizeof(struct kbkeycode)))
299 return -EFAULT;
300 switch (cmd) {
301 case KDGETKEYCODE:
302 kc = getkeycode(tmp.scancode);
303 if (kc >= 0)
304 kc = put_user(kc, &user_kbkc->keycode);
305 break;
306 case KDSETKEYCODE:
307 if (!perm)
308 return -EPERM;
309 kc = setkeycode(tmp.scancode, tmp.keycode);
310 break;
311 }
312 return kc;
313}
314
315static inline int
316do_kdgkb_ioctl(int cmd, struct kbsentry __user *user_kdgkb, int perm)
317{
318 struct kbsentry *kbs;
319 char *p;
320 u_char *q;
321 u_char __user *up;
322 int sz;
323 int delta;
324 char *first_free, *fj, *fnw;
325 int i, j, k;
326 int ret;
327
328 if (!capable(CAP_SYS_TTY_CONFIG))
329 perm = 0;
330
331 kbs = kmalloc(sizeof(*kbs), GFP_KERNEL);
332 if (!kbs) {
333 ret = -ENOMEM;
334 goto reterr;
335 }
336
337 /* we mostly copy too much here (512bytes), but who cares ;) */
338 if (copy_from_user(kbs, user_kdgkb, sizeof(struct kbsentry))) {
339 ret = -EFAULT;
340 goto reterr;
341 }
342 kbs->kb_string[sizeof(kbs->kb_string)-1] = '\0';
343 i = kbs->kb_func;
344
345 switch (cmd) {
346 case KDGKBSENT:
347 sz = sizeof(kbs->kb_string) - 1; /* sz should have been
348 a struct member */
349 up = user_kdgkb->kb_string;
350 p = func_table[i];
351 if(p)
352 for ( ; *p && sz; p++, sz--)
353 if (put_user(*p, up++)) {
354 ret = -EFAULT;
355 goto reterr;
356 }
357 if (put_user('\0', up)) {
358 ret = -EFAULT;
359 goto reterr;
360 }
361 kfree(kbs);
362 return ((p && *p) ? -EOVERFLOW : 0);
363 case KDSKBSENT:
364 if (!perm) {
365 ret = -EPERM;
366 goto reterr;
367 }
368
369 q = func_table[i];
370 first_free = funcbufptr + (funcbufsize - funcbufleft);
371 for (j = i+1; j < MAX_NR_FUNC && !func_table[j]; j++)
372 ;
373 if (j < MAX_NR_FUNC)
374 fj = func_table[j];
375 else
376 fj = first_free;
377
378 delta = (q ? -strlen(q) : 1) + strlen(kbs->kb_string);
379 if (delta <= funcbufleft) { /* it fits in current buf */
380 if (j < MAX_NR_FUNC) {
381 memmove(fj + delta, fj, first_free - fj);
382 for (k = j; k < MAX_NR_FUNC; k++)
383 if (func_table[k])
384 func_table[k] += delta;
385 }
386 if (!q)
387 func_table[i] = fj;
388 funcbufleft -= delta;
389 } else { /* allocate a larger buffer */
390 sz = 256;
391 while (sz < funcbufsize - funcbufleft + delta)
392 sz <<= 1;
393 fnw = kmalloc(sz, GFP_KERNEL);
394 if(!fnw) {
395 ret = -ENOMEM;
396 goto reterr;
397 }
398
399 if (!q)
400 func_table[i] = fj;
401 if (fj > funcbufptr)
402 memmove(fnw, funcbufptr, fj - funcbufptr);
403 for (k = 0; k < j; k++)
404 if (func_table[k])
405 func_table[k] = fnw + (func_table[k] - funcbufptr);
406
407 if (first_free > fj) {
408 memmove(fnw + (fj - funcbufptr) + delta, fj, first_free - fj);
409 for (k = j; k < MAX_NR_FUNC; k++)
410 if (func_table[k])
411 func_table[k] = fnw + (func_table[k] - funcbufptr) + delta;
412 }
413 if (funcbufptr != func_buf)
414 kfree(funcbufptr);
415 funcbufptr = fnw;
416 funcbufleft = funcbufleft - delta + sz - funcbufsize;
417 funcbufsize = sz;
418 }
419 strcpy(func_table[i], kbs->kb_string);
420 break;
421 }
422 ret = 0;
423reterr:
424 kfree(kbs);
425 return ret;
426}
427
428static inline int
429do_fontx_ioctl(int cmd, struct consolefontdesc __user *user_cfd, int perm, struct console_font_op *op)
430{
431 struct consolefontdesc cfdarg;
432 int i;
433
434 if (copy_from_user(&cfdarg, user_cfd, sizeof(struct consolefontdesc)))
435 return -EFAULT;
436
437 switch (cmd) {
438 case PIO_FONTX:
439 if (!perm)
440 return -EPERM;
441 op->op = KD_FONT_OP_SET;
442 op->flags = KD_FONT_FLAG_OLD;
443 op->width = 8;
444 op->height = cfdarg.charheight;
445 op->charcount = cfdarg.charcount;
446 op->data = cfdarg.chardata;
447 return con_font_op(vc_cons[fg_console].d, op);
448 case GIO_FONTX: {
449 op->op = KD_FONT_OP_GET;
450 op->flags = KD_FONT_FLAG_OLD;
451 op->width = 8;
452 op->height = cfdarg.charheight;
453 op->charcount = cfdarg.charcount;
454 op->data = cfdarg.chardata;
455 i = con_font_op(vc_cons[fg_console].d, op);
456 if (i)
457 return i;
458 cfdarg.charheight = op->height;
459 cfdarg.charcount = op->charcount;
460 if (copy_to_user(user_cfd, &cfdarg, sizeof(struct consolefontdesc)))
461 return -EFAULT;
462 return 0;
463 }
464 }
465 return -EINVAL;
466}
467
468static inline int
469do_unimap_ioctl(int cmd, struct unimapdesc __user *user_ud, int perm, struct vc_data *vc)
470{
471 struct unimapdesc tmp;
472
473 if (copy_from_user(&tmp, user_ud, sizeof tmp))
474 return -EFAULT;
475 if (tmp.entries)
476 if (!access_ok(VERIFY_WRITE, tmp.entries,
477 tmp.entry_ct*sizeof(struct unipair)))
478 return -EFAULT;
479 switch (cmd) {
480 case PIO_UNIMAP:
481 if (!perm)
482 return -EPERM;
483 return con_set_unimap(vc, tmp.entry_ct, tmp.entries);
484 case GIO_UNIMAP:
485 if (!perm && fg_console != vc->vc_num)
486 return -EPERM;
487 return con_get_unimap(vc, tmp.entry_ct, &(user_ud->entry_ct), tmp.entries);
488 }
489 return 0;
490}
491
492
493
494/*
495 * We handle the console-specific ioctl's here. We allow the
496 * capability to modify any console, not just the fg_console.
497 */
498int vt_ioctl(struct tty_struct *tty, struct file * file,
499 unsigned int cmd, unsigned long arg)
500{
501 struct vc_data *vc = tty->driver_data;
502 struct console_font_op op; /* used in multiple places here */
503 struct kbd_struct * kbd;
504 unsigned int console;
505 unsigned char ucval;
506 void __user *up = (void __user *)arg;
507 int i, perm;
508 int ret = 0;
509
510 console = vc->vc_num;
511
512 tty_lock();
513
514 if (!vc_cons_allocated(console)) { /* impossible? */
515 ret = -ENOIOCTLCMD;
516 goto out;
517 }
518
519
520 /*
521 * To have permissions to do most of the vt ioctls, we either have
522 * to be the owner of the tty, or have CAP_SYS_TTY_CONFIG.
523 */
524 perm = 0;
525 if (current->signal->tty == tty || capable(CAP_SYS_TTY_CONFIG))
526 perm = 1;
527
528 kbd = kbd_table + console;
529 switch (cmd) {
530 case TIOCLINUX:
531 ret = tioclinux(tty, arg);
532 break;
533 case KIOCSOUND:
534 if (!perm)
535 goto eperm;
536 /*
537 * The use of PIT_TICK_RATE is historic, it used to be
538 * the platform-dependent CLOCK_TICK_RATE between 2.6.12
539 * and 2.6.36, which was a minor but unfortunate ABI
540 * change.
541 */
542 if (arg)
543 arg = PIT_TICK_RATE / arg;
544 kd_mksound(arg, 0);
545 break;
546
547 case KDMKTONE:
548 if (!perm)
549 goto eperm;
550 {
551 unsigned int ticks, count;
552
553 /*
554 * Generate the tone for the appropriate number of ticks.
555 * If the time is zero, turn off sound ourselves.
556 */
557 ticks = HZ * ((arg >> 16) & 0xffff) / 1000;
558 count = ticks ? (arg & 0xffff) : 0;
559 if (count)
560 count = PIT_TICK_RATE / count;
561 kd_mksound(count, ticks);
562 break;
563 }
564
565 case KDGKBTYPE:
566 /*
567 * this is naive.
568 */
569 ucval = KB_101;
570 goto setchar;
571
572 /*
573 * These cannot be implemented on any machine that implements
574 * ioperm() in user level (such as Alpha PCs) or not at all.
575 *
576 * XXX: you should never use these, just call ioperm directly..
577 */
578#ifdef CONFIG_X86
579 case KDADDIO:
580 case KDDELIO:
581 /*
582 * KDADDIO and KDDELIO may be able to add ports beyond what
583 * we reject here, but to be safe...
584 */
585 if (arg < GPFIRST || arg > GPLAST) {
586 ret = -EINVAL;
587 break;
588 }
589 ret = sys_ioperm(arg, 1, (cmd == KDADDIO)) ? -ENXIO : 0;
590 break;
591
592 case KDENABIO:
593 case KDDISABIO:
594 ret = sys_ioperm(GPFIRST, GPNUM,
595 (cmd == KDENABIO)) ? -ENXIO : 0;
596 break;
597#endif
598
599 /* Linux m68k/i386 interface for setting the keyboard delay/repeat rate */
600
601 case KDKBDREP:
602 {
603 struct kbd_repeat kbrep;
604
605 if (!capable(CAP_SYS_TTY_CONFIG))
606 goto eperm;
607
608 if (copy_from_user(&kbrep, up, sizeof(struct kbd_repeat))) {
609 ret = -EFAULT;
610 break;
611 }
612 ret = kbd_rate(&kbrep);
613 if (ret)
614 break;
615 if (copy_to_user(up, &kbrep, sizeof(struct kbd_repeat)))
616 ret = -EFAULT;
617 break;
618 }
619
620 case KDSETMODE:
621 /*
622 * currently, setting the mode from KD_TEXT to KD_GRAPHICS
623 * doesn't do a whole lot. i'm not sure if it should do any
624 * restoration of modes or what...
625 *
626 * XXX It should at least call into the driver, fbdev's definitely
627 * need to restore their engine state. --BenH
628 */
629 if (!perm)
630 goto eperm;
631 switch (arg) {
632 case KD_GRAPHICS:
633 break;
634 case KD_TEXT0:
635 case KD_TEXT1:
636 arg = KD_TEXT;
637 case KD_TEXT:
638 break;
639 default:
640 ret = -EINVAL;
641 goto out;
642 }
643 if (vc->vc_mode == (unsigned char) arg)
644 break;
645 vc->vc_mode = (unsigned char) arg;
646 if (console != fg_console)
647 break;
648 /*
649 * explicitly blank/unblank the screen if switching modes
650 */
651 acquire_console_sem();
652 if (arg == KD_TEXT)
653 do_unblank_screen(1);
654 else
655 do_blank_screen(1);
656 release_console_sem();
657 break;
658
659 case KDGETMODE:
660 ucval = vc->vc_mode;
661 goto setint;
662
663 case KDMAPDISP:
664 case KDUNMAPDISP:
665 /*
666 * these work like a combination of mmap and KDENABIO.
667 * this could be easily finished.
668 */
669 ret = -EINVAL;
670 break;
671
672 case KDSKBMODE:
673 if (!perm)
674 goto eperm;
675 switch(arg) {
676 case K_RAW:
677 kbd->kbdmode = VC_RAW;
678 break;
679 case K_MEDIUMRAW:
680 kbd->kbdmode = VC_MEDIUMRAW;
681 break;
682 case K_XLATE:
683 kbd->kbdmode = VC_XLATE;
684 compute_shiftstate();
685 break;
686 case K_UNICODE:
687 kbd->kbdmode = VC_UNICODE;
688 compute_shiftstate();
689 break;
690 default:
691 ret = -EINVAL;
692 goto out;
693 }
694 tty_ldisc_flush(tty);
695 break;
696
697 case KDGKBMODE:
698 ucval = ((kbd->kbdmode == VC_RAW) ? K_RAW :
699 (kbd->kbdmode == VC_MEDIUMRAW) ? K_MEDIUMRAW :
700 (kbd->kbdmode == VC_UNICODE) ? K_UNICODE :
701 K_XLATE);
702 goto setint;
703
704 /* this could be folded into KDSKBMODE, but for compatibility
705 reasons it is not so easy to fold KDGKBMETA into KDGKBMODE */
706 case KDSKBMETA:
707 switch(arg) {
708 case K_METABIT:
709 clr_vc_kbd_mode(kbd, VC_META);
710 break;
711 case K_ESCPREFIX:
712 set_vc_kbd_mode(kbd, VC_META);
713 break;
714 default:
715 ret = -EINVAL;
716 }
717 break;
718
719 case KDGKBMETA:
720 ucval = (vc_kbd_mode(kbd, VC_META) ? K_ESCPREFIX : K_METABIT);
721 setint:
722 ret = put_user(ucval, (int __user *)arg);
723 break;
724
725 case KDGETKEYCODE:
726 case KDSETKEYCODE:
727 if(!capable(CAP_SYS_TTY_CONFIG))
728 perm = 0;
729 ret = do_kbkeycode_ioctl(cmd, up, perm);
730 break;
731
732 case KDGKBENT:
733 case KDSKBENT:
734 ret = do_kdsk_ioctl(cmd, up, perm, kbd);
735 break;
736
737 case KDGKBSENT:
738 case KDSKBSENT:
739 ret = do_kdgkb_ioctl(cmd, up, perm);
740 break;
741
742 case KDGKBDIACR:
743 {
744 struct kbdiacrs __user *a = up;
745 struct kbdiacr diacr;
746 int i;
747
748 if (put_user(accent_table_size, &a->kb_cnt)) {
749 ret = -EFAULT;
750 break;
751 }
752 for (i = 0; i < accent_table_size; i++) {
753 diacr.diacr = conv_uni_to_8bit(accent_table[i].diacr);
754 diacr.base = conv_uni_to_8bit(accent_table[i].base);
755 diacr.result = conv_uni_to_8bit(accent_table[i].result);
756 if (copy_to_user(a->kbdiacr + i, &diacr, sizeof(struct kbdiacr))) {
757 ret = -EFAULT;
758 break;
759 }
760 }
761 break;
762 }
763 case KDGKBDIACRUC:
764 {
765 struct kbdiacrsuc __user *a = up;
766
767 if (put_user(accent_table_size, &a->kb_cnt))
768 ret = -EFAULT;
769 else if (copy_to_user(a->kbdiacruc, accent_table,
770 accent_table_size*sizeof(struct kbdiacruc)))
771 ret = -EFAULT;
772 break;
773 }
774
775 case KDSKBDIACR:
776 {
777 struct kbdiacrs __user *a = up;
778 struct kbdiacr diacr;
779 unsigned int ct;
780 int i;
781
782 if (!perm)
783 goto eperm;
784 if (get_user(ct,&a->kb_cnt)) {
785 ret = -EFAULT;
786 break;
787 }
788 if (ct >= MAX_DIACR) {
789 ret = -EINVAL;
790 break;
791 }
792 accent_table_size = ct;
793 for (i = 0; i < ct; i++) {
794 if (copy_from_user(&diacr, a->kbdiacr + i, sizeof(struct kbdiacr))) {
795 ret = -EFAULT;
796 break;
797 }
798 accent_table[i].diacr = conv_8bit_to_uni(diacr.diacr);
799 accent_table[i].base = conv_8bit_to_uni(diacr.base);
800 accent_table[i].result = conv_8bit_to_uni(diacr.result);
801 }
802 break;
803 }
804
805 case KDSKBDIACRUC:
806 {
807 struct kbdiacrsuc __user *a = up;
808 unsigned int ct;
809
810 if (!perm)
811 goto eperm;
812 if (get_user(ct,&a->kb_cnt)) {
813 ret = -EFAULT;
814 break;
815 }
816 if (ct >= MAX_DIACR) {
817 ret = -EINVAL;
818 break;
819 }
820 accent_table_size = ct;
821 if (copy_from_user(accent_table, a->kbdiacruc, ct*sizeof(struct kbdiacruc)))
822 ret = -EFAULT;
823 break;
824 }
825
826 /* the ioctls below read/set the flags usually shown in the leds */
827 /* don't use them - they will go away without warning */
828 case KDGKBLED:
829 ucval = kbd->ledflagstate | (kbd->default_ledflagstate << 4);
830 goto setchar;
831
832 case KDSKBLED:
833 if (!perm)
834 goto eperm;
835 if (arg & ~0x77) {
836 ret = -EINVAL;
837 break;
838 }
839 kbd->ledflagstate = (arg & 7);
840 kbd->default_ledflagstate = ((arg >> 4) & 7);
841 set_leds();
842 break;
843
844 /* the ioctls below only set the lights, not the functions */
845 /* for those, see KDGKBLED and KDSKBLED above */
846 case KDGETLED:
847 ucval = getledstate();
848 setchar:
849 ret = put_user(ucval, (char __user *)arg);
850 break;
851
852 case KDSETLED:
853 if (!perm)
854 goto eperm;
855 setledstate(kbd, arg);
856 break;
857
858 /*
859 * A process can indicate its willingness to accept signals
860 * generated by pressing an appropriate key combination.
861 * Thus, one can have a daemon that e.g. spawns a new console
862 * upon a keypress and then changes to it.
863 * See also the kbrequest field of inittab(5).
864 */
865 case KDSIGACCEPT:
866 {
867 if (!perm || !capable(CAP_KILL))
868 goto eperm;
869 if (!valid_signal(arg) || arg < 1 || arg == SIGKILL)
870 ret = -EINVAL;
871 else {
872 spin_lock_irq(&vt_spawn_con.lock);
873 put_pid(vt_spawn_con.pid);
874 vt_spawn_con.pid = get_pid(task_pid(current));
875 vt_spawn_con.sig = arg;
876 spin_unlock_irq(&vt_spawn_con.lock);
877 }
878 break;
879 }
880
881 case VT_SETMODE:
882 {
883 struct vt_mode tmp;
884
885 if (!perm)
886 goto eperm;
887 if (copy_from_user(&tmp, up, sizeof(struct vt_mode))) {
888 ret = -EFAULT;
889 goto out;
890 }
891 if (tmp.mode != VT_AUTO && tmp.mode != VT_PROCESS) {
892 ret = -EINVAL;
893 goto out;
894 }
895 acquire_console_sem();
896 vc->vt_mode = tmp;
897 /* the frsig is ignored, so we set it to 0 */
898 vc->vt_mode.frsig = 0;
899 put_pid(vc->vt_pid);
900 vc->vt_pid = get_pid(task_pid(current));
901 /* no switch is required -- saw@shade.msu.ru */
902 vc->vt_newvt = -1;
903 release_console_sem();
904 break;
905 }
906
907 case VT_GETMODE:
908 {
909 struct vt_mode tmp;
910 int rc;
911
912 acquire_console_sem();
913 memcpy(&tmp, &vc->vt_mode, sizeof(struct vt_mode));
914 release_console_sem();
915
916 rc = copy_to_user(up, &tmp, sizeof(struct vt_mode));
917 if (rc)
918 ret = -EFAULT;
919 break;
920 }
921
922 /*
923 * Returns global vt state. Note that VT 0 is always open, since
924 * it's an alias for the current VT, and people can't use it here.
925 * We cannot return state for more than 16 VTs, since v_state is short.
926 */
927 case VT_GETSTATE:
928 {
929 struct vt_stat __user *vtstat = up;
930 unsigned short state, mask;
931
932 if (put_user(fg_console + 1, &vtstat->v_active))
933 ret = -EFAULT;
934 else {
935 state = 1; /* /dev/tty0 is always open */
936 for (i = 0, mask = 2; i < MAX_NR_CONSOLES && mask;
937 ++i, mask <<= 1)
938 if (VT_IS_IN_USE(i))
939 state |= mask;
940 ret = put_user(state, &vtstat->v_state);
941 }
942 break;
943 }
944
945 /*
946 * Returns the first available (non-opened) console.
947 */
948 case VT_OPENQRY:
949 for (i = 0; i < MAX_NR_CONSOLES; ++i)
950 if (! VT_IS_IN_USE(i))
951 break;
952 ucval = i < MAX_NR_CONSOLES ? (i+1) : -1;
953 goto setint;
954
955 /*
956 * ioctl(fd, VT_ACTIVATE, num) will cause us to switch to vt # num,
957 * with num >= 1 (switches to vt 0, our console, are not allowed, just
958 * to preserve sanity).
959 */
960 case VT_ACTIVATE:
961 if (!perm)
962 goto eperm;
963 if (arg == 0 || arg > MAX_NR_CONSOLES)
964 ret = -ENXIO;
965 else {
966 arg--;
967 acquire_console_sem();
968 ret = vc_allocate(arg);
969 release_console_sem();
970 if (ret)
971 break;
972 set_console(arg);
973 }
974 break;
975
976 case VT_SETACTIVATE:
977 {
978 struct vt_setactivate vsa;
979
980 if (!perm)
981 goto eperm;
982
983 if (copy_from_user(&vsa, (struct vt_setactivate __user *)arg,
984 sizeof(struct vt_setactivate))) {
985 ret = -EFAULT;
986 goto out;
987 }
988 if (vsa.console == 0 || vsa.console > MAX_NR_CONSOLES)
989 ret = -ENXIO;
990 else {
991 vsa.console--;
992 acquire_console_sem();
993 ret = vc_allocate(vsa.console);
994 if (ret == 0) {
995 struct vc_data *nvc;
996 /* This is safe providing we don't drop the
997 console sem between vc_allocate and
998 finishing referencing nvc */
999 nvc = vc_cons[vsa.console].d;
1000 nvc->vt_mode = vsa.mode;
1001 nvc->vt_mode.frsig = 0;
1002 put_pid(nvc->vt_pid);
1003 nvc->vt_pid = get_pid(task_pid(current));
1004 }
1005 release_console_sem();
1006 if (ret)
1007 break;
1008 /* Commence switch and lock */
1009 set_console(arg);
1010 }
1011 }
1012
1013 /*
1014 * wait until the specified VT has been activated
1015 */
1016 case VT_WAITACTIVE:
1017 if (!perm)
1018 goto eperm;
1019 if (arg == 0 || arg > MAX_NR_CONSOLES)
1020 ret = -ENXIO;
1021 else
1022 ret = vt_waitactive(arg);
1023 break;
1024
1025 /*
1026 * If a vt is under process control, the kernel will not switch to it
1027 * immediately, but postpone the operation until the process calls this
1028 * ioctl, allowing the switch to complete.
1029 *
1030 * According to the X sources this is the behavior:
1031 * 0: pending switch-from not OK
1032 * 1: pending switch-from OK
1033 * 2: completed switch-to OK
1034 */
1035 case VT_RELDISP:
1036 if (!perm)
1037 goto eperm;
1038
1039 if (vc->vt_mode.mode != VT_PROCESS) {
1040 ret = -EINVAL;
1041 break;
1042 }
1043 /*
1044 * Switching-from response
1045 */
1046 acquire_console_sem();
1047 if (vc->vt_newvt >= 0) {
1048 if (arg == 0)
1049 /*
1050 * Switch disallowed, so forget we were trying
1051 * to do it.
1052 */
1053 vc->vt_newvt = -1;
1054
1055 else {
1056 /*
1057 * The current vt has been released, so
1058 * complete the switch.
1059 */
1060 int newvt;
1061 newvt = vc->vt_newvt;
1062 vc->vt_newvt = -1;
1063 ret = vc_allocate(newvt);
1064 if (ret) {
1065 release_console_sem();
1066 break;
1067 }
1068 /*
1069 * When we actually do the console switch,
1070 * make sure we are atomic with respect to
1071 * other console switches..
1072 */
1073 complete_change_console(vc_cons[newvt].d);
1074 }
1075 } else {
1076 /*
1077 * Switched-to response
1078 */
1079 /*
1080 * If it's just an ACK, ignore it
1081 */
1082 if (arg != VT_ACKACQ)
1083 ret = -EINVAL;
1084 }
1085 release_console_sem();
1086 break;
1087
1088 /*
1089 * Disallocate memory associated to VT (but leave VT1)
1090 */
1091 case VT_DISALLOCATE:
1092 if (arg > MAX_NR_CONSOLES) {
1093 ret = -ENXIO;
1094 break;
1095 }
1096 if (arg == 0) {
1097 /* deallocate all unused consoles, but leave 0 */
1098 acquire_console_sem();
1099 for (i=1; i<MAX_NR_CONSOLES; i++)
1100 if (! VT_BUSY(i))
1101 vc_deallocate(i);
1102 release_console_sem();
1103 } else {
1104 /* deallocate a single console, if possible */
1105 arg--;
1106 if (VT_BUSY(arg))
1107 ret = -EBUSY;
1108 else if (arg) { /* leave 0 */
1109 acquire_console_sem();
1110 vc_deallocate(arg);
1111 release_console_sem();
1112 }
1113 }
1114 break;
1115
1116 case VT_RESIZE:
1117 {
1118 struct vt_sizes __user *vtsizes = up;
1119 struct vc_data *vc;
1120
1121 ushort ll,cc;
1122 if (!perm)
1123 goto eperm;
1124 if (get_user(ll, &vtsizes->v_rows) ||
1125 get_user(cc, &vtsizes->v_cols))
1126 ret = -EFAULT;
1127 else {
1128 acquire_console_sem();
1129 for (i = 0; i < MAX_NR_CONSOLES; i++) {
1130 vc = vc_cons[i].d;
1131
1132 if (vc) {
1133 vc->vc_resize_user = 1;
1134 vc_resize(vc_cons[i].d, cc, ll);
1135 }
1136 }
1137 release_console_sem();
1138 }
1139 break;
1140 }
1141
1142 case VT_RESIZEX:
1143 {
1144 struct vt_consize __user *vtconsize = up;
1145 ushort ll,cc,vlin,clin,vcol,ccol;
1146 if (!perm)
1147 goto eperm;
1148 if (!access_ok(VERIFY_READ, vtconsize,
1149 sizeof(struct vt_consize))) {
1150 ret = -EFAULT;
1151 break;
1152 }
1153 /* FIXME: Should check the copies properly */
1154 __get_user(ll, &vtconsize->v_rows);
1155 __get_user(cc, &vtconsize->v_cols);
1156 __get_user(vlin, &vtconsize->v_vlin);
1157 __get_user(clin, &vtconsize->v_clin);
1158 __get_user(vcol, &vtconsize->v_vcol);
1159 __get_user(ccol, &vtconsize->v_ccol);
1160 vlin = vlin ? vlin : vc->vc_scan_lines;
1161 if (clin) {
1162 if (ll) {
1163 if (ll != vlin/clin) {
1164 /* Parameters don't add up */
1165 ret = -EINVAL;
1166 break;
1167 }
1168 } else
1169 ll = vlin/clin;
1170 }
1171 if (vcol && ccol) {
1172 if (cc) {
1173 if (cc != vcol/ccol) {
1174 ret = -EINVAL;
1175 break;
1176 }
1177 } else
1178 cc = vcol/ccol;
1179 }
1180
1181 if (clin > 32) {
1182 ret = -EINVAL;
1183 break;
1184 }
1185
1186 for (i = 0; i < MAX_NR_CONSOLES; i++) {
1187 if (!vc_cons[i].d)
1188 continue;
1189 acquire_console_sem();
1190 if (vlin)
1191 vc_cons[i].d->vc_scan_lines = vlin;
1192 if (clin)
1193 vc_cons[i].d->vc_font.height = clin;
1194 vc_cons[i].d->vc_resize_user = 1;
1195 vc_resize(vc_cons[i].d, cc, ll);
1196 release_console_sem();
1197 }
1198 break;
1199 }
1200
1201 case PIO_FONT: {
1202 if (!perm)
1203 goto eperm;
1204 op.op = KD_FONT_OP_SET;
1205 op.flags = KD_FONT_FLAG_OLD | KD_FONT_FLAG_DONT_RECALC; /* Compatibility */
1206 op.width = 8;
1207 op.height = 0;
1208 op.charcount = 256;
1209 op.data = up;
1210 ret = con_font_op(vc_cons[fg_console].d, &op);
1211 break;
1212 }
1213
1214 case GIO_FONT: {
1215 op.op = KD_FONT_OP_GET;
1216 op.flags = KD_FONT_FLAG_OLD;
1217 op.width = 8;
1218 op.height = 32;
1219 op.charcount = 256;
1220 op.data = up;
1221 ret = con_font_op(vc_cons[fg_console].d, &op);
1222 break;
1223 }
1224
1225 case PIO_CMAP:
1226 if (!perm)
1227 ret = -EPERM;
1228 else
1229 ret = con_set_cmap(up);
1230 break;
1231
1232 case GIO_CMAP:
1233 ret = con_get_cmap(up);
1234 break;
1235
1236 case PIO_FONTX:
1237 case GIO_FONTX:
1238 ret = do_fontx_ioctl(cmd, up, perm, &op);
1239 break;
1240
1241 case PIO_FONTRESET:
1242 {
1243 if (!perm)
1244 goto eperm;
1245
1246#ifdef BROKEN_GRAPHICS_PROGRAMS
1247 /* With BROKEN_GRAPHICS_PROGRAMS defined, the default
1248 font is not saved. */
1249 ret = -ENOSYS;
1250 break;
1251#else
1252 {
1253 op.op = KD_FONT_OP_SET_DEFAULT;
1254 op.data = NULL;
1255 ret = con_font_op(vc_cons[fg_console].d, &op);
1256 if (ret)
1257 break;
1258 con_set_default_unimap(vc_cons[fg_console].d);
1259 break;
1260 }
1261#endif
1262 }
1263
1264 case KDFONTOP: {
1265 if (copy_from_user(&op, up, sizeof(op))) {
1266 ret = -EFAULT;
1267 break;
1268 }
1269 if (!perm && op.op != KD_FONT_OP_GET)
1270 goto eperm;
1271 ret = con_font_op(vc, &op);
1272 if (ret)
1273 break;
1274 if (copy_to_user(up, &op, sizeof(op)))
1275 ret = -EFAULT;
1276 break;
1277 }
1278
1279 case PIO_SCRNMAP:
1280 if (!perm)
1281 ret = -EPERM;
1282 else
1283 ret = con_set_trans_old(up);
1284 break;
1285
1286 case GIO_SCRNMAP:
1287 ret = con_get_trans_old(up);
1288 break;
1289
1290 case PIO_UNISCRNMAP:
1291 if (!perm)
1292 ret = -EPERM;
1293 else
1294 ret = con_set_trans_new(up);
1295 break;
1296
1297 case GIO_UNISCRNMAP:
1298 ret = con_get_trans_new(up);
1299 break;
1300
1301 case PIO_UNIMAPCLR:
1302 { struct unimapinit ui;
1303 if (!perm)
1304 goto eperm;
1305 ret = copy_from_user(&ui, up, sizeof(struct unimapinit));
1306 if (ret)
1307 ret = -EFAULT;
1308 else
1309 con_clear_unimap(vc, &ui);
1310 break;
1311 }
1312
1313 case PIO_UNIMAP:
1314 case GIO_UNIMAP:
1315 ret = do_unimap_ioctl(cmd, up, perm, vc);
1316 break;
1317
1318 case VT_LOCKSWITCH:
1319 if (!capable(CAP_SYS_TTY_CONFIG))
1320 goto eperm;
1321 vt_dont_switch = 1;
1322 break;
1323 case VT_UNLOCKSWITCH:
1324 if (!capable(CAP_SYS_TTY_CONFIG))
1325 goto eperm;
1326 vt_dont_switch = 0;
1327 break;
1328 case VT_GETHIFONTMASK:
1329 ret = put_user(vc->vc_hi_font_mask,
1330 (unsigned short __user *)arg);
1331 break;
1332 case VT_WAITEVENT:
1333 ret = vt_event_wait_ioctl((struct vt_event __user *)arg);
1334 break;
1335 default:
1336 ret = -ENOIOCTLCMD;
1337 }
1338out:
1339 tty_unlock();
1340 return ret;
1341eperm:
1342 ret = -EPERM;
1343 goto out;
1344}
1345
1346void reset_vc(struct vc_data *vc)
1347{
1348 vc->vc_mode = KD_TEXT;
1349 kbd_table[vc->vc_num].kbdmode = default_utf8 ? VC_UNICODE : VC_XLATE;
1350 vc->vt_mode.mode = VT_AUTO;
1351 vc->vt_mode.waitv = 0;
1352 vc->vt_mode.relsig = 0;
1353 vc->vt_mode.acqsig = 0;
1354 vc->vt_mode.frsig = 0;
1355 put_pid(vc->vt_pid);
1356 vc->vt_pid = NULL;
1357 vc->vt_newvt = -1;
1358 if (!in_interrupt()) /* Via keyboard.c:SAK() - akpm */
1359 reset_palette(vc);
1360}
1361
1362void vc_SAK(struct work_struct *work)
1363{
1364 struct vc *vc_con =
1365 container_of(work, struct vc, SAK_work);
1366 struct vc_data *vc;
1367 struct tty_struct *tty;
1368
1369 acquire_console_sem();
1370 vc = vc_con->d;
1371 if (vc) {
1372 tty = vc->port.tty;
1373 /*
1374 * SAK should also work in all raw modes and reset
1375 * them properly.
1376 */
1377 if (tty)
1378 __do_SAK(tty);
1379 reset_vc(vc);
1380 }
1381 release_console_sem();
1382}
1383
1384#ifdef CONFIG_COMPAT
1385
1386struct compat_consolefontdesc {
1387 unsigned short charcount; /* characters in font (256 or 512) */
1388 unsigned short charheight; /* scan lines per character (1-32) */
1389 compat_caddr_t chardata; /* font data in expanded form */
1390};
1391
1392static inline int
1393compat_fontx_ioctl(int cmd, struct compat_consolefontdesc __user *user_cfd,
1394 int perm, struct console_font_op *op)
1395{
1396 struct compat_consolefontdesc cfdarg;
1397 int i;
1398
1399 if (copy_from_user(&cfdarg, user_cfd, sizeof(struct compat_consolefontdesc)))
1400 return -EFAULT;
1401
1402 switch (cmd) {
1403 case PIO_FONTX:
1404 if (!perm)
1405 return -EPERM;
1406 op->op = KD_FONT_OP_SET;
1407 op->flags = KD_FONT_FLAG_OLD;
1408 op->width = 8;
1409 op->height = cfdarg.charheight;
1410 op->charcount = cfdarg.charcount;
1411 op->data = compat_ptr(cfdarg.chardata);
1412 return con_font_op(vc_cons[fg_console].d, op);
1413 case GIO_FONTX:
1414 op->op = KD_FONT_OP_GET;
1415 op->flags = KD_FONT_FLAG_OLD;
1416 op->width = 8;
1417 op->height = cfdarg.charheight;
1418 op->charcount = cfdarg.charcount;
1419 op->data = compat_ptr(cfdarg.chardata);
1420 i = con_font_op(vc_cons[fg_console].d, op);
1421 if (i)
1422 return i;
1423 cfdarg.charheight = op->height;
1424 cfdarg.charcount = op->charcount;
1425 if (copy_to_user(user_cfd, &cfdarg, sizeof(struct compat_consolefontdesc)))
1426 return -EFAULT;
1427 return 0;
1428 }
1429 return -EINVAL;
1430}
1431
1432struct compat_console_font_op {
1433 compat_uint_t op; /* operation code KD_FONT_OP_* */
1434 compat_uint_t flags; /* KD_FONT_FLAG_* */
1435 compat_uint_t width, height; /* font size */
1436 compat_uint_t charcount;
1437 compat_caddr_t data; /* font data with height fixed to 32 */
1438};
1439
1440static inline int
1441compat_kdfontop_ioctl(struct compat_console_font_op __user *fontop,
1442 int perm, struct console_font_op *op, struct vc_data *vc)
1443{
1444 int i;
1445
1446 if (copy_from_user(op, fontop, sizeof(struct compat_console_font_op)))
1447 return -EFAULT;
1448 if (!perm && op->op != KD_FONT_OP_GET)
1449 return -EPERM;
1450 op->data = compat_ptr(((struct compat_console_font_op *)op)->data);
1451 op->flags |= KD_FONT_FLAG_OLD;
1452 i = con_font_op(vc, op);
1453 if (i)
1454 return i;
1455 ((struct compat_console_font_op *)op)->data = (unsigned long)op->data;
1456 if (copy_to_user(fontop, op, sizeof(struct compat_console_font_op)))
1457 return -EFAULT;
1458 return 0;
1459}
1460
1461struct compat_unimapdesc {
1462 unsigned short entry_ct;
1463 compat_caddr_t entries;
1464};
1465
1466static inline int
1467compat_unimap_ioctl(unsigned int cmd, struct compat_unimapdesc __user *user_ud,
1468 int perm, struct vc_data *vc)
1469{
1470 struct compat_unimapdesc tmp;
1471 struct unipair __user *tmp_entries;
1472
1473 if (copy_from_user(&tmp, user_ud, sizeof tmp))
1474 return -EFAULT;
1475 tmp_entries = compat_ptr(tmp.entries);
1476 if (tmp_entries)
1477 if (!access_ok(VERIFY_WRITE, tmp_entries,
1478 tmp.entry_ct*sizeof(struct unipair)))
1479 return -EFAULT;
1480 switch (cmd) {
1481 case PIO_UNIMAP:
1482 if (!perm)
1483 return -EPERM;
1484 return con_set_unimap(vc, tmp.entry_ct, tmp_entries);
1485 case GIO_UNIMAP:
1486 if (!perm && fg_console != vc->vc_num)
1487 return -EPERM;
1488 return con_get_unimap(vc, tmp.entry_ct, &(user_ud->entry_ct), tmp_entries);
1489 }
1490 return 0;
1491}
1492
1493long vt_compat_ioctl(struct tty_struct *tty, struct file * file,
1494 unsigned int cmd, unsigned long arg)
1495{
1496 struct vc_data *vc = tty->driver_data;
1497 struct console_font_op op; /* used in multiple places here */
1498 struct kbd_struct *kbd;
1499 unsigned int console;
1500 void __user *up = (void __user *)arg;
1501 int perm;
1502 int ret = 0;
1503
1504 console = vc->vc_num;
1505
1506 tty_lock();
1507
1508 if (!vc_cons_allocated(console)) { /* impossible? */
1509 ret = -ENOIOCTLCMD;
1510 goto out;
1511 }
1512
1513 /*
1514 * To have permissions to do most of the vt ioctls, we either have
1515 * to be the owner of the tty, or have CAP_SYS_TTY_CONFIG.
1516 */
1517 perm = 0;
1518 if (current->signal->tty == tty || capable(CAP_SYS_TTY_CONFIG))
1519 perm = 1;
1520
1521 kbd = kbd_table + console;
1522 switch (cmd) {
1523 /*
1524 * these need special handlers for incompatible data structures
1525 */
1526 case PIO_FONTX:
1527 case GIO_FONTX:
1528 ret = compat_fontx_ioctl(cmd, up, perm, &op);
1529 break;
1530
1531 case KDFONTOP:
1532 ret = compat_kdfontop_ioctl(up, perm, &op, vc);
1533 break;
1534
1535 case PIO_UNIMAP:
1536 case GIO_UNIMAP:
1537 ret = compat_unimap_ioctl(cmd, up, perm, vc);
1538 break;
1539
1540 /*
1541 * all these treat 'arg' as an integer
1542 */
1543 case KIOCSOUND:
1544 case KDMKTONE:
1545#ifdef CONFIG_X86
1546 case KDADDIO:
1547 case KDDELIO:
1548#endif
1549 case KDSETMODE:
1550 case KDMAPDISP:
1551 case KDUNMAPDISP:
1552 case KDSKBMODE:
1553 case KDSKBMETA:
1554 case KDSKBLED:
1555 case KDSETLED:
1556 case KDSIGACCEPT:
1557 case VT_ACTIVATE:
1558 case VT_WAITACTIVE:
1559 case VT_RELDISP:
1560 case VT_DISALLOCATE:
1561 case VT_RESIZE:
1562 case VT_RESIZEX:
1563 goto fallback;
1564
1565 /*
1566 * the rest has a compatible data structure behind arg,
1567 * but we have to convert it to a proper 64 bit pointer.
1568 */
1569 default:
1570 arg = (unsigned long)compat_ptr(arg);
1571 goto fallback;
1572 }
1573out:
1574 tty_unlock();
1575 return ret;
1576
1577fallback:
1578 tty_unlock();
1579 return vt_ioctl(tty, file, cmd, arg);
1580}
1581
1582
1583#endif /* CONFIG_COMPAT */
1584
1585
1586/*
1587 * Performs the back end of a vt switch. Called under the console
1588 * semaphore.
1589 */
1590static void complete_change_console(struct vc_data *vc)
1591{
1592 unsigned char old_vc_mode;
1593 int old = fg_console;
1594
1595 last_console = fg_console;
1596
1597 /*
1598 * If we're switching, we could be going from KD_GRAPHICS to
1599 * KD_TEXT mode or vice versa, which means we need to blank or
1600 * unblank the screen later.
1601 */
1602 old_vc_mode = vc_cons[fg_console].d->vc_mode;
1603 switch_screen(vc);
1604
1605 /*
1606 * This can't appear below a successful kill_pid(). If it did,
1607 * then the *blank_screen operation could occur while X, having
1608 * received acqsig, is waking up on another processor. This
1609 * condition can lead to overlapping accesses to the VGA range
1610 * and the framebuffer (causing system lockups).
1611 *
1612 * To account for this we duplicate this code below only if the
1613 * controlling process is gone and we've called reset_vc.
1614 */
1615 if (old_vc_mode != vc->vc_mode) {
1616 if (vc->vc_mode == KD_TEXT)
1617 do_unblank_screen(1);
1618 else
1619 do_blank_screen(1);
1620 }
1621
1622 /*
1623 * If this new console is under process control, send it a signal
1624 * telling it that it has acquired. Also check if it has died and
1625 * clean up (similar to logic employed in change_console())
1626 */
1627 if (vc->vt_mode.mode == VT_PROCESS) {
1628 /*
1629 * Send the signal as privileged - kill_pid() will
1630 * tell us if the process has gone or something else
1631 * is awry
1632 */
1633 if (kill_pid(vc->vt_pid, vc->vt_mode.acqsig, 1) != 0) {
1634 /*
1635 * The controlling process has died, so we revert back to
1636 * normal operation. In this case, we'll also change back
1637 * to KD_TEXT mode. I'm not sure if this is strictly correct
1638 * but it saves the agony when the X server dies and the screen
1639 * remains blanked due to KD_GRAPHICS! It would be nice to do
1640 * this outside of VT_PROCESS but there is no single process
1641 * to account for and tracking tty count may be undesirable.
1642 */
1643 reset_vc(vc);
1644
1645 if (old_vc_mode != vc->vc_mode) {
1646 if (vc->vc_mode == KD_TEXT)
1647 do_unblank_screen(1);
1648 else
1649 do_blank_screen(1);
1650 }
1651 }
1652 }
1653
1654 /*
1655 * Wake anyone waiting for their VT to activate
1656 */
1657 vt_event_post(VT_EVENT_SWITCH, old, vc->vc_num);
1658 return;
1659}
1660
1661/*
1662 * Performs the front-end of a vt switch
1663 */
1664void change_console(struct vc_data *new_vc)
1665{
1666 struct vc_data *vc;
1667
1668 if (!new_vc || new_vc->vc_num == fg_console || vt_dont_switch)
1669 return;
1670
1671 /*
1672 * If this vt is in process mode, then we need to handshake with
1673 * that process before switching. Essentially, we store where that
1674 * vt wants to switch to and wait for it to tell us when it's done
1675 * (via VT_RELDISP ioctl).
1676 *
1677 * We also check to see if the controlling process still exists.
1678 * If it doesn't, we reset this vt to auto mode and continue.
1679 * This is a cheap way to track process control. The worst thing
1680 * that can happen is: we send a signal to a process, it dies, and
1681 * the switch gets "lost" waiting for a response; hopefully, the
1682 * user will try again, we'll detect the process is gone (unless
1683 * the user waits just the right amount of time :-) and revert the
1684 * vt to auto control.
1685 */
1686 vc = vc_cons[fg_console].d;
1687 if (vc->vt_mode.mode == VT_PROCESS) {
1688 /*
1689 * Send the signal as privileged - kill_pid() will
1690 * tell us if the process has gone or something else
1691 * is awry.
1692 *
1693 * We need to set vt_newvt *before* sending the signal or we
1694 * have a race.
1695 */
1696 vc->vt_newvt = new_vc->vc_num;
1697 if (kill_pid(vc->vt_pid, vc->vt_mode.relsig, 1) == 0) {
1698 /*
1699 * It worked. Mark the vt to switch to and
1700 * return. The process needs to send us a
1701 * VT_RELDISP ioctl to complete the switch.
1702 */
1703 return;
1704 }
1705
1706 /*
1707 * The controlling process has died, so we revert back to
1708 * normal operation. In this case, we'll also change back
1709 * to KD_TEXT mode. I'm not sure if this is strictly correct
1710 * but it saves the agony when the X server dies and the screen
1711 * remains blanked due to KD_GRAPHICS! It would be nice to do
1712 * this outside of VT_PROCESS but there is no single process
1713 * to account for and tracking tty count may be undesirable.
1714 */
1715 reset_vc(vc);
1716
1717 /*
1718 * Fall through to normal (VT_AUTO) handling of the switch...
1719 */
1720 }
1721
1722 /*
1723 * Ignore all switches in KD_GRAPHICS+VT_AUTO mode
1724 */
1725 if (vc->vc_mode == KD_GRAPHICS)
1726 return;
1727
1728 complete_change_console(new_vc);
1729}
1730
1731/* Perform a kernel triggered VT switch for suspend/resume */
1732
1733static int disable_vt_switch;
1734
1735int vt_move_to_console(unsigned int vt, int alloc)
1736{
1737 int prev;
1738
1739 acquire_console_sem();
1740 /* Graphics mode - up to X */
1741 if (disable_vt_switch) {
1742 release_console_sem();
1743 return 0;
1744 }
1745 prev = fg_console;
1746
1747 if (alloc && vc_allocate(vt)) {
1748 /* we can't have a free VC for now. Too bad,
1749 * we don't want to mess the screen for now. */
1750 release_console_sem();
1751 return -ENOSPC;
1752 }
1753
1754 if (set_console(vt)) {
1755 /*
1756 * We're unable to switch to the SUSPEND_CONSOLE.
1757 * Let the calling function know so it can decide
1758 * what to do.
1759 */
1760 release_console_sem();
1761 return -EIO;
1762 }
1763 release_console_sem();
1764 tty_lock();
1765 if (vt_waitactive(vt + 1)) {
1766 pr_debug("Suspend: Can't switch VCs.");
1767 tty_unlock();
1768 return -EINTR;
1769 }
1770 tty_unlock();
1771 return prev;
1772}
1773
1774/*
1775 * Normally during a suspend, we allocate a new console and switch to it.
1776 * When we resume, we switch back to the original console. This switch
1777 * can be slow, so on systems where the framebuffer can handle restoration
1778 * of video registers anyways, there's little point in doing the console
1779 * switch. This function allows you to disable it by passing it '0'.
1780 */
1781void pm_set_vt_switch(int do_switch)
1782{
1783 acquire_console_sem();
1784 disable_vt_switch = !do_switch;
1785 release_console_sem();
1786}
1787EXPORT_SYMBOL(pm_set_vt_switch);
diff --git a/drivers/char/xilinx_hwicap/xilinx_hwicap.c b/drivers/char/xilinx_hwicap/xilinx_hwicap.c
index b663d573aad9..39ccdeada791 100644
--- a/drivers/char/xilinx_hwicap/xilinx_hwicap.c
+++ b/drivers/char/xilinx_hwicap/xilinx_hwicap.c
@@ -67,7 +67,7 @@
67 * cp foo.bit /dev/icap0 67 * cp foo.bit /dev/icap0
68 * 68 *
69 * Note that unless foo.bit is an appropriately constructed partial 69 * Note that unless foo.bit is an appropriately constructed partial
70 * bitstream, this has a high likelyhood of overwriting the design 70 * bitstream, this has a high likelihood of overwriting the design
71 * currently programmed in the FPGA. 71 * currently programmed in the FPGA.
72 */ 72 */
73 73
@@ -81,7 +81,6 @@
81#include <linux/poll.h> 81#include <linux/poll.h>
82#include <linux/proc_fs.h> 82#include <linux/proc_fs.h>
83#include <linux/mutex.h> 83#include <linux/mutex.h>
84#include <linux/smp_lock.h>
85#include <linux/sysctl.h> 84#include <linux/sysctl.h>
86#include <linux/fs.h> 85#include <linux/fs.h>
87#include <linux/cdev.h> 86#include <linux/cdev.h>
@@ -112,6 +111,7 @@
112#define HWICAP_DEVICES 1 111#define HWICAP_DEVICES 1
113 112
114/* An array, which is set to true when the device is registered. */ 113/* An array, which is set to true when the device is registered. */
114static DEFINE_MUTEX(hwicap_mutex);
115static bool probed_devices[HWICAP_DEVICES]; 115static bool probed_devices[HWICAP_DEVICES];
116static struct mutex icap_sem; 116static struct mutex icap_sem;
117 117
@@ -502,7 +502,7 @@ static int hwicap_open(struct inode *inode, struct file *file)
502 struct hwicap_drvdata *drvdata; 502 struct hwicap_drvdata *drvdata;
503 int status; 503 int status;
504 504
505 lock_kernel(); 505 mutex_lock(&hwicap_mutex);
506 drvdata = container_of(inode->i_cdev, struct hwicap_drvdata, cdev); 506 drvdata = container_of(inode->i_cdev, struct hwicap_drvdata, cdev);
507 507
508 status = mutex_lock_interruptible(&drvdata->sem); 508 status = mutex_lock_interruptible(&drvdata->sem);
@@ -528,7 +528,7 @@ static int hwicap_open(struct inode *inode, struct file *file)
528 error: 528 error:
529 mutex_unlock(&drvdata->sem); 529 mutex_unlock(&drvdata->sem);
530 out: 530 out:
531 unlock_kernel(); 531 mutex_unlock(&hwicap_mutex);
532 return status; 532 return status;
533} 533}
534 534
@@ -567,6 +567,7 @@ static const struct file_operations hwicap_fops = {
567 .read = hwicap_read, 567 .read = hwicap_read,
568 .open = hwicap_open, 568 .open = hwicap_open,
569 .release = hwicap_release, 569 .release = hwicap_release,
570 .llseek = noop_llseek,
570}; 571};
571 572
572static int __devinit hwicap_setup(struct device *dev, int id, 573static int __devinit hwicap_setup(struct device *dev, int id,
@@ -713,20 +714,29 @@ static int __devexit hwicap_remove(struct device *dev)
713 return 0; /* success */ 714 return 0; /* success */
714} 715}
715 716
716static int __devinit hwicap_drv_probe(struct platform_device *pdev) 717#ifdef CONFIG_OF
718static int __devinit hwicap_of_probe(struct platform_device *op,
719 const struct hwicap_driver_config *config)
717{ 720{
718 struct resource *res; 721 struct resource res;
719 const struct config_registers *regs; 722 const unsigned int *id;
720 const char *family; 723 const char *family;
724 int rc;
725 const struct config_registers *regs;
721 726
722 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 727
723 if (!res) 728 rc = of_address_to_resource(op->dev.of_node, 0, &res);
724 return -ENODEV; 729 if (rc) {
730 dev_err(&op->dev, "invalid address\n");
731 return rc;
732 }
733
734 id = of_get_property(op->dev.of_node, "port-number", NULL);
725 735
726 /* It's most likely that we're using V4, if the family is not 736 /* It's most likely that we're using V4, if the family is not
727 specified */ 737 specified */
728 regs = &v4_config_registers; 738 regs = &v4_config_registers;
729 family = pdev->dev.platform_data; 739 family = of_get_property(op->dev.of_node, "xlnx,family", NULL);
730 740
731 if (family) { 741 if (family) {
732 if (!strcmp(family, "virtex2p")) { 742 if (!strcmp(family, "virtex2p")) {
@@ -737,54 +747,37 @@ static int __devinit hwicap_drv_probe(struct platform_device *pdev)
737 regs = &v5_config_registers; 747 regs = &v5_config_registers;
738 } 748 }
739 } 749 }
740 750 return hwicap_setup(&op->dev, id ? *id : -1, &res, config,
741 return hwicap_setup(&pdev->dev, pdev->id, res, 751 regs);
742 &buffer_icap_config, regs);
743} 752}
744 753#else
745static int __devexit hwicap_drv_remove(struct platform_device *pdev) 754static inline int hwicap_of_probe(struct platform_device *op,
755 const struct hwicap_driver_config *config)
746{ 756{
747 return hwicap_remove(&pdev->dev); 757 return -EINVAL;
748} 758}
759#endif /* CONFIG_OF */
749 760
750static struct platform_driver hwicap_platform_driver = { 761static const struct of_device_id __devinitconst hwicap_of_match[];
751 .probe = hwicap_drv_probe, 762static int __devinit hwicap_drv_probe(struct platform_device *pdev)
752 .remove = hwicap_drv_remove,
753 .driver = {
754 .owner = THIS_MODULE,
755 .name = DRIVER_NAME,
756 },
757};
758
759/* ---------------------------------------------------------------------
760 * OF bus binding
761 */
762
763#if defined(CONFIG_OF)
764static int __devinit
765hwicap_of_probe(struct platform_device *op, const struct of_device_id *match)
766{ 763{
767 struct resource res; 764 const struct of_device_id *match;
768 const unsigned int *id; 765 struct resource *res;
769 const char *family;
770 int rc;
771 const struct hwicap_driver_config *config = match->data;
772 const struct config_registers *regs; 766 const struct config_registers *regs;
767 const char *family;
773 768
774 dev_dbg(&op->dev, "hwicap_of_probe(%p, %p)\n", op, match); 769 match = of_match_device(hwicap_of_match, &pdev->dev);
775 770 if (match)
776 rc = of_address_to_resource(op->dev.of_node, 0, &res); 771 return hwicap_of_probe(pdev, match->data);
777 if (rc) {
778 dev_err(&op->dev, "invalid address\n");
779 return rc;
780 }
781 772
782 id = of_get_property(op->dev.of_node, "port-number", NULL); 773 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
774 if (!res)
775 return -ENODEV;
783 776
784 /* It's most likely that we're using V4, if the family is not 777 /* It's most likely that we're using V4, if the family is not
785 specified */ 778 specified */
786 regs = &v4_config_registers; 779 regs = &v4_config_registers;
787 family = of_get_property(op->dev.of_node, "xlnx,family", NULL); 780 family = pdev->dev.platform_data;
788 781
789 if (family) { 782 if (family) {
790 if (!strcmp(family, "virtex2p")) { 783 if (!strcmp(family, "virtex2p")) {
@@ -795,50 +788,38 @@ hwicap_of_probe(struct platform_device *op, const struct of_device_id *match)
795 regs = &v5_config_registers; 788 regs = &v5_config_registers;
796 } 789 }
797 } 790 }
798 return hwicap_setup(&op->dev, id ? *id : -1, &res, config, 791
799 regs); 792 return hwicap_setup(&pdev->dev, pdev->id, res,
793 &buffer_icap_config, regs);
800} 794}
801 795
802static int __devexit hwicap_of_remove(struct platform_device *op) 796static int __devexit hwicap_drv_remove(struct platform_device *pdev)
803{ 797{
804 return hwicap_remove(&op->dev); 798 return hwicap_remove(&pdev->dev);
805} 799}
806 800
807/* Match table for of_platform binding */ 801#ifdef CONFIG_OF
802/* Match table for device tree binding */
808static const struct of_device_id __devinitconst hwicap_of_match[] = { 803static const struct of_device_id __devinitconst hwicap_of_match[] = {
809 { .compatible = "xlnx,opb-hwicap-1.00.b", .data = &buffer_icap_config}, 804 { .compatible = "xlnx,opb-hwicap-1.00.b", .data = &buffer_icap_config},
810 { .compatible = "xlnx,xps-hwicap-1.00.a", .data = &fifo_icap_config}, 805 { .compatible = "xlnx,xps-hwicap-1.00.a", .data = &fifo_icap_config},
811 {}, 806 {},
812}; 807};
813MODULE_DEVICE_TABLE(of, hwicap_of_match); 808MODULE_DEVICE_TABLE(of, hwicap_of_match);
809#else
810#define hwicap_of_match NULL
811#endif
814 812
815static struct of_platform_driver hwicap_of_driver = { 813static struct platform_driver hwicap_platform_driver = {
816 .probe = hwicap_of_probe, 814 .probe = hwicap_drv_probe,
817 .remove = __devexit_p(hwicap_of_remove), 815 .remove = hwicap_drv_remove,
818 .driver = { 816 .driver = {
819 .name = DRIVER_NAME,
820 .owner = THIS_MODULE, 817 .owner = THIS_MODULE,
818 .name = DRIVER_NAME,
821 .of_match_table = hwicap_of_match, 819 .of_match_table = hwicap_of_match,
822 }, 820 },
823}; 821};
824 822
825/* Registration helpers to keep the number of #ifdefs to a minimum */
826static inline int __init hwicap_of_register(void)
827{
828 pr_debug("hwicap: calling of_register_platform_driver()\n");
829 return of_register_platform_driver(&hwicap_of_driver);
830}
831
832static inline void __exit hwicap_of_unregister(void)
833{
834 of_unregister_platform_driver(&hwicap_of_driver);
835}
836#else /* CONFIG_OF */
837/* CONFIG_OF not enabled; do nothing helpers */
838static inline int __init hwicap_of_register(void) { return 0; }
839static inline void __exit hwicap_of_unregister(void) { }
840#endif /* CONFIG_OF */
841
842static int __init hwicap_module_init(void) 823static int __init hwicap_module_init(void)
843{ 824{
844 dev_t devt; 825 dev_t devt;
@@ -855,21 +836,12 @@ static int __init hwicap_module_init(void)
855 return retval; 836 return retval;
856 837
857 retval = platform_driver_register(&hwicap_platform_driver); 838 retval = platform_driver_register(&hwicap_platform_driver);
858
859 if (retval)
860 goto failed1;
861
862 retval = hwicap_of_register();
863
864 if (retval) 839 if (retval)
865 goto failed2; 840 goto failed;
866 841
867 return retval; 842 return retval;
868 843
869 failed2: 844 failed:
870 platform_driver_unregister(&hwicap_platform_driver);
871
872 failed1:
873 unregister_chrdev_region(devt, HWICAP_DEVICES); 845 unregister_chrdev_region(devt, HWICAP_DEVICES);
874 846
875 return retval; 847 return retval;
@@ -883,8 +855,6 @@ static void __exit hwicap_module_cleanup(void)
883 855
884 platform_driver_unregister(&hwicap_platform_driver); 856 platform_driver_unregister(&hwicap_platform_driver);
885 857
886 hwicap_of_unregister();
887
888 unregister_chrdev_region(devt, HWICAP_DEVICES); 858 unregister_chrdev_region(devt, HWICAP_DEVICES);
889} 859}
890 860