blob: 364f066cb4979533336a37ea437d424047111402 [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>
Jonathan Corbet7558da92008-05-15 09:10:50 -060031#include <linux/smp_lock.h>
Ralf Baechle26009902006-04-05 09:45:45 +010032#include <linux/syscalls.h>
33#include <linux/moduleloader.h>
Ralf Baechlea84c96e2006-01-15 18:11:28 +000034#include <linux/interrupt.h>
Ralf Baechlee01402b2005-07-14 15:57:16 +000035#include <linux/poll.h>
36#include <linux/sched.h>
37#include <linux/wait.h>
38#include <asm/mipsmtregs.h>
Ralf Baechlebb3d7c72007-02-07 15:36:56 +000039#include <asm/mips_mt.h>
Ralf Baechle26009902006-04-05 09:45:45 +010040#include <asm/cacheflush.h>
41#include <asm/atomic.h>
Ralf Baechlee01402b2005-07-14 15:57:16 +000042#include <asm/cpu.h>
43#include <asm/processor.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;
Ralf Baechle982f6ff2009-09-17 02:25:07 +020060static int sp_stopping;
Ralf Baechle26009902006-04-05 09:45:45 +010061
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 Baechle1bbfc202009-09-29 00:52:27 +010075 unsigned int vpeflags;
76 unsigned long flags;
Ralf Baechlee01402b2005-07-14 15:57:16 +000077 int i;
Kevin D. Kissell1928cc82008-04-16 15:32:22 +020078
79 /* Ought not to be strictly necessary for SMTC builds */
80 local_irq_save(flags);
81 vpeflags = dvpe();
82 set_c0_status(0x100 << MIPS_CPU_RTLX_IRQ);
83 irq_enable_hazard();
84 evpe(vpeflags);
85 local_irq_restore(flags);
Ralf Baechlee01402b2005-07-14 15:57:16 +000086
87 for (i = 0; i < RTLX_CHANNELS; i++) {
Ralf Baechle26009902006-04-05 09:45:45 +010088 wake_up(&channel_wqs[i].lx_queue);
89 wake_up(&channel_wqs[i].rt_queue);
Ralf Baechlee01402b2005-07-14 15:57:16 +000090 }
91
Ralf Baechleafc48412005-10-31 00:30:39 +000092 return IRQ_HANDLED;
Ralf Baechlee01402b2005-07-14 15:57:16 +000093}
94
David Rientjesf5dbeaf2007-07-22 01:01:39 -070095static void __used dump_rtlx(void)
Ralf Baechlee01402b2005-07-14 15:57:16 +000096{
97 int i;
98
Ralf Baechle26009902006-04-05 09:45:45 +010099 printk("id 0x%lx state %d\n", rtlx->id, rtlx->state);
100
101 for (i = 0; i < RTLX_CHANNELS; i++) {
102 struct rtlx_channel *chan = &rtlx->channel[i];
103
104 printk(" rt_state %d lx_state %d buffer_size %d\n",
105 chan->rt_state, chan->lx_state, chan->buffer_size);
106
107 printk(" rt_read %d rt_write %d\n",
108 chan->rt_read, chan->rt_write);
109
110 printk(" lx_read %d lx_write %d\n",
111 chan->lx_read, chan->lx_write);
112
113 printk(" rt_buffer <%s>\n", chan->rt_buffer);
114 printk(" lx_buffer <%s>\n", chan->lx_buffer);
115 }
116}
117
118/* call when we have the address of the shared structure from the SP side. */
119static int rtlx_init(struct rtlx_info *rtlxi)
120{
Ralf Baechlee01402b2005-07-14 15:57:16 +0000121 if (rtlxi->id != RTLX_ID) {
Kevin D. Kissell1928cc82008-04-16 15:32:22 +0200122 printk(KERN_ERR "no valid RTLX id at 0x%p 0x%lx\n",
123 rtlxi, rtlxi->id);
Ralf Baechleafc48412005-10-31 00:30:39 +0000124 return -ENOEXEC;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000125 }
126
Ralf Baechlee01402b2005-07-14 15:57:16 +0000127 rtlx = rtlxi;
Ralf Baechleafc48412005-10-31 00:30:39 +0000128
129 return 0;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000130}
131
Ralf Baechle26009902006-04-05 09:45:45 +0100132/* notifications */
133static void starting(int vpe)
Ralf Baechlee01402b2005-07-14 15:57:16 +0000134{
Ralf Baechle26009902006-04-05 09:45:45 +0100135 int i;
136 sp_stopping = 0;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000137
Ralf Baechle26009902006-04-05 09:45:45 +0100138 /* force a reload of rtlx */
139 rtlx=NULL;
140
141 /* wake up any sleeping rtlx_open's */
142 for (i = 0; i < RTLX_CHANNELS; i++)
143 wake_up_interruptible(&channel_wqs[i].lx_queue);
144}
145
146static void stopping(int vpe)
147{
148 int i;
149
150 sp_stopping = 1;
151 for (i = 0; i < RTLX_CHANNELS; i++)
152 wake_up_interruptible(&channel_wqs[i].lx_queue);
153}
154
155
156int rtlx_open(int index, int can_sleep)
157{
Ralf Baechle9e346822007-03-15 17:08:28 +0000158 struct rtlx_info **p;
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000159 struct rtlx_channel *chan;
Ralf Baechlec4c40182007-02-23 13:40:45 +0000160 enum rtlx_state state;
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000161 int ret = 0;
Ralf Baechle26009902006-04-05 09:45:45 +0100162
163 if (index >= RTLX_CHANNELS) {
164 printk(KERN_DEBUG "rtlx_open index out of range\n");
165 return -ENOSYS;
166 }
167
Ralf Baechlec4c40182007-02-23 13:40:45 +0000168 if (atomic_inc_return(&channel_wqs[index].in_open) > 1) {
169 printk(KERN_DEBUG "rtlx_open channel %d already opened\n",
170 index);
171 ret = -EBUSY;
172 goto out_fail;
Ralf Baechle26009902006-04-05 09:45:45 +0100173 }
174
Ralf Baechlee01402b2005-07-14 15:57:16 +0000175 if (rtlx == NULL) {
Ralf Baechle07cc0c92007-07-27 19:31:10 +0100176 if( (p = vpe_get_shared(tclimit)) == NULL) {
Kevin D. Kissell1928cc82008-04-16 15:32:22 +0200177 if (can_sleep) {
178 __wait_event_interruptible(channel_wqs[index].lx_queue,
179 (p = vpe_get_shared(tclimit)), ret);
180 if (ret)
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000181 goto out_fail;
Kevin D. Kissell1928cc82008-04-16 15:32:22 +0200182 } else {
183 printk(KERN_DEBUG "No SP program loaded, and device "
184 "opened with O_NONBLOCK\n");
185 ret = -ENOSYS;
186 goto out_fail;
187 }
Ralf Baechlee01402b2005-07-14 15:57:16 +0000188 }
189
Ralf Baechle9e346822007-03-15 17:08:28 +0000190 smp_rmb();
Ralf Baechlee01402b2005-07-14 15:57:16 +0000191 if (*p == NULL) {
Ralf Baechle26009902006-04-05 09:45:45 +0100192 if (can_sleep) {
Ralf Baechle9e346822007-03-15 17:08:28 +0000193 DEFINE_WAIT(wait);
194
195 for (;;) {
Kevin D. Kissell1928cc82008-04-16 15:32:22 +0200196 prepare_to_wait(
197 &channel_wqs[index].lx_queue,
198 &wait, TASK_INTERRUPTIBLE);
Ralf Baechle9e346822007-03-15 17:08:28 +0000199 smp_rmb();
200 if (*p != NULL)
201 break;
202 if (!signal_pending(current)) {
203 schedule();
204 continue;
205 }
206 ret = -ERESTARTSYS;
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000207 goto out_fail;
Ralf Baechle9e346822007-03-15 17:08:28 +0000208 }
209 finish_wait(&channel_wqs[index].lx_queue, &wait);
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000210 } else {
Kevin D. Kissell1928cc82008-04-16 15:32:22 +0200211 pr_err(" *vpe_get_shared is NULL. "
Ralf Baechle26009902006-04-05 09:45:45 +0100212 "Has an SP program been loaded?\n");
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000213 ret = -ENOSYS;
214 goto out_fail;
Ralf Baechle26009902006-04-05 09:45:45 +0100215 }
Ralf Baechlee01402b2005-07-14 15:57:16 +0000216 }
217
Ralf Baechle26009902006-04-05 09:45:45 +0100218 if ((unsigned int)*p < KSEG0) {
Kevin D. Kissell1928cc82008-04-16 15:32:22 +0200219 printk(KERN_WARNING "vpe_get_shared returned an "
220 "invalid pointer maybe an error code %d\n",
221 (int)*p);
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000222 ret = -ENOSYS;
223 goto out_fail;
Ralf Baechle26009902006-04-05 09:45:45 +0100224 }
225
Ralf Baechlec4c40182007-02-23 13:40:45 +0000226 if ((ret = rtlx_init(*p)) < 0)
227 goto out_ret;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000228 }
229
Ralf Baechle26009902006-04-05 09:45:45 +0100230 chan = &rtlx->channel[index];
Ralf Baechlee01402b2005-07-14 15:57:16 +0000231
Ralf Baechlec4c40182007-02-23 13:40:45 +0000232 state = xchg(&chan->lx_state, RTLX_STATE_OPENED);
233 if (state == RTLX_STATE_OPENED) {
234 ret = -EBUSY;
235 goto out_fail;
236 }
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000237
238out_fail:
Ralf Baechlec4c40182007-02-23 13:40:45 +0000239 smp_mb();
240 atomic_dec(&channel_wqs[index].in_open);
241 smp_mb();
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000242
Ralf Baechlec4c40182007-02-23 13:40:45 +0000243out_ret:
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000244 return ret;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000245}
246
Ralf Baechle26009902006-04-05 09:45:45 +0100247int rtlx_release(int index)
Ralf Baechlee01402b2005-07-14 15:57:16 +0000248{
Kevin D. Kissell1928cc82008-04-16 15:32:22 +0200249 if (rtlx == NULL) {
250 pr_err("rtlx_release() with null rtlx\n");
251 return 0;
252 }
Ralf Baechle26009902006-04-05 09:45:45 +0100253 rtlx->channel[index].lx_state = RTLX_STATE_UNUSED;
Ralf Baechleafc48412005-10-31 00:30:39 +0000254 return 0;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000255}
256
Ralf Baechle26009902006-04-05 09:45:45 +0100257unsigned int rtlx_read_poll(int index, int can_sleep)
Ralf Baechlee01402b2005-07-14 15:57:16 +0000258{
Ralf Baechle26009902006-04-05 09:45:45 +0100259 struct rtlx_channel *chan;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000260
Ralf Baechle26009902006-04-05 09:45:45 +0100261 if (rtlx == NULL)
262 return 0;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000263
Ralf Baechle26009902006-04-05 09:45:45 +0100264 chan = &rtlx->channel[index];
Ralf Baechlee01402b2005-07-14 15:57:16 +0000265
266 /* data available to read? */
Ralf Baechle26009902006-04-05 09:45:45 +0100267 if (chan->lx_read == chan->lx_write) {
268 if (can_sleep) {
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000269 int ret = 0;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000270
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000271 __wait_event_interruptible(channel_wqs[index].lx_queue,
Kevin D. Kissell1928cc82008-04-16 15:32:22 +0200272 (chan->lx_read != chan->lx_write) ||
273 sp_stopping, ret);
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000274 if (ret)
275 return ret;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000276
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000277 if (sp_stopping)
278 return 0;
279 } else
Ralf Baechle26009902006-04-05 09:45:45 +0100280 return 0;
281 }
282
283 return (chan->lx_write + chan->buffer_size - chan->lx_read)
284 % chan->buffer_size;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000285}
286
Ralf Baechle26009902006-04-05 09:45:45 +0100287static inline int write_spacefree(int read, int write, int size)
Ralf Baechlee01402b2005-07-14 15:57:16 +0000288{
Ralf Baechle26009902006-04-05 09:45:45 +0100289 if (read == write) {
290 /*
291 * Never fill the buffer completely, so indexes are always
292 * equal if empty and only empty, or !equal if data available
293 */
294 return size - 1;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000295 }
296
Ralf Baechle26009902006-04-05 09:45:45 +0100297 return ((read + size - write) % size) - 1;
298}
299
300unsigned int rtlx_write_poll(int index)
301{
302 struct rtlx_channel *chan = &rtlx->channel[index];
Kevin D. Kissell1928cc82008-04-16 15:32:22 +0200303
304 return write_spacefree(chan->rt_read, chan->rt_write,
305 chan->buffer_size);
Ralf Baechle26009902006-04-05 09:45:45 +0100306}
307
Ralf Baechle7f5a7712007-04-25 15:08:57 +0100308ssize_t rtlx_read(int index, void __user *buff, size_t count)
Ralf Baechle26009902006-04-05 09:45:45 +0100309{
Ralf Baechle61dcc6f2007-03-15 17:10:16 +0000310 size_t lx_write, fl = 0L;
Ralf Baechle26009902006-04-05 09:45:45 +0100311 struct rtlx_channel *lx;
Ralf Baechle46230aa2007-03-16 12:16:27 +0000312 unsigned long failed;
Ralf Baechle26009902006-04-05 09:45:45 +0100313
314 if (rtlx == NULL)
315 return -ENOSYS;
316
317 lx = &rtlx->channel[index];
318
Ralf Baechlebc4809e2007-03-15 17:13:47 +0000319 mutex_lock(&channel_wqs[index].mutex);
Ralf Baechle61dcc6f2007-03-15 17:10:16 +0000320 smp_rmb();
321 lx_write = lx->lx_write;
322
Ralf Baechlee01402b2005-07-14 15:57:16 +0000323 /* find out how much in total */
Ralf Baechleafc48412005-10-31 00:30:39 +0000324 count = min(count,
Ralf Baechle61dcc6f2007-03-15 17:10:16 +0000325 (size_t)(lx_write + lx->buffer_size - lx->lx_read)
Ralf Baechle26009902006-04-05 09:45:45 +0100326 % lx->buffer_size);
Ralf Baechlee01402b2005-07-14 15:57:16 +0000327
328 /* then how much from the read pointer onwards */
Ralf Baechle61dcc6f2007-03-15 17:10:16 +0000329 fl = min(count, (size_t)lx->buffer_size - lx->lx_read);
Ralf Baechlee01402b2005-07-14 15:57:16 +0000330
Ralf Baechle46230aa2007-03-16 12:16:27 +0000331 failed = copy_to_user(buff, lx->lx_buffer + lx->lx_read, fl);
332 if (failed)
333 goto out;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000334
335 /* and if there is anything left at the beginning of the buffer */
Ralf Baechle61dcc6f2007-03-15 17:10:16 +0000336 if (count - fl)
Ralf Baechle46230aa2007-03-16 12:16:27 +0000337 failed = copy_to_user(buff + fl, lx->lx_buffer, count - fl);
338
339out:
340 count -= failed;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000341
Ralf Baechle61dcc6f2007-03-15 17:10:16 +0000342 smp_wmb();
343 lx->lx_read = (lx->lx_read + count) % lx->buffer_size;
344 smp_wmb();
Ralf Baechlebc4809e2007-03-15 17:13:47 +0000345 mutex_unlock(&channel_wqs[index].mutex);
Ralf Baechlee01402b2005-07-14 15:57:16 +0000346
Ralf Baechleafc48412005-10-31 00:30:39 +0000347 return count;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000348}
349
Ralf Baechle7f5a7712007-04-25 15:08:57 +0100350ssize_t rtlx_write(int index, const void __user *buffer, size_t count)
Ralf Baechlee01402b2005-07-14 15:57:16 +0000351{
Ralf Baechlee01402b2005-07-14 15:57:16 +0000352 struct rtlx_channel *rt;
Ralf Baechle7f5a7712007-04-25 15:08:57 +0100353 unsigned long failed;
Ralf Baechle61dcc6f2007-03-15 17:10:16 +0000354 size_t rt_read;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000355 size_t fl;
Ralf Baechle26009902006-04-05 09:45:45 +0100356
357 if (rtlx == NULL)
358 return(-ENOSYS);
359
360 rt = &rtlx->channel[index];
361
Ralf Baechlebc4809e2007-03-15 17:13:47 +0000362 mutex_lock(&channel_wqs[index].mutex);
Ralf Baechle61dcc6f2007-03-15 17:10:16 +0000363 smp_rmb();
364 rt_read = rt->rt_read;
365
Ralf Baechle26009902006-04-05 09:45:45 +0100366 /* total number of bytes to copy */
Kevin D. Kissell1928cc82008-04-16 15:32:22 +0200367 count = min(count, (size_t)write_spacefree(rt_read, rt->rt_write,
368 rt->buffer_size));
Ralf Baechle26009902006-04-05 09:45:45 +0100369
370 /* first bit from write pointer to the end of the buffer, or count */
371 fl = min(count, (size_t) rt->buffer_size - rt->rt_write);
372
Ralf Baechle46230aa2007-03-16 12:16:27 +0000373 failed = copy_from_user(rt->rt_buffer + rt->rt_write, buffer, fl);
374 if (failed)
375 goto out;
Ralf Baechle26009902006-04-05 09:45:45 +0100376
377 /* if there's any left copy to the beginning of the buffer */
Ralf Baechle46230aa2007-03-16 12:16:27 +0000378 if (count - fl) {
379 failed = copy_from_user(rt->rt_buffer, buffer + fl, count - fl);
380 }
381
382out:
Ralf Baechle7f5a7712007-04-25 15:08:57 +0100383 count -= failed;
Ralf Baechle26009902006-04-05 09:45:45 +0100384
Ralf Baechle61dcc6f2007-03-15 17:10:16 +0000385 smp_wmb();
386 rt->rt_write = (rt->rt_write + count) % rt->buffer_size;
387 smp_wmb();
Ralf Baechlebc4809e2007-03-15 17:13:47 +0000388 mutex_unlock(&channel_wqs[index].mutex);
Ralf Baechle26009902006-04-05 09:45:45 +0100389
Ralf Baechle61dcc6f2007-03-15 17:10:16 +0000390 return count;
Ralf Baechle26009902006-04-05 09:45:45 +0100391}
392
393
394static int file_open(struct inode *inode, struct file *filp)
395{
Ralf Baechle1bbfc202009-09-29 00:52:27 +0100396 return rtlx_open(iminor(inode), (filp->f_flags & O_NONBLOCK) ? 0 : 1);
Ralf Baechle26009902006-04-05 09:45:45 +0100397}
398
399static int file_release(struct inode *inode, struct file *filp)
400{
Ralf Baechle1bbfc202009-09-29 00:52:27 +0100401 return rtlx_release(iminor(inode));
Ralf Baechle26009902006-04-05 09:45:45 +0100402}
403
404static unsigned int file_poll(struct file *file, poll_table * wait)
405{
406 int minor;
407 unsigned int mask = 0;
408
Josef Sipek1b04fe92006-12-08 02:37:20 -0800409 minor = iminor(file->f_path.dentry->d_inode);
Ralf Baechle26009902006-04-05 09:45:45 +0100410
411 poll_wait(file, &channel_wqs[minor].rt_queue, wait);
412 poll_wait(file, &channel_wqs[minor].lx_queue, wait);
413
414 if (rtlx == NULL)
415 return 0;
416
417 /* data available to read? */
418 if (rtlx_read_poll(minor, 0))
419 mask |= POLLIN | POLLRDNORM;
420
421 /* space to write */
422 if (rtlx_write_poll(minor))
423 mask |= POLLOUT | POLLWRNORM;
424
425 return mask;
426}
427
428static ssize_t file_read(struct file *file, char __user * buffer, size_t count,
429 loff_t * ppos)
430{
Josef Sipek1b04fe92006-12-08 02:37:20 -0800431 int minor = iminor(file->f_path.dentry->d_inode);
Ralf Baechle26009902006-04-05 09:45:45 +0100432
433 /* data available? */
434 if (!rtlx_read_poll(minor, (file->f_flags & O_NONBLOCK) ? 0 : 1)) {
435 return 0; // -EAGAIN makes cat whinge
436 }
437
Ralf Baechle46230aa2007-03-16 12:16:27 +0000438 return rtlx_read(minor, buffer, count);
Ralf Baechle26009902006-04-05 09:45:45 +0100439}
440
441static ssize_t file_write(struct file *file, const char __user * buffer,
442 size_t count, loff_t * ppos)
443{
444 int minor;
445 struct rtlx_channel *rt;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000446
Josef Sipek1b04fe92006-12-08 02:37:20 -0800447 minor = iminor(file->f_path.dentry->d_inode);
Ralf Baechlee01402b2005-07-14 15:57:16 +0000448 rt = &rtlx->channel[minor];
449
450 /* any space left... */
Ralf Baechle26009902006-04-05 09:45:45 +0100451 if (!rtlx_write_poll(minor)) {
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000452 int ret = 0;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000453
454 if (file->f_flags & O_NONBLOCK)
Ralf Baechleafc48412005-10-31 00:30:39 +0000455 return -EAGAIN;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000456
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000457 __wait_event_interruptible(channel_wqs[minor].rt_queue,
458 rtlx_write_poll(minor),
459 ret);
460 if (ret)
461 return ret;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000462 }
463
Ralf Baechle46230aa2007-03-16 12:16:27 +0000464 return rtlx_write(minor, buffer, count);
Ralf Baechlee01402b2005-07-14 15:57:16 +0000465}
466
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800467static const struct file_operations rtlx_fops = {
Ralf Baechle26009902006-04-05 09:45:45 +0100468 .owner = THIS_MODULE,
469 .open = file_open,
470 .release = file_release,
471 .write = file_write,
472 .read = file_read,
473 .poll = file_poll
Ralf Baechlee01402b2005-07-14 15:57:16 +0000474};
475
Ralf Baechle26009902006-04-05 09:45:45 +0100476static struct irqaction rtlx_irq = {
477 .handler = rtlx_interrupt,
Thomas Gleixnerf40298f2006-07-01 19:29:20 -0700478 .flags = IRQF_DISABLED,
Ralf Baechle26009902006-04-05 09:45:45 +0100479 .name = "RTLX",
480};
481
Atsushi Nemoto97dcb822007-01-08 02:14:29 +0900482static int rtlx_irq_num = MIPS_CPU_IRQ_BASE + MIPS_CPU_RTLX_IRQ;
Ralf Baechle26009902006-04-05 09:45:45 +0100483
Ralf Baechleafc48412005-10-31 00:30:39 +0000484static char register_chrdev_failed[] __initdata =
485 KERN_ERR "rtlx_module_init: unable to register device\n";
486
Ralf Baechle9d5a3f52007-07-28 00:51:45 +0100487static int __init rtlx_module_init(void)
Ralf Baechlee01402b2005-07-14 15:57:16 +0000488{
Ralf Baechlebb3d7c72007-02-07 15:36:56 +0000489 struct device *dev;
490 int i, err;
Ralf Baechle26009902006-04-05 09:45:45 +0100491
Ralf Baechle07cc0c92007-07-27 19:31:10 +0100492 if (!cpu_has_mipsmt) {
493 printk("VPE loader: not a MIPS MT capable processor\n");
494 return -ENODEV;
495 }
496
497 if (tclimit == 0) {
498 printk(KERN_WARNING "No TCs reserved for AP/SP, not "
499 "initializing RTLX.\nPass maxtcs=<n> argument as kernel "
500 "argument\n");
501
502 return -ENODEV;
503 }
504
Ralf Baechleafc48412005-10-31 00:30:39 +0000505 major = register_chrdev(0, module_name, &rtlx_fops);
506 if (major < 0) {
507 printk(register_chrdev_failed);
508 return major;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000509 }
510
Ralf Baechle26009902006-04-05 09:45:45 +0100511 /* initialise the wait queues */
512 for (i = 0; i < RTLX_CHANNELS; i++) {
513 init_waitqueue_head(&channel_wqs[i].rt_queue);
514 init_waitqueue_head(&channel_wqs[i].lx_queue);
Ralf Baechlec4c40182007-02-23 13:40:45 +0000515 atomic_set(&channel_wqs[i].in_open, 0);
Ralf Baechlebc4809e2007-03-15 17:13:47 +0000516 mutex_init(&channel_wqs[i].mutex);
Ralf Baechlebb3d7c72007-02-07 15:36:56 +0000517
Greg Kroah-Hartmana9b12612008-07-21 20:03:34 -0700518 dev = device_create(mt_class, NULL, MKDEV(major, i), NULL,
519 "%s%d", module_name, i);
Ralf Baechlebb3d7c72007-02-07 15:36:56 +0000520 if (IS_ERR(dev)) {
521 err = PTR_ERR(dev);
522 goto out_chrdev;
523 }
Ralf Baechle26009902006-04-05 09:45:45 +0100524 }
525
526 /* set up notifiers */
527 notify.start = starting;
528 notify.stop = stopping;
Ralf Baechle07cc0c92007-07-27 19:31:10 +0100529 vpe_notify(tclimit, &notify);
Ralf Baechle26009902006-04-05 09:45:45 +0100530
531 if (cpu_has_vint)
532 set_vi_handler(MIPS_CPU_RTLX_IRQ, rtlx_dispatch);
Kevin D. Kissell1928cc82008-04-16 15:32:22 +0200533 else {
534 pr_err("APRP RTLX init on non-vectored-interrupt processor\n");
535 err = -ENODEV;
536 goto out_chrdev;
537 }
Ralf Baechle26009902006-04-05 09:45:45 +0100538
539 rtlx_irq.dev_id = rtlx;
540 setup_irq(rtlx_irq_num, &rtlx_irq);
541
Ralf Baechleafc48412005-10-31 00:30:39 +0000542 return 0;
Ralf Baechlebb3d7c72007-02-07 15:36:56 +0000543
544out_chrdev:
545 for (i = 0; i < RTLX_CHANNELS; i++)
546 device_destroy(mt_class, MKDEV(major, i));
547
548 return err;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000549}
550
Ralf Baechleafc48412005-10-31 00:30:39 +0000551static void __exit rtlx_module_exit(void)
Ralf Baechlee01402b2005-07-14 15:57:16 +0000552{
Ralf Baechlebb3d7c72007-02-07 15:36:56 +0000553 int i;
554
555 for (i = 0; i < RTLX_CHANNELS; i++)
556 device_destroy(mt_class, MKDEV(major, i));
557
Ralf Baechlee01402b2005-07-14 15:57:16 +0000558 unregister_chrdev(major, module_name);
559}
560
561module_init(rtlx_module_init);
562module_exit(rtlx_module_exit);
Ralf Baechleafc48412005-10-31 00:30:39 +0000563
Ralf Baechlee01402b2005-07-14 15:57:16 +0000564MODULE_DESCRIPTION("MIPS RTLX");
Ralf Baechle26009902006-04-05 09:45:45 +0100565MODULE_AUTHOR("Elizabeth Oldham, MIPS Technologies, Inc.");
Ralf Baechlee01402b2005-07-14 15:57:16 +0000566MODULE_LICENSE("GPL");