cec-adap.c 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880
  1. /*
  2. * cec-adap.c - HDMI Consumer Electronics Control framework - CEC adapter
  3. *
  4. * Copyright 2016 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
  5. *
  6. * This program is free software; you may redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; version 2 of the License.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  11. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  12. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  13. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  14. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  15. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  16. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  17. * SOFTWARE.
  18. */
  19. #include <linux/errno.h>
  20. #include <linux/init.h>
  21. #include <linux/module.h>
  22. #include <linux/kernel.h>
  23. #include <linux/kmod.h>
  24. #include <linux/ktime.h>
  25. #include <linux/slab.h>
  26. #include <linux/mm.h>
  27. #include <linux/string.h>
  28. #include <linux/types.h>
  29. #include "cec-priv.h"
  30. static int cec_report_features(struct cec_adapter *adap, unsigned int la_idx);
  31. static int cec_report_phys_addr(struct cec_adapter *adap, unsigned int la_idx);
  32. /*
  33. * 400 ms is the time it takes for one 16 byte message to be
  34. * transferred and 5 is the maximum number of retries. Add
  35. * another 100 ms as a margin. So if the transmit doesn't
  36. * finish before that time something is really wrong and we
  37. * have to time out.
  38. *
  39. * This is a sign that something it really wrong and a warning
  40. * will be issued.
  41. */
  42. #define CEC_XFER_TIMEOUT_MS (5 * 400 + 100)
  43. #define call_op(adap, op, arg...) \
  44. (adap->ops->op ? adap->ops->op(adap, ## arg) : 0)
  45. #define call_void_op(adap, op, arg...) \
  46. do { \
  47. if (adap->ops->op) \
  48. adap->ops->op(adap, ## arg); \
  49. } while (0)
  50. static int cec_log_addr2idx(const struct cec_adapter *adap, u8 log_addr)
  51. {
  52. int i;
  53. for (i = 0; i < adap->log_addrs.num_log_addrs; i++)
  54. if (adap->log_addrs.log_addr[i] == log_addr)
  55. return i;
  56. return -1;
  57. }
  58. static unsigned int cec_log_addr2dev(const struct cec_adapter *adap, u8 log_addr)
  59. {
  60. int i = cec_log_addr2idx(adap, log_addr);
  61. return adap->log_addrs.primary_device_type[i < 0 ? 0 : i];
  62. }
  63. /*
  64. * Queue a new event for this filehandle. If ts == 0, then set it
  65. * to the current time.
  66. *
  67. * The two events that are currently defined do not need to keep track
  68. * of intermediate events, so no actual queue of events is needed,
  69. * instead just store the latest state and the total number of lost
  70. * messages.
  71. *
  72. * Should new events be added in the future that require intermediate
  73. * results to be queued as well, then a proper queue data structure is
  74. * required. But until then, just keep it simple.
  75. */
  76. void cec_queue_event_fh(struct cec_fh *fh,
  77. const struct cec_event *new_ev, u64 ts)
  78. {
  79. struct cec_event *ev = &fh->events[new_ev->event - 1];
  80. if (ts == 0)
  81. ts = ktime_get_ns();
  82. mutex_lock(&fh->lock);
  83. if (new_ev->event == CEC_EVENT_LOST_MSGS &&
  84. fh->pending_events & (1 << new_ev->event)) {
  85. /*
  86. * If there is already a lost_msgs event, then just
  87. * update the lost_msgs count. This effectively
  88. * merges the old and new events into one.
  89. */
  90. ev->lost_msgs.lost_msgs += new_ev->lost_msgs.lost_msgs;
  91. goto unlock;
  92. }
  93. /*
  94. * Intermediate states are not interesting, so just
  95. * overwrite any older event.
  96. */
  97. *ev = *new_ev;
  98. ev->ts = ts;
  99. fh->pending_events |= 1 << new_ev->event;
  100. unlock:
  101. mutex_unlock(&fh->lock);
  102. wake_up_interruptible(&fh->wait);
  103. }
  104. /* Queue a new event for all open filehandles. */
  105. static void cec_queue_event(struct cec_adapter *adap,
  106. const struct cec_event *ev)
  107. {
  108. u64 ts = ktime_get_ns();
  109. struct cec_fh *fh;
  110. mutex_lock(&adap->devnode.lock);
  111. list_for_each_entry(fh, &adap->devnode.fhs, list)
  112. cec_queue_event_fh(fh, ev, ts);
  113. mutex_unlock(&adap->devnode.lock);
  114. }
  115. /*
  116. * Queue a new message for this filehandle. If there is no more room
  117. * in the queue, then send the LOST_MSGS event instead.
  118. */
  119. static void cec_queue_msg_fh(struct cec_fh *fh, const struct cec_msg *msg)
  120. {
  121. static const struct cec_event ev_lost_msg = {
  122. .ts = 0,
  123. .event = CEC_EVENT_LOST_MSGS,
  124. .flags = 0,
  125. {
  126. .lost_msgs.lost_msgs = 1,
  127. },
  128. };
  129. struct cec_msg_entry *entry;
  130. mutex_lock(&fh->lock);
  131. entry = kmalloc(sizeof(*entry), GFP_KERNEL);
  132. if (!entry)
  133. goto lost_msgs;
  134. entry->msg = *msg;
  135. /* Add new msg at the end of the queue */
  136. list_add_tail(&entry->list, &fh->msgs);
  137. /*
  138. * if the queue now has more than CEC_MAX_MSG_RX_QUEUE_SZ
  139. * messages, drop the oldest one and send a lost message event.
  140. */
  141. if (fh->queued_msgs == CEC_MAX_MSG_RX_QUEUE_SZ) {
  142. list_del(&entry->list);
  143. goto lost_msgs;
  144. }
  145. fh->queued_msgs++;
  146. mutex_unlock(&fh->lock);
  147. wake_up_interruptible(&fh->wait);
  148. return;
  149. lost_msgs:
  150. mutex_unlock(&fh->lock);
  151. cec_queue_event_fh(fh, &ev_lost_msg, 0);
  152. }
  153. /*
  154. * Queue the message for those filehandles that are in monitor mode.
  155. * If valid_la is true (this message is for us or was sent by us),
  156. * then pass it on to any monitoring filehandle. If this message
  157. * isn't for us or from us, then only give it to filehandles that
  158. * are in MONITOR_ALL mode.
  159. *
  160. * This can only happen if the CEC_CAP_MONITOR_ALL capability is
  161. * set and the CEC adapter was placed in 'monitor all' mode.
  162. */
  163. static void cec_queue_msg_monitor(struct cec_adapter *adap,
  164. const struct cec_msg *msg,
  165. bool valid_la)
  166. {
  167. struct cec_fh *fh;
  168. u32 monitor_mode = valid_la ? CEC_MODE_MONITOR :
  169. CEC_MODE_MONITOR_ALL;
  170. mutex_lock(&adap->devnode.lock);
  171. list_for_each_entry(fh, &adap->devnode.fhs, list) {
  172. if (fh->mode_follower >= monitor_mode)
  173. cec_queue_msg_fh(fh, msg);
  174. }
  175. mutex_unlock(&adap->devnode.lock);
  176. }
  177. /*
  178. * Queue the message for follower filehandles.
  179. */
  180. static void cec_queue_msg_followers(struct cec_adapter *adap,
  181. const struct cec_msg *msg)
  182. {
  183. struct cec_fh *fh;
  184. mutex_lock(&adap->devnode.lock);
  185. list_for_each_entry(fh, &adap->devnode.fhs, list) {
  186. if (fh->mode_follower == CEC_MODE_FOLLOWER)
  187. cec_queue_msg_fh(fh, msg);
  188. }
  189. mutex_unlock(&adap->devnode.lock);
  190. }
  191. /* Notify userspace of an adapter state change. */
  192. static void cec_post_state_event(struct cec_adapter *adap)
  193. {
  194. struct cec_event ev = {
  195. .event = CEC_EVENT_STATE_CHANGE,
  196. };
  197. ev.state_change.phys_addr = adap->phys_addr;
  198. ev.state_change.log_addr_mask = adap->log_addrs.log_addr_mask;
  199. cec_queue_event(adap, &ev);
  200. }
  201. /*
  202. * A CEC transmit (and a possible wait for reply) completed.
  203. * If this was in blocking mode, then complete it, otherwise
  204. * queue the message for userspace to dequeue later.
  205. *
  206. * This function is called with adap->lock held.
  207. */
  208. static void cec_data_completed(struct cec_data *data)
  209. {
  210. /*
  211. * Delete this transmit from the filehandle's xfer_list since
  212. * we're done with it.
  213. *
  214. * Note that if the filehandle is closed before this transmit
  215. * finished, then the release() function will set data->fh to NULL.
  216. * Without that we would be referring to a closed filehandle.
  217. */
  218. if (data->fh)
  219. list_del(&data->xfer_list);
  220. if (data->blocking) {
  221. /*
  222. * Someone is blocking so mark the message as completed
  223. * and call complete.
  224. */
  225. data->completed = true;
  226. complete(&data->c);
  227. } else {
  228. /*
  229. * No blocking, so just queue the message if needed and
  230. * free the memory.
  231. */
  232. if (data->fh)
  233. cec_queue_msg_fh(data->fh, &data->msg);
  234. kfree(data);
  235. }
  236. }
  237. /*
  238. * A pending CEC transmit needs to be cancelled, either because the CEC
  239. * adapter is disabled or the transmit takes an impossibly long time to
  240. * finish.
  241. *
  242. * This function is called with adap->lock held.
  243. */
  244. static void cec_data_cancel(struct cec_data *data)
  245. {
  246. /*
  247. * It's either the current transmit, or it is a pending
  248. * transmit. Take the appropriate action to clear it.
  249. */
  250. if (data->adap->transmitting == data) {
  251. data->adap->transmitting = NULL;
  252. } else {
  253. list_del_init(&data->list);
  254. if (!(data->msg.tx_status & CEC_TX_STATUS_OK))
  255. data->adap->transmit_queue_sz--;
  256. }
  257. /* Mark it as an error */
  258. data->msg.tx_ts = ktime_get_ns();
  259. data->msg.tx_status = CEC_TX_STATUS_ERROR |
  260. CEC_TX_STATUS_MAX_RETRIES;
  261. data->attempts = 0;
  262. data->msg.tx_error_cnt = 1;
  263. /* Queue transmitted message for monitoring purposes */
  264. cec_queue_msg_monitor(data->adap, &data->msg, 1);
  265. cec_data_completed(data);
  266. }
  267. /*
  268. * Main CEC state machine
  269. *
  270. * Wait until the thread should be stopped, or we are not transmitting and
  271. * a new transmit message is queued up, in which case we start transmitting
  272. * that message. When the adapter finished transmitting the message it will
  273. * call cec_transmit_done().
  274. *
  275. * If the adapter is disabled, then remove all queued messages instead.
  276. *
  277. * If the current transmit times out, then cancel that transmit.
  278. */
  279. int cec_thread_func(void *_adap)
  280. {
  281. struct cec_adapter *adap = _adap;
  282. for (;;) {
  283. unsigned int signal_free_time;
  284. struct cec_data *data;
  285. bool timeout = false;
  286. u8 attempts;
  287. if (adap->transmitting) {
  288. int err;
  289. /*
  290. * We are transmitting a message, so add a timeout
  291. * to prevent the state machine to get stuck waiting
  292. * for this message to finalize and add a check to
  293. * see if the adapter is disabled in which case the
  294. * transmit should be canceled.
  295. */
  296. err = wait_event_interruptible_timeout(adap->kthread_waitq,
  297. kthread_should_stop() ||
  298. (!adap->is_configured && !adap->is_configuring) ||
  299. (!adap->transmitting &&
  300. !list_empty(&adap->transmit_queue)),
  301. msecs_to_jiffies(CEC_XFER_TIMEOUT_MS));
  302. timeout = err == 0;
  303. } else {
  304. /* Otherwise we just wait for something to happen. */
  305. wait_event_interruptible(adap->kthread_waitq,
  306. kthread_should_stop() ||
  307. (!adap->transmitting &&
  308. !list_empty(&adap->transmit_queue)));
  309. }
  310. mutex_lock(&adap->lock);
  311. if ((!adap->is_configured && !adap->is_configuring) ||
  312. kthread_should_stop()) {
  313. /*
  314. * If the adapter is disabled, or we're asked to stop,
  315. * then cancel any pending transmits.
  316. */
  317. while (!list_empty(&adap->transmit_queue)) {
  318. data = list_first_entry(&adap->transmit_queue,
  319. struct cec_data, list);
  320. cec_data_cancel(data);
  321. }
  322. if (adap->transmitting)
  323. cec_data_cancel(adap->transmitting);
  324. /*
  325. * Cancel the pending timeout work. We have to unlock
  326. * the mutex when flushing the work since
  327. * cec_wait_timeout() will take it. This is OK since
  328. * no new entries can be added to wait_queue as long
  329. * as adap->transmitting is NULL, which it is due to
  330. * the cec_data_cancel() above.
  331. */
  332. while (!list_empty(&adap->wait_queue)) {
  333. data = list_first_entry(&adap->wait_queue,
  334. struct cec_data, list);
  335. if (!cancel_delayed_work(&data->work)) {
  336. mutex_unlock(&adap->lock);
  337. flush_scheduled_work();
  338. mutex_lock(&adap->lock);
  339. }
  340. cec_data_cancel(data);
  341. }
  342. goto unlock;
  343. }
  344. if (adap->transmitting && timeout) {
  345. /*
  346. * If we timeout, then log that. This really shouldn't
  347. * happen and is an indication of a faulty CEC adapter
  348. * driver, or the CEC bus is in some weird state.
  349. */
  350. dprintk(0, "message %*ph timed out!\n",
  351. adap->transmitting->msg.len,
  352. adap->transmitting->msg.msg);
  353. /* Just give up on this. */
  354. cec_data_cancel(adap->transmitting);
  355. goto unlock;
  356. }
  357. /*
  358. * If we are still transmitting, or there is nothing new to
  359. * transmit, then just continue waiting.
  360. */
  361. if (adap->transmitting || list_empty(&adap->transmit_queue))
  362. goto unlock;
  363. /* Get a new message to transmit */
  364. data = list_first_entry(&adap->transmit_queue,
  365. struct cec_data, list);
  366. list_del_init(&data->list);
  367. adap->transmit_queue_sz--;
  368. /* Make this the current transmitting message */
  369. adap->transmitting = data;
  370. /*
  371. * Suggested number of attempts as per the CEC 2.0 spec:
  372. * 4 attempts is the default, except for 'secondary poll
  373. * messages', i.e. poll messages not sent during the adapter
  374. * configuration phase when it allocates logical addresses.
  375. */
  376. if (data->msg.len == 1 && adap->is_configured)
  377. attempts = 2;
  378. else
  379. attempts = 4;
  380. /* Set the suggested signal free time */
  381. if (data->attempts) {
  382. /* should be >= 3 data bit periods for a retry */
  383. signal_free_time = CEC_SIGNAL_FREE_TIME_RETRY;
  384. } else if (data->new_initiator) {
  385. /* should be >= 5 data bit periods for new initiator */
  386. signal_free_time = CEC_SIGNAL_FREE_TIME_NEW_INITIATOR;
  387. } else {
  388. /*
  389. * should be >= 7 data bit periods for sending another
  390. * frame immediately after another.
  391. */
  392. signal_free_time = CEC_SIGNAL_FREE_TIME_NEXT_XFER;
  393. }
  394. if (data->attempts == 0)
  395. data->attempts = attempts;
  396. /* Tell the adapter to transmit, cancel on error */
  397. if (adap->ops->adap_transmit(adap, data->attempts,
  398. signal_free_time, &data->msg))
  399. cec_data_cancel(data);
  400. unlock:
  401. mutex_unlock(&adap->lock);
  402. if (kthread_should_stop())
  403. break;
  404. }
  405. return 0;
  406. }
  407. /*
  408. * Called by the CEC adapter if a transmit finished.
  409. */
  410. void cec_transmit_done(struct cec_adapter *adap, u8 status, u8 arb_lost_cnt,
  411. u8 nack_cnt, u8 low_drive_cnt, u8 error_cnt)
  412. {
  413. struct cec_data *data;
  414. struct cec_msg *msg;
  415. u64 ts = ktime_get_ns();
  416. dprintk(2, "cec_transmit_done %02x\n", status);
  417. mutex_lock(&adap->lock);
  418. data = adap->transmitting;
  419. if (!data) {
  420. /*
  421. * This can happen if a transmit was issued and the cable is
  422. * unplugged while the transmit is ongoing. Ignore this
  423. * transmit in that case.
  424. */
  425. dprintk(1, "cec_transmit_done without an ongoing transmit!\n");
  426. goto unlock;
  427. }
  428. msg = &data->msg;
  429. /* Drivers must fill in the status! */
  430. WARN_ON(status == 0);
  431. msg->tx_ts = ts;
  432. msg->tx_status |= status;
  433. msg->tx_arb_lost_cnt += arb_lost_cnt;
  434. msg->tx_nack_cnt += nack_cnt;
  435. msg->tx_low_drive_cnt += low_drive_cnt;
  436. msg->tx_error_cnt += error_cnt;
  437. /* Mark that we're done with this transmit */
  438. adap->transmitting = NULL;
  439. /*
  440. * If there are still retry attempts left and there was an error and
  441. * the hardware didn't signal that it retried itself (by setting
  442. * CEC_TX_STATUS_MAX_RETRIES), then we will retry ourselves.
  443. */
  444. if (data->attempts > 1 &&
  445. !(status & (CEC_TX_STATUS_MAX_RETRIES | CEC_TX_STATUS_OK))) {
  446. /* Retry this message */
  447. data->attempts--;
  448. /* Add the message in front of the transmit queue */
  449. list_add(&data->list, &adap->transmit_queue);
  450. adap->transmit_queue_sz++;
  451. goto wake_thread;
  452. }
  453. data->attempts = 0;
  454. /* Always set CEC_TX_STATUS_MAX_RETRIES on error */
  455. if (!(status & CEC_TX_STATUS_OK))
  456. msg->tx_status |= CEC_TX_STATUS_MAX_RETRIES;
  457. /* Queue transmitted message for monitoring purposes */
  458. cec_queue_msg_monitor(adap, msg, 1);
  459. if ((status & CEC_TX_STATUS_OK) && adap->is_configured &&
  460. msg->timeout) {
  461. /*
  462. * Queue the message into the wait queue if we want to wait
  463. * for a reply.
  464. */
  465. list_add_tail(&data->list, &adap->wait_queue);
  466. schedule_delayed_work(&data->work,
  467. msecs_to_jiffies(msg->timeout));
  468. } else {
  469. /* Otherwise we're done */
  470. cec_data_completed(data);
  471. }
  472. wake_thread:
  473. /*
  474. * Wake up the main thread to see if another message is ready
  475. * for transmitting or to retry the current message.
  476. */
  477. wake_up_interruptible(&adap->kthread_waitq);
  478. unlock:
  479. mutex_unlock(&adap->lock);
  480. }
  481. EXPORT_SYMBOL_GPL(cec_transmit_done);
  482. /*
  483. * Called when waiting for a reply times out.
  484. */
  485. static void cec_wait_timeout(struct work_struct *work)
  486. {
  487. struct cec_data *data = container_of(work, struct cec_data, work.work);
  488. struct cec_adapter *adap = data->adap;
  489. mutex_lock(&adap->lock);
  490. /*
  491. * Sanity check in case the timeout and the arrival of the message
  492. * happened at the same time.
  493. */
  494. if (list_empty(&data->list))
  495. goto unlock;
  496. /* Mark the message as timed out */
  497. list_del_init(&data->list);
  498. data->msg.rx_ts = ktime_get_ns();
  499. data->msg.rx_status = CEC_RX_STATUS_TIMEOUT;
  500. cec_data_completed(data);
  501. unlock:
  502. mutex_unlock(&adap->lock);
  503. }
  504. /*
  505. * Transmit a message. The fh argument may be NULL if the transmit is not
  506. * associated with a specific filehandle.
  507. *
  508. * This function is called with adap->lock held.
  509. */
  510. int cec_transmit_msg_fh(struct cec_adapter *adap, struct cec_msg *msg,
  511. struct cec_fh *fh, bool block)
  512. {
  513. struct cec_data *data;
  514. u8 last_initiator = 0xff;
  515. unsigned int timeout;
  516. int res = 0;
  517. msg->rx_ts = 0;
  518. msg->tx_ts = 0;
  519. msg->rx_status = 0;
  520. msg->tx_status = 0;
  521. msg->tx_arb_lost_cnt = 0;
  522. msg->tx_nack_cnt = 0;
  523. msg->tx_low_drive_cnt = 0;
  524. msg->tx_error_cnt = 0;
  525. msg->sequence = ++adap->sequence;
  526. if (!msg->sequence)
  527. msg->sequence = ++adap->sequence;
  528. if (msg->reply && msg->timeout == 0) {
  529. /* Make sure the timeout isn't 0. */
  530. msg->timeout = 1000;
  531. }
  532. if (msg->timeout)
  533. msg->flags &= CEC_MSG_FL_REPLY_TO_FOLLOWERS;
  534. else
  535. msg->flags = 0;
  536. /* Sanity checks */
  537. if (msg->len == 0 || msg->len > CEC_MAX_MSG_SIZE) {
  538. dprintk(1, "cec_transmit_msg: invalid length %d\n", msg->len);
  539. return -EINVAL;
  540. }
  541. if (msg->timeout && msg->len == 1) {
  542. dprintk(1, "cec_transmit_msg: can't reply for poll msg\n");
  543. return -EINVAL;
  544. }
  545. memset(msg->msg + msg->len, 0, sizeof(msg->msg) - msg->len);
  546. if (msg->len == 1) {
  547. if (cec_msg_initiator(msg) != 0xf ||
  548. cec_msg_destination(msg) == 0xf) {
  549. dprintk(1, "cec_transmit_msg: invalid poll message\n");
  550. return -EINVAL;
  551. }
  552. if (cec_has_log_addr(adap, cec_msg_destination(msg))) {
  553. /*
  554. * If the destination is a logical address our adapter
  555. * has already claimed, then just NACK this.
  556. * It depends on the hardware what it will do with a
  557. * POLL to itself (some OK this), so it is just as
  558. * easy to handle it here so the behavior will be
  559. * consistent.
  560. */
  561. msg->tx_ts = ktime_get_ns();
  562. msg->tx_status = CEC_TX_STATUS_NACK |
  563. CEC_TX_STATUS_MAX_RETRIES;
  564. msg->tx_nack_cnt = 1;
  565. return 0;
  566. }
  567. }
  568. if (msg->len > 1 && !cec_msg_is_broadcast(msg) &&
  569. cec_has_log_addr(adap, cec_msg_destination(msg))) {
  570. dprintk(1, "cec_transmit_msg: destination is the adapter itself\n");
  571. return -EINVAL;
  572. }
  573. if (cec_msg_initiator(msg) != 0xf &&
  574. !cec_has_log_addr(adap, cec_msg_initiator(msg))) {
  575. dprintk(1, "cec_transmit_msg: initiator has unknown logical address %d\n",
  576. cec_msg_initiator(msg));
  577. return -EINVAL;
  578. }
  579. if (!adap->is_configured && !adap->is_configuring)
  580. return -ENONET;
  581. if (adap->transmit_queue_sz >= CEC_MAX_MSG_TX_QUEUE_SZ)
  582. return -EBUSY;
  583. data = kzalloc(sizeof(*data), GFP_KERNEL);
  584. if (!data)
  585. return -ENOMEM;
  586. if (msg->len > 1 && msg->msg[1] == CEC_MSG_CDC_MESSAGE) {
  587. msg->msg[2] = adap->phys_addr >> 8;
  588. msg->msg[3] = adap->phys_addr & 0xff;
  589. }
  590. if (msg->timeout)
  591. dprintk(2, "cec_transmit_msg: %*ph (wait for 0x%02x%s)\n",
  592. msg->len, msg->msg, msg->reply, !block ? ", nb" : "");
  593. else
  594. dprintk(2, "cec_transmit_msg: %*ph%s\n",
  595. msg->len, msg->msg, !block ? " (nb)" : "");
  596. data->msg = *msg;
  597. data->fh = fh;
  598. data->adap = adap;
  599. data->blocking = block;
  600. /*
  601. * Determine if this message follows a message from the same
  602. * initiator. Needed to determine the free signal time later on.
  603. */
  604. if (msg->len > 1) {
  605. if (!(list_empty(&adap->transmit_queue))) {
  606. const struct cec_data *last;
  607. last = list_last_entry(&adap->transmit_queue,
  608. const struct cec_data, list);
  609. last_initiator = cec_msg_initiator(&last->msg);
  610. } else if (adap->transmitting) {
  611. last_initiator =
  612. cec_msg_initiator(&adap->transmitting->msg);
  613. }
  614. }
  615. data->new_initiator = last_initiator != cec_msg_initiator(msg);
  616. init_completion(&data->c);
  617. INIT_DELAYED_WORK(&data->work, cec_wait_timeout);
  618. if (fh)
  619. list_add_tail(&data->xfer_list, &fh->xfer_list);
  620. list_add_tail(&data->list, &adap->transmit_queue);
  621. adap->transmit_queue_sz++;
  622. if (!adap->transmitting)
  623. wake_up_interruptible(&adap->kthread_waitq);
  624. /* All done if we don't need to block waiting for completion */
  625. if (!block)
  626. return 0;
  627. /*
  628. * If we don't get a completion before this time something is really
  629. * wrong and we time out.
  630. */
  631. timeout = CEC_XFER_TIMEOUT_MS;
  632. /* Add the requested timeout if we have to wait for a reply as well */
  633. if (msg->timeout)
  634. timeout += msg->timeout;
  635. /*
  636. * Release the lock and wait, retake the lock afterwards.
  637. */
  638. mutex_unlock(&adap->lock);
  639. res = wait_for_completion_killable_timeout(&data->c,
  640. msecs_to_jiffies(timeout));
  641. mutex_lock(&adap->lock);
  642. if (data->completed) {
  643. /* The transmit completed (possibly with an error) */
  644. *msg = data->msg;
  645. kfree(data);
  646. return 0;
  647. }
  648. /*
  649. * The wait for completion timed out or was interrupted, so mark this
  650. * as non-blocking and disconnect from the filehandle since it is
  651. * still 'in flight'. When it finally completes it will just drop the
  652. * result silently.
  653. */
  654. data->blocking = false;
  655. if (data->fh)
  656. list_del(&data->xfer_list);
  657. data->fh = NULL;
  658. if (res == 0) { /* timed out */
  659. /* Check if the reply or the transmit failed */
  660. if (msg->timeout && (msg->tx_status & CEC_TX_STATUS_OK))
  661. msg->rx_status = CEC_RX_STATUS_TIMEOUT;
  662. else
  663. msg->tx_status = CEC_TX_STATUS_MAX_RETRIES;
  664. }
  665. return res > 0 ? 0 : res;
  666. }
  667. /* Helper function to be used by drivers and this framework. */
  668. int cec_transmit_msg(struct cec_adapter *adap, struct cec_msg *msg,
  669. bool block)
  670. {
  671. int ret;
  672. mutex_lock(&adap->lock);
  673. ret = cec_transmit_msg_fh(adap, msg, NULL, block);
  674. mutex_unlock(&adap->lock);
  675. return ret;
  676. }
  677. EXPORT_SYMBOL_GPL(cec_transmit_msg);
  678. /*
  679. * I don't like forward references but without this the low-level
  680. * cec_received_msg() function would come after a bunch of high-level
  681. * CEC protocol handling functions. That was very confusing.
  682. */
  683. static int cec_receive_notify(struct cec_adapter *adap, struct cec_msg *msg,
  684. bool is_reply);
  685. #define DIRECTED 0x80
  686. #define BCAST1_4 0x40
  687. #define BCAST2_0 0x20 /* broadcast only allowed for >= 2.0 */
  688. #define BCAST (BCAST1_4 | BCAST2_0)
  689. #define BOTH (BCAST | DIRECTED)
  690. /*
  691. * Specify minimum length and whether the message is directed, broadcast
  692. * or both. Messages that do not match the criteria are ignored as per
  693. * the CEC specification.
  694. */
  695. static const u8 cec_msg_size[256] = {
  696. [CEC_MSG_ACTIVE_SOURCE] = 4 | BCAST,
  697. [CEC_MSG_IMAGE_VIEW_ON] = 2 | DIRECTED,
  698. [CEC_MSG_TEXT_VIEW_ON] = 2 | DIRECTED,
  699. [CEC_MSG_INACTIVE_SOURCE] = 4 | DIRECTED,
  700. [CEC_MSG_REQUEST_ACTIVE_SOURCE] = 2 | BCAST,
  701. [CEC_MSG_ROUTING_CHANGE] = 6 | BCAST,
  702. [CEC_MSG_ROUTING_INFORMATION] = 4 | BCAST,
  703. [CEC_MSG_SET_STREAM_PATH] = 4 | BCAST,
  704. [CEC_MSG_STANDBY] = 2 | BOTH,
  705. [CEC_MSG_RECORD_OFF] = 2 | DIRECTED,
  706. [CEC_MSG_RECORD_ON] = 3 | DIRECTED,
  707. [CEC_MSG_RECORD_STATUS] = 3 | DIRECTED,
  708. [CEC_MSG_RECORD_TV_SCREEN] = 2 | DIRECTED,
  709. [CEC_MSG_CLEAR_ANALOGUE_TIMER] = 13 | DIRECTED,
  710. [CEC_MSG_CLEAR_DIGITAL_TIMER] = 16 | DIRECTED,
  711. [CEC_MSG_CLEAR_EXT_TIMER] = 13 | DIRECTED,
  712. [CEC_MSG_SET_ANALOGUE_TIMER] = 13 | DIRECTED,
  713. [CEC_MSG_SET_DIGITAL_TIMER] = 16 | DIRECTED,
  714. [CEC_MSG_SET_EXT_TIMER] = 13 | DIRECTED,
  715. [CEC_MSG_SET_TIMER_PROGRAM_TITLE] = 2 | DIRECTED,
  716. [CEC_MSG_TIMER_CLEARED_STATUS] = 3 | DIRECTED,
  717. [CEC_MSG_TIMER_STATUS] = 3 | DIRECTED,
  718. [CEC_MSG_CEC_VERSION] = 3 | DIRECTED,
  719. [CEC_MSG_GET_CEC_VERSION] = 2 | DIRECTED,
  720. [CEC_MSG_GIVE_PHYSICAL_ADDR] = 2 | DIRECTED,
  721. [CEC_MSG_GET_MENU_LANGUAGE] = 2 | DIRECTED,
  722. [CEC_MSG_REPORT_PHYSICAL_ADDR] = 5 | BCAST,
  723. [CEC_MSG_SET_MENU_LANGUAGE] = 5 | BCAST,
  724. [CEC_MSG_REPORT_FEATURES] = 6 | BCAST,
  725. [CEC_MSG_GIVE_FEATURES] = 2 | DIRECTED,
  726. [CEC_MSG_DECK_CONTROL] = 3 | DIRECTED,
  727. [CEC_MSG_DECK_STATUS] = 3 | DIRECTED,
  728. [CEC_MSG_GIVE_DECK_STATUS] = 3 | DIRECTED,
  729. [CEC_MSG_PLAY] = 3 | DIRECTED,
  730. [CEC_MSG_GIVE_TUNER_DEVICE_STATUS] = 3 | DIRECTED,
  731. [CEC_MSG_SELECT_ANALOGUE_SERVICE] = 6 | DIRECTED,
  732. [CEC_MSG_SELECT_DIGITAL_SERVICE] = 9 | DIRECTED,
  733. [CEC_MSG_TUNER_DEVICE_STATUS] = 7 | DIRECTED,
  734. [CEC_MSG_TUNER_STEP_DECREMENT] = 2 | DIRECTED,
  735. [CEC_MSG_TUNER_STEP_INCREMENT] = 2 | DIRECTED,
  736. [CEC_MSG_DEVICE_VENDOR_ID] = 5 | BCAST,
  737. [CEC_MSG_GIVE_DEVICE_VENDOR_ID] = 2 | DIRECTED,
  738. [CEC_MSG_VENDOR_COMMAND] = 2 | DIRECTED,
  739. [CEC_MSG_VENDOR_COMMAND_WITH_ID] = 5 | BOTH,
  740. [CEC_MSG_VENDOR_REMOTE_BUTTON_DOWN] = 2 | BOTH,
  741. [CEC_MSG_VENDOR_REMOTE_BUTTON_UP] = 2 | BOTH,
  742. [CEC_MSG_SET_OSD_STRING] = 3 | DIRECTED,
  743. [CEC_MSG_GIVE_OSD_NAME] = 2 | DIRECTED,
  744. [CEC_MSG_SET_OSD_NAME] = 2 | DIRECTED,
  745. [CEC_MSG_MENU_REQUEST] = 3 | DIRECTED,
  746. [CEC_MSG_MENU_STATUS] = 3 | DIRECTED,
  747. [CEC_MSG_USER_CONTROL_PRESSED] = 3 | DIRECTED,
  748. [CEC_MSG_USER_CONTROL_RELEASED] = 2 | DIRECTED,
  749. [CEC_MSG_GIVE_DEVICE_POWER_STATUS] = 2 | DIRECTED,
  750. [CEC_MSG_REPORT_POWER_STATUS] = 3 | DIRECTED | BCAST2_0,
  751. [CEC_MSG_FEATURE_ABORT] = 4 | DIRECTED,
  752. [CEC_MSG_ABORT] = 2 | DIRECTED,
  753. [CEC_MSG_GIVE_AUDIO_STATUS] = 2 | DIRECTED,
  754. [CEC_MSG_GIVE_SYSTEM_AUDIO_MODE_STATUS] = 2 | DIRECTED,
  755. [CEC_MSG_REPORT_AUDIO_STATUS] = 3 | DIRECTED,
  756. [CEC_MSG_REPORT_SHORT_AUDIO_DESCRIPTOR] = 2 | DIRECTED,
  757. [CEC_MSG_REQUEST_SHORT_AUDIO_DESCRIPTOR] = 2 | DIRECTED,
  758. [CEC_MSG_SET_SYSTEM_AUDIO_MODE] = 3 | BOTH,
  759. [CEC_MSG_SYSTEM_AUDIO_MODE_REQUEST] = 2 | DIRECTED,
  760. [CEC_MSG_SYSTEM_AUDIO_MODE_STATUS] = 3 | DIRECTED,
  761. [CEC_MSG_SET_AUDIO_RATE] = 3 | DIRECTED,
  762. [CEC_MSG_INITIATE_ARC] = 2 | DIRECTED,
  763. [CEC_MSG_REPORT_ARC_INITIATED] = 2 | DIRECTED,
  764. [CEC_MSG_REPORT_ARC_TERMINATED] = 2 | DIRECTED,
  765. [CEC_MSG_REQUEST_ARC_INITIATION] = 2 | DIRECTED,
  766. [CEC_MSG_REQUEST_ARC_TERMINATION] = 2 | DIRECTED,
  767. [CEC_MSG_TERMINATE_ARC] = 2 | DIRECTED,
  768. [CEC_MSG_REQUEST_CURRENT_LATENCY] = 4 | BCAST,
  769. [CEC_MSG_REPORT_CURRENT_LATENCY] = 7 | BCAST,
  770. [CEC_MSG_CDC_MESSAGE] = 2 | BCAST,
  771. };
  772. /* Called by the CEC adapter if a message is received */
  773. void cec_received_msg(struct cec_adapter *adap, struct cec_msg *msg)
  774. {
  775. struct cec_data *data;
  776. u8 msg_init = cec_msg_initiator(msg);
  777. u8 msg_dest = cec_msg_destination(msg);
  778. u8 cmd = msg->msg[1];
  779. bool is_reply = false;
  780. bool valid_la = true;
  781. u8 min_len = 0;
  782. if (WARN_ON(!msg->len || msg->len > CEC_MAX_MSG_SIZE))
  783. return;
  784. /*
  785. * Some CEC adapters will receive the messages that they transmitted.
  786. * This test filters out those messages by checking if we are the
  787. * initiator, and just returning in that case.
  788. *
  789. * Note that this won't work if this is an Unregistered device.
  790. *
  791. * It is bad practice if the hardware receives the message that it
  792. * transmitted and luckily most CEC adapters behave correctly in this
  793. * respect.
  794. */
  795. if (msg_init != CEC_LOG_ADDR_UNREGISTERED &&
  796. cec_has_log_addr(adap, msg_init))
  797. return;
  798. msg->rx_ts = ktime_get_ns();
  799. msg->rx_status = CEC_RX_STATUS_OK;
  800. msg->sequence = msg->reply = msg->timeout = 0;
  801. msg->tx_status = 0;
  802. msg->tx_ts = 0;
  803. msg->tx_arb_lost_cnt = 0;
  804. msg->tx_nack_cnt = 0;
  805. msg->tx_low_drive_cnt = 0;
  806. msg->tx_error_cnt = 0;
  807. msg->flags = 0;
  808. memset(msg->msg + msg->len, 0, sizeof(msg->msg) - msg->len);
  809. mutex_lock(&adap->lock);
  810. dprintk(2, "cec_received_msg: %*ph\n", msg->len, msg->msg);
  811. /* Check if this message was for us (directed or broadcast). */
  812. if (!cec_msg_is_broadcast(msg))
  813. valid_la = cec_has_log_addr(adap, msg_dest);
  814. /*
  815. * Check if the length is not too short or if the message is a
  816. * broadcast message where a directed message was expected or
  817. * vice versa. If so, then the message has to be ignored (according
  818. * to section CEC 7.3 and CEC 12.2).
  819. */
  820. if (valid_la && msg->len > 1 && cec_msg_size[cmd]) {
  821. u8 dir_fl = cec_msg_size[cmd] & BOTH;
  822. min_len = cec_msg_size[cmd] & 0x1f;
  823. if (msg->len < min_len)
  824. valid_la = false;
  825. else if (!cec_msg_is_broadcast(msg) && !(dir_fl & DIRECTED))
  826. valid_la = false;
  827. else if (cec_msg_is_broadcast(msg) && !(dir_fl & BCAST1_4))
  828. valid_la = false;
  829. else if (cec_msg_is_broadcast(msg) &&
  830. adap->log_addrs.cec_version >= CEC_OP_CEC_VERSION_2_0 &&
  831. !(dir_fl & BCAST2_0))
  832. valid_la = false;
  833. }
  834. if (valid_la && min_len) {
  835. /* These messages have special length requirements */
  836. switch (cmd) {
  837. case CEC_MSG_TIMER_STATUS:
  838. if (msg->msg[2] & 0x10) {
  839. switch (msg->msg[2] & 0xf) {
  840. case CEC_OP_PROG_INFO_NOT_ENOUGH_SPACE:
  841. case CEC_OP_PROG_INFO_MIGHT_NOT_BE_ENOUGH_SPACE:
  842. if (msg->len < 5)
  843. valid_la = false;
  844. break;
  845. }
  846. } else if ((msg->msg[2] & 0xf) == CEC_OP_PROG_ERROR_DUPLICATE) {
  847. if (msg->len < 5)
  848. valid_la = false;
  849. }
  850. break;
  851. case CEC_MSG_RECORD_ON:
  852. switch (msg->msg[2]) {
  853. case CEC_OP_RECORD_SRC_OWN:
  854. break;
  855. case CEC_OP_RECORD_SRC_DIGITAL:
  856. if (msg->len < 10)
  857. valid_la = false;
  858. break;
  859. case CEC_OP_RECORD_SRC_ANALOG:
  860. if (msg->len < 7)
  861. valid_la = false;
  862. break;
  863. case CEC_OP_RECORD_SRC_EXT_PLUG:
  864. if (msg->len < 4)
  865. valid_la = false;
  866. break;
  867. case CEC_OP_RECORD_SRC_EXT_PHYS_ADDR:
  868. if (msg->len < 5)
  869. valid_la = false;
  870. break;
  871. }
  872. break;
  873. }
  874. }
  875. /* It's a valid message and not a poll or CDC message */
  876. if (valid_la && msg->len > 1 && cmd != CEC_MSG_CDC_MESSAGE) {
  877. bool abort = cmd == CEC_MSG_FEATURE_ABORT;
  878. /* The aborted command is in msg[2] */
  879. if (abort)
  880. cmd = msg->msg[2];
  881. /*
  882. * Walk over all transmitted messages that are waiting for a
  883. * reply.
  884. */
  885. list_for_each_entry(data, &adap->wait_queue, list) {
  886. struct cec_msg *dst = &data->msg;
  887. /*
  888. * The *only* CEC message that has two possible replies
  889. * is CEC_MSG_INITIATE_ARC.
  890. * In this case allow either of the two replies.
  891. */
  892. if (!abort && dst->msg[1] == CEC_MSG_INITIATE_ARC &&
  893. (cmd == CEC_MSG_REPORT_ARC_INITIATED ||
  894. cmd == CEC_MSG_REPORT_ARC_TERMINATED) &&
  895. (dst->reply == CEC_MSG_REPORT_ARC_INITIATED ||
  896. dst->reply == CEC_MSG_REPORT_ARC_TERMINATED))
  897. dst->reply = cmd;
  898. /* Does the command match? */
  899. if ((abort && cmd != dst->msg[1]) ||
  900. (!abort && cmd != dst->reply))
  901. continue;
  902. /* Does the addressing match? */
  903. if (msg_init != cec_msg_destination(dst) &&
  904. !cec_msg_is_broadcast(dst))
  905. continue;
  906. /* We got a reply */
  907. memcpy(dst->msg, msg->msg, msg->len);
  908. dst->len = msg->len;
  909. dst->rx_ts = msg->rx_ts;
  910. dst->rx_status = msg->rx_status;
  911. if (abort)
  912. dst->rx_status |= CEC_RX_STATUS_FEATURE_ABORT;
  913. msg->flags = dst->flags;
  914. /* Remove it from the wait_queue */
  915. list_del_init(&data->list);
  916. /* Cancel the pending timeout work */
  917. if (!cancel_delayed_work(&data->work)) {
  918. mutex_unlock(&adap->lock);
  919. flush_scheduled_work();
  920. mutex_lock(&adap->lock);
  921. }
  922. /*
  923. * Mark this as a reply, provided someone is still
  924. * waiting for the answer.
  925. */
  926. if (data->fh)
  927. is_reply = true;
  928. cec_data_completed(data);
  929. break;
  930. }
  931. }
  932. mutex_unlock(&adap->lock);
  933. /* Pass the message on to any monitoring filehandles */
  934. cec_queue_msg_monitor(adap, msg, valid_la);
  935. /* We're done if it is not for us or a poll message */
  936. if (!valid_la || msg->len <= 1)
  937. return;
  938. if (adap->log_addrs.log_addr_mask == 0)
  939. return;
  940. /*
  941. * Process the message on the protocol level. If is_reply is true,
  942. * then cec_receive_notify() won't pass on the reply to the listener(s)
  943. * since that was already done by cec_data_completed() above.
  944. */
  945. cec_receive_notify(adap, msg, is_reply);
  946. }
  947. EXPORT_SYMBOL_GPL(cec_received_msg);
  948. /* Logical Address Handling */
  949. /*
  950. * Attempt to claim a specific logical address.
  951. *
  952. * This function is called with adap->lock held.
  953. */
  954. static int cec_config_log_addr(struct cec_adapter *adap,
  955. unsigned int idx,
  956. unsigned int log_addr)
  957. {
  958. struct cec_log_addrs *las = &adap->log_addrs;
  959. struct cec_msg msg = { };
  960. int err;
  961. if (cec_has_log_addr(adap, log_addr))
  962. return 0;
  963. /* Send poll message */
  964. msg.len = 1;
  965. msg.msg[0] = 0xf0 | log_addr;
  966. err = cec_transmit_msg_fh(adap, &msg, NULL, true);
  967. /*
  968. * While trying to poll the physical address was reset
  969. * and the adapter was unconfigured, so bail out.
  970. */
  971. if (!adap->is_configuring)
  972. return -EINTR;
  973. if (err)
  974. return err;
  975. if (msg.tx_status & CEC_TX_STATUS_OK)
  976. return 0;
  977. /*
  978. * Message not acknowledged, so this logical
  979. * address is free to use.
  980. */
  981. err = adap->ops->adap_log_addr(adap, log_addr);
  982. if (err)
  983. return err;
  984. las->log_addr[idx] = log_addr;
  985. las->log_addr_mask |= 1 << log_addr;
  986. adap->phys_addrs[log_addr] = adap->phys_addr;
  987. dprintk(2, "claimed addr %d (%d)\n", log_addr,
  988. las->primary_device_type[idx]);
  989. return 1;
  990. }
  991. /*
  992. * Unconfigure the adapter: clear all logical addresses and send
  993. * the state changed event.
  994. *
  995. * This function is called with adap->lock held.
  996. */
  997. static void cec_adap_unconfigure(struct cec_adapter *adap)
  998. {
  999. WARN_ON(adap->ops->adap_log_addr(adap, CEC_LOG_ADDR_INVALID));
  1000. adap->log_addrs.log_addr_mask = 0;
  1001. adap->is_configuring = false;
  1002. adap->is_configured = false;
  1003. memset(adap->phys_addrs, 0xff, sizeof(adap->phys_addrs));
  1004. wake_up_interruptible(&adap->kthread_waitq);
  1005. cec_post_state_event(adap);
  1006. }
  1007. /*
  1008. * Attempt to claim the required logical addresses.
  1009. */
  1010. static int cec_config_thread_func(void *arg)
  1011. {
  1012. /* The various LAs for each type of device */
  1013. static const u8 tv_log_addrs[] = {
  1014. CEC_LOG_ADDR_TV, CEC_LOG_ADDR_SPECIFIC,
  1015. CEC_LOG_ADDR_INVALID
  1016. };
  1017. static const u8 record_log_addrs[] = {
  1018. CEC_LOG_ADDR_RECORD_1, CEC_LOG_ADDR_RECORD_2,
  1019. CEC_LOG_ADDR_RECORD_3,
  1020. CEC_LOG_ADDR_BACKUP_1, CEC_LOG_ADDR_BACKUP_2,
  1021. CEC_LOG_ADDR_INVALID
  1022. };
  1023. static const u8 tuner_log_addrs[] = {
  1024. CEC_LOG_ADDR_TUNER_1, CEC_LOG_ADDR_TUNER_2,
  1025. CEC_LOG_ADDR_TUNER_3, CEC_LOG_ADDR_TUNER_4,
  1026. CEC_LOG_ADDR_BACKUP_1, CEC_LOG_ADDR_BACKUP_2,
  1027. CEC_LOG_ADDR_INVALID
  1028. };
  1029. static const u8 playback_log_addrs[] = {
  1030. CEC_LOG_ADDR_PLAYBACK_1, CEC_LOG_ADDR_PLAYBACK_2,
  1031. CEC_LOG_ADDR_PLAYBACK_3,
  1032. CEC_LOG_ADDR_BACKUP_1, CEC_LOG_ADDR_BACKUP_2,
  1033. CEC_LOG_ADDR_INVALID
  1034. };
  1035. static const u8 audiosystem_log_addrs[] = {
  1036. CEC_LOG_ADDR_AUDIOSYSTEM,
  1037. CEC_LOG_ADDR_INVALID
  1038. };
  1039. static const u8 specific_use_log_addrs[] = {
  1040. CEC_LOG_ADDR_SPECIFIC,
  1041. CEC_LOG_ADDR_BACKUP_1, CEC_LOG_ADDR_BACKUP_2,
  1042. CEC_LOG_ADDR_INVALID
  1043. };
  1044. static const u8 *type2addrs[6] = {
  1045. [CEC_LOG_ADDR_TYPE_TV] = tv_log_addrs,
  1046. [CEC_LOG_ADDR_TYPE_RECORD] = record_log_addrs,
  1047. [CEC_LOG_ADDR_TYPE_TUNER] = tuner_log_addrs,
  1048. [CEC_LOG_ADDR_TYPE_PLAYBACK] = playback_log_addrs,
  1049. [CEC_LOG_ADDR_TYPE_AUDIOSYSTEM] = audiosystem_log_addrs,
  1050. [CEC_LOG_ADDR_TYPE_SPECIFIC] = specific_use_log_addrs,
  1051. };
  1052. static const u16 type2mask[] = {
  1053. [CEC_LOG_ADDR_TYPE_TV] = CEC_LOG_ADDR_MASK_TV,
  1054. [CEC_LOG_ADDR_TYPE_RECORD] = CEC_LOG_ADDR_MASK_RECORD,
  1055. [CEC_LOG_ADDR_TYPE_TUNER] = CEC_LOG_ADDR_MASK_TUNER,
  1056. [CEC_LOG_ADDR_TYPE_PLAYBACK] = CEC_LOG_ADDR_MASK_PLAYBACK,
  1057. [CEC_LOG_ADDR_TYPE_AUDIOSYSTEM] = CEC_LOG_ADDR_MASK_AUDIOSYSTEM,
  1058. [CEC_LOG_ADDR_TYPE_SPECIFIC] = CEC_LOG_ADDR_MASK_SPECIFIC,
  1059. };
  1060. struct cec_adapter *adap = arg;
  1061. struct cec_log_addrs *las = &adap->log_addrs;
  1062. int err;
  1063. int i, j;
  1064. mutex_lock(&adap->lock);
  1065. dprintk(1, "physical address: %x.%x.%x.%x, claim %d logical addresses\n",
  1066. cec_phys_addr_exp(adap->phys_addr), las->num_log_addrs);
  1067. las->log_addr_mask = 0;
  1068. if (las->log_addr_type[0] == CEC_LOG_ADDR_TYPE_UNREGISTERED)
  1069. goto configured;
  1070. for (i = 0; i < las->num_log_addrs; i++) {
  1071. unsigned int type = las->log_addr_type[i];
  1072. const u8 *la_list;
  1073. u8 last_la;
  1074. /*
  1075. * The TV functionality can only map to physical address 0.
  1076. * For any other address, try the Specific functionality
  1077. * instead as per the spec.
  1078. */
  1079. if (adap->phys_addr && type == CEC_LOG_ADDR_TYPE_TV)
  1080. type = CEC_LOG_ADDR_TYPE_SPECIFIC;
  1081. la_list = type2addrs[type];
  1082. last_la = las->log_addr[i];
  1083. las->log_addr[i] = CEC_LOG_ADDR_INVALID;
  1084. if (last_la == CEC_LOG_ADDR_INVALID ||
  1085. last_la == CEC_LOG_ADDR_UNREGISTERED ||
  1086. !(last_la & type2mask[type]))
  1087. last_la = la_list[0];
  1088. err = cec_config_log_addr(adap, i, last_la);
  1089. if (err > 0) /* Reused last LA */
  1090. continue;
  1091. if (err < 0)
  1092. goto unconfigure;
  1093. for (j = 0; la_list[j] != CEC_LOG_ADDR_INVALID; j++) {
  1094. /* Tried this one already, skip it */
  1095. if (la_list[j] == last_la)
  1096. continue;
  1097. /* The backup addresses are CEC 2.0 specific */
  1098. if ((la_list[j] == CEC_LOG_ADDR_BACKUP_1 ||
  1099. la_list[j] == CEC_LOG_ADDR_BACKUP_2) &&
  1100. las->cec_version < CEC_OP_CEC_VERSION_2_0)
  1101. continue;
  1102. err = cec_config_log_addr(adap, i, la_list[j]);
  1103. if (err == 0) /* LA is in use */
  1104. continue;
  1105. if (err < 0)
  1106. goto unconfigure;
  1107. /* Done, claimed an LA */
  1108. break;
  1109. }
  1110. if (la_list[j] == CEC_LOG_ADDR_INVALID)
  1111. dprintk(1, "could not claim LA %d\n", i);
  1112. }
  1113. if (adap->log_addrs.log_addr_mask == 0 &&
  1114. !(las->flags & CEC_LOG_ADDRS_FL_ALLOW_UNREG_FALLBACK))
  1115. goto unconfigure;
  1116. configured:
  1117. if (adap->log_addrs.log_addr_mask == 0) {
  1118. /* Fall back to unregistered */
  1119. las->log_addr[0] = CEC_LOG_ADDR_UNREGISTERED;
  1120. las->log_addr_mask = 1 << las->log_addr[0];
  1121. for (i = 1; i < las->num_log_addrs; i++)
  1122. las->log_addr[i] = CEC_LOG_ADDR_INVALID;
  1123. }
  1124. adap->is_configured = true;
  1125. adap->is_configuring = false;
  1126. cec_post_state_event(adap);
  1127. mutex_unlock(&adap->lock);
  1128. for (i = 0; i < las->num_log_addrs; i++) {
  1129. if (las->log_addr[i] == CEC_LOG_ADDR_INVALID ||
  1130. (las->flags & CEC_LOG_ADDRS_FL_CDC_ONLY))
  1131. continue;
  1132. /*
  1133. * Report Features must come first according
  1134. * to CEC 2.0
  1135. */
  1136. if (las->log_addr[i] != CEC_LOG_ADDR_UNREGISTERED)
  1137. cec_report_features(adap, i);
  1138. cec_report_phys_addr(adap, i);
  1139. }
  1140. for (i = las->num_log_addrs; i < CEC_MAX_LOG_ADDRS; i++)
  1141. las->log_addr[i] = CEC_LOG_ADDR_INVALID;
  1142. mutex_lock(&adap->lock);
  1143. adap->kthread_config = NULL;
  1144. mutex_unlock(&adap->lock);
  1145. complete(&adap->config_completion);
  1146. return 0;
  1147. unconfigure:
  1148. for (i = 0; i < las->num_log_addrs; i++)
  1149. las->log_addr[i] = CEC_LOG_ADDR_INVALID;
  1150. cec_adap_unconfigure(adap);
  1151. adap->kthread_config = NULL;
  1152. mutex_unlock(&adap->lock);
  1153. complete(&adap->config_completion);
  1154. return 0;
  1155. }
  1156. /*
  1157. * Called from either __cec_s_phys_addr or __cec_s_log_addrs to claim the
  1158. * logical addresses.
  1159. *
  1160. * This function is called with adap->lock held.
  1161. */
  1162. static void cec_claim_log_addrs(struct cec_adapter *adap, bool block)
  1163. {
  1164. if (WARN_ON(adap->is_configuring || adap->is_configured))
  1165. return;
  1166. init_completion(&adap->config_completion);
  1167. /* Ready to kick off the thread */
  1168. adap->is_configuring = true;
  1169. adap->kthread_config = kthread_run(cec_config_thread_func, adap,
  1170. "ceccfg-%s", adap->name);
  1171. if (IS_ERR(adap->kthread_config)) {
  1172. adap->kthread_config = NULL;
  1173. } else if (block) {
  1174. mutex_unlock(&adap->lock);
  1175. wait_for_completion(&adap->config_completion);
  1176. mutex_lock(&adap->lock);
  1177. }
  1178. }
  1179. /* Set a new physical address and send an event notifying userspace of this.
  1180. *
  1181. * This function is called with adap->lock held.
  1182. */
  1183. void __cec_s_phys_addr(struct cec_adapter *adap, u16 phys_addr, bool block)
  1184. {
  1185. if (phys_addr == adap->phys_addr || adap->devnode.unregistered)
  1186. return;
  1187. if (phys_addr == CEC_PHYS_ADDR_INVALID ||
  1188. adap->phys_addr != CEC_PHYS_ADDR_INVALID) {
  1189. adap->phys_addr = CEC_PHYS_ADDR_INVALID;
  1190. cec_post_state_event(adap);
  1191. cec_adap_unconfigure(adap);
  1192. /* Disabling monitor all mode should always succeed */
  1193. if (adap->monitor_all_cnt)
  1194. WARN_ON(call_op(adap, adap_monitor_all_enable, false));
  1195. WARN_ON(adap->ops->adap_enable(adap, false));
  1196. if (phys_addr == CEC_PHYS_ADDR_INVALID)
  1197. return;
  1198. }
  1199. if (adap->ops->adap_enable(adap, true))
  1200. return;
  1201. if (adap->monitor_all_cnt &&
  1202. call_op(adap, adap_monitor_all_enable, true)) {
  1203. WARN_ON(adap->ops->adap_enable(adap, false));
  1204. return;
  1205. }
  1206. adap->phys_addr = phys_addr;
  1207. cec_post_state_event(adap);
  1208. if (adap->log_addrs.num_log_addrs)
  1209. cec_claim_log_addrs(adap, block);
  1210. }
  1211. void cec_s_phys_addr(struct cec_adapter *adap, u16 phys_addr, bool block)
  1212. {
  1213. if (IS_ERR_OR_NULL(adap))
  1214. return;
  1215. mutex_lock(&adap->lock);
  1216. __cec_s_phys_addr(adap, phys_addr, block);
  1217. mutex_unlock(&adap->lock);
  1218. }
  1219. EXPORT_SYMBOL_GPL(cec_s_phys_addr);
  1220. /*
  1221. * Called from either the ioctl or a driver to set the logical addresses.
  1222. *
  1223. * This function is called with adap->lock held.
  1224. */
  1225. int __cec_s_log_addrs(struct cec_adapter *adap,
  1226. struct cec_log_addrs *log_addrs, bool block)
  1227. {
  1228. u16 type_mask = 0;
  1229. int i;
  1230. if (adap->devnode.unregistered)
  1231. return -ENODEV;
  1232. if (!log_addrs || log_addrs->num_log_addrs == 0) {
  1233. adap->log_addrs.num_log_addrs = 0;
  1234. cec_adap_unconfigure(adap);
  1235. return 0;
  1236. }
  1237. if (log_addrs->flags & CEC_LOG_ADDRS_FL_CDC_ONLY) {
  1238. /*
  1239. * Sanitize log_addrs fields if a CDC-Only device is
  1240. * requested.
  1241. */
  1242. log_addrs->num_log_addrs = 1;
  1243. log_addrs->osd_name[0] = '\0';
  1244. log_addrs->vendor_id = CEC_VENDOR_ID_NONE;
  1245. log_addrs->log_addr_type[0] = CEC_LOG_ADDR_TYPE_UNREGISTERED;
  1246. /*
  1247. * This is just an internal convention since a CDC-Only device
  1248. * doesn't have to be a switch. But switches already use
  1249. * unregistered, so it makes some kind of sense to pick this
  1250. * as the primary device. Since a CDC-Only device never sends
  1251. * any 'normal' CEC messages this primary device type is never
  1252. * sent over the CEC bus.
  1253. */
  1254. log_addrs->primary_device_type[0] = CEC_OP_PRIM_DEVTYPE_SWITCH;
  1255. log_addrs->all_device_types[0] = 0;
  1256. log_addrs->features[0][0] = 0;
  1257. log_addrs->features[0][1] = 0;
  1258. }
  1259. /* Ensure the osd name is 0-terminated */
  1260. log_addrs->osd_name[sizeof(log_addrs->osd_name) - 1] = '\0';
  1261. /* Sanity checks */
  1262. if (log_addrs->num_log_addrs > adap->available_log_addrs) {
  1263. dprintk(1, "num_log_addrs > %d\n", adap->available_log_addrs);
  1264. return -EINVAL;
  1265. }
  1266. /*
  1267. * Vendor ID is a 24 bit number, so check if the value is
  1268. * within the correct range.
  1269. */
  1270. if (log_addrs->vendor_id != CEC_VENDOR_ID_NONE &&
  1271. (log_addrs->vendor_id & 0xff000000) != 0)
  1272. return -EINVAL;
  1273. if (log_addrs->cec_version != CEC_OP_CEC_VERSION_1_4 &&
  1274. log_addrs->cec_version != CEC_OP_CEC_VERSION_2_0)
  1275. return -EINVAL;
  1276. if (log_addrs->num_log_addrs > 1)
  1277. for (i = 0; i < log_addrs->num_log_addrs; i++)
  1278. if (log_addrs->log_addr_type[i] ==
  1279. CEC_LOG_ADDR_TYPE_UNREGISTERED) {
  1280. dprintk(1, "num_log_addrs > 1 can't be combined with unregistered LA\n");
  1281. return -EINVAL;
  1282. }
  1283. for (i = 0; i < log_addrs->num_log_addrs; i++) {
  1284. const u8 feature_sz = ARRAY_SIZE(log_addrs->features[0]);
  1285. u8 *features = log_addrs->features[i];
  1286. bool op_is_dev_features = false;
  1287. unsigned j;
  1288. log_addrs->log_addr[i] = CEC_LOG_ADDR_INVALID;
  1289. if (type_mask & (1 << log_addrs->log_addr_type[i])) {
  1290. dprintk(1, "duplicate logical address type\n");
  1291. return -EINVAL;
  1292. }
  1293. type_mask |= 1 << log_addrs->log_addr_type[i];
  1294. if ((type_mask & (1 << CEC_LOG_ADDR_TYPE_RECORD)) &&
  1295. (type_mask & (1 << CEC_LOG_ADDR_TYPE_PLAYBACK))) {
  1296. /* Record already contains the playback functionality */
  1297. dprintk(1, "invalid record + playback combination\n");
  1298. return -EINVAL;
  1299. }
  1300. if (log_addrs->primary_device_type[i] >
  1301. CEC_OP_PRIM_DEVTYPE_PROCESSOR) {
  1302. dprintk(1, "unknown primary device type\n");
  1303. return -EINVAL;
  1304. }
  1305. if (log_addrs->primary_device_type[i] == 2) {
  1306. dprintk(1, "invalid primary device type\n");
  1307. return -EINVAL;
  1308. }
  1309. if (log_addrs->log_addr_type[i] > CEC_LOG_ADDR_TYPE_UNREGISTERED) {
  1310. dprintk(1, "unknown logical address type\n");
  1311. return -EINVAL;
  1312. }
  1313. for (j = 0; j < feature_sz; j++) {
  1314. if ((features[j] & 0x80) == 0) {
  1315. if (op_is_dev_features)
  1316. break;
  1317. op_is_dev_features = true;
  1318. }
  1319. }
  1320. if (!op_is_dev_features || j == feature_sz) {
  1321. dprintk(1, "malformed features\n");
  1322. return -EINVAL;
  1323. }
  1324. /* Zero unused part of the feature array */
  1325. memset(features + j + 1, 0, feature_sz - j - 1);
  1326. }
  1327. if (log_addrs->cec_version >= CEC_OP_CEC_VERSION_2_0) {
  1328. if (log_addrs->num_log_addrs > 2) {
  1329. dprintk(1, "CEC 2.0 allows no more than 2 logical addresses\n");
  1330. return -EINVAL;
  1331. }
  1332. if (log_addrs->num_log_addrs == 2) {
  1333. if (!(type_mask & ((1 << CEC_LOG_ADDR_TYPE_AUDIOSYSTEM) |
  1334. (1 << CEC_LOG_ADDR_TYPE_TV)))) {
  1335. dprintk(1, "Two LAs is only allowed for audiosystem and TV\n");
  1336. return -EINVAL;
  1337. }
  1338. if (!(type_mask & ((1 << CEC_LOG_ADDR_TYPE_PLAYBACK) |
  1339. (1 << CEC_LOG_ADDR_TYPE_RECORD)))) {
  1340. dprintk(1, "An audiosystem/TV can only be combined with record or playback\n");
  1341. return -EINVAL;
  1342. }
  1343. }
  1344. }
  1345. /* Zero unused LAs */
  1346. for (i = log_addrs->num_log_addrs; i < CEC_MAX_LOG_ADDRS; i++) {
  1347. log_addrs->primary_device_type[i] = 0;
  1348. log_addrs->log_addr_type[i] = 0;
  1349. log_addrs->all_device_types[i] = 0;
  1350. memset(log_addrs->features[i], 0,
  1351. sizeof(log_addrs->features[i]));
  1352. }
  1353. log_addrs->log_addr_mask = adap->log_addrs.log_addr_mask;
  1354. adap->log_addrs = *log_addrs;
  1355. if (adap->phys_addr != CEC_PHYS_ADDR_INVALID)
  1356. cec_claim_log_addrs(adap, block);
  1357. return 0;
  1358. }
  1359. int cec_s_log_addrs(struct cec_adapter *adap,
  1360. struct cec_log_addrs *log_addrs, bool block)
  1361. {
  1362. int err;
  1363. mutex_lock(&adap->lock);
  1364. err = __cec_s_log_addrs(adap, log_addrs, block);
  1365. mutex_unlock(&adap->lock);
  1366. return err;
  1367. }
  1368. EXPORT_SYMBOL_GPL(cec_s_log_addrs);
  1369. /* High-level core CEC message handling */
  1370. /* Transmit the Report Features message */
  1371. static int cec_report_features(struct cec_adapter *adap, unsigned int la_idx)
  1372. {
  1373. struct cec_msg msg = { };
  1374. const struct cec_log_addrs *las = &adap->log_addrs;
  1375. const u8 *features = las->features[la_idx];
  1376. bool op_is_dev_features = false;
  1377. unsigned int idx;
  1378. /* This is 2.0 and up only */
  1379. if (adap->log_addrs.cec_version < CEC_OP_CEC_VERSION_2_0)
  1380. return 0;
  1381. /* Report Features */
  1382. msg.msg[0] = (las->log_addr[la_idx] << 4) | 0x0f;
  1383. msg.len = 4;
  1384. msg.msg[1] = CEC_MSG_REPORT_FEATURES;
  1385. msg.msg[2] = adap->log_addrs.cec_version;
  1386. msg.msg[3] = las->all_device_types[la_idx];
  1387. /* Write RC Profiles first, then Device Features */
  1388. for (idx = 0; idx < ARRAY_SIZE(las->features[0]); idx++) {
  1389. msg.msg[msg.len++] = features[idx];
  1390. if ((features[idx] & CEC_OP_FEAT_EXT) == 0) {
  1391. if (op_is_dev_features)
  1392. break;
  1393. op_is_dev_features = true;
  1394. }
  1395. }
  1396. return cec_transmit_msg(adap, &msg, false);
  1397. }
  1398. /* Transmit the Report Physical Address message */
  1399. static int cec_report_phys_addr(struct cec_adapter *adap, unsigned int la_idx)
  1400. {
  1401. const struct cec_log_addrs *las = &adap->log_addrs;
  1402. struct cec_msg msg = { };
  1403. /* Report Physical Address */
  1404. msg.msg[0] = (las->log_addr[la_idx] << 4) | 0x0f;
  1405. cec_msg_report_physical_addr(&msg, adap->phys_addr,
  1406. las->primary_device_type[la_idx]);
  1407. dprintk(2, "config: la %d pa %x.%x.%x.%x\n",
  1408. las->log_addr[la_idx],
  1409. cec_phys_addr_exp(adap->phys_addr));
  1410. return cec_transmit_msg(adap, &msg, false);
  1411. }
  1412. /* Transmit the Feature Abort message */
  1413. static int cec_feature_abort_reason(struct cec_adapter *adap,
  1414. struct cec_msg *msg, u8 reason)
  1415. {
  1416. struct cec_msg tx_msg = { };
  1417. /*
  1418. * Don't reply with CEC_MSG_FEATURE_ABORT to a CEC_MSG_FEATURE_ABORT
  1419. * message!
  1420. */
  1421. if (msg->msg[1] == CEC_MSG_FEATURE_ABORT)
  1422. return 0;
  1423. cec_msg_set_reply_to(&tx_msg, msg);
  1424. cec_msg_feature_abort(&tx_msg, msg->msg[1], reason);
  1425. return cec_transmit_msg(adap, &tx_msg, false);
  1426. }
  1427. static int cec_feature_abort(struct cec_adapter *adap, struct cec_msg *msg)
  1428. {
  1429. return cec_feature_abort_reason(adap, msg,
  1430. CEC_OP_ABORT_UNRECOGNIZED_OP);
  1431. }
  1432. static int cec_feature_refused(struct cec_adapter *adap, struct cec_msg *msg)
  1433. {
  1434. return cec_feature_abort_reason(adap, msg,
  1435. CEC_OP_ABORT_REFUSED);
  1436. }
  1437. /*
  1438. * Called when a CEC message is received. This function will do any
  1439. * necessary core processing. The is_reply bool is true if this message
  1440. * is a reply to an earlier transmit.
  1441. *
  1442. * The message is either a broadcast message or a valid directed message.
  1443. */
  1444. static int cec_receive_notify(struct cec_adapter *adap, struct cec_msg *msg,
  1445. bool is_reply)
  1446. {
  1447. bool is_broadcast = cec_msg_is_broadcast(msg);
  1448. u8 dest_laddr = cec_msg_destination(msg);
  1449. u8 init_laddr = cec_msg_initiator(msg);
  1450. u8 devtype = cec_log_addr2dev(adap, dest_laddr);
  1451. int la_idx = cec_log_addr2idx(adap, dest_laddr);
  1452. bool from_unregistered = init_laddr == 0xf;
  1453. struct cec_msg tx_cec_msg = { };
  1454. dprintk(1, "cec_receive_notify: %*ph\n", msg->len, msg->msg);
  1455. /* If this is a CDC-Only device, then ignore any non-CDC messages */
  1456. if (cec_is_cdc_only(&adap->log_addrs) &&
  1457. msg->msg[1] != CEC_MSG_CDC_MESSAGE)
  1458. return 0;
  1459. if (adap->ops->received) {
  1460. /* Allow drivers to process the message first */
  1461. if (adap->ops->received(adap, msg) != -ENOMSG)
  1462. return 0;
  1463. }
  1464. /*
  1465. * REPORT_PHYSICAL_ADDR, CEC_MSG_USER_CONTROL_PRESSED and
  1466. * CEC_MSG_USER_CONTROL_RELEASED messages always have to be
  1467. * handled by the CEC core, even if the passthrough mode is on.
  1468. * The others are just ignored if passthrough mode is on.
  1469. */
  1470. switch (msg->msg[1]) {
  1471. case CEC_MSG_GET_CEC_VERSION:
  1472. case CEC_MSG_GIVE_DEVICE_VENDOR_ID:
  1473. case CEC_MSG_ABORT:
  1474. case CEC_MSG_GIVE_DEVICE_POWER_STATUS:
  1475. case CEC_MSG_GIVE_PHYSICAL_ADDR:
  1476. case CEC_MSG_GIVE_OSD_NAME:
  1477. case CEC_MSG_GIVE_FEATURES:
  1478. /*
  1479. * Skip processing these messages if the passthrough mode
  1480. * is on.
  1481. */
  1482. if (adap->passthrough)
  1483. goto skip_processing;
  1484. /* Ignore if addressing is wrong */
  1485. if (is_broadcast || from_unregistered)
  1486. return 0;
  1487. break;
  1488. case CEC_MSG_USER_CONTROL_PRESSED:
  1489. case CEC_MSG_USER_CONTROL_RELEASED:
  1490. /* Wrong addressing mode: don't process */
  1491. if (is_broadcast || from_unregistered)
  1492. goto skip_processing;
  1493. break;
  1494. case CEC_MSG_REPORT_PHYSICAL_ADDR:
  1495. /*
  1496. * This message is always processed, regardless of the
  1497. * passthrough setting.
  1498. *
  1499. * Exception: don't process if wrong addressing mode.
  1500. */
  1501. if (!is_broadcast)
  1502. goto skip_processing;
  1503. break;
  1504. default:
  1505. break;
  1506. }
  1507. cec_msg_set_reply_to(&tx_cec_msg, msg);
  1508. switch (msg->msg[1]) {
  1509. /* The following messages are processed but still passed through */
  1510. case CEC_MSG_REPORT_PHYSICAL_ADDR: {
  1511. u16 pa = (msg->msg[2] << 8) | msg->msg[3];
  1512. if (!from_unregistered)
  1513. adap->phys_addrs[init_laddr] = pa;
  1514. dprintk(1, "Reported physical address %x.%x.%x.%x for logical address %d\n",
  1515. cec_phys_addr_exp(pa), init_laddr);
  1516. break;
  1517. }
  1518. case CEC_MSG_USER_CONTROL_PRESSED:
  1519. if (!(adap->capabilities & CEC_CAP_RC) ||
  1520. !(adap->log_addrs.flags & CEC_LOG_ADDRS_FL_ALLOW_RC_PASSTHRU))
  1521. break;
  1522. #if IS_REACHABLE(CONFIG_RC_CORE)
  1523. switch (msg->msg[2]) {
  1524. /*
  1525. * Play function, this message can have variable length
  1526. * depending on the specific play function that is used.
  1527. */
  1528. case 0x60:
  1529. if (msg->len == 2)
  1530. rc_keydown(adap->rc, RC_TYPE_CEC,
  1531. msg->msg[2], 0);
  1532. else
  1533. rc_keydown(adap->rc, RC_TYPE_CEC,
  1534. msg->msg[2] << 8 | msg->msg[3], 0);
  1535. break;
  1536. /*
  1537. * Other function messages that are not handled.
  1538. * Currently the RC framework does not allow to supply an
  1539. * additional parameter to a keypress. These "keys" contain
  1540. * other information such as channel number, an input number
  1541. * etc.
  1542. * For the time being these messages are not processed by the
  1543. * framework and are simply forwarded to the user space.
  1544. */
  1545. case 0x56: case 0x57:
  1546. case 0x67: case 0x68: case 0x69: case 0x6a:
  1547. break;
  1548. default:
  1549. rc_keydown(adap->rc, RC_TYPE_CEC, msg->msg[2], 0);
  1550. break;
  1551. }
  1552. #endif
  1553. break;
  1554. case CEC_MSG_USER_CONTROL_RELEASED:
  1555. if (!(adap->capabilities & CEC_CAP_RC) ||
  1556. !(adap->log_addrs.flags & CEC_LOG_ADDRS_FL_ALLOW_RC_PASSTHRU))
  1557. break;
  1558. #if IS_REACHABLE(CONFIG_RC_CORE)
  1559. rc_keyup(adap->rc);
  1560. #endif
  1561. break;
  1562. /*
  1563. * The remaining messages are only processed if the passthrough mode
  1564. * is off.
  1565. */
  1566. case CEC_MSG_GET_CEC_VERSION:
  1567. cec_msg_cec_version(&tx_cec_msg, adap->log_addrs.cec_version);
  1568. return cec_transmit_msg(adap, &tx_cec_msg, false);
  1569. case CEC_MSG_GIVE_PHYSICAL_ADDR:
  1570. /* Do nothing for CEC switches using addr 15 */
  1571. if (devtype == CEC_OP_PRIM_DEVTYPE_SWITCH && dest_laddr == 15)
  1572. return 0;
  1573. cec_msg_report_physical_addr(&tx_cec_msg, adap->phys_addr, devtype);
  1574. return cec_transmit_msg(adap, &tx_cec_msg, false);
  1575. case CEC_MSG_GIVE_DEVICE_VENDOR_ID:
  1576. if (adap->log_addrs.vendor_id == CEC_VENDOR_ID_NONE)
  1577. return cec_feature_abort(adap, msg);
  1578. cec_msg_device_vendor_id(&tx_cec_msg, adap->log_addrs.vendor_id);
  1579. return cec_transmit_msg(adap, &tx_cec_msg, false);
  1580. case CEC_MSG_ABORT:
  1581. /* Do nothing for CEC switches */
  1582. if (devtype == CEC_OP_PRIM_DEVTYPE_SWITCH)
  1583. return 0;
  1584. return cec_feature_refused(adap, msg);
  1585. case CEC_MSG_GIVE_OSD_NAME: {
  1586. if (adap->log_addrs.osd_name[0] == 0)
  1587. return cec_feature_abort(adap, msg);
  1588. cec_msg_set_osd_name(&tx_cec_msg, adap->log_addrs.osd_name);
  1589. return cec_transmit_msg(adap, &tx_cec_msg, false);
  1590. }
  1591. case CEC_MSG_GIVE_FEATURES:
  1592. if (adap->log_addrs.cec_version >= CEC_OP_CEC_VERSION_2_0)
  1593. return cec_report_features(adap, la_idx);
  1594. return 0;
  1595. default:
  1596. /*
  1597. * Unprocessed messages are aborted if userspace isn't doing
  1598. * any processing either.
  1599. */
  1600. if (!is_broadcast && !is_reply && !adap->follower_cnt &&
  1601. !adap->cec_follower && msg->msg[1] != CEC_MSG_FEATURE_ABORT)
  1602. return cec_feature_abort(adap, msg);
  1603. break;
  1604. }
  1605. skip_processing:
  1606. /* If this was a reply, then we're done, unless otherwise specified */
  1607. if (is_reply && !(msg->flags & CEC_MSG_FL_REPLY_TO_FOLLOWERS))
  1608. return 0;
  1609. /*
  1610. * Send to the exclusive follower if there is one, otherwise send
  1611. * to all followers.
  1612. */
  1613. if (adap->cec_follower)
  1614. cec_queue_msg_fh(adap->cec_follower, msg);
  1615. else
  1616. cec_queue_msg_followers(adap, msg);
  1617. return 0;
  1618. }
  1619. /*
  1620. * Helper functions to keep track of the 'monitor all' use count.
  1621. *
  1622. * These functions are called with adap->lock held.
  1623. */
  1624. int cec_monitor_all_cnt_inc(struct cec_adapter *adap)
  1625. {
  1626. int ret = 0;
  1627. if (adap->monitor_all_cnt == 0)
  1628. ret = call_op(adap, adap_monitor_all_enable, 1);
  1629. if (ret == 0)
  1630. adap->monitor_all_cnt++;
  1631. return ret;
  1632. }
  1633. void cec_monitor_all_cnt_dec(struct cec_adapter *adap)
  1634. {
  1635. adap->monitor_all_cnt--;
  1636. if (adap->monitor_all_cnt == 0)
  1637. WARN_ON(call_op(adap, adap_monitor_all_enable, 0));
  1638. }
  1639. #ifdef CONFIG_MEDIA_CEC_DEBUG
  1640. /*
  1641. * Log the current state of the CEC adapter.
  1642. * Very useful for debugging.
  1643. */
  1644. int cec_adap_status(struct seq_file *file, void *priv)
  1645. {
  1646. struct cec_adapter *adap = dev_get_drvdata(file->private);
  1647. struct cec_data *data;
  1648. mutex_lock(&adap->lock);
  1649. seq_printf(file, "configured: %d\n", adap->is_configured);
  1650. seq_printf(file, "configuring: %d\n", adap->is_configuring);
  1651. seq_printf(file, "phys_addr: %x.%x.%x.%x\n",
  1652. cec_phys_addr_exp(adap->phys_addr));
  1653. seq_printf(file, "number of LAs: %d\n", adap->log_addrs.num_log_addrs);
  1654. seq_printf(file, "LA mask: 0x%04x\n", adap->log_addrs.log_addr_mask);
  1655. if (adap->cec_follower)
  1656. seq_printf(file, "has CEC follower%s\n",
  1657. adap->passthrough ? " (in passthrough mode)" : "");
  1658. if (adap->cec_initiator)
  1659. seq_puts(file, "has CEC initiator\n");
  1660. if (adap->monitor_all_cnt)
  1661. seq_printf(file, "file handles in Monitor All mode: %u\n",
  1662. adap->monitor_all_cnt);
  1663. data = adap->transmitting;
  1664. if (data)
  1665. seq_printf(file, "transmitting message: %*ph (reply: %02x, timeout: %ums)\n",
  1666. data->msg.len, data->msg.msg, data->msg.reply,
  1667. data->msg.timeout);
  1668. seq_printf(file, "pending transmits: %u\n", adap->transmit_queue_sz);
  1669. list_for_each_entry(data, &adap->transmit_queue, list) {
  1670. seq_printf(file, "queued tx message: %*ph (reply: %02x, timeout: %ums)\n",
  1671. data->msg.len, data->msg.msg, data->msg.reply,
  1672. data->msg.timeout);
  1673. }
  1674. list_for_each_entry(data, &adap->wait_queue, list) {
  1675. seq_printf(file, "message waiting for reply: %*ph (reply: %02x, timeout: %ums)\n",
  1676. data->msg.len, data->msg.msg, data->msg.reply,
  1677. data->msg.timeout);
  1678. }
  1679. call_void_op(adap, adap_status, file);
  1680. mutex_unlock(&adap->lock);
  1681. return 0;
  1682. }
  1683. #endif