ivtv-fileops.c 31 KB

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