2 * Copyright (C) 2005 MIPS Technologies, Inc. All rights reserved.
3 * Copyright (C) 2005, 06 Ralf Baechle (ralf@linux-mips.org)
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.
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
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.
20 #include <linux/kernel.h>
21 #include <linux/module.h>
23 #include <linux/init.h>
24 #include <asm/uaccess.h>
25 #include <linux/slab.h>
26 #include <linux/list.h>
27 #include <linux/vmalloc.h>
28 #include <linux/elf.h>
29 #include <linux/seq_file.h>
30 #include <linux/syscalls.h>
31 #include <linux/moduleloader.h>
32 #include <linux/interrupt.h>
33 #include <linux/poll.h>
34 #include <linux/sched.h>
35 #include <linux/wait.h>
36 #include <asm/mipsmtregs.h>
37 #include <asm/cacheflush.h>
38 #include <asm/atomic.h>
40 #include <asm/processor.h>
41 #include <asm/system.h>
45 #define RTLX_TARG_VPE 1
47 static struct rtlx_info
*rtlx
;
49 static char module_name
[] = "rtlx";
51 static struct chan_waitqueues
{
52 wait_queue_head_t rt_queue
;
53 wait_queue_head_t lx_queue
;
55 } channel_wqs
[RTLX_CHANNELS
];
57 static struct irqaction irq
;
59 static struct vpe_notifications notify
;
60 static int sp_stopping
= 0;
62 extern void *vpe_get_shared(int index
);
64 static void rtlx_dispatch(void)
66 do_IRQ(MIPS_CPU_IRQ_BASE
+ MIPS_CPU_RTLX_IRQ
);
70 /* Interrupt handler may be called before rtlx_init has otherwise had
73 static irqreturn_t
rtlx_interrupt(int irq
, void *dev_id
)
77 for (i
= 0; i
< RTLX_CHANNELS
; i
++) {
78 wake_up(&channel_wqs
[i
].lx_queue
);
79 wake_up(&channel_wqs
[i
].rt_queue
);
85 static __attribute_used__
void dump_rtlx(void)
89 printk("id 0x%lx state %d\n", rtlx
->id
, rtlx
->state
);
91 for (i
= 0; i
< RTLX_CHANNELS
; i
++) {
92 struct rtlx_channel
*chan
= &rtlx
->channel
[i
];
94 printk(" rt_state %d lx_state %d buffer_size %d\n",
95 chan
->rt_state
, chan
->lx_state
, chan
->buffer_size
);
97 printk(" rt_read %d rt_write %d\n",
98 chan
->rt_read
, chan
->rt_write
);
100 printk(" lx_read %d lx_write %d\n",
101 chan
->lx_read
, chan
->lx_write
);
103 printk(" rt_buffer <%s>\n", chan
->rt_buffer
);
104 printk(" lx_buffer <%s>\n", chan
->lx_buffer
);
108 /* call when we have the address of the shared structure from the SP side. */
109 static int rtlx_init(struct rtlx_info
*rtlxi
)
111 if (rtlxi
->id
!= RTLX_ID
) {
112 printk(KERN_ERR
"no valid RTLX id at 0x%p 0x%x\n", rtlxi
, rtlxi
->id
);
122 static void starting(int vpe
)
127 /* force a reload of rtlx */
130 /* wake up any sleeping rtlx_open's */
131 for (i
= 0; i
< RTLX_CHANNELS
; i
++)
132 wake_up_interruptible(&channel_wqs
[i
].lx_queue
);
135 static void stopping(int vpe
)
140 for (i
= 0; i
< RTLX_CHANNELS
; i
++)
141 wake_up_interruptible(&channel_wqs
[i
].lx_queue
);
145 int rtlx_open(int index
, int can_sleep
)
148 struct rtlx_channel
*chan
;
149 volatile struct rtlx_info
**p
;
151 if (index
>= RTLX_CHANNELS
) {
152 printk(KERN_DEBUG
"rtlx_open index out of range\n");
156 if (channel_wqs
[index
].in_open
) {
157 printk(KERN_DEBUG
"rtlx_open channel %d already opened\n", index
);
161 channel_wqs
[index
].in_open
++;
164 if( (p
= vpe_get_shared(RTLX_TARG_VPE
)) == NULL
) {
166 DECLARE_WAITQUEUE(wait
, current
);
169 add_wait_queue(&channel_wqs
[index
].lx_queue
, &wait
);
171 set_current_state(TASK_INTERRUPTIBLE
);
172 while ((p
= vpe_get_shared(RTLX_TARG_VPE
)) == NULL
) {
174 set_current_state(TASK_INTERRUPTIBLE
);
177 set_current_state(TASK_RUNNING
);
178 remove_wait_queue(&channel_wqs
[index
].lx_queue
, &wait
);
182 printk( KERN_DEBUG
"No SP program loaded, and device "
183 "opened with O_NONBLOCK\n");
184 channel_wqs
[index
].in_open
= 0;
191 DECLARE_WAITQUEUE(wait
, current
);
194 add_wait_queue(&channel_wqs
[index
].lx_queue
, &wait
);
196 set_current_state(TASK_INTERRUPTIBLE
);
200 /* reset task state to interruptable otherwise
201 we'll whizz round here like a very fast loopy
202 thing. schedule() appears to return with state
205 If the loaded SP program, for whatever reason,
206 doesn't set up the shared structure *p will never
207 become true. So whoever connected to either /dev/rt?
208 or if it was kspd, will then take up rather a lot of
212 set_current_state(TASK_INTERRUPTIBLE
);
215 set_current_state(TASK_RUNNING
);
216 remove_wait_queue(&channel_wqs
[index
].lx_queue
, &wait
);
221 printk(" *vpe_get_shared is NULL. "
222 "Has an SP program been loaded?\n");
223 channel_wqs
[index
].in_open
= 0;
228 if ((unsigned int)*p
< KSEG0
) {
229 printk(KERN_WARNING
"vpe_get_shared returned an invalid pointer "
230 "maybe an error code %d\n", (int)*p
);
231 channel_wqs
[index
].in_open
= 0;
235 if ((ret
= rtlx_init(*p
)) < 0) {
236 channel_wqs
[index
].in_open
= 0;
241 chan
= &rtlx
->channel
[index
];
243 if (chan
->lx_state
== RTLX_STATE_OPENED
) {
244 channel_wqs
[index
].in_open
= 0;
248 chan
->lx_state
= RTLX_STATE_OPENED
;
249 channel_wqs
[index
].in_open
= 0;
253 int rtlx_release(int index
)
255 rtlx
->channel
[index
].lx_state
= RTLX_STATE_UNUSED
;
259 unsigned int rtlx_read_poll(int index
, int can_sleep
)
261 struct rtlx_channel
*chan
;
266 chan
= &rtlx
->channel
[index
];
268 /* data available to read? */
269 if (chan
->lx_read
== chan
->lx_write
) {
271 DECLARE_WAITQUEUE(wait
, current
);
274 add_wait_queue(&channel_wqs
[index
].lx_queue
, &wait
);
276 set_current_state(TASK_INTERRUPTIBLE
);
277 while (chan
->lx_read
== chan
->lx_write
) {
280 set_current_state(TASK_INTERRUPTIBLE
);
283 set_current_state(TASK_RUNNING
);
284 remove_wait_queue(&channel_wqs
[index
].lx_queue
, &wait
);
289 set_current_state(TASK_RUNNING
);
290 remove_wait_queue(&channel_wqs
[index
].lx_queue
, &wait
);
298 return (chan
->lx_write
+ chan
->buffer_size
- chan
->lx_read
)
302 static inline int write_spacefree(int read
, int write
, int size
)
306 * Never fill the buffer completely, so indexes are always
307 * equal if empty and only empty, or !equal if data available
312 return ((read
+ size
- write
) % size
) - 1;
315 unsigned int rtlx_write_poll(int index
)
317 struct rtlx_channel
*chan
= &rtlx
->channel
[index
];
318 return write_spacefree(chan
->rt_read
, chan
->rt_write
, chan
->buffer_size
);
321 static inline void copy_to(void *dst
, void *src
, size_t count
, int user
)
324 copy_to_user(dst
, src
, count
);
326 memcpy(dst
, src
, count
);
329 static inline void copy_from(void *dst
, void *src
, size_t count
, int user
)
332 copy_from_user(dst
, src
, count
);
334 memcpy(dst
, src
, count
);
337 ssize_t
rtlx_read(int index
, void *buff
, size_t count
, int user
)
340 struct rtlx_channel
*lx
;
345 lx
= &rtlx
->channel
[index
];
347 /* find out how much in total */
349 (size_t)(lx
->lx_write
+ lx
->buffer_size
- lx
->lx_read
)
352 /* then how much from the read pointer onwards */
353 fl
= min( count
, (size_t)lx
->buffer_size
- lx
->lx_read
);
355 copy_to(buff
, &lx
->lx_buffer
[lx
->lx_read
], fl
, user
);
357 /* and if there is anything left at the beginning of the buffer */
359 copy_to (buff
+ fl
, lx
->lx_buffer
, count
- fl
, user
);
361 /* update the index */
362 lx
->lx_read
+= count
;
363 lx
->lx_read
%= lx
->buffer_size
;
368 ssize_t
rtlx_write(int index
, void *buffer
, size_t count
, int user
)
370 struct rtlx_channel
*rt
;
376 rt
= &rtlx
->channel
[index
];
378 /* total number of bytes to copy */
380 (size_t)write_spacefree(rt
->rt_read
, rt
->rt_write
,
383 /* first bit from write pointer to the end of the buffer, or count */
384 fl
= min(count
, (size_t) rt
->buffer_size
- rt
->rt_write
);
386 copy_from (&rt
->rt_buffer
[rt
->rt_write
], buffer
, fl
, user
);
388 /* if there's any left copy to the beginning of the buffer */
390 copy_from (rt
->rt_buffer
, buffer
+ fl
, count
- fl
, user
);
392 rt
->rt_write
+= count
;
393 rt
->rt_write
%= rt
->buffer_size
;
399 static int file_open(struct inode
*inode
, struct file
*filp
)
401 int minor
= iminor(inode
);
403 return rtlx_open(minor
, (filp
->f_flags
& O_NONBLOCK
) ? 0 : 1);
406 static int file_release(struct inode
*inode
, struct file
*filp
)
408 int minor
= iminor(inode
);
410 return rtlx_release(minor
);
413 static unsigned int file_poll(struct file
*file
, poll_table
* wait
)
416 unsigned int mask
= 0;
418 minor
= iminor(file
->f_path
.dentry
->d_inode
);
420 poll_wait(file
, &channel_wqs
[minor
].rt_queue
, wait
);
421 poll_wait(file
, &channel_wqs
[minor
].lx_queue
, wait
);
426 /* data available to read? */
427 if (rtlx_read_poll(minor
, 0))
428 mask
|= POLLIN
| POLLRDNORM
;
431 if (rtlx_write_poll(minor
))
432 mask
|= POLLOUT
| POLLWRNORM
;
437 static ssize_t
file_read(struct file
*file
, char __user
* buffer
, size_t count
,
440 int minor
= iminor(file
->f_path
.dentry
->d_inode
);
442 /* data available? */
443 if (!rtlx_read_poll(minor
, (file
->f_flags
& O_NONBLOCK
) ? 0 : 1)) {
444 return 0; // -EAGAIN makes cat whinge
447 return rtlx_read(minor
, buffer
, count
, 1);
450 static ssize_t
file_write(struct file
*file
, const char __user
* buffer
,
451 size_t count
, loff_t
* ppos
)
454 struct rtlx_channel
*rt
;
455 DECLARE_WAITQUEUE(wait
, current
);
457 minor
= iminor(file
->f_path
.dentry
->d_inode
);
458 rt
= &rtlx
->channel
[minor
];
460 /* any space left... */
461 if (!rtlx_write_poll(minor
)) {
463 if (file
->f_flags
& O_NONBLOCK
)
466 add_wait_queue(&channel_wqs
[minor
].rt_queue
, &wait
);
467 set_current_state(TASK_INTERRUPTIBLE
);
469 while (!rtlx_write_poll(minor
))
472 set_current_state(TASK_RUNNING
);
473 remove_wait_queue(&channel_wqs
[minor
].rt_queue
, &wait
);
476 return rtlx_write(minor
, (void *)buffer
, count
, 1);
479 static struct file_operations rtlx_fops
= {
480 .owner
= THIS_MODULE
,
482 .release
= file_release
,
488 static struct irqaction rtlx_irq
= {
489 .handler
= rtlx_interrupt
,
490 .flags
= IRQF_DISABLED
,
494 static int rtlx_irq_num
= MIPS_CPU_IRQ_BASE
+ MIPS_CPU_RTLX_IRQ
;
496 static char register_chrdev_failed
[] __initdata
=
497 KERN_ERR
"rtlx_module_init: unable to register device\n";
499 static int rtlx_module_init(void)
503 major
= register_chrdev(0, module_name
, &rtlx_fops
);
505 printk(register_chrdev_failed
);
509 /* initialise the wait queues */
510 for (i
= 0; i
< RTLX_CHANNELS
; i
++) {
511 init_waitqueue_head(&channel_wqs
[i
].rt_queue
);
512 init_waitqueue_head(&channel_wqs
[i
].lx_queue
);
513 channel_wqs
[i
].in_open
= 0;
516 /* set up notifiers */
517 notify
.start
= starting
;
518 notify
.stop
= stopping
;
519 vpe_notify(RTLX_TARG_VPE
, ¬ify
);
522 set_vi_handler(MIPS_CPU_RTLX_IRQ
, rtlx_dispatch
);
524 rtlx_irq
.dev_id
= rtlx
;
525 setup_irq(rtlx_irq_num
, &rtlx_irq
);
530 static void __exit
rtlx_module_exit(void)
532 unregister_chrdev(major
, module_name
);
535 module_init(rtlx_module_init
);
536 module_exit(rtlx_module_exit
);
538 MODULE_DESCRIPTION("MIPS RTLX");
539 MODULE_AUTHOR("Elizabeth Oldham, MIPS Technologies, Inc.");
540 MODULE_LICENSE("GPL");