hda_hwdep.c 17.7 KB
Newer Older
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
/*
 * HWDEP Interface for HD-audio codec
 *
 * Copyright (c) 2007 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/slab.h>
#include <linux/pci.h>
#include <linux/compat.h>
#include <linux/mutex.h>
T
Takashi Iwai 已提交
26
#include <linux/ctype.h>
27
#include <linux/firmware.h>
28 29 30 31
#include <sound/core.h>
#include "hda_codec.h"
#include "hda_local.h"
#include <sound/hda_hwdep.h>
32
#include <sound/minors.h>
33

34 35 36 37 38 39
/* hint string pair */
struct hda_hint {
	const char *key;
	const char *val;	/* contained in the same alloc as key */
};

40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
/*
 * write/read an out-of-bound verb
 */
static int verb_write_ioctl(struct hda_codec *codec,
			    struct hda_verb_ioctl __user *arg)
{
	u32 verb, res;

	if (get_user(verb, &arg->verb))
		return -EFAULT;
	res = snd_hda_codec_read(codec, verb >> 24, 0,
				 (verb >> 8) & 0xffff, verb & 0xff);
	if (put_user(res, &arg->res))
		return -EFAULT;
	return 0;
}

static int get_wcap_ioctl(struct hda_codec *codec,
			  struct hda_verb_ioctl __user *arg)
{
	u32 verb, res;
	
	if (get_user(verb, &arg->verb))
		return -EFAULT;
	res = get_wcaps(codec, verb >> 24);
	if (put_user(res, &arg->res))
		return -EFAULT;
	return 0;
}


/*
 */
static int hda_hwdep_ioctl(struct snd_hwdep *hw, struct file *file,
			   unsigned int cmd, unsigned long arg)
{
	struct hda_codec *codec = hw->private_data;
	void __user *argp = (void __user *)arg;

	switch (cmd) {
	case HDA_IOCTL_PVERSION:
		return put_user(HDA_HWDEP_VERSION, (int __user *)argp);
	case HDA_IOCTL_VERB_WRITE:
		return verb_write_ioctl(codec, argp);
	case HDA_IOCTL_GET_WCAP:
		return get_wcap_ioctl(codec, argp);
	}
	return -ENOIOCTLCMD;
}

#ifdef CONFIG_COMPAT
static int hda_hwdep_ioctl_compat(struct snd_hwdep *hw, struct file *file,
				  unsigned int cmd, unsigned long arg)
{
94
	return hda_hwdep_ioctl(hw, file, cmd, (unsigned long)compat_ptr(arg));
95 96 97 98 99
}
#endif

static int hda_hwdep_open(struct snd_hwdep *hw, struct file *file)
{
100
#ifndef CONFIG_SND_DEBUG_VERBOSE
101 102 103 104 105 106
	if (!capable(CAP_SYS_RAWIO))
		return -EACCES;
#endif
	return 0;
}

T
Takashi Iwai 已提交
107 108
static void clear_hwdep_elements(struct hda_codec *codec)
{
T
Takashi Iwai 已提交
109 110
	int i;

T
Takashi Iwai 已提交
111 112
	/* clear init verbs */
	snd_array_free(&codec->init_verbs);
T
Takashi Iwai 已提交
113
	/* clear hints */
114 115 116 117
	for (i = 0; i < codec->hints.used; i++) {
		struct hda_hint *hint = snd_array_elem(&codec->hints, i);
		kfree(hint->key); /* we don't need to free hint->val */
	}
T
Takashi Iwai 已提交
118
	snd_array_free(&codec->hints);
119
	snd_array_free(&codec->user_pins);
T
Takashi Iwai 已提交
120 121 122 123 124 125 126
}

static void hwdep_free(struct snd_hwdep *hwdep)
{
	clear_hwdep_elements(hwdep->private_data);
}

127
int /*__devinit*/ snd_hda_create_hwdep(struct hda_codec *codec)
128 129 130 131 132 133 134 135 136 137 138 139 140
{
	char hwname[16];
	struct snd_hwdep *hwdep;
	int err;

	sprintf(hwname, "HDA Codec %d", codec->addr);
	err = snd_hwdep_new(codec->bus->card, hwname, codec->addr, &hwdep);
	if (err < 0)
		return err;
	codec->hwdep = hwdep;
	sprintf(hwdep->name, "HDA Codec %d", codec->addr);
	hwdep->iface = SNDRV_HWDEP_IFACE_HDA;
	hwdep->private_data = codec;
T
Takashi Iwai 已提交
141
	hwdep->private_free = hwdep_free;
142 143 144 145 146 147 148 149
	hwdep->exclusive = 1;

	hwdep->ops.open = hda_hwdep_open;
	hwdep->ops.ioctl = hda_hwdep_ioctl;
#ifdef CONFIG_COMPAT
	hwdep->ops.ioctl_compat = hda_hwdep_ioctl_compat;
#endif

T
Takashi Iwai 已提交
150
	snd_array_init(&codec->init_verbs, sizeof(struct hda_verb), 32);
151
	snd_array_init(&codec->hints, sizeof(struct hda_hint), 32);
152
	snd_array_init(&codec->user_pins, sizeof(struct hda_pincfg), 16);
T
Takashi Iwai 已提交
153

154 155
	return 0;
}
156

157 158
#ifdef CONFIG_SND_HDA_RECONFIG

159 160 161 162 163 164
/*
 * sysfs interface
 */

static int clear_codec(struct hda_codec *codec)
{
165 166 167 168 169 170 171
	int err;

	err = snd_hda_codec_reset(codec);
	if (err < 0) {
		snd_printk(KERN_ERR "The codec is being used, can't free.\n");
		return err;
	}
T
Takashi Iwai 已提交
172
	clear_hwdep_elements(codec);
173 174 175 176 177 178 179
	return 0;
}

static int reconfig_codec(struct hda_codec *codec)
{
	int err;

180
	snd_hda_power_up(codec);
181
	snd_printk(KERN_INFO "hda-codec: reconfiguring\n");
182 183 184 185
	err = snd_hda_codec_reset(codec);
	if (err < 0) {
		snd_printk(KERN_ERR
			   "The codec is being used, can't reconfigure.\n");
186
		goto error;
187
	}
188 189
	err = snd_hda_codec_configure(codec);
	if (err < 0)
190
		goto error;
191
	/* rebuild PCMs */
T
Takashi Iwai 已提交
192
	err = snd_hda_codec_build_pcms(codec);
193
	if (err < 0)
194
		goto error;
195 196 197
	/* rebuild mixers */
	err = snd_hda_codec_build_controls(codec);
	if (err < 0)
198 199 200 201 202
		goto error;
	err = snd_card_register(codec->bus->card);
 error:
	snd_hda_power_down(codec);
	return err;
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
}

/*
 * allocate a string at most len chars, and remove the trailing EOL
 */
static char *kstrndup_noeol(const char *src, size_t len)
{
	char *s = kstrndup(src, len, GFP_KERNEL);
	char *p;
	if (!s)
		return NULL;
	p = strchr(s, '\n');
	if (p)
		*p = 0;
	return s;
}

#define CODEC_INFO_SHOW(type)					\
static ssize_t type##_show(struct device *dev,			\
			   struct device_attribute *attr,	\
			   char *buf)				\
{								\
	struct snd_hwdep *hwdep = dev_get_drvdata(dev);		\
	struct hda_codec *codec = hwdep->private_data;		\
	return sprintf(buf, "0x%x\n", codec->type);		\
}

#define CODEC_INFO_STR_SHOW(type)				\
static ssize_t type##_show(struct device *dev,			\
			     struct device_attribute *attr,	\
					char *buf)		\
{								\
	struct snd_hwdep *hwdep = dev_get_drvdata(dev);		\
	struct hda_codec *codec = hwdep->private_data;		\
	return sprintf(buf, "%s\n",				\
		       codec->type ? codec->type : "");		\
}

CODEC_INFO_SHOW(vendor_id);
CODEC_INFO_SHOW(subsystem_id);
CODEC_INFO_SHOW(revision_id);
CODEC_INFO_SHOW(afg);
CODEC_INFO_SHOW(mfg);
246 247
CODEC_INFO_STR_SHOW(vendor_name);
CODEC_INFO_STR_SHOW(chip_name);
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
CODEC_INFO_STR_SHOW(modelname);

#define CODEC_INFO_STORE(type)					\
static ssize_t type##_store(struct device *dev,			\
			    struct device_attribute *attr,	\
			    const char *buf, size_t count)	\
{								\
	struct snd_hwdep *hwdep = dev_get_drvdata(dev);		\
	struct hda_codec *codec = hwdep->private_data;		\
	char *after;						\
	codec->type = simple_strtoul(buf, &after, 0);		\
	return count;						\
}

#define CODEC_INFO_STR_STORE(type)				\
static ssize_t type##_store(struct device *dev,			\
			    struct device_attribute *attr,	\
			    const char *buf, size_t count)	\
{								\
	struct snd_hwdep *hwdep = dev_get_drvdata(dev);		\
	struct hda_codec *codec = hwdep->private_data;		\
	char *s = kstrndup_noeol(buf, 64);			\
	if (!s)							\
		return -ENOMEM;					\
	kfree(codec->type);					\
	codec->type = s;					\
	return count;						\
}

CODEC_INFO_STORE(vendor_id);
CODEC_INFO_STORE(subsystem_id);
CODEC_INFO_STORE(revision_id);
280 281
CODEC_INFO_STR_STORE(vendor_name);
CODEC_INFO_STR_STORE(chip_name);
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
CODEC_INFO_STR_STORE(modelname);

#define CODEC_ACTION_STORE(type)				\
static ssize_t type##_store(struct device *dev,			\
			    struct device_attribute *attr,	\
			    const char *buf, size_t count)	\
{								\
	struct snd_hwdep *hwdep = dev_get_drvdata(dev);		\
	struct hda_codec *codec = hwdep->private_data;		\
	int err = 0;						\
	if (*buf)						\
		err = type##_codec(codec);			\
	return err < 0 ? err : count;				\
}

CODEC_ACTION_STORE(reconfig);
CODEC_ACTION_STORE(clear);

300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
static ssize_t init_verbs_show(struct device *dev,
			       struct device_attribute *attr,
			       char *buf)
{
	struct snd_hwdep *hwdep = dev_get_drvdata(dev);
	struct hda_codec *codec = hwdep->private_data;
	int i, len = 0;
	for (i = 0; i < codec->init_verbs.used; i++) {
		struct hda_verb *v = snd_array_elem(&codec->init_verbs, i);
		len += snprintf(buf + len, PAGE_SIZE - len,
				"0x%02x 0x%03x 0x%04x\n",
				v->nid, v->verb, v->param);
	}
	return len;
}

316
static int parse_init_verbs(struct hda_codec *codec, const char *buf)
T
Takashi Iwai 已提交
317
{
318 319
	struct hda_verb *v;
	int nid, verb, param;
T
Takashi Iwai 已提交
320

321 322 323
	if (sscanf(buf, "%i %i %i", &nid, &verb, &param) != 3)
		return -EINVAL;
	if (!nid || !verb)
T
Takashi Iwai 已提交
324 325 326 327
		return -EINVAL;
	v = snd_array_new(&codec->init_verbs);
	if (!v)
		return -ENOMEM;
328 329 330
	v->nid = nid;
	v->verb = verb;
	v->param = param;
331 332 333 334 335 336 337 338 339 340 341 342
	return 0;
}

static ssize_t init_verbs_store(struct device *dev,
				struct device_attribute *attr,
				const char *buf, size_t count)
{
	struct snd_hwdep *hwdep = dev_get_drvdata(dev);
	struct hda_codec *codec = hwdep->private_data;
	int err = parse_init_verbs(codec, buf);
	if (err < 0)
		return err;
T
Takashi Iwai 已提交
343 344 345
	return count;
}

346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
static ssize_t hints_show(struct device *dev,
			  struct device_attribute *attr,
			  char *buf)
{
	struct snd_hwdep *hwdep = dev_get_drvdata(dev);
	struct hda_codec *codec = hwdep->private_data;
	int i, len = 0;
	for (i = 0; i < codec->hints.used; i++) {
		struct hda_hint *hint = snd_array_elem(&codec->hints, i);
		len += snprintf(buf + len, PAGE_SIZE - len,
				"%s = %s\n", hint->key, hint->val);
	}
	return len;
}

361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
static struct hda_hint *get_hint(struct hda_codec *codec, const char *key)
{
	int i;

	for (i = 0; i < codec->hints.used; i++) {
		struct hda_hint *hint = snd_array_elem(&codec->hints, i);
		if (!strcmp(hint->key, key))
			return hint;
	}
	return NULL;
}

static void remove_trail_spaces(char *str)
{
	char *p;
	if (!*str)
		return;
	p = str + strlen(str) - 1;
	for (; isspace(*p); p--) {
		*p = 0;
		if (p == str)
			return;
	}
}

#define MAX_HINTS	1024

388
static int parse_hints(struct hda_codec *codec, const char *buf)
T
Takashi Iwai 已提交
389
{
390 391
	char *key, *val;
	struct hda_hint *hint;
T
Takashi Iwai 已提交
392

393 394 395
	while (isspace(*buf))
		buf++;
	if (!*buf || *buf == '#' || *buf == '\n')
396
		return 0;
397 398 399 400
	if (*buf == '=')
		return -EINVAL;
	key = kstrndup_noeol(buf, 1024);
	if (!key)
T
Takashi Iwai 已提交
401
		return -ENOMEM;
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
	/* extract key and val */
	val = strchr(key, '=');
	if (!val) {
		kfree(key);
		return -EINVAL;
	}
	*val++ = 0;
	while (isspace(*val))
		val++;
	remove_trail_spaces(key);
	remove_trail_spaces(val);
	hint = get_hint(codec, key);
	if (hint) {
		/* replace */
		kfree(hint->key);
		hint->key = key;
		hint->val = val;
419
		return 0;
420 421 422 423 424 425
	}
	/* allocate a new hint entry */
	if (codec->hints.used >= MAX_HINTS)
		hint = NULL;
	else
		hint = snd_array_new(&codec->hints);
T
Takashi Iwai 已提交
426
	if (!hint) {
427
		kfree(key);
T
Takashi Iwai 已提交
428 429
		return -ENOMEM;
	}
430 431
	hint->key = key;
	hint->val = val;
432 433 434 435 436 437 438 439 440 441 442 443
	return 0;
}

static ssize_t hints_store(struct device *dev,
			   struct device_attribute *attr,
			   const char *buf, size_t count)
{
	struct snd_hwdep *hwdep = dev_get_drvdata(dev);
	struct hda_codec *codec = hwdep->private_data;
	int err = parse_hints(codec, buf);
	if (err < 0)
		return err;
T
Takashi Iwai 已提交
444 445 446
	return count;
}

447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
static ssize_t pin_configs_show(struct hda_codec *codec,
				struct snd_array *list,
				char *buf)
{
	int i, len = 0;
	for (i = 0; i < list->used; i++) {
		struct hda_pincfg *pin = snd_array_elem(list, i);
		len += sprintf(buf + len, "0x%02x 0x%08x\n",
			       pin->nid, pin->cfg);
	}
	return len;
}

static ssize_t init_pin_configs_show(struct device *dev,
				     struct device_attribute *attr,
				     char *buf)
{
	struct snd_hwdep *hwdep = dev_get_drvdata(dev);
	struct hda_codec *codec = hwdep->private_data;
	return pin_configs_show(codec, &codec->init_pins, buf);
}

469 470 471
static ssize_t user_pin_configs_show(struct device *dev,
				     struct device_attribute *attr,
				     char *buf)
472 473 474
{
	struct snd_hwdep *hwdep = dev_get_drvdata(dev);
	struct hda_codec *codec = hwdep->private_data;
475
	return pin_configs_show(codec, &codec->user_pins, buf);
476 477
}

478 479 480
static ssize_t driver_pin_configs_show(struct device *dev,
				       struct device_attribute *attr,
				       char *buf)
481 482 483
{
	struct snd_hwdep *hwdep = dev_get_drvdata(dev);
	struct hda_codec *codec = hwdep->private_data;
484
	return pin_configs_show(codec, &codec->driver_pins, buf);
485 486 487 488
}

#define MAX_PIN_CONFIGS		32

489
static int parse_user_pin_configs(struct hda_codec *codec, const char *buf)
490 491 492 493 494 495 496
{
	int nid, cfg;

	if (sscanf(buf, "%i %i", &nid, &cfg) != 2)
		return -EINVAL;
	if (!nid)
		return -EINVAL;
497 498 499 500 501 502 503 504 505 506
	return snd_hda_add_pincfg(codec, &codec->user_pins, nid, cfg);
}

static ssize_t user_pin_configs_store(struct device *dev,
				      struct device_attribute *attr,
				      const char *buf, size_t count)
{
	struct snd_hwdep *hwdep = dev_get_drvdata(dev);
	struct hda_codec *codec = hwdep->private_data;
	int err = parse_user_pin_configs(codec, buf);
507 508 509 510 511
	if (err < 0)
		return err;
	return count;
}

512 513 514 515 516 517 518 519 520 521 522 523 524
#define CODEC_ATTR_RW(type) \
	__ATTR(type, 0644, type##_show, type##_store)
#define CODEC_ATTR_RO(type) \
	__ATTR_RO(type)
#define CODEC_ATTR_WO(type) \
	__ATTR(type, 0200, NULL, type##_store)

static struct device_attribute codec_attrs[] = {
	CODEC_ATTR_RW(vendor_id),
	CODEC_ATTR_RW(subsystem_id),
	CODEC_ATTR_RW(revision_id),
	CODEC_ATTR_RO(afg),
	CODEC_ATTR_RO(mfg),
525 526
	CODEC_ATTR_RW(vendor_name),
	CODEC_ATTR_RW(chip_name),
527
	CODEC_ATTR_RW(modelname),
528 529
	CODEC_ATTR_RW(init_verbs),
	CODEC_ATTR_RW(hints),
530
	CODEC_ATTR_RO(init_pin_configs),
531 532
	CODEC_ATTR_RW(user_pin_configs),
	CODEC_ATTR_RO(driver_pin_configs),
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
	CODEC_ATTR_WO(reconfig),
	CODEC_ATTR_WO(clear),
};

/*
 * create sysfs files on hwdep directory
 */
int snd_hda_hwdep_add_sysfs(struct hda_codec *codec)
{
	struct snd_hwdep *hwdep = codec->hwdep;
	int i;

	for (i = 0; i < ARRAY_SIZE(codec_attrs); i++)
		snd_add_device_sysfs_file(SNDRV_DEVICE_TYPE_HWDEP, hwdep->card,
					  hwdep->device, &codec_attrs[i]);
	return 0;
}
550

551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575
/*
 * Look for hint string
 */
const char *snd_hda_get_hint(struct hda_codec *codec, const char *key)
{
	struct hda_hint *hint = get_hint(codec, key);
	return hint ? hint->val : NULL;
}
EXPORT_SYMBOL_HDA(snd_hda_get_hint);

int snd_hda_get_bool_hint(struct hda_codec *codec, const char *key)
{
	const char *p = snd_hda_get_hint(codec, key);
	if (!p || !*p)
		return -ENOENT;
	switch (toupper(*p)) {
	case 'T': /* true */
	case 'Y': /* yes */
	case '1':
		return 1;
	}
	return 0;
}
EXPORT_SYMBOL_HDA(snd_hda_get_bool_hint);

576
#endif /* CONFIG_SND_HDA_RECONFIG */
577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753

#ifdef CONFIG_SND_HDA_PATCH_LOADER

/* parser mode */
enum {
	LINE_MODE_NONE,
	LINE_MODE_CODEC,
	LINE_MODE_MODEL,
	LINE_MODE_PINCFG,
	LINE_MODE_VERB,
	LINE_MODE_HINT,
	NUM_LINE_MODES,
};

static inline int strmatch(const char *a, const char *b)
{
	return strnicmp(a, b, strlen(b)) == 0;
}

/* parse the contents after the line "[codec]"
 * accept only the line with three numbers, and assign the current codec
 */
static void parse_codec_mode(char *buf, struct hda_bus *bus,
			     struct hda_codec **codecp)
{
	unsigned int vendorid, subid, caddr;
	struct hda_codec *codec;

	*codecp = NULL;
	if (sscanf(buf, "%i %i %i", &vendorid, &subid, &caddr) == 3) {
		list_for_each_entry(codec, &bus->codec_list, list) {
			if (codec->addr == caddr) {
				*codecp = codec;
				break;
			}
		}
	}
}

/* parse the contents after the other command tags, [pincfg], [verb],
 * [hint] and [model]
 * just pass to the sysfs helper (only when any codec was specified)
 */
static void parse_pincfg_mode(char *buf, struct hda_bus *bus,
			      struct hda_codec **codecp)
{
	if (!*codecp)
		return;
	parse_user_pin_configs(*codecp, buf);
}

static void parse_verb_mode(char *buf, struct hda_bus *bus,
			    struct hda_codec **codecp)
{
	if (!*codecp)
		return;
	parse_init_verbs(*codecp, buf);
}

static void parse_hint_mode(char *buf, struct hda_bus *bus,
			    struct hda_codec **codecp)
{
	if (!*codecp)
		return;
	parse_hints(*codecp, buf);
}

static void parse_model_mode(char *buf, struct hda_bus *bus,
			     struct hda_codec **codecp)
{
	if (!*codecp)
		return;
	kfree((*codecp)->modelname);
	(*codecp)->modelname = kstrdup(buf, GFP_KERNEL);
}

struct hda_patch_item {
	const char *tag;
	void (*parser)(char *buf, struct hda_bus *bus, struct hda_codec **retc);
};

static struct hda_patch_item patch_items[NUM_LINE_MODES] = {
	[LINE_MODE_CODEC] = { "[codec]", parse_codec_mode },
	[LINE_MODE_MODEL] = { "[model]", parse_model_mode },
	[LINE_MODE_VERB] = { "[verb]", parse_verb_mode },
	[LINE_MODE_PINCFG] = { "[pincfg]", parse_pincfg_mode },
	[LINE_MODE_HINT] = { "[hint]", parse_hint_mode },
};

/* check the line starting with '[' -- change the parser mode accodingly */
static int parse_line_mode(char *buf, struct hda_bus *bus)
{
	int i;
	for (i = 0; i < ARRAY_SIZE(patch_items); i++) {
		if (!patch_items[i].tag)
			continue;
		if (strmatch(buf, patch_items[i].tag))
			return i;
	}
	return LINE_MODE_NONE;
}

/* copy one line from the buffer in fw, and update the fields in fw
 * return zero if it reaches to the end of the buffer, or non-zero
 * if successfully copied a line
 *
 * the spaces at the beginning and the end of the line are stripped
 */
static int get_line_from_fw(char *buf, int size, struct firmware *fw)
{
	int len;
	const char *p = fw->data;
	while (isspace(*p) && fw->size) {
		p++;
		fw->size--;
	}
	if (!fw->size)
		return 0;
	if (size < fw->size)
		size = fw->size;

	for (len = 0; len < fw->size; len++) {
		if (!*p)
			break;
		if (*p == '\n') {
			p++;
			len++;
			break;
		}
		if (len < size)
			*buf++ = *p++;
	}
	*buf = 0;
	fw->size -= len;
	fw->data = p;
	remove_trail_spaces(buf);
	return 1;
}

/*
 * load a "patch" firmware file and parse it
 */
int snd_hda_load_patch(struct hda_bus *bus, const char *patch)
{
	int err;
	const struct firmware *fw;
	struct firmware tmp;
	char buf[128];
	struct hda_codec *codec;
	int line_mode;
	struct device *dev = bus->card->dev;

	if (snd_BUG_ON(!dev))
		return -ENODEV;
	err = request_firmware(&fw, patch, dev);
	if (err < 0) {
		printk(KERN_ERR "hda-codec: Cannot load the patch '%s'\n",
		       patch);
		return err;
	}

	tmp = *fw;
	line_mode = LINE_MODE_NONE;
	codec = NULL;
	while (get_line_from_fw(buf, sizeof(buf) - 1, &tmp)) {
		if (!*buf || *buf == '#' || *buf == '\n')
			continue;
		if (*buf == '[')
			line_mode = parse_line_mode(buf, bus);
		else if (patch_items[line_mode].parser)
			patch_items[line_mode].parser(buf, bus, &codec);
	}
	release_firmware(fw);
	return 0;
}
EXPORT_SYMBOL_HDA(snd_hda_load_patch);
#endif /* CONFIG_SND_HDA_PATCH_LOADER */