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