hc
2023-12-06 08f87f769b595151be1afeff53e144f543faa614
kernel/sound/pci/hda/hda_jack.c
....@@ -1,12 +1,8 @@
1
+// SPDX-License-Identifier: GPL-2.0-or-later
12 /*
23 * Jack-detection handling for HD-audio
34 *
45 * Copyright (c) 2011 Takashi Iwai <tiwai@suse.de>
5
- *
6
- * This driver is free software; you can redistribute it and/or modify
7
- * it under the terms of the GNU General Public License as published by
8
- * the Free Software Foundation; either version 2 of the License, or
9
- * (at your option) any later version.
106 */
117
128 #include <linux/init.h>
....@@ -15,7 +11,7 @@
1511 #include <sound/core.h>
1612 #include <sound/control.h>
1713 #include <sound/jack.h>
18
-#include "hda_codec.h"
14
+#include <sound/hda_codec.h>
1915 #include "hda_local.h"
2016 #include "hda_auto_parser.h"
2117 #include "hda_jack.h"
....@@ -47,7 +43,7 @@
4743 EXPORT_SYMBOL_GPL(is_jack_detectable);
4844
4945 /* execute pin sense measurement */
50
-static u32 read_pin_sense(struct hda_codec *codec, hda_nid_t nid)
46
+static u32 read_pin_sense(struct hda_codec *codec, hda_nid_t nid, int dev_id)
5147 {
5248 u32 pincap;
5349 u32 val;
....@@ -59,19 +55,57 @@
5955 AC_VERB_SET_PIN_SENSE, 0);
6056 }
6157 val = snd_hda_codec_read(codec, nid, 0,
62
- AC_VERB_GET_PIN_SENSE, 0);
58
+ AC_VERB_GET_PIN_SENSE, dev_id);
6359 if (codec->inv_jack_detect)
6460 val ^= AC_PINSENSE_PRESENCE;
6561 return val;
6662 }
6763
6864 /**
69
- * snd_hda_jack_tbl_get - query the jack-table entry for the given NID
65
+ * snd_hda_jack_tbl_get_mst - query the jack-table entry for the given NID
7066 * @codec: the HDA codec
7167 * @nid: pin NID to refer to
68
+ * @dev_id: pin device entry id
7269 */
7370 struct hda_jack_tbl *
74
-snd_hda_jack_tbl_get(struct hda_codec *codec, hda_nid_t nid)
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)
75109 {
76110 struct hda_jack_tbl *jack = codec->jacktbl.list;
77111 int i;
....@@ -83,45 +117,44 @@
83117 return jack;
84118 return NULL;
85119 }
86
-EXPORT_SYMBOL_GPL(snd_hda_jack_tbl_get);
87
-
88
-/**
89
- * snd_hda_jack_tbl_get_from_tag - query the jack-table entry for the given tag
90
- * @codec: the HDA codec
91
- * @tag: tag value to refer to
92
- */
93
-struct hda_jack_tbl *
94
-snd_hda_jack_tbl_get_from_tag(struct hda_codec *codec, unsigned char tag)
95
-{
96
- struct hda_jack_tbl *jack = codec->jacktbl.list;
97
- int i;
98
-
99
- if (!tag || !jack)
100
- return NULL;
101
- for (i = 0; i < codec->jacktbl.used; i++, jack++)
102
- if (jack->tag == tag)
103
- return jack;
104
- return NULL;
105
-}
106
-EXPORT_SYMBOL_GPL(snd_hda_jack_tbl_get_from_tag);
107120
108121 /**
109122 * snd_hda_jack_tbl_new - create a jack-table entry for the given NID
110123 * @codec: the HDA codec
111124 * @nid: pin NID to assign
125
+ * @dev_id: pin device entry id
112126 */
113127 static struct hda_jack_tbl *
114
-snd_hda_jack_tbl_new(struct hda_codec *codec, hda_nid_t nid)
128
+snd_hda_jack_tbl_new(struct hda_codec *codec, hda_nid_t nid, int dev_id)
115129 {
116
- struct hda_jack_tbl *jack = snd_hda_jack_tbl_get(codec, nid);
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
+
117137 if (jack)
118138 return jack;
119139 jack = snd_array_new(&codec->jacktbl);
120140 if (!jack)
121141 return NULL;
122142 jack->nid = nid;
143
+ jack->dev_id = dev_id;
123144 jack->jack_dirty = 1;
124
- jack->tag = codec->jacktbl.used;
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
+
125158 return jack;
126159 }
127160
....@@ -157,10 +190,12 @@
157190 if (jack->phantom_jack)
158191 jack->pin_sense = AC_PINSENSE_PRESENCE;
159192 else
160
- jack->pin_sense = read_pin_sense(codec, jack->nid);
193
+ jack->pin_sense = read_pin_sense(codec, jack->nid,
194
+ jack->dev_id);
161195
162196 /* A gating jack indicates the jack is invalid if gating is unplugged */
163
- if (jack->gating_jack && !snd_hda_jack_detect(codec, jack->gating_jack))
197
+ if (jack->gating_jack &&
198
+ !snd_hda_jack_detect_mst(codec, jack->gating_jack, jack->dev_id))
164199 jack->pin_sense &= ~AC_PINSENSE_PRESENCE;
165200
166201 jack->jack_dirty = 0;
....@@ -168,7 +203,8 @@
168203 /* If a jack is gated by this one update it. */
169204 if (jack->gated_jack) {
170205 struct hda_jack_tbl *gated =
171
- snd_hda_jack_tbl_get(codec, jack->gated_jack);
206
+ snd_hda_jack_tbl_get_mst(codec, jack->gated_jack,
207
+ jack->dev_id);
172208 if (gated) {
173209 gated->jack_dirty = 1;
174210 jack_detect_update(codec, gated);
....@@ -195,71 +231,99 @@
195231 EXPORT_SYMBOL_GPL(snd_hda_jack_set_dirty_all);
196232
197233 /**
198
- * snd_hda_pin_sense - execute pin sense measurement
234
+ * snd_hda_jack_pin_sense - execute pin sense measurement
199235 * @codec: the CODEC to sense
200236 * @nid: the pin NID to sense
237
+ * @dev_id: pin device entry id
201238 *
202239 * Execute necessary pin sense measurement and return its Presence Detect,
203240 * Impedance, ELD Valid etc. status bits.
204241 */
205
-u32 snd_hda_pin_sense(struct hda_codec *codec, hda_nid_t nid)
242
+u32 snd_hda_jack_pin_sense(struct hda_codec *codec, hda_nid_t nid, int dev_id)
206243 {
207
- struct hda_jack_tbl *jack = snd_hda_jack_tbl_get(codec, nid);
244
+ struct hda_jack_tbl *jack =
245
+ snd_hda_jack_tbl_get_mst(codec, nid, dev_id);
208246 if (jack) {
209247 jack_detect_update(codec, jack);
210248 return jack->pin_sense;
211249 }
212
- return read_pin_sense(codec, nid);
250
+ return read_pin_sense(codec, nid, dev_id);
213251 }
214
-EXPORT_SYMBOL_GPL(snd_hda_pin_sense);
252
+EXPORT_SYMBOL_GPL(snd_hda_jack_pin_sense);
215253
216254 /**
217
- * snd_hda_jack_detect_state - query pin Presence Detect status
255
+ * snd_hda_jack_detect_state_mst - query pin Presence Detect status
218256 * @codec: the CODEC to sense
219257 * @nid: the pin NID to sense
258
+ * @dev_id: pin device entry id
220259 *
221260 * Query and return the pin's Presence Detect status, as either
222261 * HDA_JACK_NOT_PRESENT, HDA_JACK_PRESENT or HDA_JACK_PHANTOM.
223262 */
224
-int snd_hda_jack_detect_state(struct hda_codec *codec, hda_nid_t nid)
263
+int snd_hda_jack_detect_state_mst(struct hda_codec *codec,
264
+ hda_nid_t nid, int dev_id)
225265 {
226
- struct hda_jack_tbl *jack = snd_hda_jack_tbl_get(codec, nid);
266
+ struct hda_jack_tbl *jack =
267
+ snd_hda_jack_tbl_get_mst(codec, nid, dev_id);
227268 if (jack && jack->phantom_jack)
228269 return HDA_JACK_PHANTOM;
229
- else if (snd_hda_pin_sense(codec, nid) & AC_PINSENSE_PRESENCE)
270
+ else if (snd_hda_jack_pin_sense(codec, nid, dev_id) &
271
+ AC_PINSENSE_PRESENCE)
230272 return HDA_JACK_PRESENT;
231273 else
232274 return HDA_JACK_NOT_PRESENT;
233275 }
234
-EXPORT_SYMBOL_GPL(snd_hda_jack_detect_state);
276
+EXPORT_SYMBOL_GPL(snd_hda_jack_detect_state_mst);
277
+
278
+static struct hda_jack_callback *
279
+find_callback_from_list(struct hda_jack_tbl *jack,
280
+ hda_jack_callback_fn func)
281
+{
282
+ struct hda_jack_callback *cb;
283
+
284
+ if (!func)
285
+ return NULL;
286
+
287
+ for (cb = jack->callback; cb; cb = cb->next) {
288
+ if (cb->func == func)
289
+ return cb;
290
+ }
291
+
292
+ return NULL;
293
+}
235294
236295 /**
237
- * snd_hda_jack_detect_enable - enable the jack-detection
296
+ * snd_hda_jack_detect_enable_mst - enable the jack-detection
238297 * @codec: the HDA codec
239298 * @nid: pin NID to enable
240299 * @func: callback function to register
300
+ * @dev_id: pin device entry id
241301 *
242302 * In the case of error, the return value will be a pointer embedded with
243303 * errno. Check and handle the return value appropriately with standard
244304 * macros such as @IS_ERR() and @PTR_ERR().
245305 */
246306 struct hda_jack_callback *
247
-snd_hda_jack_detect_enable_callback(struct hda_codec *codec, hda_nid_t nid,
248
- hda_jack_callback_fn func)
307
+snd_hda_jack_detect_enable_callback_mst(struct hda_codec *codec, hda_nid_t nid,
308
+ int dev_id, hda_jack_callback_fn func)
249309 {
250310 struct hda_jack_tbl *jack;
251311 struct hda_jack_callback *callback = NULL;
252312 int err;
253313
254
- jack = snd_hda_jack_tbl_new(codec, nid);
314
+ jack = snd_hda_jack_tbl_new(codec, nid, dev_id);
255315 if (!jack)
256316 return ERR_PTR(-ENOMEM);
257
- if (func) {
317
+
318
+ callback = find_callback_from_list(jack, func);
319
+
320
+ if (func && !callback) {
258321 callback = kzalloc(sizeof(*callback), GFP_KERNEL);
259322 if (!callback)
260323 return ERR_PTR(-ENOMEM);
261324 callback->func = func;
262325 callback->nid = jack->nid;
326
+ callback->dev_id = jack->dev_id;
263327 callback->next = jack->callback;
264328 jack->callback = callback;
265329 }
....@@ -276,19 +340,24 @@
276340 return ERR_PTR(err);
277341 return callback;
278342 }
279
-EXPORT_SYMBOL_GPL(snd_hda_jack_detect_enable_callback);
343
+EXPORT_SYMBOL_GPL(snd_hda_jack_detect_enable_callback_mst);
280344
281345 /**
282346 * snd_hda_jack_detect_enable - Enable the jack detection on the given pin
283347 * @codec: the HDA codec
284348 * @nid: pin NID to enable jack detection
349
+ * @dev_id: pin device entry id
285350 *
286351 * Enable the jack detection with the default callback. Returns zero if
287352 * successful or a negative error code.
288353 */
289
-int snd_hda_jack_detect_enable(struct hda_codec *codec, hda_nid_t nid)
354
+int snd_hda_jack_detect_enable(struct hda_codec *codec, hda_nid_t nid,
355
+ int dev_id)
290356 {
291
- return PTR_ERR_OR_ZERO(snd_hda_jack_detect_enable_callback(codec, nid, NULL));
357
+ return PTR_ERR_OR_ZERO(snd_hda_jack_detect_enable_callback_mst(codec,
358
+ nid,
359
+ dev_id,
360
+ NULL));
292361 }
293362 EXPORT_SYMBOL_GPL(snd_hda_jack_detect_enable);
294363
....@@ -303,8 +372,11 @@
303372 int snd_hda_jack_set_gating_jack(struct hda_codec *codec, hda_nid_t gated_nid,
304373 hda_nid_t gating_nid)
305374 {
306
- struct hda_jack_tbl *gated = snd_hda_jack_tbl_new(codec, gated_nid);
307
- struct hda_jack_tbl *gating = snd_hda_jack_tbl_new(codec, gating_nid);
375
+ struct hda_jack_tbl *gated = snd_hda_jack_tbl_new(codec, gated_nid, 0);
376
+ struct hda_jack_tbl *gating =
377
+ snd_hda_jack_tbl_new(codec, gating_nid, 0);
378
+
379
+ WARN_ON(codec->dp_mst);
308380
309381 if (!gated || !gating)
310382 return -EINVAL;
....@@ -339,9 +411,15 @@
339411 if (jack->nid) {
340412 if (!jack->jack || jack->block_report)
341413 continue;
342
- state = get_jack_plug_state(jack->pin_sense);
343
- snd_jack_report(jack->jack,
344
- state ? jack->type : 0);
414
+ state = jack->button_state;
415
+ if (get_jack_plug_state(jack->pin_sense))
416
+ state |= jack->type;
417
+ snd_jack_report(jack->jack, state);
418
+ if (jack->button_state) {
419
+ snd_jack_report(jack->jack,
420
+ state & ~jack->button_state);
421
+ jack->button_state = 0; /* button released */
422
+ }
345423 }
346424 }
347425 EXPORT_SYMBOL_GPL(snd_hda_jack_report_sync);
....@@ -374,43 +452,62 @@
374452 }
375453
376454 /**
377
- * snd_hda_jack_add_kctl - Add a kctl for the given pin
455
+ * snd_hda_jack_add_kctl_mst - Add a kctl for the given pin
378456 * @codec: the HDA codec
379457 * @nid: pin NID to assign
458
+ * @dev_id : pin device entry id
380459 * @name: string name for the jack
381460 * @phantom_jack: flag to deal as a phantom jack
461
+ * @type: jack type bits to be reported, 0 for guessing from pincfg
462
+ * @keymap: optional jack / key mapping
382463 *
383464 * This assigns a jack-detection kctl to the given pin. The kcontrol
384465 * will have the given name and index.
385466 */
386
-int snd_hda_jack_add_kctl(struct hda_codec *codec, hda_nid_t nid,
387
- const char *name, bool phantom_jack)
467
+int snd_hda_jack_add_kctl_mst(struct hda_codec *codec, hda_nid_t nid,
468
+ int dev_id, const char *name, bool phantom_jack,
469
+ int type, const struct hda_jack_keymap *keymap)
388470 {
389471 struct hda_jack_tbl *jack;
390
- int err, state, type;
472
+ const struct hda_jack_keymap *map;
473
+ int err, state, buttons;
391474
392
- jack = snd_hda_jack_tbl_new(codec, nid);
475
+ jack = snd_hda_jack_tbl_new(codec, nid, dev_id);
393476 if (!jack)
394477 return 0;
395478 if (jack->jack)
396479 return 0; /* already created */
397480
398
- type = get_input_jack_type(codec, nid);
399
- err = snd_jack_new(codec->card, name, type,
481
+ if (!type)
482
+ type = get_input_jack_type(codec, nid);
483
+
484
+ buttons = 0;
485
+ if (keymap) {
486
+ for (map = keymap; map->type; map++)
487
+ buttons |= map->type;
488
+ }
489
+
490
+ err = snd_jack_new(codec->card, name, type | buttons,
400491 &jack->jack, true, phantom_jack);
401492 if (err < 0)
402493 return err;
403494
404495 jack->phantom_jack = !!phantom_jack;
405496 jack->type = type;
497
+ jack->button_state = 0;
406498 jack->jack->private_data = jack;
407499 jack->jack->private_free = hda_free_jack_priv;
408
- state = snd_hda_jack_detect(codec, nid);
500
+ if (keymap) {
501
+ for (map = keymap; map->type; map++)
502
+ snd_jack_set_key(jack->jack, map->type, map->key);
503
+ }
504
+
505
+ state = snd_hda_jack_detect_mst(codec, nid, dev_id);
409506 snd_jack_report(jack->jack, state ? jack->type : 0);
410507
411508 return 0;
412509 }
413
-EXPORT_SYMBOL_GPL(snd_hda_jack_add_kctl);
510
+EXPORT_SYMBOL_GPL(snd_hda_jack_add_kctl_mst);
414511
415512 static int add_jack_kctl(struct hda_codec *codec, hda_nid_t nid,
416513 const struct auto_pin_cfg *cfg,
....@@ -420,6 +517,8 @@
420517 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
421518 int err;
422519 bool phantom_jack;
520
+
521
+ WARN_ON(codec->dp_mst);
423522
424523 if (!nid)
425524 return 0;
....@@ -437,12 +536,12 @@
437536 if (phantom_jack)
438537 /* Example final name: "Internal Mic Phantom Jack" */
439538 strncat(name, " Phantom", sizeof(name) - strlen(name) - 1);
440
- err = snd_hda_jack_add_kctl(codec, nid, name, phantom_jack);
539
+ err = snd_hda_jack_add_kctl(codec, nid, name, phantom_jack, 0, NULL);
441540 if (err < 0)
442541 return err;
443542
444543 if (!phantom_jack)
445
- return snd_hda_jack_detect_enable(codec, nid);
544
+ return snd_hda_jack_detect_enable(codec, nid, 0);
446545 return 0;
447546 }
448547
....@@ -508,19 +607,26 @@
508607 }
509608 EXPORT_SYMBOL_GPL(snd_hda_jack_add_kctls);
510609
511
-static void call_jack_callback(struct hda_codec *codec,
610
+static void call_jack_callback(struct hda_codec *codec, unsigned int res,
512611 struct hda_jack_tbl *jack)
513612 {
514613 struct hda_jack_callback *cb;
515614
516
- for (cb = jack->callback; cb; cb = cb->next)
615
+ for (cb = jack->callback; cb; cb = cb->next) {
616
+ cb->jack = jack;
617
+ cb->unsol_res = res;
517618 cb->func(codec, cb);
619
+ }
518620 if (jack->gated_jack) {
519621 struct hda_jack_tbl *gated =
520
- snd_hda_jack_tbl_get(codec, jack->gated_jack);
622
+ snd_hda_jack_tbl_get_mst(codec, jack->gated_jack,
623
+ jack->dev_id);
521624 if (gated) {
522
- for (cb = gated->callback; cb; cb = cb->next)
625
+ for (cb = gated->callback; cb; cb = cb->next) {
626
+ cb->jack = gated;
627
+ cb->unsol_res = res;
523628 cb->func(codec, cb);
629
+ }
524630 }
525631 }
526632 }
....@@ -533,14 +639,21 @@
533639 void snd_hda_jack_unsol_event(struct hda_codec *codec, unsigned int res)
534640 {
535641 struct hda_jack_tbl *event;
536
- int tag = (res >> AC_UNSOL_RES_TAG_SHIFT) & 0x7f;
642
+ int tag = (res & AC_UNSOL_RES_TAG) >> AC_UNSOL_RES_TAG_SHIFT;
537643
538
- event = snd_hda_jack_tbl_get_from_tag(codec, tag);
644
+ if (codec->dp_mst) {
645
+ int dev_entry =
646
+ (res & AC_UNSOL_RES_DE) >> AC_UNSOL_RES_DE_SHIFT;
647
+
648
+ event = snd_hda_jack_tbl_get_from_tag(codec, tag, dev_entry);
649
+ } else {
650
+ event = snd_hda_jack_tbl_get_from_tag(codec, tag, 0);
651
+ }
539652 if (!event)
540653 return;
541654 event->jack_dirty = 1;
542655
543
- call_jack_callback(codec, event);
656
+ call_jack_callback(codec, res, event);
544657 snd_hda_jack_report_sync(codec);
545658 }
546659 EXPORT_SYMBOL_GPL(snd_hda_jack_unsol_event);
....@@ -566,7 +679,7 @@
566679 if (old_sense == get_jack_plug_state(jack->pin_sense))
567680 continue;
568681 changes = 1;
569
- call_jack_callback(codec, jack);
682
+ call_jack_callback(codec, 0, jack);
570683 }
571684 if (changes)
572685 snd_hda_jack_report_sync(codec);