ivtv-fileops.c 32 KB

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