aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/staging/comedi/drivers/contec_pci_dio.c
blob: e3659bd6e85e2a5907830acd86097ed4a25d9086 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*
    comedi/drivers/contec_pci_dio.c

    COMEDI - Linux Control and Measurement Device Interface
    Copyright (C) 2000 David A. Schleef <ds@schleef.org>

    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: contec_pci_dio
Description: Contec PIO1616L digital I/O board
Devices: [Contec] PIO1616L (contec_pci_dio)
Author: Stefano Rivoir <s.rivoir@gts.it>
Updated: Wed, 27 Jun 2007 13:00:06 +0100
Status: works

Configuration Options:
  [0] - PCI bus of device (optional)
  [1] - PCI slot of device (optional)
  If bus/slot is not specified, the first supported
  PCI device found will be used.
*/

#include "../comedidev.h"

#include "comedi_pci.h"

enum contec_model {
	PIO1616L = 0,
};

struct contec_board {
	const char *name;
	int model;
	int in_ports;
	int out_ports;
	int in_offs;
	int out_offs;
	int out_boffs;
};
static const struct contec_board contec_boards[] = {
	{"PIO1616L", PIO1616L, 16, 16, 0, 2, 10},
};

#define PCI_DEVICE_ID_PIO1616L 0x8172
static DEFINE_PCI_DEVICE_TABLE(contec_pci_table) = {
	{ PCI_DEVICE(PCI_VENDOR_ID_CONTEC, PCI_DEVICE_ID_PIO1616L),
		.driver_data = PIO1616L },
	{0}
};

MODULE_DEVICE_TABLE(pci, contec_pci_table);

#define thisboard ((const struct contec_board *)dev->board_ptr)

struct contec_private {
	int data;

	struct pci_dev *pci_dev;

};

#define devpriv ((struct contec_private *)dev->private)

static int contec_attach(struct comedi_device *dev,
			 struct comedi_devconfig *it);
static int contec_detach(struct comedi_device *dev);
static struct comedi_driver driver_contec = {
	.driver_name = "contec_pci_dio",
	.module = THIS_MODULE,
	.attach = contec_attach,
	.detach = contec_detach,
};

/* Classic digital IO */
static int contec_di_insn_bits(struct comedi_device *dev,
			       struct comedi_subdevice *s,
			       struct comedi_insn *insn, unsigned int *data);
static int contec_do_insn_bits(struct comedi_device *dev,
			       struct comedi_subdevice *s,
			       struct comedi_insn *insn, unsigned int *data);

#if 0
static int contec_cmdtest(struct comedi_device *dev, struct comedi_subdevice *s,
			  struct comedi_cmd *cmd);

static int contec_ns_to_timer(unsigned int *ns, int round);
#endif

static int contec_attach(struct comedi_device *dev, struct comedi_devconfig *it)
{
	struct pci_dev *pcidev = NULL;
	struct comedi_subdevice *s;

	printk("comedi%d: contec: ", dev->minor);

	dev->board_name = thisboard->name;

	if (alloc_private(dev, sizeof(struct contec_private)) < 0)
		return -ENOMEM;

	if (alloc_subdevices(dev, 2) < 0)
		return -ENOMEM;

	for_each_pci_dev(pcidev) {
		if (pcidev->vendor == PCI_VENDOR_ID_CONTEC &&
		    pcidev->device == PCI_DEVICE_ID_PIO1616L) {
			if (it->options[0] || it->options[1]) {
				/* Check bus and slot. */
				if (it->options[0] != pcidev->bus->number ||
				    it->options[1] != PCI_SLOT(pcidev->devfn)) {
					continue;
				}
			}
			devpriv->pci_dev = pcidev;
			if (comedi_pci_enable(pcidev, "contec_pci_dio")) {
				printk
				    ("error enabling PCI device and request regions!\n");
				return -EIO;
			}
			dev->iobase = pci_resource_start(pcidev, 0);
			printk(" base addr %lx ", dev->iobase);

			dev->board_ptr = contec_boards + 0;

			s = dev->subdevices + 0;

			s->type = COMEDI_SUBD_DI;
			s->subdev_flags = SDF_READABLE;
			s->n_chan = 16;
			s->maxdata = 1;
			s->range_table = &range_digital;
			s->insn_bits = contec_di_insn_bits;

			s = dev->subdevices + 1;
			s->type = COMEDI_SUBD_DO;
			s->subdev_flags = SDF_WRITABLE;
			s->n_chan = 16;
			s->maxdata = 1;
			s->range_table = &range_digital;
			s->insn_bits = contec_do_insn_bits;

			printk("attached\n");

			return 1;
		}
	}

	printk("card not present!\n");

	return -EIO;
}

static int contec_detach(struct comedi_device *dev)
{
	printk("comedi%d: contec: remove\n", dev->minor);

	if (devpriv && devpriv->pci_dev) {
		if (dev->iobase)
			comedi_pci_disable(devpriv->pci_dev);
		pci_dev_put(devpriv->pci_dev);
	}

	return 0;
}

#if 0
static int contec_cmdtest(struct comedi_device *dev, struct comedi_subdevice *s,
			  struct comedi_cmd *cmd)
{
	printk("contec_cmdtest called\n");
	return 0;
}

static int contec_ns_to_timer(unsigned int *ns, int round)
{
	return *ns;
}
#endif

static int contec_do_insn_bits(struct comedi_device *dev,
			       struct comedi_subdevice *s,
			       struct comedi_insn *insn, unsigned int *data)
{

	dev_dbg(dev->hw_dev, "contec_do_insn_bits called\n");
	dev_dbg(dev->hw_dev, "data: %d %d\n", data[0], data[1]);

	if (insn->n != 2)
		return -EINVAL;

	if (data[0]) {
		s->state &= ~data[0];
		s->state |= data[0] & data[1];
		dev_dbg(dev->hw_dev, "out: %d on %lx\n", s->state,
			dev->iobase + thisboard->out_offs);
		outw(s->state, dev->iobase + thisboard->out_offs);
	}
	return 2;
}

static int contec_di_insn_bits(struct comedi_device *dev,
			       struct comedi_subdevice *s,
			       struct comedi_insn *insn, unsigned int *data)
{

	dev_dbg(dev->hw_dev, "contec_di_insn_bits called\n");
	dev_dbg(dev->hw_dev, "data: %d %d\n", data[0], data[1]);

	if (insn->n != 2)
		return -EINVAL;

	data[1] = inw(dev->iobase + thisboard->in_offs);

	return 2;
}

static int __devinit driver_contec_pci_probe(struct pci_dev *dev,
					     const struct pci_device_id *ent)
{
	return comedi_pci_auto_config(dev, driver_contec.driver_name);
}

static void __devexit driver_contec_pci_remove(struct pci_dev *dev)
{
	comedi_pci_auto_unconfig(dev);
}

static struct pci_driver driver_contec_pci_driver = {
	.id_table = contec_pci_table,
	.probe = &driver_contec_pci_probe,
	.remove = __devexit_p(&driver_contec_pci_remove)
};

static int __init driver_contec_init_module(void)
{
	int retval;

	retval = comedi_driver_register(&driver_contec);
	if (retval < 0)
		return retval;

	driver_contec_pci_driver.name = (char *)driver_contec.driver_name;
	return pci_register_driver(&driver_contec_pci_driver);
}

static void __exit driver_contec_cleanup_module(void)
{
	pci_unregister_driver(&driver_contec_pci_driver);
	comedi_driver_unregister(&driver_contec);
}

module_init(driver_contec_init_module);
module_exit(driver_contec_cleanup_module);

MODULE_AUTHOR("Comedi http://www.comedi.org");
MODULE_DESCRIPTION("Comedi low-level driver");
MODULE_LICENSE("GPL");