xref: /linux/arch/mips/kernel/rtlx.c (revision 9ce7677cfd7cd871adb457c80bea3b581b839641)
1 /*
2  * Copyright (C) 2005 MIPS Technologies, Inc.  All rights reserved.
3  *
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.
7  *
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
11  *  for more details.
12  *
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.
16  *
17  */
18 
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/fs.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>
28 #include <asm/cpu.h>
29 #include <asm/processor.h>
30 #include <asm/rtlx.h>
31 #include <asm/uaccess.h>
32 
33 #define RTLX_TARG_VPE 1
34 
35 static struct rtlx_info *rtlx;
36 static int major;
37 static char module_name[] = "rtlx";
38 static struct irqaction irq;
39 static int irq_num;
40 
41 static inline int spacefree(int read, int write, int size)
42 {
43 	if (read == write) {
44 		/*
45 		 * never fill the buffer completely, so indexes are always
46 		 * equal if empty and only empty, or !equal if data available
47 		 */
48 		return size - 1;
49 	}
50 
51 	return ((read + size - write) % size) - 1;
52 }
53 
54 static struct chan_waitqueues {
55 	wait_queue_head_t rt_queue;
56 	wait_queue_head_t lx_queue;
57 } channel_wqs[RTLX_CHANNELS];
58 
59 extern void *vpe_get_shared(int index);
60 
61 static void rtlx_dispatch(struct pt_regs *regs)
62 {
63 	do_IRQ(MIPSCPU_INT_BASE + MIPS_CPU_RTLX_IRQ, regs);
64 }
65 
66 static irqreturn_t rtlx_interrupt(int irq, void *dev_id, struct pt_regs *regs)
67 {
68 	int i;
69 
70 	for (i = 0; i < RTLX_CHANNELS; i++) {
71 		struct rtlx_channel *chan = &rtlx->channel[i];
72 
73 		if (chan->lx_read != chan->lx_write)
74 			wake_up_interruptible(&channel_wqs[i].lx_queue);
75 	}
76 
77 	return IRQ_HANDLED;
78 }
79 
80 /* call when we have the address of the shared structure from the SP side. */
81 static int rtlx_init(struct rtlx_info *rtlxi)
82 {
83 	int i;
84 
85 	if (rtlxi->id != RTLX_ID) {
86 		printk(KERN_WARNING "no valid RTLX id at 0x%p\n", rtlxi);
87 		return -ENOEXEC;
88 	}
89 
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);
94 	}
95 
96 	/* set up for interrupt handling */
97 	memset(&irq, 0, sizeof(struct irqaction));
98 
99 	if (cpu_has_vint)
100 		set_vi_handler(MIPS_CPU_RTLX_IRQ, rtlx_dispatch);
101 
102 	irq_num = MIPSCPU_INT_BASE + MIPS_CPU_RTLX_IRQ;
103 	irq.handler = rtlx_interrupt;
104 	irq.flags = SA_INTERRUPT;
105 	irq.name = "RTLX";
106 	irq.dev_id = rtlx;
107 	setup_irq(irq_num, &irq);
108 
109 	rtlx = rtlxi;
110 
111 	return 0;
112 }
113 
114 /* only allow one open process at a time to open each channel */
115 static int rtlx_open(struct inode *inode, struct file *filp)
116 {
117 	int minor, ret;
118 	struct rtlx_channel *chan;
119 
120 	/* assume only 1 device at the mo. */
121 	minor = MINOR(inode->i_rdev);
122 
123 	if (rtlx == NULL) {
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");
128 			return -EFAULT;
129 		}
130 
131 		if (*p == NULL) {
132 			printk(KERN_ERR "vpe_shared %p %p\n", p, *p);
133 			return -EFAULT;
134 		}
135 
136 		if ((ret = rtlx_init(*p)) < 0)
137 			return ret;
138 	}
139 
140 	chan = &rtlx->channel[minor];
141 
142 	if (test_and_set_bit(RTLX_STATE_OPENED, &chan->lx_state))
143 		return -EBUSY;
144 
145 	return 0;
146 }
147 
148 static int rtlx_release(struct inode *inode, struct file *filp)
149 {
150 	int minor = MINOR(inode->i_rdev);
151 
152 	clear_bit(RTLX_STATE_OPENED, &rtlx->channel[minor].lx_state);
153 	smp_mb__after_clear_bit();
154 
155 	return 0;
156 }
157 
158 static unsigned int rtlx_poll(struct file *file, poll_table * wait)
159 {
160 	int minor;
161 	unsigned int mask = 0;
162 	struct rtlx_channel *chan;
163 
164 	minor = MINOR(file->f_dentry->d_inode->i_rdev);
165 	chan = &rtlx->channel[minor];
166 
167 	poll_wait(file, &channel_wqs[minor].rt_queue, wait);
168 	poll_wait(file, &channel_wqs[minor].lx_queue, wait);
169 
170 	/* data available to read? */
171 	if (chan->lx_read != chan->lx_write)
172 		mask |= POLLIN | POLLRDNORM;
173 
174 	/* space to write */
175 	if (spacefree(chan->rt_read, chan->rt_write, chan->buffer_size))
176 		mask |= POLLOUT | POLLWRNORM;
177 
178 	return mask;
179 }
180 
181 static ssize_t rtlx_read(struct file *file, char __user * buffer, size_t count,
182 			 loff_t * ppos)
183 {
184 	unsigned long failed;
185 	size_t fl = 0L;
186 	int minor;
187 	struct rtlx_channel *lx;
188 	DECLARE_WAITQUEUE(wait, current);
189 
190 	minor = MINOR(file->f_dentry->d_inode->i_rdev);
191 	lx = &rtlx->channel[minor];
192 
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 */
197 
198 		/* go to sleep */
199 		add_wait_queue(&channel_wqs[minor].lx_queue, &wait);
200 		set_current_state(TASK_INTERRUPTIBLE);
201 
202 		while (lx->lx_write == lx->lx_read)
203 			schedule();
204 
205 		set_current_state(TASK_RUNNING);
206 		remove_wait_queue(&channel_wqs[minor].lx_queue, &wait);
207 
208 		/* back running */
209 	}
210 
211 	/* find out how much in total */
212 	count = min(count,
213 		    (size_t)(lx->lx_write + lx->buffer_size - lx->lx_read) % lx->buffer_size);
214 
215 	/* then how much from the read pointer onwards */
216 	fl = min(count, (size_t)lx->buffer_size - lx->lx_read);
217 
218 	failed = copy_to_user (buffer, &lx->lx_buffer[lx->lx_read], fl);
219 	if (failed) {
220 		count = fl - failed;
221 		goto out;
222 	}
223 
224 	/* and if there is anything left at the beginning of the buffer */
225 	if (count - fl) {
226 		failed = copy_to_user (buffer + fl, lx->lx_buffer, count - fl);
227 		if (failed) {
228 			count -= failed;
229 			goto out;
230 		}
231 	}
232 
233 out:
234 	/* update the index */
235 	lx->lx_read += count;
236 	lx->lx_read %= lx->buffer_size;
237 
238 	return count;
239 }
240 
241 static ssize_t rtlx_write(struct file *file, const char __user * buffer,
242 			  size_t count, loff_t * ppos)
243 {
244 	unsigned long failed;
245 	int minor;
246 	struct rtlx_channel *rt;
247 	size_t fl;
248 	DECLARE_WAITQUEUE(wait, current);
249 
250 	minor = MINOR(file->f_dentry->d_inode->i_rdev);
251 	rt = &rtlx->channel[minor];
252 
253 	/* any space left... */
254 	if (!spacefree(rt->rt_read, rt->rt_write, rt->buffer_size)) {
255 
256 		if (file->f_flags & O_NONBLOCK)
257 			return -EAGAIN;
258 
259 		add_wait_queue(&channel_wqs[minor].rt_queue, &wait);
260 		set_current_state(TASK_INTERRUPTIBLE);
261 
262 		while (!spacefree(rt->rt_read, rt->rt_write, rt->buffer_size))
263 			schedule();
264 
265 		set_current_state(TASK_RUNNING);
266 		remove_wait_queue(&channel_wqs[minor].rt_queue, &wait);
267 	}
268 
269 	/* total number of bytes to copy */
270 	count = min(count, (size_t)spacefree(rt->rt_read, rt->rt_write, rt->buffer_size) );
271 
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);
274 
275 	failed = copy_from_user(&rt->rt_buffer[rt->rt_write], buffer, fl);
276 	if (failed) {
277 		count = fl - failed;
278 		goto out;
279 	}
280 
281 	/* if there's any left copy to the beginning of the buffer */
282 	if (count - fl) {
283 		failed = copy_from_user(rt->rt_buffer, buffer + fl, count - fl);
284 		if (failed) {
285 			count -= failed;
286 			goto out;
287 		}
288 	}
289 
290 out:
291 	rt->rt_write += count;
292 	rt->rt_write %= rt->buffer_size;
293 
294 	return count;
295 }
296 
297 static struct file_operations rtlx_fops = {
298 	.owner		= THIS_MODULE,
299 	.open		= rtlx_open,
300 	.release	= rtlx_release,
301 	.write		= rtlx_write,
302 	.read		= rtlx_read,
303 	.poll		= rtlx_poll
304 };
305 
306 static char register_chrdev_failed[] __initdata =
307 	KERN_ERR "rtlx_module_init: unable to register device\n";
308 
309 static int __init rtlx_module_init(void)
310 {
311 	major = register_chrdev(0, module_name, &rtlx_fops);
312 	if (major < 0) {
313 		printk(register_chrdev_failed);
314 		return major;
315 	}
316 
317 	return 0;
318 }
319 
320 static void __exit rtlx_module_exit(void)
321 {
322 	unregister_chrdev(major, module_name);
323 }
324 
325 module_init(rtlx_module_init);
326 module_exit(rtlx_module_exit);
327 
328 MODULE_DESCRIPTION("MIPS RTLX");
329 MODULE_AUTHOR("Elizabeth Clarke, MIPS Technologies, Inc.");
330 MODULE_LICENSE("GPL");
331