ivtv-fileops.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013
  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 <media/saa7115.h>
  32. /* This function tries to claim the stream for a specific file descriptor.
  33. If no one else is using this stream then the stream is claimed and
  34. associated VBI streams are also automatically claimed.
  35. Possible error returns: -EBUSY if someone else has claimed
  36. the stream or 0 on success. */
  37. static int ivtv_claim_stream(struct ivtv_open_id *id, int type)
  38. {
  39. struct ivtv *itv = id->itv;
  40. struct ivtv_stream *s = &itv->streams[type];
  41. struct ivtv_stream *s_vbi;
  42. int vbi_type;
  43. if (test_and_set_bit(IVTV_F_S_CLAIMED, &s->s_flags)) {
  44. /* someone already claimed this stream */
  45. if (s->id == id->open_id) {
  46. /* yes, this file descriptor did. So that's OK. */
  47. return 0;
  48. }
  49. if (s->id == -1 && (type == IVTV_DEC_STREAM_TYPE_VBI ||
  50. type == IVTV_ENC_STREAM_TYPE_VBI)) {
  51. /* VBI is handled already internally, now also assign
  52. the file descriptor to this stream for external
  53. reading of the stream. */
  54. s->id = id->open_id;
  55. IVTV_DEBUG_INFO("Start Read VBI\n");
  56. return 0;
  57. }
  58. /* someone else is using this stream already */
  59. IVTV_DEBUG_INFO("Stream %d is busy\n", type);
  60. return -EBUSY;
  61. }
  62. s->id = id->open_id;
  63. if (type == IVTV_DEC_STREAM_TYPE_VBI) {
  64. /* Enable reinsertion interrupt */
  65. ivtv_clear_irq_mask(itv, IVTV_IRQ_DEC_VBI_RE_INSERT);
  66. }
  67. /* IVTV_DEC_STREAM_TYPE_MPG needs to claim IVTV_DEC_STREAM_TYPE_VBI,
  68. IVTV_ENC_STREAM_TYPE_MPG needs to claim IVTV_ENC_STREAM_TYPE_VBI
  69. (provided VBI insertion is on and sliced VBI is selected), for all
  70. other streams we're done */
  71. if (type == IVTV_DEC_STREAM_TYPE_MPG) {
  72. vbi_type = IVTV_DEC_STREAM_TYPE_VBI;
  73. } else if (type == IVTV_ENC_STREAM_TYPE_MPG &&
  74. itv->vbi.insert_mpeg && !ivtv_raw_vbi(itv)) {
  75. vbi_type = IVTV_ENC_STREAM_TYPE_VBI;
  76. } else {
  77. return 0;
  78. }
  79. s_vbi = &itv->streams[vbi_type];
  80. if (!test_and_set_bit(IVTV_F_S_CLAIMED, &s_vbi->s_flags)) {
  81. /* Enable reinsertion interrupt */
  82. if (vbi_type == IVTV_DEC_STREAM_TYPE_VBI)
  83. ivtv_clear_irq_mask(itv, IVTV_IRQ_DEC_VBI_RE_INSERT);
  84. }
  85. /* mark that it is used internally */
  86. set_bit(IVTV_F_S_INTERNAL_USE, &s_vbi->s_flags);
  87. return 0;
  88. }
  89. /* This function releases a previously claimed stream. It will take into
  90. account associated VBI streams. */
  91. void ivtv_release_stream(struct ivtv_stream *s)
  92. {
  93. struct ivtv *itv = s->itv;
  94. struct ivtv_stream *s_vbi;
  95. s->id = -1;
  96. if ((s->type == IVTV_DEC_STREAM_TYPE_VBI || s->type == IVTV_ENC_STREAM_TYPE_VBI) &&
  97. test_bit(IVTV_F_S_INTERNAL_USE, &s->s_flags)) {
  98. /* this stream is still in use internally */
  99. return;
  100. }
  101. if (!test_and_clear_bit(IVTV_F_S_CLAIMED, &s->s_flags)) {
  102. IVTV_DEBUG_WARN("Release stream %s not in use!\n", s->name);
  103. return;
  104. }
  105. ivtv_flush_queues(s);
  106. /* disable reinsertion interrupt */
  107. if (s->type == IVTV_DEC_STREAM_TYPE_VBI)
  108. ivtv_set_irq_mask(itv, IVTV_IRQ_DEC_VBI_RE_INSERT);
  109. /* IVTV_DEC_STREAM_TYPE_MPG needs to release IVTV_DEC_STREAM_TYPE_VBI,
  110. IVTV_ENC_STREAM_TYPE_MPG needs to release IVTV_ENC_STREAM_TYPE_VBI,
  111. for all other streams we're done */
  112. if (s->type == IVTV_DEC_STREAM_TYPE_MPG)
  113. s_vbi = &itv->streams[IVTV_DEC_STREAM_TYPE_VBI];
  114. else if (s->type == IVTV_ENC_STREAM_TYPE_MPG)
  115. s_vbi = &itv->streams[IVTV_ENC_STREAM_TYPE_VBI];
  116. else
  117. return;
  118. /* clear internal use flag */
  119. if (!test_and_clear_bit(IVTV_F_S_INTERNAL_USE, &s_vbi->s_flags)) {
  120. /* was already cleared */
  121. return;
  122. }
  123. if (s_vbi->id != -1) {
  124. /* VBI stream still claimed by a file descriptor */
  125. return;
  126. }
  127. /* disable reinsertion interrupt */
  128. if (s_vbi->type == IVTV_DEC_STREAM_TYPE_VBI)
  129. ivtv_set_irq_mask(itv, IVTV_IRQ_DEC_VBI_RE_INSERT);
  130. clear_bit(IVTV_F_S_CLAIMED, &s_vbi->s_flags);
  131. ivtv_flush_queues(s_vbi);
  132. }
  133. static void ivtv_dualwatch(struct ivtv *itv)
  134. {
  135. struct v4l2_tuner vt;
  136. u32 new_bitmap;
  137. u32 new_stereo_mode;
  138. const u32 stereo_mask = 0x0300;
  139. const u32 dual = 0x0200;
  140. new_stereo_mode = itv->params.audio_properties & stereo_mask;
  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. new_bitmap = new_stereo_mode | (itv->params.audio_properties & ~stereo_mask);
  148. IVTV_DEBUG_INFO("dualwatch: change stereo flag from 0x%x to 0x%x. new audio_bitmask=0x%ux\n",
  149. itv->dualwatch_stereo_mode, new_stereo_mode, new_bitmap);
  150. if (ivtv_vapi(itv, CX2341X_ENC_SET_AUDIO_PROPERTIES, 1, new_bitmap) == 0) {
  151. itv->dualwatch_stereo_mode = new_stereo_mode;
  152. return;
  153. }
  154. IVTV_DEBUG_INFO("dualwatch: changing stereo flag failed\n");
  155. }
  156. static void ivtv_update_pgm_info(struct ivtv *itv)
  157. {
  158. u32 wr_idx = (read_enc(itv->pgm_info_offset) - itv->pgm_info_offset - 4) / 24;
  159. int cnt;
  160. int i = 0;
  161. if (wr_idx >= itv->pgm_info_num) {
  162. IVTV_DEBUG_WARN("Invalid PGM index %d (>= %d)\n", wr_idx, itv->pgm_info_num);
  163. return;
  164. }
  165. cnt = (wr_idx + itv->pgm_info_num - itv->pgm_info_write_idx) % itv->pgm_info_num;
  166. while (i < cnt) {
  167. int idx = (itv->pgm_info_write_idx + i) % itv->pgm_info_num;
  168. struct v4l2_enc_idx_entry *e = itv->pgm_info + idx;
  169. u32 addr = itv->pgm_info_offset + 4 + idx * 24;
  170. const int mapping[8] = { -1, V4L2_ENC_IDX_FRAME_I, V4L2_ENC_IDX_FRAME_P, -1,
  171. V4L2_ENC_IDX_FRAME_B, -1, -1, -1 };
  172. // 1=I, 2=P, 4=B
  173. e->offset = read_enc(addr + 4) + ((u64)read_enc(addr + 8) << 32);
  174. if (e->offset > itv->mpg_data_received) {
  175. break;
  176. }
  177. e->offset += itv->vbi_data_inserted;
  178. e->length = read_enc(addr);
  179. e->pts = read_enc(addr + 16) + ((u64)(read_enc(addr + 20) & 1) << 32);
  180. e->flags = mapping[read_enc(addr + 12) & 7];
  181. i++;
  182. }
  183. itv->pgm_info_write_idx = (itv->pgm_info_write_idx + i) % itv->pgm_info_num;
  184. }
  185. static struct ivtv_buffer *ivtv_get_buffer(struct ivtv_stream *s, int non_block, int *err)
  186. {
  187. struct ivtv *itv = s->itv;
  188. struct ivtv_stream *s_vbi = &itv->streams[IVTV_ENC_STREAM_TYPE_VBI];
  189. struct ivtv_buffer *buf;
  190. DEFINE_WAIT(wait);
  191. *err = 0;
  192. while (1) {
  193. if (s->type == IVTV_ENC_STREAM_TYPE_MPG) {
  194. /* Process pending program info updates and pending VBI data */
  195. ivtv_update_pgm_info(itv);
  196. if (time_after(jiffies,
  197. itv->dualwatch_jiffies +
  198. msecs_to_jiffies(1000))) {
  199. itv->dualwatch_jiffies = jiffies;
  200. ivtv_dualwatch(itv);
  201. }
  202. if (test_bit(IVTV_F_S_INTERNAL_USE, &s_vbi->s_flags) &&
  203. !test_bit(IVTV_F_S_APPL_IO, &s_vbi->s_flags)) {
  204. while ((buf = ivtv_dequeue(s_vbi, &s_vbi->q_full))) {
  205. /* byteswap and process VBI data */
  206. ivtv_process_vbi_data(itv, buf, s_vbi->dma_pts, s_vbi->type);
  207. ivtv_enqueue(s_vbi, buf, &s_vbi->q_free);
  208. }
  209. }
  210. buf = &itv->vbi.sliced_mpeg_buf;
  211. if (buf->readpos != buf->bytesused) {
  212. return buf;
  213. }
  214. }
  215. /* do we have leftover data? */
  216. buf = ivtv_dequeue(s, &s->q_io);
  217. if (buf)
  218. return buf;
  219. /* do we have new data? */
  220. buf = ivtv_dequeue(s, &s->q_full);
  221. if (buf) {
  222. if ((buf->b_flags & IVTV_F_B_NEED_BUF_SWAP) == 0)
  223. return buf;
  224. buf->b_flags &= ~IVTV_F_B_NEED_BUF_SWAP;
  225. if (s->type == IVTV_ENC_STREAM_TYPE_MPG)
  226. /* byteswap MPG data */
  227. ivtv_buf_swap(buf);
  228. else if (s->type != IVTV_DEC_STREAM_TYPE_VBI) {
  229. /* byteswap and process VBI data */
  230. ivtv_process_vbi_data(itv, buf, s->dma_pts, s->type);
  231. }
  232. return buf;
  233. }
  234. /* return if end of stream */
  235. if (s->type != IVTV_DEC_STREAM_TYPE_VBI && !test_bit(IVTV_F_S_STREAMING, &s->s_flags)) {
  236. IVTV_DEBUG_INFO("EOS %s\n", s->name);
  237. return NULL;
  238. }
  239. /* return if file was opened with O_NONBLOCK */
  240. if (non_block) {
  241. *err = -EAGAIN;
  242. return NULL;
  243. }
  244. /* wait for more data to arrive */
  245. prepare_to_wait(&s->waitq, &wait, TASK_INTERRUPTIBLE);
  246. /* New buffers might have become available before we were added to the waitqueue */
  247. if (!s->q_full.buffers)
  248. schedule();
  249. finish_wait(&s->waitq, &wait);
  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->id == -1) {
  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 -EPERM;
  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 = filp->private_data;
  454. struct ivtv *itv = id->itv;
  455. struct ivtv_stream *s = &itv->streams[id->type];
  456. int rc;
  457. IVTV_DEBUG_HI_FILE("read %zd bytes from %s\n", count, s->name);
  458. mutex_lock(&itv->serialize_lock);
  459. rc = ivtv_start_capture(id);
  460. mutex_unlock(&itv->serialize_lock);
  461. if (rc)
  462. return rc;
  463. return ivtv_read_pos(s, buf, count, pos, filp->f_flags & O_NONBLOCK);
  464. }
  465. int ivtv_start_decoding(struct ivtv_open_id *id, int speed)
  466. {
  467. struct ivtv *itv = id->itv;
  468. struct ivtv_stream *s = &itv->streams[id->type];
  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. ivtv_start_v4l2_decode_stream(s, 0);
  476. }
  477. if (s->type == IVTV_DEC_STREAM_TYPE_MPG)
  478. return ivtv_set_speed(itv, speed);
  479. return 0;
  480. }
  481. ssize_t ivtv_v4l2_write(struct file *filp, const char __user *user_buf, size_t count, loff_t *pos)
  482. {
  483. struct ivtv_open_id *id = filp->private_data;
  484. struct ivtv *itv = id->itv;
  485. struct ivtv_stream *s = &itv->streams[id->type];
  486. struct yuv_playback_info *yi = &itv->yuv_info;
  487. struct ivtv_buffer *buf;
  488. struct ivtv_queue q;
  489. int bytes_written = 0;
  490. int mode;
  491. int rc;
  492. DEFINE_WAIT(wait);
  493. IVTV_DEBUG_HI_FILE("write %zd bytes to %s\n", count, s->name);
  494. if (s->type != IVTV_DEC_STREAM_TYPE_MPG &&
  495. s->type != IVTV_DEC_STREAM_TYPE_YUV &&
  496. s->type != IVTV_DEC_STREAM_TYPE_VOUT)
  497. /* not decoder streams */
  498. return -EPERM;
  499. /* Try to claim this stream */
  500. if (ivtv_claim_stream(id, s->type))
  501. return -EBUSY;
  502. /* This stream does not need to start any decoding */
  503. if (s->type == IVTV_DEC_STREAM_TYPE_VOUT) {
  504. int elems = count / sizeof(struct v4l2_sliced_vbi_data);
  505. set_bit(IVTV_F_S_APPL_IO, &s->s_flags);
  506. ivtv_write_vbi(itv, (const struct v4l2_sliced_vbi_data *)user_buf, elems);
  507. return elems * sizeof(struct v4l2_sliced_vbi_data);
  508. }
  509. mode = s->type == IVTV_DEC_STREAM_TYPE_MPG ? OUT_MPG : OUT_YUV;
  510. if (ivtv_set_output_mode(itv, mode) != mode) {
  511. ivtv_release_stream(s);
  512. return -EBUSY;
  513. }
  514. ivtv_queue_init(&q);
  515. set_bit(IVTV_F_S_APPL_IO, &s->s_flags);
  516. /* Start decoder (returns 0 if already started) */
  517. mutex_lock(&itv->serialize_lock);
  518. rc = ivtv_start_decoding(id, itv->speed);
  519. mutex_unlock(&itv->serialize_lock);
  520. if (rc) {
  521. IVTV_DEBUG_WARN("Failed start decode stream %s\n", s->name);
  522. /* failure, clean up */
  523. clear_bit(IVTV_F_S_STREAMING, &s->s_flags);
  524. clear_bit(IVTV_F_S_APPL_IO, &s->s_flags);
  525. return rc;
  526. }
  527. retry:
  528. /* If possible, just DMA the entire frame - Check the data transfer size
  529. since we may get here before the stream has been fully set-up */
  530. if (mode == OUT_YUV && s->q_full.length == 0 && itv->dma_data_req_size) {
  531. while (count >= itv->dma_data_req_size) {
  532. rc = ivtv_yuv_udma_stream_frame(itv, (void __user *)user_buf);
  533. if (rc < 0)
  534. return rc;
  535. bytes_written += itv->dma_data_req_size;
  536. user_buf += itv->dma_data_req_size;
  537. count -= itv->dma_data_req_size;
  538. }
  539. if (count == 0) {
  540. IVTV_DEBUG_HI_FILE("Wrote %d bytes to %s (%d)\n", bytes_written, s->name, s->q_full.bytesused);
  541. return bytes_written;
  542. }
  543. }
  544. for (;;) {
  545. /* Gather buffers */
  546. while (q.length - q.bytesused < count && (buf = ivtv_dequeue(s, &s->q_io)))
  547. ivtv_enqueue(s, buf, &q);
  548. while (q.length - q.bytesused < count && (buf = ivtv_dequeue(s, &s->q_free))) {
  549. ivtv_enqueue(s, buf, &q);
  550. }
  551. if (q.buffers)
  552. break;
  553. if (filp->f_flags & O_NONBLOCK)
  554. return -EAGAIN;
  555. prepare_to_wait(&s->waitq, &wait, TASK_INTERRUPTIBLE);
  556. /* New buffers might have become free before we were added to the waitqueue */
  557. if (!s->q_free.buffers)
  558. schedule();
  559. finish_wait(&s->waitq, &wait);
  560. if (signal_pending(current)) {
  561. IVTV_DEBUG_INFO("User stopped %s\n", s->name);
  562. return -EINTR;
  563. }
  564. }
  565. /* copy user data into buffers */
  566. while ((buf = ivtv_dequeue(s, &q))) {
  567. /* yuv is a pain. Don't copy more data than needed for a single
  568. frame, otherwise we lose sync with the incoming stream */
  569. if (s->type == IVTV_DEC_STREAM_TYPE_YUV &&
  570. yi->stream_size + count > itv->dma_data_req_size)
  571. rc = ivtv_buf_copy_from_user(s, buf, user_buf,
  572. itv->dma_data_req_size - yi->stream_size);
  573. else
  574. rc = ivtv_buf_copy_from_user(s, buf, user_buf, count);
  575. /* Make sure we really got all the user data */
  576. if (rc < 0) {
  577. ivtv_queue_move(s, &q, NULL, &s->q_free, 0);
  578. return rc;
  579. }
  580. user_buf += rc;
  581. count -= rc;
  582. bytes_written += rc;
  583. if (s->type == IVTV_DEC_STREAM_TYPE_YUV) {
  584. yi->stream_size += rc;
  585. /* If we have a complete yuv frame, break loop now */
  586. if (yi->stream_size == itv->dma_data_req_size) {
  587. ivtv_enqueue(s, buf, &s->q_full);
  588. yi->stream_size = 0;
  589. break;
  590. }
  591. }
  592. if (buf->bytesused != s->buf_size) {
  593. /* incomplete, leave in q_io for next time */
  594. ivtv_enqueue(s, buf, &s->q_io);
  595. break;
  596. }
  597. /* Byteswap MPEG buffer */
  598. if (s->type == IVTV_DEC_STREAM_TYPE_MPG)
  599. ivtv_buf_swap(buf);
  600. ivtv_enqueue(s, buf, &s->q_full);
  601. }
  602. if (test_bit(IVTV_F_S_NEEDS_DATA, &s->s_flags)) {
  603. if (s->q_full.length >= itv->dma_data_req_size) {
  604. int got_sig;
  605. if (mode == OUT_YUV)
  606. ivtv_yuv_setup_stream_frame(itv);
  607. prepare_to_wait(&itv->dma_waitq, &wait, TASK_INTERRUPTIBLE);
  608. while (!(got_sig = signal_pending(current)) &&
  609. test_bit(IVTV_F_S_DMA_PENDING, &s->s_flags)) {
  610. schedule();
  611. }
  612. finish_wait(&itv->dma_waitq, &wait);
  613. if (got_sig) {
  614. IVTV_DEBUG_INFO("User interrupted %s\n", s->name);
  615. return -EINTR;
  616. }
  617. clear_bit(IVTV_F_S_NEEDS_DATA, &s->s_flags);
  618. ivtv_queue_move(s, &s->q_full, NULL, &s->q_predma, itv->dma_data_req_size);
  619. ivtv_dma_stream_dec_prepare(s, itv->dma_data_req_offset + IVTV_DECODER_OFFSET, 1);
  620. }
  621. }
  622. /* more user data is available, wait until buffers become free
  623. to transfer the rest. */
  624. if (count && !(filp->f_flags & O_NONBLOCK))
  625. goto retry;
  626. IVTV_DEBUG_HI_FILE("Wrote %d bytes to %s (%d)\n", bytes_written, s->name, s->q_full.bytesused);
  627. return bytes_written;
  628. }
  629. unsigned int ivtv_v4l2_dec_poll(struct file *filp, poll_table *wait)
  630. {
  631. struct ivtv_open_id *id = filp->private_data;
  632. struct ivtv *itv = id->itv;
  633. struct ivtv_stream *s = &itv->streams[id->type];
  634. int res = 0;
  635. /* add stream's waitq to the poll list */
  636. IVTV_DEBUG_HI_FILE("Decoder poll\n");
  637. poll_wait(filp, &s->waitq, wait);
  638. set_bit(IVTV_F_I_EV_VSYNC_ENABLED, &itv->i_flags);
  639. if (test_bit(IVTV_F_I_EV_VSYNC, &itv->i_flags) ||
  640. test_bit(IVTV_F_I_EV_DEC_STOPPED, &itv->i_flags))
  641. res = POLLPRI;
  642. /* Allow write if buffers are available for writing */
  643. if (s->q_free.buffers)
  644. res |= POLLOUT | POLLWRNORM;
  645. return res;
  646. }
  647. unsigned int ivtv_v4l2_enc_poll(struct file *filp, poll_table * wait)
  648. {
  649. struct ivtv_open_id *id = filp->private_data;
  650. struct ivtv *itv = id->itv;
  651. struct ivtv_stream *s = &itv->streams[id->type];
  652. int eof = test_bit(IVTV_F_S_STREAMOFF, &s->s_flags);
  653. /* Start a capture if there is none */
  654. if (!eof && !test_bit(IVTV_F_S_STREAMING, &s->s_flags)) {
  655. int rc;
  656. mutex_lock(&itv->serialize_lock);
  657. rc = ivtv_start_capture(id);
  658. mutex_unlock(&itv->serialize_lock);
  659. if (rc) {
  660. IVTV_DEBUG_INFO("Could not start capture for %s (%d)\n",
  661. s->name, rc);
  662. return POLLERR;
  663. }
  664. IVTV_DEBUG_FILE("Encoder poll started capture\n");
  665. }
  666. /* add stream's waitq to the poll list */
  667. IVTV_DEBUG_HI_FILE("Encoder poll\n");
  668. poll_wait(filp, &s->waitq, wait);
  669. if (s->q_full.length || s->q_io.length)
  670. return POLLIN | POLLRDNORM;
  671. if (eof)
  672. return POLLHUP;
  673. return 0;
  674. }
  675. void ivtv_stop_capture(struct ivtv_open_id *id, int gop_end)
  676. {
  677. struct ivtv *itv = id->itv;
  678. struct ivtv_stream *s = &itv->streams[id->type];
  679. IVTV_DEBUG_FILE("close() of %s\n", s->name);
  680. /* 'Unclaim' this stream */
  681. /* Stop capturing */
  682. if (test_bit(IVTV_F_S_STREAMING, &s->s_flags)) {
  683. struct ivtv_stream *s_vbi = &itv->streams[IVTV_ENC_STREAM_TYPE_VBI];
  684. IVTV_DEBUG_INFO("close stopping capture\n");
  685. /* Special case: a running VBI capture for VBI insertion
  686. in the mpeg stream. Need to stop that too. */
  687. if (id->type == IVTV_ENC_STREAM_TYPE_MPG &&
  688. test_bit(IVTV_F_S_STREAMING, &s_vbi->s_flags) &&
  689. !test_bit(IVTV_F_S_APPL_IO, &s_vbi->s_flags)) {
  690. IVTV_DEBUG_INFO("close stopping embedded VBI capture\n");
  691. ivtv_stop_v4l2_encode_stream(s_vbi, 0);
  692. }
  693. if ((id->type == IVTV_DEC_STREAM_TYPE_VBI ||
  694. id->type == IVTV_ENC_STREAM_TYPE_VBI) &&
  695. test_bit(IVTV_F_S_INTERNAL_USE, &s->s_flags)) {
  696. /* Also used internally, don't stop capturing */
  697. s->id = -1;
  698. }
  699. else {
  700. ivtv_stop_v4l2_encode_stream(s, gop_end);
  701. }
  702. }
  703. if (!gop_end) {
  704. clear_bit(IVTV_F_S_APPL_IO, &s->s_flags);
  705. clear_bit(IVTV_F_S_STREAMOFF, &s->s_flags);
  706. ivtv_release_stream(s);
  707. }
  708. }
  709. static void ivtv_stop_decoding(struct ivtv_open_id *id, int flags, u64 pts)
  710. {
  711. struct ivtv *itv = id->itv;
  712. struct ivtv_stream *s = &itv->streams[id->type];
  713. IVTV_DEBUG_FILE("close() of %s\n", s->name);
  714. /* Stop decoding */
  715. if (test_bit(IVTV_F_S_STREAMING, &s->s_flags)) {
  716. IVTV_DEBUG_INFO("close stopping decode\n");
  717. ivtv_stop_v4l2_decode_stream(s, flags, pts);
  718. itv->output_mode = OUT_NONE;
  719. }
  720. clear_bit(IVTV_F_S_APPL_IO, &s->s_flags);
  721. clear_bit(IVTV_F_S_STREAMOFF, &s->s_flags);
  722. if (id->type == IVTV_DEC_STREAM_TYPE_YUV && test_bit(IVTV_F_I_DECODING_YUV, &itv->i_flags)) {
  723. /* Restore registers we've changed & clean up any mess we've made */
  724. ivtv_yuv_close(itv);
  725. }
  726. if (itv->output_mode == OUT_UDMA_YUV && id->yuv_frames)
  727. itv->output_mode = OUT_NONE;
  728. itv->speed = 0;
  729. clear_bit(IVTV_F_I_DEC_PAUSED, &itv->i_flags);
  730. ivtv_release_stream(s);
  731. }
  732. int ivtv_v4l2_close(struct file *filp)
  733. {
  734. struct ivtv_open_id *id = filp->private_data;
  735. struct ivtv *itv = id->itv;
  736. struct ivtv_stream *s = &itv->streams[id->type];
  737. IVTV_DEBUG_FILE("close %s\n", s->name);
  738. v4l2_prio_close(&itv->prio, &id->prio);
  739. /* Easy case first: this stream was never claimed by us */
  740. if (s->id != id->open_id) {
  741. kfree(id);
  742. return 0;
  743. }
  744. /* 'Unclaim' this stream */
  745. /* Stop radio */
  746. mutex_lock(&itv->serialize_lock);
  747. if (id->type == IVTV_ENC_STREAM_TYPE_RAD) {
  748. /* Closing radio device, return to TV mode */
  749. ivtv_mute(itv);
  750. /* Mark that the radio is no longer in use */
  751. clear_bit(IVTV_F_I_RADIO_USER, &itv->i_flags);
  752. /* Switch tuner to TV */
  753. ivtv_call_all(itv, core, s_std, itv->std);
  754. /* Select correct audio input (i.e. TV tuner or Line in) */
  755. ivtv_audio_set_io(itv);
  756. if (itv->hw_flags & IVTV_HW_SAA711X) {
  757. ivtv_call_hw(itv, IVTV_HW_SAA711X, video, s_crystal_freq,
  758. SAA7115_FREQ_32_11_MHZ, 0);
  759. }
  760. if (atomic_read(&itv->capturing) > 0) {
  761. /* Undo video mute */
  762. ivtv_vapi(itv, CX2341X_ENC_MUTE_VIDEO, 1,
  763. itv->params.video_mute | (itv->params.video_mute_yuv << 8));
  764. }
  765. /* Done! Unmute and continue. */
  766. ivtv_unmute(itv);
  767. ivtv_release_stream(s);
  768. } else if (s->type >= IVTV_DEC_STREAM_TYPE_MPG) {
  769. struct ivtv_stream *s_vout = &itv->streams[IVTV_DEC_STREAM_TYPE_VOUT];
  770. ivtv_stop_decoding(id, VIDEO_CMD_STOP_TO_BLACK | VIDEO_CMD_STOP_IMMEDIATELY, 0);
  771. /* If all output streams are closed, and if the user doesn't have
  772. IVTV_DEC_STREAM_TYPE_VOUT open, then disable CC on TV-out. */
  773. if (itv->output_mode == OUT_NONE && !test_bit(IVTV_F_S_APPL_IO, &s_vout->s_flags)) {
  774. /* disable CC on TV-out */
  775. ivtv_disable_cc(itv);
  776. }
  777. } else {
  778. ivtv_stop_capture(id, 0);
  779. }
  780. kfree(id);
  781. mutex_unlock(&itv->serialize_lock);
  782. return 0;
  783. }
  784. static int ivtv_serialized_open(struct ivtv_stream *s, struct file *filp)
  785. {
  786. struct ivtv *itv = s->itv;
  787. struct ivtv_open_id *item;
  788. IVTV_DEBUG_FILE("open %s\n", s->name);
  789. if (s->type == IVTV_DEC_STREAM_TYPE_MPG &&
  790. test_bit(IVTV_F_S_CLAIMED, &itv->streams[IVTV_DEC_STREAM_TYPE_YUV].s_flags))
  791. return -EBUSY;
  792. if (s->type == IVTV_DEC_STREAM_TYPE_YUV &&
  793. test_bit(IVTV_F_S_CLAIMED, &itv->streams[IVTV_DEC_STREAM_TYPE_MPG].s_flags))
  794. return -EBUSY;
  795. if (s->type == IVTV_DEC_STREAM_TYPE_YUV) {
  796. if (read_reg(0x82c) == 0) {
  797. IVTV_ERR("Tried to open YUV output device but need to send data to mpeg decoder before it can be used\n");
  798. /* return -ENODEV; */
  799. }
  800. ivtv_udma_alloc(itv);
  801. }
  802. /* Allocate memory */
  803. item = kmalloc(sizeof(struct ivtv_open_id), GFP_KERNEL);
  804. if (NULL == item) {
  805. IVTV_DEBUG_WARN("nomem on v4l2 open\n");
  806. return -ENOMEM;
  807. }
  808. item->itv = itv;
  809. item->type = s->type;
  810. v4l2_prio_open(&itv->prio, &item->prio);
  811. item->open_id = itv->open_id++;
  812. filp->private_data = item;
  813. if (item->type == IVTV_ENC_STREAM_TYPE_RAD) {
  814. /* Try to claim this stream */
  815. if (ivtv_claim_stream(item, item->type)) {
  816. /* No, it's already in use */
  817. kfree(item);
  818. return -EBUSY;
  819. }
  820. if (!test_bit(IVTV_F_I_RADIO_USER, &itv->i_flags)) {
  821. if (atomic_read(&itv->capturing) > 0) {
  822. /* switching to radio while capture is
  823. in progress is not polite */
  824. ivtv_release_stream(s);
  825. kfree(item);
  826. return -EBUSY;
  827. }
  828. }
  829. /* Mark that the radio is being used. */
  830. set_bit(IVTV_F_I_RADIO_USER, &itv->i_flags);
  831. /* We have the radio */
  832. ivtv_mute(itv);
  833. /* Switch tuner to radio */
  834. ivtv_call_all(itv, tuner, s_radio);
  835. /* Select the correct audio input (i.e. radio tuner) */
  836. ivtv_audio_set_io(itv);
  837. if (itv->hw_flags & IVTV_HW_SAA711X) {
  838. ivtv_call_hw(itv, IVTV_HW_SAA711X, video, s_crystal_freq,
  839. SAA7115_FREQ_32_11_MHZ, SAA7115_FREQ_FL_APLL);
  840. }
  841. /* Done! Unmute and continue. */
  842. ivtv_unmute(itv);
  843. }
  844. /* YUV or MPG Decoding Mode? */
  845. if (s->type == IVTV_DEC_STREAM_TYPE_MPG) {
  846. clear_bit(IVTV_F_I_DEC_YUV, &itv->i_flags);
  847. } else if (s->type == IVTV_DEC_STREAM_TYPE_YUV) {
  848. set_bit(IVTV_F_I_DEC_YUV, &itv->i_flags);
  849. /* For yuv, we need to know the dma size before we start */
  850. itv->dma_data_req_size =
  851. 1080 * ((itv->yuv_info.v4l2_src_h + 31) & ~31);
  852. itv->yuv_info.stream_size = 0;
  853. }
  854. return 0;
  855. }
  856. int ivtv_v4l2_open(struct file *filp)
  857. {
  858. int res;
  859. struct ivtv *itv = NULL;
  860. struct ivtv_stream *s = NULL;
  861. struct video_device *vdev = video_devdata(filp);
  862. s = video_get_drvdata(vdev);
  863. itv = s->itv;
  864. mutex_lock(&itv->serialize_lock);
  865. if (ivtv_init_on_first_open(itv)) {
  866. IVTV_ERR("Failed to initialize on minor %d\n",
  867. vdev->minor);
  868. mutex_unlock(&itv->serialize_lock);
  869. return -ENXIO;
  870. }
  871. res = ivtv_serialized_open(s, filp);
  872. mutex_unlock(&itv->serialize_lock);
  873. return res;
  874. }
  875. void ivtv_mute(struct ivtv *itv)
  876. {
  877. if (atomic_read(&itv->capturing))
  878. ivtv_vapi(itv, CX2341X_ENC_MUTE_AUDIO, 1, 1);
  879. IVTV_DEBUG_INFO("Mute\n");
  880. }
  881. void ivtv_unmute(struct ivtv *itv)
  882. {
  883. if (atomic_read(&itv->capturing)) {
  884. ivtv_msleep_timeout(100, 0);
  885. ivtv_vapi(itv, CX2341X_ENC_MISC, 1, 12);
  886. ivtv_vapi(itv, CX2341X_ENC_MUTE_AUDIO, 1, 0);
  887. }
  888. IVTV_DEBUG_INFO("Unmute\n");
  889. }