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