ivtv-fileops.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918
  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-audio.h"
  27. #include "ivtv-streams.h"
  28. #include "ivtv-yuv.h"
  29. #include "ivtv-controls.h"
  30. #include "ivtv-ioctl.h"
  31. /* This function tries to claim the stream for a specific file descriptor.
  32. If no one else is using this stream then the stream is claimed and
  33. associated VBI streams are also automatically claimed.
  34. Possible error returns: -EBUSY if someone else has claimed
  35. the stream or 0 on success. */
  36. int ivtv_claim_stream(struct ivtv_open_id *id, int type)
  37. {
  38. struct ivtv *itv = id->itv;
  39. struct ivtv_stream *s = &itv->streams[type];
  40. struct ivtv_stream *s_vbi;
  41. int vbi_type;
  42. if (test_and_set_bit(IVTV_F_S_CLAIMED, &s->s_flags)) {
  43. /* someone already claimed this stream */
  44. if (s->id == id->open_id) {
  45. /* yes, this file descriptor did. So that's OK. */
  46. return 0;
  47. }
  48. if (s->id == -1 && (type == IVTV_DEC_STREAM_TYPE_VBI ||
  49. type == IVTV_ENC_STREAM_TYPE_VBI)) {
  50. /* VBI is handled already internally, now also assign
  51. the file descriptor to this stream for external
  52. reading of the stream. */
  53. s->id = id->open_id;
  54. IVTV_DEBUG_INFO("Start Read VBI\n");
  55. return 0;
  56. }
  57. /* someone else is using this stream already */
  58. IVTV_DEBUG_INFO("Stream %d is busy\n", type);
  59. return -EBUSY;
  60. }
  61. s->id = id->open_id;
  62. if (type == IVTV_DEC_STREAM_TYPE_VBI) {
  63. /* Enable reinsertion interrupt */
  64. ivtv_clear_irq_mask(itv, IVTV_IRQ_DEC_VBI_RE_INSERT);
  65. }
  66. /* IVTV_DEC_STREAM_TYPE_MPG needs to claim IVTV_DEC_STREAM_TYPE_VBI,
  67. IVTV_ENC_STREAM_TYPE_MPG needs to claim IVTV_ENC_STREAM_TYPE_VBI
  68. (provided VBI insertion is on and sliced VBI is selected), for all
  69. other streams we're done */
  70. if (type == IVTV_DEC_STREAM_TYPE_MPG) {
  71. vbi_type = IVTV_DEC_STREAM_TYPE_VBI;
  72. } else if (type == IVTV_ENC_STREAM_TYPE_MPG &&
  73. itv->vbi.insert_mpeg && itv->vbi.sliced_in->service_set) {
  74. vbi_type = IVTV_ENC_STREAM_TYPE_VBI;
  75. } else {
  76. return 0;
  77. }
  78. s_vbi = &itv->streams[vbi_type];
  79. if (!test_and_set_bit(IVTV_F_S_CLAIMED, &s_vbi->s_flags)) {
  80. /* Enable reinsertion interrupt */
  81. if (vbi_type == IVTV_DEC_STREAM_TYPE_VBI)
  82. ivtv_clear_irq_mask(itv, IVTV_IRQ_DEC_VBI_RE_INSERT);
  83. }
  84. /* mark that it is used internally */
  85. set_bit(IVTV_F_S_INTERNAL_USE, &s_vbi->s_flags);
  86. return 0;
  87. }
  88. /* This function releases a previously claimed stream. It will take into
  89. account associated VBI streams. */
  90. void ivtv_release_stream(struct ivtv_stream *s)
  91. {
  92. struct ivtv *itv = s->itv;
  93. struct ivtv_stream *s_vbi;
  94. s->id = -1;
  95. if ((s->type == IVTV_DEC_STREAM_TYPE_VBI || s->type == IVTV_ENC_STREAM_TYPE_VBI) &&
  96. test_bit(IVTV_F_S_INTERNAL_USE, &s->s_flags)) {
  97. /* this stream is still in use internally */
  98. return;
  99. }
  100. if (!test_and_clear_bit(IVTV_F_S_CLAIMED, &s->s_flags)) {
  101. IVTV_DEBUG_WARN("Release stream %s not in use!\n", s->name);
  102. return;
  103. }
  104. ivtv_flush_queues(s);
  105. /* disable reinsertion interrupt */
  106. if (s->type == IVTV_DEC_STREAM_TYPE_VBI)
  107. ivtv_set_irq_mask(itv, IVTV_IRQ_DEC_VBI_RE_INSERT);
  108. /* IVTV_DEC_STREAM_TYPE_MPG needs to release IVTV_DEC_STREAM_TYPE_VBI,
  109. IVTV_ENC_STREAM_TYPE_MPG needs to release IVTV_ENC_STREAM_TYPE_VBI,
  110. for all other streams we're done */
  111. if (s->type == IVTV_DEC_STREAM_TYPE_MPG)
  112. s_vbi = &itv->streams[IVTV_DEC_STREAM_TYPE_VBI];
  113. else if (s->type == IVTV_ENC_STREAM_TYPE_MPG)
  114. s_vbi = &itv->streams[IVTV_ENC_STREAM_TYPE_VBI];
  115. else
  116. return;
  117. /* clear internal use flag */
  118. if (!test_and_clear_bit(IVTV_F_S_INTERNAL_USE, &s_vbi->s_flags)) {
  119. /* was already cleared */
  120. return;
  121. }
  122. if (s_vbi->id != -1) {
  123. /* VBI stream still claimed by a file descriptor */
  124. return;
  125. }
  126. /* disable reinsertion interrupt */
  127. if (s_vbi->type == IVTV_DEC_STREAM_TYPE_VBI)
  128. ivtv_set_irq_mask(itv, IVTV_IRQ_DEC_VBI_RE_INSERT);
  129. clear_bit(IVTV_F_S_CLAIMED, &s_vbi->s_flags);
  130. ivtv_flush_queues(s_vbi);
  131. }
  132. static void ivtv_dualwatch(struct ivtv *itv)
  133. {
  134. struct v4l2_tuner vt;
  135. u16 new_bitmap;
  136. u16 new_stereo_mode;
  137. const u16 stereo_mask = 0x0300;
  138. const u16 dual = 0x0200;
  139. new_stereo_mode = itv->params.audio_properties & stereo_mask;
  140. memset(&vt, 0, sizeof(vt));
  141. ivtv_call_i2c_clients(itv, VIDIOC_G_TUNER, &vt);
  142. if (vt.audmode == V4L2_TUNER_MODE_LANG1_LANG2 && (vt.rxsubchans & V4L2_TUNER_SUB_LANG2))
  143. new_stereo_mode = dual;
  144. if (new_stereo_mode == itv->dualwatch_stereo_mode)
  145. return;
  146. new_bitmap = new_stereo_mode | (itv->params.audio_properties & ~stereo_mask);
  147. IVTV_DEBUG_INFO("dualwatch: change stereo flag from 0x%x to 0x%x. new audio_bitmask=0x%ux\n",
  148. itv->dualwatch_stereo_mode, new_stereo_mode, new_bitmap);
  149. if (ivtv_vapi(itv, CX2341X_ENC_SET_AUDIO_PROPERTIES, 1, new_bitmap) == 0) {
  150. itv->dualwatch_stereo_mode = new_stereo_mode;
  151. return;
  152. }
  153. IVTV_DEBUG_INFO("dualwatch: changing stereo flag failed\n");
  154. }
  155. static void ivtv_update_pgm_info(struct ivtv *itv)
  156. {
  157. u32 wr_idx = (read_enc(itv->pgm_info_offset) - itv->pgm_info_offset - 4) / 24;
  158. int cnt;
  159. int i = 0;
  160. if (wr_idx >= itv->pgm_info_num) {
  161. IVTV_DEBUG_WARN("Invalid PGM index %d (>= %d)\n", wr_idx, itv->pgm_info_num);
  162. return;
  163. }
  164. cnt = (wr_idx + itv->pgm_info_num - itv->pgm_info_write_idx) % itv->pgm_info_num;
  165. while (i < cnt) {
  166. int idx = (itv->pgm_info_write_idx + i) % itv->pgm_info_num;
  167. struct v4l2_enc_idx_entry *e = itv->pgm_info + idx;
  168. u32 addr = itv->pgm_info_offset + 4 + idx * 24;
  169. const int mapping[] = { V4L2_ENC_IDX_FRAME_P, V4L2_ENC_IDX_FRAME_I, V4L2_ENC_IDX_FRAME_B, 0 };
  170. e->offset = read_enc(addr + 4) + ((u64)read_enc(addr + 8) << 32);
  171. if (e->offset > itv->mpg_data_received) {
  172. break;
  173. }
  174. e->offset += itv->vbi_data_inserted;
  175. e->length = read_enc(addr);
  176. e->pts = read_enc(addr + 16) + ((u64)(read_enc(addr + 20) & 1) << 32);
  177. e->flags = mapping[read_enc(addr + 12) & 3];
  178. i++;
  179. }
  180. itv->pgm_info_write_idx = (itv->pgm_info_write_idx + i) % itv->pgm_info_num;
  181. }
  182. static struct ivtv_buffer *ivtv_get_buffer(struct ivtv_stream *s, int non_block, int *err)
  183. {
  184. struct ivtv *itv = s->itv;
  185. struct ivtv_stream *s_vbi = &itv->streams[IVTV_ENC_STREAM_TYPE_VBI];
  186. struct ivtv_buffer *buf;
  187. DEFINE_WAIT(wait);
  188. *err = 0;
  189. while (1) {
  190. if (s->type == IVTV_ENC_STREAM_TYPE_MPG) {
  191. /* Process pending program info updates and pending VBI data */
  192. ivtv_update_pgm_info(itv);
  193. if (jiffies - itv->dualwatch_jiffies > HZ) {
  194. itv->dualwatch_jiffies = jiffies;
  195. ivtv_dualwatch(itv);
  196. }
  197. if (test_bit(IVTV_F_S_INTERNAL_USE, &s_vbi->s_flags) &&
  198. !test_bit(IVTV_F_S_APPL_IO, &s_vbi->s_flags)) {
  199. while ((buf = ivtv_dequeue(s_vbi, &s_vbi->q_full))) {
  200. /* byteswap and process VBI data */
  201. ivtv_process_vbi_data(itv, buf, s_vbi->dma_pts, s_vbi->type);
  202. ivtv_enqueue(s_vbi, buf, &s_vbi->q_free);
  203. }
  204. }
  205. buf = &itv->vbi.sliced_mpeg_buf;
  206. if (buf->readpos != buf->bytesused) {
  207. return buf;
  208. }
  209. }
  210. /* do we have leftover data? */
  211. buf = ivtv_dequeue(s, &s->q_io);
  212. if (buf)
  213. return buf;
  214. /* do we have new data? */
  215. buf = ivtv_dequeue(s, &s->q_full);
  216. if (buf) {
  217. if (!test_and_clear_bit(IVTV_F_B_NEED_BUF_SWAP, &buf->b_flags))
  218. return buf;
  219. if (s->type == IVTV_ENC_STREAM_TYPE_MPG)
  220. /* byteswap MPG data */
  221. ivtv_buf_swap(buf);
  222. else if (s->type != IVTV_DEC_STREAM_TYPE_VBI) {
  223. /* byteswap and process VBI data */
  224. ivtv_process_vbi_data(itv, buf, s->dma_pts, s->type);
  225. }
  226. return buf;
  227. }
  228. /* return if file was opened with O_NONBLOCK */
  229. if (non_block) {
  230. *err = -EAGAIN;
  231. return NULL;
  232. }
  233. /* return if end of stream */
  234. if (s->type != IVTV_DEC_STREAM_TYPE_VBI && !test_bit(IVTV_F_S_STREAMING, &s->s_flags)) {
  235. clear_bit(IVTV_F_S_STREAMOFF, &s->s_flags);
  236. IVTV_DEBUG_INFO("EOS %s\n", s->name);
  237. return NULL;
  238. }
  239. /* wait for more data to arrive */
  240. prepare_to_wait(&s->waitq, &wait, TASK_INTERRUPTIBLE);
  241. /* New buffers might have become available before we were added to the waitqueue */
  242. if (!s->q_full.buffers)
  243. schedule();
  244. finish_wait(&s->waitq, &wait);
  245. if (signal_pending(current)) {
  246. /* return if a signal was received */
  247. IVTV_DEBUG_INFO("User stopped %s\n", s->name);
  248. *err = -EINTR;
  249. return NULL;
  250. }
  251. }
  252. }
  253. static void ivtv_setup_sliced_vbi_buf(struct ivtv *itv)
  254. {
  255. int idx = itv->vbi.inserted_frame % IVTV_VBI_FRAMES;
  256. itv->vbi.sliced_mpeg_buf.buf = itv->vbi.sliced_mpeg_data[idx];
  257. itv->vbi.sliced_mpeg_buf.bytesused = itv->vbi.sliced_mpeg_size[idx];
  258. itv->vbi.sliced_mpeg_buf.readpos = 0;
  259. }
  260. static size_t ivtv_copy_buf_to_user(struct ivtv_stream *s, struct ivtv_buffer *buf,
  261. char __user *ubuf, size_t ucount)
  262. {
  263. struct ivtv *itv = s->itv;
  264. size_t len = buf->bytesused - buf->readpos;
  265. if (len > ucount) len = ucount;
  266. if (itv->vbi.insert_mpeg && s->type == IVTV_ENC_STREAM_TYPE_MPG &&
  267. itv->vbi.sliced_in->service_set && buf != &itv->vbi.sliced_mpeg_buf) {
  268. const char *start = buf->buf + buf->readpos;
  269. const char *p = start + 1;
  270. const u8 *q;
  271. u8 ch = itv->search_pack_header ? 0xba : 0xe0;
  272. int stuffing, i;
  273. while (start + len > p && (q = memchr(p, 0, start + len - p))) {
  274. p = q + 1;
  275. if ((char *)q + 15 >= buf->buf + buf->bytesused ||
  276. q[1] != 0 || q[2] != 1 || q[3] != ch) {
  277. continue;
  278. }
  279. if (!itv->search_pack_header) {
  280. if ((q[6] & 0xc0) != 0x80)
  281. continue;
  282. if (((q[7] & 0xc0) == 0x80 && (q[9] & 0xf0) == 0x20) ||
  283. ((q[7] & 0xc0) == 0xc0 && (q[9] & 0xf0) == 0x30)) {
  284. ch = 0xba;
  285. itv->search_pack_header = 1;
  286. p = q + 9;
  287. }
  288. continue;
  289. }
  290. stuffing = q[13] & 7;
  291. /* all stuffing bytes must be 0xff */
  292. for (i = 0; i < stuffing; i++)
  293. if (q[14 + i] != 0xff)
  294. break;
  295. if (i == stuffing && (q[4] & 0xc4) == 0x44 && (q[12] & 3) == 3 &&
  296. q[14 + stuffing] == 0 && q[15 + stuffing] == 0 &&
  297. q[16 + stuffing] == 1) {
  298. itv->search_pack_header = 0;
  299. len = (char *)q - start;
  300. ivtv_setup_sliced_vbi_buf(itv);
  301. break;
  302. }
  303. }
  304. }
  305. if (copy_to_user(ubuf, (u8 *)buf->buf + buf->readpos, len)) {
  306. IVTV_DEBUG_WARN("copy %zd bytes to user failed for %s\n", len, s->name);
  307. return -EFAULT;
  308. }
  309. /*IVTV_INFO("copied %lld %d %d %d %d %d vbi %d\n", itv->mpg_data_received, len, ucount,
  310. buf->readpos, buf->bytesused, buf->bytesused - buf->readpos - len,
  311. buf == &itv->vbi.sliced_mpeg_buf); */
  312. buf->readpos += len;
  313. if (s->type == IVTV_ENC_STREAM_TYPE_MPG && buf != &itv->vbi.sliced_mpeg_buf)
  314. itv->mpg_data_received += len;
  315. return len;
  316. }
  317. static ssize_t ivtv_read(struct ivtv_stream *s, char __user *ubuf, size_t tot_count, int non_block)
  318. {
  319. struct ivtv *itv = s->itv;
  320. size_t tot_written = 0;
  321. int single_frame = 0;
  322. if (atomic_read(&itv->capturing) == 0 && s->id == -1) {
  323. /* shouldn't happen */
  324. IVTV_DEBUG_WARN("Stream %s not initialized before read\n", s->name);
  325. return -EIO;
  326. }
  327. /* Each VBI buffer is one frame, the v4l2 API says that for VBI the frames should
  328. arrive one-by-one, so make sure we never output more than one VBI frame at a time */
  329. if (s->type == IVTV_DEC_STREAM_TYPE_VBI ||
  330. (s->type == IVTV_ENC_STREAM_TYPE_VBI && itv->vbi.sliced_in->service_set))
  331. single_frame = 1;
  332. for (;;) {
  333. struct ivtv_buffer *buf;
  334. int rc;
  335. buf = ivtv_get_buffer(s, non_block, &rc);
  336. if (buf == NULL && rc == -EAGAIN && tot_written)
  337. break;
  338. if (buf == NULL)
  339. return rc;
  340. rc = ivtv_copy_buf_to_user(s, buf, ubuf + tot_written, tot_count - tot_written);
  341. if (buf != &itv->vbi.sliced_mpeg_buf) {
  342. ivtv_enqueue(s, buf, (buf->readpos == buf->bytesused) ? &s->q_free : &s->q_io);
  343. }
  344. else if (buf->readpos == buf->bytesused) {
  345. int idx = itv->vbi.inserted_frame % IVTV_VBI_FRAMES;
  346. itv->vbi.sliced_mpeg_size[idx] = 0;
  347. itv->vbi.inserted_frame++;
  348. itv->vbi_data_inserted += buf->bytesused;
  349. }
  350. if (rc < 0)
  351. return rc;
  352. tot_written += rc;
  353. if (tot_written == tot_count || single_frame)
  354. break;
  355. }
  356. return tot_written;
  357. }
  358. static ssize_t ivtv_read_pos(struct ivtv_stream *s, char __user *ubuf, size_t count,
  359. loff_t *pos, int non_block)
  360. {
  361. ssize_t rc = count ? ivtv_read(s, ubuf, count, non_block) : 0;
  362. struct ivtv *itv = s->itv;
  363. IVTV_DEBUG_INFO("read %zd from %s, got %zd\n", count, s->name, rc);
  364. if (rc > 0)
  365. pos += rc;
  366. return rc;
  367. }
  368. int ivtv_start_capture(struct ivtv_open_id *id)
  369. {
  370. struct ivtv *itv = id->itv;
  371. struct ivtv_stream *s = &itv->streams[id->type];
  372. struct ivtv_stream *s_vbi;
  373. if (s->type == IVTV_ENC_STREAM_TYPE_RAD ||
  374. s->type == IVTV_DEC_STREAM_TYPE_MPG ||
  375. s->type == IVTV_DEC_STREAM_TYPE_YUV ||
  376. s->type == IVTV_DEC_STREAM_TYPE_VOUT) {
  377. /* you cannot read from these stream types. */
  378. return -EPERM;
  379. }
  380. /* Try to claim this stream. */
  381. if (ivtv_claim_stream(id, s->type))
  382. return -EBUSY;
  383. /* This stream does not need to start capturing */
  384. if (s->type == IVTV_DEC_STREAM_TYPE_VBI) {
  385. set_bit(IVTV_F_S_APPL_IO, &s->s_flags);
  386. return 0;
  387. }
  388. /* If capture is already in progress, then we also have to
  389. do nothing extra. */
  390. if (test_bit(IVTV_F_S_STREAMOFF, &s->s_flags) || test_and_set_bit(IVTV_F_S_STREAMING, &s->s_flags)) {
  391. set_bit(IVTV_F_S_APPL_IO, &s->s_flags);
  392. return 0;
  393. }
  394. /* Start VBI capture if required */
  395. s_vbi = &itv->streams[IVTV_ENC_STREAM_TYPE_VBI];
  396. if (s->type == IVTV_ENC_STREAM_TYPE_MPG &&
  397. test_bit(IVTV_F_S_INTERNAL_USE, &s_vbi->s_flags) &&
  398. !test_and_set_bit(IVTV_F_S_STREAMING, &s_vbi->s_flags)) {
  399. /* Note: the IVTV_ENC_STREAM_TYPE_VBI is claimed
  400. automatically when the MPG stream is claimed.
  401. We only need to start the VBI capturing. */
  402. if (ivtv_start_v4l2_encode_stream(s_vbi)) {
  403. IVTV_DEBUG_WARN("VBI capture start failed\n");
  404. /* Failure, clean up and return an error */
  405. clear_bit(IVTV_F_S_STREAMING, &s_vbi->s_flags);
  406. clear_bit(IVTV_F_S_STREAMING, &s->s_flags);
  407. /* also releases the associated VBI stream */
  408. ivtv_release_stream(s);
  409. return -EIO;
  410. }
  411. IVTV_DEBUG_INFO("VBI insertion started\n");
  412. }
  413. /* Tell the card to start capturing */
  414. if (!ivtv_start_v4l2_encode_stream(s)) {
  415. /* We're done */
  416. set_bit(IVTV_F_S_APPL_IO, &s->s_flags);
  417. /* Resume a possibly paused encoder */
  418. if (test_and_clear_bit(IVTV_F_I_ENC_PAUSED, &itv->i_flags))
  419. ivtv_vapi(itv, CX2341X_ENC_PAUSE_ENCODER, 1, 1);
  420. return 0;
  421. }
  422. /* failure, clean up */
  423. IVTV_DEBUG_WARN("Failed to start capturing for stream %s\n", s->name);
  424. /* Note: the IVTV_ENC_STREAM_TYPE_VBI is released
  425. automatically when the MPG stream is released.
  426. We only need to stop the VBI capturing. */
  427. if (s->type == IVTV_ENC_STREAM_TYPE_MPG &&
  428. test_bit(IVTV_F_S_STREAMING, &s_vbi->s_flags)) {
  429. ivtv_stop_v4l2_encode_stream(s_vbi, 0);
  430. clear_bit(IVTV_F_S_STREAMING, &s_vbi->s_flags);
  431. }
  432. clear_bit(IVTV_F_S_STREAMING, &s->s_flags);
  433. ivtv_release_stream(s);
  434. return -EIO;
  435. }
  436. ssize_t ivtv_v4l2_read(struct file * filp, char __user *buf, size_t count, loff_t * pos)
  437. {
  438. struct ivtv_open_id *id = filp->private_data;
  439. struct ivtv *itv = id->itv;
  440. struct ivtv_stream *s = &itv->streams[id->type];
  441. int rc;
  442. IVTV_DEBUG_IOCTL("read %zd bytes from %s\n", count, s->name);
  443. rc = ivtv_start_capture(id);
  444. if (rc)
  445. return rc;
  446. return ivtv_read_pos(s, buf, count, pos, filp->f_flags & O_NONBLOCK);
  447. }
  448. int ivtv_start_decoding(struct ivtv_open_id *id, int speed)
  449. {
  450. struct ivtv *itv = id->itv;
  451. struct ivtv_stream *s = &itv->streams[id->type];
  452. if (atomic_read(&itv->decoding) == 0) {
  453. if (ivtv_claim_stream(id, s->type)) {
  454. /* someone else is using this stream already */
  455. IVTV_DEBUG_WARN("start decode, stream already claimed\n");
  456. return -EBUSY;
  457. }
  458. ivtv_start_v4l2_decode_stream(s, 0);
  459. }
  460. if (s->type == IVTV_DEC_STREAM_TYPE_MPG)
  461. return ivtv_set_speed(itv, speed);
  462. return 0;
  463. }
  464. ssize_t ivtv_v4l2_write(struct file *filp, const char __user *user_buf, size_t count, loff_t *pos)
  465. {
  466. struct ivtv_open_id *id = filp->private_data;
  467. struct ivtv *itv = id->itv;
  468. struct ivtv_stream *s = &itv->streams[id->type];
  469. struct ivtv_buffer *buf;
  470. struct ivtv_queue q;
  471. int bytes_written = 0;
  472. int mode;
  473. int rc;
  474. DEFINE_WAIT(wait);
  475. IVTV_DEBUG_IOCTL("write %zd bytes to %s\n", count, s->name);
  476. if (s->type != IVTV_DEC_STREAM_TYPE_MPG &&
  477. s->type != IVTV_DEC_STREAM_TYPE_YUV &&
  478. s->type != IVTV_DEC_STREAM_TYPE_VOUT)
  479. /* not decoder streams */
  480. return -EPERM;
  481. /* Try to claim this stream */
  482. if (ivtv_claim_stream(id, s->type))
  483. return -EBUSY;
  484. /* This stream does not need to start any decoding */
  485. if (s->type == IVTV_DEC_STREAM_TYPE_VOUT) {
  486. set_bit(IVTV_F_S_APPL_IO, &s->s_flags);
  487. return ivtv_write_vbi(itv, user_buf, count);
  488. }
  489. mode = s->type == IVTV_DEC_STREAM_TYPE_MPG ? OUT_MPG : OUT_YUV;
  490. if (ivtv_set_output_mode(itv, mode) != mode) {
  491. ivtv_release_stream(s);
  492. return -EBUSY;
  493. }
  494. ivtv_queue_init(&q);
  495. set_bit(IVTV_F_S_APPL_IO, &s->s_flags);
  496. retry:
  497. for (;;) {
  498. /* Gather buffers */
  499. while (q.length - q.bytesused < count && (buf = ivtv_dequeue(s, &s->q_io)))
  500. ivtv_enqueue(s, buf, &q);
  501. while (q.length - q.bytesused < count && (buf = ivtv_dequeue(s, &s->q_free))) {
  502. ivtv_enqueue(s, buf, &q);
  503. }
  504. if (q.buffers)
  505. break;
  506. if (filp->f_flags & O_NONBLOCK)
  507. return -EAGAIN;
  508. prepare_to_wait(&s->waitq, &wait, TASK_INTERRUPTIBLE);
  509. /* New buffers might have become free before we were added to the waitqueue */
  510. if (!s->q_free.buffers)
  511. schedule();
  512. finish_wait(&s->waitq, &wait);
  513. if (signal_pending(current)) {
  514. IVTV_DEBUG_INFO("User stopped %s\n", s->name);
  515. return -EINTR;
  516. }
  517. }
  518. /* copy user data into buffers */
  519. while ((buf = ivtv_dequeue(s, &q))) {
  520. /* Make sure we really got all the user data */
  521. rc = ivtv_buf_copy_from_user(s, buf, user_buf, count);
  522. if (rc < 0) {
  523. ivtv_queue_move(s, &q, NULL, &s->q_free, 0);
  524. return rc;
  525. }
  526. user_buf += rc;
  527. count -= rc;
  528. bytes_written += rc;
  529. if (buf->bytesused != s->buf_size) {
  530. /* incomplete, leave in q_io for next time */
  531. ivtv_enqueue(s, buf, &s->q_io);
  532. break;
  533. }
  534. /* Byteswap MPEG buffer */
  535. if (s->type == IVTV_DEC_STREAM_TYPE_MPG)
  536. ivtv_buf_swap(buf);
  537. ivtv_enqueue(s, buf, &s->q_full);
  538. }
  539. /* Start decoder (returns 0 if already started) */
  540. rc = ivtv_start_decoding(id, itv->speed);
  541. if (rc) {
  542. IVTV_DEBUG_WARN("Failed start decode stream %s\n", s->name);
  543. /* failure, clean up */
  544. clear_bit(IVTV_F_S_STREAMING, &s->s_flags);
  545. clear_bit(IVTV_F_S_APPL_IO, &s->s_flags);
  546. return rc;
  547. }
  548. if (test_bit(IVTV_F_S_NEEDS_DATA, &s->s_flags)) {
  549. if (s->q_full.length >= itv->dma_data_req_size) {
  550. int got_sig;
  551. prepare_to_wait(&itv->dma_waitq, &wait, TASK_INTERRUPTIBLE);
  552. while (!(got_sig = signal_pending(current)) &&
  553. test_bit(IVTV_F_S_DMA_PENDING, &s->s_flags)) {
  554. schedule();
  555. }
  556. finish_wait(&itv->dma_waitq, &wait);
  557. if (got_sig) {
  558. IVTV_DEBUG_INFO("User interrupted %s\n", s->name);
  559. return -EINTR;
  560. }
  561. clear_bit(IVTV_F_S_NEEDS_DATA, &s->s_flags);
  562. ivtv_queue_move(s, &s->q_full, NULL, &s->q_predma, itv->dma_data_req_size);
  563. ivtv_dma_stream_dec_prepare(s, itv->dma_data_req_offset + IVTV_DECODER_OFFSET, 1);
  564. }
  565. }
  566. /* more user data is available, wait until buffers become free
  567. to transfer the rest. */
  568. if (count && !(filp->f_flags & O_NONBLOCK))
  569. goto retry;
  570. IVTV_DEBUG_INFO("Wrote %d bytes to %s (%d)\n", bytes_written, s->name, s->q_full.bytesused);
  571. return bytes_written;
  572. }
  573. unsigned int ivtv_v4l2_dec_poll(struct file *filp, poll_table *wait)
  574. {
  575. struct ivtv_open_id *id = filp->private_data;
  576. struct ivtv *itv = id->itv;
  577. struct ivtv_stream *s = &itv->streams[id->type];
  578. int res = 0;
  579. /* add stream's waitq to the poll list */
  580. poll_wait(filp, &s->waitq, wait);
  581. set_bit(IVTV_F_I_EV_VSYNC_ENABLED, &itv->i_flags);
  582. if (test_bit(IVTV_F_I_EV_VSYNC, &itv->i_flags) ||
  583. test_bit(IVTV_F_I_EV_DEC_STOPPED, &itv->i_flags))
  584. res = POLLPRI;
  585. /* Allow write if buffers are available for writing */
  586. if (s->q_free.buffers)
  587. res |= POLLOUT | POLLWRNORM;
  588. return res;
  589. }
  590. unsigned int ivtv_v4l2_enc_poll(struct file *filp, poll_table * wait)
  591. {
  592. struct ivtv_open_id *id = filp->private_data;
  593. struct ivtv *itv = id->itv;
  594. struct ivtv_stream *s = &itv->streams[id->type];
  595. int eof = test_bit(IVTV_F_S_STREAMOFF, &s->s_flags);
  596. /* Start a capture if there is none */
  597. if (!eof && !test_bit(IVTV_F_S_STREAMING, &s->s_flags)) {
  598. int rc = ivtv_start_capture(id);
  599. if (rc) {
  600. IVTV_DEBUG_INFO("Could not start capture for %s (%d)\n",
  601. s->name, rc);
  602. return POLLERR;
  603. }
  604. }
  605. /* add stream's waitq to the poll list */
  606. poll_wait(filp, &s->waitq, wait);
  607. if (eof || s->q_full.length)
  608. return POLLIN | POLLRDNORM;
  609. return 0;
  610. }
  611. void ivtv_stop_capture(struct ivtv_open_id *id, int gop_end)
  612. {
  613. struct ivtv *itv = id->itv;
  614. struct ivtv_stream *s = &itv->streams[id->type];
  615. IVTV_DEBUG_IOCTL("close() of %s\n", s->name);
  616. /* 'Unclaim' this stream */
  617. /* Stop capturing */
  618. if (test_bit(IVTV_F_S_STREAMING, &s->s_flags)) {
  619. struct ivtv_stream *s_vbi = &itv->streams[IVTV_ENC_STREAM_TYPE_VBI];
  620. IVTV_DEBUG_INFO("close stopping capture\n");
  621. /* Special case: a running VBI capture for VBI insertion
  622. in the mpeg stream. Need to stop that too. */
  623. if (id->type == IVTV_ENC_STREAM_TYPE_MPG &&
  624. test_bit(IVTV_F_S_STREAMING, &s_vbi->s_flags) &&
  625. !test_bit(IVTV_F_S_APPL_IO, &s_vbi->s_flags)) {
  626. IVTV_DEBUG_INFO("close stopping embedded VBI capture\n");
  627. ivtv_stop_v4l2_encode_stream(s_vbi, 0);
  628. }
  629. if ((id->type == IVTV_DEC_STREAM_TYPE_VBI ||
  630. id->type == IVTV_ENC_STREAM_TYPE_VBI) &&
  631. test_bit(IVTV_F_S_INTERNAL_USE, &s->s_flags)) {
  632. /* Also used internally, don't stop capturing */
  633. s->id = -1;
  634. }
  635. else {
  636. ivtv_stop_v4l2_encode_stream(s, gop_end);
  637. }
  638. }
  639. clear_bit(IVTV_F_S_APPL_IO, &s->s_flags);
  640. clear_bit(IVTV_F_S_STREAMOFF, &s->s_flags);
  641. ivtv_release_stream(s);
  642. }
  643. void ivtv_stop_decoding(struct ivtv_open_id *id, int flags, u64 pts)
  644. {
  645. struct ivtv *itv = id->itv;
  646. struct ivtv_stream *s = &itv->streams[id->type];
  647. IVTV_DEBUG_IOCTL("close() of %s\n", s->name);
  648. /* Stop decoding */
  649. if (test_bit(IVTV_F_S_STREAMING, &s->s_flags)) {
  650. IVTV_DEBUG_INFO("close stopping decode\n");
  651. ivtv_stop_v4l2_decode_stream(s, flags, pts);
  652. }
  653. clear_bit(IVTV_F_S_APPL_IO, &s->s_flags);
  654. clear_bit(IVTV_F_S_STREAMOFF, &s->s_flags);
  655. if (id->type == IVTV_DEC_STREAM_TYPE_YUV && test_bit(IVTV_F_I_DECODING_YUV, &itv->i_flags)) {
  656. /* Restore registers we've changed & clean up any mess we've made */
  657. ivtv_yuv_close(itv);
  658. }
  659. if (s->type == IVTV_DEC_STREAM_TYPE_YUV && itv->output_mode == OUT_YUV)
  660. itv->output_mode = OUT_NONE;
  661. else if (s->type == IVTV_DEC_STREAM_TYPE_MPG && itv->output_mode == OUT_MPG)
  662. itv->output_mode = OUT_NONE;
  663. itv->speed = 0;
  664. ivtv_release_stream(s);
  665. }
  666. int ivtv_v4l2_close(struct inode *inode, struct file *filp)
  667. {
  668. struct ivtv_open_id *id = filp->private_data;
  669. struct ivtv *itv = id->itv;
  670. struct ivtv_stream *s = &itv->streams[id->type];
  671. IVTV_DEBUG_IOCTL("close() of %s\n", s->name);
  672. /* Easy case first: this stream was never claimed by us */
  673. if (s->id != id->open_id) {
  674. kfree(id);
  675. return 0;
  676. }
  677. /* 'Unclaim' this stream */
  678. /* Stop radio */
  679. if (id->type == IVTV_ENC_STREAM_TYPE_RAD) {
  680. /* Closing radio device, return to TV mode */
  681. ivtv_mute(itv);
  682. /* Mark that the radio is no longer in use */
  683. clear_bit(IVTV_F_I_RADIO_USER, &itv->i_flags);
  684. /* Switch tuner to TV */
  685. ivtv_call_i2c_clients(itv, VIDIOC_S_STD, &itv->std);
  686. /* Select correct audio input (i.e. TV tuner or Line in) */
  687. ivtv_audio_set_io(itv);
  688. /* Done! Unmute and continue. */
  689. ivtv_unmute(itv);
  690. ivtv_release_stream(s);
  691. } else if (s->type >= IVTV_DEC_STREAM_TYPE_MPG) {
  692. ivtv_stop_decoding(id, VIDEO_CMD_STOP_TO_BLACK | VIDEO_CMD_STOP_IMMEDIATELY, 0);
  693. } else {
  694. ivtv_stop_capture(id, 0);
  695. }
  696. kfree(id);
  697. return 0;
  698. }
  699. int ivtv_v4l2_open(struct inode *inode, struct file *filp)
  700. {
  701. int x, y = 0;
  702. struct ivtv_open_id *item;
  703. struct ivtv *itv = NULL;
  704. struct ivtv_stream *s = NULL;
  705. int minor = MINOR(inode->i_rdev);
  706. /* Find which card this open was on */
  707. spin_lock(&ivtv_cards_lock);
  708. for (x = 0; itv == NULL && x < ivtv_cards_active; x++) {
  709. /* find out which stream this open was on */
  710. for (y = 0; y < IVTV_MAX_STREAMS; y++) {
  711. s = &ivtv_cards[x]->streams[y];
  712. if (s->v4l2dev && s->v4l2dev->minor == minor) {
  713. itv = ivtv_cards[x];
  714. break;
  715. }
  716. }
  717. }
  718. spin_unlock(&ivtv_cards_lock);
  719. if (itv == NULL) {
  720. /* Couldn't find a device registered
  721. on that minor, shouldn't happen! */
  722. printk(KERN_WARNING "ivtv: no ivtv device found on minor %d\n", minor);
  723. return -ENXIO;
  724. }
  725. if (y == IVTV_DEC_STREAM_TYPE_MPG &&
  726. test_bit(IVTV_F_S_CLAIMED, &itv->streams[IVTV_DEC_STREAM_TYPE_YUV].s_flags))
  727. return -EBUSY;
  728. if (y == IVTV_DEC_STREAM_TYPE_YUV &&
  729. test_bit(IVTV_F_S_CLAIMED, &itv->streams[IVTV_DEC_STREAM_TYPE_MPG].s_flags))
  730. return -EBUSY;
  731. if (y == IVTV_DEC_STREAM_TYPE_YUV) {
  732. if (read_reg(0x82c) == 0) {
  733. IVTV_ERR("Tried to open YUV output device but need to send data to mpeg decoder before it can be used\n");
  734. /* return -ENODEV; */
  735. }
  736. ivtv_udma_alloc(itv);
  737. }
  738. /* Allocate memory */
  739. item = kmalloc(sizeof(struct ivtv_open_id), GFP_KERNEL);
  740. if (NULL == item) {
  741. IVTV_DEBUG_WARN("nomem on v4l2 open\n");
  742. return -ENOMEM;
  743. }
  744. item->itv = itv;
  745. item->type = y;
  746. item->open_id = itv->open_id++;
  747. filp->private_data = item;
  748. if (item->type == IVTV_ENC_STREAM_TYPE_RAD) {
  749. /* Try to claim this stream */
  750. if (ivtv_claim_stream(item, item->type)) {
  751. /* No, it's already in use */
  752. kfree(item);
  753. return -EBUSY;
  754. }
  755. /* We have the radio */
  756. ivtv_mute(itv);
  757. /* Switch tuner to radio */
  758. ivtv_call_i2c_clients(itv, AUDC_SET_RADIO, NULL);
  759. /* Mark that the radio is being used. */
  760. set_bit(IVTV_F_I_RADIO_USER, &itv->i_flags);
  761. /* Select the correct audio input (i.e. radio tuner) */
  762. ivtv_audio_set_io(itv);
  763. /* Done! Unmute and continue. */
  764. ivtv_unmute(itv);
  765. }
  766. /* YUV or MPG Decoding Mode? */
  767. if (y == IVTV_DEC_STREAM_TYPE_MPG)
  768. clear_bit(IVTV_F_I_DEC_YUV, &itv->i_flags);
  769. else if (y == IVTV_DEC_STREAM_TYPE_YUV)
  770. {
  771. set_bit(IVTV_F_I_DEC_YUV, &itv->i_flags);
  772. }
  773. return 0;
  774. }
  775. void ivtv_mute(struct ivtv *itv)
  776. {
  777. struct v4l2_control ctrl = { V4L2_CID_AUDIO_MUTE, 1 };
  778. /* Mute sound to avoid pop */
  779. ivtv_control_ioctls(itv, VIDIOC_S_CTRL, &ctrl);
  780. if (atomic_read(&itv->capturing))
  781. ivtv_vapi(itv, CX2341X_ENC_MUTE_AUDIO, 1, 1);
  782. IVTV_DEBUG_INFO("Mute\n");
  783. }
  784. void ivtv_unmute(struct ivtv *itv)
  785. {
  786. struct v4l2_control ctrl = { V4L2_CID_AUDIO_MUTE, 0 };
  787. /* initialize or refresh input */
  788. if (atomic_read(&itv->capturing) == 0)
  789. ivtv_vapi(itv, CX2341X_ENC_INITIALIZE_INPUT, 0);
  790. ivtv_sleep_timeout(HZ / 10, 0);
  791. if (atomic_read(&itv->capturing)) {
  792. ivtv_vapi(itv, CX2341X_ENC_MISC, 1, 12);
  793. ivtv_vapi(itv, CX2341X_ENC_MUTE_AUDIO, 1, 0);
  794. }
  795. /* Unmute */
  796. ivtv_control_ioctls(itv, VIDIOC_S_CTRL, &ctrl);
  797. IVTV_DEBUG_INFO("Unmute\n");
  798. }