410655d4c98a14b46400c3e3c238696a8a11ecf7
[openwrt/openwrt.git] / package / lqtapi / src / include / linux / tapi / tapi.h
1 #ifndef __LINUX_TAPI_H__
2 #define __LINUX_TAPI_H__
3
4 #include <linux/kernel.h>
5 #include <linux/device.h>
6 #include <linux/mutex.h>
7
8 #include <linux/input.h>
9
10 #include <asm/atomic.h>
11 #include <linux/list.h>
12
13 #include <linux/cdev.h>
14
15 #include <linux/skbuff.h>
16 #include <linux/wait.h>
17
18 #include <linux/tapi/tapi-event.h>
19
20 struct tapi_device;
21
22 struct tapi_char_device {
23 struct tapi_device *tdev;
24 struct device dev;
25 struct cdev cdev;
26 };
27
28 static inline struct tapi_char_device *cdev_to_tapi_char_device(struct cdev *cdev)
29 {
30 return container_of(cdev, struct tapi_char_device, cdev);
31 }
32
33 int tapi_char_device_register(struct tapi_device *tdev,
34 struct tapi_char_device *tchrdev, const struct file_operations *fops);
35
36
37 struct tapi_endpoint {
38 unsigned int id;
39 void *data;
40 };
41
42 static inline void tapi_endpoint_set_data(struct tapi_endpoint *ep, void *data)
43 {
44 ep->data = data;
45 }
46
47 static inline void *tapi_endpoint_get_data(struct tapi_endpoint *ep)
48 {
49 return ep->data;
50 }
51
52 struct tapi_port {
53 unsigned int id;
54 struct tapi_endpoint ep;
55 struct input_dev *input;
56 struct tapi_char_device chrdev;
57 };
58
59 struct tapi_stream {
60 unsigned int id;
61 struct list_head head;
62 struct tapi_endpoint ep;
63
64 struct sk_buff_head recv_queue;
65 wait_queue_head_t recv_wait;
66 struct sk_buff_head send_queue;
67 };
68
69 struct tapi_link {
70 unsigned int id;
71 struct list_head head;
72 };
73
74 enum tapi_codec {
75 TAPI_CODEC_L16,
76 };
77
78 struct tapi_stream_config {
79 enum tapi_codec codec;
80 unsigned int buffer_size;
81 };
82
83 struct tapi_ops {
84 int (*send_dtmf_events)(struct tapi_device *, struct tapi_port *port,
85 struct tapi_dtmf_event *, size_t num_events, unsigned int dealy);
86 int (*send_dtmf_event)(struct tapi_device *, struct tapi_port *port,
87 struct tapi_dtmf_event *);
88 int (*ring)(struct tapi_device *, struct tapi_port *port, bool ring);
89
90 struct tapi_stream *(*stream_alloc)(struct tapi_device *);
91 void (*stream_free)(struct tapi_device *, struct tapi_stream *);
92 int (*stream_configure)(struct tapi_device *, struct tapi_stream *,
93 struct tapi_stream_config *);
94 int (*stream_start)(struct tapi_device *, struct tapi_stream *);
95 int (*stream_stop)(struct tapi_device *, struct tapi_stream *);
96 int (*stream_send)(struct tapi_device *, struct tapi_stream *,
97 struct sk_buff *);
98
99 struct tapi_link *(*link_alloc)(struct tapi_device *,
100 struct tapi_endpoint *ep1, struct tapi_endpoint *ep2);
101 void (*link_free)(struct tapi_device *, struct tapi_link *);
102 int (*link_enable)(struct tapi_device *, struct tapi_link *);
103 int (*link_disable)(struct tapi_device *, struct tapi_link *);
104
105 int (*sync)(struct tapi_device *);
106 };
107
108 int tapi_stream_recv(struct tapi_device *, struct tapi_stream *, struct sk_buff *);
109
110 struct tapi_device {
111 unsigned int id;
112
113 const struct tapi_ops *ops;
114 unsigned int num_ports;
115
116 struct device dev;
117
118 struct mutex lock;
119
120 struct tapi_port *ports;
121 struct list_head streams;
122 struct list_head links;
123 atomic_t stream_id;
124 atomic_t link_id;
125
126 struct tapi_char_device stream_dev;
127 struct tapi_char_device control_dev;
128 };
129
130 static inline struct tapi_device *dev_to_tapi(struct device *dev)
131 {
132 return container_of(dev, struct tapi_device, dev);
133 }
134
135 static inline struct tapi_stream *tapi_stream_from_id(struct tapi_device *tdev,
136 unsigned int id)
137 {
138 struct tapi_stream *stream;
139
140 mutex_lock(&tdev->lock);
141
142 list_for_each_entry(stream, &tdev->streams, head) {
143 if (stream->id == id)
144 goto out;
145 }
146 stream = NULL;
147
148 out:
149 mutex_unlock(&tdev->lock);
150 return stream;
151 }
152
153 struct tapi_link *tapi_link_alloc(struct tapi_device *, struct tapi_endpoint *,
154 struct tapi_endpoint *);
155 void tapi_link_free(struct tapi_device *, struct tapi_link *);
156
157 struct tapi_stream *tapi_stream_alloc(struct tapi_device *tdev);
158 void tapi_stream_free(struct tapi_device *tdev, struct tapi_stream *stream);
159
160 static inline int tapi_sync(struct tapi_device *tdev)
161 {
162 if (!tdev->ops || !tdev->ops->sync)
163 return 0;
164
165 return tdev->ops->sync(tdev);
166 }
167
168 static inline int tapi_link_enable(struct tapi_device *tdev,
169 struct tapi_link *link)
170 {
171 if (!tdev->ops || !tdev->ops->link_enable)
172 return 0;
173
174 return tdev->ops->link_enable(tdev, link);
175 }
176
177 static inline int tapi_link_disable(struct tapi_device *tdev,
178 struct tapi_link *link)
179 {
180 if (!tdev->ops || !tdev->ops->link_disable)
181 return 0;
182
183 return tdev->ops->link_disable(tdev, link);
184 }
185
186 static inline int tapi_port_send_dtmf(struct tapi_device *tdev,
187 struct tapi_port *port, struct tapi_dtmf_event *dtmf)
188 {
189 if (!tdev->ops || !tdev->ops->send_dtmf_event)
190 return -ENOSYS;
191
192 return tdev->ops->send_dtmf_event(tdev, port, dtmf);
193 }
194
195 static inline int tapi_port_set_ring(struct tapi_device *tdev,
196 struct tapi_port *port, bool ring)
197 {
198 if (!tdev->ops || !tdev->ops->ring)
199 return -ENOSYS;
200
201 return tdev->ops->ring(tdev, port, ring);
202 }
203
204 static inline int tapi_stream_start(struct tapi_device *tdev,
205 struct tapi_stream *stream)
206 {
207 if (!tdev->ops || !tdev->ops->stream_start)
208 return -ENOSYS;
209
210 return tdev->ops->stream_start(tdev, stream);
211 }
212
213 static inline int tapi_stream_stop(struct tapi_device *tdev,
214 struct tapi_stream *stream)
215 {
216 if (!tdev->ops || !tdev->ops->stream_stop)
217 return -ENOSYS;
218
219 return tdev->ops->stream_stop(tdev, stream);
220 }
221
222 int tapi_device_register(struct tapi_device *tdev, const char *name,
223 struct device *parent);
224 void tapi_device_unregister(struct tapi_device *tdev);
225
226 struct tapi_sysfs_port;
227
228 struct tapi_sysfs_port *tapi_port_alloc(struct tapi_device *tdev, unsigned int id);
229 void tapi_port_delete(struct tapi_sysfs_port *);
230
231 #endif