cec-adap.c 59 KB

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