blob: b88f1c18ff4dc2fe7b5877972ec39314e0d5639d [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;
Kevin D. Kissell1928cc82008-04-16 15:32:22 +020075 unsigned int flags, vpeflags;
76
77 /* Ought not to be strictly necessary for SMTC builds */
78 local_irq_save(flags);
79 vpeflags = dvpe();
80 set_c0_status(0x100 << MIPS_CPU_RTLX_IRQ);
81 irq_enable_hazard();
82 evpe(vpeflags);
83 local_irq_restore(flags);
Ralf Baechlee01402b2005-07-14 15:57:16 +000084
85 for (i = 0; i < RTLX_CHANNELS; i++) {
Ralf Baechle26009902006-04-05 09:45:45 +010086 wake_up(&channel_wqs[i].lx_queue);
87 wake_up(&channel_wqs[i].rt_queue);
Ralf Baechlee01402b2005-07-14 15:57:16 +000088 }
89
Ralf Baechleafc48412005-10-31 00:30:39 +000090 return IRQ_HANDLED;
Ralf Baechlee01402b2005-07-14 15:57:16 +000091}
92
David Rientjesf5dbeaf2007-07-22 01:01:39 -070093static void __used dump_rtlx(void)
Ralf Baechlee01402b2005-07-14 15:57:16 +000094{
95 int i;
96
Ralf Baechle26009902006-04-05 09:45:45 +010097 printk("id 0x%lx state %d\n", rtlx->id, rtlx->state);
98
99 for (i = 0; i < RTLX_CHANNELS; i++) {
100 struct rtlx_channel *chan = &rtlx->channel[i];
101
102 printk(" rt_state %d lx_state %d buffer_size %d\n",
103 chan->rt_state, chan->lx_state, chan->buffer_size);
104
105 printk(" rt_read %d rt_write %d\n",
106 chan->rt_read, chan->rt_write);
107
108 printk(" lx_read %d lx_write %d\n",
109 chan->lx_read, chan->lx_write);
110
111 printk(" rt_buffer <%s>\n", chan->rt_buffer);
112 printk(" lx_buffer <%s>\n", chan->lx_buffer);
113 }
114}
115
116/* call when we have the address of the shared structure from the SP side. */
117static int rtlx_init(struct rtlx_info *rtlxi)
118{
Ralf Baechlee01402b2005-07-14 15:57:16 +0000119 if (rtlxi->id != RTLX_ID) {
Kevin D. Kissell1928cc82008-04-16 15:32:22 +0200120 printk(KERN_ERR "no valid RTLX id at 0x%p 0x%lx\n",
121 rtlxi, rtlxi->id);
Ralf Baechleafc48412005-10-31 00:30:39 +0000122 return -ENOEXEC;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000123 }
124
Ralf Baechlee01402b2005-07-14 15:57:16 +0000125 rtlx = rtlxi;
Ralf Baechleafc48412005-10-31 00:30:39 +0000126
127 return 0;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000128}
129
Ralf Baechle26009902006-04-05 09:45:45 +0100130/* notifications */
131static void starting(int vpe)
Ralf Baechlee01402b2005-07-14 15:57:16 +0000132{
Ralf Baechle26009902006-04-05 09:45:45 +0100133 int i;
134 sp_stopping = 0;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000135
Ralf Baechle26009902006-04-05 09:45:45 +0100136 /* force a reload of rtlx */
137 rtlx=NULL;
138
139 /* wake up any sleeping rtlx_open's */
140 for (i = 0; i < RTLX_CHANNELS; i++)
141 wake_up_interruptible(&channel_wqs[i].lx_queue);
142}
143
144static void stopping(int vpe)
145{
146 int i;
147
148 sp_stopping = 1;
149 for (i = 0; i < RTLX_CHANNELS; i++)
150 wake_up_interruptible(&channel_wqs[i].lx_queue);
151}
152
153
154int rtlx_open(int index, int can_sleep)
155{
Ralf Baechle9e346822007-03-15 17:08:28 +0000156 struct rtlx_info **p;
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000157 struct rtlx_channel *chan;
Ralf Baechlec4c40182007-02-23 13:40:45 +0000158 enum rtlx_state state;
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000159 int ret = 0;
Ralf Baechle26009902006-04-05 09:45:45 +0100160
161 if (index >= RTLX_CHANNELS) {
162 printk(KERN_DEBUG "rtlx_open index out of range\n");
163 return -ENOSYS;
164 }
165
Ralf Baechlec4c40182007-02-23 13:40:45 +0000166 if (atomic_inc_return(&channel_wqs[index].in_open) > 1) {
167 printk(KERN_DEBUG "rtlx_open channel %d already opened\n",
168 index);
169 ret = -EBUSY;
170 goto out_fail;
Ralf Baechle26009902006-04-05 09:45:45 +0100171 }
172
Ralf Baechlee01402b2005-07-14 15:57:16 +0000173 if (rtlx == NULL) {
Ralf Baechle07cc0c92007-07-27 19:31:10 +0100174 if( (p = vpe_get_shared(tclimit)) == NULL) {
Kevin D. Kissell1928cc82008-04-16 15:32:22 +0200175 if (can_sleep) {
176 __wait_event_interruptible(channel_wqs[index].lx_queue,
177 (p = vpe_get_shared(tclimit)), ret);
178 if (ret)
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000179 goto out_fail;
Kevin D. Kissell1928cc82008-04-16 15:32:22 +0200180 } else {
181 printk(KERN_DEBUG "No SP program loaded, and device "
182 "opened with O_NONBLOCK\n");
183 ret = -ENOSYS;
184 goto out_fail;
185 }
Ralf Baechlee01402b2005-07-14 15:57:16 +0000186 }
187
Ralf Baechle9e346822007-03-15 17:08:28 +0000188 smp_rmb();
Ralf Baechlee01402b2005-07-14 15:57:16 +0000189 if (*p == NULL) {
Ralf Baechle26009902006-04-05 09:45:45 +0100190 if (can_sleep) {
Ralf Baechle9e346822007-03-15 17:08:28 +0000191 DEFINE_WAIT(wait);
192
193 for (;;) {
Kevin D. Kissell1928cc82008-04-16 15:32:22 +0200194 prepare_to_wait(
195 &channel_wqs[index].lx_queue,
196 &wait, TASK_INTERRUPTIBLE);
Ralf Baechle9e346822007-03-15 17:08:28 +0000197 smp_rmb();
198 if (*p != NULL)
199 break;
200 if (!signal_pending(current)) {
201 schedule();
202 continue;
203 }
204 ret = -ERESTARTSYS;
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000205 goto out_fail;
Ralf Baechle9e346822007-03-15 17:08:28 +0000206 }
207 finish_wait(&channel_wqs[index].lx_queue, &wait);
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000208 } else {
Kevin D. Kissell1928cc82008-04-16 15:32:22 +0200209 pr_err(" *vpe_get_shared is NULL. "
Ralf Baechle26009902006-04-05 09:45:45 +0100210 "Has an SP program been loaded?\n");
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000211 ret = -ENOSYS;
212 goto out_fail;
Ralf Baechle26009902006-04-05 09:45:45 +0100213 }
Ralf Baechlee01402b2005-07-14 15:57:16 +0000214 }
215
Ralf Baechle26009902006-04-05 09:45:45 +0100216 if ((unsigned int)*p < KSEG0) {
Kevin D. Kissell1928cc82008-04-16 15:32:22 +0200217 printk(KERN_WARNING "vpe_get_shared returned an "
218 "invalid pointer maybe an error code %d\n",
219 (int)*p);
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000220 ret = -ENOSYS;
221 goto out_fail;
Ralf Baechle26009902006-04-05 09:45:45 +0100222 }
223
Ralf Baechlec4c40182007-02-23 13:40:45 +0000224 if ((ret = rtlx_init(*p)) < 0)
225 goto out_ret;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000226 }
227
Ralf Baechle26009902006-04-05 09:45:45 +0100228 chan = &rtlx->channel[index];
Ralf Baechlee01402b2005-07-14 15:57:16 +0000229
Ralf Baechlec4c40182007-02-23 13:40:45 +0000230 state = xchg(&chan->lx_state, RTLX_STATE_OPENED);
231 if (state == RTLX_STATE_OPENED) {
232 ret = -EBUSY;
233 goto out_fail;
234 }
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000235
236out_fail:
Ralf Baechlec4c40182007-02-23 13:40:45 +0000237 smp_mb();
238 atomic_dec(&channel_wqs[index].in_open);
239 smp_mb();
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000240
Ralf Baechlec4c40182007-02-23 13:40:45 +0000241out_ret:
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000242 return ret;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000243}
244
Ralf Baechle26009902006-04-05 09:45:45 +0100245int rtlx_release(int index)
Ralf Baechlee01402b2005-07-14 15:57:16 +0000246{
Kevin D. Kissell1928cc82008-04-16 15:32:22 +0200247 if (rtlx == NULL) {
248 pr_err("rtlx_release() with null rtlx\n");
249 return 0;
250 }
Ralf Baechle26009902006-04-05 09:45:45 +0100251 rtlx->channel[index].lx_state = RTLX_STATE_UNUSED;
Ralf Baechleafc48412005-10-31 00:30:39 +0000252 return 0;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000253}
254
Ralf Baechle26009902006-04-05 09:45:45 +0100255unsigned int rtlx_read_poll(int index, int can_sleep)
Ralf Baechlee01402b2005-07-14 15:57:16 +0000256{
Ralf Baechle26009902006-04-05 09:45:45 +0100257 struct rtlx_channel *chan;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000258
Ralf Baechle26009902006-04-05 09:45:45 +0100259 if (rtlx == NULL)
260 return 0;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000261
Ralf Baechle26009902006-04-05 09:45:45 +0100262 chan = &rtlx->channel[index];
Ralf Baechlee01402b2005-07-14 15:57:16 +0000263
264 /* data available to read? */
Ralf Baechle26009902006-04-05 09:45:45 +0100265 if (chan->lx_read == chan->lx_write) {
266 if (can_sleep) {
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000267 int ret = 0;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000268
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000269 __wait_event_interruptible(channel_wqs[index].lx_queue,
Kevin D. Kissell1928cc82008-04-16 15:32:22 +0200270 (chan->lx_read != chan->lx_write) ||
271 sp_stopping, ret);
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000272 if (ret)
273 return ret;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000274
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000275 if (sp_stopping)
276 return 0;
277 } else
Ralf Baechle26009902006-04-05 09:45:45 +0100278 return 0;
279 }
280
281 return (chan->lx_write + chan->buffer_size - chan->lx_read)
282 % chan->buffer_size;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000283}
284
Ralf Baechle26009902006-04-05 09:45:45 +0100285static inline int write_spacefree(int read, int write, int size)
Ralf Baechlee01402b2005-07-14 15:57:16 +0000286{
Ralf Baechle26009902006-04-05 09:45:45 +0100287 if (read == write) {
288 /*
289 * Never fill the buffer completely, so indexes are always
290 * equal if empty and only empty, or !equal if data available
291 */
292 return size - 1;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000293 }
294
Ralf Baechle26009902006-04-05 09:45:45 +0100295 return ((read + size - write) % size) - 1;
296}
297
298unsigned int rtlx_write_poll(int index)
299{
300 struct rtlx_channel *chan = &rtlx->channel[index];
Kevin D. Kissell1928cc82008-04-16 15:32:22 +0200301
302 return write_spacefree(chan->rt_read, chan->rt_write,
303 chan->buffer_size);
Ralf Baechle26009902006-04-05 09:45:45 +0100304}
305
Ralf Baechle7f5a7712007-04-25 15:08:57 +0100306ssize_t rtlx_read(int index, void __user *buff, size_t count)
Ralf Baechle26009902006-04-05 09:45:45 +0100307{
Ralf Baechle61dcc6f2007-03-15 17:10:16 +0000308 size_t lx_write, fl = 0L;
Ralf Baechle26009902006-04-05 09:45:45 +0100309 struct rtlx_channel *lx;
Ralf Baechle46230aa2007-03-16 12:16:27 +0000310 unsigned long failed;
Ralf Baechle26009902006-04-05 09:45:45 +0100311
312 if (rtlx == NULL)
313 return -ENOSYS;
314
315 lx = &rtlx->channel[index];
316
Ralf Baechlebc4809e2007-03-15 17:13:47 +0000317 mutex_lock(&channel_wqs[index].mutex);
Ralf Baechle61dcc6f2007-03-15 17:10:16 +0000318 smp_rmb();
319 lx_write = lx->lx_write;
320
Ralf Baechlee01402b2005-07-14 15:57:16 +0000321 /* find out how much in total */
Ralf Baechleafc48412005-10-31 00:30:39 +0000322 count = min(count,
Ralf Baechle61dcc6f2007-03-15 17:10:16 +0000323 (size_t)(lx_write + lx->buffer_size - lx->lx_read)
Ralf Baechle26009902006-04-05 09:45:45 +0100324 % lx->buffer_size);
Ralf Baechlee01402b2005-07-14 15:57:16 +0000325
326 /* then how much from the read pointer onwards */
Ralf Baechle61dcc6f2007-03-15 17:10:16 +0000327 fl = min(count, (size_t)lx->buffer_size - lx->lx_read);
Ralf Baechlee01402b2005-07-14 15:57:16 +0000328
Ralf Baechle46230aa2007-03-16 12:16:27 +0000329 failed = copy_to_user(buff, lx->lx_buffer + lx->lx_read, fl);
330 if (failed)
331 goto out;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000332
333 /* and if there is anything left at the beginning of the buffer */
Ralf Baechle61dcc6f2007-03-15 17:10:16 +0000334 if (count - fl)
Ralf Baechle46230aa2007-03-16 12:16:27 +0000335 failed = copy_to_user(buff + fl, lx->lx_buffer, count - fl);
336
337out:
338 count -= failed;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000339
Ralf Baechle61dcc6f2007-03-15 17:10:16 +0000340 smp_wmb();
341 lx->lx_read = (lx->lx_read + count) % lx->buffer_size;
342 smp_wmb();
Ralf Baechlebc4809e2007-03-15 17:13:47 +0000343 mutex_unlock(&channel_wqs[index].mutex);
Ralf Baechlee01402b2005-07-14 15:57:16 +0000344
Ralf Baechleafc48412005-10-31 00:30:39 +0000345 return count;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000346}
347
Ralf Baechle7f5a7712007-04-25 15:08:57 +0100348ssize_t rtlx_write(int index, const void __user *buffer, size_t count)
Ralf Baechlee01402b2005-07-14 15:57:16 +0000349{
Ralf Baechlee01402b2005-07-14 15:57:16 +0000350 struct rtlx_channel *rt;
Ralf Baechle7f5a7712007-04-25 15:08:57 +0100351 unsigned long failed;
Ralf Baechle61dcc6f2007-03-15 17:10:16 +0000352 size_t rt_read;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000353 size_t fl;
Ralf Baechle26009902006-04-05 09:45:45 +0100354
355 if (rtlx == NULL)
356 return(-ENOSYS);
357
358 rt = &rtlx->channel[index];
359
Ralf Baechlebc4809e2007-03-15 17:13:47 +0000360 mutex_lock(&channel_wqs[index].mutex);
Ralf Baechle61dcc6f2007-03-15 17:10:16 +0000361 smp_rmb();
362 rt_read = rt->rt_read;
363
Ralf Baechle26009902006-04-05 09:45:45 +0100364 /* total number of bytes to copy */
Kevin D. Kissell1928cc82008-04-16 15:32:22 +0200365 count = min(count, (size_t)write_spacefree(rt_read, rt->rt_write,
366 rt->buffer_size));
Ralf Baechle26009902006-04-05 09:45:45 +0100367
368 /* first bit from write pointer to the end of the buffer, or count */
369 fl = min(count, (size_t) rt->buffer_size - rt->rt_write);
370
Ralf Baechle46230aa2007-03-16 12:16:27 +0000371 failed = copy_from_user(rt->rt_buffer + rt->rt_write, buffer, fl);
372 if (failed)
373 goto out;
Ralf Baechle26009902006-04-05 09:45:45 +0100374
375 /* if there's any left copy to the beginning of the buffer */
Ralf Baechle46230aa2007-03-16 12:16:27 +0000376 if (count - fl) {
377 failed = copy_from_user(rt->rt_buffer, buffer + fl, count - fl);
378 }
379
380out:
Ralf Baechle7f5a7712007-04-25 15:08:57 +0100381 count -= failed;
Ralf Baechle26009902006-04-05 09:45:45 +0100382
Ralf Baechle61dcc6f2007-03-15 17:10:16 +0000383 smp_wmb();
384 rt->rt_write = (rt->rt_write + count) % rt->buffer_size;
385 smp_wmb();
Ralf Baechlebc4809e2007-03-15 17:13:47 +0000386 mutex_unlock(&channel_wqs[index].mutex);
Ralf Baechle26009902006-04-05 09:45:45 +0100387
Ralf Baechle61dcc6f2007-03-15 17:10:16 +0000388 return count;
Ralf Baechle26009902006-04-05 09:45:45 +0100389}
390
391
392static int file_open(struct inode *inode, struct file *filp)
393{
Ralf Baechle79e55bc2006-04-24 17:15:10 +0100394 int minor = iminor(inode);
Ralf Baechle26009902006-04-05 09:45:45 +0100395
396 return rtlx_open(minor, (filp->f_flags & O_NONBLOCK) ? 0 : 1);
397}
398
399static int file_release(struct inode *inode, struct file *filp)
400{
Ralf Baechle79e55bc2006-04-24 17:15:10 +0100401 int minor = iminor(inode);
Ralf Baechle26009902006-04-05 09:45:45 +0100402
403 return rtlx_release(minor);
404}
405
406static unsigned int file_poll(struct file *file, poll_table * wait)
407{
408 int minor;
409 unsigned int mask = 0;
410
Josef Sipek1b04fe92006-12-08 02:37:20 -0800411 minor = iminor(file->f_path.dentry->d_inode);
Ralf Baechle26009902006-04-05 09:45:45 +0100412
413 poll_wait(file, &channel_wqs[minor].rt_queue, wait);
414 poll_wait(file, &channel_wqs[minor].lx_queue, wait);
415
416 if (rtlx == NULL)
417 return 0;
418
419 /* data available to read? */
420 if (rtlx_read_poll(minor, 0))
421 mask |= POLLIN | POLLRDNORM;
422
423 /* space to write */
424 if (rtlx_write_poll(minor))
425 mask |= POLLOUT | POLLWRNORM;
426
427 return mask;
428}
429
430static ssize_t file_read(struct file *file, char __user * buffer, size_t count,
431 loff_t * ppos)
432{
Josef Sipek1b04fe92006-12-08 02:37:20 -0800433 int minor = iminor(file->f_path.dentry->d_inode);
Ralf Baechle26009902006-04-05 09:45:45 +0100434
435 /* data available? */
436 if (!rtlx_read_poll(minor, (file->f_flags & O_NONBLOCK) ? 0 : 1)) {
437 return 0; // -EAGAIN makes cat whinge
438 }
439
Ralf Baechle46230aa2007-03-16 12:16:27 +0000440 return rtlx_read(minor, buffer, count);
Ralf Baechle26009902006-04-05 09:45:45 +0100441}
442
443static ssize_t file_write(struct file *file, const char __user * buffer,
444 size_t count, loff_t * ppos)
445{
446 int minor;
447 struct rtlx_channel *rt;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000448
Josef Sipek1b04fe92006-12-08 02:37:20 -0800449 minor = iminor(file->f_path.dentry->d_inode);
Ralf Baechlee01402b2005-07-14 15:57:16 +0000450 rt = &rtlx->channel[minor];
451
452 /* any space left... */
Ralf Baechle26009902006-04-05 09:45:45 +0100453 if (!rtlx_write_poll(minor)) {
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000454 int ret = 0;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000455
456 if (file->f_flags & O_NONBLOCK)
Ralf Baechleafc48412005-10-31 00:30:39 +0000457 return -EAGAIN;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000458
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000459 __wait_event_interruptible(channel_wqs[minor].rt_queue,
460 rtlx_write_poll(minor),
461 ret);
462 if (ret)
463 return ret;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000464 }
465
Ralf Baechle46230aa2007-03-16 12:16:27 +0000466 return rtlx_write(minor, buffer, count);
Ralf Baechlee01402b2005-07-14 15:57:16 +0000467}
468
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800469static const struct file_operations rtlx_fops = {
Ralf Baechle26009902006-04-05 09:45:45 +0100470 .owner = THIS_MODULE,
471 .open = file_open,
472 .release = file_release,
473 .write = file_write,
474 .read = file_read,
475 .poll = file_poll
Ralf Baechlee01402b2005-07-14 15:57:16 +0000476};
477
Ralf Baechle26009902006-04-05 09:45:45 +0100478static struct irqaction rtlx_irq = {
479 .handler = rtlx_interrupt,
Thomas Gleixnerf40298f2006-07-01 19:29:20 -0700480 .flags = IRQF_DISABLED,
Ralf Baechle26009902006-04-05 09:45:45 +0100481 .name = "RTLX",
482};
483
Atsushi Nemoto97dcb822007-01-08 02:14:29 +0900484static int rtlx_irq_num = MIPS_CPU_IRQ_BASE + MIPS_CPU_RTLX_IRQ;
Ralf Baechle26009902006-04-05 09:45:45 +0100485
Ralf Baechleafc48412005-10-31 00:30:39 +0000486static char register_chrdev_failed[] __initdata =
487 KERN_ERR "rtlx_module_init: unable to register device\n";
488
Ralf Baechle9d5a3f52007-07-28 00:51:45 +0100489static int __init rtlx_module_init(void)
Ralf Baechlee01402b2005-07-14 15:57:16 +0000490{
Ralf Baechlebb3d7c72007-02-07 15:36:56 +0000491 struct device *dev;
492 int i, err;
Ralf Baechle26009902006-04-05 09:45:45 +0100493
Ralf Baechle07cc0c92007-07-27 19:31:10 +0100494 if (!cpu_has_mipsmt) {
495 printk("VPE loader: not a MIPS MT capable processor\n");
496 return -ENODEV;
497 }
498
499 if (tclimit == 0) {
500 printk(KERN_WARNING "No TCs reserved for AP/SP, not "
501 "initializing RTLX.\nPass maxtcs=<n> argument as kernel "
502 "argument\n");
503
504 return -ENODEV;
505 }
506
Ralf Baechleafc48412005-10-31 00:30:39 +0000507 major = register_chrdev(0, module_name, &rtlx_fops);
508 if (major < 0) {
509 printk(register_chrdev_failed);
510 return major;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000511 }
512
Ralf Baechle26009902006-04-05 09:45:45 +0100513 /* initialise the wait queues */
514 for (i = 0; i < RTLX_CHANNELS; i++) {
515 init_waitqueue_head(&channel_wqs[i].rt_queue);
516 init_waitqueue_head(&channel_wqs[i].lx_queue);
Ralf Baechlec4c40182007-02-23 13:40:45 +0000517 atomic_set(&channel_wqs[i].in_open, 0);
Ralf Baechlebc4809e2007-03-15 17:13:47 +0000518 mutex_init(&channel_wqs[i].mutex);
Ralf Baechlebb3d7c72007-02-07 15:36:56 +0000519
520 dev = device_create(mt_class, NULL, MKDEV(major, i),
521 "%s%d", module_name, i);
522 if (IS_ERR(dev)) {
523 err = PTR_ERR(dev);
524 goto out_chrdev;
525 }
Ralf Baechle26009902006-04-05 09:45:45 +0100526 }
527
528 /* set up notifiers */
529 notify.start = starting;
530 notify.stop = stopping;
Ralf Baechle07cc0c92007-07-27 19:31:10 +0100531 vpe_notify(tclimit, &notify);
Ralf Baechle26009902006-04-05 09:45:45 +0100532
533 if (cpu_has_vint)
534 set_vi_handler(MIPS_CPU_RTLX_IRQ, rtlx_dispatch);
Kevin D. Kissell1928cc82008-04-16 15:32:22 +0200535 else {
536 pr_err("APRP RTLX init on non-vectored-interrupt processor\n");
537 err = -ENODEV;
538 goto out_chrdev;
539 }
Ralf Baechle26009902006-04-05 09:45:45 +0100540
541 rtlx_irq.dev_id = rtlx;
542 setup_irq(rtlx_irq_num, &rtlx_irq);
543
Ralf Baechleafc48412005-10-31 00:30:39 +0000544 return 0;
Ralf Baechlebb3d7c72007-02-07 15:36:56 +0000545
546out_chrdev:
547 for (i = 0; i < RTLX_CHANNELS; i++)
548 device_destroy(mt_class, MKDEV(major, i));
549
550 return err;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000551}
552
Ralf Baechleafc48412005-10-31 00:30:39 +0000553static void __exit rtlx_module_exit(void)
Ralf Baechlee01402b2005-07-14 15:57:16 +0000554{
Ralf Baechlebb3d7c72007-02-07 15:36:56 +0000555 int i;
556
557 for (i = 0; i < RTLX_CHANNELS; i++)
558 device_destroy(mt_class, MKDEV(major, i));
559
Ralf Baechlee01402b2005-07-14 15:57:16 +0000560 unregister_chrdev(major, module_name);
561}
562
563module_init(rtlx_module_init);
564module_exit(rtlx_module_exit);
Ralf Baechleafc48412005-10-31 00:30:39 +0000565
Ralf Baechlee01402b2005-07-14 15:57:16 +0000566MODULE_DESCRIPTION("MIPS RTLX");
Ralf Baechle26009902006-04-05 09:45:45 +0100567MODULE_AUTHOR("Elizabeth Oldham, MIPS Technologies, Inc.");
Ralf Baechlee01402b2005-07-14 15:57:16 +0000568MODULE_LICENSE("GPL");