cx18-mailbox.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  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. #include <stdarg.h>
  22. #include "cx18-driver.h"
  23. #include "cx18-io.h"
  24. #include "cx18-scb.h"
  25. #include "cx18-irq.h"
  26. #include "cx18-mailbox.h"
  27. #include "cx18-queue.h"
  28. #include "cx18-streams.h"
  29. static const char *rpu_str[] = { "APU", "CPU", "EPU", "HPU" };
  30. #define API_FAST (1 << 2) /* Short timeout */
  31. #define API_SLOW (1 << 3) /* Additional 300ms timeout */
  32. struct cx18_api_info {
  33. u32 cmd;
  34. u8 flags; /* Flags, see above */
  35. u8 rpu; /* Processing unit */
  36. const char *name; /* The name of the command */
  37. };
  38. #define API_ENTRY(rpu, x, f) { (x), (f), (rpu), #x }
  39. static const struct cx18_api_info api_info[] = {
  40. /* MPEG encoder API */
  41. API_ENTRY(CPU, CX18_CPU_SET_CHANNEL_TYPE, 0),
  42. API_ENTRY(CPU, CX18_EPU_DEBUG, 0),
  43. API_ENTRY(CPU, CX18_CREATE_TASK, 0),
  44. API_ENTRY(CPU, CX18_DESTROY_TASK, 0),
  45. API_ENTRY(CPU, CX18_CPU_CAPTURE_START, API_SLOW),
  46. API_ENTRY(CPU, CX18_CPU_CAPTURE_STOP, API_SLOW),
  47. API_ENTRY(CPU, CX18_CPU_CAPTURE_PAUSE, 0),
  48. API_ENTRY(CPU, CX18_CPU_CAPTURE_RESUME, 0),
  49. API_ENTRY(CPU, CX18_CPU_SET_CHANNEL_TYPE, 0),
  50. API_ENTRY(CPU, CX18_CPU_SET_STREAM_OUTPUT_TYPE, 0),
  51. API_ENTRY(CPU, CX18_CPU_SET_VIDEO_IN, 0),
  52. API_ENTRY(CPU, CX18_CPU_SET_VIDEO_RATE, 0),
  53. API_ENTRY(CPU, CX18_CPU_SET_VIDEO_RESOLUTION, 0),
  54. API_ENTRY(CPU, CX18_CPU_SET_FILTER_PARAM, 0),
  55. API_ENTRY(CPU, CX18_CPU_SET_SPATIAL_FILTER_TYPE, 0),
  56. API_ENTRY(CPU, CX18_CPU_SET_MEDIAN_CORING, 0),
  57. API_ENTRY(CPU, CX18_CPU_SET_INDEXTABLE, 0),
  58. API_ENTRY(CPU, CX18_CPU_SET_AUDIO_PARAMETERS, 0),
  59. API_ENTRY(CPU, CX18_CPU_SET_VIDEO_MUTE, 0),
  60. API_ENTRY(CPU, CX18_CPU_SET_AUDIO_MUTE, 0),
  61. API_ENTRY(CPU, CX18_CPU_SET_MISC_PARAMETERS, 0),
  62. API_ENTRY(CPU, CX18_CPU_SET_RAW_VBI_PARAM, API_SLOW),
  63. API_ENTRY(CPU, CX18_CPU_SET_CAPTURE_LINE_NO, 0),
  64. API_ENTRY(CPU, CX18_CPU_SET_COPYRIGHT, 0),
  65. API_ENTRY(CPU, CX18_CPU_SET_AUDIO_PID, 0),
  66. API_ENTRY(CPU, CX18_CPU_SET_VIDEO_PID, 0),
  67. API_ENTRY(CPU, CX18_CPU_SET_VER_CROP_LINE, 0),
  68. API_ENTRY(CPU, CX18_CPU_SET_GOP_STRUCTURE, 0),
  69. API_ENTRY(CPU, CX18_CPU_SET_SCENE_CHANGE_DETECTION, 0),
  70. API_ENTRY(CPU, CX18_CPU_SET_ASPECT_RATIO, 0),
  71. API_ENTRY(CPU, CX18_CPU_SET_SKIP_INPUT_FRAME, 0),
  72. API_ENTRY(CPU, CX18_CPU_SET_SLICED_VBI_PARAM, 0),
  73. API_ENTRY(CPU, CX18_CPU_SET_USERDATA_PLACE_HOLDER, 0),
  74. API_ENTRY(CPU, CX18_CPU_GET_ENC_PTS, 0),
  75. API_ENTRY(CPU, CX18_CPU_DE_SET_MDL_ACK, 0),
  76. API_ENTRY(CPU, CX18_CPU_DE_SET_MDL, API_FAST),
  77. API_ENTRY(CPU, CX18_APU_RESETAI, API_FAST),
  78. API_ENTRY(CPU, CX18_CPU_DE_RELEASE_MDL, API_SLOW),
  79. API_ENTRY(0, 0, 0),
  80. };
  81. static const struct cx18_api_info *find_api_info(u32 cmd)
  82. {
  83. int i;
  84. for (i = 0; api_info[i].cmd; i++)
  85. if (api_info[i].cmd == cmd)
  86. return &api_info[i];
  87. return NULL;
  88. }
  89. static void dump_mb(struct cx18 *cx, struct cx18_mailbox *mb, char *name)
  90. {
  91. char argstr[MAX_MB_ARGUMENTS*11+1];
  92. char *p;
  93. int i;
  94. if (!(cx18_debug & CX18_DBGFLG_API))
  95. return;
  96. for (i = 0, p = argstr; i < MAX_MB_ARGUMENTS; i++, p += 11) {
  97. /* kernel snprintf() appends '\0' always */
  98. snprintf(p, 12, " %#010x", mb->args[i]);
  99. }
  100. CX18_DEBUG_API("%s: req %#010x ack %#010x cmd %#010x err %#010x args%s"
  101. "\n", name, mb->request, mb->ack, mb->cmd, mb->error, argstr);
  102. }
  103. /*
  104. * Functions that run in a work_queue work handling context
  105. */
  106. static void epu_dma_done(struct cx18 *cx, struct cx18_epu_work_order *order)
  107. {
  108. u32 handle, mdl_ack_count;
  109. struct cx18_mailbox *mb;
  110. struct cx18_mdl_ack *mdl_ack;
  111. struct cx18_stream *s;
  112. struct cx18_buffer *buf;
  113. int i;
  114. mb = &order->mb;
  115. handle = mb->args[0];
  116. s = cx18_handle_to_stream(cx, handle);
  117. if (s == NULL) {
  118. CX18_WARN("Got DMA done notification for unknown/inactive"
  119. " handle %d\n", handle);
  120. return;
  121. }
  122. mdl_ack_count = mb->args[2];
  123. mdl_ack = order->mdl_ack;
  124. for (i = 0; i < mdl_ack_count; i++, mdl_ack++) {
  125. buf = cx18_queue_get_buf(s, mdl_ack->id, mdl_ack->data_used);
  126. CX18_DEBUG_HI_DMA("DMA DONE for %s (buffer %d)\n", s->name,
  127. mdl_ack->id);
  128. if (buf == NULL) {
  129. CX18_WARN("Could not find buf %d for stream %s\n",
  130. mdl_ack->id, s->name);
  131. continue;
  132. }
  133. cx18_buf_sync_for_cpu(s, buf);
  134. if (s->type == CX18_ENC_STREAM_TYPE_TS && s->dvb.enabled) {
  135. CX18_DEBUG_HI_DMA("TS recv bytesused = %d\n",
  136. buf->bytesused);
  137. dvb_dmx_swfilter(&s->dvb.demux, buf->buf,
  138. buf->bytesused);
  139. cx18_buf_sync_for_device(s, buf);
  140. if (s->handle != CX18_INVALID_TASK_HANDLE &&
  141. test_bit(CX18_F_S_STREAMING, &s->s_flags))
  142. cx18_vapi(cx,
  143. CX18_CPU_DE_SET_MDL, 5, s->handle,
  144. (void __iomem *)
  145. &cx->scb->cpu_mdl[buf->id] - cx->enc_mem,
  146. 1, buf->id, s->buf_size);
  147. } else
  148. set_bit(CX18_F_B_NEED_BUF_SWAP, &buf->b_flags);
  149. }
  150. wake_up(&cx->dma_waitq);
  151. if (s->id != -1)
  152. wake_up(&s->waitq);
  153. }
  154. static void epu_debug(struct cx18 *cx, struct cx18_epu_work_order *order)
  155. {
  156. char *p;
  157. char *str = order->str;
  158. CX18_DEBUG_INFO("%x %s\n", order->mb.args[0], str);
  159. p = strchr(str, '.');
  160. if (!test_bit(CX18_F_I_LOADED_FW, &cx->i_flags) && p && p > str)
  161. CX18_INFO("FW version: %s\n", p - 1);
  162. }
  163. static void epu_cmd(struct cx18 *cx, struct cx18_epu_work_order *order)
  164. {
  165. switch (order->rpu) {
  166. case CPU:
  167. {
  168. switch (order->mb.cmd) {
  169. case CX18_EPU_DMA_DONE:
  170. epu_dma_done(cx, order);
  171. break;
  172. case CX18_EPU_DEBUG:
  173. epu_debug(cx, order);
  174. break;
  175. default:
  176. CX18_WARN("Unknown CPU to EPU mailbox command %#0x\n",
  177. order->mb.cmd);
  178. break;
  179. }
  180. break;
  181. }
  182. case APU:
  183. CX18_WARN("Unknown APU to EPU mailbox command %#0x\n",
  184. order->mb.cmd);
  185. break;
  186. default:
  187. break;
  188. }
  189. }
  190. static
  191. void free_epu_work_order(struct cx18 *cx, struct cx18_epu_work_order *order)
  192. {
  193. atomic_set(&order->pending, 0);
  194. }
  195. void cx18_epu_work_handler(struct work_struct *work)
  196. {
  197. struct cx18_epu_work_order *order =
  198. container_of(work, struct cx18_epu_work_order, work);
  199. struct cx18 *cx = order->cx;
  200. epu_cmd(cx, order);
  201. free_epu_work_order(cx, order);
  202. }
  203. /*
  204. * Functions that run in an interrupt handling context
  205. */
  206. static void mb_ack_irq(struct cx18 *cx, const struct cx18_epu_work_order *order)
  207. {
  208. struct cx18_mailbox __iomem *ack_mb;
  209. u32 ack_irq, req;
  210. switch (order->rpu) {
  211. case APU:
  212. ack_irq = IRQ_EPU_TO_APU_ACK;
  213. ack_mb = &cx->scb->apu2epu_mb;
  214. break;
  215. case CPU:
  216. ack_irq = IRQ_EPU_TO_CPU_ACK;
  217. ack_mb = &cx->scb->cpu2epu_mb;
  218. break;
  219. default:
  220. CX18_WARN("Unhandled RPU (%d) for command %x ack\n",
  221. order->rpu, order->mb.cmd);
  222. return;
  223. }
  224. req = order->mb.request;
  225. /* Don't ack if the RPU has gotten impatient and timed us out */
  226. if (req != cx18_readl(cx, &ack_mb->request) ||
  227. req == cx18_readl(cx, &ack_mb->ack))
  228. return;
  229. cx18_writel(cx, req, &ack_mb->ack);
  230. cx18_write_reg_expect(cx, ack_irq, SW2_INT_SET, ack_irq, ack_irq);
  231. return;
  232. }
  233. static int epu_dma_done_irq(struct cx18 *cx, struct cx18_epu_work_order *order,
  234. int stale)
  235. {
  236. u32 handle, mdl_ack_offset, mdl_ack_count;
  237. struct cx18_mailbox *mb;
  238. mb = &order->mb;
  239. handle = mb->args[0];
  240. mdl_ack_offset = mb->args[1];
  241. mdl_ack_count = mb->args[2];
  242. if (handle == CX18_INVALID_TASK_HANDLE ||
  243. mdl_ack_count == 0 || mdl_ack_count > CX18_MAX_MDL_ACKS) {
  244. if (!stale)
  245. mb_ack_irq(cx, order);
  246. return -1;
  247. }
  248. cx18_memcpy_fromio(cx, order->mdl_ack, cx->enc_mem + mdl_ack_offset,
  249. sizeof(struct cx18_mdl_ack) * mdl_ack_count);
  250. if (!stale)
  251. mb_ack_irq(cx, order);
  252. return 1;
  253. }
  254. static
  255. int epu_debug_irq(struct cx18 *cx, struct cx18_epu_work_order *order, int stale)
  256. {
  257. u32 str_offset;
  258. char *str = order->str;
  259. str[0] = '\0';
  260. str_offset = order->mb.args[1];
  261. if (str_offset) {
  262. cx18_setup_page(cx, str_offset);
  263. cx18_memcpy_fromio(cx, str, cx->enc_mem + str_offset, 252);
  264. str[252] = '\0';
  265. cx18_setup_page(cx, SCB_OFFSET);
  266. }
  267. if (!stale)
  268. mb_ack_irq(cx, order);
  269. return str_offset ? 1 : 0;
  270. }
  271. static inline
  272. int epu_cmd_irq(struct cx18 *cx, struct cx18_epu_work_order *order, int stale)
  273. {
  274. int ret = -1;
  275. switch (order->rpu) {
  276. case CPU:
  277. {
  278. switch (order->mb.cmd) {
  279. case CX18_EPU_DMA_DONE:
  280. ret = epu_dma_done_irq(cx, order, stale);
  281. break;
  282. case CX18_EPU_DEBUG:
  283. ret = epu_debug_irq(cx, order, stale);
  284. break;
  285. default:
  286. CX18_WARN("Unknown CPU to EPU mailbox command %#0x\n",
  287. order->mb.cmd);
  288. break;
  289. }
  290. break;
  291. }
  292. case APU:
  293. CX18_WARN("Unknown APU to EPU mailbox command %#0x\n",
  294. order->mb.cmd);
  295. break;
  296. default:
  297. break;
  298. }
  299. return ret;
  300. }
  301. static inline
  302. struct cx18_epu_work_order *alloc_epu_work_order_irq(struct cx18 *cx)
  303. {
  304. int i;
  305. struct cx18_epu_work_order *order = NULL;
  306. for (i = 0; i < CX18_MAX_EPU_WORK_ORDERS; i++) {
  307. /*
  308. * We only need "pending" atomic to inspect its contents,
  309. * and need not do a check and set because:
  310. * 1. Any work handler thread only clears "pending" and only
  311. * on one, particular work order at a time, per handler thread.
  312. * 2. "pending" is only set here, and we're serialized because
  313. * we're called in an IRQ handler context.
  314. */
  315. if (atomic_read(&cx->epu_work_order[i].pending) == 0) {
  316. order = &cx->epu_work_order[i];
  317. atomic_set(&order->pending, 1);
  318. break;
  319. }
  320. }
  321. return order;
  322. }
  323. void cx18_api_epu_cmd_irq(struct cx18 *cx, int rpu)
  324. {
  325. struct cx18_mailbox __iomem *mb;
  326. struct cx18_mailbox *order_mb;
  327. struct cx18_epu_work_order *order;
  328. int stale = 0;
  329. int submit;
  330. switch (rpu) {
  331. case CPU:
  332. mb = &cx->scb->cpu2epu_mb;
  333. break;
  334. case APU:
  335. mb = &cx->scb->apu2epu_mb;
  336. break;
  337. default:
  338. return;
  339. }
  340. order = alloc_epu_work_order_irq(cx);
  341. if (order == NULL) {
  342. CX18_WARN("Unable to find blank work order form to schedule "
  343. "incoming mailbox command processing\n");
  344. return;
  345. }
  346. order->rpu = rpu;
  347. order_mb = &order->mb;
  348. cx18_memcpy_fromio(cx, order_mb, mb, sizeof(struct cx18_mailbox));
  349. if (order_mb->request == order_mb->ack) {
  350. CX18_WARN("Possibly falling behind: %s self-ack'ed our incoming"
  351. " %s to EPU mailbox (sequence no. %u)\n",
  352. rpu_str[rpu], rpu_str[rpu], order_mb->request);
  353. dump_mb(cx, order_mb, "incoming");
  354. stale = 1;
  355. }
  356. /*
  357. * Individual EPU command processing is responsible for ack-ing
  358. * a non-stale mailbox as soon as possible
  359. */
  360. submit = epu_cmd_irq(cx, order, stale);
  361. if (submit > 0) {
  362. queue_work(cx18_work_queue, &order->work);
  363. }
  364. }
  365. /*
  366. * Functions called from a non-interrupt, non work_queue context
  367. */
  368. static void cx18_api_log_ack_delay(struct cx18 *cx, int msecs)
  369. {
  370. if (msecs > CX18_MAX_MB_ACK_DELAY)
  371. msecs = CX18_MAX_MB_ACK_DELAY;
  372. atomic_inc(&cx->mbox_stats.mb_ack_delay[msecs]);
  373. }
  374. static int cx18_api_call(struct cx18 *cx, u32 cmd, int args, u32 data[])
  375. {
  376. const struct cx18_api_info *info = find_api_info(cmd);
  377. u32 state, irq, req, ack, err;
  378. struct cx18_mailbox __iomem *mb;
  379. u32 __iomem *xpu_state;
  380. wait_queue_head_t *waitq;
  381. struct mutex *mb_lock;
  382. long int timeout, ret;
  383. int i;
  384. if (info == NULL) {
  385. CX18_WARN("unknown cmd %x\n", cmd);
  386. return -EINVAL;
  387. }
  388. if (cmd == CX18_CPU_DE_SET_MDL)
  389. CX18_DEBUG_HI_API("%s\n", info->name);
  390. else
  391. CX18_DEBUG_API("%s\n", info->name);
  392. switch (info->rpu) {
  393. case APU:
  394. waitq = &cx->mb_apu_waitq;
  395. mb_lock = &cx->epu2apu_mb_lock;
  396. irq = IRQ_EPU_TO_APU;
  397. mb = &cx->scb->epu2apu_mb;
  398. xpu_state = &cx->scb->apu_state;
  399. break;
  400. case CPU:
  401. waitq = &cx->mb_cpu_waitq;
  402. mb_lock = &cx->epu2cpu_mb_lock;
  403. irq = IRQ_EPU_TO_CPU;
  404. mb = &cx->scb->epu2cpu_mb;
  405. xpu_state = &cx->scb->cpu_state;
  406. break;
  407. default:
  408. CX18_WARN("Unknown RPU (%d) for API call\n", info->rpu);
  409. return -EINVAL;
  410. }
  411. mutex_lock(mb_lock);
  412. /*
  413. * Wait for an in-use mailbox to complete
  414. *
  415. * If the XPU is responding with Ack's, the mailbox shouldn't be in
  416. * a busy state, since we serialize access to it on our end.
  417. *
  418. * If the wait for ack after sending a previous command was interrupted
  419. * by a signal, we may get here and find a busy mailbox. After waiting,
  420. * mark it "not busy" from our end, if the XPU hasn't ack'ed it still.
  421. */
  422. state = cx18_readl(cx, xpu_state);
  423. req = cx18_readl(cx, &mb->request);
  424. timeout = msecs_to_jiffies(20); /* 1 field at 50 Hz vertical refresh */
  425. ret = wait_event_timeout(*waitq,
  426. (ack = cx18_readl(cx, &mb->ack)) == req,
  427. timeout);
  428. if (req != ack) {
  429. /* waited long enough, make the mbox "not busy" from our end */
  430. cx18_writel(cx, req, &mb->ack);
  431. CX18_ERR("mbox was found stuck busy when setting up for %s; "
  432. "clearing busy and trying to proceed\n", info->name);
  433. } else if (ret != timeout)
  434. CX18_DEBUG_API("waited %u usecs for busy mbox to be acked\n",
  435. jiffies_to_usecs(timeout-ret));
  436. /* Build the outgoing mailbox */
  437. req = ((req & 0xfffffffe) == 0xfffffffe) ? 1 : req + 1;
  438. cx18_writel(cx, cmd, &mb->cmd);
  439. for (i = 0; i < args; i++)
  440. cx18_writel(cx, data[i], &mb->args[i]);
  441. cx18_writel(cx, 0, &mb->error);
  442. cx18_writel(cx, req, &mb->request);
  443. cx18_writel(cx, req - 1, &mb->ack); /* ensure ack & req are distinct */
  444. /*
  445. * Notify the XPU and wait for it to send an Ack back
  446. * 21 ms = ~ 0.5 frames at a frame rate of 24 fps
  447. * 42 ms = ~ 1 frame at a frame rate of 24 fps
  448. */
  449. timeout = msecs_to_jiffies((info->flags & API_FAST) ? 21 : 42);
  450. CX18_DEBUG_HI_IRQ("sending interrupt SW1: %x to send %s\n",
  451. irq, info->name);
  452. cx18_write_reg_expect(cx, irq, SW1_INT_SET, irq, irq);
  453. ret = wait_event_timeout(
  454. *waitq,
  455. cx18_readl(cx, &mb->ack) == cx18_readl(cx, &mb->request),
  456. timeout);
  457. if (ret == 0) {
  458. /* Timed out */
  459. mutex_unlock(mb_lock);
  460. i = jiffies_to_msecs(timeout);
  461. cx18_api_log_ack_delay(cx, i);
  462. CX18_WARN("sending %s timed out waiting %d msecs for RPU "
  463. "acknowledgement\n", info->name, i);
  464. return -EINVAL;
  465. } else if (ret < 0) {
  466. /* Interrupted */
  467. mutex_unlock(mb_lock);
  468. CX18_WARN("sending %s was interrupted waiting for RPU"
  469. "acknowledgement\n", info->name);
  470. return -EINTR;
  471. }
  472. i = jiffies_to_msecs(timeout-ret);
  473. cx18_api_log_ack_delay(cx, i);
  474. if (ret != timeout)
  475. CX18_DEBUG_HI_API("waited %u msecs for %s to be acked\n",
  476. i, info->name);
  477. /* Collect data returned by the XPU */
  478. for (i = 0; i < MAX_MB_ARGUMENTS; i++)
  479. data[i] = cx18_readl(cx, &mb->args[i]);
  480. err = cx18_readl(cx, &mb->error);
  481. mutex_unlock(mb_lock);
  482. /*
  483. * Wait for XPU to perform extra actions for the caller in some cases.
  484. * e.g. CX18_CPU_DE_RELEASE_MDL will cause the CPU to send all buffers
  485. * back in a burst shortly thereafter
  486. */
  487. if (info->flags & API_SLOW)
  488. cx18_msleep_timeout(300, 0);
  489. if (err)
  490. CX18_DEBUG_API("mailbox error %08x for command %s\n", err,
  491. info->name);
  492. return err ? -EIO : 0;
  493. }
  494. int cx18_api(struct cx18 *cx, u32 cmd, int args, u32 data[])
  495. {
  496. return cx18_api_call(cx, cmd, args, data);
  497. }
  498. static int cx18_set_filter_param(struct cx18_stream *s)
  499. {
  500. struct cx18 *cx = s->cx;
  501. u32 mode;
  502. int ret;
  503. mode = (cx->filter_mode & 1) ? 2 : (cx->spatial_strength ? 1 : 0);
  504. ret = cx18_vapi(cx, CX18_CPU_SET_FILTER_PARAM, 4,
  505. s->handle, 1, mode, cx->spatial_strength);
  506. mode = (cx->filter_mode & 2) ? 2 : (cx->temporal_strength ? 1 : 0);
  507. ret = ret ? ret : cx18_vapi(cx, CX18_CPU_SET_FILTER_PARAM, 4,
  508. s->handle, 0, mode, cx->temporal_strength);
  509. ret = ret ? ret : cx18_vapi(cx, CX18_CPU_SET_FILTER_PARAM, 4,
  510. s->handle, 2, cx->filter_mode >> 2, 0);
  511. return ret;
  512. }
  513. int cx18_api_func(void *priv, u32 cmd, int in, int out,
  514. u32 data[CX2341X_MBOX_MAX_DATA])
  515. {
  516. struct cx18 *cx = priv;
  517. struct cx18_stream *s = &cx->streams[CX18_ENC_STREAM_TYPE_MPG];
  518. switch (cmd) {
  519. case CX2341X_ENC_SET_OUTPUT_PORT:
  520. return 0;
  521. case CX2341X_ENC_SET_FRAME_RATE:
  522. return cx18_vapi(cx, CX18_CPU_SET_VIDEO_IN, 6,
  523. s->handle, 0, 0, 0, 0, data[0]);
  524. case CX2341X_ENC_SET_FRAME_SIZE:
  525. return cx18_vapi(cx, CX18_CPU_SET_VIDEO_RESOLUTION, 3,
  526. s->handle, data[1], data[0]);
  527. case CX2341X_ENC_SET_STREAM_TYPE:
  528. return cx18_vapi(cx, CX18_CPU_SET_STREAM_OUTPUT_TYPE, 2,
  529. s->handle, data[0]);
  530. case CX2341X_ENC_SET_ASPECT_RATIO:
  531. return cx18_vapi(cx, CX18_CPU_SET_ASPECT_RATIO, 2,
  532. s->handle, data[0]);
  533. case CX2341X_ENC_SET_GOP_PROPERTIES:
  534. return cx18_vapi(cx, CX18_CPU_SET_GOP_STRUCTURE, 3,
  535. s->handle, data[0], data[1]);
  536. case CX2341X_ENC_SET_GOP_CLOSURE:
  537. return 0;
  538. case CX2341X_ENC_SET_AUDIO_PROPERTIES:
  539. return cx18_vapi(cx, CX18_CPU_SET_AUDIO_PARAMETERS, 2,
  540. s->handle, data[0]);
  541. case CX2341X_ENC_MUTE_AUDIO:
  542. return cx18_vapi(cx, CX18_CPU_SET_AUDIO_MUTE, 2,
  543. s->handle, data[0]);
  544. case CX2341X_ENC_SET_BIT_RATE:
  545. return cx18_vapi(cx, CX18_CPU_SET_VIDEO_RATE, 5,
  546. s->handle, data[0], data[1], data[2], data[3]);
  547. case CX2341X_ENC_MUTE_VIDEO:
  548. return cx18_vapi(cx, CX18_CPU_SET_VIDEO_MUTE, 2,
  549. s->handle, data[0]);
  550. case CX2341X_ENC_SET_FRAME_DROP_RATE:
  551. return cx18_vapi(cx, CX18_CPU_SET_SKIP_INPUT_FRAME, 2,
  552. s->handle, data[0]);
  553. case CX2341X_ENC_MISC:
  554. return cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 4,
  555. s->handle, data[0], data[1], data[2]);
  556. case CX2341X_ENC_SET_DNR_FILTER_MODE:
  557. cx->filter_mode = (data[0] & 3) | (data[1] << 2);
  558. return cx18_set_filter_param(s);
  559. case CX2341X_ENC_SET_DNR_FILTER_PROPS:
  560. cx->spatial_strength = data[0];
  561. cx->temporal_strength = data[1];
  562. return cx18_set_filter_param(s);
  563. case CX2341X_ENC_SET_SPATIAL_FILTER_TYPE:
  564. return cx18_vapi(cx, CX18_CPU_SET_SPATIAL_FILTER_TYPE, 3,
  565. s->handle, data[0], data[1]);
  566. case CX2341X_ENC_SET_CORING_LEVELS:
  567. return cx18_vapi(cx, CX18_CPU_SET_MEDIAN_CORING, 5,
  568. s->handle, data[0], data[1], data[2], data[3]);
  569. }
  570. CX18_WARN("Unknown cmd %x\n", cmd);
  571. return 0;
  572. }
  573. int cx18_vapi_result(struct cx18 *cx, u32 data[MAX_MB_ARGUMENTS],
  574. u32 cmd, int args, ...)
  575. {
  576. va_list ap;
  577. int i;
  578. va_start(ap, args);
  579. for (i = 0; i < args; i++)
  580. data[i] = va_arg(ap, u32);
  581. va_end(ap);
  582. return cx18_api(cx, cmd, args, data);
  583. }
  584. int cx18_vapi(struct cx18 *cx, u32 cmd, int args, ...)
  585. {
  586. u32 data[MAX_MB_ARGUMENTS];
  587. va_list ap;
  588. int i;
  589. if (cx == NULL) {
  590. CX18_ERR("cx == NULL (cmd=%x)\n", cmd);
  591. return 0;
  592. }
  593. if (args > MAX_MB_ARGUMENTS) {
  594. CX18_ERR("args too big (cmd=%x)\n", cmd);
  595. args = MAX_MB_ARGUMENTS;
  596. }
  597. va_start(ap, args);
  598. for (i = 0; i < args; i++)
  599. data[i] = va_arg(ap, u32);
  600. va_end(ap);
  601. return cx18_api(cx, cmd, args, data);
  602. }