blob: 0233798f71552c4c64b85e9a61e45f3aafc9fe6c [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 Baechle26009902006-04-05 09:45:45 +010043#include <asm/system.h>
44#include <asm/vpe.h>
Ralf Baechlee01402b2005-07-14 15:57:16 +000045#include <asm/rtlx.h>
46
Ralf Baechleafc48412005-10-31 00:30:39 +000047static struct rtlx_info *rtlx;
Ralf Baechlee01402b2005-07-14 15:57:16 +000048static int major;
49static char module_name[] = "rtlx";
Ralf Baechlee01402b2005-07-14 15:57:16 +000050
51static struct chan_waitqueues {
52 wait_queue_head_t rt_queue;
53 wait_queue_head_t lx_queue;
Ralf Baechlec4c40182007-02-23 13:40:45 +000054 atomic_t in_open;
Ralf Baechlebc4809e2007-03-15 17:13:47 +000055 struct mutex mutex;
Ralf Baechlee01402b2005-07-14 15:57:16 +000056} channel_wqs[RTLX_CHANNELS];
57
Ralf Baechle26009902006-04-05 09:45:45 +010058static struct vpe_notifications notify;
59static int sp_stopping = 0;
60
Ralf Baechlee01402b2005-07-14 15:57:16 +000061extern void *vpe_get_shared(int index);
62
Ralf Baechle937a8012006-10-07 19:44:33 +010063static void rtlx_dispatch(void)
Ralf Baechlee01402b2005-07-14 15:57:16 +000064{
Atsushi Nemoto97dcb822007-01-08 02:14:29 +090065 do_IRQ(MIPS_CPU_IRQ_BASE + MIPS_CPU_RTLX_IRQ);
Ralf Baechlee01402b2005-07-14 15:57:16 +000066}
67
Ralf Baechle26009902006-04-05 09:45:45 +010068
69/* Interrupt handler may be called before rtlx_init has otherwise had
70 a chance to run.
71*/
Ralf Baechle937a8012006-10-07 19:44:33 +010072static irqreturn_t rtlx_interrupt(int irq, void *dev_id)
Ralf Baechlee01402b2005-07-14 15:57:16 +000073{
Ralf Baechlee01402b2005-07-14 15:57:16 +000074 int i;
75
76 for (i = 0; i < RTLX_CHANNELS; i++) {
Ralf Baechle26009902006-04-05 09:45:45 +010077 wake_up(&channel_wqs[i].lx_queue);
78 wake_up(&channel_wqs[i].rt_queue);
Ralf Baechlee01402b2005-07-14 15:57:16 +000079 }
80
Ralf Baechleafc48412005-10-31 00:30:39 +000081 return IRQ_HANDLED;
Ralf Baechlee01402b2005-07-14 15:57:16 +000082}
83
David Rientjesf5dbeaf2007-07-22 01:01:39 -070084static void __used dump_rtlx(void)
Ralf Baechlee01402b2005-07-14 15:57:16 +000085{
86 int i;
87
Ralf Baechle26009902006-04-05 09:45:45 +010088 printk("id 0x%lx state %d\n", rtlx->id, rtlx->state);
89
90 for (i = 0; i < RTLX_CHANNELS; i++) {
91 struct rtlx_channel *chan = &rtlx->channel[i];
92
93 printk(" rt_state %d lx_state %d buffer_size %d\n",
94 chan->rt_state, chan->lx_state, chan->buffer_size);
95
96 printk(" rt_read %d rt_write %d\n",
97 chan->rt_read, chan->rt_write);
98
99 printk(" lx_read %d lx_write %d\n",
100 chan->lx_read, chan->lx_write);
101
102 printk(" rt_buffer <%s>\n", chan->rt_buffer);
103 printk(" lx_buffer <%s>\n", chan->lx_buffer);
104 }
105}
106
107/* call when we have the address of the shared structure from the SP side. */
108static int rtlx_init(struct rtlx_info *rtlxi)
109{
Ralf Baechlee01402b2005-07-14 15:57:16 +0000110 if (rtlxi->id != RTLX_ID) {
Ralf Baechlee606c102007-09-12 16:31:12 +0100111 printk(KERN_ERR "no valid RTLX id at 0x%p 0x%lx\n", rtlxi, rtlxi->id);
Ralf Baechleafc48412005-10-31 00:30:39 +0000112 return -ENOEXEC;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000113 }
114
Ralf Baechlee01402b2005-07-14 15:57:16 +0000115 rtlx = rtlxi;
Ralf Baechleafc48412005-10-31 00:30:39 +0000116
117 return 0;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000118}
119
Ralf Baechle26009902006-04-05 09:45:45 +0100120/* notifications */
121static void starting(int vpe)
Ralf Baechlee01402b2005-07-14 15:57:16 +0000122{
Ralf Baechle26009902006-04-05 09:45:45 +0100123 int i;
124 sp_stopping = 0;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000125
Ralf Baechle26009902006-04-05 09:45:45 +0100126 /* force a reload of rtlx */
127 rtlx=NULL;
128
129 /* wake up any sleeping rtlx_open's */
130 for (i = 0; i < RTLX_CHANNELS; i++)
131 wake_up_interruptible(&channel_wqs[i].lx_queue);
132}
133
134static void stopping(int vpe)
135{
136 int i;
137
138 sp_stopping = 1;
139 for (i = 0; i < RTLX_CHANNELS; i++)
140 wake_up_interruptible(&channel_wqs[i].lx_queue);
141}
142
143
144int rtlx_open(int index, int can_sleep)
145{
Ralf Baechle9e346822007-03-15 17:08:28 +0000146 struct rtlx_info **p;
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000147 struct rtlx_channel *chan;
Ralf Baechlec4c40182007-02-23 13:40:45 +0000148 enum rtlx_state state;
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000149 int ret = 0;
Ralf Baechle26009902006-04-05 09:45:45 +0100150
151 if (index >= RTLX_CHANNELS) {
152 printk(KERN_DEBUG "rtlx_open index out of range\n");
153 return -ENOSYS;
154 }
155
Ralf Baechlec4c40182007-02-23 13:40:45 +0000156 if (atomic_inc_return(&channel_wqs[index].in_open) > 1) {
157 printk(KERN_DEBUG "rtlx_open channel %d already opened\n",
158 index);
159 ret = -EBUSY;
160 goto out_fail;
Ralf Baechle26009902006-04-05 09:45:45 +0100161 }
162
Ralf Baechlee01402b2005-07-14 15:57:16 +0000163 if (rtlx == NULL) {
Ralf Baechle07cc0c92007-07-27 19:31:10 +0100164 if( (p = vpe_get_shared(tclimit)) == NULL) {
Ralf Baechle26009902006-04-05 09:45:45 +0100165 if (can_sleep) {
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000166 __wait_event_interruptible(channel_wqs[index].lx_queue,
Ralf Baechle07cc0c92007-07-27 19:31:10 +0100167 (p = vpe_get_shared(tclimit)),
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000168 ret);
169 if (ret)
170 goto out_fail;
Ralf Baechle26009902006-04-05 09:45:45 +0100171 } else {
Ralf Baechlec4c40182007-02-23 13:40:45 +0000172 printk(KERN_DEBUG "No SP program loaded, and device "
Ralf Baechle26009902006-04-05 09:45:45 +0100173 "opened with O_NONBLOCK\n");
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000174 ret = -ENOSYS;
175 goto out_fail;
Ralf Baechle26009902006-04-05 09:45:45 +0100176 }
Ralf Baechlee01402b2005-07-14 15:57:16 +0000177 }
178
Ralf Baechle9e346822007-03-15 17:08:28 +0000179 smp_rmb();
Ralf Baechlee01402b2005-07-14 15:57:16 +0000180 if (*p == NULL) {
Ralf Baechle26009902006-04-05 09:45:45 +0100181 if (can_sleep) {
Ralf Baechle9e346822007-03-15 17:08:28 +0000182 DEFINE_WAIT(wait);
183
184 for (;;) {
185 prepare_to_wait(&channel_wqs[index].lx_queue, &wait, TASK_INTERRUPTIBLE);
186 smp_rmb();
187 if (*p != NULL)
188 break;
189 if (!signal_pending(current)) {
190 schedule();
191 continue;
192 }
193 ret = -ERESTARTSYS;
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000194 goto out_fail;
Ralf Baechle9e346822007-03-15 17:08:28 +0000195 }
196 finish_wait(&channel_wqs[index].lx_queue, &wait);
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000197 } else {
Ralf Baechle26009902006-04-05 09:45:45 +0100198 printk(" *vpe_get_shared is NULL. "
199 "Has an SP program been loaded?\n");
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000200 ret = -ENOSYS;
201 goto out_fail;
Ralf Baechle26009902006-04-05 09:45:45 +0100202 }
Ralf Baechlee01402b2005-07-14 15:57:16 +0000203 }
204
Ralf Baechle26009902006-04-05 09:45:45 +0100205 if ((unsigned int)*p < KSEG0) {
206 printk(KERN_WARNING "vpe_get_shared returned an invalid pointer "
207 "maybe an error code %d\n", (int)*p);
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000208 ret = -ENOSYS;
209 goto out_fail;
Ralf Baechle26009902006-04-05 09:45:45 +0100210 }
211
Ralf Baechlec4c40182007-02-23 13:40:45 +0000212 if ((ret = rtlx_init(*p)) < 0)
213 goto out_ret;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000214 }
215
Ralf Baechle26009902006-04-05 09:45:45 +0100216 chan = &rtlx->channel[index];
Ralf Baechlee01402b2005-07-14 15:57:16 +0000217
Ralf Baechlec4c40182007-02-23 13:40:45 +0000218 state = xchg(&chan->lx_state, RTLX_STATE_OPENED);
219 if (state == RTLX_STATE_OPENED) {
220 ret = -EBUSY;
221 goto out_fail;
222 }
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000223
224out_fail:
Ralf Baechlec4c40182007-02-23 13:40:45 +0000225 smp_mb();
226 atomic_dec(&channel_wqs[index].in_open);
227 smp_mb();
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000228
Ralf Baechlec4c40182007-02-23 13:40:45 +0000229out_ret:
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000230 return ret;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000231}
232
Ralf Baechle26009902006-04-05 09:45:45 +0100233int rtlx_release(int index)
Ralf Baechlee01402b2005-07-14 15:57:16 +0000234{
Ralf Baechle26009902006-04-05 09:45:45 +0100235 rtlx->channel[index].lx_state = RTLX_STATE_UNUSED;
Ralf Baechleafc48412005-10-31 00:30:39 +0000236 return 0;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000237}
238
Ralf Baechle26009902006-04-05 09:45:45 +0100239unsigned int rtlx_read_poll(int index, int can_sleep)
Ralf Baechlee01402b2005-07-14 15:57:16 +0000240{
Ralf Baechle26009902006-04-05 09:45:45 +0100241 struct rtlx_channel *chan;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000242
Ralf Baechle26009902006-04-05 09:45:45 +0100243 if (rtlx == NULL)
244 return 0;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000245
Ralf Baechle26009902006-04-05 09:45:45 +0100246 chan = &rtlx->channel[index];
Ralf Baechlee01402b2005-07-14 15:57:16 +0000247
248 /* data available to read? */
Ralf Baechle26009902006-04-05 09:45:45 +0100249 if (chan->lx_read == chan->lx_write) {
250 if (can_sleep) {
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000251 int ret = 0;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000252
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000253 __wait_event_interruptible(channel_wqs[index].lx_queue,
254 chan->lx_read != chan->lx_write || sp_stopping,
255 ret);
256 if (ret)
257 return ret;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000258
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000259 if (sp_stopping)
260 return 0;
261 } else
Ralf Baechle26009902006-04-05 09:45:45 +0100262 return 0;
263 }
264
265 return (chan->lx_write + chan->buffer_size - chan->lx_read)
266 % chan->buffer_size;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000267}
268
Ralf Baechle26009902006-04-05 09:45:45 +0100269static inline int write_spacefree(int read, int write, int size)
Ralf Baechlee01402b2005-07-14 15:57:16 +0000270{
Ralf Baechle26009902006-04-05 09:45:45 +0100271 if (read == write) {
272 /*
273 * Never fill the buffer completely, so indexes are always
274 * equal if empty and only empty, or !equal if data available
275 */
276 return size - 1;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000277 }
278
Ralf Baechle26009902006-04-05 09:45:45 +0100279 return ((read + size - write) % size) - 1;
280}
281
282unsigned int rtlx_write_poll(int index)
283{
284 struct rtlx_channel *chan = &rtlx->channel[index];
285 return write_spacefree(chan->rt_read, chan->rt_write, chan->buffer_size);
286}
287
Ralf Baechle7f5a7712007-04-25 15:08:57 +0100288ssize_t rtlx_read(int index, void __user *buff, size_t count)
Ralf Baechle26009902006-04-05 09:45:45 +0100289{
Ralf Baechle61dcc6f2007-03-15 17:10:16 +0000290 size_t lx_write, fl = 0L;
Ralf Baechle26009902006-04-05 09:45:45 +0100291 struct rtlx_channel *lx;
Ralf Baechle46230aa2007-03-16 12:16:27 +0000292 unsigned long failed;
Ralf Baechle26009902006-04-05 09:45:45 +0100293
294 if (rtlx == NULL)
295 return -ENOSYS;
296
297 lx = &rtlx->channel[index];
298
Ralf Baechlebc4809e2007-03-15 17:13:47 +0000299 mutex_lock(&channel_wqs[index].mutex);
Ralf Baechle61dcc6f2007-03-15 17:10:16 +0000300 smp_rmb();
301 lx_write = lx->lx_write;
302
Ralf Baechlee01402b2005-07-14 15:57:16 +0000303 /* find out how much in total */
Ralf Baechleafc48412005-10-31 00:30:39 +0000304 count = min(count,
Ralf Baechle61dcc6f2007-03-15 17:10:16 +0000305 (size_t)(lx_write + lx->buffer_size - lx->lx_read)
Ralf Baechle26009902006-04-05 09:45:45 +0100306 % lx->buffer_size);
Ralf Baechlee01402b2005-07-14 15:57:16 +0000307
308 /* then how much from the read pointer onwards */
Ralf Baechle61dcc6f2007-03-15 17:10:16 +0000309 fl = min(count, (size_t)lx->buffer_size - lx->lx_read);
Ralf Baechlee01402b2005-07-14 15:57:16 +0000310
Ralf Baechle46230aa2007-03-16 12:16:27 +0000311 failed = copy_to_user(buff, lx->lx_buffer + lx->lx_read, fl);
312 if (failed)
313 goto out;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000314
315 /* and if there is anything left at the beginning of the buffer */
Ralf Baechle61dcc6f2007-03-15 17:10:16 +0000316 if (count - fl)
Ralf Baechle46230aa2007-03-16 12:16:27 +0000317 failed = copy_to_user(buff + fl, lx->lx_buffer, count - fl);
318
319out:
320 count -= failed;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000321
Ralf Baechle61dcc6f2007-03-15 17:10:16 +0000322 smp_wmb();
323 lx->lx_read = (lx->lx_read + count) % lx->buffer_size;
324 smp_wmb();
Ralf Baechlebc4809e2007-03-15 17:13:47 +0000325 mutex_unlock(&channel_wqs[index].mutex);
Ralf Baechlee01402b2005-07-14 15:57:16 +0000326
Ralf Baechleafc48412005-10-31 00:30:39 +0000327 return count;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000328}
329
Ralf Baechle7f5a7712007-04-25 15:08:57 +0100330ssize_t rtlx_write(int index, const void __user *buffer, size_t count)
Ralf Baechlee01402b2005-07-14 15:57:16 +0000331{
Ralf Baechlee01402b2005-07-14 15:57:16 +0000332 struct rtlx_channel *rt;
Ralf Baechle7f5a7712007-04-25 15:08:57 +0100333 unsigned long failed;
Ralf Baechle61dcc6f2007-03-15 17:10:16 +0000334 size_t rt_read;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000335 size_t fl;
Ralf Baechle26009902006-04-05 09:45:45 +0100336
337 if (rtlx == NULL)
338 return(-ENOSYS);
339
340 rt = &rtlx->channel[index];
341
Ralf Baechlebc4809e2007-03-15 17:13:47 +0000342 mutex_lock(&channel_wqs[index].mutex);
Ralf Baechle61dcc6f2007-03-15 17:10:16 +0000343 smp_rmb();
344 rt_read = rt->rt_read;
345
Ralf Baechle26009902006-04-05 09:45:45 +0100346 /* total number of bytes to copy */
347 count = min(count,
Ralf Baechle61dcc6f2007-03-15 17:10:16 +0000348 (size_t)write_spacefree(rt_read, rt->rt_write, rt->buffer_size));
Ralf Baechle26009902006-04-05 09:45:45 +0100349
350 /* first bit from write pointer to the end of the buffer, or count */
351 fl = min(count, (size_t) rt->buffer_size - rt->rt_write);
352
Ralf Baechle46230aa2007-03-16 12:16:27 +0000353 failed = copy_from_user(rt->rt_buffer + rt->rt_write, buffer, fl);
354 if (failed)
355 goto out;
Ralf Baechle26009902006-04-05 09:45:45 +0100356
357 /* if there's any left copy to the beginning of the buffer */
Ralf Baechle46230aa2007-03-16 12:16:27 +0000358 if (count - fl) {
359 failed = copy_from_user(rt->rt_buffer, buffer + fl, count - fl);
360 }
361
362out:
Ralf Baechle7f5a7712007-04-25 15:08:57 +0100363 count -= failed;
Ralf Baechle26009902006-04-05 09:45:45 +0100364
Ralf Baechle61dcc6f2007-03-15 17:10:16 +0000365 smp_wmb();
366 rt->rt_write = (rt->rt_write + count) % rt->buffer_size;
367 smp_wmb();
Ralf Baechlebc4809e2007-03-15 17:13:47 +0000368 mutex_unlock(&channel_wqs[index].mutex);
Ralf Baechle26009902006-04-05 09:45:45 +0100369
Ralf Baechle61dcc6f2007-03-15 17:10:16 +0000370 return count;
Ralf Baechle26009902006-04-05 09:45:45 +0100371}
372
373
374static int file_open(struct inode *inode, struct file *filp)
375{
Ralf Baechle79e55bc2006-04-24 17:15:10 +0100376 int minor = iminor(inode);
Ralf Baechle26009902006-04-05 09:45:45 +0100377
378 return rtlx_open(minor, (filp->f_flags & O_NONBLOCK) ? 0 : 1);
379}
380
381static int file_release(struct inode *inode, struct file *filp)
382{
Ralf Baechle79e55bc2006-04-24 17:15:10 +0100383 int minor = iminor(inode);
Ralf Baechle26009902006-04-05 09:45:45 +0100384
385 return rtlx_release(minor);
386}
387
388static unsigned int file_poll(struct file *file, poll_table * wait)
389{
390 int minor;
391 unsigned int mask = 0;
392
Josef Sipek1b04fe92006-12-08 02:37:20 -0800393 minor = iminor(file->f_path.dentry->d_inode);
Ralf Baechle26009902006-04-05 09:45:45 +0100394
395 poll_wait(file, &channel_wqs[minor].rt_queue, wait);
396 poll_wait(file, &channel_wqs[minor].lx_queue, wait);
397
398 if (rtlx == NULL)
399 return 0;
400
401 /* data available to read? */
402 if (rtlx_read_poll(minor, 0))
403 mask |= POLLIN | POLLRDNORM;
404
405 /* space to write */
406 if (rtlx_write_poll(minor))
407 mask |= POLLOUT | POLLWRNORM;
408
409 return mask;
410}
411
412static ssize_t file_read(struct file *file, char __user * buffer, size_t count,
413 loff_t * ppos)
414{
Josef Sipek1b04fe92006-12-08 02:37:20 -0800415 int minor = iminor(file->f_path.dentry->d_inode);
Ralf Baechle26009902006-04-05 09:45:45 +0100416
417 /* data available? */
418 if (!rtlx_read_poll(minor, (file->f_flags & O_NONBLOCK) ? 0 : 1)) {
419 return 0; // -EAGAIN makes cat whinge
420 }
421
Ralf Baechle46230aa2007-03-16 12:16:27 +0000422 return rtlx_read(minor, buffer, count);
Ralf Baechle26009902006-04-05 09:45:45 +0100423}
424
425static ssize_t file_write(struct file *file, const char __user * buffer,
426 size_t count, loff_t * ppos)
427{
428 int minor;
429 struct rtlx_channel *rt;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000430
Josef Sipek1b04fe92006-12-08 02:37:20 -0800431 minor = iminor(file->f_path.dentry->d_inode);
Ralf Baechlee01402b2005-07-14 15:57:16 +0000432 rt = &rtlx->channel[minor];
433
434 /* any space left... */
Ralf Baechle26009902006-04-05 09:45:45 +0100435 if (!rtlx_write_poll(minor)) {
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000436 int ret = 0;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000437
438 if (file->f_flags & O_NONBLOCK)
Ralf Baechleafc48412005-10-31 00:30:39 +0000439 return -EAGAIN;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000440
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000441 __wait_event_interruptible(channel_wqs[minor].rt_queue,
442 rtlx_write_poll(minor),
443 ret);
444 if (ret)
445 return ret;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000446 }
447
Ralf Baechle46230aa2007-03-16 12:16:27 +0000448 return rtlx_write(minor, buffer, count);
Ralf Baechlee01402b2005-07-14 15:57:16 +0000449}
450
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800451static const struct file_operations rtlx_fops = {
Ralf Baechle26009902006-04-05 09:45:45 +0100452 .owner = THIS_MODULE,
453 .open = file_open,
454 .release = file_release,
455 .write = file_write,
456 .read = file_read,
457 .poll = file_poll
Ralf Baechlee01402b2005-07-14 15:57:16 +0000458};
459
Ralf Baechle26009902006-04-05 09:45:45 +0100460static struct irqaction rtlx_irq = {
461 .handler = rtlx_interrupt,
Thomas Gleixnerf40298f2006-07-01 19:29:20 -0700462 .flags = IRQF_DISABLED,
Ralf Baechle26009902006-04-05 09:45:45 +0100463 .name = "RTLX",
464};
465
Atsushi Nemoto97dcb822007-01-08 02:14:29 +0900466static int rtlx_irq_num = MIPS_CPU_IRQ_BASE + MIPS_CPU_RTLX_IRQ;
Ralf Baechle26009902006-04-05 09:45:45 +0100467
Ralf Baechleafc48412005-10-31 00:30:39 +0000468static char register_chrdev_failed[] __initdata =
469 KERN_ERR "rtlx_module_init: unable to register device\n";
470
Ralf Baechle9d5a3f52007-07-28 00:51:45 +0100471static int __init rtlx_module_init(void)
Ralf Baechlee01402b2005-07-14 15:57:16 +0000472{
Ralf Baechlebb3d7c72007-02-07 15:36:56 +0000473 struct device *dev;
474 int i, err;
Ralf Baechle26009902006-04-05 09:45:45 +0100475
Ralf Baechle07cc0c92007-07-27 19:31:10 +0100476 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
Ralf Baechleafc48412005-10-31 00:30:39 +0000489 major = register_chrdev(0, module_name, &rtlx_fops);
490 if (major < 0) {
491 printk(register_chrdev_failed);
492 return major;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000493 }
494
Ralf Baechle26009902006-04-05 09:45:45 +0100495 /* 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);
Ralf Baechlec4c40182007-02-23 13:40:45 +0000499 atomic_set(&channel_wqs[i].in_open, 0);
Ralf Baechlebc4809e2007-03-15 17:13:47 +0000500 mutex_init(&channel_wqs[i].mutex);
Ralf Baechlebb3d7c72007-02-07 15:36:56 +0000501
502 dev = device_create(mt_class, NULL, MKDEV(major, i),
503 "%s%d", module_name, i);
504 if (IS_ERR(dev)) {
505 err = PTR_ERR(dev);
506 goto out_chrdev;
507 }
Ralf Baechle26009902006-04-05 09:45:45 +0100508 }
509
510 /* set up notifiers */
511 notify.start = starting;
512 notify.stop = stopping;
Ralf Baechle07cc0c92007-07-27 19:31:10 +0100513 vpe_notify(tclimit, &notify);
Ralf Baechle26009902006-04-05 09:45:45 +0100514
515 if (cpu_has_vint)
516 set_vi_handler(MIPS_CPU_RTLX_IRQ, rtlx_dispatch);
517
518 rtlx_irq.dev_id = rtlx;
519 setup_irq(rtlx_irq_num, &rtlx_irq);
520
Ralf Baechleafc48412005-10-31 00:30:39 +0000521 return 0;
Ralf Baechlebb3d7c72007-02-07 15:36:56 +0000522
523out_chrdev:
524 for (i = 0; i < RTLX_CHANNELS; i++)
525 device_destroy(mt_class, MKDEV(major, i));
526
527 return err;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000528}
529
Ralf Baechleafc48412005-10-31 00:30:39 +0000530static void __exit rtlx_module_exit(void)
Ralf Baechlee01402b2005-07-14 15:57:16 +0000531{
Ralf Baechlebb3d7c72007-02-07 15:36:56 +0000532 int i;
533
534 for (i = 0; i < RTLX_CHANNELS; i++)
535 device_destroy(mt_class, MKDEV(major, i));
536
Ralf Baechlee01402b2005-07-14 15:57:16 +0000537 unregister_chrdev(major, module_name);
538}
539
540module_init(rtlx_module_init);
541module_exit(rtlx_module_exit);
Ralf Baechleafc48412005-10-31 00:30:39 +0000542
Ralf Baechlee01402b2005-07-14 15:57:16 +0000543MODULE_DESCRIPTION("MIPS RTLX");
Ralf Baechle26009902006-04-05 09:45:45 +0100544MODULE_AUTHOR("Elizabeth Oldham, MIPS Technologies, Inc.");
Ralf Baechlee01402b2005-07-14 15:57:16 +0000545MODULE_LICENSE("GPL");