cec-api.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * cec-api.c - HDMI Consumer Electronics Control framework - API
  4. *
  5. * Copyright 2016 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
  6. */
  7. #include <linux/errno.h>
  8. #include <linux/init.h>
  9. #include <linux/module.h>
  10. #include <linux/kernel.h>
  11. #include <linux/kmod.h>
  12. #include <linux/ktime.h>
  13. #include <linux/slab.h>
  14. #include <linux/mm.h>
  15. #include <linux/string.h>
  16. #include <linux/types.h>
  17. #include <linux/uaccess.h>
  18. #include <linux/version.h>
  19. #include <media/cec-pin.h>
  20. #include "cec-priv.h"
  21. #include "cec-pin-priv.h"
  22. static inline struct cec_devnode *cec_devnode_data(struct file *filp)
  23. {
  24. struct cec_fh *fh = filp->private_data;
  25. return &fh->adap->devnode;
  26. }
  27. /* CEC file operations */
  28. static __poll_t cec_poll(struct file *filp,
  29. struct poll_table_struct *poll)
  30. {
  31. struct cec_fh *fh = filp->private_data;
  32. struct cec_adapter *adap = fh->adap;
  33. __poll_t res = 0;
  34. poll_wait(filp, &fh->wait, poll);
  35. if (!cec_is_registered(adap))
  36. return EPOLLERR | EPOLLHUP | EPOLLPRI;
  37. mutex_lock(&adap->lock);
  38. if (adap->is_configured &&
  39. adap->transmit_queue_sz < CEC_MAX_MSG_TX_QUEUE_SZ)
  40. res |= EPOLLOUT | EPOLLWRNORM;
  41. if (fh->queued_msgs)
  42. res |= EPOLLIN | EPOLLRDNORM;
  43. if (fh->total_queued_events)
  44. res |= EPOLLPRI;
  45. mutex_unlock(&adap->lock);
  46. return res;
  47. }
  48. static bool cec_is_busy(const struct cec_adapter *adap,
  49. const struct cec_fh *fh)
  50. {
  51. bool valid_initiator = adap->cec_initiator && adap->cec_initiator == fh;
  52. bool valid_follower = adap->cec_follower && adap->cec_follower == fh;
  53. /*
  54. * Exclusive initiators and followers can always access the CEC adapter
  55. */
  56. if (valid_initiator || valid_follower)
  57. return false;
  58. /*
  59. * All others can only access the CEC adapter if there is no
  60. * exclusive initiator and they are in INITIATOR mode.
  61. */
  62. return adap->cec_initiator ||
  63. fh->mode_initiator == CEC_MODE_NO_INITIATOR;
  64. }
  65. static long cec_adap_g_caps(struct cec_adapter *adap,
  66. struct cec_caps __user *parg)
  67. {
  68. struct cec_caps caps = {};
  69. strscpy(caps.driver, adap->devnode.dev.parent->driver->name,
  70. sizeof(caps.driver));
  71. strscpy(caps.name, adap->name, sizeof(caps.name));
  72. caps.available_log_addrs = adap->available_log_addrs;
  73. caps.capabilities = adap->capabilities;
  74. caps.version = LINUX_VERSION_CODE;
  75. if (copy_to_user(parg, &caps, sizeof(caps)))
  76. return -EFAULT;
  77. return 0;
  78. }
  79. static long cec_adap_g_phys_addr(struct cec_adapter *adap,
  80. __u16 __user *parg)
  81. {
  82. u16 phys_addr;
  83. mutex_lock(&adap->lock);
  84. phys_addr = adap->phys_addr;
  85. mutex_unlock(&adap->lock);
  86. if (copy_to_user(parg, &phys_addr, sizeof(phys_addr)))
  87. return -EFAULT;
  88. return 0;
  89. }
  90. static int cec_validate_phys_addr(u16 phys_addr)
  91. {
  92. int i;
  93. if (phys_addr == CEC_PHYS_ADDR_INVALID)
  94. return 0;
  95. for (i = 0; i < 16; i += 4)
  96. if (phys_addr & (0xf << i))
  97. break;
  98. if (i == 16)
  99. return 0;
  100. for (i += 4; i < 16; i += 4)
  101. if ((phys_addr & (0xf << i)) == 0)
  102. return -EINVAL;
  103. return 0;
  104. }
  105. static long cec_adap_s_phys_addr(struct cec_adapter *adap, struct cec_fh *fh,
  106. bool block, __u16 __user *parg)
  107. {
  108. u16 phys_addr;
  109. long err;
  110. if (!(adap->capabilities & CEC_CAP_PHYS_ADDR))
  111. return -ENOTTY;
  112. if (copy_from_user(&phys_addr, parg, sizeof(phys_addr)))
  113. return -EFAULT;
  114. err = cec_validate_phys_addr(phys_addr);
  115. if (err)
  116. return err;
  117. mutex_lock(&adap->lock);
  118. if (cec_is_busy(adap, fh))
  119. err = -EBUSY;
  120. else
  121. __cec_s_phys_addr(adap, phys_addr, block);
  122. mutex_unlock(&adap->lock);
  123. return err;
  124. }
  125. static long cec_adap_g_log_addrs(struct cec_adapter *adap,
  126. struct cec_log_addrs __user *parg)
  127. {
  128. struct cec_log_addrs log_addrs;
  129. mutex_lock(&adap->lock);
  130. /*
  131. * We use memcpy here instead of assignment since there is a
  132. * hole at the end of struct cec_log_addrs that an assignment
  133. * might ignore. So when we do copy_to_user() we could leak
  134. * one byte of memory.
  135. */
  136. memcpy(&log_addrs, &adap->log_addrs, sizeof(log_addrs));
  137. if (!adap->is_configured)
  138. memset(log_addrs.log_addr, CEC_LOG_ADDR_INVALID,
  139. sizeof(log_addrs.log_addr));
  140. mutex_unlock(&adap->lock);
  141. if (copy_to_user(parg, &log_addrs, sizeof(log_addrs)))
  142. return -EFAULT;
  143. return 0;
  144. }
  145. static long cec_adap_s_log_addrs(struct cec_adapter *adap, struct cec_fh *fh,
  146. bool block, struct cec_log_addrs __user *parg)
  147. {
  148. struct cec_log_addrs log_addrs;
  149. long err = -EBUSY;
  150. if (!(adap->capabilities & CEC_CAP_LOG_ADDRS))
  151. return -ENOTTY;
  152. if (copy_from_user(&log_addrs, parg, sizeof(log_addrs)))
  153. return -EFAULT;
  154. log_addrs.flags &= CEC_LOG_ADDRS_FL_ALLOW_UNREG_FALLBACK |
  155. CEC_LOG_ADDRS_FL_ALLOW_RC_PASSTHRU |
  156. CEC_LOG_ADDRS_FL_CDC_ONLY;
  157. mutex_lock(&adap->lock);
  158. if (!adap->is_claiming_log_addrs && !adap->is_configuring &&
  159. (!log_addrs.num_log_addrs || !adap->is_configured) &&
  160. !cec_is_busy(adap, fh)) {
  161. err = __cec_s_log_addrs(adap, &log_addrs, block);
  162. if (!err)
  163. log_addrs = adap->log_addrs;
  164. }
  165. mutex_unlock(&adap->lock);
  166. if (err)
  167. return err;
  168. if (copy_to_user(parg, &log_addrs, sizeof(log_addrs)))
  169. return -EFAULT;
  170. return 0;
  171. }
  172. static long cec_adap_g_connector_info(struct cec_adapter *adap,
  173. struct cec_log_addrs __user *parg)
  174. {
  175. int ret = 0;
  176. if (!(adap->capabilities & CEC_CAP_CONNECTOR_INFO))
  177. return -ENOTTY;
  178. mutex_lock(&adap->lock);
  179. if (copy_to_user(parg, &adap->conn_info, sizeof(adap->conn_info)))
  180. ret = -EFAULT;
  181. mutex_unlock(&adap->lock);
  182. return ret;
  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. if (!(adap->capabilities & CEC_CAP_TRANSMIT))
  190. return -ENOTTY;
  191. if (copy_from_user(&msg, parg, sizeof(msg)))
  192. return -EFAULT;
  193. mutex_lock(&adap->lock);
  194. if (adap->log_addrs.num_log_addrs == 0)
  195. err = -EPERM;
  196. else if (adap->is_configuring)
  197. err = -ENONET;
  198. else if (cec_is_busy(adap, fh))
  199. err = -EBUSY;
  200. else
  201. err = cec_transmit_msg_fh(adap, &msg, fh, block);
  202. mutex_unlock(&adap->lock);
  203. if (err)
  204. return err;
  205. if (copy_to_user(parg, &msg, sizeof(msg)))
  206. return -EFAULT;
  207. return 0;
  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. u32 timeout = msg->timeout;
  213. int res;
  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. list_del(&entry->list);
  223. *msg = entry->msg;
  224. kfree(entry);
  225. fh->queued_msgs--;
  226. mutex_unlock(&fh->lock);
  227. /* restore original timeout value */
  228. msg->timeout = timeout;
  229. return 0;
  230. }
  231. /* No, return EAGAIN in non-blocking mode or wait */
  232. mutex_unlock(&fh->lock);
  233. /* Return when in non-blocking mode */
  234. if (!block)
  235. return -EAGAIN;
  236. if (msg->timeout) {
  237. /* The user specified a timeout */
  238. res = wait_event_interruptible_timeout(fh->wait,
  239. fh->queued_msgs,
  240. msecs_to_jiffies(msg->timeout));
  241. if (res == 0)
  242. res = -ETIMEDOUT;
  243. else if (res > 0)
  244. res = 0;
  245. } else {
  246. /* Wait indefinitely */
  247. res = wait_event_interruptible(fh->wait,
  248. fh->queued_msgs);
  249. }
  250. /* Exit on error, otherwise loop to get the new message */
  251. } while (!res);
  252. return res;
  253. }
  254. static long cec_receive(struct cec_adapter *adap, struct cec_fh *fh,
  255. bool block, struct cec_msg __user *parg)
  256. {
  257. struct cec_msg msg = {};
  258. long err;
  259. if (copy_from_user(&msg, parg, sizeof(msg)))
  260. return -EFAULT;
  261. err = cec_receive_msg(fh, &msg, block);
  262. if (err)
  263. return err;
  264. msg.flags = 0;
  265. if (copy_to_user(parg, &msg, sizeof(msg)))
  266. return -EFAULT;
  267. return 0;
  268. }
  269. static long cec_dqevent(struct cec_adapter *adap, struct cec_fh *fh,
  270. bool block, struct cec_event __user *parg)
  271. {
  272. struct cec_event_entry *ev = NULL;
  273. u64 ts = ~0ULL;
  274. unsigned int i;
  275. unsigned int ev_idx;
  276. long err = 0;
  277. mutex_lock(&fh->lock);
  278. while (!fh->total_queued_events && block) {
  279. mutex_unlock(&fh->lock);
  280. err = wait_event_interruptible(fh->wait,
  281. fh->total_queued_events);
  282. if (err)
  283. return err;
  284. mutex_lock(&fh->lock);
  285. }
  286. /* Find the oldest event */
  287. for (i = 0; i < CEC_NUM_EVENTS; i++) {
  288. struct cec_event_entry *entry =
  289. list_first_entry_or_null(&fh->events[i],
  290. struct cec_event_entry, list);
  291. if (entry && entry->ev.ts <= ts) {
  292. ev = entry;
  293. ev_idx = i;
  294. ts = ev->ev.ts;
  295. }
  296. }
  297. if (!ev) {
  298. err = -EAGAIN;
  299. goto unlock;
  300. }
  301. list_del(&ev->list);
  302. if (copy_to_user(parg, &ev->ev, sizeof(ev->ev)))
  303. err = -EFAULT;
  304. if (ev_idx >= CEC_NUM_CORE_EVENTS)
  305. kfree(ev);
  306. fh->queued_events[ev_idx]--;
  307. fh->total_queued_events--;
  308. unlock:
  309. mutex_unlock(&fh->lock);
  310. return err;
  311. }
  312. static long cec_g_mode(struct cec_adapter *adap, struct cec_fh *fh,
  313. u32 __user *parg)
  314. {
  315. u32 mode = fh->mode_initiator | fh->mode_follower;
  316. if (copy_to_user(parg, &mode, sizeof(mode)))
  317. return -EFAULT;
  318. return 0;
  319. }
  320. static long cec_s_mode(struct cec_adapter *adap, struct cec_fh *fh,
  321. u32 __user *parg)
  322. {
  323. u32 mode;
  324. u8 mode_initiator;
  325. u8 mode_follower;
  326. bool send_pin_event = false;
  327. long err = 0;
  328. if (copy_from_user(&mode, parg, sizeof(mode)))
  329. return -EFAULT;
  330. if (mode & ~(CEC_MODE_INITIATOR_MSK | CEC_MODE_FOLLOWER_MSK)) {
  331. dprintk(1, "%s: invalid mode bits set\n", __func__);
  332. return -EINVAL;
  333. }
  334. mode_initiator = mode & CEC_MODE_INITIATOR_MSK;
  335. mode_follower = mode & CEC_MODE_FOLLOWER_MSK;
  336. if (mode_initiator > CEC_MODE_EXCL_INITIATOR ||
  337. mode_follower > CEC_MODE_MONITOR_ALL) {
  338. dprintk(1, "%s: unknown mode\n", __func__);
  339. return -EINVAL;
  340. }
  341. if (mode_follower == CEC_MODE_MONITOR_ALL &&
  342. !(adap->capabilities & CEC_CAP_MONITOR_ALL)) {
  343. dprintk(1, "%s: MONITOR_ALL not supported\n", __func__);
  344. return -EINVAL;
  345. }
  346. if (mode_follower == CEC_MODE_MONITOR_PIN &&
  347. !(adap->capabilities & CEC_CAP_MONITOR_PIN)) {
  348. dprintk(1, "%s: MONITOR_PIN not supported\n", __func__);
  349. return -EINVAL;
  350. }
  351. /* Follower modes should always be able to send CEC messages */
  352. if ((mode_initiator == CEC_MODE_NO_INITIATOR ||
  353. !(adap->capabilities & CEC_CAP_TRANSMIT)) &&
  354. mode_follower >= CEC_MODE_FOLLOWER &&
  355. mode_follower <= CEC_MODE_EXCL_FOLLOWER_PASSTHRU) {
  356. dprintk(1, "%s: cannot transmit\n", __func__);
  357. return -EINVAL;
  358. }
  359. /* Monitor modes require CEC_MODE_NO_INITIATOR */
  360. if (mode_initiator && mode_follower >= CEC_MODE_MONITOR_PIN) {
  361. dprintk(1, "%s: monitor modes require NO_INITIATOR\n",
  362. __func__);
  363. return -EINVAL;
  364. }
  365. /* Monitor modes require CAP_NET_ADMIN */
  366. if (mode_follower >= CEC_MODE_MONITOR_PIN && !capable(CAP_NET_ADMIN))
  367. return -EPERM;
  368. mutex_lock(&adap->lock);
  369. /*
  370. * You can't become exclusive follower if someone else already
  371. * has that job.
  372. */
  373. if ((mode_follower == CEC_MODE_EXCL_FOLLOWER ||
  374. mode_follower == CEC_MODE_EXCL_FOLLOWER_PASSTHRU) &&
  375. adap->cec_follower && adap->cec_follower != fh)
  376. err = -EBUSY;
  377. /*
  378. * You can't become exclusive initiator if someone else already
  379. * has that job.
  380. */
  381. if (mode_initiator == CEC_MODE_EXCL_INITIATOR &&
  382. adap->cec_initiator && adap->cec_initiator != fh)
  383. err = -EBUSY;
  384. if (!err) {
  385. bool old_mon_all = fh->mode_follower == CEC_MODE_MONITOR_ALL;
  386. bool new_mon_all = mode_follower == CEC_MODE_MONITOR_ALL;
  387. if (old_mon_all != new_mon_all) {
  388. if (new_mon_all)
  389. err = cec_monitor_all_cnt_inc(adap);
  390. else
  391. cec_monitor_all_cnt_dec(adap);
  392. }
  393. }
  394. if (!err) {
  395. bool old_mon_pin = fh->mode_follower == CEC_MODE_MONITOR_PIN;
  396. bool new_mon_pin = mode_follower == CEC_MODE_MONITOR_PIN;
  397. if (old_mon_pin != new_mon_pin) {
  398. send_pin_event = new_mon_pin;
  399. if (new_mon_pin)
  400. err = cec_monitor_pin_cnt_inc(adap);
  401. else
  402. cec_monitor_pin_cnt_dec(adap);
  403. }
  404. }
  405. if (err) {
  406. mutex_unlock(&adap->lock);
  407. return err;
  408. }
  409. if (fh->mode_follower == CEC_MODE_FOLLOWER)
  410. adap->follower_cnt--;
  411. if (mode_follower == CEC_MODE_FOLLOWER)
  412. adap->follower_cnt++;
  413. if (send_pin_event) {
  414. struct cec_event ev = {
  415. .flags = CEC_EVENT_FL_INITIAL_STATE,
  416. };
  417. ev.event = adap->cec_pin_is_high ? CEC_EVENT_PIN_CEC_HIGH :
  418. CEC_EVENT_PIN_CEC_LOW;
  419. cec_queue_event_fh(fh, &ev, 0);
  420. }
  421. if (mode_follower == CEC_MODE_EXCL_FOLLOWER ||
  422. mode_follower == CEC_MODE_EXCL_FOLLOWER_PASSTHRU) {
  423. adap->passthrough =
  424. mode_follower == CEC_MODE_EXCL_FOLLOWER_PASSTHRU;
  425. adap->cec_follower = fh;
  426. } else if (adap->cec_follower == fh) {
  427. adap->passthrough = false;
  428. adap->cec_follower = NULL;
  429. }
  430. if (mode_initiator == CEC_MODE_EXCL_INITIATOR)
  431. adap->cec_initiator = fh;
  432. else if (adap->cec_initiator == fh)
  433. adap->cec_initiator = NULL;
  434. fh->mode_initiator = mode_initiator;
  435. fh->mode_follower = mode_follower;
  436. mutex_unlock(&adap->lock);
  437. return 0;
  438. }
  439. static long cec_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  440. {
  441. struct cec_fh *fh = filp->private_data;
  442. struct cec_adapter *adap = fh->adap;
  443. bool block = !(filp->f_flags & O_NONBLOCK);
  444. void __user *parg = (void __user *)arg;
  445. if (!cec_is_registered(adap))
  446. return -ENODEV;
  447. switch (cmd) {
  448. case CEC_ADAP_G_CAPS:
  449. return cec_adap_g_caps(adap, parg);
  450. case CEC_ADAP_G_PHYS_ADDR:
  451. return cec_adap_g_phys_addr(adap, parg);
  452. case CEC_ADAP_S_PHYS_ADDR:
  453. return cec_adap_s_phys_addr(adap, fh, block, parg);
  454. case CEC_ADAP_G_LOG_ADDRS:
  455. return cec_adap_g_log_addrs(adap, parg);
  456. case CEC_ADAP_S_LOG_ADDRS:
  457. return cec_adap_s_log_addrs(adap, fh, block, parg);
  458. case CEC_ADAP_G_CONNECTOR_INFO:
  459. return cec_adap_g_connector_info(adap, parg);
  460. case CEC_TRANSMIT:
  461. return cec_transmit(adap, fh, block, parg);
  462. case CEC_RECEIVE:
  463. return cec_receive(adap, fh, block, parg);
  464. case CEC_DQEVENT:
  465. return cec_dqevent(adap, fh, block, parg);
  466. case CEC_G_MODE:
  467. return cec_g_mode(adap, fh, parg);
  468. case CEC_S_MODE:
  469. return cec_s_mode(adap, fh, parg);
  470. default:
  471. return -ENOTTY;
  472. }
  473. }
  474. static int cec_open(struct inode *inode, struct file *filp)
  475. {
  476. struct cec_devnode *devnode =
  477. container_of(inode->i_cdev, struct cec_devnode, cdev);
  478. struct cec_adapter *adap = to_cec_adapter(devnode);
  479. struct cec_fh *fh = kzalloc(sizeof(*fh), GFP_KERNEL);
  480. /*
  481. * Initial events that are automatically sent when the cec device is
  482. * opened.
  483. */
  484. struct cec_event ev = {
  485. .event = CEC_EVENT_STATE_CHANGE,
  486. .flags = CEC_EVENT_FL_INITIAL_STATE,
  487. };
  488. unsigned int i;
  489. int err;
  490. if (!fh)
  491. return -ENOMEM;
  492. INIT_LIST_HEAD(&fh->msgs);
  493. INIT_LIST_HEAD(&fh->xfer_list);
  494. for (i = 0; i < CEC_NUM_EVENTS; i++)
  495. INIT_LIST_HEAD(&fh->events[i]);
  496. mutex_init(&fh->lock);
  497. init_waitqueue_head(&fh->wait);
  498. fh->mode_initiator = CEC_MODE_INITIATOR;
  499. fh->adap = adap;
  500. err = cec_get_device(adap);
  501. if (err) {
  502. kfree(fh);
  503. return err;
  504. }
  505. filp->private_data = fh;
  506. /* Queue up initial state events */
  507. ev.state_change.phys_addr = adap->phys_addr;
  508. ev.state_change.log_addr_mask = adap->log_addrs.log_addr_mask;
  509. ev.state_change.have_conn_info =
  510. adap->conn_info.type != CEC_CONNECTOR_TYPE_NO_CONNECTOR;
  511. cec_queue_event_fh(fh, &ev, 0);
  512. #ifdef CONFIG_CEC_PIN
  513. if (adap->pin && adap->pin->ops->read_hpd &&
  514. !adap->devnode.unregistered) {
  515. err = adap->pin->ops->read_hpd(adap);
  516. if (err >= 0) {
  517. ev.event = err ? CEC_EVENT_PIN_HPD_HIGH :
  518. CEC_EVENT_PIN_HPD_LOW;
  519. cec_queue_event_fh(fh, &ev, 0);
  520. }
  521. }
  522. if (adap->pin && adap->pin->ops->read_5v &&
  523. !adap->devnode.unregistered) {
  524. err = adap->pin->ops->read_5v(adap);
  525. if (err >= 0) {
  526. ev.event = err ? CEC_EVENT_PIN_5V_HIGH :
  527. CEC_EVENT_PIN_5V_LOW;
  528. cec_queue_event_fh(fh, &ev, 0);
  529. }
  530. }
  531. #endif
  532. mutex_lock(&devnode->lock);
  533. mutex_lock(&devnode->lock_fhs);
  534. list_add(&fh->list, &devnode->fhs);
  535. mutex_unlock(&devnode->lock_fhs);
  536. mutex_unlock(&devnode->lock);
  537. return 0;
  538. }
  539. /* Override for the release function */
  540. static int cec_release(struct inode *inode, struct file *filp)
  541. {
  542. struct cec_devnode *devnode = cec_devnode_data(filp);
  543. struct cec_adapter *adap = to_cec_adapter(devnode);
  544. struct cec_fh *fh = filp->private_data;
  545. unsigned int i;
  546. mutex_lock(&adap->lock);
  547. if (adap->cec_initiator == fh)
  548. adap->cec_initiator = NULL;
  549. if (adap->cec_follower == fh) {
  550. adap->cec_follower = NULL;
  551. adap->passthrough = false;
  552. }
  553. if (fh->mode_follower == CEC_MODE_FOLLOWER)
  554. adap->follower_cnt--;
  555. if (fh->mode_follower == CEC_MODE_MONITOR_PIN)
  556. cec_monitor_pin_cnt_dec(adap);
  557. if (fh->mode_follower == CEC_MODE_MONITOR_ALL)
  558. cec_monitor_all_cnt_dec(adap);
  559. mutex_unlock(&adap->lock);
  560. mutex_lock(&devnode->lock);
  561. mutex_lock(&devnode->lock_fhs);
  562. list_del(&fh->list);
  563. mutex_unlock(&devnode->lock_fhs);
  564. mutex_unlock(&devnode->lock);
  565. /* Unhook pending transmits from this filehandle. */
  566. mutex_lock(&adap->lock);
  567. while (!list_empty(&fh->xfer_list)) {
  568. struct cec_data *data =
  569. list_first_entry(&fh->xfer_list, struct cec_data, xfer_list);
  570. data->blocking = false;
  571. data->fh = NULL;
  572. list_del_init(&data->xfer_list);
  573. }
  574. mutex_unlock(&adap->lock);
  575. mutex_lock(&fh->lock);
  576. while (!list_empty(&fh->msgs)) {
  577. struct cec_msg_entry *entry =
  578. list_first_entry(&fh->msgs, struct cec_msg_entry, list);
  579. list_del(&entry->list);
  580. kfree(entry);
  581. }
  582. for (i = CEC_NUM_CORE_EVENTS; i < CEC_NUM_EVENTS; i++) {
  583. while (!list_empty(&fh->events[i])) {
  584. struct cec_event_entry *entry =
  585. list_first_entry(&fh->events[i],
  586. struct cec_event_entry, list);
  587. list_del(&entry->list);
  588. kfree(entry);
  589. }
  590. }
  591. mutex_unlock(&fh->lock);
  592. kfree(fh);
  593. cec_put_device(adap);
  594. filp->private_data = NULL;
  595. return 0;
  596. }
  597. const struct file_operations cec_devnode_fops = {
  598. .owner = THIS_MODULE,
  599. .open = cec_open,
  600. .unlocked_ioctl = cec_ioctl,
  601. .compat_ioctl = cec_ioctl,
  602. .release = cec_release,
  603. .poll = cec_poll,
  604. };