cx18-fileops.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890
  1. /*
  2. * cx18 file operation functions
  3. *
  4. * Derived from ivtv-fileops.c
  5. *
  6. * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl>
  7. * Copyright (C) 2008 Andy Walls <awalls@md.metrocast.net>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  22. * 02111-1307 USA
  23. */
  24. #include "cx18-driver.h"
  25. #include "cx18-fileops.h"
  26. #include "cx18-i2c.h"
  27. #include "cx18-queue.h"
  28. #include "cx18-vbi.h"
  29. #include "cx18-audio.h"
  30. #include "cx18-mailbox.h"
  31. #include "cx18-scb.h"
  32. #include "cx18-streams.h"
  33. #include "cx18-controls.h"
  34. #include "cx18-ioctl.h"
  35. #include "cx18-cards.h"
  36. #include <media/v4l2-event.h>
  37. /* This function tries to claim the stream for a specific file descriptor.
  38. If no one else is using this stream then the stream is claimed and
  39. associated VBI and IDX streams are also automatically claimed.
  40. Possible error returns: -EBUSY if someone else has claimed
  41. the stream or 0 on success. */
  42. int cx18_claim_stream(struct cx18_open_id *id, int type)
  43. {
  44. struct cx18 *cx = id->cx;
  45. struct cx18_stream *s = &cx->streams[type];
  46. struct cx18_stream *s_assoc;
  47. /* Nothing should ever try to directly claim the IDX stream */
  48. if (type == CX18_ENC_STREAM_TYPE_IDX) {
  49. CX18_WARN("MPEG Index stream cannot be claimed "
  50. "directly, but something tried.\n");
  51. return -EINVAL;
  52. }
  53. if (test_and_set_bit(CX18_F_S_CLAIMED, &s->s_flags)) {
  54. /* someone already claimed this stream */
  55. if (s->id == id->open_id) {
  56. /* yes, this file descriptor did. So that's OK. */
  57. return 0;
  58. }
  59. if (s->id == -1 && type == CX18_ENC_STREAM_TYPE_VBI) {
  60. /* VBI is handled already internally, now also assign
  61. the file descriptor to this stream for external
  62. reading of the stream. */
  63. s->id = id->open_id;
  64. CX18_DEBUG_INFO("Start Read VBI\n");
  65. return 0;
  66. }
  67. /* someone else is using this stream already */
  68. CX18_DEBUG_INFO("Stream %d is busy\n", type);
  69. return -EBUSY;
  70. }
  71. s->id = id->open_id;
  72. /*
  73. * CX18_ENC_STREAM_TYPE_MPG needs to claim:
  74. * CX18_ENC_STREAM_TYPE_VBI, if VBI insertion is on for sliced VBI, or
  75. * CX18_ENC_STREAM_TYPE_IDX, if VBI insertion is off for sliced VBI
  76. * (We don't yet fix up MPEG Index entries for our inserted packets).
  77. *
  78. * For all other streams we're done.
  79. */
  80. if (type != CX18_ENC_STREAM_TYPE_MPG)
  81. return 0;
  82. s_assoc = &cx->streams[CX18_ENC_STREAM_TYPE_IDX];
  83. if (cx->vbi.insert_mpeg && !cx18_raw_vbi(cx))
  84. s_assoc = &cx->streams[CX18_ENC_STREAM_TYPE_VBI];
  85. else if (!cx18_stream_enabled(s_assoc))
  86. return 0;
  87. set_bit(CX18_F_S_CLAIMED, &s_assoc->s_flags);
  88. /* mark that it is used internally */
  89. set_bit(CX18_F_S_INTERNAL_USE, &s_assoc->s_flags);
  90. return 0;
  91. }
  92. EXPORT_SYMBOL(cx18_claim_stream);
  93. /* This function releases a previously claimed stream. It will take into
  94. account associated VBI streams. */
  95. void cx18_release_stream(struct cx18_stream *s)
  96. {
  97. struct cx18 *cx = s->cx;
  98. struct cx18_stream *s_assoc;
  99. s->id = -1;
  100. if (s->type == CX18_ENC_STREAM_TYPE_IDX) {
  101. /*
  102. * The IDX stream is only used internally, and can
  103. * only be indirectly unclaimed by unclaiming the MPG stream.
  104. */
  105. return;
  106. }
  107. if (s->type == CX18_ENC_STREAM_TYPE_VBI &&
  108. test_bit(CX18_F_S_INTERNAL_USE, &s->s_flags)) {
  109. /* this stream is still in use internally */
  110. return;
  111. }
  112. if (!test_and_clear_bit(CX18_F_S_CLAIMED, &s->s_flags)) {
  113. CX18_DEBUG_WARN("Release stream %s not in use!\n", s->name);
  114. return;
  115. }
  116. cx18_flush_queues(s);
  117. /*
  118. * CX18_ENC_STREAM_TYPE_MPG needs to release the
  119. * CX18_ENC_STREAM_TYPE_VBI and/or CX18_ENC_STREAM_TYPE_IDX streams.
  120. *
  121. * For all other streams we're done.
  122. */
  123. if (s->type != CX18_ENC_STREAM_TYPE_MPG)
  124. return;
  125. /* Unclaim the associated MPEG Index stream */
  126. s_assoc = &cx->streams[CX18_ENC_STREAM_TYPE_IDX];
  127. if (test_and_clear_bit(CX18_F_S_INTERNAL_USE, &s_assoc->s_flags)) {
  128. clear_bit(CX18_F_S_CLAIMED, &s_assoc->s_flags);
  129. cx18_flush_queues(s_assoc);
  130. }
  131. /* Unclaim the associated VBI stream */
  132. s_assoc = &cx->streams[CX18_ENC_STREAM_TYPE_VBI];
  133. if (test_and_clear_bit(CX18_F_S_INTERNAL_USE, &s_assoc->s_flags)) {
  134. if (s_assoc->id == -1) {
  135. /*
  136. * The VBI stream is not still claimed by a file
  137. * descriptor, so completely unclaim it.
  138. */
  139. clear_bit(CX18_F_S_CLAIMED, &s_assoc->s_flags);
  140. cx18_flush_queues(s_assoc);
  141. }
  142. }
  143. }
  144. EXPORT_SYMBOL(cx18_release_stream);
  145. static void cx18_dualwatch(struct cx18 *cx)
  146. {
  147. struct v4l2_tuner vt;
  148. u32 new_stereo_mode;
  149. const u32 dual = 0x0200;
  150. new_stereo_mode = v4l2_ctrl_g_ctrl(cx->cxhdl.audio_mode);
  151. memset(&vt, 0, sizeof(vt));
  152. cx18_call_all(cx, tuner, g_tuner, &vt);
  153. if (vt.audmode == V4L2_TUNER_MODE_LANG1_LANG2 &&
  154. (vt.rxsubchans & V4L2_TUNER_SUB_LANG2))
  155. new_stereo_mode = dual;
  156. if (new_stereo_mode == cx->dualwatch_stereo_mode)
  157. return;
  158. CX18_DEBUG_INFO("dualwatch: change stereo flag from 0x%x to 0x%x.\n",
  159. cx->dualwatch_stereo_mode, new_stereo_mode);
  160. if (v4l2_ctrl_s_ctrl(cx->cxhdl.audio_mode, new_stereo_mode))
  161. CX18_DEBUG_INFO("dualwatch: changing stereo flag failed\n");
  162. }
  163. static struct cx18_mdl *cx18_get_mdl(struct cx18_stream *s, int non_block,
  164. int *err)
  165. {
  166. struct cx18 *cx = s->cx;
  167. struct cx18_stream *s_vbi = &cx->streams[CX18_ENC_STREAM_TYPE_VBI];
  168. struct cx18_mdl *mdl;
  169. DEFINE_WAIT(wait);
  170. *err = 0;
  171. while (1) {
  172. if (s->type == CX18_ENC_STREAM_TYPE_MPG) {
  173. /* Process pending program updates and VBI data */
  174. if (time_after(jiffies, cx->dualwatch_jiffies + msecs_to_jiffies(1000))) {
  175. cx->dualwatch_jiffies = jiffies;
  176. cx18_dualwatch(cx);
  177. }
  178. if (test_bit(CX18_F_S_INTERNAL_USE, &s_vbi->s_flags) &&
  179. !test_bit(CX18_F_S_APPL_IO, &s_vbi->s_flags)) {
  180. while ((mdl = cx18_dequeue(s_vbi,
  181. &s_vbi->q_full))) {
  182. /* byteswap and process VBI data */
  183. cx18_process_vbi_data(cx, mdl,
  184. s_vbi->type);
  185. cx18_stream_put_mdl_fw(s_vbi, mdl);
  186. }
  187. }
  188. mdl = &cx->vbi.sliced_mpeg_mdl;
  189. if (mdl->readpos != mdl->bytesused)
  190. return mdl;
  191. }
  192. /* do we have new data? */
  193. mdl = cx18_dequeue(s, &s->q_full);
  194. if (mdl) {
  195. if (!test_and_clear_bit(CX18_F_M_NEED_SWAP,
  196. &mdl->m_flags))
  197. return mdl;
  198. if (s->type == CX18_ENC_STREAM_TYPE_MPG)
  199. /* byteswap MPG data */
  200. cx18_mdl_swap(mdl);
  201. else {
  202. /* byteswap and process VBI data */
  203. cx18_process_vbi_data(cx, mdl, s->type);
  204. }
  205. return mdl;
  206. }
  207. /* return if end of stream */
  208. if (!test_bit(CX18_F_S_STREAMING, &s->s_flags)) {
  209. CX18_DEBUG_INFO("EOS %s\n", s->name);
  210. return NULL;
  211. }
  212. /* return if file was opened with O_NONBLOCK */
  213. if (non_block) {
  214. *err = -EAGAIN;
  215. return NULL;
  216. }
  217. /* wait for more data to arrive */
  218. prepare_to_wait(&s->waitq, &wait, TASK_INTERRUPTIBLE);
  219. /* New buffers might have become available before we were added
  220. to the waitqueue */
  221. if (!atomic_read(&s->q_full.depth))
  222. schedule();
  223. finish_wait(&s->waitq, &wait);
  224. if (signal_pending(current)) {
  225. /* return if a signal was received */
  226. CX18_DEBUG_INFO("User stopped %s\n", s->name);
  227. *err = -EINTR;
  228. return NULL;
  229. }
  230. }
  231. }
  232. static void cx18_setup_sliced_vbi_mdl(struct cx18 *cx)
  233. {
  234. struct cx18_mdl *mdl = &cx->vbi.sliced_mpeg_mdl;
  235. struct cx18_buffer *buf = &cx->vbi.sliced_mpeg_buf;
  236. int idx = cx->vbi.inserted_frame % CX18_VBI_FRAMES;
  237. buf->buf = cx->vbi.sliced_mpeg_data[idx];
  238. buf->bytesused = cx->vbi.sliced_mpeg_size[idx];
  239. buf->readpos = 0;
  240. mdl->curr_buf = NULL;
  241. mdl->bytesused = cx->vbi.sliced_mpeg_size[idx];
  242. mdl->readpos = 0;
  243. }
  244. static size_t cx18_copy_buf_to_user(struct cx18_stream *s,
  245. struct cx18_buffer *buf, char __user *ubuf, size_t ucount, bool *stop)
  246. {
  247. struct cx18 *cx = s->cx;
  248. size_t len = buf->bytesused - buf->readpos;
  249. *stop = false;
  250. if (len > ucount)
  251. len = ucount;
  252. if (cx->vbi.insert_mpeg && s->type == CX18_ENC_STREAM_TYPE_MPG &&
  253. !cx18_raw_vbi(cx) && buf != &cx->vbi.sliced_mpeg_buf) {
  254. /*
  255. * Try to find a good splice point in the PS, just before
  256. * an MPEG-2 Program Pack start code, and provide only
  257. * up to that point to the user, so it's easy to insert VBI data
  258. * the next time around.
  259. *
  260. * This will not work for an MPEG-2 TS and has only been
  261. * verified by analysis to work for an MPEG-2 PS. Helen Buus
  262. * pointed out this works for the CX23416 MPEG-2 DVD compatible
  263. * stream, and research indicates both the MPEG 2 SVCD and DVD
  264. * stream types use an MPEG-2 PS container.
  265. */
  266. /*
  267. * An MPEG-2 Program Stream (PS) is a series of
  268. * MPEG-2 Program Packs terminated by an
  269. * MPEG Program End Code after the last Program Pack.
  270. * A Program Pack may hold a PS System Header packet and any
  271. * number of Program Elementary Stream (PES) Packets
  272. */
  273. const char *start = buf->buf + buf->readpos;
  274. const char *p = start + 1;
  275. const u8 *q;
  276. u8 ch = cx->search_pack_header ? 0xba : 0xe0;
  277. int stuffing, i;
  278. while (start + len > p) {
  279. /* Scan for a 0 to find a potential MPEG-2 start code */
  280. q = memchr(p, 0, start + len - p);
  281. if (q == NULL)
  282. break;
  283. p = q + 1;
  284. /*
  285. * Keep looking if not a
  286. * MPEG-2 Pack header start code: 0x00 0x00 0x01 0xba
  287. * or MPEG-2 video PES start code: 0x00 0x00 0x01 0xe0
  288. */
  289. if ((char *)q + 15 >= buf->buf + buf->bytesused ||
  290. q[1] != 0 || q[2] != 1 || q[3] != ch)
  291. continue;
  292. /* If expecting the primary video PES */
  293. if (!cx->search_pack_header) {
  294. /* Continue if it couldn't be a PES packet */
  295. if ((q[6] & 0xc0) != 0x80)
  296. continue;
  297. /* Check if a PTS or PTS & DTS follow */
  298. if (((q[7] & 0xc0) == 0x80 && /* PTS only */
  299. (q[9] & 0xf0) == 0x20) || /* PTS only */
  300. ((q[7] & 0xc0) == 0xc0 && /* PTS & DTS */
  301. (q[9] & 0xf0) == 0x30)) { /* DTS follows */
  302. /* Assume we found the video PES hdr */
  303. ch = 0xba; /* next want a Program Pack*/
  304. cx->search_pack_header = 1;
  305. p = q + 9; /* Skip this video PES hdr */
  306. }
  307. continue;
  308. }
  309. /* We may have found a Program Pack start code */
  310. /* Get the count of stuffing bytes & verify them */
  311. stuffing = q[13] & 7;
  312. /* all stuffing bytes must be 0xff */
  313. for (i = 0; i < stuffing; i++)
  314. if (q[14 + i] != 0xff)
  315. break;
  316. if (i == stuffing && /* right number of stuffing bytes*/
  317. (q[4] & 0xc4) == 0x44 && /* marker check */
  318. (q[12] & 3) == 3 && /* marker check */
  319. q[14 + stuffing] == 0 && /* PES Pack or Sys Hdr */
  320. q[15 + stuffing] == 0 &&
  321. q[16 + stuffing] == 1) {
  322. /* We declare we actually found a Program Pack*/
  323. cx->search_pack_header = 0; /* expect vid PES */
  324. len = (char *)q - start;
  325. cx18_setup_sliced_vbi_mdl(cx);
  326. *stop = true;
  327. break;
  328. }
  329. }
  330. }
  331. if (copy_to_user(ubuf, (u8 *)buf->buf + buf->readpos, len)) {
  332. CX18_DEBUG_WARN("copy %zd bytes to user failed for %s\n",
  333. len, s->name);
  334. return -EFAULT;
  335. }
  336. buf->readpos += len;
  337. if (s->type == CX18_ENC_STREAM_TYPE_MPG &&
  338. buf != &cx->vbi.sliced_mpeg_buf)
  339. cx->mpg_data_received += len;
  340. return len;
  341. }
  342. static size_t cx18_copy_mdl_to_user(struct cx18_stream *s,
  343. struct cx18_mdl *mdl, char __user *ubuf, size_t ucount)
  344. {
  345. size_t tot_written = 0;
  346. int rc;
  347. bool stop = false;
  348. if (mdl->curr_buf == NULL)
  349. mdl->curr_buf = list_first_entry(&mdl->buf_list,
  350. struct cx18_buffer, list);
  351. if (list_entry_is_past_end(mdl->curr_buf, &mdl->buf_list, list)) {
  352. /*
  353. * For some reason we've exhausted the buffers, but the MDL
  354. * object still said some data was unread.
  355. * Fix that and bail out.
  356. */
  357. mdl->readpos = mdl->bytesused;
  358. return 0;
  359. }
  360. list_for_each_entry_from(mdl->curr_buf, &mdl->buf_list, list) {
  361. if (mdl->curr_buf->readpos >= mdl->curr_buf->bytesused)
  362. continue;
  363. rc = cx18_copy_buf_to_user(s, mdl->curr_buf, ubuf + tot_written,
  364. ucount - tot_written, &stop);
  365. if (rc < 0)
  366. return rc;
  367. mdl->readpos += rc;
  368. tot_written += rc;
  369. if (stop || /* Forced stopping point for VBI insertion */
  370. tot_written >= ucount || /* Reader request statisfied */
  371. mdl->curr_buf->readpos < mdl->curr_buf->bytesused ||
  372. mdl->readpos >= mdl->bytesused) /* MDL buffers drained */
  373. break;
  374. }
  375. return tot_written;
  376. }
  377. static ssize_t cx18_read(struct cx18_stream *s, char __user *ubuf,
  378. size_t tot_count, int non_block)
  379. {
  380. struct cx18 *cx = s->cx;
  381. size_t tot_written = 0;
  382. int single_frame = 0;
  383. if (atomic_read(&cx->ana_capturing) == 0 && s->id == -1) {
  384. /* shouldn't happen */
  385. CX18_DEBUG_WARN("Stream %s not initialized before read\n",
  386. s->name);
  387. return -EIO;
  388. }
  389. /* Each VBI buffer is one frame, the v4l2 API says that for VBI the
  390. frames should arrive one-by-one, so make sure we never output more
  391. than one VBI frame at a time */
  392. if (s->type == CX18_ENC_STREAM_TYPE_VBI && !cx18_raw_vbi(cx))
  393. single_frame = 1;
  394. for (;;) {
  395. struct cx18_mdl *mdl;
  396. int rc;
  397. mdl = cx18_get_mdl(s, non_block, &rc);
  398. /* if there is no data available... */
  399. if (mdl == NULL) {
  400. /* if we got data, then return that regardless */
  401. if (tot_written)
  402. break;
  403. /* EOS condition */
  404. if (rc == 0) {
  405. clear_bit(CX18_F_S_STREAMOFF, &s->s_flags);
  406. clear_bit(CX18_F_S_APPL_IO, &s->s_flags);
  407. cx18_release_stream(s);
  408. }
  409. /* set errno */
  410. return rc;
  411. }
  412. rc = cx18_copy_mdl_to_user(s, mdl, ubuf + tot_written,
  413. tot_count - tot_written);
  414. if (mdl != &cx->vbi.sliced_mpeg_mdl) {
  415. if (mdl->readpos == mdl->bytesused)
  416. cx18_stream_put_mdl_fw(s, mdl);
  417. else
  418. cx18_push(s, mdl, &s->q_full);
  419. } else if (mdl->readpos == mdl->bytesused) {
  420. int idx = cx->vbi.inserted_frame % CX18_VBI_FRAMES;
  421. cx->vbi.sliced_mpeg_size[idx] = 0;
  422. cx->vbi.inserted_frame++;
  423. cx->vbi_data_inserted += mdl->bytesused;
  424. }
  425. if (rc < 0)
  426. return rc;
  427. tot_written += rc;
  428. if (tot_written == tot_count || single_frame)
  429. break;
  430. }
  431. return tot_written;
  432. }
  433. static ssize_t cx18_read_pos(struct cx18_stream *s, char __user *ubuf,
  434. size_t count, loff_t *pos, int non_block)
  435. {
  436. ssize_t rc = count ? cx18_read(s, ubuf, count, non_block) : 0;
  437. struct cx18 *cx = s->cx;
  438. CX18_DEBUG_HI_FILE("read %zd from %s, got %zd\n", count, s->name, rc);
  439. if (rc > 0)
  440. pos += rc;
  441. return rc;
  442. }
  443. int cx18_start_capture(struct cx18_open_id *id)
  444. {
  445. struct cx18 *cx = id->cx;
  446. struct cx18_stream *s = &cx->streams[id->type];
  447. struct cx18_stream *s_vbi;
  448. struct cx18_stream *s_idx;
  449. if (s->type == CX18_ENC_STREAM_TYPE_RAD) {
  450. /* you cannot read from these stream types. */
  451. return -EPERM;
  452. }
  453. /* Try to claim this stream. */
  454. if (cx18_claim_stream(id, s->type))
  455. return -EBUSY;
  456. /* If capture is already in progress, then we also have to
  457. do nothing extra. */
  458. if (test_bit(CX18_F_S_STREAMOFF, &s->s_flags) ||
  459. test_and_set_bit(CX18_F_S_STREAMING, &s->s_flags)) {
  460. set_bit(CX18_F_S_APPL_IO, &s->s_flags);
  461. return 0;
  462. }
  463. /* Start associated VBI or IDX stream capture if required */
  464. s_vbi = &cx->streams[CX18_ENC_STREAM_TYPE_VBI];
  465. s_idx = &cx->streams[CX18_ENC_STREAM_TYPE_IDX];
  466. if (s->type == CX18_ENC_STREAM_TYPE_MPG) {
  467. /*
  468. * The VBI and IDX streams should have been claimed
  469. * automatically, if for internal use, when the MPG stream was
  470. * claimed. We only need to start these streams capturing.
  471. */
  472. if (test_bit(CX18_F_S_INTERNAL_USE, &s_idx->s_flags) &&
  473. !test_and_set_bit(CX18_F_S_STREAMING, &s_idx->s_flags)) {
  474. if (cx18_start_v4l2_encode_stream(s_idx)) {
  475. CX18_DEBUG_WARN("IDX capture start failed\n");
  476. clear_bit(CX18_F_S_STREAMING, &s_idx->s_flags);
  477. goto start_failed;
  478. }
  479. CX18_DEBUG_INFO("IDX capture started\n");
  480. }
  481. if (test_bit(CX18_F_S_INTERNAL_USE, &s_vbi->s_flags) &&
  482. !test_and_set_bit(CX18_F_S_STREAMING, &s_vbi->s_flags)) {
  483. if (cx18_start_v4l2_encode_stream(s_vbi)) {
  484. CX18_DEBUG_WARN("VBI capture start failed\n");
  485. clear_bit(CX18_F_S_STREAMING, &s_vbi->s_flags);
  486. goto start_failed;
  487. }
  488. CX18_DEBUG_INFO("VBI insertion started\n");
  489. }
  490. }
  491. /* Tell the card to start capturing */
  492. if (!cx18_start_v4l2_encode_stream(s)) {
  493. /* We're done */
  494. set_bit(CX18_F_S_APPL_IO, &s->s_flags);
  495. /* Resume a possibly paused encoder */
  496. if (test_and_clear_bit(CX18_F_I_ENC_PAUSED, &cx->i_flags))
  497. cx18_vapi(cx, CX18_CPU_CAPTURE_PAUSE, 1, s->handle);
  498. return 0;
  499. }
  500. start_failed:
  501. CX18_DEBUG_WARN("Failed to start capturing for stream %s\n", s->name);
  502. /*
  503. * The associated VBI and IDX streams for internal use are released
  504. * automatically when the MPG stream is released. We only need to stop
  505. * the associated stream.
  506. */
  507. if (s->type == CX18_ENC_STREAM_TYPE_MPG) {
  508. /* Stop the IDX stream which is always for internal use */
  509. if (test_bit(CX18_F_S_STREAMING, &s_idx->s_flags)) {
  510. cx18_stop_v4l2_encode_stream(s_idx, 0);
  511. clear_bit(CX18_F_S_STREAMING, &s_idx->s_flags);
  512. }
  513. /* Stop the VBI stream, if only running for internal use */
  514. if (test_bit(CX18_F_S_STREAMING, &s_vbi->s_flags) &&
  515. !test_bit(CX18_F_S_APPL_IO, &s_vbi->s_flags)) {
  516. cx18_stop_v4l2_encode_stream(s_vbi, 0);
  517. clear_bit(CX18_F_S_STREAMING, &s_vbi->s_flags);
  518. }
  519. }
  520. clear_bit(CX18_F_S_STREAMING, &s->s_flags);
  521. cx18_release_stream(s); /* Also releases associated streams */
  522. return -EIO;
  523. }
  524. ssize_t cx18_v4l2_read(struct file *filp, char __user *buf, size_t count,
  525. loff_t *pos)
  526. {
  527. struct cx18_open_id *id = file2id(filp);
  528. struct cx18 *cx = id->cx;
  529. struct cx18_stream *s = &cx->streams[id->type];
  530. int rc;
  531. CX18_DEBUG_HI_FILE("read %zd bytes from %s\n", count, s->name);
  532. mutex_lock(&cx->serialize_lock);
  533. rc = cx18_start_capture(id);
  534. mutex_unlock(&cx->serialize_lock);
  535. if (rc)
  536. return rc;
  537. if ((s->vb_type == V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
  538. (id->type == CX18_ENC_STREAM_TYPE_YUV)) {
  539. return videobuf_read_stream(&s->vbuf_q, buf, count, pos, 0,
  540. filp->f_flags & O_NONBLOCK);
  541. }
  542. return cx18_read_pos(s, buf, count, pos, filp->f_flags & O_NONBLOCK);
  543. }
  544. unsigned int cx18_v4l2_enc_poll(struct file *filp, poll_table *wait)
  545. {
  546. unsigned long req_events = poll_requested_events(wait);
  547. struct cx18_open_id *id = file2id(filp);
  548. struct cx18 *cx = id->cx;
  549. struct cx18_stream *s = &cx->streams[id->type];
  550. int eof = test_bit(CX18_F_S_STREAMOFF, &s->s_flags);
  551. unsigned res = 0;
  552. /* Start a capture if there is none */
  553. if (!eof && !test_bit(CX18_F_S_STREAMING, &s->s_flags) &&
  554. (req_events & (POLLIN | POLLRDNORM))) {
  555. int rc;
  556. mutex_lock(&cx->serialize_lock);
  557. rc = cx18_start_capture(id);
  558. mutex_unlock(&cx->serialize_lock);
  559. if (rc) {
  560. CX18_DEBUG_INFO("Could not start capture for %s (%d)\n",
  561. s->name, rc);
  562. return POLLERR;
  563. }
  564. CX18_DEBUG_FILE("Encoder poll started capture\n");
  565. }
  566. if ((s->vb_type == V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
  567. (id->type == CX18_ENC_STREAM_TYPE_YUV)) {
  568. int videobuf_poll = videobuf_poll_stream(filp, &s->vbuf_q, wait);
  569. if (v4l2_event_pending(&id->fh))
  570. res |= POLLPRI;
  571. if (eof && videobuf_poll == POLLERR)
  572. return res | POLLHUP;
  573. return res | videobuf_poll;
  574. }
  575. /* add stream's waitq to the poll list */
  576. CX18_DEBUG_HI_FILE("Encoder poll\n");
  577. if (v4l2_event_pending(&id->fh))
  578. res |= POLLPRI;
  579. else
  580. poll_wait(filp, &s->waitq, wait);
  581. if (atomic_read(&s->q_full.depth))
  582. return res | POLLIN | POLLRDNORM;
  583. if (eof)
  584. return res | POLLHUP;
  585. return res;
  586. }
  587. int cx18_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
  588. {
  589. struct cx18_open_id *id = file->private_data;
  590. struct cx18 *cx = id->cx;
  591. struct cx18_stream *s = &cx->streams[id->type];
  592. int eof = test_bit(CX18_F_S_STREAMOFF, &s->s_flags);
  593. if ((s->vb_type == V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
  594. (id->type == CX18_ENC_STREAM_TYPE_YUV)) {
  595. /* Start a capture if there is none */
  596. if (!eof && !test_bit(CX18_F_S_STREAMING, &s->s_flags)) {
  597. int rc;
  598. mutex_lock(&cx->serialize_lock);
  599. rc = cx18_start_capture(id);
  600. mutex_unlock(&cx->serialize_lock);
  601. if (rc) {
  602. CX18_DEBUG_INFO(
  603. "Could not start capture for %s (%d)\n",
  604. s->name, rc);
  605. return -EINVAL;
  606. }
  607. CX18_DEBUG_FILE("Encoder mmap started capture\n");
  608. }
  609. return videobuf_mmap_mapper(&s->vbuf_q, vma);
  610. }
  611. return -EINVAL;
  612. }
  613. void cx18_vb_timeout(unsigned long data)
  614. {
  615. struct cx18_stream *s = (struct cx18_stream *)data;
  616. struct cx18_videobuf_buffer *buf;
  617. unsigned long flags;
  618. /* Return all of the buffers in error state, so the vbi/vid inode
  619. * can return from blocking.
  620. */
  621. spin_lock_irqsave(&s->vb_lock, flags);
  622. while (!list_empty(&s->vb_capture)) {
  623. buf = list_entry(s->vb_capture.next,
  624. struct cx18_videobuf_buffer, vb.queue);
  625. list_del(&buf->vb.queue);
  626. buf->vb.state = VIDEOBUF_ERROR;
  627. wake_up(&buf->vb.done);
  628. }
  629. spin_unlock_irqrestore(&s->vb_lock, flags);
  630. }
  631. void cx18_stop_capture(struct cx18_open_id *id, int gop_end)
  632. {
  633. struct cx18 *cx = id->cx;
  634. struct cx18_stream *s = &cx->streams[id->type];
  635. struct cx18_stream *s_vbi = &cx->streams[CX18_ENC_STREAM_TYPE_VBI];
  636. struct cx18_stream *s_idx = &cx->streams[CX18_ENC_STREAM_TYPE_IDX];
  637. CX18_DEBUG_IOCTL("close() of %s\n", s->name);
  638. /* 'Unclaim' this stream */
  639. /* Stop capturing */
  640. if (test_bit(CX18_F_S_STREAMING, &s->s_flags)) {
  641. CX18_DEBUG_INFO("close stopping capture\n");
  642. if (id->type == CX18_ENC_STREAM_TYPE_MPG) {
  643. /* Stop internal use associated VBI and IDX streams */
  644. if (test_bit(CX18_F_S_STREAMING, &s_vbi->s_flags) &&
  645. !test_bit(CX18_F_S_APPL_IO, &s_vbi->s_flags)) {
  646. CX18_DEBUG_INFO("close stopping embedded VBI "
  647. "capture\n");
  648. cx18_stop_v4l2_encode_stream(s_vbi, 0);
  649. }
  650. if (test_bit(CX18_F_S_STREAMING, &s_idx->s_flags)) {
  651. CX18_DEBUG_INFO("close stopping IDX capture\n");
  652. cx18_stop_v4l2_encode_stream(s_idx, 0);
  653. }
  654. }
  655. if (id->type == CX18_ENC_STREAM_TYPE_VBI &&
  656. test_bit(CX18_F_S_INTERNAL_USE, &s->s_flags))
  657. /* Also used internally, don't stop capturing */
  658. s->id = -1;
  659. else
  660. cx18_stop_v4l2_encode_stream(s, gop_end);
  661. }
  662. if (!gop_end) {
  663. clear_bit(CX18_F_S_APPL_IO, &s->s_flags);
  664. clear_bit(CX18_F_S_STREAMOFF, &s->s_flags);
  665. cx18_release_stream(s);
  666. }
  667. }
  668. int cx18_v4l2_close(struct file *filp)
  669. {
  670. struct v4l2_fh *fh = filp->private_data;
  671. struct cx18_open_id *id = fh2id(fh);
  672. struct cx18 *cx = id->cx;
  673. struct cx18_stream *s = &cx->streams[id->type];
  674. CX18_DEBUG_IOCTL("close() of %s\n", s->name);
  675. mutex_lock(&cx->serialize_lock);
  676. /* Stop radio */
  677. if (id->type == CX18_ENC_STREAM_TYPE_RAD &&
  678. v4l2_fh_is_singular_file(filp)) {
  679. /* Closing radio device, return to TV mode */
  680. cx18_mute(cx);
  681. /* Mark that the radio is no longer in use */
  682. clear_bit(CX18_F_I_RADIO_USER, &cx->i_flags);
  683. /* Switch tuner to TV */
  684. cx18_call_all(cx, video, s_std, cx->std);
  685. /* Select correct audio input (i.e. TV tuner or Line in) */
  686. cx18_audio_set_io(cx);
  687. if (atomic_read(&cx->ana_capturing) > 0) {
  688. /* Undo video mute */
  689. cx18_vapi(cx, CX18_CPU_SET_VIDEO_MUTE, 2, s->handle,
  690. (v4l2_ctrl_g_ctrl(cx->cxhdl.video_mute) |
  691. (v4l2_ctrl_g_ctrl(cx->cxhdl.video_mute_yuv) << 8)));
  692. }
  693. /* Done! Unmute and continue. */
  694. cx18_unmute(cx);
  695. }
  696. v4l2_fh_del(fh);
  697. v4l2_fh_exit(fh);
  698. /* 'Unclaim' this stream */
  699. if (s->id == id->open_id)
  700. cx18_stop_capture(id, 0);
  701. kfree(id);
  702. mutex_unlock(&cx->serialize_lock);
  703. return 0;
  704. }
  705. static int cx18_serialized_open(struct cx18_stream *s, struct file *filp)
  706. {
  707. struct cx18 *cx = s->cx;
  708. struct cx18_open_id *item;
  709. CX18_DEBUG_FILE("open %s\n", s->name);
  710. /* Allocate memory */
  711. item = kzalloc(sizeof(struct cx18_open_id), GFP_KERNEL);
  712. if (NULL == item) {
  713. CX18_DEBUG_WARN("nomem on v4l2 open\n");
  714. return -ENOMEM;
  715. }
  716. v4l2_fh_init(&item->fh, &s->video_dev);
  717. item->cx = cx;
  718. item->type = s->type;
  719. item->open_id = cx->open_id++;
  720. filp->private_data = &item->fh;
  721. v4l2_fh_add(&item->fh);
  722. if (item->type == CX18_ENC_STREAM_TYPE_RAD &&
  723. v4l2_fh_is_singular_file(filp)) {
  724. if (!test_bit(CX18_F_I_RADIO_USER, &cx->i_flags)) {
  725. if (atomic_read(&cx->ana_capturing) > 0) {
  726. /* switching to radio while capture is
  727. in progress is not polite */
  728. v4l2_fh_del(&item->fh);
  729. v4l2_fh_exit(&item->fh);
  730. kfree(item);
  731. return -EBUSY;
  732. }
  733. }
  734. /* Mark that the radio is being used. */
  735. set_bit(CX18_F_I_RADIO_USER, &cx->i_flags);
  736. /* We have the radio */
  737. cx18_mute(cx);
  738. /* Switch tuner to radio */
  739. cx18_call_all(cx, tuner, s_radio);
  740. /* Select the correct audio input (i.e. radio tuner) */
  741. cx18_audio_set_io(cx);
  742. /* Done! Unmute and continue. */
  743. cx18_unmute(cx);
  744. }
  745. return 0;
  746. }
  747. int cx18_v4l2_open(struct file *filp)
  748. {
  749. int res;
  750. struct video_device *video_dev = video_devdata(filp);
  751. struct cx18_stream *s = video_get_drvdata(video_dev);
  752. struct cx18 *cx = s->cx;
  753. mutex_lock(&cx->serialize_lock);
  754. if (cx18_init_on_first_open(cx)) {
  755. CX18_ERR("Failed to initialize on %s\n",
  756. video_device_node_name(video_dev));
  757. mutex_unlock(&cx->serialize_lock);
  758. return -ENXIO;
  759. }
  760. res = cx18_serialized_open(s, filp);
  761. mutex_unlock(&cx->serialize_lock);
  762. return res;
  763. }
  764. void cx18_mute(struct cx18 *cx)
  765. {
  766. u32 h;
  767. if (atomic_read(&cx->ana_capturing)) {
  768. h = cx18_find_handle(cx);
  769. if (h != CX18_INVALID_TASK_HANDLE)
  770. cx18_vapi(cx, CX18_CPU_SET_AUDIO_MUTE, 2, h, 1);
  771. else
  772. CX18_ERR("Can't find valid task handle for mute\n");
  773. }
  774. CX18_DEBUG_INFO("Mute\n");
  775. }
  776. void cx18_unmute(struct cx18 *cx)
  777. {
  778. u32 h;
  779. if (atomic_read(&cx->ana_capturing)) {
  780. h = cx18_find_handle(cx);
  781. if (h != CX18_INVALID_TASK_HANDLE) {
  782. cx18_msleep_timeout(100, 0);
  783. cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 2, h, 12);
  784. cx18_vapi(cx, CX18_CPU_SET_AUDIO_MUTE, 2, h, 0);
  785. } else
  786. CX18_ERR("Can't find valid task handle for unmute\n");
  787. }
  788. CX18_DEBUG_INFO("Unmute\n");
  789. }