cec-adap.c 63 KB

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