ctdaio.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  1. /**
  2. * Copyright (C) 2008, Creative Technology Ltd. All Rights Reserved.
  3. *
  4. * This source file is released under GPL v2 license (no other versions).
  5. * See the COPYING file included in the main directory of this source
  6. * distribution for the license terms and conditions.
  7. *
  8. * @File ctdaio.c
  9. *
  10. * @Brief
  11. * This file contains the implementation of Digital Audio Input Output
  12. * resource management object.
  13. *
  14. * @Author Liu Chun
  15. * @Date May 23 2008
  16. *
  17. */
  18. #include "ctdaio.h"
  19. #include "cthardware.h"
  20. #include "ctimap.h"
  21. #include <linux/slab.h>
  22. #include <linux/kernel.h>
  23. #define DAIO_OUT_MAX SPDIFOO
  24. struct daio_usage {
  25. unsigned short data;
  26. };
  27. struct daio_rsc_idx {
  28. unsigned short left;
  29. unsigned short right;
  30. };
  31. static struct daio_rsc_idx idx_20k1[NUM_DAIOTYP] = {
  32. [LINEO1] = {.left = 0x00, .right = 0x01},
  33. [LINEO2] = {.left = 0x18, .right = 0x19},
  34. [LINEO3] = {.left = 0x08, .right = 0x09},
  35. [LINEO4] = {.left = 0x10, .right = 0x11},
  36. [LINEIM] = {.left = 0x1b5, .right = 0x1bd},
  37. [SPDIFOO] = {.left = 0x20, .right = 0x21},
  38. [SPDIFIO] = {.left = 0x15, .right = 0x1d},
  39. [SPDIFI1] = {.left = 0x95, .right = 0x9d},
  40. };
  41. static struct daio_rsc_idx idx_20k2[NUM_DAIOTYP] = {
  42. [LINEO1] = {.left = 0x40, .right = 0x41},
  43. [LINEO2] = {.left = 0x60, .right = 0x61},
  44. [LINEO3] = {.left = 0x50, .right = 0x51},
  45. [LINEO4] = {.left = 0x70, .right = 0x71},
  46. [LINEIM] = {.left = 0x45, .right = 0xc5},
  47. [MIC] = {.left = 0x55, .right = 0xd5},
  48. [SPDIFOO] = {.left = 0x00, .right = 0x01},
  49. [SPDIFIO] = {.left = 0x05, .right = 0x85},
  50. };
  51. static int daio_master(struct rsc *rsc)
  52. {
  53. /* Actually, this is not the resource index of DAIO.
  54. * For DAO, it is the input mapper index. And, for DAI,
  55. * it is the output time-slot index. */
  56. return rsc->conj = rsc->idx;
  57. }
  58. static int daio_index(const struct rsc *rsc)
  59. {
  60. return rsc->conj;
  61. }
  62. static int daio_out_next_conj(struct rsc *rsc)
  63. {
  64. return rsc->conj += 2;
  65. }
  66. static int daio_in_next_conj_20k1(struct rsc *rsc)
  67. {
  68. return rsc->conj += 0x200;
  69. }
  70. static int daio_in_next_conj_20k2(struct rsc *rsc)
  71. {
  72. return rsc->conj += 0x100;
  73. }
  74. static struct rsc_ops daio_out_rsc_ops = {
  75. .master = daio_master,
  76. .next_conj = daio_out_next_conj,
  77. .index = daio_index,
  78. .output_slot = NULL,
  79. };
  80. static struct rsc_ops daio_in_rsc_ops_20k1 = {
  81. .master = daio_master,
  82. .next_conj = daio_in_next_conj_20k1,
  83. .index = NULL,
  84. .output_slot = daio_index,
  85. };
  86. static struct rsc_ops daio_in_rsc_ops_20k2 = {
  87. .master = daio_master,
  88. .next_conj = daio_in_next_conj_20k2,
  89. .index = NULL,
  90. .output_slot = daio_index,
  91. };
  92. static unsigned int daio_device_index(enum DAIOTYP type, struct hw *hw)
  93. {
  94. switch (hw->chip_type) {
  95. case ATC20K1:
  96. switch (type) {
  97. case SPDIFOO: return 0;
  98. case SPDIFIO: return 0;
  99. case SPDIFI1: return 1;
  100. case LINEO1: return 4;
  101. case LINEO2: return 7;
  102. case LINEO3: return 5;
  103. case LINEO4: return 6;
  104. case LINEIM: return 7;
  105. default: return -EINVAL;
  106. }
  107. case ATC20K2:
  108. switch (type) {
  109. case SPDIFOO: return 0;
  110. case SPDIFIO: return 0;
  111. case LINEO1: return 4;
  112. case LINEO2: return 7;
  113. case LINEO3: return 5;
  114. case LINEO4: return 6;
  115. case LINEIM: return 4;
  116. case MIC: return 5;
  117. default: return -EINVAL;
  118. }
  119. default:
  120. return -EINVAL;
  121. }
  122. }
  123. static int dao_rsc_reinit(struct dao *dao, const struct dao_desc *desc);
  124. static int dao_spdif_get_spos(struct dao *dao, unsigned int *spos)
  125. {
  126. dao->hw->dao_get_spos(dao->ctrl_blk, spos);
  127. return 0;
  128. }
  129. static int dao_spdif_set_spos(struct dao *dao, unsigned int spos)
  130. {
  131. dao->hw->dao_set_spos(dao->ctrl_blk, spos);
  132. return 0;
  133. }
  134. static int dao_commit_write(struct dao *dao)
  135. {
  136. dao->hw->dao_commit_write(dao->hw,
  137. daio_device_index(dao->daio.type, dao->hw), dao->ctrl_blk);
  138. return 0;
  139. }
  140. static int dao_set_left_input(struct dao *dao, struct rsc *input)
  141. {
  142. struct imapper *entry;
  143. struct daio *daio = &dao->daio;
  144. int i;
  145. entry = kzalloc((sizeof(*entry) * daio->rscl.msr), GFP_KERNEL);
  146. if (!entry)
  147. return -ENOMEM;
  148. dao->ops->clear_left_input(dao);
  149. /* Program master and conjugate resources */
  150. input->ops->master(input);
  151. daio->rscl.ops->master(&daio->rscl);
  152. for (i = 0; i < daio->rscl.msr; i++, entry++) {
  153. entry->slot = input->ops->output_slot(input);
  154. entry->user = entry->addr = daio->rscl.ops->index(&daio->rscl);
  155. dao->mgr->imap_add(dao->mgr, entry);
  156. dao->imappers[i] = entry;
  157. input->ops->next_conj(input);
  158. daio->rscl.ops->next_conj(&daio->rscl);
  159. }
  160. input->ops->master(input);
  161. daio->rscl.ops->master(&daio->rscl);
  162. return 0;
  163. }
  164. static int dao_set_right_input(struct dao *dao, struct rsc *input)
  165. {
  166. struct imapper *entry;
  167. struct daio *daio = &dao->daio;
  168. int i;
  169. entry = kzalloc((sizeof(*entry) * daio->rscr.msr), GFP_KERNEL);
  170. if (!entry)
  171. return -ENOMEM;
  172. dao->ops->clear_right_input(dao);
  173. /* Program master and conjugate resources */
  174. input->ops->master(input);
  175. daio->rscr.ops->master(&daio->rscr);
  176. for (i = 0; i < daio->rscr.msr; i++, entry++) {
  177. entry->slot = input->ops->output_slot(input);
  178. entry->user = entry->addr = daio->rscr.ops->index(&daio->rscr);
  179. dao->mgr->imap_add(dao->mgr, entry);
  180. dao->imappers[daio->rscl.msr + i] = entry;
  181. input->ops->next_conj(input);
  182. daio->rscr.ops->next_conj(&daio->rscr);
  183. }
  184. input->ops->master(input);
  185. daio->rscr.ops->master(&daio->rscr);
  186. return 0;
  187. }
  188. static int dao_clear_left_input(struct dao *dao)
  189. {
  190. struct imapper *entry;
  191. struct daio *daio = &dao->daio;
  192. int i;
  193. if (!dao->imappers[0])
  194. return 0;
  195. entry = dao->imappers[0];
  196. dao->mgr->imap_delete(dao->mgr, entry);
  197. /* Program conjugate resources */
  198. for (i = 1; i < daio->rscl.msr; i++) {
  199. entry = dao->imappers[i];
  200. dao->mgr->imap_delete(dao->mgr, entry);
  201. dao->imappers[i] = NULL;
  202. }
  203. kfree(dao->imappers[0]);
  204. dao->imappers[0] = NULL;
  205. return 0;
  206. }
  207. static int dao_clear_right_input(struct dao *dao)
  208. {
  209. struct imapper *entry;
  210. struct daio *daio = &dao->daio;
  211. int i;
  212. if (!dao->imappers[daio->rscl.msr])
  213. return 0;
  214. entry = dao->imappers[daio->rscl.msr];
  215. dao->mgr->imap_delete(dao->mgr, entry);
  216. /* Program conjugate resources */
  217. for (i = 1; i < daio->rscr.msr; i++) {
  218. entry = dao->imappers[daio->rscl.msr + i];
  219. dao->mgr->imap_delete(dao->mgr, entry);
  220. dao->imappers[daio->rscl.msr + i] = NULL;
  221. }
  222. kfree(dao->imappers[daio->rscl.msr]);
  223. dao->imappers[daio->rscl.msr] = NULL;
  224. return 0;
  225. }
  226. static struct dao_rsc_ops dao_ops = {
  227. .set_spos = dao_spdif_set_spos,
  228. .commit_write = dao_commit_write,
  229. .get_spos = dao_spdif_get_spos,
  230. .reinit = dao_rsc_reinit,
  231. .set_left_input = dao_set_left_input,
  232. .set_right_input = dao_set_right_input,
  233. .clear_left_input = dao_clear_left_input,
  234. .clear_right_input = dao_clear_right_input,
  235. };
  236. static int dai_set_srt_srcl(struct dai *dai, struct rsc *src)
  237. {
  238. src->ops->master(src);
  239. dai->hw->dai_srt_set_srcm(dai->ctrl_blk, src->ops->index(src));
  240. return 0;
  241. }
  242. static int dai_set_srt_srcr(struct dai *dai, struct rsc *src)
  243. {
  244. src->ops->master(src);
  245. dai->hw->dai_srt_set_srco(dai->ctrl_blk, src->ops->index(src));
  246. return 0;
  247. }
  248. static int dai_set_srt_msr(struct dai *dai, unsigned int msr)
  249. {
  250. unsigned int rsr;
  251. for (rsr = 0; msr > 1; msr >>= 1)
  252. rsr++;
  253. dai->hw->dai_srt_set_rsr(dai->ctrl_blk, rsr);
  254. return 0;
  255. }
  256. static int dai_set_enb_src(struct dai *dai, unsigned int enb)
  257. {
  258. dai->hw->dai_srt_set_ec(dai->ctrl_blk, enb);
  259. return 0;
  260. }
  261. static int dai_set_enb_srt(struct dai *dai, unsigned int enb)
  262. {
  263. dai->hw->dai_srt_set_et(dai->ctrl_blk, enb);
  264. return 0;
  265. }
  266. static int dai_commit_write(struct dai *dai)
  267. {
  268. dai->hw->dai_commit_write(dai->hw,
  269. daio_device_index(dai->daio.type, dai->hw), dai->ctrl_blk);
  270. return 0;
  271. }
  272. static struct dai_rsc_ops dai_ops = {
  273. .set_srt_srcl = dai_set_srt_srcl,
  274. .set_srt_srcr = dai_set_srt_srcr,
  275. .set_srt_msr = dai_set_srt_msr,
  276. .set_enb_src = dai_set_enb_src,
  277. .set_enb_srt = dai_set_enb_srt,
  278. .commit_write = dai_commit_write,
  279. };
  280. static int daio_rsc_init(struct daio *daio,
  281. const struct daio_desc *desc,
  282. struct hw *hw)
  283. {
  284. int err;
  285. unsigned int idx_l, idx_r;
  286. switch (hw->chip_type) {
  287. case ATC20K1:
  288. idx_l = idx_20k1[desc->type].left;
  289. idx_r = idx_20k1[desc->type].right;
  290. break;
  291. case ATC20K2:
  292. idx_l = idx_20k2[desc->type].left;
  293. idx_r = idx_20k2[desc->type].right;
  294. break;
  295. default:
  296. return -EINVAL;
  297. }
  298. err = rsc_init(&daio->rscl, idx_l, DAIO, desc->msr, hw);
  299. if (err)
  300. return err;
  301. err = rsc_init(&daio->rscr, idx_r, DAIO, desc->msr, hw);
  302. if (err)
  303. goto error1;
  304. /* Set daio->rscl/r->ops to daio specific ones */
  305. if (desc->type <= DAIO_OUT_MAX) {
  306. daio->rscl.ops = daio->rscr.ops = &daio_out_rsc_ops;
  307. } else {
  308. switch (hw->chip_type) {
  309. case ATC20K1:
  310. daio->rscl.ops = daio->rscr.ops = &daio_in_rsc_ops_20k1;
  311. break;
  312. case ATC20K2:
  313. daio->rscl.ops = daio->rscr.ops = &daio_in_rsc_ops_20k2;
  314. break;
  315. default:
  316. break;
  317. }
  318. }
  319. daio->type = desc->type;
  320. return 0;
  321. error1:
  322. rsc_uninit(&daio->rscl);
  323. return err;
  324. }
  325. static int daio_rsc_uninit(struct daio *daio)
  326. {
  327. rsc_uninit(&daio->rscl);
  328. rsc_uninit(&daio->rscr);
  329. return 0;
  330. }
  331. static int dao_rsc_init(struct dao *dao,
  332. const struct daio_desc *desc,
  333. struct daio_mgr *mgr)
  334. {
  335. struct hw *hw = mgr->mgr.hw;
  336. unsigned int conf;
  337. int err;
  338. err = daio_rsc_init(&dao->daio, desc, mgr->mgr.hw);
  339. if (err)
  340. return err;
  341. dao->imappers = kzalloc(sizeof(void *)*desc->msr*2, GFP_KERNEL);
  342. if (!dao->imappers) {
  343. err = -ENOMEM;
  344. goto error1;
  345. }
  346. dao->ops = &dao_ops;
  347. dao->mgr = mgr;
  348. dao->hw = hw;
  349. err = hw->dao_get_ctrl_blk(&dao->ctrl_blk);
  350. if (err)
  351. goto error2;
  352. hw->daio_mgr_dsb_dao(mgr->mgr.ctrl_blk,
  353. daio_device_index(dao->daio.type, hw));
  354. hw->daio_mgr_commit_write(hw, mgr->mgr.ctrl_blk);
  355. conf = (desc->msr & 0x7) | (desc->passthru << 3);
  356. hw->daio_mgr_dao_init(mgr->mgr.ctrl_blk,
  357. daio_device_index(dao->daio.type, hw), conf);
  358. hw->daio_mgr_enb_dao(mgr->mgr.ctrl_blk,
  359. daio_device_index(dao->daio.type, hw));
  360. hw->daio_mgr_commit_write(hw, mgr->mgr.ctrl_blk);
  361. return 0;
  362. error2:
  363. kfree(dao->imappers);
  364. dao->imappers = NULL;
  365. error1:
  366. daio_rsc_uninit(&dao->daio);
  367. return err;
  368. }
  369. static int dao_rsc_uninit(struct dao *dao)
  370. {
  371. if (dao->imappers) {
  372. if (dao->imappers[0])
  373. dao_clear_left_input(dao);
  374. if (dao->imappers[dao->daio.rscl.msr])
  375. dao_clear_right_input(dao);
  376. kfree(dao->imappers);
  377. dao->imappers = NULL;
  378. }
  379. dao->hw->dao_put_ctrl_blk(dao->ctrl_blk);
  380. dao->hw = dao->ctrl_blk = NULL;
  381. daio_rsc_uninit(&dao->daio);
  382. return 0;
  383. }
  384. static int dao_rsc_reinit(struct dao *dao, const struct dao_desc *desc)
  385. {
  386. struct daio_mgr *mgr = dao->mgr;
  387. struct daio_desc dsc = {0};
  388. dsc.type = dao->daio.type;
  389. dsc.msr = desc->msr;
  390. dsc.passthru = desc->passthru;
  391. dao_rsc_uninit(dao);
  392. return dao_rsc_init(dao, &dsc, mgr);
  393. }
  394. static int dai_rsc_init(struct dai *dai,
  395. const struct daio_desc *desc,
  396. struct daio_mgr *mgr)
  397. {
  398. int err;
  399. struct hw *hw = mgr->mgr.hw;
  400. unsigned int rsr, msr;
  401. err = daio_rsc_init(&dai->daio, desc, mgr->mgr.hw);
  402. if (err)
  403. return err;
  404. dai->ops = &dai_ops;
  405. dai->hw = mgr->mgr.hw;
  406. err = hw->dai_get_ctrl_blk(&dai->ctrl_blk);
  407. if (err)
  408. goto error1;
  409. for (rsr = 0, msr = desc->msr; msr > 1; msr >>= 1)
  410. rsr++;
  411. hw->dai_srt_set_rsr(dai->ctrl_blk, rsr);
  412. hw->dai_srt_set_drat(dai->ctrl_blk, 0);
  413. /* default to disabling control of a SRC */
  414. hw->dai_srt_set_ec(dai->ctrl_blk, 0);
  415. hw->dai_srt_set_et(dai->ctrl_blk, 0); /* default to disabling SRT */
  416. hw->dai_commit_write(hw,
  417. daio_device_index(dai->daio.type, dai->hw), dai->ctrl_blk);
  418. return 0;
  419. error1:
  420. daio_rsc_uninit(&dai->daio);
  421. return err;
  422. }
  423. static int dai_rsc_uninit(struct dai *dai)
  424. {
  425. dai->hw->dai_put_ctrl_blk(dai->ctrl_blk);
  426. dai->hw = dai->ctrl_blk = NULL;
  427. daio_rsc_uninit(&dai->daio);
  428. return 0;
  429. }
  430. static int daio_mgr_get_rsc(struct rsc_mgr *mgr, enum DAIOTYP type)
  431. {
  432. if (((struct daio_usage *)mgr->rscs)->data & (0x1 << type))
  433. return -ENOENT;
  434. ((struct daio_usage *)mgr->rscs)->data |= (0x1 << type);
  435. return 0;
  436. }
  437. static int daio_mgr_put_rsc(struct rsc_mgr *mgr, enum DAIOTYP type)
  438. {
  439. ((struct daio_usage *)mgr->rscs)->data &= ~(0x1 << type);
  440. return 0;
  441. }
  442. static int get_daio_rsc(struct daio_mgr *mgr,
  443. const struct daio_desc *desc,
  444. struct daio **rdaio)
  445. {
  446. int err;
  447. unsigned long flags;
  448. *rdaio = NULL;
  449. /* Check whether there are sufficient daio resources to meet request. */
  450. spin_lock_irqsave(&mgr->mgr_lock, flags);
  451. err = daio_mgr_get_rsc(&mgr->mgr, desc->type);
  452. spin_unlock_irqrestore(&mgr->mgr_lock, flags);
  453. if (err) {
  454. dev_err(mgr->card->dev,
  455. "Can't meet DAIO resource request!\n");
  456. return err;
  457. }
  458. err = -ENOMEM;
  459. /* Allocate mem for daio resource */
  460. if (desc->type <= DAIO_OUT_MAX) {
  461. struct dao *dao = kzalloc(sizeof(*dao), GFP_KERNEL);
  462. if (!dao)
  463. goto error;
  464. err = dao_rsc_init(dao, desc, mgr);
  465. if (err) {
  466. kfree(dao);
  467. goto error;
  468. }
  469. *rdaio = &dao->daio;
  470. } else {
  471. struct dai *dai = kzalloc(sizeof(*dai), GFP_KERNEL);
  472. if (!dai)
  473. goto error;
  474. err = dai_rsc_init(dai, desc, mgr);
  475. if (err) {
  476. kfree(dai);
  477. goto error;
  478. }
  479. *rdaio = &dai->daio;
  480. }
  481. mgr->daio_enable(mgr, *rdaio);
  482. mgr->commit_write(mgr);
  483. return 0;
  484. error:
  485. spin_lock_irqsave(&mgr->mgr_lock, flags);
  486. daio_mgr_put_rsc(&mgr->mgr, desc->type);
  487. spin_unlock_irqrestore(&mgr->mgr_lock, flags);
  488. return err;
  489. }
  490. static int put_daio_rsc(struct daio_mgr *mgr, struct daio *daio)
  491. {
  492. unsigned long flags;
  493. mgr->daio_disable(mgr, daio);
  494. mgr->commit_write(mgr);
  495. spin_lock_irqsave(&mgr->mgr_lock, flags);
  496. daio_mgr_put_rsc(&mgr->mgr, daio->type);
  497. spin_unlock_irqrestore(&mgr->mgr_lock, flags);
  498. if (daio->type <= DAIO_OUT_MAX) {
  499. dao_rsc_uninit(container_of(daio, struct dao, daio));
  500. kfree(container_of(daio, struct dao, daio));
  501. } else {
  502. dai_rsc_uninit(container_of(daio, struct dai, daio));
  503. kfree(container_of(daio, struct dai, daio));
  504. }
  505. return 0;
  506. }
  507. static int daio_mgr_enb_daio(struct daio_mgr *mgr, struct daio *daio)
  508. {
  509. struct hw *hw = mgr->mgr.hw;
  510. if (DAIO_OUT_MAX >= daio->type) {
  511. hw->daio_mgr_enb_dao(mgr->mgr.ctrl_blk,
  512. daio_device_index(daio->type, hw));
  513. } else {
  514. hw->daio_mgr_enb_dai(mgr->mgr.ctrl_blk,
  515. daio_device_index(daio->type, hw));
  516. }
  517. return 0;
  518. }
  519. static int daio_mgr_dsb_daio(struct daio_mgr *mgr, struct daio *daio)
  520. {
  521. struct hw *hw = mgr->mgr.hw;
  522. if (DAIO_OUT_MAX >= daio->type) {
  523. hw->daio_mgr_dsb_dao(mgr->mgr.ctrl_blk,
  524. daio_device_index(daio->type, hw));
  525. } else {
  526. hw->daio_mgr_dsb_dai(mgr->mgr.ctrl_blk,
  527. daio_device_index(daio->type, hw));
  528. }
  529. return 0;
  530. }
  531. static int daio_map_op(void *data, struct imapper *entry)
  532. {
  533. struct rsc_mgr *mgr = &((struct daio_mgr *)data)->mgr;
  534. struct hw *hw = mgr->hw;
  535. hw->daio_mgr_set_imaparc(mgr->ctrl_blk, entry->slot);
  536. hw->daio_mgr_set_imapnxt(mgr->ctrl_blk, entry->next);
  537. hw->daio_mgr_set_imapaddr(mgr->ctrl_blk, entry->addr);
  538. hw->daio_mgr_commit_write(mgr->hw, mgr->ctrl_blk);
  539. return 0;
  540. }
  541. static int daio_imap_add(struct daio_mgr *mgr, struct imapper *entry)
  542. {
  543. unsigned long flags;
  544. int err;
  545. spin_lock_irqsave(&mgr->imap_lock, flags);
  546. if (!entry->addr && mgr->init_imap_added) {
  547. input_mapper_delete(&mgr->imappers, mgr->init_imap,
  548. daio_map_op, mgr);
  549. mgr->init_imap_added = 0;
  550. }
  551. err = input_mapper_add(&mgr->imappers, entry, daio_map_op, mgr);
  552. spin_unlock_irqrestore(&mgr->imap_lock, flags);
  553. return err;
  554. }
  555. static int daio_imap_delete(struct daio_mgr *mgr, struct imapper *entry)
  556. {
  557. unsigned long flags;
  558. int err;
  559. spin_lock_irqsave(&mgr->imap_lock, flags);
  560. err = input_mapper_delete(&mgr->imappers, entry, daio_map_op, mgr);
  561. if (list_empty(&mgr->imappers)) {
  562. input_mapper_add(&mgr->imappers, mgr->init_imap,
  563. daio_map_op, mgr);
  564. mgr->init_imap_added = 1;
  565. }
  566. spin_unlock_irqrestore(&mgr->imap_lock, flags);
  567. return err;
  568. }
  569. static int daio_mgr_commit_write(struct daio_mgr *mgr)
  570. {
  571. struct hw *hw = mgr->mgr.hw;
  572. hw->daio_mgr_commit_write(hw, mgr->mgr.ctrl_blk);
  573. return 0;
  574. }
  575. int daio_mgr_create(struct hw *hw, struct daio_mgr **rdaio_mgr)
  576. {
  577. int err, i;
  578. struct daio_mgr *daio_mgr;
  579. struct imapper *entry;
  580. *rdaio_mgr = NULL;
  581. daio_mgr = kzalloc(sizeof(*daio_mgr), GFP_KERNEL);
  582. if (!daio_mgr)
  583. return -ENOMEM;
  584. err = rsc_mgr_init(&daio_mgr->mgr, DAIO, NUM_DAIOTYP, hw);
  585. if (err)
  586. goto error1;
  587. spin_lock_init(&daio_mgr->mgr_lock);
  588. spin_lock_init(&daio_mgr->imap_lock);
  589. INIT_LIST_HEAD(&daio_mgr->imappers);
  590. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  591. if (!entry) {
  592. err = -ENOMEM;
  593. goto error2;
  594. }
  595. entry->slot = entry->addr = entry->next = entry->user = 0;
  596. list_add(&entry->list, &daio_mgr->imappers);
  597. daio_mgr->init_imap = entry;
  598. daio_mgr->init_imap_added = 1;
  599. daio_mgr->get_daio = get_daio_rsc;
  600. daio_mgr->put_daio = put_daio_rsc;
  601. daio_mgr->daio_enable = daio_mgr_enb_daio;
  602. daio_mgr->daio_disable = daio_mgr_dsb_daio;
  603. daio_mgr->imap_add = daio_imap_add;
  604. daio_mgr->imap_delete = daio_imap_delete;
  605. daio_mgr->commit_write = daio_mgr_commit_write;
  606. daio_mgr->card = hw->card;
  607. for (i = 0; i < 8; i++) {
  608. hw->daio_mgr_dsb_dao(daio_mgr->mgr.ctrl_blk, i);
  609. hw->daio_mgr_dsb_dai(daio_mgr->mgr.ctrl_blk, i);
  610. }
  611. hw->daio_mgr_commit_write(hw, daio_mgr->mgr.ctrl_blk);
  612. *rdaio_mgr = daio_mgr;
  613. return 0;
  614. error2:
  615. rsc_mgr_uninit(&daio_mgr->mgr);
  616. error1:
  617. kfree(daio_mgr);
  618. return err;
  619. }
  620. int daio_mgr_destroy(struct daio_mgr *daio_mgr)
  621. {
  622. unsigned long flags;
  623. /* free daio input mapper list */
  624. spin_lock_irqsave(&daio_mgr->imap_lock, flags);
  625. free_input_mapper_list(&daio_mgr->imappers);
  626. spin_unlock_irqrestore(&daio_mgr->imap_lock, flags);
  627. rsc_mgr_uninit(&daio_mgr->mgr);
  628. kfree(daio_mgr);
  629. return 0;
  630. }