Merge branch 'x86-kaslr-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[cascardo/linux.git] / drivers / staging / comedi / drivers / dt2817.c
1 /*
2     comedi/drivers/dt2817.c
3     Hardware driver for Data Translation DT2817
4
5     COMEDI - Linux Control and Measurement Device Interface
6     Copyright (C) 1998 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: dt2817
20 Description: Data Translation DT2817
21 Author: ds
22 Status: complete
23 Devices: [Data Translation] DT2817 (dt2817)
24
25 A very simple digital I/O card.  Four banks of 8 lines, each bank
26 is configurable for input or output.  One wonders why it takes a
27 50 page manual to describe this thing.
28
29 The driver (which, btw, is much less than 50 pages) has 1 subdevice
30 with 32 channels, configurable in groups of 8.
31
32 Configuration options:
33   [0] - I/O port base base address
34 */
35
36 #include <linux/module.h>
37 #include "../comedidev.h"
38
39 #define DT2817_SIZE 5
40
41 #define DT2817_CR 0
42 #define DT2817_DATA 1
43
44 static int dt2817_dio_insn_config(struct comedi_device *dev,
45                                   struct comedi_subdevice *s,
46                                   struct comedi_insn *insn,
47                                   unsigned int *data)
48 {
49         unsigned int chan = CR_CHAN(insn->chanspec);
50         unsigned int oe = 0;
51         unsigned int mask;
52         int ret;
53
54         if (chan < 8)
55                 mask = 0x000000ff;
56         else if (chan < 16)
57                 mask = 0x0000ff00;
58         else if (chan < 24)
59                 mask = 0x00ff0000;
60         else
61                 mask = 0xff000000;
62
63         ret = comedi_dio_insn_config(dev, s, insn, data, mask);
64         if (ret)
65                 return ret;
66
67         if (s->io_bits & 0x000000ff)
68                 oe |= 0x1;
69         if (s->io_bits & 0x0000ff00)
70                 oe |= 0x2;
71         if (s->io_bits & 0x00ff0000)
72                 oe |= 0x4;
73         if (s->io_bits & 0xff000000)
74                 oe |= 0x8;
75
76         outb(oe, dev->iobase + DT2817_CR);
77
78         return insn->n;
79 }
80
81 static int dt2817_dio_insn_bits(struct comedi_device *dev,
82                                 struct comedi_subdevice *s,
83                                 struct comedi_insn *insn,
84                                 unsigned int *data)
85 {
86         unsigned long iobase = dev->iobase + DT2817_DATA;
87         unsigned int mask;
88         unsigned int val;
89
90         mask = comedi_dio_update_state(s, data);
91         if (mask) {
92                 if (mask & 0x000000ff)
93                         outb(s->state & 0xff, iobase + 0);
94                 if (mask & 0x0000ff00)
95                         outb((s->state >> 8) & 0xff, iobase + 1);
96                 if (mask & 0x00ff0000)
97                         outb((s->state >> 16) & 0xff, iobase + 2);
98                 if (mask & 0xff000000)
99                         outb((s->state >> 24) & 0xff, iobase + 3);
100         }
101
102         val = inb(iobase + 0);
103         val |= (inb(iobase + 1) << 8);
104         val |= (inb(iobase + 2) << 16);
105         val |= (inb(iobase + 3) << 24);
106
107         data[1] = val;
108
109         return insn->n;
110 }
111
112 static int dt2817_attach(struct comedi_device *dev, struct comedi_devconfig *it)
113 {
114         int ret;
115         struct comedi_subdevice *s;
116
117         ret = comedi_request_region(dev, it->options[0], DT2817_SIZE);
118         if (ret)
119                 return ret;
120
121         ret = comedi_alloc_subdevices(dev, 1);
122         if (ret)
123                 return ret;
124
125         s = &dev->subdevices[0];
126
127         s->n_chan = 32;
128         s->type = COMEDI_SUBD_DIO;
129         s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
130         s->range_table = &range_digital;
131         s->maxdata = 1;
132         s->insn_bits = dt2817_dio_insn_bits;
133         s->insn_config = dt2817_dio_insn_config;
134
135         s->state = 0;
136         outb(0, dev->iobase + DT2817_CR);
137
138         return 0;
139 }
140
141 static struct comedi_driver dt2817_driver = {
142         .driver_name    = "dt2817",
143         .module         = THIS_MODULE,
144         .attach         = dt2817_attach,
145         .detach         = comedi_legacy_detach,
146 };
147 module_comedi_driver(dt2817_driver);
148
149 MODULE_AUTHOR("Comedi http://www.comedi.org");
150 MODULE_DESCRIPTION("Comedi low-level driver");
151 MODULE_LICENSE("GPL");