Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2005 MIPS Technologies, Inc. All rights reserved. |
Ralf Baechle | a84c96e | 2006-01-15 18:11:28 +0000 | [diff] [blame^] | 3 | * Copyright (C) 2005, 06 Ralf Baechle (ralf@linux-mips.org) |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 4 | * |
| 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 | |
| 20 | #include <linux/kernel.h> |
| 21 | #include <linux/module.h> |
| 22 | #include <linux/fs.h> |
| 23 | #include <linux/init.h> |
Ralf Baechle | a84c96e | 2006-01-15 18:11:28 +0000 | [diff] [blame^] | 24 | #include <linux/interrupt.h> |
| 25 | #include <linux/irq.h> |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 26 | #include <linux/poll.h> |
| 27 | #include <linux/sched.h> |
| 28 | #include <linux/wait.h> |
Ralf Baechle | a84c96e | 2006-01-15 18:11:28 +0000 | [diff] [blame^] | 29 | |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 30 | #include <asm/mipsmtregs.h> |
Ralf Baechle | afc4841 | 2005-10-31 00:30:39 +0000 | [diff] [blame] | 31 | #include <asm/bitops.h> |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 32 | #include <asm/cpu.h> |
| 33 | #include <asm/processor.h> |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 34 | #include <asm/rtlx.h> |
Ralf Baechle | afc4841 | 2005-10-31 00:30:39 +0000 | [diff] [blame] | 35 | #include <asm/uaccess.h> |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 36 | |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 37 | #define RTLX_TARG_VPE 1 |
| 38 | |
Ralf Baechle | afc4841 | 2005-10-31 00:30:39 +0000 | [diff] [blame] | 39 | static struct rtlx_info *rtlx; |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 40 | static int major; |
| 41 | static char module_name[] = "rtlx"; |
Ralf Baechle | afc4841 | 2005-10-31 00:30:39 +0000 | [diff] [blame] | 42 | static struct irqaction irq; |
| 43 | static int irq_num; |
| 44 | |
| 45 | static inline int spacefree(int read, int write, int size) |
| 46 | { |
| 47 | if (read == write) { |
| 48 | /* |
| 49 | * never fill the buffer completely, so indexes are always |
| 50 | * equal if empty and only empty, or !equal if data available |
| 51 | */ |
| 52 | return size - 1; |
| 53 | } |
| 54 | |
| 55 | return ((read + size - write) % size) - 1; |
| 56 | } |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 57 | |
| 58 | static struct chan_waitqueues { |
| 59 | wait_queue_head_t rt_queue; |
| 60 | wait_queue_head_t lx_queue; |
| 61 | } channel_wqs[RTLX_CHANNELS]; |
| 62 | |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 63 | extern void *vpe_get_shared(int index); |
| 64 | |
| 65 | static void rtlx_dispatch(struct pt_regs *regs) |
| 66 | { |
| 67 | do_IRQ(MIPSCPU_INT_BASE + MIPS_CPU_RTLX_IRQ, regs); |
| 68 | } |
| 69 | |
Ralf Baechle | afc4841 | 2005-10-31 00:30:39 +0000 | [diff] [blame] | 70 | static irqreturn_t rtlx_interrupt(int irq, void *dev_id, struct pt_regs *regs) |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 71 | { |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 72 | int i; |
| 73 | |
| 74 | for (i = 0; i < RTLX_CHANNELS; i++) { |
| 75 | struct rtlx_channel *chan = &rtlx->channel[i]; |
| 76 | |
| 77 | if (chan->lx_read != chan->lx_write) |
| 78 | wake_up_interruptible(&channel_wqs[i].lx_queue); |
| 79 | } |
| 80 | |
Ralf Baechle | afc4841 | 2005-10-31 00:30:39 +0000 | [diff] [blame] | 81 | return IRQ_HANDLED; |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | /* call when we have the address of the shared structure from the SP side. */ |
| 85 | static int rtlx_init(struct rtlx_info *rtlxi) |
| 86 | { |
| 87 | int i; |
| 88 | |
| 89 | if (rtlxi->id != RTLX_ID) { |
| 90 | printk(KERN_WARNING "no valid RTLX id at 0x%p\n", rtlxi); |
Ralf Baechle | afc4841 | 2005-10-31 00:30:39 +0000 | [diff] [blame] | 91 | return -ENOEXEC; |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 92 | } |
| 93 | |
| 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); |
| 98 | } |
| 99 | |
| 100 | /* set up for interrupt handling */ |
| 101 | memset(&irq, 0, sizeof(struct irqaction)); |
| 102 | |
Ralf Baechle | afc4841 | 2005-10-31 00:30:39 +0000 | [diff] [blame] | 103 | if (cpu_has_vint) |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 104 | set_vi_handler(MIPS_CPU_RTLX_IRQ, rtlx_dispatch); |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 105 | |
| 106 | irq_num = MIPSCPU_INT_BASE + MIPS_CPU_RTLX_IRQ; |
| 107 | irq.handler = rtlx_interrupt; |
| 108 | irq.flags = SA_INTERRUPT; |
| 109 | irq.name = "RTLX"; |
| 110 | irq.dev_id = rtlx; |
| 111 | setup_irq(irq_num, &irq); |
| 112 | |
| 113 | rtlx = rtlxi; |
Ralf Baechle | afc4841 | 2005-10-31 00:30:39 +0000 | [diff] [blame] | 114 | |
| 115 | return 0; |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | /* only allow one open process at a time to open each channel */ |
| 119 | static int rtlx_open(struct inode *inode, struct file *filp) |
| 120 | { |
| 121 | int minor, ret; |
| 122 | struct rtlx_channel *chan; |
| 123 | |
| 124 | /* assume only 1 device at the mo. */ |
| 125 | minor = MINOR(inode->i_rdev); |
| 126 | |
| 127 | if (rtlx == NULL) { |
| 128 | struct rtlx_info **p; |
| 129 | if( (p = vpe_get_shared(RTLX_TARG_VPE)) == NULL) { |
Ralf Baechle | afc4841 | 2005-10-31 00:30:39 +0000 | [diff] [blame] | 130 | printk(KERN_ERR "vpe_get_shared is NULL. " |
| 131 | "Has an SP program been loaded?\n"); |
| 132 | return -EFAULT; |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | if (*p == NULL) { |
Ralf Baechle | afc4841 | 2005-10-31 00:30:39 +0000 | [diff] [blame] | 136 | printk(KERN_ERR "vpe_shared %p %p\n", p, *p); |
| 137 | return -EFAULT; |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | if ((ret = rtlx_init(*p)) < 0) |
Ralf Baechle | afc4841 | 2005-10-31 00:30:39 +0000 | [diff] [blame] | 141 | return ret; |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | chan = &rtlx->channel[minor]; |
| 145 | |
Ralf Baechle | afc4841 | 2005-10-31 00:30:39 +0000 | [diff] [blame] | 146 | if (test_and_set_bit(RTLX_STATE_OPENED, &chan->lx_state)) |
| 147 | return -EBUSY; |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 148 | |
Ralf Baechle | afc4841 | 2005-10-31 00:30:39 +0000 | [diff] [blame] | 149 | return 0; |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | static int rtlx_release(struct inode *inode, struct file *filp) |
| 153 | { |
Ralf Baechle | afc4841 | 2005-10-31 00:30:39 +0000 | [diff] [blame] | 154 | int minor = MINOR(inode->i_rdev); |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 155 | |
Ralf Baechle | afc4841 | 2005-10-31 00:30:39 +0000 | [diff] [blame] | 156 | clear_bit(RTLX_STATE_OPENED, &rtlx->channel[minor].lx_state); |
| 157 | smp_mb__after_clear_bit(); |
| 158 | |
| 159 | return 0; |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | static unsigned int rtlx_poll(struct file *file, poll_table * wait) |
| 163 | { |
| 164 | int minor; |
| 165 | unsigned int mask = 0; |
| 166 | struct rtlx_channel *chan; |
| 167 | |
| 168 | minor = MINOR(file->f_dentry->d_inode->i_rdev); |
| 169 | chan = &rtlx->channel[minor]; |
| 170 | |
| 171 | poll_wait(file, &channel_wqs[minor].rt_queue, wait); |
| 172 | poll_wait(file, &channel_wqs[minor].lx_queue, wait); |
| 173 | |
| 174 | /* data available to read? */ |
| 175 | if (chan->lx_read != chan->lx_write) |
| 176 | mask |= POLLIN | POLLRDNORM; |
| 177 | |
| 178 | /* space to write */ |
| 179 | if (spacefree(chan->rt_read, chan->rt_write, chan->buffer_size)) |
| 180 | mask |= POLLOUT | POLLWRNORM; |
| 181 | |
Ralf Baechle | afc4841 | 2005-10-31 00:30:39 +0000 | [diff] [blame] | 182 | return mask; |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | static ssize_t rtlx_read(struct file *file, char __user * buffer, size_t count, |
| 186 | loff_t * ppos) |
| 187 | { |
Ralf Baechle | afc4841 | 2005-10-31 00:30:39 +0000 | [diff] [blame] | 188 | unsigned long failed; |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 189 | size_t fl = 0L; |
| 190 | int minor; |
| 191 | struct rtlx_channel *lx; |
| 192 | DECLARE_WAITQUEUE(wait, current); |
| 193 | |
| 194 | minor = MINOR(file->f_dentry->d_inode->i_rdev); |
| 195 | lx = &rtlx->channel[minor]; |
| 196 | |
| 197 | /* data available? */ |
| 198 | if (lx->lx_write == lx->lx_read) { |
| 199 | if (file->f_flags & O_NONBLOCK) |
Ralf Baechle | afc4841 | 2005-10-31 00:30:39 +0000 | [diff] [blame] | 200 | return 0; /* -EAGAIN makes cat whinge */ |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 201 | |
| 202 | /* go to sleep */ |
| 203 | add_wait_queue(&channel_wqs[minor].lx_queue, &wait); |
| 204 | set_current_state(TASK_INTERRUPTIBLE); |
| 205 | |
| 206 | while (lx->lx_write == lx->lx_read) |
| 207 | schedule(); |
| 208 | |
| 209 | set_current_state(TASK_RUNNING); |
| 210 | remove_wait_queue(&channel_wqs[minor].lx_queue, &wait); |
| 211 | |
| 212 | /* back running */ |
| 213 | } |
| 214 | |
| 215 | /* find out how much in total */ |
Ralf Baechle | afc4841 | 2005-10-31 00:30:39 +0000 | [diff] [blame] | 216 | count = min(count, |
| 217 | (size_t)(lx->lx_write + lx->buffer_size - lx->lx_read) % lx->buffer_size); |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 218 | |
| 219 | /* then how much from the read pointer onwards */ |
Ralf Baechle | afc4841 | 2005-10-31 00:30:39 +0000 | [diff] [blame] | 220 | fl = min(count, (size_t)lx->buffer_size - lx->lx_read); |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 221 | |
Ralf Baechle | afc4841 | 2005-10-31 00:30:39 +0000 | [diff] [blame] | 222 | failed = copy_to_user (buffer, &lx->lx_buffer[lx->lx_read], fl); |
| 223 | if (failed) { |
| 224 | count = fl - failed; |
| 225 | goto out; |
| 226 | } |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 227 | |
| 228 | /* and if there is anything left at the beginning of the buffer */ |
Ralf Baechle | afc4841 | 2005-10-31 00:30:39 +0000 | [diff] [blame] | 229 | if (count - fl) { |
| 230 | failed = copy_to_user (buffer + fl, lx->lx_buffer, count - fl); |
| 231 | if (failed) { |
| 232 | count -= failed; |
| 233 | goto out; |
| 234 | } |
| 235 | } |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 236 | |
Ralf Baechle | afc4841 | 2005-10-31 00:30:39 +0000 | [diff] [blame] | 237 | out: |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 238 | /* update the index */ |
| 239 | lx->lx_read += count; |
| 240 | lx->lx_read %= lx->buffer_size; |
| 241 | |
Ralf Baechle | afc4841 | 2005-10-31 00:30:39 +0000 | [diff] [blame] | 242 | return count; |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | static ssize_t rtlx_write(struct file *file, const char __user * buffer, |
| 246 | size_t count, loff_t * ppos) |
| 247 | { |
Ralf Baechle | afc4841 | 2005-10-31 00:30:39 +0000 | [diff] [blame] | 248 | unsigned long failed; |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 249 | int minor; |
| 250 | struct rtlx_channel *rt; |
| 251 | size_t fl; |
| 252 | DECLARE_WAITQUEUE(wait, current); |
| 253 | |
| 254 | minor = MINOR(file->f_dentry->d_inode->i_rdev); |
| 255 | rt = &rtlx->channel[minor]; |
| 256 | |
| 257 | /* any space left... */ |
| 258 | if (!spacefree(rt->rt_read, rt->rt_write, rt->buffer_size)) { |
| 259 | |
| 260 | if (file->f_flags & O_NONBLOCK) |
Ralf Baechle | afc4841 | 2005-10-31 00:30:39 +0000 | [diff] [blame] | 261 | return -EAGAIN; |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 262 | |
| 263 | add_wait_queue(&channel_wqs[minor].rt_queue, &wait); |
| 264 | set_current_state(TASK_INTERRUPTIBLE); |
| 265 | |
| 266 | while (!spacefree(rt->rt_read, rt->rt_write, rt->buffer_size)) |
| 267 | schedule(); |
| 268 | |
| 269 | set_current_state(TASK_RUNNING); |
| 270 | remove_wait_queue(&channel_wqs[minor].rt_queue, &wait); |
| 271 | } |
| 272 | |
| 273 | /* total number of bytes to copy */ |
Ralf Baechle | afc4841 | 2005-10-31 00:30:39 +0000 | [diff] [blame] | 274 | count = min(count, (size_t)spacefree(rt->rt_read, rt->rt_write, rt->buffer_size) ); |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 275 | |
| 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); |
| 278 | |
Ralf Baechle | afc4841 | 2005-10-31 00:30:39 +0000 | [diff] [blame] | 279 | failed = copy_from_user(&rt->rt_buffer[rt->rt_write], buffer, fl); |
| 280 | if (failed) { |
| 281 | count = fl - failed; |
| 282 | goto out; |
| 283 | } |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 284 | |
| 285 | /* if there's any left copy to the beginning of the buffer */ |
Ralf Baechle | afc4841 | 2005-10-31 00:30:39 +0000 | [diff] [blame] | 286 | if (count - fl) { |
| 287 | failed = copy_from_user(rt->rt_buffer, buffer + fl, count - fl); |
| 288 | if (failed) { |
| 289 | count -= failed; |
| 290 | goto out; |
| 291 | } |
| 292 | } |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 293 | |
Ralf Baechle | afc4841 | 2005-10-31 00:30:39 +0000 | [diff] [blame] | 294 | out: |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 295 | rt->rt_write += count; |
| 296 | rt->rt_write %= rt->buffer_size; |
| 297 | |
Ralf Baechle | afc4841 | 2005-10-31 00:30:39 +0000 | [diff] [blame] | 298 | return count; |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | static struct file_operations rtlx_fops = { |
Ralf Baechle | afc4841 | 2005-10-31 00:30:39 +0000 | [diff] [blame] | 302 | .owner = THIS_MODULE, |
| 303 | .open = rtlx_open, |
| 304 | .release = rtlx_release, |
| 305 | .write = rtlx_write, |
| 306 | .read = rtlx_read, |
| 307 | .poll = rtlx_poll |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 308 | }; |
| 309 | |
Ralf Baechle | afc4841 | 2005-10-31 00:30:39 +0000 | [diff] [blame] | 310 | static char register_chrdev_failed[] __initdata = |
| 311 | KERN_ERR "rtlx_module_init: unable to register device\n"; |
| 312 | |
| 313 | static int __init rtlx_module_init(void) |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 314 | { |
Ralf Baechle | afc4841 | 2005-10-31 00:30:39 +0000 | [diff] [blame] | 315 | major = register_chrdev(0, module_name, &rtlx_fops); |
| 316 | if (major < 0) { |
| 317 | printk(register_chrdev_failed); |
| 318 | return major; |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 319 | } |
| 320 | |
Ralf Baechle | afc4841 | 2005-10-31 00:30:39 +0000 | [diff] [blame] | 321 | return 0; |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 322 | } |
| 323 | |
Ralf Baechle | afc4841 | 2005-10-31 00:30:39 +0000 | [diff] [blame] | 324 | static void __exit rtlx_module_exit(void) |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 325 | { |
| 326 | unregister_chrdev(major, module_name); |
| 327 | } |
| 328 | |
| 329 | module_init(rtlx_module_init); |
| 330 | module_exit(rtlx_module_exit); |
Ralf Baechle | afc4841 | 2005-10-31 00:30:39 +0000 | [diff] [blame] | 331 | |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 332 | MODULE_DESCRIPTION("MIPS RTLX"); |
Ralf Baechle | afc4841 | 2005-10-31 00:30:39 +0000 | [diff] [blame] | 333 | MODULE_AUTHOR("Elizabeth Clarke, MIPS Technologies, Inc."); |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 334 | MODULE_LICENSE("GPL"); |