cec-api.c 15 KB

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