cec-adap.c 61 KB

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