1 /*
2  * Copyright (C) 2015-2017 Alibaba Group Holding Limited
3  */
4 
5 #ifndef AUDIO_H
6 #define AUDIO_H
7 
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <stdbool.h>
11 #include <aos/vfs.h>
12 #include <aos/list.h>
13 #include <aos/kernel.h>
14 #include "control.h"
15 
16 #ifdef __cplusplus
17 extern "C" {
18 #endif
19 
20 #define _IOC_NONE	0U                                 /**< 0x00 */
21 #define _IOC_WRITE	1U                                 /**< 0x01 */
22 #define _IOC_READ	2U                                 /**< 0x02 */
23 #define _IOC_TYPECHECK(t) (sizeof(t))
24 #define _IOC(dir,type,nr,size) (((dir)  << 30) | \
25 								((size) << 16) | \
26 	                            ((type) << 8)  | \
27 	                            ((nr)   << 0))
28 
29 /* audio device type */
30 #define AUDIO_DEVICE_TYPE_PCM_CAPTURE       0
31 #define AUDIO_DEVICE_TYPE_PCM_PLAYBACK      1
32 #define AUDIO_DEVICE_TYPE_CONTROL           2
33 
34 /* pcm device ioctl */
35 #define _IO(type,nr)		                _IOC(_IOC_NONE,(type),(nr),0)
36 #define _IOR(type,nr,size)	                _IOC(_IOC_READ,(type),(nr),(_IOC_TYPECHECK(size)))
37 #define _IOW(type,nr,size)	                _IOC(_IOC_WRITE,(type),(nr),(_IOC_TYPECHECK(size)))
38 #define _IOWR(type,nr,size)	                _IOC(_IOC_READ|_IOC_WRITE,(type),(nr),(_IOC_TYPECHECK(size)))
39 
40 #define AUDIO_PCM_IOCTL_HW_PARAMS           _IOWR('A', 0x11, audio_hw_params_t)
41 #define AUDIO_PCM_IOCTL_SW_PARAMS           _IOWR('A', 0x12, audio_sw_params_t)
42 #define AUDIO_PCM_IOCTL_PREPARE             _IO('A', 0x40)
43 #define AUDIO_PCM_IOCTL_START               _IO('A', 0x42)
44 #define AUDIO_PCM_IOCTL_WRITEI_FRAMES       _IOW('A', 0x50, audio_xferi_t)
45 #define AUDIO_PCM_IOCTL_READI_FRAMES        _IOR('A', 0x51, audio_xferi_t)
46 #define AUDIO_PCM_IOCTL_WRITEN_FRAMES       _IOW('A', 0x52, audio_xfern_t)
47 #define AUDIO_PCM_IOCTL_READN_FRAMES        _IOR('A', 0x53, audio_xfern_t)
48 #define AUDIO_PCM_IOCTL_DROP                _IO('A', 0x43)   /* looks like only for read */
49 #define AUDIO_PCM_IOCTL_DRAIN               _IO('A', 0x44)
50 #define AUDIO_PCM_IOCTL_PAUSE               _IOW('A', 0x45, int)
51 #define AUDIO_PCM_IOCTL_SUSPEND             _IO('A', 0x46)
52 #define AUDIO_PCM_IOCTL_RESUME              _IO('A', 0x47)
53 #define AUDIO_PCM_IOCTL_RECOVER             _IO('A', 0x80)   /* aos private */
54 
55 
56 /* mixer virtual device ioctl */
57 #define AUDIO_CTL_IOCTL_CARD_INFO	        _IOR('U', 0x01, struct audio_ctl_card_info)
58 #define AUDIO_CTL_IOCTL_ELEM_LIST	        _IOWR('U', 0x10, struct audio_ctl_elem_list)
59 #define AUDIO_CTL_IOCTL_ELEM_INFO	        _IOWR('U', 0x11, struct audio_ctl_elem_info)
60 #define AUDIO_CTL_IOCTL_ELEM_READ	        _IOWR('U', 0x12, struct audio_ctl_elem_value)
61 #define AUDIO_CTL_IOCTL_ELEM_WRITE	        _IOWR('U', 0x13, struct audio_ctl_elem_value)
62 #define AUDIO_CTL_IOCTL_TLV_READ	        _IOWR('U', 0x1a, struct audio_ctl_tlv)
63 #define AUDIO_CTL_IOCTL_TLV_WRITE	        _IOWR('U', 0x1b, struct audio_ctl_tlv)
64 #define AUDIO_CTL_IOCTL_TLV_CMD	            _IOWR('U', 0x1c, struct audio_ctl_tlv)
65 
66 
67 #define RET_ERR                 -1
68 #define RET_SUCCESS              0
69 
70 typedef struct {
71     int block;                              /**< 0: non-block mode, otherwise block mode, */
72     int interleave;                         /**< 0: non-interleave access, otherwise interleave access, */
73     int rate;                               /**< sample rate, e.g. 8KHz, 16KHz */
74     int channels;                           /**< channel number, e.g. CH1/ CH2 */
75     int sample_bits;                        /**< sample size in bit */
76 } audio_hw_params_t;
77 
78 typedef struct {
79     aos_hdl_t *hdl;                         /**< aos_event_t handler pointer */
80     int period;                             /**< sample size in bit */
81 } audio_sw_params_t;
82 
83 typedef struct {
84 	int result;                             /**< transfer result, 0/ SUCCESS, otherwise FAILED */
85 	void *buf;                              /**< data buffer */
86 	unsigned int frames;                    /**< data buffer size in frame */
87 } audio_xferi_t;
88 
89 typedef struct {
90 	int result;                             /**< transfer result, 0/ SUCCESS, otherwise FAILED */
91 	void **bufs;                            /**< data buffer */
92 	unsigned int frames;                    /**< data buffer size in frame */
93 } audio_xfern_t;
94 
95 typedef struct {
96     struct dlist_s list;                    /**< audio device node */
97     int type;                               /**< audio device type */
98 	char *name;                             /**< transfer result, 0/ SUCCESS, otherwise FAILED */
99     file_ops_t *f_ops;                      /**< file operations */
100 	void *private_data;                     /**< private data buffer */
101 } audio_device_t;
102 
103 
104 int audio_register_device(int type, const char *name, void *private_data);
105 int audio_unregister_device(audio_device_t *dev);
106 audio_device_t *audio_get_device(const char *name);
107 
108 
109 #ifdef __cplusplus
110 }
111 #endif
112 #endif /* AUDIO_H */
113 
114