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