cec-adap.c 55 KB

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