]> rtime.felk.cvut.cz Git - mf6xx.git/blob - src/uio/mf614/kernel/mf614.c
4ca80933133446def7f95830f8a17ca2cf787a31
[mf6xx.git] / src / uio / mf614 / kernel / mf614.c
1 /*
2  * UIO driver fo Humusoft MF614 DAQ card.
3  * Copyright (C) 2010--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 #define PCI_VENDOR_ID_HUMUSOFT          0x186c
30 #define PCI_DEVICE_ID_MF614             0x0614
31 #define PCI_SUBVENDOR_ID_HUMUSOFT       0x186c
32 #define PCI_SUBDEVICE_DEVICE            0x0000
33
34 #define STATUS_REG                      0x10
35 #define STATUS_T5INTE                   (u32) (1 << 19)
36 #define STATUS_T5                       (u32) (1 << 3)
37 #define STATUS_CCINTE                   (u32) (1 << 18)
38 #define STATUS_CC                       (u32) (1 << 2)
39
40
41 static irqreturn_t mf614_irq_handler(int irq, struct uio_info *info)
42 {
43         void __iomem *status_reg = info->priv;
44         status_reg = ((u8*) status_reg) + STATUS_REG;
45
46
47         if ((ioread32(status_reg) & STATUS_T5INTE) 
48                 && (ioread32(status_reg) & STATUS_T5))
49         {
50                 //disable interrupt
51                 iowrite32(ioread32(status_reg) & ~STATUS_T5INTE, status_reg);
52                 return IRQ_HANDLED;
53         }
54
55         if ((ioread32(status_reg) & STATUS_CCINTE) 
56                 && (ioread32(status_reg) & STATUS_CC))
57         {
58                 //disable interrupt
59                 iowrite32(ioread32(status_reg) & ~STATUS_CCINTE, status_reg);
60                 return IRQ_HANDLED;
61         }
62
63         return IRQ_NONE;
64 }
65
66 static int mf614_irqcontrol(struct uio_info *info, s32 irq_on)
67 {
68         void __iomem *status_reg = info->priv;
69         status_reg = ((u8*) status_reg) + STATUS_REG;
70
71
72         if (irq_on == 0) { /* Disable interrupts */
73                 iowrite32(ioread32(status_reg) & ~STATUS_T5INTE, status_reg);
74                 iowrite32(ioread32(status_reg) & ~STATUS_CCINTE, status_reg);
75         } 
76         else if (irq_on == 1) {
77                 iowrite32(ioread32(status_reg) | STATUS_T5INTE, status_reg);
78                 iowrite32(ioread32(status_reg) | STATUS_CCINTE, status_reg);
79         }
80
81         return 0;
82 }
83
84 static int __devinit mf614_pci_probe(struct pci_dev *dev,
85                                      const struct pci_device_id *id)
86 {
87         struct uio_info *info;
88
89         info = kzalloc(sizeof(struct uio_info), GFP_KERNEL);
90         if (!info)
91                 return -ENOMEM;
92         
93         if (pci_enable_device(dev))
94                 goto out_free;
95
96         if (pci_request_regions(dev, "mf614"))
97                 goto out_disable;
98         
99         info->name = "MF614";
100         info->version = "0.0.1";
101         
102         info->port[0].name = "Board programming registers";
103         info->port[0].porttype = UIO_PORT_X86;
104         info->port[0].start = pci_resource_start(dev, 0);
105         if (!info->port[0].start)
106                 goto out_release;
107         info->port[0].size = pci_resource_len(dev, 0);
108
109
110         info->port[1].name = "OX9162 local configuration registers";    
111         info->port[1].porttype = UIO_PORT_X86;
112         info->port[1].start = pci_resource_start(dev, 2);
113         if (!info->port[1].start)
114                 goto out_release;
115         info->port[1].size = pci_resource_len(dev, 2);
116
117         info->priv = pci_iomap(dev, 2, 0);
118         if (!info->priv)
119                 goto out_release; 
120
121
122         info->mem[0].name = "Board programming registers";
123         info->mem[0].addr = pci_resource_start(dev, 4);
124         if (!info->mem[0].addr)
125                 goto out_release;
126         info->mem[0].size = pci_resource_len(dev, 4);
127         info->mem[0].memtype = UIO_MEM_PHYS;
128         info->mem[0].internal_addr = pci_ioremap_bar(dev, 4);
129         if (!info->mem[0].internal_addr)
130                 goto out_release;
131
132         
133         info->mem[1].name = "OX9162 local configuration registers";
134         info->mem[1].addr = pci_resource_start(dev, 3);
135         if (!info->mem[1].addr)
136                 goto out_release;
137         info->mem[1].size = pci_resource_len(dev, 3);
138         info->mem[1].memtype = UIO_MEM_PHYS;
139         info->mem[1].internal_addr = pci_ioremap_bar(dev, 3);
140         if (!info->mem[1].internal_addr)
141                 goto out_release;
142
143         
144         info->irq = dev->irq;
145         info->irq_flags = IRQF_SHARED;
146         info->handler = mf614_irq_handler;
147
148         info->irqcontrol = mf614_irqcontrol;
149
150         if(uio_register_device(&dev->dev, info))
151                 goto out_unmap;
152
153         pci_set_drvdata(dev, info);
154
155         return 0;
156
157 out_unmap:
158         iounmap(info->mem[0].internal_addr);
159         iounmap(info->mem[1].internal_addr);
160         pci_iounmap(dev, info->priv);
161 out_release:
162         pci_release_regions(dev);
163 out_disable:
164         pci_disable_device(dev);
165 out_free:
166         kfree(info);
167         return -ENODEV;
168 }
169
170 static void mf614_pci_remove(struct pci_dev *dev)
171 {
172         struct uio_info *info = pci_get_drvdata(dev);
173         void __iomem *status_reg = info->priv;
174         status_reg = ((u8*) status_reg) + STATUS_REG;
175         
176         /* Disable interrupts */
177         iowrite32(ioread32(status_reg) & ~STATUS_T5INTE, status_reg);   
178         iowrite32(ioread32(status_reg) & ~STATUS_CCINTE, status_reg);   
179         
180         uio_unregister_device(info);
181         pci_release_regions(dev);
182         pci_disable_device(dev);
183         pci_set_drvdata(dev, NULL);
184         pci_iounmap(dev, info->priv);
185         iounmap(info->mem[0].internal_addr);
186         iounmap(info->mem[1].internal_addr);
187         
188         kfree(info);
189 }
190
191 static struct pci_device_id mf614_pci_id[] __devinitdata = {
192         {
193                 .vendor = PCI_VENDOR_ID_HUMUSOFT,
194                 .device = PCI_DEVICE_ID_MF614,
195                 .subvendor = PCI_SUBVENDOR_ID_HUMUSOFT,
196                 .subdevice = PCI_SUBDEVICE_DEVICE,
197         },
198         { 0, }
199 };
200 MODULE_DEVICE_TABLE(pci, mf614_pci_driver);
201
202 static struct pci_driver mf614_pci_driver = {
203         .name = "mf614",
204         .id_table = mf614_pci_id,
205         .probe = mf614_pci_probe, 
206         .remove = mf614_pci_remove,
207 };
208
209 static int __init mf614_init_module(void)
210 {
211         return pci_register_driver(&mf614_pci_driver);
212 }
213
214 static void __exit mf614_exit_module(void)
215 {
216         pci_unregister_driver(&mf614_pci_driver);
217 }
218
219 module_init(mf614_init_module);
220 module_exit(mf614_exit_module);
221
222 MODULE_LICENSE("GPL v2");
223 MODULE_AUTHOR("Rostislav Lisovy <lisovy@gmail.com>");