From 22691aec19371c0817565c3af54e276884e908ee Mon Sep 17 00:00:00 2001 From: Ian Abbott Date: Fri, 12 Apr 2013 17:01:54 +0100 Subject: [PATCH] staging: comedi: amplc_pc263: split out PCI support The "amplc_pc263" module is a hybrid driver for Amplicon PC263 (ISA) and PCI263 (PCI) and uses conditional compilation (and compiler optimization) to compile in the support for the different bus types. Split out support for the PCI263 into a new module "amplc_pci263", retaining support for the PC263 in the existing module "amplc_pc263". Don't bother supporting the legacy attach mechanism for PCI board in the new module as that is no longer in vogue for the comedi drivers and the PCI263 board has no special configuration requirements. Although the code to handle the single subdevice of each board is the same for both drivers, this is only a small amount of code and I don't think it's worth creating a common module to handle it. Signed-off-by: Ian Abbott Reviewed-by: H Hartley Sweeten Signed-off-by: Greg Kroah-Hartman --- drivers/staging/comedi/Kconfig | 13 +- drivers/staging/comedi/drivers/Makefile | 3 +- drivers/staging/comedi/drivers/amplc_pc263.c | 278 ++---------------- drivers/staging/comedi/drivers/amplc_pci263.c | 127 ++++++++ 4 files changed, 152 insertions(+), 269 deletions(-) create mode 100644 drivers/staging/comedi/drivers/amplc_pci263.c diff --git a/drivers/staging/comedi/Kconfig b/drivers/staging/comedi/Kconfig index 67993c069a68..9600225ff641 100644 --- a/drivers/staging/comedi/Kconfig +++ b/drivers/staging/comedi/Kconfig @@ -230,9 +230,9 @@ config COMEDI_AMPLC_PC236_ISA config COMEDI_AMPLC_PC263_ISA tristate "Amplicon PC263 relay board support" - select COMEDI_AMPLC_PC263 ---help--- - Enable support for Amplicon PC263 ISA relay board. + Enable support for Amplicon PC263 ISA relay board. This board has + 16 reed relay output channels. To compile this driver as a module, choose M here: the module will be called amplc_pc263. @@ -771,12 +771,12 @@ config COMEDI_AMPLC_PC236_PCI config COMEDI_AMPLC_PC263_PCI tristate "Amplicon PCI263 relay board support" - select COMEDI_AMPLC_PC263 ---help--- - Enable support for Amplicon PCI263 relay board. + Enable support for Amplicon PCI263 relay board. This is a PCI board + with 16 reed relay output channels. To compile this driver as a module, choose M here: the module will be - called amplc_pc263. + called amplc_pci263. config COMEDI_AMPLC_PCI224 tristate "Amplicon PCI224 and PCI234 support" @@ -1251,9 +1251,6 @@ config COMEDI_AMPLC_PC236 tristate select COMEDI_8255 -config COMEDI_AMPLC_PC263 - tristate - config COMEDI_DAS08 tristate select COMEDI_8255 diff --git a/drivers/staging/comedi/drivers/Makefile b/drivers/staging/comedi/drivers/Makefile index 4857d46d684a..bb5634e49883 100644 --- a/drivers/staging/comedi/drivers/Makefile +++ b/drivers/staging/comedi/drivers/Makefile @@ -13,6 +13,7 @@ obj-$(CONFIG_COMEDI_SKEL) += skel.o # Comedi ISA drivers obj-$(CONFIG_COMEDI_ACL7225B) += acl7225b.o obj-$(CONFIG_COMEDI_AMPLC_DIO200_ISA) += amplc_dio200.o +obj-$(CONFIG_COMEDI_AMPLC_PC263_ISA) += amplc_pc263.o obj-$(CONFIG_COMEDI_PCL711) += pcl711.o obj-$(CONFIG_COMEDI_PCL724) += pcl724.o obj-$(CONFIG_COMEDI_PCL725) += pcl725.o @@ -80,7 +81,7 @@ obj-$(CONFIG_COMEDI_ADV_PCI1724) += adv_pci1724.o obj-$(CONFIG_COMEDI_ADV_PCI_DIO) += adv_pci_dio.o obj-$(CONFIG_COMEDI_AMPLC_DIO200_PCI) += amplc_dio200_pci.o obj-$(CONFIG_COMEDI_AMPLC_PC236) += amplc_pc236.o -obj-$(CONFIG_COMEDI_AMPLC_PC263) += amplc_pc263.o +obj-$(CONFIG_COMEDI_AMPLC_PC263_PCI) += amplc_pci263.o obj-$(CONFIG_COMEDI_AMPLC_PCI224) += amplc_pci224.o obj-$(CONFIG_COMEDI_AMPLC_PCI230) += amplc_pci230.o obj-$(CONFIG_COMEDI_CONTEC_PCI_DIO) += contec_pci_dio.o diff --git a/drivers/staging/comedi/drivers/amplc_pc263.c b/drivers/staging/comedi/drivers/amplc_pc263.c index 1b1c1aac3109..1ffe37991792 100644 --- a/drivers/staging/comedi/drivers/amplc_pc263.c +++ b/drivers/staging/comedi/drivers/amplc_pc263.c @@ -24,149 +24,41 @@ */ /* Driver: amplc_pc263 -Description: Amplicon PC263, PCI263 +Description: Amplicon PC263 Author: Ian Abbott -Devices: [Amplicon] PC263 (pc263), PCI263 (pci263 or amplc_pc263) -Updated: Wed, 22 Oct 2008 14:10:53 +0100 +Devices: [Amplicon] PC263 (pc263) +Updated: Fri, 12 Apr 2013 15:19:36 +0100 Status: works -Configuration options - PC263: +Configuration options: [0] - I/O port base address -Configuration options - PCI263: - [0] - PCI bus of device (optional) - [1] - PCI slot of device (optional) - If bus/slot is not specified, the first available PCI device will be - used. - -Each board appears as one subdevice, with 16 digital outputs, each +The board appears as one subdevice, with 16 digital outputs, each connected to a reed-relay. Relay contacts are closed when output is 1. The state of the outputs can be read. */ -#include - #include "../comedidev.h" #define PC263_DRIVER_NAME "amplc_pc263" -#define DO_ISA IS_ENABLED(CONFIG_COMEDI_AMPLC_PC263_ISA) -#define DO_PCI IS_ENABLED(CONFIG_COMEDI_AMPLC_PC263_PCI) - -/* PCI263 PCI configuration register information */ -#define PCI_DEVICE_ID_AMPLICON_PCI263 0x000c -#define PCI_DEVICE_ID_INVALID 0xffff - -/* PC263 / PCI263 registers */ +/* PC263 registers */ #define PC263_IO_SIZE 2 /* - * Board descriptions for Amplicon PC263 / PCI263. + * Board descriptions for Amplicon PC263. */ -enum pc263_bustype { isa_bustype, pci_bustype }; -enum pc263_model { pc263_model, pci263_model, anypci_model }; - struct pc263_board { const char *name; - unsigned short devid; - enum pc263_bustype bustype; - enum pc263_model model; }; + static const struct pc263_board pc263_boards[] = { -#if DO_ISA { .name = "pc263", - .bustype = isa_bustype, - .model = pc263_model, - }, -#endif -#if DO_PCI - { - .name = "pci263", - .devid = PCI_DEVICE_ID_AMPLICON_PCI263, - .bustype = pci_bustype, - .model = pci263_model, - }, - { - .name = PC263_DRIVER_NAME, - .devid = PCI_DEVICE_ID_INVALID, - .bustype = pci_bustype, - .model = anypci_model, /* wildcard */ }, -#endif }; -/* test if ISA supported and this is an ISA board */ -static inline bool is_isa_board(const struct pc263_board *board) -{ - return DO_ISA && board->bustype == isa_bustype; -} - -/* test if PCI supported and this is a PCI board */ -static inline bool is_pci_board(const struct pc263_board *board) -{ - return DO_PCI && board->bustype == pci_bustype; -} - -/* - * This function looks for a board matching the supplied PCI device. - */ -static const struct pc263_board *pc263_find_pci_board(struct pci_dev *pci_dev) -{ - unsigned int i; - - for (i = 0; i < ARRAY_SIZE(pc263_boards); i++) - if (is_pci_board(&pc263_boards[i]) && - pci_dev->device == pc263_boards[i].devid) - return &pc263_boards[i]; - return NULL; -} - - -/* - * This function looks for a PCI device matching the requested board name, - * bus and slot. - */ -static struct pci_dev *pc263_find_pci_dev(struct comedi_device *dev, - struct comedi_devconfig *it) -{ - const struct pc263_board *thisboard = comedi_board(dev); - struct pci_dev *pci_dev = NULL; - int bus = it->options[0]; - int slot = it->options[1]; - - for_each_pci_dev(pci_dev) { - if (bus || slot) { - if (bus != pci_dev->bus->number || - slot != PCI_SLOT(pci_dev->devfn)) - continue; - } - if (pci_dev->vendor != PCI_VENDOR_ID_AMPLICON) - continue; - - if (thisboard->model == anypci_model) { - /* Wildcard board matches any supported PCI board. */ - const struct pc263_board *foundboard; - - foundboard = pc263_find_pci_board(pci_dev); - if (foundboard == NULL) - continue; - /* Replace wildcard board_ptr. */ - dev->board_ptr = thisboard = foundboard; - } else { - /* Match specific model name. */ - if (pci_dev->device != thisboard->devid) - continue; - } - return pci_dev; - } - dev_err(dev->class_dev, - "No supported board found! (req. bus %d, slot %d)\n", - bus, slot); - return NULL; -} - static int pc263_do_insn_bits(struct comedi_device *dev, struct comedi_subdevice *s, struct comedi_insn *insn, unsigned int *data) @@ -183,30 +75,14 @@ static int pc263_do_insn_bits(struct comedi_device *dev, return insn->n; } -static void pc263_report_attach(struct comedi_device *dev) -{ - const struct pc263_board *thisboard = comedi_board(dev); - struct pci_dev *pcidev = comedi_to_pci_dev(dev); - char tmpbuf[40]; - - if (is_isa_board(thisboard)) - snprintf(tmpbuf, sizeof(tmpbuf), "(base %#lx) ", dev->iobase); - else if (is_pci_board(thisboard)) - snprintf(tmpbuf, sizeof(tmpbuf), "(pci %s) ", - pci_name(pcidev)); - else - tmpbuf[0] = '\0'; - dev_info(dev->class_dev, "%s %sattached\n", dev->board_name, tmpbuf); -} - -static int pc263_common_attach(struct comedi_device *dev, unsigned long iobase) +static int pc263_attach(struct comedi_device *dev, struct comedi_devconfig *it) { - const struct pc263_board *thisboard = comedi_board(dev); struct comedi_subdevice *s; int ret; - dev->board_name = thisboard->name; - dev->iobase = iobase; + ret = comedi_request_region(dev, it->options[0], PC263_IO_SIZE); + if (ret) + return ret; ret = comedi_alloc_subdevices(dev, 1); if (ret) @@ -223,147 +99,29 @@ static int pc263_common_attach(struct comedi_device *dev, unsigned long iobase) /* read initial relay state */ s->state = inb(dev->iobase) | (inb(dev->iobase + 1) << 8); - pc263_report_attach(dev); - return 1; -} - -static int pc263_pci_common_attach(struct comedi_device *dev, - struct pci_dev *pci_dev) -{ - unsigned long iobase; - int ret; - - comedi_set_hw_dev(dev, &pci_dev->dev); - - ret = comedi_pci_enable(dev); - if (ret) - return ret; - iobase = pci_resource_start(pci_dev, 2); - - return pc263_common_attach(dev, iobase); -} - -/* - * Attach is called by the Comedi core to configure the driver - * for a particular board. If you specified a board_name array - * in the driver structure, dev->board_ptr contains that - * address. - */ -static int pc263_attach(struct comedi_device *dev, struct comedi_devconfig *it) -{ - const struct pc263_board *thisboard = comedi_board(dev); - int ret; - - /* Process options and reserve resources according to bus type. */ - if (is_isa_board(thisboard)) { - ret = comedi_request_region(dev, it->options[0], PC263_IO_SIZE); - if (ret) - return ret; - return pc263_common_attach(dev, dev->iobase); - } else if (is_pci_board(thisboard)) { - struct pci_dev *pci_dev; - - pci_dev = pc263_find_pci_dev(dev, it); - if (!pci_dev) - return -EIO; - return pc263_pci_common_attach(dev, pci_dev); - } else { - dev_err(dev->class_dev, PC263_DRIVER_NAME - ": BUG! cannot determine board type!\n"); - return -EINVAL; - } -} - -/* - * The auto_attach hook is called at PCI probe time via - * comedi_pci_auto_config(). dev->board_ptr is NULL on entry. - * There should be a board entry matching the supplied PCI device. - */ -static int pc263_auto_attach(struct comedi_device *dev, - unsigned long context_unused) -{ - struct pci_dev *pci_dev; - - if (!DO_PCI) - return -EINVAL; - - pci_dev = comedi_to_pci_dev(dev); - dev_info(dev->class_dev, PC263_DRIVER_NAME ": attach pci %s\n", - pci_name(pci_dev)); - dev->board_ptr = pc263_find_pci_board(pci_dev); - if (dev->board_ptr == NULL) { - dev_err(dev->class_dev, "BUG! cannot determine board type!\n"); - return -EINVAL; - } - /* - * Need to 'get' the PCI device to match the 'put' in pc263_detach(). - * TODO: Remove the pci_dev_get() and matching pci_dev_put() once - * support for manual attachment of PCI devices via pc263_attach() - * has been removed. - */ - pci_dev_get(pci_dev); - return pc263_pci_common_attach(dev, pci_dev); + dev_info(dev->class_dev, "%s (base %#lx) attached\n", dev->board_name, + dev->iobase); + return 0; } static void pc263_detach(struct comedi_device *dev) { - const struct pc263_board *thisboard = comedi_board(dev); - - if (!thisboard) - return; - if (is_isa_board(thisboard)) { - if (dev->iobase) - release_region(dev->iobase, PC263_IO_SIZE); - } else if (is_pci_board(thisboard)) { - struct pci_dev *pcidev = comedi_to_pci_dev(dev); - comedi_pci_disable(dev); - if (pcidev) - pci_dev_put(pcidev); - } + if (dev->iobase) + release_region(dev->iobase, PC263_IO_SIZE); } -/* - * The struct comedi_driver structure tells the Comedi core module - * which functions to call to configure/deconfigure (attach/detach) - * the board, and also about the kernel module that contains - * the device code. - */ static struct comedi_driver amplc_pc263_driver = { .driver_name = PC263_DRIVER_NAME, .module = THIS_MODULE, .attach = pc263_attach, - .auto_attach = pc263_auto_attach, .detach = pc263_detach, .board_name = &pc263_boards[0].name, .offset = sizeof(struct pc263_board), .num_names = ARRAY_SIZE(pc263_boards), }; -#if DO_PCI -static DEFINE_PCI_DEVICE_TABLE(pc263_pci_table) = { - { PCI_DEVICE(PCI_VENDOR_ID_AMPLICON, PCI_DEVICE_ID_AMPLICON_PCI263) }, - {0} -}; -MODULE_DEVICE_TABLE(pci, pc263_pci_table); - -static int amplc_pc263_pci_probe(struct pci_dev *dev, - const struct pci_device_id *id) -{ - return comedi_pci_auto_config(dev, &lc_pc263_driver, - id->driver_data); -} - -static struct pci_driver amplc_pc263_pci_driver = { - .name = PC263_DRIVER_NAME, - .id_table = pc263_pci_table, - .probe = &lc_pc263_pci_probe, - .remove = comedi_pci_auto_unconfig, -}; -module_comedi_pci_driver(amplc_pc263_driver, amplc_pc263_pci_driver); -#else module_comedi_driver(amplc_pc263_driver); -#endif MODULE_AUTHOR("Comedi http://www.comedi.org"); -MODULE_DESCRIPTION("Comedi low-level driver"); +MODULE_DESCRIPTION("Comedi driver for Amplicon PC263 relay board"); MODULE_LICENSE("GPL"); diff --git a/drivers/staging/comedi/drivers/amplc_pci263.c b/drivers/staging/comedi/drivers/amplc_pci263.c new file mode 100644 index 000000000000..8b57533bf406 --- /dev/null +++ b/drivers/staging/comedi/drivers/amplc_pci263.c @@ -0,0 +1,127 @@ +/* + comedi/drivers/amplc_pci263.c + Driver for Amplicon PCI263 relay board. + + Copyright (C) 2002 MEV Ltd. + + COMEDI - Linux Control and Measurement Device Interface + Copyright (C) 2000 David A. Schleef + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + +*/ +/* +Driver: amplc_pci263 +Description: Amplicon PCI263 +Author: Ian Abbott +Devices: [Amplicon] PCI263 (amplc_pci263) +Updated: Fri, 12 Apr 2013 15:19:36 +0100 +Status: works + +Configuration options: not applicable, uses PCI auto config + +The board appears as one subdevice, with 16 digital outputs, each +connected to a reed-relay. Relay contacts are closed when output is 1. +The state of the outputs can be read. +*/ + +#include + +#include "../comedidev.h" + +#define PCI263_DRIVER_NAME "amplc_pci263" + +/* PCI263 PCI configuration register information */ +#define PCI_DEVICE_ID_AMPLICON_PCI263 0x000c + +static int pci263_do_insn_bits(struct comedi_device *dev, + struct comedi_subdevice *s, + struct comedi_insn *insn, unsigned int *data) +{ + /* The insn data is a mask in data[0] and the new data + * in data[1], each channel cooresponding to a bit. */ + if (data[0]) { + s->state &= ~data[0]; + s->state |= data[0] & data[1]; + /* Write out the new digital output lines */ + outb(s->state & 0xFF, dev->iobase); + outb(s->state >> 8, dev->iobase + 1); + } + return insn->n; +} + +static int pci263_auto_attach(struct comedi_device *dev, + unsigned long context_unused) +{ + struct pci_dev *pci_dev = comedi_to_pci_dev(dev); + struct comedi_subdevice *s; + int ret; + + ret = comedi_pci_enable(dev); + if (ret) + return ret; + + dev->iobase = pci_resource_start(pci_dev, 2); + ret = comedi_alloc_subdevices(dev, 1); + if (ret) + return ret; + + s = &dev->subdevices[0]; + /* digital output subdevice */ + s->type = COMEDI_SUBD_DO; + s->subdev_flags = SDF_READABLE | SDF_WRITABLE; + s->n_chan = 16; + s->maxdata = 1; + s->range_table = &range_digital; + s->insn_bits = pci263_do_insn_bits; + /* read initial relay state */ + s->state = inb(dev->iobase) | (inb(dev->iobase + 1) << 8); + + dev_info(dev->class_dev, "%s (pci %s) attached\n", dev->board_name, + pci_name(pci_dev)); + return 0; +} + +static struct comedi_driver amplc_pci263_driver = { + .driver_name = PCI263_DRIVER_NAME, + .module = THIS_MODULE, + .auto_attach = pci263_auto_attach, + .detach = comedi_pci_disable, +}; + +static DEFINE_PCI_DEVICE_TABLE(pci263_pci_table) = { + { PCI_DEVICE(PCI_VENDOR_ID_AMPLICON, PCI_DEVICE_ID_AMPLICON_PCI263) }, + {0} +}; +MODULE_DEVICE_TABLE(pci, pci263_pci_table); + +static int amplc_pci263_pci_probe(struct pci_dev *dev, + const struct pci_device_id *id) +{ + return comedi_pci_auto_config(dev, &lc_pci263_driver, + id->driver_data); +} + +static struct pci_driver amplc_pci263_pci_driver = { + .name = PCI263_DRIVER_NAME, + .id_table = pci263_pci_table, + .probe = &lc_pci263_pci_probe, + .remove = comedi_pci_auto_unconfig, +}; +module_comedi_pci_driver(amplc_pci263_driver, amplc_pci263_pci_driver); + +MODULE_AUTHOR("Comedi http://www.comedi.org"); +MODULE_DESCRIPTION("Comedi driver for Amplicon PCI263 relay board"); +MODULE_LICENSE("GPL"); -- 2.39.5