2 * drivers/cbus/tahvo-user.c
4 * Tahvo 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 TAHVO_MAX_IRQ_BUF_LEN 16
45 #define PFX "tahvo-user: "
47 /* Bitmap for marking the interrupt sources as having the handlers */
48 static u32 tahvo_irq_bits;
50 /* For allowing only one user process to subscribe to the tahvo interrupts */
51 static struct file *tahvo_irq_subscr = NULL;
53 /* For poll and IRQ passing */
56 struct list_head node;
59 static spinlock_t tahvo_irqs_lock;
60 static struct tahvo_irq *tahvo_irq_block;
61 static LIST_HEAD(tahvo_irqs);
62 static LIST_HEAD(tahvo_irqs_reserve);
64 /* Wait queue - used when user wants to read the device */
65 DECLARE_WAIT_QUEUE_HEAD(tahvo_user_waitqueue);
67 /* Semaphore to protect irq subscription sequence */
68 static struct mutex tahvo_mutex;
70 /* This array specifies TAHVO register types (read/write/toggle) */
71 static const u8 tahvo_access_bits[] = {
89 * The handler for all TAHVO interrupts.
91 * arg is the interrupt source in TAHVO.
93 static void tahvo_user_irq_handler(unsigned long arg)
95 struct tahvo_irq *irq;
97 /* user has to re-enable the interrupt once ready
98 * for receiving them again */
99 tahvo_disable_irq(arg);
102 spin_lock(&tahvo_irqs_lock);
103 if (list_empty(&tahvo_irqs_reserve)) {
104 spin_unlock(&tahvo_irqs_lock);
107 irq = list_entry((&tahvo_irqs_reserve)->next, struct tahvo_irq, node);
109 list_move_tail(&irq->node, &tahvo_irqs);
110 spin_unlock(&tahvo_irqs_lock);
112 /* wake up waiting thread */
113 wake_up(&tahvo_user_waitqueue);
117 * This routine sets up the interrupt handler and marks an interrupt source
118 * in TAHVO as a candidate for signal delivery to the user process.
120 static int tahvo_user_subscribe_to_irq(int id, struct file *filp)
124 mutex_lock(&tahvo_mutex);
125 if ((tahvo_irq_subscr != NULL) && (tahvo_irq_subscr != filp)) {
126 mutex_unlock(&tahvo_mutex);
129 /* Store the file pointer of the first user process registering IRQs */
130 tahvo_irq_subscr = filp;
131 mutex_unlock(&tahvo_mutex);
133 if (tahvo_irq_bits & (1 << id))
136 ret = tahvo_request_irq(id, tahvo_user_irq_handler, id, "");
140 /* Mark that this interrupt has a handler */
141 tahvo_irq_bits |= 1 << id;
147 * Unregister all TAHVO interrupt handlers
149 static void tahvo_unreg_irq_handlers(void)
156 for (id = 0; id < MAX_TAHVO_IRQ_HANDLERS; id++)
157 if (tahvo_irq_bits & (1 << id))
164 * Write to TAHVO register.
165 * Returns 0 upon success, a negative error value otherwise.
167 static int tahvo_user_write_with_mask(u32 field, u16 value)
177 /* Detect bad mask and reg */
178 if (mask == 0 || reg > TAHVO_REG_MAX ||
179 tahvo_access_bits[reg] == READ_ONLY) {
180 printk(KERN_ERR PFX "invalid arguments (reg=%#x, mask=%#x)\n",
185 /* Justify value according to mask */
186 while (!(mask & 1)) {
191 spin_lock_irqsave(&tahvo_lock, flags);
192 if (tahvo_access_bits[reg] == TOGGLE) {
193 /* No need to detect previous content of register */
196 /* Read current value of register */
197 tmp = tahvo_read_reg(reg);
199 /* Generate a new value */
200 tmp = (tmp & ~MASK(field)) | (value & MASK(field));
201 /* Write data to TAHVO */
202 tahvo_write_reg(reg, tmp);
203 spin_unlock_irqrestore(&tahvo_lock, flags);
209 * Read TAHVO register.
211 static u32 tahvo_user_read_with_mask(u32 field)
219 /* Detect bad mask and reg */
220 if (mask == 0 || reg > TAHVO_REG_MAX) {
221 printk(KERN_ERR PFX "invalid arguments (reg=%#x, mask=%#x)\n",
226 /* Read the register */
227 value = tahvo_read_reg(reg) & mask;
229 /* Right justify value */
230 while (!(mask & 1)) {
241 static int tahvo_close(struct inode *inode, struct file *filp)
243 /* Unregister all interrupts that have been registered */
244 if (tahvo_irq_subscr == filp) {
245 tahvo_unreg_irq_handlers();
246 tahvo_irq_subscr = NULL;
253 * Device control (ioctl)
255 static int tahvo_ioctl(struct inode *inode, struct file *filp,
256 unsigned int cmd, unsigned long arg)
258 struct retu_tahvo_write_parms par;
262 case URT_IOCT_IRQ_SUBSCR:
263 return tahvo_user_subscribe_to_irq(arg, filp);
264 case TAHVO_IOCH_READ:
265 return tahvo_user_read_with_mask(arg);
266 case TAHVO_IOCX_WRITE:
267 ret = copy_from_user(&par, (void __user *) arg, sizeof(par));
269 printk(KERN_ERR "copy_from_user failed: %d\n", ret);
270 par.result = tahvo_user_write_with_mask(par.field, par.value);
271 ret = copy_to_user((void __user *) arg, &par, sizeof(par));
273 printk(KERN_ERR "copy_to_user failed: %d\n", ret);
284 static ssize_t tahvo_read(struct file *filp, char *buf, size_t count,
287 struct tahvo_irq *irq;
291 /* read not permitted if neither filp nor anyone has registered IRQs */
292 if (tahvo_irq_subscr != filp)
295 if ((count < sizeof(u32)) || ((count % sizeof(u32)) != 0))
298 nr = count / sizeof(u32);
300 for (i = 0; i < nr; i++) {
305 ret = wait_event_interruptible(tahvo_user_waitqueue,
306 !list_empty(&tahvo_irqs));
310 spin_lock_irqsave(&tahvo_irqs_lock, flags);
311 irq = list_entry((&tahvo_irqs)->next, struct tahvo_irq, node);
313 list_move(&irq->node, &tahvo_irqs_reserve);
314 spin_unlock_irqrestore(&tahvo_irqs_lock, flags);
316 ret = copy_to_user(buf + i * sizeof(irq_id), &irq_id,
319 printk(KERN_ERR "copy_to_user failed: %d\n", ret);
328 static unsigned tahvo_poll(struct file *filp, struct poll_table_struct *pt)
330 if (!list_empty(&tahvo_irqs))
333 poll_wait(filp, &tahvo_user_waitqueue, pt);
335 if (!list_empty(&tahvo_irqs))
341 static struct file_operations tahvo_user_fileops = {
342 .owner = THIS_MODULE,
343 .ioctl = tahvo_ioctl,
345 .release = tahvo_close,
349 static struct miscdevice tahvo_device = {
350 .minor = MISC_DYNAMIC_MINOR,
352 .fops = &tahvo_user_fileops
358 * @return 0 if successful, error value otherwise.
360 int tahvo_user_init(void)
362 struct tahvo_irq *irq;
365 irq = kmalloc(sizeof(*irq) * TAHVO_MAX_IRQ_BUF_LEN, GFP_KERNEL);
367 printk(KERN_ERR PFX "kmalloc failed\n");
370 memset(irq, 0, sizeof(*irq) * TAHVO_MAX_IRQ_BUF_LEN);
371 for (i = 0; i < TAHVO_MAX_IRQ_BUF_LEN; i++)
372 list_add(&irq[i].node, &tahvo_irqs_reserve);
374 tahvo_irq_block = irq;
376 spin_lock_init(&tahvo_irqs_lock);
377 mutex_init(&tahvo_mutex);
379 /* Request a misc device */
380 res = misc_register(&tahvo_device);
382 printk(KERN_ERR PFX "unable to register misc device for %s\n",
394 void tahvo_user_cleanup(void)
396 /* Unregister our misc device */
397 misc_deregister(&tahvo_device);
398 /* Unregister and disable all TAHVO interrupts */
399 tahvo_unreg_irq_handlers();
400 kfree(tahvo_irq_block);
403 MODULE_DESCRIPTION("Tahvo ASIC user space functions");
404 MODULE_LICENSE("GPL");
405 MODULE_AUTHOR("Mikko Ylinen");