/drivers/media/video/ivtv/ivtv-fileops.c

https://bitbucket.org/wisechild/galaxy-nexus · C · 1065 lines · 792 code · 122 blank · 151 comment · 283 complexity · f64fb6468c4c31527838d9976f77b311 MD5 · raw file

  1. /*
  2. file operation functions
  3. Copyright (C) 2003-2004 Kevin Thayer <nufan_wfk at yahoo.com>
  4. Copyright (C) 2004 Chris Kennedy <c@groovy.org>
  5. Copyright (C) 2005-2007 Hans Verkuil <hverkuil@xs4all.nl>
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. */
  18. #include "ivtv-driver.h"
  19. #include "ivtv-fileops.h"
  20. #include "ivtv-i2c.h"
  21. #include "ivtv-queue.h"
  22. #include "ivtv-udma.h"
  23. #include "ivtv-irq.h"
  24. #include "ivtv-vbi.h"
  25. #include "ivtv-mailbox.h"
  26. #include "ivtv-routing.h"
  27. #include "ivtv-streams.h"
  28. #include "ivtv-yuv.h"
  29. #include "ivtv-ioctl.h"
  30. #include "ivtv-cards.h"
  31. #include "ivtv-firmware.h"
  32. #include <media/v4l2-event.h>
  33. #include <media/saa7115.h>
  34. /* This function tries to claim the stream for a specific file descriptor.
  35. If no one else is using this stream then the stream is claimed and
  36. associated VBI streams are also automatically claimed.
  37. Possible error returns: -EBUSY if someone else has claimed
  38. the stream or 0 on success. */
  39. static int ivtv_claim_stream(struct ivtv_open_id *id, int type)
  40. {
  41. struct ivtv *itv = id->itv;
  42. struct ivtv_stream *s = &itv->streams[type];
  43. struct ivtv_stream *s_vbi;
  44. int vbi_type;
  45. if (test_and_set_bit(IVTV_F_S_CLAIMED, &s->s_flags)) {
  46. /* someone already claimed this stream */
  47. if (s->id == id->open_id) {
  48. /* yes, this file descriptor did. So that's OK. */
  49. return 0;
  50. }
  51. if (s->id == -1 && (type == IVTV_DEC_STREAM_TYPE_VBI ||
  52. type == IVTV_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. IVTV_DEBUG_INFO("Start Read VBI\n");
  58. return 0;
  59. }
  60. /* someone else is using this stream already */
  61. IVTV_DEBUG_INFO("Stream %d is busy\n", type);
  62. return -EBUSY;
  63. }
  64. s->id = id->open_id;
  65. if (type == IVTV_DEC_STREAM_TYPE_VBI) {
  66. /* Enable reinsertion interrupt */
  67. ivtv_clear_irq_mask(itv, IVTV_IRQ_DEC_VBI_RE_INSERT);
  68. }
  69. /* IVTV_DEC_STREAM_TYPE_MPG needs to claim IVTV_DEC_STREAM_TYPE_VBI,
  70. IVTV_ENC_STREAM_TYPE_MPG needs to claim IVTV_ENC_STREAM_TYPE_VBI
  71. (provided VBI insertion is on and sliced VBI is selected), for all
  72. other streams we're done */
  73. if (type == IVTV_DEC_STREAM_TYPE_MPG) {
  74. vbi_type = IVTV_DEC_STREAM_TYPE_VBI;
  75. } else if (type == IVTV_ENC_STREAM_TYPE_MPG &&
  76. itv->vbi.insert_mpeg && !ivtv_raw_vbi(itv)) {
  77. vbi_type = IVTV_ENC_STREAM_TYPE_VBI;
  78. } else {
  79. return 0;
  80. }
  81. s_vbi = &itv->streams[vbi_type];
  82. if (!test_and_set_bit(IVTV_F_S_CLAIMED, &s_vbi->s_flags)) {
  83. /* Enable reinsertion interrupt */
  84. if (vbi_type == IVTV_DEC_STREAM_TYPE_VBI)
  85. ivtv_clear_irq_mask(itv, IVTV_IRQ_DEC_VBI_RE_INSERT);
  86. }
  87. /* mark that it is used internally */
  88. set_bit(IVTV_F_S_INTERNAL_USE, &s_vbi->s_flags);
  89. return 0;
  90. }
  91. /* This function releases a previously claimed stream. It will take into
  92. account associated VBI streams. */
  93. void ivtv_release_stream(struct ivtv_stream *s)
  94. {
  95. struct ivtv *itv = s->itv;
  96. struct ivtv_stream *s_vbi;
  97. s->id = -1;
  98. if ((s->type == IVTV_DEC_STREAM_TYPE_VBI || s->type == IVTV_ENC_STREAM_TYPE_VBI) &&
  99. test_bit(IVTV_F_S_INTERNAL_USE, &s->s_flags)) {
  100. /* this stream is still in use internally */
  101. return;
  102. }
  103. if (!test_and_clear_bit(IVTV_F_S_CLAIMED, &s->s_flags)) {
  104. IVTV_DEBUG_WARN("Release stream %s not in use!\n", s->name);
  105. return;
  106. }
  107. ivtv_flush_queues(s);
  108. /* disable reinsertion interrupt */
  109. if (s->type == IVTV_DEC_STREAM_TYPE_VBI)
  110. ivtv_set_irq_mask(itv, IVTV_IRQ_DEC_VBI_RE_INSERT);
  111. /* IVTV_DEC_STREAM_TYPE_MPG needs to release IVTV_DEC_STREAM_TYPE_VBI,
  112. IVTV_ENC_STREAM_TYPE_MPG needs to release IVTV_ENC_STREAM_TYPE_VBI,
  113. for all other streams we're done */
  114. if (s->type == IVTV_DEC_STREAM_TYPE_MPG)
  115. s_vbi = &itv->streams[IVTV_DEC_STREAM_TYPE_VBI];
  116. else if (s->type == IVTV_ENC_STREAM_TYPE_MPG)
  117. s_vbi = &itv->streams[IVTV_ENC_STREAM_TYPE_VBI];
  118. else
  119. return;
  120. /* clear internal use flag */
  121. if (!test_and_clear_bit(IVTV_F_S_INTERNAL_USE, &s_vbi->s_flags)) {
  122. /* was already cleared */
  123. return;
  124. }
  125. if (s_vbi->id != -1) {
  126. /* VBI stream still claimed by a file descriptor */
  127. return;
  128. }
  129. /* disable reinsertion interrupt */
  130. if (s_vbi->type == IVTV_DEC_STREAM_TYPE_VBI)
  131. ivtv_set_irq_mask(itv, IVTV_IRQ_DEC_VBI_RE_INSERT);
  132. clear_bit(IVTV_F_S_CLAIMED, &s_vbi->s_flags);
  133. ivtv_flush_queues(s_vbi);
  134. }
  135. static void ivtv_dualwatch(struct ivtv *itv)
  136. {
  137. struct v4l2_tuner vt;
  138. u32 new_stereo_mode;
  139. const u32 dual = 0x02;
  140. new_stereo_mode = v4l2_ctrl_g_ctrl(itv->cxhdl.audio_mode);
  141. memset(&vt, 0, sizeof(vt));
  142. ivtv_call_all(itv, tuner, g_tuner, &vt);
  143. if (vt.audmode == V4L2_TUNER_MODE_LANG1_LANG2 && (vt.rxsubchans & V4L2_TUNER_SUB_LANG2))
  144. new_stereo_mode = dual;
  145. if (new_stereo_mode == itv->dualwatch_stereo_mode)
  146. return;
  147. IVTV_DEBUG_INFO("dualwatch: change stereo flag from 0x%x to 0x%x.\n",
  148. itv->dualwatch_stereo_mode, new_stereo_mode);
  149. if (v4l2_ctrl_s_ctrl(itv->cxhdl.audio_mode, new_stereo_mode))
  150. IVTV_DEBUG_INFO("dualwatch: changing stereo flag failed\n");
  151. }
  152. static void ivtv_update_pgm_info(struct ivtv *itv)
  153. {
  154. u32 wr_idx = (read_enc(itv->pgm_info_offset) - itv->pgm_info_offset - 4) / 24;
  155. int cnt;
  156. int i = 0;
  157. if (wr_idx >= itv->pgm_info_num) {
  158. IVTV_DEBUG_WARN("Invalid PGM index %d (>= %d)\n", wr_idx, itv->pgm_info_num);
  159. return;
  160. }
  161. cnt = (wr_idx + itv->pgm_info_num - itv->pgm_info_write_idx) % itv->pgm_info_num;
  162. while (i < cnt) {
  163. int idx = (itv->pgm_info_write_idx + i) % itv->pgm_info_num;
  164. struct v4l2_enc_idx_entry *e = itv->pgm_info + idx;
  165. u32 addr = itv->pgm_info_offset + 4 + idx * 24;
  166. const int mapping[8] = { -1, V4L2_ENC_IDX_FRAME_I, V4L2_ENC_IDX_FRAME_P, -1,
  167. V4L2_ENC_IDX_FRAME_B, -1, -1, -1 };
  168. // 1=I, 2=P, 4=B
  169. e->offset = read_enc(addr + 4) + ((u64)read_enc(addr + 8) << 32);
  170. if (e->offset > itv->mpg_data_received) {
  171. break;
  172. }
  173. e->offset += itv->vbi_data_inserted;
  174. e->length = read_enc(addr);
  175. e->pts = read_enc(addr + 16) + ((u64)(read_enc(addr + 20) & 1) << 32);
  176. e->flags = mapping[read_enc(addr + 12) & 7];
  177. i++;
  178. }
  179. itv->pgm_info_write_idx = (itv->pgm_info_write_idx + i) % itv->pgm_info_num;
  180. }
  181. static struct ivtv_buffer *ivtv_get_buffer(struct ivtv_stream *s, int non_block, int *err)
  182. {
  183. struct ivtv *itv = s->itv;
  184. struct ivtv_stream *s_vbi = &itv->streams[IVTV_ENC_STREAM_TYPE_VBI];
  185. struct ivtv_buffer *buf;
  186. DEFINE_WAIT(wait);
  187. *err = 0;
  188. while (1) {
  189. if (s->type == IVTV_ENC_STREAM_TYPE_MPG) {
  190. /* Process pending program info updates and pending VBI data */
  191. ivtv_update_pgm_info(itv);
  192. if (time_after(jiffies,
  193. itv->dualwatch_jiffies +
  194. msecs_to_jiffies(1000))) {
  195. itv->dualwatch_jiffies = jiffies;
  196. ivtv_dualwatch(itv);
  197. }
  198. if (test_bit(IVTV_F_S_INTERNAL_USE, &s_vbi->s_flags) &&
  199. !test_bit(IVTV_F_S_APPL_IO, &s_vbi->s_flags)) {
  200. while ((buf = ivtv_dequeue(s_vbi, &s_vbi->q_full))) {
  201. /* byteswap and process VBI data */
  202. ivtv_process_vbi_data(itv, buf, s_vbi->dma_pts, s_vbi->type);
  203. ivtv_enqueue(s_vbi, buf, &s_vbi->q_free);
  204. }
  205. }
  206. buf = &itv->vbi.sliced_mpeg_buf;
  207. if (buf->readpos != buf->bytesused) {
  208. return buf;
  209. }
  210. }
  211. /* do we have leftover data? */
  212. buf = ivtv_dequeue(s, &s->q_io);
  213. if (buf)
  214. return buf;
  215. /* do we have new data? */
  216. buf = ivtv_dequeue(s, &s->q_full);
  217. if (buf) {
  218. if ((buf->b_flags & IVTV_F_B_NEED_BUF_SWAP) == 0)
  219. return buf;
  220. buf->b_flags &= ~IVTV_F_B_NEED_BUF_SWAP;
  221. if (s->type == IVTV_ENC_STREAM_TYPE_MPG)
  222. /* byteswap MPG data */
  223. ivtv_buf_swap(buf);
  224. else if (s->type != IVTV_DEC_STREAM_TYPE_VBI) {
  225. /* byteswap and process VBI data */
  226. ivtv_process_vbi_data(itv, buf, s->dma_pts, s->type);
  227. }
  228. return buf;
  229. }
  230. /* return if end of stream */
  231. if (s->type != IVTV_DEC_STREAM_TYPE_VBI && !test_bit(IVTV_F_S_STREAMING, &s->s_flags)) {
  232. IVTV_DEBUG_INFO("EOS %s\n", s->name);
  233. return NULL;
  234. }
  235. /* return if file was opened with O_NONBLOCK */
  236. if (non_block) {
  237. *err = -EAGAIN;
  238. return NULL;
  239. }
  240. /* wait for more data to arrive */
  241. prepare_to_wait(&s->waitq, &wait, TASK_INTERRUPTIBLE);
  242. /* New buffers might have become available before we were added to the waitqueue */
  243. if (!s->q_full.buffers)
  244. schedule();
  245. finish_wait(&s->waitq, &wait);
  246. if (signal_pending(current)) {
  247. /* return if a signal was received */
  248. IVTV_DEBUG_INFO("User stopped %s\n", s->name);
  249. *err = -EINTR;
  250. return NULL;
  251. }
  252. }
  253. }
  254. static void ivtv_setup_sliced_vbi_buf(struct ivtv *itv)
  255. {
  256. int idx = itv->vbi.inserted_frame % IVTV_VBI_FRAMES;
  257. itv->vbi.sliced_mpeg_buf.buf = itv->vbi.sliced_mpeg_data[idx];
  258. itv->vbi.sliced_mpeg_buf.bytesused = itv->vbi.sliced_mpeg_size[idx];
  259. itv->vbi.sliced_mpeg_buf.readpos = 0;
  260. }
  261. static size_t ivtv_copy_buf_to_user(struct ivtv_stream *s, struct ivtv_buffer *buf,
  262. char __user *ubuf, size_t ucount)
  263. {
  264. struct ivtv *itv = s->itv;
  265. size_t len = buf->bytesused - buf->readpos;
  266. if (len > ucount) len = ucount;
  267. if (itv->vbi.insert_mpeg && s->type == IVTV_ENC_STREAM_TYPE_MPG &&
  268. !ivtv_raw_vbi(itv) && buf != &itv->vbi.sliced_mpeg_buf) {
  269. const char *start = buf->buf + buf->readpos;
  270. const char *p = start + 1;
  271. const u8 *q;
  272. u8 ch = itv->search_pack_header ? 0xba : 0xe0;
  273. int stuffing, i;
  274. while (start + len > p && (q = memchr(p, 0, start + len - p))) {
  275. p = q + 1;
  276. if ((char *)q + 15 >= buf->buf + buf->bytesused ||
  277. q[1] != 0 || q[2] != 1 || q[3] != ch) {
  278. continue;
  279. }
  280. if (!itv->search_pack_header) {
  281. if ((q[6] & 0xc0) != 0x80)
  282. continue;
  283. if (((q[7] & 0xc0) == 0x80 && (q[9] & 0xf0) == 0x20) ||
  284. ((q[7] & 0xc0) == 0xc0 && (q[9] & 0xf0) == 0x30)) {
  285. ch = 0xba;
  286. itv->search_pack_header = 1;
  287. p = q + 9;
  288. }
  289. continue;
  290. }
  291. stuffing = q[13] & 7;
  292. /* all stuffing bytes must be 0xff */
  293. for (i = 0; i < stuffing; i++)
  294. if (q[14 + i] != 0xff)
  295. break;
  296. if (i == stuffing && (q[4] & 0xc4) == 0x44 && (q[12] & 3) == 3 &&
  297. q[14 + stuffing] == 0 && q[15 + stuffing] == 0 &&
  298. q[16 + stuffing] == 1) {
  299. itv->search_pack_header = 0;
  300. len = (char *)q - start;
  301. ivtv_setup_sliced_vbi_buf(itv);
  302. break;
  303. }
  304. }
  305. }
  306. if (copy_to_user(ubuf, (u8 *)buf->buf + buf->readpos, len)) {
  307. IVTV_DEBUG_WARN("copy %zd bytes to user failed for %s\n", len, s->name);
  308. return -EFAULT;
  309. }
  310. /*IVTV_INFO("copied %lld %d %d %d %d %d vbi %d\n", itv->mpg_data_received, len, ucount,
  311. buf->readpos, buf->bytesused, buf->bytesused - buf->readpos - len,
  312. buf == &itv->vbi.sliced_mpeg_buf); */
  313. buf->readpos += len;
  314. if (s->type == IVTV_ENC_STREAM_TYPE_MPG && buf != &itv->vbi.sliced_mpeg_buf)
  315. itv->mpg_data_received += len;
  316. return len;
  317. }
  318. static ssize_t ivtv_read(struct ivtv_stream *s, char __user *ubuf, size_t tot_count, int non_block)
  319. {
  320. struct ivtv *itv = s->itv;
  321. size_t tot_written = 0;
  322. int single_frame = 0;
  323. if (atomic_read(&itv->capturing) == 0 && s->id == -1) {
  324. /* shouldn't happen */
  325. IVTV_DEBUG_WARN("Stream %s not initialized before read\n", s->name);
  326. return -EIO;
  327. }
  328. /* Each VBI buffer is one frame, the v4l2 API says that for VBI the frames should
  329. arrive one-by-one, so make sure we never output more than one VBI frame at a time */
  330. if (s->type == IVTV_DEC_STREAM_TYPE_VBI ||
  331. (s->type == IVTV_ENC_STREAM_TYPE_VBI && !ivtv_raw_vbi(itv)))
  332. single_frame = 1;
  333. for (;;) {
  334. struct ivtv_buffer *buf;
  335. int rc;
  336. buf = ivtv_get_buffer(s, non_block, &rc);
  337. /* if there is no data available... */
  338. if (buf == NULL) {
  339. /* if we got data, then return that regardless */
  340. if (tot_written)
  341. break;
  342. /* EOS condition */
  343. if (rc == 0) {
  344. clear_bit(IVTV_F_S_STREAMOFF, &s->s_flags);
  345. clear_bit(IVTV_F_S_APPL_IO, &s->s_flags);
  346. ivtv_release_stream(s);
  347. }
  348. /* set errno */
  349. return rc;
  350. }
  351. rc = ivtv_copy_buf_to_user(s, buf, ubuf + tot_written, tot_count - tot_written);
  352. if (buf != &itv->vbi.sliced_mpeg_buf) {
  353. ivtv_enqueue(s, buf, (buf->readpos == buf->bytesused) ? &s->q_free : &s->q_io);
  354. }
  355. else if (buf->readpos == buf->bytesused) {
  356. int idx = itv->vbi.inserted_frame % IVTV_VBI_FRAMES;
  357. itv->vbi.sliced_mpeg_size[idx] = 0;
  358. itv->vbi.inserted_frame++;
  359. itv->vbi_data_inserted += buf->bytesused;
  360. }
  361. if (rc < 0)
  362. return rc;
  363. tot_written += rc;
  364. if (tot_written == tot_count || single_frame)
  365. break;
  366. }
  367. return tot_written;
  368. }
  369. static ssize_t ivtv_read_pos(struct ivtv_stream *s, char __user *ubuf, size_t count,
  370. loff_t *pos, int non_block)
  371. {
  372. ssize_t rc = count ? ivtv_read(s, ubuf, count, non_block) : 0;
  373. struct ivtv *itv = s->itv;
  374. IVTV_DEBUG_HI_FILE("read %zd from %s, got %zd\n", count, s->name, rc);
  375. if (rc > 0)
  376. pos += rc;
  377. return rc;
  378. }
  379. int ivtv_start_capture(struct ivtv_open_id *id)
  380. {
  381. struct ivtv *itv = id->itv;
  382. struct ivtv_stream *s = &itv->streams[id->type];
  383. struct ivtv_stream *s_vbi;
  384. if (s->type == IVTV_ENC_STREAM_TYPE_RAD ||
  385. s->type == IVTV_DEC_STREAM_TYPE_MPG ||
  386. s->type == IVTV_DEC_STREAM_TYPE_YUV ||
  387. s->type == IVTV_DEC_STREAM_TYPE_VOUT) {
  388. /* you cannot read from these stream types. */
  389. return -EPERM;
  390. }
  391. /* Try to claim this stream. */
  392. if (ivtv_claim_stream(id, s->type))
  393. return -EBUSY;
  394. /* This stream does not need to start capturing */
  395. if (s->type == IVTV_DEC_STREAM_TYPE_VBI) {
  396. set_bit(IVTV_F_S_APPL_IO, &s->s_flags);
  397. return 0;
  398. }
  399. /* If capture is already in progress, then we also have to
  400. do nothing extra. */
  401. if (test_bit(IVTV_F_S_STREAMOFF, &s->s_flags) || test_and_set_bit(IVTV_F_S_STREAMING, &s->s_flags)) {
  402. set_bit(IVTV_F_S_APPL_IO, &s->s_flags);
  403. return 0;
  404. }
  405. /* Start VBI capture if required */
  406. s_vbi = &itv->streams[IVTV_ENC_STREAM_TYPE_VBI];
  407. if (s->type == IVTV_ENC_STREAM_TYPE_MPG &&
  408. test_bit(IVTV_F_S_INTERNAL_USE, &s_vbi->s_flags) &&
  409. !test_and_set_bit(IVTV_F_S_STREAMING, &s_vbi->s_flags)) {
  410. /* Note: the IVTV_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 (ivtv_start_v4l2_encode_stream(s_vbi)) {
  414. IVTV_DEBUG_WARN("VBI capture start failed\n");
  415. /* Failure, clean up and return an error */
  416. clear_bit(IVTV_F_S_STREAMING, &s_vbi->s_flags);
  417. clear_bit(IVTV_F_S_STREAMING, &s->s_flags);
  418. /* also releases the associated VBI stream */
  419. ivtv_release_stream(s);
  420. return -EIO;
  421. }
  422. IVTV_DEBUG_INFO("VBI insertion started\n");
  423. }
  424. /* Tell the card to start capturing */
  425. if (!ivtv_start_v4l2_encode_stream(s)) {
  426. /* We're done */
  427. set_bit(IVTV_F_S_APPL_IO, &s->s_flags);
  428. /* Resume a possibly paused encoder */
  429. if (test_and_clear_bit(IVTV_F_I_ENC_PAUSED, &itv->i_flags))
  430. ivtv_vapi(itv, CX2341X_ENC_PAUSE_ENCODER, 1, 1);
  431. return 0;
  432. }
  433. /* failure, clean up */
  434. IVTV_DEBUG_WARN("Failed to start capturing for stream %s\n", s->name);
  435. /* Note: the IVTV_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 == IVTV_ENC_STREAM_TYPE_MPG &&
  439. test_bit(IVTV_F_S_STREAMING, &s_vbi->s_flags)) {
  440. ivtv_stop_v4l2_encode_stream(s_vbi, 0);
  441. clear_bit(IVTV_F_S_STREAMING, &s_vbi->s_flags);
  442. }
  443. clear_bit(IVTV_F_S_STREAMING, &s->s_flags);
  444. ivtv_release_stream(s);
  445. return -EIO;
  446. }
  447. ssize_t ivtv_v4l2_read(struct file * filp, char __user *buf, size_t count, loff_t * pos)
  448. {
  449. struct ivtv_open_id *id = fh2id(filp->private_data);
  450. struct ivtv *itv = id->itv;
  451. struct ivtv_stream *s = &itv->streams[id->type];
  452. int rc;
  453. IVTV_DEBUG_HI_FILE("read %zd bytes from %s\n", count, s->name);
  454. mutex_lock(&itv->serialize_lock);
  455. rc = ivtv_start_capture(id);
  456. mutex_unlock(&itv->serialize_lock);
  457. if (rc)
  458. return rc;
  459. return ivtv_read_pos(s, buf, count, pos, filp->f_flags & O_NONBLOCK);
  460. }
  461. int ivtv_start_decoding(struct ivtv_open_id *id, int speed)
  462. {
  463. struct ivtv *itv = id->itv;
  464. struct ivtv_stream *s = &itv->streams[id->type];
  465. int rc;
  466. if (atomic_read(&itv->decoding) == 0) {
  467. if (ivtv_claim_stream(id, s->type)) {
  468. /* someone else is using this stream already */
  469. IVTV_DEBUG_WARN("start decode, stream already claimed\n");
  470. return -EBUSY;
  471. }
  472. rc = ivtv_start_v4l2_decode_stream(s, 0);
  473. if (rc < 0) {
  474. if (rc == -EAGAIN)
  475. rc = ivtv_start_v4l2_decode_stream(s, 0);
  476. if (rc < 0)
  477. return rc;
  478. }
  479. }
  480. if (s->type == IVTV_DEC_STREAM_TYPE_MPG)
  481. return ivtv_set_speed(itv, speed);
  482. return 0;
  483. }
  484. ssize_t ivtv_v4l2_write(struct file *filp, const char __user *user_buf, size_t count, loff_t *pos)
  485. {
  486. struct ivtv_open_id *id = fh2id(filp->private_data);
  487. struct ivtv *itv = id->itv;
  488. struct ivtv_stream *s = &itv->streams[id->type];
  489. struct yuv_playback_info *yi = &itv->yuv_info;
  490. struct ivtv_buffer *buf;
  491. struct ivtv_queue q;
  492. int bytes_written = 0;
  493. int mode;
  494. int rc;
  495. DEFINE_WAIT(wait);
  496. IVTV_DEBUG_HI_FILE("write %zd bytes to %s\n", count, s->name);
  497. if (s->type != IVTV_DEC_STREAM_TYPE_MPG &&
  498. s->type != IVTV_DEC_STREAM_TYPE_YUV &&
  499. s->type != IVTV_DEC_STREAM_TYPE_VOUT)
  500. /* not decoder streams */
  501. return -EPERM;
  502. /* Try to claim this stream */
  503. if (ivtv_claim_stream(id, s->type))
  504. return -EBUSY;
  505. /* This stream does not need to start any decoding */
  506. if (s->type == IVTV_DEC_STREAM_TYPE_VOUT) {
  507. int elems = count / sizeof(struct v4l2_sliced_vbi_data);
  508. set_bit(IVTV_F_S_APPL_IO, &s->s_flags);
  509. return ivtv_write_vbi_from_user(itv,
  510. (const struct v4l2_sliced_vbi_data __user *)user_buf, elems);
  511. }
  512. mode = s->type == IVTV_DEC_STREAM_TYPE_MPG ? OUT_MPG : OUT_YUV;
  513. if (ivtv_set_output_mode(itv, mode) != mode) {
  514. ivtv_release_stream(s);
  515. return -EBUSY;
  516. }
  517. ivtv_queue_init(&q);
  518. set_bit(IVTV_F_S_APPL_IO, &s->s_flags);
  519. /* Start decoder (returns 0 if already started) */
  520. mutex_lock(&itv->serialize_lock);
  521. rc = ivtv_start_decoding(id, itv->speed);
  522. mutex_unlock(&itv->serialize_lock);
  523. if (rc) {
  524. IVTV_DEBUG_WARN("Failed start decode stream %s\n", s->name);
  525. /* failure, clean up */
  526. clear_bit(IVTV_F_S_STREAMING, &s->s_flags);
  527. clear_bit(IVTV_F_S_APPL_IO, &s->s_flags);
  528. return rc;
  529. }
  530. retry:
  531. /* If possible, just DMA the entire frame - Check the data transfer size
  532. since we may get here before the stream has been fully set-up */
  533. if (mode == OUT_YUV && s->q_full.length == 0 && itv->dma_data_req_size) {
  534. while (count >= itv->dma_data_req_size) {
  535. rc = ivtv_yuv_udma_stream_frame(itv, (void __user *)user_buf);
  536. if (rc < 0)
  537. return rc;
  538. bytes_written += itv->dma_data_req_size;
  539. user_buf += itv->dma_data_req_size;
  540. count -= itv->dma_data_req_size;
  541. }
  542. if (count == 0) {
  543. IVTV_DEBUG_HI_FILE("Wrote %d bytes to %s (%d)\n", bytes_written, s->name, s->q_full.bytesused);
  544. return bytes_written;
  545. }
  546. }
  547. for (;;) {
  548. /* Gather buffers */
  549. while (q.length - q.bytesused < count && (buf = ivtv_dequeue(s, &s->q_io)))
  550. ivtv_enqueue(s, buf, &q);
  551. while (q.length - q.bytesused < count && (buf = ivtv_dequeue(s, &s->q_free))) {
  552. ivtv_enqueue(s, buf, &q);
  553. }
  554. if (q.buffers)
  555. break;
  556. if (filp->f_flags & O_NONBLOCK)
  557. return -EAGAIN;
  558. prepare_to_wait(&s->waitq, &wait, TASK_INTERRUPTIBLE);
  559. /* New buffers might have become free before we were added to the waitqueue */
  560. if (!s->q_free.buffers)
  561. schedule();
  562. finish_wait(&s->waitq, &wait);
  563. if (signal_pending(current)) {
  564. IVTV_DEBUG_INFO("User stopped %s\n", s->name);
  565. return -EINTR;
  566. }
  567. }
  568. /* copy user data into buffers */
  569. while ((buf = ivtv_dequeue(s, &q))) {
  570. /* yuv is a pain. Don't copy more data than needed for a single
  571. frame, otherwise we lose sync with the incoming stream */
  572. if (s->type == IVTV_DEC_STREAM_TYPE_YUV &&
  573. yi->stream_size + count > itv->dma_data_req_size)
  574. rc = ivtv_buf_copy_from_user(s, buf, user_buf,
  575. itv->dma_data_req_size - yi->stream_size);
  576. else
  577. rc = ivtv_buf_copy_from_user(s, buf, user_buf, count);
  578. /* Make sure we really got all the user data */
  579. if (rc < 0) {
  580. ivtv_queue_move(s, &q, NULL, &s->q_free, 0);
  581. return rc;
  582. }
  583. user_buf += rc;
  584. count -= rc;
  585. bytes_written += rc;
  586. if (s->type == IVTV_DEC_STREAM_TYPE_YUV) {
  587. yi->stream_size += rc;
  588. /* If we have a complete yuv frame, break loop now */
  589. if (yi->stream_size == itv->dma_data_req_size) {
  590. ivtv_enqueue(s, buf, &s->q_full);
  591. yi->stream_size = 0;
  592. break;
  593. }
  594. }
  595. if (buf->bytesused != s->buf_size) {
  596. /* incomplete, leave in q_io for next time */
  597. ivtv_enqueue(s, buf, &s->q_io);
  598. break;
  599. }
  600. /* Byteswap MPEG buffer */
  601. if (s->type == IVTV_DEC_STREAM_TYPE_MPG)
  602. ivtv_buf_swap(buf);
  603. ivtv_enqueue(s, buf, &s->q_full);
  604. }
  605. if (test_bit(IVTV_F_S_NEEDS_DATA, &s->s_flags)) {
  606. if (s->q_full.length >= itv->dma_data_req_size) {
  607. int got_sig;
  608. if (mode == OUT_YUV)
  609. ivtv_yuv_setup_stream_frame(itv);
  610. prepare_to_wait(&itv->dma_waitq, &wait, TASK_INTERRUPTIBLE);
  611. while (!(got_sig = signal_pending(current)) &&
  612. test_bit(IVTV_F_S_DMA_PENDING, &s->s_flags)) {
  613. schedule();
  614. }
  615. finish_wait(&itv->dma_waitq, &wait);
  616. if (got_sig) {
  617. IVTV_DEBUG_INFO("User interrupted %s\n", s->name);
  618. return -EINTR;
  619. }
  620. clear_bit(IVTV_F_S_NEEDS_DATA, &s->s_flags);
  621. ivtv_queue_move(s, &s->q_full, NULL, &s->q_predma, itv->dma_data_req_size);
  622. ivtv_dma_stream_dec_prepare(s, itv->dma_data_req_offset + IVTV_DECODER_OFFSET, 1);
  623. }
  624. }
  625. /* more user data is available, wait until buffers become free
  626. to transfer the rest. */
  627. if (count && !(filp->f_flags & O_NONBLOCK))
  628. goto retry;
  629. IVTV_DEBUG_HI_FILE("Wrote %d bytes to %s (%d)\n", bytes_written, s->name, s->q_full.bytesused);
  630. return bytes_written;
  631. }
  632. unsigned int ivtv_v4l2_dec_poll(struct file *filp, poll_table *wait)
  633. {
  634. struct ivtv_open_id *id = fh2id(filp->private_data);
  635. struct ivtv *itv = id->itv;
  636. struct ivtv_stream *s = &itv->streams[id->type];
  637. int res = 0;
  638. /* add stream's waitq to the poll list */
  639. IVTV_DEBUG_HI_FILE("Decoder poll\n");
  640. /* If there are subscribed events, then only use the new event
  641. API instead of the old video.h based API. */
  642. if (!list_empty(&id->fh.events->subscribed)) {
  643. poll_wait(filp, &id->fh.events->wait, wait);
  644. /* Turn off the old-style vsync events */
  645. clear_bit(IVTV_F_I_EV_VSYNC_ENABLED, &itv->i_flags);
  646. if (v4l2_event_pending(&id->fh))
  647. res = POLLPRI;
  648. } else {
  649. /* This is the old-style API which is here only for backwards
  650. compatibility. */
  651. poll_wait(filp, &s->waitq, wait);
  652. set_bit(IVTV_F_I_EV_VSYNC_ENABLED, &itv->i_flags);
  653. if (test_bit(IVTV_F_I_EV_VSYNC, &itv->i_flags) ||
  654. test_bit(IVTV_F_I_EV_DEC_STOPPED, &itv->i_flags))
  655. res = POLLPRI;
  656. }
  657. /* Allow write if buffers are available for writing */
  658. if (s->q_free.buffers)
  659. res |= POLLOUT | POLLWRNORM;
  660. return res;
  661. }
  662. unsigned int ivtv_v4l2_enc_poll(struct file *filp, poll_table * wait)
  663. {
  664. struct ivtv_open_id *id = fh2id(filp->private_data);
  665. struct ivtv *itv = id->itv;
  666. struct ivtv_stream *s = &itv->streams[id->type];
  667. int eof = test_bit(IVTV_F_S_STREAMOFF, &s->s_flags);
  668. /* Start a capture if there is none */
  669. if (!eof && !test_bit(IVTV_F_S_STREAMING, &s->s_flags)) {
  670. int rc;
  671. mutex_lock(&itv->serialize_lock);
  672. rc = ivtv_start_capture(id);
  673. mutex_unlock(&itv->serialize_lock);
  674. if (rc) {
  675. IVTV_DEBUG_INFO("Could not start capture for %s (%d)\n",
  676. s->name, rc);
  677. return POLLERR;
  678. }
  679. IVTV_DEBUG_FILE("Encoder poll started capture\n");
  680. }
  681. /* add stream's waitq to the poll list */
  682. IVTV_DEBUG_HI_FILE("Encoder poll\n");
  683. poll_wait(filp, &s->waitq, wait);
  684. if (s->q_full.length || s->q_io.length)
  685. return POLLIN | POLLRDNORM;
  686. if (eof)
  687. return POLLHUP;
  688. return 0;
  689. }
  690. void ivtv_stop_capture(struct ivtv_open_id *id, int gop_end)
  691. {
  692. struct ivtv *itv = id->itv;
  693. struct ivtv_stream *s = &itv->streams[id->type];
  694. IVTV_DEBUG_FILE("close() of %s\n", s->name);
  695. /* 'Unclaim' this stream */
  696. /* Stop capturing */
  697. if (test_bit(IVTV_F_S_STREAMING, &s->s_flags)) {
  698. struct ivtv_stream *s_vbi = &itv->streams[IVTV_ENC_STREAM_TYPE_VBI];
  699. IVTV_DEBUG_INFO("close stopping capture\n");
  700. /* Special case: a running VBI capture for VBI insertion
  701. in the mpeg stream. Need to stop that too. */
  702. if (id->type == IVTV_ENC_STREAM_TYPE_MPG &&
  703. test_bit(IVTV_F_S_STREAMING, &s_vbi->s_flags) &&
  704. !test_bit(IVTV_F_S_APPL_IO, &s_vbi->s_flags)) {
  705. IVTV_DEBUG_INFO("close stopping embedded VBI capture\n");
  706. ivtv_stop_v4l2_encode_stream(s_vbi, 0);
  707. }
  708. if ((id->type == IVTV_DEC_STREAM_TYPE_VBI ||
  709. id->type == IVTV_ENC_STREAM_TYPE_VBI) &&
  710. test_bit(IVTV_F_S_INTERNAL_USE, &s->s_flags)) {
  711. /* Also used internally, don't stop capturing */
  712. s->id = -1;
  713. }
  714. else {
  715. ivtv_stop_v4l2_encode_stream(s, gop_end);
  716. }
  717. }
  718. if (!gop_end) {
  719. clear_bit(IVTV_F_S_APPL_IO, &s->s_flags);
  720. clear_bit(IVTV_F_S_STREAMOFF, &s->s_flags);
  721. ivtv_release_stream(s);
  722. }
  723. }
  724. static void ivtv_stop_decoding(struct ivtv_open_id *id, int flags, u64 pts)
  725. {
  726. struct ivtv *itv = id->itv;
  727. struct ivtv_stream *s = &itv->streams[id->type];
  728. IVTV_DEBUG_FILE("close() of %s\n", s->name);
  729. if (id->type == IVTV_DEC_STREAM_TYPE_YUV &&
  730. test_bit(IVTV_F_I_DECODING_YUV, &itv->i_flags)) {
  731. /* Restore registers we've changed & clean up any mess */
  732. ivtv_yuv_close(itv);
  733. }
  734. /* Stop decoding */
  735. if (test_bit(IVTV_F_S_STREAMING, &s->s_flags)) {
  736. IVTV_DEBUG_INFO("close stopping decode\n");
  737. ivtv_stop_v4l2_decode_stream(s, flags, pts);
  738. itv->output_mode = OUT_NONE;
  739. }
  740. clear_bit(IVTV_F_S_APPL_IO, &s->s_flags);
  741. clear_bit(IVTV_F_S_STREAMOFF, &s->s_flags);
  742. if (itv->output_mode == OUT_UDMA_YUV && id->yuv_frames)
  743. itv->output_mode = OUT_NONE;
  744. itv->speed = 0;
  745. clear_bit(IVTV_F_I_DEC_PAUSED, &itv->i_flags);
  746. ivtv_release_stream(s);
  747. }
  748. int ivtv_v4l2_close(struct file *filp)
  749. {
  750. struct v4l2_fh *fh = filp->private_data;
  751. struct ivtv_open_id *id = fh2id(fh);
  752. struct ivtv *itv = id->itv;
  753. struct ivtv_stream *s = &itv->streams[id->type];
  754. IVTV_DEBUG_FILE("close %s\n", s->name);
  755. v4l2_fh_del(fh);
  756. v4l2_fh_exit(fh);
  757. /* Easy case first: this stream was never claimed by us */
  758. if (s->id != id->open_id) {
  759. kfree(id);
  760. return 0;
  761. }
  762. /* 'Unclaim' this stream */
  763. /* Stop radio */
  764. mutex_lock(&itv->serialize_lock);
  765. if (id->type == IVTV_ENC_STREAM_TYPE_RAD) {
  766. /* Closing radio device, return to TV mode */
  767. ivtv_mute(itv);
  768. /* Mark that the radio is no longer in use */
  769. clear_bit(IVTV_F_I_RADIO_USER, &itv->i_flags);
  770. /* Switch tuner to TV */
  771. ivtv_call_all(itv, core, s_std, itv->std);
  772. /* Select correct audio input (i.e. TV tuner or Line in) */
  773. ivtv_audio_set_io(itv);
  774. if (itv->hw_flags & IVTV_HW_SAA711X) {
  775. ivtv_call_hw(itv, IVTV_HW_SAA711X, video, s_crystal_freq,
  776. SAA7115_FREQ_32_11_MHZ, 0);
  777. }
  778. if (atomic_read(&itv->capturing) > 0) {
  779. /* Undo video mute */
  780. ivtv_vapi(itv, CX2341X_ENC_MUTE_VIDEO, 1,
  781. v4l2_ctrl_g_ctrl(itv->cxhdl.video_mute) |
  782. (v4l2_ctrl_g_ctrl(itv->cxhdl.video_mute_yuv) << 8));
  783. }
  784. /* Done! Unmute and continue. */
  785. ivtv_unmute(itv);
  786. ivtv_release_stream(s);
  787. } else if (s->type >= IVTV_DEC_STREAM_TYPE_MPG) {
  788. struct ivtv_stream *s_vout = &itv->streams[IVTV_DEC_STREAM_TYPE_VOUT];
  789. ivtv_stop_decoding(id, VIDEO_CMD_STOP_TO_BLACK | VIDEO_CMD_STOP_IMMEDIATELY, 0);
  790. /* If all output streams are closed, and if the user doesn't have
  791. IVTV_DEC_STREAM_TYPE_VOUT open, then disable CC on TV-out. */
  792. if (itv->output_mode == OUT_NONE && !test_bit(IVTV_F_S_APPL_IO, &s_vout->s_flags)) {
  793. /* disable CC on TV-out */
  794. ivtv_disable_cc(itv);
  795. }
  796. } else {
  797. ivtv_stop_capture(id, 0);
  798. }
  799. kfree(id);
  800. mutex_unlock(&itv->serialize_lock);
  801. return 0;
  802. }
  803. static int ivtv_serialized_open(struct ivtv_stream *s, struct file *filp)
  804. {
  805. #ifdef CONFIG_VIDEO_ADV_DEBUG
  806. struct video_device *vdev = video_devdata(filp);
  807. #endif
  808. struct ivtv *itv = s->itv;
  809. struct ivtv_open_id *item;
  810. int res = 0;
  811. IVTV_DEBUG_FILE("open %s\n", s->name);
  812. #ifdef CONFIG_VIDEO_ADV_DEBUG
  813. /* Unless ivtv_fw_debug is set, error out if firmware dead. */
  814. if (ivtv_fw_debug) {
  815. IVTV_WARN("Opening %s with dead firmware lockout disabled\n",
  816. video_device_node_name(vdev));
  817. IVTV_WARN("Selected firmware errors will be ignored\n");
  818. } else {
  819. #else
  820. if (1) {
  821. #endif
  822. res = ivtv_firmware_check(itv, "ivtv_serialized_open");
  823. if (res == -EAGAIN)
  824. res = ivtv_firmware_check(itv, "ivtv_serialized_open");
  825. if (res < 0)
  826. return -EIO;
  827. }
  828. if (s->type == IVTV_DEC_STREAM_TYPE_MPG &&
  829. test_bit(IVTV_F_S_CLAIMED, &itv->streams[IVTV_DEC_STREAM_TYPE_YUV].s_flags))
  830. return -EBUSY;
  831. if (s->type == IVTV_DEC_STREAM_TYPE_YUV &&
  832. test_bit(IVTV_F_S_CLAIMED, &itv->streams[IVTV_DEC_STREAM_TYPE_MPG].s_flags))
  833. return -EBUSY;
  834. if (s->type == IVTV_DEC_STREAM_TYPE_YUV) {
  835. if (read_reg(0x82c) == 0) {
  836. IVTV_ERR("Tried to open YUV output device but need to send data to mpeg decoder before it can be used\n");
  837. /* return -ENODEV; */
  838. }
  839. ivtv_udma_alloc(itv);
  840. }
  841. /* Allocate memory */
  842. item = kzalloc(sizeof(struct ivtv_open_id), GFP_KERNEL);
  843. if (NULL == item) {
  844. IVTV_DEBUG_WARN("nomem on v4l2 open\n");
  845. return -ENOMEM;
  846. }
  847. v4l2_fh_init(&item->fh, s->vdev);
  848. if (s->type == IVTV_DEC_STREAM_TYPE_YUV ||
  849. s->type == IVTV_DEC_STREAM_TYPE_MPG) {
  850. res = v4l2_event_alloc(&item->fh, 60);
  851. }
  852. if (res < 0) {
  853. v4l2_fh_exit(&item->fh);
  854. kfree(item);
  855. return res;
  856. }
  857. item->itv = itv;
  858. item->type = s->type;
  859. item->open_id = itv->open_id++;
  860. filp->private_data = &item->fh;
  861. if (item->type == IVTV_ENC_STREAM_TYPE_RAD) {
  862. /* Try to claim this stream */
  863. if (ivtv_claim_stream(item, item->type)) {
  864. /* No, it's already in use */
  865. v4l2_fh_exit(&item->fh);
  866. kfree(item);
  867. return -EBUSY;
  868. }
  869. if (!test_bit(IVTV_F_I_RADIO_USER, &itv->i_flags)) {
  870. if (atomic_read(&itv->capturing) > 0) {
  871. /* switching to radio while capture is
  872. in progress is not polite */
  873. ivtv_release_stream(s);
  874. v4l2_fh_exit(&item->fh);
  875. kfree(item);
  876. return -EBUSY;
  877. }
  878. }
  879. /* Mark that the radio is being used. */
  880. set_bit(IVTV_F_I_RADIO_USER, &itv->i_flags);
  881. /* We have the radio */
  882. ivtv_mute(itv);
  883. /* Switch tuner to radio */
  884. ivtv_call_all(itv, tuner, s_radio);
  885. /* Select the correct audio input (i.e. radio tuner) */
  886. ivtv_audio_set_io(itv);
  887. if (itv->hw_flags & IVTV_HW_SAA711X) {
  888. ivtv_call_hw(itv, IVTV_HW_SAA711X, video, s_crystal_freq,
  889. SAA7115_FREQ_32_11_MHZ, SAA7115_FREQ_FL_APLL);
  890. }
  891. /* Done! Unmute and continue. */
  892. ivtv_unmute(itv);
  893. }
  894. /* YUV or MPG Decoding Mode? */
  895. if (s->type == IVTV_DEC_STREAM_TYPE_MPG) {
  896. clear_bit(IVTV_F_I_DEC_YUV, &itv->i_flags);
  897. } else if (s->type == IVTV_DEC_STREAM_TYPE_YUV) {
  898. set_bit(IVTV_F_I_DEC_YUV, &itv->i_flags);
  899. /* For yuv, we need to know the dma size before we start */
  900. itv->dma_data_req_size =
  901. 1080 * ((itv->yuv_info.v4l2_src_h + 31) & ~31);
  902. itv->yuv_info.stream_size = 0;
  903. }
  904. v4l2_fh_add(&item->fh);
  905. return 0;
  906. }
  907. int ivtv_v4l2_open(struct file *filp)
  908. {
  909. int res;
  910. struct ivtv *itv = NULL;
  911. struct ivtv_stream *s = NULL;
  912. struct video_device *vdev = video_devdata(filp);
  913. s = video_get_drvdata(vdev);
  914. itv = s->itv;
  915. mutex_lock(&itv->serialize_lock);
  916. if (ivtv_init_on_first_open(itv)) {
  917. IVTV_ERR("Failed to initialize on device %s\n",
  918. video_device_node_name(vdev));
  919. mutex_unlock(&itv->serialize_lock);
  920. return -ENXIO;
  921. }
  922. res = ivtv_serialized_open(s, filp);
  923. mutex_unlock(&itv->serialize_lock);
  924. return res;
  925. }
  926. void ivtv_mute(struct ivtv *itv)
  927. {
  928. if (atomic_read(&itv->capturing))
  929. ivtv_vapi(itv, CX2341X_ENC_MUTE_AUDIO, 1, 1);
  930. IVTV_DEBUG_INFO("Mute\n");
  931. }
  932. void ivtv_unmute(struct ivtv *itv)
  933. {
  934. if (atomic_read(&itv->capturing)) {
  935. ivtv_msleep_timeout(100, 0);
  936. ivtv_vapi(itv, CX2341X_ENC_MISC, 1, 12);
  937. ivtv_vapi(itv, CX2341X_ENC_MUTE_AUDIO, 1, 0);
  938. }
  939. IVTV_DEBUG_INFO("Unmute\n");
  940. }