]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/comedi/drivers/ke_counter.c
Merge branch 'core-printk-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[karo-tx-linux.git] / drivers / staging / comedi / drivers / ke_counter.c
1 /*
2     comedi/drivers/ke_counter.c
3     Comedi driver for Kolter-Electronic PCI Counter 1 Card
4
5     COMEDI - Linux Control and Measurement Device Interface
6     Copyright (C) 2000 David A. Schleef <ds@schleef.org>
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 /*
19 Driver: ke_counter
20 Description: Driver for Kolter Electronic Counter Card
21 Devices: [Kolter Electronic] PCI Counter Card (ke_counter)
22 Author: Michael Hillmann
23 Updated: Mon, 14 Apr 2008 15:42:42 +0100
24 Status: tested
25
26 Configuration Options: not applicable, uses PCI auto config
27
28 This driver is a simple driver to read the counter values from
29 Kolter Electronic PCI Counter Card.
30 */
31
32 #include <linux/pci.h>
33
34 #include "../comedidev.h"
35
36 #define CNT_CARD_DEVICE_ID      0x0014
37
38 /*-- counter write ----------------------------------------------------------*/
39
40 /* This should be used only for resetting the counters; maybe it is better
41    to make a special command 'reset'. */
42 static int cnt_winsn(struct comedi_device *dev,
43                      struct comedi_subdevice *s, struct comedi_insn *insn,
44                      unsigned int *data)
45 {
46         int chan = CR_CHAN(insn->chanspec);
47
48         outb((unsigned char)((data[0] >> 24) & 0xff),
49              dev->iobase + chan * 0x20 + 0x10);
50         outb((unsigned char)((data[0] >> 16) & 0xff),
51              dev->iobase + chan * 0x20 + 0x0c);
52         outb((unsigned char)((data[0] >> 8) & 0xff),
53              dev->iobase + chan * 0x20 + 0x08);
54         outb((unsigned char)((data[0] >> 0) & 0xff),
55              dev->iobase + chan * 0x20 + 0x04);
56
57         /* return the number of samples written */
58         return 1;
59 }
60
61 /*-- counter read -----------------------------------------------------------*/
62
63 static int cnt_rinsn(struct comedi_device *dev,
64                      struct comedi_subdevice *s, struct comedi_insn *insn,
65                      unsigned int *data)
66 {
67         unsigned char a0, a1, a2, a3, a4;
68         int chan = CR_CHAN(insn->chanspec);
69         int result;
70
71         a0 = inb(dev->iobase + chan * 0x20);
72         a1 = inb(dev->iobase + chan * 0x20 + 0x04);
73         a2 = inb(dev->iobase + chan * 0x20 + 0x08);
74         a3 = inb(dev->iobase + chan * 0x20 + 0x0c);
75         a4 = inb(dev->iobase + chan * 0x20 + 0x10);
76
77         result = (a1 + (a2 * 256) + (a3 * 65536));
78         if (a4 > 0)
79                 result = result - s->maxdata;
80
81         *data = (unsigned int)result;
82
83         /* return the number of samples read */
84         return 1;
85 }
86
87 static int cnt_auto_attach(struct comedi_device *dev,
88                                      unsigned long context_unused)
89 {
90         struct pci_dev *pcidev = comedi_to_pci_dev(dev);
91         struct comedi_subdevice *s;
92         int ret;
93
94         ret = comedi_pci_enable(dev);
95         if (ret)
96                 return ret;
97         dev->iobase = pci_resource_start(pcidev, 0);
98
99         ret = comedi_alloc_subdevices(dev, 1);
100         if (ret)
101                 return ret;
102
103         s = &dev->subdevices[0];
104         dev->read_subdev = s;
105
106         s->type = COMEDI_SUBD_COUNTER;
107         s->subdev_flags = SDF_READABLE /* | SDF_COMMON */ ;
108         s->n_chan = 3;
109         s->maxdata = 0x00ffffff;
110         s->insn_read = cnt_rinsn;
111         s->insn_write = cnt_winsn;
112
113         /*  select 20MHz clock */
114         outb(3, dev->iobase + 248);
115
116         /*  reset all counters */
117         outb(0, dev->iobase);
118         outb(0, dev->iobase + 0x20);
119         outb(0, dev->iobase + 0x40);
120
121         dev_info(dev->class_dev, "%s: %s attached\n",
122                 dev->driver->driver_name, dev->board_name);
123
124         return 0;
125 }
126
127 static struct comedi_driver ke_counter_driver = {
128         .driver_name    = "ke_counter",
129         .module         = THIS_MODULE,
130         .auto_attach    = cnt_auto_attach,
131         .detach         = comedi_pci_disable,
132 };
133
134 static int ke_counter_pci_probe(struct pci_dev *dev,
135                                 const struct pci_device_id *id)
136 {
137         return comedi_pci_auto_config(dev, &ke_counter_driver,
138                                       id->driver_data);
139 }
140
141 static DEFINE_PCI_DEVICE_TABLE(ke_counter_pci_table) = {
142         { PCI_DEVICE(PCI_VENDOR_ID_KOLTER, CNT_CARD_DEVICE_ID) },
143         { 0 }
144 };
145 MODULE_DEVICE_TABLE(pci, ke_counter_pci_table);
146
147 static struct pci_driver ke_counter_pci_driver = {
148         .name           = "ke_counter",
149         .id_table       = ke_counter_pci_table,
150         .probe          = ke_counter_pci_probe,
151         .remove         = comedi_pci_auto_unconfig,
152 };
153 module_comedi_pci_driver(ke_counter_driver, ke_counter_pci_driver);
154
155 MODULE_AUTHOR("Comedi http://www.comedi.org");
156 MODULE_DESCRIPTION("Comedi low-level driver");
157 MODULE_LICENSE("GPL");