1
2
3
4
5
6
7
8
9
10
11
12#include <linux/init.h>
13#include <linux/slab.h>
14#include <linux/export.h>
15#include <sound/core.h>
16#include <sound/control.h>
17#include <sound/jack.h>
18#include "hda_codec.h"
19#include "hda_local.h"
20#include "hda_auto_parser.h"
21#include "hda_jack.h"
22
23
24
25
26
27
28
29
30
31
32
33bool is_jack_detectable(struct hda_codec *codec, hda_nid_t nid)
34{
35 if (codec->no_jack_detect)
36 return false;
37 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_PRES_DETECT))
38 return false;
39 if (get_defcfg_misc(snd_hda_codec_get_pincfg(codec, nid)) &
40 AC_DEFCFG_MISC_NO_PRESENCE)
41 return false;
42 if (!(get_wcaps(codec, nid) & AC_WCAP_UNSOL_CAP) &&
43 !codec->jackpoll_interval)
44 return false;
45 return true;
46}
47EXPORT_SYMBOL_GPL(is_jack_detectable);
48
49
50static u32 read_pin_sense(struct hda_codec *codec, hda_nid_t nid)
51{
52 u32 pincap;
53 u32 val;
54
55 if (!codec->no_trigger_sense) {
56 pincap = snd_hda_query_pin_caps(codec, nid);
57 if (pincap & AC_PINCAP_TRIG_REQ)
58 snd_hda_codec_read(codec, nid, 0,
59 AC_VERB_SET_PIN_SENSE, 0);
60 }
61 val = snd_hda_codec_read(codec, nid, 0,
62 AC_VERB_GET_PIN_SENSE, 0);
63 if (codec->inv_jack_detect)
64 val ^= AC_PINSENSE_PRESENCE;
65 return val;
66}
67
68
69
70
71
72
73struct hda_jack_tbl *
74snd_hda_jack_tbl_get(struct hda_codec *codec, hda_nid_t nid)
75{
76 struct hda_jack_tbl *jack = codec->jacktbl.list;
77 int i;
78
79 if (!nid || !jack)
80 return NULL;
81 for (i = 0; i < codec->jacktbl.used; i++, jack++)
82 if (jack->nid == nid)
83 return jack;
84 return NULL;
85}
86EXPORT_SYMBOL_GPL(snd_hda_jack_tbl_get);
87
88
89
90
91
92
93struct hda_jack_tbl *
94snd_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}
106EXPORT_SYMBOL_GPL(snd_hda_jack_tbl_get_from_tag);
107
108
109
110
111
112
113static struct hda_jack_tbl *
114snd_hda_jack_tbl_new(struct hda_codec *codec, hda_nid_t nid)
115{
116 struct hda_jack_tbl *jack = snd_hda_jack_tbl_get(codec, nid);
117 if (jack)
118 return jack;
119 jack = snd_array_new(&codec->jacktbl);
120 if (!jack)
121 return NULL;
122 jack->nid = nid;
123 jack->jack_dirty = 1;
124 jack->tag = codec->jacktbl.used;
125 return jack;
126}
127
128void snd_hda_jack_tbl_clear(struct hda_codec *codec)
129{
130 struct hda_jack_tbl *jack = codec->jacktbl.list;
131 int i;
132
133 for (i = 0; i < codec->jacktbl.used; i++, jack++) {
134 struct hda_jack_callback *cb, *next;
135
136
137 if (!codec->bus->shutdown && jack->jack)
138 snd_device_free(codec->card, jack->jack);
139
140 for (cb = jack->callback; cb; cb = next) {
141 next = cb->next;
142 kfree(cb);
143 }
144 }
145 snd_array_free(&codec->jacktbl);
146}
147
148#define get_jack_plug_state(sense) !!(sense & AC_PINSENSE_PRESENCE)
149
150
151static void jack_detect_update(struct hda_codec *codec,
152 struct hda_jack_tbl *jack)
153{
154 if (!jack->jack_dirty)
155 return;
156
157 if (jack->phantom_jack)
158 jack->pin_sense = AC_PINSENSE_PRESENCE;
159 else
160 jack->pin_sense = read_pin_sense(codec, jack->nid);
161
162
163 if (jack->gating_jack && !snd_hda_jack_detect(codec, jack->gating_jack))
164 jack->pin_sense &= ~AC_PINSENSE_PRESENCE;
165
166 jack->jack_dirty = 0;
167
168
169 if (jack->gated_jack) {
170 struct hda_jack_tbl *gated =
171 snd_hda_jack_tbl_get(codec, jack->gated_jack);
172 if (gated) {
173 gated->jack_dirty = 1;
174 jack_detect_update(codec, gated);
175 }
176 }
177}
178
179
180
181
182
183
184
185
186void snd_hda_jack_set_dirty_all(struct hda_codec *codec)
187{
188 struct hda_jack_tbl *jack = codec->jacktbl.list;
189 int i;
190
191 for (i = 0; i < codec->jacktbl.used; i++, jack++)
192 if (jack->nid)
193 jack->jack_dirty = 1;
194}
195EXPORT_SYMBOL_GPL(snd_hda_jack_set_dirty_all);
196
197
198
199
200
201
202
203
204
205u32 snd_hda_pin_sense(struct hda_codec *codec, hda_nid_t nid)
206{
207 struct hda_jack_tbl *jack = snd_hda_jack_tbl_get(codec, nid);
208 if (jack) {
209 jack_detect_update(codec, jack);
210 return jack->pin_sense;
211 }
212 return read_pin_sense(codec, nid);
213}
214EXPORT_SYMBOL_GPL(snd_hda_pin_sense);
215
216
217
218
219
220
221
222
223
224int snd_hda_jack_detect_state(struct hda_codec *codec, hda_nid_t nid)
225{
226 struct hda_jack_tbl *jack = snd_hda_jack_tbl_get(codec, nid);
227 if (jack && jack->phantom_jack)
228 return HDA_JACK_PHANTOM;
229 else if (snd_hda_pin_sense(codec, nid) & AC_PINSENSE_PRESENCE)
230 return HDA_JACK_PRESENT;
231 else
232 return HDA_JACK_NOT_PRESENT;
233}
234EXPORT_SYMBOL_GPL(snd_hda_jack_detect_state);
235
236
237
238
239
240
241
242
243
244
245
246struct hda_jack_callback *
247snd_hda_jack_detect_enable_callback(struct hda_codec *codec, hda_nid_t nid,
248 hda_jack_callback_fn func)
249{
250 struct hda_jack_tbl *jack;
251 struct hda_jack_callback *callback = NULL;
252 int err;
253
254 jack = snd_hda_jack_tbl_new(codec, nid);
255 if (!jack)
256 return ERR_PTR(-ENOMEM);
257 if (func) {
258 callback = kzalloc(sizeof(*callback), GFP_KERNEL);
259 if (!callback)
260 return ERR_PTR(-ENOMEM);
261 callback->func = func;
262 callback->nid = jack->nid;
263 callback->next = jack->callback;
264 jack->callback = callback;
265 }
266
267 if (jack->jack_detect)
268 return callback;
269 jack->jack_detect = 1;
270 if (codec->jackpoll_interval > 0)
271 return callback;
272 err = snd_hda_codec_write_cache(codec, nid, 0,
273 AC_VERB_SET_UNSOLICITED_ENABLE,
274 AC_USRSP_EN | jack->tag);
275 if (err < 0)
276 return ERR_PTR(err);
277 return callback;
278}
279EXPORT_SYMBOL_GPL(snd_hda_jack_detect_enable_callback);
280
281
282
283
284
285
286
287
288
289int snd_hda_jack_detect_enable(struct hda_codec *codec, hda_nid_t nid)
290{
291 return PTR_ERR_OR_ZERO(snd_hda_jack_detect_enable_callback(codec, nid, NULL));
292}
293EXPORT_SYMBOL_GPL(snd_hda_jack_detect_enable);
294
295
296
297
298
299
300
301
302
303int snd_hda_jack_set_gating_jack(struct hda_codec *codec, hda_nid_t gated_nid,
304 hda_nid_t gating_nid)
305{
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);
308
309 if (!gated || !gating)
310 return -EINVAL;
311
312 gated->gating_jack = gating_nid;
313 gating->gated_jack = gated_nid;
314
315 return 0;
316}
317EXPORT_SYMBOL_GPL(snd_hda_jack_set_gating_jack);
318
319
320
321
322
323void snd_hda_jack_report_sync(struct hda_codec *codec)
324{
325 struct hda_jack_tbl *jack;
326 int i, state;
327
328
329 jack = codec->jacktbl.list;
330 for (i = 0; i < codec->jacktbl.used; i++, jack++)
331 if (jack->nid)
332 jack_detect_update(codec, jack);
333
334
335
336
337 jack = codec->jacktbl.list;
338 for (i = 0; i < codec->jacktbl.used; i++, jack++)
339 if (jack->nid) {
340 if (!jack->jack || jack->block_report)
341 continue;
342 state = get_jack_plug_state(jack->pin_sense);
343 snd_jack_report(jack->jack,
344 state ? jack->type : 0);
345 }
346}
347EXPORT_SYMBOL_GPL(snd_hda_jack_report_sync);
348
349
350static int get_input_jack_type(struct hda_codec *codec, hda_nid_t nid)
351{
352 unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
353 switch (get_defcfg_device(def_conf)) {
354 case AC_JACK_LINE_OUT:
355 case AC_JACK_SPEAKER:
356 return SND_JACK_LINEOUT;
357 case AC_JACK_HP_OUT:
358 return SND_JACK_HEADPHONE;
359 case AC_JACK_SPDIF_OUT:
360 case AC_JACK_DIG_OTHER_OUT:
361 return SND_JACK_AVOUT;
362 case AC_JACK_MIC_IN:
363 return SND_JACK_MICROPHONE;
364 default:
365 return SND_JACK_LINEIN;
366 }
367}
368
369static void hda_free_jack_priv(struct snd_jack *jack)
370{
371 struct hda_jack_tbl *jacks = jack->private_data;
372 jacks->nid = 0;
373 jacks->jack = NULL;
374}
375
376
377
378
379
380
381
382
383
384
385
386int snd_hda_jack_add_kctl(struct hda_codec *codec, hda_nid_t nid,
387 const char *name, bool phantom_jack)
388{
389 struct hda_jack_tbl *jack;
390 int err, state, type;
391
392 jack = snd_hda_jack_tbl_new(codec, nid);
393 if (!jack)
394 return 0;
395 if (jack->jack)
396 return 0;
397
398 type = get_input_jack_type(codec, nid);
399 err = snd_jack_new(codec->card, name, type,
400 &jack->jack, true, phantom_jack);
401 if (err < 0)
402 return err;
403
404 jack->phantom_jack = !!phantom_jack;
405 jack->type = type;
406 jack->jack->private_data = jack;
407 jack->jack->private_free = hda_free_jack_priv;
408 state = snd_hda_jack_detect(codec, nid);
409 snd_jack_report(jack->jack, state ? jack->type : 0);
410
411 return 0;
412}
413EXPORT_SYMBOL_GPL(snd_hda_jack_add_kctl);
414
415static int add_jack_kctl(struct hda_codec *codec, hda_nid_t nid,
416 const struct auto_pin_cfg *cfg,
417 const char *base_name)
418{
419 unsigned int def_conf, conn;
420 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
421 int err;
422 bool phantom_jack;
423
424 if (!nid)
425 return 0;
426 def_conf = snd_hda_codec_get_pincfg(codec, nid);
427 conn = get_defcfg_connect(def_conf);
428 if (conn == AC_JACK_PORT_NONE)
429 return 0;
430 phantom_jack = (conn != AC_JACK_PORT_COMPLEX) ||
431 !is_jack_detectable(codec, nid);
432
433 if (base_name)
434 strlcpy(name, base_name, sizeof(name));
435 else
436 snd_hda_get_pin_label(codec, nid, cfg, name, sizeof(name), NULL);
437 if (phantom_jack)
438
439 strncat(name, " Phantom", sizeof(name) - strlen(name) - 1);
440 err = snd_hda_jack_add_kctl(codec, nid, name, phantom_jack);
441 if (err < 0)
442 return err;
443
444 if (!phantom_jack)
445 return snd_hda_jack_detect_enable(codec, nid);
446 return 0;
447}
448
449
450
451
452
453
454int snd_hda_jack_add_kctls(struct hda_codec *codec,
455 const struct auto_pin_cfg *cfg)
456{
457 const hda_nid_t *p;
458 int i, err;
459
460 for (i = 0; i < cfg->num_inputs; i++) {
461
462
463 if (cfg->inputs[i].is_headphone_mic) {
464 if (auto_cfg_hp_outs(cfg) == 1)
465 err = add_jack_kctl(codec, auto_cfg_hp_pins(cfg)[0],
466 cfg, "Headphone Mic");
467 else
468 err = add_jack_kctl(codec, cfg->inputs[i].pin,
469 cfg, "Headphone Mic");
470 } else
471 err = add_jack_kctl(codec, cfg->inputs[i].pin, cfg,
472 NULL);
473 if (err < 0)
474 return err;
475 }
476
477 for (i = 0, p = cfg->line_out_pins; i < cfg->line_outs; i++, p++) {
478 err = add_jack_kctl(codec, *p, cfg, NULL);
479 if (err < 0)
480 return err;
481 }
482 for (i = 0, p = cfg->hp_pins; i < cfg->hp_outs; i++, p++) {
483 if (*p == *cfg->line_out_pins)
484 break;
485 err = add_jack_kctl(codec, *p, cfg, NULL);
486 if (err < 0)
487 return err;
488 }
489 for (i = 0, p = cfg->speaker_pins; i < cfg->speaker_outs; i++, p++) {
490 if (*p == *cfg->line_out_pins)
491 break;
492 err = add_jack_kctl(codec, *p, cfg, NULL);
493 if (err < 0)
494 return err;
495 }
496 for (i = 0, p = cfg->dig_out_pins; i < cfg->dig_outs; i++, p++) {
497 err = add_jack_kctl(codec, *p, cfg, NULL);
498 if (err < 0)
499 return err;
500 }
501 err = add_jack_kctl(codec, cfg->dig_in_pin, cfg, NULL);
502 if (err < 0)
503 return err;
504 err = add_jack_kctl(codec, cfg->mono_out_pin, cfg, NULL);
505 if (err < 0)
506 return err;
507 return 0;
508}
509EXPORT_SYMBOL_GPL(snd_hda_jack_add_kctls);
510
511static void call_jack_callback(struct hda_codec *codec,
512 struct hda_jack_tbl *jack)
513{
514 struct hda_jack_callback *cb;
515
516 for (cb = jack->callback; cb; cb = cb->next)
517 cb->func(codec, cb);
518 if (jack->gated_jack) {
519 struct hda_jack_tbl *gated =
520 snd_hda_jack_tbl_get(codec, jack->gated_jack);
521 if (gated) {
522 for (cb = gated->callback; cb; cb = cb->next)
523 cb->func(codec, cb);
524 }
525 }
526}
527
528
529
530
531
532
533void snd_hda_jack_unsol_event(struct hda_codec *codec, unsigned int res)
534{
535 struct hda_jack_tbl *event;
536 int tag = (res >> AC_UNSOL_RES_TAG_SHIFT) & 0x7f;
537
538 event = snd_hda_jack_tbl_get_from_tag(codec, tag);
539 if (!event)
540 return;
541 event->jack_dirty = 1;
542
543 call_jack_callback(codec, event);
544 snd_hda_jack_report_sync(codec);
545}
546EXPORT_SYMBOL_GPL(snd_hda_jack_unsol_event);
547
548
549
550
551
552
553
554
555void snd_hda_jack_poll_all(struct hda_codec *codec)
556{
557 struct hda_jack_tbl *jack = codec->jacktbl.list;
558 int i, changes = 0;
559
560 for (i = 0; i < codec->jacktbl.used; i++, jack++) {
561 unsigned int old_sense;
562 if (!jack->nid || !jack->jack_dirty || jack->phantom_jack)
563 continue;
564 old_sense = get_jack_plug_state(jack->pin_sense);
565 jack_detect_update(codec, jack);
566 if (old_sense == get_jack_plug_state(jack->pin_sense))
567 continue;
568 changes = 1;
569 call_jack_callback(codec, jack);
570 }
571 if (changes)
572 snd_hda_jack_report_sync(codec);
573}
574EXPORT_SYMBOL_GPL(snd_hda_jack_poll_all);
575
576