GNU Linux-libre 4.9.328-gnu1
[releases.git] / sound / pci / ctxfi / ctresource.c
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        ctresource.c
9  *
10  * @Brief
11  * This file contains the implementation of some generic helper functions.
12  *
13  * @Author      Liu Chun
14  * @Date        May 15 2008
15  *
16  */
17
18 #include "ctresource.h"
19 #include "cthardware.h"
20 #include <linux/err.h>
21 #include <linux/slab.h>
22
23 #define AUDIO_SLOT_BLOCK_NUM    256
24
25 /* Resource allocation based on bit-map management mechanism */
26 static int
27 get_resource(u8 *rscs, unsigned int amount,
28              unsigned int multi, unsigned int *ridx)
29 {
30         int i, j, k, n;
31
32         /* Check whether there are sufficient resources to meet request. */
33         for (i = 0, n = multi; i < amount; i++) {
34                 j = i / 8;
35                 k = i % 8;
36                 if (rscs[j] & ((u8)1 << k)) {
37                         n = multi;
38                         continue;
39                 }
40                 if (!(--n))
41                         break; /* found sufficient contiguous resources */
42         }
43
44         if (i >= amount) {
45                 /* Can not find sufficient contiguous resources */
46                 return -ENOENT;
47         }
48
49         /* Mark the contiguous bits in resource bit-map as used */
50         for (n = multi; n > 0; n--) {
51                 j = i / 8;
52                 k = i % 8;
53                 rscs[j] |= ((u8)1 << k);
54                 i--;
55         }
56
57         *ridx = i + 1;
58
59         return 0;
60 }
61
62 static int put_resource(u8 *rscs, unsigned int multi, unsigned int idx)
63 {
64         unsigned int i, j, k, n;
65
66         /* Mark the contiguous bits in resource bit-map as used */
67         for (n = multi, i = idx; n > 0; n--) {
68                 j = i / 8;
69                 k = i % 8;
70                 rscs[j] &= ~((u8)1 << k);
71                 i++;
72         }
73
74         return 0;
75 }
76
77 int mgr_get_resource(struct rsc_mgr *mgr, unsigned int n, unsigned int *ridx)
78 {
79         int err;
80
81         if (n > mgr->avail)
82                 return -ENOENT;
83
84         err = get_resource(mgr->rscs, mgr->amount, n, ridx);
85         if (!err)
86                 mgr->avail -= n;
87
88         return err;
89 }
90
91 int mgr_put_resource(struct rsc_mgr *mgr, unsigned int n, unsigned int idx)
92 {
93         put_resource(mgr->rscs, n, idx);
94         mgr->avail += n;
95
96         return 0;
97 }
98
99 static unsigned char offset_in_audio_slot_block[NUM_RSCTYP] = {
100         /* SRC channel is at Audio Ring slot 1 every 16 slots. */
101         [SRC]           = 0x1,
102         [AMIXER]        = 0x4,
103         [SUM]           = 0xc,
104 };
105
106 static int rsc_index(const struct rsc *rsc)
107 {
108     return rsc->conj;
109 }
110
111 static int audio_ring_slot(const struct rsc *rsc)
112 {
113     return (rsc->conj << 4) + offset_in_audio_slot_block[rsc->type];
114 }
115
116 static void rsc_next_conj(struct rsc *rsc)
117 {
118         unsigned int i;
119         for (i = 0; (i < 8) && (!(rsc->msr & (0x1 << i))); )
120                 i++;
121         rsc->conj += (AUDIO_SLOT_BLOCK_NUM >> i);
122 }
123
124 static void rsc_master(struct rsc *rsc)
125 {
126         rsc->conj = rsc->idx;
127 }
128
129 static const struct rsc_ops rsc_generic_ops = {
130         .index          = rsc_index,
131         .output_slot    = audio_ring_slot,
132         .master         = rsc_master,
133         .next_conj      = rsc_next_conj,
134 };
135
136 int
137 rsc_init(struct rsc *rsc, u32 idx, enum RSCTYP type, u32 msr, struct hw *hw)
138 {
139         int err = 0;
140
141         rsc->idx = idx;
142         rsc->conj = idx;
143         rsc->type = type;
144         rsc->msr = msr;
145         rsc->hw = hw;
146         rsc->ops = &rsc_generic_ops;
147         if (!hw) {
148                 rsc->ctrl_blk = NULL;
149                 return 0;
150         }
151
152         switch (type) {
153         case SRC:
154                 err = hw->src_rsc_get_ctrl_blk(&rsc->ctrl_blk);
155                 break;
156         case AMIXER:
157                 err = hw->amixer_rsc_get_ctrl_blk(&rsc->ctrl_blk);
158                 break;
159         case SRCIMP:
160         case SUM:
161         case DAIO:
162                 break;
163         default:
164                 dev_err(((struct hw *)hw)->card->dev,
165                         "Invalid resource type value %d!\n", type);
166                 return -EINVAL;
167         }
168
169         if (err) {
170                 dev_err(((struct hw *)hw)->card->dev,
171                         "Failed to get resource control block!\n");
172                 return err;
173         }
174
175         return 0;
176 }
177
178 int rsc_uninit(struct rsc *rsc)
179 {
180         if ((NULL != rsc->hw) && (NULL != rsc->ctrl_blk)) {
181                 switch (rsc->type) {
182                 case SRC:
183                         rsc->hw->src_rsc_put_ctrl_blk(rsc->ctrl_blk);
184                         break;
185                 case AMIXER:
186                         rsc->hw->amixer_rsc_put_ctrl_blk(rsc->ctrl_blk);
187                         break;
188                 case SUM:
189                 case DAIO:
190                         break;
191                 default:
192                         dev_err(((struct hw *)rsc->hw)->card->dev,
193                                 "Invalid resource type value %d!\n",
194                                 rsc->type);
195                         break;
196                 }
197
198                 rsc->hw = rsc->ctrl_blk = NULL;
199         }
200
201         rsc->idx = rsc->conj = 0;
202         rsc->type = NUM_RSCTYP;
203         rsc->msr = 0;
204
205         return 0;
206 }
207
208 int rsc_mgr_init(struct rsc_mgr *mgr, enum RSCTYP type,
209                  unsigned int amount, struct hw *hw)
210 {
211         int err = 0;
212
213         mgr->type = NUM_RSCTYP;
214
215         mgr->rscs = kzalloc(((amount + 8 - 1) / 8), GFP_KERNEL);
216         if (!mgr->rscs)
217                 return -ENOMEM;
218
219         switch (type) {
220         case SRC:
221                 err = hw->src_mgr_get_ctrl_blk(&mgr->ctrl_blk);
222                 break;
223         case SRCIMP:
224                 err = hw->srcimp_mgr_get_ctrl_blk(&mgr->ctrl_blk);
225                 break;
226         case AMIXER:
227                 err = hw->amixer_mgr_get_ctrl_blk(&mgr->ctrl_blk);
228                 break;
229         case DAIO:
230                 err = hw->daio_mgr_get_ctrl_blk(hw, &mgr->ctrl_blk);
231                 break;
232         case SUM:
233                 break;
234         default:
235                 dev_err(hw->card->dev,
236                         "Invalid resource type value %d!\n", type);
237                 err = -EINVAL;
238                 goto error;
239         }
240
241         if (err) {
242                 dev_err(hw->card->dev,
243                         "Failed to get manager control block!\n");
244                 goto error;
245         }
246
247         mgr->type = type;
248         mgr->avail = mgr->amount = amount;
249         mgr->hw = hw;
250
251         return 0;
252
253 error:
254         kfree(mgr->rscs);
255         return err;
256 }
257
258 int rsc_mgr_uninit(struct rsc_mgr *mgr)
259 {
260         if (NULL != mgr->rscs) {
261                 kfree(mgr->rscs);
262                 mgr->rscs = NULL;
263         }
264
265         if ((NULL != mgr->hw) && (NULL != mgr->ctrl_blk)) {
266                 switch (mgr->type) {
267                 case SRC:
268                         mgr->hw->src_mgr_put_ctrl_blk(mgr->ctrl_blk);
269                         break;
270                 case SRCIMP:
271                         mgr->hw->srcimp_mgr_put_ctrl_blk(mgr->ctrl_blk);
272                         break;
273                 case AMIXER:
274                         mgr->hw->amixer_mgr_put_ctrl_blk(mgr->ctrl_blk);
275                         break;
276                 case DAIO:
277                         mgr->hw->daio_mgr_put_ctrl_blk(mgr->ctrl_blk);
278                         break;
279                 case SUM:
280                         break;
281                 default:
282                         dev_err(((struct hw *)mgr->hw)->card->dev,
283                                 "Invalid resource type value %d!\n",
284                                 mgr->type);
285                         break;
286                 }
287
288                 mgr->hw = mgr->ctrl_blk = NULL;
289         }
290
291         mgr->type = NUM_RSCTYP;
292         mgr->avail = mgr->amount = 0;
293
294         return 0;
295 }