blob: 1ba00c15505be5617111c5b6877ef02ca5c63a22 [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>
22#include <linux/module.h>
23#include <linux/fs.h>
24#include <linux/init.h>
Ralf Baechle26009902006-04-05 09:45:45 +010025#include <asm/uaccess.h>
26#include <linux/slab.h>
27#include <linux/list.h>
28#include <linux/vmalloc.h>
29#include <linux/elf.h>
30#include <linux/seq_file.h>
31#include <linux/syscalls.h>
32#include <linux/moduleloader.h>
Ralf Baechlea84c96e2006-01-15 18:11:28 +000033#include <linux/interrupt.h>
Ralf Baechlee01402b2005-07-14 15:57:16 +000034#include <linux/poll.h>
35#include <linux/sched.h>
36#include <linux/wait.h>
37#include <asm/mipsmtregs.h>
Ralf Baechlebb3d7c72007-02-07 15:36:56 +000038#include <asm/mips_mt.h>
Ralf Baechle26009902006-04-05 09:45:45 +010039#include <asm/cacheflush.h>
40#include <asm/atomic.h>
Ralf Baechlee01402b2005-07-14 15:57:16 +000041#include <asm/cpu.h>
42#include <asm/processor.h>
Ralf Baechle07cc0c92007-07-27 19:31:10 +010043#include <asm/mips_mt.h>
Ralf Baechle26009902006-04-05 09:45:45 +010044#include <asm/system.h>
45#include <asm/vpe.h>
Ralf Baechlee01402b2005-07-14 15:57:16 +000046#include <asm/rtlx.h>
47
Ralf Baechleafc48412005-10-31 00:30:39 +000048static struct rtlx_info *rtlx;
Ralf Baechlee01402b2005-07-14 15:57:16 +000049static int major;
50static char module_name[] = "rtlx";
Ralf Baechlee01402b2005-07-14 15:57:16 +000051
52static struct chan_waitqueues {
53 wait_queue_head_t rt_queue;
54 wait_queue_head_t lx_queue;
Ralf Baechlec4c40182007-02-23 13:40:45 +000055 atomic_t in_open;
Ralf Baechlebc4809e2007-03-15 17:13:47 +000056 struct mutex mutex;
Ralf Baechlee01402b2005-07-14 15:57:16 +000057} channel_wqs[RTLX_CHANNELS];
58
Ralf Baechle26009902006-04-05 09:45:45 +010059static struct vpe_notifications notify;
60static int sp_stopping = 0;
61
Ralf Baechlee01402b2005-07-14 15:57:16 +000062extern void *vpe_get_shared(int index);
63
Ralf Baechle937a8012006-10-07 19:44:33 +010064static void rtlx_dispatch(void)
Ralf Baechlee01402b2005-07-14 15:57:16 +000065{
Atsushi Nemoto97dcb822007-01-08 02:14:29 +090066 do_IRQ(MIPS_CPU_IRQ_BASE + MIPS_CPU_RTLX_IRQ);
Ralf Baechlee01402b2005-07-14 15:57:16 +000067}
68
Ralf Baechle26009902006-04-05 09:45:45 +010069
70/* Interrupt handler may be called before rtlx_init has otherwise had
71 a chance to run.
72*/
Ralf Baechle937a8012006-10-07 19:44:33 +010073static irqreturn_t rtlx_interrupt(int irq, void *dev_id)
Ralf Baechlee01402b2005-07-14 15:57:16 +000074{
Ralf Baechlee01402b2005-07-14 15:57:16 +000075 int i;
76
77 for (i = 0; i < RTLX_CHANNELS; i++) {
Ralf Baechle26009902006-04-05 09:45:45 +010078 wake_up(&channel_wqs[i].lx_queue);
79 wake_up(&channel_wqs[i].rt_queue);
Ralf Baechlee01402b2005-07-14 15:57:16 +000080 }
81
Ralf Baechleafc48412005-10-31 00:30:39 +000082 return IRQ_HANDLED;
Ralf Baechlee01402b2005-07-14 15:57:16 +000083}
84
David Rientjesf5dbeaf2007-07-22 01:01:39 -070085static void __used dump_rtlx(void)
Ralf Baechlee01402b2005-07-14 15:57:16 +000086{
87 int i;
88
Ralf Baechle26009902006-04-05 09:45:45 +010089 printk("id 0x%lx state %d\n", rtlx->id, rtlx->state);
90
91 for (i = 0; i < RTLX_CHANNELS; i++) {
92 struct rtlx_channel *chan = &rtlx->channel[i];
93
94 printk(" rt_state %d lx_state %d buffer_size %d\n",
95 chan->rt_state, chan->lx_state, chan->buffer_size);
96
97 printk(" rt_read %d rt_write %d\n",
98 chan->rt_read, chan->rt_write);
99
100 printk(" lx_read %d lx_write %d\n",
101 chan->lx_read, chan->lx_write);
102
103 printk(" rt_buffer <%s>\n", chan->rt_buffer);
104 printk(" lx_buffer <%s>\n", chan->lx_buffer);
105 }
106}
107
108/* call when we have the address of the shared structure from the SP side. */
109static int rtlx_init(struct rtlx_info *rtlxi)
110{
Ralf Baechlee01402b2005-07-14 15:57:16 +0000111 if (rtlxi->id != RTLX_ID) {
Ralf Baechlee606c102007-09-12 16:31:12 +0100112 printk(KERN_ERR "no valid RTLX id at 0x%p 0x%lx\n", rtlxi, rtlxi->id);
Ralf Baechleafc48412005-10-31 00:30:39 +0000113 return -ENOEXEC;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000114 }
115
Ralf Baechlee01402b2005-07-14 15:57:16 +0000116 rtlx = rtlxi;
Ralf Baechleafc48412005-10-31 00:30:39 +0000117
118 return 0;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000119}
120
Ralf Baechle26009902006-04-05 09:45:45 +0100121/* notifications */
122static void starting(int vpe)
Ralf Baechlee01402b2005-07-14 15:57:16 +0000123{
Ralf Baechle26009902006-04-05 09:45:45 +0100124 int i;
125 sp_stopping = 0;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000126
Ralf Baechle26009902006-04-05 09:45:45 +0100127 /* force a reload of rtlx */
128 rtlx=NULL;
129
130 /* wake up any sleeping rtlx_open's */
131 for (i = 0; i < RTLX_CHANNELS; i++)
132 wake_up_interruptible(&channel_wqs[i].lx_queue);
133}
134
135static void stopping(int vpe)
136{
137 int i;
138
139 sp_stopping = 1;
140 for (i = 0; i < RTLX_CHANNELS; i++)
141 wake_up_interruptible(&channel_wqs[i].lx_queue);
142}
143
144
145int rtlx_open(int index, int can_sleep)
146{
Ralf Baechle9e346822007-03-15 17:08:28 +0000147 struct rtlx_info **p;
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000148 struct rtlx_channel *chan;
Ralf Baechlec4c40182007-02-23 13:40:45 +0000149 enum rtlx_state state;
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000150 int ret = 0;
Ralf Baechle26009902006-04-05 09:45:45 +0100151
152 if (index >= RTLX_CHANNELS) {
153 printk(KERN_DEBUG "rtlx_open index out of range\n");
154 return -ENOSYS;
155 }
156
Ralf Baechlec4c40182007-02-23 13:40:45 +0000157 if (atomic_inc_return(&channel_wqs[index].in_open) > 1) {
158 printk(KERN_DEBUG "rtlx_open channel %d already opened\n",
159 index);
160 ret = -EBUSY;
161 goto out_fail;
Ralf Baechle26009902006-04-05 09:45:45 +0100162 }
163
Ralf Baechlee01402b2005-07-14 15:57:16 +0000164 if (rtlx == NULL) {
Ralf Baechle07cc0c92007-07-27 19:31:10 +0100165 if( (p = vpe_get_shared(tclimit)) == NULL) {
Ralf Baechle26009902006-04-05 09:45:45 +0100166 if (can_sleep) {
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000167 __wait_event_interruptible(channel_wqs[index].lx_queue,
Ralf Baechle07cc0c92007-07-27 19:31:10 +0100168 (p = vpe_get_shared(tclimit)),
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000169 ret);
170 if (ret)
171 goto out_fail;
Ralf Baechle26009902006-04-05 09:45:45 +0100172 } else {
Ralf Baechlec4c40182007-02-23 13:40:45 +0000173 printk(KERN_DEBUG "No SP program loaded, and device "
Ralf Baechle26009902006-04-05 09:45:45 +0100174 "opened with O_NONBLOCK\n");
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000175 ret = -ENOSYS;
176 goto out_fail;
Ralf Baechle26009902006-04-05 09:45:45 +0100177 }
Ralf Baechlee01402b2005-07-14 15:57:16 +0000178 }
179
Ralf Baechle9e346822007-03-15 17:08:28 +0000180 smp_rmb();
Ralf Baechlee01402b2005-07-14 15:57:16 +0000181 if (*p == NULL) {
Ralf Baechle26009902006-04-05 09:45:45 +0100182 if (can_sleep) {
Ralf Baechle9e346822007-03-15 17:08:28 +0000183 DEFINE_WAIT(wait);
184
185 for (;;) {
186 prepare_to_wait(&channel_wqs[index].lx_queue, &wait, TASK_INTERRUPTIBLE);
187 smp_rmb();
188 if (*p != NULL)
189 break;
190 if (!signal_pending(current)) {
191 schedule();
192 continue;
193 }
194 ret = -ERESTARTSYS;
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000195 goto out_fail;
Ralf Baechle9e346822007-03-15 17:08:28 +0000196 }
197 finish_wait(&channel_wqs[index].lx_queue, &wait);
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000198 } else {
Ralf Baechle26009902006-04-05 09:45:45 +0100199 printk(" *vpe_get_shared is NULL. "
200 "Has an SP program been loaded?\n");
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000201 ret = -ENOSYS;
202 goto out_fail;
Ralf Baechle26009902006-04-05 09:45:45 +0100203 }
Ralf Baechlee01402b2005-07-14 15:57:16 +0000204 }
205
Ralf Baechle26009902006-04-05 09:45:45 +0100206 if ((unsigned int)*p < KSEG0) {
207 printk(KERN_WARNING "vpe_get_shared returned an invalid pointer "
208 "maybe an error code %d\n", (int)*p);
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000209 ret = -ENOSYS;
210 goto out_fail;
Ralf Baechle26009902006-04-05 09:45:45 +0100211 }
212
Ralf Baechlec4c40182007-02-23 13:40:45 +0000213 if ((ret = rtlx_init(*p)) < 0)
214 goto out_ret;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000215 }
216
Ralf Baechle26009902006-04-05 09:45:45 +0100217 chan = &rtlx->channel[index];
Ralf Baechlee01402b2005-07-14 15:57:16 +0000218
Ralf Baechlec4c40182007-02-23 13:40:45 +0000219 state = xchg(&chan->lx_state, RTLX_STATE_OPENED);
220 if (state == RTLX_STATE_OPENED) {
221 ret = -EBUSY;
222 goto out_fail;
223 }
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000224
225out_fail:
Ralf Baechlec4c40182007-02-23 13:40:45 +0000226 smp_mb();
227 atomic_dec(&channel_wqs[index].in_open);
228 smp_mb();
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000229
Ralf Baechlec4c40182007-02-23 13:40:45 +0000230out_ret:
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000231 return ret;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000232}
233
Ralf Baechle26009902006-04-05 09:45:45 +0100234int rtlx_release(int index)
Ralf Baechlee01402b2005-07-14 15:57:16 +0000235{
Ralf Baechle26009902006-04-05 09:45:45 +0100236 rtlx->channel[index].lx_state = RTLX_STATE_UNUSED;
Ralf Baechleafc48412005-10-31 00:30:39 +0000237 return 0;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000238}
239
Ralf Baechle26009902006-04-05 09:45:45 +0100240unsigned int rtlx_read_poll(int index, int can_sleep)
Ralf Baechlee01402b2005-07-14 15:57:16 +0000241{
Ralf Baechle26009902006-04-05 09:45:45 +0100242 struct rtlx_channel *chan;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000243
Ralf Baechle26009902006-04-05 09:45:45 +0100244 if (rtlx == NULL)
245 return 0;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000246
Ralf Baechle26009902006-04-05 09:45:45 +0100247 chan = &rtlx->channel[index];
Ralf Baechlee01402b2005-07-14 15:57:16 +0000248
249 /* data available to read? */
Ralf Baechle26009902006-04-05 09:45:45 +0100250 if (chan->lx_read == chan->lx_write) {
251 if (can_sleep) {
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000252 int ret = 0;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000253
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000254 __wait_event_interruptible(channel_wqs[index].lx_queue,
255 chan->lx_read != chan->lx_write || sp_stopping,
256 ret);
257 if (ret)
258 return ret;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000259
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000260 if (sp_stopping)
261 return 0;
262 } else
Ralf Baechle26009902006-04-05 09:45:45 +0100263 return 0;
264 }
265
266 return (chan->lx_write + chan->buffer_size - chan->lx_read)
267 % chan->buffer_size;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000268}
269
Ralf Baechle26009902006-04-05 09:45:45 +0100270static inline int write_spacefree(int read, int write, int size)
Ralf Baechlee01402b2005-07-14 15:57:16 +0000271{
Ralf Baechle26009902006-04-05 09:45:45 +0100272 if (read == write) {
273 /*
274 * Never fill the buffer completely, so indexes are always
275 * equal if empty and only empty, or !equal if data available
276 */
277 return size - 1;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000278 }
279
Ralf Baechle26009902006-04-05 09:45:45 +0100280 return ((read + size - write) % size) - 1;
281}
282
283unsigned int rtlx_write_poll(int index)
284{
285 struct rtlx_channel *chan = &rtlx->channel[index];
286 return write_spacefree(chan->rt_read, chan->rt_write, chan->buffer_size);
287}
288
Ralf Baechle7f5a7712007-04-25 15:08:57 +0100289ssize_t rtlx_read(int index, void __user *buff, size_t count)
Ralf Baechle26009902006-04-05 09:45:45 +0100290{
Ralf Baechle61dcc6f2007-03-15 17:10:16 +0000291 size_t lx_write, fl = 0L;
Ralf Baechle26009902006-04-05 09:45:45 +0100292 struct rtlx_channel *lx;
Ralf Baechle46230aa2007-03-16 12:16:27 +0000293 unsigned long failed;
Ralf Baechle26009902006-04-05 09:45:45 +0100294
295 if (rtlx == NULL)
296 return -ENOSYS;
297
298 lx = &rtlx->channel[index];
299
Ralf Baechlebc4809e2007-03-15 17:13:47 +0000300 mutex_lock(&channel_wqs[index].mutex);
Ralf Baechle61dcc6f2007-03-15 17:10:16 +0000301 smp_rmb();
302 lx_write = lx->lx_write;
303
Ralf Baechlee01402b2005-07-14 15:57:16 +0000304 /* find out how much in total */
Ralf Baechleafc48412005-10-31 00:30:39 +0000305 count = min(count,
Ralf Baechle61dcc6f2007-03-15 17:10:16 +0000306 (size_t)(lx_write + lx->buffer_size - lx->lx_read)
Ralf Baechle26009902006-04-05 09:45:45 +0100307 % lx->buffer_size);
Ralf Baechlee01402b2005-07-14 15:57:16 +0000308
309 /* then how much from the read pointer onwards */
Ralf Baechle61dcc6f2007-03-15 17:10:16 +0000310 fl = min(count, (size_t)lx->buffer_size - lx->lx_read);
Ralf Baechlee01402b2005-07-14 15:57:16 +0000311
Ralf Baechle46230aa2007-03-16 12:16:27 +0000312 failed = copy_to_user(buff, lx->lx_buffer + lx->lx_read, fl);
313 if (failed)
314 goto out;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000315
316 /* and if there is anything left at the beginning of the buffer */
Ralf Baechle61dcc6f2007-03-15 17:10:16 +0000317 if (count - fl)
Ralf Baechle46230aa2007-03-16 12:16:27 +0000318 failed = copy_to_user(buff + fl, lx->lx_buffer, count - fl);
319
320out:
321 count -= failed;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000322
Ralf Baechle61dcc6f2007-03-15 17:10:16 +0000323 smp_wmb();
324 lx->lx_read = (lx->lx_read + count) % lx->buffer_size;
325 smp_wmb();
Ralf Baechlebc4809e2007-03-15 17:13:47 +0000326 mutex_unlock(&channel_wqs[index].mutex);
Ralf Baechlee01402b2005-07-14 15:57:16 +0000327
Ralf Baechleafc48412005-10-31 00:30:39 +0000328 return count;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000329}
330
Ralf Baechle7f5a7712007-04-25 15:08:57 +0100331ssize_t rtlx_write(int index, const void __user *buffer, size_t count)
Ralf Baechlee01402b2005-07-14 15:57:16 +0000332{
Ralf Baechlee01402b2005-07-14 15:57:16 +0000333 struct rtlx_channel *rt;
Ralf Baechle7f5a7712007-04-25 15:08:57 +0100334 unsigned long failed;
Ralf Baechle61dcc6f2007-03-15 17:10:16 +0000335 size_t rt_read;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000336 size_t fl;
Ralf Baechle26009902006-04-05 09:45:45 +0100337
338 if (rtlx == NULL)
339 return(-ENOSYS);
340
341 rt = &rtlx->channel[index];
342
Ralf Baechlebc4809e2007-03-15 17:13:47 +0000343 mutex_lock(&channel_wqs[index].mutex);
Ralf Baechle61dcc6f2007-03-15 17:10:16 +0000344 smp_rmb();
345 rt_read = rt->rt_read;
346
Ralf Baechle26009902006-04-05 09:45:45 +0100347 /* total number of bytes to copy */
348 count = min(count,
Ralf Baechle61dcc6f2007-03-15 17:10:16 +0000349 (size_t)write_spacefree(rt_read, rt->rt_write, rt->buffer_size));
Ralf Baechle26009902006-04-05 09:45:45 +0100350
351 /* first bit from write pointer to the end of the buffer, or count */
352 fl = min(count, (size_t) rt->buffer_size - rt->rt_write);
353
Ralf Baechle46230aa2007-03-16 12:16:27 +0000354 failed = copy_from_user(rt->rt_buffer + rt->rt_write, buffer, fl);
355 if (failed)
356 goto out;
Ralf Baechle26009902006-04-05 09:45:45 +0100357
358 /* if there's any left copy to the beginning of the buffer */
Ralf Baechle46230aa2007-03-16 12:16:27 +0000359 if (count - fl) {
360 failed = copy_from_user(rt->rt_buffer, buffer + fl, count - fl);
361 }
362
363out:
Ralf Baechle7f5a7712007-04-25 15:08:57 +0100364 count -= failed;
Ralf Baechle26009902006-04-05 09:45:45 +0100365
Ralf Baechle61dcc6f2007-03-15 17:10:16 +0000366 smp_wmb();
367 rt->rt_write = (rt->rt_write + count) % rt->buffer_size;
368 smp_wmb();
Ralf Baechlebc4809e2007-03-15 17:13:47 +0000369 mutex_unlock(&channel_wqs[index].mutex);
Ralf Baechle26009902006-04-05 09:45:45 +0100370
Ralf Baechle61dcc6f2007-03-15 17:10:16 +0000371 return count;
Ralf Baechle26009902006-04-05 09:45:45 +0100372}
373
374
375static int file_open(struct inode *inode, struct file *filp)
376{
Ralf Baechle79e55bc2006-04-24 17:15:10 +0100377 int minor = iminor(inode);
Ralf Baechle26009902006-04-05 09:45:45 +0100378
379 return rtlx_open(minor, (filp->f_flags & O_NONBLOCK) ? 0 : 1);
380}
381
382static int file_release(struct inode *inode, struct file *filp)
383{
Ralf Baechle79e55bc2006-04-24 17:15:10 +0100384 int minor = iminor(inode);
Ralf Baechle26009902006-04-05 09:45:45 +0100385
386 return rtlx_release(minor);
387}
388
389static unsigned int file_poll(struct file *file, poll_table * wait)
390{
391 int minor;
392 unsigned int mask = 0;
393
Josef Sipek1b04fe92006-12-08 02:37:20 -0800394 minor = iminor(file->f_path.dentry->d_inode);
Ralf Baechle26009902006-04-05 09:45:45 +0100395
396 poll_wait(file, &channel_wqs[minor].rt_queue, wait);
397 poll_wait(file, &channel_wqs[minor].lx_queue, wait);
398
399 if (rtlx == NULL)
400 return 0;
401
402 /* data available to read? */
403 if (rtlx_read_poll(minor, 0))
404 mask |= POLLIN | POLLRDNORM;
405
406 /* space to write */
407 if (rtlx_write_poll(minor))
408 mask |= POLLOUT | POLLWRNORM;
409
410 return mask;
411}
412
413static ssize_t file_read(struct file *file, char __user * buffer, size_t count,
414 loff_t * ppos)
415{
Josef Sipek1b04fe92006-12-08 02:37:20 -0800416 int minor = iminor(file->f_path.dentry->d_inode);
Ralf Baechle26009902006-04-05 09:45:45 +0100417
418 /* data available? */
419 if (!rtlx_read_poll(minor, (file->f_flags & O_NONBLOCK) ? 0 : 1)) {
420 return 0; // -EAGAIN makes cat whinge
421 }
422
Ralf Baechle46230aa2007-03-16 12:16:27 +0000423 return rtlx_read(minor, buffer, count);
Ralf Baechle26009902006-04-05 09:45:45 +0100424}
425
426static ssize_t file_write(struct file *file, const char __user * buffer,
427 size_t count, loff_t * ppos)
428{
429 int minor;
430 struct rtlx_channel *rt;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000431
Josef Sipek1b04fe92006-12-08 02:37:20 -0800432 minor = iminor(file->f_path.dentry->d_inode);
Ralf Baechlee01402b2005-07-14 15:57:16 +0000433 rt = &rtlx->channel[minor];
434
435 /* any space left... */
Ralf Baechle26009902006-04-05 09:45:45 +0100436 if (!rtlx_write_poll(minor)) {
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000437 int ret = 0;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000438
439 if (file->f_flags & O_NONBLOCK)
Ralf Baechleafc48412005-10-31 00:30:39 +0000440 return -EAGAIN;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000441
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000442 __wait_event_interruptible(channel_wqs[minor].rt_queue,
443 rtlx_write_poll(minor),
444 ret);
445 if (ret)
446 return ret;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000447 }
448
Ralf Baechle46230aa2007-03-16 12:16:27 +0000449 return rtlx_write(minor, buffer, count);
Ralf Baechlee01402b2005-07-14 15:57:16 +0000450}
451
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800452static const struct file_operations rtlx_fops = {
Ralf Baechle26009902006-04-05 09:45:45 +0100453 .owner = THIS_MODULE,
454 .open = file_open,
455 .release = file_release,
456 .write = file_write,
457 .read = file_read,
458 .poll = file_poll
Ralf Baechlee01402b2005-07-14 15:57:16 +0000459};
460
Ralf Baechle26009902006-04-05 09:45:45 +0100461static struct irqaction rtlx_irq = {
462 .handler = rtlx_interrupt,
Thomas Gleixnerf40298f2006-07-01 19:29:20 -0700463 .flags = IRQF_DISABLED,
Ralf Baechle26009902006-04-05 09:45:45 +0100464 .name = "RTLX",
465};
466
Atsushi Nemoto97dcb822007-01-08 02:14:29 +0900467static int rtlx_irq_num = MIPS_CPU_IRQ_BASE + MIPS_CPU_RTLX_IRQ;
Ralf Baechle26009902006-04-05 09:45:45 +0100468
Ralf Baechleafc48412005-10-31 00:30:39 +0000469static char register_chrdev_failed[] __initdata =
470 KERN_ERR "rtlx_module_init: unable to register device\n";
471
Ralf Baechle9d5a3f52007-07-28 00:51:45 +0100472static int __init rtlx_module_init(void)
Ralf Baechlee01402b2005-07-14 15:57:16 +0000473{
Ralf Baechlebb3d7c72007-02-07 15:36:56 +0000474 struct device *dev;
475 int i, err;
Ralf Baechle26009902006-04-05 09:45:45 +0100476
Ralf Baechle07cc0c92007-07-27 19:31:10 +0100477 if (!cpu_has_mipsmt) {
478 printk("VPE loader: not a MIPS MT capable processor\n");
479 return -ENODEV;
480 }
481
482 if (tclimit == 0) {
483 printk(KERN_WARNING "No TCs reserved for AP/SP, not "
484 "initializing RTLX.\nPass maxtcs=<n> argument as kernel "
485 "argument\n");
486
487 return -ENODEV;
488 }
489
Ralf Baechleafc48412005-10-31 00:30:39 +0000490 major = register_chrdev(0, module_name, &rtlx_fops);
491 if (major < 0) {
492 printk(register_chrdev_failed);
493 return major;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000494 }
495
Ralf Baechle26009902006-04-05 09:45:45 +0100496 /* initialise the wait queues */
497 for (i = 0; i < RTLX_CHANNELS; i++) {
498 init_waitqueue_head(&channel_wqs[i].rt_queue);
499 init_waitqueue_head(&channel_wqs[i].lx_queue);
Ralf Baechlec4c40182007-02-23 13:40:45 +0000500 atomic_set(&channel_wqs[i].in_open, 0);
Ralf Baechlebc4809e2007-03-15 17:13:47 +0000501 mutex_init(&channel_wqs[i].mutex);
Ralf Baechlebb3d7c72007-02-07 15:36:56 +0000502
503 dev = device_create(mt_class, NULL, MKDEV(major, i),
504 "%s%d", module_name, i);
505 if (IS_ERR(dev)) {
506 err = PTR_ERR(dev);
507 goto out_chrdev;
508 }
Ralf Baechle26009902006-04-05 09:45:45 +0100509 }
510
511 /* set up notifiers */
512 notify.start = starting;
513 notify.stop = stopping;
Ralf Baechle07cc0c92007-07-27 19:31:10 +0100514 vpe_notify(tclimit, &notify);
Ralf Baechle26009902006-04-05 09:45:45 +0100515
516 if (cpu_has_vint)
517 set_vi_handler(MIPS_CPU_RTLX_IRQ, rtlx_dispatch);
518
519 rtlx_irq.dev_id = rtlx;
520 setup_irq(rtlx_irq_num, &rtlx_irq);
521
Ralf Baechleafc48412005-10-31 00:30:39 +0000522 return 0;
Ralf Baechlebb3d7c72007-02-07 15:36:56 +0000523
524out_chrdev:
525 for (i = 0; i < RTLX_CHANNELS; i++)
526 device_destroy(mt_class, MKDEV(major, i));
527
528 return err;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000529}
530
Ralf Baechleafc48412005-10-31 00:30:39 +0000531static void __exit rtlx_module_exit(void)
Ralf Baechlee01402b2005-07-14 15:57:16 +0000532{
Ralf Baechlebb3d7c72007-02-07 15:36:56 +0000533 int i;
534
535 for (i = 0; i < RTLX_CHANNELS; i++)
536 device_destroy(mt_class, MKDEV(major, i));
537
Ralf Baechlee01402b2005-07-14 15:57:16 +0000538 unregister_chrdev(major, module_name);
539}
540
541module_init(rtlx_module_init);
542module_exit(rtlx_module_exit);
Ralf Baechleafc48412005-10-31 00:30:39 +0000543
Ralf Baechlee01402b2005-07-14 15:57:16 +0000544MODULE_DESCRIPTION("MIPS RTLX");
Ralf Baechle26009902006-04-05 09:45:45 +0100545MODULE_AUTHOR("Elizabeth Oldham, MIPS Technologies, Inc.");
Ralf Baechlee01402b2005-07-14 15:57:16 +0000546MODULE_LICENSE("GPL");