ivtv-fileops.c 29 KB

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