Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

comedi: ni_mio_common: Conditionally use I/O port or MMIO

In a future patch, the port I/O functions (`inb()`, `outb()`, and
friends will only be declared in the `HAS_IOPORT` configuration option
is enabled.

The "ni_mio_common.c" file contains calls to both port I/O functions and
memory-mapped I/O functions. The file is `#include`d by "ni_atmio.c",
"ni_mio_cs.c", and "ni_pcimio.c" for the ni_atmio, ni_mio_cs, and
ni_pcimio modules, respectively. Only "ni_pcimio.c" defines the
`PCIDMA` macro before including "ni_mio_common.c" and various bits of
code in "ni_mio_common.c" is conditionally compiled according to whether
that macro is defined or not. Currently, the port I/O function calls
are compiled in regardless of whether the `PCIDMA` macro is defined or
not. However, the fact is that the ni_atmio and ni_mio_cs modules will
never call the memory-mapped I/O functions, and the ni_pcimio module
will never call the port I/O functions.

Calls to the port I/O and memory-mapped I/O functions is confined to the
`ni_writel()`, `ni_writew()`, `ni_writeb()`, `ni_readl()`, `ni_readw()`,
and `ni_readb()` functions which do a run-time test to decide whether to
call the port I/O functions or the memory-mapped I/O functions.
Conditionally compile two variants of the functions so they only call
the port I/O functions if the `PCIDMA` macro is undefined (for the
ni_atmio and ni_mio_cs modules), and only call the memory-mapped I/O
functions if the `PCIDMA` macro is defined (for the ni_pcimio module).

Add a run-time check in the `ni_E_init()` function to return an error if
the comedi device has been set up to use port I/O if `PCIDMA` is
defined, or has been set up to use memory-mapped I/O if `PCIDMA` is not
defined.

The changes make it possible to build the ni_pcimio module even if the
port I/O functions have not been declared. (The ni_atmio and ni_mio_cs
modules do still require the port I/O functions to be declared.)

Cc: Arnd Bergmann <arnd@kernel.org>
Cc: Niklas Schnelle <schnelle@linux.ibm.com>
Signed-off-by: Ian Abbott <abbotti@mev.co.uk>
Link: https://lore.kernel.org/r/20230913170712.111719-10-abbotti@mev.co.uk
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Ian Abbott and committed by
Greg Kroah-Hartman
4e1bd672 a7b9ffd8

+50 -20
+50 -20
drivers/comedi/drivers/ni_mio_common.c
··· 46 46 #include <linux/comedi/comedi_8255.h> 47 47 #include "mite.h" 48 48 49 + #ifdef PCIDMA 50 + #define IS_PCIMIO 1 51 + #else 52 + #define IS_PCIMIO 0 53 + #endif 54 + 49 55 /* A timeout count */ 50 56 #define NI_TIMEOUT 1000 51 57 ··· 225 219 226 220 static const int num_adc_stages_611x = 3; 227 221 222 + #ifdef PCIDMA 223 + 228 224 static void ni_writel(struct comedi_device *dev, unsigned int data, int reg) 229 225 { 230 - if (dev->mmio) 231 - writel(data, dev->mmio + reg); 232 - else 233 - outl(data, dev->iobase + reg); 226 + writel(data, dev->mmio + reg); 234 227 } 235 228 236 229 static void ni_writew(struct comedi_device *dev, unsigned int data, int reg) 237 230 { 238 - if (dev->mmio) 239 - writew(data, dev->mmio + reg); 240 - else 241 - outw(data, dev->iobase + reg); 231 + writew(data, dev->mmio + reg); 242 232 } 243 233 244 234 static void ni_writeb(struct comedi_device *dev, unsigned int data, int reg) 245 235 { 246 - if (dev->mmio) 247 - writeb(data, dev->mmio + reg); 248 - else 249 - outb(data, dev->iobase + reg); 236 + writeb(data, dev->mmio + reg); 250 237 } 251 238 252 239 static unsigned int ni_readl(struct comedi_device *dev, int reg) 253 240 { 254 - if (dev->mmio) 255 - return readl(dev->mmio + reg); 241 + return readl(dev->mmio + reg); 242 + } 256 243 244 + static unsigned int ni_readw(struct comedi_device *dev, int reg) 245 + { 246 + return readw(dev->mmio + reg); 247 + } 248 + 249 + static unsigned int ni_readb(struct comedi_device *dev, int reg) 250 + { 251 + return readb(dev->mmio + reg); 252 + } 253 + 254 + #else /* PCIDMA */ 255 + 256 + static void ni_writel(struct comedi_device *dev, unsigned int data, int reg) 257 + { 258 + outl(data, dev->iobase + reg); 259 + } 260 + 261 + static void ni_writew(struct comedi_device *dev, unsigned int data, int reg) 262 + { 263 + outw(data, dev->iobase + reg); 264 + } 265 + 266 + static void ni_writeb(struct comedi_device *dev, unsigned int data, int reg) 267 + { 268 + outb(data, dev->iobase + reg); 269 + } 270 + 271 + static unsigned int ni_readl(struct comedi_device *dev, int reg) 272 + { 257 273 return inl(dev->iobase + reg); 258 274 } 259 275 260 276 static unsigned int ni_readw(struct comedi_device *dev, int reg) 261 277 { 262 - if (dev->mmio) 263 - return readw(dev->mmio + reg); 264 - 265 278 return inw(dev->iobase + reg); 266 279 } 267 280 268 281 static unsigned int ni_readb(struct comedi_device *dev, int reg) 269 282 { 270 - if (dev->mmio) 271 - return readb(dev->mmio + reg); 272 - 273 283 return inb(dev->iobase + reg); 274 284 } 285 + 286 + #endif /* PCIDMA */ 275 287 276 288 /* 277 289 * We automatically take advantage of STC registers that can be ··· 6001 5977 int i; 6002 5978 const char *dev_family = devpriv->is_m_series ? "ni_mseries" 6003 5979 : "ni_eseries"; 5980 + if (!IS_PCIMIO != !dev->mmio) { 5981 + dev_err(dev->class_dev, 5982 + "%s: bug! %s device not supported.\n", 5983 + KBUILD_MODNAME, board->name); 5984 + return -ENXIO; 5985 + } 6004 5986 6005 5987 /* prepare the device for globally-named routes. */ 6006 5988 if (ni_assign_device_routes(dev_family, board->name,