cx18-fileops.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  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@radix.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. /* This function tries to claim the stream for a specific file descriptor.
  37. If no one else is using this stream then the stream is claimed and
  38. associated VBI streams are also automatically claimed.
  39. Possible error returns: -EBUSY if someone else has claimed
  40. the stream or 0 on success. */
  41. static int cx18_claim_stream(struct cx18_open_id *id, int type)
  42. {
  43. struct cx18 *cx = id->cx;
  44. struct cx18_stream *s = &cx->streams[type];
  45. struct cx18_stream *s_vbi;
  46. int vbi_type;
  47. if (test_and_set_bit(CX18_F_S_CLAIMED, &s->s_flags)) {
  48. /* someone already claimed this stream */
  49. if (s->id == id->open_id) {
  50. /* yes, this file descriptor did. So that's OK. */
  51. return 0;
  52. }
  53. if (s->id == -1 && type == CX18_ENC_STREAM_TYPE_VBI) {
  54. /* VBI is handled already internally, now also assign
  55. the file descriptor to this stream for external
  56. reading of the stream. */
  57. s->id = id->open_id;
  58. CX18_DEBUG_INFO("Start Read VBI\n");
  59. return 0;
  60. }
  61. /* someone else is using this stream already */
  62. CX18_DEBUG_INFO("Stream %d is busy\n", type);
  63. return -EBUSY;
  64. }
  65. s->id = id->open_id;
  66. /* CX18_ENC_STREAM_TYPE_MPG needs to claim CX18_ENC_STREAM_TYPE_VBI
  67. (provided VBI insertion is on and sliced VBI is selected), for all
  68. other streams we're done */
  69. if (type == CX18_ENC_STREAM_TYPE_MPG &&
  70. cx->vbi.insert_mpeg && !cx18_raw_vbi(cx)) {
  71. vbi_type = CX18_ENC_STREAM_TYPE_VBI;
  72. } else {
  73. return 0;
  74. }
  75. s_vbi = &cx->streams[vbi_type];
  76. set_bit(CX18_F_S_CLAIMED, &s_vbi->s_flags);
  77. /* mark that it is used internally */
  78. set_bit(CX18_F_S_INTERNAL_USE, &s_vbi->s_flags);
  79. return 0;
  80. }
  81. /* This function releases a previously claimed stream. It will take into
  82. account associated VBI streams. */
  83. static void cx18_release_stream(struct cx18_stream *s)
  84. {
  85. struct cx18 *cx = s->cx;
  86. struct cx18_stream *s_vbi;
  87. s->id = -1;
  88. if (s->type == CX18_ENC_STREAM_TYPE_VBI &&
  89. test_bit(CX18_F_S_INTERNAL_USE, &s->s_flags)) {
  90. /* this stream is still in use internally */
  91. return;
  92. }
  93. if (!test_and_clear_bit(CX18_F_S_CLAIMED, &s->s_flags)) {
  94. CX18_DEBUG_WARN("Release stream %s not in use!\n", s->name);
  95. return;
  96. }
  97. cx18_flush_queues(s);
  98. /* CX18_ENC_STREAM_TYPE_MPG needs to release CX18_ENC_STREAM_TYPE_VBI,
  99. for all other streams we're done */
  100. if (s->type == CX18_ENC_STREAM_TYPE_MPG)
  101. s_vbi = &cx->streams[CX18_ENC_STREAM_TYPE_VBI];
  102. else
  103. return;
  104. /* clear internal use flag */
  105. if (!test_and_clear_bit(CX18_F_S_INTERNAL_USE, &s_vbi->s_flags)) {
  106. /* was already cleared */
  107. return;
  108. }
  109. if (s_vbi->id != -1) {
  110. /* VBI stream still claimed by a file descriptor */
  111. return;
  112. }
  113. clear_bit(CX18_F_S_CLAIMED, &s_vbi->s_flags);
  114. cx18_flush_queues(s_vbi);
  115. }
  116. static void cx18_dualwatch(struct cx18 *cx)
  117. {
  118. struct v4l2_tuner vt;
  119. u32 new_bitmap;
  120. u32 new_stereo_mode;
  121. const u32 stereo_mask = 0x0300;
  122. const u32 dual = 0x0200;
  123. u32 h;
  124. new_stereo_mode = cx->params.audio_properties & stereo_mask;
  125. memset(&vt, 0, sizeof(vt));
  126. cx18_call_all(cx, tuner, g_tuner, &vt);
  127. if (vt.audmode == V4L2_TUNER_MODE_LANG1_LANG2 &&
  128. (vt.rxsubchans & V4L2_TUNER_SUB_LANG2))
  129. new_stereo_mode = dual;
  130. if (new_stereo_mode == cx->dualwatch_stereo_mode)
  131. return;
  132. new_bitmap = new_stereo_mode
  133. | (cx->params.audio_properties & ~stereo_mask);
  134. CX18_DEBUG_INFO("dualwatch: change stereo flag from 0x%x to 0x%x. "
  135. "new audio_bitmask=0x%ux\n",
  136. cx->dualwatch_stereo_mode, new_stereo_mode, new_bitmap);
  137. h = cx18_find_handle(cx);
  138. if (h == CX18_INVALID_TASK_HANDLE) {
  139. CX18_DEBUG_INFO("dualwatch: can't find valid task handle\n");
  140. return;
  141. }
  142. if (cx18_vapi(cx,
  143. CX18_CPU_SET_AUDIO_PARAMETERS, 2, h, new_bitmap) == 0) {
  144. cx->dualwatch_stereo_mode = new_stereo_mode;
  145. return;
  146. }
  147. CX18_DEBUG_INFO("dualwatch: changing stereo flag failed\n");
  148. }
  149. static struct cx18_mdl *cx18_get_mdl(struct cx18_stream *s, int non_block,
  150. int *err)
  151. {
  152. struct cx18 *cx = s->cx;
  153. struct cx18_stream *s_vbi = &cx->streams[CX18_ENC_STREAM_TYPE_VBI];
  154. struct cx18_mdl *mdl;
  155. DEFINE_WAIT(wait);
  156. *err = 0;
  157. while (1) {
  158. if (s->type == CX18_ENC_STREAM_TYPE_MPG) {
  159. /* Process pending program info updates and pending
  160. VBI data */
  161. if (time_after(jiffies, cx->dualwatch_jiffies + msecs_to_jiffies(1000))) {
  162. cx->dualwatch_jiffies = jiffies;
  163. cx18_dualwatch(cx);
  164. }
  165. if (test_bit(CX18_F_S_INTERNAL_USE, &s_vbi->s_flags) &&
  166. !test_bit(CX18_F_S_APPL_IO, &s_vbi->s_flags)) {
  167. while ((mdl = cx18_dequeue(s_vbi,
  168. &s_vbi->q_full))) {
  169. /* byteswap and process VBI data */
  170. cx18_process_vbi_data(cx, mdl,
  171. s_vbi->type);
  172. cx18_stream_put_mdl_fw(s_vbi, mdl);
  173. }
  174. }
  175. mdl = &cx->vbi.sliced_mpeg_mdl;
  176. if (mdl->readpos != mdl->bytesused)
  177. return mdl;
  178. }
  179. /* do we have new data? */
  180. mdl = cx18_dequeue(s, &s->q_full);
  181. if (mdl) {
  182. if (!test_and_clear_bit(CX18_F_M_NEED_SWAP,
  183. &mdl->m_flags))
  184. return mdl;
  185. if (s->type == CX18_ENC_STREAM_TYPE_MPG)
  186. /* byteswap MPG data */
  187. cx18_mdl_swap(mdl);
  188. else {
  189. /* byteswap and process VBI data */
  190. cx18_process_vbi_data(cx, mdl, s->type);
  191. }
  192. return mdl;
  193. }
  194. /* return if end of stream */
  195. if (!test_bit(CX18_F_S_STREAMING, &s->s_flags)) {
  196. CX18_DEBUG_INFO("EOS %s\n", s->name);
  197. return NULL;
  198. }
  199. /* return if file was opened with O_NONBLOCK */
  200. if (non_block) {
  201. *err = -EAGAIN;
  202. return NULL;
  203. }
  204. /* wait for more data to arrive */
  205. prepare_to_wait(&s->waitq, &wait, TASK_INTERRUPTIBLE);
  206. /* New buffers might have become available before we were added
  207. to the waitqueue */
  208. if (!atomic_read(&s->q_full.depth))
  209. schedule();
  210. finish_wait(&s->waitq, &wait);
  211. if (signal_pending(current)) {
  212. /* return if a signal was received */
  213. CX18_DEBUG_INFO("User stopped %s\n", s->name);
  214. *err = -EINTR;
  215. return NULL;
  216. }
  217. }
  218. }
  219. static void cx18_setup_sliced_vbi_mdl(struct cx18 *cx)
  220. {
  221. struct cx18_mdl *mdl = &cx->vbi.sliced_mpeg_mdl;
  222. struct cx18_buffer *buf = &cx->vbi.sliced_mpeg_buf;
  223. int idx = cx->vbi.inserted_frame % CX18_VBI_FRAMES;
  224. buf->buf = cx->vbi.sliced_mpeg_data[idx];
  225. buf->bytesused = cx->vbi.sliced_mpeg_size[idx];
  226. buf->readpos = 0;
  227. mdl->curr_buf = NULL;
  228. mdl->bytesused = cx->vbi.sliced_mpeg_size[idx];
  229. mdl->readpos = 0;
  230. }
  231. static size_t cx18_copy_buf_to_user(struct cx18_stream *s,
  232. struct cx18_buffer *buf, char __user *ubuf, size_t ucount, bool *stop)
  233. {
  234. struct cx18 *cx = s->cx;
  235. size_t len = buf->bytesused - buf->readpos;
  236. *stop = false;
  237. if (len > ucount)
  238. len = ucount;
  239. if (cx->vbi.insert_mpeg && s->type == CX18_ENC_STREAM_TYPE_MPG &&
  240. !cx18_raw_vbi(cx) && buf != &cx->vbi.sliced_mpeg_buf) {
  241. /*
  242. * Try to find a good splice point in the PS, just before
  243. * an MPEG-2 Program Pack start code, and provide only
  244. * up to that point to the user, so it's easy to insert VBI data
  245. * the next time around.
  246. *
  247. * This will not work for an MPEG-2 TS and has only been
  248. * verified by analysis to work for an MPEG-2 PS. Helen Buus
  249. * pointed out this works for the CX23416 MPEG-2 DVD compatible
  250. * stream, and research indicates both the MPEG 2 SVCD and DVD
  251. * stream types use an MPEG-2 PS container.
  252. */
  253. /*
  254. * An MPEG-2 Program Stream (PS) is a series of
  255. * MPEG-2 Program Packs terminated by an
  256. * MPEG Program End Code after the last Program Pack.
  257. * A Program Pack may hold a PS System Header packet and any
  258. * number of Program Elementary Stream (PES) Packets
  259. */
  260. const char *start = buf->buf + buf->readpos;
  261. const char *p = start + 1;
  262. const u8 *q;
  263. u8 ch = cx->search_pack_header ? 0xba : 0xe0;
  264. int stuffing, i;
  265. while (start + len > p) {
  266. /* Scan for a 0 to find a potential MPEG-2 start code */
  267. q = memchr(p, 0, start + len - p);
  268. if (q == NULL)
  269. break;
  270. p = q + 1;
  271. /*
  272. * Keep looking if not a
  273. * MPEG-2 Pack header start code: 0x00 0x00 0x01 0xba
  274. * or MPEG-2 video PES start code: 0x00 0x00 0x01 0xe0
  275. */
  276. if ((char *)q + 15 >= buf->buf + buf->bytesused ||
  277. q[1] != 0 || q[2] != 1 || q[3] != ch)
  278. continue;
  279. /* If expecting the primary video PES */
  280. if (!cx->search_pack_header) {
  281. /* Continue if it couldn't be a PES packet */
  282. if ((q[6] & 0xc0) != 0x80)
  283. continue;
  284. /* Check if a PTS or PTS & DTS follow */
  285. if (((q[7] & 0xc0) == 0x80 && /* PTS only */
  286. (q[9] & 0xf0) == 0x20) || /* PTS only */
  287. ((q[7] & 0xc0) == 0xc0 && /* PTS & DTS */
  288. (q[9] & 0xf0) == 0x30)) { /* DTS follows */
  289. /* Assume we found the video PES hdr */
  290. ch = 0xba; /* next want a Program Pack*/
  291. cx->search_pack_header = 1;
  292. p = q + 9; /* Skip this video PES hdr */
  293. }
  294. continue;
  295. }
  296. /* We may have found a Program Pack start code */
  297. /* Get the count of stuffing bytes & verify them */
  298. stuffing = q[13] & 7;
  299. /* all stuffing bytes must be 0xff */
  300. for (i = 0; i < stuffing; i++)
  301. if (q[14 + i] != 0xff)
  302. break;
  303. if (i == stuffing && /* right number of stuffing bytes*/
  304. (q[4] & 0xc4) == 0x44 && /* marker check */
  305. (q[12] & 3) == 3 && /* marker check */
  306. q[14 + stuffing] == 0 && /* PES Pack or Sys Hdr */
  307. q[15 + stuffing] == 0 &&
  308. q[16 + stuffing] == 1) {
  309. /* We declare we actually found a Program Pack*/
  310. cx->search_pack_header = 0; /* expect vid PES */
  311. len = (char *)q - start;
  312. cx18_setup_sliced_vbi_mdl(cx);
  313. *stop = true;
  314. break;
  315. }
  316. }
  317. }
  318. if (copy_to_user(ubuf, (u8 *)buf->buf + buf->readpos, len)) {
  319. CX18_DEBUG_WARN("copy %zd bytes to user failed for %s\n",
  320. len, s->name);
  321. return -EFAULT;
  322. }
  323. buf->readpos += len;
  324. if (s->type == CX18_ENC_STREAM_TYPE_MPG &&
  325. buf != &cx->vbi.sliced_mpeg_buf)
  326. cx->mpg_data_received += len;
  327. return len;
  328. }
  329. /**
  330. * list_entry_is_past_end - check if a previous loop cursor is off list end
  331. * @pos: the type * previously used as a loop cursor.
  332. * @head: the head for your list.
  333. * @member: the name of the list_struct within the struct.
  334. *
  335. * Check if the entry's list_head is the head of the list, thus it's not a
  336. * real entry but was the loop cursor that walked past the end
  337. */
  338. #define list_entry_is_past_end(pos, head, member) \
  339. (&pos->member == (head))
  340. static size_t cx18_copy_mdl_to_user(struct cx18_stream *s,
  341. struct cx18_mdl *mdl, char __user *ubuf, size_t ucount)
  342. {
  343. size_t tot_written = 0;
  344. int rc;
  345. bool stop = false;
  346. if (mdl->curr_buf == NULL)
  347. mdl->curr_buf = list_first_entry(&mdl->buf_list,
  348. struct cx18_buffer, list);
  349. if (list_entry_is_past_end(mdl->curr_buf, &mdl->buf_list, list)) {
  350. /*
  351. * For some reason we've exhausted the buffers, but the MDL
  352. * object still said some data was unread.
  353. * Fix that and bail out.
  354. */
  355. mdl->readpos = mdl->bytesused;
  356. return 0;
  357. }
  358. list_for_each_entry_from(mdl->curr_buf, &mdl->buf_list, list) {
  359. if (mdl->curr_buf->readpos >= mdl->curr_buf->bytesused)
  360. continue;
  361. rc = cx18_copy_buf_to_user(s, mdl->curr_buf, ubuf + tot_written,
  362. ucount - tot_written, &stop);
  363. if (rc < 0)
  364. return rc;
  365. mdl->readpos += rc;
  366. tot_written += rc;
  367. if (stop || /* Forced stopping point for VBI insertion */
  368. tot_written >= ucount || /* Reader request statisfied */
  369. mdl->curr_buf->readpos < mdl->curr_buf->bytesused ||
  370. mdl->readpos >= mdl->bytesused) /* MDL buffers drained */
  371. break;
  372. }
  373. return tot_written;
  374. }
  375. static ssize_t cx18_read(struct cx18_stream *s, char __user *ubuf,
  376. size_t tot_count, int non_block)
  377. {
  378. struct cx18 *cx = s->cx;
  379. size_t tot_written = 0;
  380. int single_frame = 0;
  381. if (atomic_read(&cx->ana_capturing) == 0 && s->id == -1) {
  382. /* shouldn't happen */
  383. CX18_DEBUG_WARN("Stream %s not initialized before read\n",
  384. s->name);
  385. return -EIO;
  386. }
  387. /* Each VBI buffer is one frame, the v4l2 API says that for VBI the
  388. frames should arrive one-by-one, so make sure we never output more
  389. than one VBI frame at a time */
  390. if (s->type == CX18_ENC_STREAM_TYPE_VBI && !cx18_raw_vbi(cx))
  391. single_frame = 1;
  392. for (;;) {
  393. struct cx18_mdl *mdl;
  394. int rc;
  395. mdl = cx18_get_mdl(s, non_block, &rc);
  396. /* if there is no data available... */
  397. if (mdl == NULL) {
  398. /* if we got data, then return that regardless */
  399. if (tot_written)
  400. break;
  401. /* EOS condition */
  402. if (rc == 0) {
  403. clear_bit(CX18_F_S_STREAMOFF, &s->s_flags);
  404. clear_bit(CX18_F_S_APPL_IO, &s->s_flags);
  405. cx18_release_stream(s);
  406. }
  407. /* set errno */
  408. return rc;
  409. }
  410. rc = cx18_copy_mdl_to_user(s, mdl, ubuf + tot_written,
  411. tot_count - tot_written);
  412. if (mdl != &cx->vbi.sliced_mpeg_mdl) {
  413. if (mdl->readpos == mdl->bytesused)
  414. cx18_stream_put_mdl_fw(s, mdl);
  415. else
  416. cx18_push(s, mdl, &s->q_full);
  417. } else if (mdl->readpos == mdl->bytesused) {
  418. int idx = cx->vbi.inserted_frame % CX18_VBI_FRAMES;
  419. cx->vbi.sliced_mpeg_size[idx] = 0;
  420. cx->vbi.inserted_frame++;
  421. cx->vbi_data_inserted += mdl->bytesused;
  422. }
  423. if (rc < 0)
  424. return rc;
  425. tot_written += rc;
  426. if (tot_written == tot_count || single_frame)
  427. break;
  428. }
  429. return tot_written;
  430. }
  431. static ssize_t cx18_read_pos(struct cx18_stream *s, char __user *ubuf,
  432. size_t count, loff_t *pos, int non_block)
  433. {
  434. ssize_t rc = count ? cx18_read(s, ubuf, count, non_block) : 0;
  435. struct cx18 *cx = s->cx;
  436. CX18_DEBUG_HI_FILE("read %zd from %s, got %zd\n", count, s->name, rc);
  437. if (rc > 0)
  438. pos += rc;
  439. return rc;
  440. }
  441. int cx18_start_capture(struct cx18_open_id *id)
  442. {
  443. struct cx18 *cx = id->cx;
  444. struct cx18_stream *s = &cx->streams[id->type];
  445. struct cx18_stream *s_vbi;
  446. if (s->type == CX18_ENC_STREAM_TYPE_RAD) {
  447. /* you cannot read from these stream types. */
  448. return -EPERM;
  449. }
  450. /* Try to claim this stream. */
  451. if (cx18_claim_stream(id, s->type))
  452. return -EBUSY;
  453. /* If capture is already in progress, then we also have to
  454. do nothing extra. */
  455. if (test_bit(CX18_F_S_STREAMOFF, &s->s_flags) ||
  456. test_and_set_bit(CX18_F_S_STREAMING, &s->s_flags)) {
  457. set_bit(CX18_F_S_APPL_IO, &s->s_flags);
  458. return 0;
  459. }
  460. /* Start VBI capture if required */
  461. s_vbi = &cx->streams[CX18_ENC_STREAM_TYPE_VBI];
  462. if (s->type == CX18_ENC_STREAM_TYPE_MPG &&
  463. test_bit(CX18_F_S_INTERNAL_USE, &s_vbi->s_flags) &&
  464. !test_and_set_bit(CX18_F_S_STREAMING, &s_vbi->s_flags)) {
  465. /* Note: the CX18_ENC_STREAM_TYPE_VBI is claimed
  466. automatically when the MPG stream is claimed.
  467. We only need to start the VBI capturing. */
  468. if (cx18_start_v4l2_encode_stream(s_vbi)) {
  469. CX18_DEBUG_WARN("VBI capture start failed\n");
  470. /* Failure, clean up and return an error */
  471. clear_bit(CX18_F_S_STREAMING, &s_vbi->s_flags);
  472. clear_bit(CX18_F_S_STREAMING, &s->s_flags);
  473. /* also releases the associated VBI stream */
  474. cx18_release_stream(s);
  475. return -EIO;
  476. }
  477. CX18_DEBUG_INFO("VBI insertion started\n");
  478. }
  479. /* Tell the card to start capturing */
  480. if (!cx18_start_v4l2_encode_stream(s)) {
  481. /* We're done */
  482. set_bit(CX18_F_S_APPL_IO, &s->s_flags);
  483. /* Resume a possibly paused encoder */
  484. if (test_and_clear_bit(CX18_F_I_ENC_PAUSED, &cx->i_flags))
  485. cx18_vapi(cx, CX18_CPU_CAPTURE_PAUSE, 1, s->handle);
  486. return 0;
  487. }
  488. /* failure, clean up */
  489. CX18_DEBUG_WARN("Failed to start capturing for stream %s\n", s->name);
  490. /* Note: the CX18_ENC_STREAM_TYPE_VBI is released
  491. automatically when the MPG stream is released.
  492. We only need to stop the VBI capturing. */
  493. if (s->type == CX18_ENC_STREAM_TYPE_MPG &&
  494. test_bit(CX18_F_S_STREAMING, &s_vbi->s_flags)) {
  495. cx18_stop_v4l2_encode_stream(s_vbi, 0);
  496. clear_bit(CX18_F_S_STREAMING, &s_vbi->s_flags);
  497. }
  498. clear_bit(CX18_F_S_STREAMING, &s->s_flags);
  499. cx18_release_stream(s);
  500. return -EIO;
  501. }
  502. ssize_t cx18_v4l2_read(struct file *filp, char __user *buf, size_t count,
  503. loff_t *pos)
  504. {
  505. struct cx18_open_id *id = filp->private_data;
  506. struct cx18 *cx = id->cx;
  507. struct cx18_stream *s = &cx->streams[id->type];
  508. int rc;
  509. CX18_DEBUG_HI_FILE("read %zd bytes from %s\n", count, s->name);
  510. mutex_lock(&cx->serialize_lock);
  511. rc = cx18_start_capture(id);
  512. mutex_unlock(&cx->serialize_lock);
  513. if (rc)
  514. return rc;
  515. return cx18_read_pos(s, buf, count, pos, filp->f_flags & O_NONBLOCK);
  516. }
  517. unsigned int cx18_v4l2_enc_poll(struct file *filp, poll_table *wait)
  518. {
  519. struct cx18_open_id *id = filp->private_data;
  520. struct cx18 *cx = id->cx;
  521. struct cx18_stream *s = &cx->streams[id->type];
  522. int eof = test_bit(CX18_F_S_STREAMOFF, &s->s_flags);
  523. /* Start a capture if there is none */
  524. if (!eof && !test_bit(CX18_F_S_STREAMING, &s->s_flags)) {
  525. int rc;
  526. mutex_lock(&cx->serialize_lock);
  527. rc = cx18_start_capture(id);
  528. mutex_unlock(&cx->serialize_lock);
  529. if (rc) {
  530. CX18_DEBUG_INFO("Could not start capture for %s (%d)\n",
  531. s->name, rc);
  532. return POLLERR;
  533. }
  534. CX18_DEBUG_FILE("Encoder poll started capture\n");
  535. }
  536. /* add stream's waitq to the poll list */
  537. CX18_DEBUG_HI_FILE("Encoder poll\n");
  538. poll_wait(filp, &s->waitq, wait);
  539. if (atomic_read(&s->q_full.depth))
  540. return POLLIN | POLLRDNORM;
  541. if (eof)
  542. return POLLHUP;
  543. return 0;
  544. }
  545. void cx18_stop_capture(struct cx18_open_id *id, int gop_end)
  546. {
  547. struct cx18 *cx = id->cx;
  548. struct cx18_stream *s = &cx->streams[id->type];
  549. CX18_DEBUG_IOCTL("close() of %s\n", s->name);
  550. /* 'Unclaim' this stream */
  551. /* Stop capturing */
  552. if (test_bit(CX18_F_S_STREAMING, &s->s_flags)) {
  553. struct cx18_stream *s_vbi =
  554. &cx->streams[CX18_ENC_STREAM_TYPE_VBI];
  555. CX18_DEBUG_INFO("close stopping capture\n");
  556. /* Special case: a running VBI capture for VBI insertion
  557. in the mpeg stream. Need to stop that too. */
  558. if (id->type == CX18_ENC_STREAM_TYPE_MPG &&
  559. test_bit(CX18_F_S_STREAMING, &s_vbi->s_flags) &&
  560. !test_bit(CX18_F_S_APPL_IO, &s_vbi->s_flags)) {
  561. CX18_DEBUG_INFO("close stopping embedded VBI capture\n");
  562. cx18_stop_v4l2_encode_stream(s_vbi, 0);
  563. }
  564. if (id->type == CX18_ENC_STREAM_TYPE_VBI &&
  565. test_bit(CX18_F_S_INTERNAL_USE, &s->s_flags))
  566. /* Also used internally, don't stop capturing */
  567. s->id = -1;
  568. else
  569. cx18_stop_v4l2_encode_stream(s, gop_end);
  570. }
  571. if (!gop_end) {
  572. clear_bit(CX18_F_S_APPL_IO, &s->s_flags);
  573. clear_bit(CX18_F_S_STREAMOFF, &s->s_flags);
  574. cx18_release_stream(s);
  575. }
  576. }
  577. int cx18_v4l2_close(struct file *filp)
  578. {
  579. struct cx18_open_id *id = filp->private_data;
  580. struct cx18 *cx = id->cx;
  581. struct cx18_stream *s = &cx->streams[id->type];
  582. CX18_DEBUG_IOCTL("close() of %s\n", s->name);
  583. v4l2_prio_close(&cx->prio, &id->prio);
  584. /* Easy case first: this stream was never claimed by us */
  585. if (s->id != id->open_id) {
  586. kfree(id);
  587. return 0;
  588. }
  589. /* 'Unclaim' this stream */
  590. /* Stop radio */
  591. mutex_lock(&cx->serialize_lock);
  592. if (id->type == CX18_ENC_STREAM_TYPE_RAD) {
  593. /* Closing radio device, return to TV mode */
  594. cx18_mute(cx);
  595. /* Mark that the radio is no longer in use */
  596. clear_bit(CX18_F_I_RADIO_USER, &cx->i_flags);
  597. /* Switch tuner to TV */
  598. cx18_call_all(cx, core, s_std, cx->std);
  599. /* Select correct audio input (i.e. TV tuner or Line in) */
  600. cx18_audio_set_io(cx);
  601. if (atomic_read(&cx->ana_capturing) > 0) {
  602. /* Undo video mute */
  603. cx18_vapi(cx, CX18_CPU_SET_VIDEO_MUTE, 2, s->handle,
  604. cx->params.video_mute |
  605. (cx->params.video_mute_yuv << 8));
  606. }
  607. /* Done! Unmute and continue. */
  608. cx18_unmute(cx);
  609. cx18_release_stream(s);
  610. } else {
  611. cx18_stop_capture(id, 0);
  612. }
  613. kfree(id);
  614. mutex_unlock(&cx->serialize_lock);
  615. return 0;
  616. }
  617. static int cx18_serialized_open(struct cx18_stream *s, struct file *filp)
  618. {
  619. struct cx18 *cx = s->cx;
  620. struct cx18_open_id *item;
  621. CX18_DEBUG_FILE("open %s\n", s->name);
  622. /* Allocate memory */
  623. item = kmalloc(sizeof(struct cx18_open_id), GFP_KERNEL);
  624. if (NULL == item) {
  625. CX18_DEBUG_WARN("nomem on v4l2 open\n");
  626. return -ENOMEM;
  627. }
  628. item->cx = cx;
  629. item->type = s->type;
  630. v4l2_prio_open(&cx->prio, &item->prio);
  631. item->open_id = cx->open_id++;
  632. filp->private_data = item;
  633. if (item->type == CX18_ENC_STREAM_TYPE_RAD) {
  634. /* Try to claim this stream */
  635. if (cx18_claim_stream(item, item->type)) {
  636. /* No, it's already in use */
  637. kfree(item);
  638. return -EBUSY;
  639. }
  640. if (!test_bit(CX18_F_I_RADIO_USER, &cx->i_flags)) {
  641. if (atomic_read(&cx->ana_capturing) > 0) {
  642. /* switching to radio while capture is
  643. in progress is not polite */
  644. cx18_release_stream(s);
  645. kfree(item);
  646. return -EBUSY;
  647. }
  648. }
  649. /* Mark that the radio is being used. */
  650. set_bit(CX18_F_I_RADIO_USER, &cx->i_flags);
  651. /* We have the radio */
  652. cx18_mute(cx);
  653. /* Switch tuner to radio */
  654. cx18_call_all(cx, tuner, s_radio);
  655. /* Select the correct audio input (i.e. radio tuner) */
  656. cx18_audio_set_io(cx);
  657. /* Done! Unmute and continue. */
  658. cx18_unmute(cx);
  659. }
  660. return 0;
  661. }
  662. int cx18_v4l2_open(struct file *filp)
  663. {
  664. int res;
  665. struct video_device *video_dev = video_devdata(filp);
  666. struct cx18_stream *s = video_get_drvdata(video_dev);
  667. struct cx18 *cx = s->cx;
  668. mutex_lock(&cx->serialize_lock);
  669. if (cx18_init_on_first_open(cx)) {
  670. CX18_ERR("Failed to initialize on minor %d\n",
  671. video_dev->minor);
  672. mutex_unlock(&cx->serialize_lock);
  673. return -ENXIO;
  674. }
  675. res = cx18_serialized_open(s, filp);
  676. mutex_unlock(&cx->serialize_lock);
  677. return res;
  678. }
  679. void cx18_mute(struct cx18 *cx)
  680. {
  681. u32 h;
  682. if (atomic_read(&cx->ana_capturing)) {
  683. h = cx18_find_handle(cx);
  684. if (h != CX18_INVALID_TASK_HANDLE)
  685. cx18_vapi(cx, CX18_CPU_SET_AUDIO_MUTE, 2, h, 1);
  686. else
  687. CX18_ERR("Can't find valid task handle for mute\n");
  688. }
  689. CX18_DEBUG_INFO("Mute\n");
  690. }
  691. void cx18_unmute(struct cx18 *cx)
  692. {
  693. u32 h;
  694. if (atomic_read(&cx->ana_capturing)) {
  695. h = cx18_find_handle(cx);
  696. if (h != CX18_INVALID_TASK_HANDLE) {
  697. cx18_msleep_timeout(100, 0);
  698. cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 2, h, 12);
  699. cx18_vapi(cx, CX18_CPU_SET_AUDIO_MUTE, 2, h, 0);
  700. } else
  701. CX18_ERR("Can't find valid task handle for unmute\n");
  702. }
  703. CX18_DEBUG_INFO("Unmute\n");
  704. }