cx18-fileops.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  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_buffer *cx18_get_buffer(struct cx18_stream *s, int non_block, int *err)
  150. {
  151. struct cx18 *cx = s->cx;
  152. struct cx18_stream *s_vbi = &cx->streams[CX18_ENC_STREAM_TYPE_VBI];
  153. struct cx18_buffer *buf;
  154. DEFINE_WAIT(wait);
  155. *err = 0;
  156. while (1) {
  157. if (s->type == CX18_ENC_STREAM_TYPE_MPG) {
  158. /* Process pending program info updates and pending
  159. VBI data */
  160. if (time_after(jiffies, cx->dualwatch_jiffies + msecs_to_jiffies(1000))) {
  161. cx->dualwatch_jiffies = jiffies;
  162. cx18_dualwatch(cx);
  163. }
  164. if (test_bit(CX18_F_S_INTERNAL_USE, &s_vbi->s_flags) &&
  165. !test_bit(CX18_F_S_APPL_IO, &s_vbi->s_flags)) {
  166. while ((buf = cx18_dequeue(s_vbi, &s_vbi->q_full))) {
  167. /* byteswap and process VBI data */
  168. cx18_process_vbi_data(cx, buf,
  169. s_vbi->type);
  170. cx18_stream_put_buf_fw(s_vbi, buf);
  171. }
  172. }
  173. buf = &cx->vbi.sliced_mpeg_buf;
  174. if (buf->readpos != buf->bytesused)
  175. return buf;
  176. }
  177. /* do we have new data? */
  178. buf = cx18_dequeue(s, &s->q_full);
  179. if (buf) {
  180. if (!test_and_clear_bit(CX18_F_B_NEED_BUF_SWAP,
  181. &buf->b_flags))
  182. return buf;
  183. if (s->type == CX18_ENC_STREAM_TYPE_MPG)
  184. /* byteswap MPG data */
  185. cx18_buf_swap(buf);
  186. else {
  187. /* byteswap and process VBI data */
  188. cx18_process_vbi_data(cx, buf, s->type);
  189. }
  190. return buf;
  191. }
  192. /* return if end of stream */
  193. if (!test_bit(CX18_F_S_STREAMING, &s->s_flags)) {
  194. CX18_DEBUG_INFO("EOS %s\n", s->name);
  195. return NULL;
  196. }
  197. /* return if file was opened with O_NONBLOCK */
  198. if (non_block) {
  199. *err = -EAGAIN;
  200. return NULL;
  201. }
  202. /* wait for more data to arrive */
  203. prepare_to_wait(&s->waitq, &wait, TASK_INTERRUPTIBLE);
  204. /* New buffers might have become available before we were added
  205. to the waitqueue */
  206. if (!atomic_read(&s->q_full.buffers))
  207. schedule();
  208. finish_wait(&s->waitq, &wait);
  209. if (signal_pending(current)) {
  210. /* return if a signal was received */
  211. CX18_DEBUG_INFO("User stopped %s\n", s->name);
  212. *err = -EINTR;
  213. return NULL;
  214. }
  215. }
  216. }
  217. static void cx18_setup_sliced_vbi_buf(struct cx18 *cx)
  218. {
  219. int idx = cx->vbi.inserted_frame % CX18_VBI_FRAMES;
  220. cx->vbi.sliced_mpeg_buf.buf = cx->vbi.sliced_mpeg_data[idx];
  221. cx->vbi.sliced_mpeg_buf.bytesused = cx->vbi.sliced_mpeg_size[idx];
  222. cx->vbi.sliced_mpeg_buf.readpos = 0;
  223. }
  224. static size_t cx18_copy_buf_to_user(struct cx18_stream *s,
  225. struct cx18_buffer *buf, char __user *ubuf, size_t ucount)
  226. {
  227. struct cx18 *cx = s->cx;
  228. size_t len = buf->bytesused - buf->readpos;
  229. if (len > ucount)
  230. len = ucount;
  231. if (cx->vbi.insert_mpeg && s->type == CX18_ENC_STREAM_TYPE_MPG &&
  232. !cx18_raw_vbi(cx) && buf != &cx->vbi.sliced_mpeg_buf) {
  233. /*
  234. * Try to find a good splice point in the PS, just before
  235. * an MPEG-2 Program Pack start code, and provide only
  236. * up to that point to the user, so it's easy to insert VBI data
  237. * the next time around.
  238. *
  239. * This will not work for an MPEG-2 TS and has only been
  240. * verified by analysis to work for an MPEG-2 PS. Helen Buus
  241. * pointed out this works for the CX23416 MPEG-2 DVD compatible
  242. * stream, and research indicates both the MPEG 2 SVCD and DVD
  243. * stream types use an MPEG-2 PS container.
  244. */
  245. /*
  246. * An MPEG-2 Program Stream (PS) is a series of
  247. * MPEG-2 Program Packs terminated by an
  248. * MPEG Program End Code after the last Program Pack.
  249. * A Program Pack may hold a PS System Header packet and any
  250. * number of Program Elementary Stream (PES) Packets
  251. */
  252. const char *start = buf->buf + buf->readpos;
  253. const char *p = start + 1;
  254. const u8 *q;
  255. u8 ch = cx->search_pack_header ? 0xba : 0xe0;
  256. int stuffing, i;
  257. while (start + len > p) {
  258. /* Scan for a 0 to find a potential MPEG-2 start code */
  259. q = memchr(p, 0, start + len - p);
  260. if (q == NULL)
  261. break;
  262. p = q + 1;
  263. /*
  264. * Keep looking if not a
  265. * MPEG-2 Pack header start code: 0x00 0x00 0x01 0xba
  266. * or MPEG-2 video PES start code: 0x00 0x00 0x01 0xe0
  267. */
  268. if ((char *)q + 15 >= buf->buf + buf->bytesused ||
  269. q[1] != 0 || q[2] != 1 || q[3] != ch)
  270. continue;
  271. /* If expecting the primary video PES */
  272. if (!cx->search_pack_header) {
  273. /* Continue if it couldn't be a PES packet */
  274. if ((q[6] & 0xc0) != 0x80)
  275. continue;
  276. /* Check if a PTS or PTS & DTS follow */
  277. if (((q[7] & 0xc0) == 0x80 && /* PTS only */
  278. (q[9] & 0xf0) == 0x20) || /* PTS only */
  279. ((q[7] & 0xc0) == 0xc0 && /* PTS & DTS */
  280. (q[9] & 0xf0) == 0x30)) { /* DTS follows */
  281. /* Assume we found the video PES hdr */
  282. ch = 0xba; /* next want a Program Pack*/
  283. cx->search_pack_header = 1;
  284. p = q + 9; /* Skip this video PES hdr */
  285. }
  286. continue;
  287. }
  288. /* We may have found a Program Pack start code */
  289. /* Get the count of stuffing bytes & verify them */
  290. stuffing = q[13] & 7;
  291. /* all stuffing bytes must be 0xff */
  292. for (i = 0; i < stuffing; i++)
  293. if (q[14 + i] != 0xff)
  294. break;
  295. if (i == stuffing && /* right number of stuffing bytes*/
  296. (q[4] & 0xc4) == 0x44 && /* marker check */
  297. (q[12] & 3) == 3 && /* marker check */
  298. q[14 + stuffing] == 0 && /* PES Pack or Sys Hdr */
  299. q[15 + stuffing] == 0 &&
  300. q[16 + stuffing] == 1) {
  301. /* We declare we actually found a Program Pack*/
  302. cx->search_pack_header = 0; /* expect vid PES */
  303. len = (char *)q - start;
  304. cx18_setup_sliced_vbi_buf(cx);
  305. break;
  306. }
  307. }
  308. }
  309. if (copy_to_user(ubuf, (u8 *)buf->buf + buf->readpos, len)) {
  310. CX18_DEBUG_WARN("copy %zd bytes to user failed for %s\n",
  311. len, s->name);
  312. return -EFAULT;
  313. }
  314. buf->readpos += len;
  315. if (s->type == CX18_ENC_STREAM_TYPE_MPG &&
  316. buf != &cx->vbi.sliced_mpeg_buf)
  317. cx->mpg_data_received += len;
  318. return len;
  319. }
  320. static ssize_t cx18_read(struct cx18_stream *s, char __user *ubuf,
  321. size_t tot_count, int non_block)
  322. {
  323. struct cx18 *cx = s->cx;
  324. size_t tot_written = 0;
  325. int single_frame = 0;
  326. if (atomic_read(&cx->ana_capturing) == 0 && s->id == -1) {
  327. /* shouldn't happen */
  328. CX18_DEBUG_WARN("Stream %s not initialized before read\n",
  329. s->name);
  330. return -EIO;
  331. }
  332. /* Each VBI buffer is one frame, the v4l2 API says that for VBI the
  333. frames should arrive one-by-one, so make sure we never output more
  334. than one VBI frame at a time */
  335. if (s->type == CX18_ENC_STREAM_TYPE_VBI && !cx18_raw_vbi(cx))
  336. single_frame = 1;
  337. for (;;) {
  338. struct cx18_buffer *buf;
  339. int rc;
  340. buf = cx18_get_buffer(s, non_block, &rc);
  341. /* if there is no data available... */
  342. if (buf == NULL) {
  343. /* if we got data, then return that regardless */
  344. if (tot_written)
  345. break;
  346. /* EOS condition */
  347. if (rc == 0) {
  348. clear_bit(CX18_F_S_STREAMOFF, &s->s_flags);
  349. clear_bit(CX18_F_S_APPL_IO, &s->s_flags);
  350. cx18_release_stream(s);
  351. }
  352. /* set errno */
  353. return rc;
  354. }
  355. rc = cx18_copy_buf_to_user(s, buf, ubuf + tot_written,
  356. tot_count - tot_written);
  357. if (buf != &cx->vbi.sliced_mpeg_buf) {
  358. if (buf->readpos == buf->bytesused)
  359. cx18_stream_put_buf_fw(s, buf);
  360. else
  361. cx18_push(s, buf, &s->q_full);
  362. } else if (buf->readpos == buf->bytesused) {
  363. int idx = cx->vbi.inserted_frame % CX18_VBI_FRAMES;
  364. cx->vbi.sliced_mpeg_size[idx] = 0;
  365. cx->vbi.inserted_frame++;
  366. cx->vbi_data_inserted += buf->bytesused;
  367. }
  368. if (rc < 0)
  369. return rc;
  370. tot_written += rc;
  371. if (tot_written == tot_count || single_frame)
  372. break;
  373. }
  374. return tot_written;
  375. }
  376. static ssize_t cx18_read_pos(struct cx18_stream *s, char __user *ubuf,
  377. size_t count, loff_t *pos, int non_block)
  378. {
  379. ssize_t rc = count ? cx18_read(s, ubuf, count, non_block) : 0;
  380. struct cx18 *cx = s->cx;
  381. CX18_DEBUG_HI_FILE("read %zd from %s, got %zd\n", count, s->name, rc);
  382. if (rc > 0)
  383. pos += rc;
  384. return rc;
  385. }
  386. int cx18_start_capture(struct cx18_open_id *id)
  387. {
  388. struct cx18 *cx = id->cx;
  389. struct cx18_stream *s = &cx->streams[id->type];
  390. struct cx18_stream *s_vbi;
  391. if (s->type == CX18_ENC_STREAM_TYPE_RAD) {
  392. /* you cannot read from these stream types. */
  393. return -EPERM;
  394. }
  395. /* Try to claim this stream. */
  396. if (cx18_claim_stream(id, s->type))
  397. return -EBUSY;
  398. /* If capture is already in progress, then we also have to
  399. do nothing extra. */
  400. if (test_bit(CX18_F_S_STREAMOFF, &s->s_flags) ||
  401. test_and_set_bit(CX18_F_S_STREAMING, &s->s_flags)) {
  402. set_bit(CX18_F_S_APPL_IO, &s->s_flags);
  403. return 0;
  404. }
  405. /* Start VBI capture if required */
  406. s_vbi = &cx->streams[CX18_ENC_STREAM_TYPE_VBI];
  407. if (s->type == CX18_ENC_STREAM_TYPE_MPG &&
  408. test_bit(CX18_F_S_INTERNAL_USE, &s_vbi->s_flags) &&
  409. !test_and_set_bit(CX18_F_S_STREAMING, &s_vbi->s_flags)) {
  410. /* Note: the CX18_ENC_STREAM_TYPE_VBI is claimed
  411. automatically when the MPG stream is claimed.
  412. We only need to start the VBI capturing. */
  413. if (cx18_start_v4l2_encode_stream(s_vbi)) {
  414. CX18_DEBUG_WARN("VBI capture start failed\n");
  415. /* Failure, clean up and return an error */
  416. clear_bit(CX18_F_S_STREAMING, &s_vbi->s_flags);
  417. clear_bit(CX18_F_S_STREAMING, &s->s_flags);
  418. /* also releases the associated VBI stream */
  419. cx18_release_stream(s);
  420. return -EIO;
  421. }
  422. CX18_DEBUG_INFO("VBI insertion started\n");
  423. }
  424. /* Tell the card to start capturing */
  425. if (!cx18_start_v4l2_encode_stream(s)) {
  426. /* We're done */
  427. set_bit(CX18_F_S_APPL_IO, &s->s_flags);
  428. /* Resume a possibly paused encoder */
  429. if (test_and_clear_bit(CX18_F_I_ENC_PAUSED, &cx->i_flags))
  430. cx18_vapi(cx, CX18_CPU_CAPTURE_PAUSE, 1, s->handle);
  431. return 0;
  432. }
  433. /* failure, clean up */
  434. CX18_DEBUG_WARN("Failed to start capturing for stream %s\n", s->name);
  435. /* Note: the CX18_ENC_STREAM_TYPE_VBI is released
  436. automatically when the MPG stream is released.
  437. We only need to stop the VBI capturing. */
  438. if (s->type == CX18_ENC_STREAM_TYPE_MPG &&
  439. test_bit(CX18_F_S_STREAMING, &s_vbi->s_flags)) {
  440. cx18_stop_v4l2_encode_stream(s_vbi, 0);
  441. clear_bit(CX18_F_S_STREAMING, &s_vbi->s_flags);
  442. }
  443. clear_bit(CX18_F_S_STREAMING, &s->s_flags);
  444. cx18_release_stream(s);
  445. return -EIO;
  446. }
  447. ssize_t cx18_v4l2_read(struct file *filp, char __user *buf, size_t count,
  448. loff_t *pos)
  449. {
  450. struct cx18_open_id *id = filp->private_data;
  451. struct cx18 *cx = id->cx;
  452. struct cx18_stream *s = &cx->streams[id->type];
  453. int rc;
  454. CX18_DEBUG_HI_FILE("read %zd bytes from %s\n", count, s->name);
  455. mutex_lock(&cx->serialize_lock);
  456. rc = cx18_start_capture(id);
  457. mutex_unlock(&cx->serialize_lock);
  458. if (rc)
  459. return rc;
  460. return cx18_read_pos(s, buf, count, pos, filp->f_flags & O_NONBLOCK);
  461. }
  462. unsigned int cx18_v4l2_enc_poll(struct file *filp, poll_table *wait)
  463. {
  464. struct cx18_open_id *id = filp->private_data;
  465. struct cx18 *cx = id->cx;
  466. struct cx18_stream *s = &cx->streams[id->type];
  467. int eof = test_bit(CX18_F_S_STREAMOFF, &s->s_flags);
  468. /* Start a capture if there is none */
  469. if (!eof && !test_bit(CX18_F_S_STREAMING, &s->s_flags)) {
  470. int rc;
  471. mutex_lock(&cx->serialize_lock);
  472. rc = cx18_start_capture(id);
  473. mutex_unlock(&cx->serialize_lock);
  474. if (rc) {
  475. CX18_DEBUG_INFO("Could not start capture for %s (%d)\n",
  476. s->name, rc);
  477. return POLLERR;
  478. }
  479. CX18_DEBUG_FILE("Encoder poll started capture\n");
  480. }
  481. /* add stream's waitq to the poll list */
  482. CX18_DEBUG_HI_FILE("Encoder poll\n");
  483. poll_wait(filp, &s->waitq, wait);
  484. if (atomic_read(&s->q_full.buffers))
  485. return POLLIN | POLLRDNORM;
  486. if (eof)
  487. return POLLHUP;
  488. return 0;
  489. }
  490. void cx18_stop_capture(struct cx18_open_id *id, int gop_end)
  491. {
  492. struct cx18 *cx = id->cx;
  493. struct cx18_stream *s = &cx->streams[id->type];
  494. CX18_DEBUG_IOCTL("close() of %s\n", s->name);
  495. /* 'Unclaim' this stream */
  496. /* Stop capturing */
  497. if (test_bit(CX18_F_S_STREAMING, &s->s_flags)) {
  498. struct cx18_stream *s_vbi =
  499. &cx->streams[CX18_ENC_STREAM_TYPE_VBI];
  500. CX18_DEBUG_INFO("close stopping capture\n");
  501. /* Special case: a running VBI capture for VBI insertion
  502. in the mpeg stream. Need to stop that too. */
  503. if (id->type == CX18_ENC_STREAM_TYPE_MPG &&
  504. test_bit(CX18_F_S_STREAMING, &s_vbi->s_flags) &&
  505. !test_bit(CX18_F_S_APPL_IO, &s_vbi->s_flags)) {
  506. CX18_DEBUG_INFO("close stopping embedded VBI capture\n");
  507. cx18_stop_v4l2_encode_stream(s_vbi, 0);
  508. }
  509. if (id->type == CX18_ENC_STREAM_TYPE_VBI &&
  510. test_bit(CX18_F_S_INTERNAL_USE, &s->s_flags))
  511. /* Also used internally, don't stop capturing */
  512. s->id = -1;
  513. else
  514. cx18_stop_v4l2_encode_stream(s, gop_end);
  515. }
  516. if (!gop_end) {
  517. clear_bit(CX18_F_S_APPL_IO, &s->s_flags);
  518. clear_bit(CX18_F_S_STREAMOFF, &s->s_flags);
  519. cx18_release_stream(s);
  520. }
  521. }
  522. int cx18_v4l2_close(struct file *filp)
  523. {
  524. struct cx18_open_id *id = filp->private_data;
  525. struct cx18 *cx = id->cx;
  526. struct cx18_stream *s = &cx->streams[id->type];
  527. CX18_DEBUG_IOCTL("close() of %s\n", s->name);
  528. v4l2_prio_close(&cx->prio, &id->prio);
  529. /* Easy case first: this stream was never claimed by us */
  530. if (s->id != id->open_id) {
  531. kfree(id);
  532. return 0;
  533. }
  534. /* 'Unclaim' this stream */
  535. /* Stop radio */
  536. mutex_lock(&cx->serialize_lock);
  537. if (id->type == CX18_ENC_STREAM_TYPE_RAD) {
  538. /* Closing radio device, return to TV mode */
  539. cx18_mute(cx);
  540. /* Mark that the radio is no longer in use */
  541. clear_bit(CX18_F_I_RADIO_USER, &cx->i_flags);
  542. /* Switch tuner to TV */
  543. cx18_call_all(cx, core, s_std, cx->std);
  544. /* Select correct audio input (i.e. TV tuner or Line in) */
  545. cx18_audio_set_io(cx);
  546. if (atomic_read(&cx->ana_capturing) > 0) {
  547. /* Undo video mute */
  548. cx18_vapi(cx, CX18_CPU_SET_VIDEO_MUTE, 2, s->handle,
  549. cx->params.video_mute |
  550. (cx->params.video_mute_yuv << 8));
  551. }
  552. /* Done! Unmute and continue. */
  553. cx18_unmute(cx);
  554. cx18_release_stream(s);
  555. } else {
  556. cx18_stop_capture(id, 0);
  557. }
  558. kfree(id);
  559. mutex_unlock(&cx->serialize_lock);
  560. return 0;
  561. }
  562. static int cx18_serialized_open(struct cx18_stream *s, struct file *filp)
  563. {
  564. struct cx18 *cx = s->cx;
  565. struct cx18_open_id *item;
  566. CX18_DEBUG_FILE("open %s\n", s->name);
  567. /* Allocate memory */
  568. item = kmalloc(sizeof(struct cx18_open_id), GFP_KERNEL);
  569. if (NULL == item) {
  570. CX18_DEBUG_WARN("nomem on v4l2 open\n");
  571. return -ENOMEM;
  572. }
  573. item->cx = cx;
  574. item->type = s->type;
  575. v4l2_prio_open(&cx->prio, &item->prio);
  576. item->open_id = cx->open_id++;
  577. filp->private_data = item;
  578. if (item->type == CX18_ENC_STREAM_TYPE_RAD) {
  579. /* Try to claim this stream */
  580. if (cx18_claim_stream(item, item->type)) {
  581. /* No, it's already in use */
  582. kfree(item);
  583. return -EBUSY;
  584. }
  585. if (!test_bit(CX18_F_I_RADIO_USER, &cx->i_flags)) {
  586. if (atomic_read(&cx->ana_capturing) > 0) {
  587. /* switching to radio while capture is
  588. in progress is not polite */
  589. cx18_release_stream(s);
  590. kfree(item);
  591. return -EBUSY;
  592. }
  593. }
  594. /* Mark that the radio is being used. */
  595. set_bit(CX18_F_I_RADIO_USER, &cx->i_flags);
  596. /* We have the radio */
  597. cx18_mute(cx);
  598. /* Switch tuner to radio */
  599. cx18_call_all(cx, tuner, s_radio);
  600. /* Select the correct audio input (i.e. radio tuner) */
  601. cx18_audio_set_io(cx);
  602. /* Done! Unmute and continue. */
  603. cx18_unmute(cx);
  604. }
  605. return 0;
  606. }
  607. int cx18_v4l2_open(struct file *filp)
  608. {
  609. int res;
  610. struct video_device *video_dev = video_devdata(filp);
  611. struct cx18_stream *s = video_get_drvdata(video_dev);
  612. struct cx18 *cx = s->cx;
  613. mutex_lock(&cx->serialize_lock);
  614. if (cx18_init_on_first_open(cx)) {
  615. CX18_ERR("Failed to initialize on minor %d\n",
  616. video_dev->minor);
  617. mutex_unlock(&cx->serialize_lock);
  618. return -ENXIO;
  619. }
  620. res = cx18_serialized_open(s, filp);
  621. mutex_unlock(&cx->serialize_lock);
  622. return res;
  623. }
  624. void cx18_mute(struct cx18 *cx)
  625. {
  626. u32 h;
  627. if (atomic_read(&cx->ana_capturing)) {
  628. h = cx18_find_handle(cx);
  629. if (h != CX18_INVALID_TASK_HANDLE)
  630. cx18_vapi(cx, CX18_CPU_SET_AUDIO_MUTE, 2, h, 1);
  631. else
  632. CX18_ERR("Can't find valid task handle for mute\n");
  633. }
  634. CX18_DEBUG_INFO("Mute\n");
  635. }
  636. void cx18_unmute(struct cx18 *cx)
  637. {
  638. u32 h;
  639. if (atomic_read(&cx->ana_capturing)) {
  640. h = cx18_find_handle(cx);
  641. if (h != CX18_INVALID_TASK_HANDLE) {
  642. cx18_msleep_timeout(100, 0);
  643. cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 2, h, 12);
  644. cx18_vapi(cx, CX18_CPU_SET_AUDIO_MUTE, 2, h, 0);
  645. } else
  646. CX18_ERR("Can't find valid task handle for unmute\n");
  647. }
  648. CX18_DEBUG_INFO("Unmute\n");
  649. }