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