ivtv-fileops.c 28 KB

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