summaryrefslogtreecommitdiffstats
path: root/exynos/multimedia/codecs/audio/exynos4/srp/ulp/src/srp_api.c
blob: b0c0e5eb020d69092c38fbe6e0d765cced530e60 (plain)
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
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/time.h>
#include <fcntl.h>
#include <ctype.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <stdio.h>

#include "srp_api.h"

#define LOG_TAG "libsrpapi"
#include <cutils/log.h>

/* Disable LOGD message */
#ifdef LOGD
#undef LOGD
#endif
#define LOGD(...)

//#define _USE_WBUF_            /* Buffering before writing srp-rp device */
//#define _DUMP_TO_FILE_
//#define _USE_FW_FROM_DISK_

#ifdef _USE_WBUF_
#define WBUF_LEN_MUL        2
#endif

static int srp_dev = -1;
static int srp_ibuf_size = 0;
static int srp_block_mode = SRP_INIT_BLOCK_MODE;

static unsigned char *wbuf;
static int wbuf_size;
static int wbuf_pos;

#ifdef _DUMP_TO_FILE_
static FILE *fp_dump = NULL;
#endif

#ifdef _USE_WBUF_
static int WriteBuff_Init(void)
{
    if (wbuf == NULL) {
        wbuf_size = srp_ibuf_size * WBUF_LEN_MUL;
        wbuf_pos = 0;
        wbuf = (unsigned char *)malloc(wbuf_size);
        LOGD("%s: WriteBuffer %dbytes allocated", __func__, wbuf_size);
        return 0;
    }

    LOGE("%s: WriteBuffer already allocated", __func__);
    return -1;
}

static int WriteBuff_Deinit(void)
{
    if (wbuf != NULL) {
        free(wbuf);
        wbuf = NULL;
        return 0;
    }

    LOGE("%s: WriteBuffer is not ready", __func__);
    return -1;
}

static int WriteBuff_Write(unsigned char *buff, int size_byte)
{
    int write_byte;

    if ((wbuf_pos + size_byte) < wbuf_size) {
        memcpy(&wbuf[wbuf_pos], buff, size_byte);
        wbuf_pos += size_byte;
    } else {
        LOGE("%s: WriteBuffer is filled [%d], ignoring write [%d]", __func__, wbuf_pos, size_byte);
        return -1;    /* Insufficient buffer */
    }

    return wbuf_pos;
}

static void WriteBuff_Consume(void)
{
    memcpy(wbuf, &wbuf[srp_ibuf_size], srp_ibuf_size * (WBUF_LEN_MUL - 1));
    wbuf_pos -= srp_ibuf_size;
}

static void WriteBuff_Flush(void)
{
    wbuf_pos = 0;
}
#endif

int SRP_Create(int block_mode)
{
    if (srp_dev == -1) {
#ifdef _USE_FW_FROM_DISK_
        SRP_Check_AltFirmware();
#endif

        srp_block_mode = block_mode;
        srp_dev = open(SRP_DEV_NAME, O_RDWR |
                    ((block_mode == SRP_INIT_NONBLOCK_MODE) ? O_NDELAY : 0));

        return srp_dev;
    }

    LOGE("%s: Device is not ready", __func__);
    return -1;    /* device alreay opened */
}

int SRP_Init(unsigned int ibuf_size)
{
    int ret;

    if (srp_dev != -1) {
        srp_ibuf_size = ibuf_size;
        ret = ioctl(srp_dev, SRP_INIT, srp_ibuf_size); /* Initialize IBUF size (4KB ~ 18KB) */

#ifdef _DUMP_TO_FILE_
        char outname[256];
        int cnt = 0;

        while (1) {
            sprintf(outname, "/data/rp_dump_%04d.mp3", cnt++);
            if (fp_dump = fopen(outname, "rb")) { /* file exist? */
                fclose(fp_dump);
            } else {
                break;
            }
        }

        LOGD("%s: Dump MP3 to %s", __func__, outname);
        if (fp_dump = fopen(outname, "wb"))
            LOGD("%s: Success to open %s", __func__, outname);
        else
            LOGD("%s: Fail to open %s", __func__, outname);
#endif

#ifdef _USE_WBUF_
        if (ret != -1)
            return WriteBuff_Init();
#else
        return ret;
#endif
    }

    LOGE("%s: Device is not ready", __func__);
    return -1;  /* device is not created */
}

#ifdef _USE_WBUF_
int SRP_Decode(void *buff, int size_byte)
{
    int ret;
    int val;
    int err_code = 0;

    if (srp_dev != -1) {
        /* Check wbuf before writing buff */
        while (wbuf_pos >= srp_ibuf_size) { /* Write_Buffer filled? (IBUF Size)*/
            LOGD("%s: Write Buffer is full, Send data to RP", __func__);

            ret = write(srp_dev, wbuf, srp_ibuf_size); /* Write Buffer to RP Driver */
            if (ret == -1) { /* Fail? */
                ioctl(srp_dev, SRP_ERROR_STATE, &val);
                if (!val) {    /* Write error? */
                    LOGE("%s: IBUF write fail", __func__);
                    return -1;
                } else {       /* Write OK, but RP decode error? */
                    err_code = val;
                    LOGE("%s: RP decode error [0x%05X]", __func__, err_code);
                }
            }
#ifdef _DUMP_TO_FILE_
            if (fp_dump)
                fwrite(wbuf, srp_ibuf_size, 1, fp_dump);
#endif
            WriteBuff_Consume();
        }

        ret = WriteBuff_Write((unsigned char *)buff, size_byte);
        if (ret == -1)
            return -1;  /* Buffering error */

        LOGD("%s: Write Buffer remain [%d]", __func__, wbuf_pos);
        return err_code;  /* Write Success */
    }

    LOGE("%s: Device is not ready", __func__);
    return -1;  /* device is not created */
}

int SRP_Send_EOS(void)
{
    int ret;
    int val;

    if (srp_dev != -1) {
        /* Check wbuf before writing buff */
        while (wbuf_pos) { /* Write_Buffer ramain?*/
            if (wbuf_pos < srp_ibuf_size) {
                memset(wbuf + wbuf_pos, 0xFF, srp_ibuf_size - wbuf_pos); /* Fill dummy data */
                wbuf_pos = srp_ibuf_size;
            }

            ret = write(srp_dev, wbuf, srp_ibuf_size); /* Write Buffer to RP Driver */
            if (ret == -1) {  /* Fail? */
                ret = ioctl(srp_dev, SRP_ERROR_STATE, &val);
                if (!val) {   /* Write error? */
                    LOGE("%s: IBUF write fail", __func__);
                    return -1;
                } else {      /* RP decoe error? */
                    LOGE("%s: RP decode error [0x%05X]", __func__, val);
                    return -1;
                }
            } else {          /* Success? */
#ifdef _DUMP_TO_FILE_
                if (fp_dump)
                    fwrite(wbuf, srp_ibuf_size, 1, fp_dump);
#endif
                WriteBuff_Consume();
            }
        }

        memset(wbuf, 0xFF, srp_ibuf_size);      /* Fill dummy data */
        write(srp_dev, wbuf, srp_ibuf_size); /* Write Buffer to RP Driver */

        /* Wait until RP decoding over */
        return ioctl(srp_dev, SRP_WAIT_EOS);
    }

    return -1; /* device is not created */
}
#else  /* Without WBUF */
int SRP_Decode(void *buff, int size_byte)
{
    int ret;
    int val;
    int err_code = 0;

    if (srp_dev != -1) {
        LOGD("%s: Send data to RP (%d bytes)", __func__, size_byte);

        ret = write(srp_dev, buff, size_byte);  /* Write Buffer to RP Driver */
        if (ret == -1) {  /* Fail? */
            ioctl(srp_dev, SRP_ERROR_STATE, &val);
            if (!val) {   /* Write error? */
                LOGE("%s: IBUF write fail", __func__);
                return -1;
            } else {      /* Write OK, but RP decode error? */
                err_code = val;
                LOGE("%s: RP decode error [0x%05X]", __func__, err_code);
            }
        }
#ifdef _DUMP_TO_FILE_
        if (fp_dump)
            fwrite(buff, size_byte, 1, fp_dump);
#endif

        return err_code; /* Write Success */
    }

    LOGE("%s: Device is not ready", __func__);
    return -1; /* device is not created */
}

int SRP_Send_EOS(void)
{
    /* Wait until RP decoding over */
    if (srp_dev != -1)
        return ioctl(srp_dev, SRP_SEND_EOS);

    return -1; /* device is not created */
}

int SRP_Resume_EOS(void)
{
    if (srp_dev != -1)
        return ioctl(srp_dev, SRP_RESUME_EOS);

    return -1; /* device is not created */
}
#endif

int SRP_Pause(void)
{
    if (srp_dev != -1)
        return ioctl(srp_dev, SRP_PAUSE);

    return -1; /* device is not created */
}

int SRP_Stop(void)
{
    if (srp_dev != -1)
        return ioctl(srp_dev, SRP_STOP);

    return -1; /* device is not created */
}

int SRP_Flush(void)
{
    if (srp_dev != -1) {
        if (ioctl(srp_dev, SRP_FLUSH) != -1) {
#ifdef _USE_WBUF_
            WriteBuff_Flush();
#endif
            return 0;
        }
    }

    return -1; /* device is not created */
}


int SRP_SetParams(int id, unsigned long val)
{
    if (srp_dev != -1)
        return 0; /* not yet */

    return -1;    /* device is not created */
}

int SRP_GetParams(int id, unsigned long *pval)
{
    if (srp_dev != -1)
        return ioctl(srp_dev, id, pval);

    return -1;    /* device is not created */
}

int SRP_Deinit(void)
{
    if (srp_dev != -1) {
#ifdef _DUMP_TO_FILE_
        if (fp_dump)
            fclose(fp_dump);
#endif

#ifdef _USE_WBUF_
        WriteBuff_Deinit();
#endif
        return ioctl(srp_dev, SRP_DEINIT); /* Deinialize */
    }

    LOGE("%s: Device is not ready", __func__);
    return -1;    /* device is not created */
}

int SRP_Terminate(void)
{
    int ret;

    if (srp_dev != -1) {
        ret = close(srp_dev);

        if (ret == 0) {
            srp_dev = -1; /* device closed */
            return 0;
        }
    }

    LOGE("%s: Device is not ready", __func__);
    return -1; /* device is not created or close error*/
}

int SRP_IsOpen(void)
{
    if (srp_dev == -1) {
        LOGD("%s: Device is not opened", __func__);
        return 0;
    }

    LOGD("%s: Device is opened", __func__);
    return 1;
}