]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/cbus/retu-user.c
[PATCH] ARM: OMAP: retu-user: sem2mutex conversion
[linux-2.6-omap-h63xx.git] / drivers / cbus / retu-user.c
1 /**
2  * drivers/cbus/retu-user.c
3  *
4  * Retu user space interface functions
5  *
6  * Copyright (C) 2004, 2005 Nokia Corporation
7  *
8  * Written by Mikko Ylinen <mikko.k.ylinen@nokia.com>
9  *
10  * This file is subject to the terms and conditions of the GNU General
11  * Public License. See the file "COPYING" in the main directory of this
12  * archive for more details.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  */
23
24 #include <linux/types.h>
25 #include <linux/kernel.h>
26 #include <linux/interrupt.h>
27 #include <linux/module.h>
28 #include <linux/init.h>
29 #include <linux/fs.h>
30 #include <linux/miscdevice.h>
31 #include <linux/poll.h>
32 #include <linux/list.h>
33 #include <linux/spinlock.h>
34 #include <linux/mutex.h>
35
36 #include <asm/uaccess.h>
37
38 #include "retu.h"
39
40 #include "user_retu_tahvo.h"
41
42 /* Maximum size of IRQ node buffer/pool */
43 #define RETU_MAX_IRQ_BUF_LEN    16
44
45 #define PFX                     "retu-user: "
46
47 /* Bitmap for marking the interrupt sources as having the handlers */
48 static u32 retu_irq_bits;
49
50 /* For allowing only one user process to subscribe to the retu interrupts */
51 static struct file *retu_irq_subscr = NULL;
52
53 /* For poll and IRQ passing */
54 struct retu_irq {
55         u32 id;
56         struct list_head node;
57 };
58
59 static spinlock_t retu_irqs_lock;
60 static struct retu_irq *retu_irq_block;
61 static LIST_HEAD(retu_irqs);
62 static LIST_HEAD(retu_irqs_reserve);
63
64 /* Wait queue - used when user wants to read the device */
65 DECLARE_WAIT_QUEUE_HEAD(retu_user_waitqueue);
66
67 /* Semaphore to protect irq subscription sequence */
68 static struct mutex retu_mutex;
69
70 /* This array specifies RETU register types (read/write/toggle) */
71 static const u8 retu_access_bits[] = {
72         1,
73         4,
74         3,
75         3,
76         1,
77         3,
78         3,
79         0,
80         3,
81         3,
82         3,
83         3,
84         3,
85         3,
86         3,
87         4,
88         4,
89         3,
90         0,
91         0,
92         0,
93         0,
94         1,
95         3,
96         3,
97         3,
98         3,
99         3,
100         3,
101         3,
102         3,
103         3
104 };
105
106 /*
107  * The handler for all RETU interrupts.
108  *
109  * arg is the interrupt source in RETU.
110  */
111 static void retu_user_irq_handler(unsigned long arg)
112 {
113         struct retu_irq *irq;
114
115         retu_ack_irq(arg);
116
117         spin_lock(&retu_irqs_lock);
118         if (list_empty(&retu_irqs_reserve)) {
119                 spin_unlock(&retu_irqs_lock);
120                 return;
121         }
122         irq = list_entry((&retu_irqs_reserve)->next, struct retu_irq, node);
123         irq->id = arg;
124         list_move_tail(&irq->node, &retu_irqs);
125         spin_unlock(&retu_irqs_lock);
126
127         /* wake up waiting thread */
128         wake_up(&retu_user_waitqueue);
129 }
130
131 /*
132  * This routine sets up the interrupt handler and marks an interrupt source
133  * in RETU as a candidate for signal delivery to the user process.
134  */
135 static int retu_user_subscribe_to_irq(int id, struct file *filp)
136 {
137         int ret;
138
139         mutex_lock(&retu_mutex);
140         if ((retu_irq_subscr != NULL) && (retu_irq_subscr != filp)) {
141                 mutex_unlock(&retu_mutex);
142                 return -EBUSY;
143         }
144         /* Store the file pointer of the first user process registering IRQs */
145         retu_irq_subscr = filp;
146         mutex_unlock(&retu_mutex);
147
148         if (retu_irq_bits & (1 << id))
149                 return 0;
150
151         ret = retu_request_irq(id, retu_user_irq_handler, id, "");
152         if (ret < 0)
153                 return ret;
154
155         /* Mark that this interrupt has a handler */
156         retu_irq_bits |= 1 << id;
157
158         return 0;
159 }
160
161 /*
162  * Unregisters all RETU interrupt handlers.
163  */
164 static void retu_unreg_irq_handlers(void)
165 {
166         int id;
167
168         if (!retu_irq_bits)
169                 return;
170
171         for (id = 0; id < MAX_RETU_IRQ_HANDLERS; id++)
172                 if (retu_irq_bits & (1 << id))
173                         retu_free_irq(id);
174
175         retu_irq_bits = 0;
176 }
177
178 /*
179  * Write to RETU register.
180  * Returns 0 upon success, a negative error value otherwise.
181  */
182 static int retu_user_write_with_mask(u32 field, u16 value)
183 {
184         u32 mask;
185         u32 reg;
186         u_short tmp;
187         unsigned long flags;
188
189         mask = MASK(field);
190         reg = REG(field);
191
192         /* Detect bad mask and reg */
193         if (mask == 0 || reg > RETU_REG_MAX ||
194             retu_access_bits[reg] == READ_ONLY) {
195                 printk(KERN_ERR PFX "invalid arguments (reg=%#x, mask=%#x)\n",
196                        reg, mask);
197                 return -EINVAL;
198         }
199
200         /* Justify value according to mask */
201         while (!(mask & 1)) {
202                 value = value << 1;
203                 mask = mask >> 1;
204         }
205
206         spin_lock_irqsave(&retu_lock, flags);
207         if (retu_access_bits[reg] == TOGGLE) {
208                 /* No need to detect previous content of register */
209                 tmp = 0;
210         } else {
211                 /* Read current value of register */
212                 tmp = retu_read_reg(reg);
213         }
214
215         /* Generate new value */
216         tmp = (tmp & ~MASK(field)) | (value & MASK(field));
217         /* Write data to RETU */
218         retu_write_reg(reg, tmp);
219         spin_unlock_irqrestore(&retu_lock, flags);
220
221         return 0;
222 }
223
224 /*
225  * Read RETU register.
226  */
227 static u32 retu_user_read_with_mask(u32 field)
228 {
229         u_short value;
230         u32 mask, reg;
231
232         mask = MASK(field);
233         reg = REG(field);
234
235         /* Detect bad mask and reg */
236         if (mask == 0 || reg > RETU_REG_MAX) {
237                 printk(KERN_ERR PFX "invalid arguments (reg=%#x, mask=%#x)\n",
238                        reg, mask);
239                 return -EINVAL;
240         }
241
242         /* Read the register */
243         value = retu_read_reg(reg) & mask;
244
245         /* Right justify value */
246         while (!(mask & 1)) {
247                 value = value >> 1;
248                 mask = mask >> 1;
249         }
250
251         return value;
252 }
253
254 /*
255  * Close device
256  */
257 static int retu_close(struct inode *inode, struct file *filp)
258 {
259         /* Unregister all interrupts that have been registered */
260         if (retu_irq_subscr == filp) {
261                 retu_unreg_irq_handlers();
262                 retu_irq_subscr = NULL;
263         }
264
265         return 0;
266 }
267
268 /*
269  * Device control (ioctl)
270  */
271 static int retu_ioctl(struct inode *inode, struct file *filp,
272                       unsigned int cmd, unsigned long arg)
273 {
274         struct retu_tahvo_write_parms par;
275
276         switch (cmd) {
277         case URT_IOCT_IRQ_SUBSCR:
278                 return retu_user_subscribe_to_irq(arg, filp);
279         case RETU_IOCH_READ:
280                 return retu_user_read_with_mask(arg);
281         case RETU_IOCX_WRITE:
282                 copy_from_user(&par, (void __user *) arg, sizeof(par));
283                 par.result = retu_user_write_with_mask(par.field, par.value);
284                 copy_to_user((void __user *) arg, &par, sizeof(par));
285                 break;
286         case RETU_IOCH_ADC_READ:
287                 return retu_read_adc(arg);
288         default:
289                 return -ENOIOCTLCMD;
290         }
291         return 0;
292 }
293
294 /*
295  * Read from device
296  */
297 static ssize_t retu_read(struct file *filp, char *buf, size_t count,
298                          loff_t * offp)
299 {
300         struct retu_irq *irq;
301
302         u32 nr, i;
303
304         /* read not permitted if neither filp nor anyone has registered IRQs */
305         if (retu_irq_subscr != filp)
306                 return -EPERM;
307
308         if ((count < sizeof(u32)) || ((count % sizeof(u32)) != 0))
309                 return -EINVAL;
310
311         nr = count / sizeof(u32);
312
313         for (i = 0; i < nr; i++) {
314                 unsigned long flags;
315                 u32 irq_id;
316                 int ret;
317
318                 ret = wait_event_interruptible(retu_user_waitqueue,
319                                                !list_empty(&retu_irqs));
320                 if (ret < 0)
321                         return ret;
322
323                 spin_lock_irqsave(&retu_irqs_lock, flags);
324                 irq = list_entry((&retu_irqs)->next, struct retu_irq, node);
325                 irq_id = irq->id;
326                 list_move(&irq->node, &retu_irqs_reserve);
327                 spin_unlock_irqrestore(&retu_irqs_lock, flags);
328
329                 copy_to_user(buf + i * sizeof(irq_id), &irq_id, sizeof(irq_id));
330
331         }
332
333         return count;
334 }
335
336 /*
337  * Poll method
338  */
339 static unsigned retu_poll(struct file *filp, struct poll_table_struct *pt)
340 {
341         if (!list_empty(&retu_irqs))
342                 return POLLIN;
343
344         poll_wait(filp, &retu_user_waitqueue, pt);
345
346         if (!list_empty(&retu_irqs))
347                 return POLLIN;
348         else
349                 return 0;
350 }
351
352 static struct file_operations retu_user_fileops = {
353         .owner = THIS_MODULE,
354         .ioctl = retu_ioctl,
355         .read = retu_read,
356         .release = retu_close,
357         .poll = retu_poll
358 };
359
360 static struct miscdevice retu_device = {
361         .minor = MISC_DYNAMIC_MINOR,
362         .name = "retu",
363         .fops = &retu_user_fileops
364 };
365
366 /*
367  * Initialization
368  *
369  * @return 0 if successful, error value otherwise.
370  */
371 int retu_user_init(void)
372 {
373         struct retu_irq *irq;
374         int res, i;
375
376         irq = kmalloc(sizeof(*irq) * RETU_MAX_IRQ_BUF_LEN, GFP_KERNEL);
377         if (irq == NULL) {
378                 printk(KERN_ERR PFX "kmalloc failed\n");
379                 return -ENOMEM;
380         }
381         memset(irq, 0, sizeof(*irq) * RETU_MAX_IRQ_BUF_LEN);
382         for (i = 0; i < RETU_MAX_IRQ_BUF_LEN; i++)
383                 list_add(&irq[i].node, &retu_irqs_reserve);
384
385         retu_irq_block = irq;
386
387         spin_lock_init(&retu_irqs_lock);
388         mutex_init(&retu_mutex);
389
390         /* Request a misc device */
391         res = misc_register(&retu_device);
392         if (res < 0) {
393                 printk(KERN_ERR PFX "unable to register misc device for %s\n",
394                        retu_device.name);
395                 kfree(irq);
396                 return res;
397         }
398
399         return 0;
400 }
401
402 /*
403  * Cleanup.
404  */
405 void retu_user_cleanup(void)
406 {
407         /* Unregister our misc device */
408         misc_deregister(&retu_device);
409         /* Unregister and disable all RETU interrupts used by this module */
410         retu_unreg_irq_handlers();
411         kfree(retu_irq_block);
412 }
413
414 MODULE_DESCRIPTION("Retu ASIC user space functions");
415 MODULE_LICENSE("GPL");
416 MODULE_AUTHOR("Mikko Ylinen");