Added some ack'ing of b interrupts, and do acks before handling
[comedi.git] / 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     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 Driver: ke_counter.o
25 Description: Driver for Kolter Electronic Counter Card
26 Devices: [Kolter Electronic] PCI Counter Card (ke_counter)
27 Author: Michael Hillmann
28 Updated: 2002.4.11
29 Status: tested
30
31 This driver is a simple driver to read the counter values from
32 Kolter Electronic PCI Counter Card.
33 */
34
35 #include <linux/comedidev.h>
36
37 #include <linux/pci.h>
38
39 #define CNT_DRIVER_NAME         "ke_counter"
40 #define PCI_VENDOR_ID_KOLTER    0x1001
41 #define CNT_CARD_DEVICE_ID      0x0014
42
43 /*-- function prototypes ----------------------------------------------------*/
44
45 static int cnt_attach(comedi_device *dev,comedi_devconfig *it);
46 static int cnt_detach(comedi_device *dev);
47
48 static struct pci_device_id cnt_pci_table[] __devinitdata =
49 {
50   { PCI_VENDOR_ID_KOLTER, CNT_CARD_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
51   { 0 }
52 };
53 MODULE_DEVICE_TABLE(pci, cnt_pci_table);
54
55 /*-- board specification structure ------------------------------------------*/
56
57 typedef struct
58 {
59   char *name;
60   int device_id;
61   int cnt_channel_nbr;
62   int cnt_bits;
63 } cnt_board_struct;
64
65 static cnt_board_struct cnt_boards[] =
66 {
67   {
68     name:            CNT_DRIVER_NAME,
69     device_id:       CNT_CARD_DEVICE_ID,
70     cnt_channel_nbr: 3,
71     cnt_bits:        24
72   }
73 };
74
75 #define cnt_board_nbr (sizeof(cnt_boards)/sizeof(cnt_board_struct))
76
77 /*-- device private structure -----------------------------------------------*/
78
79 typedef struct
80 {
81   struct pci_dev *pcidev;
82 } cnt_device_private;
83
84 #define devpriv ((cnt_device_private *)dev->private)
85
86 static comedi_driver cnt_driver =
87 {
88   driver_name: CNT_DRIVER_NAME,
89   module:      THIS_MODULE,
90   attach:      cnt_attach,
91   detach:      cnt_detach,
92 };
93 COMEDI_INITCLEANUP(cnt_driver);
94
95 /*-- counter write ----------------------------------------------------------*/
96
97 /* This should be used only for resetting the counters; maybe it is better
98    to make a special command 'reset'. */
99 static int cnt_winsn(comedi_device *dev,
100                      comedi_subdevice *s,
101                      comedi_insn *insn,
102                      lsampl_t *data)
103 {
104   int chan = CR_CHAN(insn->chanspec);
105
106   outb((unsigned char)((data[0] >> 24) & 0xff), dev->iobase + chan * 0x20 + 0x10);
107   outb((unsigned char)((data[0] >> 16) & 0xff), dev->iobase + chan * 0x20 + 0x0c);
108   outb((unsigned char)((data[0] >>  8) & 0xff), dev->iobase + chan * 0x20 + 0x08);
109   outb((unsigned char)((data[0] >>  0) & 0xff), dev->iobase + chan * 0x20 + 0x04);
110
111   /* return the number of samples written */
112   return 1;
113 }
114
115 /*-- counter read -----------------------------------------------------------*/
116
117 static int cnt_rinsn(comedi_device *dev,
118                      comedi_subdevice *s,
119                      comedi_insn *insn,
120                      lsampl_t *data)
121 {
122   unsigned char a0, a1, a2, a3, a4;
123   int chan = CR_CHAN(insn->chanspec);
124   int result;
125
126   a0 = inb(dev->iobase + chan * 0x20);
127   a1 = inb(dev->iobase + chan * 0x20 + 0x04);
128   a2 = inb(dev->iobase + chan * 0x20 + 0x08);
129   a3 = inb(dev->iobase + chan * 0x20 + 0x0c);
130   a4 = inb(dev->iobase + chan * 0x20 + 0x10);
131
132   result = (a1 + (a2 * 256) + (a3 * 65536));
133   if (a4 > 0) result = result - s->maxdata;
134
135   *data = (lsampl_t)result;
136
137   /* return the number of samples read */
138   return 1;
139 }
140
141 /*-- attach -----------------------------------------------------------------*/
142
143 static int cnt_attach(comedi_device *dev, comedi_devconfig *it)
144 {
145   comedi_subdevice *subdevice;
146   struct pci_dev   *pci_device;
147   cnt_board_struct *board;
148   unsigned long    io_base;
149   int              error, i;
150
151         /* allocate device private structure */
152         if((error = alloc_private(dev, sizeof(cnt_device_private))) < 0)
153         {
154                 return error;
155         }
156
157   /* Probe the device to determine what device in the series it is. */
158         for(pci_device = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, NULL); pci_device != NULL ; 
159                 pci_device = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, pci_device))
160   {
161     if(pci_device->vendor == PCI_VENDOR_ID_KOLTER)
162     {
163       for(i=0; i<cnt_board_nbr; i++)
164       {
165         if(cnt_boards[i].device_id == pci_device->device)
166         {
167           /* was a particular bus/slot requested? */
168           if((it->options[0] != 0) || (it->options[1] != 0))
169           {
170             /* are we on the wrong bus/slot? */
171             if(pci_device->bus->number != it->options[0] ||
172                PCI_SLOT(pci_device->devfn) != it->options[1])
173             {
174               continue;
175             }
176           }
177
178           dev->board_ptr = cnt_boards + i;
179           board = (cnt_board_struct *)dev->board_ptr;
180           goto found;
181         }
182       }
183     }
184   }
185   printk("comedi%d: no supported board found! (req. bus/slot: %d/%d)\n",
186     dev->minor, it->options[0], it->options[1]);
187   return -EIO;
188
189 found:
190   printk("comedi%d: found %s at PCI bus %d, slot %d\n",dev->minor,
191          board->name, pci_device->bus->number, PCI_SLOT(pci_device->devfn));
192   devpriv->pcidev = pci_device;
193   dev->board_name = board->name;
194
195   /* enable PCI device */
196   if((error = pci_enable_device(pci_device)) < 0)
197   {
198     return error;
199   }
200
201   /* request PCI regions */
202   if((error = pci_request_regions(pci_device, CNT_DRIVER_NAME)) < 0)
203   {
204     return error;
205   }
206
207   /* read register base address [PCI_BASE_ADDRESS #0] */
208   io_base = pci_resource_start(pci_device, 0);
209   dev->iobase = io_base;
210
211   /* allocate the subdevice structures */
212   if((error = alloc_subdevices(dev, 1)) < 0)
213   {
214     return error;
215   }
216
217   subdevice = dev->subdevices + 0;
218   dev->read_subdev = subdevice;
219
220   subdevice->type         = COMEDI_SUBD_COUNTER;
221   subdevice->subdev_flags = SDF_READABLE /* | SDF_COMMON*/;
222   subdevice->n_chan       = board->cnt_channel_nbr;
223   subdevice->maxdata      = (1 << board->cnt_bits) - 1;
224   subdevice->insn_read    = cnt_rinsn;
225   subdevice->insn_write   = cnt_winsn;
226
227   // select 20MHz clock
228   outb(3, dev->iobase + 248);
229
230   // reset all counters
231   outb(0, dev->iobase);
232   outb(0, dev->iobase + 0x20);
233   outb(0, dev->iobase + 0x40);
234
235   printk("comedi%d: " CNT_DRIVER_NAME " attached.\n",dev->minor);
236   return 0;
237 }
238
239 /*-- detach -----------------------------------------------------------------*/
240
241 static int cnt_detach(comedi_device *dev)
242 {
243         if (devpriv && devpriv->pcidev)
244         {
245                 if(dev->iobase)
246                 {
247                         pci_release_regions(devpriv->pcidev);
248                         pci_disable_device(devpriv->pcidev);
249                 }
250                 pci_dev_put(devpriv->pcidev);
251         }
252         printk("comedi%d: " CNT_DRIVER_NAME " remove\n",dev->minor);
253         return 0;
254 }