2 * drivers/cbus/retu-user.c
4 * Retu user space interface functions
6 * Copyright (C) 2004, 2005 Nokia Corporation
8 * Written by Mikko Ylinen <mikko.k.ylinen@nokia.com>
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.
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.
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
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>
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>
36 #include <asm/uaccess.h>
40 #include "user_retu_tahvo.h"
42 /* Maximum size of IRQ node buffer/pool */
43 #define RETU_MAX_IRQ_BUF_LEN 16
45 #define PFX "retu-user: "
47 /* Bitmap for marking the interrupt sources as having the handlers */
48 static u32 retu_irq_bits;
50 /* For allowing only one user process to subscribe to the retu interrupts */
51 static struct file *retu_irq_subscr = NULL;
53 /* For poll and IRQ passing */
56 struct list_head node;
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);
64 /* Wait queue - used when user wants to read the device */
65 DECLARE_WAIT_QUEUE_HEAD(retu_user_waitqueue);
67 /* Semaphore to protect irq subscription sequence */
68 static struct mutex retu_mutex;
70 /* This array specifies RETU register types (read/write/toggle) */
71 static const u8 retu_access_bits[] = {
107 * The handler for all RETU interrupts.
109 * arg is the interrupt source in RETU.
111 static void retu_user_irq_handler(unsigned long arg)
113 struct retu_irq *irq;
117 spin_lock(&retu_irqs_lock);
118 if (list_empty(&retu_irqs_reserve)) {
119 spin_unlock(&retu_irqs_lock);
122 irq = list_entry((&retu_irqs_reserve)->next, struct retu_irq, node);
124 list_move_tail(&irq->node, &retu_irqs);
125 spin_unlock(&retu_irqs_lock);
127 /* wake up waiting thread */
128 wake_up(&retu_user_waitqueue);
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.
135 static int retu_user_subscribe_to_irq(int id, struct file *filp)
139 mutex_lock(&retu_mutex);
140 if ((retu_irq_subscr != NULL) && (retu_irq_subscr != filp)) {
141 mutex_unlock(&retu_mutex);
144 /* Store the file pointer of the first user process registering IRQs */
145 retu_irq_subscr = filp;
146 mutex_unlock(&retu_mutex);
148 if (retu_irq_bits & (1 << id))
151 ret = retu_request_irq(id, retu_user_irq_handler, id, "");
155 /* Mark that this interrupt has a handler */
156 retu_irq_bits |= 1 << id;
162 * Unregisters all RETU interrupt handlers.
164 static void retu_unreg_irq_handlers(void)
171 for (id = 0; id < MAX_RETU_IRQ_HANDLERS; id++)
172 if (retu_irq_bits & (1 << id))
179 * Write to RETU register.
180 * Returns 0 upon success, a negative error value otherwise.
182 static int retu_user_write_with_mask(u32 field, u16 value)
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",
200 /* Justify value according to mask */
201 while (!(mask & 1)) {
206 spin_lock_irqsave(&retu_lock, flags);
207 if (retu_access_bits[reg] == TOGGLE) {
208 /* No need to detect previous content of register */
211 /* Read current value of register */
212 tmp = retu_read_reg(reg);
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);
225 * Read RETU register.
227 static u32 retu_user_read_with_mask(u32 field)
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",
242 /* Read the register */
243 value = retu_read_reg(reg) & mask;
245 /* Right justify value */
246 while (!(mask & 1)) {
257 static int retu_close(struct inode *inode, struct file *filp)
259 /* Unregister all interrupts that have been registered */
260 if (retu_irq_subscr == filp) {
261 retu_unreg_irq_handlers();
262 retu_irq_subscr = NULL;
269 * Device control (ioctl)
271 static int retu_ioctl(struct inode *inode, struct file *filp,
272 unsigned int cmd, unsigned long arg)
274 struct retu_tahvo_write_parms par;
277 case URT_IOCT_IRQ_SUBSCR:
278 return retu_user_subscribe_to_irq(arg, filp);
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));
286 case RETU_IOCH_ADC_READ:
287 return retu_read_adc(arg);
297 static ssize_t retu_read(struct file *filp, char *buf, size_t count,
300 struct retu_irq *irq;
304 /* read not permitted if neither filp nor anyone has registered IRQs */
305 if (retu_irq_subscr != filp)
308 if ((count < sizeof(u32)) || ((count % sizeof(u32)) != 0))
311 nr = count / sizeof(u32);
313 for (i = 0; i < nr; i++) {
318 ret = wait_event_interruptible(retu_user_waitqueue,
319 !list_empty(&retu_irqs));
323 spin_lock_irqsave(&retu_irqs_lock, flags);
324 irq = list_entry((&retu_irqs)->next, struct retu_irq, node);
326 list_move(&irq->node, &retu_irqs_reserve);
327 spin_unlock_irqrestore(&retu_irqs_lock, flags);
329 copy_to_user(buf + i * sizeof(irq_id), &irq_id, sizeof(irq_id));
339 static unsigned retu_poll(struct file *filp, struct poll_table_struct *pt)
341 if (!list_empty(&retu_irqs))
344 poll_wait(filp, &retu_user_waitqueue, pt);
346 if (!list_empty(&retu_irqs))
352 static struct file_operations retu_user_fileops = {
353 .owner = THIS_MODULE,
356 .release = retu_close,
360 static struct miscdevice retu_device = {
361 .minor = MISC_DYNAMIC_MINOR,
363 .fops = &retu_user_fileops
369 * @return 0 if successful, error value otherwise.
371 int retu_user_init(void)
373 struct retu_irq *irq;
376 irq = kmalloc(sizeof(*irq) * RETU_MAX_IRQ_BUF_LEN, GFP_KERNEL);
378 printk(KERN_ERR PFX "kmalloc failed\n");
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);
385 retu_irq_block = irq;
387 spin_lock_init(&retu_irqs_lock);
388 mutex_init(&retu_mutex);
390 /* Request a misc device */
391 res = misc_register(&retu_device);
393 printk(KERN_ERR PFX "unable to register misc device for %s\n",
405 void retu_user_cleanup(void)
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);
414 MODULE_DESCRIPTION("Retu ASIC user space functions");
415 MODULE_LICENSE("GPL");
416 MODULE_AUTHOR("Mikko Ylinen");