cx18-fileops.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  1. /*
  2. * cx18 file operation functions
  3. *
  4. * Derived from ivtv-fileops.c
  5. *
  6. * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  21. * 02111-1307 USA
  22. */
  23. #include "cx18-driver.h"
  24. #include "cx18-fileops.h"
  25. #include "cx18-i2c.h"
  26. #include "cx18-queue.h"
  27. #include "cx18-vbi.h"
  28. #include "cx18-audio.h"
  29. #include "cx18-mailbox.h"
  30. #include "cx18-scb.h"
  31. #include "cx18-streams.h"
  32. #include "cx18-controls.h"
  33. #include "cx18-ioctl.h"
  34. #include "cx18-cards.h"
  35. /* This function tries to claim the stream for a specific file descriptor.
  36. If no one else is using this stream then the stream is claimed and
  37. associated VBI streams are also automatically claimed.
  38. Possible error returns: -EBUSY if someone else has claimed
  39. the stream or 0 on success. */
  40. static int cx18_claim_stream(struct cx18_open_id *id, int type)
  41. {
  42. struct cx18 *cx = id->cx;
  43. struct cx18_stream *s = &cx->streams[type];
  44. struct cx18_stream *s_vbi;
  45. int vbi_type;
  46. if (test_and_set_bit(CX18_F_S_CLAIMED, &s->s_flags)) {
  47. /* someone already claimed this stream */
  48. if (s->id == id->open_id) {
  49. /* yes, this file descriptor did. So that's OK. */
  50. return 0;
  51. }
  52. if (s->id == -1 && type == CX18_ENC_STREAM_TYPE_VBI) {
  53. /* VBI is handled already internally, now also assign
  54. the file descriptor to this stream for external
  55. reading of the stream. */
  56. s->id = id->open_id;
  57. CX18_DEBUG_INFO("Start Read VBI\n");
  58. return 0;
  59. }
  60. /* someone else is using this stream already */
  61. CX18_DEBUG_INFO("Stream %d is busy\n", type);
  62. return -EBUSY;
  63. }
  64. s->id = id->open_id;
  65. /* CX18_DEC_STREAM_TYPE_MPG needs to claim CX18_DEC_STREAM_TYPE_VBI,
  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 && cx->vbi.sliced_in->service_set) {
  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. u16 new_bitmap;
  120. u16 new_stereo_mode;
  121. const u16 stereo_mask = 0x0300;
  122. const u16 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. if (time_after(jiffies, cx->dualwatch_jiffies + msecs_to_jiffies(1000))) {
  159. cx->dualwatch_jiffies = jiffies;
  160. cx18_dualwatch(cx);
  161. }
  162. if (test_bit(CX18_F_S_INTERNAL_USE, &s_vbi->s_flags) &&
  163. !test_bit(CX18_F_S_APPL_IO, &s_vbi->s_flags)) {
  164. while ((buf = cx18_dequeue(s_vbi, &s_vbi->q_full))) {
  165. /* byteswap and process VBI data */
  166. /* cx18_process_vbi_data(cx, buf, s_vbi->dma_pts, s_vbi->type); */
  167. cx18_enqueue(s_vbi, buf, &s_vbi->q_free);
  168. }
  169. }
  170. buf = &cx->vbi.sliced_mpeg_buf;
  171. if (buf->readpos != buf->bytesused)
  172. return buf;
  173. }
  174. /* do we have leftover data? */
  175. buf = cx18_dequeue(s, &s->q_io);
  176. if (buf)
  177. return buf;
  178. /* do we have new data? */
  179. buf = cx18_dequeue(s, &s->q_full);
  180. if (buf) {
  181. if (!test_and_clear_bit(CX18_F_B_NEED_BUF_SWAP,
  182. &buf->b_flags))
  183. return buf;
  184. if (s->type == CX18_ENC_STREAM_TYPE_MPG)
  185. /* byteswap MPG data */
  186. cx18_buf_swap(buf);
  187. else {
  188. /* byteswap and process VBI data */
  189. cx18_process_vbi_data(cx, buf,
  190. s->dma_pts, s->type);
  191. }
  192. return buf;
  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.buffers))
  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_buf(struct cx18 *cx)
  220. {
  221. int idx = cx->vbi.inserted_frame % CX18_VBI_FRAMES;
  222. cx->vbi.sliced_mpeg_buf.buf = cx->vbi.sliced_mpeg_data[idx];
  223. cx->vbi.sliced_mpeg_buf.bytesused = cx->vbi.sliced_mpeg_size[idx];
  224. cx->vbi.sliced_mpeg_buf.readpos = 0;
  225. }
  226. static size_t cx18_copy_buf_to_user(struct cx18_stream *s,
  227. struct cx18_buffer *buf, char __user *ubuf, size_t ucount)
  228. {
  229. struct cx18 *cx = s->cx;
  230. size_t len = buf->bytesused - buf->readpos;
  231. if (len > ucount)
  232. len = ucount;
  233. if (cx->vbi.insert_mpeg && s->type == CX18_ENC_STREAM_TYPE_MPG &&
  234. cx->vbi.sliced_in->service_set && buf != &cx->vbi.sliced_mpeg_buf) {
  235. const char *start = buf->buf + buf->readpos;
  236. const char *p = start + 1;
  237. const u8 *q;
  238. u8 ch = cx->search_pack_header ? 0xba : 0xe0;
  239. int stuffing, i;
  240. while (start + len > p) {
  241. q = memchr(p, 0, start + len - p);
  242. if (q == NULL)
  243. break;
  244. p = q + 1;
  245. if ((char *)q + 15 >= buf->buf + buf->bytesused ||
  246. q[1] != 0 || q[2] != 1 || q[3] != ch)
  247. continue;
  248. if (!cx->search_pack_header) {
  249. if ((q[6] & 0xc0) != 0x80)
  250. continue;
  251. if (((q[7] & 0xc0) == 0x80 &&
  252. (q[9] & 0xf0) == 0x20) ||
  253. ((q[7] & 0xc0) == 0xc0 &&
  254. (q[9] & 0xf0) == 0x30)) {
  255. ch = 0xba;
  256. cx->search_pack_header = 1;
  257. p = q + 9;
  258. }
  259. continue;
  260. }
  261. stuffing = q[13] & 7;
  262. /* all stuffing bytes must be 0xff */
  263. for (i = 0; i < stuffing; i++)
  264. if (q[14 + i] != 0xff)
  265. break;
  266. if (i == stuffing &&
  267. (q[4] & 0xc4) == 0x44 &&
  268. (q[12] & 3) == 3 &&
  269. q[14 + stuffing] == 0 &&
  270. q[15 + stuffing] == 0 &&
  271. q[16 + stuffing] == 1) {
  272. cx->search_pack_header = 0;
  273. len = (char *)q - start;
  274. cx18_setup_sliced_vbi_buf(cx);
  275. break;
  276. }
  277. }
  278. }
  279. if (copy_to_user(ubuf, (u8 *)buf->buf + buf->readpos, len)) {
  280. CX18_DEBUG_WARN("copy %zd bytes to user failed for %s\n",
  281. len, s->name);
  282. return -EFAULT;
  283. }
  284. buf->readpos += len;
  285. if (s->type == CX18_ENC_STREAM_TYPE_MPG &&
  286. buf != &cx->vbi.sliced_mpeg_buf)
  287. cx->mpg_data_received += len;
  288. return len;
  289. }
  290. static ssize_t cx18_read(struct cx18_stream *s, char __user *ubuf,
  291. size_t tot_count, int non_block)
  292. {
  293. struct cx18 *cx = s->cx;
  294. size_t tot_written = 0;
  295. int single_frame = 0;
  296. if (atomic_read(&cx->ana_capturing) == 0 && s->id == -1) {
  297. /* shouldn't happen */
  298. CX18_DEBUG_WARN("Stream %s not initialized before read\n",
  299. s->name);
  300. return -EIO;
  301. }
  302. /* Each VBI buffer is one frame, the v4l2 API says that for VBI the
  303. frames should arrive one-by-one, so make sure we never output more
  304. than one VBI frame at a time */
  305. if (s->type == CX18_ENC_STREAM_TYPE_VBI &&
  306. cx->vbi.sliced_in->service_set)
  307. single_frame = 1;
  308. for (;;) {
  309. struct cx18_buffer *buf;
  310. int rc;
  311. buf = cx18_get_buffer(s, non_block, &rc);
  312. /* if there is no data available... */
  313. if (buf == NULL) {
  314. /* if we got data, then return that regardless */
  315. if (tot_written)
  316. break;
  317. /* EOS condition */
  318. if (rc == 0) {
  319. clear_bit(CX18_F_S_STREAMOFF, &s->s_flags);
  320. clear_bit(CX18_F_S_APPL_IO, &s->s_flags);
  321. cx18_release_stream(s);
  322. }
  323. /* set errno */
  324. return rc;
  325. }
  326. rc = cx18_copy_buf_to_user(s, buf, ubuf + tot_written,
  327. tot_count - tot_written);
  328. if (buf != &cx->vbi.sliced_mpeg_buf) {
  329. if (buf->readpos == buf->bytesused) {
  330. cx18_buf_sync_for_device(s, buf);
  331. cx18_enqueue(s, buf, &s->q_free);
  332. cx18_vapi(cx, CX18_CPU_DE_SET_MDL, 5,
  333. s->handle,
  334. (void __iomem *)&cx->scb->cpu_mdl[buf->id] -
  335. cx->enc_mem,
  336. 1, buf->id, s->buf_size);
  337. } else
  338. cx18_enqueue(s, buf, &s->q_io);
  339. } else if (buf->readpos == buf->bytesused) {
  340. int idx = cx->vbi.inserted_frame % CX18_VBI_FRAMES;
  341. cx->vbi.sliced_mpeg_size[idx] = 0;
  342. cx->vbi.inserted_frame++;
  343. cx->vbi_data_inserted += buf->bytesused;
  344. }
  345. if (rc < 0)
  346. return rc;
  347. tot_written += rc;
  348. if (tot_written == tot_count || single_frame)
  349. break;
  350. }
  351. return tot_written;
  352. }
  353. static ssize_t cx18_read_pos(struct cx18_stream *s, char __user *ubuf,
  354. size_t count, loff_t *pos, int non_block)
  355. {
  356. ssize_t rc = count ? cx18_read(s, ubuf, count, non_block) : 0;
  357. struct cx18 *cx = s->cx;
  358. CX18_DEBUG_HI_FILE("read %zd from %s, got %zd\n", count, s->name, rc);
  359. if (rc > 0)
  360. pos += rc;
  361. return rc;
  362. }
  363. int cx18_start_capture(struct cx18_open_id *id)
  364. {
  365. struct cx18 *cx = id->cx;
  366. struct cx18_stream *s = &cx->streams[id->type];
  367. struct cx18_stream *s_vbi;
  368. if (s->type == CX18_ENC_STREAM_TYPE_RAD) {
  369. /* you cannot read from these stream types. */
  370. return -EPERM;
  371. }
  372. /* Try to claim this stream. */
  373. if (cx18_claim_stream(id, s->type))
  374. return -EBUSY;
  375. /* If capture is already in progress, then we also have to
  376. do nothing extra. */
  377. if (test_bit(CX18_F_S_STREAMOFF, &s->s_flags) ||
  378. test_and_set_bit(CX18_F_S_STREAMING, &s->s_flags)) {
  379. set_bit(CX18_F_S_APPL_IO, &s->s_flags);
  380. return 0;
  381. }
  382. /* Start VBI capture if required */
  383. s_vbi = &cx->streams[CX18_ENC_STREAM_TYPE_VBI];
  384. if (s->type == CX18_ENC_STREAM_TYPE_MPG &&
  385. test_bit(CX18_F_S_INTERNAL_USE, &s_vbi->s_flags) &&
  386. !test_and_set_bit(CX18_F_S_STREAMING, &s_vbi->s_flags)) {
  387. /* Note: the CX18_ENC_STREAM_TYPE_VBI is claimed
  388. automatically when the MPG stream is claimed.
  389. We only need to start the VBI capturing. */
  390. if (cx18_start_v4l2_encode_stream(s_vbi)) {
  391. CX18_DEBUG_WARN("VBI capture start failed\n");
  392. /* Failure, clean up and return an error */
  393. clear_bit(CX18_F_S_STREAMING, &s_vbi->s_flags);
  394. clear_bit(CX18_F_S_STREAMING, &s->s_flags);
  395. /* also releases the associated VBI stream */
  396. cx18_release_stream(s);
  397. return -EIO;
  398. }
  399. CX18_DEBUG_INFO("VBI insertion started\n");
  400. }
  401. /* Tell the card to start capturing */
  402. if (!cx18_start_v4l2_encode_stream(s)) {
  403. /* We're done */
  404. set_bit(CX18_F_S_APPL_IO, &s->s_flags);
  405. /* Resume a possibly paused encoder */
  406. if (test_and_clear_bit(CX18_F_I_ENC_PAUSED, &cx->i_flags))
  407. cx18_vapi(cx, CX18_CPU_CAPTURE_PAUSE, 1, s->handle);
  408. return 0;
  409. }
  410. /* failure, clean up */
  411. CX18_DEBUG_WARN("Failed to start capturing for stream %s\n", s->name);
  412. /* Note: the CX18_ENC_STREAM_TYPE_VBI is released
  413. automatically when the MPG stream is released.
  414. We only need to stop the VBI capturing. */
  415. if (s->type == CX18_ENC_STREAM_TYPE_MPG &&
  416. test_bit(CX18_F_S_STREAMING, &s_vbi->s_flags)) {
  417. cx18_stop_v4l2_encode_stream(s_vbi, 0);
  418. clear_bit(CX18_F_S_STREAMING, &s_vbi->s_flags);
  419. }
  420. clear_bit(CX18_F_S_STREAMING, &s->s_flags);
  421. cx18_release_stream(s);
  422. return -EIO;
  423. }
  424. ssize_t cx18_v4l2_read(struct file *filp, char __user *buf, size_t count,
  425. loff_t *pos)
  426. {
  427. struct cx18_open_id *id = filp->private_data;
  428. struct cx18 *cx = id->cx;
  429. struct cx18_stream *s = &cx->streams[id->type];
  430. int rc;
  431. CX18_DEBUG_HI_FILE("read %zd bytes from %s\n", count, s->name);
  432. mutex_lock(&cx->serialize_lock);
  433. rc = cx18_start_capture(id);
  434. mutex_unlock(&cx->serialize_lock);
  435. if (rc)
  436. return rc;
  437. return cx18_read_pos(s, buf, count, pos, filp->f_flags & O_NONBLOCK);
  438. }
  439. unsigned int cx18_v4l2_enc_poll(struct file *filp, poll_table *wait)
  440. {
  441. struct cx18_open_id *id = filp->private_data;
  442. struct cx18 *cx = id->cx;
  443. struct cx18_stream *s = &cx->streams[id->type];
  444. int eof = test_bit(CX18_F_S_STREAMOFF, &s->s_flags);
  445. /* Start a capture if there is none */
  446. if (!eof && !test_bit(CX18_F_S_STREAMING, &s->s_flags)) {
  447. int rc;
  448. mutex_lock(&cx->serialize_lock);
  449. rc = cx18_start_capture(id);
  450. mutex_unlock(&cx->serialize_lock);
  451. if (rc) {
  452. CX18_DEBUG_INFO("Could not start capture for %s (%d)\n",
  453. s->name, rc);
  454. return POLLERR;
  455. }
  456. CX18_DEBUG_FILE("Encoder poll started capture\n");
  457. }
  458. /* add stream's waitq to the poll list */
  459. CX18_DEBUG_HI_FILE("Encoder poll\n");
  460. poll_wait(filp, &s->waitq, wait);
  461. if (atomic_read(&s->q_full.buffers) || atomic_read(&s->q_io.buffers))
  462. return POLLIN | POLLRDNORM;
  463. if (eof)
  464. return POLLHUP;
  465. return 0;
  466. }
  467. void cx18_stop_capture(struct cx18_open_id *id, int gop_end)
  468. {
  469. struct cx18 *cx = id->cx;
  470. struct cx18_stream *s = &cx->streams[id->type];
  471. CX18_DEBUG_IOCTL("close() of %s\n", s->name);
  472. /* 'Unclaim' this stream */
  473. /* Stop capturing */
  474. if (test_bit(CX18_F_S_STREAMING, &s->s_flags)) {
  475. struct cx18_stream *s_vbi =
  476. &cx->streams[CX18_ENC_STREAM_TYPE_VBI];
  477. CX18_DEBUG_INFO("close stopping capture\n");
  478. /* Special case: a running VBI capture for VBI insertion
  479. in the mpeg stream. Need to stop that too. */
  480. if (id->type == CX18_ENC_STREAM_TYPE_MPG &&
  481. test_bit(CX18_F_S_STREAMING, &s_vbi->s_flags) &&
  482. !test_bit(CX18_F_S_APPL_IO, &s_vbi->s_flags)) {
  483. CX18_DEBUG_INFO("close stopping embedded VBI capture\n");
  484. cx18_stop_v4l2_encode_stream(s_vbi, 0);
  485. }
  486. if (id->type == CX18_ENC_STREAM_TYPE_VBI &&
  487. test_bit(CX18_F_S_INTERNAL_USE, &s->s_flags))
  488. /* Also used internally, don't stop capturing */
  489. s->id = -1;
  490. else
  491. cx18_stop_v4l2_encode_stream(s, gop_end);
  492. }
  493. if (!gop_end) {
  494. clear_bit(CX18_F_S_APPL_IO, &s->s_flags);
  495. clear_bit(CX18_F_S_STREAMOFF, &s->s_flags);
  496. cx18_release_stream(s);
  497. }
  498. }
  499. int cx18_v4l2_close(struct inode *inode, struct file *filp)
  500. {
  501. struct cx18_open_id *id = filp->private_data;
  502. struct cx18 *cx = id->cx;
  503. struct cx18_stream *s = &cx->streams[id->type];
  504. CX18_DEBUG_IOCTL("close() of %s\n", s->name);
  505. v4l2_prio_close(&cx->prio, &id->prio);
  506. /* Easy case first: this stream was never claimed by us */
  507. if (s->id != id->open_id) {
  508. kfree(id);
  509. return 0;
  510. }
  511. /* 'Unclaim' this stream */
  512. /* Stop radio */
  513. mutex_lock(&cx->serialize_lock);
  514. if (id->type == CX18_ENC_STREAM_TYPE_RAD) {
  515. /* Closing radio device, return to TV mode */
  516. cx18_mute(cx);
  517. /* Mark that the radio is no longer in use */
  518. clear_bit(CX18_F_I_RADIO_USER, &cx->i_flags);
  519. /* Switch tuner to TV */
  520. cx18_call_i2c_clients(cx, VIDIOC_S_STD, &cx->std);
  521. /* Select correct audio input (i.e. TV tuner or Line in) */
  522. cx18_audio_set_io(cx);
  523. if (atomic_read(&cx->ana_capturing) > 0) {
  524. /* Undo video mute */
  525. cx18_vapi(cx, CX18_CPU_SET_VIDEO_MUTE, 2, s->handle,
  526. cx->params.video_mute |
  527. (cx->params.video_mute_yuv << 8));
  528. }
  529. /* Done! Unmute and continue. */
  530. cx18_unmute(cx);
  531. cx18_release_stream(s);
  532. } else {
  533. cx18_stop_capture(id, 0);
  534. }
  535. kfree(id);
  536. mutex_unlock(&cx->serialize_lock);
  537. return 0;
  538. }
  539. static int cx18_serialized_open(struct cx18_stream *s, struct file *filp)
  540. {
  541. struct cx18 *cx = s->cx;
  542. struct cx18_open_id *item;
  543. CX18_DEBUG_FILE("open %s\n", s->name);
  544. /* Allocate memory */
  545. item = kmalloc(sizeof(struct cx18_open_id), GFP_KERNEL);
  546. if (NULL == item) {
  547. CX18_DEBUG_WARN("nomem on v4l2 open\n");
  548. return -ENOMEM;
  549. }
  550. item->cx = cx;
  551. item->type = s->type;
  552. v4l2_prio_open(&cx->prio, &item->prio);
  553. item->open_id = cx->open_id++;
  554. filp->private_data = item;
  555. if (item->type == CX18_ENC_STREAM_TYPE_RAD) {
  556. /* Try to claim this stream */
  557. if (cx18_claim_stream(item, item->type)) {
  558. /* No, it's already in use */
  559. kfree(item);
  560. return -EBUSY;
  561. }
  562. if (!test_bit(CX18_F_I_RADIO_USER, &cx->i_flags)) {
  563. if (atomic_read(&cx->ana_capturing) > 0) {
  564. /* switching to radio while capture is
  565. in progress is not polite */
  566. cx18_release_stream(s);
  567. kfree(item);
  568. return -EBUSY;
  569. }
  570. }
  571. /* Mark that the radio is being used. */
  572. set_bit(CX18_F_I_RADIO_USER, &cx->i_flags);
  573. /* We have the radio */
  574. cx18_mute(cx);
  575. /* Switch tuner to radio */
  576. cx18_call_i2c_clients(cx, AUDC_SET_RADIO, NULL);
  577. /* Select the correct audio input (i.e. radio tuner) */
  578. cx18_audio_set_io(cx);
  579. /* Done! Unmute and continue. */
  580. cx18_unmute(cx);
  581. }
  582. return 0;
  583. }
  584. int cx18_v4l2_open(struct inode *inode, struct file *filp)
  585. {
  586. int res, x, y = 0;
  587. struct cx18 *cx = NULL;
  588. struct cx18_stream *s = NULL;
  589. int minor = iminor(inode);
  590. /* Find which card this open was on */
  591. spin_lock(&cx18_cards_lock);
  592. for (x = 0; cx == NULL && x < cx18_cards_active; x++) {
  593. /* find out which stream this open was on */
  594. for (y = 0; y < CX18_MAX_STREAMS; y++) {
  595. if (cx18_cards[x] == NULL)
  596. continue;
  597. s = &cx18_cards[x]->streams[y];
  598. if (s->v4l2dev && s->v4l2dev->minor == minor) {
  599. cx = cx18_cards[x];
  600. break;
  601. }
  602. }
  603. }
  604. spin_unlock(&cx18_cards_lock);
  605. if (cx == NULL) {
  606. /* Couldn't find a device registered
  607. on that minor, shouldn't happen! */
  608. printk(KERN_WARNING "No cx18 device found on minor %d\n",
  609. minor);
  610. return -ENXIO;
  611. }
  612. mutex_lock(&cx->serialize_lock);
  613. if (cx18_init_on_first_open(cx)) {
  614. CX18_ERR("Failed to initialize on minor %d\n", minor);
  615. mutex_unlock(&cx->serialize_lock);
  616. return -ENXIO;
  617. }
  618. res = cx18_serialized_open(s, filp);
  619. mutex_unlock(&cx->serialize_lock);
  620. return res;
  621. }
  622. void cx18_mute(struct cx18 *cx)
  623. {
  624. u32 h;
  625. if (atomic_read(&cx->ana_capturing)) {
  626. h = cx18_find_handle(cx);
  627. if (h != CX18_INVALID_TASK_HANDLE)
  628. cx18_vapi(cx, CX18_CPU_SET_AUDIO_MUTE, 2, h, 1);
  629. else
  630. CX18_ERR("Can't find valid task handle for mute\n");
  631. }
  632. CX18_DEBUG_INFO("Mute\n");
  633. }
  634. void cx18_unmute(struct cx18 *cx)
  635. {
  636. u32 h;
  637. if (atomic_read(&cx->ana_capturing)) {
  638. h = cx18_find_handle(cx);
  639. if (h != CX18_INVALID_TASK_HANDLE) {
  640. cx18_msleep_timeout(100, 0);
  641. cx18_vapi(cx, CX18_CPU_SET_MISC_PARAMETERS, 2, h, 12);
  642. cx18_vapi(cx, CX18_CPU_SET_AUDIO_MUTE, 2, h, 0);
  643. } else
  644. CX18_ERR("Can't find valid task handle for unmute\n");
  645. }
  646. CX18_DEBUG_INFO("Unmute\n");
  647. }