1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22#include <stdarg.h>
23
24#include "ivtv-driver.h"
25#include "ivtv-mailbox.h"
26
27
28#define IVTV_MBOX_FIRMWARE_DONE 0x00000004
29#define IVTV_MBOX_DRIVER_DONE 0x00000002
30#define IVTV_MBOX_DRIVER_BUSY 0x00000001
31#define IVTV_MBOX_FREE 0x00000000
32
33
34#define IVTV_API_STD_TIMEOUT 0x02000000
35
36#define API_CACHE (1 << 0)
37#define API_RESULT (1 << 1)
38#define API_FAST_RESULT (3 << 1)
39#define API_DMA (1 << 3)
40#define API_HIGH_VOL (1 << 5)
41#define API_NO_WAIT_MB (1 << 4)
42#define API_NO_WAIT_RES (1 << 5)
43#define API_NO_POLL (1 << 6)
44
45struct ivtv_api_info {
46 int flags;
47 const char *name;
48};
49
50#define API_ENTRY(x, f) [x] = { (f), #x }
51
52static const struct ivtv_api_info api_info[256] = {
53
54 API_ENTRY(CX2341X_ENC_PING_FW, API_FAST_RESULT),
55 API_ENTRY(CX2341X_ENC_START_CAPTURE, API_RESULT | API_NO_POLL),
56 API_ENTRY(CX2341X_ENC_STOP_CAPTURE, API_RESULT),
57 API_ENTRY(CX2341X_ENC_SET_AUDIO_ID, API_CACHE),
58 API_ENTRY(CX2341X_ENC_SET_VIDEO_ID, API_CACHE),
59 API_ENTRY(CX2341X_ENC_SET_PCR_ID, API_CACHE),
60 API_ENTRY(CX2341X_ENC_SET_FRAME_RATE, API_CACHE),
61 API_ENTRY(CX2341X_ENC_SET_FRAME_SIZE, API_CACHE),
62 API_ENTRY(CX2341X_ENC_SET_BIT_RATE, API_CACHE),
63 API_ENTRY(CX2341X_ENC_SET_GOP_PROPERTIES, API_CACHE),
64 API_ENTRY(CX2341X_ENC_SET_ASPECT_RATIO, API_CACHE),
65 API_ENTRY(CX2341X_ENC_SET_DNR_FILTER_MODE, API_CACHE),
66 API_ENTRY(CX2341X_ENC_SET_DNR_FILTER_PROPS, API_CACHE),
67 API_ENTRY(CX2341X_ENC_SET_CORING_LEVELS, API_CACHE),
68 API_ENTRY(CX2341X_ENC_SET_SPATIAL_FILTER_TYPE, API_CACHE),
69 API_ENTRY(CX2341X_ENC_SET_VBI_LINE, API_RESULT),
70 API_ENTRY(CX2341X_ENC_SET_STREAM_TYPE, API_CACHE),
71 API_ENTRY(CX2341X_ENC_SET_OUTPUT_PORT, API_CACHE),
72 API_ENTRY(CX2341X_ENC_SET_AUDIO_PROPERTIES, API_CACHE),
73 API_ENTRY(CX2341X_ENC_HALT_FW, API_FAST_RESULT),
74 API_ENTRY(CX2341X_ENC_GET_VERSION, API_FAST_RESULT),
75 API_ENTRY(CX2341X_ENC_SET_GOP_CLOSURE, API_CACHE),
76 API_ENTRY(CX2341X_ENC_GET_SEQ_END, API_RESULT),
77 API_ENTRY(CX2341X_ENC_SET_PGM_INDEX_INFO, API_FAST_RESULT),
78 API_ENTRY(CX2341X_ENC_SET_VBI_CONFIG, API_RESULT),
79 API_ENTRY(CX2341X_ENC_SET_DMA_BLOCK_SIZE, API_CACHE),
80 API_ENTRY(CX2341X_ENC_GET_PREV_DMA_INFO_MB_10, API_FAST_RESULT),
81 API_ENTRY(CX2341X_ENC_GET_PREV_DMA_INFO_MB_9, API_FAST_RESULT),
82 API_ENTRY(CX2341X_ENC_SCHED_DMA_TO_HOST, API_DMA | API_HIGH_VOL),
83 API_ENTRY(CX2341X_ENC_INITIALIZE_INPUT, API_RESULT),
84 API_ENTRY(CX2341X_ENC_SET_FRAME_DROP_RATE, API_CACHE),
85 API_ENTRY(CX2341X_ENC_PAUSE_ENCODER, API_RESULT),
86 API_ENTRY(CX2341X_ENC_REFRESH_INPUT, API_NO_WAIT_MB | API_HIGH_VOL),
87 API_ENTRY(CX2341X_ENC_SET_COPYRIGHT, API_CACHE),
88 API_ENTRY(CX2341X_ENC_SET_EVENT_NOTIFICATION, API_RESULT),
89 API_ENTRY(CX2341X_ENC_SET_NUM_VSYNC_LINES, API_CACHE),
90 API_ENTRY(CX2341X_ENC_SET_PLACEHOLDER, API_CACHE),
91 API_ENTRY(CX2341X_ENC_MUTE_VIDEO, API_RESULT),
92 API_ENTRY(CX2341X_ENC_MUTE_AUDIO, API_RESULT),
93 API_ENTRY(CX2341X_ENC_SET_VERT_CROP_LINE, API_FAST_RESULT),
94 API_ENTRY(CX2341X_ENC_MISC, API_FAST_RESULT),
95
96 API_ENTRY(0xb1, API_CACHE),
97
98
99 API_ENTRY(CX2341X_DEC_PING_FW, API_FAST_RESULT),
100 API_ENTRY(CX2341X_DEC_START_PLAYBACK, API_RESULT | API_NO_POLL),
101 API_ENTRY(CX2341X_DEC_STOP_PLAYBACK, API_RESULT),
102 API_ENTRY(CX2341X_DEC_SET_PLAYBACK_SPEED, API_RESULT),
103 API_ENTRY(CX2341X_DEC_STEP_VIDEO, API_RESULT),
104 API_ENTRY(CX2341X_DEC_SET_DMA_BLOCK_SIZE, API_CACHE),
105 API_ENTRY(CX2341X_DEC_GET_XFER_INFO, API_FAST_RESULT),
106 API_ENTRY(CX2341X_DEC_GET_DMA_STATUS, API_FAST_RESULT),
107 API_ENTRY(CX2341X_DEC_SCHED_DMA_FROM_HOST, API_DMA | API_HIGH_VOL),
108 API_ENTRY(CX2341X_DEC_PAUSE_PLAYBACK, API_RESULT),
109 API_ENTRY(CX2341X_DEC_HALT_FW, API_FAST_RESULT),
110 API_ENTRY(CX2341X_DEC_SET_STANDARD, API_CACHE),
111 API_ENTRY(CX2341X_DEC_GET_VERSION, API_FAST_RESULT),
112 API_ENTRY(CX2341X_DEC_SET_STREAM_INPUT, API_CACHE),
113 API_ENTRY(CX2341X_DEC_GET_TIMING_INFO, API_RESULT ),
114 API_ENTRY(CX2341X_DEC_SET_AUDIO_MODE, API_CACHE),
115 API_ENTRY(CX2341X_DEC_SET_EVENT_NOTIFICATION, API_RESULT),
116 API_ENTRY(CX2341X_DEC_SET_DISPLAY_BUFFERS, API_CACHE),
117 API_ENTRY(CX2341X_DEC_EXTRACT_VBI, API_RESULT),
118 API_ENTRY(CX2341X_DEC_SET_DECODER_SOURCE, API_FAST_RESULT),
119 API_ENTRY(CX2341X_DEC_SET_PREBUFFERING, API_CACHE),
120
121
122 API_ENTRY(CX2341X_OSD_GET_FRAMEBUFFER, API_FAST_RESULT),
123 API_ENTRY(CX2341X_OSD_GET_PIXEL_FORMAT, API_FAST_RESULT),
124 API_ENTRY(CX2341X_OSD_SET_PIXEL_FORMAT, API_CACHE),
125 API_ENTRY(CX2341X_OSD_GET_STATE, API_FAST_RESULT),
126 API_ENTRY(CX2341X_OSD_SET_STATE, API_CACHE),
127 API_ENTRY(CX2341X_OSD_GET_OSD_COORDS, API_FAST_RESULT),
128 API_ENTRY(CX2341X_OSD_SET_OSD_COORDS, API_CACHE),
129 API_ENTRY(CX2341X_OSD_GET_SCREEN_COORDS, API_FAST_RESULT),
130 API_ENTRY(CX2341X_OSD_SET_SCREEN_COORDS, API_CACHE),
131 API_ENTRY(CX2341X_OSD_GET_GLOBAL_ALPHA, API_FAST_RESULT),
132 API_ENTRY(CX2341X_OSD_SET_GLOBAL_ALPHA, API_CACHE),
133 API_ENTRY(CX2341X_OSD_SET_BLEND_COORDS, API_CACHE),
134 API_ENTRY(CX2341X_OSD_GET_FLICKER_STATE, API_FAST_RESULT),
135 API_ENTRY(CX2341X_OSD_SET_FLICKER_STATE, API_CACHE),
136 API_ENTRY(CX2341X_OSD_BLT_COPY, API_RESULT),
137 API_ENTRY(CX2341X_OSD_BLT_FILL, API_RESULT),
138 API_ENTRY(CX2341X_OSD_BLT_TEXT, API_RESULT),
139 API_ENTRY(CX2341X_OSD_SET_FRAMEBUFFER_WINDOW, API_CACHE),
140 API_ENTRY(CX2341X_OSD_SET_CHROMA_KEY, API_CACHE),
141 API_ENTRY(CX2341X_OSD_GET_ALPHA_CONTENT_INDEX, API_FAST_RESULT),
142 API_ENTRY(CX2341X_OSD_SET_ALPHA_CONTENT_INDEX, API_CACHE)
143};
144
145static int try_mailbox(struct ivtv *itv, struct ivtv_mailbox_data *mbdata, int mb)
146{
147 u32 flags = readl(&mbdata->mbox[mb].flags);
148 int is_free = flags == IVTV_MBOX_FREE || (flags & IVTV_MBOX_FIRMWARE_DONE);
149
150
151 if (is_free && !test_and_set_bit(mb, &mbdata->busy)) {
152 write_sync(IVTV_MBOX_DRIVER_BUSY, &mbdata->mbox[mb].flags);
153 return 1;
154 }
155 return 0;
156}
157
158
159
160static int get_mailbox(struct ivtv *itv, struct ivtv_mailbox_data *mbdata, int flags)
161{
162 unsigned long then = jiffies;
163 int i, mb;
164 int max_mbox = mbdata->max_mbox;
165 int retries = 100;
166
167
168
169 if ((flags & API_FAST_RESULT) == API_RESULT)
170 max_mbox = 1;
171
172
173 for (i = 0; i < retries; i++) {
174 for (mb = 1; mb <= max_mbox; mb++)
175 if (try_mailbox(itv, mbdata, mb))
176 return mb;
177
178
179 if (!(flags & API_NO_WAIT_MB)) {
180 if (time_after(jiffies,
181 then + msecs_to_jiffies(10*retries)))
182 break;
183 ivtv_msleep_timeout(10, 0);
184 }
185 }
186 return -ENODEV;
187}
188
189static void write_mailbox(volatile struct ivtv_mailbox __iomem *mbox, int cmd, int args, u32 data[])
190{
191 int i;
192
193 write_sync(cmd, &mbox->cmd);
194 write_sync(IVTV_API_STD_TIMEOUT, &mbox->timeout);
195
196 for (i = 0; i < CX2341X_MBOX_MAX_DATA; i++)
197 write_sync(data[i], &mbox->data[i]);
198
199 write_sync(IVTV_MBOX_DRIVER_DONE | IVTV_MBOX_DRIVER_BUSY, &mbox->flags);
200}
201
202static void clear_all_mailboxes(struct ivtv *itv, struct ivtv_mailbox_data *mbdata)
203{
204 int i;
205
206 for (i = 0; i <= mbdata->max_mbox; i++) {
207 IVTV_DEBUG_WARN("Clearing mailbox %d: cmd 0x%08x flags 0x%08x\n",
208 i, readl(&mbdata->mbox[i].cmd), readl(&mbdata->mbox[i].flags));
209 write_sync(0, &mbdata->mbox[i].flags);
210 clear_bit(i, &mbdata->busy);
211 }
212}
213
214static int ivtv_api_call(struct ivtv *itv, int cmd, int args, u32 data[])
215{
216 struct ivtv_mailbox_data *mbdata = (cmd >= 128) ? &itv->enc_mbox : &itv->dec_mbox;
217 volatile struct ivtv_mailbox __iomem *mbox;
218 int api_timeout = msecs_to_jiffies(1000);
219 int flags, mb, i;
220 unsigned long then;
221
222
223 if (NULL == mbdata) {
224 IVTV_ERR("No mailbox allocated\n");
225 return -ENODEV;
226 }
227 if (args < 0 || args > CX2341X_MBOX_MAX_DATA ||
228 cmd < 0 || cmd > 255 || api_info[cmd].name == NULL) {
229 IVTV_ERR("Invalid MB call: cmd = 0x%02x, args = %d\n", cmd, args);
230 return -EINVAL;
231 }
232
233 if (api_info[cmd].flags & API_HIGH_VOL) {
234 IVTV_DEBUG_HI_MB("MB Call: %s\n", api_info[cmd].name);
235 }
236 else {
237 IVTV_DEBUG_MB("MB Call: %s\n", api_info[cmd].name);
238 }
239
240
241 for (i = args; i < CX2341X_MBOX_MAX_DATA; i++)
242 data[i] = 0;
243
244
245
246
247 if (itv->api_cache[cmd].last_jiffies &&
248 time_before(jiffies,
249 itv->api_cache[cmd].last_jiffies +
250 msecs_to_jiffies(1800000)) &&
251 !memcmp(data, itv->api_cache[cmd].data, sizeof(itv->api_cache[cmd].data))) {
252 itv->api_cache[cmd].last_jiffies = jiffies;
253 return 0;
254 }
255
256 flags = api_info[cmd].flags;
257
258 if (flags & API_DMA) {
259 for (i = 0; i < 100; i++) {
260 mb = i % (mbdata->max_mbox + 1);
261 if (try_mailbox(itv, mbdata, mb)) {
262 write_mailbox(&mbdata->mbox[mb], cmd, args, data);
263 clear_bit(mb, &mbdata->busy);
264 return 0;
265 }
266 IVTV_DEBUG_WARN("%s: mailbox %d not free %08x\n",
267 api_info[cmd].name, mb, readl(&mbdata->mbox[mb].flags));
268 }
269 IVTV_WARN("Could not find free DMA mailbox for %s\n", api_info[cmd].name);
270 clear_all_mailboxes(itv, mbdata);
271 return -EBUSY;
272 }
273
274 if ((flags & API_FAST_RESULT) == API_FAST_RESULT)
275 api_timeout = msecs_to_jiffies(100);
276
277 mb = get_mailbox(itv, mbdata, flags);
278 if (mb < 0) {
279 IVTV_DEBUG_WARN("No free mailbox found (%s)\n", api_info[cmd].name);
280 clear_all_mailboxes(itv, mbdata);
281 return -EBUSY;
282 }
283 mbox = &mbdata->mbox[mb];
284 write_mailbox(mbox, cmd, args, data);
285 if (flags & API_CACHE) {
286 memcpy(itv->api_cache[cmd].data, data, sizeof(itv->api_cache[cmd].data));
287 itv->api_cache[cmd].last_jiffies = jiffies;
288 }
289 if ((flags & API_RESULT) == 0) {
290 clear_bit(mb, &mbdata->busy);
291 return 0;
292 }
293
294
295 then = jiffies;
296
297 if (!(flags & API_NO_POLL)) {
298
299 for (i = 0; i < 100; i++) {
300 if (readl(&mbox->flags) & IVTV_MBOX_FIRMWARE_DONE)
301 break;
302 }
303 }
304 while (!(readl(&mbox->flags) & IVTV_MBOX_FIRMWARE_DONE)) {
305 if (time_after(jiffies, then + api_timeout)) {
306 IVTV_DEBUG_WARN("Could not get result (%s)\n", api_info[cmd].name);
307
308 write_sync(0, &mbox->flags);
309 clear_bit(mb, &mbdata->busy);
310 return -EIO;
311 }
312 if (flags & API_NO_WAIT_RES)
313 mdelay(1);
314 else
315 ivtv_msleep_timeout(1, 0);
316 }
317 if (time_after(jiffies, then + msecs_to_jiffies(100)))
318 IVTV_DEBUG_WARN("%s took %u jiffies\n",
319 api_info[cmd].name,
320 jiffies_to_msecs(jiffies - then));
321
322 for (i = 0; i < CX2341X_MBOX_MAX_DATA; i++)
323 data[i] = readl(&mbox->data[i]);
324 write_sync(0, &mbox->flags);
325 clear_bit(mb, &mbdata->busy);
326 return 0;
327}
328
329int ivtv_api(struct ivtv *itv, int cmd, int args, u32 data[])
330{
331 int res = ivtv_api_call(itv, cmd, args, data);
332
333
334
335
336 return (res == -EBUSY) ? ivtv_api_call(itv, cmd, args, data) : res;
337}
338
339int ivtv_api_func(void *priv, u32 cmd, int in, int out, u32 data[CX2341X_MBOX_MAX_DATA])
340{
341 return ivtv_api(priv, cmd, in, data);
342}
343
344int ivtv_vapi_result(struct ivtv *itv, u32 data[CX2341X_MBOX_MAX_DATA], int cmd, int args, ...)
345{
346 va_list ap;
347 int i;
348
349 va_start(ap, args);
350 for (i = 0; i < args; i++) {
351 data[i] = va_arg(ap, u32);
352 }
353 va_end(ap);
354 return ivtv_api(itv, cmd, args, data);
355}
356
357int ivtv_vapi(struct ivtv *itv, int cmd, int args, ...)
358{
359 u32 data[CX2341X_MBOX_MAX_DATA];
360 va_list ap;
361 int i;
362
363 va_start(ap, args);
364 for (i = 0; i < args; i++) {
365 data[i] = va_arg(ap, u32);
366 }
367 va_end(ap);
368 return ivtv_api(itv, cmd, args, data);
369}
370
371
372void ivtv_api_get_data(struct ivtv_mailbox_data *mbdata, int mb,
373 int argc, u32 data[])
374{
375 volatile u32 __iomem *p = mbdata->mbox[mb].data;
376 int i;
377 for (i = 0; i < argc; i++, p++)
378 data[i] = readl(p);
379}
380
381
382void ivtv_mailbox_cache_invalidate(struct ivtv *itv)
383{
384 int i;
385 for (i = 0; i < 256; i++)
386 itv->api_cache[i].last_jiffies = 0;
387}
388