]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - arch/mips/kernel/rtlx.c
[MIPS] Get rid of unnecessary prototypes. Fixes and optimizations for HZ > 100.
[mirror_ubuntu-artful-kernel.git] / arch / mips / kernel / rtlx.c
CommitLineData
e01402b1
RB
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>
e01402b1
RB
23#include <linux/poll.h>
24#include <linux/sched.h>
25#include <linux/wait.h>
26#include <asm/mipsmtregs.h>
afc4841d 27#include <asm/bitops.h>
e01402b1
RB
28#include <asm/cpu.h>
29#include <asm/processor.h>
e01402b1 30#include <asm/rtlx.h>
afc4841d 31#include <asm/uaccess.h>
e01402b1 32
e01402b1
RB
33#define RTLX_TARG_VPE 1
34
afc4841d 35static struct rtlx_info *rtlx;
e01402b1
RB
36static int major;
37static char module_name[] = "rtlx";
afc4841d
RB
38static struct irqaction irq;
39static int irq_num;
40
41static 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}
e01402b1
RB
53
54static struct chan_waitqueues {
55 wait_queue_head_t rt_queue;
56 wait_queue_head_t lx_queue;
57} channel_wqs[RTLX_CHANNELS];
58
e01402b1
RB
59extern void *vpe_get_shared(int index);
60
61static void rtlx_dispatch(struct pt_regs *regs)
62{
63 do_IRQ(MIPSCPU_INT_BASE + MIPS_CPU_RTLX_IRQ, regs);
64}
65
afc4841d 66static irqreturn_t rtlx_interrupt(int irq, void *dev_id, struct pt_regs *regs)
e01402b1 67{
e01402b1
RB
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
afc4841d 77 return IRQ_HANDLED;
e01402b1
RB
78}
79
80/* call when we have the address of the shared structure from the SP side. */
81static 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);
afc4841d 87 return -ENOEXEC;
e01402b1
RB
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
afc4841d 99 if (cpu_has_vint)
e01402b1 100 set_vi_handler(MIPS_CPU_RTLX_IRQ, rtlx_dispatch);
e01402b1
RB
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;
afc4841d
RB
110
111 return 0;
e01402b1
RB
112}
113
114/* only allow one open process at a time to open each channel */
115static 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) {
afc4841d
RB
126 printk(KERN_ERR "vpe_get_shared is NULL. "
127 "Has an SP program been loaded?\n");
128 return -EFAULT;
e01402b1
RB
129 }
130
131 if (*p == NULL) {
afc4841d
RB
132 printk(KERN_ERR "vpe_shared %p %p\n", p, *p);
133 return -EFAULT;
e01402b1
RB
134 }
135
136 if ((ret = rtlx_init(*p)) < 0)
afc4841d 137 return ret;
e01402b1
RB
138 }
139
140 chan = &rtlx->channel[minor];
141
afc4841d
RB
142 if (test_and_set_bit(RTLX_STATE_OPENED, &chan->lx_state))
143 return -EBUSY;
e01402b1 144
afc4841d 145 return 0;
e01402b1
RB
146}
147
148static int rtlx_release(struct inode *inode, struct file *filp)
149{
afc4841d 150 int minor = MINOR(inode->i_rdev);
e01402b1 151
afc4841d
RB
152 clear_bit(RTLX_STATE_OPENED, &rtlx->channel[minor].lx_state);
153 smp_mb__after_clear_bit();
154
155 return 0;
e01402b1
RB
156}
157
158static 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
afc4841d 178 return mask;
e01402b1
RB
179}
180
181static ssize_t rtlx_read(struct file *file, char __user * buffer, size_t count,
182 loff_t * ppos)
183{
afc4841d 184 unsigned long failed;
e01402b1
RB
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)
afc4841d 196 return 0; /* -EAGAIN makes cat whinge */
e01402b1
RB
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 */
afc4841d
RB
212 count = min(count,
213 (size_t)(lx->lx_write + lx->buffer_size - lx->lx_read) % lx->buffer_size);
e01402b1
RB
214
215 /* then how much from the read pointer onwards */
afc4841d 216 fl = min(count, (size_t)lx->buffer_size - lx->lx_read);
e01402b1 217
afc4841d
RB
218 failed = copy_to_user (buffer, &lx->lx_buffer[lx->lx_read], fl);
219 if (failed) {
220 count = fl - failed;
221 goto out;
222 }
e01402b1
RB
223
224 /* and if there is anything left at the beginning of the buffer */
afc4841d
RB
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 }
e01402b1 232
afc4841d 233out:
e01402b1
RB
234 /* update the index */
235 lx->lx_read += count;
236 lx->lx_read %= lx->buffer_size;
237
afc4841d 238 return count;
e01402b1
RB
239}
240
241static ssize_t rtlx_write(struct file *file, const char __user * buffer,
242 size_t count, loff_t * ppos)
243{
afc4841d 244 unsigned long failed;
e01402b1
RB
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)
afc4841d 257 return -EAGAIN;
e01402b1
RB
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 */
afc4841d 270 count = min(count, (size_t)spacefree(rt->rt_read, rt->rt_write, rt->buffer_size) );
e01402b1
RB
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
afc4841d
RB
275 failed = copy_from_user(&rt->rt_buffer[rt->rt_write], buffer, fl);
276 if (failed) {
277 count = fl - failed;
278 goto out;
279 }
e01402b1
RB
280
281 /* if there's any left copy to the beginning of the buffer */
afc4841d
RB
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 }
e01402b1 289
afc4841d 290out:
e01402b1
RB
291 rt->rt_write += count;
292 rt->rt_write %= rt->buffer_size;
293
afc4841d 294 return count;
e01402b1
RB
295}
296
297static struct file_operations rtlx_fops = {
afc4841d
RB
298 .owner = THIS_MODULE,
299 .open = rtlx_open,
300 .release = rtlx_release,
301 .write = rtlx_write,
302 .read = rtlx_read,
303 .poll = rtlx_poll
e01402b1
RB
304};
305
afc4841d
RB
306static char register_chrdev_failed[] __initdata =
307 KERN_ERR "rtlx_module_init: unable to register device\n";
308
309static int __init rtlx_module_init(void)
e01402b1 310{
afc4841d
RB
311 major = register_chrdev(0, module_name, &rtlx_fops);
312 if (major < 0) {
313 printk(register_chrdev_failed);
314 return major;
e01402b1
RB
315 }
316
afc4841d 317 return 0;
e01402b1
RB
318}
319
afc4841d 320static void __exit rtlx_module_exit(void)
e01402b1
RB
321{
322 unregister_chrdev(major, module_name);
323}
324
325module_init(rtlx_module_init);
326module_exit(rtlx_module_exit);
afc4841d 327
e01402b1 328MODULE_DESCRIPTION("MIPS RTLX");
afc4841d 329MODULE_AUTHOR("Elizabeth Clarke, MIPS Technologies, Inc.");
e01402b1 330MODULE_LICENSE("GPL");