GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / arch / mips / kernel / rtlx.c
blobc5f04adbb67fdc10efd9409e1699bd6ecefa9bc4
1 /*
2 * Copyright (C) 2005 MIPS Technologies, Inc. All rights reserved.
3 * Copyright (C) 2005, 06 Ralf Baechle (ralf@linux-mips.org)
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.
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.
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.
20 #include <linux/device.h>
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/fs.h>
24 #include <linux/init.h>
25 #include <asm/uaccess.h>
26 #include <linux/list.h>
27 #include <linux/vmalloc.h>
28 #include <linux/elf.h>
29 #include <linux/seq_file.h>
30 #include <linux/syscalls.h>
31 #include <linux/moduleloader.h>
32 #include <linux/interrupt.h>
33 #include <linux/poll.h>
34 #include <linux/sched.h>
35 #include <linux/wait.h>
36 #include <asm/mipsmtregs.h>
37 #include <asm/mips_mt.h>
38 #include <asm/cacheflush.h>
39 #include <asm/atomic.h>
40 #include <asm/cpu.h>
41 #include <asm/processor.h>
42 #include <asm/system.h>
43 #include <asm/vpe.h>
44 #include <asm/rtlx.h>
46 static struct rtlx_info *rtlx;
47 static int major;
48 static char module_name[] = "rtlx";
50 static struct chan_waitqueues {
51 wait_queue_head_t rt_queue;
52 wait_queue_head_t lx_queue;
53 atomic_t in_open;
54 struct mutex mutex;
55 } channel_wqs[RTLX_CHANNELS];
57 static struct vpe_notifications notify;
58 static int sp_stopping;
60 extern void *vpe_get_shared(int index);
62 /* Interrupt handler may be called before rtlx_init has otherwise had
63 a chance to run.
65 static irqreturn_t rtlx_interrupt(int irq, void *dev_id)
67 unsigned int vpeflags;
68 unsigned long flags;
69 int i;
71 /* Ought not to be strictly necessary for SMTC builds */
72 local_irq_save(flags);
73 vpeflags = dvpe();
74 set_c0_status(0x100 << MIPS_CPU_RTLX_IRQ);
75 irq_enable_hazard();
76 evpe(vpeflags);
77 local_irq_restore(flags);
79 for (i = 0; i < RTLX_CHANNELS; i++) {
80 wake_up(&channel_wqs[i].lx_queue);
81 wake_up(&channel_wqs[i].rt_queue);
84 return IRQ_HANDLED;
87 static void __used dump_rtlx(void)
89 int i;
91 printk("id 0x%lx state %d\n", rtlx->id, rtlx->state);
93 for (i = 0; i < RTLX_CHANNELS; i++) {
94 struct rtlx_channel *chan = &rtlx->channel[i];
96 printk(" rt_state %d lx_state %d buffer_size %d\n",
97 chan->rt_state, chan->lx_state, chan->buffer_size);
99 printk(" rt_read %d rt_write %d\n",
100 chan->rt_read, chan->rt_write);
102 printk(" lx_read %d lx_write %d\n",
103 chan->lx_read, chan->lx_write);
105 printk(" rt_buffer <%s>\n", chan->rt_buffer);
106 printk(" lx_buffer <%s>\n", chan->lx_buffer);
110 /* call when we have the address of the shared structure from the SP side. */
111 static int rtlx_init(struct rtlx_info *rtlxi)
113 if (rtlxi->id != RTLX_ID) {
114 printk(KERN_ERR "no valid RTLX id at 0x%p 0x%lx\n",
115 rtlxi, rtlxi->id);
116 return -ENOEXEC;
119 rtlx = rtlxi;
121 return 0;
124 /* notifications */
125 static void starting(int vpe)
127 int i;
128 sp_stopping = 0;
130 /* force a reload of rtlx */
131 rtlx=NULL;
133 /* wake up any sleeping rtlx_open's */
134 for (i = 0; i < RTLX_CHANNELS; i++)
135 wake_up_interruptible(&channel_wqs[i].lx_queue);
138 static void stopping(int vpe)
140 int i;
142 sp_stopping = 1;
143 for (i = 0; i < RTLX_CHANNELS; i++)
144 wake_up_interruptible(&channel_wqs[i].lx_queue);
148 int rtlx_open(int index, int can_sleep)
150 struct rtlx_info **p;
151 struct rtlx_channel *chan;
152 enum rtlx_state state;
153 int ret = 0;
155 if (index >= RTLX_CHANNELS) {
156 printk(KERN_DEBUG "rtlx_open index out of range\n");
157 return -ENOSYS;
160 if (atomic_inc_return(&channel_wqs[index].in_open) > 1) {
161 printk(KERN_DEBUG "rtlx_open channel %d already opened\n",
162 index);
163 ret = -EBUSY;
164 goto out_fail;
167 if (rtlx == NULL) {
168 if( (p = vpe_get_shared(tclimit)) == NULL) {
169 if (can_sleep) {
170 __wait_event_interruptible(channel_wqs[index].lx_queue,
171 (p = vpe_get_shared(tclimit)), ret);
172 if (ret)
173 goto out_fail;
174 } else {
175 printk(KERN_DEBUG "No SP program loaded, and device "
176 "opened with O_NONBLOCK\n");
177 ret = -ENOSYS;
178 goto out_fail;
182 smp_rmb();
183 if (*p == NULL) {
184 if (can_sleep) {
185 DEFINE_WAIT(wait);
187 for (;;) {
188 prepare_to_wait(
189 &channel_wqs[index].lx_queue,
190 &wait, TASK_INTERRUPTIBLE);
191 smp_rmb();
192 if (*p != NULL)
193 break;
194 if (!signal_pending(current)) {
195 schedule();
196 continue;
198 ret = -ERESTARTSYS;
199 goto out_fail;
201 finish_wait(&channel_wqs[index].lx_queue, &wait);
202 } else {
203 pr_err(" *vpe_get_shared is NULL. "
204 "Has an SP program been loaded?\n");
205 ret = -ENOSYS;
206 goto out_fail;
210 if ((unsigned int)*p < KSEG0) {
211 printk(KERN_WARNING "vpe_get_shared returned an "
212 "invalid pointer maybe an error code %d\n",
213 (int)*p);
214 ret = -ENOSYS;
215 goto out_fail;
218 if ((ret = rtlx_init(*p)) < 0)
219 goto out_ret;
222 chan = &rtlx->channel[index];
224 state = xchg(&chan->lx_state, RTLX_STATE_OPENED);
225 if (state == RTLX_STATE_OPENED) {
226 ret = -EBUSY;
227 goto out_fail;
230 out_fail:
231 smp_mb();
232 atomic_dec(&channel_wqs[index].in_open);
233 smp_mb();
235 out_ret:
236 return ret;
239 int rtlx_release(int index)
241 if (rtlx == NULL) {
242 pr_err("rtlx_release() with null rtlx\n");
243 return 0;
245 rtlx->channel[index].lx_state = RTLX_STATE_UNUSED;
246 return 0;
249 unsigned int rtlx_read_poll(int index, int can_sleep)
251 struct rtlx_channel *chan;
253 if (rtlx == NULL)
254 return 0;
256 chan = &rtlx->channel[index];
258 /* data available to read? */
259 if (chan->lx_read == chan->lx_write) {
260 if (can_sleep) {
261 int ret = 0;
263 __wait_event_interruptible(channel_wqs[index].lx_queue,
264 (chan->lx_read != chan->lx_write) ||
265 sp_stopping, ret);
266 if (ret)
267 return ret;
269 if (sp_stopping)
270 return 0;
271 } else
272 return 0;
275 return (chan->lx_write + chan->buffer_size - chan->lx_read)
276 % chan->buffer_size;
279 static inline int write_spacefree(int read, int write, int size)
281 if (read == write) {
283 * Never fill the buffer completely, so indexes are always
284 * equal if empty and only empty, or !equal if data available
286 return size - 1;
289 return ((read + size - write) % size) - 1;
292 unsigned int rtlx_write_poll(int index)
294 struct rtlx_channel *chan = &rtlx->channel[index];
296 return write_spacefree(chan->rt_read, chan->rt_write,
297 chan->buffer_size);
300 ssize_t rtlx_read(int index, void __user *buff, size_t count)
302 size_t lx_write, fl = 0L;
303 struct rtlx_channel *lx;
304 unsigned long failed;
306 if (rtlx == NULL)
307 return -ENOSYS;
309 lx = &rtlx->channel[index];
311 mutex_lock(&channel_wqs[index].mutex);
312 smp_rmb();
313 lx_write = lx->lx_write;
315 /* find out how much in total */
316 count = min(count,
317 (size_t)(lx_write + lx->buffer_size - lx->lx_read)
318 % lx->buffer_size);
320 /* then how much from the read pointer onwards */
321 fl = min(count, (size_t)lx->buffer_size - lx->lx_read);
323 failed = copy_to_user(buff, lx->lx_buffer + lx->lx_read, fl);
324 if (failed)
325 goto out;
327 /* and if there is anything left at the beginning of the buffer */
328 if (count - fl)
329 failed = copy_to_user(buff + fl, lx->lx_buffer, count - fl);
331 out:
332 count -= failed;
334 smp_wmb();
335 lx->lx_read = (lx->lx_read + count) % lx->buffer_size;
336 smp_wmb();
337 mutex_unlock(&channel_wqs[index].mutex);
339 return count;
342 ssize_t rtlx_write(int index, const void __user *buffer, size_t count)
344 struct rtlx_channel *rt;
345 unsigned long failed;
346 size_t rt_read;
347 size_t fl;
349 if (rtlx == NULL)
350 return(-ENOSYS);
352 rt = &rtlx->channel[index];
354 mutex_lock(&channel_wqs[index].mutex);
355 smp_rmb();
356 rt_read = rt->rt_read;
358 /* total number of bytes to copy */
359 count = min(count, (size_t)write_spacefree(rt_read, rt->rt_write,
360 rt->buffer_size));
362 /* first bit from write pointer to the end of the buffer, or count */
363 fl = min(count, (size_t) rt->buffer_size - rt->rt_write);
365 failed = copy_from_user(rt->rt_buffer + rt->rt_write, buffer, fl);
366 if (failed)
367 goto out;
369 /* if there's any left copy to the beginning of the buffer */
370 if (count - fl) {
371 failed = copy_from_user(rt->rt_buffer, buffer + fl, count - fl);
374 out:
375 count -= failed;
377 smp_wmb();
378 rt->rt_write = (rt->rt_write + count) % rt->buffer_size;
379 smp_wmb();
380 mutex_unlock(&channel_wqs[index].mutex);
382 return count;
386 static int file_open(struct inode *inode, struct file *filp)
388 return rtlx_open(iminor(inode), (filp->f_flags & O_NONBLOCK) ? 0 : 1);
391 static int file_release(struct inode *inode, struct file *filp)
393 return rtlx_release(iminor(inode));
396 static unsigned int file_poll(struct file *file, poll_table * wait)
398 int minor;
399 unsigned int mask = 0;
401 minor = iminor(file->f_path.dentry->d_inode);
403 poll_wait(file, &channel_wqs[minor].rt_queue, wait);
404 poll_wait(file, &channel_wqs[minor].lx_queue, wait);
406 if (rtlx == NULL)
407 return 0;
409 /* data available to read? */
410 if (rtlx_read_poll(minor, 0))
411 mask |= POLLIN | POLLRDNORM;
413 /* space to write */
414 if (rtlx_write_poll(minor))
415 mask |= POLLOUT | POLLWRNORM;
417 return mask;
420 static ssize_t file_read(struct file *file, char __user * buffer, size_t count,
421 loff_t * ppos)
423 int minor = iminor(file->f_path.dentry->d_inode);
425 /* data available? */
426 if (!rtlx_read_poll(minor, (file->f_flags & O_NONBLOCK) ? 0 : 1)) {
427 return 0; // -EAGAIN makes cat whinge
430 return rtlx_read(minor, buffer, count);
433 static ssize_t file_write(struct file *file, const char __user * buffer,
434 size_t count, loff_t * ppos)
436 int minor;
437 struct rtlx_channel *rt;
439 minor = iminor(file->f_path.dentry->d_inode);
440 rt = &rtlx->channel[minor];
442 /* any space left... */
443 if (!rtlx_write_poll(minor)) {
444 int ret = 0;
446 if (file->f_flags & O_NONBLOCK)
447 return -EAGAIN;
449 __wait_event_interruptible(channel_wqs[minor].rt_queue,
450 rtlx_write_poll(minor),
451 ret);
452 if (ret)
453 return ret;
456 return rtlx_write(minor, buffer, count);
459 static const struct file_operations rtlx_fops = {
460 .owner = THIS_MODULE,
461 .open = file_open,
462 .release = file_release,
463 .write = file_write,
464 .read = file_read,
465 .poll = file_poll
468 static char register_chrdev_failed[] __initdata =
469 KERN_ERR "rtlx_module_init: unable to register device\n";
471 static int __init rtlx_module_init(void)
473 struct device *dev;
474 int i, err, irq;
476 if (!cpu_has_mipsmt) {
477 printk("VPE loader: not a MIPS MT capable processor\n");
478 return -ENODEV;
481 if (tclimit == 0) {
482 printk(KERN_WARNING "No TCs reserved for AP/SP, not "
483 "initializing RTLX.\nPass maxtcs=<n> argument as kernel "
484 "argument\n");
486 return -ENODEV;
489 major = register_chrdev(0, module_name, &rtlx_fops);
490 if (major < 0) {
491 printk(register_chrdev_failed);
492 return major;
495 /* initialise the wait queues */
496 for (i = 0; i < RTLX_CHANNELS; i++) {
497 init_waitqueue_head(&channel_wqs[i].rt_queue);
498 init_waitqueue_head(&channel_wqs[i].lx_queue);
499 atomic_set(&channel_wqs[i].in_open, 0);
500 mutex_init(&channel_wqs[i].mutex);
502 dev = device_create(mt_class, NULL, MKDEV(major, i), NULL,
503 "%s%d", module_name, i);
504 if (IS_ERR(dev)) {
505 err = PTR_ERR(dev);
506 goto out_chrdev;
510 /* set up notifiers */
511 notify.start = starting;
512 notify.stop = stopping;
513 vpe_notify(tclimit, &notify);
514 irq = arch_get_xcpu_irq();
515 if (irq < 0) {
516 pr_err("APRP RTLX init on non-vectored-interrupt processor\n");
517 err = -ENODEV;
518 goto out_chrdev;
521 err = request_irq(irq, &rtlx_interrupt, IRQF_SHARED,
522 module_name, (void *)dev);
523 if (err)
524 goto out_chrdev;
525 return 0;
527 out_chrdev:
528 for (i = 0; i < RTLX_CHANNELS; i++)
529 device_destroy(mt_class, MKDEV(major, i));
531 return err;
534 static void __exit rtlx_module_exit(void)
536 int i;
538 for (i = 0; i < RTLX_CHANNELS; i++)
539 device_destroy(mt_class, MKDEV(major, i));
541 unregister_chrdev(major, module_name);
544 module_init(rtlx_module_init);
545 module_exit(rtlx_module_exit);
547 MODULE_DESCRIPTION("MIPS RTLX");
548 MODULE_AUTHOR("Elizabeth Oldham, MIPS Technologies, Inc.");
549 MODULE_LICENSE("GPL");