1
2
3
4
5
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
21
22
23
24
25
26
27
28
29bool 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}
43EXPORT_SYMBOL_GPL(is_jack_detectable);
44
45
46static 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)
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
66
67
68
69
70struct hda_jack_tbl *
71snd_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}
83EXPORT_SYMBOL_GPL(snd_hda_jack_tbl_get_mst);
84
85
86
87
88
89
90
91struct hda_jack_tbl *
92snd_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}
105EXPORT_SYMBOL_GPL(snd_hda_jack_tbl_get_from_tag);
106
107static struct hda_jack_tbl *
108any_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
123
124
125
126
127static struct hda_jack_tbl *
128snd_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
150
151
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
161void 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
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
184static 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
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
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
217
218
219
220
221
222void 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}
231EXPORT_SYMBOL_GPL(snd_hda_jack_set_dirty_all);
232
233
234
235
236
237
238
239
240
241
242u32 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}
252EXPORT_SYMBOL_GPL(snd_hda_jack_pin_sense);
253
254
255
256
257
258
259
260
261
262
263int 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}
276EXPORT_SYMBOL_GPL(snd_hda_jack_detect_state_mst);
277
278static struct hda_jack_callback *
279find_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}
294
295
296
297
298
299
300
301
302
303
304
305
306struct hda_jack_callback *
307snd_hda_jack_detect_enable_callback_mst(struct hda_codec *codec, hda_nid_t nid,
308 int dev_id, hda_jack_callback_fn func)
309{
310 struct hda_jack_tbl *jack;
311 struct hda_jack_callback *callback = NULL;
312 int err;
313
314 jack = snd_hda_jack_tbl_new(codec, nid, dev_id);
315 if (!jack)
316 return ERR_PTR(-ENOMEM);
317
318 callback = find_callback_from_list(jack, func);
319
320 if (func && !callback) {
321 callback = kzalloc(sizeof(*callback), GFP_KERNEL);
322 if (!callback)
323 return ERR_PTR(-ENOMEM);
324 callback->func = func;
325 callback->nid = jack->nid;
326 callback->dev_id = jack->dev_id;
327 callback->next = jack->callback;
328 jack->callback = callback;
329 }
330
331 if (jack->jack_detect)
332 return callback;
333 jack->jack_detect = 1;
334 if (codec->jackpoll_interval > 0)
335 return callback;
336 err = snd_hda_codec_write_cache(codec, nid, 0,
337 AC_VERB_SET_UNSOLICITED_ENABLE,
338 AC_USRSP_EN | jack->tag);
339 if (err < 0)
340 return ERR_PTR(err);
341 return callback;
342}
343EXPORT_SYMBOL_GPL(snd_hda_jack_detect_enable_callback_mst);
344
345
346
347
348
349
350
351
352
353
354int snd_hda_jack_detect_enable(struct hda_codec *codec, hda_nid_t nid,
355 int dev_id)
356{
357 return PTR_ERR_OR_ZERO(snd_hda_jack_detect_enable_callback_mst(codec,
358 nid,
359 dev_id,
360 NULL));
361}
362EXPORT_SYMBOL_GPL(snd_hda_jack_detect_enable);
363
364
365
366
367
368
369
370
371
372int snd_hda_jack_set_gating_jack(struct hda_codec *codec, hda_nid_t gated_nid,
373 hda_nid_t gating_nid)
374{
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);
380
381 if (!gated || !gating)
382 return -EINVAL;
383
384 gated->gating_jack = gating_nid;
385 gating->gated_jack = gated_nid;
386
387 return 0;
388}
389EXPORT_SYMBOL_GPL(snd_hda_jack_set_gating_jack);
390
391
392
393
394
395
396
397
398
399
400
401int snd_hda_jack_bind_keymap(struct hda_codec *codec, hda_nid_t key_nid,
402 const struct hda_jack_keymap *keymap,
403 hda_nid_t jack_nid)
404{
405 const struct hda_jack_keymap *map;
406 struct hda_jack_tbl *key_gen = snd_hda_jack_tbl_get(codec, key_nid);
407 struct hda_jack_tbl *report_to = snd_hda_jack_tbl_get(codec, jack_nid);
408
409 WARN_ON(codec->dp_mst);
410
411 if (!key_gen || !report_to || !report_to->jack)
412 return -EINVAL;
413
414 key_gen->key_report_jack = jack_nid;
415
416 if (keymap)
417 for (map = keymap; map->type; map++)
418 snd_jack_set_key(report_to->jack, map->type, map->key);
419
420 return 0;
421}
422EXPORT_SYMBOL_GPL(snd_hda_jack_bind_keymap);
423
424
425
426
427
428
429
430
431
432void snd_hda_jack_set_button_state(struct hda_codec *codec, hda_nid_t jack_nid,
433 int button_state)
434{
435 struct hda_jack_tbl *jack = snd_hda_jack_tbl_get(codec, jack_nid);
436
437 if (!jack)
438 return;
439
440 if (jack->key_report_jack) {
441 struct hda_jack_tbl *report_to =
442 snd_hda_jack_tbl_get(codec, jack->key_report_jack);
443
444 if (report_to) {
445 report_to->button_state = button_state;
446 return;
447 }
448 }
449
450 jack->button_state = button_state;
451}
452EXPORT_SYMBOL_GPL(snd_hda_jack_set_button_state);
453
454
455
456
457
458void snd_hda_jack_report_sync(struct hda_codec *codec)
459{
460 struct hda_jack_tbl *jack;
461 int i, state;
462
463
464 jack = codec->jacktbl.list;
465 for (i = 0; i < codec->jacktbl.used; i++, jack++)
466 if (jack->nid)
467 jack_detect_update(codec, jack);
468
469
470
471
472 jack = codec->jacktbl.list;
473 for (i = 0; i < codec->jacktbl.used; i++, jack++)
474 if (jack->nid) {
475 if (!jack->jack || jack->block_report)
476 continue;
477 state = jack->button_state;
478 if (get_jack_plug_state(jack->pin_sense))
479 state |= jack->type;
480 snd_jack_report(jack->jack, state);
481 if (jack->button_state) {
482 snd_jack_report(jack->jack,
483 state & ~jack->button_state);
484 jack->button_state = 0;
485 }
486 }
487}
488EXPORT_SYMBOL_GPL(snd_hda_jack_report_sync);
489
490
491static int get_input_jack_type(struct hda_codec *codec, hda_nid_t nid)
492{
493 unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
494 switch (get_defcfg_device(def_conf)) {
495 case AC_JACK_LINE_OUT:
496 case AC_JACK_SPEAKER:
497 return SND_JACK_LINEOUT;
498 case AC_JACK_HP_OUT:
499 return SND_JACK_HEADPHONE;
500 case AC_JACK_SPDIF_OUT:
501 case AC_JACK_DIG_OTHER_OUT:
502 return SND_JACK_AVOUT;
503 case AC_JACK_MIC_IN:
504 return SND_JACK_MICROPHONE;
505 default:
506 return SND_JACK_LINEIN;
507 }
508}
509
510static void hda_free_jack_priv(struct snd_jack *jack)
511{
512 struct hda_jack_tbl *jacks = jack->private_data;
513 jacks->nid = 0;
514 jacks->jack = NULL;
515}
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530int snd_hda_jack_add_kctl_mst(struct hda_codec *codec, hda_nid_t nid,
531 int dev_id, const char *name, bool phantom_jack,
532 int type, const struct hda_jack_keymap *keymap)
533{
534 struct hda_jack_tbl *jack;
535 const struct hda_jack_keymap *map;
536 int err, state, buttons;
537
538 jack = snd_hda_jack_tbl_new(codec, nid, dev_id);
539 if (!jack)
540 return 0;
541 if (jack->jack)
542 return 0;
543
544 if (!type)
545 type = get_input_jack_type(codec, nid);
546
547 buttons = 0;
548 if (keymap) {
549 for (map = keymap; map->type; map++)
550 buttons |= map->type;
551 }
552
553 err = snd_jack_new(codec->card, name, type | buttons,
554 &jack->jack, true, phantom_jack);
555 if (err < 0)
556 return err;
557
558 jack->phantom_jack = !!phantom_jack;
559 jack->type = type;
560 jack->button_state = 0;
561 jack->jack->private_data = jack;
562 jack->jack->private_free = hda_free_jack_priv;
563 if (keymap) {
564 for (map = keymap; map->type; map++)
565 snd_jack_set_key(jack->jack, map->type, map->key);
566 }
567
568 state = snd_hda_jack_detect_mst(codec, nid, dev_id);
569 snd_jack_report(jack->jack, state ? jack->type : 0);
570
571 return 0;
572}
573EXPORT_SYMBOL_GPL(snd_hda_jack_add_kctl_mst);
574
575static int add_jack_kctl(struct hda_codec *codec, hda_nid_t nid,
576 const struct auto_pin_cfg *cfg,
577 const char *base_name)
578{
579 unsigned int def_conf, conn;
580 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
581 int err;
582 bool phantom_jack;
583
584 WARN_ON(codec->dp_mst);
585
586 if (!nid)
587 return 0;
588 def_conf = snd_hda_codec_get_pincfg(codec, nid);
589 conn = get_defcfg_connect(def_conf);
590 if (conn == AC_JACK_PORT_NONE)
591 return 0;
592 phantom_jack = (conn != AC_JACK_PORT_COMPLEX) ||
593 !is_jack_detectable(codec, nid);
594
595 if (base_name)
596 strscpy(name, base_name, sizeof(name));
597 else
598 snd_hda_get_pin_label(codec, nid, cfg, name, sizeof(name), NULL);
599 if (phantom_jack)
600
601 strncat(name, " Phantom", sizeof(name) - strlen(name) - 1);
602 err = snd_hda_jack_add_kctl(codec, nid, name, phantom_jack, 0, NULL);
603 if (err < 0)
604 return err;
605
606 if (!phantom_jack)
607 return snd_hda_jack_detect_enable(codec, nid, 0);
608 return 0;
609}
610
611
612
613
614
615
616int snd_hda_jack_add_kctls(struct hda_codec *codec,
617 const struct auto_pin_cfg *cfg)
618{
619 const hda_nid_t *p;
620 int i, err;
621
622 for (i = 0; i < cfg->num_inputs; i++) {
623
624
625 if (cfg->inputs[i].is_headphone_mic) {
626 if (auto_cfg_hp_outs(cfg) == 1)
627 err = add_jack_kctl(codec, auto_cfg_hp_pins(cfg)[0],
628 cfg, "Headphone Mic");
629 else
630 err = add_jack_kctl(codec, cfg->inputs[i].pin,
631 cfg, "Headphone Mic");
632 } else
633 err = add_jack_kctl(codec, cfg->inputs[i].pin, cfg,
634 NULL);
635 if (err < 0)
636 return err;
637 }
638
639 for (i = 0, p = cfg->line_out_pins; i < cfg->line_outs; i++, p++) {
640 err = add_jack_kctl(codec, *p, cfg, NULL);
641 if (err < 0)
642 return err;
643 }
644 for (i = 0, p = cfg->hp_pins; i < cfg->hp_outs; i++, p++) {
645 if (*p == *cfg->line_out_pins)
646 break;
647 err = add_jack_kctl(codec, *p, cfg, NULL);
648 if (err < 0)
649 return err;
650 }
651 for (i = 0, p = cfg->speaker_pins; i < cfg->speaker_outs; i++, p++) {
652 if (*p == *cfg->line_out_pins)
653 break;
654 err = add_jack_kctl(codec, *p, cfg, NULL);
655 if (err < 0)
656 return err;
657 }
658 for (i = 0, p = cfg->dig_out_pins; i < cfg->dig_outs; i++, p++) {
659 err = add_jack_kctl(codec, *p, cfg, NULL);
660 if (err < 0)
661 return err;
662 }
663 err = add_jack_kctl(codec, cfg->dig_in_pin, cfg, NULL);
664 if (err < 0)
665 return err;
666 err = add_jack_kctl(codec, cfg->mono_out_pin, cfg, NULL);
667 if (err < 0)
668 return err;
669 return 0;
670}
671EXPORT_SYMBOL_GPL(snd_hda_jack_add_kctls);
672
673static void call_jack_callback(struct hda_codec *codec, unsigned int res,
674 struct hda_jack_tbl *jack)
675{
676 struct hda_jack_callback *cb;
677
678 for (cb = jack->callback; cb; cb = cb->next) {
679 cb->jack = jack;
680 cb->unsol_res = res;
681 cb->func(codec, cb);
682 }
683 if (jack->gated_jack) {
684 struct hda_jack_tbl *gated =
685 snd_hda_jack_tbl_get_mst(codec, jack->gated_jack,
686 jack->dev_id);
687 if (gated) {
688 for (cb = gated->callback; cb; cb = cb->next) {
689 cb->jack = gated;
690 cb->unsol_res = res;
691 cb->func(codec, cb);
692 }
693 }
694 }
695}
696
697
698
699
700
701
702void snd_hda_jack_unsol_event(struct hda_codec *codec, unsigned int res)
703{
704 struct hda_jack_tbl *event;
705 int tag = (res & AC_UNSOL_RES_TAG) >> AC_UNSOL_RES_TAG_SHIFT;
706
707 if (codec->dp_mst) {
708 int dev_entry =
709 (res & AC_UNSOL_RES_DE) >> AC_UNSOL_RES_DE_SHIFT;
710
711 event = snd_hda_jack_tbl_get_from_tag(codec, tag, dev_entry);
712 } else {
713 event = snd_hda_jack_tbl_get_from_tag(codec, tag, 0);
714 }
715 if (!event)
716 return;
717
718 if (event->key_report_jack) {
719 struct hda_jack_tbl *report_to =
720 snd_hda_jack_tbl_get_mst(codec, event->key_report_jack,
721 event->dev_id);
722 if (report_to)
723 report_to->jack_dirty = 1;
724 } else
725 event->jack_dirty = 1;
726
727 call_jack_callback(codec, res, event);
728 snd_hda_jack_report_sync(codec);
729}
730EXPORT_SYMBOL_GPL(snd_hda_jack_unsol_event);
731
732
733
734
735
736
737
738
739void snd_hda_jack_poll_all(struct hda_codec *codec)
740{
741 struct hda_jack_tbl *jack = codec->jacktbl.list;
742 int i, changes = 0;
743
744 for (i = 0; i < codec->jacktbl.used; i++, jack++) {
745 unsigned int old_sense;
746 if (!jack->nid || !jack->jack_dirty || jack->phantom_jack)
747 continue;
748 old_sense = get_jack_plug_state(jack->pin_sense);
749 jack_detect_update(codec, jack);
750 if (old_sense == get_jack_plug_state(jack->pin_sense))
751 continue;
752 changes = 1;
753 call_jack_callback(codec, 0, jack);
754 }
755 if (changes)
756 snd_hda_jack_report_sync(codec);
757}
758EXPORT_SYMBOL_GPL(snd_hda_jack_poll_all);
759
760