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 | |
Ralf Baechle | bb3d7c7 | 2007-02-07 15:36:56 +0000 | [diff] [blame] | 20 | #include <linux/device.h> |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 21 | #include <linux/kernel.h> |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 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> |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 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> |
Ralf Baechle | a84c96e | 2006-01-15 18:11:28 +0000 | [diff] [blame] | 31 | #include <linux/interrupt.h> |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 32 | #include <linux/poll.h> |
| 33 | #include <linux/sched.h> |
| 34 | #include <linux/wait.h> |
| 35 | #include <asm/mipsmtregs.h> |
Ralf Baechle | bb3d7c7 | 2007-02-07 15:36:56 +0000 | [diff] [blame] | 36 | #include <asm/mips_mt.h> |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 37 | #include <asm/cacheflush.h> |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 38 | #include <linux/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/vpe.h> |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 42 | #include <asm/rtlx.h> |
Yoichi Yuasa | 406b5ee | 2013-05-28 01:23:22 +0000 | [diff] [blame] | 43 | #include <asm/setup.h> |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 44 | |
Ralf Baechle | 982f6ff | 2009-09-17 02:25:07 +0200 | [diff] [blame] | 45 | static int sp_stopping; |
Deng-Cheng Zhu | 2c973ef | 2014-01-01 16:26:46 +0100 | [diff] [blame^] | 46 | struct rtlx_info *rtlx; |
| 47 | struct chan_waitqueues channel_wqs[RTLX_CHANNELS]; |
| 48 | struct vpe_notifications rtlx_notify; |
| 49 | void (*aprp_hook)(void) = NULL; |
| 50 | EXPORT_SYMBOL(aprp_hook); |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 51 | |
David Rientjes | f5dbeaf | 2007-07-22 01:01:39 -0700 | [diff] [blame] | 52 | static void __used dump_rtlx(void) |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 53 | { |
| 54 | int i; |
| 55 | |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 56 | printk("id 0x%lx state %d\n", rtlx->id, rtlx->state); |
| 57 | |
| 58 | for (i = 0; i < RTLX_CHANNELS; i++) { |
| 59 | struct rtlx_channel *chan = &rtlx->channel[i]; |
| 60 | |
| 61 | printk(" rt_state %d lx_state %d buffer_size %d\n", |
| 62 | chan->rt_state, chan->lx_state, chan->buffer_size); |
| 63 | |
| 64 | printk(" rt_read %d rt_write %d\n", |
| 65 | chan->rt_read, chan->rt_write); |
| 66 | |
| 67 | printk(" lx_read %d lx_write %d\n", |
| 68 | chan->lx_read, chan->lx_write); |
| 69 | |
| 70 | printk(" rt_buffer <%s>\n", chan->rt_buffer); |
| 71 | printk(" lx_buffer <%s>\n", chan->lx_buffer); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | /* call when we have the address of the shared structure from the SP side. */ |
| 76 | static int rtlx_init(struct rtlx_info *rtlxi) |
| 77 | { |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 78 | if (rtlxi->id != RTLX_ID) { |
Kevin D. Kissell | 1928cc8 | 2008-04-16 15:32:22 +0200 | [diff] [blame] | 79 | printk(KERN_ERR "no valid RTLX id at 0x%p 0x%lx\n", |
| 80 | rtlxi, rtlxi->id); |
Ralf Baechle | afc4841 | 2005-10-31 00:30:39 +0000 | [diff] [blame] | 81 | return -ENOEXEC; |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 84 | rtlx = rtlxi; |
Ralf Baechle | afc4841 | 2005-10-31 00:30:39 +0000 | [diff] [blame] | 85 | |
| 86 | return 0; |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 89 | /* notifications */ |
Deng-Cheng Zhu | 2c973ef | 2014-01-01 16:26:46 +0100 | [diff] [blame^] | 90 | void rtlx_starting(int vpe) |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 91 | { |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 92 | int i; |
| 93 | sp_stopping = 0; |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 94 | |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 95 | /* force a reload of rtlx */ |
| 96 | rtlx=NULL; |
| 97 | |
| 98 | /* wake up any sleeping rtlx_open's */ |
| 99 | for (i = 0; i < RTLX_CHANNELS; i++) |
| 100 | wake_up_interruptible(&channel_wqs[i].lx_queue); |
| 101 | } |
| 102 | |
Deng-Cheng Zhu | 2c973ef | 2014-01-01 16:26:46 +0100 | [diff] [blame^] | 103 | void rtlx_stopping(int vpe) |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 104 | { |
| 105 | int i; |
| 106 | |
| 107 | sp_stopping = 1; |
| 108 | for (i = 0; i < RTLX_CHANNELS; i++) |
| 109 | wake_up_interruptible(&channel_wqs[i].lx_queue); |
| 110 | } |
| 111 | |
| 112 | |
| 113 | int rtlx_open(int index, int can_sleep) |
| 114 | { |
Ralf Baechle | 9e34682 | 2007-03-15 17:08:28 +0000 | [diff] [blame] | 115 | struct rtlx_info **p; |
Ralf Baechle | 67e2ccc | 2007-02-22 14:19:48 +0000 | [diff] [blame] | 116 | struct rtlx_channel *chan; |
Ralf Baechle | c4c4018 | 2007-02-23 13:40:45 +0000 | [diff] [blame] | 117 | enum rtlx_state state; |
Ralf Baechle | 67e2ccc | 2007-02-22 14:19:48 +0000 | [diff] [blame] | 118 | int ret = 0; |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 119 | |
| 120 | if (index >= RTLX_CHANNELS) { |
| 121 | printk(KERN_DEBUG "rtlx_open index out of range\n"); |
| 122 | return -ENOSYS; |
| 123 | } |
| 124 | |
Ralf Baechle | c4c4018 | 2007-02-23 13:40:45 +0000 | [diff] [blame] | 125 | if (atomic_inc_return(&channel_wqs[index].in_open) > 1) { |
| 126 | printk(KERN_DEBUG "rtlx_open channel %d already opened\n", |
| 127 | index); |
| 128 | ret = -EBUSY; |
| 129 | goto out_fail; |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 130 | } |
| 131 | |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 132 | if (rtlx == NULL) { |
Ralf Baechle | 07cc0c9 | 2007-07-27 19:31:10 +0100 | [diff] [blame] | 133 | if( (p = vpe_get_shared(tclimit)) == NULL) { |
Kevin D. Kissell | 1928cc8 | 2008-04-16 15:32:22 +0200 | [diff] [blame] | 134 | if (can_sleep) { |
Peter Zijlstra | 35a2af9 | 2013-10-02 11:22:33 +0200 | [diff] [blame] | 135 | ret = __wait_event_interruptible( |
| 136 | channel_wqs[index].lx_queue, |
| 137 | (p = vpe_get_shared(tclimit))); |
Kevin D. Kissell | 1928cc8 | 2008-04-16 15:32:22 +0200 | [diff] [blame] | 138 | if (ret) |
Ralf Baechle | 67e2ccc | 2007-02-22 14:19:48 +0000 | [diff] [blame] | 139 | goto out_fail; |
Kevin D. Kissell | 1928cc8 | 2008-04-16 15:32:22 +0200 | [diff] [blame] | 140 | } else { |
| 141 | printk(KERN_DEBUG "No SP program loaded, and device " |
| 142 | "opened with O_NONBLOCK\n"); |
| 143 | ret = -ENOSYS; |
| 144 | goto out_fail; |
| 145 | } |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 146 | } |
| 147 | |
Ralf Baechle | 9e34682 | 2007-03-15 17:08:28 +0000 | [diff] [blame] | 148 | smp_rmb(); |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 149 | if (*p == NULL) { |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 150 | if (can_sleep) { |
Ralf Baechle | 9e34682 | 2007-03-15 17:08:28 +0000 | [diff] [blame] | 151 | DEFINE_WAIT(wait); |
| 152 | |
| 153 | for (;;) { |
Kevin D. Kissell | 1928cc8 | 2008-04-16 15:32:22 +0200 | [diff] [blame] | 154 | prepare_to_wait( |
| 155 | &channel_wqs[index].lx_queue, |
| 156 | &wait, TASK_INTERRUPTIBLE); |
Ralf Baechle | 9e34682 | 2007-03-15 17:08:28 +0000 | [diff] [blame] | 157 | smp_rmb(); |
| 158 | if (*p != NULL) |
| 159 | break; |
| 160 | if (!signal_pending(current)) { |
| 161 | schedule(); |
| 162 | continue; |
| 163 | } |
| 164 | ret = -ERESTARTSYS; |
Ralf Baechle | 67e2ccc | 2007-02-22 14:19:48 +0000 | [diff] [blame] | 165 | goto out_fail; |
Ralf Baechle | 9e34682 | 2007-03-15 17:08:28 +0000 | [diff] [blame] | 166 | } |
| 167 | finish_wait(&channel_wqs[index].lx_queue, &wait); |
Ralf Baechle | 67e2ccc | 2007-02-22 14:19:48 +0000 | [diff] [blame] | 168 | } else { |
Kevin D. Kissell | 1928cc8 | 2008-04-16 15:32:22 +0200 | [diff] [blame] | 169 | pr_err(" *vpe_get_shared is NULL. " |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 170 | "Has an SP program been loaded?\n"); |
Ralf Baechle | 67e2ccc | 2007-02-22 14:19:48 +0000 | [diff] [blame] | 171 | ret = -ENOSYS; |
| 172 | goto out_fail; |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 173 | } |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 176 | if ((unsigned int)*p < KSEG0) { |
Kevin D. Kissell | 1928cc8 | 2008-04-16 15:32:22 +0200 | [diff] [blame] | 177 | printk(KERN_WARNING "vpe_get_shared returned an " |
| 178 | "invalid pointer maybe an error code %d\n", |
| 179 | (int)*p); |
Ralf Baechle | 67e2ccc | 2007-02-22 14:19:48 +0000 | [diff] [blame] | 180 | ret = -ENOSYS; |
| 181 | goto out_fail; |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 182 | } |
| 183 | |
Ralf Baechle | c4c4018 | 2007-02-23 13:40:45 +0000 | [diff] [blame] | 184 | if ((ret = rtlx_init(*p)) < 0) |
| 185 | goto out_ret; |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 186 | } |
| 187 | |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 188 | chan = &rtlx->channel[index]; |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 189 | |
Ralf Baechle | c4c4018 | 2007-02-23 13:40:45 +0000 | [diff] [blame] | 190 | state = xchg(&chan->lx_state, RTLX_STATE_OPENED); |
| 191 | if (state == RTLX_STATE_OPENED) { |
| 192 | ret = -EBUSY; |
| 193 | goto out_fail; |
| 194 | } |
Ralf Baechle | 67e2ccc | 2007-02-22 14:19:48 +0000 | [diff] [blame] | 195 | |
| 196 | out_fail: |
Ralf Baechle | c4c4018 | 2007-02-23 13:40:45 +0000 | [diff] [blame] | 197 | smp_mb(); |
| 198 | atomic_dec(&channel_wqs[index].in_open); |
| 199 | smp_mb(); |
Ralf Baechle | 67e2ccc | 2007-02-22 14:19:48 +0000 | [diff] [blame] | 200 | |
Ralf Baechle | c4c4018 | 2007-02-23 13:40:45 +0000 | [diff] [blame] | 201 | out_ret: |
Ralf Baechle | 67e2ccc | 2007-02-22 14:19:48 +0000 | [diff] [blame] | 202 | return ret; |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 203 | } |
| 204 | |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 205 | int rtlx_release(int index) |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 206 | { |
Kevin D. Kissell | 1928cc8 | 2008-04-16 15:32:22 +0200 | [diff] [blame] | 207 | if (rtlx == NULL) { |
| 208 | pr_err("rtlx_release() with null rtlx\n"); |
| 209 | return 0; |
| 210 | } |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 211 | rtlx->channel[index].lx_state = RTLX_STATE_UNUSED; |
Ralf Baechle | afc4841 | 2005-10-31 00:30:39 +0000 | [diff] [blame] | 212 | return 0; |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 213 | } |
| 214 | |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 215 | unsigned int rtlx_read_poll(int index, int can_sleep) |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 216 | { |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 217 | struct rtlx_channel *chan; |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 218 | |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 219 | if (rtlx == NULL) |
| 220 | return 0; |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 221 | |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 222 | chan = &rtlx->channel[index]; |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 223 | |
| 224 | /* data available to read? */ |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 225 | if (chan->lx_read == chan->lx_write) { |
| 226 | if (can_sleep) { |
Peter Zijlstra | 35a2af9 | 2013-10-02 11:22:33 +0200 | [diff] [blame] | 227 | int ret = __wait_event_interruptible( |
| 228 | channel_wqs[index].lx_queue, |
Kevin D. Kissell | 1928cc8 | 2008-04-16 15:32:22 +0200 | [diff] [blame] | 229 | (chan->lx_read != chan->lx_write) || |
Peter Zijlstra | 35a2af9 | 2013-10-02 11:22:33 +0200 | [diff] [blame] | 230 | sp_stopping); |
Ralf Baechle | 67e2ccc | 2007-02-22 14:19:48 +0000 | [diff] [blame] | 231 | if (ret) |
| 232 | return ret; |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 233 | |
Ralf Baechle | 67e2ccc | 2007-02-22 14:19:48 +0000 | [diff] [blame] | 234 | if (sp_stopping) |
| 235 | return 0; |
| 236 | } else |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 237 | return 0; |
| 238 | } |
| 239 | |
| 240 | return (chan->lx_write + chan->buffer_size - chan->lx_read) |
| 241 | % chan->buffer_size; |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 242 | } |
| 243 | |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 244 | static inline int write_spacefree(int read, int write, int size) |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 245 | { |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 246 | if (read == write) { |
| 247 | /* |
| 248 | * Never fill the buffer completely, so indexes are always |
| 249 | * equal if empty and only empty, or !equal if data available |
| 250 | */ |
| 251 | return size - 1; |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 252 | } |
| 253 | |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 254 | return ((read + size - write) % size) - 1; |
| 255 | } |
| 256 | |
| 257 | unsigned int rtlx_write_poll(int index) |
| 258 | { |
| 259 | struct rtlx_channel *chan = &rtlx->channel[index]; |
Kevin D. Kissell | 1928cc8 | 2008-04-16 15:32:22 +0200 | [diff] [blame] | 260 | |
| 261 | return write_spacefree(chan->rt_read, chan->rt_write, |
| 262 | chan->buffer_size); |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 263 | } |
| 264 | |
Ralf Baechle | 7f5a771 | 2007-04-25 15:08:57 +0100 | [diff] [blame] | 265 | ssize_t rtlx_read(int index, void __user *buff, size_t count) |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 266 | { |
Ralf Baechle | 61dcc6f | 2007-03-15 17:10:16 +0000 | [diff] [blame] | 267 | size_t lx_write, fl = 0L; |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 268 | struct rtlx_channel *lx; |
Ralf Baechle | 46230aa | 2007-03-16 12:16:27 +0000 | [diff] [blame] | 269 | unsigned long failed; |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 270 | |
| 271 | if (rtlx == NULL) |
| 272 | return -ENOSYS; |
| 273 | |
| 274 | lx = &rtlx->channel[index]; |
| 275 | |
Ralf Baechle | bc4809e | 2007-03-15 17:13:47 +0000 | [diff] [blame] | 276 | mutex_lock(&channel_wqs[index].mutex); |
Ralf Baechle | 61dcc6f | 2007-03-15 17:10:16 +0000 | [diff] [blame] | 277 | smp_rmb(); |
| 278 | lx_write = lx->lx_write; |
| 279 | |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 280 | /* find out how much in total */ |
Ralf Baechle | afc4841 | 2005-10-31 00:30:39 +0000 | [diff] [blame] | 281 | count = min(count, |
Ralf Baechle | 61dcc6f | 2007-03-15 17:10:16 +0000 | [diff] [blame] | 282 | (size_t)(lx_write + lx->buffer_size - lx->lx_read) |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 283 | % lx->buffer_size); |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 284 | |
| 285 | /* then how much from the read pointer onwards */ |
Ralf Baechle | 61dcc6f | 2007-03-15 17:10:16 +0000 | [diff] [blame] | 286 | fl = min(count, (size_t)lx->buffer_size - lx->lx_read); |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 287 | |
Ralf Baechle | 46230aa | 2007-03-16 12:16:27 +0000 | [diff] [blame] | 288 | failed = copy_to_user(buff, lx->lx_buffer + lx->lx_read, fl); |
| 289 | if (failed) |
| 290 | goto out; |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 291 | |
| 292 | /* and if there is anything left at the beginning of the buffer */ |
Ralf Baechle | 61dcc6f | 2007-03-15 17:10:16 +0000 | [diff] [blame] | 293 | if (count - fl) |
Ralf Baechle | 46230aa | 2007-03-16 12:16:27 +0000 | [diff] [blame] | 294 | failed = copy_to_user(buff + fl, lx->lx_buffer, count - fl); |
| 295 | |
| 296 | out: |
| 297 | count -= failed; |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 298 | |
Ralf Baechle | 61dcc6f | 2007-03-15 17:10:16 +0000 | [diff] [blame] | 299 | smp_wmb(); |
| 300 | lx->lx_read = (lx->lx_read + count) % lx->buffer_size; |
| 301 | smp_wmb(); |
Ralf Baechle | bc4809e | 2007-03-15 17:13:47 +0000 | [diff] [blame] | 302 | mutex_unlock(&channel_wqs[index].mutex); |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 303 | |
Ralf Baechle | afc4841 | 2005-10-31 00:30:39 +0000 | [diff] [blame] | 304 | return count; |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 305 | } |
| 306 | |
Ralf Baechle | 7f5a771 | 2007-04-25 15:08:57 +0100 | [diff] [blame] | 307 | ssize_t rtlx_write(int index, const void __user *buffer, size_t count) |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 308 | { |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 309 | struct rtlx_channel *rt; |
Ralf Baechle | 7f5a771 | 2007-04-25 15:08:57 +0100 | [diff] [blame] | 310 | unsigned long failed; |
Ralf Baechle | 61dcc6f | 2007-03-15 17:10:16 +0000 | [diff] [blame] | 311 | size_t rt_read; |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 312 | size_t fl; |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 313 | |
| 314 | if (rtlx == NULL) |
| 315 | return(-ENOSYS); |
| 316 | |
| 317 | rt = &rtlx->channel[index]; |
| 318 | |
Ralf Baechle | bc4809e | 2007-03-15 17:13:47 +0000 | [diff] [blame] | 319 | mutex_lock(&channel_wqs[index].mutex); |
Ralf Baechle | 61dcc6f | 2007-03-15 17:10:16 +0000 | [diff] [blame] | 320 | smp_rmb(); |
| 321 | rt_read = rt->rt_read; |
| 322 | |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 323 | /* total number of bytes to copy */ |
Kevin D. Kissell | 1928cc8 | 2008-04-16 15:32:22 +0200 | [diff] [blame] | 324 | count = min(count, (size_t)write_spacefree(rt_read, rt->rt_write, |
| 325 | rt->buffer_size)); |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 326 | |
| 327 | /* first bit from write pointer to the end of the buffer, or count */ |
| 328 | fl = min(count, (size_t) rt->buffer_size - rt->rt_write); |
| 329 | |
Ralf Baechle | 46230aa | 2007-03-16 12:16:27 +0000 | [diff] [blame] | 330 | failed = copy_from_user(rt->rt_buffer + rt->rt_write, buffer, fl); |
| 331 | if (failed) |
| 332 | goto out; |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 333 | |
| 334 | /* if there's any left copy to the beginning of the buffer */ |
Ralf Baechle | 46230aa | 2007-03-16 12:16:27 +0000 | [diff] [blame] | 335 | if (count - fl) { |
| 336 | failed = copy_from_user(rt->rt_buffer, buffer + fl, count - fl); |
| 337 | } |
| 338 | |
| 339 | out: |
Ralf Baechle | 7f5a771 | 2007-04-25 15:08:57 +0100 | [diff] [blame] | 340 | count -= failed; |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 341 | |
Ralf Baechle | 61dcc6f | 2007-03-15 17:10:16 +0000 | [diff] [blame] | 342 | smp_wmb(); |
| 343 | rt->rt_write = (rt->rt_write + count) % rt->buffer_size; |
| 344 | smp_wmb(); |
Ralf Baechle | bc4809e | 2007-03-15 17:13:47 +0000 | [diff] [blame] | 345 | mutex_unlock(&channel_wqs[index].mutex); |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 346 | |
Deng-Cheng Zhu | 2c973ef | 2014-01-01 16:26:46 +0100 | [diff] [blame^] | 347 | _interrupt_sp(); |
| 348 | |
Ralf Baechle | 61dcc6f | 2007-03-15 17:10:16 +0000 | [diff] [blame] | 349 | return count; |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | |
| 353 | static int file_open(struct inode *inode, struct file *filp) |
| 354 | { |
Ralf Baechle | 1bbfc20 | 2009-09-29 00:52:27 +0100 | [diff] [blame] | 355 | return rtlx_open(iminor(inode), (filp->f_flags & O_NONBLOCK) ? 0 : 1); |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | static int file_release(struct inode *inode, struct file *filp) |
| 359 | { |
Ralf Baechle | 1bbfc20 | 2009-09-29 00:52:27 +0100 | [diff] [blame] | 360 | return rtlx_release(iminor(inode)); |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | static unsigned int file_poll(struct file *file, poll_table * wait) |
| 364 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 365 | int minor = iminor(file_inode(file)); |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 366 | unsigned int mask = 0; |
| 367 | |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 368 | poll_wait(file, &channel_wqs[minor].rt_queue, wait); |
| 369 | poll_wait(file, &channel_wqs[minor].lx_queue, wait); |
| 370 | |
| 371 | if (rtlx == NULL) |
| 372 | return 0; |
| 373 | |
| 374 | /* data available to read? */ |
| 375 | if (rtlx_read_poll(minor, 0)) |
| 376 | mask |= POLLIN | POLLRDNORM; |
| 377 | |
| 378 | /* space to write */ |
| 379 | if (rtlx_write_poll(minor)) |
| 380 | mask |= POLLOUT | POLLWRNORM; |
| 381 | |
| 382 | return mask; |
| 383 | } |
| 384 | |
| 385 | static ssize_t file_read(struct file *file, char __user * buffer, size_t count, |
| 386 | loff_t * ppos) |
| 387 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 388 | int minor = iminor(file_inode(file)); |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 389 | |
| 390 | /* data available? */ |
| 391 | if (!rtlx_read_poll(minor, (file->f_flags & O_NONBLOCK) ? 0 : 1)) { |
| 392 | return 0; // -EAGAIN makes cat whinge |
| 393 | } |
| 394 | |
Ralf Baechle | 46230aa | 2007-03-16 12:16:27 +0000 | [diff] [blame] | 395 | return rtlx_read(minor, buffer, count); |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | static ssize_t file_write(struct file *file, const char __user * buffer, |
| 399 | size_t count, loff_t * ppos) |
| 400 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 401 | int minor = iminor(file_inode(file)); |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 402 | |
| 403 | /* any space left... */ |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 404 | if (!rtlx_write_poll(minor)) { |
Peter Zijlstra | 35a2af9 | 2013-10-02 11:22:33 +0200 | [diff] [blame] | 405 | int ret; |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 406 | |
| 407 | if (file->f_flags & O_NONBLOCK) |
Ralf Baechle | afc4841 | 2005-10-31 00:30:39 +0000 | [diff] [blame] | 408 | return -EAGAIN; |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 409 | |
Peter Zijlstra | 35a2af9 | 2013-10-02 11:22:33 +0200 | [diff] [blame] | 410 | ret = __wait_event_interruptible(channel_wqs[minor].rt_queue, |
| 411 | rtlx_write_poll(minor)); |
Ralf Baechle | 67e2ccc | 2007-02-22 14:19:48 +0000 | [diff] [blame] | 412 | if (ret) |
| 413 | return ret; |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 414 | } |
| 415 | |
Ralf Baechle | 46230aa | 2007-03-16 12:16:27 +0000 | [diff] [blame] | 416 | return rtlx_write(minor, buffer, count); |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 417 | } |
| 418 | |
Deng-Cheng Zhu | 2c973ef | 2014-01-01 16:26:46 +0100 | [diff] [blame^] | 419 | const struct file_operations rtlx_fops = { |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 420 | .owner = THIS_MODULE, |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 421 | .open = file_open, |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 422 | .release = file_release, |
| 423 | .write = file_write, |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 424 | .read = file_read, |
| 425 | .poll = file_poll, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 426 | .llseek = noop_llseek, |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 427 | }; |
| 428 | |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 429 | module_init(rtlx_module_init); |
| 430 | module_exit(rtlx_module_exit); |
Ralf Baechle | afc4841 | 2005-10-31 00:30:39 +0000 | [diff] [blame] | 431 | |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 432 | MODULE_DESCRIPTION("MIPS RTLX"); |
Ralf Baechle | 2600990 | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 433 | MODULE_AUTHOR("Elizabeth Oldham, MIPS Technologies, Inc."); |
Ralf Baechle | e01402b | 2005-07-14 15:57:16 +0000 | [diff] [blame] | 434 | MODULE_LICENSE("GPL"); |