hda_jack.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Jack-detection handling for HD-audio
  4. *
  5. * Copyright (c) 2011 Takashi Iwai <[email protected]>
  6. */
  7. #include <linux/init.h>
  8. #include <linux/slab.h>
  9. #include <linux/export.h>
  10. #include <sound/core.h>
  11. #include <sound/control.h>
  12. #include <sound/jack.h>
  13. #include <sound/hda_codec.h>
  14. #include "hda_local.h"
  15. #include "hda_auto_parser.h"
  16. #include "hda_jack.h"
  17. /**
  18. * is_jack_detectable - Check whether the given pin is jack-detectable
  19. * @codec: the HDA codec
  20. * @nid: pin NID
  21. *
  22. * Check whether the given pin is capable to report the jack detection.
  23. * The jack detection might not work by various reasons, e.g. the jack
  24. * detection is prohibited in the codec level, the pin config has
  25. * AC_DEFCFG_MISC_NO_PRESENCE bit, no unsol support, etc.
  26. */
  27. bool is_jack_detectable(struct hda_codec *codec, hda_nid_t nid)
  28. {
  29. if (codec->no_jack_detect)
  30. return false;
  31. if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_PRES_DETECT))
  32. return false;
  33. if (get_defcfg_misc(snd_hda_codec_get_pincfg(codec, nid)) &
  34. AC_DEFCFG_MISC_NO_PRESENCE)
  35. return false;
  36. if (!(get_wcaps(codec, nid) & AC_WCAP_UNSOL_CAP) &&
  37. !codec->jackpoll_interval)
  38. return false;
  39. return true;
  40. }
  41. EXPORT_SYMBOL_GPL(is_jack_detectable);
  42. /* execute pin sense measurement */
  43. static u32 read_pin_sense(struct hda_codec *codec, hda_nid_t nid, int dev_id)
  44. {
  45. u32 pincap;
  46. u32 val;
  47. if (!codec->no_trigger_sense) {
  48. pincap = snd_hda_query_pin_caps(codec, nid);
  49. if (pincap & AC_PINCAP_TRIG_REQ) /* need trigger? */
  50. snd_hda_codec_read(codec, nid, 0,
  51. AC_VERB_SET_PIN_SENSE, 0);
  52. }
  53. val = snd_hda_codec_read(codec, nid, 0,
  54. AC_VERB_GET_PIN_SENSE, dev_id);
  55. if (codec->inv_jack_detect)
  56. val ^= AC_PINSENSE_PRESENCE;
  57. return val;
  58. }
  59. /**
  60. * snd_hda_jack_tbl_get_mst - query the jack-table entry for the given NID
  61. * @codec: the HDA codec
  62. * @nid: pin NID to refer to
  63. * @dev_id: pin device entry id
  64. */
  65. struct hda_jack_tbl *
  66. snd_hda_jack_tbl_get_mst(struct hda_codec *codec, hda_nid_t nid, int dev_id)
  67. {
  68. struct hda_jack_tbl *jack = codec->jacktbl.list;
  69. int i;
  70. if (!nid || !jack)
  71. return NULL;
  72. for (i = 0; i < codec->jacktbl.used; i++, jack++)
  73. if (jack->nid == nid && jack->dev_id == dev_id)
  74. return jack;
  75. return NULL;
  76. }
  77. EXPORT_SYMBOL_GPL(snd_hda_jack_tbl_get_mst);
  78. /**
  79. * snd_hda_jack_tbl_get_from_tag - query the jack-table entry for the given tag
  80. * @codec: the HDA codec
  81. * @tag: tag value to refer to
  82. * @dev_id: pin device entry id
  83. */
  84. struct hda_jack_tbl *
  85. snd_hda_jack_tbl_get_from_tag(struct hda_codec *codec,
  86. unsigned char tag, int dev_id)
  87. {
  88. struct hda_jack_tbl *jack = codec->jacktbl.list;
  89. int i;
  90. if (!tag || !jack)
  91. return NULL;
  92. for (i = 0; i < codec->jacktbl.used; i++, jack++)
  93. if (jack->tag == tag && jack->dev_id == dev_id)
  94. return jack;
  95. return NULL;
  96. }
  97. EXPORT_SYMBOL_GPL(snd_hda_jack_tbl_get_from_tag);
  98. static struct hda_jack_tbl *
  99. any_jack_tbl_get_from_nid(struct hda_codec *codec, hda_nid_t nid)
  100. {
  101. struct hda_jack_tbl *jack = codec->jacktbl.list;
  102. int i;
  103. if (!nid || !jack)
  104. return NULL;
  105. for (i = 0; i < codec->jacktbl.used; i++, jack++)
  106. if (jack->nid == nid)
  107. return jack;
  108. return NULL;
  109. }
  110. /**
  111. * snd_hda_jack_tbl_new - create a jack-table entry for the given NID
  112. * @codec: the HDA codec
  113. * @nid: pin NID to assign
  114. * @dev_id: pin device entry id
  115. */
  116. static struct hda_jack_tbl *
  117. snd_hda_jack_tbl_new(struct hda_codec *codec, hda_nid_t nid, int dev_id)
  118. {
  119. struct hda_jack_tbl *jack =
  120. snd_hda_jack_tbl_get_mst(codec, nid, dev_id);
  121. struct hda_jack_tbl *existing_nid_jack =
  122. any_jack_tbl_get_from_nid(codec, nid);
  123. WARN_ON(dev_id != 0 && !codec->dp_mst);
  124. if (jack)
  125. return jack;
  126. jack = snd_array_new(&codec->jacktbl);
  127. if (!jack)
  128. return NULL;
  129. jack->nid = nid;
  130. jack->dev_id = dev_id;
  131. jack->jack_dirty = 1;
  132. if (existing_nid_jack) {
  133. jack->tag = existing_nid_jack->tag;
  134. /*
  135. * Copy jack_detect from existing_nid_jack to avoid
  136. * snd_hda_jack_detect_enable_callback_mst() making multiple
  137. * SET_UNSOLICITED_ENABLE calls on the same pin.
  138. */
  139. jack->jack_detect = existing_nid_jack->jack_detect;
  140. } else {
  141. jack->tag = codec->jacktbl.used;
  142. }
  143. return jack;
  144. }
  145. void snd_hda_jack_tbl_disconnect(struct hda_codec *codec)
  146. {
  147. struct hda_jack_tbl *jack = codec->jacktbl.list;
  148. int i;
  149. for (i = 0; i < codec->jacktbl.used; i++, jack++) {
  150. if (!codec->bus->shutdown && jack->jack)
  151. snd_device_disconnect(codec->card, jack->jack);
  152. }
  153. }
  154. void snd_hda_jack_tbl_clear(struct hda_codec *codec)
  155. {
  156. struct hda_jack_tbl *jack = codec->jacktbl.list;
  157. int i;
  158. for (i = 0; i < codec->jacktbl.used; i++, jack++) {
  159. struct hda_jack_callback *cb, *next;
  160. /* free jack instances manually when clearing/reconfiguring */
  161. if (!codec->bus->shutdown && jack->jack)
  162. snd_device_free(codec->card, jack->jack);
  163. for (cb = jack->callback; cb; cb = next) {
  164. next = cb->next;
  165. kfree(cb);
  166. }
  167. }
  168. snd_array_free(&codec->jacktbl);
  169. }
  170. #define get_jack_plug_state(sense) !!(sense & AC_PINSENSE_PRESENCE)
  171. /* update the cached value and notification flag if needed */
  172. static void jack_detect_update(struct hda_codec *codec,
  173. struct hda_jack_tbl *jack)
  174. {
  175. if (!jack->jack_dirty)
  176. return;
  177. if (jack->phantom_jack)
  178. jack->pin_sense = AC_PINSENSE_PRESENCE;
  179. else
  180. jack->pin_sense = read_pin_sense(codec, jack->nid,
  181. jack->dev_id);
  182. /* A gating jack indicates the jack is invalid if gating is unplugged */
  183. if (jack->gating_jack &&
  184. !snd_hda_jack_detect_mst(codec, jack->gating_jack, jack->dev_id))
  185. jack->pin_sense &= ~AC_PINSENSE_PRESENCE;
  186. jack->jack_dirty = 0;
  187. /* If a jack is gated by this one update it. */
  188. if (jack->gated_jack) {
  189. struct hda_jack_tbl *gated =
  190. snd_hda_jack_tbl_get_mst(codec, jack->gated_jack,
  191. jack->dev_id);
  192. if (gated) {
  193. gated->jack_dirty = 1;
  194. jack_detect_update(codec, gated);
  195. }
  196. }
  197. }
  198. /**
  199. * snd_hda_jack_set_dirty_all - Mark all the cached as dirty
  200. * @codec: the HDA codec
  201. *
  202. * This function sets the dirty flag to all entries of jack table.
  203. * It's called from the resume path in hda_codec.c.
  204. */
  205. void snd_hda_jack_set_dirty_all(struct hda_codec *codec)
  206. {
  207. struct hda_jack_tbl *jack = codec->jacktbl.list;
  208. int i;
  209. for (i = 0; i < codec->jacktbl.used; i++, jack++)
  210. if (jack->nid)
  211. jack->jack_dirty = 1;
  212. }
  213. EXPORT_SYMBOL_GPL(snd_hda_jack_set_dirty_all);
  214. /**
  215. * snd_hda_jack_pin_sense - execute pin sense measurement
  216. * @codec: the CODEC to sense
  217. * @nid: the pin NID to sense
  218. * @dev_id: pin device entry id
  219. *
  220. * Execute necessary pin sense measurement and return its Presence Detect,
  221. * Impedance, ELD Valid etc. status bits.
  222. */
  223. u32 snd_hda_jack_pin_sense(struct hda_codec *codec, hda_nid_t nid, int dev_id)
  224. {
  225. struct hda_jack_tbl *jack =
  226. snd_hda_jack_tbl_get_mst(codec, nid, dev_id);
  227. if (jack) {
  228. jack_detect_update(codec, jack);
  229. return jack->pin_sense;
  230. }
  231. return read_pin_sense(codec, nid, dev_id);
  232. }
  233. EXPORT_SYMBOL_GPL(snd_hda_jack_pin_sense);
  234. /**
  235. * snd_hda_jack_detect_state_mst - query pin Presence Detect status
  236. * @codec: the CODEC to sense
  237. * @nid: the pin NID to sense
  238. * @dev_id: pin device entry id
  239. *
  240. * Query and return the pin's Presence Detect status, as either
  241. * HDA_JACK_NOT_PRESENT, HDA_JACK_PRESENT or HDA_JACK_PHANTOM.
  242. */
  243. int snd_hda_jack_detect_state_mst(struct hda_codec *codec,
  244. hda_nid_t nid, int dev_id)
  245. {
  246. struct hda_jack_tbl *jack =
  247. snd_hda_jack_tbl_get_mst(codec, nid, dev_id);
  248. if (jack && jack->phantom_jack)
  249. return HDA_JACK_PHANTOM;
  250. else if (snd_hda_jack_pin_sense(codec, nid, dev_id) &
  251. AC_PINSENSE_PRESENCE)
  252. return HDA_JACK_PRESENT;
  253. else
  254. return HDA_JACK_NOT_PRESENT;
  255. }
  256. EXPORT_SYMBOL_GPL(snd_hda_jack_detect_state_mst);
  257. static struct hda_jack_callback *
  258. find_callback_from_list(struct hda_jack_tbl *jack,
  259. hda_jack_callback_fn func)
  260. {
  261. struct hda_jack_callback *cb;
  262. if (!func)
  263. return NULL;
  264. for (cb = jack->callback; cb; cb = cb->next) {
  265. if (cb->func == func)
  266. return cb;
  267. }
  268. return NULL;
  269. }
  270. /**
  271. * snd_hda_jack_detect_enable_callback_mst - enable the jack-detection
  272. * @codec: the HDA codec
  273. * @nid: pin NID to enable
  274. * @func: callback function to register
  275. * @dev_id: pin device entry id
  276. *
  277. * In the case of error, the return value will be a pointer embedded with
  278. * errno. Check and handle the return value appropriately with standard
  279. * macros such as @IS_ERR() and @PTR_ERR().
  280. */
  281. struct hda_jack_callback *
  282. snd_hda_jack_detect_enable_callback_mst(struct hda_codec *codec, hda_nid_t nid,
  283. int dev_id, hda_jack_callback_fn func)
  284. {
  285. struct hda_jack_tbl *jack;
  286. struct hda_jack_callback *callback = NULL;
  287. int err;
  288. jack = snd_hda_jack_tbl_new(codec, nid, dev_id);
  289. if (!jack)
  290. return ERR_PTR(-ENOMEM);
  291. callback = find_callback_from_list(jack, func);
  292. if (func && !callback) {
  293. callback = kzalloc(sizeof(*callback), GFP_KERNEL);
  294. if (!callback)
  295. return ERR_PTR(-ENOMEM);
  296. callback->func = func;
  297. callback->nid = jack->nid;
  298. callback->dev_id = jack->dev_id;
  299. callback->next = jack->callback;
  300. jack->callback = callback;
  301. }
  302. if (jack->jack_detect)
  303. return callback; /* already registered */
  304. jack->jack_detect = 1;
  305. if (codec->jackpoll_interval > 0)
  306. return callback; /* No unsol if we're polling instead */
  307. err = snd_hda_codec_write_cache(codec, nid, 0,
  308. AC_VERB_SET_UNSOLICITED_ENABLE,
  309. AC_USRSP_EN | jack->tag);
  310. if (err < 0)
  311. return ERR_PTR(err);
  312. return callback;
  313. }
  314. EXPORT_SYMBOL_GPL(snd_hda_jack_detect_enable_callback_mst);
  315. /**
  316. * snd_hda_jack_detect_enable - Enable the jack detection on the given pin
  317. * @codec: the HDA codec
  318. * @nid: pin NID to enable jack detection
  319. * @dev_id: pin device entry id
  320. *
  321. * Enable the jack detection with the default callback. Returns zero if
  322. * successful or a negative error code.
  323. */
  324. int snd_hda_jack_detect_enable(struct hda_codec *codec, hda_nid_t nid,
  325. int dev_id)
  326. {
  327. return PTR_ERR_OR_ZERO(snd_hda_jack_detect_enable_callback_mst(codec,
  328. nid,
  329. dev_id,
  330. NULL));
  331. }
  332. EXPORT_SYMBOL_GPL(snd_hda_jack_detect_enable);
  333. /**
  334. * snd_hda_jack_set_gating_jack - Set gating jack.
  335. * @codec: the HDA codec
  336. * @gated_nid: gated pin NID
  337. * @gating_nid: gating pin NID
  338. *
  339. * Indicates the gated jack is only valid when the gating jack is plugged.
  340. */
  341. int snd_hda_jack_set_gating_jack(struct hda_codec *codec, hda_nid_t gated_nid,
  342. hda_nid_t gating_nid)
  343. {
  344. struct hda_jack_tbl *gated = snd_hda_jack_tbl_new(codec, gated_nid, 0);
  345. struct hda_jack_tbl *gating =
  346. snd_hda_jack_tbl_new(codec, gating_nid, 0);
  347. WARN_ON(codec->dp_mst);
  348. if (!gated || !gating)
  349. return -EINVAL;
  350. gated->gating_jack = gating_nid;
  351. gating->gated_jack = gated_nid;
  352. return 0;
  353. }
  354. EXPORT_SYMBOL_GPL(snd_hda_jack_set_gating_jack);
  355. /**
  356. * snd_hda_jack_bind_keymap - bind keys generated from one NID to another jack.
  357. * @codec: the HDA codec
  358. * @key_nid: key event is generated by this pin NID
  359. * @keymap: map of key type and key code
  360. * @jack_nid: key reports to the jack of this pin NID
  361. *
  362. * This function is used in the case of key is generated from one NID while is
  363. * reported to the jack of another NID.
  364. */
  365. int snd_hda_jack_bind_keymap(struct hda_codec *codec, hda_nid_t key_nid,
  366. const struct hda_jack_keymap *keymap,
  367. hda_nid_t jack_nid)
  368. {
  369. const struct hda_jack_keymap *map;
  370. struct hda_jack_tbl *key_gen = snd_hda_jack_tbl_get(codec, key_nid);
  371. struct hda_jack_tbl *report_to = snd_hda_jack_tbl_get(codec, jack_nid);
  372. WARN_ON(codec->dp_mst);
  373. if (!key_gen || !report_to || !report_to->jack)
  374. return -EINVAL;
  375. key_gen->key_report_jack = jack_nid;
  376. if (keymap)
  377. for (map = keymap; map->type; map++)
  378. snd_jack_set_key(report_to->jack, map->type, map->key);
  379. return 0;
  380. }
  381. EXPORT_SYMBOL_GPL(snd_hda_jack_bind_keymap);
  382. /**
  383. * snd_hda_jack_set_button_state - report button event to the hda_jack_tbl button_state.
  384. * @codec: the HDA codec
  385. * @jack_nid: the button event reports to the jack_tbl of this NID
  386. * @button_state: the button event captured by codec
  387. *
  388. * Codec driver calls this function to report the button event.
  389. */
  390. void snd_hda_jack_set_button_state(struct hda_codec *codec, hda_nid_t jack_nid,
  391. int button_state)
  392. {
  393. struct hda_jack_tbl *jack = snd_hda_jack_tbl_get(codec, jack_nid);
  394. if (!jack)
  395. return;
  396. if (jack->key_report_jack) {
  397. struct hda_jack_tbl *report_to =
  398. snd_hda_jack_tbl_get(codec, jack->key_report_jack);
  399. if (report_to) {
  400. report_to->button_state = button_state;
  401. return;
  402. }
  403. }
  404. jack->button_state = button_state;
  405. }
  406. EXPORT_SYMBOL_GPL(snd_hda_jack_set_button_state);
  407. /**
  408. * snd_hda_jack_report_sync - sync the states of all jacks and report if changed
  409. * @codec: the HDA codec
  410. */
  411. void snd_hda_jack_report_sync(struct hda_codec *codec)
  412. {
  413. struct hda_jack_tbl *jack;
  414. int i, state;
  415. /* update all jacks at first */
  416. jack = codec->jacktbl.list;
  417. for (i = 0; i < codec->jacktbl.used; i++, jack++)
  418. if (jack->nid)
  419. jack_detect_update(codec, jack);
  420. /* report the updated jacks; it's done after updating all jacks
  421. * to make sure that all gating jacks properly have been set
  422. */
  423. jack = codec->jacktbl.list;
  424. for (i = 0; i < codec->jacktbl.used; i++, jack++)
  425. if (jack->nid) {
  426. if (!jack->jack || jack->block_report)
  427. continue;
  428. state = jack->button_state;
  429. if (get_jack_plug_state(jack->pin_sense))
  430. state |= jack->type;
  431. snd_jack_report(jack->jack, state);
  432. if (jack->button_state) {
  433. snd_jack_report(jack->jack,
  434. state & ~jack->button_state);
  435. jack->button_state = 0; /* button released */
  436. }
  437. }
  438. }
  439. EXPORT_SYMBOL_GPL(snd_hda_jack_report_sync);
  440. /* guess the jack type from the pin-config */
  441. static int get_input_jack_type(struct hda_codec *codec, hda_nid_t nid)
  442. {
  443. unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
  444. switch (get_defcfg_device(def_conf)) {
  445. case AC_JACK_LINE_OUT:
  446. case AC_JACK_SPEAKER:
  447. return SND_JACK_LINEOUT;
  448. case AC_JACK_HP_OUT:
  449. return SND_JACK_HEADPHONE;
  450. case AC_JACK_SPDIF_OUT:
  451. case AC_JACK_DIG_OTHER_OUT:
  452. return SND_JACK_AVOUT;
  453. case AC_JACK_MIC_IN:
  454. return SND_JACK_MICROPHONE;
  455. default:
  456. return SND_JACK_LINEIN;
  457. }
  458. }
  459. static void hda_free_jack_priv(struct snd_jack *jack)
  460. {
  461. struct hda_jack_tbl *jacks = jack->private_data;
  462. jacks->nid = 0;
  463. jacks->jack = NULL;
  464. }
  465. /**
  466. * snd_hda_jack_add_kctl_mst - Add a kctl for the given pin
  467. * @codec: the HDA codec
  468. * @nid: pin NID to assign
  469. * @dev_id : pin device entry id
  470. * @name: string name for the jack
  471. * @phantom_jack: flag to deal as a phantom jack
  472. * @type: jack type bits to be reported, 0 for guessing from pincfg
  473. * @keymap: optional jack / key mapping
  474. *
  475. * This assigns a jack-detection kctl to the given pin. The kcontrol
  476. * will have the given name and index.
  477. */
  478. int snd_hda_jack_add_kctl_mst(struct hda_codec *codec, hda_nid_t nid,
  479. int dev_id, const char *name, bool phantom_jack,
  480. int type, const struct hda_jack_keymap *keymap)
  481. {
  482. struct hda_jack_tbl *jack;
  483. const struct hda_jack_keymap *map;
  484. int err, state, buttons;
  485. jack = snd_hda_jack_tbl_new(codec, nid, dev_id);
  486. if (!jack)
  487. return 0;
  488. if (jack->jack)
  489. return 0; /* already created */
  490. if (!type)
  491. type = get_input_jack_type(codec, nid);
  492. buttons = 0;
  493. if (keymap) {
  494. for (map = keymap; map->type; map++)
  495. buttons |= map->type;
  496. }
  497. err = snd_jack_new(codec->card, name, type | buttons,
  498. &jack->jack, true, phantom_jack);
  499. if (err < 0)
  500. return err;
  501. jack->phantom_jack = !!phantom_jack;
  502. jack->type = type;
  503. jack->button_state = 0;
  504. jack->jack->private_data = jack;
  505. jack->jack->private_free = hda_free_jack_priv;
  506. if (keymap) {
  507. for (map = keymap; map->type; map++)
  508. snd_jack_set_key(jack->jack, map->type, map->key);
  509. }
  510. state = snd_hda_jack_detect_mst(codec, nid, dev_id);
  511. snd_jack_report(jack->jack, state ? jack->type : 0);
  512. return 0;
  513. }
  514. EXPORT_SYMBOL_GPL(snd_hda_jack_add_kctl_mst);
  515. static int add_jack_kctl(struct hda_codec *codec, hda_nid_t nid,
  516. const struct auto_pin_cfg *cfg,
  517. const char *base_name)
  518. {
  519. unsigned int def_conf, conn;
  520. char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
  521. int err;
  522. bool phantom_jack;
  523. WARN_ON(codec->dp_mst);
  524. if (!nid)
  525. return 0;
  526. def_conf = snd_hda_codec_get_pincfg(codec, nid);
  527. conn = get_defcfg_connect(def_conf);
  528. if (conn == AC_JACK_PORT_NONE)
  529. return 0;
  530. phantom_jack = (conn != AC_JACK_PORT_COMPLEX) ||
  531. !is_jack_detectable(codec, nid);
  532. if (base_name)
  533. strscpy(name, base_name, sizeof(name));
  534. else
  535. snd_hda_get_pin_label(codec, nid, cfg, name, sizeof(name), NULL);
  536. if (phantom_jack)
  537. /* Example final name: "Internal Mic Phantom Jack" */
  538. strncat(name, " Phantom", sizeof(name) - strlen(name) - 1);
  539. err = snd_hda_jack_add_kctl(codec, nid, name, phantom_jack, 0, NULL);
  540. if (err < 0)
  541. return err;
  542. if (!phantom_jack)
  543. return snd_hda_jack_detect_enable(codec, nid, 0);
  544. return 0;
  545. }
  546. /**
  547. * snd_hda_jack_add_kctls - Add kctls for all pins included in the given pincfg
  548. * @codec: the HDA codec
  549. * @cfg: pin config table to parse
  550. */
  551. int snd_hda_jack_add_kctls(struct hda_codec *codec,
  552. const struct auto_pin_cfg *cfg)
  553. {
  554. const hda_nid_t *p;
  555. int i, err;
  556. for (i = 0; i < cfg->num_inputs; i++) {
  557. /* If we have headphone mics; make sure they get the right name
  558. before grabbed by output pins */
  559. if (cfg->inputs[i].is_headphone_mic) {
  560. if (auto_cfg_hp_outs(cfg) == 1)
  561. err = add_jack_kctl(codec, auto_cfg_hp_pins(cfg)[0],
  562. cfg, "Headphone Mic");
  563. else
  564. err = add_jack_kctl(codec, cfg->inputs[i].pin,
  565. cfg, "Headphone Mic");
  566. } else
  567. err = add_jack_kctl(codec, cfg->inputs[i].pin, cfg,
  568. NULL);
  569. if (err < 0)
  570. return err;
  571. }
  572. for (i = 0, p = cfg->line_out_pins; i < cfg->line_outs; i++, p++) {
  573. err = add_jack_kctl(codec, *p, cfg, NULL);
  574. if (err < 0)
  575. return err;
  576. }
  577. for (i = 0, p = cfg->hp_pins; i < cfg->hp_outs; i++, p++) {
  578. if (*p == *cfg->line_out_pins) /* might be duplicated */
  579. break;
  580. err = add_jack_kctl(codec, *p, cfg, NULL);
  581. if (err < 0)
  582. return err;
  583. }
  584. for (i = 0, p = cfg->speaker_pins; i < cfg->speaker_outs; i++, p++) {
  585. if (*p == *cfg->line_out_pins) /* might be duplicated */
  586. break;
  587. err = add_jack_kctl(codec, *p, cfg, NULL);
  588. if (err < 0)
  589. return err;
  590. }
  591. for (i = 0, p = cfg->dig_out_pins; i < cfg->dig_outs; i++, p++) {
  592. err = add_jack_kctl(codec, *p, cfg, NULL);
  593. if (err < 0)
  594. return err;
  595. }
  596. err = add_jack_kctl(codec, cfg->dig_in_pin, cfg, NULL);
  597. if (err < 0)
  598. return err;
  599. err = add_jack_kctl(codec, cfg->mono_out_pin, cfg, NULL);
  600. if (err < 0)
  601. return err;
  602. return 0;
  603. }
  604. EXPORT_SYMBOL_GPL(snd_hda_jack_add_kctls);
  605. static void call_jack_callback(struct hda_codec *codec, unsigned int res,
  606. struct hda_jack_tbl *jack)
  607. {
  608. struct hda_jack_callback *cb;
  609. for (cb = jack->callback; cb; cb = cb->next) {
  610. cb->jack = jack;
  611. cb->unsol_res = res;
  612. cb->func(codec, cb);
  613. }
  614. if (jack->gated_jack) {
  615. struct hda_jack_tbl *gated =
  616. snd_hda_jack_tbl_get_mst(codec, jack->gated_jack,
  617. jack->dev_id);
  618. if (gated) {
  619. for (cb = gated->callback; cb; cb = cb->next) {
  620. cb->jack = gated;
  621. cb->unsol_res = res;
  622. cb->func(codec, cb);
  623. }
  624. }
  625. }
  626. }
  627. /**
  628. * snd_hda_jack_unsol_event - Handle an unsolicited event
  629. * @codec: the HDA codec
  630. * @res: the unsolicited event data
  631. */
  632. void snd_hda_jack_unsol_event(struct hda_codec *codec, unsigned int res)
  633. {
  634. struct hda_jack_tbl *event;
  635. int tag = (res & AC_UNSOL_RES_TAG) >> AC_UNSOL_RES_TAG_SHIFT;
  636. if (codec->dp_mst) {
  637. int dev_entry =
  638. (res & AC_UNSOL_RES_DE) >> AC_UNSOL_RES_DE_SHIFT;
  639. event = snd_hda_jack_tbl_get_from_tag(codec, tag, dev_entry);
  640. } else {
  641. event = snd_hda_jack_tbl_get_from_tag(codec, tag, 0);
  642. }
  643. if (!event)
  644. return;
  645. if (event->key_report_jack) {
  646. struct hda_jack_tbl *report_to =
  647. snd_hda_jack_tbl_get_mst(codec, event->key_report_jack,
  648. event->dev_id);
  649. if (report_to)
  650. report_to->jack_dirty = 1;
  651. } else
  652. event->jack_dirty = 1;
  653. call_jack_callback(codec, res, event);
  654. snd_hda_jack_report_sync(codec);
  655. }
  656. EXPORT_SYMBOL_GPL(snd_hda_jack_unsol_event);
  657. /**
  658. * snd_hda_jack_poll_all - Poll all jacks
  659. * @codec: the HDA codec
  660. *
  661. * Poll all detectable jacks with dirty flag, update the status, call
  662. * callbacks and call snd_hda_jack_report_sync() if any changes are found.
  663. */
  664. void snd_hda_jack_poll_all(struct hda_codec *codec)
  665. {
  666. struct hda_jack_tbl *jack = codec->jacktbl.list;
  667. int i, changes = 0;
  668. for (i = 0; i < codec->jacktbl.used; i++, jack++) {
  669. unsigned int old_sense;
  670. if (!jack->nid || !jack->jack_dirty || jack->phantom_jack)
  671. continue;
  672. old_sense = get_jack_plug_state(jack->pin_sense);
  673. jack_detect_update(codec, jack);
  674. if (old_sense == get_jack_plug_state(jack->pin_sense))
  675. continue;
  676. changes = 1;
  677. call_jack_callback(codec, 0, jack);
  678. }
  679. if (changes)
  680. snd_hda_jack_report_sync(codec);
  681. }
  682. EXPORT_SYMBOL_GPL(snd_hda_jack_poll_all);