cec-adap.c 60 KB

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