123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888 |
- /**
- * 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 ctsrc.c
- *
- * @Brief
- * This file contains the implementation of the Sample Rate Convertor
- * resource management object.
- *
- * @Author Liu Chun
- * @Date May 13 2008
- *
- */
- #include "ctsrc.h"
- #include "cthardware.h"
- #include <linux/slab.h>
- #define SRC_RESOURCE_NUM 256
- #define SRCIMP_RESOURCE_NUM 256
- static unsigned int conj_mask;
- static int src_default_config_memrd(struct src *src);
- static int src_default_config_memwr(struct src *src);
- static int src_default_config_arcrw(struct src *src);
- static int (*src_default_config[3])(struct src *) = {
- [MEMRD] = src_default_config_memrd,
- [MEMWR] = src_default_config_memwr,
- [ARCRW] = src_default_config_arcrw
- };
- static int src_set_state(struct src *src, unsigned int state)
- {
- struct hw *hw;
- hw = src->rsc.hw;
- hw->src_set_state(src->rsc.ctrl_blk, state);
- return 0;
- }
- static int src_set_bm(struct src *src, unsigned int bm)
- {
- struct hw *hw;
- hw = src->rsc.hw;
- hw->src_set_bm(src->rsc.ctrl_blk, bm);
- return 0;
- }
- static int src_set_sf(struct src *src, unsigned int sf)
- {
- struct hw *hw;
- hw = src->rsc.hw;
- hw->src_set_sf(src->rsc.ctrl_blk, sf);
- return 0;
- }
- static int src_set_pm(struct src *src, unsigned int pm)
- {
- struct hw *hw;
- hw = src->rsc.hw;
- hw->src_set_pm(src->rsc.ctrl_blk, pm);
- return 0;
- }
- static int src_set_rom(struct src *src, unsigned int rom)
- {
- struct hw *hw;
- hw = src->rsc.hw;
- hw->src_set_rom(src->rsc.ctrl_blk, rom);
- return 0;
- }
- static int src_set_vo(struct src *src, unsigned int vo)
- {
- struct hw *hw;
- hw = src->rsc.hw;
- hw->src_set_vo(src->rsc.ctrl_blk, vo);
- return 0;
- }
- static int src_set_st(struct src *src, unsigned int st)
- {
- struct hw *hw;
- hw = src->rsc.hw;
- hw->src_set_st(src->rsc.ctrl_blk, st);
- return 0;
- }
- static int src_set_bp(struct src *src, unsigned int bp)
- {
- struct hw *hw;
- hw = src->rsc.hw;
- hw->src_set_bp(src->rsc.ctrl_blk, bp);
- return 0;
- }
- static int src_set_cisz(struct src *src, unsigned int cisz)
- {
- struct hw *hw;
- hw = src->rsc.hw;
- hw->src_set_cisz(src->rsc.ctrl_blk, cisz);
- return 0;
- }
- static int src_set_ca(struct src *src, unsigned int ca)
- {
- struct hw *hw;
- hw = src->rsc.hw;
- hw->src_set_ca(src->rsc.ctrl_blk, ca);
- return 0;
- }
- static int src_set_sa(struct src *src, unsigned int sa)
- {
- struct hw *hw;
- hw = src->rsc.hw;
- hw->src_set_sa(src->rsc.ctrl_blk, sa);
- return 0;
- }
- static int src_set_la(struct src *src, unsigned int la)
- {
- struct hw *hw;
- hw = src->rsc.hw;
- hw->src_set_la(src->rsc.ctrl_blk, la);
- return 0;
- }
- static int src_set_pitch(struct src *src, unsigned int pitch)
- {
- struct hw *hw;
- hw = src->rsc.hw;
- hw->src_set_pitch(src->rsc.ctrl_blk, pitch);
- return 0;
- }
- static int src_set_clear_zbufs(struct src *src)
- {
- struct hw *hw;
- hw = src->rsc.hw;
- hw->src_set_clear_zbufs(src->rsc.ctrl_blk, 1);
- return 0;
- }
- static int src_commit_write(struct src *src)
- {
- struct hw *hw;
- int i;
- unsigned int dirty = 0;
- hw = src->rsc.hw;
- src->rsc.ops->master(&src->rsc);
- if (src->rsc.msr > 1) {
- /* Save dirty flags for conjugate resource programming */
- dirty = hw->src_get_dirty(src->rsc.ctrl_blk) & conj_mask;
- }
- hw->src_commit_write(hw, src->rsc.ops->index(&src->rsc),
- src->rsc.ctrl_blk);
- /* Program conjugate parameter mixer resources */
- if (MEMWR == src->mode)
- return 0;
- for (i = 1; i < src->rsc.msr; i++) {
- src->rsc.ops->next_conj(&src->rsc);
- hw->src_set_dirty(src->rsc.ctrl_blk, dirty);
- hw->src_commit_write(hw, src->rsc.ops->index(&src->rsc),
- src->rsc.ctrl_blk);
- }
- src->rsc.ops->master(&src->rsc);
- return 0;
- }
- static int src_get_ca(struct src *src)
- {
- struct hw *hw;
- hw = src->rsc.hw;
- return hw->src_get_ca(hw, src->rsc.ops->index(&src->rsc),
- src->rsc.ctrl_blk);
- }
- static int src_init(struct src *src)
- {
- src_default_config[src->mode](src);
- return 0;
- }
- static struct src *src_next_interleave(struct src *src)
- {
- return src->intlv;
- }
- static int src_default_config_memrd(struct src *src)
- {
- struct hw *hw = src->rsc.hw;
- unsigned int rsr, msr;
- hw->src_set_state(src->rsc.ctrl_blk, SRC_STATE_OFF);
- hw->src_set_bm(src->rsc.ctrl_blk, 1);
- for (rsr = 0, msr = src->rsc.msr; msr > 1; msr >>= 1)
- rsr++;
- hw->src_set_rsr(src->rsc.ctrl_blk, rsr);
- hw->src_set_sf(src->rsc.ctrl_blk, SRC_SF_S16);
- hw->src_set_wr(src->rsc.ctrl_blk, 0);
- hw->src_set_pm(src->rsc.ctrl_blk, 0);
- hw->src_set_rom(src->rsc.ctrl_blk, 0);
- hw->src_set_vo(src->rsc.ctrl_blk, 0);
- hw->src_set_st(src->rsc.ctrl_blk, 0);
- hw->src_set_ilsz(src->rsc.ctrl_blk, src->multi - 1);
- hw->src_set_cisz(src->rsc.ctrl_blk, 0x80);
- hw->src_set_sa(src->rsc.ctrl_blk, 0x0);
- hw->src_set_la(src->rsc.ctrl_blk, 0x1000);
- hw->src_set_ca(src->rsc.ctrl_blk, 0x80);
- hw->src_set_pitch(src->rsc.ctrl_blk, 0x1000000);
- hw->src_set_clear_zbufs(src->rsc.ctrl_blk, 1);
- src->rsc.ops->master(&src->rsc);
- hw->src_commit_write(hw, src->rsc.ops->index(&src->rsc),
- src->rsc.ctrl_blk);
- for (msr = 1; msr < src->rsc.msr; msr++) {
- src->rsc.ops->next_conj(&src->rsc);
- hw->src_set_pitch(src->rsc.ctrl_blk, 0x1000000);
- hw->src_commit_write(hw, src->rsc.ops->index(&src->rsc),
- src->rsc.ctrl_blk);
- }
- src->rsc.ops->master(&src->rsc);
- return 0;
- }
- static int src_default_config_memwr(struct src *src)
- {
- struct hw *hw = src->rsc.hw;
- hw->src_set_state(src->rsc.ctrl_blk, SRC_STATE_OFF);
- hw->src_set_bm(src->rsc.ctrl_blk, 1);
- hw->src_set_rsr(src->rsc.ctrl_blk, 0);
- hw->src_set_sf(src->rsc.ctrl_blk, SRC_SF_S16);
- hw->src_set_wr(src->rsc.ctrl_blk, 1);
- hw->src_set_pm(src->rsc.ctrl_blk, 0);
- hw->src_set_rom(src->rsc.ctrl_blk, 0);
- hw->src_set_vo(src->rsc.ctrl_blk, 0);
- hw->src_set_st(src->rsc.ctrl_blk, 0);
- hw->src_set_ilsz(src->rsc.ctrl_blk, 0);
- hw->src_set_cisz(src->rsc.ctrl_blk, 0x80);
- hw->src_set_sa(src->rsc.ctrl_blk, 0x0);
- hw->src_set_la(src->rsc.ctrl_blk, 0x1000);
- hw->src_set_ca(src->rsc.ctrl_blk, 0x80);
- hw->src_set_pitch(src->rsc.ctrl_blk, 0x1000000);
- hw->src_set_clear_zbufs(src->rsc.ctrl_blk, 1);
- src->rsc.ops->master(&src->rsc);
- hw->src_commit_write(hw, src->rsc.ops->index(&src->rsc),
- src->rsc.ctrl_blk);
- return 0;
- }
- static int src_default_config_arcrw(struct src *src)
- {
- struct hw *hw = src->rsc.hw;
- unsigned int rsr, msr;
- unsigned int dirty;
- hw->src_set_state(src->rsc.ctrl_blk, SRC_STATE_OFF);
- hw->src_set_bm(src->rsc.ctrl_blk, 0);
- for (rsr = 0, msr = src->rsc.msr; msr > 1; msr >>= 1)
- rsr++;
- hw->src_set_rsr(src->rsc.ctrl_blk, rsr);
- hw->src_set_sf(src->rsc.ctrl_blk, SRC_SF_F32);
- hw->src_set_wr(src->rsc.ctrl_blk, 0);
- hw->src_set_pm(src->rsc.ctrl_blk, 0);
- hw->src_set_rom(src->rsc.ctrl_blk, 0);
- hw->src_set_vo(src->rsc.ctrl_blk, 0);
- hw->src_set_st(src->rsc.ctrl_blk, 0);
- hw->src_set_ilsz(src->rsc.ctrl_blk, 0);
- hw->src_set_cisz(src->rsc.ctrl_blk, 0x80);
- hw->src_set_sa(src->rsc.ctrl_blk, 0x0);
- /*hw->src_set_sa(src->rsc.ctrl_blk, 0x100);*/
- hw->src_set_la(src->rsc.ctrl_blk, 0x1000);
- /*hw->src_set_la(src->rsc.ctrl_blk, 0x03ffffe0);*/
- hw->src_set_ca(src->rsc.ctrl_blk, 0x80);
- hw->src_set_pitch(src->rsc.ctrl_blk, 0x1000000);
- hw->src_set_clear_zbufs(src->rsc.ctrl_blk, 1);
- dirty = hw->src_get_dirty(src->rsc.ctrl_blk);
- src->rsc.ops->master(&src->rsc);
- for (msr = 0; msr < src->rsc.msr; msr++) {
- hw->src_set_dirty(src->rsc.ctrl_blk, dirty);
- hw->src_commit_write(hw, src->rsc.ops->index(&src->rsc),
- src->rsc.ctrl_blk);
- src->rsc.ops->next_conj(&src->rsc);
- }
- src->rsc.ops->master(&src->rsc);
- return 0;
- }
- static const struct src_rsc_ops src_rsc_ops = {
- .set_state = src_set_state,
- .set_bm = src_set_bm,
- .set_sf = src_set_sf,
- .set_pm = src_set_pm,
- .set_rom = src_set_rom,
- .set_vo = src_set_vo,
- .set_st = src_set_st,
- .set_bp = src_set_bp,
- .set_cisz = src_set_cisz,
- .set_ca = src_set_ca,
- .set_sa = src_set_sa,
- .set_la = src_set_la,
- .set_pitch = src_set_pitch,
- .set_clr_zbufs = src_set_clear_zbufs,
- .commit_write = src_commit_write,
- .get_ca = src_get_ca,
- .init = src_init,
- .next_interleave = src_next_interleave,
- };
- static int
- src_rsc_init(struct src *src, u32 idx,
- const struct src_desc *desc, struct src_mgr *mgr)
- {
- int err;
- int i, n;
- struct src *p;
- n = (MEMRD == desc->mode) ? desc->multi : 1;
- for (i = 0, p = src; i < n; i++, p++) {
- err = rsc_init(&p->rsc, idx + i, SRC, desc->msr, mgr->mgr.hw);
- if (err)
- goto error1;
- /* Initialize src specific rsc operations */
- p->ops = &src_rsc_ops;
- p->multi = (0 == i) ? desc->multi : 1;
- p->mode = desc->mode;
- src_default_config[desc->mode](p);
- mgr->src_enable(mgr, p);
- p->intlv = p + 1;
- }
- (--p)->intlv = NULL; /* Set @intlv of the last SRC to NULL */
- mgr->commit_write(mgr);
- return 0;
- error1:
- for (i--, p--; i >= 0; i--, p--) {
- mgr->src_disable(mgr, p);
- rsc_uninit(&p->rsc);
- }
- mgr->commit_write(mgr);
- return err;
- }
- static int src_rsc_uninit(struct src *src, struct src_mgr *mgr)
- {
- int i, n;
- struct src *p;
- n = (MEMRD == src->mode) ? src->multi : 1;
- for (i = 0, p = src; i < n; i++, p++) {
- mgr->src_disable(mgr, p);
- rsc_uninit(&p->rsc);
- p->multi = 0;
- p->ops = NULL;
- p->mode = NUM_SRCMODES;
- p->intlv = NULL;
- }
- mgr->commit_write(mgr);
- return 0;
- }
- static int
- get_src_rsc(struct src_mgr *mgr, const struct src_desc *desc, struct src **rsrc)
- {
- unsigned int idx = SRC_RESOURCE_NUM;
- int err;
- struct src *src;
- unsigned long flags;
- *rsrc = NULL;
- /* Check whether there are sufficient src resources to meet request. */
- spin_lock_irqsave(&mgr->mgr_lock, flags);
- if (MEMRD == desc->mode)
- err = mgr_get_resource(&mgr->mgr, desc->multi, &idx);
- else
- err = mgr_get_resource(&mgr->mgr, 1, &idx);
- spin_unlock_irqrestore(&mgr->mgr_lock, flags);
- if (err) {
- dev_err(mgr->card->dev,
- "Can't meet SRC resource request!\n");
- return err;
- }
- /* Allocate mem for master src resource */
- if (MEMRD == desc->mode)
- src = kcalloc(desc->multi, sizeof(*src), GFP_KERNEL);
- else
- src = kzalloc(sizeof(*src), GFP_KERNEL);
- if (!src) {
- err = -ENOMEM;
- goto error1;
- }
- err = src_rsc_init(src, idx, desc, mgr);
- if (err)
- goto error2;
- *rsrc = src;
- return 0;
- error2:
- kfree(src);
- error1:
- spin_lock_irqsave(&mgr->mgr_lock, flags);
- if (MEMRD == desc->mode)
- mgr_put_resource(&mgr->mgr, desc->multi, idx);
- else
- mgr_put_resource(&mgr->mgr, 1, idx);
- spin_unlock_irqrestore(&mgr->mgr_lock, flags);
- return err;
- }
- static int put_src_rsc(struct src_mgr *mgr, struct src *src)
- {
- unsigned long flags;
- spin_lock_irqsave(&mgr->mgr_lock, flags);
- src->rsc.ops->master(&src->rsc);
- if (MEMRD == src->mode)
- mgr_put_resource(&mgr->mgr, src->multi,
- src->rsc.ops->index(&src->rsc));
- else
- mgr_put_resource(&mgr->mgr, 1, src->rsc.ops->index(&src->rsc));
- spin_unlock_irqrestore(&mgr->mgr_lock, flags);
- src_rsc_uninit(src, mgr);
- kfree(src);
- return 0;
- }
- static int src_enable_s(struct src_mgr *mgr, struct src *src)
- {
- struct hw *hw = mgr->mgr.hw;
- int i;
- src->rsc.ops->master(&src->rsc);
- for (i = 0; i < src->rsc.msr; i++) {
- hw->src_mgr_enbs_src(mgr->mgr.ctrl_blk,
- src->rsc.ops->index(&src->rsc));
- src->rsc.ops->next_conj(&src->rsc);
- }
- src->rsc.ops->master(&src->rsc);
- return 0;
- }
- static int src_enable(struct src_mgr *mgr, struct src *src)
- {
- struct hw *hw = mgr->mgr.hw;
- int i;
- src->rsc.ops->master(&src->rsc);
- for (i = 0; i < src->rsc.msr; i++) {
- hw->src_mgr_enb_src(mgr->mgr.ctrl_blk,
- src->rsc.ops->index(&src->rsc));
- src->rsc.ops->next_conj(&src->rsc);
- }
- src->rsc.ops->master(&src->rsc);
- return 0;
- }
- static int src_disable(struct src_mgr *mgr, struct src *src)
- {
- struct hw *hw = mgr->mgr.hw;
- int i;
- src->rsc.ops->master(&src->rsc);
- for (i = 0; i < src->rsc.msr; i++) {
- hw->src_mgr_dsb_src(mgr->mgr.ctrl_blk,
- src->rsc.ops->index(&src->rsc));
- src->rsc.ops->next_conj(&src->rsc);
- }
- src->rsc.ops->master(&src->rsc);
- return 0;
- }
- static int src_mgr_commit_write(struct src_mgr *mgr)
- {
- struct hw *hw = mgr->mgr.hw;
- hw->src_mgr_commit_write(hw, mgr->mgr.ctrl_blk);
- return 0;
- }
- int src_mgr_create(struct hw *hw, struct src_mgr **rsrc_mgr)
- {
- int err, i;
- struct src_mgr *src_mgr;
- *rsrc_mgr = NULL;
- src_mgr = kzalloc(sizeof(*src_mgr), GFP_KERNEL);
- if (!src_mgr)
- return -ENOMEM;
- err = rsc_mgr_init(&src_mgr->mgr, SRC, SRC_RESOURCE_NUM, hw);
- if (err)
- goto error1;
- spin_lock_init(&src_mgr->mgr_lock);
- conj_mask = hw->src_dirty_conj_mask();
- src_mgr->get_src = get_src_rsc;
- src_mgr->put_src = put_src_rsc;
- src_mgr->src_enable_s = src_enable_s;
- src_mgr->src_enable = src_enable;
- src_mgr->src_disable = src_disable;
- src_mgr->commit_write = src_mgr_commit_write;
- src_mgr->card = hw->card;
- /* Disable all SRC resources. */
- for (i = 0; i < 256; i++)
- hw->src_mgr_dsb_src(src_mgr->mgr.ctrl_blk, i);
- hw->src_mgr_commit_write(hw, src_mgr->mgr.ctrl_blk);
- *rsrc_mgr = src_mgr;
- return 0;
- error1:
- kfree(src_mgr);
- return err;
- }
- int src_mgr_destroy(struct src_mgr *src_mgr)
- {
- rsc_mgr_uninit(&src_mgr->mgr);
- kfree(src_mgr);
- return 0;
- }
- /* SRCIMP resource manager operations */
- static int srcimp_master(struct rsc *rsc)
- {
- rsc->conj = 0;
- return rsc->idx = container_of(rsc, struct srcimp, rsc)->idx[0];
- }
- static int srcimp_next_conj(struct rsc *rsc)
- {
- rsc->conj++;
- return container_of(rsc, struct srcimp, rsc)->idx[rsc->conj];
- }
- static int srcimp_index(const struct rsc *rsc)
- {
- return container_of(rsc, struct srcimp, rsc)->idx[rsc->conj];
- }
- static const struct rsc_ops srcimp_basic_rsc_ops = {
- .master = srcimp_master,
- .next_conj = srcimp_next_conj,
- .index = srcimp_index,
- .output_slot = NULL,
- };
- static int srcimp_map(struct srcimp *srcimp, struct src *src, struct rsc *input)
- {
- struct imapper *entry;
- int i;
- srcimp->rsc.ops->master(&srcimp->rsc);
- src->rsc.ops->master(&src->rsc);
- input->ops->master(input);
- /* Program master and conjugate resources */
- for (i = 0; i < srcimp->rsc.msr; i++) {
- entry = &srcimp->imappers[i];
- entry->slot = input->ops->output_slot(input);
- entry->user = src->rsc.ops->index(&src->rsc);
- entry->addr = srcimp->rsc.ops->index(&srcimp->rsc);
- srcimp->mgr->imap_add(srcimp->mgr, entry);
- srcimp->mapped |= (0x1 << i);
- srcimp->rsc.ops->next_conj(&srcimp->rsc);
- input->ops->next_conj(input);
- }
- srcimp->rsc.ops->master(&srcimp->rsc);
- input->ops->master(input);
- return 0;
- }
- static int srcimp_unmap(struct srcimp *srcimp)
- {
- int i;
- /* Program master and conjugate resources */
- for (i = 0; i < srcimp->rsc.msr; i++) {
- if (srcimp->mapped & (0x1 << i)) {
- srcimp->mgr->imap_delete(srcimp->mgr,
- &srcimp->imappers[i]);
- srcimp->mapped &= ~(0x1 << i);
- }
- }
- return 0;
- }
- static const struct srcimp_rsc_ops srcimp_ops = {
- .map = srcimp_map,
- .unmap = srcimp_unmap
- };
- static int srcimp_rsc_init(struct srcimp *srcimp,
- const struct srcimp_desc *desc,
- struct srcimp_mgr *mgr)
- {
- int err;
- err = rsc_init(&srcimp->rsc, srcimp->idx[0],
- SRCIMP, desc->msr, mgr->mgr.hw);
- if (err)
- return err;
- /* Reserve memory for imapper nodes */
- srcimp->imappers = kzalloc(sizeof(struct imapper)*desc->msr,
- GFP_KERNEL);
- if (!srcimp->imappers) {
- err = -ENOMEM;
- goto error1;
- }
- /* Set srcimp specific operations */
- srcimp->rsc.ops = &srcimp_basic_rsc_ops;
- srcimp->ops = &srcimp_ops;
- srcimp->mgr = mgr;
- srcimp->rsc.ops->master(&srcimp->rsc);
- return 0;
- error1:
- rsc_uninit(&srcimp->rsc);
- return err;
- }
- static int srcimp_rsc_uninit(struct srcimp *srcimp)
- {
- kfree(srcimp->imappers);
- srcimp->imappers = NULL;
- srcimp->ops = NULL;
- srcimp->mgr = NULL;
- rsc_uninit(&srcimp->rsc);
- return 0;
- }
- static int get_srcimp_rsc(struct srcimp_mgr *mgr,
- const struct srcimp_desc *desc,
- struct srcimp **rsrcimp)
- {
- int err, i;
- unsigned int idx;
- struct srcimp *srcimp;
- unsigned long flags;
- *rsrcimp = NULL;
- /* Allocate mem for SRCIMP resource */
- srcimp = kzalloc(sizeof(*srcimp), GFP_KERNEL);
- if (!srcimp)
- return -ENOMEM;
- /* Check whether there are sufficient SRCIMP resources. */
- err = 0;
- spin_lock_irqsave(&mgr->mgr_lock, flags);
- for (i = 0; i < desc->msr; i++) {
- err = mgr_get_resource(&mgr->mgr, 1, &idx);
- if (err)
- break;
- srcimp->idx[i] = idx;
- }
- spin_unlock_irqrestore(&mgr->mgr_lock, flags);
- if (err) {
- dev_err(mgr->card->dev,
- "Can't meet SRCIMP resource request!\n");
- goto error1;
- }
- err = srcimp_rsc_init(srcimp, desc, mgr);
- if (err)
- goto error1;
- *rsrcimp = srcimp;
- return 0;
- error1:
- spin_lock_irqsave(&mgr->mgr_lock, flags);
- for (i--; i >= 0; i--)
- mgr_put_resource(&mgr->mgr, 1, srcimp->idx[i]);
- spin_unlock_irqrestore(&mgr->mgr_lock, flags);
- kfree(srcimp);
- return err;
- }
- static int put_srcimp_rsc(struct srcimp_mgr *mgr, struct srcimp *srcimp)
- {
- unsigned long flags;
- int i;
- spin_lock_irqsave(&mgr->mgr_lock, flags);
- for (i = 0; i < srcimp->rsc.msr; i++)
- mgr_put_resource(&mgr->mgr, 1, srcimp->idx[i]);
- spin_unlock_irqrestore(&mgr->mgr_lock, flags);
- srcimp_rsc_uninit(srcimp);
- kfree(srcimp);
- return 0;
- }
- static int srcimp_map_op(void *data, struct imapper *entry)
- {
- struct rsc_mgr *mgr = &((struct srcimp_mgr *)data)->mgr;
- struct hw *hw = mgr->hw;
- hw->srcimp_mgr_set_imaparc(mgr->ctrl_blk, entry->slot);
- hw->srcimp_mgr_set_imapuser(mgr->ctrl_blk, entry->user);
- hw->srcimp_mgr_set_imapnxt(mgr->ctrl_blk, entry->next);
- hw->srcimp_mgr_set_imapaddr(mgr->ctrl_blk, entry->addr);
- hw->srcimp_mgr_commit_write(mgr->hw, mgr->ctrl_blk);
- return 0;
- }
- static int srcimp_imap_add(struct srcimp_mgr *mgr, struct imapper *entry)
- {
- unsigned long flags;
- int err;
- spin_lock_irqsave(&mgr->imap_lock, flags);
- if ((0 == entry->addr) && (mgr->init_imap_added)) {
- input_mapper_delete(&mgr->imappers,
- mgr->init_imap, srcimp_map_op, mgr);
- mgr->init_imap_added = 0;
- }
- err = input_mapper_add(&mgr->imappers, entry, srcimp_map_op, mgr);
- spin_unlock_irqrestore(&mgr->imap_lock, flags);
- return err;
- }
- static int srcimp_imap_delete(struct srcimp_mgr *mgr, struct imapper *entry)
- {
- unsigned long flags;
- int err;
- spin_lock_irqsave(&mgr->imap_lock, flags);
- err = input_mapper_delete(&mgr->imappers, entry, srcimp_map_op, mgr);
- if (list_empty(&mgr->imappers)) {
- input_mapper_add(&mgr->imappers, mgr->init_imap,
- srcimp_map_op, mgr);
- mgr->init_imap_added = 1;
- }
- spin_unlock_irqrestore(&mgr->imap_lock, flags);
- return err;
- }
- int srcimp_mgr_create(struct hw *hw, struct srcimp_mgr **rsrcimp_mgr)
- {
- int err;
- struct srcimp_mgr *srcimp_mgr;
- struct imapper *entry;
- *rsrcimp_mgr = NULL;
- srcimp_mgr = kzalloc(sizeof(*srcimp_mgr), GFP_KERNEL);
- if (!srcimp_mgr)
- return -ENOMEM;
- err = rsc_mgr_init(&srcimp_mgr->mgr, SRCIMP, SRCIMP_RESOURCE_NUM, hw);
- if (err)
- goto error1;
- spin_lock_init(&srcimp_mgr->mgr_lock);
- spin_lock_init(&srcimp_mgr->imap_lock);
- INIT_LIST_HEAD(&srcimp_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, &srcimp_mgr->imappers);
- srcimp_mgr->init_imap = entry;
- srcimp_mgr->init_imap_added = 1;
- srcimp_mgr->get_srcimp = get_srcimp_rsc;
- srcimp_mgr->put_srcimp = put_srcimp_rsc;
- srcimp_mgr->imap_add = srcimp_imap_add;
- srcimp_mgr->imap_delete = srcimp_imap_delete;
- srcimp_mgr->card = hw->card;
- *rsrcimp_mgr = srcimp_mgr;
- return 0;
- error2:
- rsc_mgr_uninit(&srcimp_mgr->mgr);
- error1:
- kfree(srcimp_mgr);
- return err;
- }
- int srcimp_mgr_destroy(struct srcimp_mgr *srcimp_mgr)
- {
- unsigned long flags;
- /* free src input mapper list */
- spin_lock_irqsave(&srcimp_mgr->imap_lock, flags);
- free_input_mapper_list(&srcimp_mgr->imappers);
- spin_unlock_irqrestore(&srcimp_mgr->imap_lock, flags);
- rsc_mgr_uninit(&srcimp_mgr->mgr);
- kfree(srcimp_mgr);
- return 0;
- }
|