cx18-fileops.c 20 KB

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