2 * Copyright (C) 2005 MIPS Technologies, Inc. All rights reserved.
4 * This program is free software; you can distribute it and/or modify it
5 * under the terms of the GNU General Public License (Version 2) as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
19 #include <linux/kernel.h>
20 #include <linux/module.h>
22 #include <linux/init.h>
23 #include <asm/uaccess.h>
24 #include <linux/slab.h>
25 #include <linux/list.h>
26 #include <linux/vmalloc.h>
27 #include <linux/elf.h>
28 #include <linux/seq_file.h>
29 #include <linux/syscalls.h>
30 #include <linux/moduleloader.h>
31 #include <linux/interrupt.h>
32 #include <linux/poll.h>
33 #include <linux/sched.h>
34 #include <linux/wait.h>
35 #include <asm/mipsmtregs.h>
36 #include <asm/cacheflush.h>
37 #include <asm/atomic.h>
39 #include <asm/processor.h>
40 #include <asm/system.h>
44 #define RTLX_TARG_VPE 1
46 struct rtlx_info *rtlx;
48 static char module_name[] = "rtlx";
49 static inline int spacefree(int read, int write, int size);
51 static struct chan_waitqueues {
52 wait_queue_head_t rt_queue;
53 wait_queue_head_t lx_queue;
54 } channel_wqs[RTLX_CHANNELS];
56 static struct irqaction irq;
59 extern void *vpe_get_shared(int index);
61 static void rtlx_dispatch(struct pt_regs *regs)
63 do_IRQ(MIPSCPU_INT_BASE + MIPS_CPU_RTLX_IRQ, regs);
66 irqreturn_t rtlx_interrupt(int irq, void *dev_id, struct pt_regs *regs)
68 irqreturn_t r = IRQ_HANDLED;
71 for (i = 0; i < RTLX_CHANNELS; i++) {
72 struct rtlx_channel *chan = &rtlx->channel[i];
74 if (chan->lx_read != chan->lx_write)
75 wake_up_interruptible(&channel_wqs[i].lx_queue);
85 printk("id 0x%lx state %d\n", rtlx->id, rtlx->state);
87 for (i = 0; i < RTLX_CHANNELS; i++) {
88 struct rtlx_channel *chan = &rtlx->channel[i];
90 printk(" rt_state %d lx_state %d buffer_size %d\n",
91 chan->rt_state, chan->lx_state, chan->buffer_size);
93 printk(" rt_read %d rt_write %d\n",
94 chan->rt_read, chan->rt_write);
96 printk(" lx_read %d lx_write %d\n",
97 chan->lx_read, chan->lx_write);
99 printk(" rt_buffer <%s>\n", chan->rt_buffer);
100 printk(" lx_buffer <%s>\n", chan->lx_buffer);
104 /* call when we have the address of the shared structure from the SP side. */
105 static int rtlx_init(struct rtlx_info *rtlxi)
109 if (rtlxi->id != RTLX_ID) {
110 printk(KERN_WARNING "no valid RTLX id at 0x%p\n", rtlxi);
114 /* initialise the wait queues */
115 for (i = 0; i < RTLX_CHANNELS; i++) {
116 init_waitqueue_head(&channel_wqs[i].rt_queue);
117 init_waitqueue_head(&channel_wqs[i].lx_queue);
120 /* set up for interrupt handling */
121 memset(&irq, 0, sizeof(struct irqaction));
124 set_vi_handler(MIPS_CPU_RTLX_IRQ, rtlx_dispatch);
127 irq_num = MIPSCPU_INT_BASE + MIPS_CPU_RTLX_IRQ;
128 irq.handler = rtlx_interrupt;
129 irq.flags = SA_INTERRUPT;
132 setup_irq(irq_num, &irq);
138 /* only allow one open process at a time to open each channel */
139 static int rtlx_open(struct inode *inode, struct file *filp)
142 struct rtlx_channel *chan;
144 /* assume only 1 device at the mo. */
145 minor = MINOR(inode->i_rdev);
148 struct rtlx_info **p;
149 if( (p = vpe_get_shared(RTLX_TARG_VPE)) == NULL) {
150 printk(" vpe_get_shared is NULL. Has an SP program been loaded?\n");
155 printk(" vpe_shared %p %p\n", p, *p);
159 if ((ret = rtlx_init(*p)) < 0)
163 chan = &rtlx->channel[minor];
166 if (chan->lx_state == RTLX_STATE_OPENED)
169 chan->lx_state = RTLX_STATE_OPENED;
173 static int rtlx_release(struct inode *inode, struct file *filp)
177 minor = MINOR(inode->i_rdev);
178 rtlx->channel[minor].lx_state = RTLX_STATE_UNUSED;
182 static unsigned int rtlx_poll(struct file *file, poll_table * wait)
185 unsigned int mask = 0;
186 struct rtlx_channel *chan;
188 minor = MINOR(file->f_dentry->d_inode->i_rdev);
189 chan = &rtlx->channel[minor];
191 poll_wait(file, &channel_wqs[minor].rt_queue, wait);
192 poll_wait(file, &channel_wqs[minor].lx_queue, wait);
194 /* data available to read? */
195 if (chan->lx_read != chan->lx_write)
196 mask |= POLLIN | POLLRDNORM;
199 if (spacefree(chan->rt_read, chan->rt_write, chan->buffer_size))
200 mask |= POLLOUT | POLLWRNORM;
205 static ssize_t rtlx_read(struct file *file, char __user * buffer, size_t count,
210 struct rtlx_channel *lx;
211 DECLARE_WAITQUEUE(wait, current);
213 minor = MINOR(file->f_dentry->d_inode->i_rdev);
214 lx = &rtlx->channel[minor];
216 /* data available? */
217 if (lx->lx_write == lx->lx_read) {
218 if (file->f_flags & O_NONBLOCK)
219 return (0); // -EAGAIN makes cat whinge
222 add_wait_queue(&channel_wqs[minor].lx_queue, &wait);
223 set_current_state(TASK_INTERRUPTIBLE);
225 while (lx->lx_write == lx->lx_read)
228 set_current_state(TASK_RUNNING);
229 remove_wait_queue(&channel_wqs[minor].lx_queue, &wait);
234 /* find out how much in total */
236 (size_t)(lx->lx_write + lx->buffer_size - lx->lx_read) % lx->buffer_size);
238 /* then how much from the read pointer onwards */
239 fl = min( count, (size_t)lx->buffer_size - lx->lx_read);
241 copy_to_user (buffer, &lx->lx_buffer[lx->lx_read], fl);
243 /* and if there is anything left at the beginning of the buffer */
245 copy_to_user (buffer + fl, lx->lx_buffer, count - fl);
247 /* update the index */
248 lx->lx_read += count;
249 lx->lx_read %= lx->buffer_size;
254 static inline int spacefree(int read, int write, int size)
257 /* never fill the buffer completely, so indexes are always equal if empty
258 and only empty, or !equal if data available */
262 return ((read + size - write) % size) - 1;
265 static ssize_t rtlx_write(struct file *file, const char __user * buffer,
266 size_t count, loff_t * ppos)
269 struct rtlx_channel *rt;
271 DECLARE_WAITQUEUE(wait, current);
273 minor = MINOR(file->f_dentry->d_inode->i_rdev);
274 rt = &rtlx->channel[minor];
276 /* any space left... */
277 if (!spacefree(rt->rt_read, rt->rt_write, rt->buffer_size)) {
279 if (file->f_flags & O_NONBLOCK)
282 add_wait_queue(&channel_wqs[minor].rt_queue, &wait);
283 set_current_state(TASK_INTERRUPTIBLE);
285 while (!spacefree(rt->rt_read, rt->rt_write, rt->buffer_size))
288 set_current_state(TASK_RUNNING);
289 remove_wait_queue(&channel_wqs[minor].rt_queue, &wait);
292 /* total number of bytes to copy */
293 count = min( count, (size_t)spacefree(rt->rt_read, rt->rt_write, rt->buffer_size) );
295 /* first bit from write pointer to the end of the buffer, or count */
296 fl = min(count, (size_t) rt->buffer_size - rt->rt_write);
298 copy_from_user(&rt->rt_buffer[rt->rt_write], buffer, fl);
300 /* if there's any left copy to the beginning of the buffer */
302 copy_from_user(rt->rt_buffer, buffer + fl, count - fl);
304 rt->rt_write += count;
305 rt->rt_write %= rt->buffer_size;
310 static struct file_operations rtlx_fops = {
311 .owner = THIS_MODULE,
313 .release = rtlx_release,
319 static int rtlx_module_init(void)
321 if ((major = register_chrdev(RTLX_MAJOR, module_name, &rtlx_fops)) < 0) {
322 printk("rtlx_module_init: unable to register device\n");
332 static void rtlx_module_exit(void)
334 unregister_chrdev(major, module_name);
337 module_init(rtlx_module_init);
338 module_exit(rtlx_module_exit);
339 MODULE_DESCRIPTION("MIPS RTLX");
340 MODULE_AUTHOR("Elizabeth Clarke, MIPS Technologies, Inc");
341 MODULE_LICENSE("GPL");