cec-api.c 17 KB

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