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