cec-adap.c 68 KB

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