ivtv-fileops.c 29 KB

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