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 <linux/interrupt.h>
25 #include <linux/irq.h>
26 #include <linux/poll.h>
27 #include <linux/sched.h>
28 #include <linux/wait.h>
30 #include <asm/mipsmtregs.h>
31 #include <asm/bitops.h>
33 #include <asm/processor.h>
35 #include <asm/uaccess.h>
37 #define RTLX_TARG_VPE 1
39 static struct rtlx_info
*rtlx
;
41 static char module_name
[] = "rtlx";
42 static struct irqaction irq
;
45 static inline int spacefree(int read
, int write
, int size
)
49 * never fill the buffer completely, so indexes are always
50 * equal if empty and only empty, or !equal if data available
55 return ((read
+ size
- write
) % size
) - 1;
58 static struct chan_waitqueues
{
59 wait_queue_head_t rt_queue
;
60 wait_queue_head_t lx_queue
;
61 } channel_wqs
[RTLX_CHANNELS
];
63 extern void *vpe_get_shared(int index
);
65 static void rtlx_dispatch(struct pt_regs
*regs
)
67 do_IRQ(MIPSCPU_INT_BASE
+ MIPS_CPU_RTLX_IRQ
, regs
);
70 static irqreturn_t
rtlx_interrupt(int irq
, void *dev_id
, struct pt_regs
*regs
)
74 for (i
= 0; i
< RTLX_CHANNELS
; i
++) {
75 struct rtlx_channel
*chan
= &rtlx
->channel
[i
];
77 if (chan
->lx_read
!= chan
->lx_write
)
78 wake_up_interruptible(&channel_wqs
[i
].lx_queue
);
84 /* call when we have the address of the shared structure from the SP side. */
85 static int rtlx_init(struct rtlx_info
*rtlxi
)
89 if (rtlxi
->id
!= RTLX_ID
) {
90 printk(KERN_WARNING
"no valid RTLX id at 0x%p\n", rtlxi
);
94 /* initialise the wait queues */
95 for (i
= 0; i
< RTLX_CHANNELS
; i
++) {
96 init_waitqueue_head(&channel_wqs
[i
].rt_queue
);
97 init_waitqueue_head(&channel_wqs
[i
].lx_queue
);
100 /* set up for interrupt handling */
101 memset(&irq
, 0, sizeof(struct irqaction
));
104 set_vi_handler(MIPS_CPU_RTLX_IRQ
, rtlx_dispatch
);
106 irq_num
= MIPSCPU_INT_BASE
+ MIPS_CPU_RTLX_IRQ
;
107 irq
.handler
= rtlx_interrupt
;
108 irq
.flags
= SA_INTERRUPT
;
111 setup_irq(irq_num
, &irq
);
118 /* only allow one open process at a time to open each channel */
119 static int rtlx_open(struct inode
*inode
, struct file
*filp
)
122 struct rtlx_channel
*chan
;
124 /* assume only 1 device at the mo. */
125 minor
= MINOR(inode
->i_rdev
);
128 struct rtlx_info
**p
;
129 if( (p
= vpe_get_shared(RTLX_TARG_VPE
)) == NULL
) {
130 printk(KERN_ERR
"vpe_get_shared is NULL. "
131 "Has an SP program been loaded?\n");
136 printk(KERN_ERR
"vpe_shared %p %p\n", p
, *p
);
140 if ((ret
= rtlx_init(*p
)) < 0)
144 chan
= &rtlx
->channel
[minor
];
146 if (test_and_set_bit(RTLX_STATE_OPENED
, &chan
->lx_state
))
152 static int rtlx_release(struct inode
*inode
, struct file
*filp
)
154 int minor
= MINOR(inode
->i_rdev
);
156 clear_bit(RTLX_STATE_OPENED
, &rtlx
->channel
[minor
].lx_state
);
157 smp_mb__after_clear_bit();
162 static unsigned int rtlx_poll(struct file
*file
, poll_table
* wait
)
165 unsigned int mask
= 0;
166 struct rtlx_channel
*chan
;
168 minor
= MINOR(file
->f_dentry
->d_inode
->i_rdev
);
169 chan
= &rtlx
->channel
[minor
];
171 poll_wait(file
, &channel_wqs
[minor
].rt_queue
, wait
);
172 poll_wait(file
, &channel_wqs
[minor
].lx_queue
, wait
);
174 /* data available to read? */
175 if (chan
->lx_read
!= chan
->lx_write
)
176 mask
|= POLLIN
| POLLRDNORM
;
179 if (spacefree(chan
->rt_read
, chan
->rt_write
, chan
->buffer_size
))
180 mask
|= POLLOUT
| POLLWRNORM
;
185 static ssize_t
rtlx_read(struct file
*file
, char __user
* buffer
, size_t count
,
188 unsigned long failed
;
191 struct rtlx_channel
*lx
;
192 DECLARE_WAITQUEUE(wait
, current
);
194 minor
= MINOR(file
->f_dentry
->d_inode
->i_rdev
);
195 lx
= &rtlx
->channel
[minor
];
197 /* data available? */
198 if (lx
->lx_write
== lx
->lx_read
) {
199 if (file
->f_flags
& O_NONBLOCK
)
200 return 0; /* -EAGAIN makes cat whinge */
203 add_wait_queue(&channel_wqs
[minor
].lx_queue
, &wait
);
204 set_current_state(TASK_INTERRUPTIBLE
);
206 while (lx
->lx_write
== lx
->lx_read
)
209 set_current_state(TASK_RUNNING
);
210 remove_wait_queue(&channel_wqs
[minor
].lx_queue
, &wait
);
215 /* find out how much in total */
217 (size_t)(lx
->lx_write
+ lx
->buffer_size
- lx
->lx_read
) % lx
->buffer_size
);
219 /* then how much from the read pointer onwards */
220 fl
= min(count
, (size_t)lx
->buffer_size
- lx
->lx_read
);
222 failed
= copy_to_user (buffer
, &lx
->lx_buffer
[lx
->lx_read
], fl
);
228 /* and if there is anything left at the beginning of the buffer */
230 failed
= copy_to_user (buffer
+ fl
, lx
->lx_buffer
, count
- fl
);
238 /* update the index */
239 lx
->lx_read
+= count
;
240 lx
->lx_read
%= lx
->buffer_size
;
245 static ssize_t
rtlx_write(struct file
*file
, const char __user
* buffer
,
246 size_t count
, loff_t
* ppos
)
248 unsigned long failed
;
250 struct rtlx_channel
*rt
;
252 DECLARE_WAITQUEUE(wait
, current
);
254 minor
= MINOR(file
->f_dentry
->d_inode
->i_rdev
);
255 rt
= &rtlx
->channel
[minor
];
257 /* any space left... */
258 if (!spacefree(rt
->rt_read
, rt
->rt_write
, rt
->buffer_size
)) {
260 if (file
->f_flags
& O_NONBLOCK
)
263 add_wait_queue(&channel_wqs
[minor
].rt_queue
, &wait
);
264 set_current_state(TASK_INTERRUPTIBLE
);
266 while (!spacefree(rt
->rt_read
, rt
->rt_write
, rt
->buffer_size
))
269 set_current_state(TASK_RUNNING
);
270 remove_wait_queue(&channel_wqs
[minor
].rt_queue
, &wait
);
273 /* total number of bytes to copy */
274 count
= min(count
, (size_t)spacefree(rt
->rt_read
, rt
->rt_write
, rt
->buffer_size
) );
276 /* first bit from write pointer to the end of the buffer, or count */
277 fl
= min(count
, (size_t) rt
->buffer_size
- rt
->rt_write
);
279 failed
= copy_from_user(&rt
->rt_buffer
[rt
->rt_write
], buffer
, fl
);
285 /* if there's any left copy to the beginning of the buffer */
287 failed
= copy_from_user(rt
->rt_buffer
, buffer
+ fl
, count
- fl
);
295 rt
->rt_write
+= count
;
296 rt
->rt_write
%= rt
->buffer_size
;
301 static struct file_operations rtlx_fops
= {
302 .owner
= THIS_MODULE
,
304 .release
= rtlx_release
,
310 static char register_chrdev_failed
[] __initdata
=
311 KERN_ERR
"rtlx_module_init: unable to register device\n";
313 static int __init
rtlx_module_init(void)
315 major
= register_chrdev(0, module_name
, &rtlx_fops
);
317 printk(register_chrdev_failed
);
324 static void __exit
rtlx_module_exit(void)
326 unregister_chrdev(major
, module_name
);
329 module_init(rtlx_module_init
);
330 module_exit(rtlx_module_exit
);
332 MODULE_DESCRIPTION("MIPS RTLX");
333 MODULE_AUTHOR("Elizabeth Clarke, MIPS Technologies, Inc.");
334 MODULE_LICENSE("GPL");