• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /netgear-R7000-V1.0.7.12_1.2.5/components/opensource/linux/linux-2.6.36/arch/mips/kernel/
1/*
2 * Copyright (C) 2005 MIPS Technologies, Inc.  All rights reserved.
3 * Copyright (C) 2005, 06 Ralf Baechle (ralf@linux-mips.org)
4 *
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/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>
45
46static struct rtlx_info *rtlx;
47static int major;
48static char module_name[] = "rtlx";
49
50static 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];
56
57static struct vpe_notifications notify;
58static int sp_stopping;
59
60extern void *vpe_get_shared(int index);
61
62/* Interrupt handler may be called before rtlx_init has otherwise had
63   a chance to run.
64*/
65static irqreturn_t rtlx_interrupt(int irq, void *dev_id)
66{
67	unsigned int vpeflags;
68	unsigned long flags;
69	int i;
70
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);
78
79	for (i = 0; i < RTLX_CHANNELS; i++) {
80			wake_up(&channel_wqs[i].lx_queue);
81			wake_up(&channel_wqs[i].rt_queue);
82	}
83
84	return IRQ_HANDLED;
85}
86
87static void __used dump_rtlx(void)
88{
89	int i;
90
91	printk("id 0x%lx state %d\n", rtlx->id, rtlx->state);
92
93	for (i = 0; i < RTLX_CHANNELS; i++) {
94		struct rtlx_channel *chan = &rtlx->channel[i];
95
96		printk(" rt_state %d lx_state %d buffer_size %d\n",
97		       chan->rt_state, chan->lx_state, chan->buffer_size);
98
99		printk(" rt_read %d rt_write %d\n",
100		       chan->rt_read, chan->rt_write);
101
102		printk(" lx_read %d lx_write %d\n",
103		       chan->lx_read, chan->lx_write);
104
105		printk(" rt_buffer <%s>\n", chan->rt_buffer);
106		printk(" lx_buffer <%s>\n", chan->lx_buffer);
107	}
108}
109
110/* call when we have the address of the shared structure from the SP side. */
111static int rtlx_init(struct rtlx_info *rtlxi)
112{
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;
117	}
118
119	rtlx = rtlxi;
120
121	return 0;
122}
123
124/* notifications */
125static void starting(int vpe)
126{
127	int i;
128	sp_stopping = 0;
129
130	/* force a reload of rtlx */
131	rtlx=NULL;
132
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);
136}
137
138static void stopping(int vpe)
139{
140	int i;
141
142	sp_stopping = 1;
143	for (i = 0; i < RTLX_CHANNELS; i++)
144		wake_up_interruptible(&channel_wqs[i].lx_queue);
145}
146
147
148int rtlx_open(int index, int can_sleep)
149{
150	struct rtlx_info **p;
151	struct rtlx_channel *chan;
152	enum rtlx_state state;
153	int ret = 0;
154
155	if (index >= RTLX_CHANNELS) {
156		printk(KERN_DEBUG "rtlx_open index out of range\n");
157		return -ENOSYS;
158	}
159
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;
165	}
166
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;
179		    }
180		}
181
182		smp_rmb();
183		if (*p == NULL) {
184			if (can_sleep) {
185				DEFINE_WAIT(wait);
186
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;
197					}
198					ret = -ERESTARTSYS;
199					goto out_fail;
200				}
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;
207			}
208		}
209
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;
216		}
217
218		if ((ret = rtlx_init(*p)) < 0)
219			goto out_ret;
220	}
221
222	chan = &rtlx->channel[index];
223
224	state = xchg(&chan->lx_state, RTLX_STATE_OPENED);
225	if (state == RTLX_STATE_OPENED) {
226		ret = -EBUSY;
227		goto out_fail;
228	}
229
230out_fail:
231	smp_mb();
232	atomic_dec(&channel_wqs[index].in_open);
233	smp_mb();
234
235out_ret:
236	return ret;
237}
238
239int rtlx_release(int index)
240{
241	if (rtlx == NULL) {
242		pr_err("rtlx_release() with null rtlx\n");
243		return 0;
244	}
245	rtlx->channel[index].lx_state = RTLX_STATE_UNUSED;
246	return 0;
247}
248
249unsigned int rtlx_read_poll(int index, int can_sleep)
250{
251 	struct rtlx_channel *chan;
252
253 	if (rtlx == NULL)
254 		return 0;
255
256 	chan = &rtlx->channel[index];
257
258	/* data available to read? */
259	if (chan->lx_read == chan->lx_write) {
260		if (can_sleep) {
261			int ret = 0;
262
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;
268
269			if (sp_stopping)
270				return 0;
271		} else
272			return 0;
273	}
274
275	return (chan->lx_write + chan->buffer_size - chan->lx_read)
276	       % chan->buffer_size;
277}
278
279static inline int write_spacefree(int read, int write, int size)
280{
281	if (read == write) {
282		/*
283		 * Never fill the buffer completely, so indexes are always
284		 * equal if empty and only empty, or !equal if data available
285		 */
286		return size - 1;
287	}
288
289	return ((read + size - write) % size) - 1;
290}
291
292unsigned int rtlx_write_poll(int index)
293{
294	struct rtlx_channel *chan = &rtlx->channel[index];
295
296	return write_spacefree(chan->rt_read, chan->rt_write,
297				chan->buffer_size);
298}
299
300ssize_t rtlx_read(int index, void __user *buff, size_t count)
301{
302	size_t lx_write, fl = 0L;
303	struct rtlx_channel *lx;
304	unsigned long failed;
305
306	if (rtlx == NULL)
307		return -ENOSYS;
308
309	lx = &rtlx->channel[index];
310
311	mutex_lock(&channel_wqs[index].mutex);
312	smp_rmb();
313	lx_write = lx->lx_write;
314
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);
319
320	/* then how much from the read pointer onwards */
321	fl = min(count, (size_t)lx->buffer_size - lx->lx_read);
322
323	failed = copy_to_user(buff, lx->lx_buffer + lx->lx_read, fl);
324	if (failed)
325		goto out;
326
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);
330
331out:
332	count -= failed;
333
334	smp_wmb();
335	lx->lx_read = (lx->lx_read + count) % lx->buffer_size;
336	smp_wmb();
337	mutex_unlock(&channel_wqs[index].mutex);
338
339	return count;
340}
341
342ssize_t rtlx_write(int index, const void __user *buffer, size_t count)
343{
344	struct rtlx_channel *rt;
345	unsigned long failed;
346	size_t rt_read;
347	size_t fl;
348
349	if (rtlx == NULL)
350		return(-ENOSYS);
351
352	rt = &rtlx->channel[index];
353
354	mutex_lock(&channel_wqs[index].mutex);
355	smp_rmb();
356	rt_read = rt->rt_read;
357
358	/* total number of bytes to copy */
359	count = min(count, (size_t)write_spacefree(rt_read, rt->rt_write,
360							rt->buffer_size));
361
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);
364
365	failed = copy_from_user(rt->rt_buffer + rt->rt_write, buffer, fl);
366	if (failed)
367		goto out;
368
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);
372	}
373
374out:
375	count -= failed;
376
377	smp_wmb();
378	rt->rt_write = (rt->rt_write + count) % rt->buffer_size;
379	smp_wmb();
380	mutex_unlock(&channel_wqs[index].mutex);
381
382	return count;
383}
384
385
386static int file_open(struct inode *inode, struct file *filp)
387{
388	return rtlx_open(iminor(inode), (filp->f_flags & O_NONBLOCK) ? 0 : 1);
389}
390
391static int file_release(struct inode *inode, struct file *filp)
392{
393	return rtlx_release(iminor(inode));
394}
395
396static unsigned int file_poll(struct file *file, poll_table * wait)
397{
398	int minor;
399	unsigned int mask = 0;
400
401	minor = iminor(file->f_path.dentry->d_inode);
402
403	poll_wait(file, &channel_wqs[minor].rt_queue, wait);
404	poll_wait(file, &channel_wqs[minor].lx_queue, wait);
405
406	if (rtlx == NULL)
407		return 0;
408
409	/* data available to read? */
410	if (rtlx_read_poll(minor, 0))
411		mask |= POLLIN | POLLRDNORM;
412
413	/* space to write */
414	if (rtlx_write_poll(minor))
415		mask |= POLLOUT | POLLWRNORM;
416
417	return mask;
418}
419
420static ssize_t file_read(struct file *file, char __user * buffer, size_t count,
421			 loff_t * ppos)
422{
423	int minor = iminor(file->f_path.dentry->d_inode);
424
425	/* data available? */
426	if (!rtlx_read_poll(minor, (file->f_flags & O_NONBLOCK) ? 0 : 1)) {
427		return 0;	// -EAGAIN makes cat whinge
428	}
429
430	return rtlx_read(minor, buffer, count);
431}
432
433static ssize_t file_write(struct file *file, const char __user * buffer,
434			  size_t count, loff_t * ppos)
435{
436	int minor;
437	struct rtlx_channel *rt;
438
439	minor = iminor(file->f_path.dentry->d_inode);
440	rt = &rtlx->channel[minor];
441
442	/* any space left... */
443	if (!rtlx_write_poll(minor)) {
444		int ret = 0;
445
446		if (file->f_flags & O_NONBLOCK)
447			return -EAGAIN;
448
449		__wait_event_interruptible(channel_wqs[minor].rt_queue,
450		                           rtlx_write_poll(minor),
451		                           ret);
452		if (ret)
453			return ret;
454	}
455
456	return rtlx_write(minor, buffer, count);
457}
458
459static 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
466};
467
468static char register_chrdev_failed[] __initdata =
469	KERN_ERR "rtlx_module_init: unable to register device\n";
470
471static int __init rtlx_module_init(void)
472{
473	struct device *dev;
474	int i, err, irq;
475
476	if (!cpu_has_mipsmt) {
477		printk("VPE loader: not a MIPS MT capable processor\n");
478		return -ENODEV;
479	}
480
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");
485
486		return -ENODEV;
487	}
488
489	major = register_chrdev(0, module_name, &rtlx_fops);
490	if (major < 0) {
491		printk(register_chrdev_failed);
492		return major;
493	}
494
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);
501
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;
507		}
508	}
509
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;
519	}
520
521	err = request_irq(irq, &rtlx_interrupt, IRQF_SHARED,
522		module_name, (void *)dev);
523	if (err)
524		goto out_chrdev;
525	return 0;
526
527out_chrdev:
528	for (i = 0; i < RTLX_CHANNELS; i++)
529		device_destroy(mt_class, MKDEV(major, i));
530
531	return err;
532}
533
534static void __exit rtlx_module_exit(void)
535{
536	int i;
537
538	for (i = 0; i < RTLX_CHANNELS; i++)
539		device_destroy(mt_class, MKDEV(major, i));
540
541	unregister_chrdev(major, module_name);
542}
543
544module_init(rtlx_module_init);
545module_exit(rtlx_module_exit);
546
547MODULE_DESCRIPTION("MIPS RTLX");
548MODULE_AUTHOR("Elizabeth Oldham, MIPS Technologies, Inc.");
549MODULE_LICENSE("GPL");
550