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