cec-ioc-receive.rst 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. .. SPDX-License-Identifier: GFDL-1.1-no-invariants-or-later
  2. .. c:namespace:: CEC
  3. .. _CEC_TRANSMIT:
  4. .. _CEC_RECEIVE:
  5. ***********************************
  6. ioctls CEC_RECEIVE and CEC_TRANSMIT
  7. ***********************************
  8. Name
  9. ====
  10. CEC_RECEIVE, CEC_TRANSMIT - Receive or transmit a CEC message
  11. Synopsis
  12. ========
  13. .. c:macro:: CEC_RECEIVE
  14. ``int ioctl(int fd, CEC_RECEIVE, struct cec_msg *argp)``
  15. .. c:macro:: CEC_TRANSMIT
  16. ``int ioctl(int fd, CEC_TRANSMIT, struct cec_msg *argp)``
  17. Arguments
  18. =========
  19. ``fd``
  20. File descriptor returned by :c:func:`open()`.
  21. ``argp``
  22. Pointer to struct cec_msg.
  23. Description
  24. ===========
  25. To receive a CEC message the application has to fill in the
  26. ``timeout`` field of struct :c:type:`cec_msg` and pass it to
  27. :ref:`ioctl CEC_RECEIVE <CEC_RECEIVE>`.
  28. If the file descriptor is in non-blocking mode and there are no received
  29. messages pending, then it will return -1 and set errno to the ``EAGAIN``
  30. error code. If the file descriptor is in blocking mode and ``timeout``
  31. is non-zero and no message arrived within ``timeout`` milliseconds, then
  32. it will return -1 and set errno to the ``ETIMEDOUT`` error code.
  33. A received message can be:
  34. 1. a message received from another CEC device (the ``sequence`` field will
  35. be 0, ``tx_status`` will be 0 and ``rx_status`` will be non-zero).
  36. 2. the transmit result of an earlier non-blocking transmit (the ``sequence``
  37. field will be non-zero, ``tx_status`` will be non-zero and ``rx_status``
  38. will be 0).
  39. 3. the reply to an earlier non-blocking transmit (the ``sequence`` field will
  40. be non-zero, ``tx_status`` will be 0 and ``rx_status`` will be non-zero).
  41. To send a CEC message the application has to fill in the struct
  42. :c:type:`cec_msg` and pass it to :ref:`ioctl CEC_TRANSMIT <CEC_TRANSMIT>`.
  43. The :ref:`ioctl CEC_TRANSMIT <CEC_TRANSMIT>` is only available if
  44. ``CEC_CAP_TRANSMIT`` is set. If there is no more room in the transmit
  45. queue, then it will return -1 and set errno to the ``EBUSY`` error code.
  46. The transmit queue has enough room for 18 messages (about 1 second worth
  47. of 2-byte messages). Note that the CEC kernel framework will also reply
  48. to core messages (see :ref:`cec-core-processing`), so it is not a good
  49. idea to fully fill up the transmit queue.
  50. If the file descriptor is in non-blocking mode then the transmit will
  51. return 0 and the result of the transmit will be available via
  52. :ref:`ioctl CEC_RECEIVE <CEC_RECEIVE>` once the transmit has finished.
  53. If a non-blocking transmit also specified waiting for a reply, then
  54. the reply will arrive in a later message. The ``sequence`` field can
  55. be used to associate both transmit results and replies with the original
  56. transmit.
  57. Normally calling :ref:`ioctl CEC_TRANSMIT <CEC_TRANSMIT>` when the physical
  58. address is invalid (due to e.g. a disconnect) will return ``ENONET``.
  59. However, the CEC specification allows sending messages from 'Unregistered' to
  60. 'TV' when the physical address is invalid since some TVs pull the hotplug detect
  61. pin of the HDMI connector low when they go into standby, or when switching to
  62. another input.
  63. When the hotplug detect pin goes low the EDID disappears, and thus the
  64. physical address, but the cable is still connected and CEC still works.
  65. In order to detect/wake up the device it is allowed to send poll and 'Image/Text
  66. View On' messages from initiator 0xf ('Unregistered') to destination 0 ('TV').
  67. .. tabularcolumns:: |p{1.0cm}|p{3.5cm}|p{12.8cm}|
  68. .. c:type:: cec_msg
  69. .. cssclass:: longtable
  70. .. flat-table:: struct cec_msg
  71. :header-rows: 0
  72. :stub-columns: 0
  73. :widths: 1 1 16
  74. * - __u64
  75. - ``tx_ts``
  76. - Timestamp in ns of when the last byte of the message was transmitted.
  77. The timestamp has been taken from the ``CLOCK_MONOTONIC`` clock. To access
  78. the same clock from userspace use :c:func:`clock_gettime`.
  79. * - __u64
  80. - ``rx_ts``
  81. - Timestamp in ns of when the last byte of the message was received.
  82. The timestamp has been taken from the ``CLOCK_MONOTONIC`` clock. To access
  83. the same clock from userspace use :c:func:`clock_gettime`.
  84. * - __u32
  85. - ``len``
  86. - The length of the message. For :ref:`ioctl CEC_TRANSMIT <CEC_TRANSMIT>` this is filled in
  87. by the application. The driver will fill this in for
  88. :ref:`ioctl CEC_RECEIVE <CEC_RECEIVE>`. For :ref:`ioctl CEC_TRANSMIT <CEC_TRANSMIT>` it will be
  89. filled in by the driver with the length of the reply message if ``reply`` was set.
  90. * - __u32
  91. - ``timeout``
  92. - The timeout in milliseconds. This is the time the device will wait
  93. for a message to be received before timing out. If it is set to 0,
  94. then it will wait indefinitely when it is called by :ref:`ioctl CEC_RECEIVE <CEC_RECEIVE>`.
  95. If it is 0 and it is called by :ref:`ioctl CEC_TRANSMIT <CEC_TRANSMIT>`,
  96. then it will be replaced by 1000 if the ``reply`` is non-zero or
  97. ignored if ``reply`` is 0.
  98. * - __u32
  99. - ``sequence``
  100. - A non-zero sequence number is automatically assigned by the CEC framework
  101. for all transmitted messages. It is used by the CEC framework when it queues
  102. the transmit result for a non-blocking transmit. This allows the application
  103. to associate the received message with the original transmit.
  104. In addition, if a non-blocking transmit will wait for a reply (ii.e. ``timeout``
  105. was not 0), then the ``sequence`` field of the reply will be set to the sequence
  106. value of the original transmit. This allows the application to associate the
  107. received message with the original transmit.
  108. * - __u32
  109. - ``flags``
  110. - Flags. See :ref:`cec-msg-flags` for a list of available flags.
  111. * - __u8
  112. - ``msg[16]``
  113. - The message payload. For :ref:`ioctl CEC_TRANSMIT <CEC_TRANSMIT>` this is filled in by the
  114. application. The driver will fill this in for :ref:`ioctl CEC_RECEIVE <CEC_RECEIVE>`.
  115. For :ref:`ioctl CEC_TRANSMIT <CEC_TRANSMIT>` it will be filled in by the driver with
  116. the payload of the reply message if ``timeout`` was set.
  117. * - __u8
  118. - ``reply``
  119. - Wait until this message is replied. If ``reply`` is 0 and the
  120. ``timeout`` is 0, then don't wait for a reply but return after
  121. transmitting the message. Ignored by :ref:`ioctl CEC_RECEIVE <CEC_RECEIVE>`.
  122. The case where ``reply`` is 0 (this is the opcode for the Feature Abort
  123. message) and ``timeout`` is non-zero is specifically allowed to make it
  124. possible to send a message and wait up to ``timeout`` milliseconds for a
  125. Feature Abort reply. In this case ``rx_status`` will either be set
  126. to :ref:`CEC_RX_STATUS_TIMEOUT <CEC-RX-STATUS-TIMEOUT>` or
  127. :ref:`CEC_RX_STATUS_FEATURE_ABORT <CEC-RX-STATUS-FEATURE-ABORT>`.
  128. If the transmitter message is ``CEC_MSG_INITIATE_ARC`` then the ``reply``
  129. values ``CEC_MSG_REPORT_ARC_INITIATED`` and ``CEC_MSG_REPORT_ARC_TERMINATED``
  130. are processed differently: either value will match both possible replies.
  131. The reason is that the ``CEC_MSG_INITIATE_ARC`` message is the only CEC
  132. message that has two possible replies other than Feature Abort. The
  133. ``reply`` field will be updated with the actual reply so that it is
  134. synchronized with the contents of the received message.
  135. * - __u8
  136. - ``rx_status``
  137. - The status bits of the received message. See
  138. :ref:`cec-rx-status` for the possible status values.
  139. * - __u8
  140. - ``tx_status``
  141. - The status bits of the transmitted message. See
  142. :ref:`cec-tx-status` for the possible status values.
  143. When calling :ref:`ioctl CEC_TRANSMIT <CEC_TRANSMIT>` in non-blocking mode,
  144. this field will be 0 if the transmit started, or non-0 if the transmit
  145. result is known immediately. The latter would be the case when attempting
  146. to transmit a Poll message to yourself. That results in a
  147. :ref:`CEC_TX_STATUS_NACK <CEC-TX-STATUS-NACK>` without ever actually
  148. transmitting the Poll message.
  149. * - __u8
  150. - ``tx_arb_lost_cnt``
  151. - A counter of the number of transmit attempts that resulted in the
  152. Arbitration Lost error. This is only set if the hardware supports
  153. this, otherwise it is always 0. This counter is only valid if the
  154. :ref:`CEC_TX_STATUS_ARB_LOST <CEC-TX-STATUS-ARB-LOST>` status bit is set.
  155. * - __u8
  156. - ``tx_nack_cnt``
  157. - A counter of the number of transmit attempts that resulted in the
  158. Not Acknowledged error. This is only set if the hardware supports
  159. this, otherwise it is always 0. This counter is only valid if the
  160. :ref:`CEC_TX_STATUS_NACK <CEC-TX-STATUS-NACK>` status bit is set.
  161. * - __u8
  162. - ``tx_low_drive_cnt``
  163. - A counter of the number of transmit attempts that resulted in the
  164. Arbitration Lost error. This is only set if the hardware supports
  165. this, otherwise it is always 0. This counter is only valid if the
  166. :ref:`CEC_TX_STATUS_LOW_DRIVE <CEC-TX-STATUS-LOW-DRIVE>` status bit is set.
  167. * - __u8
  168. - ``tx_error_cnt``
  169. - A counter of the number of transmit errors other than Arbitration
  170. Lost or Not Acknowledged. This is only set if the hardware
  171. supports this, otherwise it is always 0. This counter is only
  172. valid if the :ref:`CEC_TX_STATUS_ERROR <CEC-TX-STATUS-ERROR>` status bit is set.
  173. .. tabularcolumns:: |p{6.2cm}|p{1.0cm}|p{10.1cm}|
  174. .. _cec-msg-flags:
  175. .. flat-table:: Flags for struct cec_msg
  176. :header-rows: 0
  177. :stub-columns: 0
  178. :widths: 3 1 4
  179. * .. _`CEC-MSG-FL-REPLY-TO-FOLLOWERS`:
  180. - ``CEC_MSG_FL_REPLY_TO_FOLLOWERS``
  181. - 1
  182. - If a CEC transmit expects a reply, then by default that reply is only sent to
  183. the filehandle that called :ref:`ioctl CEC_TRANSMIT <CEC_TRANSMIT>`. If this
  184. flag is set, then the reply is also sent to all followers, if any. If the
  185. filehandle that called :ref:`ioctl CEC_TRANSMIT <CEC_TRANSMIT>` is also a
  186. follower, then that filehandle will receive the reply twice: once as the
  187. result of the :ref:`ioctl CEC_TRANSMIT <CEC_TRANSMIT>`, and once via
  188. :ref:`ioctl CEC_RECEIVE <CEC_RECEIVE>`.
  189. * .. _`CEC-MSG-FL-RAW`:
  190. - ``CEC_MSG_FL_RAW``
  191. - 2
  192. - Normally CEC messages are validated before transmitting them. If this
  193. flag is set when :ref:`ioctl CEC_TRANSMIT <CEC_TRANSMIT>` is called,
  194. then no validation takes place and the message is transmitted as-is.
  195. This is useful when debugging CEC issues.
  196. This flag is only allowed if the process has the ``CAP_SYS_RAWIO``
  197. capability. If that is not set, then the ``EPERM`` error code is
  198. returned.
  199. * .. _`CEC-MSG-FL-REPLY-VENDOR-ID`:
  200. - ``CEC_MSG_FL_REPLY_VENDOR_ID``
  201. - 4
  202. - This flag is only available if the ``CEC_CAP_REPLY_VENDOR_ID`` capability
  203. is set. If this flag is set, then the reply is expected to consist of
  204. the ``CEC_MSG_VENDOR_COMMAND_WITH_ID`` opcode followed by the Vendor ID
  205. (in bytes 1-4 of the message), followed by the ``struct cec_msg``
  206. ``reply`` field.
  207. Note that this assumes that the byte after the Vendor ID is a
  208. vendor-specific opcode.
  209. This flag makes it easier to wait for replies to vendor commands.
  210. .. tabularcolumns:: |p{5.6cm}|p{0.9cm}|p{10.8cm}|
  211. .. _cec-tx-status:
  212. .. flat-table:: CEC Transmit Status
  213. :header-rows: 0
  214. :stub-columns: 0
  215. :widths: 3 1 16
  216. * .. _`CEC-TX-STATUS-OK`:
  217. - ``CEC_TX_STATUS_OK``
  218. - 0x01
  219. - The message was transmitted successfully. This is mutually
  220. exclusive with :ref:`CEC_TX_STATUS_MAX_RETRIES <CEC-TX-STATUS-MAX-RETRIES>`.
  221. Other bits can still be set if earlier attempts met with failure before
  222. the transmit was eventually successful.
  223. * .. _`CEC-TX-STATUS-ARB-LOST`:
  224. - ``CEC_TX_STATUS_ARB_LOST``
  225. - 0x02
  226. - CEC line arbitration was lost, i.e. another transmit started at the
  227. same time with a higher priority. Optional status, not all hardware
  228. can detect this error condition.
  229. * .. _`CEC-TX-STATUS-NACK`:
  230. - ``CEC_TX_STATUS_NACK``
  231. - 0x04
  232. - Message was not acknowledged. Note that some hardware cannot tell apart
  233. a 'Not Acknowledged' status from other error conditions, i.e. the result
  234. of a transmit is just OK or FAIL. In that case this status will be
  235. returned when the transmit failed.
  236. * .. _`CEC-TX-STATUS-LOW-DRIVE`:
  237. - ``CEC_TX_STATUS_LOW_DRIVE``
  238. - 0x08
  239. - Low drive was detected on the CEC bus. This indicates that a
  240. follower detected an error on the bus and requests a
  241. retransmission. Optional status, not all hardware can detect this
  242. error condition.
  243. * .. _`CEC-TX-STATUS-ERROR`:
  244. - ``CEC_TX_STATUS_ERROR``
  245. - 0x10
  246. - Some error occurred. This is used for any errors that do not fit
  247. ``CEC_TX_STATUS_ARB_LOST`` or ``CEC_TX_STATUS_LOW_DRIVE``, either because
  248. the hardware could not tell which error occurred, or because the hardware
  249. tested for other conditions besides those two. Optional status.
  250. * .. _`CEC-TX-STATUS-MAX-RETRIES`:
  251. - ``CEC_TX_STATUS_MAX_RETRIES``
  252. - 0x20
  253. - The transmit failed after one or more retries. This status bit is
  254. mutually exclusive with :ref:`CEC_TX_STATUS_OK <CEC-TX-STATUS-OK>`.
  255. Other bits can still be set to explain which failures were seen.
  256. * .. _`CEC-TX-STATUS-ABORTED`:
  257. - ``CEC_TX_STATUS_ABORTED``
  258. - 0x40
  259. - The transmit was aborted due to an HDMI disconnect, or the adapter
  260. was unconfigured, or a transmit was interrupted, or the driver
  261. returned an error when attempting to start a transmit.
  262. * .. _`CEC-TX-STATUS-TIMEOUT`:
  263. - ``CEC_TX_STATUS_TIMEOUT``
  264. - 0x80
  265. - The transmit timed out. This should not normally happen and this
  266. indicates a driver problem.
  267. .. tabularcolumns:: |p{5.6cm}|p{0.9cm}|p{10.8cm}|
  268. .. _cec-rx-status:
  269. .. flat-table:: CEC Receive Status
  270. :header-rows: 0
  271. :stub-columns: 0
  272. :widths: 3 1 16
  273. * .. _`CEC-RX-STATUS-OK`:
  274. - ``CEC_RX_STATUS_OK``
  275. - 0x01
  276. - The message was received successfully.
  277. * .. _`CEC-RX-STATUS-TIMEOUT`:
  278. - ``CEC_RX_STATUS_TIMEOUT``
  279. - 0x02
  280. - The reply to an earlier transmitted message timed out.
  281. * .. _`CEC-RX-STATUS-FEATURE-ABORT`:
  282. - ``CEC_RX_STATUS_FEATURE_ABORT``
  283. - 0x04
  284. - The message was received successfully but the reply was
  285. ``CEC_MSG_FEATURE_ABORT``. This status is only set if this message
  286. was the reply to an earlier transmitted message.
  287. * .. _`CEC-RX-STATUS-ABORTED`:
  288. - ``CEC_RX_STATUS_ABORTED``
  289. - 0x08
  290. - The wait for a reply to an earlier transmitted message was aborted
  291. because the HDMI cable was disconnected, the adapter was unconfigured
  292. or the :ref:`CEC_TRANSMIT <CEC_RECEIVE>` that waited for a
  293. reply was interrupted.
  294. Return Value
  295. ============
  296. On success 0 is returned, on error -1 and the ``errno`` variable is set
  297. appropriately. The generic error codes are described at the
  298. :ref:`Generic Error Codes <gen-errors>` chapter.
  299. The :ref:`ioctl CEC_RECEIVE <CEC_RECEIVE>` can return the following
  300. error codes:
  301. EAGAIN
  302. No messages are in the receive queue, and the filehandle is in non-blocking mode.
  303. ETIMEDOUT
  304. The ``timeout`` was reached while waiting for a message.
  305. ERESTARTSYS
  306. The wait for a message was interrupted (e.g. by Ctrl-C).
  307. The :ref:`ioctl CEC_TRANSMIT <CEC_TRANSMIT>` can return the following
  308. error codes:
  309. ENOTTY
  310. The ``CEC_CAP_TRANSMIT`` capability wasn't set, so this ioctl is not supported.
  311. EPERM
  312. The CEC adapter is not configured, i.e. :ref:`ioctl CEC_ADAP_S_LOG_ADDRS <CEC_ADAP_S_LOG_ADDRS>`
  313. has never been called, or ``CEC_MSG_FL_RAW`` was used from a process that
  314. did not have the ``CAP_SYS_RAWIO`` capability.
  315. ENONET
  316. The CEC adapter is not configured, i.e. :ref:`ioctl CEC_ADAP_S_LOG_ADDRS <CEC_ADAP_S_LOG_ADDRS>`
  317. was called, but the physical address is invalid so no logical address was claimed.
  318. An exception is made in this case for transmits from initiator 0xf ('Unregistered')
  319. to destination 0 ('TV'). In that case the transmit will proceed as usual.
  320. EBUSY
  321. Another filehandle is in exclusive follower or initiator mode, or the filehandle
  322. is in mode ``CEC_MODE_NO_INITIATOR``. This is also returned if the transmit
  323. queue is full.
  324. EINVAL
  325. The contents of struct :c:type:`cec_msg` is invalid.
  326. ERESTARTSYS
  327. The wait for a successful transmit was interrupted (e.g. by Ctrl-C).