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