blob: ce72bfff5e29298c5c065bb59f0473295054443b [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
Ralf Baechlebb3d7c72007-02-07 15:36:56 +000020#include <linux/device.h>
Ralf Baechlee01402b2005-07-14 15:57:16 +000021#include <linux/kernel.h>
Ralf Baechlee01402b2005-07-14 15:57:16 +000022#include <linux/fs.h>
23#include <linux/init.h>
Ralf Baechle26009902006-04-05 09:45:45 +010024#include <asm/uaccess.h>
Ralf Baechle26009902006-04-05 09:45:45 +010025#include <linux/list.h>
26#include <linux/vmalloc.h>
27#include <linux/elf.h>
28#include <linux/seq_file.h>
29#include <linux/syscalls.h>
30#include <linux/moduleloader.h>
Ralf Baechlea84c96e2006-01-15 18:11:28 +000031#include <linux/interrupt.h>
Ralf Baechlee01402b2005-07-14 15:57:16 +000032#include <linux/poll.h>
33#include <linux/sched.h>
34#include <linux/wait.h>
35#include <asm/mipsmtregs.h>
Ralf Baechlebb3d7c72007-02-07 15:36:56 +000036#include <asm/mips_mt.h>
Ralf Baechle26009902006-04-05 09:45:45 +010037#include <asm/cacheflush.h>
Arun Sharma600634972011-07-26 16:09:06 -070038#include <linux/atomic.h>
Ralf Baechlee01402b2005-07-14 15:57:16 +000039#include <asm/cpu.h>
40#include <asm/processor.h>
Ralf Baechle26009902006-04-05 09:45:45 +010041#include <asm/vpe.h>
Ralf Baechlee01402b2005-07-14 15:57:16 +000042#include <asm/rtlx.h>
43
Ralf Baechleafc48412005-10-31 00:30:39 +000044static struct rtlx_info *rtlx;
Ralf Baechlee01402b2005-07-14 15:57:16 +000045static int major;
46static char module_name[] = "rtlx";
Ralf Baechlee01402b2005-07-14 15:57:16 +000047
48static struct chan_waitqueues {
49 wait_queue_head_t rt_queue;
50 wait_queue_head_t lx_queue;
Ralf Baechlec4c40182007-02-23 13:40:45 +000051 atomic_t in_open;
Ralf Baechlebc4809e2007-03-15 17:13:47 +000052 struct mutex mutex;
Ralf Baechlee01402b2005-07-14 15:57:16 +000053} channel_wqs[RTLX_CHANNELS];
54
Ralf Baechle26009902006-04-05 09:45:45 +010055static struct vpe_notifications notify;
Ralf Baechle982f6ff2009-09-17 02:25:07 +020056static int sp_stopping;
Ralf Baechle26009902006-04-05 09:45:45 +010057
Ralf Baechlee01402b2005-07-14 15:57:16 +000058extern void *vpe_get_shared(int index);
59
Ralf Baechle937a8012006-10-07 19:44:33 +010060static void rtlx_dispatch(void)
Ralf Baechlee01402b2005-07-14 15:57:16 +000061{
Atsushi Nemoto97dcb822007-01-08 02:14:29 +090062 do_IRQ(MIPS_CPU_IRQ_BASE + MIPS_CPU_RTLX_IRQ);
Ralf Baechlee01402b2005-07-14 15:57:16 +000063}
64
Ralf Baechle26009902006-04-05 09:45:45 +010065
66/* Interrupt handler may be called before rtlx_init has otherwise had
67 a chance to run.
68*/
Ralf Baechle937a8012006-10-07 19:44:33 +010069static irqreturn_t rtlx_interrupt(int irq, void *dev_id)
Ralf Baechlee01402b2005-07-14 15:57:16 +000070{
Ralf Baechle1bbfc202009-09-29 00:52:27 +010071 unsigned int vpeflags;
72 unsigned long flags;
Ralf Baechlee01402b2005-07-14 15:57:16 +000073 int i;
Kevin D. Kissell1928cc82008-04-16 15:32:22 +020074
75 /* Ought not to be strictly necessary for SMTC builds */
76 local_irq_save(flags);
77 vpeflags = dvpe();
78 set_c0_status(0x100 << MIPS_CPU_RTLX_IRQ);
79 irq_enable_hazard();
80 evpe(vpeflags);
81 local_irq_restore(flags);
Ralf Baechlee01402b2005-07-14 15:57:16 +000082
83 for (i = 0; i < RTLX_CHANNELS; i++) {
Ralf Baechle26009902006-04-05 09:45:45 +010084 wake_up(&channel_wqs[i].lx_queue);
85 wake_up(&channel_wqs[i].rt_queue);
Ralf Baechlee01402b2005-07-14 15:57:16 +000086 }
87
Ralf Baechleafc48412005-10-31 00:30:39 +000088 return IRQ_HANDLED;
Ralf Baechlee01402b2005-07-14 15:57:16 +000089}
90
David Rientjesf5dbeaf2007-07-22 01:01:39 -070091static void __used dump_rtlx(void)
Ralf Baechlee01402b2005-07-14 15:57:16 +000092{
93 int i;
94
Ralf Baechle26009902006-04-05 09:45:45 +010095 printk("id 0x%lx state %d\n", rtlx->id, rtlx->state);
96
97 for (i = 0; i < RTLX_CHANNELS; i++) {
98 struct rtlx_channel *chan = &rtlx->channel[i];
99
100 printk(" rt_state %d lx_state %d buffer_size %d\n",
101 chan->rt_state, chan->lx_state, chan->buffer_size);
102
103 printk(" rt_read %d rt_write %d\n",
104 chan->rt_read, chan->rt_write);
105
106 printk(" lx_read %d lx_write %d\n",
107 chan->lx_read, chan->lx_write);
108
109 printk(" rt_buffer <%s>\n", chan->rt_buffer);
110 printk(" lx_buffer <%s>\n", chan->lx_buffer);
111 }
112}
113
114/* call when we have the address of the shared structure from the SP side. */
115static int rtlx_init(struct rtlx_info *rtlxi)
116{
Ralf Baechlee01402b2005-07-14 15:57:16 +0000117 if (rtlxi->id != RTLX_ID) {
Kevin D. Kissell1928cc82008-04-16 15:32:22 +0200118 printk(KERN_ERR "no valid RTLX id at 0x%p 0x%lx\n",
119 rtlxi, rtlxi->id);
Ralf Baechleafc48412005-10-31 00:30:39 +0000120 return -ENOEXEC;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000121 }
122
Ralf Baechlee01402b2005-07-14 15:57:16 +0000123 rtlx = rtlxi;
Ralf Baechleafc48412005-10-31 00:30:39 +0000124
125 return 0;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000126}
127
Ralf Baechle26009902006-04-05 09:45:45 +0100128/* notifications */
129static void starting(int vpe)
Ralf Baechlee01402b2005-07-14 15:57:16 +0000130{
Ralf Baechle26009902006-04-05 09:45:45 +0100131 int i;
132 sp_stopping = 0;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000133
Ralf Baechle26009902006-04-05 09:45:45 +0100134 /* force a reload of rtlx */
135 rtlx=NULL;
136
137 /* wake up any sleeping rtlx_open's */
138 for (i = 0; i < RTLX_CHANNELS; i++)
139 wake_up_interruptible(&channel_wqs[i].lx_queue);
140}
141
142static void stopping(int vpe)
143{
144 int i;
145
146 sp_stopping = 1;
147 for (i = 0; i < RTLX_CHANNELS; i++)
148 wake_up_interruptible(&channel_wqs[i].lx_queue);
149}
150
151
152int rtlx_open(int index, int can_sleep)
153{
Ralf Baechle9e346822007-03-15 17:08:28 +0000154 struct rtlx_info **p;
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000155 struct rtlx_channel *chan;
Ralf Baechlec4c40182007-02-23 13:40:45 +0000156 enum rtlx_state state;
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000157 int ret = 0;
Ralf Baechle26009902006-04-05 09:45:45 +0100158
159 if (index >= RTLX_CHANNELS) {
160 printk(KERN_DEBUG "rtlx_open index out of range\n");
161 return -ENOSYS;
162 }
163
Ralf Baechlec4c40182007-02-23 13:40:45 +0000164 if (atomic_inc_return(&channel_wqs[index].in_open) > 1) {
165 printk(KERN_DEBUG "rtlx_open channel %d already opened\n",
166 index);
167 ret = -EBUSY;
168 goto out_fail;
Ralf Baechle26009902006-04-05 09:45:45 +0100169 }
170
Ralf Baechlee01402b2005-07-14 15:57:16 +0000171 if (rtlx == NULL) {
Ralf Baechle07cc0c92007-07-27 19:31:10 +0100172 if( (p = vpe_get_shared(tclimit)) == NULL) {
Kevin D. Kissell1928cc82008-04-16 15:32:22 +0200173 if (can_sleep) {
174 __wait_event_interruptible(channel_wqs[index].lx_queue,
175 (p = vpe_get_shared(tclimit)), ret);
176 if (ret)
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000177 goto out_fail;
Kevin D. Kissell1928cc82008-04-16 15:32:22 +0200178 } else {
179 printk(KERN_DEBUG "No SP program loaded, and device "
180 "opened with O_NONBLOCK\n");
181 ret = -ENOSYS;
182 goto out_fail;
183 }
Ralf Baechlee01402b2005-07-14 15:57:16 +0000184 }
185
Ralf Baechle9e346822007-03-15 17:08:28 +0000186 smp_rmb();
Ralf Baechlee01402b2005-07-14 15:57:16 +0000187 if (*p == NULL) {
Ralf Baechle26009902006-04-05 09:45:45 +0100188 if (can_sleep) {
Ralf Baechle9e346822007-03-15 17:08:28 +0000189 DEFINE_WAIT(wait);
190
191 for (;;) {
Kevin D. Kissell1928cc82008-04-16 15:32:22 +0200192 prepare_to_wait(
193 &channel_wqs[index].lx_queue,
194 &wait, TASK_INTERRUPTIBLE);
Ralf Baechle9e346822007-03-15 17:08:28 +0000195 smp_rmb();
196 if (*p != NULL)
197 break;
198 if (!signal_pending(current)) {
199 schedule();
200 continue;
201 }
202 ret = -ERESTARTSYS;
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000203 goto out_fail;
Ralf Baechle9e346822007-03-15 17:08:28 +0000204 }
205 finish_wait(&channel_wqs[index].lx_queue, &wait);
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000206 } else {
Kevin D. Kissell1928cc82008-04-16 15:32:22 +0200207 pr_err(" *vpe_get_shared is NULL. "
Ralf Baechle26009902006-04-05 09:45:45 +0100208 "Has an SP program been loaded?\n");
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000209 ret = -ENOSYS;
210 goto out_fail;
Ralf Baechle26009902006-04-05 09:45:45 +0100211 }
Ralf Baechlee01402b2005-07-14 15:57:16 +0000212 }
213
Ralf Baechle26009902006-04-05 09:45:45 +0100214 if ((unsigned int)*p < KSEG0) {
Kevin D. Kissell1928cc82008-04-16 15:32:22 +0200215 printk(KERN_WARNING "vpe_get_shared returned an "
216 "invalid pointer maybe an error code %d\n",
217 (int)*p);
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000218 ret = -ENOSYS;
219 goto out_fail;
Ralf Baechle26009902006-04-05 09:45:45 +0100220 }
221
Ralf Baechlec4c40182007-02-23 13:40:45 +0000222 if ((ret = rtlx_init(*p)) < 0)
223 goto out_ret;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000224 }
225
Ralf Baechle26009902006-04-05 09:45:45 +0100226 chan = &rtlx->channel[index];
Ralf Baechlee01402b2005-07-14 15:57:16 +0000227
Ralf Baechlec4c40182007-02-23 13:40:45 +0000228 state = xchg(&chan->lx_state, RTLX_STATE_OPENED);
229 if (state == RTLX_STATE_OPENED) {
230 ret = -EBUSY;
231 goto out_fail;
232 }
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000233
234out_fail:
Ralf Baechlec4c40182007-02-23 13:40:45 +0000235 smp_mb();
236 atomic_dec(&channel_wqs[index].in_open);
237 smp_mb();
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000238
Ralf Baechlec4c40182007-02-23 13:40:45 +0000239out_ret:
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000240 return ret;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000241}
242
Ralf Baechle26009902006-04-05 09:45:45 +0100243int rtlx_release(int index)
Ralf Baechlee01402b2005-07-14 15:57:16 +0000244{
Kevin D. Kissell1928cc82008-04-16 15:32:22 +0200245 if (rtlx == NULL) {
246 pr_err("rtlx_release() with null rtlx\n");
247 return 0;
248 }
Ralf Baechle26009902006-04-05 09:45:45 +0100249 rtlx->channel[index].lx_state = RTLX_STATE_UNUSED;
Ralf Baechleafc48412005-10-31 00:30:39 +0000250 return 0;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000251}
252
Ralf Baechle26009902006-04-05 09:45:45 +0100253unsigned int rtlx_read_poll(int index, int can_sleep)
Ralf Baechlee01402b2005-07-14 15:57:16 +0000254{
Ralf Baechle70342282013-01-22 12:59:30 +0100255 struct rtlx_channel *chan;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000256
Ralf Baechle70342282013-01-22 12:59:30 +0100257 if (rtlx == NULL)
258 return 0;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000259
Ralf Baechle70342282013-01-22 12:59:30 +0100260 chan = &rtlx->channel[index];
Ralf Baechlee01402b2005-07-14 15:57:16 +0000261
262 /* data available to read? */
Ralf Baechle26009902006-04-05 09:45:45 +0100263 if (chan->lx_read == chan->lx_write) {
264 if (can_sleep) {
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000265 int ret = 0;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000266
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000267 __wait_event_interruptible(channel_wqs[index].lx_queue,
Kevin D. Kissell1928cc82008-04-16 15:32:22 +0200268 (chan->lx_read != chan->lx_write) ||
269 sp_stopping, ret);
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000270 if (ret)
271 return ret;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000272
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000273 if (sp_stopping)
274 return 0;
275 } else
Ralf Baechle26009902006-04-05 09:45:45 +0100276 return 0;
277 }
278
279 return (chan->lx_write + chan->buffer_size - chan->lx_read)
280 % chan->buffer_size;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000281}
282
Ralf Baechle26009902006-04-05 09:45:45 +0100283static inline int write_spacefree(int read, int write, int size)
Ralf Baechlee01402b2005-07-14 15:57:16 +0000284{
Ralf Baechle26009902006-04-05 09:45:45 +0100285 if (read == write) {
286 /*
287 * Never fill the buffer completely, so indexes are always
288 * equal if empty and only empty, or !equal if data available
289 */
290 return size - 1;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000291 }
292
Ralf Baechle26009902006-04-05 09:45:45 +0100293 return ((read + size - write) % size) - 1;
294}
295
296unsigned int rtlx_write_poll(int index)
297{
298 struct rtlx_channel *chan = &rtlx->channel[index];
Kevin D. Kissell1928cc82008-04-16 15:32:22 +0200299
300 return write_spacefree(chan->rt_read, chan->rt_write,
301 chan->buffer_size);
Ralf Baechle26009902006-04-05 09:45:45 +0100302}
303
Ralf Baechle7f5a7712007-04-25 15:08:57 +0100304ssize_t rtlx_read(int index, void __user *buff, size_t count)
Ralf Baechle26009902006-04-05 09:45:45 +0100305{
Ralf Baechle61dcc6f2007-03-15 17:10:16 +0000306 size_t lx_write, fl = 0L;
Ralf Baechle26009902006-04-05 09:45:45 +0100307 struct rtlx_channel *lx;
Ralf Baechle46230aa2007-03-16 12:16:27 +0000308 unsigned long failed;
Ralf Baechle26009902006-04-05 09:45:45 +0100309
310 if (rtlx == NULL)
311 return -ENOSYS;
312
313 lx = &rtlx->channel[index];
314
Ralf Baechlebc4809e2007-03-15 17:13:47 +0000315 mutex_lock(&channel_wqs[index].mutex);
Ralf Baechle61dcc6f2007-03-15 17:10:16 +0000316 smp_rmb();
317 lx_write = lx->lx_write;
318
Ralf Baechlee01402b2005-07-14 15:57:16 +0000319 /* find out how much in total */
Ralf Baechleafc48412005-10-31 00:30:39 +0000320 count = min(count,
Ralf Baechle61dcc6f2007-03-15 17:10:16 +0000321 (size_t)(lx_write + lx->buffer_size - lx->lx_read)
Ralf Baechle26009902006-04-05 09:45:45 +0100322 % lx->buffer_size);
Ralf Baechlee01402b2005-07-14 15:57:16 +0000323
324 /* then how much from the read pointer onwards */
Ralf Baechle61dcc6f2007-03-15 17:10:16 +0000325 fl = min(count, (size_t)lx->buffer_size - lx->lx_read);
Ralf Baechlee01402b2005-07-14 15:57:16 +0000326
Ralf Baechle46230aa2007-03-16 12:16:27 +0000327 failed = copy_to_user(buff, lx->lx_buffer + lx->lx_read, fl);
328 if (failed)
329 goto out;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000330
331 /* and if there is anything left at the beginning of the buffer */
Ralf Baechle61dcc6f2007-03-15 17:10:16 +0000332 if (count - fl)
Ralf Baechle46230aa2007-03-16 12:16:27 +0000333 failed = copy_to_user(buff + fl, lx->lx_buffer, count - fl);
334
335out:
336 count -= failed;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000337
Ralf Baechle61dcc6f2007-03-15 17:10:16 +0000338 smp_wmb();
339 lx->lx_read = (lx->lx_read + count) % lx->buffer_size;
340 smp_wmb();
Ralf Baechlebc4809e2007-03-15 17:13:47 +0000341 mutex_unlock(&channel_wqs[index].mutex);
Ralf Baechlee01402b2005-07-14 15:57:16 +0000342
Ralf Baechleafc48412005-10-31 00:30:39 +0000343 return count;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000344}
345
Ralf Baechle7f5a7712007-04-25 15:08:57 +0100346ssize_t rtlx_write(int index, const void __user *buffer, size_t count)
Ralf Baechlee01402b2005-07-14 15:57:16 +0000347{
Ralf Baechlee01402b2005-07-14 15:57:16 +0000348 struct rtlx_channel *rt;
Ralf Baechle7f5a7712007-04-25 15:08:57 +0100349 unsigned long failed;
Ralf Baechle61dcc6f2007-03-15 17:10:16 +0000350 size_t rt_read;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000351 size_t fl;
Ralf Baechle26009902006-04-05 09:45:45 +0100352
353 if (rtlx == NULL)
354 return(-ENOSYS);
355
356 rt = &rtlx->channel[index];
357
Ralf Baechlebc4809e2007-03-15 17:13:47 +0000358 mutex_lock(&channel_wqs[index].mutex);
Ralf Baechle61dcc6f2007-03-15 17:10:16 +0000359 smp_rmb();
360 rt_read = rt->rt_read;
361
Ralf Baechle26009902006-04-05 09:45:45 +0100362 /* total number of bytes to copy */
Kevin D. Kissell1928cc82008-04-16 15:32:22 +0200363 count = min(count, (size_t)write_spacefree(rt_read, rt->rt_write,
364 rt->buffer_size));
Ralf Baechle26009902006-04-05 09:45:45 +0100365
366 /* first bit from write pointer to the end of the buffer, or count */
367 fl = min(count, (size_t) rt->buffer_size - rt->rt_write);
368
Ralf Baechle46230aa2007-03-16 12:16:27 +0000369 failed = copy_from_user(rt->rt_buffer + rt->rt_write, buffer, fl);
370 if (failed)
371 goto out;
Ralf Baechle26009902006-04-05 09:45:45 +0100372
373 /* if there's any left copy to the beginning of the buffer */
Ralf Baechle46230aa2007-03-16 12:16:27 +0000374 if (count - fl) {
375 failed = copy_from_user(rt->rt_buffer, buffer + fl, count - fl);
376 }
377
378out:
Ralf Baechle7f5a7712007-04-25 15:08:57 +0100379 count -= failed;
Ralf Baechle26009902006-04-05 09:45:45 +0100380
Ralf Baechle61dcc6f2007-03-15 17:10:16 +0000381 smp_wmb();
382 rt->rt_write = (rt->rt_write + count) % rt->buffer_size;
383 smp_wmb();
Ralf Baechlebc4809e2007-03-15 17:13:47 +0000384 mutex_unlock(&channel_wqs[index].mutex);
Ralf Baechle26009902006-04-05 09:45:45 +0100385
Ralf Baechle61dcc6f2007-03-15 17:10:16 +0000386 return count;
Ralf Baechle26009902006-04-05 09:45:45 +0100387}
388
389
390static int file_open(struct inode *inode, struct file *filp)
391{
Ralf Baechle1bbfc202009-09-29 00:52:27 +0100392 return rtlx_open(iminor(inode), (filp->f_flags & O_NONBLOCK) ? 0 : 1);
Ralf Baechle26009902006-04-05 09:45:45 +0100393}
394
395static int file_release(struct inode *inode, struct file *filp)
396{
Ralf Baechle1bbfc202009-09-29 00:52:27 +0100397 return rtlx_release(iminor(inode));
Ralf Baechle26009902006-04-05 09:45:45 +0100398}
399
400static unsigned int file_poll(struct file *file, poll_table * wait)
401{
402 int minor;
403 unsigned int mask = 0;
404
Josef Sipek1b04fe92006-12-08 02:37:20 -0800405 minor = iminor(file->f_path.dentry->d_inode);
Ralf Baechle26009902006-04-05 09:45:45 +0100406
407 poll_wait(file, &channel_wqs[minor].rt_queue, wait);
408 poll_wait(file, &channel_wqs[minor].lx_queue, wait);
409
410 if (rtlx == NULL)
411 return 0;
412
413 /* data available to read? */
414 if (rtlx_read_poll(minor, 0))
415 mask |= POLLIN | POLLRDNORM;
416
417 /* space to write */
418 if (rtlx_write_poll(minor))
419 mask |= POLLOUT | POLLWRNORM;
420
421 return mask;
422}
423
424static ssize_t file_read(struct file *file, char __user * buffer, size_t count,
425 loff_t * ppos)
426{
Josef Sipek1b04fe92006-12-08 02:37:20 -0800427 int minor = iminor(file->f_path.dentry->d_inode);
Ralf Baechle26009902006-04-05 09:45:45 +0100428
429 /* data available? */
430 if (!rtlx_read_poll(minor, (file->f_flags & O_NONBLOCK) ? 0 : 1)) {
431 return 0; // -EAGAIN makes cat whinge
432 }
433
Ralf Baechle46230aa2007-03-16 12:16:27 +0000434 return rtlx_read(minor, buffer, count);
Ralf Baechle26009902006-04-05 09:45:45 +0100435}
436
437static ssize_t file_write(struct file *file, const char __user * buffer,
438 size_t count, loff_t * ppos)
439{
440 int minor;
441 struct rtlx_channel *rt;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000442
Josef Sipek1b04fe92006-12-08 02:37:20 -0800443 minor = iminor(file->f_path.dentry->d_inode);
Ralf Baechlee01402b2005-07-14 15:57:16 +0000444 rt = &rtlx->channel[minor];
445
446 /* any space left... */
Ralf Baechle26009902006-04-05 09:45:45 +0100447 if (!rtlx_write_poll(minor)) {
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000448 int ret = 0;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000449
450 if (file->f_flags & O_NONBLOCK)
Ralf Baechleafc48412005-10-31 00:30:39 +0000451 return -EAGAIN;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000452
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000453 __wait_event_interruptible(channel_wqs[minor].rt_queue,
Ralf Baechle70342282013-01-22 12:59:30 +0100454 rtlx_write_poll(minor),
455 ret);
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000456 if (ret)
457 return ret;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000458 }
459
Ralf Baechle46230aa2007-03-16 12:16:27 +0000460 return rtlx_write(minor, buffer, count);
Ralf Baechlee01402b2005-07-14 15:57:16 +0000461}
462
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800463static const struct file_operations rtlx_fops = {
Ralf Baechle26009902006-04-05 09:45:45 +0100464 .owner = THIS_MODULE,
Ralf Baechle70342282013-01-22 12:59:30 +0100465 .open = file_open,
Ralf Baechle26009902006-04-05 09:45:45 +0100466 .release = file_release,
467 .write = file_write,
Ralf Baechle70342282013-01-22 12:59:30 +0100468 .read = file_read,
469 .poll = file_poll,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200470 .llseek = noop_llseek,
Ralf Baechlee01402b2005-07-14 15:57:16 +0000471};
472
Ralf Baechle26009902006-04-05 09:45:45 +0100473static struct irqaction rtlx_irq = {
474 .handler = rtlx_interrupt,
Ralf Baechle26009902006-04-05 09:45:45 +0100475 .name = "RTLX",
476};
477
Atsushi Nemoto97dcb822007-01-08 02:14:29 +0900478static int rtlx_irq_num = MIPS_CPU_IRQ_BASE + MIPS_CPU_RTLX_IRQ;
Ralf Baechle26009902006-04-05 09:45:45 +0100479
Ralf Baechleafc48412005-10-31 00:30:39 +0000480static char register_chrdev_failed[] __initdata =
481 KERN_ERR "rtlx_module_init: unable to register device\n";
482
Ralf Baechle9d5a3f52007-07-28 00:51:45 +0100483static int __init rtlx_module_init(void)
Ralf Baechlee01402b2005-07-14 15:57:16 +0000484{
Ralf Baechlebb3d7c72007-02-07 15:36:56 +0000485 struct device *dev;
486 int i, err;
Ralf Baechle26009902006-04-05 09:45:45 +0100487
Ralf Baechle07cc0c92007-07-27 19:31:10 +0100488 if (!cpu_has_mipsmt) {
489 printk("VPE loader: not a MIPS MT capable processor\n");
490 return -ENODEV;
491 }
492
493 if (tclimit == 0) {
494 printk(KERN_WARNING "No TCs reserved for AP/SP, not "
495 "initializing RTLX.\nPass maxtcs=<n> argument as kernel "
496 "argument\n");
497
498 return -ENODEV;
499 }
500
Ralf Baechleafc48412005-10-31 00:30:39 +0000501 major = register_chrdev(0, module_name, &rtlx_fops);
502 if (major < 0) {
503 printk(register_chrdev_failed);
504 return major;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000505 }
506
Ralf Baechle26009902006-04-05 09:45:45 +0100507 /* initialise the wait queues */
508 for (i = 0; i < RTLX_CHANNELS; i++) {
509 init_waitqueue_head(&channel_wqs[i].rt_queue);
510 init_waitqueue_head(&channel_wqs[i].lx_queue);
Ralf Baechlec4c40182007-02-23 13:40:45 +0000511 atomic_set(&channel_wqs[i].in_open, 0);
Ralf Baechlebc4809e2007-03-15 17:13:47 +0000512 mutex_init(&channel_wqs[i].mutex);
Ralf Baechlebb3d7c72007-02-07 15:36:56 +0000513
Greg Kroah-Hartmana9b12612008-07-21 20:03:34 -0700514 dev = device_create(mt_class, NULL, MKDEV(major, i), NULL,
515 "%s%d", module_name, i);
Ralf Baechlebb3d7c72007-02-07 15:36:56 +0000516 if (IS_ERR(dev)) {
517 err = PTR_ERR(dev);
518 goto out_chrdev;
519 }
Ralf Baechle26009902006-04-05 09:45:45 +0100520 }
521
522 /* set up notifiers */
523 notify.start = starting;
524 notify.stop = stopping;
Ralf Baechle07cc0c92007-07-27 19:31:10 +0100525 vpe_notify(tclimit, &notify);
Ralf Baechle26009902006-04-05 09:45:45 +0100526
527 if (cpu_has_vint)
528 set_vi_handler(MIPS_CPU_RTLX_IRQ, rtlx_dispatch);
Kevin D. Kissell1928cc82008-04-16 15:32:22 +0200529 else {
530 pr_err("APRP RTLX init on non-vectored-interrupt processor\n");
531 err = -ENODEV;
532 goto out_chrdev;
533 }
Ralf Baechle26009902006-04-05 09:45:45 +0100534
535 rtlx_irq.dev_id = rtlx;
536 setup_irq(rtlx_irq_num, &rtlx_irq);
537
Ralf Baechleafc48412005-10-31 00:30:39 +0000538 return 0;
Ralf Baechlebb3d7c72007-02-07 15:36:56 +0000539
540out_chrdev:
541 for (i = 0; i < RTLX_CHANNELS; i++)
542 device_destroy(mt_class, MKDEV(major, i));
543
544 return err;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000545}
546
Ralf Baechleafc48412005-10-31 00:30:39 +0000547static void __exit rtlx_module_exit(void)
Ralf Baechlee01402b2005-07-14 15:57:16 +0000548{
Ralf Baechlebb3d7c72007-02-07 15:36:56 +0000549 int i;
550
551 for (i = 0; i < RTLX_CHANNELS; i++)
552 device_destroy(mt_class, MKDEV(major, i));
553
Ralf Baechlee01402b2005-07-14 15:57:16 +0000554 unregister_chrdev(major, module_name);
555}
556
557module_init(rtlx_module_init);
558module_exit(rtlx_module_exit);
Ralf Baechleafc48412005-10-31 00:30:39 +0000559
Ralf Baechlee01402b2005-07-14 15:57:16 +0000560MODULE_DESCRIPTION("MIPS RTLX");
Ralf Baechle26009902006-04-05 09:45:45 +0100561MODULE_AUTHOR("Elizabeth Oldham, MIPS Technologies, Inc.");
Ralf Baechlee01402b2005-07-14 15:57:16 +0000562MODULE_LICENSE("GPL");