Merge branch 'topic/vsp1' into patchwork
[deliverable/linux.git] / drivers / staging / media / cec / cec-api.c
1 /*
2 * cec-api.c - HDMI Consumer Electronics Control framework - API
3 *
4 * Copyright 2016 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
5 *
6 * This program is free software; you may redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
11 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
12 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
13 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
14 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
15 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
16 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17 * SOFTWARE.
18 */
19
20 #include <linux/errno.h>
21 #include <linux/init.h>
22 #include <linux/module.h>
23 #include <linux/kernel.h>
24 #include <linux/kmod.h>
25 #include <linux/ktime.h>
26 #include <linux/slab.h>
27 #include <linux/mm.h>
28 #include <linux/string.h>
29 #include <linux/types.h>
30 #include <linux/uaccess.h>
31 #include <linux/version.h>
32
33 #include "cec-priv.h"
34
35 static inline struct cec_devnode *cec_devnode_data(struct file *filp)
36 {
37 struct cec_fh *fh = filp->private_data;
38
39 return &fh->adap->devnode;
40 }
41
42 /* CEC file operations */
43
44 static unsigned int cec_poll(struct file *filp,
45 struct poll_table_struct *poll)
46 {
47 struct cec_devnode *devnode = cec_devnode_data(filp);
48 struct cec_fh *fh = filp->private_data;
49 struct cec_adapter *adap = fh->adap;
50 unsigned int res = 0;
51
52 if (!devnode->registered)
53 return POLLERR | POLLHUP;
54 mutex_lock(&adap->lock);
55 if (adap->is_configured)
56 res |= POLLOUT | POLLWRNORM;
57 if (fh->queued_msgs)
58 res |= POLLIN | POLLRDNORM;
59 if (fh->pending_events)
60 res |= POLLPRI;
61 poll_wait(filp, &fh->wait, poll);
62 mutex_unlock(&adap->lock);
63 return res;
64 }
65
66 static bool cec_is_busy(const struct cec_adapter *adap,
67 const struct cec_fh *fh)
68 {
69 bool valid_initiator = adap->cec_initiator && adap->cec_initiator == fh;
70 bool valid_follower = adap->cec_follower && adap->cec_follower == fh;
71
72 /*
73 * Exclusive initiators and followers can always access the CEC adapter
74 */
75 if (valid_initiator || valid_follower)
76 return false;
77 /*
78 * All others can only access the CEC adapter if there is no
79 * exclusive initiator and they are in INITIATOR mode.
80 */
81 return adap->cec_initiator ||
82 fh->mode_initiator == CEC_MODE_NO_INITIATOR;
83 }
84
85 static long cec_adap_g_caps(struct cec_adapter *adap,
86 struct cec_caps __user *parg)
87 {
88 struct cec_caps caps = {};
89
90 strlcpy(caps.driver, adap->devnode.parent->driver->name,
91 sizeof(caps.driver));
92 strlcpy(caps.name, adap->name, sizeof(caps.name));
93 caps.available_log_addrs = adap->available_log_addrs;
94 caps.capabilities = adap->capabilities;
95 caps.version = LINUX_VERSION_CODE;
96 if (copy_to_user(parg, &caps, sizeof(caps)))
97 return -EFAULT;
98 return 0;
99 }
100
101 static long cec_adap_g_phys_addr(struct cec_adapter *adap,
102 __u16 __user *parg)
103 {
104 u16 phys_addr;
105
106 mutex_lock(&adap->lock);
107 phys_addr = adap->phys_addr;
108 mutex_unlock(&adap->lock);
109 if (copy_to_user(parg, &phys_addr, sizeof(phys_addr)))
110 return -EFAULT;
111 return 0;
112 }
113
114 static long cec_adap_s_phys_addr(struct cec_adapter *adap, struct cec_fh *fh,
115 bool block, __u16 __user *parg)
116 {
117 u16 phys_addr;
118 long err;
119
120 if (!(adap->capabilities & CEC_CAP_PHYS_ADDR))
121 return -ENOTTY;
122 if (copy_from_user(&phys_addr, parg, sizeof(phys_addr)))
123 return -EFAULT;
124
125 err = cec_phys_addr_validate(phys_addr, NULL, NULL);
126 if (err)
127 return err;
128 mutex_lock(&adap->lock);
129 if (cec_is_busy(adap, fh))
130 err = -EBUSY;
131 else
132 __cec_s_phys_addr(adap, phys_addr, block);
133 mutex_unlock(&adap->lock);
134 return err;
135 }
136
137 static long cec_adap_g_log_addrs(struct cec_adapter *adap,
138 struct cec_log_addrs __user *parg)
139 {
140 struct cec_log_addrs log_addrs;
141
142 mutex_lock(&adap->lock);
143 log_addrs = adap->log_addrs;
144 if (!adap->is_configured)
145 memset(log_addrs.log_addr, CEC_LOG_ADDR_INVALID,
146 sizeof(log_addrs.log_addr));
147 mutex_unlock(&adap->lock);
148
149 if (copy_to_user(parg, &log_addrs, sizeof(log_addrs)))
150 return -EFAULT;
151 return 0;
152 }
153
154 static long cec_adap_s_log_addrs(struct cec_adapter *adap, struct cec_fh *fh,
155 bool block, struct cec_log_addrs __user *parg)
156 {
157 struct cec_log_addrs log_addrs;
158 long err = -EBUSY;
159
160 if (!(adap->capabilities & CEC_CAP_LOG_ADDRS))
161 return -ENOTTY;
162 if (copy_from_user(&log_addrs, parg, sizeof(log_addrs)))
163 return -EFAULT;
164 log_addrs.flags = 0;
165 mutex_lock(&adap->lock);
166 if (!adap->is_configuring &&
167 (!log_addrs.num_log_addrs || !adap->is_configured) &&
168 !cec_is_busy(adap, fh)) {
169 err = __cec_s_log_addrs(adap, &log_addrs, block);
170 if (!err)
171 log_addrs = adap->log_addrs;
172 }
173 mutex_unlock(&adap->lock);
174 if (err)
175 return err;
176 if (copy_to_user(parg, &log_addrs, sizeof(log_addrs)))
177 return -EFAULT;
178 return 0;
179 }
180
181 static long cec_transmit(struct cec_adapter *adap, struct cec_fh *fh,
182 bool block, struct cec_msg __user *parg)
183 {
184 struct cec_msg msg = {};
185 long err = 0;
186
187 if (!(adap->capabilities & CEC_CAP_TRANSMIT))
188 return -ENOTTY;
189 if (copy_from_user(&msg, parg, sizeof(msg)))
190 return -EFAULT;
191 mutex_lock(&adap->lock);
192 if (!adap->is_configured) {
193 err = -ENONET;
194 } else if (cec_is_busy(adap, fh)) {
195 err = -EBUSY;
196 } else {
197 if (!block || !msg.reply)
198 fh = NULL;
199 err = cec_transmit_msg_fh(adap, &msg, fh, block);
200 }
201 mutex_unlock(&adap->lock);
202 if (err)
203 return err;
204 if (copy_to_user(parg, &msg, sizeof(msg)))
205 return -EFAULT;
206 return 0;
207 }
208
209 /* Called by CEC_RECEIVE: wait for a message to arrive */
210 static int cec_receive_msg(struct cec_fh *fh, struct cec_msg *msg, bool block)
211 {
212 int res;
213
214 do {
215 mutex_lock(&fh->lock);
216 /* Are there received messages queued up? */
217 if (fh->queued_msgs) {
218 /* Yes, return the first one */
219 struct cec_msg_entry *entry =
220 list_first_entry(&fh->msgs,
221 struct cec_msg_entry, list);
222
223 list_del(&entry->list);
224 *msg = entry->msg;
225 kfree(entry);
226 fh->queued_msgs--;
227 mutex_unlock(&fh->lock);
228 return 0;
229 }
230
231 /* No, return EAGAIN in non-blocking mode or wait */
232 mutex_unlock(&fh->lock);
233
234 /* Return when in non-blocking mode */
235 if (!block)
236 return -EAGAIN;
237
238 if (msg->timeout) {
239 /* The user specified a timeout */
240 res = wait_event_interruptible_timeout(fh->wait,
241 fh->queued_msgs,
242 msecs_to_jiffies(msg->timeout));
243 if (res == 0)
244 res = -ETIMEDOUT;
245 else if (res > 0)
246 res = 0;
247 } else {
248 /* Wait indefinitely */
249 res = wait_event_interruptible(fh->wait,
250 fh->queued_msgs);
251 }
252 /* Exit on error, otherwise loop to get the new message */
253 } while (!res);
254 return res;
255 }
256
257 static long cec_receive(struct cec_adapter *adap, struct cec_fh *fh,
258 bool block, struct cec_msg __user *parg)
259 {
260 struct cec_msg msg = {};
261 long err = 0;
262
263 if (copy_from_user(&msg, parg, sizeof(msg)))
264 return -EFAULT;
265 mutex_lock(&adap->lock);
266 if (!adap->is_configured && fh->mode_follower < CEC_MODE_MONITOR)
267 err = -ENONET;
268 mutex_unlock(&adap->lock);
269 if (err)
270 return err;
271
272 err = cec_receive_msg(fh, &msg, block);
273 if (err)
274 return err;
275 if (copy_to_user(parg, &msg, sizeof(msg)))
276 return -EFAULT;
277 return 0;
278 }
279
280 static long cec_dqevent(struct cec_adapter *adap, struct cec_fh *fh,
281 bool block, struct cec_event __user *parg)
282 {
283 struct cec_event *ev = NULL;
284 u64 ts = ~0ULL;
285 unsigned int i;
286 long err = 0;
287
288 mutex_lock(&fh->lock);
289 while (!fh->pending_events && block) {
290 mutex_unlock(&fh->lock);
291 err = wait_event_interruptible(fh->wait, fh->pending_events);
292 if (err)
293 return err;
294 mutex_lock(&fh->lock);
295 }
296
297 /* Find the oldest event */
298 for (i = 0; i < CEC_NUM_EVENTS; i++) {
299 if (fh->pending_events & (1 << (i + 1)) &&
300 fh->events[i].ts <= ts) {
301 ev = &fh->events[i];
302 ts = ev->ts;
303 }
304 }
305 if (!ev) {
306 err = -EAGAIN;
307 goto unlock;
308 }
309
310 if (copy_to_user(parg, ev, sizeof(*ev))) {
311 err = -EFAULT;
312 goto unlock;
313 }
314
315 fh->pending_events &= ~(1 << ev->event);
316
317 unlock:
318 mutex_unlock(&fh->lock);
319 return err;
320 }
321
322 static long cec_g_mode(struct cec_adapter *adap, struct cec_fh *fh,
323 u32 __user *parg)
324 {
325 u32 mode = fh->mode_initiator | fh->mode_follower;
326
327 if (copy_to_user(parg, &mode, sizeof(mode)))
328 return -EFAULT;
329 return 0;
330 }
331
332 static long cec_s_mode(struct cec_adapter *adap, struct cec_fh *fh,
333 u32 __user *parg)
334 {
335 u32 mode;
336 u8 mode_initiator;
337 u8 mode_follower;
338 long err = 0;
339
340 if (copy_from_user(&mode, parg, sizeof(mode)))
341 return -EFAULT;
342 if (mode & ~(CEC_MODE_INITIATOR_MSK | CEC_MODE_FOLLOWER_MSK))
343 return -EINVAL;
344
345 mode_initiator = mode & CEC_MODE_INITIATOR_MSK;
346 mode_follower = mode & CEC_MODE_FOLLOWER_MSK;
347
348 if (mode_initiator > CEC_MODE_EXCL_INITIATOR ||
349 mode_follower > CEC_MODE_MONITOR_ALL)
350 return -EINVAL;
351
352 if (mode_follower == CEC_MODE_MONITOR_ALL &&
353 !(adap->capabilities & CEC_CAP_MONITOR_ALL))
354 return -EINVAL;
355
356 /* Follower modes should always be able to send CEC messages */
357 if ((mode_initiator == CEC_MODE_NO_INITIATOR ||
358 !(adap->capabilities & CEC_CAP_TRANSMIT)) &&
359 mode_follower >= CEC_MODE_FOLLOWER &&
360 mode_follower <= CEC_MODE_EXCL_FOLLOWER_PASSTHRU)
361 return -EINVAL;
362
363 /* Monitor modes require CEC_MODE_NO_INITIATOR */
364 if (mode_initiator && mode_follower >= CEC_MODE_MONITOR)
365 return -EINVAL;
366
367 /* Monitor modes require CAP_NET_ADMIN */
368 if (mode_follower >= CEC_MODE_MONITOR && !capable(CAP_NET_ADMIN))
369 return -EPERM;
370
371 mutex_lock(&adap->lock);
372 /*
373 * You can't become exclusive follower if someone else already
374 * has that job.
375 */
376 if ((mode_follower == CEC_MODE_EXCL_FOLLOWER ||
377 mode_follower == CEC_MODE_EXCL_FOLLOWER_PASSTHRU) &&
378 adap->cec_follower && adap->cec_follower != fh)
379 err = -EBUSY;
380 /*
381 * You can't become exclusive initiator if someone else already
382 * has that job.
383 */
384 if (mode_initiator == CEC_MODE_EXCL_INITIATOR &&
385 adap->cec_initiator && adap->cec_initiator != fh)
386 err = -EBUSY;
387
388 if (!err) {
389 bool old_mon_all = fh->mode_follower == CEC_MODE_MONITOR_ALL;
390 bool new_mon_all = mode_follower == CEC_MODE_MONITOR_ALL;
391
392 if (old_mon_all != new_mon_all) {
393 if (new_mon_all)
394 err = cec_monitor_all_cnt_inc(adap);
395 else
396 cec_monitor_all_cnt_dec(adap);
397 }
398 }
399
400 if (err) {
401 mutex_unlock(&adap->lock);
402 return err;
403 }
404
405 if (fh->mode_follower == CEC_MODE_FOLLOWER)
406 adap->follower_cnt--;
407 if (mode_follower == CEC_MODE_FOLLOWER)
408 adap->follower_cnt++;
409 if (mode_follower == CEC_MODE_EXCL_FOLLOWER ||
410 mode_follower == CEC_MODE_EXCL_FOLLOWER_PASSTHRU) {
411 adap->passthrough =
412 mode_follower == CEC_MODE_EXCL_FOLLOWER_PASSTHRU;
413 adap->cec_follower = fh;
414 } else if (adap->cec_follower == fh) {
415 adap->passthrough = false;
416 adap->cec_follower = NULL;
417 }
418 if (mode_initiator == CEC_MODE_EXCL_INITIATOR)
419 adap->cec_initiator = fh;
420 else if (adap->cec_initiator == fh)
421 adap->cec_initiator = NULL;
422 fh->mode_initiator = mode_initiator;
423 fh->mode_follower = mode_follower;
424 mutex_unlock(&adap->lock);
425 return 0;
426 }
427
428 static long cec_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
429 {
430 struct cec_devnode *devnode = cec_devnode_data(filp);
431 struct cec_fh *fh = filp->private_data;
432 struct cec_adapter *adap = fh->adap;
433 bool block = !(filp->f_flags & O_NONBLOCK);
434 void __user *parg = (void __user *)arg;
435
436 if (!devnode->registered)
437 return -EIO;
438
439 switch (cmd) {
440 case CEC_ADAP_G_CAPS:
441 return cec_adap_g_caps(adap, parg);
442
443 case CEC_ADAP_G_PHYS_ADDR:
444 return cec_adap_g_phys_addr(adap, parg);
445
446 case CEC_ADAP_S_PHYS_ADDR:
447 return cec_adap_s_phys_addr(adap, fh, block, parg);
448
449 case CEC_ADAP_G_LOG_ADDRS:
450 return cec_adap_g_log_addrs(adap, parg);
451
452 case CEC_ADAP_S_LOG_ADDRS:
453 return cec_adap_s_log_addrs(adap, fh, block, parg);
454
455 case CEC_TRANSMIT:
456 return cec_transmit(adap, fh, block, parg);
457
458 case CEC_RECEIVE:
459 return cec_receive(adap, fh, block, parg);
460
461 case CEC_DQEVENT:
462 return cec_dqevent(adap, fh, block, parg);
463
464 case CEC_G_MODE:
465 return cec_g_mode(adap, fh, parg);
466
467 case CEC_S_MODE:
468 return cec_s_mode(adap, fh, parg);
469
470 default:
471 return -ENOTTY;
472 }
473 }
474
475 static int cec_open(struct inode *inode, struct file *filp)
476 {
477 struct cec_devnode *devnode =
478 container_of(inode->i_cdev, struct cec_devnode, cdev);
479 struct cec_adapter *adap = to_cec_adapter(devnode);
480 struct cec_fh *fh = kzalloc(sizeof(*fh), GFP_KERNEL);
481 /*
482 * Initial events that are automatically sent when the cec device is
483 * opened.
484 */
485 struct cec_event ev_state = {
486 .event = CEC_EVENT_STATE_CHANGE,
487 .flags = CEC_EVENT_FL_INITIAL_STATE,
488 };
489 int err;
490
491 if (!fh)
492 return -ENOMEM;
493
494 INIT_LIST_HEAD(&fh->msgs);
495 INIT_LIST_HEAD(&fh->xfer_list);
496 mutex_init(&fh->lock);
497 init_waitqueue_head(&fh->wait);
498
499 fh->mode_initiator = CEC_MODE_INITIATOR;
500 fh->adap = adap;
501
502 err = cec_get_device(devnode);
503 if (err) {
504 kfree(fh);
505 return err;
506 }
507
508 filp->private_data = fh;
509
510 mutex_lock(&devnode->fhs_lock);
511 /* Queue up initial state events */
512 ev_state.state_change.phys_addr = adap->phys_addr;
513 ev_state.state_change.log_addr_mask = adap->log_addrs.log_addr_mask;
514 cec_queue_event_fh(fh, &ev_state, 0);
515
516 list_add(&fh->list, &devnode->fhs);
517 mutex_unlock(&devnode->fhs_lock);
518
519 return 0;
520 }
521
522 /* Override for the release function */
523 static int cec_release(struct inode *inode, struct file *filp)
524 {
525 struct cec_devnode *devnode = cec_devnode_data(filp);
526 struct cec_adapter *adap = to_cec_adapter(devnode);
527 struct cec_fh *fh = filp->private_data;
528
529 mutex_lock(&adap->lock);
530 if (adap->cec_initiator == fh)
531 adap->cec_initiator = NULL;
532 if (adap->cec_follower == fh) {
533 adap->cec_follower = NULL;
534 adap->passthrough = false;
535 }
536 if (fh->mode_follower == CEC_MODE_FOLLOWER)
537 adap->follower_cnt--;
538 if (fh->mode_follower == CEC_MODE_MONITOR_ALL)
539 cec_monitor_all_cnt_dec(adap);
540 mutex_unlock(&adap->lock);
541
542 mutex_lock(&devnode->fhs_lock);
543 list_del(&fh->list);
544 mutex_unlock(&devnode->fhs_lock);
545
546 /* Unhook pending transmits from this filehandle. */
547 mutex_lock(&adap->lock);
548 while (!list_empty(&fh->xfer_list)) {
549 struct cec_data *data =
550 list_first_entry(&fh->xfer_list, struct cec_data, xfer_list);
551
552 data->blocking = false;
553 data->fh = NULL;
554 list_del(&data->xfer_list);
555 }
556 mutex_unlock(&adap->lock);
557 while (!list_empty(&fh->msgs)) {
558 struct cec_msg_entry *entry =
559 list_first_entry(&fh->msgs, struct cec_msg_entry, list);
560
561 list_del(&entry->list);
562 kfree(entry);
563 }
564 kfree(fh);
565
566 cec_put_device(devnode);
567 filp->private_data = NULL;
568 return 0;
569 }
570
571 const struct file_operations cec_devnode_fops = {
572 .owner = THIS_MODULE,
573 .open = cec_open,
574 .unlocked_ioctl = cec_ioctl,
575 .release = cec_release,
576 .poll = cec_poll,
577 .llseek = no_llseek,
578 };
This page took 0.044317 seconds and 6 git commands to generate.