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