Merge tag 'tty-4.8-rc5' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/tty
[deliverable/linux.git] / arch / mips / kernel / rtlx.c
CommitLineData
e01402b1 1/*
5792bf64
SH
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 *
e01402b1 6 * Copyright (C) 2005 MIPS Technologies, Inc. All rights reserved.
a84c96e2 7 * Copyright (C) 2005, 06 Ralf Baechle (ralf@linux-mips.org)
5792bf64 8 * Copyright (C) 2013 Imagination Technologies Ltd.
e01402b1 9 */
e01402b1 10#include <linux/kernel.h>
e01402b1 11#include <linux/fs.h>
2600990e
RB
12#include <linux/syscalls.h>
13#include <linux/moduleloader.h>
5792bf64 14#include <linux/atomic.h>
e01402b1 15#include <asm/mipsmtregs.h>
bb3d7c7f 16#include <asm/mips_mt.h>
e01402b1 17#include <asm/processor.h>
e01402b1 18#include <asm/rtlx.h>
406b5ee2 19#include <asm/setup.h>
5792bf64 20#include <asm/vpe.h>
e01402b1 21
982f6ffe 22static int sp_stopping;
2c973ef0
DCZ
23struct rtlx_info *rtlx;
24struct chan_waitqueues channel_wqs[RTLX_CHANNELS];
25struct vpe_notifications rtlx_notify;
26void (*aprp_hook)(void) = NULL;
27EXPORT_SYMBOL(aprp_hook);
e01402b1 28
f5dbeaf5 29static void __used dump_rtlx(void)
e01402b1
RB
30{
31 int i;
32
5792bf64 33 pr_info("id 0x%lx state %d\n", rtlx->id, rtlx->state);
e01402b1 34
e01402b1 35 for (i = 0; i < RTLX_CHANNELS; i++) {
2600990e 36 struct rtlx_channel *chan = &rtlx->channel[i];
e01402b1 37
5792bf64
SH
38 pr_info(" rt_state %d lx_state %d buffer_size %d\n",
39 chan->rt_state, chan->lx_state, chan->buffer_size);
e01402b1 40
5792bf64
SH
41 pr_info(" rt_read %d rt_write %d\n",
42 chan->rt_read, chan->rt_write);
e01402b1 43
5792bf64
SH
44 pr_info(" lx_read %d lx_write %d\n",
45 chan->lx_read, chan->lx_write);
2600990e 46
5792bf64
SH
47 pr_info(" rt_buffer <%s>\n", chan->rt_buffer);
48 pr_info(" lx_buffer <%s>\n", chan->lx_buffer);
2600990e
RB
49 }
50}
51
52/* call when we have the address of the shared structure from the SP side. */
53static int rtlx_init(struct rtlx_info *rtlxi)
54{
55 if (rtlxi->id != RTLX_ID) {
5792bf64 56 pr_err("no valid RTLX id at 0x%p 0x%lx\n", rtlxi, rtlxi->id);
2600990e
RB
57 return -ENOEXEC;
58 }
e01402b1
RB
59
60 rtlx = rtlxi;
afc4841d
RB
61
62 return 0;
e01402b1
RB
63}
64
2600990e 65/* notifications */
2c973ef0 66void rtlx_starting(int vpe)
e01402b1 67{
2600990e
RB
68 int i;
69 sp_stopping = 0;
70
71 /* force a reload of rtlx */
5792bf64 72 rtlx = NULL;
2600990e
RB
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
2c973ef0 79void rtlx_stopping(int vpe)
2600990e
RB
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
89int rtlx_open(int index, int can_sleep)
90{
9e346820 91 struct rtlx_info **p;
67e2ccce 92 struct rtlx_channel *chan;
c4c4018b 93 enum rtlx_state state;
67e2ccce 94 int ret = 0;
e01402b1 95
2600990e 96 if (index >= RTLX_CHANNELS) {
3dc4bf31 97 pr_debug("rtlx_open index out of range\n");
2600990e
RB
98 return -ENOSYS;
99 }
100
c4c4018b 101 if (atomic_inc_return(&channel_wqs[index].in_open) > 1) {
3dc4bf31 102 pr_debug("rtlx_open channel %d already opened\n", index);
c4c4018b
RB
103 ret = -EBUSY;
104 goto out_fail;
2600990e
RB
105 }
106
e01402b1 107 if (rtlx == NULL) {
5792bf64
SH
108 p = vpe_get_shared(aprp_cpu_index());
109 if (p == NULL) {
110 if (can_sleep) {
111 ret = __wait_event_interruptible(
35a2af94 112 channel_wqs[index].lx_queue,
5792bf64
SH
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;
67e2ccce 119 goto out_fail;
5792bf64 120 }
e01402b1
RB
121 }
122
9e346820 123 smp_rmb();
e01402b1 124 if (*p == NULL) {
2600990e 125 if (can_sleep) {
9e346820
RB
126 DEFINE_WAIT(wait);
127
128 for (;;) {
1928cc84
KK
129 prepare_to_wait(
130 &channel_wqs[index].lx_queue,
131 &wait, TASK_INTERRUPTIBLE);
9e346820
RB
132 smp_rmb();
133 if (*p != NULL)
134 break;
135 if (!signal_pending(current)) {
136 schedule();
137 continue;
138 }
139 ret = -ERESTARTSYS;
67e2ccce 140 goto out_fail;
9e346820 141 }
5792bf64
SH
142 finish_wait(&channel_wqs[index].lx_queue,
143 &wait);
67e2ccce 144 } else {
5792bf64 145 pr_err(" *vpe_get_shared is NULL. Has an SP program been loaded?\n");
67e2ccce
RB
146 ret = -ENOSYS;
147 goto out_fail;
2600990e
RB
148 }
149 }
150
151 if ((unsigned int)*p < KSEG0) {
5792bf64
SH
152 pr_warn("vpe_get_shared returned an invalid pointer maybe an error code %d\n",
153 (int)*p);
67e2ccce
RB
154 ret = -ENOSYS;
155 goto out_fail;
e01402b1
RB
156 }
157
5792bf64
SH
158 ret = rtlx_init(*p);
159 if (ret < 0)
c4c4018b 160 goto out_ret;
e01402b1
RB
161 }
162
2600990e 163 chan = &rtlx->channel[index];
e01402b1 164
c4c4018b
RB
165 state = xchg(&chan->lx_state, RTLX_STATE_OPENED);
166 if (state == RTLX_STATE_OPENED) {
167 ret = -EBUSY;
168 goto out_fail;
169 }
67e2ccce
RB
170
171out_fail:
c4c4018b
RB
172 smp_mb();
173 atomic_dec(&channel_wqs[index].in_open);
174 smp_mb();
67e2ccce 175
c4c4018b 176out_ret:
67e2ccce 177 return ret;
e01402b1
RB
178}
179
2600990e 180int rtlx_release(int index)
e01402b1 181{
1928cc84
KK
182 if (rtlx == NULL) {
183 pr_err("rtlx_release() with null rtlx\n");
184 return 0;
185 }
2600990e 186 rtlx->channel[index].lx_state = RTLX_STATE_UNUSED;
afc4841d 187 return 0;
e01402b1
RB
188}
189
2600990e 190unsigned int rtlx_read_poll(int index, int can_sleep)
e01402b1 191{
70342287 192 struct rtlx_channel *chan;
e01402b1 193
70342287
RB
194 if (rtlx == NULL)
195 return 0;
e01402b1 196
70342287 197 chan = &rtlx->channel[index];
e01402b1
RB
198
199 /* data available to read? */
2600990e
RB
200 if (chan->lx_read == chan->lx_write) {
201 if (can_sleep) {
35a2af94
PZ
202 int ret = __wait_event_interruptible(
203 channel_wqs[index].lx_queue,
1928cc84 204 (chan->lx_read != chan->lx_write) ||
35a2af94 205 sp_stopping);
67e2ccce
RB
206 if (ret)
207 return ret;
e01402b1 208
67e2ccce
RB
209 if (sp_stopping)
210 return 0;
211 } else
2600990e
RB
212 return 0;
213 }
214
215 return (chan->lx_write + chan->buffer_size - chan->lx_read)
216 % chan->buffer_size;
e01402b1
RB
217}
218
2600990e 219static inline int write_spacefree(int read, int write, int size)
e01402b1 220{
2600990e
RB
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;
227 }
e01402b1 228
2600990e
RB
229 return ((read + size - write) % size) - 1;
230}
e01402b1 231
2600990e
RB
232unsigned int rtlx_write_poll(int index)
233{
234 struct rtlx_channel *chan = &rtlx->channel[index];
1928cc84
KK
235
236 return write_spacefree(chan->rt_read, chan->rt_write,
237 chan->buffer_size);
2600990e 238}
e01402b1 239
7f5a7716 240ssize_t rtlx_read(int index, void __user *buff, size_t count)
2600990e 241{
61dcc6f4 242 size_t lx_write, fl = 0L;
2600990e 243 struct rtlx_channel *lx;
46230aa6 244 unsigned long failed;
e01402b1 245
2600990e
RB
246 if (rtlx == NULL)
247 return -ENOSYS;
248
249 lx = &rtlx->channel[index];
e01402b1 250
bc4809e9 251 mutex_lock(&channel_wqs[index].mutex);
61dcc6f4
RB
252 smp_rmb();
253 lx_write = lx->lx_write;
254
e01402b1 255 /* find out how much in total */
afc4841d 256 count = min(count,
61dcc6f4 257 (size_t)(lx_write + lx->buffer_size - lx->lx_read)
2600990e 258 % lx->buffer_size);
e01402b1
RB
259
260 /* then how much from the read pointer onwards */
61dcc6f4 261 fl = min(count, (size_t)lx->buffer_size - lx->lx_read);
e01402b1 262
46230aa6
RB
263 failed = copy_to_user(buff, lx->lx_buffer + lx->lx_read, fl);
264 if (failed)
265 goto out;
e01402b1
RB
266
267 /* and if there is anything left at the beginning of the buffer */
61dcc6f4 268 if (count - fl)
46230aa6
RB
269 failed = copy_to_user(buff + fl, lx->lx_buffer, count - fl);
270
271out:
272 count -= failed;
e01402b1 273
61dcc6f4
RB
274 smp_wmb();
275 lx->lx_read = (lx->lx_read + count) % lx->buffer_size;
276 smp_wmb();
bc4809e9 277 mutex_unlock(&channel_wqs[index].mutex);
e01402b1 278
afc4841d 279 return count;
e01402b1
RB
280}
281
7f5a7716 282ssize_t rtlx_write(int index, const void __user *buffer, size_t count)
2600990e
RB
283{
284 struct rtlx_channel *rt;
7f5a7716 285 unsigned long failed;
61dcc6f4 286 size_t rt_read;
2600990e
RB
287 size_t fl;
288
289 if (rtlx == NULL)
5792bf64 290 return -ENOSYS;
2600990e
RB
291
292 rt = &rtlx->channel[index];
293
bc4809e9 294 mutex_lock(&channel_wqs[index].mutex);
61dcc6f4
RB
295 smp_rmb();
296 rt_read = rt->rt_read;
297
2600990e 298 /* total number of bytes to copy */
5792bf64
SH
299 count = min_t(size_t, count, write_spacefree(rt_read, rt->rt_write,
300 rt->buffer_size));
2600990e
RB
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
46230aa6
RB
305 failed = copy_from_user(rt->rt_buffer + rt->rt_write, buffer, fl);
306 if (failed)
307 goto out;
2600990e
RB
308
309 /* if there's any left copy to the beginning of the buffer */
5792bf64 310 if (count - fl)
46230aa6 311 failed = copy_from_user(rt->rt_buffer, buffer + fl, count - fl);
46230aa6
RB
312
313out:
7f5a7716 314 count -= failed;
2600990e 315
61dcc6f4
RB
316 smp_wmb();
317 rt->rt_write = (rt->rt_write + count) % rt->buffer_size;
318 smp_wmb();
bc4809e9 319 mutex_unlock(&channel_wqs[index].mutex);
2600990e 320
2c973ef0
DCZ
321 _interrupt_sp();
322
61dcc6f4 323 return count;
2600990e
RB
324}
325
326
327static int file_open(struct inode *inode, struct file *filp)
328{
1bbfc20d 329 return rtlx_open(iminor(inode), (filp->f_flags & O_NONBLOCK) ? 0 : 1);
2600990e
RB
330}
331
332static int file_release(struct inode *inode, struct file *filp)
333{
1bbfc20d 334 return rtlx_release(iminor(inode));
2600990e
RB
335}
336
5792bf64 337static unsigned int file_poll(struct file *file, poll_table *wait)
2600990e 338{
496ad9aa 339 int minor = iminor(file_inode(file));
2600990e
RB
340 unsigned int mask = 0;
341
2600990e
RB
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
5792bf64
SH
359static ssize_t file_read(struct file *file, char __user *buffer, size_t count,
360 loff_t *ppos)
2600990e 361{
496ad9aa 362 int minor = iminor(file_inode(file));
2600990e
RB
363
364 /* data available? */
5792bf64
SH
365 if (!rtlx_read_poll(minor, (file->f_flags & O_NONBLOCK) ? 0 : 1))
366 return 0; /* -EAGAIN makes 'cat' whine */
2600990e 367
46230aa6 368 return rtlx_read(minor, buffer, count);
2600990e
RB
369}
370
5792bf64
SH
371static ssize_t file_write(struct file *file, const char __user *buffer,
372 size_t count, loff_t *ppos)
e01402b1 373{
496ad9aa 374 int minor = iminor(file_inode(file));
e01402b1
RB
375
376 /* any space left... */
2600990e 377 if (!rtlx_write_poll(minor)) {
35a2af94 378 int ret;
e01402b1
RB
379
380 if (file->f_flags & O_NONBLOCK)
afc4841d 381 return -EAGAIN;
e01402b1 382
35a2af94
PZ
383 ret = __wait_event_interruptible(channel_wqs[minor].rt_queue,
384 rtlx_write_poll(minor));
67e2ccce
RB
385 if (ret)
386 return ret;
e01402b1
RB
387 }
388
46230aa6 389 return rtlx_write(minor, buffer, count);
e01402b1
RB
390}
391
2c973ef0 392const struct file_operations rtlx_fops = {
2600990e 393 .owner = THIS_MODULE,
5792bf64 394 .open = file_open,
2600990e
RB
395 .release = file_release,
396 .write = file_write,
5792bf64
SH
397 .read = file_read,
398 .poll = file_poll,
6038f373 399 .llseek = noop_llseek,
e01402b1
RB
400};
401
e01402b1
RB
402module_init(rtlx_module_init);
403module_exit(rtlx_module_exit);
afc4841d 404
e01402b1 405MODULE_DESCRIPTION("MIPS RTLX");
2600990e 406MODULE_AUTHOR("Elizabeth Oldham, MIPS Technologies, Inc.");
e01402b1 407MODULE_LICENSE("GPL");
This page took 0.701192 seconds and 5 git commands to generate.