libcec-00-imx6-support.patch 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  1. From a7f3315333c1a79259a763c6542a6cd71855b357 Mon Sep 17 00:00:00 2001
  2. From: Stefan Saraev <stefan@saraev.ca>
  3. Date: Thu, 7 May 2015 11:35:04 +0300
  4. Subject: [PATCH] imx support
  5. note. I am NOT the author of the patch. just rebased it
  6. ---
  7. include/cectypes.h | 14 +-
  8. src/libcec/CECTypeUtils.h | 2 +
  9. src/libcec/adapter/AdapterFactory.cpp | 29 +-
  10. src/libcec/adapter/IMX/AdapterMessageQueue.h | 134 +++++++++
  11. .../adapter/IMX/IMXCECAdapterCommunication.cpp | 328 +++++++++++++++++++++
  12. .../adapter/IMX/IMXCECAdapterCommunication.h | 119 ++++++++
  13. src/libcec/adapter/IMX/IMXCECAdapterDetection.cpp | 42 +++
  14. src/libcec/adapter/IMX/IMXCECAdapterDetection.h | 36 +++
  15. src/libcec/cmake/CheckPlatformSupport.cmake | 10 +
  16. src/libcec/cmake/DisplayPlatformSupport.cmake | 6 +
  17. src/libcec/env.h.in | 3 +
  18. 11 files changed, 720 insertions(+), 3 deletions(-)
  19. create mode 100644 src/libcec/adapter/IMX/AdapterMessageQueue.h
  20. create mode 100644 src/libcec/adapter/IMX/IMXCECAdapterCommunication.cpp
  21. create mode 100644 src/libcec/adapter/IMX/IMXCECAdapterCommunication.h
  22. create mode 100644 src/libcec/adapter/IMX/IMXCECAdapterDetection.cpp
  23. create mode 100644 src/libcec/adapter/IMX/IMXCECAdapterDetection.h
  24. diff --git a/include/cectypes.h b/include/cectypes.h
  25. index acff259..0cfee8c 100644
  26. --- a/include/cectypes.h
  27. +++ b/include/cectypes.h
  28. @@ -309,6 +309,17 @@ namespace CEC {
  29. #define CEC_EXYNOS_VIRTUAL_COM "Exynos"
  30. /*!
  31. + * the path to use for the i.MX CEC wire
  32. + */
  33. +#define CEC_IMX_PATH "/dev/mxc_hdmi_cec"
  34. +
  35. +/*!
  36. + * the name of the virtual COM port to use for the i.MX CEC wire
  37. + */
  38. +#define CEC_IMX_VIRTUAL_COM "i.MX"
  39. +
  40. +
  41. +/*!
  42. * Mimimum client version
  43. */
  44. #define CEC_MIN_LIB_VERSION 3
  45. @@ -876,7 +887,8 @@ typedef enum cec_adapter_type
  46. ADAPTERTYPE_P8_DAUGHTERBOARD = 0x2,
  47. ADAPTERTYPE_RPI = 0x100,
  48. ADAPTERTYPE_TDA995x = 0x200,
  49. - ADAPTERTYPE_EXYNOS = 0x300
  50. + ADAPTERTYPE_EXYNOS = 0x300,
  51. + ADAPTERTYPE_IMX = 0x400
  52. } cec_adapter_type;
  53. /** force exporting through swig */
  54. diff --git a/src/libcec/CECTypeUtils.h b/src/libcec/CECTypeUtils.h
  55. index fd8046a..48cb215 100644
  56. --- a/src/libcec/CECTypeUtils.h
  57. +++ b/src/libcec/CECTypeUtils.h
  58. @@ -765,6 +765,8 @@ namespace CEC
  59. return "Raspberry Pi";
  60. case ADAPTERTYPE_TDA995x:
  61. return "TDA995x";
  62. + case ADAPTERTYPE_IMX:
  63. + return "i.MX";
  64. default:
  65. return "unknown";
  66. }
  67. diff --git a/src/libcec/adapter/AdapterFactory.cpp b/src/libcec/adapter/AdapterFactory.cpp
  68. index da05725..92e378b 100644
  69. --- a/src/libcec/adapter/AdapterFactory.cpp
  70. +++ b/src/libcec/adapter/AdapterFactory.cpp
  71. @@ -58,6 +58,11 @@
  72. #include "Exynos/ExynosCECAdapterCommunication.h"
  73. #endif
  74. +#if defined(HAVE_IMX_API)
  75. +#include "IMX/IMXCECAdapterDetection.h"
  76. +#include "IMX/IMXCECAdapterCommunication.h"
  77. +#endif
  78. +
  79. using namespace CEC;
  80. int8_t CAdapterFactory::FindAdapters(cec_adapter *deviceList, uint8_t iBufSize, const char *strDevicePath /* = NULL */)
  81. @@ -127,7 +132,22 @@ int8_t CAdapterFactory::DetectAdapters(cec_adapter_descriptor *deviceList, uint8
  82. #endif
  83. -#if !defined(HAVE_RPI_API) && !defined(HAVE_P8_USB) && !defined(HAVE_TDA995X_API)
  84. +
  85. +#if defined(HAVE_IMX_API)
  86. + if (iAdaptersFound < iBufSize && CIMXCECAdapterDetection::FindAdapter() &&
  87. + (!strDevicePath || !strcmp(strDevicePath, CEC_IMX_VIRTUAL_COM)))
  88. + {
  89. + snprintf(deviceList[iAdaptersFound].strComPath, sizeof(deviceList[iAdaptersFound].strComPath), CEC_IMX_PATH);
  90. + snprintf(deviceList[iAdaptersFound].strComName, sizeof(deviceList[iAdaptersFound].strComName), CEC_IMX_VIRTUAL_COM);
  91. + deviceList[iAdaptersFound].iVendorId = IMX_ADAPTER_VID;
  92. + deviceList[iAdaptersFound].iProductId = IMX_ADAPTER_PID;
  93. + deviceList[iAdaptersFound].adapterType = ADAPTERTYPE_IMX;
  94. + iAdaptersFound++;
  95. + }
  96. +#endif
  97. +
  98. +
  99. +#if !defined(HAVE_RPI_API) && !defined(HAVE_P8_USB) && !defined(HAVE_TDA995X_API) && !defined(HAVE_IMX_API)
  100. #error "libCEC doesn't have support for any type of adapter. please check your build system or configuration"
  101. #endif
  102. @@ -151,11 +171,16 @@ IAdapterCommunication *CAdapterFactory::GetInstance(const char *strPort, uint16_
  103. return new CRPiCECAdapterCommunication(m_lib->m_cec);
  104. #endif
  105. +#if defined(HAVE_IMX_API)
  106. + if (!strcmp(strPort, CEC_IMX_VIRTUAL_COM))
  107. + return new CIMXCECAdapterCommunication(m_lib->m_cec);
  108. +#endif
  109. +
  110. #if defined(HAVE_P8_USB)
  111. return new CUSBCECAdapterCommunication(m_lib->m_cec, strPort, iBaudRate);
  112. #endif
  113. -#if !defined(HAVE_RPI_API) && !defined(HAVE_P8_USB) && !defined(HAVE_TDA995X_API) && !defined(HAVE_EXYNOS_API)
  114. +#if !defined(HAVE_RPI_API) && !defined(HAVE_P8_USB) && !defined(HAVE_TDA995X_API) && !defined(HAVE_EXYNOS_API) && !defined(HAVE_IMX_API)
  115. return NULL;
  116. #endif
  117. }
  118. diff --git a/src/libcec/adapter/IMX/AdapterMessageQueue.h b/src/libcec/adapter/IMX/AdapterMessageQueue.h
  119. new file mode 100644
  120. index 0000000..af6742c
  121. --- /dev/null
  122. +++ b/src/libcec/adapter/IMX/AdapterMessageQueue.h
  123. @@ -0,0 +1,134 @@
  124. +#pragma once
  125. +/*
  126. + * This file is part of the libCEC(R) library.
  127. + *
  128. + * libCEC(R) is Copyright (C) 2011-2013 Pulse-Eight Limited. All rights reserved.
  129. + * libCEC(R) is an original work, containing original code.
  130. + *
  131. + * libCEC(R) is a trademark of Pulse-Eight Limited.
  132. + *
  133. + * This program is dual-licensed; you can redistribute it and/or modify
  134. + * it under the terms of the GNU General Public License as published by
  135. + * the Free Software Foundation; either version 2 of the License, or
  136. + * (at your option) any later version.
  137. + *
  138. + * This program is distributed in the hope that it will be useful,
  139. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  140. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  141. + * GNU General Public License for more details.
  142. + *
  143. + * You should have received a copy of the GNU General Public License
  144. + * along with this program; if not, write to the Free Software
  145. + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  146. + *
  147. + *
  148. + * Alternatively, you can license this library under a commercial license,
  149. + * please contact Pulse-Eight Licensing for more information.
  150. + *
  151. + * For more information contact:
  152. + * Pulse-Eight Licensing <license@pulse-eight.com>
  153. + * http://www.pulse-eight.com/
  154. + * http://www.pulse-eight.net/
  155. + */
  156. +
  157. +#include "platform/threads/mutex.h"
  158. +
  159. +namespace CEC
  160. +{
  161. + using namespace PLATFORM;
  162. +
  163. + class CAdapterMessageQueueEntry
  164. + {
  165. + public:
  166. + CAdapterMessageQueueEntry(const cec_command &command)
  167. + : m_bWaiting(true), m_retval((uint32_t)-1), m_bSucceeded(false)
  168. + {
  169. + m_hash = hashValue(
  170. + uint32_t(command.opcode_set ? command.opcode : CEC_OPCODE_NONE),
  171. + command.initiator, command.destination);
  172. + }
  173. +
  174. + virtual ~CAdapterMessageQueueEntry(void) {}
  175. +
  176. + /*!
  177. + * @brief Query result from worker thread
  178. + */
  179. + uint32_t Result() const
  180. + {
  181. + return m_retval;
  182. + }
  183. +
  184. + /*!
  185. + * @brief Signal waiting threads
  186. + */
  187. + void Broadcast(void)
  188. + {
  189. + CLockObject lock(m_mutex);
  190. + m_condition.Broadcast();
  191. + }
  192. +
  193. + /*!
  194. + * @brief Signal waiting thread(s) when message matches this entry
  195. + */
  196. + bool CheckMatch(uint32_t opcode, cec_logical_address initiator,
  197. + cec_logical_address destination, uint32_t response)
  198. + {
  199. + uint32_t hash = hashValue(opcode, initiator, destination);
  200. +
  201. + if (hash == m_hash)
  202. + {
  203. + CLockObject lock(m_mutex);
  204. +
  205. + m_retval = response;
  206. + m_bSucceeded = true;
  207. + m_condition.Signal();
  208. + return true;
  209. + }
  210. +
  211. + return false;
  212. + }
  213. +
  214. + /*!
  215. + * @brief Wait for a response to this command.
  216. + * @param iTimeout The timeout to use while waiting.
  217. + * @return True when a response was received before the timeout passed, false otherwise.
  218. + */
  219. + bool Wait(uint32_t iTimeout)
  220. + {
  221. + CLockObject lock(m_mutex);
  222. +
  223. + bool bReturn = m_bSucceeded ? true : m_condition.Wait(m_mutex, m_bSucceeded, iTimeout);
  224. + m_bWaiting = false;
  225. + return bReturn;
  226. + }
  227. +
  228. + /*!
  229. + * @return True while a thread is waiting for a signal or isn't waiting yet, false otherwise.
  230. + */
  231. + bool IsWaiting(void)
  232. + {
  233. + CLockObject lock(m_mutex);
  234. + return m_bWaiting;
  235. + }
  236. +
  237. + /*!
  238. + * @return Hash value for given cec_command
  239. + */
  240. + static uint32_t hashValue(uint32_t opcode,
  241. + cec_logical_address initiator,
  242. + cec_logical_address destination)
  243. + {
  244. + return 1 | ((uint32_t)initiator << 8) |
  245. + ((uint32_t)destination << 16) | ((uint32_t)opcode << 16);
  246. + }
  247. +
  248. + private:
  249. + bool m_bWaiting; /**< true while a thread is waiting or when it hasn't started waiting yet */
  250. + PLATFORM::CCondition<bool> m_condition; /**< the condition to wait on */
  251. + PLATFORM::CMutex m_mutex; /**< mutex for changes to this class */
  252. + uint32_t m_hash;
  253. + uint32_t m_retval;
  254. + bool m_bSucceeded;
  255. + };
  256. +
  257. +};
  258. diff --git a/src/libcec/adapter/IMX/IMXCECAdapterCommunication.cpp b/src/libcec/adapter/IMX/IMXCECAdapterCommunication.cpp
  259. new file mode 100644
  260. index 0000000..2daa8cb
  261. --- /dev/null
  262. +++ b/src/libcec/adapter/IMX/IMXCECAdapterCommunication.cpp
  263. @@ -0,0 +1,328 @@
  264. +/*
  265. + * This file is part of the libCEC(R) library.
  266. + *
  267. + * libCEC(R) is Copyright (C) 2011-2013 Pulse-Eight Limited. All rights reserved.
  268. + * libCEC(R) is an original work, containing original code.
  269. + *
  270. + * libCEC(R) is a trademark of Pulse-Eight Limited.
  271. + *
  272. + * IMX adpater port is Copyright (C) 2013 by Stephan Rafin
  273. + *
  274. + * You can redistribute this file and/or modify
  275. + * it under the terms of the GNU General Public License as published by
  276. + * the Free Software Foundation; either version 2 of the License, or
  277. + * (at your option) any later version.
  278. + *
  279. + * This program is distributed in the hope that it will be useful,
  280. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  281. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  282. + * GNU General Public License for more details.
  283. + *
  284. + * You should have received a copy of the GNU General Public License
  285. + * along with this program; if not, write to the Free Software
  286. + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  287. + *
  288. + *
  289. + */
  290. +
  291. +#include "env.h"
  292. +
  293. +#if defined(HAVE_IMX_API)
  294. +#include "IMXCECAdapterCommunication.h"
  295. +
  296. +#include "CECTypeUtils.h"
  297. +#include "LibCEC.h"
  298. +#include "platform/sockets/cdevsocket.h"
  299. +#include "platform/util/StdString.h"
  300. +#include "platform/util/buffer.h"
  301. +
  302. +/*
  303. + * Ioctl definitions from kernel header
  304. + */
  305. +#define HDMICEC_IOC_MAGIC 'H'
  306. +#define HDMICEC_IOC_SETLOGICALADDRESS _IOW(HDMICEC_IOC_MAGIC, 1, unsigned char)
  307. +#define HDMICEC_IOC_STARTDEVICE _IO(HDMICEC_IOC_MAGIC, 2)
  308. +#define HDMICEC_IOC_STOPDEVICE _IO(HDMICEC_IOC_MAGIC, 3)
  309. +#define HDMICEC_IOC_GETPHYADDRESS _IOR(HDMICEC_IOC_MAGIC, 4, unsigned char[4])
  310. +
  311. +#define MAX_CEC_MESSAGE_LEN 17
  312. +
  313. +#define MESSAGE_TYPE_RECEIVE_SUCCESS 1
  314. +#define MESSAGE_TYPE_NOACK 2
  315. +#define MESSAGE_TYPE_DISCONNECTED 3
  316. +#define MESSAGE_TYPE_CONNECTED 4
  317. +#define MESSAGE_TYPE_SEND_SUCCESS 5
  318. +
  319. +typedef struct hdmi_cec_event{
  320. + int event_type;
  321. + int msg_len;
  322. + unsigned char msg[MAX_CEC_MESSAGE_LEN];
  323. +}hdmi_cec_event;
  324. +
  325. +
  326. +using namespace std;
  327. +using namespace CEC;
  328. +using namespace PLATFORM;
  329. +
  330. +#include "AdapterMessageQueue.h"
  331. +
  332. +#define LIB_CEC m_callback->GetLib()
  333. +
  334. +// these are defined in nxp private header file
  335. +#define CEC_MSG_SUCCESS 0x00 /*Message transmisson Succeed*/
  336. +#define CEC_CSP_OFF_STATE 0x80 /*CSP in Off State*/
  337. +#define CEC_BAD_REQ_SERVICE 0x81 /*Bad .req service*/
  338. +#define CEC_MSG_FAIL_UNABLE_TO_ACCESS 0x82 /*Message transmisson failed: Unable to access CEC line*/
  339. +#define CEC_MSG_FAIL_ARBITRATION_ERROR 0x83 /*Message transmisson failed: Arbitration error*/
  340. +#define CEC_MSG_FAIL_BIT_TIMMING_ERROR 0x84 /*Message transmisson failed: Bit timming error*/
  341. +#define CEC_MSG_FAIL_DEST_NOT_ACK 0x85 /*Message transmisson failed: Destination Address not aknowledged*/
  342. +#define CEC_MSG_FAIL_DATA_NOT_ACK 0x86 /*Message transmisson failed: Databyte not acknowledged*/
  343. +
  344. +
  345. +CIMXCECAdapterCommunication::CIMXCECAdapterCommunication(IAdapterCommunicationCallback *callback) :
  346. + IAdapterCommunication(callback)/*,
  347. + m_bLogicalAddressChanged(false)*/
  348. +{
  349. + CLockObject lock(m_mutex);
  350. +
  351. + m_iNextMessage = 0;
  352. + //m_logicalAddresses.Clear();
  353. + m_logicalAddress = CECDEVICE_UNKNOWN;
  354. + m_bLogicalAddressRegistered = false;
  355. + m_bInitialised = false;
  356. + m_dev = new CCDevSocket(CEC_IMX_PATH);
  357. +}
  358. +
  359. +CIMXCECAdapterCommunication::~CIMXCECAdapterCommunication(void)
  360. +{
  361. + Close();
  362. +
  363. + CLockObject lock(m_mutex);
  364. + delete m_dev;
  365. + m_dev = 0;
  366. +}
  367. +
  368. +bool CIMXCECAdapterCommunication::IsOpen(void)
  369. +{
  370. + return IsInitialised() && m_dev->IsOpen();
  371. +}
  372. +
  373. +bool CIMXCECAdapterCommunication::Open(uint32_t iTimeoutMs, bool UNUSED(bSkipChecks), bool bStartListening)
  374. +{
  375. + if (m_dev->Open(iTimeoutMs))
  376. + {
  377. + if (!bStartListening || CreateThread()) {
  378. + if (m_dev->Ioctl(HDMICEC_IOC_STARTDEVICE, NULL) == 0) {
  379. + m_bInitialised = true;
  380. + return true;
  381. + }
  382. + LIB_CEC->AddLog(CEC_LOG_ERROR, "%s: Unable to start device\n", __func__);
  383. + }
  384. + m_dev->Close();
  385. + }
  386. +
  387. + return false;
  388. +}
  389. +
  390. +
  391. +void CIMXCECAdapterCommunication::Close(void)
  392. +{
  393. + StopThread(0);
  394. +
  395. + CLockObject lock(m_mutex);
  396. + if (!m_bInitialised) {
  397. + return;
  398. + }
  399. + if (m_dev->Ioctl(HDMICEC_IOC_STOPDEVICE, NULL) != 0) {
  400. + LIB_CEC->AddLog(CEC_LOG_ERROR, "%s: Unable to stop device\n", __func__);
  401. + }
  402. + m_dev->Close();
  403. + m_bInitialised = false;
  404. +}
  405. +
  406. +
  407. +std::string CIMXCECAdapterCommunication::GetError(void) const
  408. +{
  409. + std::string strError(m_strError);
  410. + return strError;
  411. +}
  412. +
  413. +
  414. +cec_adapter_message_state CIMXCECAdapterCommunication::Write(
  415. + const cec_command &data, bool &UNUSED(bRetry), uint8_t UNUSED(iLineTimeout), bool UNUSED(bIsReply))
  416. +{
  417. + //cec_frame frame;
  418. + unsigned char message[MAX_CEC_MESSAGE_LEN];
  419. + int msg_len = 1;
  420. + cec_adapter_message_state rc = ADAPTER_MESSAGE_STATE_ERROR;
  421. +
  422. + if ((size_t)data.parameters.size + data.opcode_set + 1 > sizeof(message))
  423. + {
  424. + LIB_CEC->AddLog(CEC_LOG_ERROR, "%s: data size too large !", __func__);
  425. + return ADAPTER_MESSAGE_STATE_ERROR;
  426. + }
  427. +
  428. + message[0] = (data.initiator << 4) | (data.destination & 0x0f);
  429. + if (data.opcode_set)
  430. + {
  431. + message[1] = data.opcode;
  432. + msg_len++;
  433. + memcpy(&message[2], data.parameters.data, data.parameters.size);
  434. + msg_len+=data.parameters.size;
  435. + }
  436. +
  437. + if (m_dev->Write(message, msg_len) == msg_len)
  438. + {
  439. + rc = ADAPTER_MESSAGE_STATE_SENT_ACKED;
  440. + }
  441. + else
  442. + LIB_CEC->AddLog(CEC_LOG_ERROR, "%s: sent command error !", __func__);
  443. +
  444. + return rc;
  445. +}
  446. +
  447. +
  448. +uint16_t CIMXCECAdapterCommunication::GetFirmwareVersion(void)
  449. +{
  450. + /* FIXME add ioctl ? */
  451. + return 0;
  452. +}
  453. +
  454. +
  455. +cec_vendor_id CIMXCECAdapterCommunication::GetVendorId(void)
  456. +{
  457. + return CEC_VENDOR_UNKNOWN;
  458. +}
  459. +
  460. +
  461. +uint16_t CIMXCECAdapterCommunication::GetPhysicalAddress(void)
  462. +{
  463. + uint32_t info;
  464. + uint16_t phy_addr;
  465. +
  466. + if (m_dev->Ioctl(HDMICEC_IOC_GETPHYADDRESS, &info) != 0)
  467. + {
  468. + LIB_CEC->AddLog(CEC_LOG_ERROR, "%s: HDMICEC_IOC_GETPHYADDRESS failed !", __func__);
  469. + return CEC_INVALID_PHYSICAL_ADDRESS;
  470. + }
  471. + /* Rebuild 16 bit raw value from fsl 32 bits value */
  472. + phy_addr = ((info & 0x0f) << 12) | (info & 0x0f00) |
  473. + ((info & 0x0f0000) >> 12) | ((info & 0x0f000000) >> 24);
  474. +
  475. + return phy_addr;
  476. +}
  477. +
  478. +
  479. +cec_logical_addresses CIMXCECAdapterCommunication::GetLogicalAddresses(void)
  480. +{
  481. + cec_logical_addresses addresses;
  482. + addresses.Clear();
  483. +
  484. + CLockObject lock(m_mutex);
  485. + if ((m_logicalAddress & (CECDEVICE_UNKNOWN | CECDEVICE_UNREGISTERED)) == 0)
  486. + addresses.Set(m_logicalAddress);
  487. +
  488. + return addresses;
  489. +}
  490. +
  491. +void CIMXCECAdapterCommunication::HandleLogicalAddressLost(cec_logical_address UNUSED(oldAddress))
  492. +{
  493. + UnregisterLogicalAddress();
  494. +}
  495. +
  496. +bool CIMXCECAdapterCommunication::UnregisterLogicalAddress(void)
  497. +{
  498. + CLockObject lock(m_mutex);
  499. + if (!m_bLogicalAddressRegistered)
  500. + return true;
  501. +
  502. + if (m_dev->Ioctl(HDMICEC_IOC_SETLOGICALADDRESS, (void *)CECDEVICE_BROADCAST) != 0)
  503. + {
  504. + LIB_CEC->AddLog(CEC_LOG_ERROR, "%s: HDMICEC_IOC_SETLOGICALADDRESS failed !", __func__);
  505. + return false;
  506. + }
  507. +
  508. + m_logicalAddress = CECDEVICE_UNKNOWN;
  509. + m_bLogicalAddressRegistered = false;
  510. + return true;
  511. +}
  512. +
  513. +bool CIMXCECAdapterCommunication::RegisterLogicalAddress(const cec_logical_address address)
  514. +{
  515. + CLockObject lock(m_mutex);
  516. +
  517. + if (m_logicalAddress == address && m_bLogicalAddressRegistered)
  518. + {
  519. + return true;
  520. + }
  521. +
  522. + if (m_dev->Ioctl(HDMICEC_IOC_SETLOGICALADDRESS, (void *)address) != 0)
  523. + {
  524. + LIB_CEC->AddLog(CEC_LOG_ERROR, "%s: HDMICEC_IOC_SETLOGICALADDRESS failed !", __func__);
  525. + return false;
  526. + }
  527. +
  528. + m_logicalAddress = address;
  529. + m_bLogicalAddressRegistered = true;
  530. + return true;
  531. +}
  532. +
  533. +bool CIMXCECAdapterCommunication::SetLogicalAddresses(const cec_logical_addresses &addresses)
  534. +{
  535. + int log_addr = addresses.primary;
  536. +
  537. + return RegisterLogicalAddress((cec_logical_address)log_addr);
  538. +}
  539. +
  540. +void *CIMXCECAdapterCommunication::Process(void)
  541. +{
  542. + bool bHandled;
  543. + hdmi_cec_event event;
  544. + int ret;
  545. +
  546. + uint32_t opcode, status;
  547. + cec_logical_address initiator, destination;
  548. +
  549. + while (!IsStopped())
  550. + {
  551. + ret = m_dev->Read((char *)&event, sizeof(event), 5000);
  552. + if (ret > 0)
  553. + {
  554. +
  555. + initiator = cec_logical_address(event.msg[0] >> 4);
  556. + destination = cec_logical_address(event.msg[0] & 0x0f);
  557. +
  558. + //LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s: Read data : type : %d initiator %d dest %d", __func__, event.event_type, initiator, destination);
  559. + if (event.event_type == MESSAGE_TYPE_RECEIVE_SUCCESS)
  560. + /* Message received */
  561. + {
  562. + cec_command cmd;
  563. +
  564. + cec_command::Format(
  565. + cmd, initiator, destination,
  566. + ( event.msg_len > 1 ) ? cec_opcode(event.msg[1]) : CEC_OPCODE_NONE);
  567. +
  568. + for( uint8_t i = 2; i < event.msg_len; i++ )
  569. + cmd.parameters.PushBack(event.msg[i]);
  570. +
  571. + if (!IsStopped())
  572. + m_callback->OnCommandReceived(cmd);
  573. + }
  574. +
  575. + if (event.event_type == MESSAGE_TYPE_CONNECTED)
  576. + /* HDMI has just been reconnected - Notify phy address*/
  577. + {
  578. + uint16_t iNewAddress = GetPhysicalAddress();
  579. + m_callback->HandlePhysicalAddressChanged(iNewAddress);
  580. + }
  581. + /* We are not interested in other events */
  582. + } /*else {
  583. + LIB_CEC->AddLog(CEC_LOG_DEBUG, "%s: Read returned %d", __func__, ret);
  584. + }*/
  585. +
  586. + }
  587. +
  588. + return 0;
  589. +}
  590. +
  591. +#endif // HAVE_IMX_API
  592. diff --git a/src/libcec/adapter/IMX/IMXCECAdapterCommunication.h b/src/libcec/adapter/IMX/IMXCECAdapterCommunication.h
  593. new file mode 100644
  594. index 0000000..9e899a3
  595. --- /dev/null
  596. +++ b/src/libcec/adapter/IMX/IMXCECAdapterCommunication.h
  597. @@ -0,0 +1,119 @@
  598. +#pragma once
  599. +/*
  600. + * This file is part of the libCEC(R) library.
  601. + *
  602. + * libCEC(R) is Copyright (C) 2011-2013 Pulse-Eight Limited. All rights reserved.
  603. + * libCEC(R) is an original work, containing original code.
  604. + *
  605. + * libCEC(R) is a trademark of Pulse-Eight Limited.
  606. + *
  607. + * IMX adpater port is Copyright (C) 2013 by Stephan Rafin
  608. + *
  609. + * You can redistribute this file and/or modify
  610. + * it under the terms of the GNU General Public License as published by
  611. + * the Free Software Foundation; either version 2 of the License, or
  612. + * (at your option) any later version.
  613. + *
  614. + * This program is distributed in the hope that it will be useful,
  615. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  616. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  617. + * GNU General Public License for more details.
  618. + *
  619. + * You should have received a copy of the GNU General Public License
  620. + * along with this program; if not, write to the Free Software
  621. + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  622. + *
  623. + *
  624. + */
  625. +
  626. +#if defined(HAVE_IMX_API)
  627. +
  628. +#include "platform/threads/mutex.h"
  629. +#include "platform/threads/threads.h"
  630. +#include "platform/sockets/socket.h"
  631. +#include "adapter/AdapterCommunication.h"
  632. +#include <map>
  633. +
  634. +#define IMX_ADAPTER_VID 0x0471 /*FIXME TBD*/
  635. +#define IMX_ADAPTER_PID 0x1001
  636. +
  637. +
  638. +
  639. +namespace PLATFORM
  640. +{
  641. + class CCDevSocket;
  642. +};
  643. +
  644. +
  645. +namespace CEC
  646. +{
  647. + class CAdapterMessageQueueEntry;
  648. +
  649. + class CIMXCECAdapterCommunication : public IAdapterCommunication, public PLATFORM::CThread
  650. + {
  651. + public:
  652. + /*!
  653. + * @brief Create a new USB-CEC communication handler.
  654. + * @param callback The callback to use for incoming CEC commands.
  655. + */
  656. + CIMXCECAdapterCommunication(IAdapterCommunicationCallback *callback);
  657. + virtual ~CIMXCECAdapterCommunication(void);
  658. +
  659. + /** @name IAdapterCommunication implementation */
  660. + ///{
  661. + bool Open(uint32_t iTimeoutMs = CEC_DEFAULT_CONNECT_TIMEOUT, bool bSkipChecks = false, bool bStartListening = true);
  662. + void Close(void);
  663. + bool IsOpen(void);
  664. + std::string GetError(void) const;
  665. + cec_adapter_message_state Write(const cec_command &data, bool &bRetry, uint8_t iLineTimeout, bool bIsReply);
  666. +
  667. + bool SetLineTimeout(uint8_t UNUSED(iTimeout)) { return true; }
  668. + bool StartBootloader(void) { return false; }
  669. + bool SetLogicalAddresses(const cec_logical_addresses &addresses);
  670. + cec_logical_addresses GetLogicalAddresses(void);
  671. + bool PingAdapter(void) { return IsInitialised(); }
  672. + uint16_t GetFirmwareVersion(void);
  673. + uint32_t GetFirmwareBuildDate(void) { return 0; }
  674. + bool IsRunningLatestFirmware(void) { return true; }
  675. + bool PersistConfiguration(const libcec_configuration & UNUSED(configuration)) { return false; }
  676. + bool GetConfiguration(libcec_configuration & UNUSED(configuration)) { return false; }
  677. + std::string GetPortName(void) { return std::string("IMX"); }
  678. + uint16_t GetPhysicalAddress(void);
  679. + bool SetControlledMode(bool UNUSED(controlled)) { return true; }
  680. + cec_vendor_id GetVendorId(void);
  681. + bool SupportsSourceLogicalAddress(const cec_logical_address address) { return address > CECDEVICE_TV && address <= CECDEVICE_BROADCAST; }
  682. + cec_adapter_type GetAdapterType(void) { return ADAPTERTYPE_IMX; }
  683. + uint16_t GetAdapterVendorId(void) const { return IMX_ADAPTER_VID; }
  684. + uint16_t GetAdapterProductId(void) const { return IMX_ADAPTER_PID; }
  685. + void HandleLogicalAddressLost(cec_logical_address UNUSED(oldAddress));
  686. + void SetActiveSource(bool UNUSED(bSetTo), bool UNUSED(bClientUnregistered)) {}
  687. + bool RegisterLogicalAddress(const cec_logical_address address);
  688. + ///}
  689. +
  690. + /** @name PLATFORM::CThread implementation */
  691. + ///{
  692. + void *Process(void);
  693. + ///}
  694. +
  695. + private:
  696. + bool IsInitialised(void) const { return m_bInitialised; };
  697. + bool UnregisterLogicalAddress(void);
  698. +
  699. + std::string m_strError; /**< current error message */
  700. +
  701. + //cec_logical_addresses m_logicalAddresses;
  702. + cec_logical_address m_logicalAddress;
  703. +
  704. + PLATFORM::CMutex m_mutex;
  705. + PLATFORM::CCDevSocket *m_dev; /**< the device connection */
  706. + bool m_bLogicalAddressRegistered;
  707. + bool m_bInitialised;
  708. +
  709. + PLATFORM::CMutex m_messageMutex;
  710. + uint32_t m_iNextMessage;
  711. + std::map<uint32_t, CAdapterMessageQueueEntry *> m_messages;
  712. + };
  713. +
  714. +};
  715. +
  716. +#endif
  717. diff --git a/src/libcec/adapter/IMX/IMXCECAdapterDetection.cpp b/src/libcec/adapter/IMX/IMXCECAdapterDetection.cpp
  718. new file mode 100644
  719. index 0000000..6c93c45
  720. --- /dev/null
  721. +++ b/src/libcec/adapter/IMX/IMXCECAdapterDetection.cpp
  722. @@ -0,0 +1,42 @@
  723. +/*
  724. + * This file is part of the libCEC(R) library.
  725. + *
  726. + * libCEC(R) is Copyright (C) 2011-2013 Pulse-Eight Limited. All rights reserved.
  727. + * libCEC(R) is an original work, containing original code.
  728. + *
  729. + * libCEC(R) is a trademark of Pulse-Eight Limited.
  730. + *
  731. + * IMX adpater port is Copyright (C) 2013 by Stephan Rafin
  732. + *
  733. + * You can redistribute this file and/or modify
  734. + * it under the terms of the GNU General Public License as published by
  735. + * the Free Software Foundation; either version 2 of the License, or
  736. + * (at your option) any later version.
  737. + *
  738. + * This program is distributed in the hope that it will be useful,
  739. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  740. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  741. + * GNU General Public License for more details.
  742. + *
  743. + * You should have received a copy of the GNU General Public License
  744. + * along with this program; if not, write to the Free Software
  745. + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  746. + *
  747. + *
  748. + */
  749. +
  750. +#include "env.h"
  751. +#include <stdio.h>
  752. +
  753. +#if defined(HAVE_IMX_API)
  754. +#include "IMXCECAdapterDetection.h"
  755. +
  756. +
  757. +using namespace CEC;
  758. +
  759. +bool CIMXCECAdapterDetection::FindAdapter(void)
  760. +{
  761. + return access(CEC_IMX_PATH, 0) == 0;
  762. +}
  763. +
  764. +#endif
  765. diff --git a/src/libcec/adapter/IMX/IMXCECAdapterDetection.h b/src/libcec/adapter/IMX/IMXCECAdapterDetection.h
  766. new file mode 100644
  767. index 0000000..d54891d
  768. --- /dev/null
  769. +++ b/src/libcec/adapter/IMX/IMXCECAdapterDetection.h
  770. @@ -0,0 +1,36 @@
  771. +#pragma once
  772. +/*
  773. + * This file is part of the libCEC(R) library.
  774. + *
  775. + * libCEC(R) is Copyright (C) 2011-2013 Pulse-Eight Limited. All rights reserved.
  776. + * libCEC(R) is an original work, containing original code.
  777. + *
  778. + * libCEC(R) is a trademark of Pulse-Eight Limited.
  779. + *
  780. + * IMX adpater port is Copyright (C) 2013 by Stephan Rafin
  781. + *
  782. + * You can redistribute this file and/or modify
  783. + * it under the terms of the GNU General Public License as published by
  784. + * the Free Software Foundation; either version 2 of the License, or
  785. + * (at your option) any later version.
  786. + *
  787. + * This program is distributed in the hope that it will be useful,
  788. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  789. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  790. + * GNU General Public License for more details.
  791. + *
  792. + * You should have received a copy of the GNU General Public License
  793. + * along with this program; if not, write to the Free Software
  794. + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  795. + *
  796. + *
  797. + */
  798. +
  799. +namespace CEC
  800. +{
  801. + class CIMXCECAdapterDetection
  802. + {
  803. + public:
  804. + static bool FindAdapter(void);
  805. + };
  806. +}
  807. diff --git a/src/libcec/cmake/CheckPlatformSupport.cmake b/src/libcec/cmake/CheckPlatformSupport.cmake
  808. index 828cdb2..8b47795 100644
  809. --- a/src/libcec/cmake/CheckPlatformSupport.cmake
  810. +++ b/src/libcec/cmake/CheckPlatformSupport.cmake
  811. @@ -8,6 +8,7 @@
  812. # HAVE_RANDR 1 if xrandr is supported
  813. # HAVE_LIBUDEV 1 if udev is supported
  814. # HAVE_RPI_API 1 if Raspberry Pi is supported
  815. +# HAVE_IMX_API 1 if i.MX is supported
  816. # HAVE_TDA995X_API 1 if TDA995X is supported
  817. # HAVE_EXYNOS_API 1 if Exynos is supported
  818. # HAVE_P8_USB_DETECT 1 if Pulse-Eight devices can be auto-detected
  819. @@ -87,6 +88,15 @@ else()
  820. list(APPEND CEC_SOURCES ${CEC_SOURCES_ADAPTER_RPI})
  821. endif()
  822. + # i.MX
  823. + if (HAVE_IMX_API)
  824. + set(LIB_INFO "${LIB_INFO}, 'IMX'")
  825. + set(CEC_SOURCES_ADAPTER_IMX adapter/IMX/IMXCECAdapterDetection.cpp
  826. + adapter/IMX/IMXCECAdapterCommunication.cpp)
  827. + source_group("Source Files\\adapter\\IMX" FILES ${CEC_SOURCES_ADAPTER_IMX})
  828. + list(APPEND CEC_SOURCES ${CEC_SOURCES_ADAPTER_IMX})
  829. + endif()
  830. +
  831. # TDA995x
  832. check_include_files("tda998x_ioctl.h;comps/tmdlHdmiCEC/inc/tmdlHdmiCEC_Types.h" HAVE_TDA995X_API)
  833. if (HAVE_TDA995X_API)
  834. diff --git a/src/libcec/cmake/DisplayPlatformSupport.cmake b/src/libcec/cmake/DisplayPlatformSupport.cmake
  835. index feee111..0309c56 100644
  836. --- a/src/libcec/cmake/DisplayPlatformSupport.cmake
  837. +++ b/src/libcec/cmake/DisplayPlatformSupport.cmake
  838. @@ -32,6 +32,12 @@ else()
  839. message(STATUS "Raspberry Pi support: no")
  840. endif()
  841. +if (HAVE_IMX_API)
  842. + message(STATUS "i.MX support: yes")
  843. +else()
  844. + message(STATUS "i.MX support: no")
  845. +endif()
  846. +
  847. if (HAVE_TDA995X_API)
  848. message(STATUS "TDA995x support: yes")
  849. else()
  850. diff --git a/src/libcec/env.h.in b/src/libcec/env.h.in
  851. index a1a1f26..f90e729 100644
  852. --- a/src/libcec/env.h.in
  853. +++ b/src/libcec/env.h.in
  854. @@ -60,6 +60,9 @@
  855. /* Define to 1 for Raspberry Pi support */
  856. #cmakedefine HAVE_RPI_API @HAVE_RPI_API@
  857. +/* Define to 1 for IMX support */
  858. +#cmakedefine HAVE_IMX_API @HAVE_IMX_API@
  859. +
  860. /* Define to 1 for TDA995x support */
  861. #cmakedefine HAVE_TDA995X_API @HAVE_TDA995X_API@
  862. --
  863. 2.1.4