ivtv-fileops.c 32 KB

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