blob: 8cf24d716d418f56c1221de16154fa242724c4d8 [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 Baechlee01402b2005-07-14 15:57:16 +000047#define RTLX_TARG_VPE 1
48
Ralf Baechleafc48412005-10-31 00:30:39 +000049static struct rtlx_info *rtlx;
Ralf Baechlee01402b2005-07-14 15:57:16 +000050static int major;
51static char module_name[] = "rtlx";
Ralf Baechlee01402b2005-07-14 15:57:16 +000052
53static struct chan_waitqueues {
54 wait_queue_head_t rt_queue;
55 wait_queue_head_t lx_queue;
Ralf Baechlec4c40182007-02-23 13:40:45 +000056 atomic_t in_open;
Ralf Baechlebc4809e2007-03-15 17:13:47 +000057 struct mutex mutex;
Ralf Baechlee01402b2005-07-14 15:57:16 +000058} channel_wqs[RTLX_CHANNELS];
59
Ralf Baechle26009902006-04-05 09:45:45 +010060static struct irqaction irq;
61static int irq_num;
62static struct vpe_notifications notify;
63static int sp_stopping = 0;
64
Ralf Baechlee01402b2005-07-14 15:57:16 +000065extern void *vpe_get_shared(int index);
66
Ralf Baechle937a8012006-10-07 19:44:33 +010067static void rtlx_dispatch(void)
Ralf Baechlee01402b2005-07-14 15:57:16 +000068{
Atsushi Nemoto97dcb822007-01-08 02:14:29 +090069 do_IRQ(MIPS_CPU_IRQ_BASE + MIPS_CPU_RTLX_IRQ);
Ralf Baechlee01402b2005-07-14 15:57:16 +000070}
71
Ralf Baechle26009902006-04-05 09:45:45 +010072
73/* Interrupt handler may be called before rtlx_init has otherwise had
74 a chance to run.
75*/
Ralf Baechle937a8012006-10-07 19:44:33 +010076static irqreturn_t rtlx_interrupt(int irq, void *dev_id)
Ralf Baechlee01402b2005-07-14 15:57:16 +000077{
Ralf Baechlee01402b2005-07-14 15:57:16 +000078 int i;
79
80 for (i = 0; i < RTLX_CHANNELS; i++) {
Ralf Baechle26009902006-04-05 09:45:45 +010081 wake_up(&channel_wqs[i].lx_queue);
82 wake_up(&channel_wqs[i].rt_queue);
Ralf Baechlee01402b2005-07-14 15:57:16 +000083 }
84
Ralf Baechleafc48412005-10-31 00:30:39 +000085 return IRQ_HANDLED;
Ralf Baechlee01402b2005-07-14 15:57:16 +000086}
87
David Rientjesf5dbeaf2007-07-22 01:01:39 -070088static void __used dump_rtlx(void)
Ralf Baechlee01402b2005-07-14 15:57:16 +000089{
90 int i;
91
Ralf Baechle26009902006-04-05 09:45:45 +010092 printk("id 0x%lx state %d\n", rtlx->id, rtlx->state);
93
94 for (i = 0; i < RTLX_CHANNELS; i++) {
95 struct rtlx_channel *chan = &rtlx->channel[i];
96
97 printk(" rt_state %d lx_state %d buffer_size %d\n",
98 chan->rt_state, chan->lx_state, chan->buffer_size);
99
100 printk(" rt_read %d rt_write %d\n",
101 chan->rt_read, chan->rt_write);
102
103 printk(" lx_read %d lx_write %d\n",
104 chan->lx_read, chan->lx_write);
105
106 printk(" rt_buffer <%s>\n", chan->rt_buffer);
107 printk(" lx_buffer <%s>\n", chan->lx_buffer);
108 }
109}
110
111/* call when we have the address of the shared structure from the SP side. */
112static int rtlx_init(struct rtlx_info *rtlxi)
113{
Ralf Baechlee01402b2005-07-14 15:57:16 +0000114 if (rtlxi->id != RTLX_ID) {
Ralf Baechle26009902006-04-05 09:45:45 +0100115 printk(KERN_ERR "no valid RTLX id at 0x%p 0x%x\n", rtlxi, rtlxi->id);
Ralf Baechleafc48412005-10-31 00:30:39 +0000116 return -ENOEXEC;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000117 }
118
Ralf Baechlee01402b2005-07-14 15:57:16 +0000119 rtlx = rtlxi;
Ralf Baechleafc48412005-10-31 00:30:39 +0000120
121 return 0;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000122}
123
Ralf Baechle26009902006-04-05 09:45:45 +0100124/* notifications */
125static void starting(int vpe)
Ralf Baechlee01402b2005-07-14 15:57:16 +0000126{
Ralf Baechle26009902006-04-05 09:45:45 +0100127 int i;
128 sp_stopping = 0;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000129
Ralf Baechle26009902006-04-05 09:45:45 +0100130 /* force a reload of rtlx */
131 rtlx=NULL;
132
133 /* wake up any sleeping rtlx_open's */
134 for (i = 0; i < RTLX_CHANNELS; i++)
135 wake_up_interruptible(&channel_wqs[i].lx_queue);
136}
137
138static void stopping(int vpe)
139{
140 int i;
141
142 sp_stopping = 1;
143 for (i = 0; i < RTLX_CHANNELS; i++)
144 wake_up_interruptible(&channel_wqs[i].lx_queue);
145}
146
147
148int rtlx_open(int index, int can_sleep)
149{
Ralf Baechle9e346822007-03-15 17:08:28 +0000150 struct rtlx_info **p;
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000151 struct rtlx_channel *chan;
Ralf Baechlec4c40182007-02-23 13:40:45 +0000152 enum rtlx_state state;
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000153 int ret = 0;
Ralf Baechle26009902006-04-05 09:45:45 +0100154
155 if (index >= RTLX_CHANNELS) {
156 printk(KERN_DEBUG "rtlx_open index out of range\n");
157 return -ENOSYS;
158 }
159
Ralf Baechlec4c40182007-02-23 13:40:45 +0000160 if (atomic_inc_return(&channel_wqs[index].in_open) > 1) {
161 printk(KERN_DEBUG "rtlx_open channel %d already opened\n",
162 index);
163 ret = -EBUSY;
164 goto out_fail;
Ralf Baechle26009902006-04-05 09:45:45 +0100165 }
166
Ralf Baechlee01402b2005-07-14 15:57:16 +0000167 if (rtlx == NULL) {
Ralf Baechlee01402b2005-07-14 15:57:16 +0000168 if( (p = vpe_get_shared(RTLX_TARG_VPE)) == NULL) {
Ralf Baechle26009902006-04-05 09:45:45 +0100169 if (can_sleep) {
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000170 __wait_event_interruptible(channel_wqs[index].lx_queue,
171 (p = vpe_get_shared(RTLX_TARG_VPE)),
172 ret);
173 if (ret)
174 goto out_fail;
Ralf Baechle26009902006-04-05 09:45:45 +0100175 } else {
Ralf Baechlec4c40182007-02-23 13:40:45 +0000176 printk(KERN_DEBUG "No SP program loaded, and device "
Ralf Baechle26009902006-04-05 09:45:45 +0100177 "opened with O_NONBLOCK\n");
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000178 ret = -ENOSYS;
179 goto out_fail;
Ralf Baechle26009902006-04-05 09:45:45 +0100180 }
Ralf Baechlee01402b2005-07-14 15:57:16 +0000181 }
182
Ralf Baechle9e346822007-03-15 17:08:28 +0000183 smp_rmb();
Ralf Baechlee01402b2005-07-14 15:57:16 +0000184 if (*p == NULL) {
Ralf Baechle26009902006-04-05 09:45:45 +0100185 if (can_sleep) {
Ralf Baechle9e346822007-03-15 17:08:28 +0000186 DEFINE_WAIT(wait);
187
188 for (;;) {
189 prepare_to_wait(&channel_wqs[index].lx_queue, &wait, TASK_INTERRUPTIBLE);
190 smp_rmb();
191 if (*p != NULL)
192 break;
193 if (!signal_pending(current)) {
194 schedule();
195 continue;
196 }
197 ret = -ERESTARTSYS;
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000198 goto out_fail;
Ralf Baechle9e346822007-03-15 17:08:28 +0000199 }
200 finish_wait(&channel_wqs[index].lx_queue, &wait);
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000201 } else {
Ralf Baechle26009902006-04-05 09:45:45 +0100202 printk(" *vpe_get_shared is NULL. "
203 "Has an SP program been loaded?\n");
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000204 ret = -ENOSYS;
205 goto out_fail;
Ralf Baechle26009902006-04-05 09:45:45 +0100206 }
Ralf Baechlee01402b2005-07-14 15:57:16 +0000207 }
208
Ralf Baechle26009902006-04-05 09:45:45 +0100209 if ((unsigned int)*p < KSEG0) {
210 printk(KERN_WARNING "vpe_get_shared returned an invalid pointer "
211 "maybe an error code %d\n", (int)*p);
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000212 ret = -ENOSYS;
213 goto out_fail;
Ralf Baechle26009902006-04-05 09:45:45 +0100214 }
215
Ralf Baechlec4c40182007-02-23 13:40:45 +0000216 if ((ret = rtlx_init(*p)) < 0)
217 goto out_ret;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000218 }
219
Ralf Baechle26009902006-04-05 09:45:45 +0100220 chan = &rtlx->channel[index];
Ralf Baechlee01402b2005-07-14 15:57:16 +0000221
Ralf Baechlec4c40182007-02-23 13:40:45 +0000222 state = xchg(&chan->lx_state, RTLX_STATE_OPENED);
223 if (state == RTLX_STATE_OPENED) {
224 ret = -EBUSY;
225 goto out_fail;
226 }
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000227
228out_fail:
Ralf Baechlec4c40182007-02-23 13:40:45 +0000229 smp_mb();
230 atomic_dec(&channel_wqs[index].in_open);
231 smp_mb();
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000232
Ralf Baechlec4c40182007-02-23 13:40:45 +0000233out_ret:
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000234 return ret;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000235}
236
Ralf Baechle26009902006-04-05 09:45:45 +0100237int rtlx_release(int index)
Ralf Baechlee01402b2005-07-14 15:57:16 +0000238{
Ralf Baechle26009902006-04-05 09:45:45 +0100239 rtlx->channel[index].lx_state = RTLX_STATE_UNUSED;
Ralf Baechleafc48412005-10-31 00:30:39 +0000240 return 0;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000241}
242
Ralf Baechle26009902006-04-05 09:45:45 +0100243unsigned int rtlx_read_poll(int index, int can_sleep)
Ralf Baechlee01402b2005-07-14 15:57:16 +0000244{
Ralf Baechle26009902006-04-05 09:45:45 +0100245 struct rtlx_channel *chan;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000246
Ralf Baechle26009902006-04-05 09:45:45 +0100247 if (rtlx == NULL)
248 return 0;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000249
Ralf Baechle26009902006-04-05 09:45:45 +0100250 chan = &rtlx->channel[index];
Ralf Baechlee01402b2005-07-14 15:57:16 +0000251
252 /* data available to read? */
Ralf Baechle26009902006-04-05 09:45:45 +0100253 if (chan->lx_read == chan->lx_write) {
254 if (can_sleep) {
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000255 int ret = 0;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000256
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000257 __wait_event_interruptible(channel_wqs[index].lx_queue,
258 chan->lx_read != chan->lx_write || sp_stopping,
259 ret);
260 if (ret)
261 return ret;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000262
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000263 if (sp_stopping)
264 return 0;
265 } else
Ralf Baechle26009902006-04-05 09:45:45 +0100266 return 0;
267 }
268
269 return (chan->lx_write + chan->buffer_size - chan->lx_read)
270 % chan->buffer_size;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000271}
272
Ralf Baechle26009902006-04-05 09:45:45 +0100273static inline int write_spacefree(int read, int write, int size)
Ralf Baechlee01402b2005-07-14 15:57:16 +0000274{
Ralf Baechle26009902006-04-05 09:45:45 +0100275 if (read == write) {
276 /*
277 * Never fill the buffer completely, so indexes are always
278 * equal if empty and only empty, or !equal if data available
279 */
280 return size - 1;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000281 }
282
Ralf Baechle26009902006-04-05 09:45:45 +0100283 return ((read + size - write) % size) - 1;
284}
285
286unsigned int rtlx_write_poll(int index)
287{
288 struct rtlx_channel *chan = &rtlx->channel[index];
289 return write_spacefree(chan->rt_read, chan->rt_write, chan->buffer_size);
290}
291
Ralf Baechle7f5a7712007-04-25 15:08:57 +0100292ssize_t rtlx_read(int index, void __user *buff, size_t count)
Ralf Baechle26009902006-04-05 09:45:45 +0100293{
Ralf Baechle61dcc6f2007-03-15 17:10:16 +0000294 size_t lx_write, fl = 0L;
Ralf Baechle26009902006-04-05 09:45:45 +0100295 struct rtlx_channel *lx;
Ralf Baechle46230aa2007-03-16 12:16:27 +0000296 unsigned long failed;
Ralf Baechle26009902006-04-05 09:45:45 +0100297
298 if (rtlx == NULL)
299 return -ENOSYS;
300
301 lx = &rtlx->channel[index];
302
Ralf Baechlebc4809e2007-03-15 17:13:47 +0000303 mutex_lock(&channel_wqs[index].mutex);
Ralf Baechle61dcc6f2007-03-15 17:10:16 +0000304 smp_rmb();
305 lx_write = lx->lx_write;
306
Ralf Baechlee01402b2005-07-14 15:57:16 +0000307 /* find out how much in total */
Ralf Baechleafc48412005-10-31 00:30:39 +0000308 count = min(count,
Ralf Baechle61dcc6f2007-03-15 17:10:16 +0000309 (size_t)(lx_write + lx->buffer_size - lx->lx_read)
Ralf Baechle26009902006-04-05 09:45:45 +0100310 % lx->buffer_size);
Ralf Baechlee01402b2005-07-14 15:57:16 +0000311
312 /* then how much from the read pointer onwards */
Ralf Baechle61dcc6f2007-03-15 17:10:16 +0000313 fl = min(count, (size_t)lx->buffer_size - lx->lx_read);
Ralf Baechlee01402b2005-07-14 15:57:16 +0000314
Ralf Baechle46230aa2007-03-16 12:16:27 +0000315 failed = copy_to_user(buff, lx->lx_buffer + lx->lx_read, fl);
316 if (failed)
317 goto out;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000318
319 /* and if there is anything left at the beginning of the buffer */
Ralf Baechle61dcc6f2007-03-15 17:10:16 +0000320 if (count - fl)
Ralf Baechle46230aa2007-03-16 12:16:27 +0000321 failed = copy_to_user(buff + fl, lx->lx_buffer, count - fl);
322
323out:
324 count -= failed;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000325
Ralf Baechle61dcc6f2007-03-15 17:10:16 +0000326 smp_wmb();
327 lx->lx_read = (lx->lx_read + count) % lx->buffer_size;
328 smp_wmb();
Ralf Baechlebc4809e2007-03-15 17:13:47 +0000329 mutex_unlock(&channel_wqs[index].mutex);
Ralf Baechlee01402b2005-07-14 15:57:16 +0000330
Ralf Baechleafc48412005-10-31 00:30:39 +0000331 return count;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000332}
333
Ralf Baechle7f5a7712007-04-25 15:08:57 +0100334ssize_t rtlx_write(int index, const void __user *buffer, size_t count)
Ralf Baechlee01402b2005-07-14 15:57:16 +0000335{
Ralf Baechlee01402b2005-07-14 15:57:16 +0000336 struct rtlx_channel *rt;
Ralf Baechle7f5a7712007-04-25 15:08:57 +0100337 unsigned long failed;
Ralf Baechle61dcc6f2007-03-15 17:10:16 +0000338 size_t rt_read;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000339 size_t fl;
Ralf Baechle26009902006-04-05 09:45:45 +0100340
341 if (rtlx == NULL)
342 return(-ENOSYS);
343
344 rt = &rtlx->channel[index];
345
Ralf Baechlebc4809e2007-03-15 17:13:47 +0000346 mutex_lock(&channel_wqs[index].mutex);
Ralf Baechle61dcc6f2007-03-15 17:10:16 +0000347 smp_rmb();
348 rt_read = rt->rt_read;
349
Ralf Baechle26009902006-04-05 09:45:45 +0100350 /* total number of bytes to copy */
351 count = min(count,
Ralf Baechle61dcc6f2007-03-15 17:10:16 +0000352 (size_t)write_spacefree(rt_read, rt->rt_write, rt->buffer_size));
Ralf Baechle26009902006-04-05 09:45:45 +0100353
354 /* first bit from write pointer to the end of the buffer, or count */
355 fl = min(count, (size_t) rt->buffer_size - rt->rt_write);
356
Ralf Baechle46230aa2007-03-16 12:16:27 +0000357 failed = copy_from_user(rt->rt_buffer + rt->rt_write, buffer, fl);
358 if (failed)
359 goto out;
Ralf Baechle26009902006-04-05 09:45:45 +0100360
361 /* if there's any left copy to the beginning of the buffer */
Ralf Baechle46230aa2007-03-16 12:16:27 +0000362 if (count - fl) {
363 failed = copy_from_user(rt->rt_buffer, buffer + fl, count - fl);
364 }
365
366out:
Ralf Baechle7f5a7712007-04-25 15:08:57 +0100367 count -= failed;
Ralf Baechle26009902006-04-05 09:45:45 +0100368
Ralf Baechle61dcc6f2007-03-15 17:10:16 +0000369 smp_wmb();
370 rt->rt_write = (rt->rt_write + count) % rt->buffer_size;
371 smp_wmb();
Ralf Baechlebc4809e2007-03-15 17:13:47 +0000372 mutex_unlock(&channel_wqs[index].mutex);
Ralf Baechle26009902006-04-05 09:45:45 +0100373
Ralf Baechle61dcc6f2007-03-15 17:10:16 +0000374 return count;
Ralf Baechle26009902006-04-05 09:45:45 +0100375}
376
377
378static int file_open(struct inode *inode, struct file *filp)
379{
Ralf Baechle79e55bc2006-04-24 17:15:10 +0100380 int minor = iminor(inode);
Ralf Baechle26009902006-04-05 09:45:45 +0100381
382 return rtlx_open(minor, (filp->f_flags & O_NONBLOCK) ? 0 : 1);
383}
384
385static int file_release(struct inode *inode, struct file *filp)
386{
Ralf Baechle79e55bc2006-04-24 17:15:10 +0100387 int minor = iminor(inode);
Ralf Baechle26009902006-04-05 09:45:45 +0100388
389 return rtlx_release(minor);
390}
391
392static unsigned int file_poll(struct file *file, poll_table * wait)
393{
394 int minor;
395 unsigned int mask = 0;
396
Josef Sipek1b04fe92006-12-08 02:37:20 -0800397 minor = iminor(file->f_path.dentry->d_inode);
Ralf Baechle26009902006-04-05 09:45:45 +0100398
399 poll_wait(file, &channel_wqs[minor].rt_queue, wait);
400 poll_wait(file, &channel_wqs[minor].lx_queue, wait);
401
402 if (rtlx == NULL)
403 return 0;
404
405 /* data available to read? */
406 if (rtlx_read_poll(minor, 0))
407 mask |= POLLIN | POLLRDNORM;
408
409 /* space to write */
410 if (rtlx_write_poll(minor))
411 mask |= POLLOUT | POLLWRNORM;
412
413 return mask;
414}
415
416static ssize_t file_read(struct file *file, char __user * buffer, size_t count,
417 loff_t * ppos)
418{
Josef Sipek1b04fe92006-12-08 02:37:20 -0800419 int minor = iminor(file->f_path.dentry->d_inode);
Ralf Baechle26009902006-04-05 09:45:45 +0100420
421 /* data available? */
422 if (!rtlx_read_poll(minor, (file->f_flags & O_NONBLOCK) ? 0 : 1)) {
423 return 0; // -EAGAIN makes cat whinge
424 }
425
Ralf Baechle46230aa2007-03-16 12:16:27 +0000426 return rtlx_read(minor, buffer, count);
Ralf Baechle26009902006-04-05 09:45:45 +0100427}
428
429static ssize_t file_write(struct file *file, const char __user * buffer,
430 size_t count, loff_t * ppos)
431{
432 int minor;
433 struct rtlx_channel *rt;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000434
Josef Sipek1b04fe92006-12-08 02:37:20 -0800435 minor = iminor(file->f_path.dentry->d_inode);
Ralf Baechlee01402b2005-07-14 15:57:16 +0000436 rt = &rtlx->channel[minor];
437
438 /* any space left... */
Ralf Baechle26009902006-04-05 09:45:45 +0100439 if (!rtlx_write_poll(minor)) {
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000440 int ret = 0;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000441
442 if (file->f_flags & O_NONBLOCK)
Ralf Baechleafc48412005-10-31 00:30:39 +0000443 return -EAGAIN;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000444
Ralf Baechle67e2ccc2007-02-22 14:19:48 +0000445 __wait_event_interruptible(channel_wqs[minor].rt_queue,
446 rtlx_write_poll(minor),
447 ret);
448 if (ret)
449 return ret;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000450 }
451
Ralf Baechle46230aa2007-03-16 12:16:27 +0000452 return rtlx_write(minor, buffer, count);
Ralf Baechlee01402b2005-07-14 15:57:16 +0000453}
454
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800455static const struct file_operations rtlx_fops = {
Ralf Baechle26009902006-04-05 09:45:45 +0100456 .owner = THIS_MODULE,
457 .open = file_open,
458 .release = file_release,
459 .write = file_write,
460 .read = file_read,
461 .poll = file_poll
Ralf Baechlee01402b2005-07-14 15:57:16 +0000462};
463
Ralf Baechle26009902006-04-05 09:45:45 +0100464static struct irqaction rtlx_irq = {
465 .handler = rtlx_interrupt,
Thomas Gleixnerf40298f2006-07-01 19:29:20 -0700466 .flags = IRQF_DISABLED,
Ralf Baechle26009902006-04-05 09:45:45 +0100467 .name = "RTLX",
468};
469
Atsushi Nemoto97dcb822007-01-08 02:14:29 +0900470static int rtlx_irq_num = MIPS_CPU_IRQ_BASE + MIPS_CPU_RTLX_IRQ;
Ralf Baechle26009902006-04-05 09:45:45 +0100471
Ralf Baechleafc48412005-10-31 00:30:39 +0000472static char register_chrdev_failed[] __initdata =
473 KERN_ERR "rtlx_module_init: unable to register device\n";
474
Ralf Baechle26009902006-04-05 09:45:45 +0100475static int rtlx_module_init(void)
Ralf Baechlee01402b2005-07-14 15:57:16 +0000476{
Ralf Baechlebb3d7c72007-02-07 15:36:56 +0000477 struct device *dev;
478 int i, err;
Ralf Baechle26009902006-04-05 09:45:45 +0100479
Ralf Baechleafc48412005-10-31 00:30:39 +0000480 major = register_chrdev(0, module_name, &rtlx_fops);
481 if (major < 0) {
482 printk(register_chrdev_failed);
483 return major;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000484 }
485
Ralf Baechle26009902006-04-05 09:45:45 +0100486 /* initialise the wait queues */
487 for (i = 0; i < RTLX_CHANNELS; i++) {
488 init_waitqueue_head(&channel_wqs[i].rt_queue);
489 init_waitqueue_head(&channel_wqs[i].lx_queue);
Ralf Baechlec4c40182007-02-23 13:40:45 +0000490 atomic_set(&channel_wqs[i].in_open, 0);
Ralf Baechlebc4809e2007-03-15 17:13:47 +0000491 mutex_init(&channel_wqs[i].mutex);
Ralf Baechlebb3d7c72007-02-07 15:36:56 +0000492
493 dev = device_create(mt_class, NULL, MKDEV(major, i),
494 "%s%d", module_name, i);
495 if (IS_ERR(dev)) {
496 err = PTR_ERR(dev);
497 goto out_chrdev;
498 }
Ralf Baechle26009902006-04-05 09:45:45 +0100499 }
500
501 /* set up notifiers */
502 notify.start = starting;
503 notify.stop = stopping;
504 vpe_notify(RTLX_TARG_VPE, &notify);
505
506 if (cpu_has_vint)
507 set_vi_handler(MIPS_CPU_RTLX_IRQ, rtlx_dispatch);
508
509 rtlx_irq.dev_id = rtlx;
510 setup_irq(rtlx_irq_num, &rtlx_irq);
511
Ralf Baechleafc48412005-10-31 00:30:39 +0000512 return 0;
Ralf Baechlebb3d7c72007-02-07 15:36:56 +0000513
514out_chrdev:
515 for (i = 0; i < RTLX_CHANNELS; i++)
516 device_destroy(mt_class, MKDEV(major, i));
517
518 return err;
Ralf Baechlee01402b2005-07-14 15:57:16 +0000519}
520
Ralf Baechleafc48412005-10-31 00:30:39 +0000521static void __exit rtlx_module_exit(void)
Ralf Baechlee01402b2005-07-14 15:57:16 +0000522{
Ralf Baechlebb3d7c72007-02-07 15:36:56 +0000523 int i;
524
525 for (i = 0; i < RTLX_CHANNELS; i++)
526 device_destroy(mt_class, MKDEV(major, i));
527
Ralf Baechlee01402b2005-07-14 15:57:16 +0000528 unregister_chrdev(major, module_name);
529}
530
531module_init(rtlx_module_init);
532module_exit(rtlx_module_exit);
Ralf Baechleafc48412005-10-31 00:30:39 +0000533
Ralf Baechlee01402b2005-07-14 15:57:16 +0000534MODULE_DESCRIPTION("MIPS RTLX");
Ralf Baechle26009902006-04-05 09:45:45 +0100535MODULE_AUTHOR("Elizabeth Oldham, MIPS Technologies, Inc.");
Ralf Baechlee01402b2005-07-14 15:57:16 +0000536MODULE_LICENSE("GPL");