Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
8255_pci.c
Go to the documentation of this file.
1 /*
2  * COMEDI driver for generic PCI based 8255 digital i/o boards
3  * Copyright (C) 2012 H Hartley Sweeten <[email protected]>
4  *
5  * Based on the tested adl_pci7296 driver written by:
6  * Jon Grierson <[email protected]>
7  * and the experimental cb_pcidio driver written by:
8  * Yoshiya Matsuzaka
9  *
10  * COMEDI - Linux Control and Measurement Device Interface
11  * Copyright (C) 2000 David A. Schleef <[email protected]>
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26  */
27 
28 /*
29 Driver: 8255_pci
30 Description: Generic PCI based 8255 Digital I/O boards
31 Devices: (ADLink) PCI-7224 [adl_pci-7224] - 24 channels
32  (ADLink) PCI-7248 [adl_pci-7248] - 48 channels
33  (ADLink) PCI-7296 [adl_pci-7296] - 96 channels
34  (Measurement Computing) PCI-DIO24 [cb_pci-dio24] - 24 channels
35  (Measurement Computing) PCI-DIO24H [cb_pci-dio24h] - 24 channels
36  (Measurement Computing) PCI-DIO48H [cb_pci-dio48h] - 48 channels
37  (Measurement Computing) PCI-DIO96H [cb_pci-dio96h] - 96 channels
38  (National Instruments) PCI-DIO-96 [ni_pci-dio-96] - 96 channels
39  (National Instruments) PCI-DIO-96B [ni_pci-dio-96b] - 96 channels
40  (National Instruments) PXI-6508 [ni_pxi-6508] - 96 channels
41  (National Instruments) PCI-6503 [ni_pci-6503] - 24 channels
42  (National Instruments) PCI-6503B [ni_pci-6503b] - 24 channels
43  (National Instruments) PCI-6503X [ni_pci-6503x] - 24 channels
44  (National Instruments) PXI-6503 [ni_pxi-6503] - 24 channels
45 Author: H Hartley Sweeten <[email protected]>
46 Updated: Wed, 12 Sep 2012 11:52:01 -0700
47 Status: untested
48 
49 Some of these boards also have an 8254 programmable timer/counter
50 chip. This chip is not currently supported by this driver.
51 
52 Interrupt support for these boards is also not currently supported.
53 
54 Configuration Options: not applicable, uses PCI auto config
55 */
56 
57 #include "../comedidev.h"
58 
59 #include "8255.h"
60 
61 /*
62  * PCI Device ID's supported by this driver
63  */
64 #define PCI_DEVICE_ID_ADLINK_PCI7224 0x7224
65 #define PCI_DEVICE_ID_ADLINK_PCI7248 0x7248
66 #define PCI_DEVICE_ID_ADLINK_PCI7296 0x7296
67 
68 /* ComputerBoards is now known as Measurement Computing */
69 #define PCI_VENDOR_ID_CB 0x1307
70 
71 #define PCI_DEVICE_ID_CB_PCIDIO48H 0x000b
72 #define PCI_DEVICE_ID_CB_PCIDIO24H 0x0014
73 #define PCI_DEVICE_ID_CB_PCIDIO96H 0x0017
74 #define PCI_DEVICE_ID_CB_PCIDIO24 0x0028
75 
76 #define PCI_DEVICE_ID_NI_PCIDIO96 0x0160
77 #define PCI_DEVICE_ID_NI_PCI6503 0x0400
78 #define PCI_DEVICE_ID_NI_PCI6503B 0x1250
79 #define PCI_DEVICE_ID_NI_PXI6508 0x13c0
80 #define PCI_DEVICE_ID_NI_PCIDIO96B 0x1630
81 #define PCI_DEVICE_ID_NI_PCI6503X 0x17d0
82 #define PCI_DEVICE_ID_NI_PXI_6503 0x1800
83 
85  const char *name;
86  unsigned short vendor;
87  unsigned short device;
88  int dio_badr;
89  int is_mmio;
90  int n_8255;
91 };
92 
93 static const struct pci_8255_boardinfo pci_8255_boards[] = {
94  {
95  .name = "adl_pci-7224",
96  .vendor = PCI_VENDOR_ID_ADLINK,
98  .dio_badr = 2,
99  .n_8255 = 1,
100  }, {
101  .name = "adl_pci-7248",
102  .vendor = PCI_VENDOR_ID_ADLINK,
104  .dio_badr = 2,
105  .n_8255 = 2,
106  }, {
107  .name = "adl_pci-7296",
108  .vendor = PCI_VENDOR_ID_ADLINK,
110  .dio_badr = 2,
111  .n_8255 = 4,
112  }, {
113  .name = "cb_pci-dio24",
114  .vendor = PCI_VENDOR_ID_CB,
115  .device = PCI_DEVICE_ID_CB_PCIDIO24,
116  .dio_badr = 2,
117  .n_8255 = 1,
118  }, {
119  .name = "cb_pci-dio24h",
120  .vendor = PCI_VENDOR_ID_CB,
121  .device = PCI_DEVICE_ID_CB_PCIDIO24H,
122  .dio_badr = 2,
123  .n_8255 = 1,
124  }, {
125  .name = "cb_pci-dio48h",
126  .vendor = PCI_VENDOR_ID_CB,
127  .device = PCI_DEVICE_ID_CB_PCIDIO48H,
128  .dio_badr = 1,
129  .n_8255 = 2,
130  }, {
131  .name = "cb_pci-dio96h",
132  .vendor = PCI_VENDOR_ID_CB,
133  .device = PCI_DEVICE_ID_CB_PCIDIO96H,
134  .dio_badr = 2,
135  .n_8255 = 4,
136  }, {
137  .name = "ni_pci-dio-96",
138  .vendor = PCI_VENDOR_ID_NI,
139  .device = PCI_DEVICE_ID_NI_PCIDIO96,
140  .dio_badr = 1,
141  .is_mmio = 1,
142  .n_8255 = 4,
143  }, {
144  .name = "ni_pci-dio-96b",
145  .vendor = PCI_VENDOR_ID_NI,
146  .device = PCI_DEVICE_ID_NI_PCIDIO96B,
147  .dio_badr = 1,
148  .is_mmio = 1,
149  .n_8255 = 4,
150  }, {
151  .name = "ni_pxi-6508",
152  .vendor = PCI_VENDOR_ID_NI,
153  .device = PCI_DEVICE_ID_NI_PXI6508,
154  .dio_badr = 1,
155  .is_mmio = 1,
156  .n_8255 = 4,
157  }, {
158  .name = "ni_pci-6503",
159  .vendor = PCI_VENDOR_ID_NI,
160  .device = PCI_DEVICE_ID_NI_PCI6503,
161  .dio_badr = 1,
162  .is_mmio = 1,
163  .n_8255 = 1,
164  }, {
165  .name = "ni_pci-6503b",
166  .vendor = PCI_VENDOR_ID_NI,
167  .device = PCI_DEVICE_ID_NI_PCI6503B,
168  .dio_badr = 1,
169  .is_mmio = 1,
170  .n_8255 = 1,
171  }, {
172  .name = "ni_pci-6503x",
173  .vendor = PCI_VENDOR_ID_NI,
174  .device = PCI_DEVICE_ID_NI_PCI6503X,
175  .dio_badr = 1,
176  .is_mmio = 1,
177  .n_8255 = 1,
178  }, {
179  .name = "ni_pxi-6503",
180  .vendor = PCI_VENDOR_ID_NI,
181  .device = PCI_DEVICE_ID_NI_PXI_6503,
182  .dio_badr = 1,
183  .is_mmio = 1,
184  .n_8255 = 1,
185  },
186 };
187 
190 };
191 
192 static int pci_8255_mmio(int dir, int port, int data, unsigned long iobase)
193 {
194  void __iomem *mmio_base = (void __iomem *)iobase;
195 
196  if (dir) {
197  writeb(data, mmio_base + port);
198  return 0;
199  } else {
200  return readb(mmio_base + port);
201  }
202 }
203 
204 static const void *pci_8255_find_boardinfo(struct comedi_device *dev,
205  struct pci_dev *pcidev)
206 {
207  const struct pci_8255_boardinfo *board;
208  int i;
209 
210  for (i = 0; i < ARRAY_SIZE(pci_8255_boards); i++) {
211  board = &pci_8255_boards[i];
212  if (pcidev->vendor == board->vendor &&
213  pcidev->device == board->device)
214  return board;
215  }
216  return NULL;
217 }
218 
219 static int pci_8255_attach_pci(struct comedi_device *dev,
220  struct pci_dev *pcidev)
221 {
222  const struct pci_8255_boardinfo *board;
223  struct pci_8255_private *devpriv;
224  struct comedi_subdevice *s;
226  unsigned long len;
227  int ret;
228  int i;
229 
230  comedi_set_hw_dev(dev, &pcidev->dev);
231 
232  board = pci_8255_find_boardinfo(dev, pcidev);
233  if (!board)
234  return -ENODEV;
235  dev->board_ptr = board;
236  dev->board_name = board->name;
237 
238  ret = alloc_private(dev, sizeof(*devpriv));
239  if (ret < 0)
240  return ret;
241  devpriv = dev->private;
242 
243  ret = comedi_pci_enable(pcidev, dev->board_name);
244  if (ret)
245  return ret;
246  iobase = pci_resource_start(pcidev, board->dio_badr);
247  len = pci_resource_len(pcidev, board->dio_badr);
248 
249  if (board->is_mmio) {
250  devpriv->mmio_base = ioremap(iobase, len);
251  if (!devpriv->mmio_base)
252  return -ENOMEM;
253  }
254  dev->iobase = iobase;
255 
256  /*
257  * One, two, or four subdevices are setup by this driver depending
258  * on the number of channels provided by the board. Each subdevice
259  * has 24 channels supported by the 8255 module.
260  */
261  ret = comedi_alloc_subdevices(dev, board->n_8255);
262  if (ret)
263  return ret;
264 
265  for (i = 0; i < board->n_8255; i++) {
266  s = &dev->subdevices[i];
267  if (board->is_mmio) {
268  iobase = (unsigned long)(devpriv->mmio_base + (i * 4));
269  ret = subdev_8255_init(dev, s, pci_8255_mmio, iobase);
270  } else {
271  iobase = dev->iobase + (i * 4);
272  ret = subdev_8255_init(dev, s, NULL, iobase);
273  }
274  if (ret)
275  return ret;
276  }
277 
278  dev_info(dev->class_dev, "%s attached (%d digital i/o channels)\n",
279  dev->board_name, board->n_8255 * 24);
280 
281  return 0;
282 }
283 
284 static void pci_8255_detach(struct comedi_device *dev)
285 {
286  struct pci_dev *pcidev = comedi_to_pci_dev(dev);
287  const struct pci_8255_boardinfo *board = comedi_board(dev);
288  struct pci_8255_private *devpriv = dev->private;
289  struct comedi_subdevice *s;
290  int i;
291 
292  if (!board || !devpriv)
293  return;
294  if (dev->subdevices) {
295  for (i = 0; i < board->n_8255; i++) {
296  s = &dev->subdevices[i];
297  subdev_8255_cleanup(dev, s);
298  }
299  }
300  if (pcidev) {
301  if (devpriv->mmio_base)
302  iounmap(devpriv->mmio_base);
303  if (dev->iobase)
304  comedi_pci_disable(pcidev);
305  }
306 }
307 
308 static struct comedi_driver pci_8255_driver = {
309  .driver_name = "8255_pci",
310  .module = THIS_MODULE,
311  .attach_pci = pci_8255_attach_pci,
312  .detach = pci_8255_detach,
313 };
314 
315 static int __devinit pci_8255_pci_probe(struct pci_dev *dev,
316  const struct pci_device_id *ent)
317 {
318  return comedi_pci_auto_config(dev, &pci_8255_driver);
319 }
320 
321 static void __devexit pci_8255_pci_remove(struct pci_dev *dev)
322 {
324 }
325 
326 static DEFINE_PCI_DEVICE_TABLE(pci_8255_pci_table) = {
341  { 0 }
342 };
343 MODULE_DEVICE_TABLE(pci, pci_8255_pci_table);
344 
345 static struct pci_driver pci_8255_pci_driver = {
346  .name = "8255_pci",
347  .id_table = pci_8255_pci_table,
348  .probe = pci_8255_pci_probe,
349  .remove = __devexit_p(pci_8255_pci_remove),
350 };
351 module_comedi_pci_driver(pci_8255_driver, pci_8255_pci_driver);
352 
353 MODULE_DESCRIPTION("COMEDI - Generic PCI based 8255 Digital I/O boards");
354 MODULE_AUTHOR("Comedi http://www.comedi.org");
355 MODULE_LICENSE("GPL");