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;
278 case URT_IOCT_IRQ_SUBSCR:
279 return retu_user_subscribe_to_irq(arg, filp);
281 return retu_user_read_with_mask(arg);
282 case RETU_IOCX_WRITE:
283 ret = copy_from_user(&par, (void __user *) arg, sizeof(par));
285 printk(KERN_ERR "copy_from_user failed: %d\n", ret);
286 par.result = retu_user_write_with_mask(par.field, par.value);
287 ret = copy_to_user((void __user *) arg, &par, sizeof(par));
289 printk(KERN_ERR "copy_to_user failed: %d\n", ret);
291 case RETU_IOCH_ADC_READ:
292 return retu_read_adc(arg);
302 static ssize_t retu_read(struct file *filp, char *buf, size_t count,
305 struct retu_irq *irq;
309 /* read not permitted if neither filp nor anyone has registered IRQs */
310 if (retu_irq_subscr != filp)
313 if ((count < sizeof(u32)) || ((count % sizeof(u32)) != 0))
316 nr = count / sizeof(u32);
318 for (i = 0; i < nr; i++) {
323 ret = wait_event_interruptible(retu_user_waitqueue,
324 !list_empty(&retu_irqs));
328 spin_lock_irqsave(&retu_irqs_lock, flags);
329 irq = list_entry((&retu_irqs)->next, struct retu_irq, node);
331 list_move(&irq->node, &retu_irqs_reserve);
332 spin_unlock_irqrestore(&retu_irqs_lock, flags);
334 ret = copy_to_user(buf + i * sizeof(irq_id), &irq_id,
337 printk(KERN_ERR "copy_to_user failed: %d\n", ret);
346 static unsigned retu_poll(struct file *filp, struct poll_table_struct *pt)
348 if (!list_empty(&retu_irqs))
351 poll_wait(filp, &retu_user_waitqueue, pt);
353 if (!list_empty(&retu_irqs))
359 static struct file_operations retu_user_fileops = {
360 .owner = THIS_MODULE,
363 .release = retu_close,
367 static struct miscdevice retu_device = {
368 .minor = MISC_DYNAMIC_MINOR,
370 .fops = &retu_user_fileops
376 * @return 0 if successful, error value otherwise.
378 int retu_user_init(void)
380 struct retu_irq *irq;
383 irq = kmalloc(sizeof(*irq) * RETU_MAX_IRQ_BUF_LEN, GFP_KERNEL);
385 printk(KERN_ERR PFX "kmalloc failed\n");
388 memset(irq, 0, sizeof(*irq) * RETU_MAX_IRQ_BUF_LEN);
389 for (i = 0; i < RETU_MAX_IRQ_BUF_LEN; i++)
390 list_add(&irq[i].node, &retu_irqs_reserve);
392 retu_irq_block = irq;
394 spin_lock_init(&retu_irqs_lock);
395 mutex_init(&retu_mutex);
397 /* Request a misc device */
398 res = misc_register(&retu_device);
400 printk(KERN_ERR PFX "unable to register misc device for %s\n",
412 void retu_user_cleanup(void)
414 /* Unregister our misc device */
415 misc_deregister(&retu_device);
416 /* Unregister and disable all RETU interrupts used by this module */
417 retu_unreg_irq_handlers();
418 kfree(retu_irq_block);
421 MODULE_DESCRIPTION("Retu ASIC user space functions");
422 MODULE_LICENSE("GPL");
423 MODULE_AUTHOR("Mikko Ylinen");