blob: 986a9cf230673010e6f9ae678798d74de0a51474 [file] [log] [blame]
Ralf Baechlee01402b2005-07-14 15:57:16 +00001/*
2 * Copyright (C) 2005 MIPS Technologies, Inc. All rights reserved.
Ralf Baechlea84c96e2006-01-15 18:11:28 +00003 * Copyright (C) 2005, 06 Ralf Baechle (ralf@linux-mips.org)
Ralf Baechlee01402b2005-07-14 15:57:16 +00004 *
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.
8 *
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.
13 *
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.
17 *
18 */
19
20#include <linux/kernel.h>
21#include <linux/module.h>
22#include <linux/fs.h>
23#include <linux/init.h>
Ralf Baechlea84c96e2006-01-15 18:11:28 +000024#include <linux/interrupt.h>
25#include <linux/irq.h>
Ralf Baechlee01402b2005-07-14 15:57:16 +000026#include <linux/poll.h>
27#include <linux/sched.h>
28#include <linux/wait.h>
Ralf Baechlea84c96e2006-01-15 18:11:28 +000029
Ralf Baechlee01402b2005-07-14 15:57:16 +000030#include <asm/mipsmtregs.h>
Ralf Baechleafc48412005-10-31 00:30:39 +000031#include <asm/bitops.h>
Ralf Baechlee01402b2005-07-14 15:57:16 +000032#include <asm/cpu.h>
33#include <asm/processor.h>
Ralf Baechlee01402b2005-07-14 15:57:16 +000034#include <asm/rtlx.h>
Ralf Baechleafc48412005-10-31 00:30:39 +000035#include <asm/uaccess.h>
Ralf Baechlee01402b2005-07-14 15:57:16 +000036
Ralf Baechlee01402b2005-07-14 15:57:16 +000037#define RTLX_TARG_VPE 1
38
Ralf Baechleafc48412005-10-31 00:30:39 +000039static struct rtlx_info *rtlx;
Ralf Baechlee01402b2005-07-14 15:57:16 +000040static int major;
41static char module_name[] = "rtlx";
Ralf Baechleafc48412005-10-31 00:30:39 +000042static struct irqaction irq;
43static int irq_num;
44
45static inline int spacefree(int read, int write, int size)
46{
47 if (read == write) {
48 /*
49 * never fill the buffer completely, so indexes are always
50 * equal if empty and only empty, or !equal if data available
51 */
52 return size - 1;
53 }
54
55 return ((read + size - write) % size) - 1;
56}
Ralf Baechlee01402b2005-07-14 15:57:16 +000057
58static struct chan_waitqueues {
59 wait_queue_head_t rt_queue;
60 wait_queue_head_t lx_queue;
61} channel_wqs[RTLX_CHANNELS];
62
Ralf Baechlee01402b2005-07-14 15:57:16 +000063extern void *vpe_get_shared(int index);
64
65static void rtlx_dispatch(struct pt_regs *regs)
66{
67 do_IRQ(MIPSCPU_INT_BASE + MIPS_CPU_RTLX_IRQ, regs);
68}
69
Ralf Baechleafc48412005-10-31 00:30:39 +000070static irqreturn_t rtlx_interrupt(int irq, void *dev_id, struct pt_regs *regs)
Ralf Baechlee01402b2005-07-14 15:57:16 +000071{
Ralf Baechlee01402b2005-07-14 15:57:16 +000072 int i;
73
74 for (i = 0; i < RTLX_CHANNELS; i++) {
75 struct rtlx_channel *chan = &rtlx->channel[i];
76
77 if (chan->lx_read != chan->lx_write)
78 wake_up_interruptible(&channel_wqs[i].lx_queue);
79 }
80
Ralf Baechleafc48412005-10-31 00:30:39 +000081 return IRQ_HANDLED;
Ralf Baechlee01402b2005-07-14 15:57:16 +000082}
83
84/* call when we have the address of the shared structure from the SP side. */
85static int rtlx_init(struct rtlx_info *rtlxi)
86{
87 int i;
88
89 if (rtlxi->id != RTLX_ID) {
90 printk(KERN_WARNING "no valid RTLX id at 0x%p\n", rtlxi);
Ralf Baechleafc48412005-10-31 00:30:39 +000091 return -ENOEXEC;
Ralf Baechlee01402b2005-07-14 15:57:16 +000092 }
93
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);
98 }
99
100 /* set up for interrupt handling */
101 memset(&irq, 0, sizeof(struct irqaction));
102
Ralf Baechleafc48412005-10-31 00:30:39 +0000103 if (cpu_has_vint)
Ralf Baechlee01402b2005-07-14 15:57:16 +0000104 set_vi_handler(MIPS_CPU_RTLX_IRQ, rtlx_dispatch);
Ralf Baechlee01402b2005-07-14 15:57:16 +0000105
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);
112
113 rtlx = rtlxi;
Ralf Baechleafc48412005-10-31 00:30:39 +0000114
115 return 0;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000116}
117
118/* only allow one open process at a time to open each channel */
119static int rtlx_open(struct inode *inode, struct file *filp)
120{
121 int minor, ret;
122 struct rtlx_channel *chan;
123
124 /* assume only 1 device at the mo. */
125 minor = MINOR(inode->i_rdev);
126
127 if (rtlx == NULL) {
128 struct rtlx_info **p;
129 if( (p = vpe_get_shared(RTLX_TARG_VPE)) == NULL) {
Ralf Baechleafc48412005-10-31 00:30:39 +0000130 printk(KERN_ERR "vpe_get_shared is NULL. "
131 "Has an SP program been loaded?\n");
132 return -EFAULT;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000133 }
134
135 if (*p == NULL) {
Ralf Baechleafc48412005-10-31 00:30:39 +0000136 printk(KERN_ERR "vpe_shared %p %p\n", p, *p);
137 return -EFAULT;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000138 }
139
140 if ((ret = rtlx_init(*p)) < 0)
Ralf Baechleafc48412005-10-31 00:30:39 +0000141 return ret;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000142 }
143
144 chan = &rtlx->channel[minor];
145
Ralf Baechleafc48412005-10-31 00:30:39 +0000146 if (test_and_set_bit(RTLX_STATE_OPENED, &chan->lx_state))
147 return -EBUSY;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000148
Ralf Baechleafc48412005-10-31 00:30:39 +0000149 return 0;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000150}
151
152static int rtlx_release(struct inode *inode, struct file *filp)
153{
Ralf Baechleafc48412005-10-31 00:30:39 +0000154 int minor = MINOR(inode->i_rdev);
Ralf Baechlee01402b2005-07-14 15:57:16 +0000155
Ralf Baechleafc48412005-10-31 00:30:39 +0000156 clear_bit(RTLX_STATE_OPENED, &rtlx->channel[minor].lx_state);
157 smp_mb__after_clear_bit();
158
159 return 0;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000160}
161
162static unsigned int rtlx_poll(struct file *file, poll_table * wait)
163{
164 int minor;
165 unsigned int mask = 0;
166 struct rtlx_channel *chan;
167
168 minor = MINOR(file->f_dentry->d_inode->i_rdev);
169 chan = &rtlx->channel[minor];
170
171 poll_wait(file, &channel_wqs[minor].rt_queue, wait);
172 poll_wait(file, &channel_wqs[minor].lx_queue, wait);
173
174 /* data available to read? */
175 if (chan->lx_read != chan->lx_write)
176 mask |= POLLIN | POLLRDNORM;
177
178 /* space to write */
179 if (spacefree(chan->rt_read, chan->rt_write, chan->buffer_size))
180 mask |= POLLOUT | POLLWRNORM;
181
Ralf Baechleafc48412005-10-31 00:30:39 +0000182 return mask;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000183}
184
185static ssize_t rtlx_read(struct file *file, char __user * buffer, size_t count,
186 loff_t * ppos)
187{
Ralf Baechleafc48412005-10-31 00:30:39 +0000188 unsigned long failed;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000189 size_t fl = 0L;
190 int minor;
191 struct rtlx_channel *lx;
192 DECLARE_WAITQUEUE(wait, current);
193
194 minor = MINOR(file->f_dentry->d_inode->i_rdev);
195 lx = &rtlx->channel[minor];
196
197 /* data available? */
198 if (lx->lx_write == lx->lx_read) {
199 if (file->f_flags & O_NONBLOCK)
Ralf Baechleafc48412005-10-31 00:30:39 +0000200 return 0; /* -EAGAIN makes cat whinge */
Ralf Baechlee01402b2005-07-14 15:57:16 +0000201
202 /* go to sleep */
203 add_wait_queue(&channel_wqs[minor].lx_queue, &wait);
204 set_current_state(TASK_INTERRUPTIBLE);
205
206 while (lx->lx_write == lx->lx_read)
207 schedule();
208
209 set_current_state(TASK_RUNNING);
210 remove_wait_queue(&channel_wqs[minor].lx_queue, &wait);
211
212 /* back running */
213 }
214
215 /* find out how much in total */
Ralf Baechleafc48412005-10-31 00:30:39 +0000216 count = min(count,
217 (size_t)(lx->lx_write + lx->buffer_size - lx->lx_read) % lx->buffer_size);
Ralf Baechlee01402b2005-07-14 15:57:16 +0000218
219 /* then how much from the read pointer onwards */
Ralf Baechleafc48412005-10-31 00:30:39 +0000220 fl = min(count, (size_t)lx->buffer_size - lx->lx_read);
Ralf Baechlee01402b2005-07-14 15:57:16 +0000221
Ralf Baechleafc48412005-10-31 00:30:39 +0000222 failed = copy_to_user (buffer, &lx->lx_buffer[lx->lx_read], fl);
223 if (failed) {
224 count = fl - failed;
225 goto out;
226 }
Ralf Baechlee01402b2005-07-14 15:57:16 +0000227
228 /* and if there is anything left at the beginning of the buffer */
Ralf Baechleafc48412005-10-31 00:30:39 +0000229 if (count - fl) {
230 failed = copy_to_user (buffer + fl, lx->lx_buffer, count - fl);
231 if (failed) {
232 count -= failed;
233 goto out;
234 }
235 }
Ralf Baechlee01402b2005-07-14 15:57:16 +0000236
Ralf Baechleafc48412005-10-31 00:30:39 +0000237out:
Ralf Baechlee01402b2005-07-14 15:57:16 +0000238 /* update the index */
239 lx->lx_read += count;
240 lx->lx_read %= lx->buffer_size;
241
Ralf Baechleafc48412005-10-31 00:30:39 +0000242 return count;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000243}
244
245static ssize_t rtlx_write(struct file *file, const char __user * buffer,
246 size_t count, loff_t * ppos)
247{
Ralf Baechleafc48412005-10-31 00:30:39 +0000248 unsigned long failed;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000249 int minor;
250 struct rtlx_channel *rt;
251 size_t fl;
252 DECLARE_WAITQUEUE(wait, current);
253
254 minor = MINOR(file->f_dentry->d_inode->i_rdev);
255 rt = &rtlx->channel[minor];
256
257 /* any space left... */
258 if (!spacefree(rt->rt_read, rt->rt_write, rt->buffer_size)) {
259
260 if (file->f_flags & O_NONBLOCK)
Ralf Baechleafc48412005-10-31 00:30:39 +0000261 return -EAGAIN;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000262
263 add_wait_queue(&channel_wqs[minor].rt_queue, &wait);
264 set_current_state(TASK_INTERRUPTIBLE);
265
266 while (!spacefree(rt->rt_read, rt->rt_write, rt->buffer_size))
267 schedule();
268
269 set_current_state(TASK_RUNNING);
270 remove_wait_queue(&channel_wqs[minor].rt_queue, &wait);
271 }
272
273 /* total number of bytes to copy */
Ralf Baechleafc48412005-10-31 00:30:39 +0000274 count = min(count, (size_t)spacefree(rt->rt_read, rt->rt_write, rt->buffer_size) );
Ralf Baechlee01402b2005-07-14 15:57:16 +0000275
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);
278
Ralf Baechleafc48412005-10-31 00:30:39 +0000279 failed = copy_from_user(&rt->rt_buffer[rt->rt_write], buffer, fl);
280 if (failed) {
281 count = fl - failed;
282 goto out;
283 }
Ralf Baechlee01402b2005-07-14 15:57:16 +0000284
285 /* if there's any left copy to the beginning of the buffer */
Ralf Baechleafc48412005-10-31 00:30:39 +0000286 if (count - fl) {
287 failed = copy_from_user(rt->rt_buffer, buffer + fl, count - fl);
288 if (failed) {
289 count -= failed;
290 goto out;
291 }
292 }
Ralf Baechlee01402b2005-07-14 15:57:16 +0000293
Ralf Baechleafc48412005-10-31 00:30:39 +0000294out:
Ralf Baechlee01402b2005-07-14 15:57:16 +0000295 rt->rt_write += count;
296 rt->rt_write %= rt->buffer_size;
297
Ralf Baechleafc48412005-10-31 00:30:39 +0000298 return count;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000299}
300
301static struct file_operations rtlx_fops = {
Ralf Baechleafc48412005-10-31 00:30:39 +0000302 .owner = THIS_MODULE,
303 .open = rtlx_open,
304 .release = rtlx_release,
305 .write = rtlx_write,
306 .read = rtlx_read,
307 .poll = rtlx_poll
Ralf Baechlee01402b2005-07-14 15:57:16 +0000308};
309
Ralf Baechleafc48412005-10-31 00:30:39 +0000310static char register_chrdev_failed[] __initdata =
311 KERN_ERR "rtlx_module_init: unable to register device\n";
312
313static int __init rtlx_module_init(void)
Ralf Baechlee01402b2005-07-14 15:57:16 +0000314{
Ralf Baechleafc48412005-10-31 00:30:39 +0000315 major = register_chrdev(0, module_name, &rtlx_fops);
316 if (major < 0) {
317 printk(register_chrdev_failed);
318 return major;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000319 }
320
Ralf Baechleafc48412005-10-31 00:30:39 +0000321 return 0;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000322}
323
Ralf Baechleafc48412005-10-31 00:30:39 +0000324static void __exit rtlx_module_exit(void)
Ralf Baechlee01402b2005-07-14 15:57:16 +0000325{
326 unregister_chrdev(major, module_name);
327}
328
329module_init(rtlx_module_init);
330module_exit(rtlx_module_exit);
Ralf Baechleafc48412005-10-31 00:30:39 +0000331
Ralf Baechlee01402b2005-07-14 15:57:16 +0000332MODULE_DESCRIPTION("MIPS RTLX");
Ralf Baechleafc48412005-10-31 00:30:39 +0000333MODULE_AUTHOR("Elizabeth Clarke, MIPS Technologies, Inc.");
Ralf Baechlee01402b2005-07-14 15:57:16 +0000334MODULE_LICENSE("GPL");