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 | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 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> |
Ralf Baechle | a84c96e | 2006-01-15 18:11:28 +0000 | [diff] [blame] | 32 | #include <linux/interrupt.h> |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 33 | #include <linux/poll.h> |
| 34 | #include <linux/sched.h> |
| 35 | #include <linux/wait.h> |
| 36 | #include <asm/mipsmtregs.h> |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 37 | #include <asm/cacheflush.h> |
| 38 | #include <asm/atomic.h> |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 39 | #include <asm/cpu.h> |
| 40 | #include <asm/processor.h> |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 41 | #include <asm/system.h> |
| 42 | #include <asm/vpe.h> |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 43 | #include <asm/rtlx.h> |
| 44 | |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 45 | #define RTLX_TARG_VPE 1 |
| 46 | |
Ralf Baechle | afc4841 | 2005-10-31 00:30:39 +0000 | [diff] [blame] | 47 | static struct rtlx_info *rtlx; |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 48 | static int major; |
| 49 | static char module_name[] = "rtlx"; |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 50 | |
| 51 | static struct chan_waitqueues { |
| 52 | wait_queue_head_t rt_queue; |
| 53 | wait_queue_head_t lx_queue; |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 54 | int in_open; |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 55 | } channel_wqs[RTLX_CHANNELS]; |
| 56 | |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 57 | static struct irqaction irq; |
| 58 | static int irq_num; |
| 59 | static struct vpe_notifications notify; |
| 60 | static int sp_stopping = 0; |
| 61 | |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 62 | extern void *vpe_get_shared(int index); |
| 63 | |
Ralf Baechle | 937a801 | 2006-10-07 19:44:33 +0100 | [diff] [blame] | 64 | static void rtlx_dispatch(void) |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 65 | { |
Atsushi Nemoto | 97dcb82 | 2007-01-08 02:14:29 +0900 | [diff] [blame] | 66 | do_IRQ(MIPS_CPU_IRQ_BASE + MIPS_CPU_RTLX_IRQ); |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 69 | |
| 70 | /* Interrupt handler may be called before rtlx_init has otherwise had |
| 71 | a chance to run. |
| 72 | */ |
Ralf Baechle | 937a801 | 2006-10-07 19:44:33 +0100 | [diff] [blame] | 73 | static irqreturn_t rtlx_interrupt(int irq, void *dev_id) |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 74 | { |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 75 | int i; |
| 76 | |
| 77 | for (i = 0; i < RTLX_CHANNELS; i++) { |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 78 | wake_up(&channel_wqs[i].lx_queue); |
| 79 | wake_up(&channel_wqs[i].rt_queue); |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Ralf Baechle | afc4841 | 2005-10-31 00:30:39 +0000 | [diff] [blame] | 82 | return IRQ_HANDLED; |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 85 | static __attribute_used__ void dump_rtlx(void) |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 86 | { |
| 87 | int i; |
| 88 | |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 89 | printk("id 0x%lx state %d\n", rtlx->id, rtlx->state); |
| 90 | |
| 91 | for (i = 0; i < RTLX_CHANNELS; i++) { |
| 92 | struct rtlx_channel *chan = &rtlx->channel[i]; |
| 93 | |
| 94 | printk(" rt_state %d lx_state %d buffer_size %d\n", |
| 95 | chan->rt_state, chan->lx_state, chan->buffer_size); |
| 96 | |
| 97 | printk(" rt_read %d rt_write %d\n", |
| 98 | chan->rt_read, chan->rt_write); |
| 99 | |
| 100 | printk(" lx_read %d lx_write %d\n", |
| 101 | chan->lx_read, chan->lx_write); |
| 102 | |
| 103 | printk(" rt_buffer <%s>\n", chan->rt_buffer); |
| 104 | printk(" lx_buffer <%s>\n", chan->lx_buffer); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | /* call when we have the address of the shared structure from the SP side. */ |
| 109 | static int rtlx_init(struct rtlx_info *rtlxi) |
| 110 | { |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 111 | if (rtlxi->id != RTLX_ID) { |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 112 | printk(KERN_ERR "no valid RTLX id at 0x%p 0x%x\n", rtlxi, rtlxi->id); |
Ralf Baechle | afc4841 | 2005-10-31 00:30:39 +0000 | [diff] [blame] | 113 | return -ENOEXEC; |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 116 | rtlx = rtlxi; |
Ralf Baechle | afc4841 | 2005-10-31 00:30:39 +0000 | [diff] [blame] | 117 | |
| 118 | return 0; |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 121 | /* notifications */ |
| 122 | static void starting(int vpe) |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 123 | { |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 124 | int i; |
| 125 | sp_stopping = 0; |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 126 | |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 127 | /* force a reload of rtlx */ |
| 128 | rtlx=NULL; |
| 129 | |
| 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); |
| 133 | } |
| 134 | |
| 135 | static void stopping(int vpe) |
| 136 | { |
| 137 | int i; |
| 138 | |
| 139 | sp_stopping = 1; |
| 140 | for (i = 0; i < RTLX_CHANNELS; i++) |
| 141 | wake_up_interruptible(&channel_wqs[i].lx_queue); |
| 142 | } |
| 143 | |
| 144 | |
| 145 | int rtlx_open(int index, int can_sleep) |
| 146 | { |
| 147 | int ret; |
| 148 | struct rtlx_channel *chan; |
| 149 | volatile struct rtlx_info **p; |
| 150 | |
| 151 | if (index >= RTLX_CHANNELS) { |
| 152 | printk(KERN_DEBUG "rtlx_open index out of range\n"); |
| 153 | return -ENOSYS; |
| 154 | } |
| 155 | |
| 156 | if (channel_wqs[index].in_open) { |
| 157 | printk(KERN_DEBUG "rtlx_open channel %d already opened\n", index); |
| 158 | return -EBUSY; |
| 159 | } |
| 160 | |
| 161 | channel_wqs[index].in_open++; |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 162 | |
| 163 | if (rtlx == NULL) { |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 164 | if( (p = vpe_get_shared(RTLX_TARG_VPE)) == NULL) { |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 165 | if (can_sleep) { |
| 166 | DECLARE_WAITQUEUE(wait, current); |
| 167 | |
| 168 | /* go to sleep */ |
| 169 | add_wait_queue(&channel_wqs[index].lx_queue, &wait); |
| 170 | |
| 171 | set_current_state(TASK_INTERRUPTIBLE); |
| 172 | while ((p = vpe_get_shared(RTLX_TARG_VPE)) == NULL) { |
| 173 | schedule(); |
| 174 | set_current_state(TASK_INTERRUPTIBLE); |
| 175 | } |
| 176 | |
| 177 | set_current_state(TASK_RUNNING); |
| 178 | remove_wait_queue(&channel_wqs[index].lx_queue, &wait); |
| 179 | |
| 180 | /* back running */ |
| 181 | } else { |
| 182 | printk( KERN_DEBUG "No SP program loaded, and device " |
| 183 | "opened with O_NONBLOCK\n"); |
| 184 | channel_wqs[index].in_open = 0; |
| 185 | return -ENOSYS; |
| 186 | } |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | if (*p == NULL) { |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 190 | if (can_sleep) { |
| 191 | DECLARE_WAITQUEUE(wait, current); |
| 192 | |
| 193 | /* go to sleep */ |
| 194 | add_wait_queue(&channel_wqs[index].lx_queue, &wait); |
| 195 | |
| 196 | set_current_state(TASK_INTERRUPTIBLE); |
| 197 | while (*p == NULL) { |
| 198 | schedule(); |
| 199 | |
| 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 |
| 203 | set to TASK_RUNNING. |
| 204 | |
| 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 |
| 209 | processor cycles. |
| 210 | */ |
| 211 | |
| 212 | set_current_state(TASK_INTERRUPTIBLE); |
| 213 | } |
| 214 | |
| 215 | set_current_state(TASK_RUNNING); |
| 216 | remove_wait_queue(&channel_wqs[index].lx_queue, &wait); |
| 217 | |
| 218 | /* back running */ |
| 219 | } |
| 220 | else { |
| 221 | printk(" *vpe_get_shared is NULL. " |
| 222 | "Has an SP program been loaded?\n"); |
| 223 | channel_wqs[index].in_open = 0; |
| 224 | return -ENOSYS; |
| 225 | } |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 226 | } |
| 227 | |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 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; |
| 232 | return -ENOSYS; |
| 233 | } |
| 234 | |
| 235 | if ((ret = rtlx_init(*p)) < 0) { |
| 236 | channel_wqs[index].in_open = 0; |
| 237 | return ret; |
| 238 | } |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 239 | } |
| 240 | |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 241 | chan = &rtlx->channel[index]; |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 242 | |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 243 | if (chan->lx_state == RTLX_STATE_OPENED) { |
| 244 | channel_wqs[index].in_open = 0; |
| 245 | return -EBUSY; |
| 246 | } |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 247 | |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 248 | chan->lx_state = RTLX_STATE_OPENED; |
| 249 | channel_wqs[index].in_open = 0; |
Ralf Baechle | afc4841 | 2005-10-31 00:30:39 +0000 | [diff] [blame] | 250 | return 0; |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 253 | int rtlx_release(int index) |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 254 | { |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 255 | rtlx->channel[index].lx_state = RTLX_STATE_UNUSED; |
Ralf Baechle | afc4841 | 2005-10-31 00:30:39 +0000 | [diff] [blame] | 256 | return 0; |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 257 | } |
| 258 | |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 259 | unsigned int rtlx_read_poll(int index, int can_sleep) |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 260 | { |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 261 | struct rtlx_channel *chan; |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 262 | |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 263 | if (rtlx == NULL) |
| 264 | return 0; |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 265 | |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 266 | chan = &rtlx->channel[index]; |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 267 | |
| 268 | /* data available to read? */ |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 269 | if (chan->lx_read == chan->lx_write) { |
| 270 | if (can_sleep) { |
| 271 | DECLARE_WAITQUEUE(wait, current); |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 272 | |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 273 | /* go to sleep */ |
| 274 | add_wait_queue(&channel_wqs[index].lx_queue, &wait); |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 275 | |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 276 | set_current_state(TASK_INTERRUPTIBLE); |
| 277 | while (chan->lx_read == chan->lx_write) { |
| 278 | schedule(); |
| 279 | |
| 280 | set_current_state(TASK_INTERRUPTIBLE); |
| 281 | |
| 282 | if (sp_stopping) { |
| 283 | set_current_state(TASK_RUNNING); |
| 284 | remove_wait_queue(&channel_wqs[index].lx_queue, &wait); |
| 285 | return 0; |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | set_current_state(TASK_RUNNING); |
| 290 | remove_wait_queue(&channel_wqs[index].lx_queue, &wait); |
| 291 | |
| 292 | /* back running */ |
| 293 | } |
| 294 | else |
| 295 | return 0; |
| 296 | } |
| 297 | |
| 298 | return (chan->lx_write + chan->buffer_size - chan->lx_read) |
| 299 | % chan->buffer_size; |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 300 | } |
| 301 | |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 302 | static inline int write_spacefree(int read, int write, int size) |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 303 | { |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 304 | if (read == write) { |
| 305 | /* |
| 306 | * Never fill the buffer completely, so indexes are always |
| 307 | * equal if empty and only empty, or !equal if data available |
| 308 | */ |
| 309 | return size - 1; |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 310 | } |
| 311 | |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 312 | return ((read + size - write) % size) - 1; |
| 313 | } |
| 314 | |
| 315 | unsigned int rtlx_write_poll(int index) |
| 316 | { |
| 317 | struct rtlx_channel *chan = &rtlx->channel[index]; |
| 318 | return write_spacefree(chan->rt_read, chan->rt_write, chan->buffer_size); |
| 319 | } |
| 320 | |
| 321 | static inline void copy_to(void *dst, void *src, size_t count, int user) |
| 322 | { |
| 323 | if (user) |
| 324 | copy_to_user(dst, src, count); |
| 325 | else |
| 326 | memcpy(dst, src, count); |
| 327 | } |
| 328 | |
| 329 | static inline void copy_from(void *dst, void *src, size_t count, int user) |
| 330 | { |
| 331 | if (user) |
| 332 | copy_from_user(dst, src, count); |
| 333 | else |
| 334 | memcpy(dst, src, count); |
| 335 | } |
| 336 | |
| 337 | ssize_t rtlx_read(int index, void *buff, size_t count, int user) |
| 338 | { |
| 339 | size_t fl = 0L; |
| 340 | struct rtlx_channel *lx; |
| 341 | |
| 342 | if (rtlx == NULL) |
| 343 | return -ENOSYS; |
| 344 | |
| 345 | lx = &rtlx->channel[index]; |
| 346 | |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 347 | /* find out how much in total */ |
Ralf Baechle | afc4841 | 2005-10-31 00:30:39 +0000 | [diff] [blame] | 348 | count = min(count, |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 349 | (size_t)(lx->lx_write + lx->buffer_size - lx->lx_read) |
| 350 | % lx->buffer_size); |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 351 | |
| 352 | /* then how much from the read pointer onwards */ |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 353 | fl = min( count, (size_t)lx->buffer_size - lx->lx_read); |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 354 | |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 355 | copy_to(buff, &lx->lx_buffer[lx->lx_read], fl, user); |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 356 | |
| 357 | /* and if there is anything left at the beginning of the buffer */ |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 358 | if ( count - fl ) |
| 359 | copy_to (buff + fl, lx->lx_buffer, count - fl, user); |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 360 | |
| 361 | /* update the index */ |
| 362 | lx->lx_read += count; |
| 363 | lx->lx_read %= lx->buffer_size; |
| 364 | |
Ralf Baechle | afc4841 | 2005-10-31 00:30:39 +0000 | [diff] [blame] | 365 | return count; |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 366 | } |
| 367 | |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 368 | ssize_t rtlx_write(int index, void *buffer, size_t count, int user) |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 369 | { |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 370 | struct rtlx_channel *rt; |
| 371 | size_t fl; |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 372 | |
| 373 | if (rtlx == NULL) |
| 374 | return(-ENOSYS); |
| 375 | |
| 376 | rt = &rtlx->channel[index]; |
| 377 | |
| 378 | /* total number of bytes to copy */ |
| 379 | count = min(count, |
| 380 | (size_t)write_spacefree(rt->rt_read, rt->rt_write, |
| 381 | rt->buffer_size)); |
| 382 | |
| 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); |
| 385 | |
| 386 | copy_from (&rt->rt_buffer[rt->rt_write], buffer, fl, user); |
| 387 | |
| 388 | /* if there's any left copy to the beginning of the buffer */ |
| 389 | if( count - fl ) |
| 390 | copy_from (rt->rt_buffer, buffer + fl, count - fl, user); |
| 391 | |
| 392 | rt->rt_write += count; |
| 393 | rt->rt_write %= rt->buffer_size; |
| 394 | |
| 395 | return(count); |
| 396 | } |
| 397 | |
| 398 | |
| 399 | static int file_open(struct inode *inode, struct file *filp) |
| 400 | { |
Ralf Baechle | 79e55bc | 2006-04-24 17:15:10 +0100 | [diff] [blame] | 401 | int minor = iminor(inode); |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 402 | |
| 403 | return rtlx_open(minor, (filp->f_flags & O_NONBLOCK) ? 0 : 1); |
| 404 | } |
| 405 | |
| 406 | static int file_release(struct inode *inode, struct file *filp) |
| 407 | { |
Ralf Baechle | 79e55bc | 2006-04-24 17:15:10 +0100 | [diff] [blame] | 408 | int minor = iminor(inode); |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 409 | |
| 410 | return rtlx_release(minor); |
| 411 | } |
| 412 | |
| 413 | static unsigned int file_poll(struct file *file, poll_table * wait) |
| 414 | { |
| 415 | int minor; |
| 416 | unsigned int mask = 0; |
| 417 | |
Josef Sipek | 1b04fe9 | 2006-12-08 02:37:20 -0800 | [diff] [blame] | 418 | minor = iminor(file->f_path.dentry->d_inode); |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 419 | |
| 420 | poll_wait(file, &channel_wqs[minor].rt_queue, wait); |
| 421 | poll_wait(file, &channel_wqs[minor].lx_queue, wait); |
| 422 | |
| 423 | if (rtlx == NULL) |
| 424 | return 0; |
| 425 | |
| 426 | /* data available to read? */ |
| 427 | if (rtlx_read_poll(minor, 0)) |
| 428 | mask |= POLLIN | POLLRDNORM; |
| 429 | |
| 430 | /* space to write */ |
| 431 | if (rtlx_write_poll(minor)) |
| 432 | mask |= POLLOUT | POLLWRNORM; |
| 433 | |
| 434 | return mask; |
| 435 | } |
| 436 | |
| 437 | static ssize_t file_read(struct file *file, char __user * buffer, size_t count, |
| 438 | loff_t * ppos) |
| 439 | { |
Josef Sipek | 1b04fe9 | 2006-12-08 02:37:20 -0800 | [diff] [blame] | 440 | int minor = iminor(file->f_path.dentry->d_inode); |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 441 | |
| 442 | /* data available? */ |
| 443 | if (!rtlx_read_poll(minor, (file->f_flags & O_NONBLOCK) ? 0 : 1)) { |
| 444 | return 0; // -EAGAIN makes cat whinge |
| 445 | } |
| 446 | |
| 447 | return rtlx_read(minor, buffer, count, 1); |
| 448 | } |
| 449 | |
| 450 | static ssize_t file_write(struct file *file, const char __user * buffer, |
| 451 | size_t count, loff_t * ppos) |
| 452 | { |
| 453 | int minor; |
| 454 | struct rtlx_channel *rt; |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 455 | DECLARE_WAITQUEUE(wait, current); |
| 456 | |
Josef Sipek | 1b04fe9 | 2006-12-08 02:37:20 -0800 | [diff] [blame] | 457 | minor = iminor(file->f_path.dentry->d_inode); |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 458 | rt = &rtlx->channel[minor]; |
| 459 | |
| 460 | /* any space left... */ |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 461 | if (!rtlx_write_poll(minor)) { |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 462 | |
| 463 | if (file->f_flags & O_NONBLOCK) |
Ralf Baechle | afc4841 | 2005-10-31 00:30:39 +0000 | [diff] [blame] | 464 | return -EAGAIN; |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 465 | |
| 466 | add_wait_queue(&channel_wqs[minor].rt_queue, &wait); |
| 467 | set_current_state(TASK_INTERRUPTIBLE); |
| 468 | |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 469 | while (!rtlx_write_poll(minor)) |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 470 | schedule(); |
| 471 | |
| 472 | set_current_state(TASK_RUNNING); |
| 473 | remove_wait_queue(&channel_wqs[minor].rt_queue, &wait); |
| 474 | } |
| 475 | |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 476 | return rtlx_write(minor, (void *)buffer, count, 1); |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | static struct file_operations rtlx_fops = { |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 480 | .owner = THIS_MODULE, |
| 481 | .open = file_open, |
| 482 | .release = file_release, |
| 483 | .write = file_write, |
| 484 | .read = file_read, |
| 485 | .poll = file_poll |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 486 | }; |
| 487 | |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 488 | static struct irqaction rtlx_irq = { |
| 489 | .handler = rtlx_interrupt, |
Thomas Gleixner | f40298f | 2006-07-01 19:29:20 -0700 | [diff] [blame] | 490 | .flags = IRQF_DISABLED, |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 491 | .name = "RTLX", |
| 492 | }; |
| 493 | |
Atsushi Nemoto | 97dcb82 | 2007-01-08 02:14:29 +0900 | [diff] [blame] | 494 | static int rtlx_irq_num = MIPS_CPU_IRQ_BASE + MIPS_CPU_RTLX_IRQ; |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 495 | |
Ralf Baechle | afc4841 | 2005-10-31 00:30:39 +0000 | [diff] [blame] | 496 | static char register_chrdev_failed[] __initdata = |
| 497 | KERN_ERR "rtlx_module_init: unable to register device\n"; |
| 498 | |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 499 | static int rtlx_module_init(void) |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 500 | { |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 501 | int i; |
| 502 | |
Ralf Baechle | afc4841 | 2005-10-31 00:30:39 +0000 | [diff] [blame] | 503 | major = register_chrdev(0, module_name, &rtlx_fops); |
| 504 | if (major < 0) { |
| 505 | printk(register_chrdev_failed); |
| 506 | return major; |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 507 | } |
| 508 | |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 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; |
| 514 | } |
| 515 | |
| 516 | /* set up notifiers */ |
| 517 | notify.start = starting; |
| 518 | notify.stop = stopping; |
| 519 | vpe_notify(RTLX_TARG_VPE, ¬ify); |
| 520 | |
| 521 | if (cpu_has_vint) |
| 522 | set_vi_handler(MIPS_CPU_RTLX_IRQ, rtlx_dispatch); |
| 523 | |
| 524 | rtlx_irq.dev_id = rtlx; |
| 525 | setup_irq(rtlx_irq_num, &rtlx_irq); |
| 526 | |
Ralf Baechle | afc4841 | 2005-10-31 00:30:39 +0000 | [diff] [blame] | 527 | return 0; |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 528 | } |
| 529 | |
Ralf Baechle | afc4841 | 2005-10-31 00:30:39 +0000 | [diff] [blame] | 530 | static void __exit rtlx_module_exit(void) |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 531 | { |
| 532 | unregister_chrdev(major, module_name); |
| 533 | } |
| 534 | |
| 535 | module_init(rtlx_module_init); |
| 536 | module_exit(rtlx_module_exit); |
Ralf Baechle | afc4841 | 2005-10-31 00:30:39 +0000 | [diff] [blame] | 537 | |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 538 | MODULE_DESCRIPTION("MIPS RTLX"); |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 539 | MODULE_AUTHOR("Elizabeth Oldham, MIPS Technologies, Inc."); |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 540 | MODULE_LICENSE("GPL"); |