hda_codec.c 139 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/*
 * Universal Interface for Intel High Definition Audio Codec
 *
 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
 *
 *
 *  This driver is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This driver is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 */

#include <linux/init.h>
#include <linux/delay.h>
#include <linux/slab.h>
#include <linux/pci.h>
26
#include <linux/mutex.h>
Linus Torvalds's avatar
Linus Torvalds committed
27
28
29
#include <sound/core.h>
#include "hda_codec.h"
#include <sound/asoundef.h>
30
#include <sound/tlv.h>
Linus Torvalds's avatar
Linus Torvalds committed
31
#include <sound/initval.h>
32
#include <sound/jack.h>
Linus Torvalds's avatar
Linus Torvalds committed
33
#include "hda_local.h"
34
#include "hda_beep.h"
35
#include <sound/hda_hwdep.h>
Linus Torvalds's avatar
Linus Torvalds committed
36
37
38
39
40
41
42
43
44
45
46
47

/*
 * vendor / preset table
 */

struct hda_vendor_id {
	unsigned int id;
	const char *name;
};

/* codec vendor labels */
static struct hda_vendor_id hda_vendor_ids[] = {
48
	{ 0x1002, "ATI" },
49
	{ 0x1013, "Cirrus Logic" },
50
	{ 0x1057, "Motorola" },
51
	{ 0x1095, "Silicon Image" },
52
	{ 0x10de, "Nvidia" },
53
	{ 0x10ec, "Realtek" },
54
	{ 0x1102, "Creative" },
55
	{ 0x1106, "VIA" },
56
	{ 0x111d, "IDT" },
57
	{ 0x11c1, "LSI" },
58
	{ 0x11d4, "Analog Devices" },
Linus Torvalds's avatar
Linus Torvalds committed
59
	{ 0x13f6, "C-Media" },
60
	{ 0x14f1, "Conexant" },
61
62
	{ 0x17e8, "Chrontel" },
	{ 0x1854, "LG" },
63
	{ 0x1aec, "Wolfson Microelectronics" },
Linus Torvalds's avatar
Linus Torvalds committed
64
	{ 0x434d, "C-Media" },
65
	{ 0x8086, "Intel" },
Matt's avatar
Matt committed
66
	{ 0x8384, "SigmaTel" },
Linus Torvalds's avatar
Linus Torvalds committed
67
68
69
	{} /* terminator */
};

70
71
72
73
74
75
76
77
78
79
static DEFINE_MUTEX(preset_mutex);
static LIST_HEAD(hda_preset_tables);

int snd_hda_add_codec_preset(struct hda_codec_preset_list *preset)
{
	mutex_lock(&preset_mutex);
	list_add_tail(&preset->list, &hda_preset_tables);
	mutex_unlock(&preset_mutex);
	return 0;
}
80
EXPORT_SYMBOL_HDA(snd_hda_add_codec_preset);
81
82
83
84
85
86
87
88

int snd_hda_delete_codec_preset(struct hda_codec_preset_list *preset)
{
	mutex_lock(&preset_mutex);
	list_del(&preset->list);
	mutex_unlock(&preset_mutex);
	return 0;
}
89
EXPORT_SYMBOL_HDA(snd_hda_delete_codec_preset);
Linus Torvalds's avatar
Linus Torvalds committed
90

91
92
93
94
95
96
97
#ifdef CONFIG_SND_HDA_POWER_SAVE
static void hda_power_work(struct work_struct *work);
static void hda_keep_power_on(struct hda_codec *codec);
#else
static inline void hda_keep_power_on(struct hda_codec *codec) {}
#endif

98
99
100
101
102
103
104
/**
 * snd_hda_get_jack_location - Give a location string of the jack
 * @cfg: pin default config value
 *
 * Parse the pin default config value and returns the string of the
 * jack location, e.g. "Rear", "Front", etc.
 */
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
const char *snd_hda_get_jack_location(u32 cfg)
{
	static char *bases[7] = {
		"N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
	};
	static unsigned char specials_idx[] = {
		0x07, 0x08,
		0x17, 0x18, 0x19,
		0x37, 0x38
	};
	static char *specials[] = {
		"Rear Panel", "Drive Bar",
		"Riser", "HDMI", "ATAPI",
		"Mobile-In", "Mobile-Out"
	};
	int i;
	cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT;
	if ((cfg & 0x0f) < 7)
		return bases[cfg & 0x0f];
	for (i = 0; i < ARRAY_SIZE(specials_idx); i++) {
		if (cfg == specials_idx[i])
			return specials[i];
	}
	return "UNKNOWN";
}
130
EXPORT_SYMBOL_HDA(snd_hda_get_jack_location);
131

132
133
134
135
136
137
138
/**
 * snd_hda_get_jack_connectivity - Give a connectivity string of the jack
 * @cfg: pin default config value
 *
 * Parse the pin default config value and returns the string of the
 * jack connectivity, i.e. external or internal connection.
 */
139
140
141
142
143
144
const char *snd_hda_get_jack_connectivity(u32 cfg)
{
	static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" };

	return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3];
}
145
EXPORT_SYMBOL_HDA(snd_hda_get_jack_connectivity);
146

147
148
149
150
151
152
153
/**
 * snd_hda_get_jack_type - Give a type string of the jack
 * @cfg: pin default config value
 *
 * Parse the pin default config value and returns the string of the
 * jack type, i.e. the purpose of the jack, such as Line-Out or CD.
 */
154
155
156
157
158
159
160
161
162
163
164
165
const char *snd_hda_get_jack_type(u32 cfg)
{
	static char *jack_types[16] = {
		"Line Out", "Speaker", "HP Out", "CD",
		"SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
		"Line In", "Aux", "Mic", "Telephony",
		"SPDIF In", "Digitial In", "Reserved", "Other"
	};

	return jack_types[(cfg & AC_DEFCFG_DEVICE)
				>> AC_DEFCFG_DEVICE_SHIFT];
}
166
EXPORT_SYMBOL_HDA(snd_hda_get_jack_type);
167

168
169
170
171
172
173
174
175
176
/*
 * Compose a 32bit command word to be sent to the HD-audio controller
 */
static inline unsigned int
make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int direct,
	       unsigned int verb, unsigned int parm)
{
	u32 val;

177
178
	if ((codec->addr & ~0xf) || (direct & ~1) || (nid & ~0x7f) ||
	    (verb & ~0xfff) || (parm & ~0xffff)) {
179
180
181
182
183
184
		printk(KERN_ERR "hda-codec: out of range cmd %x:%x:%x:%x:%x\n",
		       codec->addr, direct, nid, verb, parm);
		return ~0;
	}

	val = (u32)codec->addr << 28;
185
186
187
188
189
190
191
	val |= (u32)direct << 27;
	val |= (u32)nid << 20;
	val |= verb << 8;
	val |= parm;
	return val;
}

192
193
194
195
196
197
198
/*
 * Send and receive a verb
 */
static int codec_exec_verb(struct hda_codec *codec, unsigned int cmd,
			   unsigned int *res)
{
	struct hda_bus *bus = codec->bus;
199
	int err;
200

201
202
203
	if (cmd == ~0)
		return -1;

204
205
	if (res)
		*res = -1;
206
 again:
207
208
209
	snd_hda_power_up(codec);
	mutex_lock(&bus->cmd_mutex);
	err = bus->ops.command(bus, cmd);
210
	if (!err && res)
211
		*res = bus->ops.get_response(bus, codec->addr);
212
213
	mutex_unlock(&bus->cmd_mutex);
	snd_hda_power_down(codec);
214
215
216
217
218
219
220
221
222
223
224
	if (res && *res == -1 && bus->rirb_error) {
		if (bus->response_reset) {
			snd_printd("hda_codec: resetting BUS due to "
				   "fatal communication error\n");
			bus->ops.bus_reset(bus);
		}
		goto again;
	}
	/* clear reset-flag when the communication gets recovered */
	if (!err)
		bus->response_reset = 0;
225
226
227
	return err;
}

Linus Torvalds's avatar
Linus Torvalds committed
228
229
230
231
232
233
234
235
236
237
238
239
/**
 * snd_hda_codec_read - send a command and get the response
 * @codec: the HDA codec
 * @nid: NID to send the command
 * @direct: direct flag
 * @verb: the verb to send
 * @parm: the parameter for the verb
 *
 * Send a single command and read the corresponding response.
 *
 * Returns the obtained response value, or -1 for an error.
 */
240
241
unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
				int direct,
Linus Torvalds's avatar
Linus Torvalds committed
242
243
				unsigned int verb, unsigned int parm)
{
244
245
246
	unsigned cmd = make_codec_cmd(codec, nid, direct, verb, parm);
	unsigned int res;
	codec_exec_verb(codec, cmd, &res);
Linus Torvalds's avatar
Linus Torvalds committed
247
248
	return res;
}
249
EXPORT_SYMBOL_HDA(snd_hda_codec_read);
Linus Torvalds's avatar
Linus Torvalds committed
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265

/**
 * snd_hda_codec_write - send a single command without waiting for response
 * @codec: the HDA codec
 * @nid: NID to send the command
 * @direct: direct flag
 * @verb: the verb to send
 * @parm: the parameter for the verb
 *
 * Send a single command without waiting for response.
 *
 * Returns 0 if successful, or a negative error code.
 */
int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct,
			 unsigned int verb, unsigned int parm)
{
266
	unsigned int cmd = make_codec_cmd(codec, nid, direct, verb, parm);
267
	unsigned int res;
268
269
	return codec_exec_verb(codec, cmd,
			       codec->bus->sync_write ? &res : NULL);
Linus Torvalds's avatar
Linus Torvalds committed
270
}
271
EXPORT_SYMBOL_HDA(snd_hda_codec_write);
Linus Torvalds's avatar
Linus Torvalds committed
272
273
274
275
276
277
278
279
280
281
282
283
284
285

/**
 * snd_hda_sequence_write - sequence writes
 * @codec: the HDA codec
 * @seq: VERB array to send
 *
 * Send the commands sequentially from the given array.
 * The array must be terminated with NID=0.
 */
void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
{
	for (; seq->nid; seq++)
		snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
}
286
EXPORT_SYMBOL_HDA(snd_hda_sequence_write);
Linus Torvalds's avatar
Linus Torvalds committed
287
288
289
290
291
292
293
294
295
296

/**
 * snd_hda_get_sub_nodes - get the range of sub nodes
 * @codec: the HDA codec
 * @nid: NID to parse
 * @start_id: the pointer to store the start NID
 *
 * Parse the NID and store the start NID of its sub-nodes.
 * Returns the number of sub-nodes.
 */
297
298
int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
			  hda_nid_t *start_id)
Linus Torvalds's avatar
Linus Torvalds committed
299
300
301
302
{
	unsigned int parm;

	parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
303
304
	if (parm == -1)
		return 0;
Linus Torvalds's avatar
Linus Torvalds committed
305
306
307
	*start_id = (parm >> 16) & 0x7fff;
	return (int)(parm & 0x7fff);
}
308
EXPORT_SYMBOL_HDA(snd_hda_get_sub_nodes);
Linus Torvalds's avatar
Linus Torvalds committed
309

310
311
312
313
314
315
static int _hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
				hda_nid_t *conn_list, int max_conns);
static bool add_conn_list(struct snd_array *array, hda_nid_t nid);
static int copy_conn_list(hda_nid_t nid, hda_nid_t *dst, int max_dst,
			  hda_nid_t *src, int len);

Linus Torvalds's avatar
Linus Torvalds committed
316
317
318
319
320
321
322
323
324
325
326
327
328
/**
 * snd_hda_get_connections - get connection list
 * @codec: the HDA codec
 * @nid: NID to parse
 * @conn_list: connection list array
 * @max_conns: max. number of connections to store
 *
 * Parses the connection list of the given widget and stores the list
 * of NIDs.
 *
 * Returns the number of connections, or a negative error code.
 */
int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
329
330
331
			     hda_nid_t *conn_list, int max_conns)
{
	struct snd_array *array = &codec->conn_lists;
332
	int i, len, old_used;
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
	hda_nid_t list[HDA_MAX_CONNECTIONS];

	/* look up the cached results */
	for (i = 0; i < array->used; ) {
		hda_nid_t *p = snd_array_elem(array, i);
		len = p[1];
		if (nid == *p)
			return copy_conn_list(nid, conn_list, max_conns,
					      p + 2, len);
		i += len + 2;
	}

	len = _hda_get_connections(codec, nid, list, HDA_MAX_CONNECTIONS);
	if (len < 0)
		return len;

	/* add to the cache */
	old_used = array->used;
	if (!add_conn_list(array, nid) || !add_conn_list(array, len))
		goto error_add;
	for (i = 0; i < len; i++)
		if (!add_conn_list(array, list[i]))
			goto error_add;

	return copy_conn_list(nid, conn_list, max_conns, list, len);
		
 error_add:
	array->used = old_used;
	return -ENOMEM;
}
EXPORT_SYMBOL_HDA(snd_hda_get_connections);

static int _hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
			     hda_nid_t *conn_list, int max_conns)
Linus Torvalds's avatar
Linus Torvalds committed
367
368
{
	unsigned int parm;
369
	int i, conn_len, conns;
Linus Torvalds's avatar
Linus Torvalds committed
370
	unsigned int shift, num_elems, mask;
371
	unsigned int wcaps;
372
	hda_nid_t prev_nid;
Linus Torvalds's avatar
Linus Torvalds committed
373

374
375
	if (snd_BUG_ON(!conn_list || max_conns <= 0))
		return -EINVAL;
Linus Torvalds's avatar
Linus Torvalds committed
376

377
378
379
	wcaps = get_wcaps(codec, nid);
	if (!(wcaps & AC_WCAP_CONN_LIST) &&
	    get_wcaps_type(wcaps) != AC_WID_VOL_KNB) {
380
381
382
383
384
		snd_printk(KERN_WARNING "hda_codec: "
			   "connection list not available for 0x%x\n", nid);
		return -EINVAL;
	}

Linus Torvalds's avatar
Linus Torvalds committed
385
386
387
388
389
390
391
392
393
394
395
396
397
	parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
	if (parm & AC_CLIST_LONG) {
		/* long form */
		shift = 16;
		num_elems = 2;
	} else {
		/* short form */
		shift = 8;
		num_elems = 4;
	}
	conn_len = parm & AC_CLIST_LENGTH;
	mask = (1 << (shift-1)) - 1;

398
	if (!conn_len)
Linus Torvalds's avatar
Linus Torvalds committed
399
400
401
402
		return 0; /* no connection */

	if (conn_len == 1) {
		/* single connection */
403
404
		parm = snd_hda_codec_read(codec, nid, 0,
					  AC_VERB_GET_CONNECT_LIST, 0);
405
406
		if (parm == -1 && codec->bus->rirb_error)
			return -EIO;
Linus Torvalds's avatar
Linus Torvalds committed
407
408
409
410
411
412
		conn_list[0] = parm & mask;
		return 1;
	}

	/* multi connection */
	conns = 0;
413
414
415
416
417
	prev_nid = 0;
	for (i = 0; i < conn_len; i++) {
		int range_val;
		hda_nid_t val, n;

418
		if (i % num_elems == 0) {
419
420
			parm = snd_hda_codec_read(codec, nid, 0,
						  AC_VERB_GET_CONNECT_LIST, i);
421
422
423
			if (parm == -1 && codec->bus->rirb_error)
				return -EIO;
		}
424
		range_val = !!(parm & (1 << (shift-1))); /* ranges */
425
		val = parm & mask;
426
427
428
429
430
431
		if (val == 0) {
			snd_printk(KERN_WARNING "hda_codec: "
				   "invalid CONNECT_LIST verb %x[%i]:%x\n",
				    nid, i, parm);
			return 0;
		}
432
433
434
		parm >>= shift;
		if (range_val) {
			/* ranges between the previous and this one */
435
436
437
438
			if (!prev_nid || prev_nid >= val) {
				snd_printk(KERN_WARNING "hda_codec: "
					   "invalid dep_range_val %x:%x\n",
					   prev_nid, val);
439
440
441
442
				continue;
			}
			for (n = prev_nid + 1; n <= val; n++) {
				if (conns >= max_conns) {
443
444
445
					snd_printk(KERN_ERR "hda_codec: "
						   "Too many connections %d for NID 0x%x\n",
						   conns, nid);
Linus Torvalds's avatar
Linus Torvalds committed
446
					return -EINVAL;
447
448
				}
				conn_list[conns++] = n;
Linus Torvalds's avatar
Linus Torvalds committed
449
			}
450
451
		} else {
			if (conns >= max_conns) {
452
453
454
				snd_printk(KERN_ERR "hda_codec: "
					   "Too many connections %d for NID 0x%x\n",
					   conns, nid);
455
456
457
				return -EINVAL;
			}
			conn_list[conns++] = val;
Linus Torvalds's avatar
Linus Torvalds committed
458
		}
459
		prev_nid = val;
Linus Torvalds's avatar
Linus Torvalds committed
460
461
462
463
	}
	return conns;
}

464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
static bool add_conn_list(struct snd_array *array, hda_nid_t nid)
{
	hda_nid_t *p = snd_array_new(array);
	if (!p)
		return false;
	*p = nid;
	return true;
}

static int copy_conn_list(hda_nid_t nid, hda_nid_t *dst, int max_dst,
			  hda_nid_t *src, int len)
{
	if (len > max_dst) {
		snd_printk(KERN_ERR "hda_codec: "
			   "Too many connections %d for NID 0x%x\n",
			   len, nid);
		return -EINVAL;
	}
	memcpy(dst, src, len * sizeof(hda_nid_t));
	return len;
}
Linus Torvalds's avatar
Linus Torvalds committed
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502

/**
 * snd_hda_queue_unsol_event - add an unsolicited event to queue
 * @bus: the BUS
 * @res: unsolicited event (lower 32bit of RIRB entry)
 * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
 *
 * Adds the given event to the queue.  The events are processed in
 * the workqueue asynchronously.  Call this function in the interrupt
 * hanlder when RIRB receives an unsolicited event.
 *
 * Returns 0 if successful, or a negative error code.
 */
int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
{
	struct hda_bus_unsolicited *unsol;
	unsigned int wp;

503
504
	unsol = bus->unsol;
	if (!unsol)
Linus Torvalds's avatar
Linus Torvalds committed
505
506
507
508
509
510
511
512
513
		return 0;

	wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
	unsol->wp = wp;

	wp <<= 1;
	unsol->queue[wp] = res;
	unsol->queue[wp + 1] = res_ex;

Takashi Iwai's avatar
Takashi Iwai committed
514
	queue_work(bus->workq, &unsol->work);
Linus Torvalds's avatar
Linus Torvalds committed
515
516
517

	return 0;
}
518
EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event);
Linus Torvalds's avatar
Linus Torvalds committed
519
520

/*
Wu Fengguang's avatar
Wu Fengguang committed
521
 * process queued unsolicited events
Linus Torvalds's avatar
Linus Torvalds committed
522
 */
David Howells's avatar
David Howells committed
523
static void process_unsol_events(struct work_struct *work)
Linus Torvalds's avatar
Linus Torvalds committed
524
{
David Howells's avatar
David Howells committed
525
526
527
	struct hda_bus_unsolicited *unsol =
		container_of(work, struct hda_bus_unsolicited, work);
	struct hda_bus *bus = unsol->bus;
Linus Torvalds's avatar
Linus Torvalds committed
528
529
530
531
532
533
534
535
536
	struct hda_codec *codec;
	unsigned int rp, caddr, res;

	while (unsol->rp != unsol->wp) {
		rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
		unsol->rp = rp;
		rp <<= 1;
		res = unsol->queue[rp];
		caddr = unsol->queue[rp + 1];
537
		if (!(caddr & (1 << 4))) /* no unsolicited event? */
Linus Torvalds's avatar
Linus Torvalds committed
538
539
540
541
542
543
544
545
546
547
			continue;
		codec = bus->caddr_tbl[caddr & 0x0f];
		if (codec && codec->patch_ops.unsol_event)
			codec->patch_ops.unsol_event(codec, res);
	}
}

/*
 * initialize unsolicited queue
 */
548
static int init_unsol_queue(struct hda_bus *bus)
Linus Torvalds's avatar
Linus Torvalds committed
549
550
551
{
	struct hda_bus_unsolicited *unsol;

552
553
554
	if (bus->unsol) /* already initialized */
		return 0;

555
	unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
556
557
558
	if (!unsol) {
		snd_printk(KERN_ERR "hda_codec: "
			   "can't allocate unsolicited queue\n");
Linus Torvalds's avatar
Linus Torvalds committed
559
560
		return -ENOMEM;
	}
David Howells's avatar
David Howells committed
561
562
	INIT_WORK(&unsol->work, process_unsol_events);
	unsol->bus = bus;
Linus Torvalds's avatar
Linus Torvalds committed
563
564
565
566
567
568
569
570
571
572
573
	bus->unsol = unsol;
	return 0;
}

/*
 * destructor
 */
static void snd_hda_codec_free(struct hda_codec *codec);

static int snd_hda_bus_free(struct hda_bus *bus)
{
574
	struct hda_codec *codec, *n;
Linus Torvalds's avatar
Linus Torvalds committed
575

576
	if (!bus)
Linus Torvalds's avatar
Linus Torvalds committed
577
		return 0;
Takashi Iwai's avatar
Takashi Iwai committed
578
579
580
	if (bus->workq)
		flush_workqueue(bus->workq);
	if (bus->unsol)
Linus Torvalds's avatar
Linus Torvalds committed
581
		kfree(bus->unsol);
582
	list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
Linus Torvalds's avatar
Linus Torvalds committed
583
584
585
586
		snd_hda_codec_free(codec);
	}
	if (bus->ops.private_free)
		bus->ops.private_free(bus);
Takashi Iwai's avatar
Takashi Iwai committed
587
588
	if (bus->workq)
		destroy_workqueue(bus->workq);
Linus Torvalds's avatar
Linus Torvalds committed
589
590
591
592
	kfree(bus);
	return 0;
}

593
static int snd_hda_bus_dev_free(struct snd_device *device)
Linus Torvalds's avatar
Linus Torvalds committed
594
595
{
	struct hda_bus *bus = device->device_data;
596
	bus->shutdown = 1;
Linus Torvalds's avatar
Linus Torvalds committed
597
598
599
	return snd_hda_bus_free(bus);
}

600
601
602
603
604
605
606
#ifdef CONFIG_SND_HDA_HWDEP
static int snd_hda_bus_dev_register(struct snd_device *device)
{
	struct hda_bus *bus = device->device_data;
	struct hda_codec *codec;
	list_for_each_entry(codec, &bus->codec_list, list) {
		snd_hda_hwdep_add_sysfs(codec);
607
		snd_hda_hwdep_add_power_sysfs(codec);
608
609
610
611
612
613
614
	}
	return 0;
}
#else
#define snd_hda_bus_dev_register	NULL
#endif

Linus Torvalds's avatar
Linus Torvalds committed
615
616
617
618
619
620
621
622
/**
 * snd_hda_bus_new - create a HDA bus
 * @card: the card entry
 * @temp: the template for hda_bus information
 * @busp: the pointer to store the created bus instance
 *
 * Returns 0 if successful, or a negative error code.
 */
623
int /*__devinit*/ snd_hda_bus_new(struct snd_card *card,
624
625
			      const struct hda_bus_template *temp,
			      struct hda_bus **busp)
Linus Torvalds's avatar
Linus Torvalds committed
626
627
628
{
	struct hda_bus *bus;
	int err;
629
	static struct snd_device_ops dev_ops = {
630
		.dev_register = snd_hda_bus_dev_register,
Linus Torvalds's avatar
Linus Torvalds committed
631
632
633
		.dev_free = snd_hda_bus_dev_free,
	};

634
635
636
637
	if (snd_BUG_ON(!temp))
		return -EINVAL;
	if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
		return -EINVAL;
Linus Torvalds's avatar
Linus Torvalds committed
638
639
640
641

	if (busp)
		*busp = NULL;

642
	bus = kzalloc(sizeof(*bus), GFP_KERNEL);
Linus Torvalds's avatar
Linus Torvalds committed
643
644
645
646
647
648
649
650
651
	if (bus == NULL) {
		snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
		return -ENOMEM;
	}

	bus->card = card;
	bus->private_data = temp->private_data;
	bus->pci = temp->pci;
	bus->modelname = temp->modelname;
652
	bus->power_save = temp->power_save;
Linus Torvalds's avatar
Linus Torvalds committed
653
654
	bus->ops = temp->ops;

655
	mutex_init(&bus->cmd_mutex);
656
	mutex_init(&bus->prepare_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
657
658
	INIT_LIST_HEAD(&bus->codec_list);

659
660
661
	snprintf(bus->workq_name, sizeof(bus->workq_name),
		 "hd-audio%d", card->number);
	bus->workq = create_singlethread_workqueue(bus->workq_name);
Takashi Iwai's avatar
Takashi Iwai committed
662
	if (!bus->workq) {
663
664
		snd_printk(KERN_ERR "cannot create workqueue %s\n",
			   bus->workq_name);
Takashi Iwai's avatar
Takashi Iwai committed
665
666
667
668
		kfree(bus);
		return -ENOMEM;
	}

669
670
	err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
	if (err < 0) {
Linus Torvalds's avatar
Linus Torvalds committed
671
672
673
674
675
676
677
		snd_hda_bus_free(bus);
		return err;
	}
	if (busp)
		*busp = bus;
	return 0;
}
678
EXPORT_SYMBOL_HDA(snd_hda_bus_new);
Linus Torvalds's avatar
Linus Torvalds committed
679

680
681
#ifdef CONFIG_SND_HDA_GENERIC
#define is_generic_config(codec) \
682
	(codec->modelname && !strcmp(codec->modelname, "generic"))
683
684
685
686
#else
#define is_generic_config(codec)	0
#endif

687
#ifdef MODULE
688
689
#define HDA_MODREQ_MAX_COUNT	2	/* two request_modules()'s */
#else
690
#define HDA_MODREQ_MAX_COUNT	0	/* all presets are statically linked */
691
692
#endif

Linus Torvalds's avatar
Linus Torvalds committed
693
694
695
/*
 * find a matching codec preset
 */
696
static const struct hda_codec_preset *
697
find_codec_preset(struct hda_codec *codec)
Linus Torvalds's avatar
Linus Torvalds committed
698
{
699
700
701
	struct hda_codec_preset_list *tbl;
	const struct hda_codec_preset *preset;
	int mod_requested = 0;
Linus Torvalds's avatar
Linus Torvalds committed
702

703
	if (is_generic_config(codec))
704
705
		return NULL; /* use the generic parser */

706
707
708
709
710
711
712
713
 again:
	mutex_lock(&preset_mutex);
	list_for_each_entry(tbl, &hda_preset_tables, list) {
		if (!try_module_get(tbl->owner)) {
			snd_printk(KERN_ERR "hda_codec: cannot module_get\n");
			continue;
		}
		for (preset = tbl->preset; preset->id; preset++) {
Linus Torvalds's avatar
Linus Torvalds committed
714
			u32 mask = preset->mask;
715
716
717
718
			if (preset->afg && preset->afg != codec->afg)
				continue;
			if (preset->mfg && preset->mfg != codec->mfg)
				continue;
719
			if (!mask)
Linus Torvalds's avatar
Linus Torvalds committed
720
				mask = ~0;
721
			if (preset->id == (codec->vendor_id & mask) &&
722
			    (!preset->rev ||
723
724
725
			     preset->rev == codec->revision_id)) {
				mutex_unlock(&preset_mutex);
				codec->owner = tbl->owner;
Linus Torvalds's avatar
Linus Torvalds committed
726
				return preset;
727
			}
Linus Torvalds's avatar
Linus Torvalds committed
728
		}
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
		module_put(tbl->owner);
	}
	mutex_unlock(&preset_mutex);

	if (mod_requested < HDA_MODREQ_MAX_COUNT) {
		char name[32];
		if (!mod_requested)
			snprintf(name, sizeof(name), "snd-hda-codec-id:%08x",
				 codec->vendor_id);
		else
			snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*",
				 (codec->vendor_id >> 16) & 0xffff);
		request_module(name);
		mod_requested++;
		goto again;
Linus Torvalds's avatar
Linus Torvalds committed
744
745
746
747
748
	}
	return NULL;
}

/*
749
 * get_codec_name - store the codec name
Linus Torvalds's avatar
Linus Torvalds committed
750
 */
751
static int get_codec_name(struct hda_codec *codec)
Linus Torvalds's avatar
Linus Torvalds committed
752
753
754
755
{
	const struct hda_vendor_id *c;
	const char *vendor = NULL;
	u16 vendor_id = codec->vendor_id >> 16;
756
757
758
759
	char tmp[16];

	if (codec->vendor_name)
		goto get_chip_name;
Linus Torvalds's avatar
Linus Torvalds committed
760
761
762
763
764
765
766

	for (c = hda_vendor_ids; c->id; c++) {
		if (c->id == vendor_id) {
			vendor = c->name;
			break;
		}
	}
767
	if (!vendor) {
Linus Torvalds's avatar
Linus Torvalds committed
768
769
770
		sprintf(tmp, "Generic %04x", vendor_id);
		vendor = tmp;
	}
771
772
773
774
775
776
777
778
	codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
	if (!codec->vendor_name)
		return -ENOMEM;

 get_chip_name:
	if (codec->chip_name)
		return 0;

Linus Torvalds's avatar
Linus Torvalds committed
779
	if (codec->preset && codec->preset->name)
780
781
782
783
784
785
		codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL);
	else {
		sprintf(tmp, "ID %x", codec->vendor_id & 0xffff);
		codec->chip_name = kstrdup(tmp, GFP_KERNEL);
	}
	if (!codec->chip_name)
786
787
		return -ENOMEM;
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
788
789
790
}

/*
Sasha Khapyorsky's avatar
Sasha Khapyorsky committed
791
 * look for an AFG and MFG nodes
Linus Torvalds's avatar
Linus Torvalds committed
792
 */
793
static void /*__devinit*/ setup_fg_nodes(struct hda_codec *codec)
Linus Torvalds's avatar
Linus Torvalds committed
794
{
795
	int i, total_nodes, function_id;
Linus Torvalds's avatar
Linus Torvalds committed
796
797
798
799
	hda_nid_t nid;

	total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
	for (i = 0; i < total_nodes; i++, nid++) {
800
		function_id = snd_hda_param_read(codec, nid,
801
						AC_PAR_FUNCTION_TYPE);
802
		switch (function_id & 0xff) {
Sasha Khapyorsky's avatar
Sasha Khapyorsky committed
803
804
		case AC_GRP_AUDIO_FUNCTION:
			codec->afg = nid;
805
806
			codec->afg_function_id = function_id & 0xff;
			codec->afg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky's avatar
Sasha Khapyorsky committed
807
808
809
			break;
		case AC_GRP_MODEM_FUNCTION:
			codec->mfg = nid;
810
811
			codec->mfg_function_id = function_id & 0xff;
			codec->mfg_unsol = (function_id >> 8) & 1;
Sasha Khapyorsky's avatar
Sasha Khapyorsky committed
812
813
814
815
			break;
		default:
			break;
		}
Linus Torvalds's avatar
Linus Torvalds committed
816
817
818
	}
}

819
820
821
822
823
824
825
826
827
828
829
/*
 * read widget caps for each widget and store in cache
 */
static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
{
	int i;
	hda_nid_t nid;

	codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
						 &codec->start_nid);
	codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
830
	if (!codec->wcaps)
831
832
833
834
835
836
837
838
		return -ENOMEM;
	nid = codec->start_nid;
	for (i = 0; i < codec->num_nodes; i++, nid++)
		codec->wcaps[i] = snd_hda_param_read(codec, nid,
						     AC_PAR_AUDIO_WIDGET_CAP);
	return 0;
}

839
840
841
842
843
844
845
846
847
/* read all pin default configurations and save codec->init_pins */
static int read_pin_defaults(struct hda_codec *codec)
{
	int i;
	hda_nid_t nid = codec->start_nid;

	for (i = 0; i < codec->num_nodes; i++, nid++) {
		struct hda_pincfg *pin;
		unsigned int wcaps = get_wcaps(codec, nid);
848
		unsigned int wid_type = get_wcaps_type(wcaps);
849
850
851
852
853
854
855
856
		if (wid_type != AC_WID_PIN)
			continue;
		pin = snd_array_new(&codec->init_pins);
		if (!pin)
			return -ENOMEM;
		pin->nid = nid;
		pin->cfg = snd_hda_codec_read(codec, nid, 0,
					      AC_VERB_GET_CONFIG_DEFAULT, 0);
857
858
859
		pin->ctrl = snd_hda_codec_read(codec, nid, 0,
					       AC_VERB_GET_PIN_WIDGET_CONTROL,
					       0);
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
	}
	return 0;
}

/* look up the given pin config list and return the item matching with NID */
static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
					 struct snd_array *array,
					 hda_nid_t nid)
{
	int i;
	for (i = 0; i < array->used; i++) {
		struct hda_pincfg *pin = snd_array_elem(array, i);
		if (pin->nid == nid)
			return pin;
	}
	return NULL;
}

/* write a config value for the given NID */
static void set_pincfg(struct hda_codec *codec, hda_nid_t nid,
		       unsigned int cfg)
{
	int i;
	for (i = 0; i < 4; i++) {
		snd_hda_codec_write(codec, nid, 0,
				    AC_VERB_SET_CONFIG_DEFAULT_BYTES_0 + i,
				    cfg & 0xff);
		cfg >>= 8;
	}
}

/* set the current pin config value for the given NID.
 * the value is cached, and read via snd_hda_codec_get_pincfg()
 */
int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
		       hda_nid_t nid, unsigned int cfg)
{
	struct hda_pincfg *pin;
898
	unsigned int oldcfg;
899

900
901
902
	if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
		return -EINVAL;

903
	oldcfg = snd_hda_codec_get_pincfg(codec, nid);
904
905
906
907
908
909
910
911
	pin = look_up_pincfg(codec, list, nid);
	if (!pin) {
		pin = snd_array_new(list);
		if (!pin)
			return -ENOMEM;
		pin->nid = nid;
	}
	pin->cfg = cfg;
912
913
914
915
916
917
918

	/* change only when needed; e.g. if the pincfg is already present
	 * in user_pins[], don't write it
	 */
	cfg = snd_hda_codec_get_pincfg(codec, nid);
	if (oldcfg != cfg)
		set_pincfg(codec, nid, cfg);
919
920
921
	return 0;
}

922
923
924
925
926
927
928
929
930
931
/**
 * snd_hda_codec_set_pincfg - Override a pin default configuration
 * @codec: the HDA codec
 * @nid: NID to set the pin config
 * @cfg: the pin default config value
 *
 * Override a pin default configuration value in the cache.
 * This value can be read by snd_hda_codec_get_pincfg() in a higher
 * priority than the real hardware value.
 */
932
933
934
int snd_hda_codec_set_pincfg(struct hda_codec *codec,
			     hda_nid_t nid, unsigned int cfg)
{
935
	return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
936
937
938
}
EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg);

939
940
941
942
943
944
945
946
947
/**
 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
 * @codec: the HDA codec
 * @nid: NID to get the pin config
 *
 * Get the current pin config value of the given pin NID.
 * If the pincfg value is cached or overridden via sysfs or driver,
 * returns the cached value.
 */
948
949
950
951
952
unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
{
	struct hda_pincfg *pin;

#ifdef CONFIG_SND_HDA_HWDEP
953
	pin = look_up_pincfg(codec, &codec->user_pins, nid);
954
955
956
	if (pin)
		return pin->cfg;
#endif
957
958
959
	pin = look_up_pincfg(codec, &codec->driver_pins, nid);
	if (pin)
		return pin->cfg;
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
	pin = look_up_pincfg(codec, &codec->init_pins, nid);
	if (pin)
		return pin->cfg;
	return 0;
}
EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg);

/* restore all current pin configs */
static void restore_pincfgs(struct hda_codec *codec)
{
	int i;
	for (i = 0; i < codec->init_pins.used; i++) {
		struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
		set_pincfg(codec, pin->nid,
			   snd_hda_codec_get_pincfg(codec, pin->nid));
	}
}
977

978
979
980
981
982
983
984
985
986
987
/**
 * snd_hda_shutup_pins - Shut up all pins
 * @codec: the HDA codec
 *
 * Clear all pin controls to shup up before suspend for avoiding click noise.
 * The controls aren't cached so that they can be resumed properly.
 */
void snd_hda_shutup_pins(struct hda_codec *codec)
{
	int i;
988
989
990
991
992
	/* don't shut up pins when unloading the driver; otherwise it breaks
	 * the default pin setup at the next load of the driver
	 */
	if (codec->bus->shutdown)
		return;
993
994
995
996
997
998
	for (i = 0; i < codec->init_pins.used; i++) {
		struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
		/* use read here for syncing after issuing each verb */
		snd_hda_codec_read(codec, pin->nid, 0,
				   AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
	}
999
	codec->pins_shutup = 1;
1000
}