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 <linux/poll.h>
24 #include <linux/sched.h>
25 #include <linux/wait.h>
26 #include <asm/mipsmtregs.h>
27 #include <asm/bitops.h>
29 #include <asm/processor.h>
31 #include <asm/uaccess.h>
33 #define RTLX_TARG_VPE 1
35 static struct rtlx_info *rtlx;
37 static char module_name[] = "rtlx";
38 static struct irqaction irq;
41 static inline int spacefree(int read, int write, int size)
45 * never fill the buffer completely, so indexes are always
46 * equal if empty and only empty, or !equal if data available
51 return ((read + size - write) % size) - 1;
54 static struct chan_waitqueues {
55 wait_queue_head_t rt_queue;
56 wait_queue_head_t lx_queue;
57 } channel_wqs[RTLX_CHANNELS];
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 static irqreturn_t rtlx_interrupt(int irq, void *dev_id, struct pt_regs *regs)
70 for (i = 0; i < RTLX_CHANNELS; i++) {
71 struct rtlx_channel *chan = &rtlx->channel[i];
73 if (chan->lx_read != chan->lx_write)
74 wake_up_interruptible(&channel_wqs[i].lx_queue);
80 /* call when we have the address of the shared structure from the SP side. */
81 static int rtlx_init(struct rtlx_info *rtlxi)
85 if (rtlxi->id != RTLX_ID) {
86 printk(KERN_WARNING "no valid RTLX id at 0x%p\n", rtlxi);
90 /* initialise the wait queues */
91 for (i = 0; i < RTLX_CHANNELS; i++) {
92 init_waitqueue_head(&channel_wqs[i].rt_queue);
93 init_waitqueue_head(&channel_wqs[i].lx_queue);
96 /* set up for interrupt handling */
97 memset(&irq, 0, sizeof(struct irqaction));
100 set_vi_handler(MIPS_CPU_RTLX_IRQ, rtlx_dispatch);
102 irq_num = MIPSCPU_INT_BASE + MIPS_CPU_RTLX_IRQ;
103 irq.handler = rtlx_interrupt;
104 irq.flags = SA_INTERRUPT;
107 setup_irq(irq_num, &irq);
114 /* only allow one open process at a time to open each channel */
115 static int rtlx_open(struct inode *inode, struct file *filp)
118 struct rtlx_channel *chan;
120 /* assume only 1 device at the mo. */
121 minor = MINOR(inode->i_rdev);
124 struct rtlx_info **p;
125 if( (p = vpe_get_shared(RTLX_TARG_VPE)) == NULL) {
126 printk(KERN_ERR "vpe_get_shared is NULL. "
127 "Has an SP program been loaded?\n");
132 printk(KERN_ERR "vpe_shared %p %p\n", p, *p);
136 if ((ret = rtlx_init(*p)) < 0)
140 chan = &rtlx->channel[minor];
142 if (test_and_set_bit(RTLX_STATE_OPENED, &chan->lx_state))
148 static int rtlx_release(struct inode *inode, struct file *filp)
150 int minor = MINOR(inode->i_rdev);
152 clear_bit(RTLX_STATE_OPENED, &rtlx->channel[minor].lx_state);
153 smp_mb__after_clear_bit();
158 static unsigned int rtlx_poll(struct file *file, poll_table * wait)
161 unsigned int mask = 0;
162 struct rtlx_channel *chan;
164 minor = MINOR(file->f_dentry->d_inode->i_rdev);
165 chan = &rtlx->channel[minor];
167 poll_wait(file, &channel_wqs[minor].rt_queue, wait);
168 poll_wait(file, &channel_wqs[minor].lx_queue, wait);
170 /* data available to read? */
171 if (chan->lx_read != chan->lx_write)
172 mask |= POLLIN | POLLRDNORM;
175 if (spacefree(chan->rt_read, chan->rt_write, chan->buffer_size))
176 mask |= POLLOUT | POLLWRNORM;
181 static ssize_t rtlx_read(struct file *file, char __user * buffer, size_t count,
184 unsigned long failed;
187 struct rtlx_channel *lx;
188 DECLARE_WAITQUEUE(wait, current);
190 minor = MINOR(file->f_dentry->d_inode->i_rdev);
191 lx = &rtlx->channel[minor];
193 /* data available? */
194 if (lx->lx_write == lx->lx_read) {
195 if (file->f_flags & O_NONBLOCK)
196 return 0; /* -EAGAIN makes cat whinge */
199 add_wait_queue(&channel_wqs[minor].lx_queue, &wait);
200 set_current_state(TASK_INTERRUPTIBLE);
202 while (lx->lx_write == lx->lx_read)
205 set_current_state(TASK_RUNNING);
206 remove_wait_queue(&channel_wqs[minor].lx_queue, &wait);
211 /* find out how much in total */
213 (size_t)(lx->lx_write + lx->buffer_size - lx->lx_read) % lx->buffer_size);
215 /* then how much from the read pointer onwards */
216 fl = min(count, (size_t)lx->buffer_size - lx->lx_read);
218 failed = copy_to_user (buffer, &lx->lx_buffer[lx->lx_read], fl);
224 /* and if there is anything left at the beginning of the buffer */
226 failed = copy_to_user (buffer + fl, lx->lx_buffer, count - fl);
234 /* update the index */
235 lx->lx_read += count;
236 lx->lx_read %= lx->buffer_size;
241 static ssize_t rtlx_write(struct file *file, const char __user * buffer,
242 size_t count, loff_t * ppos)
244 unsigned long failed;
246 struct rtlx_channel *rt;
248 DECLARE_WAITQUEUE(wait, current);
250 minor = MINOR(file->f_dentry->d_inode->i_rdev);
251 rt = &rtlx->channel[minor];
253 /* any space left... */
254 if (!spacefree(rt->rt_read, rt->rt_write, rt->buffer_size)) {
256 if (file->f_flags & O_NONBLOCK)
259 add_wait_queue(&channel_wqs[minor].rt_queue, &wait);
260 set_current_state(TASK_INTERRUPTIBLE);
262 while (!spacefree(rt->rt_read, rt->rt_write, rt->buffer_size))
265 set_current_state(TASK_RUNNING);
266 remove_wait_queue(&channel_wqs[minor].rt_queue, &wait);
269 /* total number of bytes to copy */
270 count = min(count, (size_t)spacefree(rt->rt_read, rt->rt_write, rt->buffer_size) );
272 /* first bit from write pointer to the end of the buffer, or count */
273 fl = min(count, (size_t) rt->buffer_size - rt->rt_write);
275 failed = copy_from_user(&rt->rt_buffer[rt->rt_write], buffer, fl);
281 /* if there's any left copy to the beginning of the buffer */
283 failed = copy_from_user(rt->rt_buffer, buffer + fl, count - fl);
291 rt->rt_write += count;
292 rt->rt_write %= rt->buffer_size;
297 static struct file_operations rtlx_fops = {
298 .owner = THIS_MODULE,
300 .release = rtlx_release,
306 static char register_chrdev_failed[] __initdata =
307 KERN_ERR "rtlx_module_init: unable to register device\n";
309 static int __init rtlx_module_init(void)
311 major = register_chrdev(0, module_name, &rtlx_fops);
313 printk(register_chrdev_failed);
320 static void __exit rtlx_module_exit(void)
322 unregister_chrdev(major, module_name);
325 module_init(rtlx_module_init);
326 module_exit(rtlx_module_exit);
328 MODULE_DESCRIPTION("MIPS RTLX");
329 MODULE_AUTHOR("Elizabeth Clarke, MIPS Technologies, Inc.");
330 MODULE_LICENSE("GPL");