]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - drivers/media/cec/cec-api.c
iio: imu: inv_mpu6050: test whoami first and against all known values
[mirror_ubuntu-artful-kernel.git] / drivers / 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 adap->transmit_queue_sz < CEC_MAX_MSG_TX_QUEUE_SZ)
57 res |= POLLOUT | POLLWRNORM;
58 if (fh->queued_msgs)
59 res |= POLLIN | POLLRDNORM;
60 if (fh->pending_events)
61 res |= POLLPRI;
62 poll_wait(filp, &fh->wait, poll);
63 mutex_unlock(&adap->lock);
64 return res;
65 }
66
67 static bool cec_is_busy(const struct cec_adapter *adap,
68 const struct cec_fh *fh)
69 {
70 bool valid_initiator = adap->cec_initiator && adap->cec_initiator == fh;
71 bool valid_follower = adap->cec_follower && adap->cec_follower == fh;
72
73 /*
74 * Exclusive initiators and followers can always access the CEC adapter
75 */
76 if (valid_initiator || valid_follower)
77 return false;
78 /*
79 * All others can only access the CEC adapter if there is no
80 * exclusive initiator and they are in INITIATOR mode.
81 */
82 return adap->cec_initiator ||
83 fh->mode_initiator == CEC_MODE_NO_INITIATOR;
84 }
85
86 static long cec_adap_g_caps(struct cec_adapter *adap,
87 struct cec_caps __user *parg)
88 {
89 struct cec_caps caps = {};
90
91 strlcpy(caps.driver, adap->devnode.dev.parent->driver->name,
92 sizeof(caps.driver));
93 strlcpy(caps.name, adap->name, sizeof(caps.name));
94 caps.available_log_addrs = adap->available_log_addrs;
95 caps.capabilities = adap->capabilities;
96 caps.version = LINUX_VERSION_CODE;
97 if (copy_to_user(parg, &caps, sizeof(caps)))
98 return -EFAULT;
99 return 0;
100 }
101
102 static long cec_adap_g_phys_addr(struct cec_adapter *adap,
103 __u16 __user *parg)
104 {
105 u16 phys_addr;
106
107 mutex_lock(&adap->lock);
108 phys_addr = adap->phys_addr;
109 mutex_unlock(&adap->lock);
110 if (copy_to_user(parg, &phys_addr, sizeof(phys_addr)))
111 return -EFAULT;
112 return 0;
113 }
114
115 static long cec_adap_s_phys_addr(struct cec_adapter *adap, struct cec_fh *fh,
116 bool block, __u16 __user *parg)
117 {
118 u16 phys_addr;
119 long err;
120
121 if (!(adap->capabilities & CEC_CAP_PHYS_ADDR))
122 return -ENOTTY;
123 if (copy_from_user(&phys_addr, parg, sizeof(phys_addr)))
124 return -EFAULT;
125
126 err = cec_phys_addr_validate(phys_addr, NULL, NULL);
127 if (err)
128 return err;
129 mutex_lock(&adap->lock);
130 if (cec_is_busy(adap, fh))
131 err = -EBUSY;
132 else
133 __cec_s_phys_addr(adap, phys_addr, block);
134 mutex_unlock(&adap->lock);
135 return err;
136 }
137
138 static long cec_adap_g_log_addrs(struct cec_adapter *adap,
139 struct cec_log_addrs __user *parg)
140 {
141 struct cec_log_addrs log_addrs;
142
143 mutex_lock(&adap->lock);
144 log_addrs = adap->log_addrs;
145 if (!adap->is_configured)
146 memset(log_addrs.log_addr, CEC_LOG_ADDR_INVALID,
147 sizeof(log_addrs.log_addr));
148 mutex_unlock(&adap->lock);
149
150 if (copy_to_user(parg, &log_addrs, sizeof(log_addrs)))
151 return -EFAULT;
152 return 0;
153 }
154
155 static long cec_adap_s_log_addrs(struct cec_adapter *adap, struct cec_fh *fh,
156 bool block, struct cec_log_addrs __user *parg)
157 {
158 struct cec_log_addrs log_addrs;
159 long err = -EBUSY;
160
161 if (!(adap->capabilities & CEC_CAP_LOG_ADDRS))
162 return -ENOTTY;
163 if (copy_from_user(&log_addrs, parg, sizeof(log_addrs)))
164 return -EFAULT;
165 log_addrs.flags &= CEC_LOG_ADDRS_FL_ALLOW_UNREG_FALLBACK |
166 CEC_LOG_ADDRS_FL_ALLOW_RC_PASSTHRU |
167 CEC_LOG_ADDRS_FL_CDC_ONLY;
168 mutex_lock(&adap->lock);
169 if (!adap->is_configuring &&
170 (!log_addrs.num_log_addrs || !adap->is_configured) &&
171 !cec_is_busy(adap, fh)) {
172 err = __cec_s_log_addrs(adap, &log_addrs, block);
173 if (!err)
174 log_addrs = adap->log_addrs;
175 }
176 mutex_unlock(&adap->lock);
177 if (err)
178 return err;
179 if (copy_to_user(parg, &log_addrs, sizeof(log_addrs)))
180 return -EFAULT;
181 return 0;
182 }
183
184 static long cec_transmit(struct cec_adapter *adap, struct cec_fh *fh,
185 bool block, struct cec_msg __user *parg)
186 {
187 struct cec_msg msg = {};
188 long err = 0;
189
190 if (!(adap->capabilities & CEC_CAP_TRANSMIT))
191 return -ENOTTY;
192 if (copy_from_user(&msg, parg, sizeof(msg)))
193 return -EFAULT;
194
195 /* A CDC-Only device can only send CDC messages */
196 if ((adap->log_addrs.flags & CEC_LOG_ADDRS_FL_CDC_ONLY) &&
197 (msg.len == 1 || msg.msg[1] != CEC_MSG_CDC_MESSAGE))
198 return -EINVAL;
199
200 mutex_lock(&adap->lock);
201 if (adap->log_addrs.num_log_addrs == 0)
202 err = -EPERM;
203 else if (adap->is_configuring)
204 err = -ENONET;
205 else if (!adap->is_configured && msg.msg[0] != 0xf0)
206 err = -ENONET;
207 else if (cec_is_busy(adap, fh))
208 err = -EBUSY;
209 else
210 err = cec_transmit_msg_fh(adap, &msg, fh, block);
211 mutex_unlock(&adap->lock);
212 if (err)
213 return err;
214 if (copy_to_user(parg, &msg, sizeof(msg)))
215 return -EFAULT;
216 return 0;
217 }
218
219 /* Called by CEC_RECEIVE: wait for a message to arrive */
220 static int cec_receive_msg(struct cec_fh *fh, struct cec_msg *msg, bool block)
221 {
222 u32 timeout = msg->timeout;
223 int res;
224
225 do {
226 mutex_lock(&fh->lock);
227 /* Are there received messages queued up? */
228 if (fh->queued_msgs) {
229 /* Yes, return the first one */
230 struct cec_msg_entry *entry =
231 list_first_entry(&fh->msgs,
232 struct cec_msg_entry, list);
233
234 list_del(&entry->list);
235 *msg = entry->msg;
236 kfree(entry);
237 fh->queued_msgs--;
238 mutex_unlock(&fh->lock);
239 /* restore original timeout value */
240 msg->timeout = timeout;
241 return 0;
242 }
243
244 /* No, return EAGAIN in non-blocking mode or wait */
245 mutex_unlock(&fh->lock);
246
247 /* Return when in non-blocking mode */
248 if (!block)
249 return -EAGAIN;
250
251 if (msg->timeout) {
252 /* The user specified a timeout */
253 res = wait_event_interruptible_timeout(fh->wait,
254 fh->queued_msgs,
255 msecs_to_jiffies(msg->timeout));
256 if (res == 0)
257 res = -ETIMEDOUT;
258 else if (res > 0)
259 res = 0;
260 } else {
261 /* Wait indefinitely */
262 res = wait_event_interruptible(fh->wait,
263 fh->queued_msgs);
264 }
265 /* Exit on error, otherwise loop to get the new message */
266 } while (!res);
267 return res;
268 }
269
270 static long cec_receive(struct cec_adapter *adap, struct cec_fh *fh,
271 bool block, struct cec_msg __user *parg)
272 {
273 struct cec_msg msg = {};
274 long err = 0;
275
276 if (copy_from_user(&msg, parg, sizeof(msg)))
277 return -EFAULT;
278 mutex_lock(&adap->lock);
279 if (!adap->is_configured && fh->mode_follower < CEC_MODE_MONITOR)
280 err = -ENONET;
281 mutex_unlock(&adap->lock);
282 if (err)
283 return err;
284
285 err = cec_receive_msg(fh, &msg, block);
286 if (err)
287 return err;
288 msg.flags = 0;
289 if (copy_to_user(parg, &msg, sizeof(msg)))
290 return -EFAULT;
291 return 0;
292 }
293
294 static long cec_dqevent(struct cec_adapter *adap, struct cec_fh *fh,
295 bool block, struct cec_event __user *parg)
296 {
297 struct cec_event *ev = NULL;
298 u64 ts = ~0ULL;
299 unsigned int i;
300 long err = 0;
301
302 mutex_lock(&fh->lock);
303 while (!fh->pending_events && block) {
304 mutex_unlock(&fh->lock);
305 err = wait_event_interruptible(fh->wait, fh->pending_events);
306 if (err)
307 return err;
308 mutex_lock(&fh->lock);
309 }
310
311 /* Find the oldest event */
312 for (i = 0; i < CEC_NUM_EVENTS; i++) {
313 if (fh->pending_events & (1 << (i + 1)) &&
314 fh->events[i].ts <= ts) {
315 ev = &fh->events[i];
316 ts = ev->ts;
317 }
318 }
319 if (!ev) {
320 err = -EAGAIN;
321 goto unlock;
322 }
323
324 if (copy_to_user(parg, ev, sizeof(*ev))) {
325 err = -EFAULT;
326 goto unlock;
327 }
328
329 fh->pending_events &= ~(1 << ev->event);
330
331 unlock:
332 mutex_unlock(&fh->lock);
333 return err;
334 }
335
336 static long cec_g_mode(struct cec_adapter *adap, struct cec_fh *fh,
337 u32 __user *parg)
338 {
339 u32 mode = fh->mode_initiator | fh->mode_follower;
340
341 if (copy_to_user(parg, &mode, sizeof(mode)))
342 return -EFAULT;
343 return 0;
344 }
345
346 static long cec_s_mode(struct cec_adapter *adap, struct cec_fh *fh,
347 u32 __user *parg)
348 {
349 u32 mode;
350 u8 mode_initiator;
351 u8 mode_follower;
352 long err = 0;
353
354 if (copy_from_user(&mode, parg, sizeof(mode)))
355 return -EFAULT;
356 if (mode & ~(CEC_MODE_INITIATOR_MSK | CEC_MODE_FOLLOWER_MSK))
357 return -EINVAL;
358
359 mode_initiator = mode & CEC_MODE_INITIATOR_MSK;
360 mode_follower = mode & CEC_MODE_FOLLOWER_MSK;
361
362 if (mode_initiator > CEC_MODE_EXCL_INITIATOR ||
363 mode_follower > CEC_MODE_MONITOR_ALL)
364 return -EINVAL;
365
366 if (mode_follower == CEC_MODE_MONITOR_ALL &&
367 !(adap->capabilities & CEC_CAP_MONITOR_ALL))
368 return -EINVAL;
369
370 /* Follower modes should always be able to send CEC messages */
371 if ((mode_initiator == CEC_MODE_NO_INITIATOR ||
372 !(adap->capabilities & CEC_CAP_TRANSMIT)) &&
373 mode_follower >= CEC_MODE_FOLLOWER &&
374 mode_follower <= CEC_MODE_EXCL_FOLLOWER_PASSTHRU)
375 return -EINVAL;
376
377 /* Monitor modes require CEC_MODE_NO_INITIATOR */
378 if (mode_initiator && mode_follower >= CEC_MODE_MONITOR)
379 return -EINVAL;
380
381 /* Monitor modes require CAP_NET_ADMIN */
382 if (mode_follower >= CEC_MODE_MONITOR && !capable(CAP_NET_ADMIN))
383 return -EPERM;
384
385 mutex_lock(&adap->lock);
386 /*
387 * You can't become exclusive follower if someone else already
388 * has that job.
389 */
390 if ((mode_follower == CEC_MODE_EXCL_FOLLOWER ||
391 mode_follower == CEC_MODE_EXCL_FOLLOWER_PASSTHRU) &&
392 adap->cec_follower && adap->cec_follower != fh)
393 err = -EBUSY;
394 /*
395 * You can't become exclusive initiator if someone else already
396 * has that job.
397 */
398 if (mode_initiator == CEC_MODE_EXCL_INITIATOR &&
399 adap->cec_initiator && adap->cec_initiator != fh)
400 err = -EBUSY;
401
402 if (!err) {
403 bool old_mon_all = fh->mode_follower == CEC_MODE_MONITOR_ALL;
404 bool new_mon_all = mode_follower == CEC_MODE_MONITOR_ALL;
405
406 if (old_mon_all != new_mon_all) {
407 if (new_mon_all)
408 err = cec_monitor_all_cnt_inc(adap);
409 else
410 cec_monitor_all_cnt_dec(adap);
411 }
412 }
413
414 if (err) {
415 mutex_unlock(&adap->lock);
416 return err;
417 }
418
419 if (fh->mode_follower == CEC_MODE_FOLLOWER)
420 adap->follower_cnt--;
421 if (mode_follower == CEC_MODE_FOLLOWER)
422 adap->follower_cnt++;
423 if (mode_follower == CEC_MODE_EXCL_FOLLOWER ||
424 mode_follower == CEC_MODE_EXCL_FOLLOWER_PASSTHRU) {
425 adap->passthrough =
426 mode_follower == CEC_MODE_EXCL_FOLLOWER_PASSTHRU;
427 adap->cec_follower = fh;
428 } else if (adap->cec_follower == fh) {
429 adap->passthrough = false;
430 adap->cec_follower = NULL;
431 }
432 if (mode_initiator == CEC_MODE_EXCL_INITIATOR)
433 adap->cec_initiator = fh;
434 else if (adap->cec_initiator == fh)
435 adap->cec_initiator = NULL;
436 fh->mode_initiator = mode_initiator;
437 fh->mode_follower = mode_follower;
438 mutex_unlock(&adap->lock);
439 return 0;
440 }
441
442 static long cec_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
443 {
444 struct cec_devnode *devnode = cec_devnode_data(filp);
445 struct cec_fh *fh = filp->private_data;
446 struct cec_adapter *adap = fh->adap;
447 bool block = !(filp->f_flags & O_NONBLOCK);
448 void __user *parg = (void __user *)arg;
449
450 if (!devnode->registered)
451 return -ENODEV;
452
453 switch (cmd) {
454 case CEC_ADAP_G_CAPS:
455 return cec_adap_g_caps(adap, parg);
456
457 case CEC_ADAP_G_PHYS_ADDR:
458 return cec_adap_g_phys_addr(adap, parg);
459
460 case CEC_ADAP_S_PHYS_ADDR:
461 return cec_adap_s_phys_addr(adap, fh, block, parg);
462
463 case CEC_ADAP_G_LOG_ADDRS:
464 return cec_adap_g_log_addrs(adap, parg);
465
466 case CEC_ADAP_S_LOG_ADDRS:
467 return cec_adap_s_log_addrs(adap, fh, block, parg);
468
469 case CEC_TRANSMIT:
470 return cec_transmit(adap, fh, block, parg);
471
472 case CEC_RECEIVE:
473 return cec_receive(adap, fh, block, parg);
474
475 case CEC_DQEVENT:
476 return cec_dqevent(adap, fh, block, parg);
477
478 case CEC_G_MODE:
479 return cec_g_mode(adap, fh, parg);
480
481 case CEC_S_MODE:
482 return cec_s_mode(adap, fh, parg);
483
484 default:
485 return -ENOTTY;
486 }
487 }
488
489 static int cec_open(struct inode *inode, struct file *filp)
490 {
491 struct cec_devnode *devnode =
492 container_of(inode->i_cdev, struct cec_devnode, cdev);
493 struct cec_adapter *adap = to_cec_adapter(devnode);
494 struct cec_fh *fh = kzalloc(sizeof(*fh), GFP_KERNEL);
495 /*
496 * Initial events that are automatically sent when the cec device is
497 * opened.
498 */
499 struct cec_event ev_state = {
500 .event = CEC_EVENT_STATE_CHANGE,
501 .flags = CEC_EVENT_FL_INITIAL_STATE,
502 };
503 int err;
504
505 if (!fh)
506 return -ENOMEM;
507
508 INIT_LIST_HEAD(&fh->msgs);
509 INIT_LIST_HEAD(&fh->xfer_list);
510 mutex_init(&fh->lock);
511 init_waitqueue_head(&fh->wait);
512
513 fh->mode_initiator = CEC_MODE_INITIATOR;
514 fh->adap = adap;
515
516 err = cec_get_device(devnode);
517 if (err) {
518 kfree(fh);
519 return err;
520 }
521
522 mutex_lock(&devnode->lock);
523 if (list_empty(&devnode->fhs) &&
524 adap->phys_addr == CEC_PHYS_ADDR_INVALID) {
525 err = adap->ops->adap_enable(adap, true);
526 if (err) {
527 mutex_unlock(&devnode->lock);
528 kfree(fh);
529 return err;
530 }
531 }
532 filp->private_data = fh;
533
534 /* Queue up initial state events */
535 ev_state.state_change.phys_addr = adap->phys_addr;
536 ev_state.state_change.log_addr_mask = adap->log_addrs.log_addr_mask;
537 cec_queue_event_fh(fh, &ev_state, 0);
538
539 list_add(&fh->list, &devnode->fhs);
540 mutex_unlock(&devnode->lock);
541
542 return 0;
543 }
544
545 /* Override for the release function */
546 static int cec_release(struct inode *inode, struct file *filp)
547 {
548 struct cec_devnode *devnode = cec_devnode_data(filp);
549 struct cec_adapter *adap = to_cec_adapter(devnode);
550 struct cec_fh *fh = filp->private_data;
551
552 mutex_lock(&adap->lock);
553 if (adap->cec_initiator == fh)
554 adap->cec_initiator = NULL;
555 if (adap->cec_follower == fh) {
556 adap->cec_follower = NULL;
557 adap->passthrough = false;
558 }
559 if (fh->mode_follower == CEC_MODE_FOLLOWER)
560 adap->follower_cnt--;
561 if (fh->mode_follower == CEC_MODE_MONITOR_ALL)
562 cec_monitor_all_cnt_dec(adap);
563 mutex_unlock(&adap->lock);
564
565 mutex_lock(&devnode->lock);
566 list_del(&fh->list);
567 if (list_empty(&devnode->fhs) &&
568 adap->phys_addr == CEC_PHYS_ADDR_INVALID) {
569 WARN_ON(adap->ops->adap_enable(adap, false));
570 }
571 mutex_unlock(&devnode->lock);
572
573 /* Unhook pending transmits from this filehandle. */
574 mutex_lock(&adap->lock);
575 while (!list_empty(&fh->xfer_list)) {
576 struct cec_data *data =
577 list_first_entry(&fh->xfer_list, struct cec_data, xfer_list);
578
579 data->blocking = false;
580 data->fh = NULL;
581 list_del(&data->xfer_list);
582 }
583 mutex_unlock(&adap->lock);
584 while (!list_empty(&fh->msgs)) {
585 struct cec_msg_entry *entry =
586 list_first_entry(&fh->msgs, struct cec_msg_entry, list);
587
588 list_del(&entry->list);
589 kfree(entry);
590 }
591 kfree(fh);
592
593 cec_put_device(devnode);
594 filp->private_data = NULL;
595 return 0;
596 }
597
598 const struct file_operations cec_devnode_fops = {
599 .owner = THIS_MODULE,
600 .open = cec_open,
601 .unlocked_ioctl = cec_ioctl,
602 .release = cec_release,
603 .poll = cec_poll,
604 .llseek = no_llseek,
605 };