V4L/DVB (9593): cx18: Add outgoing mailbox mutexes and check for ack via waitq vs...
[safe/jmp/linux-2.6] / drivers / media / video / cx18 / cx18-mailbox.c
1 /*
2  *  cx18 mailbox functions
3  *
4  *  Copyright (C) 2007  Hans Verkuil <hverkuil@xs4all.nl>
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19  *  02111-1307  USA
20  */
21
22 #include <stdarg.h>
23
24 #include "cx18-driver.h"
25 #include "cx18-io.h"
26 #include "cx18-scb.h"
27 #include "cx18-irq.h"
28 #include "cx18-mailbox.h"
29
30 #define API_FAST (1 << 2) /* Short timeout */
31 #define API_SLOW (1 << 3) /* Additional 300ms timeout */
32
33 struct cx18_api_info {
34         u32 cmd;
35         u8 flags;               /* Flags, see above */
36         u8 rpu;                 /* Processing unit */
37         const char *name;       /* The name of the command */
38 };
39
40 #define API_ENTRY(rpu, x, f) { (x), (f), (rpu), #x }
41
42 static const struct cx18_api_info api_info[] = {
43         /* MPEG encoder API */
44         API_ENTRY(CPU, CX18_CPU_SET_CHANNEL_TYPE,               0),
45         API_ENTRY(CPU, CX18_EPU_DEBUG,                          0),
46         API_ENTRY(CPU, CX18_CREATE_TASK,                        0),
47         API_ENTRY(CPU, CX18_DESTROY_TASK,                       0),
48         API_ENTRY(CPU, CX18_CPU_CAPTURE_START,                  API_SLOW),
49         API_ENTRY(CPU, CX18_CPU_CAPTURE_STOP,                   API_SLOW),
50         API_ENTRY(CPU, CX18_CPU_CAPTURE_PAUSE,                  0),
51         API_ENTRY(CPU, CX18_CPU_CAPTURE_RESUME,                 0),
52         API_ENTRY(CPU, CX18_CPU_SET_CHANNEL_TYPE,               0),
53         API_ENTRY(CPU, CX18_CPU_SET_STREAM_OUTPUT_TYPE,         0),
54         API_ENTRY(CPU, CX18_CPU_SET_VIDEO_IN,                   0),
55         API_ENTRY(CPU, CX18_CPU_SET_VIDEO_RATE,                 0),
56         API_ENTRY(CPU, CX18_CPU_SET_VIDEO_RESOLUTION,           0),
57         API_ENTRY(CPU, CX18_CPU_SET_FILTER_PARAM,               0),
58         API_ENTRY(CPU, CX18_CPU_SET_SPATIAL_FILTER_TYPE,        0),
59         API_ENTRY(CPU, CX18_CPU_SET_MEDIAN_CORING,              0),
60         API_ENTRY(CPU, CX18_CPU_SET_INDEXTABLE,                 0),
61         API_ENTRY(CPU, CX18_CPU_SET_AUDIO_PARAMETERS,           0),
62         API_ENTRY(CPU, CX18_CPU_SET_VIDEO_MUTE,                 0),
63         API_ENTRY(CPU, CX18_CPU_SET_AUDIO_MUTE,                 0),
64         API_ENTRY(CPU, CX18_CPU_SET_MISC_PARAMETERS,            0),
65         API_ENTRY(CPU, CX18_CPU_SET_RAW_VBI_PARAM,              API_SLOW),
66         API_ENTRY(CPU, CX18_CPU_SET_CAPTURE_LINE_NO,            0),
67         API_ENTRY(CPU, CX18_CPU_SET_COPYRIGHT,                  0),
68         API_ENTRY(CPU, CX18_CPU_SET_AUDIO_PID,                  0),
69         API_ENTRY(CPU, CX18_CPU_SET_VIDEO_PID,                  0),
70         API_ENTRY(CPU, CX18_CPU_SET_VER_CROP_LINE,              0),
71         API_ENTRY(CPU, CX18_CPU_SET_GOP_STRUCTURE,              0),
72         API_ENTRY(CPU, CX18_CPU_SET_SCENE_CHANGE_DETECTION,     0),
73         API_ENTRY(CPU, CX18_CPU_SET_ASPECT_RATIO,               0),
74         API_ENTRY(CPU, CX18_CPU_SET_SKIP_INPUT_FRAME,           0),
75         API_ENTRY(CPU, CX18_CPU_SET_SLICED_VBI_PARAM,           0),
76         API_ENTRY(CPU, CX18_CPU_SET_USERDATA_PLACE_HOLDER,      0),
77         API_ENTRY(CPU, CX18_CPU_GET_ENC_PTS,                    0),
78         API_ENTRY(CPU, CX18_CPU_DE_SET_MDL_ACK,                 0),
79         API_ENTRY(CPU, CX18_CPU_DE_SET_MDL,                     API_FAST),
80         API_ENTRY(CPU, CX18_APU_RESETAI,                        API_FAST),
81         API_ENTRY(CPU, CX18_CPU_DE_RELEASE_MDL,                 API_SLOW),
82         API_ENTRY(0, 0,                                         0),
83 };
84
85 static const struct cx18_api_info *find_api_info(u32 cmd)
86 {
87         int i;
88
89         for (i = 0; api_info[i].cmd; i++)
90                 if (api_info[i].cmd == cmd)
91                         return &api_info[i];
92         return NULL;
93 }
94
95 static struct cx18_mailbox __iomem *cx18_mb_is_complete(struct cx18 *cx, int rpu,
96                 u32 *state, u32 *irq, u32 *req)
97 {
98         struct cx18_mailbox __iomem *mb = NULL;
99         int wait_count = 0;
100         u32 ack;
101
102         switch (rpu) {
103         case APU:
104                 mb = &cx->scb->epu2apu_mb;
105                 *state = cx18_readl(cx, &cx->scb->apu_state);
106                 *irq = cx18_readl(cx, &cx->scb->epu2apu_irq);
107                 break;
108
109         case CPU:
110                 mb = &cx->scb->epu2cpu_mb;
111                 *state = cx18_readl(cx, &cx->scb->cpu_state);
112                 *irq = cx18_readl(cx, &cx->scb->epu2cpu_irq);
113                 break;
114
115         default:
116                 break;
117         }
118
119         if (mb == NULL)
120                 return mb;
121
122         do {
123                 *req = cx18_readl(cx, &mb->request);
124                 ack = cx18_readl(cx, &mb->ack);
125                 wait_count++;
126         } while (*req != ack && wait_count < 600);
127
128         if (*req == ack) {
129                 (*req)++;
130                 if (*req == 0 || *req == 0xffffffff)
131                         *req = 1;
132                 return mb;
133         }
134         return NULL;
135 }
136
137 long cx18_mb_ack(struct cx18 *cx, const struct cx18_mailbox *mb, int rpu)
138 {
139         struct cx18_mailbox __iomem *ack_mb;
140         u32 ack_irq;
141
142         switch (rpu) {
143         case APU:
144                 ack_irq = IRQ_EPU_TO_APU_ACK;
145                 ack_mb = &cx->scb->apu2epu_mb;
146                 break;
147         case CPU:
148                 ack_irq = IRQ_EPU_TO_CPU_ACK;
149                 ack_mb = &cx->scb->cpu2epu_mb;
150                 break;
151         default:
152                 CX18_WARN("Unhandled RPU (%d) for command %x ack\n",
153                           rpu, mb->cmd);
154                 return -EINVAL;
155         }
156
157         cx18_setup_page(cx, SCB_OFFSET);
158         cx18_write_sync(cx, mb->request, &ack_mb->ack);
159         cx18_write_reg_expect(cx, ack_irq, SW2_INT_SET, ack_irq, ack_irq);
160         return 0;
161 }
162
163
164 static int cx18_api_call(struct cx18 *cx, u32 cmd, int args, u32 data[])
165 {
166         const struct cx18_api_info *info = find_api_info(cmd);
167         u32 state = 0, irq = 0, req, oldreq, err;
168         struct cx18_mailbox __iomem *mb;
169         wait_queue_head_t *waitq;
170         struct mutex *mb_lock;
171         int timeout = 100;
172         int sig = 0;
173         int i;
174
175         if (info == NULL) {
176                 CX18_WARN("unknown cmd %x\n", cmd);
177                 return -EINVAL;
178         }
179
180         if (cmd == CX18_CPU_DE_SET_MDL)
181                 CX18_DEBUG_HI_API("%s\n", info->name);
182         else
183                 CX18_DEBUG_API("%s\n", info->name);
184
185         switch (info->rpu) {
186         case APU:
187                 waitq = &cx->mb_apu_waitq;
188                 mb_lock = &cx->epu2apu_mb_lock;
189                 break;
190         case CPU:
191                 waitq = &cx->mb_cpu_waitq;
192                 mb_lock = &cx->epu2cpu_mb_lock;
193                 break;
194         default:
195                 CX18_WARN("Unknown RPU (%d) for API call\n", info->rpu);
196                 return -EINVAL;
197         }
198
199         mutex_lock(mb_lock);
200         cx18_setup_page(cx, SCB_OFFSET);
201         mb = cx18_mb_is_complete(cx, info->rpu, &state, &irq, &req);
202
203         if (mb == NULL) {
204                 mutex_unlock(mb_lock);
205                 CX18_ERR("mb %s busy\n", info->name);
206                 return -EBUSY;
207         }
208
209         oldreq = req - 1;
210         cx18_writel(cx, cmd, &mb->cmd);
211         for (i = 0; i < args; i++)
212                 cx18_writel(cx, data[i], &mb->args[i]);
213         cx18_writel(cx, 0, &mb->error);
214         cx18_writel(cx, req, &mb->request);
215
216         if (info->flags & API_FAST)
217                 timeout /= 2;
218         cx18_write_reg_expect(cx, irq, SW1_INT_SET, irq, irq);
219
220         sig = wait_event_interruptible_timeout(
221                        *waitq,
222                        cx18_readl(cx, &mb->ack) == cx18_readl(cx, &mb->request),
223                        msecs_to_jiffies(timeout));
224         if (sig == 0) {
225                 /* Timed out */
226                 cx18_writel(cx, oldreq, &mb->request);
227                 mutex_unlock(mb_lock);
228                 CX18_ERR("sending %s timed out waiting for RPU to respond\n",
229                          info->name);
230                 return -EINVAL;
231         } else if (sig < 0) {
232                 /* Interrupted */
233                 cx18_writel(cx, oldreq, &mb->request);
234                 mutex_unlock(mb_lock);
235                 CX18_WARN("sending %s interrupted waiting for RPU to respond\n",
236                           info->name);
237                 return -EINTR;
238         }
239
240         for (i = 0; i < MAX_MB_ARGUMENTS; i++)
241                 data[i] = cx18_readl(cx, &mb->args[i]);
242         err = cx18_readl(cx, &mb->error);
243         mutex_unlock(mb_lock);
244         if (info->flags & API_SLOW)
245                 cx18_msleep_timeout(300, 0);
246         if (err)
247                 CX18_DEBUG_API("mailbox error %08x for command %s\n", err,
248                                 info->name);
249         return err ? -EIO : 0;
250 }
251
252 int cx18_api(struct cx18 *cx, u32 cmd, int args, u32 data[])
253 {
254         int res = cx18_api_call(cx, cmd, args, data);
255
256         /* Allow a single retry, probably already too late though.
257            If there is no free mailbox then that is usually an indication
258            of a more serious problem. */
259         return (res == -EBUSY) ? cx18_api_call(cx, cmd, args, data) : res;
260 }
261
262 static int cx18_set_filter_param(struct cx18_stream *s)
263 {
264         struct cx18 *cx = s->cx;
265         u32 mode;
266         int ret;
267
268         mode = (cx->filter_mode & 1) ? 2 : (cx->spatial_strength ? 1 : 0);
269         ret = cx18_vapi(cx, CX18_CPU_SET_FILTER_PARAM, 4,
270                         s->handle, 1, mode, cx->spatial_strength);
271         mode = (cx->filter_mode & 2) ? 2 : (cx->temporal_strength ? 1 : 0);
272         ret = ret ? ret : cx18_vapi(cx, CX18_CPU_SET_FILTER_PARAM, 4,
273                         s->handle, 0, mode, cx->temporal_strength);
274         ret = ret ? ret : cx18_vapi(cx, CX18_CPU_SET_FILTER_PARAM, 4,
275                         s->handle, 2, cx->filter_mode >> 2, 0);
276         return ret;
277 }
278
279 int cx18_api_func(void *priv, u32 cmd, int in, int out,
280                 u32 data[CX2341X_MBOX_MAX_DATA])
281 {
282         struct cx18 *cx = priv;
283         struct cx18_stream *s = &cx->streams[CX18_ENC_STREAM_TYPE_MPG];
284
285         switch (cmd) {
286         case CX2341X_ENC_SET_OUTPUT_PORT:
287                 return 0;
288         case CX2341X_ENC_SET_FRAME_RATE:
289                 return cx18_vapi(cx, CX18_CPU_SET_VIDEO_IN, 6,
290                                 s->handle, 0, 0, 0, 0, data[0]);
291         case CX2341X_ENC_SET_FRAME_SIZE:
292                 return cx18_vapi(cx, CX18_CPU_SET_VIDEO_RESOLUTION, 3,
293                                 s->handle, data[1], data[0]);
294         case CX2341X_ENC_SET_STREAM_TYPE:
295                 return cx18_vapi(cx, CX18_CPU_SET_STREAM_OUTPUT_TYPE, 2,
296                                 s->handle, data[0]);
297         case CX2341X_ENC_SET_ASPECT_RATIO:
298                 return cx18_vapi(cx, CX18_CPU_SET_ASPECT_RATIO, 2,
299                                 s->handle, data[0]);
300
301         case CX2341X_ENC_SET_GOP_PROPERTIES:
302                 return cx18_vapi(cx, CX18_CPU_SET_GOP_STRUCTURE, 3,
303                                 s->handle, data[0], data[1]);
304         case CX2341X_ENC_SET_GOP_CLOSURE:
305                 return 0;
306         case CX2341X_ENC_SET_AUDIO_PROPERTIES:
307                 return cx18_vapi(cx, CX18_CPU_SET_AUDIO_PARAMETERS, 2,
308                                 s->handle, data[0]);
309         case CX2341X_ENC_MUTE_AUDIO:
310                 return cx18_vapi(cx, CX18_CPU_SET_AUDIO_MUTE, 2,
311                                 s->handle, data[0]);
312         case CX2341X_ENC_SET_BIT_RATE:
313                 return cx18_vapi(cx, CX18_CPU_SET_VIDEO_RATE, 5,
314                                 s->handle, data[0], data[1], data[2], data[3]);
315         case CX2341X_ENC_MUTE_VIDEO:
316                 return cx18_vapi(cx, CX18_CPU_SET_VIDEO_MUTE, 2,
317                                 s->handle, data[0]);
318         case CX2341X_ENC_SET_FRAME_DROP_RATE:
319                 return cx18_vapi(cx, CX18_CPU_SET_SKIP_INPUT_FRAME, 2,
320                                 s->handle, data[0]);
321         case CX2341X_ENC_MISC:
322                 return cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 4,
323                                 s->handle, data[0], data[1], data[2]);
324         case CX2341X_ENC_SET_DNR_FILTER_MODE:
325                 cx->filter_mode = (data[0] & 3) | (data[1] << 2);
326                 return cx18_set_filter_param(s);
327         case CX2341X_ENC_SET_DNR_FILTER_PROPS:
328                 cx->spatial_strength = data[0];
329                 cx->temporal_strength = data[1];
330                 return cx18_set_filter_param(s);
331         case CX2341X_ENC_SET_SPATIAL_FILTER_TYPE:
332                 return cx18_vapi(cx, CX18_CPU_SET_SPATIAL_FILTER_TYPE, 3,
333                                 s->handle, data[0], data[1]);
334         case CX2341X_ENC_SET_CORING_LEVELS:
335                 return cx18_vapi(cx, CX18_CPU_SET_MEDIAN_CORING, 5,
336                                 s->handle, data[0], data[1], data[2], data[3]);
337         }
338         CX18_WARN("Unknown cmd %x\n", cmd);
339         return 0;
340 }
341
342 int cx18_vapi_result(struct cx18 *cx, u32 data[MAX_MB_ARGUMENTS],
343                 u32 cmd, int args, ...)
344 {
345         va_list ap;
346         int i;
347
348         va_start(ap, args);
349         for (i = 0; i < args; i++)
350                 data[i] = va_arg(ap, u32);
351         va_end(ap);
352         return cx18_api(cx, cmd, args, data);
353 }
354
355 int cx18_vapi(struct cx18 *cx, u32 cmd, int args, ...)
356 {
357         u32 data[MAX_MB_ARGUMENTS];
358         va_list ap;
359         int i;
360
361         if (cx == NULL) {
362                 CX18_ERR("cx == NULL (cmd=%x)\n", cmd);
363                 return 0;
364         }
365         if (args > MAX_MB_ARGUMENTS) {
366                 CX18_ERR("args too big (cmd=%x)\n", cmd);
367                 args = MAX_MB_ARGUMENTS;
368         }
369         va_start(ap, args);
370         for (i = 0; i < args; i++)
371                 data[i] = va_arg(ap, u32);
372         va_end(ap);
373         return cx18_api(cx, cmd, args, data);
374 }