ivtv-fileops.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966
  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-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 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_FILE("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_FILE("read %zd bytes from %s\n", count, s->name);
  447. mutex_lock(&itv->serialize_lock);
  448. rc = ivtv_start_capture(id);
  449. mutex_unlock(&itv->serialize_lock);
  450. if (rc)
  451. return rc;
  452. return ivtv_read_pos(s, buf, count, pos, filp->f_flags & O_NONBLOCK);
  453. }
  454. int ivtv_start_decoding(struct ivtv_open_id *id, int speed)
  455. {
  456. struct ivtv *itv = id->itv;
  457. struct ivtv_stream *s = &itv->streams[id->type];
  458. if (atomic_read(&itv->decoding) == 0) {
  459. if (ivtv_claim_stream(id, s->type)) {
  460. /* someone else is using this stream already */
  461. IVTV_DEBUG_WARN("start decode, stream already claimed\n");
  462. return -EBUSY;
  463. }
  464. ivtv_start_v4l2_decode_stream(s, 0);
  465. }
  466. if (s->type == IVTV_DEC_STREAM_TYPE_MPG)
  467. return ivtv_set_speed(itv, speed);
  468. return 0;
  469. }
  470. ssize_t ivtv_v4l2_write(struct file *filp, const char __user *user_buf, size_t count, loff_t *pos)
  471. {
  472. struct ivtv_open_id *id = filp->private_data;
  473. struct ivtv *itv = id->itv;
  474. struct ivtv_stream *s = &itv->streams[id->type];
  475. struct ivtv_buffer *buf;
  476. struct ivtv_queue q;
  477. int bytes_written = 0;
  478. int mode;
  479. int rc;
  480. DEFINE_WAIT(wait);
  481. IVTV_DEBUG_HI_FILE("write %zd bytes to %s\n", count, s->name);
  482. if (s->type != IVTV_DEC_STREAM_TYPE_MPG &&
  483. s->type != IVTV_DEC_STREAM_TYPE_YUV &&
  484. s->type != IVTV_DEC_STREAM_TYPE_VOUT)
  485. /* not decoder streams */
  486. return -EPERM;
  487. /* Try to claim this stream */
  488. if (ivtv_claim_stream(id, s->type))
  489. return -EBUSY;
  490. /* This stream does not need to start any decoding */
  491. if (s->type == IVTV_DEC_STREAM_TYPE_VOUT) {
  492. set_bit(IVTV_F_S_APPL_IO, &s->s_flags);
  493. return ivtv_write_vbi(itv, user_buf, count);
  494. }
  495. mode = s->type == IVTV_DEC_STREAM_TYPE_MPG ? OUT_MPG : OUT_YUV;
  496. if (ivtv_set_output_mode(itv, mode) != mode) {
  497. ivtv_release_stream(s);
  498. return -EBUSY;
  499. }
  500. ivtv_queue_init(&q);
  501. set_bit(IVTV_F_S_APPL_IO, &s->s_flags);
  502. retry:
  503. for (;;) {
  504. /* Gather buffers */
  505. while (q.length - q.bytesused < count && (buf = ivtv_dequeue(s, &s->q_io)))
  506. ivtv_enqueue(s, buf, &q);
  507. while (q.length - q.bytesused < count && (buf = ivtv_dequeue(s, &s->q_free))) {
  508. ivtv_enqueue(s, buf, &q);
  509. }
  510. if (q.buffers)
  511. break;
  512. if (filp->f_flags & O_NONBLOCK)
  513. return -EAGAIN;
  514. prepare_to_wait(&s->waitq, &wait, TASK_INTERRUPTIBLE);
  515. /* New buffers might have become free before we were added to the waitqueue */
  516. if (!s->q_free.buffers)
  517. schedule();
  518. finish_wait(&s->waitq, &wait);
  519. if (signal_pending(current)) {
  520. IVTV_DEBUG_INFO("User stopped %s\n", s->name);
  521. return -EINTR;
  522. }
  523. }
  524. /* copy user data into buffers */
  525. while ((buf = ivtv_dequeue(s, &q))) {
  526. /* Make sure we really got all the user data */
  527. rc = ivtv_buf_copy_from_user(s, buf, user_buf, count);
  528. if (rc < 0) {
  529. ivtv_queue_move(s, &q, NULL, &s->q_free, 0);
  530. return rc;
  531. }
  532. user_buf += rc;
  533. count -= rc;
  534. bytes_written += rc;
  535. if (buf->bytesused != s->buf_size) {
  536. /* incomplete, leave in q_io for next time */
  537. ivtv_enqueue(s, buf, &s->q_io);
  538. break;
  539. }
  540. /* Byteswap MPEG buffer */
  541. if (s->type == IVTV_DEC_STREAM_TYPE_MPG)
  542. ivtv_buf_swap(buf);
  543. ivtv_enqueue(s, buf, &s->q_full);
  544. }
  545. /* Start decoder (returns 0 if already started) */
  546. mutex_lock(&itv->serialize_lock);
  547. rc = ivtv_start_decoding(id, itv->speed);
  548. mutex_unlock(&itv->serialize_lock);
  549. if (rc) {
  550. IVTV_DEBUG_WARN("Failed start decode stream %s\n", s->name);
  551. /* failure, clean up */
  552. clear_bit(IVTV_F_S_STREAMING, &s->s_flags);
  553. clear_bit(IVTV_F_S_APPL_IO, &s->s_flags);
  554. return rc;
  555. }
  556. if (test_bit(IVTV_F_S_NEEDS_DATA, &s->s_flags)) {
  557. if (s->q_full.length >= itv->dma_data_req_size) {
  558. int got_sig;
  559. prepare_to_wait(&itv->dma_waitq, &wait, TASK_INTERRUPTIBLE);
  560. while (!(got_sig = signal_pending(current)) &&
  561. test_bit(IVTV_F_S_DMA_PENDING, &s->s_flags)) {
  562. schedule();
  563. }
  564. finish_wait(&itv->dma_waitq, &wait);
  565. if (got_sig) {
  566. IVTV_DEBUG_INFO("User interrupted %s\n", s->name);
  567. return -EINTR;
  568. }
  569. clear_bit(IVTV_F_S_NEEDS_DATA, &s->s_flags);
  570. ivtv_queue_move(s, &s->q_full, NULL, &s->q_predma, itv->dma_data_req_size);
  571. ivtv_dma_stream_dec_prepare(s, itv->dma_data_req_offset + IVTV_DECODER_OFFSET, 1);
  572. }
  573. }
  574. /* more user data is available, wait until buffers become free
  575. to transfer the rest. */
  576. if (count && !(filp->f_flags & O_NONBLOCK))
  577. goto retry;
  578. IVTV_DEBUG_HI_FILE("Wrote %d bytes to %s (%d)\n", bytes_written, s->name, s->q_full.bytesused);
  579. return bytes_written;
  580. }
  581. unsigned int ivtv_v4l2_dec_poll(struct file *filp, poll_table *wait)
  582. {
  583. struct ivtv_open_id *id = filp->private_data;
  584. struct ivtv *itv = id->itv;
  585. struct ivtv_stream *s = &itv->streams[id->type];
  586. int res = 0;
  587. /* add stream's waitq to the poll list */
  588. IVTV_DEBUG_HI_FILE("Decoder poll\n");
  589. poll_wait(filp, &s->waitq, wait);
  590. set_bit(IVTV_F_I_EV_VSYNC_ENABLED, &itv->i_flags);
  591. if (test_bit(IVTV_F_I_EV_VSYNC, &itv->i_flags) ||
  592. test_bit(IVTV_F_I_EV_DEC_STOPPED, &itv->i_flags))
  593. res = POLLPRI;
  594. /* Allow write if buffers are available for writing */
  595. if (s->q_free.buffers)
  596. res |= POLLOUT | POLLWRNORM;
  597. return res;
  598. }
  599. unsigned int ivtv_v4l2_enc_poll(struct file *filp, poll_table * wait)
  600. {
  601. struct ivtv_open_id *id = filp->private_data;
  602. struct ivtv *itv = id->itv;
  603. struct ivtv_stream *s = &itv->streams[id->type];
  604. int eof = test_bit(IVTV_F_S_STREAMOFF, &s->s_flags);
  605. /* Start a capture if there is none */
  606. if (!eof && !test_bit(IVTV_F_S_STREAMING, &s->s_flags)) {
  607. int rc;
  608. mutex_lock(&itv->serialize_lock);
  609. rc = ivtv_start_capture(id);
  610. mutex_unlock(&itv->serialize_lock);
  611. if (rc) {
  612. IVTV_DEBUG_INFO("Could not start capture for %s (%d)\n",
  613. s->name, rc);
  614. return POLLERR;
  615. }
  616. IVTV_DEBUG_FILE("Encoder poll started capture\n");
  617. }
  618. /* add stream's waitq to the poll list */
  619. IVTV_DEBUG_HI_FILE("Encoder poll\n");
  620. poll_wait(filp, &s->waitq, wait);
  621. if (eof || s->q_full.length)
  622. return POLLIN | POLLRDNORM;
  623. return 0;
  624. }
  625. void ivtv_stop_capture(struct ivtv_open_id *id, int gop_end)
  626. {
  627. struct ivtv *itv = id->itv;
  628. struct ivtv_stream *s = &itv->streams[id->type];
  629. IVTV_DEBUG_FILE("close() of %s\n", s->name);
  630. /* 'Unclaim' this stream */
  631. /* Stop capturing */
  632. if (test_bit(IVTV_F_S_STREAMING, &s->s_flags)) {
  633. struct ivtv_stream *s_vbi = &itv->streams[IVTV_ENC_STREAM_TYPE_VBI];
  634. IVTV_DEBUG_INFO("close stopping capture\n");
  635. /* Special case: a running VBI capture for VBI insertion
  636. in the mpeg stream. Need to stop that too. */
  637. if (id->type == IVTV_ENC_STREAM_TYPE_MPG &&
  638. test_bit(IVTV_F_S_STREAMING, &s_vbi->s_flags) &&
  639. !test_bit(IVTV_F_S_APPL_IO, &s_vbi->s_flags)) {
  640. IVTV_DEBUG_INFO("close stopping embedded VBI capture\n");
  641. ivtv_stop_v4l2_encode_stream(s_vbi, 0);
  642. }
  643. if ((id->type == IVTV_DEC_STREAM_TYPE_VBI ||
  644. id->type == IVTV_ENC_STREAM_TYPE_VBI) &&
  645. test_bit(IVTV_F_S_INTERNAL_USE, &s->s_flags)) {
  646. /* Also used internally, don't stop capturing */
  647. s->id = -1;
  648. }
  649. else {
  650. ivtv_stop_v4l2_encode_stream(s, gop_end);
  651. }
  652. }
  653. clear_bit(IVTV_F_S_APPL_IO, &s->s_flags);
  654. clear_bit(IVTV_F_S_STREAMOFF, &s->s_flags);
  655. ivtv_release_stream(s);
  656. }
  657. static void ivtv_stop_decoding(struct ivtv_open_id *id, int flags, u64 pts)
  658. {
  659. struct ivtv *itv = id->itv;
  660. struct ivtv_stream *s = &itv->streams[id->type];
  661. IVTV_DEBUG_FILE("close() of %s\n", s->name);
  662. /* Stop decoding */
  663. if (test_bit(IVTV_F_S_STREAMING, &s->s_flags)) {
  664. IVTV_DEBUG_INFO("close stopping decode\n");
  665. ivtv_stop_v4l2_decode_stream(s, flags, pts);
  666. itv->output_mode = OUT_NONE;
  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 (itv->output_mode == OUT_UDMA_YUV && id->yuv_frames)
  675. itv->output_mode = OUT_NONE;
  676. itv->speed = 0;
  677. clear_bit(IVTV_F_I_DEC_PAUSED, &itv->i_flags);
  678. ivtv_release_stream(s);
  679. }
  680. int ivtv_v4l2_close(struct inode *inode, struct file *filp)
  681. {
  682. struct ivtv_open_id *id = filp->private_data;
  683. struct ivtv *itv = id->itv;
  684. struct ivtv_stream *s = &itv->streams[id->type];
  685. IVTV_DEBUG_FILE("close %s\n", s->name);
  686. v4l2_prio_close(&itv->prio, &id->prio);
  687. /* Easy case first: this stream was never claimed by us */
  688. if (s->id != id->open_id) {
  689. kfree(id);
  690. return 0;
  691. }
  692. /* 'Unclaim' this stream */
  693. /* Stop radio */
  694. mutex_lock(&itv->serialize_lock);
  695. if (id->type == IVTV_ENC_STREAM_TYPE_RAD) {
  696. /* Closing radio device, return to TV mode */
  697. ivtv_mute(itv);
  698. /* Mark that the radio is no longer in use */
  699. clear_bit(IVTV_F_I_RADIO_USER, &itv->i_flags);
  700. /* Switch tuner to TV */
  701. ivtv_call_i2c_clients(itv, VIDIOC_S_STD, &itv->std);
  702. /* Select correct audio input (i.e. TV tuner or Line in) */
  703. ivtv_audio_set_io(itv);
  704. if (itv->hw_flags & IVTV_HW_SAA711X)
  705. {
  706. struct v4l2_crystal_freq crystal_freq;
  707. crystal_freq.freq = SAA7115_FREQ_32_11_MHZ;
  708. crystal_freq.flags = 0;
  709. ivtv_saa7115(itv, VIDIOC_INT_S_CRYSTAL_FREQ, &crystal_freq);
  710. }
  711. /* Done! Unmute and continue. */
  712. ivtv_unmute(itv);
  713. ivtv_release_stream(s);
  714. } else if (s->type >= IVTV_DEC_STREAM_TYPE_MPG) {
  715. struct ivtv_stream *s_vout = &itv->streams[IVTV_DEC_STREAM_TYPE_VOUT];
  716. ivtv_stop_decoding(id, VIDEO_CMD_STOP_TO_BLACK | VIDEO_CMD_STOP_IMMEDIATELY, 0);
  717. /* If all output streams are closed, and if the user doesn't have
  718. IVTV_DEC_STREAM_TYPE_VOUT open, then disable VBI on TV-out. */
  719. if (itv->output_mode == OUT_NONE && !test_bit(IVTV_F_S_APPL_IO, &s_vout->s_flags)) {
  720. /* disable VBI on TV-out */
  721. ivtv_disable_vbi(itv);
  722. }
  723. } else {
  724. ivtv_stop_capture(id, 0);
  725. }
  726. kfree(id);
  727. mutex_unlock(&itv->serialize_lock);
  728. return 0;
  729. }
  730. static int ivtv_serialized_open(struct ivtv_stream *s, struct file *filp)
  731. {
  732. struct ivtv *itv = s->itv;
  733. struct ivtv_open_id *item;
  734. IVTV_DEBUG_FILE("open %s\n", s->name);
  735. if (s->type == IVTV_DEC_STREAM_TYPE_MPG &&
  736. test_bit(IVTV_F_S_CLAIMED, &itv->streams[IVTV_DEC_STREAM_TYPE_YUV].s_flags))
  737. return -EBUSY;
  738. if (s->type == IVTV_DEC_STREAM_TYPE_YUV &&
  739. test_bit(IVTV_F_S_CLAIMED, &itv->streams[IVTV_DEC_STREAM_TYPE_MPG].s_flags))
  740. return -EBUSY;
  741. if (s->type == IVTV_DEC_STREAM_TYPE_YUV) {
  742. if (read_reg(0x82c) == 0) {
  743. IVTV_ERR("Tried to open YUV output device but need to send data to mpeg decoder before it can be used\n");
  744. /* return -ENODEV; */
  745. }
  746. ivtv_udma_alloc(itv);
  747. }
  748. /* Allocate memory */
  749. item = kmalloc(sizeof(struct ivtv_open_id), GFP_KERNEL);
  750. if (NULL == item) {
  751. IVTV_DEBUG_WARN("nomem on v4l2 open\n");
  752. return -ENOMEM;
  753. }
  754. item->itv = itv;
  755. item->type = s->type;
  756. v4l2_prio_open(&itv->prio, &item->prio);
  757. item->open_id = itv->open_id++;
  758. filp->private_data = item;
  759. if (item->type == IVTV_ENC_STREAM_TYPE_RAD) {
  760. /* Try to claim this stream */
  761. if (ivtv_claim_stream(item, item->type)) {
  762. /* No, it's already in use */
  763. kfree(item);
  764. return -EBUSY;
  765. }
  766. if (!test_bit(IVTV_F_I_RADIO_USER, &itv->i_flags)) {
  767. if (atomic_read(&itv->capturing) > 0) {
  768. /* switching to radio while capture is
  769. in progress is not polite */
  770. kfree(item);
  771. return -EBUSY;
  772. }
  773. }
  774. /* Mark that the radio is being used. */
  775. set_bit(IVTV_F_I_RADIO_USER, &itv->i_flags);
  776. /* We have the radio */
  777. ivtv_mute(itv);
  778. /* Switch tuner to radio */
  779. ivtv_call_i2c_clients(itv, AUDC_SET_RADIO, NULL);
  780. /* Select the correct audio input (i.e. radio tuner) */
  781. ivtv_audio_set_io(itv);
  782. if (itv->hw_flags & IVTV_HW_SAA711X)
  783. {
  784. struct v4l2_crystal_freq crystal_freq;
  785. crystal_freq.freq = SAA7115_FREQ_32_11_MHZ;
  786. crystal_freq.flags = SAA7115_FREQ_FL_APLL;
  787. ivtv_saa7115(itv, VIDIOC_INT_S_CRYSTAL_FREQ, &crystal_freq);
  788. }
  789. /* Done! Unmute and continue. */
  790. ivtv_unmute(itv);
  791. }
  792. /* YUV or MPG Decoding Mode? */
  793. if (s->type == IVTV_DEC_STREAM_TYPE_MPG)
  794. clear_bit(IVTV_F_I_DEC_YUV, &itv->i_flags);
  795. else if (s->type == IVTV_DEC_STREAM_TYPE_YUV)
  796. set_bit(IVTV_F_I_DEC_YUV, &itv->i_flags);
  797. return 0;
  798. }
  799. int ivtv_v4l2_open(struct inode *inode, struct file *filp)
  800. {
  801. int res, x, y = 0;
  802. struct ivtv *itv = NULL;
  803. struct ivtv_stream *s = NULL;
  804. int minor = iminor(inode);
  805. /* Find which card this open was on */
  806. spin_lock(&ivtv_cards_lock);
  807. for (x = 0; itv == NULL && x < ivtv_cards_active; x++) {
  808. /* find out which stream this open was on */
  809. for (y = 0; y < IVTV_MAX_STREAMS; y++) {
  810. s = &ivtv_cards[x]->streams[y];
  811. if (s->v4l2dev && s->v4l2dev->minor == minor) {
  812. itv = ivtv_cards[x];
  813. break;
  814. }
  815. }
  816. }
  817. spin_unlock(&ivtv_cards_lock);
  818. if (itv == NULL) {
  819. /* Couldn't find a device registered
  820. on that minor, shouldn't happen! */
  821. IVTV_WARN("No ivtv device found on minor %d\n", minor);
  822. return -ENXIO;
  823. }
  824. mutex_lock(&itv->serialize_lock);
  825. if (ivtv_init_on_first_open(itv)) {
  826. IVTV_ERR("Failed to initialize on minor %d\n", minor);
  827. mutex_unlock(&itv->serialize_lock);
  828. return -ENXIO;
  829. }
  830. res = ivtv_serialized_open(s, filp);
  831. mutex_unlock(&itv->serialize_lock);
  832. return res;
  833. }
  834. void ivtv_mute(struct ivtv *itv)
  835. {
  836. if (atomic_read(&itv->capturing))
  837. ivtv_vapi(itv, CX2341X_ENC_MUTE_AUDIO, 1, 1);
  838. IVTV_DEBUG_INFO("Mute\n");
  839. }
  840. void ivtv_unmute(struct ivtv *itv)
  841. {
  842. if (atomic_read(&itv->capturing)) {
  843. ivtv_msleep_timeout(100, 0);
  844. ivtv_vapi(itv, CX2341X_ENC_MISC, 1, 12);
  845. ivtv_vapi(itv, CX2341X_ENC_MUTE_AUDIO, 1, 0);
  846. }
  847. IVTV_DEBUG_INFO("Unmute\n");
  848. }