cec-api.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  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->is_configured)
  177. err = -ENONET;
  178. else if (cec_is_busy(adap, fh))
  179. err = -EBUSY;
  180. else
  181. err = cec_transmit_msg_fh(adap, &msg, fh, block);
  182. mutex_unlock(&adap->lock);
  183. if (err)
  184. return err;
  185. if (copy_to_user(parg, &msg, sizeof(msg)))
  186. return -EFAULT;
  187. return 0;
  188. }
  189. /* Called by CEC_RECEIVE: wait for a message to arrive */
  190. static int cec_receive_msg(struct cec_fh *fh, struct cec_msg *msg, bool block)
  191. {
  192. u32 timeout = msg->timeout;
  193. int res;
  194. do {
  195. mutex_lock(&fh->lock);
  196. /* Are there received messages queued up? */
  197. if (fh->queued_msgs) {
  198. /* Yes, return the first one */
  199. struct cec_msg_entry *entry =
  200. list_first_entry(&fh->msgs,
  201. struct cec_msg_entry, list);
  202. list_del(&entry->list);
  203. *msg = entry->msg;
  204. kfree(entry);
  205. fh->queued_msgs--;
  206. mutex_unlock(&fh->lock);
  207. /* restore original timeout value */
  208. msg->timeout = timeout;
  209. return 0;
  210. }
  211. /* No, return EAGAIN in non-blocking mode or wait */
  212. mutex_unlock(&fh->lock);
  213. /* Return when in non-blocking mode */
  214. if (!block)
  215. return -EAGAIN;
  216. if (msg->timeout) {
  217. /* The user specified a timeout */
  218. res = wait_event_interruptible_timeout(fh->wait,
  219. fh->queued_msgs,
  220. msecs_to_jiffies(msg->timeout));
  221. if (res == 0)
  222. res = -ETIMEDOUT;
  223. else if (res > 0)
  224. res = 0;
  225. } else {
  226. /* Wait indefinitely */
  227. res = wait_event_interruptible(fh->wait,
  228. fh->queued_msgs);
  229. }
  230. /* Exit on error, otherwise loop to get the new message */
  231. } while (!res);
  232. return res;
  233. }
  234. static long cec_receive(struct cec_adapter *adap, struct cec_fh *fh,
  235. bool block, struct cec_msg __user *parg)
  236. {
  237. struct cec_msg msg = {};
  238. long err = 0;
  239. if (copy_from_user(&msg, parg, sizeof(msg)))
  240. return -EFAULT;
  241. mutex_lock(&adap->lock);
  242. if (!adap->is_configured && fh->mode_follower < CEC_MODE_MONITOR)
  243. err = -ENONET;
  244. mutex_unlock(&adap->lock);
  245. if (err)
  246. return err;
  247. err = cec_receive_msg(fh, &msg, block);
  248. if (err)
  249. return err;
  250. msg.flags = 0;
  251. if (copy_to_user(parg, &msg, sizeof(msg)))
  252. return -EFAULT;
  253. return 0;
  254. }
  255. static long cec_dqevent(struct cec_adapter *adap, struct cec_fh *fh,
  256. bool block, struct cec_event __user *parg)
  257. {
  258. struct cec_event *ev = NULL;
  259. u64 ts = ~0ULL;
  260. unsigned int i;
  261. long err = 0;
  262. mutex_lock(&fh->lock);
  263. while (!fh->pending_events && block) {
  264. mutex_unlock(&fh->lock);
  265. err = wait_event_interruptible(fh->wait, fh->pending_events);
  266. if (err)
  267. return err;
  268. mutex_lock(&fh->lock);
  269. }
  270. /* Find the oldest event */
  271. for (i = 0; i < CEC_NUM_EVENTS; i++) {
  272. if (fh->pending_events & (1 << (i + 1)) &&
  273. fh->events[i].ts <= ts) {
  274. ev = &fh->events[i];
  275. ts = ev->ts;
  276. }
  277. }
  278. if (!ev) {
  279. err = -EAGAIN;
  280. goto unlock;
  281. }
  282. if (copy_to_user(parg, ev, sizeof(*ev))) {
  283. err = -EFAULT;
  284. goto unlock;
  285. }
  286. fh->pending_events &= ~(1 << ev->event);
  287. unlock:
  288. mutex_unlock(&fh->lock);
  289. return err;
  290. }
  291. static long cec_g_mode(struct cec_adapter *adap, struct cec_fh *fh,
  292. u32 __user *parg)
  293. {
  294. u32 mode = fh->mode_initiator | fh->mode_follower;
  295. if (copy_to_user(parg, &mode, sizeof(mode)))
  296. return -EFAULT;
  297. return 0;
  298. }
  299. static long cec_s_mode(struct cec_adapter *adap, struct cec_fh *fh,
  300. u32 __user *parg)
  301. {
  302. u32 mode;
  303. u8 mode_initiator;
  304. u8 mode_follower;
  305. long err = 0;
  306. if (copy_from_user(&mode, parg, sizeof(mode)))
  307. return -EFAULT;
  308. if (mode & ~(CEC_MODE_INITIATOR_MSK | CEC_MODE_FOLLOWER_MSK))
  309. return -EINVAL;
  310. mode_initiator = mode & CEC_MODE_INITIATOR_MSK;
  311. mode_follower = mode & CEC_MODE_FOLLOWER_MSK;
  312. if (mode_initiator > CEC_MODE_EXCL_INITIATOR ||
  313. mode_follower > CEC_MODE_MONITOR_ALL)
  314. return -EINVAL;
  315. if (mode_follower == CEC_MODE_MONITOR_ALL &&
  316. !(adap->capabilities & CEC_CAP_MONITOR_ALL))
  317. return -EINVAL;
  318. /* Follower modes should always be able to send CEC messages */
  319. if ((mode_initiator == CEC_MODE_NO_INITIATOR ||
  320. !(adap->capabilities & CEC_CAP_TRANSMIT)) &&
  321. mode_follower >= CEC_MODE_FOLLOWER &&
  322. mode_follower <= CEC_MODE_EXCL_FOLLOWER_PASSTHRU)
  323. return -EINVAL;
  324. /* Monitor modes require CEC_MODE_NO_INITIATOR */
  325. if (mode_initiator && mode_follower >= CEC_MODE_MONITOR)
  326. return -EINVAL;
  327. /* Monitor modes require CAP_NET_ADMIN */
  328. if (mode_follower >= CEC_MODE_MONITOR && !capable(CAP_NET_ADMIN))
  329. return -EPERM;
  330. mutex_lock(&adap->lock);
  331. /*
  332. * You can't become exclusive follower if someone else already
  333. * has that job.
  334. */
  335. if ((mode_follower == CEC_MODE_EXCL_FOLLOWER ||
  336. mode_follower == CEC_MODE_EXCL_FOLLOWER_PASSTHRU) &&
  337. adap->cec_follower && adap->cec_follower != fh)
  338. err = -EBUSY;
  339. /*
  340. * You can't become exclusive initiator if someone else already
  341. * has that job.
  342. */
  343. if (mode_initiator == CEC_MODE_EXCL_INITIATOR &&
  344. adap->cec_initiator && adap->cec_initiator != fh)
  345. err = -EBUSY;
  346. if (!err) {
  347. bool old_mon_all = fh->mode_follower == CEC_MODE_MONITOR_ALL;
  348. bool new_mon_all = mode_follower == CEC_MODE_MONITOR_ALL;
  349. if (old_mon_all != new_mon_all) {
  350. if (new_mon_all)
  351. err = cec_monitor_all_cnt_inc(adap);
  352. else
  353. cec_monitor_all_cnt_dec(adap);
  354. }
  355. }
  356. if (err) {
  357. mutex_unlock(&adap->lock);
  358. return err;
  359. }
  360. if (fh->mode_follower == CEC_MODE_FOLLOWER)
  361. adap->follower_cnt--;
  362. if (mode_follower == CEC_MODE_FOLLOWER)
  363. adap->follower_cnt++;
  364. if (mode_follower == CEC_MODE_EXCL_FOLLOWER ||
  365. mode_follower == CEC_MODE_EXCL_FOLLOWER_PASSTHRU) {
  366. adap->passthrough =
  367. mode_follower == CEC_MODE_EXCL_FOLLOWER_PASSTHRU;
  368. adap->cec_follower = fh;
  369. } else if (adap->cec_follower == fh) {
  370. adap->passthrough = false;
  371. adap->cec_follower = NULL;
  372. }
  373. if (mode_initiator == CEC_MODE_EXCL_INITIATOR)
  374. adap->cec_initiator = fh;
  375. else if (adap->cec_initiator == fh)
  376. adap->cec_initiator = NULL;
  377. fh->mode_initiator = mode_initiator;
  378. fh->mode_follower = mode_follower;
  379. mutex_unlock(&adap->lock);
  380. return 0;
  381. }
  382. static long cec_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  383. {
  384. struct cec_devnode *devnode = cec_devnode_data(filp);
  385. struct cec_fh *fh = filp->private_data;
  386. struct cec_adapter *adap = fh->adap;
  387. bool block = !(filp->f_flags & O_NONBLOCK);
  388. void __user *parg = (void __user *)arg;
  389. if (!devnode->registered)
  390. return -ENODEV;
  391. switch (cmd) {
  392. case CEC_ADAP_G_CAPS:
  393. return cec_adap_g_caps(adap, parg);
  394. case CEC_ADAP_G_PHYS_ADDR:
  395. return cec_adap_g_phys_addr(adap, parg);
  396. case CEC_ADAP_S_PHYS_ADDR:
  397. return cec_adap_s_phys_addr(adap, fh, block, parg);
  398. case CEC_ADAP_G_LOG_ADDRS:
  399. return cec_adap_g_log_addrs(adap, parg);
  400. case CEC_ADAP_S_LOG_ADDRS:
  401. return cec_adap_s_log_addrs(adap, fh, block, parg);
  402. case CEC_TRANSMIT:
  403. return cec_transmit(adap, fh, block, parg);
  404. case CEC_RECEIVE:
  405. return cec_receive(adap, fh, block, parg);
  406. case CEC_DQEVENT:
  407. return cec_dqevent(adap, fh, block, parg);
  408. case CEC_G_MODE:
  409. return cec_g_mode(adap, fh, parg);
  410. case CEC_S_MODE:
  411. return cec_s_mode(adap, fh, parg);
  412. default:
  413. return -ENOTTY;
  414. }
  415. }
  416. static int cec_open(struct inode *inode, struct file *filp)
  417. {
  418. struct cec_devnode *devnode =
  419. container_of(inode->i_cdev, struct cec_devnode, cdev);
  420. struct cec_adapter *adap = to_cec_adapter(devnode);
  421. struct cec_fh *fh = kzalloc(sizeof(*fh), GFP_KERNEL);
  422. /*
  423. * Initial events that are automatically sent when the cec device is
  424. * opened.
  425. */
  426. struct cec_event ev_state = {
  427. .event = CEC_EVENT_STATE_CHANGE,
  428. .flags = CEC_EVENT_FL_INITIAL_STATE,
  429. };
  430. int err;
  431. if (!fh)
  432. return -ENOMEM;
  433. INIT_LIST_HEAD(&fh->msgs);
  434. INIT_LIST_HEAD(&fh->xfer_list);
  435. mutex_init(&fh->lock);
  436. init_waitqueue_head(&fh->wait);
  437. fh->mode_initiator = CEC_MODE_INITIATOR;
  438. fh->adap = adap;
  439. err = cec_get_device(devnode);
  440. if (err) {
  441. kfree(fh);
  442. return err;
  443. }
  444. filp->private_data = fh;
  445. mutex_lock(&devnode->lock);
  446. /* Queue up initial state events */
  447. ev_state.state_change.phys_addr = adap->phys_addr;
  448. ev_state.state_change.log_addr_mask = adap->log_addrs.log_addr_mask;
  449. cec_queue_event_fh(fh, &ev_state, 0);
  450. list_add(&fh->list, &devnode->fhs);
  451. mutex_unlock(&devnode->lock);
  452. return 0;
  453. }
  454. /* Override for the release function */
  455. static int cec_release(struct inode *inode, struct file *filp)
  456. {
  457. struct cec_devnode *devnode = cec_devnode_data(filp);
  458. struct cec_adapter *adap = to_cec_adapter(devnode);
  459. struct cec_fh *fh = filp->private_data;
  460. mutex_lock(&adap->lock);
  461. if (adap->cec_initiator == fh)
  462. adap->cec_initiator = NULL;
  463. if (adap->cec_follower == fh) {
  464. adap->cec_follower = NULL;
  465. adap->passthrough = false;
  466. }
  467. if (fh->mode_follower == CEC_MODE_FOLLOWER)
  468. adap->follower_cnt--;
  469. if (fh->mode_follower == CEC_MODE_MONITOR_ALL)
  470. cec_monitor_all_cnt_dec(adap);
  471. mutex_unlock(&adap->lock);
  472. mutex_lock(&devnode->lock);
  473. list_del(&fh->list);
  474. mutex_unlock(&devnode->lock);
  475. /* Unhook pending transmits from this filehandle. */
  476. mutex_lock(&adap->lock);
  477. while (!list_empty(&fh->xfer_list)) {
  478. struct cec_data *data =
  479. list_first_entry(&fh->xfer_list, struct cec_data, xfer_list);
  480. data->blocking = false;
  481. data->fh = NULL;
  482. list_del(&data->xfer_list);
  483. }
  484. mutex_unlock(&adap->lock);
  485. while (!list_empty(&fh->msgs)) {
  486. struct cec_msg_entry *entry =
  487. list_first_entry(&fh->msgs, struct cec_msg_entry, list);
  488. list_del(&entry->list);
  489. kfree(entry);
  490. }
  491. kfree(fh);
  492. cec_put_device(devnode);
  493. filp->private_data = NULL;
  494. return 0;
  495. }
  496. const struct file_operations cec_devnode_fops = {
  497. .owner = THIS_MODULE,
  498. .open = cec_open,
  499. .unlocked_ioctl = cec_ioctl,
  500. .release = cec_release,
  501. .poll = cec_poll,
  502. .llseek = no_llseek,
  503. };