ivtv-fileops.c 28 KB

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