2 comedi/drivers/amplc_pc236.c
3 Driver for Amplicon PC36AT and PCI236 DIO boards.
5 Copyright (C) 2002 MEV Ltd. <http://www.mev.co.uk/>
7 COMEDI - Linux Control and Measurement Device Interface
8 Copyright (C) 2000 David A. Schleef <ds@schleef.org>
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.
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.
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.
27 Description: Amplicon PC36AT, PCI236
28 Author: Ian Abbott <abbotti@mev.co.uk>
29 Devices: [Amplicon] PC36AT (pc36at), PCI236 (pci236 or amplc_pc236)
30 Updated: Wed, 01 Apr 2009 15:41:25 +0100
33 Configuration options - PC36AT:
34 [0] - I/O port base address
37 Configuration options - PCI236:
38 [0] - PCI bus of device (optional)
39 [1] - PCI slot of device (optional)
40 If bus/slot is not specified, the first available PCI device will be
43 The PC36AT ISA board and PCI236 PCI board have a single 8255 appearing
46 Subdevice 1 pretends to be a digital input device, but it always returns
47 0 when read. However, if you run a command with scan_begin_src=TRIG_EXT,
48 a rising edge on port C bit 3 acts as an external trigger, which can be
49 used to wake up tasks. This is like the comedi_parport device, but the
50 only way to physically disable the interrupt on the PC36AT is to remove
51 the IRQ jumper. If no interrupt is connected, then subdevice 1 is
55 #include <linux/pci.h>
56 #include <linux/interrupt.h>
58 #include "../comedidev.h"
60 #include "comedi_fc.h"
64 #define PC236_DRIVER_NAME "amplc_pc236"
66 #define DO_ISA IS_ENABLED(CONFIG_COMEDI_AMPLC_PC236_ISA)
67 #define DO_PCI IS_ENABLED(CONFIG_COMEDI_AMPLC_PC236_PCI)
69 /* PCI236 PCI configuration register information */
70 #define PCI_DEVICE_ID_AMPLICON_PCI236 0x0009
71 #define PCI_DEVICE_ID_INVALID 0xffff
73 /* PC36AT / PCI236 registers */
75 #define PC236_IO_SIZE 4
76 #define PC236_LCR_IO_SIZE 128
78 /* Disable, and clear, interrupts */
79 #define PCI236_INTR_DISABLE (PLX9052_INTCSR_LI1POL | \
80 PLX9052_INTCSR_LI2POL | \
81 PLX9052_INTCSR_LI1SEL | \
82 PLX9052_INTCSR_LI1CLRINT)
84 /* Enable, and clear, interrupts */
85 #define PCI236_INTR_ENABLE (PLX9052_INTCSR_LI1ENAB | \
86 PLX9052_INTCSR_LI1POL | \
87 PLX9052_INTCSR_LI2POL | \
88 PLX9052_INTCSR_PCIENAB | \
89 PLX9052_INTCSR_LI1SEL | \
90 PLX9052_INTCSR_LI1CLRINT)
93 * Board descriptions for Amplicon PC36AT and PCI236.
96 enum pc236_bustype { isa_bustype, pci_bustype };
97 enum pc236_model { pc36at_model, pci236_model, anypci_model };
101 unsigned short devid;
102 enum pc236_bustype bustype;
103 enum pc236_model model;
105 static const struct pc236_board pc236_boards[] = {
109 .bustype = isa_bustype,
110 .model = pc36at_model,
116 .devid = PCI_DEVICE_ID_AMPLICON_PCI236,
117 .bustype = pci_bustype,
118 .model = pci236_model,
121 .name = PC236_DRIVER_NAME,
122 .devid = PCI_DEVICE_ID_INVALID,
123 .bustype = pci_bustype,
124 .model = anypci_model, /* wildcard */
129 /* this structure is for data unique to this hardware driver. If
130 several hardware drivers keep similar information in this structure,
131 feel free to suggest moving the variable to the struct comedi_device struct.
133 struct pc236_private {
134 unsigned long lcr_iobase; /* PLX PCI9052 config registers in PCIBAR1 */
138 /* test if ISA supported and this is an ISA board */
139 static inline bool is_isa_board(const struct pc236_board *board)
141 return DO_ISA && board->bustype == isa_bustype;
144 /* test if PCI supported and this is a PCI board */
145 static inline bool is_pci_board(const struct pc236_board *board)
147 return DO_PCI && board->bustype == pci_bustype;
151 * This function looks for a board matching the supplied PCI device.
153 static const struct pc236_board *pc236_find_pci_board(struct pci_dev *pci_dev)
157 for (i = 0; i < ARRAY_SIZE(pc236_boards); i++)
158 if (is_pci_board(&pc236_boards[i]) &&
159 pci_dev->device == pc236_boards[i].devid)
160 return &pc236_boards[i];
165 * This function looks for a PCI device matching the requested board name,
168 static struct pci_dev *pc236_find_pci_dev(struct comedi_device *dev,
169 struct comedi_devconfig *it)
171 const struct pc236_board *thisboard = comedi_board(dev);
172 struct pci_dev *pci_dev = NULL;
173 int bus = it->options[0];
174 int slot = it->options[1];
176 for_each_pci_dev(pci_dev) {
178 if (bus != pci_dev->bus->number ||
179 slot != PCI_SLOT(pci_dev->devfn))
182 if (pci_dev->vendor != PCI_VENDOR_ID_AMPLICON)
185 if (thisboard->model == anypci_model) {
186 /* Wildcard board matches any supported PCI board. */
187 const struct pc236_board *foundboard;
189 foundboard = pc236_find_pci_board(pci_dev);
190 if (foundboard == NULL)
192 /* Replace wildcard board_ptr. */
193 dev->board_ptr = foundboard;
195 /* Match specific model name. */
196 if (pci_dev->device != thisboard->devid)
201 dev_err(dev->class_dev,
202 "No supported board found! (req. bus %d, slot %d)\n",
208 * This function is called to mark the interrupt as disabled (no command
209 * configured on subdevice 1) and to physically disable the interrupt
210 * (not possible on the PC36AT, except by removing the IRQ jumper!).
212 static void pc236_intr_disable(struct comedi_device *dev)
214 const struct pc236_board *thisboard = comedi_board(dev);
215 struct pc236_private *devpriv = dev->private;
218 spin_lock_irqsave(&dev->spinlock, flags);
219 devpriv->enable_irq = 0;
220 if (is_pci_board(thisboard))
221 outl(PCI236_INTR_DISABLE, devpriv->lcr_iobase + PLX9052_INTCSR);
222 spin_unlock_irqrestore(&dev->spinlock, flags);
226 * This function is called to mark the interrupt as enabled (a command
227 * configured on subdevice 1) and to physically enable the interrupt
228 * (not possible on the PC36AT, except by (re)connecting the IRQ jumper!).
230 static void pc236_intr_enable(struct comedi_device *dev)
232 const struct pc236_board *thisboard = comedi_board(dev);
233 struct pc236_private *devpriv = dev->private;
236 spin_lock_irqsave(&dev->spinlock, flags);
237 devpriv->enable_irq = 1;
238 if (is_pci_board(thisboard))
239 outl(PCI236_INTR_ENABLE, devpriv->lcr_iobase + PLX9052_INTCSR);
240 spin_unlock_irqrestore(&dev->spinlock, flags);
244 * This function is called when an interrupt occurs to check whether
245 * the interrupt has been marked as enabled and was generated by the
246 * board. If so, the function prepares the hardware for the next
248 * Returns 0 if the interrupt should be ignored.
250 static int pc236_intr_check(struct comedi_device *dev)
252 const struct pc236_board *thisboard = comedi_board(dev);
253 struct pc236_private *devpriv = dev->private;
258 spin_lock_irqsave(&dev->spinlock, flags);
259 if (devpriv->enable_irq) {
261 if (is_pci_board(thisboard)) {
262 intcsr = inl(devpriv->lcr_iobase + PLX9052_INTCSR);
263 if (!(intcsr & PLX9052_INTCSR_LI1STAT)) {
266 /* Clear interrupt and keep it enabled. */
267 outl(PCI236_INTR_ENABLE,
268 devpriv->lcr_iobase + PLX9052_INTCSR);
272 spin_unlock_irqrestore(&dev->spinlock, flags);
278 * Input from subdevice 1.
279 * Copied from the comedi_parport driver.
281 static int pc236_intr_insn(struct comedi_device *dev,
282 struct comedi_subdevice *s, struct comedi_insn *insn,
290 * Subdevice 1 command test.
291 * Copied from the comedi_parport driver.
293 static int pc236_intr_cmdtest(struct comedi_device *dev,
294 struct comedi_subdevice *s,
295 struct comedi_cmd *cmd)
299 /* Step 1 : check if triggers are trivially valid */
301 err |= cfc_check_trigger_src(&cmd->start_src, TRIG_NOW);
302 err |= cfc_check_trigger_src(&cmd->scan_begin_src, TRIG_EXT);
303 err |= cfc_check_trigger_src(&cmd->convert_src, TRIG_FOLLOW);
304 err |= cfc_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
305 err |= cfc_check_trigger_src(&cmd->stop_src, TRIG_NONE);
310 /* Step 2a : make sure trigger sources are unique */
311 /* Step 2b : and mutually compatible */
316 /* Step 3: check it arguments are trivially valid */
318 err |= cfc_check_trigger_arg_is(&cmd->start_arg, 0);
319 err |= cfc_check_trigger_arg_is(&cmd->scan_begin_arg, 0);
320 err |= cfc_check_trigger_arg_is(&cmd->convert_arg, 0);
321 err |= cfc_check_trigger_arg_is(&cmd->scan_end_arg, 1);
322 err |= cfc_check_trigger_arg_is(&cmd->stop_arg, 0);
327 /* step 4: ignored */
336 * Subdevice 1 command.
338 static int pc236_intr_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
340 pc236_intr_enable(dev);
346 * Subdevice 1 cancel command.
348 static int pc236_intr_cancel(struct comedi_device *dev,
349 struct comedi_subdevice *s)
351 pc236_intr_disable(dev);
357 * Interrupt service routine.
358 * Based on the comedi_parport driver.
360 static irqreturn_t pc236_interrupt(int irq, void *d)
362 struct comedi_device *dev = d;
363 struct comedi_subdevice *s = &dev->subdevices[1];
366 handled = pc236_intr_check(dev);
367 if (dev->attached && handled) {
368 comedi_buf_put(s->async, 0);
369 s->async->events |= COMEDI_CB_BLOCK | COMEDI_CB_EOS;
370 comedi_event(dev, s);
372 return IRQ_RETVAL(handled);
375 static void pc236_report_attach(struct comedi_device *dev, unsigned int irq)
377 const struct pc236_board *thisboard = comedi_board(dev);
378 struct pci_dev *pcidev = comedi_to_pci_dev(dev);
382 if (is_isa_board(thisboard))
383 tmplen = scnprintf(tmpbuf, sizeof(tmpbuf),
384 "(base %#lx) ", dev->iobase);
385 else if (is_pci_board(thisboard))
386 tmplen = scnprintf(tmpbuf, sizeof(tmpbuf),
387 "(pci %s) ", pci_name(pcidev));
391 tmplen += scnprintf(&tmpbuf[tmplen], sizeof(tmpbuf) - tmplen,
393 (dev->irq ? "" : " UNAVAILABLE"));
395 tmplen += scnprintf(&tmpbuf[tmplen], sizeof(tmpbuf) - tmplen,
397 dev_info(dev->class_dev, "%s %sattached\n",
398 dev->board_name, tmpbuf);
401 static int pc236_common_attach(struct comedi_device *dev, unsigned long iobase,
402 unsigned int irq, unsigned long req_irq_flags)
404 const struct pc236_board *thisboard = comedi_board(dev);
405 struct comedi_subdevice *s;
408 dev->board_name = thisboard->name;
409 dev->iobase = iobase;
411 ret = comedi_alloc_subdevices(dev, 2);
415 s = &dev->subdevices[0];
416 /* digital i/o subdevice (8255) */
417 ret = subdev_8255_init(dev, s, NULL, iobase);
419 dev_err(dev->class_dev, "error! out of memory!\n");
422 s = &dev->subdevices[1];
423 dev->read_subdev = s;
424 s->type = COMEDI_SUBD_UNUSED;
425 pc236_intr_disable(dev);
427 if (request_irq(irq, pc236_interrupt, req_irq_flags,
428 PC236_DRIVER_NAME, dev) >= 0) {
430 s->type = COMEDI_SUBD_DI;
431 s->subdev_flags = SDF_READABLE | SDF_CMD_READ;
434 s->range_table = &range_digital;
435 s->insn_bits = pc236_intr_insn;
436 s->do_cmdtest = pc236_intr_cmdtest;
437 s->do_cmd = pc236_intr_cmd;
438 s->cancel = pc236_intr_cancel;
441 pc236_report_attach(dev, irq);
445 static int pc236_pci_common_attach(struct comedi_device *dev,
446 struct pci_dev *pci_dev)
448 struct pc236_private *devpriv = dev->private;
449 unsigned long iobase;
452 comedi_set_hw_dev(dev, &pci_dev->dev);
454 ret = comedi_pci_enable(dev);
458 devpriv->lcr_iobase = pci_resource_start(pci_dev, 1);
459 iobase = pci_resource_start(pci_dev, 2);
460 return pc236_common_attach(dev, iobase, pci_dev->irq, IRQF_SHARED);
464 * Attach is called by the Comedi core to configure the driver
465 * for a particular board. If you specified a board_name array
466 * in the driver structure, dev->board_ptr contains that
469 static int pc236_attach(struct comedi_device *dev, struct comedi_devconfig *it)
471 const struct pc236_board *thisboard = comedi_board(dev);
472 struct pc236_private *devpriv;
475 devpriv = kzalloc(sizeof(*devpriv), GFP_KERNEL);
478 dev->private = devpriv;
480 /* Process options according to bus type. */
481 if (is_isa_board(thisboard)) {
482 ret = comedi_request_region(dev, it->options[0], PC236_IO_SIZE);
486 return pc236_common_attach(dev, dev->iobase, it->options[1], 0);
487 } else if (is_pci_board(thisboard)) {
488 struct pci_dev *pci_dev;
490 pci_dev = pc236_find_pci_dev(dev, it);
493 return pc236_pci_common_attach(dev, pci_dev);
495 dev_err(dev->class_dev, PC236_DRIVER_NAME
496 ": BUG! cannot determine board type!\n");
502 * The auto_attach hook is called at PCI probe time via
503 * comedi_pci_auto_config(). dev->board_ptr is NULL on entry.
504 * There should be a board entry matching the supplied PCI device.
506 static int pc236_auto_attach(struct comedi_device *dev,
507 unsigned long context_unused)
509 struct pci_dev *pci_dev = comedi_to_pci_dev(dev);
510 struct pc236_private *devpriv;
515 dev_info(dev->class_dev, PC236_DRIVER_NAME ": attach pci %s\n",
518 devpriv = kzalloc(sizeof(*devpriv), GFP_KERNEL);
521 dev->private = devpriv;
523 dev->board_ptr = pc236_find_pci_board(pci_dev);
524 if (dev->board_ptr == NULL) {
525 dev_err(dev->class_dev, "BUG! cannot determine board type!\n");
529 * Need to 'get' the PCI device to match the 'put' in pc236_detach().
530 * TODO: Remove the pci_dev_get() and matching pci_dev_put() once
531 * support for manual attachment of PCI devices via pc236_attach()
534 pci_dev_get(pci_dev);
535 return pc236_pci_common_attach(dev, pci_dev);
538 static void pc236_detach(struct comedi_device *dev)
540 const struct pc236_board *thisboard = comedi_board(dev);
545 pc236_intr_disable(dev);
546 comedi_spriv_free(dev, 0);
547 if (is_isa_board(thisboard)) {
548 comedi_legacy_detach(dev);
549 } else if (is_pci_board(thisboard)) {
550 struct pci_dev *pcidev = comedi_to_pci_dev(dev);
552 free_irq(dev->irq, dev);
553 comedi_pci_disable(dev);
560 * The struct comedi_driver structure tells the Comedi core module
561 * which functions to call to configure/deconfigure (attach/detach)
562 * the board, and also about the kernel module that contains
565 static struct comedi_driver amplc_pc236_driver = {
566 .driver_name = PC236_DRIVER_NAME,
567 .module = THIS_MODULE,
568 .attach = pc236_attach,
569 .auto_attach = pc236_auto_attach,
570 .detach = pc236_detach,
571 .board_name = &pc236_boards[0].name,
572 .offset = sizeof(struct pc236_board),
573 .num_names = ARRAY_SIZE(pc236_boards),
577 static DEFINE_PCI_DEVICE_TABLE(pc236_pci_table) = {
578 { PCI_DEVICE(PCI_VENDOR_ID_AMPLICON, PCI_DEVICE_ID_AMPLICON_PCI236) },
582 MODULE_DEVICE_TABLE(pci, pc236_pci_table);
584 static int amplc_pc236_pci_probe(struct pci_dev *dev,
585 const struct pci_device_id *id)
587 return comedi_pci_auto_config(dev, &lc_pc236_driver,
591 static struct pci_driver amplc_pc236_pci_driver = {
592 .name = PC236_DRIVER_NAME,
593 .id_table = pc236_pci_table,
594 .probe = &lc_pc236_pci_probe,
595 .remove = comedi_pci_auto_unconfig,
598 module_comedi_pci_driver(amplc_pc236_driver, amplc_pc236_pci_driver);
600 module_comedi_driver(amplc_pc236_driver);
603 MODULE_AUTHOR("Comedi http://www.comedi.org");
604 MODULE_DESCRIPTION("Comedi low-level driver");
605 MODULE_LICENSE("GPL");