ctdaio.c 17 KB
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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 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 94 95 96 97 98 99 100 101 102 103 104 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 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 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 246 247 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 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 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 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 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 576 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 754 755 756 757 758
/**
 * Copyright (C) 2008, Creative Technology Ltd. All Rights Reserved.
 *
 * This source file is released under GPL v2 license (no other versions).
 * See the COPYING file included in the main directory of this source
 * distribution for the license terms and conditions.
 *
 * @File	ctdaio.c
 *
 * @Brief
 * This file contains the implementation of Digital Audio Input Output
 * resource management object.
 *
 * @Author	Liu Chun
 * @Date 	May 23 2008
 *
 */

#include "ctdaio.h"
#include "cthardware.h"
#include "ctimap.h"
#include <linux/slab.h>
#include <linux/kernel.h>

#define DAIO_OUT_MAX		SPDIFOO

struct daio_usage {
	unsigned short data;
};

struct daio_rsc_idx {
	unsigned short left;
	unsigned short right;
};

static struct daio_rsc_idx idx_20k1[NUM_DAIOTYP] = {
	[LINEO1] = {.left = 0x00, .right = 0x01},
	[LINEO2] = {.left = 0x18, .right = 0x19},
	[LINEO3] = {.left = 0x08, .right = 0x09},
	[LINEO4] = {.left = 0x10, .right = 0x11},
	[LINEIM] = {.left = 0x1b5, .right = 0x1bd},
	[SPDIFOO] = {.left = 0x20, .right = 0x21},
	[SPDIFIO] = {.left = 0x15, .right = 0x1d},
	[SPDIFI1] = {.left = 0x95, .right = 0x9d},
};

static struct daio_rsc_idx idx_20k2[NUM_DAIOTYP] = {
	[LINEO1] = {.left = 0x40, .right = 0x41},
	[LINEO2] = {.left = 0x60, .right = 0x61},
	[LINEO3] = {.left = 0x50, .right = 0x51},
	[LINEO4] = {.left = 0x70, .right = 0x71},
	[LINEIM] = {.left = 0x45, .right = 0xc5},
	[MIC]	 = {.left = 0x55, .right = 0xd5},
	[SPDIFOO] = {.left = 0x00, .right = 0x01},
	[SPDIFIO] = {.left = 0x05, .right = 0x85},
};

static int daio_master(struct rsc *rsc)
{
	/* Actually, this is not the resource index of DAIO.
	 * For DAO, it is the input mapper index. And, for DAI,
	 * it is the output time-slot index. */
	return rsc->conj = rsc->idx;
}

static int daio_index(const struct rsc *rsc)
{
	return rsc->conj;
}

static int daio_out_next_conj(struct rsc *rsc)
{
	return rsc->conj += 2;
}

static int daio_in_next_conj_20k1(struct rsc *rsc)
{
	return rsc->conj += 0x200;
}

static int daio_in_next_conj_20k2(struct rsc *rsc)
{
	return rsc->conj += 0x100;
}

static const struct rsc_ops daio_out_rsc_ops = {
	.master		= daio_master,
	.next_conj	= daio_out_next_conj,
	.index		= daio_index,
	.output_slot	= NULL,
};

static const struct rsc_ops daio_in_rsc_ops_20k1 = {
	.master		= daio_master,
	.next_conj	= daio_in_next_conj_20k1,
	.index		= NULL,
	.output_slot	= daio_index,
};

static const struct rsc_ops daio_in_rsc_ops_20k2 = {
	.master		= daio_master,
	.next_conj	= daio_in_next_conj_20k2,
	.index		= NULL,
	.output_slot	= daio_index,
};

static unsigned int daio_device_index(enum DAIOTYP type, struct hw *hw)
{
	switch (hw->chip_type) {
	case ATC20K1:
		switch (type) {
		case SPDIFOO:	return 0;
		case SPDIFIO:	return 0;
		case SPDIFI1:	return 1;
		case LINEO1:	return 4;
		case LINEO2:	return 7;
		case LINEO3:	return 5;
		case LINEO4:	return 6;
		case LINEIM:	return 7;
		default:	return -EINVAL;
		}
	case ATC20K2:
		switch (type) {
		case SPDIFOO:	return 0;
		case SPDIFIO:	return 0;
		case LINEO1:	return 4;
		case LINEO2:	return 7;
		case LINEO3:	return 5;
		case LINEO4:	return 6;
		case LINEIM:	return 4;
		case MIC:	return 5;
		default:	return -EINVAL;
		}
	default:
		return -EINVAL;
	}
}

static int dao_rsc_reinit(struct dao *dao, const struct dao_desc *desc);

static int dao_spdif_get_spos(struct dao *dao, unsigned int *spos)
{
	dao->hw->dao_get_spos(dao->ctrl_blk, spos);
	return 0;
}

static int dao_spdif_set_spos(struct dao *dao, unsigned int spos)
{
	dao->hw->dao_set_spos(dao->ctrl_blk, spos);
	return 0;
}

static int dao_commit_write(struct dao *dao)
{
	dao->hw->dao_commit_write(dao->hw,
		daio_device_index(dao->daio.type, dao->hw), dao->ctrl_blk);
	return 0;
}

static int dao_set_left_input(struct dao *dao, struct rsc *input)
{
	struct imapper *entry;
	struct daio *daio = &dao->daio;
	int i;

	entry = kzalloc((sizeof(*entry) * daio->rscl.msr), GFP_KERNEL);
	if (!entry)
		return -ENOMEM;

	dao->ops->clear_left_input(dao);
	/* Program master and conjugate resources */
	input->ops->master(input);
	daio->rscl.ops->master(&daio->rscl);
	for (i = 0; i < daio->rscl.msr; i++, entry++) {
		entry->slot = input->ops->output_slot(input);
		entry->user = entry->addr = daio->rscl.ops->index(&daio->rscl);
		dao->mgr->imap_add(dao->mgr, entry);
		dao->imappers[i] = entry;

		input->ops->next_conj(input);
		daio->rscl.ops->next_conj(&daio->rscl);
	}
	input->ops->master(input);
	daio->rscl.ops->master(&daio->rscl);

	return 0;
}

static int dao_set_right_input(struct dao *dao, struct rsc *input)
{
	struct imapper *entry;
	struct daio *daio = &dao->daio;
	int i;

	entry = kzalloc((sizeof(*entry) * daio->rscr.msr), GFP_KERNEL);
	if (!entry)
		return -ENOMEM;

	dao->ops->clear_right_input(dao);
	/* Program master and conjugate resources */
	input->ops->master(input);
	daio->rscr.ops->master(&daio->rscr);
	for (i = 0; i < daio->rscr.msr; i++, entry++) {
		entry->slot = input->ops->output_slot(input);
		entry->user = entry->addr = daio->rscr.ops->index(&daio->rscr);
		dao->mgr->imap_add(dao->mgr, entry);
		dao->imappers[daio->rscl.msr + i] = entry;

		input->ops->next_conj(input);
		daio->rscr.ops->next_conj(&daio->rscr);
	}
	input->ops->master(input);
	daio->rscr.ops->master(&daio->rscr);

	return 0;
}

static int dao_clear_left_input(struct dao *dao)
{
	struct imapper *entry;
	struct daio *daio = &dao->daio;
	int i;

	if (!dao->imappers[0])
		return 0;

	entry = dao->imappers[0];
	dao->mgr->imap_delete(dao->mgr, entry);
	/* Program conjugate resources */
	for (i = 1; i < daio->rscl.msr; i++) {
		entry = dao->imappers[i];
		dao->mgr->imap_delete(dao->mgr, entry);
		dao->imappers[i] = NULL;
	}

	kfree(dao->imappers[0]);
	dao->imappers[0] = NULL;

	return 0;
}

static int dao_clear_right_input(struct dao *dao)
{
	struct imapper *entry;
	struct daio *daio = &dao->daio;
	int i;

	if (!dao->imappers[daio->rscl.msr])
		return 0;

	entry = dao->imappers[daio->rscl.msr];
	dao->mgr->imap_delete(dao->mgr, entry);
	/* Program conjugate resources */
	for (i = 1; i < daio->rscr.msr; i++) {
		entry = dao->imappers[daio->rscl.msr + i];
		dao->mgr->imap_delete(dao->mgr, entry);
		dao->imappers[daio->rscl.msr + i] = NULL;
	}

	kfree(dao->imappers[daio->rscl.msr]);
	dao->imappers[daio->rscl.msr] = NULL;

	return 0;
}

static const struct dao_rsc_ops dao_ops = {
	.set_spos		= dao_spdif_set_spos,
	.commit_write		= dao_commit_write,
	.get_spos		= dao_spdif_get_spos,
	.reinit			= dao_rsc_reinit,
	.set_left_input		= dao_set_left_input,
	.set_right_input	= dao_set_right_input,
	.clear_left_input	= dao_clear_left_input,
	.clear_right_input	= dao_clear_right_input,
};

static int dai_set_srt_srcl(struct dai *dai, struct rsc *src)
{
	src->ops->master(src);
	dai->hw->dai_srt_set_srcm(dai->ctrl_blk, src->ops->index(src));
	return 0;
}

static int dai_set_srt_srcr(struct dai *dai, struct rsc *src)
{
	src->ops->master(src);
	dai->hw->dai_srt_set_srco(dai->ctrl_blk, src->ops->index(src));
	return 0;
}

static int dai_set_srt_msr(struct dai *dai, unsigned int msr)
{
	unsigned int rsr;

	for (rsr = 0; msr > 1; msr >>= 1)
		rsr++;

	dai->hw->dai_srt_set_rsr(dai->ctrl_blk, rsr);
	return 0;
}

static int dai_set_enb_src(struct dai *dai, unsigned int enb)
{
	dai->hw->dai_srt_set_ec(dai->ctrl_blk, enb);
	return 0;
}

static int dai_set_enb_srt(struct dai *dai, unsigned int enb)
{
	dai->hw->dai_srt_set_et(dai->ctrl_blk, enb);
	return 0;
}

static int dai_commit_write(struct dai *dai)
{
	dai->hw->dai_commit_write(dai->hw,
		daio_device_index(dai->daio.type, dai->hw), dai->ctrl_blk);
	return 0;
}

static const struct dai_rsc_ops dai_ops = {
	.set_srt_srcl		= dai_set_srt_srcl,
	.set_srt_srcr		= dai_set_srt_srcr,
	.set_srt_msr		= dai_set_srt_msr,
	.set_enb_src		= dai_set_enb_src,
	.set_enb_srt		= dai_set_enb_srt,
	.commit_write		= dai_commit_write,
};

static int daio_rsc_init(struct daio *daio,
			 const struct daio_desc *desc,
			 struct hw *hw)
{
	int err;
	unsigned int idx_l, idx_r;

	switch (hw->chip_type) {
	case ATC20K1:
		idx_l = idx_20k1[desc->type].left;
		idx_r = idx_20k1[desc->type].right;
		break;
	case ATC20K2:
		idx_l = idx_20k2[desc->type].left;
		idx_r = idx_20k2[desc->type].right;
		break;
	default:
		return -EINVAL;
	}
	err = rsc_init(&daio->rscl, idx_l, DAIO, desc->msr, hw);
	if (err)
		return err;

	err = rsc_init(&daio->rscr, idx_r, DAIO, desc->msr, hw);
	if (err)
		goto error1;

	/* Set daio->rscl/r->ops to daio specific ones */
	if (desc->type <= DAIO_OUT_MAX) {
		daio->rscl.ops = daio->rscr.ops = &daio_out_rsc_ops;
	} else {
		switch (hw->chip_type) {
		case ATC20K1:
			daio->rscl.ops = daio->rscr.ops = &daio_in_rsc_ops_20k1;
			break;
		case ATC20K2:
			daio->rscl.ops = daio->rscr.ops = &daio_in_rsc_ops_20k2;
			break;
		default:
			break;
		}
	}
	daio->type = desc->type;

	return 0;

error1:
	rsc_uninit(&daio->rscl);
	return err;
}

static int daio_rsc_uninit(struct daio *daio)
{
	rsc_uninit(&daio->rscl);
	rsc_uninit(&daio->rscr);

	return 0;
}

static int dao_rsc_init(struct dao *dao,
			const struct daio_desc *desc,
			struct daio_mgr *mgr)
{
	struct hw *hw = mgr->mgr.hw;
	unsigned int conf;
	int err;

	err = daio_rsc_init(&dao->daio, desc, mgr->mgr.hw);
	if (err)
		return err;

	dao->imappers = kzalloc(sizeof(void *)*desc->msr*2, GFP_KERNEL);
	if (!dao->imappers) {
		err = -ENOMEM;
		goto error1;
	}
	dao->ops = &dao_ops;
	dao->mgr = mgr;
	dao->hw = hw;
	err = hw->dao_get_ctrl_blk(&dao->ctrl_blk);
	if (err)
		goto error2;

	hw->daio_mgr_dsb_dao(mgr->mgr.ctrl_blk,
			daio_device_index(dao->daio.type, hw));
	hw->daio_mgr_commit_write(hw, mgr->mgr.ctrl_blk);

	conf = (desc->msr & 0x7) | (desc->passthru << 3);
	hw->daio_mgr_dao_init(mgr->mgr.ctrl_blk,
			daio_device_index(dao->daio.type, hw), conf);
	hw->daio_mgr_enb_dao(mgr->mgr.ctrl_blk,
			daio_device_index(dao->daio.type, hw));
	hw->daio_mgr_commit_write(hw, mgr->mgr.ctrl_blk);

	return 0;

error2:
	kfree(dao->imappers);
	dao->imappers = NULL;
error1:
	daio_rsc_uninit(&dao->daio);
	return err;
}

static int dao_rsc_uninit(struct dao *dao)
{
	if (dao->imappers) {
		if (dao->imappers[0])
			dao_clear_left_input(dao);

		if (dao->imappers[dao->daio.rscl.msr])
			dao_clear_right_input(dao);

		kfree(dao->imappers);
		dao->imappers = NULL;
	}
	dao->hw->dao_put_ctrl_blk(dao->ctrl_blk);
	dao->hw = dao->ctrl_blk = NULL;
	daio_rsc_uninit(&dao->daio);

	return 0;
}

static int dao_rsc_reinit(struct dao *dao, const struct dao_desc *desc)
{
	struct daio_mgr *mgr = dao->mgr;
	struct daio_desc dsc = {0};

	dsc.type = dao->daio.type;
	dsc.msr = desc->msr;
	dsc.passthru = desc->passthru;
	dao_rsc_uninit(dao);
	return dao_rsc_init(dao, &dsc, mgr);
}

static int dai_rsc_init(struct dai *dai,
			const struct daio_desc *desc,
			struct daio_mgr *mgr)
{
	int err;
	struct hw *hw = mgr->mgr.hw;
	unsigned int rsr, msr;

	err = daio_rsc_init(&dai->daio, desc, mgr->mgr.hw);
	if (err)
		return err;

	dai->ops = &dai_ops;
	dai->hw = mgr->mgr.hw;
	err = hw->dai_get_ctrl_blk(&dai->ctrl_blk);
	if (err)
		goto error1;

	for (rsr = 0, msr = desc->msr; msr > 1; msr >>= 1)
		rsr++;

	hw->dai_srt_set_rsr(dai->ctrl_blk, rsr);
	hw->dai_srt_set_drat(dai->ctrl_blk, 0);
	/* default to disabling control of a SRC */
	hw->dai_srt_set_ec(dai->ctrl_blk, 0);
	hw->dai_srt_set_et(dai->ctrl_blk, 0); /* default to disabling SRT */
	hw->dai_commit_write(hw,
		daio_device_index(dai->daio.type, dai->hw), dai->ctrl_blk);

	return 0;

error1:
	daio_rsc_uninit(&dai->daio);
	return err;
}

static int dai_rsc_uninit(struct dai *dai)
{
	dai->hw->dai_put_ctrl_blk(dai->ctrl_blk);
	dai->hw = dai->ctrl_blk = NULL;
	daio_rsc_uninit(&dai->daio);
	return 0;
}

static int daio_mgr_get_rsc(struct rsc_mgr *mgr, enum DAIOTYP type)
{
	if (((struct daio_usage *)mgr->rscs)->data & (0x1 << type))
		return -ENOENT;

	((struct daio_usage *)mgr->rscs)->data |= (0x1 << type);

	return 0;
}

static int daio_mgr_put_rsc(struct rsc_mgr *mgr, enum DAIOTYP type)
{
	((struct daio_usage *)mgr->rscs)->data &= ~(0x1 << type);

	return 0;
}

static int get_daio_rsc(struct daio_mgr *mgr,
			const struct daio_desc *desc,
			struct daio **rdaio)
{
	int err;
	unsigned long flags;

	*rdaio = NULL;

	/* Check whether there are sufficient daio resources to meet request. */
	spin_lock_irqsave(&mgr->mgr_lock, flags);
	err = daio_mgr_get_rsc(&mgr->mgr, desc->type);
	spin_unlock_irqrestore(&mgr->mgr_lock, flags);
	if (err) {
		dev_err(mgr->card->dev,
			"Can't meet DAIO resource request!\n");
		return err;
	}

	err = -ENOMEM;
	/* Allocate mem for daio resource */
	if (desc->type <= DAIO_OUT_MAX) {
		struct dao *dao = kzalloc(sizeof(*dao), GFP_KERNEL);
		if (!dao)
			goto error;

		err = dao_rsc_init(dao, desc, mgr);
		if (err) {
			kfree(dao);
			goto error;
		}

		*rdaio = &dao->daio;
	} else {
		struct dai *dai = kzalloc(sizeof(*dai), GFP_KERNEL);
		if (!dai)
			goto error;

		err = dai_rsc_init(dai, desc, mgr);
		if (err) {
			kfree(dai);
			goto error;
		}

		*rdaio = &dai->daio;
	}

	mgr->daio_enable(mgr, *rdaio);
	mgr->commit_write(mgr);

	return 0;

error:
	spin_lock_irqsave(&mgr->mgr_lock, flags);
	daio_mgr_put_rsc(&mgr->mgr, desc->type);
	spin_unlock_irqrestore(&mgr->mgr_lock, flags);
	return err;
}

static int put_daio_rsc(struct daio_mgr *mgr, struct daio *daio)
{
	unsigned long flags;

	mgr->daio_disable(mgr, daio);
	mgr->commit_write(mgr);

	spin_lock_irqsave(&mgr->mgr_lock, flags);
	daio_mgr_put_rsc(&mgr->mgr, daio->type);
	spin_unlock_irqrestore(&mgr->mgr_lock, flags);

	if (daio->type <= DAIO_OUT_MAX) {
		dao_rsc_uninit(container_of(daio, struct dao, daio));
		kfree(container_of(daio, struct dao, daio));
	} else {
		dai_rsc_uninit(container_of(daio, struct dai, daio));
		kfree(container_of(daio, struct dai, daio));
	}

	return 0;
}

static int daio_mgr_enb_daio(struct daio_mgr *mgr, struct daio *daio)
{
	struct hw *hw = mgr->mgr.hw;

	if (DAIO_OUT_MAX >= daio->type) {
		hw->daio_mgr_enb_dao(mgr->mgr.ctrl_blk,
				daio_device_index(daio->type, hw));
	} else {
		hw->daio_mgr_enb_dai(mgr->mgr.ctrl_blk,
				daio_device_index(daio->type, hw));
	}
	return 0;
}

static int daio_mgr_dsb_daio(struct daio_mgr *mgr, struct daio *daio)
{
	struct hw *hw = mgr->mgr.hw;

	if (DAIO_OUT_MAX >= daio->type) {
		hw->daio_mgr_dsb_dao(mgr->mgr.ctrl_blk,
				daio_device_index(daio->type, hw));
	} else {
		hw->daio_mgr_dsb_dai(mgr->mgr.ctrl_blk,
				daio_device_index(daio->type, hw));
	}
	return 0;
}

static int daio_map_op(void *data, struct imapper *entry)
{
	struct rsc_mgr *mgr = &((struct daio_mgr *)data)->mgr;
	struct hw *hw = mgr->hw;

	hw->daio_mgr_set_imaparc(mgr->ctrl_blk, entry->slot);
	hw->daio_mgr_set_imapnxt(mgr->ctrl_blk, entry->next);
	hw->daio_mgr_set_imapaddr(mgr->ctrl_blk, entry->addr);
	hw->daio_mgr_commit_write(mgr->hw, mgr->ctrl_blk);

	return 0;
}

static int daio_imap_add(struct daio_mgr *mgr, struct imapper *entry)
{
	unsigned long flags;
	int err;

	spin_lock_irqsave(&mgr->imap_lock, flags);
	if (!entry->addr && mgr->init_imap_added) {
		input_mapper_delete(&mgr->imappers, mgr->init_imap,
							daio_map_op, mgr);
		mgr->init_imap_added = 0;
	}
	err = input_mapper_add(&mgr->imappers, entry, daio_map_op, mgr);
	spin_unlock_irqrestore(&mgr->imap_lock, flags);

	return err;
}

static int daio_imap_delete(struct daio_mgr *mgr, struct imapper *entry)
{
	unsigned long flags;
	int err;

	spin_lock_irqsave(&mgr->imap_lock, flags);
	err = input_mapper_delete(&mgr->imappers, entry, daio_map_op, mgr);
	if (list_empty(&mgr->imappers)) {
		input_mapper_add(&mgr->imappers, mgr->init_imap,
							daio_map_op, mgr);
		mgr->init_imap_added = 1;
	}
	spin_unlock_irqrestore(&mgr->imap_lock, flags);

	return err;
}

static int daio_mgr_commit_write(struct daio_mgr *mgr)
{
	struct hw *hw = mgr->mgr.hw;

	hw->daio_mgr_commit_write(hw, mgr->mgr.ctrl_blk);
	return 0;
}

int daio_mgr_create(struct hw *hw, struct daio_mgr **rdaio_mgr)
{
	int err, i;
	struct daio_mgr *daio_mgr;
	struct imapper *entry;

	*rdaio_mgr = NULL;
	daio_mgr = kzalloc(sizeof(*daio_mgr), GFP_KERNEL);
	if (!daio_mgr)
		return -ENOMEM;

	err = rsc_mgr_init(&daio_mgr->mgr, DAIO, NUM_DAIOTYP, hw);
	if (err)
		goto error1;

	spin_lock_init(&daio_mgr->mgr_lock);
	spin_lock_init(&daio_mgr->imap_lock);
	INIT_LIST_HEAD(&daio_mgr->imappers);
	entry = kzalloc(sizeof(*entry), GFP_KERNEL);
	if (!entry) {
		err = -ENOMEM;
		goto error2;
	}
	entry->slot = entry->addr = entry->next = entry->user = 0;
	list_add(&entry->list, &daio_mgr->imappers);
	daio_mgr->init_imap = entry;
	daio_mgr->init_imap_added = 1;

	daio_mgr->get_daio = get_daio_rsc;
	daio_mgr->put_daio = put_daio_rsc;
	daio_mgr->daio_enable = daio_mgr_enb_daio;
	daio_mgr->daio_disable = daio_mgr_dsb_daio;
	daio_mgr->imap_add = daio_imap_add;
	daio_mgr->imap_delete = daio_imap_delete;
	daio_mgr->commit_write = daio_mgr_commit_write;
	daio_mgr->card = hw->card;

	for (i = 0; i < 8; i++) {
		hw->daio_mgr_dsb_dao(daio_mgr->mgr.ctrl_blk, i);
		hw->daio_mgr_dsb_dai(daio_mgr->mgr.ctrl_blk, i);
	}
	hw->daio_mgr_commit_write(hw, daio_mgr->mgr.ctrl_blk);

	*rdaio_mgr = daio_mgr;

	return 0;

error2:
	rsc_mgr_uninit(&daio_mgr->mgr);
error1:
	kfree(daio_mgr);
	return err;
}

int daio_mgr_destroy(struct daio_mgr *daio_mgr)
{
	unsigned long flags;

	/* free daio input mapper list */
	spin_lock_irqsave(&daio_mgr->imap_lock, flags);
	free_input_mapper_list(&daio_mgr->imappers);
	spin_unlock_irqrestore(&daio_mgr->imap_lock, flags);

	rsc_mgr_uninit(&daio_mgr->mgr);
	kfree(daio_mgr);

	return 0;
}