net: add skb_dst_force() in sock_queue_err_skb()
[linux/fpc-iii.git] / drivers / uio / uio_sercos3.c
bloba187fa14c5c084d54dcd687e908f8dc43817a55c
1 /* sercos3: UIO driver for the Automata Sercos III PCI card
3 Copyright (C) 2008 Linutronix GmbH
4 Author: John Ogness <john.ogness@linutronix.de>
6 This is a straight-forward UIO driver, where interrupts are disabled
7 by the interrupt handler and re-enabled via a write to the UIO device
8 by the userspace-part.
10 The only part that may seem odd is the use of a logical OR when
11 storing and restoring enabled interrupts. This is done because the
12 userspace-part could directly modify the Interrupt Enable Register
13 at any time. To reduce possible conflicts, the kernel driver uses
14 a logical OR to make more controlled changes (rather than blindly
15 overwriting previous values).
17 Race conditions exist if the userspace-part directly modifies the
18 Interrupt Enable Register while in operation. The consequences are
19 that certain interrupts would fail to be enabled or disabled. For
20 this reason, the userspace-part should only directly modify the
21 Interrupt Enable Register at the beginning (to get things going).
22 The userspace-part can safely disable interrupts at any time using
23 a write to the UIO device.
26 #include <linux/device.h>
27 #include <linux/module.h>
28 #include <linux/pci.h>
29 #include <linux/uio_driver.h>
30 #include <linux/io.h>
31 #include <linux/slab.h>
33 /* ID's for SERCOS III PCI card (PLX 9030) */
34 #define SERCOS_SUB_VENDOR_ID 0x1971
35 #define SERCOS_SUB_SYSID_3530 0x3530
36 #define SERCOS_SUB_SYSID_3535 0x3535
37 #define SERCOS_SUB_SYSID_3780 0x3780
39 /* Interrupt Enable Register */
40 #define IER0_OFFSET 0x08
42 /* Interrupt Status Register */
43 #define ISR0_OFFSET 0x18
45 struct sercos3_priv {
46 u32 ier0_cache;
47 spinlock_t ier0_cache_lock;
50 /* this function assumes ier0_cache_lock is locked! */
51 static void sercos3_disable_interrupts(struct uio_info *info,
52 struct sercos3_priv *priv)
54 void __iomem *ier0 = info->mem[3].internal_addr + IER0_OFFSET;
56 /* add enabled interrupts to cache */
57 priv->ier0_cache |= ioread32(ier0);
59 /* disable interrupts */
60 iowrite32(0, ier0);
63 /* this function assumes ier0_cache_lock is locked! */
64 static void sercos3_enable_interrupts(struct uio_info *info,
65 struct sercos3_priv *priv)
67 void __iomem *ier0 = info->mem[3].internal_addr + IER0_OFFSET;
69 /* restore previously enabled interrupts */
70 iowrite32(ioread32(ier0) | priv->ier0_cache, ier0);
71 priv->ier0_cache = 0;
74 static irqreturn_t sercos3_handler(int irq, struct uio_info *info)
76 struct sercos3_priv *priv = info->priv;
77 void __iomem *isr0 = info->mem[3].internal_addr + ISR0_OFFSET;
78 void __iomem *ier0 = info->mem[3].internal_addr + IER0_OFFSET;
80 if (!(ioread32(isr0) & ioread32(ier0)))
81 return IRQ_NONE;
83 spin_lock(&priv->ier0_cache_lock);
84 sercos3_disable_interrupts(info, priv);
85 spin_unlock(&priv->ier0_cache_lock);
87 return IRQ_HANDLED;
90 static int sercos3_irqcontrol(struct uio_info *info, s32 irq_on)
92 struct sercos3_priv *priv = info->priv;
94 spin_lock_irq(&priv->ier0_cache_lock);
95 if (irq_on)
96 sercos3_enable_interrupts(info, priv);
97 else
98 sercos3_disable_interrupts(info, priv);
99 spin_unlock_irq(&priv->ier0_cache_lock);
101 return 0;
104 static int sercos3_setup_iomem(struct pci_dev *dev, struct uio_info *info,
105 int n, int pci_bar)
107 info->mem[n].addr = pci_resource_start(dev, pci_bar);
108 if (!info->mem[n].addr)
109 return -1;
110 info->mem[n].internal_addr = ioremap(pci_resource_start(dev, pci_bar),
111 pci_resource_len(dev, pci_bar));
112 if (!info->mem[n].internal_addr)
113 return -1;
114 info->mem[n].size = pci_resource_len(dev, pci_bar);
115 info->mem[n].memtype = UIO_MEM_PHYS;
116 return 0;
119 static int __devinit sercos3_pci_probe(struct pci_dev *dev,
120 const struct pci_device_id *id)
122 struct uio_info *info;
123 struct sercos3_priv *priv;
124 int i;
126 info = kzalloc(sizeof(struct uio_info), GFP_KERNEL);
127 if (!info)
128 return -ENOMEM;
130 priv = kzalloc(sizeof(struct sercos3_priv), GFP_KERNEL);
131 if (!priv)
132 goto out_free;
134 if (pci_enable_device(dev))
135 goto out_free_priv;
137 if (pci_request_regions(dev, "sercos3"))
138 goto out_disable;
140 /* we only need PCI BAR's 0, 2, 3, 4, 5 */
141 if (sercos3_setup_iomem(dev, info, 0, 0))
142 goto out_unmap;
143 if (sercos3_setup_iomem(dev, info, 1, 2))
144 goto out_unmap;
145 if (sercos3_setup_iomem(dev, info, 2, 3))
146 goto out_unmap;
147 if (sercos3_setup_iomem(dev, info, 3, 4))
148 goto out_unmap;
149 if (sercos3_setup_iomem(dev, info, 4, 5))
150 goto out_unmap;
152 spin_lock_init(&priv->ier0_cache_lock);
153 info->priv = priv;
154 info->name = "Sercos_III_PCI";
155 info->version = "0.0.1";
156 info->irq = dev->irq;
157 info->irq_flags = IRQF_SHARED;
158 info->handler = sercos3_handler;
159 info->irqcontrol = sercos3_irqcontrol;
161 pci_set_drvdata(dev, info);
163 if (uio_register_device(&dev->dev, info))
164 goto out_unmap;
166 return 0;
168 out_unmap:
169 for (i = 0; i < 5; i++) {
170 if (info->mem[i].internal_addr)
171 iounmap(info->mem[i].internal_addr);
173 pci_release_regions(dev);
174 out_disable:
175 pci_disable_device(dev);
176 out_free_priv:
177 kfree(priv);
178 out_free:
179 kfree(info);
180 return -ENODEV;
183 static void sercos3_pci_remove(struct pci_dev *dev)
185 struct uio_info *info = pci_get_drvdata(dev);
186 int i;
188 uio_unregister_device(info);
189 pci_release_regions(dev);
190 pci_disable_device(dev);
191 pci_set_drvdata(dev, NULL);
192 for (i = 0; i < 5; i++) {
193 if (info->mem[i].internal_addr)
194 iounmap(info->mem[i].internal_addr);
196 kfree(info->priv);
197 kfree(info);
200 static struct pci_device_id sercos3_pci_ids[] __devinitdata = {
202 .vendor = PCI_VENDOR_ID_PLX,
203 .device = PCI_DEVICE_ID_PLX_9030,
204 .subvendor = SERCOS_SUB_VENDOR_ID,
205 .subdevice = SERCOS_SUB_SYSID_3530,
208 .vendor = PCI_VENDOR_ID_PLX,
209 .device = PCI_DEVICE_ID_PLX_9030,
210 .subvendor = SERCOS_SUB_VENDOR_ID,
211 .subdevice = SERCOS_SUB_SYSID_3535,
214 .vendor = PCI_VENDOR_ID_PLX,
215 .device = PCI_DEVICE_ID_PLX_9030,
216 .subvendor = SERCOS_SUB_VENDOR_ID,
217 .subdevice = SERCOS_SUB_SYSID_3780,
219 { 0, }
222 static struct pci_driver sercos3_pci_driver = {
223 .name = "sercos3",
224 .id_table = sercos3_pci_ids,
225 .probe = sercos3_pci_probe,
226 .remove = sercos3_pci_remove,
229 static int __init sercos3_init_module(void)
231 return pci_register_driver(&sercos3_pci_driver);
234 static void __exit sercos3_exit_module(void)
236 pci_unregister_driver(&sercos3_pci_driver);
239 module_init(sercos3_init_module);
240 module_exit(sercos3_exit_module);
242 MODULE_DESCRIPTION("UIO driver for the Automata Sercos III PCI card");
243 MODULE_AUTHOR("John Ogness <john.ogness@linutronix.de>");
244 MODULE_LICENSE("GPL v2");