[ARM] CONFIG_CPU_MPCORE -> CONFIG_CPU_32v6K
[linux-2.6/verdex.git] / arch / mips / kernel / rtlx.c
blob986a9cf230673010e6f9ae678798d74de0a51474
1 /*
2 * Copyright (C) 2005 MIPS Technologies, Inc. All rights reserved.
3 * Copyright (C) 2005, 06 Ralf Baechle (ralf@linux-mips.org)
5 * This program is free software; you can distribute it and/or modify it
6 * under the terms of the GNU General Public License (Version 2) as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/fs.h>
23 #include <linux/init.h>
24 #include <linux/interrupt.h>
25 #include <linux/irq.h>
26 #include <linux/poll.h>
27 #include <linux/sched.h>
28 #include <linux/wait.h>
30 #include <asm/mipsmtregs.h>
31 #include <asm/bitops.h>
32 #include <asm/cpu.h>
33 #include <asm/processor.h>
34 #include <asm/rtlx.h>
35 #include <asm/uaccess.h>
37 #define RTLX_TARG_VPE 1
39 static struct rtlx_info *rtlx;
40 static int major;
41 static char module_name[] = "rtlx";
42 static struct irqaction irq;
43 static int irq_num;
45 static inline int spacefree(int read, int write, int size)
47 if (read == write) {
49 * never fill the buffer completely, so indexes are always
50 * equal if empty and only empty, or !equal if data available
52 return size - 1;
55 return ((read + size - write) % size) - 1;
58 static struct chan_waitqueues {
59 wait_queue_head_t rt_queue;
60 wait_queue_head_t lx_queue;
61 } channel_wqs[RTLX_CHANNELS];
63 extern void *vpe_get_shared(int index);
65 static void rtlx_dispatch(struct pt_regs *regs)
67 do_IRQ(MIPSCPU_INT_BASE + MIPS_CPU_RTLX_IRQ, regs);
70 static irqreturn_t rtlx_interrupt(int irq, void *dev_id, struct pt_regs *regs)
72 int i;
74 for (i = 0; i < RTLX_CHANNELS; i++) {
75 struct rtlx_channel *chan = &rtlx->channel[i];
77 if (chan->lx_read != chan->lx_write)
78 wake_up_interruptible(&channel_wqs[i].lx_queue);
81 return IRQ_HANDLED;
84 /* call when we have the address of the shared structure from the SP side. */
85 static int rtlx_init(struct rtlx_info *rtlxi)
87 int i;
89 if (rtlxi->id != RTLX_ID) {
90 printk(KERN_WARNING "no valid RTLX id at 0x%p\n", rtlxi);
91 return -ENOEXEC;
94 /* initialise the wait queues */
95 for (i = 0; i < RTLX_CHANNELS; i++) {
96 init_waitqueue_head(&channel_wqs[i].rt_queue);
97 init_waitqueue_head(&channel_wqs[i].lx_queue);
100 /* set up for interrupt handling */
101 memset(&irq, 0, sizeof(struct irqaction));
103 if (cpu_has_vint)
104 set_vi_handler(MIPS_CPU_RTLX_IRQ, rtlx_dispatch);
106 irq_num = MIPSCPU_INT_BASE + MIPS_CPU_RTLX_IRQ;
107 irq.handler = rtlx_interrupt;
108 irq.flags = SA_INTERRUPT;
109 irq.name = "RTLX";
110 irq.dev_id = rtlx;
111 setup_irq(irq_num, &irq);
113 rtlx = rtlxi;
115 return 0;
118 /* only allow one open process at a time to open each channel */
119 static int rtlx_open(struct inode *inode, struct file *filp)
121 int minor, ret;
122 struct rtlx_channel *chan;
124 /* assume only 1 device at the mo. */
125 minor = MINOR(inode->i_rdev);
127 if (rtlx == NULL) {
128 struct rtlx_info **p;
129 if( (p = vpe_get_shared(RTLX_TARG_VPE)) == NULL) {
130 printk(KERN_ERR "vpe_get_shared is NULL. "
131 "Has an SP program been loaded?\n");
132 return -EFAULT;
135 if (*p == NULL) {
136 printk(KERN_ERR "vpe_shared %p %p\n", p, *p);
137 return -EFAULT;
140 if ((ret = rtlx_init(*p)) < 0)
141 return ret;
144 chan = &rtlx->channel[minor];
146 if (test_and_set_bit(RTLX_STATE_OPENED, &chan->lx_state))
147 return -EBUSY;
149 return 0;
152 static int rtlx_release(struct inode *inode, struct file *filp)
154 int minor = MINOR(inode->i_rdev);
156 clear_bit(RTLX_STATE_OPENED, &rtlx->channel[minor].lx_state);
157 smp_mb__after_clear_bit();
159 return 0;
162 static unsigned int rtlx_poll(struct file *file, poll_table * wait)
164 int minor;
165 unsigned int mask = 0;
166 struct rtlx_channel *chan;
168 minor = MINOR(file->f_dentry->d_inode->i_rdev);
169 chan = &rtlx->channel[minor];
171 poll_wait(file, &channel_wqs[minor].rt_queue, wait);
172 poll_wait(file, &channel_wqs[minor].lx_queue, wait);
174 /* data available to read? */
175 if (chan->lx_read != chan->lx_write)
176 mask |= POLLIN | POLLRDNORM;
178 /* space to write */
179 if (spacefree(chan->rt_read, chan->rt_write, chan->buffer_size))
180 mask |= POLLOUT | POLLWRNORM;
182 return mask;
185 static ssize_t rtlx_read(struct file *file, char __user * buffer, size_t count,
186 loff_t * ppos)
188 unsigned long failed;
189 size_t fl = 0L;
190 int minor;
191 struct rtlx_channel *lx;
192 DECLARE_WAITQUEUE(wait, current);
194 minor = MINOR(file->f_dentry->d_inode->i_rdev);
195 lx = &rtlx->channel[minor];
197 /* data available? */
198 if (lx->lx_write == lx->lx_read) {
199 if (file->f_flags & O_NONBLOCK)
200 return 0; /* -EAGAIN makes cat whinge */
202 /* go to sleep */
203 add_wait_queue(&channel_wqs[minor].lx_queue, &wait);
204 set_current_state(TASK_INTERRUPTIBLE);
206 while (lx->lx_write == lx->lx_read)
207 schedule();
209 set_current_state(TASK_RUNNING);
210 remove_wait_queue(&channel_wqs[minor].lx_queue, &wait);
212 /* back running */
215 /* find out how much in total */
216 count = min(count,
217 (size_t)(lx->lx_write + lx->buffer_size - lx->lx_read) % lx->buffer_size);
219 /* then how much from the read pointer onwards */
220 fl = min(count, (size_t)lx->buffer_size - lx->lx_read);
222 failed = copy_to_user (buffer, &lx->lx_buffer[lx->lx_read], fl);
223 if (failed) {
224 count = fl - failed;
225 goto out;
228 /* and if there is anything left at the beginning of the buffer */
229 if (count - fl) {
230 failed = copy_to_user (buffer + fl, lx->lx_buffer, count - fl);
231 if (failed) {
232 count -= failed;
233 goto out;
237 out:
238 /* update the index */
239 lx->lx_read += count;
240 lx->lx_read %= lx->buffer_size;
242 return count;
245 static ssize_t rtlx_write(struct file *file, const char __user * buffer,
246 size_t count, loff_t * ppos)
248 unsigned long failed;
249 int minor;
250 struct rtlx_channel *rt;
251 size_t fl;
252 DECLARE_WAITQUEUE(wait, current);
254 minor = MINOR(file->f_dentry->d_inode->i_rdev);
255 rt = &rtlx->channel[minor];
257 /* any space left... */
258 if (!spacefree(rt->rt_read, rt->rt_write, rt->buffer_size)) {
260 if (file->f_flags & O_NONBLOCK)
261 return -EAGAIN;
263 add_wait_queue(&channel_wqs[minor].rt_queue, &wait);
264 set_current_state(TASK_INTERRUPTIBLE);
266 while (!spacefree(rt->rt_read, rt->rt_write, rt->buffer_size))
267 schedule();
269 set_current_state(TASK_RUNNING);
270 remove_wait_queue(&channel_wqs[minor].rt_queue, &wait);
273 /* total number of bytes to copy */
274 count = min(count, (size_t)spacefree(rt->rt_read, rt->rt_write, rt->buffer_size) );
276 /* first bit from write pointer to the end of the buffer, or count */
277 fl = min(count, (size_t) rt->buffer_size - rt->rt_write);
279 failed = copy_from_user(&rt->rt_buffer[rt->rt_write], buffer, fl);
280 if (failed) {
281 count = fl - failed;
282 goto out;
285 /* if there's any left copy to the beginning of the buffer */
286 if (count - fl) {
287 failed = copy_from_user(rt->rt_buffer, buffer + fl, count - fl);
288 if (failed) {
289 count -= failed;
290 goto out;
294 out:
295 rt->rt_write += count;
296 rt->rt_write %= rt->buffer_size;
298 return count;
301 static struct file_operations rtlx_fops = {
302 .owner = THIS_MODULE,
303 .open = rtlx_open,
304 .release = rtlx_release,
305 .write = rtlx_write,
306 .read = rtlx_read,
307 .poll = rtlx_poll
310 static char register_chrdev_failed[] __initdata =
311 KERN_ERR "rtlx_module_init: unable to register device\n";
313 static int __init rtlx_module_init(void)
315 major = register_chrdev(0, module_name, &rtlx_fops);
316 if (major < 0) {
317 printk(register_chrdev_failed);
318 return major;
321 return 0;
324 static void __exit rtlx_module_exit(void)
326 unregister_chrdev(major, module_name);
329 module_init(rtlx_module_init);
330 module_exit(rtlx_module_exit);
332 MODULE_DESCRIPTION("MIPS RTLX");
333 MODULE_AUTHOR("Elizabeth Clarke, MIPS Technologies, Inc.");
334 MODULE_LICENSE("GPL");