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