]> rtime.felk.cvut.cz Git - mf6xx.git/blob - src/uio/mf624/kernel/mf624.c
UIO: Fixed pointer arithmetic + coding style.
[mf6xx.git] / src / uio / mf624 / kernel / mf624.c
1 /*
2  * UIO driver fo Humusoft MF624 DAQ card.
3  * Copyright (C) 2011 Rostislav Lisovy <lisovy@gmail.com>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19
20 #include <linux/init.h>
21 #include <linux/module.h>
22 #include <linux/device.h>
23 #include <linux/pci.h>
24 #include <linux/slab.h>
25 #include <linux/io.h>
26 #include <linux/kernel.h>
27 #include <linux/uio_driver.h>
28
29
30 #define PCI_VENDOR_ID_HUMUSOFT          0x186c
31 #define PCI_DEVICE_ID_MF624             0x0624
32 #define PCI_SUBVENDOR_ID_HUMUSOFT       0x186c
33 #define PCI_SUBDEVICE_DEVICE            0x0624
34
35 #define INTCSR                          0x4C
36 #define INTCSR_ADINT_ENABLE             (1 << 0)
37 #define INTCSR_CTR4INT_ENABLE           (1 << 3)
38 #define INTCSR_PCIINT_ENABLE            (1 << 6)
39 #define INTCSR_ADINT_STATUS             (1 << 2)
40 #define INTCSR_CTR4INT_STATUS           (1 << 5)
41
42 enum mf624_interrupt_source {ADC, CTR4, ALL};
43
44
45 void mf624_disable_interrupt(enum mf624_interrupt_source source,
46                                 struct uio_info *info)
47 {
48         u8 __iomem *INTCSR_reg = (u8 *)info->mem[0].internal_addr + INTCSR;
49
50         switch (source) {
51         case ADC:
52                 iowrite32(ioread32(INTCSR_reg) & ~(INTCSR_ADINT_ENABLE),
53                         INTCSR_reg);
54                 break;
55         case CTR4:
56                 iowrite32(ioread32(INTCSR_reg) & ~(INTCSR_CTR4INT_ENABLE),
57                         INTCSR_reg);
58                 break;
59         case ALL:
60                 iowrite32(ioread32(INTCSR_reg) & ~(INTCSR_ADINT_ENABLE |
61                         INTCSR_CTR4INT_ENABLE), INTCSR_reg);
62                 break;
63         default:
64                 iowrite32(ioread32(INTCSR_reg) & ~(INTCSR_ADINT_ENABLE |
65                         INTCSR_CTR4INT_ENABLE), INTCSR_reg);
66                 break;
67         }
68 }
69
70 void mf624_enable_interrupt(enum mf624_interrupt_source source,
71                                 struct uio_info *info)
72 {
73         u8 __iomem *INTCSR_reg = (u8 *)info->mem[0].internal_addr + INTCSR;
74
75         switch (source) {
76         case ADC:
77                 iowrite32(ioread32(INTCSR_reg) | (INTCSR_ADINT_ENABLE),
78                         INTCSR_reg);
79                 break;
80         case CTR4:
81                 iowrite32(ioread32(INTCSR_reg) | (INTCSR_CTR4INT_ENABLE),
82                         INTCSR_reg);
83                 break;
84         case ALL:
85                 iowrite32(ioread32(INTCSR_reg) | (INTCSR_ADINT_ENABLE |
86                         INTCSR_CTR4INT_ENABLE), INTCSR_reg);
87                 break;
88         default:
89                 iowrite32(ioread32(INTCSR_reg) | (INTCSR_ADINT_ENABLE |
90                         INTCSR_CTR4INT_ENABLE), INTCSR_reg);
91                 break;
92         }
93 }
94
95 static irqreturn_t mf624_irq_handler(int irq, struct uio_info *info)
96 {
97         u8 __iomem *INTCSR_reg = (u8 *)info->mem[0].internal_addr + INTCSR;
98
99         if (((ioread32(INTCSR_reg) & INTCSR_ADINT_ENABLE) > 0)
100         && ((ioread32(INTCSR_reg) & INTCSR_ADINT_STATUS) > 0)) {
101                 /* disable interrupt */
102                 mf624_disable_interrupt(ADC, info);
103                 return IRQ_HANDLED;
104         }
105
106         if (((ioread32(INTCSR_reg) & INTCSR_CTR4INT_ENABLE) > 0)
107         && ((ioread32(INTCSR_reg) & INTCSR_CTR4INT_STATUS) > 0)) {
108                 /* disable interrupt */
109                 mf624_disable_interrupt(CTR4, info);
110                 return IRQ_HANDLED;
111         }
112
113         return IRQ_NONE;
114 }
115
116 static int mf624_irqcontrol(struct uio_info *info, s32 irq_on)
117 {
118         if (irq_on == 0) { /* Disable interrupts */
119                 mf624_disable_interrupt(ALL, info);
120         } else if (irq_on == 1) {
121                 mf624_enable_interrupt(ALL, info);
122         }
123
124         return 0;
125 }
126
127 static int __devinit mf624_pci_probe(struct pci_dev *dev,
128                                 const struct pci_device_id *id)
129 {
130         struct uio_info *info;
131
132         info = kzalloc(sizeof(struct uio_info), GFP_KERNEL);
133         if (!info)
134                 return -ENOMEM;
135
136         if (pci_enable_device(dev))
137                 goto out_free;
138
139         if (pci_request_regions(dev, "mf624"))
140                 goto out_disable;
141
142         info->name = "mf624";
143         info->version = "0.0.1";
144
145         /* BAR0 */
146         info->mem[0].name = "PCI chipset, interrupts, status "
147                         "bits, special functions";
148         info->mem[0].addr = pci_resource_start(dev, 0);
149         if (!info->mem[0].addr)
150                 goto out_release;
151         info->mem[0].size = pci_resource_len(dev, 0);
152         info->mem[0].memtype = UIO_MEM_PHYS;
153         info->mem[0].internal_addr = pci_ioremap_bar(dev, 0);
154         if (!info->mem[0].internal_addr)
155                 goto out_release;
156
157
158         /* BAR2 */
159         info->mem[1].name = "ADC, DAC, DIO";
160         info->mem[1].addr = pci_resource_start(dev, 2);
161         if (!info->mem[1].addr)
162                 goto out_release;
163         info->mem[1].size = pci_resource_len(dev, 2);
164         info->mem[1].memtype = UIO_MEM_PHYS;
165         info->mem[1].internal_addr = pci_ioremap_bar(dev, 2);
166         if (!info->mem[1].internal_addr)
167                 goto out_release;
168
169
170         /* BAR4 */
171         info->mem[2].name = "Counter/timer chip";
172         info->mem[2].addr = pci_resource_start(dev, 4);
173         if (!info->mem[2].addr)
174                 goto out_release;
175         info->mem[2].size = pci_resource_len(dev, 4);
176         info->mem[2].memtype = UIO_MEM_PHYS;
177         info->mem[2].internal_addr = pci_ioremap_bar(dev, 4);
178         if (!info->mem[2].internal_addr)
179                 goto out_release;
180
181
182         info->irq = dev->irq;
183         info->irq_flags = IRQF_SHARED;
184         info->handler = mf624_irq_handler;
185
186         info->irqcontrol = mf624_irqcontrol;
187
188         if (uio_register_device(&dev->dev, info))
189                 goto out_unmap;
190
191         pci_set_drvdata(dev, info);
192
193         return 0;
194
195
196 out_unmap:
197         iounmap(info->mem[0].internal_addr);
198         iounmap(info->mem[1].internal_addr);
199         iounmap(info->mem[2].internal_addr);
200 out_release:
201         pci_release_regions(dev);
202 out_disable:
203         pci_disable_device(dev);
204 out_free:
205         kfree(info);
206         return -ENODEV;
207 }
208
209 static void mf624_pci_remove(struct pci_dev *dev)
210 {
211         struct uio_info *info = pci_get_drvdata(dev);
212
213         mf624_disable_interrupt(ALL, info);
214
215         uio_unregister_device(info);
216         pci_release_regions(dev);
217         pci_disable_device(dev);
218         pci_set_drvdata(dev, NULL);
219
220         iounmap(info->mem[0].internal_addr);
221         iounmap(info->mem[1].internal_addr);
222         iounmap(info->mem[2].internal_addr);
223
224         kfree(info);
225 }
226
227 static struct pci_device_id mf624_pci_id[] __devinitdata = {
228         {
229                 .vendor = PCI_VENDOR_ID_HUMUSOFT,
230                 .device = PCI_DEVICE_ID_MF624,
231                 .subvendor = PCI_SUBVENDOR_ID_HUMUSOFT,
232                 .subdevice = PCI_SUBDEVICE_DEVICE,
233         },
234         { 0, }
235 };
236
237 static struct pci_driver mf624_pci_driver = {
238         .name = "mf624",
239         .id_table = mf624_pci_id,
240         .probe = mf624_pci_probe,
241         .remove = mf624_pci_remove,
242 };
243 MODULE_DEVICE_TABLE(pci, mf624_pci_driver);
244
245 static int __init mf624_init_module(void)
246 {
247         return pci_register_driver(&mf624_pci_driver);
248 }
249
250 static void __exit mf624_exit_module(void)
251 {
252         pci_unregister_driver(&mf624_pci_driver);
253 }
254
255 module_init(mf624_init_module);
256 module_exit(mf624_exit_module);
257
258 MODULE_LICENSE("GPL v2");
259 MODULE_AUTHOR("Rostislav Lisovy <lisovy@gmail.com>");